blob: 924bb18e905530343a440ae4641c4fb2e8c3e16d [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
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 "board.h"
30#include "fsl_gpio.h"
31#include "fsl_port.h"
32#include "fsl_clock.h"
33#include "fsl_lpuart.h"
34
35#include "clock_config.h"
36
37//--------------------------------------------------------------------+
38// Forward USB interrupt events to TinyUSB IRQ Handler
39//--------------------------------------------------------------------+
40void USB0_IRQHandler(void)
41{
42 tud_int_handler(0);
43}
44
45void board_init(void)
46{
47 /* Enable port clocks for UART/LED/Button pins */
48 CLOCK_EnableClock(UART_PIN_CLOCK);
49 CLOCK_EnableClock(LED_PIN_CLOCK);
50 CLOCK_EnableClock(BUTTON_PIN_CLOCK);
51
52 gpio_pin_config_t led_config = { kGPIO_DigitalOutput, 0 };
53 GPIO_PinInit(LED_GPIO, LED_PIN, &led_config);
54 PORT_SetPinMux(LED_PORT, LED_PIN, kPORT_MuxAsGpio);
55
56 gpio_pin_config_t button_config = { kGPIO_DigitalInput, 0 };
57 GPIO_PinInit(BUTTON_GPIO, BUTTON_PIN, &button_config);
58 const port_pin_config_t BUTTON_CFG = {
59 kPORT_PullUp,
60 kPORT_FastSlewRate,
61 kPORT_PassiveFilterDisable,
62 kPORT_LowDriveStrength,
63 kPORT_MuxAsGpio
64 };
65 PORT_SetPinConfig(BUTTON_PORT, BUTTON_PIN, &BUTTON_CFG);
66
67 /* PORTA1 (pin 23) is configured as LPUART0_RX */
68 PORT_SetPinMux(PORTA, 1U, kPORT_MuxAlt2);
69 /* PORTA2 (pin 24) is configured as LPUART0_TX */
70 PORT_SetPinMux(PORTA, 2U, kPORT_MuxAlt2);
71
72 SIM->SOPT5 = ((SIM->SOPT5 &
73 /* Mask bits to zero which are setting */
74 (~(SIM_SOPT5_LPUART0TXSRC_MASK | SIM_SOPT5_LPUART0RXSRC_MASK)))
75 /* LPUART0 Transmit Data Source Select: LPUART0_TX pin. */
76 | SIM_SOPT5_LPUART0TXSRC(SOPT5_LPUART0TXSRC_LPUART_TX)
77 /* LPUART0 Receive Data Source Select: LPUART_RX pin. */
78 | SIM_SOPT5_LPUART0RXSRC(SOPT5_LPUART0RXSRC_LPUART_RX));
79
80 BOARD_BootClockRUN();
81 SystemCoreClockUpdate();
82 CLOCK_SetLpuart0Clock(1);
83
84#if CFG_TUSB_OS == OPT_OS_NONE
85 // 1ms tick timer
86 SysTick_Config(SystemCoreClock / 1000);
87#elif CFG_TUSB_OS == OPT_OS_FREERTOS
88 // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
89 NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
90#endif
91
92 lpuart_config_t uart_config;
93 LPUART_GetDefaultConfig(&uart_config);
94 uart_config.baudRate_Bps = CFG_BOARD_UART_BAUDRATE;
95 uart_config.enableTx = true;
96 uart_config.enableRx = true;
97 LPUART_Init(UART_PORT, &uart_config, CLOCK_GetFreq(kCLOCK_McgIrc48MClk));
98
99 // USB
100 CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcIrc48M, 48000000U);
101}
102
103//--------------------------------------------------------------------+
104// Board porting API
105//--------------------------------------------------------------------+
106
107void board_led_write(bool state)
108{
109 GPIO_PinWrite(LED_GPIO, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
110}
111
112uint32_t board_button_read(void)
113{
114 return BUTTON_STATE_ACTIVE == GPIO_PinRead(BUTTON_GPIO, BUTTON_PIN);
115}
116
117int board_uart_read(uint8_t* buf, int len)
118{
119 LPUART_ReadBlocking(UART_PORT, buf, len);
120 return len;
121}
122
123int board_uart_write(void const * buf, int len)
124{
125 LPUART_WriteBlocking(UART_PORT, (uint8_t const*) buf, len);
126 return len;
127}
128
129#if CFG_TUSB_OS == OPT_OS_NONE
130volatile uint32_t system_ticks = 0;
131void SysTick_Handler(void)
132{
133 system_ticks++;
134}
135
136uint32_t board_millis(void)
137{
138 return system_ticks;
139}
140#endif