blob: a9a67ae3aad49813ec282002bd93cfbabdd2a249 [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 "../board.h"
29
30//--------------------------------------------------------------------+
31// Forward USB interrupt events to TinyUSB IRQ Handler
32//--------------------------------------------------------------------+
33void USB_IRQHandler(void)
34{
35 tud_int_handler(0);
36}
37
38//--------------------------------------------------------------------+
39// MACRO TYPEDEF CONSTANT ENUM
40//--------------------------------------------------------------------+
41#define LED_PORT 0
42#define LED_PIN 7
43
44// Joytick Down if connected to LPCXpresso Base board
45#define BUTTON_PORT 1
46#define BUTTON_PIN 20
47
48//static const struct {
49// uint8_t port;
50// uint8_t pin;
51//} buttons[] =
52//{
53// {1, 22 }, // Joystick up
54// {1, 20 }, // Joystick down
55// {1, 23 }, // Joystick left
56// {1, 21 }, // Joystick right
57// {1, 19 }, // Joystick press
58// {0, 1 }, // SW3
59//};
60
61/* System oscillator rate and RTC oscillator rate */
62const uint32_t OscRateIn = 12000000;
63const uint32_t ExtRateIn = 0;
64
65/* Pin muxing table, only items that need changing from their default pin
66 state are in this table. */
67static const PINMUX_GRP_T pinmuxing[] =
68{
69 {0, 1, (IOCON_FUNC1 | IOCON_RESERVED_BIT_7 | IOCON_MODE_INACT)}, /* PIO0_1 used for CLKOUT */
70 {0, 2, (IOCON_FUNC1 | IOCON_RESERVED_BIT_7 | IOCON_MODE_PULLUP)}, /* PIO0_2 used for SSEL */
71 {0, 3, (IOCON_FUNC1 | IOCON_RESERVED_BIT_7 | IOCON_MODE_INACT)}, /* PIO0_3 used for USB_VBUS */
72 {0, 6, (IOCON_FUNC1 | IOCON_RESERVED_BIT_7 | IOCON_MODE_INACT)}, /* PIO0_6 used for USB_CONNECT */
73 {0, 8, (IOCON_FUNC1 | IOCON_RESERVED_BIT_7 | IOCON_MODE_INACT)}, /* PIO0_8 used for MISO0 */
74 {0, 9, (IOCON_FUNC1 | IOCON_RESERVED_BIT_7 | IOCON_MODE_INACT)}, /* PIO0_9 used for MOSI0 */
75 {0, 11, (IOCON_FUNC2 | IOCON_ADMODE_EN | IOCON_FILT_DIS)}, /* PIO0_11 used for AD0 */
76 {0, 18, (IOCON_FUNC1 | IOCON_RESERVED_BIT_7 | IOCON_MODE_INACT)}, /* PIO0_18 used for RXD */
77 {0, 19, (IOCON_FUNC1 | IOCON_RESERVED_BIT_7 | IOCON_MODE_INACT)}, /* PIO0_19 used for TXD */
78 {1, 29, (IOCON_FUNC1 | IOCON_RESERVED_BIT_7 | IOCON_MODE_INACT)}, /* PIO1_29 used for SCK0 */
79};
80
81// Invoked by startup code
82void SystemInit(void)
83{
84 /* Enable IOCON clock */
85 Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_IOCON);
86 Chip_IOCON_SetPinMuxing(LPC_IOCON, pinmuxing, sizeof(pinmuxing) / sizeof(PINMUX_GRP_T));
87 Chip_SetupXtalClocking();
88}
89
90void board_init(void)
91{
92 SystemCoreClockUpdate();
93
94#if CFG_TUSB_OS == OPT_OS_NONE
95 // 1ms tick timer
96 SysTick_Config(SystemCoreClock / 1000);
97#elif CFG_TUSB_OS == OPT_OS_FREERTOS
98 // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
99 NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
100#endif
101
102 Chip_GPIO_Init(LPC_GPIO_PORT);
103
104 // LED
105 Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, LED_PORT, LED_PIN);
106
107 // Button
108 Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, BUTTON_PORT, BUTTON_PIN);
109
110 // USB: Setup PLL clock, and power
111 Chip_USB_Init();
112}
113
114//--------------------------------------------------------------------+
115// Board porting API
116//--------------------------------------------------------------------+
117
118#if CFG_TUSB_OS == OPT_OS_NONE
119volatile uint32_t system_ticks = 0;
120void SysTick_Handler (void)
121{
122 system_ticks++;
123}
124
125uint32_t board_millis(void)
126{
127 return system_ticks;
128}
129#endif
130
131void board_led_write(bool state)
132{
133 Chip_GPIO_SetPinState(LPC_GPIO_PORT, LED_PORT, LED_PIN, state);
134}
135
136uint32_t board_button_read(void)
137{
138 // active low
139 return Chip_GPIO_GetPinState(LPC_GPIO_PORT, BUTTON_PORT, BUTTON_PIN) ? 0 : 1;
140}
141
142int board_uart_read(uint8_t* buf, int len)
143{
144 (void) buf; (void) len;
145 return 0;
146}
147
148int board_uart_write(void const * buf, int len)
149{
150 (void) buf; (void) len;
151 return 0;
152}