main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "dma.h"
  24. #include "i2c.h"
  25. #include "spi.h"
  26. #include "tim.h"
  27. #include "usb_otg.h"
  28. #include "gpio.h"
  29. /* Private includes ----------------------------------------------------------*/
  30. /* USER CODE BEGIN Includes */
  31. #include "RTC.h"
  32. #include "lcd.h"
  33. /* USER CODE END Includes */
  34. /* Private typedef -----------------------------------------------------------*/
  35. /* USER CODE BEGIN PTD */
  36. /* USER CODE END PTD */
  37. /* Private define ------------------------------------------------------------*/
  38. /* USER CODE BEGIN PD */
  39. void TestNiveau();
  40. void TestSelect(uint8_t i);
  41. void TestSreen();
  42. void TestUsb();
  43. void TestSPI();
  44. void TestVentilateur();
  45. void TestADC();
  46. void TestRelais();
  47. void TestExtender();
  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_DMA_Init();
  82. MX_ADC1_Init();
  83. MX_I2C1_Init();
  84. MX_I2C2_Init();
  85. MX_SPI1_Init();
  86. MX_USB_OTG_FS_PCD_Init();
  87. MX_TIM2_Init();
  88. MX_TIM3_Init();
  89. MX_TIM4_Init();
  90. MX_TIM5_Init();
  91. /* USER CODE BEGIN 2 */
  92. testRTC();
  93. testBatteryRTC();
  94. testEeprom();
  95. TestNiveau();
  96. TestSreen();
  97. TestUsb();
  98. TestSPI();
  99. TestVentilateur();
  100. TestADC();
  101. TestRelais();
  102. TestExtender();
  103. /* USER CODE END 2 */
  104. /* Infinite loop */
  105. /* USER CODE BEGIN WHILE */
  106. while (1)
  107. {
  108. /* USER CODE END WHILE */
  109. /* USER CODE BEGIN 3 */
  110. }
  111. /* USER CODE END 3 */
  112. }
  113. /**
  114. * @brief System Clock Configuration
  115. * @retval None
  116. */
  117. void SystemClock_Config(void)
  118. {
  119. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  120. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  121. /** Configure the main internal regulator output voltage
  122. */
  123. __HAL_RCC_PWR_CLK_ENABLE();
  124. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  125. /** Initializes the CPU, AHB and APB busses clocks
  126. */
  127. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
  128. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  129. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  130. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  131. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  132. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  133. RCC_OscInitStruct.PLL.PLLM = 10;
  134. RCC_OscInitStruct.PLL.PLLN = 72;
  135. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  136. RCC_OscInitStruct.PLL.PLLQ = 3;
  137. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  138. {
  139. Error_Handler();
  140. }
  141. /** Initializes the CPU, AHB and APB busses clocks
  142. */
  143. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  144. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  145. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  146. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  147. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  148. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  149. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  150. {
  151. Error_Handler();
  152. }
  153. }
  154. /* USER CODE BEGIN 4 */
  155. void TestNiveau(){
  156. while(1){
  157. HAL_GPIO_ReadPin(TechniqueBas_GPIO_Port,TechniqueBas_Pin);
  158. }
  159. }
  160. void TestSelect(uint8_t i){
  161. i=i+10;
  162. HAL_Delay(i);
  163. }
  164. void TestSreen(){
  165. Lcd_PortType ports[] = {D4_GPIO_Port, D5_GPIO_Port, D6_GPIO_Port, D7_GPIO_Port};
  166. Lcd_PinType pins[] = {D4_Pin, D5_Pin, D6_Pin, D7_Pin};
  167. Lcd_HandleTypeDef lcd;
  168. lcd = Lcd_create(ports, pins, RS_GPIO_Port, RS_Pin, E_GPIO_Port, E_Pin, LCD_4_BIT_MODE);
  169. Lcd_string(&lcd, "4ilo - 4bit");
  170. Lcd_cursor(&lcd, 1,6);
  171. Lcd_int(&lcd, -500);
  172. }
  173. void TestUsb(){
  174. }
  175. void TestSPI(){
  176. uint8_t data[1];
  177. HAL_SPI_Receive(&hspi1,data,1,1000);
  178. data[1]++;
  179. HAL_SPI_Transmit(&hspi1,data,1,1000);
  180. }
  181. void TestVentilateur(){
  182. TIM_OC_InitTypeDef sConfigOC;
  183. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  184. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  185. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  186. for (int i = 0; i<31250-1;i=i+3125){
  187. sConfigOC.Pulse = i;
  188. HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2);
  189. HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
  190. HAL_Delay(1000);
  191. }
  192. }
  193. uint32_t buffer[3];
  194. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){
  195. uint32_t tempeau = buffer[0];
  196. uint32_t tempair = buffer[1];
  197. uint32_t ph = buffer[2];
  198. HAL_ADC_Stop_DMA(&hadc1);
  199. }
  200. void TestADC(){
  201. HAL_ADC_Start_DMA(&hadc1,buffer,3);
  202. }
  203. void TestRelais(){
  204. HAL_I2C_Master_Transmit(&hi2c2, 0x40, 0xAA, 1, 100);
  205. uint8_t value[1];
  206. HAL_I2C_Master_Receive(&hi2c2, 0x40, value, 1, 100);
  207. HAL_Delay(100);
  208. }
  209. void TestExtender(){
  210. HAL_I2C_Master_Transmit(&hi2c2, 0x4E, 0xAA, 1, 100);
  211. uint8_t value[1];
  212. HAL_I2C_Master_Receive(&hi2c2, 0x4E, value, 1, 100);
  213. HAL_Delay(100);
  214. }
  215. /* USER CODE END 4 */
  216. /**
  217. * @brief Period elapsed callback in non blocking mode
  218. * @note This function is called when TIM1 interrupt took place, inside
  219. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  220. * a global variable "uwTick" used as application time base.
  221. * @param htim : TIM handle
  222. * @retval None
  223. */
  224. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  225. {
  226. /* USER CODE BEGIN Callback 0 */
  227. /* USER CODE END Callback 0 */
  228. if (htim->Instance == TIM1) {
  229. HAL_IncTick();
  230. }
  231. /* USER CODE BEGIN Callback 1 */
  232. /* USER CODE END Callback 1 */
  233. }
  234. /**
  235. * @brief This function is executed in case of error occurrence.
  236. * @retval None
  237. */
  238. void Error_Handler(void)
  239. {
  240. /* USER CODE BEGIN Error_Handler_Debug */
  241. /* User can add his own implementation to report the HAL error return state */
  242. /* USER CODE END Error_Handler_Debug */
  243. }
  244. #ifdef USE_FULL_ASSERT
  245. /**
  246. * @brief Reports the name of the source file and the source line number
  247. * where the assert_param error has occurred.
  248. * @param file: pointer to the source file name
  249. * @param line: assert_param error line source number
  250. * @retval None
  251. */
  252. void assert_failed(uint8_t *file, uint32_t line)
  253. {
  254. /* USER CODE BEGIN 6 */
  255. /* User can add his own implementation to report the file name and line number,
  256. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  257. /* USER CODE END 6 */
  258. }
  259. #endif /* USE_FULL_ASSERT */
  260. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/