main.c 8.4 KB

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