family.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "hpl/gclk/hpl_gclk_base.h"
  32. #include "hpl_mclk_config.h"
  33. //--------------------------------------------------------------------+
  34. // Forward USB interrupt events to TinyUSB IRQ Handler
  35. //--------------------------------------------------------------------+
  36. void USB_Handler(void)
  37. {
  38. tud_int_handler(0);
  39. }
  40. //--------------------------------------------------------------------+
  41. // MACRO TYPEDEF CONSTANT ENUM DECLARATION
  42. //--------------------------------------------------------------------+
  43. /* Referenced GCLKs (out of 0~4), should be initialized firstly */
  44. #define _GCLK_INIT_1ST 0x00000000
  45. /* Not referenced GCLKs, initialized last */
  46. #define _GCLK_INIT_LAST 0x0000001F
  47. void board_init(void)
  48. {
  49. // Clock init ( follow hpl_init.c )
  50. hri_nvmctrl_set_CTRLB_RWS_bf(NVMCTRL, CONF_NVM_WAIT_STATE);
  51. _set_performance_level(2);
  52. _osc32kctrl_init_sources();
  53. _oscctrl_init_sources();
  54. _mclk_init();
  55. #if _GCLK_INIT_1ST
  56. _gclk_init_generators_by_fref(_GCLK_INIT_1ST);
  57. #endif
  58. _oscctrl_init_referenced_generators();
  59. _gclk_init_generators_by_fref(_GCLK_INIT_LAST);
  60. #if (CONF_PORT_EVCTRL_PORT_0 | CONF_PORT_EVCTRL_PORT_1 | CONF_PORT_EVCTRL_PORT_2 | CONF_PORT_EVCTRL_PORT_3)
  61. hri_port_set_EVCTRL_reg(PORT, 0, CONF_PORTA_EVCTRL);
  62. hri_port_set_EVCTRL_reg(PORT, 1, CONF_PORTB_EVCTRL);
  63. #endif
  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. SysTick_Config(CONF_CPU_FREQUENCY / 1000);
  68. // Led init
  69. gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
  70. gpio_set_pin_level(LED_PIN, !LED_STATE_ON);
  71. // Button init
  72. gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
  73. gpio_set_pin_pull_mode(BUTTON_PIN, BUTTON_STATE_ACTIVE ? GPIO_PULL_DOWN : GPIO_PULL_UP);
  74. #if CFG_TUSB_OS == OPT_OS_FREERTOS
  75. // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
  76. NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
  77. #endif
  78. /* USB Clock init
  79. * The USB module requires a GCLK_USB of 48 MHz ~ 0.25% clock
  80. * for low speed and full speed operation. */
  81. hri_gclk_write_PCHCTRL_reg(GCLK, USB_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK1_Val | GCLK_PCHCTRL_CHEN);
  82. hri_mclk_set_AHBMASK_USB_bit(MCLK);
  83. hri_mclk_set_APBBMASK_USB_bit(MCLK);
  84. // USB Pin Init
  85. gpio_set_pin_direction(PIN_PA24, GPIO_DIRECTION_OUT);
  86. gpio_set_pin_level(PIN_PA24, false);
  87. gpio_set_pin_pull_mode(PIN_PA24, GPIO_PULL_OFF);
  88. gpio_set_pin_direction(PIN_PA25, GPIO_DIRECTION_OUT);
  89. gpio_set_pin_level(PIN_PA25, false);
  90. gpio_set_pin_pull_mode(PIN_PA25, GPIO_PULL_OFF);
  91. gpio_set_pin_function(PIN_PA24, PINMUX_PA24G_USB_DM);
  92. gpio_set_pin_function(PIN_PA25, PINMUX_PA25G_USB_DP);
  93. // Output 500hz PWM on PB23 (TCC0 WO[3]) so we can validate the GCLK1 clock speed
  94. // hri_mclk_set_APBCMASK_TCC0_bit(MCLK);
  95. // TCC0->PER.bit.PER = 48000000 / 1000;
  96. // TCC0->CC[3].bit.CC = 48000000 / 2000;
  97. // TCC0->CTRLA.bit.ENABLE = true;
  98. //
  99. // gpio_set_pin_function(PIN_PB23, PINMUX_PB23F_TCC0_WO3);
  100. // hri_gclk_write_PCHCTRL_reg(GCLK, TCC0_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK1_Val | GCLK_PCHCTRL_CHEN);
  101. }
  102. //--------------------------------------------------------------------+
  103. // Board porting API
  104. //--------------------------------------------------------------------+
  105. void board_led_write(bool state)
  106. {
  107. gpio_set_pin_level(LED_PIN, state);
  108. }
  109. uint32_t board_button_read(void)
  110. {
  111. // button is active low
  112. return gpio_get_pin_level(BUTTON_PIN) ? 0 : 1;
  113. }
  114. int board_uart_read(uint8_t* buf, int len)
  115. {
  116. (void) buf; (void) len;
  117. return 0;
  118. }
  119. int board_uart_write(void const * buf, int len)
  120. {
  121. (void) buf; (void) len;
  122. return 0;
  123. }
  124. #if CFG_TUSB_OS == OPT_OS_NONE
  125. volatile uint32_t system_ticks = 0;
  126. void SysTick_Handler (void)
  127. {
  128. system_ticks++;
  129. }
  130. uint32_t board_millis(void)
  131. {
  132. return system_ticks;
  133. }
  134. #endif
  135. void _init(void)
  136. {
  137. }