Facebook
From Botched Bee, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 256
  1.  
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   ** This notice applies to any and all portions of this file
  8.   * that are not between comment pairs USER CODE BEGIN and
  9.   * USER CODE END. Other portions of this file, whether
  10.   * inserted by the user or by software development tools
  11.   * are owned by their respective copyright owners.
  12.   *
  13.   * COPYRIGHT(c) 2019 STMicroelectronics
  14.   *
  15.   * Redistribution and use in source and binary forms, with or without modification,
  16.   * are permitted provided that the following conditions are met:
  17.   *   1. Redistributions of source code must retain the above copyright notice,
  18.   *      this list of conditions and the following disclaimer.
  19.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  20.   *      this list of conditions and the following disclaimer in the documentation
  21.   *      and/or other materials provided with the distribution.
  22.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  23.   *      may be used to endorse or promote products derived from this software
  24.   *      without specific prior written permission.
  25.   *
  26.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.   *
  37.   ******************************************************************************
  38.   */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41. #include "stm32f3xx_hal.h"
  42.  
  43. /* USER CODE BEGIN Includes */
  44.  
  45. /* USER CODE END Includes */
  46.  
  47. /* Private variables ---------------------------------------------------------*/
  48. ADC_HandleTypeDef hadc3;
  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. static void MX_GPIO_Init(void);
  58. static void MX_ADC3_Init(void);
  59.  
  60. /* USER CODE BEGIN PFP */
  61. /* Private function prototypes -----------------------------------------------*/
  62.  
  63. /* USER CODE END PFP */
  64.  
  65. /* USER CODE BEGIN 0 */
  66.  
  67. /* USER CODE END 0 */
  68.  
  69. /**
  70.   * @brief  The application entry point.
  71.   *
  72.   * @retval None
  73.   */
  74. int main(void)
  75. {
  76.   /* USER CODE BEGIN 1 */
  77.  
  78.   /* USER CODE END 1 */
  79.  
  80.   /* MCU Configuration----------------------------------------------------------*/
  81.  
  82.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83.   HAL_Init();
  84.  
  85.   /* USER CODE BEGIN Init */
  86.  
  87.   /* USER CODE END Init */
  88.  
  89.   /* Configure the system clock */
  90.   SystemClock_Config();
  91.  
  92.   /* USER CODE BEGIN SysInit */
  93.  
  94.   /* USER CODE END SysInit */
  95.  
  96.   /* Initialize all configured peripherals */
  97.   MX_GPIO_Init();
  98.   MX_ADC3_Init();
  99.   /* USER CODE BEGIN 2 */
  100.  
  101.   /* USER CODE END 2 */
  102.  
  103.   /* Infinite loop */
  104.   /* USER CODE BEGIN WHILE */
  105.   while (1)
  106.   {
  107.                 HAL_ADC_Start(&hadc3);
  108.                 for(int i=0;i<3;i++){
  109.                 if(HAL_ADC_PollForConversion(&hadc3,200)==HAL_OK){
  110.                 data[i]=HAL_ADC_GetValue(&hadc3);
  111.                 }
  112.   /* USER CODE END WHILE */
  113.  
  114.   /* USER CODE BEGIN 3 */
  115.  
  116.   }
  117.   /* USER CODE END 3 */
  118.  
  119. }
  120.  
  121. /**
  122.   * @brief System Clock Configuration
  123.   * @retval None
  124.   */
  125. void SystemClock_Config(void)
  126. {
  127.  
  128.   RCC_OscInitTypeDef RCC_OscInitStruct;
  129.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  130.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  131.  
  132.     /**Initializes the CPU, AHB and APB busses clocks
  133.     */
  134.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  135.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  136.   RCC_OscInitStruct.HSICalibrationValue = 16;
  137.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  138.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  139.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  140.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  141.   {
  142.     _Error_Handler(__FILE__, __LINE__);
  143.   }
  144.  
  145.     /**Initializes the CPU, AHB and APB busses clocks
  146.     */
  147.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  148.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  149.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  150.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  151.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  152.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  153.  
  154.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  155.   {
  156.     _Error_Handler(__FILE__, __LINE__);
  157.   }
  158.  
  159.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC34;
  160.   PeriphClkInit.Adc34ClockSelection = RCC_ADC34PLLCLK_DIV1;
  161.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  162.   {
  163.     _Error_Handler(__FILE__, __LINE__);
  164.   }
  165.  
  166.     /**Configure the Systick interrupt time
  167.     */
  168.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  169.  
  170.     /**Configure the Systick
  171.     */
  172.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  173.  
  174.   /* SysTick_IRQn interrupt configuration */
  175.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  176. }
  177.  
  178. /* ADC3 init function */
  179. static void MX_ADC3_Init(void)
  180. {
  181.  
  182.   ADC_MultiModeTypeDef multimode;
  183.   ADC_ChannelConfTypeDef sConfig;
  184.  
  185.     /**Common config
  186.     */
  187.   hadc3.Instance = ADC3;
  188.   hadc3.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  189.   hadc3.Init.Resolution = ADC_RESOLUTION_12B;
  190.   hadc3.Init.ScanConvMode = ADC_SCAN_DISABLE;
  191.   hadc3.Init.ContinuousConvMode = ENABLE;
  192.   hadc3.Init.DiscontinuousConvMode = DISABLE;
  193.   hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  194.   hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  195.   hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  196.   hadc3.Init.NbrOfConversion = 1;
  197.   hadc3.Init.DMAContinuousRequests = DISABLE;
  198.   hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  199.   hadc3.Init.LowPowerAutoWait = DISABLE;
  200.   hadc3.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  201.   if (HAL_ADC_Init(&hadc3) != HAL_OK)
  202.   {
  203.     _Error_Handler(__FILE__, __LINE__);
  204.   }
  205.  
  206.     /**Configure the ADC multi-mode
  207.     */
  208.   multimode.Mode = ADC_MODE_INDEPENDENT;
  209.   if (HAL_ADCEx_MultiModeConfigChannel(&hadc3, &multimode) != HAL_OK)
  210.   {
  211.     _Error_Handler(__FILE__, __LINE__);
  212.   }
  213.  
  214.     /**Configure Regular Channel
  215.     */
  216.   sConfig.Channel = ADC_CHANNEL_5;
  217.   sConfig.Rank = ADC_REGULAR_RANK_1;
  218.   sConfig.SingleDiff = ADC_SINGLE_ENDED;
  219.   sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  220.   sConfig.OffsetNumber = ADC_OFFSET_NONE;
  221.   sConfig.Offset = 0;
  222.   if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  223.   {
  224.     _Error_Handler(__FILE__, __LINE__);
  225.   }
  226.  
  227. }
  228.  
  229. /** Configure pins as
  230.         * Analog
  231.         * Input
  232.         * Output
  233.         * EVENT_OUT
  234.         * EXTI
  235.      PA5   ------> SPI1_SCK
  236.      PA6   ------> SPI1_MISO
  237.      PA7   ------> SPI1_MOSI
  238.      PA11   ------> USB_DM
  239.      PA12   ------> USB_DP
  240.      PB6   ------> I2C1_SCL
  241.      PB7   ------> I2C1_SDA
  242. */
  243. static void MX_GPIO_Init(void)
  244. {
  245.  
  246.   GPIO_InitTypeDef GPIO_InitStruct;
  247.  
  248.   /* GPIO Ports Clock Enable */
  249.   __HAL_RCC_GPIOE_CLK_ENABLE();
  250.   __HAL_RCC_GPIOC_CLK_ENABLE();
  251.   __HAL_RCC_GPIOF_CLK_ENABLE();
  252.   __HAL_RCC_GPIOA_CLK_ENABLE();
  253.   __HAL_RCC_GPIOB_CLK_ENABLE();
  254.   __HAL_RCC_GPIOD_CLK_ENABLE();
  255.  
  256.   /*Configure GPIO pin Output Level */
  257.   HAL_GPIO_WritePin(GPIOE, CS_I2C_SPI_Pin|LD4_Pin|LD3_Pin|LD5_Pin
  258.                           |LD7_Pin|LD9_Pin|LD10_Pin|LD8_Pin
  259.                           |LD6_Pin, GPIO_PIN_RESET);
  260.  
  261.   /*Configure GPIO pins : DRDY_Pin MEMS_INT3_Pin MEMS_INT4_Pin MEMS_INT1_Pin
  262.                            MEMS_INT2_Pin */
  263.   GPIO_InitStruct.Pin = DRDY_Pin|MEMS_INT3_Pin|MEMS_INT4_Pin|MEMS_INT1_Pin
  264.                           |MEMS_INT2_Pin;
  265.   GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  266.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  267.   HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  268.  
  269.   /*Configure GPIO pins : CS_I2C_SPI_Pin LD4_Pin LD3_Pin LD5_Pin
  270.                            LD7_Pin LD9_Pin LD10_Pin LD8_Pin
  271.                            LD6_Pin */
  272.   GPIO_InitStruct.Pin = CS_I2C_SPI_Pin|LD4_Pin|LD3_Pin|LD5_Pin
  273.                           |LD7_Pin|LD9_Pin|LD10_Pin|LD8_Pin
  274.                           |LD6_Pin;
  275.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  276.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  277.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  278.   HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  279.  
  280.   /*Configure GPIO pin : B1_Pin */
  281.   GPIO_InitStruct.Pin = B1_Pin;
  282.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  283.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  284.   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  285.  
  286.   /*Configure GPIO pins : SPI1_SCK_Pin SPI1_MISO_Pin SPI1_MISOA7_Pin */
  287.   GPIO_InitStruct.Pin = SPI1_SCK_Pin|SPI1_MISO_Pin|SPI1_MISOA7_Pin;
  288.   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  289.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  290.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  291.   GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  292.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  293.  
  294.   /*Configure GPIO pins : DM_Pin DP_Pin */
  295.   GPIO_InitStruct.Pin = DM_Pin|DP_Pin;
  296.   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  297.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  298.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  299.   GPIO_InitStruct.Alternate = GPIO_AF14_USB;
  300.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  301.  
  302.   /*Configure GPIO pins : I2C1_SCL_Pin I2C1_SDA_Pin */
  303.   GPIO_InitStruct.Pin = I2C1_SCL_Pin|I2C1_SDA_Pin;
  304.   GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  305.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  306.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  307.   GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  308.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  309.  
  310. }
  311.  
  312. /* USER CODE BEGIN 4 */
  313.  
  314. /* USER CODE END 4 */
  315.  
  316. /**
  317.   * @brief  This function is executed in case of error occurrence.
  318.   * @param  file: The file name as string.
  319.   * @param  line: The line in file as a number.
  320.   * @retval None
  321.   */
  322. void _Error_Handler(char *file, int line)
  323. {
  324.   /* USER CODE BEGIN Error_Handler_Debug */
  325.   /* User can add his own implementation to report the HAL error return state */
  326.   while(1)
  327.   {
  328.   }
  329.   /* USER CODE END Error_Handler_Debug */
  330. }
  331.  
  332. #ifdef  USE_FULL_ASSERT
  333. /**
  334.   * @brief  Reports the name of the source file and the source line number
  335.   *         where the assert_param error has occurred.
  336.   * @param  file: pointer to the source file name
  337.   * @param  line: assert_param error line source number
  338.   * @retval None
  339.   */
  340. void assert_failed(uint8_t* file, uint32_t line)
  341. {
  342.   /* USER CODE BEGIN 6 */
  343.   /* User can add his own implementation to report the file name and line number,
  344.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  345.   /* USER CODE END 6 */
  346. }
  347. #endif /* USE_FULL_ASSERT */
  348.  
  349. /**
  350.   * @}
  351.   */
  352.  
  353. /**
  354.   * @}
  355.   */
  356.  
  357. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  358.