double_m33_express.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2018, hathach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. * This file is part of the TinyUSB stack.
  25. */
  26. #include "../board.h"
  27. #include "fsl_device_registers.h"
  28. #include "fsl_gpio.h"
  29. #include "fsl_power.h"
  30. #include "fsl_iocon.h"
  31. #include "fsl_usart.h"
  32. #include "fsl_sctimer.h"
  33. #include "sct_neopixel.h"
  34. //--------------------------------------------------------------------+
  35. // Forward USB interrupt events to TinyUSB IRQ Handler
  36. //--------------------------------------------------------------------+
  37. void USB0_IRQHandler(void)
  38. {
  39. tud_int_handler(0);
  40. }
  41. void USB1_IRQHandler(void)
  42. {
  43. tud_int_handler(1);
  44. }
  45. //--------------------------------------------------------------------+
  46. // MACRO TYPEDEF CONSTANT ENUM
  47. //--------------------------------------------------------------------+
  48. #define LED_PORT 0
  49. #define LED_PIN 1
  50. #define LED_STATE_ON 1
  51. // WAKE button
  52. #define BUTTON_PORT 0
  53. #define BUTTON_PIN 5
  54. #define BUTTON_STATE_ACTIVE 0
  55. // Number of neopixels
  56. #define NEOPIXEL_NUMBER 2
  57. #define NEOPIXEL_PORT 0
  58. #define NEOPIXEL_PIN 27
  59. #define NEOPIXEL_CH 6
  60. #define NEOPIXEL_TYPE 0
  61. // UART
  62. #define UART_DEV USART0
  63. // IOCON pin mux
  64. #define IOCON_PIO_DIGITAL_EN 0x0100u /*!<@brief Enables digital function */
  65. #define IOCON_PIO_FUNC0 0x00u /*!<@brief Selects pin function 0 */
  66. #define IOCON_PIO_FUNC1 0x01u /*!<@brief Selects pin function 1 */
  67. #define IOCON_PIO_FUNC4 0x04u /*!<@brief Selects pin function 4 */
  68. #define IOCON_PIO_FUNC7 0x07u /*!<@brief Selects pin function 7 */
  69. #define IOCON_PIO_INV_DI 0x00u /*!<@brief Input function is not inverted */
  70. #define IOCON_PIO_MODE_INACT 0x00u /*!<@brief No addition pin function */
  71. #define IOCON_PIO_OPENDRAIN_DI 0x00u /*!<@brief Open drain is disabled */
  72. #define IOCON_PIO_SLEW_STANDARD 0x00u /*!<@brief Standard mode, output slew rate control is enabled */
  73. #define IOCON_PIO_DIG_FUNC0_EN (IOCON_PIO_DIGITAL_EN | IOCON_PIO_FUNC0) /*!<@brief Digital pin function 0 enabled */
  74. #define IOCON_PIO_DIG_FUNC1_EN (IOCON_PIO_DIGITAL_EN | IOCON_PIO_FUNC1) /*!<@brief Digital pin function 1 enabled */
  75. #define IOCON_PIO_DIG_FUNC4_EN (IOCON_PIO_DIGITAL_EN | IOCON_PIO_FUNC4) /*!<@brief Digital pin function 2 enabled */
  76. #define IOCON_PIO_DIG_FUNC7_EN (IOCON_PIO_DIGITAL_EN | IOCON_PIO_FUNC7) /*!<@brief Digital pin function 2 enabled */
  77. // Global Variables
  78. uint32_t pixelData[NEOPIXEL_NUMBER];
  79. /****************************************************************
  80. name: BOARD_BootClockFROHF96M
  81. outputs:
  82. - {id: SYSTICK_clock.outFreq, value: 96 MHz}
  83. - {id: System_clock.outFreq, value: 96 MHz}
  84. settings:
  85. - {id: SYSCON.MAINCLKSELA.sel, value: SYSCON.fro_hf}
  86. sources:
  87. - {id: SYSCON.fro_hf.outFreq, value: 96 MHz}
  88. ******************************************************************/
  89. void BootClockFROHF96M(void)
  90. {
  91. /*!< Set up the clock sources */
  92. /*!< Set up FRO */
  93. POWER_DisablePD(kPDRUNCFG_PD_FRO192M); /*!< Ensure FRO is on */
  94. CLOCK_SetupFROClocking(12000000U); /*!< Set up FRO to the 12 MHz, just for sure */
  95. CLOCK_AttachClk(kFRO12M_to_MAIN_CLK); /*!< Switch to FRO 12MHz first to ensure we can change voltage without
  96. accidentally being below the voltage for current speed */
  97. CLOCK_SetupFROClocking(96000000U); /*!< Set up high frequency FRO output to selected frequency */
  98. POWER_SetVoltageForFreq(96000000U); /*!< Set voltage for the one of the fastest clock outputs: System clock output */
  99. CLOCK_SetFLASHAccessCyclesForFreq(96000000U); /*!< Set FLASH wait states for core */
  100. /*!< Set up dividers */
  101. CLOCK_SetClkDiv(kCLOCK_DivAhbClk, 1U, false); /*!< Set AHBCLKDIV divider to value 1 */
  102. /*!< Set up clock selectors - Attach clocks to the peripheries */
  103. CLOCK_AttachClk(kFRO_HF_to_MAIN_CLK); /*!< Switch MAIN_CLK to FRO_HF */
  104. /*!< Set SystemCoreClock variable. */
  105. SystemCoreClock = 96000000U;
  106. }
  107. void board_init(void)
  108. {
  109. // Enable IOCON clock
  110. CLOCK_EnableClock(kCLOCK_Iocon);
  111. // Init 96 MHz clock
  112. BootClockFROHF96M();
  113. #if CFG_TUSB_OS == OPT_OS_NONE
  114. // 1ms tick timer
  115. SysTick_Config(SystemCoreClock / 1000);
  116. #elif CFG_TUSB_OS == OPT_OS_FREERTOS
  117. // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
  118. NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
  119. #endif
  120. GPIO_PortInit(GPIO, 0);
  121. GPIO_PortInit(GPIO, 1);
  122. // LED
  123. /* PORT0 PIN1 configured as PIO0_1 */
  124. IOCON_PinMuxSet(IOCON, 0U, 1U, IOCON_PIO_DIG_FUNC0_EN);
  125. gpio_pin_config_t const led_config = { kGPIO_DigitalOutput, 1};
  126. GPIO_PinInit(GPIO, LED_PORT, LED_PIN, &led_config);
  127. // Neopixel
  128. /* PORT0 PIN27 configured as SCT0_OUT6 */
  129. IOCON_PinMuxSet(IOCON, NEOPIXEL_PORT, NEOPIXEL_PIN, IOCON_PIO_DIG_FUNC4_EN);
  130. sctpix_init(NEOPIXEL_TYPE);
  131. sctpix_addCh(NEOPIXEL_CH, pixelData, NEOPIXEL_NUMBER);
  132. sctpix_setPixel(NEOPIXEL_CH, 0, 0x100010);
  133. sctpix_setPixel(NEOPIXEL_CH, 1, 0x100010);
  134. sctpix_show();
  135. // Button
  136. /* PORT0 PIN5 configured as PIO0_5 */
  137. IOCON_PinMuxSet(IOCON, BUTTON_PORT, BUTTON_PIN, IOCON_PIO_DIG_FUNC0_EN);
  138. gpio_pin_config_t const button_config = { kGPIO_DigitalInput, 0};
  139. GPIO_PinInit(GPIO, BUTTON_PORT, BUTTON_PIN, &button_config);
  140. // UART
  141. /* PORT0 PIN29 (coords: 92) is configured as FC0_RXD_SDA_MOSI_DATA */
  142. IOCON_PinMuxSet(IOCON, 0U, 29U, IOCON_PIO_DIG_FUNC1_EN);
  143. /* PORT0 PIN30 (coords: 94) is configured as FC0_TXD_SCL_MISO_WS */
  144. IOCON_PinMuxSet(IOCON, 0U, 30U, IOCON_PIO_DIG_FUNC1_EN);
  145. #if defined(UART_DEV) && CFG_TUSB_DEBUG
  146. // Enable UART when debug log is on
  147. CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
  148. usart_config_t uart_config;
  149. USART_GetDefaultConfig(&uart_config);
  150. uart_config.baudRate_Bps = CFG_BOARD_UART_BAUDRATE;
  151. uart_config.enableTx = true;
  152. uart_config.enableRx = true;
  153. USART_Init(UART_DEV, &uart_config, 12000000);
  154. #endif
  155. // USB VBUS
  156. /* PORT0 PIN22 configured as USB0_VBUS */
  157. IOCON_PinMuxSet(IOCON, 0U, 22U, IOCON_PIO_DIG_FUNC7_EN);
  158. // USB Controller
  159. POWER_DisablePD(kPDRUNCFG_PD_USB0_PHY); /*Turn on USB0 Phy */
  160. POWER_DisablePD(kPDRUNCFG_PD_USB1_PHY); /*< Turn on USB1 Phy */
  161. /* reset the IP to make sure it's in reset state. */
  162. RESET_PeripheralReset(kUSB0D_RST_SHIFT_RSTn);
  163. RESET_PeripheralReset(kUSB0HSL_RST_SHIFT_RSTn);
  164. RESET_PeripheralReset(kUSB0HMR_RST_SHIFT_RSTn);
  165. RESET_PeripheralReset(kUSB1H_RST_SHIFT_RSTn);
  166. RESET_PeripheralReset(kUSB1D_RST_SHIFT_RSTn);
  167. RESET_PeripheralReset(kUSB1_RST_SHIFT_RSTn);
  168. RESET_PeripheralReset(kUSB1RAM_RST_SHIFT_RSTn);
  169. #if (defined CFG_TUSB_RHPORT1_MODE) && (CFG_TUSB_RHPORT1_MODE & OPT_MODE_DEVICE)
  170. CLOCK_EnableClock(kCLOCK_Usbh1);
  171. /* Put PHY powerdown under software control */
  172. USBHSH->PORTMODE = USBHSH_PORTMODE_SW_PDCOM_MASK;
  173. /* According to reference mannual, device mode setting has to be set by access usb host register */
  174. USBHSH->PORTMODE |= USBHSH_PORTMODE_DEV_ENABLE_MASK;
  175. /* enable usb1 host clock */
  176. CLOCK_DisableClock(kCLOCK_Usbh1);
  177. #endif
  178. #if (defined CFG_TUSB_RHPORT0_MODE) && (CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE)
  179. // Enable USB Clock Adjustments to trim the FRO for the full speed controller
  180. ANACTRL->FRO192M_CTRL |= ANACTRL_FRO192M_CTRL_USBCLKADJ_MASK;
  181. CLOCK_SetClkDiv(kCLOCK_DivUsb0Clk, 1, false);
  182. CLOCK_AttachClk(kFRO_HF_to_USB0_CLK);
  183. /* enable usb0 host clock */
  184. CLOCK_EnableClock(kCLOCK_Usbhsl0);
  185. /*According to reference mannual, device mode setting has to be set by access usb host register */
  186. USBFSH->PORTMODE |= USBFSH_PORTMODE_DEV_ENABLE_MASK;
  187. /* disable usb0 host clock */
  188. CLOCK_DisableClock(kCLOCK_Usbhsl0);
  189. CLOCK_EnableUsbfs0DeviceClock(kCLOCK_UsbfsSrcFro, CLOCK_GetFreq(kCLOCK_FroHf)); /* enable USB Device clock */
  190. #endif
  191. }
  192. //--------------------------------------------------------------------+
  193. // Board porting API
  194. //--------------------------------------------------------------------+
  195. void board_led_write(bool state)
  196. {
  197. GPIO_PinWrite(GPIO, LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
  198. if (state) {
  199. sctpix_setPixel(NEOPIXEL_CH, 0, 0x100000);
  200. sctpix_setPixel(NEOPIXEL_CH, 1, 0x101010);
  201. } else {
  202. sctpix_setPixel(NEOPIXEL_CH, 0, 0x001000);
  203. sctpix_setPixel(NEOPIXEL_CH, 1, 0x000010);
  204. }
  205. sctpix_show();
  206. }
  207. uint32_t board_button_read(void)
  208. {
  209. // active low
  210. return BUTTON_STATE_ACTIVE == GPIO_PinRead(GPIO, BUTTON_PORT, BUTTON_PIN);
  211. }
  212. int board_uart_read(uint8_t* buf, int len)
  213. {
  214. (void) buf; (void) len;
  215. return 0;
  216. }
  217. int board_uart_write(void const * buf, int len)
  218. {
  219. (void) buf; (void) len;
  220. return 0;
  221. }
  222. #if CFG_TUSB_OS == OPT_OS_NONE
  223. volatile uint32_t system_ticks = 0;
  224. void SysTick_Handler(void)
  225. {
  226. system_ticks++;
  227. }
  228. uint32_t board_millis(void)
  229. {
  230. return system_ticks;
  231. }
  232. #endif