blob: 7f5984a3e3037a7fc9b4c005f37d6323a690ddde [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
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 "bsp/board.h"
29#include "board.h"
30
31//--------------------------------------------------------------------+
32// Forward USB interrupt events to TinyUSB IRQ Handler
33//--------------------------------------------------------------------+
34void USB_IRQHandler(void)
35{
36 tud_int_handler(0);
37}
38
39//--------------------------------------------------------------------+
40// MACRO TYPEDEF CONSTANT ENUM
41//--------------------------------------------------------------------+
42
43/* System oscillator rate and RTC oscillator rate */
44const uint32_t OscRateIn = XTAL_OscRateIn;
45const uint32_t RTCOscRateIn = XTAL_RTCOscRateIn;
46
47// Invoked by startup code
48void SystemInit(void)
49{
50 Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SRAM1);
51 Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SRAM2);
52 Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_IOCON);
53 Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);
54 Chip_SYSCTL_PeriphReset(RESET_IOCON);
55
56 board_lpc15_pinmux_swm_init();
57
58 Chip_SetupXtalClocking();
59}
60
61void board_init(void)
62{
63 SystemCoreClockUpdate();
64
65 // 1ms tick timer
66 SysTick_Config(SystemCoreClock / 1000);
67
68#if CFG_TUSB_OS == OPT_OS_FREERTOS
69 // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
70 NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
71#endif
72
73 Chip_GPIO_Init(LPC_GPIO);
74
75 // LED
76 Chip_GPIO_SetPinDIROutput(LPC_GPIO, LED_PORT, LED_PIN);
77
78 // Button
79 Chip_GPIO_SetPinDIRInput(LPC_GPIO, BUTTON_PORT, BUTTON_PIN);
80
81 // UART
82 Chip_Clock_SetUARTBaseClockRate(Chip_Clock_GetMainClockRate(), false);
83 Chip_UART_Init(UART_PORT);
84 Chip_UART_ConfigData(UART_PORT, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1);
85 Chip_UART_SetBaud(UART_PORT, CFG_BOARD_UART_BAUDRATE);
86 Chip_UART_Enable(UART_PORT);
87 Chip_UART_TXEnable(UART_PORT);
88
89 // USB: Setup PLL clock, and power
90 Chip_USB_Init();
91}
92
93//--------------------------------------------------------------------+
94// Board porting API
95//--------------------------------------------------------------------+
96
97void board_led_write(bool state)
98{
99 Chip_GPIO_SetPinState(LPC_GPIO, LED_PORT, LED_PIN, state);
100}
101
102uint32_t board_button_read(void)
103{
104 // active low
105 return Chip_GPIO_GetPinState(LPC_GPIO, BUTTON_PORT, BUTTON_PIN) ? 0 : 1;
106}
107
108int board_uart_read(uint8_t* buf, int len)
109{
110 (void) buf; (void) len;
111 return 0;
112}
113
114int board_uart_write(void const * buf, int len)
115{
116 return Chip_UART_SendBlocking(UART_PORT, buf, len);
117}
118
119#if CFG_TUSB_OS == OPT_OS_NONE
120volatile uint32_t system_ticks = 0;
121void SysTick_Handler (void)
122{
123 system_ticks++;
124}
125
126uint32_t board_millis(void)
127{
128 return system_ticks;
129}
130#endif