main.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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. #include "cmsis_os.h"
  22. #include "usb_device.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include "ImC/imc_api.h"
  26. /* USER CODE END Includes */
  27. /* Private typedef -----------------------------------------------------------*/
  28. typedef StaticTask_t osStaticThreadDef_t;
  29. /* USER CODE BEGIN PTD */
  30. /* USER CODE END PTD */
  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. #define FRAM_MEM_SIZE (0x100000) // 1 MiB = 2 * 512 * 1024 bytes
  34. #define MRAM_MEM_SIZE (0x800000) // 8 MiB = 2 * 4 * 1024 * 1024 bytes
  35. #define MEM_TYPE_MRAM (1)
  36. #define MEM_TYPE_FRAM (2)
  37. #define MEM_SIZE_HALF (3)
  38. #define MEM_SIZE_FULL (4)
  39. #define MEM_TEST_TYPE MEM_TYPE_MRAM
  40. #define MEM_TEST_SIZE MEM_SIZE_HALF
  41. #define MEM_TEST_ADDR (0x68000000U)
  42. #if !defined(MEM_TEST_TYPE)
  43. #error "Choose the memory type for test!"
  44. #endif
  45. #if !defined(MEM_TEST_SIZE)
  46. #error "Choose the memory size for test!"
  47. #endif
  48. #if (MEM_TEST_TYPE == MEM_TYPE_MRAM)
  49. #if (MEM_TEST_SIZE == MEM_SIZE_FULL)
  50. #define MEMORY_SIZE MRAM_MEM_SIZE
  51. #elif (MEM_TEST_SIZE == MEM_SIZE_HALF)
  52. #define MEMORY_SIZE (MRAM_MEM_SIZE / 2)
  53. #else
  54. #error "Invalid memory size"
  55. #endif
  56. #elif (MEM_TEST_TYPE == MEM_TYPE_FRAM)
  57. #if (MEM_TEST_SIZE == MEM_SIZE_FULL)
  58. #define MEMORY_SIZE FRAM_MEM_SIZE
  59. #elif (MEM_TEST_SIZE == MEM_SIZE_HALF)
  60. #define MEMORY_SIZE (FRAM_MEM_SIZE / 2)
  61. #else
  62. #error "Invalid memory size"
  63. #endif
  64. #else
  65. #error "Invalid memory type"
  66. #endif
  67. /* USER CODE END PD */
  68. /* Private macro -------------------------------------------------------------*/
  69. /* USER CODE BEGIN PM */
  70. /* USER CODE END PM */
  71. /* Private variables ---------------------------------------------------------*/
  72. ADC_HandleTypeDef hadc1;
  73. FDCAN_HandleTypeDef hfdcan1;
  74. I2C_HandleTypeDef hi2c1;
  75. I2C_HandleTypeDef hi2c3;
  76. SPI_HandleTypeDef hspi1;
  77. SPI_HandleTypeDef hspi2;
  78. TIM_HandleTypeDef htim7;
  79. UART_HandleTypeDef huart4;
  80. UART_HandleTypeDef huart1;
  81. UART_HandleTypeDef huart2;
  82. UART_HandleTypeDef huart3;
  83. SRAM_HandleTypeDef hsram1;
  84. SRAM_HandleTypeDef hsram2;
  85. SRAM_HandleTypeDef hsram3;
  86. SRAM_HandleTypeDef hsram4;
  87. /* Definitions for taskEPS */
  88. osThreadId_t taskEPSHandle;
  89. uint32_t taskEPSBuffer[ 128 ];
  90. osStaticThreadDef_t taskEPSControlBlock;
  91. const osThreadAttr_t taskEPS_attributes = {
  92. .name = "taskEPS",
  93. .stack_mem = &taskEPSBuffer[0],
  94. .stack_size = sizeof(taskEPSBuffer),
  95. .cb_mem = &taskEPSControlBlock,
  96. .cb_size = sizeof(taskEPSControlBlock),
  97. .priority = (osPriority_t) osPriorityNormal,
  98. };
  99. /* Definitions for taskSnap */
  100. osThreadId_t taskSnapHandle;
  101. uint32_t taskSnapBuffer[ 512 ];
  102. osStaticThreadDef_t taskSnapControlBlock;
  103. const osThreadAttr_t taskSnap_attributes = {
  104. .name = "taskSnap",
  105. .stack_mem = &taskSnapBuffer[0],
  106. .stack_size = sizeof(taskSnapBuffer),
  107. .cb_mem = &taskSnapControlBlock,
  108. .cb_size = sizeof(taskSnapControlBlock),
  109. .priority = (osPriority_t) osPriorityNormal,
  110. };
  111. /* Definitions for taskAI */
  112. osThreadId_t taskAIHandle;
  113. uint32_t taskAIBuffer[ 37968 ];
  114. osStaticThreadDef_t taskAIControlBlock;
  115. const osThreadAttr_t taskAI_attributes = {
  116. .name = "taskAI",
  117. .stack_mem = &taskAIBuffer[0],
  118. .stack_size = sizeof(taskAIBuffer),
  119. .cb_mem = &taskAIControlBlock,
  120. .cb_size = sizeof(taskAIControlBlock),
  121. .priority = (osPriority_t) osPriorityNormal,
  122. };
  123. /* USER CODE BEGIN PV */
  124. /* USER CODE END PV */
  125. /* Private function prototypes -----------------------------------------------*/
  126. void SystemClock_Config(void);
  127. void PeriphCommonClock_Config(void);
  128. static void MX_GPIO_Init(void);
  129. static void MX_ADC1_Init(void);
  130. static void MX_FDCAN1_Init(void);
  131. static void MX_FMC_Init(void);
  132. static void MX_I2C1_Init(void);
  133. static void MX_I2C3_Init(void);
  134. static void MX_SPI1_Init(void);
  135. static void MX_SPI2_Init(void);
  136. static void MX_TIM7_Init(void);
  137. static void MX_UART4_Init(void);
  138. static void MX_USART1_UART_Init(void);
  139. static void MX_USART2_UART_Init(void);
  140. static void MX_USART3_UART_Init(void);
  141. static void MX_ICACHE_Init(void);
  142. static void MX_GTZC_Init(void);
  143. void taskEPSRunner(void *argument);
  144. void taskSnapRunner(void *argument);
  145. void taskAIRunner(void *argument);
  146. /* USER CODE BEGIN PFP */
  147. /* USER CODE END PFP */
  148. /* Private user code ---------------------------------------------------------*/
  149. /* USER CODE BEGIN 0 */
  150. #ifdef __cplusplus
  151. extern "C" int _write(int32_t file, uint8_t *ptr, int32_t len) {
  152. #else
  153. int _write(int32_t file, uint8_t *ptr, int32_t len) {
  154. #endif
  155. if( HAL_UART_Transmit(&huart1, ptr, len, len) == HAL_OK ) return len;
  156. else return 0;
  157. }
  158. void vApplicationStackOverflowHook(TaskHandle_t* pxTask , char* pcTaskName ) {
  159. __disable_irq();
  160. //todo: Indicate Overflow with LEDs
  161. printf("stack overflow at %s\r\n", pcTaskName);
  162. for(;;) ;
  163. }
  164. int32_t hal_uart_ifx_send(sc03mpd_ifx_t* ifx, uint8_t* buf, uint16_t len);
  165. int32_t hal_uart_ifx_recv(sc03mpd_ifx_t* ifx, uint8_t* buf, uint16_t len);
  166. int32_t hal_uart_ifx_send(sc03mpd_ifx_t* ifx, uint8_t* buf, uint16_t len)
  167. {
  168. return (HAL_UART_Transmit((UART_HandleTypeDef*)ifx->context, buf, len, 100) == HAL_OK)? len : -1;
  169. }
  170. int32_t hal_uart_ifx_recv(sc03mpd_ifx_t* ifx, uint8_t* buf, uint16_t len)
  171. {
  172. return (HAL_UARTEx_ReceiveToIdle((UART_HandleTypeDef*)ifx->context, buf, len, &len, 500) == HAL_OK)? len : -1;
  173. }
  174. /* USER CODE END 0 */
  175. /**
  176. * @brief The application entry point.
  177. * @retval int
  178. */
  179. int main(void)
  180. {
  181. /* USER CODE BEGIN 1 */
  182. /* USER CODE END 1 */
  183. /* MCU Configuration--------------------------------------------------------*/
  184. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  185. HAL_Init();
  186. /* USER CODE BEGIN Init */
  187. /* USER CODE END Init */
  188. /* Configure the system clock */
  189. SystemClock_Config();
  190. /* Configure the peripherals common clocks */
  191. PeriphCommonClock_Config();
  192. /* GTZC initialisation */
  193. MX_GTZC_Init();
  194. /* USER CODE BEGIN SysInit */
  195. /* USER CODE END SysInit */
  196. /* Initialize all configured peripherals */
  197. MX_GPIO_Init();
  198. MX_ADC1_Init();
  199. MX_FDCAN1_Init();
  200. MX_FMC_Init();
  201. MX_I2C1_Init();
  202. MX_I2C3_Init();
  203. MX_SPI1_Init();
  204. MX_SPI2_Init();
  205. MX_TIM7_Init();
  206. MX_UART4_Init();
  207. MX_USART1_UART_Init();
  208. MX_USART2_UART_Init();
  209. MX_USART3_UART_Init();
  210. MX_ICACHE_Init();
  211. MX_USB_Device_Init();
  212. /* USER CODE BEGIN 2 */
  213. printf("\r\n\r\n\r\n");
  214. printf("**************************\r\n");
  215. printf("** TEST APP INFORMATION **\r\n");
  216. printf(" - SBC M33 freeRTOS, truztzone, CAM, EPS\r\n");
  217. printf(" - 2023. 7. 13. 12:00\r\n");
  218. printf("**************************\r\n");
  219. printf("\r\n\r\n\r\n");
  220. /* after SBC boot-up, sbc power gpio pin should be ON */
  221. imc_sbc_power_on(0); // delay(param) should be 0ms
  222. /* USER CODE END 2 */
  223. /* Init scheduler */
  224. osKernelInitialize();
  225. /* USER CODE BEGIN RTOS_MUTEX */
  226. /* add mutexes, ... */
  227. /* USER CODE END RTOS_MUTEX */
  228. /* USER CODE BEGIN RTOS_SEMAPHORES */
  229. /* add semaphores, ... */
  230. /* USER CODE END RTOS_SEMAPHORES */
  231. /* USER CODE BEGIN RTOS_TIMERS */
  232. /* start timers, add new ones, ... */
  233. /* USER CODE END RTOS_TIMERS */
  234. /* USER CODE BEGIN RTOS_QUEUES */
  235. /* add queues, ... */
  236. /* USER CODE END RTOS_QUEUES */
  237. /* Create the thread(s) */
  238. /* creation of taskEPS */
  239. taskEPSHandle = osThreadNew(taskEPSRunner, NULL, &taskEPS_attributes);
  240. /* creation of taskSnap */
  241. taskSnapHandle = osThreadNew(taskSnapRunner, NULL, &taskSnap_attributes);
  242. /* creation of taskAI */
  243. taskAIHandle = osThreadNew(taskAIRunner, NULL, &taskAI_attributes);
  244. /* USER CODE BEGIN RTOS_THREADS */
  245. /* add threads, ... */
  246. /* USER CODE END RTOS_THREADS */
  247. /* USER CODE BEGIN RTOS_EVENTS */
  248. /* add events, ... */
  249. /* USER CODE END RTOS_EVENTS */
  250. /* Start scheduler */
  251. osKernelStart();
  252. /* We should never get here as control is now taken by the scheduler */
  253. /* Infinite loop */
  254. /* USER CODE BEGIN WHILE */
  255. while (1)
  256. {
  257. /* USER CODE END WHILE */
  258. /* USER CODE BEGIN 3 */
  259. }
  260. /* USER CODE END 3 */
  261. }
  262. /**
  263. * @brief System Clock Configuration
  264. * @retval None
  265. */
  266. void SystemClock_Config(void)
  267. {
  268. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  269. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  270. /** Configure the main internal regulator output voltage
  271. */
  272. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  273. {
  274. Error_Handler();
  275. }
  276. /** Configure LSE Drive Capability
  277. */
  278. HAL_PWR_EnableBkUpAccess();
  279. __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
  280. /** Initializes the RCC Oscillators according to the specified parameters
  281. * in the RCC_OscInitTypeDef structure.
  282. */
  283. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE
  284. |RCC_OSCILLATORTYPE_MSI;
  285. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  286. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  287. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  288. RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  289. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  290. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  291. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  292. RCC_OscInitStruct.PLL.PLLM = 1;
  293. RCC_OscInitStruct.PLL.PLLN = 16;
  294. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  295. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV4;
  296. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV4;
  297. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  298. {
  299. Error_Handler();
  300. }
  301. /** Initializes the CPU, AHB and APB buses clocks
  302. */
  303. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  304. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  305. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  306. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  307. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  308. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  309. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  310. {
  311. Error_Handler();
  312. }
  313. /** Enable MSI Auto calibration
  314. */
  315. HAL_RCCEx_EnableMSIPLLMode();
  316. }
  317. /**
  318. * @brief Peripherals Common Clock Configuration
  319. * @retval None
  320. */
  321. void PeriphCommonClock_Config(void)
  322. {
  323. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  324. /** Initializes the common periph clock
  325. */
  326. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB|RCC_PERIPHCLK_ADC;
  327. PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
  328. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
  329. PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSAI1SOURCE_MSI;
  330. PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
  331. PeriphClkInit.PLLSAI1.PLLSAI1N = 24;
  332. PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
  333. PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  334. PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
  335. PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK|RCC_PLLSAI1_ADC1CLK;
  336. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  337. {
  338. Error_Handler();
  339. }
  340. }
  341. /**
  342. * @brief ADC1 Initialization Function
  343. * @param None
  344. * @retval None
  345. */
  346. static void MX_ADC1_Init(void)
  347. {
  348. /* USER CODE BEGIN ADC1_Init 0 */
  349. /* USER CODE END ADC1_Init 0 */
  350. ADC_MultiModeTypeDef multimode = {0};
  351. ADC_ChannelConfTypeDef sConfig = {0};
  352. /* USER CODE BEGIN ADC1_Init 1 */
  353. /* USER CODE END ADC1_Init 1 */
  354. /** Common config
  355. */
  356. hadc1.Instance = ADC1;
  357. hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  358. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  359. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  360. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  361. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  362. hadc1.Init.LowPowerAutoWait = DISABLE;
  363. hadc1.Init.ContinuousConvMode = DISABLE;
  364. hadc1.Init.NbrOfConversion = 1;
  365. hadc1.Init.DiscontinuousConvMode = DISABLE;
  366. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  367. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  368. hadc1.Init.DMAContinuousRequests = DISABLE;
  369. hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  370. hadc1.Init.OversamplingMode = DISABLE;
  371. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  372. {
  373. Error_Handler();
  374. }
  375. /** Configure the ADC multi-mode
  376. */
  377. multimode.Mode = ADC_MODE_INDEPENDENT;
  378. if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
  379. {
  380. Error_Handler();
  381. }
  382. /** Configure Regular Channel
  383. */
  384. sConfig.Channel = ADC_CHANNEL_1;
  385. sConfig.Rank = ADC_REGULAR_RANK_1;
  386. sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  387. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  388. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  389. sConfig.Offset = 0;
  390. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  391. {
  392. Error_Handler();
  393. }
  394. /* USER CODE BEGIN ADC1_Init 2 */
  395. /* USER CODE END ADC1_Init 2 */
  396. }
  397. /**
  398. * @brief FDCAN1 Initialization Function
  399. * @param None
  400. * @retval None
  401. */
  402. static void MX_FDCAN1_Init(void)
  403. {
  404. /* USER CODE BEGIN FDCAN1_Init 0 */
  405. /* USER CODE END FDCAN1_Init 0 */
  406. /* USER CODE BEGIN FDCAN1_Init 1 */
  407. /* USER CODE END FDCAN1_Init 1 */
  408. hfdcan1.Instance = FDCAN1;
  409. hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1;
  410. hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
  411. hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
  412. hfdcan1.Init.AutoRetransmission = DISABLE;
  413. hfdcan1.Init.TransmitPause = DISABLE;
  414. hfdcan1.Init.ProtocolException = DISABLE;
  415. hfdcan1.Init.NominalPrescaler = 16;
  416. hfdcan1.Init.NominalSyncJumpWidth = 1;
  417. hfdcan1.Init.NominalTimeSeg1 = 2;
  418. hfdcan1.Init.NominalTimeSeg2 = 2;
  419. hfdcan1.Init.DataPrescaler = 1;
  420. hfdcan1.Init.DataSyncJumpWidth = 1;
  421. hfdcan1.Init.DataTimeSeg1 = 1;
  422. hfdcan1.Init.DataTimeSeg2 = 1;
  423. hfdcan1.Init.StdFiltersNbr = 0;
  424. hfdcan1.Init.ExtFiltersNbr = 0;
  425. hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
  426. if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
  427. {
  428. Error_Handler();
  429. }
  430. /* USER CODE BEGIN FDCAN1_Init 2 */
  431. /* USER CODE END FDCAN1_Init 2 */
  432. }
  433. /**
  434. * @brief GTZC Initialization Function
  435. * @param None
  436. * @retval None
  437. */
  438. static void MX_GTZC_Init(void)
  439. {
  440. /* USER CODE BEGIN GTZC_Init 0 */
  441. /* USER CODE END GTZC_Init 0 */
  442. /* USER CODE BEGIN GTZC_Init 1 */
  443. /* USER CODE END GTZC_Init 1 */
  444. /* USER CODE BEGIN GTZC_Init 2 */
  445. /* USER CODE END GTZC_Init 2 */
  446. }
  447. /**
  448. * @brief I2C1 Initialization Function
  449. * @param None
  450. * @retval None
  451. */
  452. static void MX_I2C1_Init(void)
  453. {
  454. /* USER CODE BEGIN I2C1_Init 0 */
  455. /* USER CODE END I2C1_Init 0 */
  456. /* USER CODE BEGIN I2C1_Init 1 */
  457. /* USER CODE END I2C1_Init 1 */
  458. hi2c1.Instance = I2C1;
  459. hi2c1.Init.Timing = 0x00707CBB;
  460. hi2c1.Init.OwnAddress1 = 0;
  461. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  462. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  463. hi2c1.Init.OwnAddress2 = 0;
  464. hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  465. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  466. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  467. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  468. {
  469. Error_Handler();
  470. }
  471. /** Configure Analogue filter
  472. */
  473. if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  474. {
  475. Error_Handler();
  476. }
  477. /** Configure Digital filter
  478. */
  479. if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  480. {
  481. Error_Handler();
  482. }
  483. /* USER CODE BEGIN I2C1_Init 2 */
  484. /* USER CODE END I2C1_Init 2 */
  485. }
  486. /**
  487. * @brief I2C3 Initialization Function
  488. * @param None
  489. * @retval None
  490. */
  491. static void MX_I2C3_Init(void)
  492. {
  493. /* USER CODE BEGIN I2C3_Init 0 */
  494. /* USER CODE END I2C3_Init 0 */
  495. /* USER CODE BEGIN I2C3_Init 1 */
  496. /* USER CODE END I2C3_Init 1 */
  497. hi2c3.Instance = I2C3;
  498. hi2c3.Init.Timing = 0x00707CBB;
  499. hi2c3.Init.OwnAddress1 = 0;
  500. hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  501. hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  502. hi2c3.Init.OwnAddress2 = 0;
  503. hi2c3.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  504. hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  505. hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  506. if (HAL_I2C_Init(&hi2c3) != HAL_OK)
  507. {
  508. Error_Handler();
  509. }
  510. /** Configure Analogue filter
  511. */
  512. if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  513. {
  514. Error_Handler();
  515. }
  516. /** Configure Digital filter
  517. */
  518. if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
  519. {
  520. Error_Handler();
  521. }
  522. /* USER CODE BEGIN I2C3_Init 2 */
  523. /* USER CODE END I2C3_Init 2 */
  524. }
  525. /**
  526. * @brief ICACHE Initialization Function
  527. * @param None
  528. * @retval None
  529. */
  530. static void MX_ICACHE_Init(void)
  531. {
  532. /* USER CODE BEGIN ICACHE_Init 0 */
  533. /* USER CODE END ICACHE_Init 0 */
  534. /* USER CODE BEGIN ICACHE_Init 1 */
  535. /* USER CODE END ICACHE_Init 1 */
  536. /** Enable instruction cache in 1-way (direct mapped cache)
  537. */
  538. if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
  539. {
  540. Error_Handler();
  541. }
  542. if (HAL_ICACHE_Enable() != HAL_OK)
  543. {
  544. Error_Handler();
  545. }
  546. /* USER CODE BEGIN ICACHE_Init 2 */
  547. /* USER CODE END ICACHE_Init 2 */
  548. }
  549. /**
  550. * @brief SPI1 Initialization Function
  551. * @param None
  552. * @retval None
  553. */
  554. static void MX_SPI1_Init(void)
  555. {
  556. /* USER CODE BEGIN SPI1_Init 0 */
  557. /* USER CODE END SPI1_Init 0 */
  558. /* USER CODE BEGIN SPI1_Init 1 */
  559. /* USER CODE END SPI1_Init 1 */
  560. /* SPI1 parameter configuration*/
  561. hspi1.Instance = SPI1;
  562. hspi1.Init.Mode = SPI_MODE_MASTER;
  563. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  564. hspi1.Init.DataSize = SPI_DATASIZE_4BIT;
  565. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  566. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  567. hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
  568. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  569. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  570. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  571. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  572. hspi1.Init.CRCPolynomial = 7;
  573. hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  574. hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  575. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  576. {
  577. Error_Handler();
  578. }
  579. /* USER CODE BEGIN SPI1_Init 2 */
  580. /* USER CODE END SPI1_Init 2 */
  581. }
  582. /**
  583. * @brief SPI2 Initialization Function
  584. * @param None
  585. * @retval None
  586. */
  587. static void MX_SPI2_Init(void)
  588. {
  589. /* USER CODE BEGIN SPI2_Init 0 */
  590. /* USER CODE END SPI2_Init 0 */
  591. /* USER CODE BEGIN SPI2_Init 1 */
  592. /* USER CODE END SPI2_Init 1 */
  593. /* SPI2 parameter configuration*/
  594. hspi2.Instance = SPI2;
  595. hspi2.Init.Mode = SPI_MODE_MASTER;
  596. hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  597. hspi2.Init.DataSize = SPI_DATASIZE_4BIT;
  598. hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  599. hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  600. hspi2.Init.NSS = SPI_NSS_HARD_OUTPUT;
  601. hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  602. hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  603. hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  604. hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  605. hspi2.Init.CRCPolynomial = 7;
  606. hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  607. hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  608. if (HAL_SPI_Init(&hspi2) != HAL_OK)
  609. {
  610. Error_Handler();
  611. }
  612. /* USER CODE BEGIN SPI2_Init 2 */
  613. /* USER CODE END SPI2_Init 2 */
  614. }
  615. /**
  616. * @brief TIM7 Initialization Function
  617. * @param None
  618. * @retval None
  619. */
  620. static void MX_TIM7_Init(void)
  621. {
  622. /* USER CODE BEGIN TIM7_Init 0 */
  623. /* USER CODE END TIM7_Init 0 */
  624. TIM_MasterConfigTypeDef sMasterConfig = {0};
  625. /* USER CODE BEGIN TIM7_Init 1 */
  626. /* USER CODE END TIM7_Init 1 */
  627. htim7.Instance = TIM7;
  628. htim7.Init.Prescaler = 0;
  629. htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  630. htim7.Init.Period = 65535;
  631. htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  632. if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
  633. {
  634. Error_Handler();
  635. }
  636. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  637. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  638. if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
  639. {
  640. Error_Handler();
  641. }
  642. /* USER CODE BEGIN TIM7_Init 2 */
  643. /* USER CODE END TIM7_Init 2 */
  644. }
  645. /**
  646. * @brief UART4 Initialization Function
  647. * @param None
  648. * @retval None
  649. */
  650. static void MX_UART4_Init(void)
  651. {
  652. /* USER CODE BEGIN UART4_Init 0 */
  653. /* USER CODE END UART4_Init 0 */
  654. /* USER CODE BEGIN UART4_Init 1 */
  655. /* USER CODE END UART4_Init 1 */
  656. huart4.Instance = UART4;
  657. huart4.Init.BaudRate = 115200;
  658. huart4.Init.WordLength = UART_WORDLENGTH_8B;
  659. huart4.Init.StopBits = UART_STOPBITS_1;
  660. huart4.Init.Parity = UART_PARITY_NONE;
  661. huart4.Init.Mode = UART_MODE_TX_RX;
  662. huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  663. huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  664. huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  665. huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  666. huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  667. if (HAL_UART_Init(&huart4) != HAL_OK)
  668. {
  669. Error_Handler();
  670. }
  671. if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  672. {
  673. Error_Handler();
  674. }
  675. if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  676. {
  677. Error_Handler();
  678. }
  679. if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
  680. {
  681. Error_Handler();
  682. }
  683. /* USER CODE BEGIN UART4_Init 2 */
  684. /* USER CODE END UART4_Init 2 */
  685. }
  686. /**
  687. * @brief USART1 Initialization Function
  688. * @param None
  689. * @retval None
  690. */
  691. static void MX_USART1_UART_Init(void)
  692. {
  693. /* USER CODE BEGIN USART1_Init 0 */
  694. /* USER CODE END USART1_Init 0 */
  695. /* USER CODE BEGIN USART1_Init 1 */
  696. /* USER CODE END USART1_Init 1 */
  697. huart1.Instance = USART1;
  698. huart1.Init.BaudRate = 115200;
  699. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  700. huart1.Init.StopBits = UART_STOPBITS_1;
  701. huart1.Init.Parity = UART_PARITY_NONE;
  702. huart1.Init.Mode = UART_MODE_TX_RX;
  703. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  704. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  705. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  706. huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  707. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  708. if (HAL_UART_Init(&huart1) != HAL_OK)
  709. {
  710. Error_Handler();
  711. }
  712. if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  713. {
  714. Error_Handler();
  715. }
  716. if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  717. {
  718. Error_Handler();
  719. }
  720. if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  721. {
  722. Error_Handler();
  723. }
  724. /* USER CODE BEGIN USART1_Init 2 */
  725. /* USER CODE END USART1_Init 2 */
  726. }
  727. /**
  728. * @brief USART2 Initialization Function
  729. * @param None
  730. * @retval None
  731. */
  732. static void MX_USART2_UART_Init(void)
  733. {
  734. /* USER CODE BEGIN USART2_Init 0 */
  735. /* USER CODE END USART2_Init 0 */
  736. /* USER CODE BEGIN USART2_Init 1 */
  737. /* USER CODE END USART2_Init 1 */
  738. huart2.Instance = USART2;
  739. huart2.Init.BaudRate = 115200;
  740. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  741. huart2.Init.StopBits = UART_STOPBITS_1;
  742. huart2.Init.Parity = UART_PARITY_NONE;
  743. huart2.Init.Mode = UART_MODE_TX_RX;
  744. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  745. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  746. huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  747. huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  748. huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  749. if (HAL_UART_Init(&huart2) != HAL_OK)
  750. {
  751. Error_Handler();
  752. }
  753. if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  754. {
  755. Error_Handler();
  756. }
  757. if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  758. {
  759. Error_Handler();
  760. }
  761. if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
  762. {
  763. Error_Handler();
  764. }
  765. /* USER CODE BEGIN USART2_Init 2 */
  766. /* USER CODE END USART2_Init 2 */
  767. }
  768. /**
  769. * @brief USART3 Initialization Function
  770. * @param None
  771. * @retval None
  772. */
  773. static void MX_USART3_UART_Init(void)
  774. {
  775. /* USER CODE BEGIN USART3_Init 0 */
  776. /* USER CODE END USART3_Init 0 */
  777. /* USER CODE BEGIN USART3_Init 1 */
  778. /*
  779. * use below code if code is re-generated by .ioc modification
  780. #if CAM_CHANGE_BAUDRATE
  781. huart3.Init.BaudRate = 38400;
  782. #else
  783. huart3.Init.BaudRate = 115200;
  784. #endif
  785. */
  786. /* USER CODE END USART3_Init 1 */
  787. huart3.Instance = USART3;
  788. #if CAM_CHANGE_BAUDRATE
  789. huart3.Init.BaudRate = 38400;
  790. #else
  791. huart3.Init.BaudRate = 115200;
  792. #endif
  793. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  794. huart3.Init.StopBits = UART_STOPBITS_1;
  795. huart3.Init.Parity = UART_PARITY_NONE;
  796. huart3.Init.Mode = UART_MODE_TX_RX;
  797. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  798. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  799. huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  800. huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  801. huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  802. if (HAL_UART_Init(&huart3) != HAL_OK)
  803. {
  804. Error_Handler();
  805. }
  806. if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  807. {
  808. Error_Handler();
  809. }
  810. if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  811. {
  812. Error_Handler();
  813. }
  814. if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK)
  815. {
  816. Error_Handler();
  817. }
  818. /* USER CODE BEGIN USART3_Init 2 */
  819. /* USER CODE END USART3_Init 2 */
  820. }
  821. /* FMC initialization function */
  822. static void MX_FMC_Init(void)
  823. {
  824. /* USER CODE BEGIN FMC_Init 0 */
  825. /* USER CODE END FMC_Init 0 */
  826. FMC_NORSRAM_TimingTypeDef Timing = {0};
  827. /* USER CODE BEGIN FMC_Init 1 */
  828. /* USER CODE END FMC_Init 1 */
  829. /** Perform the SRAM1 memory initialization sequence
  830. */
  831. hsram1.Instance = FMC_NORSRAM_DEVICE;
  832. hsram1.Extended = FMC_NORSRAM_EXTENDED_DEVICE;
  833. /* hsram1.Init */
  834. hsram1.Init.NSBank = FMC_NORSRAM_BANK1;
  835. hsram1.Init.DataAddressMux = FMC_DATA_ADDRESS_MUX_DISABLE;
  836. hsram1.Init.MemoryType = FMC_MEMORY_TYPE_SRAM;
  837. hsram1.Init.MemoryDataWidth = FMC_NORSRAM_MEM_BUS_WIDTH_16;
  838. hsram1.Init.BurstAccessMode = FMC_BURST_ACCESS_MODE_DISABLE;
  839. hsram1.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW;
  840. hsram1.Init.WaitSignalActive = FMC_WAIT_TIMING_BEFORE_WS;
  841. hsram1.Init.WriteOperation = FMC_WRITE_OPERATION_ENABLE;
  842. hsram1.Init.WaitSignal = FMC_WAIT_SIGNAL_DISABLE;
  843. hsram1.Init.ExtendedMode = FMC_EXTENDED_MODE_DISABLE;
  844. hsram1.Init.AsynchronousWait = FMC_ASYNCHRONOUS_WAIT_DISABLE;
  845. hsram1.Init.WriteBurst = FMC_WRITE_BURST_DISABLE;
  846. hsram1.Init.ContinuousClock = FMC_CONTINUOUS_CLOCK_SYNC_ONLY;
  847. hsram1.Init.WriteFifo = FMC_WRITE_FIFO_ENABLE;
  848. hsram1.Init.NBLSetupTime = 0;
  849. hsram1.Init.PageSize = FMC_PAGE_SIZE_NONE;
  850. hsram1.Init.MaxChipSelectPulse = DISABLE;
  851. /* Timing */
  852. Timing.AddressSetupTime = 15;
  853. Timing.AddressHoldTime = 15;
  854. Timing.DataSetupTime = 30;
  855. Timing.DataHoldTime = 0;
  856. Timing.BusTurnAroundDuration = 2;
  857. Timing.CLKDivision = 16;
  858. Timing.DataLatency = 17;
  859. Timing.AccessMode = FMC_ACCESS_MODE_A;
  860. /* ExtTiming */
  861. if (HAL_SRAM_Init(&hsram1, &Timing, NULL) != HAL_OK)
  862. {
  863. Error_Handler( );
  864. }
  865. /** Perform the SRAM2 memory initialization sequence
  866. */
  867. hsram2.Instance = FMC_NORSRAM_DEVICE;
  868. hsram2.Extended = FMC_NORSRAM_EXTENDED_DEVICE;
  869. /* hsram2.Init */
  870. hsram2.Init.NSBank = FMC_NORSRAM_BANK2;
  871. hsram2.Init.DataAddressMux = FMC_DATA_ADDRESS_MUX_DISABLE;
  872. hsram2.Init.MemoryType = FMC_MEMORY_TYPE_SRAM;
  873. hsram2.Init.MemoryDataWidth = FMC_NORSRAM_MEM_BUS_WIDTH_16;
  874. hsram2.Init.BurstAccessMode = FMC_BURST_ACCESS_MODE_DISABLE;
  875. hsram2.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW;
  876. hsram2.Init.WaitSignalActive = FMC_WAIT_TIMING_BEFORE_WS;
  877. hsram2.Init.WriteOperation = FMC_WRITE_OPERATION_ENABLE;
  878. hsram2.Init.WaitSignal = FMC_WAIT_SIGNAL_DISABLE;
  879. hsram2.Init.ExtendedMode = FMC_EXTENDED_MODE_DISABLE;
  880. hsram2.Init.AsynchronousWait = FMC_ASYNCHRONOUS_WAIT_DISABLE;
  881. hsram2.Init.WriteBurst = FMC_WRITE_BURST_DISABLE;
  882. hsram2.Init.ContinuousClock = FMC_CONTINUOUS_CLOCK_SYNC_ONLY;
  883. hsram2.Init.WriteFifo = FMC_WRITE_FIFO_ENABLE;
  884. hsram2.Init.NBLSetupTime = 0;
  885. hsram2.Init.PageSize = FMC_PAGE_SIZE_NONE;
  886. hsram2.Init.MaxChipSelectPulse = DISABLE;
  887. /* Timing */
  888. Timing.AddressSetupTime = 15;
  889. Timing.AddressHoldTime = 15;
  890. Timing.DataSetupTime = 30;
  891. Timing.DataHoldTime = 0;
  892. Timing.BusTurnAroundDuration = 2;
  893. Timing.CLKDivision = 16;
  894. Timing.DataLatency = 17;
  895. Timing.AccessMode = FMC_ACCESS_MODE_A;
  896. /* ExtTiming */
  897. if (HAL_SRAM_Init(&hsram2, &Timing, NULL) != HAL_OK)
  898. {
  899. Error_Handler( );
  900. }
  901. /** Perform the SRAM3 memory initialization sequence
  902. */
  903. hsram3.Instance = FMC_NORSRAM_DEVICE;
  904. hsram3.Extended = FMC_NORSRAM_EXTENDED_DEVICE;
  905. /* hsram3.Init */
  906. hsram3.Init.NSBank = FMC_NORSRAM_BANK3;
  907. hsram3.Init.DataAddressMux = FMC_DATA_ADDRESS_MUX_DISABLE;
  908. hsram3.Init.MemoryType = FMC_MEMORY_TYPE_PSRAM;
  909. hsram3.Init.MemoryDataWidth = FMC_NORSRAM_MEM_BUS_WIDTH_16;
  910. hsram3.Init.BurstAccessMode = FMC_BURST_ACCESS_MODE_DISABLE;
  911. hsram3.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW;
  912. hsram3.Init.WaitSignalActive = FMC_WAIT_TIMING_BEFORE_WS;
  913. hsram3.Init.WriteOperation = FMC_WRITE_OPERATION_ENABLE;
  914. hsram3.Init.WaitSignal = FMC_WAIT_SIGNAL_DISABLE;
  915. hsram3.Init.ExtendedMode = FMC_EXTENDED_MODE_DISABLE;
  916. hsram3.Init.AsynchronousWait = FMC_ASYNCHRONOUS_WAIT_DISABLE;
  917. hsram3.Init.WriteBurst = FMC_WRITE_BURST_DISABLE;
  918. hsram3.Init.ContinuousClock = FMC_CONTINUOUS_CLOCK_SYNC_ONLY;
  919. hsram3.Init.WriteFifo = FMC_WRITE_FIFO_ENABLE;
  920. hsram3.Init.NBLSetupTime = 0;
  921. hsram3.Init.PageSize = FMC_PAGE_SIZE_NONE;
  922. hsram3.Init.MaxChipSelectPulse = DISABLE;
  923. /* Timing */
  924. Timing.AddressSetupTime = 15;
  925. Timing.AddressHoldTime = 15;
  926. Timing.DataSetupTime = 30;
  927. Timing.DataHoldTime = 0;
  928. Timing.BusTurnAroundDuration = 2;
  929. Timing.CLKDivision = 16;
  930. Timing.DataLatency = 17;
  931. Timing.AccessMode = FMC_ACCESS_MODE_A;
  932. /* ExtTiming */
  933. if (HAL_SRAM_Init(&hsram3, &Timing, NULL) != HAL_OK)
  934. {
  935. Error_Handler( );
  936. }
  937. /** Perform the SRAM4 memory initialization sequence
  938. */
  939. hsram4.Instance = FMC_NORSRAM_DEVICE;
  940. hsram4.Extended = FMC_NORSRAM_EXTENDED_DEVICE;
  941. /* hsram4.Init */
  942. hsram4.Init.NSBank = FMC_NORSRAM_BANK4;
  943. hsram4.Init.DataAddressMux = FMC_DATA_ADDRESS_MUX_DISABLE;
  944. hsram4.Init.MemoryType = FMC_MEMORY_TYPE_SRAM;
  945. hsram4.Init.MemoryDataWidth = FMC_NORSRAM_MEM_BUS_WIDTH_16;
  946. hsram4.Init.BurstAccessMode = FMC_BURST_ACCESS_MODE_DISABLE;
  947. hsram4.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW;
  948. hsram4.Init.WaitSignalActive = FMC_WAIT_TIMING_BEFORE_WS;
  949. hsram4.Init.WriteOperation = FMC_WRITE_OPERATION_DISABLE;
  950. hsram4.Init.WaitSignal = FMC_WAIT_SIGNAL_DISABLE;
  951. hsram4.Init.ExtendedMode = FMC_EXTENDED_MODE_DISABLE;
  952. hsram4.Init.AsynchronousWait = FMC_ASYNCHRONOUS_WAIT_DISABLE;
  953. hsram4.Init.WriteBurst = FMC_WRITE_BURST_DISABLE;
  954. hsram4.Init.ContinuousClock = FMC_CONTINUOUS_CLOCK_SYNC_ONLY;
  955. hsram4.Init.WriteFifo = FMC_WRITE_FIFO_ENABLE;
  956. hsram4.Init.NBLSetupTime = 0;
  957. hsram4.Init.PageSize = FMC_PAGE_SIZE_NONE;
  958. hsram4.Init.MaxChipSelectPulse = DISABLE;
  959. /* Timing */
  960. Timing.AddressSetupTime = 15;
  961. Timing.AddressHoldTime = 15;
  962. Timing.DataSetupTime = 255;
  963. Timing.DataHoldTime = 0;
  964. Timing.BusTurnAroundDuration = 15;
  965. Timing.CLKDivision = 16;
  966. Timing.DataLatency = 17;
  967. Timing.AccessMode = FMC_ACCESS_MODE_A;
  968. /* ExtTiming */
  969. if (HAL_SRAM_Init(&hsram4, &Timing, NULL) != HAL_OK)
  970. {
  971. Error_Handler( );
  972. }
  973. /* USER CODE BEGIN FMC_Init 2 */
  974. /* USER CODE END FMC_Init 2 */
  975. }
  976. /**
  977. * @brief GPIO Initialization Function
  978. * @param None
  979. * @retval None
  980. */
  981. static void MX_GPIO_Init(void)
  982. {
  983. GPIO_InitTypeDef GPIO_InitStruct = {0};
  984. /* USER CODE BEGIN MX_GPIO_Init_1 */
  985. /* USER CODE END MX_GPIO_Init_1 */
  986. /* GPIO Ports Clock Enable */
  987. __HAL_RCC_GPIOE_CLK_ENABLE();
  988. __HAL_RCC_GPIOC_CLK_ENABLE();
  989. __HAL_RCC_GPIOF_CLK_ENABLE();
  990. __HAL_RCC_GPIOH_CLK_ENABLE();
  991. __HAL_RCC_GPIOA_CLK_ENABLE();
  992. __HAL_RCC_GPIOB_CLK_ENABLE();
  993. __HAL_RCC_GPIOG_CLK_ENABLE();
  994. __HAL_RCC_GPIOD_CLK_ENABLE();
  995. HAL_PWREx_EnableVddIO2();
  996. /*Configure GPIO pin Output Level */
  997. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
  998. /*Configure GPIO pin Output Level */
  999. HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_11, GPIO_PIN_RESET);
  1000. /*Configure GPIO pin Output Level */
  1001. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
  1002. /*Configure GPIO pin Output Level */
  1003. HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6|GPIO_PIN_11|GPIO_PIN_15, GPIO_PIN_RESET);
  1004. /*Configure GPIO pin Output Level */
  1005. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
  1006. /*Configure GPIO pin Output Level */
  1007. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
  1008. /*Configure GPIO pins : PC13 PC6 */
  1009. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_6;
  1010. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1011. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1012. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1013. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  1014. /*Configure GPIO pins : PF6 PF7 PF11 */
  1015. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_11;
  1016. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1017. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1018. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1019. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  1020. /*Configure GPIO pins : PF8 PF9 PF10 */
  1021. GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
  1022. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  1023. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1024. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  1025. /*Configure GPIO pin : PB2 */
  1026. GPIO_InitStruct.Pin = GPIO_PIN_2;
  1027. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1028. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1029. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1030. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  1031. /*Configure GPIO pins : PG6 PG15 */
  1032. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_15;
  1033. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1034. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1035. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1036. HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
  1037. /*Configure GPIO pin : PC7 */
  1038. GPIO_InitStruct.Pin = GPIO_PIN_7;
  1039. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1040. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1041. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1042. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  1043. /*Configure GPIO pin : PA8 */
  1044. GPIO_InitStruct.Pin = GPIO_PIN_8;
  1045. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1046. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1047. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1048. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  1049. /*Configure GPIO pin : PD2 */
  1050. GPIO_InitStruct.Pin = GPIO_PIN_2;
  1051. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1052. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1053. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1054. GPIO_InitStruct.Alternate = GPIO_AF12_SDMMC1;
  1055. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  1056. /*Configure GPIO pin : PD3 */
  1057. GPIO_InitStruct.Pin = GPIO_PIN_3;
  1058. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1059. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1060. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  1061. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  1062. /*Configure GPIO pin : PG11 */
  1063. GPIO_InitStruct.Pin = GPIO_PIN_11;
  1064. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  1065. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1066. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  1067. HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
  1068. /* USER CODE BEGIN MX_GPIO_Init_2 */
  1069. /* USER CODE END MX_GPIO_Init_2 */
  1070. }
  1071. /* USER CODE BEGIN 4 */
  1072. /* USER CODE END 4 */
  1073. /* USER CODE BEGIN Header_taskEPSRunner */
  1074. /**
  1075. * @brief Function implementing the taskEPS thread.
  1076. * @param argument: Not used
  1077. * @retval None
  1078. */
  1079. /* USER CODE END Header_taskEPSRunner */
  1080. void taskEPSRunner(void *argument)
  1081. {
  1082. /* USER CODE BEGIN 5 */
  1083. /* Infinite loop */
  1084. /* energy_level: 1~7, and SBC power off when the level is 1 */
  1085. uint8_t energy_level_prev = 8;
  1086. uint8_t energy_level_curr = 8;
  1087. for(;;)
  1088. {
  1089. energy_level_curr = imc_get_energy_level();
  1090. if (energy_level_curr != energy_level_prev)
  1091. {
  1092. printf("[EPS] E Level: %d\r\n",
  1093. energy_level_curr);
  1094. if (energy_level_curr <= EPS_SBC_OFF_LEVEL)
  1095. {
  1096. imc_sbc_power_off(0);
  1097. }
  1098. energy_level_prev = energy_level_curr;
  1099. }
  1100. osDelay(DELAY_AFTER_WORK);
  1101. }
  1102. /* USER CODE END 5 */
  1103. }
  1104. /* USER CODE BEGIN Header_taskSnapRunner */
  1105. /**
  1106. * @brief Function implementing the taskSnap thread.
  1107. * @param argument: Not used
  1108. * @retval None
  1109. */
  1110. uint32_t trial = 1;
  1111. extern uint16_t ilen;
  1112. /* USER CODE END Header_taskSnapRunner */
  1113. void taskSnapRunner(void *argument)
  1114. {
  1115. /* USER CODE BEGIN taskSnapRunner */
  1116. sc03mpd_ifx_t ifx = {
  1117. .context = (void*)&huart3,
  1118. .sendif = hal_uart_ifx_send,
  1119. .recvif = hal_uart_ifx_recv,
  1120. };
  1121. /* Infinite loop */
  1122. while (1)
  1123. {
  1124. // osSemaphoreWait(empty, osWaitForever);
  1125. /*
  1126. * 2022. 11. 10. TEST for EPS-SBC-CAM; SBC controls CAM ON/OFF; KETI EPS does NOT wait 2.5s for CAM init.
  1127. */
  1128. printf ("\r\n\r\n#%lu\r\n\r\n", trial);
  1129. trial++;
  1130. imc_cam_power_on(0);
  1131. /* camera init */
  1132. MX_USART3_UART_Init(); // (M33) moved from imc_sc03mpd_init
  1133. SC03MPD_ASSERT(imc_sc03mpd_init(&ifx), "[CAM] FAILED init", ERROR)
  1134. /* execute once when change CAM default baudrate */
  1135. #if CAM_CHANGE_BAUDRATE
  1136. sc03mpd_set_baud(&ifx, SC03MPD_BDR_115200, 1); // default: SC03MPD_BDR_38400
  1137. printf ("[CAM] OK change default baudrate");
  1138. while (1)
  1139. {
  1140. osDelay(1);
  1141. }
  1142. #endif
  1143. #if 0
  1144. /* image capture and download when GPIO pin 1 UP */
  1145. /* wait for GPIO pin 1 UP */
  1146. osEvent event = osMessageGet(queueSnapReqHandle, osWaitForever);
  1147. if (event.status != osEventMessage)
  1148. continue;
  1149. printf("[CAM] frame capture requested (%lu)\r\n", event.value.v);
  1150. #endif
  1151. /*
  1152. * 2022. 8. 25. fix logical seq. error under V0706 protocol; stop -> read length -> read data -> resume
  1153. * 2022. 8. 26. simply combine three functions; asked by kylee
  1154. */
  1155. SC03MPD_ASSERT(imc_sc03mpd_capture(&ifx), "[CAM] FAILED capture", ERROR)
  1156. /*
  1157. * 2022. 11. 10. TEST for EPS-SBC-CAM
  1158. */
  1159. imc_cam_power_off(0);
  1160. /* DO SOMETHING */
  1161. #if 0
  1162. if (decodeMyImage((uint8_t*)IMG_RAM_ADDR, ilen, 0, 0, 0, NULL, 1) != DEC_ENON)
  1163. goto ERROR;
  1164. // osSemaphoreRelease(full);
  1165. #else
  1166. printf ("[SBC] wait %dms for next job\r\n", DELAY_AFTER_WORK);
  1167. osDelay(DELAY_AFTER_WORK);
  1168. #endif
  1169. continue;
  1170. ERROR:
  1171. /*
  1172. * 2022. 7. 20. failure --> UART stuck continuously; works only after HW reset
  1173. * 2022. 8. 25. add USART3 init. and reset fn.; download or capture failure --> normal
  1174. * 2022. 8. 25. after SC03MPD power off/on, works well after few failures
  1175. * 2022. 11. 11. start of this while loop, CAM init is called
  1176. */
  1177. #if 1
  1178. printf("[CAM] ERROR HANDLING; CAM OFF > %dms delay > CAM ON\r\n", DELAY_BEFORE_ERR_HANDLING);
  1179. imc_cam_power_off(0);
  1180. // osSemaphoreRelease(empty);
  1181. osDelay(DELAY_BEFORE_ERR_HANDLING);
  1182. continue;
  1183. #else
  1184. osDelay(DELAY_BEFORE_ERR_HANDLING);
  1185. SC03MPD_ASSERT(imc_sc03mpd_init(&ifx), "[CAM] FAILED ERROR HANDLING; init", ERROR)
  1186. #endif
  1187. }
  1188. #if 0
  1189. EXIT:
  1190. osDelay(1000);
  1191. osThreadTerminate(NULL);
  1192. #endif
  1193. /* USER CODE END taskSnapRunner */
  1194. }
  1195. /* USER CODE BEGIN Header_taskAIRunner */
  1196. /**
  1197. * @brief Function implementing the taskAI thread.
  1198. * @param argument: Not used
  1199. * @retval None
  1200. */
  1201. /* USER CODE END Header_taskAIRunner */
  1202. void taskAIRunner(void *argument)
  1203. {
  1204. /* USER CODE BEGIN taskAIRunner */
  1205. /* Infinite loop */
  1206. for(;;)
  1207. {
  1208. osDelay(DELAY_AFTER_WORK);
  1209. }
  1210. /* USER CODE END taskAIRunner */
  1211. }
  1212. /**
  1213. * @brief Period elapsed callback in non blocking mode
  1214. * @note This function is called when TIM2 interrupt took place, inside
  1215. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  1216. * a global variable "uwTick" used as application time base.
  1217. * @param htim : TIM handle
  1218. * @retval None
  1219. */
  1220. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  1221. {
  1222. /* USER CODE BEGIN Callback 0 */
  1223. /* USER CODE END Callback 0 */
  1224. if (htim->Instance == TIM2) {
  1225. HAL_IncTick();
  1226. }
  1227. /* USER CODE BEGIN Callback 1 */
  1228. /* USER CODE END Callback 1 */
  1229. }
  1230. /**
  1231. * @brief This function is executed in case of error occurrence.
  1232. * @retval None
  1233. */
  1234. void Error_Handler(void)
  1235. {
  1236. /* USER CODE BEGIN Error_Handler_Debug */
  1237. /* User can add his own implementation to report the HAL error return state */
  1238. __disable_irq();
  1239. while (1)
  1240. {
  1241. }
  1242. /* USER CODE END Error_Handler_Debug */
  1243. }
  1244. #ifdef USE_FULL_ASSERT
  1245. /**
  1246. * @brief Reports the name of the source file and the source line number
  1247. * where the assert_param error has occurred.
  1248. * @param file: pointer to the source file name
  1249. * @param line: assert_param error line source number
  1250. * @retval None
  1251. */
  1252. void assert_failed(uint8_t *file, uint32_t line)
  1253. {
  1254. /* USER CODE BEGIN 6 */
  1255. /* User can add his own implementation to report the file name and line number,
  1256. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  1257. /* USER CODE END 6 */
  1258. }
  1259. #endif /* USE_FULL_ASSERT */