Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2018, hathach (tinyusb.org) |
| 5 | * Copyright (c) 2020, Koji Kitayama |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | * |
| 25 | * This file is part of the TinyUSB stack. |
| 26 | */ |
| 27 | |
| 28 | #include "../board.h" |
| 29 | #include "fsl_device_registers.h" |
| 30 | #include "fsl_gpio.h" |
| 31 | #include "fsl_port.h" |
| 32 | #include "fsl_clock.h" |
| 33 | #include "fsl_lpsci.h" |
| 34 | |
| 35 | #include "clock_config.h" |
| 36 | |
| 37 | //--------------------------------------------------------------------+ |
| 38 | // Forward USB interrupt events to TinyUSB IRQ Handler |
| 39 | //--------------------------------------------------------------------+ |
| 40 | void USB0_IRQHandler(void) |
| 41 | { |
| 42 | tud_int_handler(0); |
| 43 | } |
| 44 | |
| 45 | //--------------------------------------------------------------------+ |
| 46 | // MACRO TYPEDEF CONSTANT ENUM DECLARATION |
| 47 | //--------------------------------------------------------------------+ |
| 48 | // LED |
| 49 | #define LED_PINMUX IOMUXC_GPIO_AD_B0_09_GPIO1_IO09 |
| 50 | #define LED_PORT GPIOB |
| 51 | #define LED_PIN_CLOCK kCLOCK_PortB |
| 52 | #define LED_PIN_PORT PORTB |
| 53 | #define LED_PIN 19U |
| 54 | #define LED_PIN_FUNCTION kPORT_MuxAsGpio |
| 55 | #define LED_STATE_ON 0 |
| 56 | |
| 57 | // Button |
| 58 | #define BUTTON_PORT GPIOC |
| 59 | #define BUTTON_PIN_CLOCK kCLOCK_PortC |
| 60 | #define BUTTON_PIN_PORT PORTC |
| 61 | #define BUTTON_PIN 9U |
| 62 | #define BUTTON_PIN_FUNCTION kPORT_MuxAsGpio |
| 63 | #define BUTTON_STATE_ACTIVE 0 |
| 64 | |
| 65 | // UART |
| 66 | #define UART_PORT UART0 |
| 67 | #define UART_PIN_CLOCK kCLOCK_PortA |
| 68 | #define UART_PIN_PORT PORTA |
| 69 | #define UART_PIN_RX 1u |
| 70 | #define UART_PIN_TX 2u |
| 71 | #define UART_PIN_FUNCTION kPORT_MuxAlt2 |
| 72 | #define SOPT5_UART0RXSRC_UART_RX 0x00u /*!< UART0 receive data source select: UART0_RX pin */ |
| 73 | #define SOPT5_UART0TXSRC_UART_TX 0x00u /*!< UART0 transmit data source select: UART0_TX pin */ |
| 74 | |
| 75 | const uint8_t dcd_data[] = { 0x00 }; |
| 76 | |
| 77 | void board_init(void) |
| 78 | { |
| 79 | BOARD_BootClockRUN(); |
| 80 | SystemCoreClockUpdate(); |
| 81 | |
| 82 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 83 | // 1ms tick timer |
| 84 | SysTick_Config(SystemCoreClock / 1000); |
| 85 | #elif CFG_TUSB_OS == OPT_OS_FREERTOS |
| 86 | // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) |
| 87 | NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); |
| 88 | #endif |
| 89 | |
| 90 | // LED |
| 91 | CLOCK_EnableClock(LED_PIN_CLOCK); |
| 92 | PORT_SetPinMux(LED_PIN_PORT, LED_PIN, LED_PIN_FUNCTION); |
| 93 | gpio_pin_config_t led_config = { kGPIO_DigitalOutput, 0 }; |
| 94 | GPIO_PinInit(LED_PORT, LED_PIN, &led_config); |
| 95 | board_led_write(false); |
| 96 | |
| 97 | #if defined(BUTTON_PORT) && defined(BUTTON_PIN) |
| 98 | // Button |
| 99 | CLOCK_EnableClock(BUTTON_PIN_CLOCK); |
| 100 | port_pin_config_t button_port = { |
| 101 | .pullSelect = kPORT_PullUp, |
| 102 | .mux = BUTTON_PIN_FUNCTION, |
| 103 | }; |
| 104 | PORT_SetPinConfig(BUTTON_PIN_PORT, BUTTON_PIN, &button_port); |
| 105 | gpio_pin_config_t button_config = { kGPIO_DigitalInput, 0 }; |
| 106 | GPIO_PinInit(BUTTON_PORT, BUTTON_PIN, &button_config); |
| 107 | #endif |
| 108 | |
| 109 | // UART |
| 110 | CLOCK_EnableClock(UART_PIN_CLOCK); |
| 111 | PORT_SetPinMux(UART_PIN_PORT, UART_PIN_RX, UART_PIN_FUNCTION); |
| 112 | PORT_SetPinMux(UART_PIN_PORT, UART_PIN_TX, UART_PIN_FUNCTION); |
| 113 | SIM->SOPT5 = ((SIM->SOPT5 & |
| 114 | (~(SIM_SOPT5_UART0TXSRC_MASK | SIM_SOPT5_UART0RXSRC_MASK))) |
| 115 | | SIM_SOPT5_UART0TXSRC(SOPT5_UART0TXSRC_UART_TX) |
| 116 | | SIM_SOPT5_UART0RXSRC(SOPT5_UART0RXSRC_UART_RX) |
| 117 | ); |
| 118 | |
| 119 | lpsci_config_t uart_config; |
| 120 | CLOCK_SetLpsci0Clock(1); |
| 121 | LPSCI_GetDefaultConfig(&uart_config); |
| 122 | uart_config.baudRate_Bps = CFG_BOARD_UART_BAUDRATE; |
| 123 | uart_config.enableTx = true; |
| 124 | uart_config.enableRx = true; |
| 125 | LPSCI_Init(UART_PORT, &uart_config, CLOCK_GetPllFllSelClkFreq()); |
| 126 | |
| 127 | // USB |
| 128 | CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcPll0, CLOCK_GetFreq(kCLOCK_PllFllSelClk)); |
| 129 | } |
| 130 | |
| 131 | //--------------------------------------------------------------------+ |
| 132 | // Board porting API |
| 133 | //--------------------------------------------------------------------+ |
| 134 | |
| 135 | void board_led_write(bool state) |
| 136 | { |
| 137 | GPIO_WritePinOutput(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); |
| 138 | } |
| 139 | |
| 140 | uint32_t board_button_read(void) |
| 141 | { |
| 142 | #if defined(BUTTON_PORT) && defined(BUTTON_PIN) |
| 143 | return BUTTON_STATE_ACTIVE == GPIO_ReadPinInput(BUTTON_PORT, BUTTON_PIN); |
| 144 | #endif |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | int board_uart_read(uint8_t* buf, int len) |
| 149 | { |
| 150 | LPSCI_ReadBlocking(UART_PORT, buf, len); |
| 151 | return len; |
| 152 | } |
| 153 | |
| 154 | int board_uart_write(void const * buf, int len) |
| 155 | { |
| 156 | LPSCI_WriteBlocking(UART_PORT, (uint8_t const*) buf, len); |
| 157 | return len; |
| 158 | } |
| 159 | |
| 160 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 161 | volatile uint32_t system_ticks = 0; |
| 162 | void SysTick_Handler(void) |
| 163 | { |
| 164 | system_ticks++; |
| 165 | } |
| 166 | |
| 167 | uint32_t board_millis(void) |
| 168 | { |
| 169 | return system_ticks; |
| 170 | } |
| 171 | #endif |