Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2019 Ha Thach (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 | * This file is part of the TinyUSB stack. |
| 25 | */ |
| 26 | |
| 27 | #include "chip.h" |
| 28 | #include "../board.h" |
| 29 | |
| 30 | #define LED_PORT 1 |
| 31 | #define LED_PIN 18 |
| 32 | #define LED_STATE_ON 1 |
| 33 | |
| 34 | // JOYSTICK_DOWN if using LPCXpresso Base Board |
| 35 | #define BUTTON_PORT 0 |
| 36 | #define BUTTON_PIN 15 |
| 37 | #define BUTTON_STATE_ACTIVE 0 |
| 38 | |
| 39 | #define BOARD_UART_PORT LPC_UART3 |
| 40 | |
| 41 | /* System oscillator rate and RTC oscillator rate */ |
| 42 | const uint32_t OscRateIn = 10000000; |
| 43 | const uint32_t RTCOscRateIn = 32768; |
| 44 | |
| 45 | /* Pin muxing configuration */ |
| 46 | static const PINMUX_GRP_T pinmuxing[] = |
| 47 | { |
| 48 | {LED_PORT, LED_PIN, IOCON_MODE_INACT | IOCON_FUNC0}, |
| 49 | {BUTTON_PORT, BUTTON_PIN, IOCON_FUNC0 | IOCON_MODE_PULLUP}, |
| 50 | }; |
| 51 | |
| 52 | static const PINMUX_GRP_T pin_usb_mux[] = |
| 53 | { |
| 54 | {0, 29, IOCON_MODE_INACT | IOCON_FUNC1}, // D+ |
| 55 | {0, 30, IOCON_MODE_INACT | IOCON_FUNC1}, // D- |
| 56 | {2, 9, IOCON_MODE_INACT | IOCON_FUNC1}, // Connect |
| 57 | |
| 58 | {1, 19, IOCON_MODE_INACT | IOCON_FUNC2}, // USB_PPWR |
| 59 | {1, 22, IOCON_MODE_INACT | IOCON_FUNC2}, // USB_PWRD |
| 60 | |
| 61 | /* VBUS is not connected on this board, so leave the pin at default setting. */ |
| 62 | /*Chip_IOCON_PinMux(LPC_IOCON, 1, 30, IOCON_MODE_INACT, IOCON_FUNC2);*/ /* USB VBUS */ |
| 63 | }; |
| 64 | |
| 65 | // Invoked by startup code |
| 66 | void SystemInit(void) |
| 67 | { |
| 68 | #ifdef __USE_LPCOPEN |
| 69 | extern void (* const g_pfnVectors[])(void); |
| 70 | unsigned int *pSCB_VTOR = (unsigned int *) 0xE000ED08; |
| 71 | *pSCB_VTOR = (unsigned int) g_pfnVectors; |
| 72 | #endif |
| 73 | |
| 74 | Chip_IOCON_Init(LPC_IOCON); |
| 75 | Chip_IOCON_SetPinMuxing(LPC_IOCON, pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T)); |
| 76 | Chip_SetupXtalClocking(); |
| 77 | } |
| 78 | |
| 79 | void board_init(void) |
| 80 | { |
| 81 | SystemCoreClockUpdate(); |
| 82 | |
| 83 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 84 | // 1ms tick timer |
| 85 | SysTick_Config(SystemCoreClock / 1000); |
| 86 | #elif CFG_TUSB_OS == OPT_OS_FREERTOS |
| 87 | // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) |
| 88 | NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); |
| 89 | #endif |
| 90 | |
| 91 | Chip_GPIO_Init(LPC_GPIO); |
| 92 | |
| 93 | // LED |
| 94 | Chip_GPIO_SetPinDIROutput(LPC_GPIO, LED_PORT, LED_PIN); |
| 95 | |
| 96 | // Button |
| 97 | Chip_GPIO_SetPinDIRInput(LPC_GPIO, BUTTON_PORT, BUTTON_PIN); |
| 98 | |
| 99 | #if 0 |
| 100 | //------------- UART -------------// |
| 101 | PINSEL_CFG_Type PinCfg = |
| 102 | { |
| 103 | .Portnum = 0, |
| 104 | .Pinnum = 0, // TXD is P0.0 |
| 105 | .Funcnum = 2, |
| 106 | .OpenDrain = 0, |
| 107 | .Pinmode = 0 |
| 108 | }; |
| 109 | PINSEL_ConfigPin(&PinCfg); |
| 110 | |
| 111 | PinCfg.Portnum = 0; |
| 112 | PinCfg.Pinnum = 1; // RXD is P0.1 |
| 113 | PINSEL_ConfigPin(&PinCfg); |
| 114 | |
| 115 | UART_CFG_Type UARTConfigStruct; |
| 116 | UART_ConfigStructInit(&UARTConfigStruct); |
| 117 | UARTConfigStruct.Baud_rate = CFG_BOARD_UART_BAUDRATE; |
| 118 | |
| 119 | UART_Init(BOARD_UART_PORT, &UARTConfigStruct); |
| 120 | UART_TxCmd(BOARD_UART_PORT, ENABLE); // Enable UART Transmit |
| 121 | #endif |
| 122 | |
| 123 | //------------- USB -------------// |
| 124 | Chip_IOCON_SetPinMuxing(LPC_IOCON, pin_usb_mux, sizeof(pin_usb_mux) / sizeof(PINMUX_GRP_T)); |
| 125 | Chip_USB_Init(); |
| 126 | |
| 127 | enum { |
| 128 | USBCLK_DEVCIE = 0x12, // AHB + Device |
| 129 | USBCLK_HOST = 0x19, // AHB + Host + OTG |
| 130 | // 0x1B // Host + Device + OTG + AHB |
| 131 | }; |
| 132 | |
| 133 | uint32_t const clk_en = TUSB_OPT_DEVICE_ENABLED ? USBCLK_DEVCIE : USBCLK_HOST; |
| 134 | |
| 135 | LPC_USB->OTGClkCtrl = clk_en; |
| 136 | while ( (LPC_USB->OTGClkSt & clk_en) != clk_en ); |
| 137 | |
| 138 | #if TUSB_OPT_HOST_ENABLED |
| 139 | // set portfunc to host !!! |
| 140 | LPC_USB->StCtrl = 0x3; // should be 1 |
| 141 | #endif |
| 142 | } |
| 143 | |
| 144 | //--------------------------------------------------------------------+ |
| 145 | // USB Interrupt Handler |
| 146 | //--------------------------------------------------------------------+ |
| 147 | void USB_IRQHandler(void) |
| 148 | { |
| 149 | #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_HOST |
| 150 | tuh_int_handler(0); |
| 151 | #endif |
| 152 | |
| 153 | #if CFG_TUSB_RHPORT0_MODE & OPT_MODE_DEVICE |
| 154 | tud_int_handler(0); |
| 155 | #endif |
| 156 | } |
| 157 | |
| 158 | //--------------------------------------------------------------------+ |
| 159 | // Board porting API |
| 160 | //--------------------------------------------------------------------+ |
| 161 | |
| 162 | void board_led_write(bool state) |
| 163 | { |
| 164 | Chip_GPIO_SetPinState(LPC_GPIO, LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); |
| 165 | } |
| 166 | |
| 167 | uint32_t board_button_read(void) |
| 168 | { |
| 169 | return BUTTON_STATE_ACTIVE == Chip_GPIO_GetPinState(LPC_GPIO, BUTTON_PORT, BUTTON_PIN); |
| 170 | } |
| 171 | |
| 172 | int board_uart_read(uint8_t* buf, int len) |
| 173 | { |
| 174 | // return UART_ReceiveByte(BOARD_UART_PORT); |
| 175 | (void) buf; (void) len; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | int board_uart_write(void const * buf, int len) |
| 180 | { |
| 181 | // UART_Send(BOARD_UART_PORT, &c, 1, BLOCKING); |
| 182 | (void) buf; (void) len; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | #if CFG_TUSB_OS == OPT_OS_NONE |
| 187 | volatile uint32_t system_ticks = 0; |
| 188 | void SysTick_Handler (void) |
| 189 | { |
| 190 | system_ticks++; |
| 191 | } |
| 192 | |
| 193 | uint32_t board_millis(void) |
| 194 | { |
| 195 | return system_ticks; |
| 196 | } |
| 197 | #endif |