main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "cmsis_os.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include "tusb.h"
  26. #include "ide_async.h"
  27. #include "cdc.h"
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. /* USER CODE END PTD */
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */
  35. /* Private macro -------------------------------------------------------------*/
  36. /* USER CODE BEGIN PM */
  37. /* USER CODE END PM */
  38. /* Private variables ---------------------------------------------------------*/
  39. RTC_HandleTypeDef hrtc;
  40. UART_HandleTypeDef huart2;
  41. PCD_HandleTypeDef hpcd_USB_OTG_HS;
  42. /* Definitions for tinyusb */
  43. osThreadId_t tinyusbHandle;
  44. const osThreadAttr_t tinyusb_attributes = {
  45. .name = "tinyusb",
  46. .stack_size = 1024 * 4,
  47. .priority = (osPriority_t) osPriorityNormal,
  48. };
  49. /* Definitions for ide */
  50. osThreadId_t ideHandle;
  51. const osThreadAttr_t ide_attributes = {
  52. .name = "ide",
  53. .stack_size = 1024 * 4,
  54. .priority = (osPriority_t) osPriorityNormal,
  55. };
  56. /* Definitions for ide */
  57. osThreadId_t cdcHandle;
  58. const osThreadAttr_t cdc_attributes = {
  59. .name = "cdc",
  60. .stack_size = 1024 * 4,
  61. .priority = (osPriority_t) osPriorityNormal,
  62. };
  63. /* USER CODE BEGIN PV */
  64. /* USER CODE END PV */
  65. /* Private function prototypes -----------------------------------------------*/
  66. void SystemClock_Config(void);
  67. static void MX_GPIO_Init(void);
  68. static void MX_RTC_Init(void);
  69. static void MX_USART2_UART_Init(void);
  70. static void MX_USB_OTG_HS_PCD_Init(void);
  71. void tinyusb_task(void *argument);
  72. void ide_task(void *argument);
  73. void cdc_task(void *argument);
  74. void cdctask(void);
  75. /* USER CODE BEGIN PFP */
  76. /* USER CODE END PFP */
  77. /* Private user code ---------------------------------------------------------*/
  78. /* USER CODE BEGIN 0 */
  79. #ifdef DEBUG
  80. int _write (int fhdl, const void *buf, size_t count) {
  81. uint8_t const* buf8 = (uint8_t const*) buf;
  82. for(size_t i=0; i<count; i++) {
  83. ITM_SendChar(buf8[i]);
  84. }
  85. return count;
  86. }
  87. #endif
  88. /* USER CODE END 0 */
  89. /**
  90. * @brief The application entry point.
  91. * @retval int
  92. */
  93. int main(void)
  94. {
  95. /* USER CODE BEGIN 1 */
  96. /* USER CODE END 1 */
  97. /* MCU Configuration--------------------------------------------------------*/
  98. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  99. HAL_Init();
  100. /* USER CODE BEGIN Init */
  101. /* USER CODE END Init */
  102. /* Configure the system clock */
  103. SystemClock_Config();
  104. /* USER CODE BEGIN SysInit */
  105. #ifdef DEBUG
  106. setvbuf(stdout, NULL, _IONBF, 0); // disable stdout buffering
  107. #endif
  108. /* USER CODE END SysInit */
  109. /* Initialize all configured peripherals */
  110. MX_GPIO_Init();
  111. MX_RTC_Init();
  112. MX_USART2_UART_Init();
  113. MX_USB_OTG_HS_PCD_Init();
  114. /* USER CODE BEGIN 2 */
  115. /* USER CODE END 2 */
  116. /* Init scheduler */
  117. osKernelInitialize();
  118. /* USER CODE BEGIN RTOS_MUTEX */
  119. /* add mutexes, ... */
  120. /* USER CODE END RTOS_MUTEX */
  121. /* USER CODE BEGIN RTOS_SEMAPHORES */
  122. /* add semaphores, ... */
  123. /* USER CODE END RTOS_SEMAPHORES */
  124. /* USER CODE BEGIN RTOS_TIMERS */
  125. /* start timers, add new ones, ... */
  126. /* USER CODE END RTOS_TIMERS */
  127. /* USER CODE BEGIN RTOS_QUEUES */
  128. /* add queues, ... */
  129. /* USER CODE END RTOS_QUEUES */
  130. /* Create the thread(s) */
  131. /* creation of tinyusb */
  132. tinyusbHandle = osThreadNew(tinyusb_task, NULL, &tinyusb_attributes);
  133. /* creation of ide */
  134. ideHandle = osThreadNew(ide_task, NULL, &ide_attributes);
  135. /* creation of cdc */
  136. cdcHandle = osThreadNew(cdc_task, NULL, &cdc_attributes);
  137. /* USER CODE BEGIN RTOS_THREADS */
  138. /* add threads, ... */
  139. /* USER CODE END RTOS_THREADS */
  140. /* USER CODE BEGIN RTOS_EVENTS */
  141. /* add events, ... */
  142. /* USER CODE END RTOS_EVENTS */
  143. /* Start scheduler */
  144. osKernelStart();
  145. /* We should never get here as control is now taken by the scheduler */
  146. /* Infinite loop */
  147. /* USER CODE BEGIN WHILE */
  148. while (1)
  149. {
  150. /* USER CODE END WHILE */
  151. /* USER CODE BEGIN 3 */
  152. }
  153. /* USER CODE END 3 */
  154. }
  155. /**
  156. * @brief System Clock Configuration
  157. * @retval None
  158. */
  159. void SystemClock_Config(void)
  160. {
  161. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  162. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  163. /** Configure LSE Drive Capability
  164. */
  165. HAL_PWR_EnableBkUpAccess();
  166. __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
  167. /** Configure the main internal regulator output voltage
  168. */
  169. __HAL_RCC_PWR_CLK_ENABLE();
  170. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  171. /** Initializes the RCC Oscillators according to the specified parameters
  172. * in the RCC_OscInitTypeDef structure.
  173. */
  174. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  175. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  176. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  177. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  178. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  179. RCC_OscInitStruct.PLL.PLLM = 8;
  180. RCC_OscInitStruct.PLL.PLLN = 216;
  181. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  182. RCC_OscInitStruct.PLL.PLLQ = 9;
  183. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  184. {
  185. Error_Handler();
  186. }
  187. /** Activate the Over-Drive mode
  188. */
  189. if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  190. {
  191. Error_Handler();
  192. }
  193. /** Initializes the CPU, AHB and APB buses clocks
  194. */
  195. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  196. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  197. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  198. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  199. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  200. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  201. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  202. {
  203. Error_Handler();
  204. }
  205. }
  206. /**
  207. * @brief RTC Initialization Function
  208. * @param None
  209. * @retval None
  210. */
  211. static void MX_RTC_Init(void)
  212. {
  213. /* USER CODE BEGIN RTC_Init 0 */
  214. /* USER CODE END RTC_Init 0 */
  215. /* USER CODE BEGIN RTC_Init 1 */
  216. /* USER CODE END RTC_Init 1 */
  217. /** Initialize RTC Only
  218. */
  219. hrtc.Instance = RTC;
  220. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  221. hrtc.Init.AsynchPrediv = 127;
  222. hrtc.Init.SynchPrediv = 255;
  223. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  224. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  225. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  226. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  227. {
  228. Error_Handler();
  229. }
  230. /* USER CODE BEGIN RTC_Init 2 */
  231. /* USER CODE END RTC_Init 2 */
  232. }
  233. /**
  234. * @brief USART2 Initialization Function
  235. * @param None
  236. * @retval None
  237. */
  238. static void MX_USART2_UART_Init(void)
  239. {
  240. /* USER CODE BEGIN USART2_Init 0 */
  241. /* USER CODE END USART2_Init 0 */
  242. /* USER CODE BEGIN USART2_Init 1 */
  243. /* USER CODE END USART2_Init 1 */
  244. huart2.Instance = USART2;
  245. huart2.Init.BaudRate = 115200;
  246. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  247. huart2.Init.StopBits = UART_STOPBITS_1;
  248. huart2.Init.Parity = UART_PARITY_NONE;
  249. huart2.Init.Mode = UART_MODE_TX_RX;
  250. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  251. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  252. huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  253. huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  254. if (HAL_UART_Init(&huart2) != HAL_OK)
  255. {
  256. Error_Handler();
  257. }
  258. /* USER CODE BEGIN USART2_Init 2 */
  259. /* USER CODE END USART2_Init 2 */
  260. }
  261. /**
  262. * @brief USB_OTG_HS Initialization Function
  263. * @param None
  264. * @retval None
  265. */
  266. static void MX_USB_OTG_HS_PCD_Init(void)
  267. {
  268. /* USER CODE BEGIN USB_OTG_HS_Init 0 */
  269. /* USER CODE END USB_OTG_HS_Init 0 */
  270. /* USER CODE BEGIN USB_OTG_HS_Init 1 */
  271. /* USER CODE END USB_OTG_HS_Init 1 */
  272. hpcd_USB_OTG_HS.Instance = USB_OTG_HS;
  273. hpcd_USB_OTG_HS.Init.dev_endpoints = 9;
  274. hpcd_USB_OTG_HS.Init.dma_enable = DISABLE;
  275. hpcd_USB_OTG_HS.Init.phy_itface = USB_OTG_HS_EMBEDDED_PHY;
  276. hpcd_USB_OTG_HS.Init.Sof_enable = DISABLE;
  277. hpcd_USB_OTG_HS.Init.low_power_enable = DISABLE;
  278. hpcd_USB_OTG_HS.Init.lpm_enable = DISABLE;
  279. hpcd_USB_OTG_HS.Init.vbus_sensing_enable = DISABLE;
  280. hpcd_USB_OTG_HS.Init.use_dedicated_ep1 = DISABLE;
  281. hpcd_USB_OTG_HS.Init.use_external_vbus = DISABLE;
  282. if (HAL_PCD_Init(&hpcd_USB_OTG_HS) != HAL_OK)
  283. {
  284. Error_Handler();
  285. }
  286. /* USER CODE BEGIN USB_OTG_HS_Init 2 */
  287. /* USER CODE END USB_OTG_HS_Init 2 */
  288. }
  289. /**
  290. * @brief GPIO Initialization Function
  291. * @param None
  292. * @retval None
  293. */
  294. static void MX_GPIO_Init(void)
  295. {
  296. GPIO_InitTypeDef GPIO_InitStruct = {0};
  297. /* USER CODE BEGIN MX_GPIO_Init_1 */
  298. /* USER CODE END MX_GPIO_Init_1 */
  299. /* GPIO Ports Clock Enable */
  300. __HAL_RCC_GPIOE_CLK_ENABLE();
  301. __HAL_RCC_GPIOC_CLK_ENABLE();
  302. __HAL_RCC_GPIOH_CLK_ENABLE();
  303. __HAL_RCC_GPIOA_CLK_ENABLE();
  304. __HAL_RCC_GPIOB_CLK_ENABLE();
  305. __HAL_RCC_GPIOD_CLK_ENABLE();
  306. /*Configure GPIO pin Output Level */
  307. HAL_GPIO_WritePin(GPIOC, IDE_DIOR_Pin|IDE_CS0_Pin|IDE_CS1_Pin|IDE_DIOW_Pin, GPIO_PIN_RESET);
  308. /*Configure GPIO pin Output Level */
  309. HAL_GPIO_WritePin(GPIOB, LED_2_Pin|LED_3_Pin|IDE_RESET_Pin|LED_1_Pin
  310. |TXS0108E_OE2_Pin|TXS0108E_OE_Pin|LED_4_Pin, GPIO_PIN_RESET);
  311. /*Configure GPIO pin Output Level */
  312. HAL_GPIO_WritePin(GPIOD, IDE_DA0_Pin|IDE_DA1_Pin|IDE_DA2_Pin, GPIO_PIN_RESET);
  313. /*Configure GPIO pins : IDE_DD4_Pin IDE_DD5_Pin IDE_DD6_Pin IDE_DD7_Pin
  314. IDE_DD8_Pin IDE_DD9_Pin IDE_DD10_Pin IDE_DD11_Pin
  315. IDE_DD12_Pin IDE_DD13_Pin IDE_DD14_Pin IDE_DD15_Pin */
  316. GPIO_InitStruct.Pin = IDE_DD4_Pin|IDE_DD5_Pin|IDE_DD6_Pin|IDE_DD7_Pin
  317. |IDE_DD8_Pin|IDE_DD9_Pin|IDE_DD10_Pin|IDE_DD11_Pin
  318. |IDE_DD12_Pin|IDE_DD13_Pin|IDE_DD14_Pin|IDE_DD15_Pin;
  319. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  320. GPIO_InitStruct.Pull = GPIO_NOPULL;
  321. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  322. /*Configure GPIO pins : IDE_DIOR_Pin IDE_CS0_Pin IDE_CS1_Pin IDE_DIOW_Pin */
  323. GPIO_InitStruct.Pin = IDE_DIOR_Pin|IDE_CS0_Pin|IDE_CS1_Pin|IDE_DIOW_Pin;
  324. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  325. GPIO_InitStruct.Pull = GPIO_NOPULL;
  326. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  327. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  328. /*Configure GPIO pins : LED_2_Pin LED_3_Pin LED_1_Pin TXS0108E_OE2_Pin
  329. TXS0108E_OE_Pin LED_4_Pin */
  330. GPIO_InitStruct.Pin = LED_2_Pin|LED_3_Pin|LED_1_Pin|TXS0108E_OE2_Pin
  331. |TXS0108E_OE_Pin|LED_4_Pin;
  332. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  333. GPIO_InitStruct.Pull = GPIO_NOPULL;
  334. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  335. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  336. /*Configure GPIO pin : IDE_RESET_Pin */
  337. GPIO_InitStruct.Pin = IDE_RESET_Pin;
  338. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  339. GPIO_InitStruct.Pull = GPIO_NOPULL;
  340. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  341. HAL_GPIO_Init(IDE_RESET_GPIO_Port, &GPIO_InitStruct);
  342. /*Configure GPIO pins : IDE_DA0_Pin IDE_DA1_Pin IDE_DA2_Pin */
  343. GPIO_InitStruct.Pin = IDE_DA0_Pin|IDE_DA1_Pin|IDE_DA2_Pin;
  344. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  345. GPIO_InitStruct.Pull = GPIO_NOPULL;
  346. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  347. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  348. /*Configure GPIO pins : IDE_DD0_Pin IDE_DD1_Pin IDE_DD2_Pin IDE_DD3_Pin */
  349. GPIO_InitStruct.Pin = IDE_DD0_Pin|IDE_DD1_Pin|IDE_DD2_Pin|IDE_DD3_Pin;
  350. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  351. GPIO_InitStruct.Pull = GPIO_NOPULL;
  352. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  353. /* USER CODE BEGIN MX_GPIO_Init_2 */
  354. /* USER CODE END MX_GPIO_Init_2 */
  355. }
  356. /* USER CODE BEGIN 4 */
  357. /* USER CODE END 4 */
  358. /* USER CODE BEGIN Header_tinyusb_task */
  359. /**
  360. * @brief Function implementing the tinyusb thread.
  361. * @param argument: Not used
  362. * @retval None
  363. */
  364. /* USER CODE END Header_tinyusb_task */
  365. void tinyusb_task(void *argument)
  366. {
  367. /* USER CODE BEGIN 5 */
  368. tusb_init();
  369. /* Infinite loop */
  370. while(1) {
  371. tud_task();
  372. }
  373. /* USER CODE END 5 */
  374. }
  375. void cdc_task(void *argument)
  376. {
  377. /* USER CODE BEGIN 5 */
  378. /* Infinite loop */
  379. while(1) {
  380. cdctask();
  381. }
  382. /* USER CODE END 5 */
  383. }
  384. /* USER CODE BEGIN Header_ide_task */
  385. /**
  386. * @brief Function implementing the ide thread.
  387. * @param argument: Not used
  388. * @retval None
  389. */
  390. /* USER CODE END Header_ide_task */
  391. void ide_task(void *argument)
  392. {
  393. /* USER CODE BEGIN ide_task */
  394. ide_async_init();
  395. /* Infinite loop */
  396. while (1) {
  397. ide_async_main_loop_step();
  398. }
  399. /* USER CODE END ide_task */
  400. }
  401. /**
  402. * @brief This function is executed in case of error occurrence.
  403. * @retval None
  404. */
  405. void Error_Handler(void)
  406. {
  407. /* USER CODE BEGIN Error_Handler_Debug */
  408. /* User can add his own implementation to report the HAL error return state */
  409. __disable_irq();
  410. while (1)
  411. {
  412. }
  413. /* USER CODE END Error_Handler_Debug */
  414. }
  415. #ifdef USE_FULL_ASSERT
  416. /**
  417. * @brief Reports the name of the source file and the source line number
  418. * where the assert_param error has occurred.
  419. * @param file: pointer to the source file name
  420. * @param line: assert_param error line source number
  421. * @retval None
  422. */
  423. void assert_failed(uint8_t *file, uint32_t line)
  424. {
  425. /* USER CODE BEGIN 6 */
  426. /* User can add his own implementation to report the file name and line number,
  427. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  428. /* USER CODE END 6 */
  429. }
  430. #endif /* USE_FULL_ASSERT */