main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2022 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. /* Private includes ----------------------------------------------------------*/
  22. /* USER CODE BEGIN Includes */
  23. #include<stdio.h>
  24. #include "oneWire.h"
  25. #include "oneWireDriver.h"
  26. /* USER CODE END Includes */
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29. /* USER CODE END PTD */
  30. /* Private define ------------------------------------------------------------*/
  31. /* USER CODE BEGIN PD */
  32. #define MAX_NO_OF_OW 5
  33. #define TIME_BETWEEN_READS 10000
  34. /* USER CODE END PD */
  35. /* Private macro -------------------------------------------------------------*/
  36. /* USER CODE BEGIN PM */
  37. /* USER CODE END PM */
  38. /* Private variables ---------------------------------------------------------*/
  39. ADC_HandleTypeDef hadc1;
  40. TIM_HandleTypeDef htim1;
  41. UART_HandleTypeDef huart2;
  42. /* USER CODE BEGIN PV */
  43. extern uint32_t adcCounter;
  44. extern uint16_t maxWaveDiff; // Stores the latest measured ADC Wave data (MAX-MIN)
  45. /* USER CODE END PV */
  46. /* Private function prototypes -----------------------------------------------*/
  47. void SystemClock_Config(void);
  48. static void MX_GPIO_Init(void);
  49. static void MX_USART2_UART_Init(void);
  50. static void MX_ADC1_Init(void);
  51. static void MX_TIM1_Init(void);
  52. static void MX_NVIC_Init(void);
  53. /* USER CODE BEGIN PFP */
  54. void handleCmdRx();
  55. /* USER CODE END PFP */
  56. /* Private user code ---------------------------------------------------------*/
  57. /* USER CODE BEGIN 0 */
  58. uint16_t getADCDiff() {
  59. volatile uint32_t firstCnt;
  60. int32_t timeoutCnt = 20;
  61. firstCnt = adcCounter;
  62. HAL_TIM_Base_Stop(&htim1); // Stop TIM1-Counter to maybe stop disturbances in measurments
  63. ADC1->CR1 = ADC_IT_EOC; // Enable interrupt
  64. while( timeoutCnt > 0 && adcCounter < (firstCnt+3) ) {
  65. timeoutCnt--;
  66. HAL_Delay(10); // Three measurements takes around 140mS. 20*10 = 200mS timeout
  67. }
  68. ADC1->CR1 = 0; // Disable interrupts
  69. HAL_TIM_Base_Start(&htim1); // Start TIM1 again
  70. if( timeoutCnt == 0 ) return 0;
  71. return maxWaveDiff;
  72. }
  73. /**
  74. * This function is a copy of the HAL_Delay() but with the extra add-on
  75. * to do some UART Rx work while waiting
  76. */
  77. void waitForNextPeriod(uint32_t Delay)
  78. {
  79. uint32_t tickstart = HAL_GetTick();
  80. uint32_t wait = Delay;
  81. /* Add a freq to guarantee minimum wait */
  82. if (wait < HAL_MAX_DELAY) wait += (uint32_t)(uwTickFreq);
  83. while ((HAL_GetTick() - tickstart) < wait)
  84. {
  85. handleCmdRx();
  86. }
  87. }
  88. /* USER CODE END 0 */
  89. /**
  90. * @brief The application entry point.
  91. * @retval int
  92. */
  93. int main(void)
  94. {
  95. /* USER CODE BEGIN 1 */
  96. /* USER CODE END 1 */
  97. /* MCU Configuration--------------------------------------------------------*/
  98. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  99. HAL_Init();
  100. /* USER CODE BEGIN Init */
  101. /* USER CODE END Init */
  102. /* Configure the system clock */
  103. SystemClock_Config();
  104. /* USER CODE BEGIN SysInit */
  105. /* USER CODE END SysInit */
  106. /* Initialize all configured peripherals */
  107. MX_GPIO_Init();
  108. MX_USART2_UART_Init();
  109. MX_ADC1_Init();
  110. MX_TIM1_Init();
  111. /* Initialize interrupts */
  112. MX_NVIC_Init();
  113. /* USER CODE BEGIN 2 */
  114. // Start TIM1 (used for uS delays)
  115. HAL_TIM_Base_Start(&htim1);
  116. // Enable USART1 Interrupts
  117. USART2->CR1 |= USART_CR1_RXNEIE | USART_CR1_TXEIE;
  118. // Enable OneWire sensors
  119. init_gpio_pin();
  120. // Start continous ADC-conversion
  121. HAL_Delay(10);
  122. ADC1->SR = 0;
  123. ADC1->CR2 = ADC_CR2_ADON | ADC_CR2_CONT;
  124. //ADC1->CR1 = ADC_IT_EOC; // Only start IT_EOC when actually reading data
  125. ADC1->CR2 |= ADC_CR2_ADON;
  126. /* USER CODE END 2 */
  127. /* Infinite loop */
  128. /* USER CODE BEGIN WHILE */
  129. // ------------- Search for OneWireSensors
  130. //Found device: 0x080C25372F3D253F <-- Crap
  131. //Found device: 0x28FF22DA551603C3 <-- This is the probe
  132. reset_oneWireSearch();
  133. uint64_t adr[MAX_NO_OF_OW] = {0,0,0,0,0};
  134. uint8_t adrCnt=0;
  135. while( oneWireSearch(adr+adrCnt) ) {
  136. //printf("Found device: 0x");
  137. //for( uint8_t i = 8; i>0; i--) printf("%02X",(uint8_t)(adr[adrCnt] >> ((i-1)*8))&0xFF);
  138. //printf("\n");
  139. adrCnt++;
  140. }
  141. //printf("\n");
  142. reset_oneWireSearch();
  143. while (1)
  144. {
  145. /* USER CODE END WHILE */
  146. /* USER CODE BEGIN 3 */
  147. uint32_t startTime = HAL_GetTick();
  148. uint16_t adcDiff = getADCDiff();
  149. //printf("Cnt:%lu Diff:%u mV:%u\n",adcCounter, adcDiff, (adcDiff*805)/1000);
  150. printf("{VPP,0,%u}\n",adcDiff);
  151. for(uint8_t sId=0;sId<MAX_NO_OF_OW && adr[sId]>0 ;sId++) {
  152. float temp = readTemperature(adr[sId]);
  153. printf("{OWT,");
  154. for( uint8_t i = 8; i>0; i--) printf("%02X",(uint8_t)(adr[sId] >> ((i-1)*8))&0xFF);
  155. printf(",%.2f}\n",temp);
  156. }
  157. waitForNextPeriod( TIME_BETWEEN_READS-(HAL_GetTick()-startTime)-1 );
  158. }
  159. /* USER CODE END 3 */
  160. }
  161. /**
  162. * @brief System Clock Configuration
  163. * @retval None
  164. */
  165. void SystemClock_Config(void)
  166. {
  167. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  168. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  169. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  170. /** Initializes the RCC Oscillators according to the specified parameters
  171. * in the RCC_OscInitTypeDef structure.
  172. */
  173. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  174. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  175. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  176. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  177. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  178. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  179. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL7;
  180. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  181. {
  182. Error_Handler();
  183. }
  184. /** Initializes the CPU, AHB and APB buses clocks
  185. */
  186. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  187. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  188. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  189. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  190. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  191. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  192. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  193. {
  194. Error_Handler();
  195. }
  196. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  197. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV4;
  198. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  199. {
  200. Error_Handler();
  201. }
  202. }
  203. /**
  204. * @brief NVIC Configuration.
  205. * @retval None
  206. */
  207. static void MX_NVIC_Init(void)
  208. {
  209. /* ADC1_2_IRQn interrupt configuration */
  210. HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
  211. HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
  212. /* USART2_IRQn interrupt configuration */
  213. HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  214. HAL_NVIC_EnableIRQ(USART2_IRQn);
  215. }
  216. /**
  217. * @brief ADC1 Initialization Function
  218. * @param None
  219. * @retval None
  220. */
  221. static void MX_ADC1_Init(void)
  222. {
  223. /* USER CODE BEGIN ADC1_Init 0 */
  224. /* USER CODE END ADC1_Init 0 */
  225. ADC_ChannelConfTypeDef sConfig = {0};
  226. /* USER CODE BEGIN ADC1_Init 1 */
  227. /* USER CODE END ADC1_Init 1 */
  228. /** Common config
  229. */
  230. hadc1.Instance = ADC1;
  231. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  232. hadc1.Init.ContinuousConvMode = ENABLE;
  233. hadc1.Init.DiscontinuousConvMode = DISABLE;
  234. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  235. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  236. hadc1.Init.NbrOfConversion = 1;
  237. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  238. {
  239. Error_Handler();
  240. }
  241. /** Configure Regular Channel
  242. */
  243. sConfig.Channel = ADC_CHANNEL_5;
  244. sConfig.Rank = ADC_REGULAR_RANK_1;
  245. sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  246. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  247. {
  248. Error_Handler();
  249. }
  250. /* USER CODE BEGIN ADC1_Init 2 */
  251. HAL_ADCEx_Calibration_Start(&hadc1);
  252. // Set sampling frequency
  253. sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
  254. HAL_ADC_ConfigChannel(&hadc1, &sConfig);
  255. /* USER CODE END ADC1_Init 2 */
  256. }
  257. /**
  258. * @brief TIM1 Initialization Function
  259. * @param None
  260. * @retval None
  261. */
  262. static void MX_TIM1_Init(void)
  263. {
  264. /* USER CODE BEGIN TIM1_Init 0 */
  265. /* USER CODE END TIM1_Init 0 */
  266. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  267. TIM_MasterConfigTypeDef sMasterConfig = {0};
  268. TIM_OC_InitTypeDef sConfigOC = {0};
  269. TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
  270. /* USER CODE BEGIN TIM1_Init 1 */
  271. /* USER CODE END TIM1_Init 1 */
  272. htim1.Instance = TIM1;
  273. htim1.Init.Prescaler = 55;
  274. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  275. htim1.Init.Period = 0xfffe;
  276. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  277. htim1.Init.RepetitionCounter = 0;
  278. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  279. if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  280. {
  281. Error_Handler();
  282. }
  283. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  284. if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  285. {
  286. Error_Handler();
  287. }
  288. if (HAL_TIM_OC_Init(&htim1) != HAL_OK)
  289. {
  290. Error_Handler();
  291. }
  292. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  293. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  294. if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  295. {
  296. Error_Handler();
  297. }
  298. sConfigOC.OCMode = TIM_OCMODE_FORCED_ACTIVE;
  299. sConfigOC.Pulse = 0;
  300. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  301. sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  302. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  303. sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  304. sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  305. if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  306. {
  307. Error_Handler();
  308. }
  309. sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  310. sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  311. sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  312. sBreakDeadTimeConfig.DeadTime = 0;
  313. sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  314. sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  315. sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  316. if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  317. {
  318. Error_Handler();
  319. }
  320. /* USER CODE BEGIN TIM1_Init 2 */
  321. /* USER CODE END TIM1_Init 2 */
  322. HAL_TIM_MspPostInit(&htim1);
  323. }
  324. /**
  325. * @brief USART2 Initialization Function
  326. * @param None
  327. * @retval None
  328. */
  329. static void MX_USART2_UART_Init(void)
  330. {
  331. /* USER CODE BEGIN USART2_Init 0 */
  332. /* USER CODE END USART2_Init 0 */
  333. /* USER CODE BEGIN USART2_Init 1 */
  334. /* USER CODE END USART2_Init 1 */
  335. huart2.Instance = USART2;
  336. huart2.Init.BaudRate = 19200;
  337. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  338. huart2.Init.StopBits = UART_STOPBITS_1;
  339. huart2.Init.Parity = UART_PARITY_NONE;
  340. huart2.Init.Mode = UART_MODE_TX_RX;
  341. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  342. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  343. if (HAL_UART_Init(&huart2) != HAL_OK)
  344. {
  345. Error_Handler();
  346. }
  347. /* USER CODE BEGIN USART2_Init 2 */
  348. /* USER CODE END USART2_Init 2 */
  349. }
  350. /**
  351. * @brief GPIO Initialization Function
  352. * @param None
  353. * @retval None
  354. */
  355. static void MX_GPIO_Init(void)
  356. {
  357. GPIO_InitTypeDef GPIO_InitStruct = {0};
  358. /* GPIO Ports Clock Enable */
  359. __HAL_RCC_GPIOC_CLK_ENABLE();
  360. __HAL_RCC_GPIOD_CLK_ENABLE();
  361. __HAL_RCC_GPIOA_CLK_ENABLE();
  362. /*Configure GPIO pin Output Level */
  363. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  364. /*Configure GPIO pin : PC13 */
  365. GPIO_InitStruct.Pin = GPIO_PIN_13;
  366. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  367. GPIO_InitStruct.Pull = GPIO_NOPULL;
  368. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  369. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  370. /*Configure GPIO pin : PC14 */
  371. GPIO_InitStruct.Pin = GPIO_PIN_14;
  372. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  373. GPIO_InitStruct.Pull = GPIO_NOPULL;
  374. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  375. }
  376. /* USER CODE BEGIN 4 */
  377. /* USER CODE END 4 */
  378. /**
  379. * @brief This function is executed in case of error occurrence.
  380. * @retval None
  381. */
  382. void Error_Handler(void)
  383. {
  384. /* USER CODE BEGIN Error_Handler_Debug */
  385. /* User can add his own implementation to report the HAL error return state */
  386. __disable_irq();
  387. while (1)
  388. {
  389. }
  390. /* USER CODE END Error_Handler_Debug */
  391. }
  392. #ifdef USE_FULL_ASSERT
  393. /**
  394. * @brief Reports the name of the source file and the source line number
  395. * where the assert_param error has occurred.
  396. * @param file: pointer to the source file name
  397. * @param line: assert_param error line source number
  398. * @retval None
  399. */
  400. void assert_failed(uint8_t *file, uint32_t line)
  401. {
  402. /* USER CODE BEGIN 6 */
  403. /* User can add his own implementation to report the file name and line number,
  404. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  405. /* USER CODE END 6 */
  406. }
  407. #endif /* USE_FULL_ASSERT */