main.c 14 KB

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