main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "adc.h"
  23. #include "i2c.h"
  24. #include "spi.h"
  25. #include "tim.h"
  26. #include "usb_otg.h"
  27. #include "gpio.h"
  28. /* Private includes ----------------------------------------------------------*/
  29. /* USER CODE BEGIN Includes */
  30. /* USER CODE END Includes */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33. /* USER CODE END PTD */
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. void testAquit();
  37. void testLed();
  38. void testSortie();
  39. void testSPI();
  40. void testVent();
  41. void testtemp();
  42. void TestOn();
  43. void TestOFF();
  44. void TestPWM();
  45. /* USER CODE END PD */
  46. /* Private macro -------------------------------------------------------------*/
  47. /* USER CODE BEGIN PM */
  48. /* USER CODE END PM */
  49. /* Private variables ---------------------------------------------------------*/
  50. /* USER CODE BEGIN PV */
  51. /* USER CODE END PV */
  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. /* USER CODE BEGIN PFP */
  55. /* USER CODE END PFP */
  56. /* Private user code ---------------------------------------------------------*/
  57. /* USER CODE BEGIN 0 */
  58. /* USER CODE END 0 */
  59. /**
  60. * @brief The application entry point.
  61. * @retval int
  62. */
  63. int main(void)
  64. {
  65. /* USER CODE BEGIN 1 */
  66. /* USER CODE END 1 */
  67. /* MCU Configuration--------------------------------------------------------*/
  68. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  69. HAL_Init();
  70. /* USER CODE BEGIN Init */
  71. /* USER CODE END Init */
  72. /* Configure the system clock */
  73. SystemClock_Config();
  74. /* USER CODE BEGIN SysInit */
  75. /* USER CODE END SysInit */
  76. /* Initialize all configured peripherals */
  77. MX_GPIO_Init();
  78. MX_USB_OTG_FS_USB_Init();
  79. MX_TIM4_Init();
  80. MX_TIM3_Init();
  81. MX_I2C3_Init();
  82. MX_I2C2_Init();
  83. MX_SPI1_Init();
  84. MX_TIM9_Init();
  85. MX_TIM1_Init();
  86. MX_ADC1_Init();
  87. MX_TIM2_Init();
  88. /* USER CODE BEGIN 2 */
  89. testRTC();
  90. testBatteryRTC();
  91. testEeprom();
  92. testAquit();
  93. testLed();
  94. testSortie();
  95. testSPI();
  96. testVent();
  97. testtemp();
  98. TestOn();
  99. TestOFF();
  100. TestPWM();
  101. /* USER CODE END 2 */
  102. /* Infinite loop */
  103. /* USER CODE BEGIN WHILE */
  104. while (1)
  105. {
  106. /* USER CODE END WHILE */
  107. /* USER CODE BEGIN 3 */
  108. }
  109. /* USER CODE END 3 */
  110. }
  111. /**
  112. * @brief System Clock Configuration
  113. * @retval None
  114. */
  115. void SystemClock_Config(void)
  116. {
  117. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  118. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  119. /** Configure the main internal regulator output voltage
  120. */
  121. __HAL_RCC_PWR_CLK_ENABLE();
  122. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  123. /** Initializes the CPU, AHB and APB busses clocks
  124. */
  125. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  126. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  127. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  128. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  129. RCC_OscInitStruct.PLL.PLLM = 25;
  130. RCC_OscInitStruct.PLL.PLLN = 336;
  131. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  132. RCC_OscInitStruct.PLL.PLLQ = 7;
  133. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  134. {
  135. Error_Handler();
  136. }
  137. /** Initializes the CPU, AHB and APB busses clocks
  138. */
  139. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  140. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  141. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  142. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  143. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  144. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  145. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. }
  150. /* USER CODE BEGIN 4 */
  151. void testAquit(){
  152. while (HAL_GPIO_ReadPin(aquit_GPIO_Port,aquit_Pin));
  153. HAL_Delay(1);
  154. }
  155. void testLed(){
  156. HAL_GPIO_WritePin(LED_vert_GPIO_Port,LED_vert_Pin,0);
  157. HAL_GPIO_WritePin(LED_orange_GPIO_Port,LED_orange_Pin,0);
  158. HAL_GPIO_WritePin(LED_rouge_GPIO_Port,LED_rouge_Pin,0);
  159. HAL_Delay(1000);
  160. HAL_GPIO_WritePin(LED_vert_GPIO_Port,LED_vert_Pin,1);
  161. HAL_GPIO_WritePin(LED_orange_GPIO_Port,LED_orange_Pin,1);
  162. HAL_GPIO_WritePin(LED_rouge_GPIO_Port,LED_rouge_Pin,1);
  163. HAL_Delay(1000);
  164. }
  165. void testSortie(){
  166. HAL_GPIO_WritePin(S1_GPIO_Port,S1_Pin,0);
  167. HAL_GPIO_WritePin(S2_GPIO_Port,S2_Pin,0);
  168. HAL_GPIO_WritePin(S3_GPIO_Port,S3_Pin,0);
  169. HAL_GPIO_WritePin(S4_GPIO_Port,S4_Pin,0);
  170. HAL_GPIO_WritePin(S5_GPIO_Port,S5_Pin,0);
  171. HAL_GPIO_WritePin(S6_GPIO_Port,S6_Pin,0);
  172. HAL_GPIO_WritePin(S7_GPIO_Port,S7_Pin,0);
  173. HAL_GPIO_WritePin(S8_GPIO_Port,S8_Pin,0);
  174. HAL_GPIO_WritePin(S9_GPIO_Port,S9_Pin,0);
  175. HAL_GPIO_WritePin(S10_GPIO_Port,S10_Pin,0);
  176. HAL_Delay(1000);
  177. HAL_GPIO_WritePin(S1_GPIO_Port,S1_Pin,1);
  178. HAL_GPIO_WritePin(S2_GPIO_Port,S2_Pin,1);
  179. HAL_GPIO_WritePin(S3_GPIO_Port,S3_Pin,1);
  180. HAL_GPIO_WritePin(S4_GPIO_Port,S4_Pin,1);
  181. HAL_GPIO_WritePin(S5_GPIO_Port,S5_Pin,1);
  182. HAL_GPIO_WritePin(S6_GPIO_Port,S6_Pin,1);
  183. HAL_GPIO_WritePin(S7_GPIO_Port,S7_Pin,1);
  184. HAL_GPIO_WritePin(S8_GPIO_Port,S8_Pin,1);
  185. HAL_GPIO_WritePin(S9_GPIO_Port,S9_Pin,1);
  186. HAL_GPIO_WritePin(S10_GPIO_Port,S10_Pin,1);
  187. HAL_Delay(1000);
  188. }
  189. void testSPI(){
  190. uint8_t data[1];
  191. HAL_SPI_Receive(&hspi1,data,1,1000);
  192. data[1]++;
  193. HAL_SPI_Transmit(&hspi1,data,1,1000);
  194. }
  195. void testVent(){
  196. TIM_OC_InitTypeDef sConfigOC;
  197. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  198. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  199. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  200. for (int i = 0; i<31250-1;i=i+3125){
  201. sConfigOC.Pulse = i;
  202. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  203. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  204. HAL_Delay(1000);
  205. }
  206. }
  207. void testtemp(){
  208. int i;
  209. while(1){
  210. HAL_ADC_Start(&hadc1);
  211. HAL_ADC_PollForConversion(&hadc1,1000);
  212. i=HAL_ADC_GetValue(&hadc1);
  213. }
  214. }
  215. void TestPWM(){
  216. TIM_OC_InitTypeDef sConfigOC;
  217. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  218. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  219. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  220. for (int i = 0; i<31250-1;i=i+3125){
  221. sConfigOC.Pulse = i;
  222. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3);
  223. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
  224. HAL_Delay(1000);
  225. }
  226. }
  227. uint32_t buffer[1];
  228. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){
  229. uint32_t temp = buffer[0];
  230. HAL_ADC_Stop_DMA(&hadc1);
  231. }
  232. void TestADC(){
  233. HAL_ADC_Start_DMA(&hadc1,buffer,1);
  234. }
  235. /* USER CODE END 4 */
  236. /**
  237. * @brief Period elapsed callback in non blocking mode
  238. * @note This function is called when TIM5 interrupt took place, inside
  239. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  240. * a global variable "uwTick" used as application time base.
  241. * @param htim : TIM handle
  242. * @retval None
  243. */
  244. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  245. {
  246. /* USER CODE BEGIN Callback 0 */
  247. /* USER CODE END Callback 0 */
  248. if (htim->Instance == TIM5) {
  249. HAL_IncTick();
  250. }
  251. /* USER CODE BEGIN Callback 1 */
  252. /* USER CODE END Callback 1 */
  253. }
  254. /**
  255. * @brief This function is executed in case of error occurrence.
  256. * @retval None
  257. */
  258. void Error_Handler(void)
  259. {
  260. /* USER CODE BEGIN Error_Handler_Debug */
  261. /* User can add his own implementation to report the HAL error return state */
  262. /* USER CODE END Error_Handler_Debug */
  263. }
  264. #ifdef USE_FULL_ASSERT
  265. /**
  266. * @brief Reports the name of the source file and the source line number
  267. * where the assert_param error has occurred.
  268. * @param file: pointer to the source file name
  269. * @param line: assert_param error line source number
  270. * @retval None
  271. */
  272. void assert_failed(uint8_t *file, uint32_t line)
  273. {
  274. /* USER CODE BEGIN 6 */
  275. /* User can add his own implementation to report the file name and line number,
  276. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  277. /* USER CODE END 6 */
  278. }
  279. #endif /* USE_FULL_ASSERT */
  280. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/