same54xplainedpro.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2021 Jean Gressmann <jean@0x42.de>
  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. */
  25. #include <sam.h>
  26. #include "bsp/board.h"
  27. #include <hal/include/hal_gpio.h>
  28. //--------------------------------------------------------------------+
  29. // Forward USB interrupt events to TinyUSB IRQ Handler
  30. //--------------------------------------------------------------------+
  31. void USB_0_Handler(void)
  32. {
  33. tud_int_handler(0);
  34. }
  35. void USB_1_Handler(void)
  36. {
  37. tud_int_handler(0);
  38. }
  39. void USB_2_Handler(void)
  40. {
  41. tud_int_handler(0);
  42. }
  43. void USB_3_Handler(void)
  44. {
  45. tud_int_handler(0);
  46. }
  47. //--------------------------------------------------------------------+
  48. // MACRO TYPEDEF CONSTANT ENUM DECLARATION
  49. //--------------------------------------------------------------------+
  50. #define LED_PIN PIN_PC18
  51. #define BUTTON_PIN PIN_PB31
  52. #define BOARD_SERCOM SERCOM2
  53. #define BOARD_NAME "Microchip SAM E54 Xplained Pro"
  54. static inline void init_clock(void)
  55. {
  56. /* configure for a 12MHz crystal connected to XIN1/XOUT1 */
  57. OSCCTRL->XOSCCTRL[1].reg =
  58. OSCCTRL_XOSCCTRL_STARTUP(6) | // 1.953 ms
  59. OSCCTRL_XOSCCTRL_RUNSTDBY |
  60. OSCCTRL_XOSCCTRL_ENALC |
  61. OSCCTRL_XOSCCTRL_IMULT(4) | OSCCTRL_XOSCCTRL_IPTAT(3) | // 8MHz to 16MHz
  62. OSCCTRL_XOSCCTRL_XTALEN |
  63. OSCCTRL_XOSCCTRL_ENABLE;
  64. while(0 == OSCCTRL->STATUS.bit.XOSCRDY1);
  65. OSCCTRL->Dpll[0].DPLLCTRLB.reg = OSCCTRL_DPLLCTRLB_DIV(5) | OSCCTRL_DPLLCTRLB_REFCLK_XOSC1; /* 12MHz / 12 = 1Mhz, input = XOSC1 */
  66. OSCCTRL->Dpll[0].DPLLRATIO.reg = OSCCTRL_DPLLRATIO_LDRFRAC(0x0) | OSCCTRL_DPLLRATIO_LDR((CONF_CPU_FREQUENCY / 1000000)-1); /* multiply to get CONF_CPU_FREQUENCY (default = 120MHz) */
  67. OSCCTRL->Dpll[0].DPLLCTRLA.reg = OSCCTRL_DPLLCTRLA_RUNSTDBY | OSCCTRL_DPLLCTRLA_ENABLE;
  68. while(0 == OSCCTRL->Dpll[0].DPLLSTATUS.bit.CLKRDY); /* wait for the PLL0 to be ready */
  69. OSCCTRL->Dpll[1].DPLLCTRLB.reg = OSCCTRL_DPLLCTRLB_DIV(5) | OSCCTRL_DPLLCTRLB_REFCLK_XOSC1; /* 12MHz / 12 = 1Mhz, input = XOSC1 */
  70. OSCCTRL->Dpll[1].DPLLRATIO.reg = OSCCTRL_DPLLRATIO_LDRFRAC(0x0) | OSCCTRL_DPLLRATIO_LDR(47); /* multiply by 48 -> 48 MHz */
  71. OSCCTRL->Dpll[1].DPLLCTRLA.reg = OSCCTRL_DPLLCTRLA_RUNSTDBY | OSCCTRL_DPLLCTRLA_ENABLE;
  72. while(0 == OSCCTRL->Dpll[1].DPLLSTATUS.bit.CLKRDY); /* wait for the PLL1 to be ready */
  73. /* configure clock-generator 0 to use DPLL0 as source -> GCLK0 is used for the core */
  74. GCLK->GENCTRL[0].reg =
  75. GCLK_GENCTRL_DIV(0) |
  76. GCLK_GENCTRL_RUNSTDBY |
  77. GCLK_GENCTRL_GENEN |
  78. GCLK_GENCTRL_SRC_DPLL0 |
  79. GCLK_GENCTRL_IDC;
  80. while(1 == GCLK->SYNCBUSY.bit.GENCTRL0); /* wait for the synchronization between clock domains to be complete */
  81. /* configure clock-generator 1 to use DPLL1 as source -> for use with some peripheral */
  82. GCLK->GENCTRL[1].reg =
  83. GCLK_GENCTRL_DIV(0) |
  84. GCLK_GENCTRL_RUNSTDBY |
  85. GCLK_GENCTRL_GENEN |
  86. GCLK_GENCTRL_SRC_DPLL1 |
  87. GCLK_GENCTRL_IDC ;
  88. while(1 == GCLK->SYNCBUSY.bit.GENCTRL1); /* wait for the synchronization between clock domains to be complete */
  89. }
  90. static inline void uart_init(void)
  91. {
  92. gpio_set_pin_function(PIN_PB24, PINMUX_PB24D_SERCOM2_PAD1);
  93. gpio_set_pin_function(PIN_PB25, PINMUX_PB25D_SERCOM2_PAD0);
  94. MCLK->APBBMASK.bit.SERCOM2_ = 1;
  95. GCLK->PCHCTRL[SERCOM2_GCLK_ID_CORE].reg = GCLK_PCHCTRL_GEN_GCLK1 | GCLK_PCHCTRL_CHEN; /* setup SERCOM to use GLCK1 -> 48MHz */
  96. BOARD_SERCOM->USART.CTRLA.bit.SWRST = 1; /* reset and disable SERCOM -> enable configuration */
  97. while (BOARD_SERCOM->USART.SYNCBUSY.bit.SWRST);
  98. BOARD_SERCOM->USART.CTRLA.reg =
  99. SERCOM_USART_CTRLA_SAMPR(0) | /* 0 = 16x / arithmetic baud rate, 1 = 16x / fractional baud rate */
  100. SERCOM_USART_CTRLA_SAMPA(0) | /* 16x over sampling */
  101. SERCOM_USART_CTRLA_FORM(0) | /* 0x0 USART frame, 0x1 USART frame with parity, ... */
  102. SERCOM_USART_CTRLA_DORD | /* LSB first */
  103. SERCOM_USART_CTRLA_MODE(1) | /* 0x0 USART with external clock, 0x1 USART with internal clock */
  104. SERCOM_USART_CTRLA_RXPO(1) | /* SERCOM PAD[1] is used for data reception */
  105. SERCOM_USART_CTRLA_TXPO(0); /* SERCOM PAD[0] is used for data transmission */
  106. BOARD_SERCOM->USART.CTRLB.reg = /* RXEM = 0 -> receiver disabled, LINCMD = 0 -> normal USART transmission, SFDE = 0 -> start-of-frame detection disabled, SBMODE = 0 -> one stop bit, CHSIZE = 0 -> 8 bits */
  107. SERCOM_USART_CTRLB_TXEN | /* transmitter enabled */
  108. SERCOM_USART_CTRLB_RXEN; /* receiver enabled */
  109. // BOARD_SERCOM->USART.BAUD.reg = SERCOM_USART_BAUD_FRAC_FP(0) | SERCOM_USART_BAUD_FRAC_BAUD(26); /* 48000000/(16*115200) = 26.041666667 */
  110. BOARD_SERCOM->USART.BAUD.reg = SERCOM_USART_BAUD_BAUD(63019); /* 65536*(1−16*115200/48000000) */
  111. BOARD_SERCOM->SPI.CTRLA.bit.ENABLE = 1; /* activate SERCOM */
  112. while (BOARD_SERCOM->USART.SYNCBUSY.bit.ENABLE); /* wait for SERCOM to be ready */
  113. }
  114. static inline void uart_send_buffer(uint8_t const *text, size_t len)
  115. {
  116. for (size_t i = 0; i < len; ++i) {
  117. BOARD_SERCOM->USART.DATA.reg = text[i];
  118. while((BOARD_SERCOM->USART.INTFLAG.reg & SERCOM_SPI_INTFLAG_TXC) == 0);
  119. }
  120. }
  121. static inline void uart_send_str(const char* text)
  122. {
  123. while (*text) {
  124. BOARD_SERCOM->USART.DATA.reg = *text++;
  125. while((BOARD_SERCOM->USART.INTFLAG.reg & SERCOM_SPI_INTFLAG_TXC) == 0);
  126. }
  127. }
  128. void board_init(void)
  129. {
  130. init_clock();
  131. SystemCoreClock = CONF_CPU_FREQUENCY;
  132. #if CFG_TUSB_OS == OPT_OS_NONE
  133. SysTick_Config(CONF_CPU_FREQUENCY / 1000);
  134. #endif
  135. uart_init();
  136. #if CFG_TUSB_DEBUG >= 2
  137. uart_send_str(BOARD_NAME " UART initialized\n");
  138. tu_printf(BOARD_NAME " reset cause %#02x\n", RSTC->RCAUSE.reg);
  139. #endif
  140. // LED0 init
  141. gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
  142. gpio_set_pin_level(LED_PIN, 0);
  143. #if CFG_TUSB_DEBUG >= 2
  144. uart_send_str(BOARD_NAME " LED pin configured\n");
  145. #endif
  146. // BTN0 init
  147. gpio_set_pin_function(BUTTON_PIN, GPIO_DIRECTION_IN);
  148. gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
  149. gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
  150. #if CFG_TUSB_DEBUG >= 2
  151. uart_send_str(BOARD_NAME " Button pin configured\n");
  152. #endif
  153. #if CFG_TUSB_OS == OPT_OS_FREERTOS
  154. // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
  155. NVIC_SetPriority(USB_0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
  156. NVIC_SetPriority(USB_1_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
  157. NVIC_SetPriority(USB_2_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
  158. NVIC_SetPriority(USB_3_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
  159. #endif
  160. #if TUSB_OPT_DEVICE_ENABLED
  161. #if CFG_TUSB_DEBUG >= 2
  162. uart_send_str(BOARD_NAME " USB device enabled\n");
  163. #endif
  164. /* USB clock init
  165. * The USB module requires a GCLK_USB of 48 MHz ~ 0.25% clock
  166. * for low speed and full speed operation.
  167. */
  168. hri_gclk_write_PCHCTRL_reg(GCLK, USB_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK1_Val | GCLK_PCHCTRL_CHEN);
  169. hri_mclk_set_AHBMASK_USB_bit(MCLK);
  170. hri_mclk_set_APBBMASK_USB_bit(MCLK);
  171. // USB pin init
  172. gpio_set_pin_direction(PIN_PA24, GPIO_DIRECTION_OUT);
  173. gpio_set_pin_level(PIN_PA24, false);
  174. gpio_set_pin_pull_mode(PIN_PA24, GPIO_PULL_OFF);
  175. gpio_set_pin_direction(PIN_PA25, GPIO_DIRECTION_OUT);
  176. gpio_set_pin_level(PIN_PA25, false);
  177. gpio_set_pin_pull_mode(PIN_PA25, GPIO_PULL_OFF);
  178. gpio_set_pin_function(PIN_PA24, PINMUX_PA24H_USB_DM);
  179. gpio_set_pin_function(PIN_PA25, PINMUX_PA25H_USB_DP);
  180. #if CFG_TUSB_DEBUG >= 2
  181. uart_send_str(BOARD_NAME " USB device configured\n");
  182. #endif
  183. #endif
  184. }
  185. //--------------------------------------------------------------------+
  186. // Board porting API
  187. //--------------------------------------------------------------------+
  188. void board_led_write(bool state)
  189. {
  190. gpio_set_pin_level(LED_PIN, state);
  191. }
  192. uint32_t board_button_read(void)
  193. {
  194. return (PORT->Group[1].IN.reg & 0x80000000) != 0x80000000;
  195. }
  196. int board_uart_read(uint8_t* buf, int len)
  197. {
  198. (void) buf; (void) len;
  199. return 0;
  200. }
  201. int board_uart_write(void const * buf, int len)
  202. {
  203. if (len < 0) {
  204. uart_send_str(buf);
  205. } else {
  206. uart_send_buffer(buf, len);
  207. }
  208. return len;
  209. }
  210. #if CFG_TUSB_OS == OPT_OS_NONE
  211. volatile uint32_t system_ticks = 0;
  212. void SysTick_Handler(void)
  213. {
  214. system_ticks++;
  215. }
  216. uint32_t board_millis(void)
  217. {
  218. return system_ticks;
  219. }
  220. #endif
  221. // Required by __libc_init_array in startup code if we are compiling using
  222. // -nostdlib/-nostartfiles.
  223. void _init(void)
  224. {
  225. }