same70_xplained.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. uart_busy = false;
  47. }
  48. //------------- IMPLEMENTATION -------------//
  49. void board_init(void)
  50. {
  51. init_mcu();
  52. /* Disable Watchdog */
  53. hri_wdt_set_MR_WDDIS_bit(WDT);
  54. // LED
  55. _pmc_enable_periph_clock(ID_PIOC);
  56. gpio_set_pin_level(LED_PIN, false);
  57. gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
  58. gpio_set_pin_function(LED_PIN, GPIO_PIN_FUNCTION_OFF);
  59. // Button
  60. _pmc_enable_periph_clock(ID_PIOA);
  61. gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
  62. gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
  63. gpio_set_pin_function(BUTTON_PIN, GPIO_PIN_FUNCTION_OFF);
  64. // Uart via EDBG Com
  65. _pmc_enable_periph_clock(ID_USART1);
  66. gpio_set_pin_function(UART_RX_PIN, MUX_PA21A_USART1_RXD1);
  67. gpio_set_pin_function(UART_TX_PIN, MUX_PB4D_USART1_TXD1);
  68. usart_async_init(&edbg_com, USART1, edbg_com_buffer, sizeof(edbg_com_buffer), _usart_get_usart_async());
  69. usart_async_set_baud_rate(&edbg_com, CFG_BOARD_UART_BAUDRATE);
  70. usart_async_register_callback(&edbg_com, USART_ASYNC_TXC_CB, tx_cb_EDBG_COM);
  71. // usart_async_register_callback(&EDBG_COM, USART_ASYNC_RXC_CB, rx_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. #if 0
  78. // USB Pin, Clock init
  79. /* Clear SYSIO 10 & 11 for USB DM & DP */
  80. hri_matrix_clear_CCFG_SYSIO_reg(MATRIX, CCFG_SYSIO_SYSIO10 | CCFG_SYSIO_SYSIO11);
  81. // Enable clock
  82. _pmc_enable_periph_clock(ID_UDP);
  83. /* USB Device mode & Transceiver active */
  84. hri_matrix_write_CCFG_USBMR_reg(MATRIX, CCFG_USBMR_USBMODE);
  85. #endif
  86. }
  87. //--------------------------------------------------------------------+
  88. // USB Interrupt Handler
  89. //--------------------------------------------------------------------+
  90. void UDP_Handler(void)
  91. {
  92. #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE
  93. tud_int_handler(0);
  94. #endif
  95. }
  96. //--------------------------------------------------------------------+
  97. // Board porting API
  98. //--------------------------------------------------------------------+
  99. void board_led_write(bool state)
  100. {
  101. gpio_set_pin_level(LED_PIN, state);
  102. }
  103. uint32_t board_button_read(void)
  104. {
  105. return BUTTON_STATE_ACTIVE == gpio_get_pin_level(BUTTON_PIN);
  106. }
  107. int board_uart_read(uint8_t* buf, int len)
  108. {
  109. (void) buf; (void) len;
  110. return 0;
  111. }
  112. int board_uart_write(void const * buf, int len)
  113. {
  114. // while until previous transfer is complete
  115. while(uart_busy) {}
  116. uart_busy = true;
  117. io_write(&edbg_com.io, buf, len);
  118. return len;
  119. }
  120. #if CFG_TUSB_OS == OPT_OS_NONE
  121. volatile uint32_t system_ticks = 0;
  122. void SysTick_Handler (void)
  123. {
  124. system_ticks++;
  125. }
  126. uint32_t board_millis(void)
  127. {
  128. return system_ticks;
  129. }
  130. #endif
  131. // Required by __libc_init_array in startup code if we are compiling using
  132. // -nostdlib/-nostartfiles.
  133. void _init(void)
  134. {
  135. }