main.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**
  2. ******************************************************************************
  3. * @file : main.c
  4. * @brief : Main program body
  5. ******************************************************************************
  6. * This notice applies to any and all portions of this file
  7. * that are not between comment pairs USER CODE BEGIN and
  8. * USER CODE END. Other portions of this file, whether
  9. * inserted by the user or by software development tools
  10. * are owned by their respective copyright owners.
  11. *
  12. * Copyright (c) 2018 STMicroelectronics International N.V.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted, provided that the following conditions are met:
  17. *
  18. * 1. Redistribution of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * 3. Neither the name of STMicroelectronics nor the names of other
  24. * contributors to this software may be used to endorse or promote products
  25. * derived from this software without specific written permission.
  26. * 4. This software, including modifications and/or derivative works of this
  27. * software, must execute solely and exclusively on microcontroller or
  28. * microprocessor devices manufactured by or for STMicroelectronics.
  29. * 5. Redistribution and use of this software other than as permitted under
  30. * this license is void and will automatically terminate your rights under
  31. * this license.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  34. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  35. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  36. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  37. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  38. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  41. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  42. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  44. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. ******************************************************************************
  47. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "main.h"
  50. #include "stm32f4xx_hal.h"
  51. #include "adc.h"
  52. #include "dma.h"
  53. #include "fatfs.h"
  54. #include "i2c.h"
  55. #include "lwip.h"
  56. #include "sdio.h"
  57. #include "spi.h"
  58. #include "tim.h"
  59. #include "usart.h"
  60. #include "gpio.h"
  61. /* USER CODE BEGIN Includes */
  62. #include "bexus_sensors.h"
  63. #include "bexus_sysclock.h"
  64. #include "bexus_ms_exchange.h"
  65. #include "bexus_udp_client.h"
  66. /* USER CODE END Includes */
  67. /* Private variables ---------------------------------------------------------*/
  68. /* USER CODE BEGIN PV */
  69. /* Private variables ---------------------------------------------------------*/
  70. uint32_t start_main, end_main;
  71. struct Dataset *dataset_rx, *dataset_rx_old;
  72. struct Dataset rx_buf;
  73. uint32_t rx_success, crc_error;
  74. /* USER CODE END PV */
  75. /* Private function prototypes -----------------------------------------------*/
  76. void SystemClock_Config(void);
  77. /* USER CODE BEGIN PFP */
  78. /* Private function prototypes -----------------------------------------------*/
  79. /* USER CODE END PFP */
  80. /* USER CODE BEGIN 0 */
  81. /* USER CODE END 0 */
  82. /**
  83. * @brief The application entry point.
  84. *
  85. * @retval None
  86. */
  87. int main(void)
  88. {
  89. /* USER CODE BEGIN 1 */
  90. /* USER CODE END 1 */
  91. /* MCU Configuration----------------------------------------------------------*/
  92. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  93. HAL_Init();
  94. /* USER CODE BEGIN Init */
  95. /* USER CODE END Init */
  96. /* Configure the system clock */
  97. SystemClock_Config();
  98. /* USER CODE BEGIN SysInit */
  99. /* USER CODE END SysInit */
  100. /* Initialize all configured peripherals */
  101. MX_GPIO_Init();
  102. MX_DMA_Init();
  103. MX_LWIP_Init();
  104. MX_I2C1_Init();
  105. MX_I2C2_Init();
  106. MX_USART6_UART_Init();
  107. MX_USART3_UART_Init();
  108. MX_ADC3_Init();
  109. MX_SDIO_SD_Init();
  110. MX_FATFS_Init();
  111. MX_ADC1_Init();
  112. MX_SPI4_Init();
  113. MX_TIM6_Init();
  114. MX_USART2_UART_Init();
  115. /* USER CODE BEGIN 2 */
  116. Udp_Init();
  117. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_12,GPIO_PIN_RESET);
  118. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_14,GPIO_PIN_SET);
  119. HAL_Delay(12);
  120. SD_Init();
  121. Sensors_Init();
  122. Data_Exchange_Init();
  123. HAL_TIM_Base_Start_IT(&htim6);
  124. HAL_UART_Receive_DMA(&huart3, (uint8_t*)&rx_buf, sizeof(struct Dataset));
  125. /* USER CODE END 2 */
  126. /* Infinite loop */
  127. /* USER CODE BEGIN WHILE */
  128. while (1)
  129. {
  130. /* USER CODE END WHILE */
  131. /* USER CODE BEGIN 3 */
  132. start_main = millis();
  133. Sensors();
  134. Data_Exchange();
  135. Mode_Select();
  136. #ifdef BOARD_HAS_ETHERNET
  137. MX_LWIP_Process();
  138. if(millis()>60000 && !isNetworkUp()){
  139. NVIC_SystemReset();
  140. }
  141. #endif
  142. end_main = millis() - start_main;
  143. //UU_PutString("ABC");
  144. //UU_PutString("\r\n");
  145. }
  146. /* USER CODE END 3 */
  147. }
  148. /**
  149. * @brief System Clock Configuration
  150. * @retval None
  151. */
  152. void SystemClock_Config(void)
  153. {
  154. RCC_OscInitTypeDef RCC_OscInitStruct;
  155. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  156. /**Configure the main internal regulator output voltage
  157. */
  158. __HAL_RCC_PWR_CLK_ENABLE();
  159. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  160. /**Initializes the CPU, AHB and APB busses clocks
  161. */
  162. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  163. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  164. RCC_OscInitStruct.HSICalibrationValue = 16;
  165. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  166. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  167. RCC_OscInitStruct.PLL.PLLM = 8;
  168. RCC_OscInitStruct.PLL.PLLN = 168;
  169. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  170. RCC_OscInitStruct.PLL.PLLQ = 7;
  171. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  172. {
  173. _Error_Handler(__FILE__, __LINE__);
  174. }
  175. /**Initializes the CPU, AHB and APB busses clocks
  176. */
  177. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  178. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  179. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  180. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  181. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  182. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  183. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  184. {
  185. _Error_Handler(__FILE__, __LINE__);
  186. }
  187. /**Configure the Systick interrupt time
  188. */
  189. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  190. /**Configure the Systick
  191. */
  192. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  193. /* SysTick_IRQn interrupt configuration */
  194. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  195. }
  196. /* USER CODE BEGIN 4 */
  197. UART_HandleTypeDef *uart_dma;
  198. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  199. {
  200. uart_dma = huart;
  201. if(huart->Instance==USART2)
  202. {
  203. if (encode(aRxBuffer[0])) // Did a new valid sentence come in?
  204. newData = true;
  205. }
  206. else if(huart->Instance==USART3)
  207. {
  208. dataset_rx = &Dataset_Slave[buff_sensor_data_index];
  209. // uint32_t crc = 0;
  210. //
  211. // for(int i=0; i<sizeof(struct Dataset) - 4; i++){
  212. // uint8_t *b = (uint8_t*)&rx_buf + i;
  213. // crc += *b;
  214. // }
  215. //
  216. // if(rx_buf.crc == crc) {
  217. memcpy(dataset_rx, &rx_buf, sizeof(struct Dataset));
  218. if(memcmp(dataset_rx, dataset_rx_old, sizeof(struct Dataset)) == 0){ //if old data is same -> no heartbeat
  219. heartbeat=0;
  220. }
  221. else{
  222. heartbeat=1;
  223. rx_success ++;
  224. }
  225. dataset_rx_old = dataset_rx;
  226. // } else {
  227. // crc_error++;
  228. // }
  229. } else {
  230. rx_success--;
  231. }
  232. }
  233. /* USER CODE END 4 */
  234. /**
  235. * @brief Period elapsed callback in non blocking mode
  236. * @note This function is called when TIM1 interrupt took place, inside
  237. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  238. * a global variable "uwTick" used as application time base.
  239. * @param htim : TIM handle
  240. * @retval None
  241. */
  242. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  243. {
  244. /* USER CODE BEGIN Callback 0 */
  245. /* USER CODE END Callback 0 */
  246. if (htim->Instance == TIM1) {
  247. HAL_IncTick();
  248. }
  249. /* USER CODE BEGIN Callback 1 */
  250. /* USER CODE END Callback 1 */
  251. }
  252. /**
  253. * @brief This function is executed in case of error occurrence.
  254. * @param file: The file name as string.
  255. * @param line: The line in file as a number.
  256. * @retval None
  257. */
  258. void _Error_Handler(char *file, int line)
  259. {
  260. /* USER CODE BEGIN Error_Handler_Debug */
  261. /* User can add his own implementation to report the HAL error return state */
  262. while(1)
  263. {
  264. }
  265. /* USER CODE END Error_Handler_Debug */
  266. }
  267. #ifdef USE_FULL_ASSERT
  268. /**
  269. * @brief Reports the name of the source file and the source line number
  270. * where the assert_param error has occurred.
  271. * @param file: pointer to the source file name
  272. * @param line: assert_param error line source number
  273. * @retval None
  274. */
  275. void assert_failed(uint8_t* file, uint32_t line)
  276. {
  277. /* USER CODE BEGIN 6 */
  278. /* User can add his own implementation to report the file name and line number,
  279. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  280. /* USER CODE END 6 */
  281. }
  282. #endif /* USE_FULL_ASSERT */
  283. /**
  284. * @}
  285. */
  286. /**
  287. * @}
  288. */
  289. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/