family.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (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 "sam.h"
  27. #include "bsp/board.h"
  28. #include "board.h"
  29. #include "hal/include/hal_gpio.h"
  30. #include "hal/include/hal_init.h"
  31. #include "hri/hri_nvmctrl_d21.h"
  32. #include "hpl/gclk/hpl_gclk_base.h"
  33. #include "hpl_pm_config.h"
  34. #include "hpl/pm/hpl_pm_base.h"
  35. //--------------------------------------------------------------------+
  36. // Forward USB interrupt events to TinyUSB IRQ Handler
  37. //--------------------------------------------------------------------+
  38. void USB_Handler(void)
  39. {
  40. tud_int_handler(0);
  41. }
  42. //--------------------------------------------------------------------+
  43. // UART support
  44. //--------------------------------------------------------------------+
  45. static void uart_init(void);
  46. //--------------------------------------------------------------------+
  47. // MACRO TYPEDEF CONSTANT ENUM DECLARATION
  48. //--------------------------------------------------------------------+
  49. /* Referenced GCLKs, should be initialized firstly */
  50. #define _GCLK_INIT_1ST (1 << 0 | 1 << 1)
  51. /* Not referenced GCLKs, initialized last */
  52. #define _GCLK_INIT_LAST (~_GCLK_INIT_1ST)
  53. void board_init(void)
  54. {
  55. // Clock init ( follow hpl_init.c )
  56. hri_nvmctrl_set_CTRLB_RWS_bf(NVMCTRL, 2);
  57. _pm_init();
  58. _sysctrl_init_sources();
  59. #if _GCLK_INIT_1ST
  60. _gclk_init_generators_by_fref(_GCLK_INIT_1ST);
  61. #endif
  62. _sysctrl_init_referenced_generators();
  63. _gclk_init_generators_by_fref(_GCLK_INIT_LAST);
  64. // Update SystemCoreClock since it is hard coded with asf4 and not correct
  65. // Init 1ms tick timer (samd SystemCoreClock may not correct)
  66. SystemCoreClock = CONF_CPU_FREQUENCY;
  67. #if CFG_TUSB_OS == OPT_OS_NONE
  68. SysTick_Config(CONF_CPU_FREQUENCY / 1000);
  69. #endif
  70. // Led init
  71. #ifdef LED_PIN
  72. gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
  73. board_led_write(false);
  74. #endif
  75. // Button init
  76. #ifdef BUTTON_PIN
  77. gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
  78. gpio_set_pin_pull_mode(BUTTON_PIN, BUTTON_STATE_ACTIVE ? GPIO_PULL_DOWN : GPIO_PULL_UP);
  79. #endif
  80. uart_init();
  81. #if CFG_TUSB_OS == OPT_OS_FREERTOS
  82. // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
  83. NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
  84. #endif
  85. /* USB Clock init
  86. * The USB module requires a GCLK_USB of 48 MHz ~ 0.25% clock
  87. * for low speed and full speed operation. */
  88. _pm_enable_bus_clock(PM_BUS_APBB, USB);
  89. _pm_enable_bus_clock(PM_BUS_AHB, USB);
  90. _gclk_enable_channel(USB_GCLK_ID, GCLK_CLKCTRL_GEN_GCLK0_Val);
  91. // USB Pin Init
  92. gpio_set_pin_direction(PIN_PA24, GPIO_DIRECTION_OUT);
  93. gpio_set_pin_level(PIN_PA24, false);
  94. gpio_set_pin_pull_mode(PIN_PA24, GPIO_PULL_OFF);
  95. gpio_set_pin_direction(PIN_PA25, GPIO_DIRECTION_OUT);
  96. gpio_set_pin_level(PIN_PA25, false);
  97. gpio_set_pin_pull_mode(PIN_PA25, GPIO_PULL_OFF);
  98. gpio_set_pin_function(PIN_PA24, PINMUX_PA24G_USB_DM);
  99. gpio_set_pin_function(PIN_PA25, PINMUX_PA25G_USB_DP);
  100. // Output 500hz PWM on D12 (PA19 - TCC0 WO[3]) so we can validate the GCLK0 clock speed with a Saleae.
  101. _pm_enable_bus_clock(PM_BUS_APBC, TCC0);
  102. TCC0->PER.bit.PER = 48000000 / 1000;
  103. TCC0->CC[3].bit.CC = 48000000 / 2000;
  104. TCC0->CTRLA.bit.ENABLE = true;
  105. gpio_set_pin_function(PIN_PA19, PINMUX_PA19F_TCC0_WO3);
  106. _gclk_enable_channel(TCC0_GCLK_ID, GCLK_CLKCTRL_GEN_GCLK0_Val);
  107. }
  108. //--------------------------------------------------------------------+
  109. // Board porting API
  110. //--------------------------------------------------------------------+
  111. void board_led_write(bool state)
  112. {
  113. (void)state;
  114. #ifdef LED_PIN
  115. gpio_set_pin_level(LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
  116. #endif
  117. }
  118. uint32_t board_button_read(void)
  119. {
  120. #ifdef BUTTON_PIN
  121. return BUTTON_STATE_ACTIVE == gpio_get_pin_level(BUTTON_PIN);
  122. #else
  123. return 0;
  124. #endif
  125. }
  126. #if defined(UART_SERCOM)
  127. #define BOARD_SERCOM2(n) SERCOM ## n
  128. #define BOARD_SERCOM(n) BOARD_SERCOM2(n)
  129. static void uart_init(void)
  130. {
  131. #if UART_SERCOM == 0
  132. gpio_set_pin_function(PIN_PA06, PINMUX_PA06D_SERCOM0_PAD2);
  133. gpio_set_pin_function(PIN_PA07, PINMUX_PA07D_SERCOM0_PAD3);
  134. // setup clock (48MHz)
  135. _pm_enable_bus_clock(PM_BUS_APBC, SERCOM0);
  136. _gclk_enable_channel(SERCOM0_GCLK_ID_CORE, GCLK_CLKCTRL_GEN_GCLK0_Val);
  137. SERCOM0->USART.CTRLA.bit.SWRST = 1; /* reset SERCOM & enable config */
  138. while(SERCOM0->USART.SYNCBUSY.bit.SWRST);
  139. SERCOM0->USART.CTRLA.reg = /* CMODE = 0 -> async, SAMPA = 0, FORM = 0 -> USART frame, SMPR = 0 -> arithmetic baud rate */
  140. SERCOM_USART_CTRLA_SAMPR(1) | /* 0 = 16x / arithmetic baud rate, 1 = 16x / fractional baud rate */
  141. // SERCOM_USART_CTRLA_FORM(0) | /* 0 = USART Frame, 2 = LIN Master */
  142. SERCOM_USART_CTRLA_DORD | /* LSB first */
  143. SERCOM_USART_CTRLA_MODE(1) | /* 0 = Asynchronous, 1 = USART with internal clock */
  144. SERCOM_USART_CTRLA_RXPO(3) | /* pad 3 */
  145. SERCOM_USART_CTRLA_TXPO(1); /* pad 2 */
  146. SERCOM0->USART.CTRLB.reg =
  147. SERCOM_USART_CTRLB_TXEN | /* tx enabled */
  148. SERCOM_USART_CTRLB_RXEN; /* rx enabled */
  149. SERCOM0->USART.BAUD.reg = SERCOM_USART_BAUD_FRAC_FP(0) | SERCOM_USART_BAUD_FRAC_BAUD(26);
  150. SERCOM0->USART.CTRLA.bit.ENABLE = 1; /* activate SERCOM */
  151. while(SERCOM0->USART.SYNCBUSY.bit.ENABLE); /* wait for SERCOM to be ready */
  152. #endif
  153. }
  154. static inline void uart_send_buffer(uint8_t const *text, size_t len)
  155. {
  156. for (size_t i = 0; i < len; ++i) {
  157. BOARD_SERCOM(UART_SERCOM)->USART.DATA.reg = text[i];
  158. while((BOARD_SERCOM(UART_SERCOM)->USART.INTFLAG.reg & SERCOM_USART_INTFLAG_TXC) == 0);
  159. }
  160. }
  161. static inline void uart_send_str(const char* text)
  162. {
  163. while (*text) {
  164. BOARD_SERCOM(UART_SERCOM)->USART.DATA.reg = *text++;
  165. while((BOARD_SERCOM(UART_SERCOM)->USART.INTFLAG.reg & SERCOM_USART_INTFLAG_TXC) == 0);
  166. }
  167. }
  168. int board_uart_read(uint8_t* buf, int len)
  169. {
  170. (void) buf; (void) len;
  171. return 0;
  172. }
  173. int board_uart_write(void const * buf, int len)
  174. {
  175. if (len < 0) {
  176. uart_send_str(buf);
  177. } else {
  178. uart_send_buffer(buf, len);
  179. }
  180. return len;
  181. }
  182. #else // ! defined(UART_SERCOM)
  183. static void uart_init(void)
  184. {
  185. }
  186. int board_uart_read(uint8_t* buf, int len)
  187. {
  188. (void) buf; (void) len;
  189. return 0;
  190. }
  191. int board_uart_write(void const * buf, int len)
  192. {
  193. (void) buf; (void) len;
  194. return 0;
  195. }
  196. #endif
  197. #if CFG_TUSB_OS == OPT_OS_NONE
  198. volatile uint32_t system_ticks = 0;
  199. void SysTick_Handler (void)
  200. {
  201. system_ticks++;
  202. }
  203. uint32_t board_millis(void)
  204. {
  205. return system_ticks;
  206. }
  207. #endif