blob: ba0b2700a2c404d52403a20321c1eeb5f10f52a9 [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 "bsp/board.h"
28#include "board.h"
29
30#include "broadcom/interrupts.h"
31#include "broadcom/io.h"
32#include "broadcom/mmu.h"
33#include "broadcom/caches.h"
34#include "broadcom/vcmailbox.h"
35
36// LED
37#define LED_PIN 18
38#define LED_STATE_ON 1
39
40// Button
41#define BUTTON_PIN 16
42#define BUTTON_STATE_ACTIVE 0
43
44//--------------------------------------------------------------------+
45// Forward USB interrupt events to TinyUSB IRQ Handler
46//--------------------------------------------------------------------+
47void USB_IRQHandler(void)
48{
49 tud_int_handler(0);
50}
51
52//--------------------------------------------------------------------+
53// MACRO TYPEDEF CONSTANT ENUM
54//--------------------------------------------------------------------+
55
56//--------------------------------------------------------------------+
57// Board porting API
58//--------------------------------------------------------------------+
59void board_init(void)
60{
61 setup_mmu_flat_map();
62 init_caches();
63
64 // LED
65 gpio_initOutputPinWithPullNone(LED_PIN);
66 board_led_write(true);
67
68 // Button
69 // TODO
70
71 // Uart
72 uart_init();
73
74 // Turn on USB peripheral.
75 vcmailbox_set_power_state(VCMAILBOX_DEVICE_USB_HCD, true);
76
77 // Timer 1/1024 second tick
78 SYSTMR->CS_b.M1 = 1;
79 SYSTMR->C1 = SYSTMR->CLO + 977;
80 BP_EnableIRQ(TIMER_1_IRQn);
81
82 BP_SetPriority(USB_IRQn, 0x00);
83 BP_ClearPendingIRQ(USB_IRQn);
84 BP_EnableIRQ(USB_IRQn);
85 BP_EnableIRQs();
86}
87
88void board_led_write(bool state)
89{
90 gpio_setPinOutputBool(LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
91}
92
93uint32_t board_button_read(void)
94{
95 return 0;
96}
97
98int board_uart_read(uint8_t* buf, int len)
99{
100 (void) buf; (void) len;
101 return 0;
102}
103
104int board_uart_write(void const * buf, int len)
105{
106 for (int i = 0; i < len; i++) {
107 const char* cbuf = buf;
108 while (!UART1->STAT_b.TX_READY) {}
109 if (cbuf[i] == '\n') {
110 UART1->IO = '\r';
111 while (!UART1->STAT_b.TX_READY) {}
112 }
113 UART1->IO = cbuf[i];
114 }
115 return len;
116}
117
118#if CFG_TUSB_OS == OPT_OS_NONE
119volatile uint32_t system_ticks = 0;
120
121void TIMER_1_IRQHandler(void)
122{
123 system_ticks++;
124 SYSTMR->C1 += 977;
125 SYSTMR->CS_b.M1 = 1;
126}
127
128uint32_t board_millis(void)
129{
130 return system_ticks;
131}
132#endif
133
134void HardFault_Handler (void)
135{
136 // asm("bkpt");
137}
138
139// Required by __libc_init_array in startup code if we are compiling using
140// -nostdlib/-nostartfiles.
141void _init(void)
142{
143
144}