Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 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 | |
| 26 | #include "sam.h" |
| 27 | #include "bsp/board.h" |
| 28 | |
| 29 | #include "peripheral_clk_config.h" |
| 30 | #include "hpl/usart/hpl_usart_base.h" |
| 31 | #include "hpl/pmc/hpl_pmc.h" |
| 32 | #include "hal/include/hal_init.h" |
| 33 | #include "hal/include/hal_usart_async.h" |
| 34 | #include "hal/include/hal_gpio.h" |
| 35 | |
| 36 | |
| 37 | // You can get the board here: |
| 38 | // https://www.aliexpress.com/item/1005003173783268.html |
| 39 | |
| 40 | //--------------------------------------------------------------------+ |
| 41 | // MACRO TYPEDEF CONSTANT ENUM DECLARATION |
| 42 | //--------------------------------------------------------------------+ |
| 43 | |
| 44 | #define LED_PIN GPIO(GPIO_PORTA, 15) |
| 45 | |
| 46 | #define BUTTON_PIN GPIO(GPIO_PORTA, 21) |
| 47 | #define BUTTON_STATE_ACTIVE 0 |
| 48 | |
| 49 | #define UART_TX_PIN GPIO(GPIO_PORTB, 1) |
| 50 | #define UART_RX_PIN GPIO(GPIO_PORTB, 0) |
| 51 | |
| 52 | static struct usart_async_descriptor edbg_com; |
| 53 | static uint8_t edbg_com_buffer[64]; |
| 54 | static volatile bool uart_busy = false; |
| 55 | |
| 56 | static void tx_cb_EDBG_COM(const struct usart_async_descriptor *const io_descr) |
| 57 | { |
| 58 | (void) io_descr; |
| 59 | uart_busy = false; |
| 60 | } |
| 61 | |
| 62 | //------------- IMPLEMENTATION -------------// |
| 63 | void board_init(void) |
| 64 | { |
| 65 | init_mcu(); |
| 66 | |
| 67 | /* Disable Watchdog */ |
| 68 | hri_wdt_set_MR_WDDIS_bit(WDT); |
| 69 | |
| 70 | // LED |
| 71 | _pmc_enable_periph_clock(ID_PIOB); |
| 72 | gpio_set_pin_level(LED_PIN, false); |
| 73 | gpio_set_pin_direction(LED_PIN, GPIO_DIRECTION_OUT); |
| 74 | gpio_set_pin_function(LED_PIN, GPIO_PIN_FUNCTION_OFF); |
| 75 | |
| 76 | // Button |
| 77 | _pmc_enable_periph_clock(ID_PIOA); |
| 78 | gpio_set_pin_direction(BUTTON_PIN, GPIO_DIRECTION_IN); |
| 79 | gpio_set_pin_pull_mode(BUTTON_PIN, GPIO_PULL_UP); |
| 80 | gpio_set_pin_function(BUTTON_PIN, GPIO_PIN_FUNCTION_OFF); |
| 81 | |
| 82 | // Uart via EDBG Com |
| 83 | _pmc_enable_periph_clock(ID_USART1); |
| 84 | gpio_set_pin_function(UART_RX_PIN, MUX_PA21A_USART1_RXD1); |
| 85 | gpio_set_pin_function(UART_TX_PIN, MUX_PB4D_USART1_TXD1); |
| 86 | |
| 87 | usart_async_init(&edbg_com, USART1, edbg_com_buffer, sizeof(edbg_com_buffer), _usart_get_usart_async()); |
| 88 | usart_async_set_baud_rate(&edbg_com, CFG_BOARD_UART_BAUDRATE); |
| 89 | usart_async_register_callback(&edbg_com, USART_ASYNC_TXC_CB, tx_cb_EDBG_COM); |
| 90 | usart_async_enable(&edbg_com); |
| 91 | |
| 92 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 93 | // 1ms tick timer (samd SystemCoreClock may not correct) |
| 94 | SysTick_Config(CONF_CPU_FREQUENCY / 1000); |
| 95 | #endif |
| 96 | |
| 97 | // Enable USB clock |
| 98 | _pmc_enable_periph_clock(ID_USBHS); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | //--------------------------------------------------------------------+ |
| 103 | // USB Interrupt Handler |
| 104 | //--------------------------------------------------------------------+ |
| 105 | void USBHS_Handler(void) |
| 106 | { |
| 107 | tud_int_handler(0); |
| 108 | } |
| 109 | |
| 110 | //--------------------------------------------------------------------+ |
| 111 | // Board porting API |
| 112 | //--------------------------------------------------------------------+ |
| 113 | |
| 114 | void board_led_write(bool state) |
| 115 | { |
| 116 | gpio_set_pin_level(LED_PIN, state); |
| 117 | } |
| 118 | |
| 119 | uint32_t board_button_read(void) |
| 120 | { |
| 121 | return BUTTON_STATE_ACTIVE == gpio_get_pin_level(BUTTON_PIN); |
| 122 | } |
| 123 | |
| 124 | int board_uart_read(uint8_t* buf, int len) |
| 125 | { |
| 126 | (void) buf; (void) len; |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int board_uart_write(void const * buf, int len) |
| 131 | { |
| 132 | // while until previous transfer is complete |
| 133 | while(uart_busy) {} |
| 134 | uart_busy = true; |
| 135 | |
| 136 | io_write(&edbg_com.io, buf, len); |
| 137 | return len; |
| 138 | } |
| 139 | |
| 140 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 141 | volatile uint32_t system_ticks = 0; |
| 142 | |
| 143 | void SysTick_Handler (void) |
| 144 | { |
| 145 | system_ticks++; |
| 146 | } |
| 147 | |
| 148 | uint32_t board_millis(void) |
| 149 | { |
| 150 | return system_ticks; |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | // Required by __libc_init_array in startup code if we are compiling using |
| 155 | // -nostdlib/-nostartfiles. |
| 156 | void _init(void) |
| 157 | { |
| 158 | |
| 159 | } |