main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 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 "nbus_app.h"
  24. #include "memory_ec20.h"
  25. #if MODULE == MODULE_FSR
  26. #include "app_adc.h"
  27. #endif
  28. #if MODULE == MODULE_DUMMY
  29. #include "app_dummy.h"
  30. #endif
  31. #if MODULE == MODULE_IMU
  32. #include "app_imu.h"
  33. #endif
  34. #include "app_interface.h"
  35. /* USER CODE END Includes */
  36. /* Private typedef -----------------------------------------------------------*/
  37. /* USER CODE BEGIN PTD */
  38. /* USER CODE END PTD */
  39. /* Private define ------------------------------------------------------------*/
  40. /* USER CODE BEGIN PD */
  41. /* USER CODE END PD */
  42. /* Private macro -------------------------------------------------------------*/
  43. /* USER CODE BEGIN PM */
  44. /* USER CODE END PM */
  45. /* Private variables ---------------------------------------------------------*/
  46. ADC_HandleTypeDef hadc;
  47. DMA_HandleTypeDef hdma_adc;
  48. SPI_HandleTypeDef hspi1;
  49. TIM_HandleTypeDef htim21;
  50. UART_HandleTypeDef huart1;
  51. DMA_HandleTypeDef hdma_usart1_rx;
  52. DMA_HandleTypeDef hdma_usart1_tx;
  53. /* USER CODE BEGIN PV */
  54. /* USER CODE END PV */
  55. /* Private function prototypes -----------------------------------------------*/
  56. void SystemClock_Config(void);
  57. static void MX_GPIO_Init(void);
  58. static void MX_DMA_Init(void);
  59. static void MX_TIM21_Init(void);
  60. static void MX_ADC_Init(void);
  61. static void MX_SPI1_Init(void);
  62. static void MX_USART1_UART_Init(void);
  63. /* USER CODE BEGIN PFP */
  64. /* USER CODE END PFP */
  65. /* Private user code ---------------------------------------------------------*/
  66. /* USER CODE BEGIN 0 */
  67. #define MAX_SYSTICK 0xFFFFFFFF
  68. uint8_t data[NBUS_PACKET_MAX_SIZE]; // tmp read buffer
  69. uint8_t *dataUART; // pointee for rx_buffer
  70. volatile uint8_t dataL;
  71. volatile uint8_t dataI;
  72. volatile uint32_t uart_timeout = MAX_SYSTICK;
  73. #if MODULE == MODULE_IMU
  74. volatile uint8_t icm_data_ready = 0;
  75. #endif
  76. inline void uart_send(uint8_t *data, int n)
  77. {
  78. #if USE_USART_DMA_TX == 1
  79. HAL_UART_Transmit_DMA(&huart1, data, n);
  80. #else
  81. HAL_UART_Transmit(&huart1, data, n, 10);
  82. #endif
  83. }
  84. inline void uart_receive(uint8_t *dataNBUS, int n)
  85. {
  86. dataUART = dataNBUS;
  87. dataL = 0;
  88. dataI = 0;
  89. data[0] = 0;
  90. uart_timeout = HAL_GetTick();
  91. HAL_UARTEx_ReceiveToIdle_DMA(&huart1, data, n);
  92. }
  93. inline void led_on(){
  94. HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_SET);
  95. }
  96. inline void led_off(){
  97. HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
  98. }
  99. inline void led_toggle(){
  100. HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
  101. }
  102. inline void app_delay(uint8_t ms){
  103. HAL_Delay(ms);
  104. }
  105. static inline uint8_t loop_callback(nBusStateCallbackType_t state_check) {
  106. // traba kontrolovat kazdu stav (state_check) zvmlast. Moznost doplnit dalsie kontroly
  107. if (state_check == CallbackType_SENSOR) {
  108. #if MODULE == MODULE_IMU
  109. if(icm_data_ready == 1){
  110. icm_data_ready = 0;
  111. return 1; // interrupt from external sensor: data ready
  112. }
  113. #endif
  114. return 0;
  115. }
  116. if (state_check == CallbackType_UART) {
  117. // timemout: 1 - 2 ms
  118. if(HAL_GetTick() - 1 > uart_timeout) {
  119. dataL = 0;
  120. dataI = 0;
  121. data[0] = 0;
  122. uart_timeout = MAX_SYSTICK;
  123. // led_off();
  124. HAL_UARTEx_ReceiveToIdle_DMA(&huart1, data, NBUS_PACKET_MAX_SIZE);
  125. return 1; // UART receive timeout
  126. }
  127. return 0;
  128. }
  129. return 0;
  130. }
  131. // Application callbacks
  132. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
  133. HAL_UARTEx_ReceiveToIdle_DMA(huart, data, NBUS_PACKET_MAX_SIZE);
  134. uint8_t copy_offset = 0;
  135. if (dataL == 0) {
  136. uart_timeout = HAL_GetTick();
  137. dataL = data[0];
  138. dataI = 0;
  139. if (Size == 1) {
  140. return;
  141. }
  142. Size--;
  143. copy_offset = 1;
  144. }
  145. memcpy(&dataUART[dataI], &data[copy_offset], Size);
  146. dataI += Size;
  147. if(dataI >= dataL){
  148. nbus_cb_UART_RX(dataI);
  149. dataL=0;
  150. dataI=0;
  151. data[0]=0;
  152. uart_timeout = MAX_SYSTICK;
  153. }
  154. }
  155. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart){
  156. HAL_UARTEx_ReceiveToIdle_DMA(huart, data, NBUS_PACKET_MAX_SIZE);
  157. }
  158. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
  159. #if MODULE == MODULE_IMU
  160. icm_data_ready = 1;
  161. #endif
  162. }
  163. /* USER CODE END 0 */
  164. /**
  165. * @brief The application entry point.
  166. * @retval int
  167. */
  168. int main(void)
  169. {
  170. /* USER CODE BEGIN 1 */
  171. /* USER CODE END 1 */
  172. /* MCU Configuration--------------------------------------------------------*/
  173. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  174. HAL_Init();
  175. /* USER CODE BEGIN Init */
  176. /* USER CODE END Init */
  177. /* Configure the system clock */
  178. SystemClock_Config();
  179. /* USER CODE BEGIN SysInit */
  180. /* USER CODE END SysInit */
  181. /* Initialize all configured peripherals */
  182. MX_GPIO_Init();
  183. MX_DMA_Init();
  184. MX_TIM21_Init();
  185. MX_SPI1_Init();
  186. MX_USART1_UART_Init();
  187. /* USER CODE BEGIN 2 */
  188. dataI=0;
  189. dataL=0;
  190. #ifdef MODULE_INIT_IP_ADC
  191. MX_ADC_Init();
  192. #endif
  193. #ifdef MODULE_INIT_IP_SPI
  194. MX_SPI1_Init();
  195. #endif
  196. #if MODULE_MASTER == 1
  197. MX_RTC_Init();
  198. #endif
  199. nBusPlatformInterface_t hw_platform = {
  200. uart_receive,
  201. uart_send,
  202. led_on,
  203. led_off,
  204. led_toggle,
  205. app_delay,
  206. loop_callback,
  207. };
  208. #if MODULE_MASTER == 1
  209. periph.rtc = &hrtc;
  210. #endif
  211. #if MODULE == MODULE_DUMMY
  212. nbus_init(getDummyDriver(), &hw_platform);
  213. nbus_init_app(NULL, NULL);
  214. #endif
  215. #if MODULE == MODULE_FSR
  216. nbus_init(getMcuAdcDriver(), &hw_platform);
  217. nbus_init_app(&hadc, NULL);
  218. #endif
  219. #if MODULE == MODULE_IMU
  220. icm20948_Config config;
  221. McuPin_typeDef pinCS;
  222. pinCS.pin = SPI_SS_Pin;
  223. pinCS.port = SPI_SS_GPIO_Port;
  224. config.pinCS = &pinCS;
  225. config.gyro.low_pass_filter = GYRO_lpf_196_6Hz;
  226. config.gyro.sample_rate = GYRO_samplerate_281_3Hz;
  227. config.accel.low_pass_filter = ACCEL_lpf_246Hz;
  228. config.accel.sample_rate = ACCEL_samplerate_281_3Hz;
  229. config.int_source = interrupt_RAW_DATA_0_RDY_EN;
  230. config.mag.mode = mag_mode_power_down;
  231. nbus_init(getImuDriver(), &hw_platform);
  232. nbus_init_app(&hspi1, &config);
  233. #endif
  234. nBus_MemoryDriver memory_ec20 = {
  235. DS28EC20_init,
  236. DS28EC20_readData4B,
  237. DS28EC20_readData2B,
  238. DS28EC20_readData1B,
  239. DS28EC20_writeData,
  240. DS28EC20_getId,
  241. DS28EC20_getCapacity
  242. };
  243. memory_ec20.init(ONE_WIRE_GPIO_Port, ONE_WIRE_Pin);
  244. nbus_init_memory_driver(&memory_ec20);
  245. nbus_stack();
  246. /* USER CODE END 2 */
  247. /* Infinite loop */
  248. /* USER CODE BEGIN WHILE */
  249. while (1)
  250. {
  251. /* USER CODE END WHILE */
  252. /* USER CODE BEGIN 3 */
  253. }
  254. /* USER CODE END 3 */
  255. }
  256. /**
  257. * @brief System Clock Configuration
  258. * @retval None
  259. */
  260. void SystemClock_Config(void)
  261. {
  262. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  263. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  264. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  265. /** Configure the main internal regulator output voltage
  266. */
  267. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  268. /** Initializes the RCC Oscillators according to the specified parameters
  269. * in the RCC_OscInitTypeDef structure.
  270. */
  271. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  272. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  273. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  274. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  275. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  276. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  277. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  278. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  279. {
  280. Error_Handler();
  281. }
  282. /** Initializes the CPU, AHB and APB buses clocks
  283. */
  284. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  285. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  286. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  287. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  288. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  289. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  290. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  291. {
  292. Error_Handler();
  293. }
  294. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  295. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  296. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  297. {
  298. Error_Handler();
  299. }
  300. }
  301. /**
  302. * @brief ADC Initialization Function
  303. * @param None
  304. * @retval None
  305. */
  306. static void MX_ADC_Init(void)
  307. {
  308. /* USER CODE BEGIN ADC_Init 0 */
  309. /* USER CODE END ADC_Init 0 */
  310. ADC_ChannelConfTypeDef sConfig = {0};
  311. /* USER CODE BEGIN ADC_Init 1 */
  312. /* USER CODE END ADC_Init 1 */
  313. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  314. */
  315. hadc.Instance = ADC1;
  316. hadc.Init.OversamplingMode = DISABLE;
  317. hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  318. hadc.Init.Resolution = ADC_RESOLUTION_12B;
  319. hadc.Init.SamplingTime = ADC_SAMPLETIME_79CYCLES_5;
  320. hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  321. hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  322. hadc.Init.ContinuousConvMode = ENABLE;
  323. hadc.Init.DiscontinuousConvMode = DISABLE;
  324. hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  325. hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  326. hadc.Init.DMAContinuousRequests = ENABLE;
  327. hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  328. hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  329. hadc.Init.LowPowerAutoWait = DISABLE;
  330. hadc.Init.LowPowerFrequencyMode = DISABLE;
  331. hadc.Init.LowPowerAutoPowerOff = DISABLE;
  332. if (HAL_ADC_Init(&hadc) != HAL_OK)
  333. {
  334. Error_Handler();
  335. }
  336. /** Configure for the selected ADC regular channel to be converted.
  337. */
  338. sConfig.Channel = ADC_CHANNEL_0;
  339. sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  340. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  341. {
  342. Error_Handler();
  343. }
  344. /** Configure for the selected ADC regular channel to be converted.
  345. */
  346. sConfig.Channel = ADC_CHANNEL_1;
  347. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  348. {
  349. Error_Handler();
  350. }
  351. /** Configure for the selected ADC regular channel to be converted.
  352. */
  353. sConfig.Channel = ADC_CHANNEL_2;
  354. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  355. {
  356. Error_Handler();
  357. }
  358. /** Configure for the selected ADC regular channel to be converted.
  359. */
  360. sConfig.Channel = ADC_CHANNEL_3;
  361. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  362. {
  363. Error_Handler();
  364. }
  365. /** Configure for the selected ADC regular channel to be converted.
  366. */
  367. sConfig.Channel = ADC_CHANNEL_4;
  368. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  369. {
  370. Error_Handler();
  371. }
  372. /** Configure for the selected ADC regular channel to be converted.
  373. */
  374. sConfig.Channel = ADC_CHANNEL_5;
  375. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  376. {
  377. Error_Handler();
  378. }
  379. /** Configure for the selected ADC regular channel to be converted.
  380. */
  381. sConfig.Channel = ADC_CHANNEL_6;
  382. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  383. {
  384. Error_Handler();
  385. }
  386. /** Configure for the selected ADC regular channel to be converted.
  387. */
  388. sConfig.Channel = ADC_CHANNEL_7;
  389. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  390. {
  391. Error_Handler();
  392. }
  393. /* USER CODE BEGIN ADC_Init 2 */
  394. /* USER CODE END ADC_Init 2 */
  395. }
  396. /**
  397. * @brief SPI1 Initialization Function
  398. * @param None
  399. * @retval None
  400. */
  401. static void MX_SPI1_Init(void)
  402. {
  403. /* USER CODE BEGIN SPI1_Init 0 */
  404. /* USER CODE END SPI1_Init 0 */
  405. /* USER CODE BEGIN SPI1_Init 1 */
  406. /* USER CODE END SPI1_Init 1 */
  407. /* SPI1 parameter configuration*/
  408. hspi1.Instance = SPI1;
  409. hspi1.Init.Mode = SPI_MODE_MASTER;
  410. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  411. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  412. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  413. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  414. hspi1.Init.NSS = SPI_NSS_SOFT;
  415. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  416. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  417. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  418. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  419. hspi1.Init.CRCPolynomial = 7;
  420. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  421. {
  422. Error_Handler();
  423. }
  424. /* USER CODE BEGIN SPI1_Init 2 */
  425. /* USER CODE END SPI1_Init 2 */
  426. }
  427. /**
  428. * @brief TIM21 Initialization Function
  429. * @param None
  430. * @retval None
  431. */
  432. static void MX_TIM21_Init(void)
  433. {
  434. /* USER CODE BEGIN TIM21_Init 0 */
  435. /* USER CODE END TIM21_Init 0 */
  436. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  437. TIM_MasterConfigTypeDef sMasterConfig = {0};
  438. /* USER CODE BEGIN TIM21_Init 1 */
  439. /* USER CODE END TIM21_Init 1 */
  440. htim21.Instance = TIM21;
  441. htim21.Init.Prescaler = 32000;
  442. htim21.Init.CounterMode = TIM_COUNTERMODE_UP;
  443. htim21.Init.Period = 100;
  444. htim21.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  445. htim21.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  446. if (HAL_TIM_Base_Init(&htim21) != HAL_OK)
  447. {
  448. Error_Handler();
  449. }
  450. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  451. if (HAL_TIM_ConfigClockSource(&htim21, &sClockSourceConfig) != HAL_OK)
  452. {
  453. Error_Handler();
  454. }
  455. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  456. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  457. if (HAL_TIMEx_MasterConfigSynchronization(&htim21, &sMasterConfig) != HAL_OK)
  458. {
  459. Error_Handler();
  460. }
  461. /* USER CODE BEGIN TIM21_Init 2 */
  462. /* USER CODE END TIM21_Init 2 */
  463. }
  464. /**
  465. * @brief USART1 Initialization Function
  466. * @param None
  467. * @retval None
  468. */
  469. static void MX_USART1_UART_Init(void)
  470. {
  471. /* USER CODE BEGIN USART1_Init 0 */
  472. /* USER CODE END USART1_Init 0 */
  473. /* USER CODE BEGIN USART1_Init 1 */
  474. /* USER CODE END USART1_Init 1 */
  475. huart1.Instance = USART1;
  476. huart1.Init.BaudRate = UART_BAUDRATE;
  477. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  478. huart1.Init.StopBits = UART_STOPBITS_1;
  479. huart1.Init.Parity = UART_PARITY_NONE;
  480. huart1.Init.Mode = UART_MODE_TX_RX;
  481. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  482. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  483. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  484. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  485. if (HAL_RS485Ex_Init(&huart1, UART_DE_POLARITY_HIGH, 0, 0) != HAL_OK)
  486. {
  487. Error_Handler();
  488. }
  489. /* USER CODE BEGIN USART1_Init 2 */
  490. /* USER CODE END USART1_Init 2 */
  491. }
  492. /**
  493. * Enable DMA controller clock
  494. */
  495. static void MX_DMA_Init(void)
  496. {
  497. /* DMA controller clock enable */
  498. __HAL_RCC_DMA1_CLK_ENABLE();
  499. /* DMA interrupt init */
  500. /* DMA1_Channel1_IRQn interrupt configuration */
  501. HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  502. HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  503. /* DMA1_Channel2_3_IRQn interrupt configuration */
  504. HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
  505. HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
  506. }
  507. /**
  508. * @brief GPIO Initialization Function
  509. * @param None
  510. * @retval None
  511. */
  512. static void MX_GPIO_Init(void)
  513. {
  514. GPIO_InitTypeDef GPIO_InitStruct = {0};
  515. /* USER CODE BEGIN MX_GPIO_Init_1 */
  516. /* USER CODE END MX_GPIO_Init_1 */
  517. /* GPIO Ports Clock Enable */
  518. __HAL_RCC_GPIOC_CLK_ENABLE();
  519. __HAL_RCC_GPIOA_CLK_ENABLE();
  520. __HAL_RCC_GPIOB_CLK_ENABLE();
  521. /*Configure GPIO pin Output Level */
  522. HAL_GPIO_WritePin(GPIOB, LD3_Pin|ONE_WIRE_Pin, GPIO_PIN_RESET);
  523. /*Configure GPIO pin Output Level */
  524. HAL_GPIO_WritePin(SPI_SS_GPIO_Port, SPI_SS_Pin, GPIO_PIN_SET);
  525. /*Configure GPIO pin : LD3_Pin */
  526. GPIO_InitStruct.Pin = LD3_Pin;
  527. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  528. GPIO_InitStruct.Pull = GPIO_NOPULL;
  529. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  530. HAL_GPIO_Init(LD3_GPIO_Port, &GPIO_InitStruct);
  531. /*Configure GPIO pin : SPI_SS_Pin */
  532. GPIO_InitStruct.Pin = SPI_SS_Pin;
  533. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  534. GPIO_InitStruct.Pull = GPIO_PULLUP;
  535. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  536. HAL_GPIO_Init(SPI_SS_GPIO_Port, &GPIO_InitStruct);
  537. /*Configure GPIO pin : SPI_INT_Pin */
  538. GPIO_InitStruct.Pin = SPI_INT_Pin;
  539. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  540. GPIO_InitStruct.Pull = GPIO_PULLUP;
  541. HAL_GPIO_Init(SPI_INT_GPIO_Port, &GPIO_InitStruct);
  542. /*Configure GPIO pin : ONE_WIRE_Pin */
  543. GPIO_InitStruct.Pin = ONE_WIRE_Pin;
  544. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  545. GPIO_InitStruct.Pull = GPIO_PULLUP;
  546. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  547. HAL_GPIO_Init(ONE_WIRE_GPIO_Port, &GPIO_InitStruct);
  548. /*Configure GPIO pins : PB6 PB7 */
  549. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  550. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  551. GPIO_InitStruct.Pull = GPIO_NOPULL;
  552. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  553. GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
  554. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  555. /* EXTI interrupt init*/
  556. HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  557. HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
  558. /* USER CODE BEGIN MX_GPIO_Init_2 */
  559. /* USER CODE END MX_GPIO_Init_2 */
  560. }
  561. /* USER CODE BEGIN 4 */
  562. /* USER CODE END 4 */
  563. /**
  564. * @brief This function is executed in case of error occurrence.
  565. * @retval None
  566. */
  567. void Error_Handler(void)
  568. {
  569. /* USER CODE BEGIN Error_Handler_Debug */
  570. /* User can add his own implementation to report the HAL error return state */
  571. __disable_irq();
  572. while (1)
  573. {
  574. }
  575. /* USER CODE END Error_Handler_Debug */
  576. }
  577. #ifdef USE_FULL_ASSERT
  578. /**
  579. * @brief Reports the name of the source file and the source line number
  580. * where the assert_param error has occurred.
  581. * @param file: pointer to the source file name
  582. * @param line: assert_param error line source number
  583. * @retval None
  584. */
  585. void assert_failed(uint8_t *file, uint32_t line)
  586. {
  587. /* USER CODE BEGIN 6 */
  588. /* User can add his own implementation to report the file name and line number,
  589. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  590. /* USER CODE END 6 */
  591. }
  592. #endif /* USE_FULL_ASSERT */