same70_xplained.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019, 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. */
  25. #include "sam.h"
  26. #include "bsp/board.h"
  27. #include "peripheral_clk_config.h"
  28. #include "hpl/usart/hpl_usart_base.h"
  29. #include "hpl/pmc/hpl_pmc.h"
  30. #include "hal/include/hal_init.h"
  31. #include "hal/include/hal_usart_async.h"
  32. #include "hal/include/hal_gpio.h"
  33. //--------------------------------------------------------------------+
  34. // MACRO TYPEDEF CONSTANT ENUM DECLARATION
  35. //--------------------------------------------------------------------+
  36. #define LED_PIN GPIO(GPIO_PORTC, 8)
  37. #define BUTTON_PIN GPIO(GPIO_PORTA, 11)
  38. #define BUTTON_STATE_ACTIVE 0
  39. #define UART_TX_PIN GPIO(GPIO_PORTB, 4)
  40. #define UART_RX_PIN GPIO(GPIO_PORTA, 21)
  41. static struct usart_async_descriptor edbg_com;
  42. static uint8_t edbg_com_buffer[64];
  43. static volatile bool uart_busy = false;
  44. static void tx_cb_EDBG_COM(const struct usart_async_descriptor *const io_descr)
  45. {
  46. (void) io_descr;
  47. uart_busy = false;
  48. }
  49. //------------- IMPLEMENTATION -------------//
  50. void board_init(void)
  51. {
  52. init_mcu();
  53. /* Disable Watchdog */
  54. hri_wdt_set_MR_WDDIS_bit(WDT);
  55. // LED
  56. _pmc_enable_periph_clock(ID_PIOC);
  57. gpio_set_pin_level(LED_PIN, false);
  58. gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
  59. gpio_set_pin_function(LED_PIN, GPIO_PIN_FUNCTION_OFF);
  60. // Button
  61. _pmc_enable_periph_clock(ID_PIOA);
  62. gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
  63. gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
  64. gpio_set_pin_function(BUTTON_PIN, GPIO_PIN_FUNCTION_OFF);
  65. // Uart via EDBG Com
  66. _pmc_enable_periph_clock(ID_USART1);
  67. gpio_set_pin_function(UART_RX_PIN, MUX_PA21A_USART1_RXD1);
  68. gpio_set_pin_function(UART_TX_PIN, MUX_PB4D_USART1_TXD1);
  69. usart_async_init(&edbg_com, USART1, edbg_com_buffer, sizeof(edbg_com_buffer), _usart_get_usart_async());
  70. usart_async_set_baud_rate(&edbg_com, CFG_BOARD_UART_BAUDRATE);
  71. usart_async_register_callback(&edbg_com, USART_ASYNC_TXC_CB, tx_cb_EDBG_COM);
  72. usart_async_enable(&edbg_com);
  73. #if CFG_TUSB_OS == OPT_OS_NONE
  74. // 1ms tick timer (samd SystemCoreClock may not correct)
  75. SysTick_Config(CONF_CPU_FREQUENCY / 1000);
  76. #endif
  77. // Enable USB clock
  78. _pmc_enable_periph_clock(ID_USBHS);
  79. }
  80. //--------------------------------------------------------------------+
  81. // USB Interrupt Handler
  82. //--------------------------------------------------------------------+
  83. void USBHS_Handler(void)
  84. {
  85. tud_int_handler(0);
  86. }
  87. //--------------------------------------------------------------------+
  88. // Board porting API
  89. //--------------------------------------------------------------------+
  90. void board_led_write(bool state)
  91. {
  92. gpio_set_pin_level(LED_PIN, state);
  93. }
  94. uint32_t board_button_read(void)
  95. {
  96. return BUTTON_STATE_ACTIVE == gpio_get_pin_level(BUTTON_PIN);
  97. }
  98. int board_uart_read(uint8_t* buf, int len)
  99. {
  100. (void) buf; (void) len;
  101. return 0;
  102. }
  103. int board_uart_write(void const * buf, int len)
  104. {
  105. // while until previous transfer is complete
  106. while(uart_busy) {}
  107. uart_busy = true;
  108. io_write(&edbg_com.io, buf, len);
  109. return len;
  110. }
  111. #if CFG_TUSB_OS == OPT_OS_NONE
  112. volatile uint32_t system_ticks = 0;
  113. void SysTick_Handler (void)
  114. {
  115. system_ticks++;
  116. }
  117. uint32_t board_millis(void)
  118. {
  119. return system_ticks;
  120. }
  121. #endif
  122. // Required by __libc_init_array in startup code if we are compiling using
  123. // -nostdlib/-nostartfiles.
  124. void _init(void)
  125. {
  126. }