Advertisement
Guest User

Untitled

a guest
Jan 4th, 2017
2,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.39 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f0xx_hal.h"
  3. #include "usb_device.h"
  4.  
  5. /* USER CODE BEGIN Includes */
  6.  
  7. /* Temperature sensor calibration value address */
  8. #define TEMP110_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7C2))
  9. #define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8))
  10. #define VREFINT_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7BA))
  11. #define VDD_CALIB ((uint16_t) (330))
  12. #define VDD_APPLI ((uint16_t) (300))
  13.  
  14. /* USER CODE END Includes */
  15.  
  16. /* Private variables ---------------------------------------------------------*/
  17. ADC_HandleTypeDef hadc;
  18. DMA_HandleTypeDef hdma_adc;
  19.  
  20. /* USER CODE BEGIN PV */
  21. /* Private variables ---------------------------------------------------------*/
  22.  
  23. volatile uint8_t i = 0;
  24. volatile uint16_t adcVal[4];
  25. volatile float vdd;
  26. volatile uint32_t vrefCal;
  27. volatile float temp;
  28. volatile float dBm;
  29.  
  30. /* USER CODE END PV */
  31.  
  32. /* Private function prototypes -----------------------------------------------*/
  33. void SystemClock_Config(void);
  34. static void MX_GPIO_Init(void);
  35. static void MX_DMA_Init(void);
  36. static void MX_ADC_Init(void);
  37.  
  38. /* USER CODE BEGIN PFP */
  39. /* Private function prototypes -----------------------------------------------*/
  40.  
  41. /* USER CODE END PFP */
  42.  
  43. /* USER CODE BEGIN 0 */
  44.  
  45. /* USER CODE END 0 */
  46.  
  47. int main(void)
  48. {
  49.  
  50.   /* USER CODE BEGIN 1 */
  51.  
  52.  
  53.   /* USER CODE END 1 */
  54.  
  55.   /* MCU Configuration----------------------------------------------------------*/
  56.  
  57.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  58.   HAL_Init();
  59.  
  60.   /* Configure the system clock */
  61.   SystemClock_Config();
  62.  
  63.   /* Initialize all configured peripherals */
  64.   MX_GPIO_Init();
  65.   MX_DMA_Init();
  66.   MX_ADC_Init();
  67.   MX_USB_DEVICE_Init();
  68.  
  69.   /* USER CODE BEGIN 2 */
  70.  
  71.   HAL_ADC_Start_IT(&hadc);
  72.  
  73.   /* USER CODE END 2 */
  74.  
  75.   /* Infinite loop */
  76.   /* USER CODE BEGIN WHILE */
  77.   while (1)
  78.   {
  79.   /* USER CODE END WHILE */
  80.  
  81.   /* USER CODE BEGIN 3 */
  82.       HAL_Delay(100);
  83.       HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
  84.       updateVdd();
  85.       updateTemp();
  86.   }
  87.   /* USER CODE END 3 */
  88.  
  89. }
  90.  
  91. void updateVdd(void) {
  92.     vrefCal = *VREFINT_CAL_ADDR;
  93.     vdd = 3.3 * (float)(*VREFINT_CAL_ADDR) / adcVal[2];
  94. }
  95.  
  96. void updateTemp(void) {
  97.     volatile float temperature; /* will contain the temperature in degree Celsius */
  98.     temperature = (((float) adcVal[1] * vdd / 3300) - (float) *TEMP30_CAL_ADDR );
  99.     temperature = temperature * (float)(110 - 30);
  100.     temperature = temperature / (float)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR);
  101.     temp = temperature + 30;
  102. }
  103.  
  104. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
  105.     if (__HAL_ADC_GET_FLAG(hadc,ADC_FLAG_EOC)) {
  106.         adcVal[i] = HAL_ADC_GetValue(hadc);
  107.         i++;
  108.     }
  109.     if (__HAL_ADC_GET_FLAG(hadc,ADC_FLAG_EOS)) {
  110.         i = 0;
  111.     }
  112. }
  113.  
  114. /** System Clock Configuration
  115. */
  116. void SystemClock_Config(void)
  117. {
  118.  
  119.   RCC_OscInitTypeDef RCC_OscInitStruct;
  120.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  121.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  122.  
  123.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI14|RCC_OSCILLATORTYPE_HSI48;
  124.   RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  125.   RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  126.   RCC_OscInitStruct.HSI14CalibrationValue = 16;
  127.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  128.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  129.  
  130.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  131.                               |RCC_CLOCKTYPE_PCLK1;
  132.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
  133.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  134.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  135.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
  136.  
  137.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  138.   PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
  139.   HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  140.  
  141.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  142.  
  143.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  144.  
  145.   /* SysTick_IRQn interrupt configuration */
  146.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  147. }
  148.  
  149. /* ADC init function */
  150. void MX_ADC_Init(void)
  151. {
  152.  
  153.   ADC_ChannelConfTypeDef sConfig;
  154.  
  155.     /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  156.     */
  157.   hadc.Instance = ADC1;
  158.   hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  159.   hadc.Init.Resolution = ADC_RESOLUTION_12B;
  160.   hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  161.   hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  162.   hadc.Init.EOCSelection = ADC_EOC_SINGLE_SEQ_CONV;
  163.   hadc.Init.LowPowerAutoWait = DISABLE;
  164.   hadc.Init.LowPowerAutoPowerOff = DISABLE;
  165.   hadc.Init.ContinuousConvMode = ENABLE;
  166.   hadc.Init.DiscontinuousConvMode = DISABLE;
  167.   hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  168.   hadc.Init.DMAContinuousRequests = DISABLE;
  169.   hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  170.   HAL_ADC_Init(&hadc);
  171.  
  172.   //ADC_TempSensorVrefintCmd(ENABLE);
  173.  
  174.     /**Configure for the selected ADC regular channel to be converted.
  175.     */
  176.   sConfig.Channel = ADC_CHANNEL_3;
  177.   sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  178.   sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
  179.   HAL_ADC_ConfigChannel(&hadc, &sConfig);
  180.  
  181.     /**Configure for the selected ADC regular channel to be converted.
  182.     */
  183.   sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  184.   HAL_ADC_ConfigChannel(&hadc, &sConfig);
  185.  
  186.     /**Configure for the selected ADC regular channel to be converted.
  187.     */
  188.   sConfig.Channel = ADC_CHANNEL_VREFINT;
  189.   HAL_ADC_ConfigChannel(&hadc, &sConfig);
  190.  
  191.     /**Configure for the selected ADC regular channel to be converted.
  192.     */
  193.   sConfig.Channel = ADC_CHANNEL_VBAT;
  194.   HAL_ADC_ConfigChannel(&hadc, &sConfig);
  195.  
  196. }
  197.  
  198. /**
  199.   * Enable DMA controller clock
  200.   */
  201. void MX_DMA_Init(void)
  202. {
  203.   /* DMA controller clock enable */
  204.   __HAL_RCC_DMA1_CLK_ENABLE();
  205.  
  206.   /* DMA interrupt init */
  207.   /* DMA1_Channel1_IRQn interrupt configuration */
  208.   HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  209.   HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  210.  
  211. }
  212.  
  213. /** Configure pins as
  214.         * Analog
  215.         * Input
  216.         * Output
  217.         * EVENT_OUT
  218.         * EXTI
  219. */
  220. void MX_GPIO_Init(void)
  221. {
  222.  
  223.   GPIO_InitTypeDef GPIO_InitStruct;
  224.  
  225.   /* GPIO Ports Clock Enable */
  226.   __HAL_RCC_GPIOB_CLK_ENABLE();
  227.   __HAL_RCC_GPIOF_CLK_ENABLE();
  228.   __HAL_RCC_GPIOA_CLK_ENABLE();
  229.  
  230.   /*Configure GPIO pin : BOOT_Pin */
  231.   GPIO_InitStruct.Pin = BOOT_Pin;
  232.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  233.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  234.   HAL_GPIO_Init(BOOT_GPIO_Port, &GPIO_InitStruct);
  235.  
  236.   /*Configure GPIO pins : W1_Pin W2_Pin */
  237.   GPIO_InitStruct.Pin = W1_Pin|W2_Pin;
  238.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  239.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  240.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  241.  
  242.   /*Configure GPIO pins : W3_Pin W4_Pin */
  243.   GPIO_InitStruct.Pin = W3_Pin|W4_Pin;
  244.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  245.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  246.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  247.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  248.  
  249.   /*Configure GPIO pin : LED_Pin */
  250.   GPIO_InitStruct.Pin = LED_Pin;
  251.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  252.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  253.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  254.   HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
  255.  
  256.   /*Configure GPIO pins : C1_Pin C2_Pin */
  257.   GPIO_InitStruct.Pin = C1_Pin|C2_Pin;
  258.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  259.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  260.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  261.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  262.  
  263.   /*Configure GPIO pin Output Level */
  264.   HAL_GPIO_WritePin(GPIOA, W3_Pin|W4_Pin, GPIO_PIN_RESET);
  265.  
  266.   /*Configure GPIO pin Output Level */
  267.   HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
  268.  
  269.   /*Configure GPIO pin Output Level */
  270.   HAL_GPIO_WritePin(GPIOB, C1_Pin|C2_Pin, GPIO_PIN_RESET);
  271.  
  272. }
  273.  
  274. /* USER CODE BEGIN 4 */
  275.  
  276. /* USER CODE END 4 */
  277.  
  278. #ifdef USE_FULL_ASSERT
  279.  
  280. /**
  281.    * @brief Reports the name of the source file and the source line number
  282.    * where the assert_param error has occurred.
  283.    * @param file: pointer to the source file name
  284.    * @param line: assert_param error line source number
  285.    * @retval None
  286.    */
  287. void assert_failed(uint8_t* file, uint32_t line)
  288. {
  289.   /* USER CODE BEGIN 6 */
  290.   /* User can add his own implementation to report the file name and line number,
  291.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  292.   /* USER CODE END 6 */
  293.  
  294. }
  295.  
  296. #endif
  297.  
  298. /**
  299.   * @}
  300.   */
  301.  
  302. /**
  303.   * @}
  304. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement