1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Brent Kowal (Analog Devices, Inc)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
/* metadata:
manufacturer: Analog Devices
*/
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-prototypes" // _mxc_crit_get_state()
#pragma GCC diagnostic ignored "-Wredundant-decls"
#endif
#include "gpio.h"
#include "mxc_sys.h"
#if __has_include("mcr_regs.h")
#include "mcr_regs.h"
#endif
#include "mxc_device.h"
#include "uart.h"
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#include "board.h"
#include "bsp/board_api.h"
//--------------------------------------------------------------------+
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
void USB_IRQHandler(void) {
tud_int_handler(0);
}
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+
mxc_uart_regs_t *ConsoleUart = MXC_UART_GET_UART(UART_NUM);
void board_init(void) {
#if CFG_TUSB_OS == OPT_OS_NONE
// 1ms tick timer
SysTick_Config(SystemCoreClock / 1000);
#elif CFG_TUSB_OS == OPT_OS_FREERTOS
// Explicitly disable systick to prevent its ISR from running before scheduler start
SysTick->CTRL &= ~1U;
// If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
NVIC_SetPriority(USB_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
#endif
mxc_gpio_cfg_t gpioConfig;
// LED
gpioConfig.drvstr = MXC_GPIO_DRVSTR_0;
gpioConfig.func = MXC_GPIO_FUNC_OUT;
gpioConfig.mask = LED_PIN;
gpioConfig.pad = MXC_GPIO_PAD_NONE;
gpioConfig.port = LED_PORT;
gpioConfig.vssel = LED_VDDIO;
MXC_GPIO_Config(&gpioConfig);
board_led_write(false);
// Button
gpioConfig.drvstr = MXC_GPIO_DRVSTR_0;
gpioConfig.func = MXC_GPIO_FUNC_IN;
gpioConfig.mask = BUTTON_PIN;
gpioConfig.pad = BUTTON_PULL;
gpioConfig.port = BUTTON_PORT;
gpioConfig.vssel = MXC_GPIO_VSSEL_VDDIO;
MXC_GPIO_Config(&gpioConfig);
// UART
#if MAX_PERIPH_ID == 14
MXC_UART_Init(ConsoleUart, CFG_BOARD_UART_BAUDRATE, UART_MAP);
#elif MAX_PERIPH_ID == 18 || MAX_PERIPH_ID == 87
MXC_UART_Init(ConsoleUart, CFG_BOARD_UART_BAUDRATE, MXC_UART_IBRO_CLK);
#if MAX_PERIPH_ID == 87
UART_PORT->vssel |= UART_VDDIO_BITS; // Set necessary bits to 3.3V
#endif
#endif
//USB
#if defined(MAX32650)
// Startup the HIRC96M clock if it's not on already
if (!(MXC_GCR->clk_ctrl & MXC_F_GCR_CLK_CTRL_HIRC96_EN)) {
MXC_GCR->clk_ctrl |= MXC_F_GCR_CLK_CTRL_HIRC96_EN;
MXC_SYS_Clock_Timeout(MXC_F_GCR_CLK_CTRL_HIRC96_RDY);
}
MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_USB);
MXC_SYS_Reset_Periph(MXC_SYS_RESET_USB);
#elif defined(MAX32665) || defined(MAX32666)
// Startup the HIRC96M clock if it's not on already
if (!(MXC_GCR->clkcn & MXC_F_GCR_CLKCN_HIRC96M_EN)) {
MXC_GCR->clkcn |= MXC_F_GCR_CLKCN_HIRC96M_EN;
}
MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_USB);
MXC_SYS_Reset_Periph(MXC_SYS_RESET_USB);
#elif defined(MAX32690)
MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IPO);
MXC_MCR->ldoctrl |= MXC_F_MCR_LDOCTRL_0P9EN;
MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_USB);
MXC_SYS_Reset_Periph(MXC_SYS_RESET0_USB);
# elif defined(MAX78002)
MXC_MCR->ldoctrl |= MXC_F_MCR_LDOCTRL_0P9EN;
MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_USB);
#else
#error "Unsupported MAXIM MCU for board_dfu_init"
#endif
}
//--------------------------------------------------------------------+
// Board porting API
//--------------------------------------------------------------------+
void board_led_write(bool state) {
#if LED_STATE_ON
state = !state;
#endif
if (state) {
MXC_GPIO_OutClr(LED_PORT, LED_PIN);
} else {
MXC_GPIO_OutSet(LED_PORT, LED_PIN);
}
}
uint32_t board_button_read(void) {
uint32_t state = MXC_GPIO_InGet(BUTTON_PORT, BUTTON_PIN) ? 1 : 0;
return BUTTON_STATE_ACTIVE == state;
}
size_t board_get_unique_id(uint8_t id[], size_t max_len) {
#if defined(MAX32650)
// USN is 13 bytes on this device
(void) max_len;
MXC_SYS_GetUSN(id, 13);
return 13;
#else
uint8_t hw_id[MXC_SYS_USN_CHECKSUM_LEN]; //USN Buffer
MXC_SYS_GetUSN(hw_id, NULL); // 2nd parameter is optional checksum buffer
size_t act_len = TU_MIN(max_len, MXC_SYS_USN_LEN);
memcpy(id, hw_id, act_len);
return act_len;
#endif
}
int board_uart_read(uint8_t *buf, int len) {
int uart_val;
int act_len = 0;
while (act_len < len) {
if ((uart_val = MXC_UART_ReadCharacterRaw(ConsoleUart)) == E_UNDERFLOW) {
break;
} else {
*buf++ = (uint8_t) uart_val;
act_len++;
}
}
return act_len;
}
int board_uart_write(void const *buf, int len) {
const uint8_t *p = (const uint8_t *) buf;
int count = 0;
while (count < len) {
if (MXC_UART_GetTXFIFOAvailable(ConsoleUart) > 0) {
MXC_UART_WriteCharacterRaw(ConsoleUart, p[count]);
count++;
} else {
break;
}
}
return count;
}
#if CFG_TUSB_OS == OPT_OS_NONE
volatile uint32_t system_ticks = 0;
void SysTick_Handler(void) {
system_ticks++;
}
uint32_t tusb_time_millis_api(void) {
return system_ticks;
}
#endif
void HardFault_Handler(void) {
__asm("BKPT #0\n");
}
// Required by __libc_init_array in startup code if we are compiling using
// -nostdlib/-nostartfiles.
void _init(void) {
}
|