samg55xplained.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "hal/include/hal_init.h"
  29. #include "hal/include/hpl_usart_sync.h"
  30. #include "hpl/pmc/hpl_pmc.h"
  31. #include "hal/include/hal_gpio.h"
  32. //--------------------------------------------------------------------+
  33. // MACRO TYPEDEF CONSTANT ENUM DECLARATION
  34. //--------------------------------------------------------------------+
  35. #define LED_PIN GPIO(GPIO_PORTA, 6)
  36. #define BUTTON_PIN GPIO(GPIO_PORTA, 2)
  37. #define BUTTON_STATE_ACTIVE 0
  38. #define UART_TX_PIN GPIO(GPIO_PORTA, 28)
  39. #define UART_RX_PIN GPIO(GPIO_PORTA, 27)
  40. struct _usart_sync_device edbg_com;
  41. //------------- IMPLEMENTATION -------------//
  42. void board_init(void)
  43. {
  44. init_mcu();
  45. _pmc_enable_periph_clock(ID_PIOA);
  46. /* Disable Watchdog */
  47. hri_wdt_set_MR_WDDIS_bit(WDT);
  48. // LED
  49. gpio_set_pin_level(LED_PIN, false);
  50. gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT);
  51. gpio_set_pin_function(LED_PIN, GPIO_PIN_FUNCTION_OFF);
  52. // Button
  53. gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN);
  54. gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP);
  55. gpio_set_pin_function(BUTTON_PIN, GPIO_PIN_FUNCTION_OFF);
  56. // Uart via EDBG Com
  57. _pmc_enable_periph_clock(ID_FLEXCOM7);
  58. gpio_set_pin_function(UART_RX_PIN, MUX_PA27B_FLEXCOM7_RXD);
  59. gpio_set_pin_function(UART_TX_PIN, MUX_PA28B_FLEXCOM7_TXD);
  60. _usart_sync_init(&edbg_com, FLEXCOM7);
  61. _usart_sync_set_baud_rate(&edbg_com, CFG_BOARD_UART_BAUDRATE);
  62. _usart_sync_set_mode(&edbg_com, USART_MODE_ASYNCHRONOUS);
  63. _usart_sync_enable(&edbg_com);
  64. #if CFG_TUSB_OS == OPT_OS_NONE
  65. // 1ms tick timer (samd SystemCoreClock may not correct)
  66. SysTick_Config(CONF_CPU_FREQUENCY / 1000);
  67. #endif
  68. // USB Pin, Clock init
  69. /* Clear SYSIO 10 & 11 for USB DM & DP */
  70. hri_matrix_clear_CCFG_SYSIO_reg(MATRIX, CCFG_SYSIO_SYSIO10 | CCFG_SYSIO_SYSIO11);
  71. // Enable clock
  72. _pmc_enable_periph_clock(ID_UDP);
  73. /* USB Device mode & Transceiver active */
  74. hri_matrix_write_CCFG_USBMR_reg(MATRIX, CCFG_USBMR_USBMODE);
  75. }
  76. //--------------------------------------------------------------------+
  77. // USB Interrupt Handler
  78. //--------------------------------------------------------------------+
  79. void UDP_Handler(void)
  80. {
  81. #if CFG_TUD_ENABLED
  82. tud_int_handler(0);
  83. #endif
  84. }
  85. //--------------------------------------------------------------------+
  86. // Board porting API
  87. //--------------------------------------------------------------------+
  88. void board_led_write(bool state)
  89. {
  90. gpio_set_pin_level(LED_PIN, state);
  91. }
  92. uint32_t board_button_read(void)
  93. {
  94. return BUTTON_STATE_ACTIVE == gpio_get_pin_level(BUTTON_PIN);
  95. }
  96. int board_uart_read(uint8_t* buf, int len)
  97. {
  98. (void) buf; (void) len;
  99. return 0;
  100. }
  101. int board_uart_write(void const * buf, int len)
  102. {
  103. uint8_t const * buf8 = (uint8_t const *) buf;
  104. for(int i=0; i<len; i++)
  105. {
  106. while ( !_usart_sync_is_ready_to_send(&edbg_com) ) {}
  107. _usart_sync_write_byte(&edbg_com, buf8[i]);
  108. }
  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. }