Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.14 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   *
  7.   * COPYRIGHT(c) 2017 STMicroelectronics
  8.   *
  9.   * Redistribution and use in source and binary forms, with or without modification,
  10.   * are permitted provided that the following conditions are met:
  11.   *   1. Redistributions of source code must retain the above copyright notice,
  12.   *      this list of conditions and the following disclaimer.
  13.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  14.   *      this list of conditions and the following disclaimer in the documentation
  15.   *      and/or other materials provided with the distribution.
  16.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  17.   *      may be used to endorse or promote products derived from this software
  18.   *      without specific prior written permission.
  19.   *
  20.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.   *
  31.   ******************************************************************************
  32.   */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f3xx_hal.h"
  35.  
  36. /* USER CODE BEGIN Includes */
  37. #include <string.h>
  38. /* USER CODE END Includes */
  39.  
  40. /* Private variables ---------------------------------------------------------*/
  41. I2C_HandleTypeDef hi2c1;
  42. I2C_HandleTypeDef hi2c2;
  43. DMA_HandleTypeDef hdma_i2c2_rx;
  44. DMA_HandleTypeDef hdma_i2c2_tx;
  45.  
  46. SPI_HandleTypeDef hspi1;
  47.  
  48. PCD_HandleTypeDef hpcd_USB_FS;
  49.  
  50. /* USER CODE BEGIN PV */
  51. /* Private variables ---------------------------------------------------------*/
  52.  
  53. /* USER CODE END PV */
  54.  
  55. /* Private function prototypes -----------------------------------------------*/
  56. void SystemClock_Config(void);
  57. void Error_Handler(void);
  58. static void MX_GPIO_Init(void);
  59. static void MX_DMA_Init(void);
  60. static void MX_I2C1_Init(void);
  61. static void MX_SPI1_Init(void);
  62. static void MX_USB_PCD_Init(void);
  63. static void MX_I2C2_Init(void);
  64.  
  65. /* USER CODE BEGIN PFP */
  66. /* Private function prototypes -----------------------------------------------*/
  67.  
  68. /* USER CODE END PFP */
  69.  
  70. /* USER CODE BEGIN 0 */
  71.  
  72. /* USER CODE END 0 */
  73.  
  74. int main(void)
  75. {
  76.  
  77.   /* USER CODE BEGIN 1 */
  78.  
  79.   /* USER CODE END 1 */
  80.  
  81.   /* MCU Configuration----------------------------------------------------------*/
  82.  
  83.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  84.   HAL_Init();
  85.  
  86.   /* Configure the system clock */
  87.   SystemClock_Config();
  88.  
  89.   /* Initialize all configured peripherals */
  90.   MX_GPIO_Init();
  91.   MX_DMA_Init();
  92.   MX_I2C1_Init();
  93.   MX_SPI1_Init();
  94.   MX_USB_PCD_Init();
  95.   MX_I2C2_Init();
  96.  
  97.   /* USER CODE BEGIN 2 */
  98.   uint8_t nDevAddress = 0x20 << 1;
  99.  
  100.   uint8_t pData[21];
  101.   pData[0] = 0x00;
  102.  
  103.   //Read data starting register 0x00 manually (blocking)
  104.   HAL_I2C_Master_Transmit(&hi2c2, nDevAddress, pData, 1, 10000);
  105.  
  106.   memset(pData, 0, 21);
  107.   HAL_I2C_Master_Receive(&hi2c2, nDevAddress, pData, 21, 10000);
  108.  
  109.   //perform same read using memread (blocking)
  110.   memset(pData, 0, 21);
  111.   HAL_I2C_Mem_Read(&hi2c2, nDevAddress, 0, 1, pData, 21, 10000);
  112.  
  113.   //perform memread in dma mode (non blocking)
  114.   memset(pData, 0, 21);
  115.   HAL_I2C_Mem_Read_DMA(&hi2c2, nDevAddress, 0, 1, pData, 16);
  116.  
  117.   while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY);
  118.  
  119.   int bp=1;
  120.   /* USER CODE END 2 */
  121.  
  122.   /* Infinite loop */
  123.   /* USER CODE BEGIN WHILE */
  124.   while (1)
  125.   {
  126.  
  127.   /* USER CODE END WHILE */
  128.  
  129.   /* USER CODE BEGIN 3 */
  130.  
  131.   }
  132.   /* USER CODE END 3 */
  133.  
  134. }
  135.  
  136. /** System Clock Configuration
  137. */
  138. void SystemClock_Config(void)
  139. {
  140.  
  141.   RCC_OscInitTypeDef RCC_OscInitStruct;
  142.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  143.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  144.  
  145.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
  146.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  147.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  148.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  149.   RCC_OscInitStruct.HSICalibrationValue = 16;
  150.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  151.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  152.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  153.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  154.   {
  155.     Error_Handler();
  156.   }
  157.  
  158.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  159.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  160.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  161.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  162.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  163.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  164.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  165.   {
  166.     Error_Handler();
  167.   }
  168.  
  169.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB|RCC_PERIPHCLK_I2C1
  170.                               |RCC_PERIPHCLK_I2C2;
  171.   PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  172.   PeriphClkInit.I2c2ClockSelection = RCC_I2C2CLKSOURCE_HSI;
  173.   PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL;
  174.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  175.   {
  176.     Error_Handler();
  177.   }
  178.  
  179.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  180.  
  181.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  182.  
  183.   /* SysTick_IRQn interrupt configuration */
  184.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  185. }
  186.  
  187. /* I2C1 init function */
  188. static void MX_I2C1_Init(void)
  189. {
  190.  
  191.   hi2c1.Instance = I2C1;
  192.   hi2c1.Init.Timing = 0x2000090E;
  193.   hi2c1.Init.OwnAddress1 = 0;
  194.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  195.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  196.   hi2c1.Init.OwnAddress2 = 0;
  197.   hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  198.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  199.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  200.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  201.   {
  202.     Error_Handler();
  203.   }
  204.  
  205.     /**Configure Analogue filter
  206.     */
  207.   if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  208.   {
  209.     Error_Handler();
  210.   }
  211.  
  212. }
  213.  
  214. /* I2C2 init function */
  215. static void MX_I2C2_Init(void)
  216. {
  217.  
  218.   hi2c2.Instance = I2C2;
  219.   hi2c2.Init.Timing = 0x2000090E;
  220.   hi2c2.Init.OwnAddress1 = 0;
  221.   hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  222.   hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  223.   hi2c2.Init.OwnAddress2 = 0;
  224.   hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  225.   hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  226.   hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  227.   if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  228.   {
  229.     Error_Handler();
  230.   }
  231.  
  232.     /**Configure Analogue filter
  233.     */
  234.   if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  235.   {
  236.     Error_Handler();
  237.   }
  238.  
  239. }
  240.  
  241. /* SPI1 init function */
  242. static void MX_SPI1_Init(void)
  243. {
  244.  
  245.   hspi1.Instance = SPI1;
  246.   hspi1.Init.Mode = SPI_MODE_MASTER;
  247.   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  248.   hspi1.Init.DataSize = SPI_DATASIZE_4BIT;
  249.   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  250.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  251.   hspi1.Init.NSS = SPI_NSS_SOFT;
  252.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  253.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  254.   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  255.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  256.   hspi1.Init.CRCPolynomial = 7;
  257.   hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  258.   hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  259.   if (HAL_SPI_Init(&hspi1) != HAL_OK)
  260.   {
  261.     Error_Handler();
  262.   }
  263.  
  264. }
  265.  
  266. /* USB init function */
  267. static void MX_USB_PCD_Init(void)
  268. {
  269.  
  270.   hpcd_USB_FS.Instance = USB;
  271.   hpcd_USB_FS.Init.dev_endpoints = 8;
  272.   hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
  273.   hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64;
  274.   hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  275.   hpcd_USB_FS.Init.low_power_enable = DISABLE;
  276.   hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
  277.   if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
  278.   {
  279.     Error_Handler();
  280.   }
  281.  
  282. }
  283.  
  284. /**
  285.   * Enable DMA controller clock
  286.   */
  287. static void MX_DMA_Init(void)
  288. {
  289.   /* DMA controller clock enable */
  290.   __HAL_RCC_DMA1_CLK_ENABLE();
  291.  
  292.   /* DMA interrupt init */
  293.   /* DMA1_Channel4_IRQn interrupt configuration */
  294.   HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
  295.   HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
  296.   /* DMA1_Channel5_IRQn interrupt configuration */
  297.   HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
  298.   HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
  299.   /* DMA1_Channel6_IRQn interrupt configuration */
  300.   HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0);
  301.   HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
  302.   /* DMA1_Channel7_IRQn interrupt configuration */
  303.   HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);
  304.   HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);
  305.  
  306. }
  307.  
  308. /** Configure pins as
  309.         * Analog
  310.         * Input
  311.         * Output
  312.         * EVENT_OUT
  313.         * EXTI
  314. */
  315. static void MX_GPIO_Init(void)
  316. {
  317.  
  318.   GPIO_InitTypeDef GPIO_InitStruct;
  319.  
  320.   /* GPIO Ports Clock Enable */
  321.   __HAL_RCC_GPIOE_CLK_ENABLE();
  322.   __HAL_RCC_GPIOC_CLK_ENABLE();
  323.   __HAL_RCC_GPIOF_CLK_ENABLE();
  324.   __HAL_RCC_GPIOA_CLK_ENABLE();
  325.   __HAL_RCC_GPIOB_CLK_ENABLE();
  326.  
  327.   /*Configure GPIO pins : DRDY_Pin MEMS_INT3_Pin MEMS_INT4_Pin MEMS_INT1_Pin
  328.                            MEMS_INT2_Pin */
  329.   GPIO_InitStruct.Pin = DRDY_Pin|MEMS_INT3_Pin|MEMS_INT4_Pin|MEMS_INT1_Pin
  330.                           |MEMS_INT2_Pin;
  331.   GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  332.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  333.   HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  334.  
  335.   /*Configure GPIO pins : CS_I2C_SPI_Pin LD4_Pin LD3_Pin LD5_Pin
  336.                            LD7_Pin LD9_Pin LD10_Pin LD8_Pin
  337.                            LD6_Pin */
  338.   GPIO_InitStruct.Pin = CS_I2C_SPI_Pin|LD4_Pin|LD3_Pin|LD5_Pin
  339.                           |LD7_Pin|LD9_Pin|LD10_Pin|LD8_Pin
  340.                           |LD6_Pin;
  341.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  342.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  343.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  344.   HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  345.  
  346.   /*Configure GPIO pin : B1_Pin */
  347.   GPIO_InitStruct.Pin = B1_Pin;
  348.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  349.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  350.   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  351.  
  352.   /*Configure GPIO pin Output Level */
  353.   HAL_GPIO_WritePin(GPIOE, CS_I2C_SPI_Pin|LD4_Pin|LD3_Pin|LD5_Pin
  354.                           |LD7_Pin|LD9_Pin|LD10_Pin|LD8_Pin
  355.                           |LD6_Pin, GPIO_PIN_RESET);
  356.  
  357. }
  358.  
  359. /* USER CODE BEGIN 4 */
  360.  
  361. /* USER CODE END 4 */
  362.  
  363. /**
  364.   * @brief  This function is executed in case of error occurrence.
  365.   * @param  None
  366.   * @retval None
  367.   */
  368. void Error_Handler(void)
  369. {
  370.   /* USER CODE BEGIN Error_Handler */
  371.   /* User can add his own implementation to report the HAL error return state */
  372.   while(1)
  373.   {
  374.   }
  375.   /* USER CODE END Error_Handler */
  376. }
  377.  
  378. #ifdef USE_FULL_ASSERT
  379.  
  380. /**
  381.    * @brief Reports the name of the source file and the source line number
  382.    * where the assert_param error has occurred.
  383.    * @param file: pointer to the source file name
  384.    * @param line: assert_param error line source number
  385.    * @retval None
  386.    */
  387. void assert_failed(uint8_t* file, uint32_t line)
  388. {
  389.   /* USER CODE BEGIN 6 */
  390.   /* User can add his own implementation to report the file name and line number,
  391.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  392.   /* USER CODE END 6 */
  393.  
  394. }
  395.  
  396. #endif
  397.  
  398. /**
  399.   * @}
  400.   */
  401.  
  402. /**
  403.   * @}
  404. */
  405.  
  406. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement