forked from modm-io/modm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtusb_port.cpp.in
123 lines (105 loc) · 3.19 KB
/
tusb_port.cpp.in
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
/*
* Copyright (c) 2020, Erik Henriksson
* Copyright (c) 2020, 2024, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------
#include <modm/platform/device.hpp>
#include <modm/architecture/interface/atomic_lock.hpp>
#include <modm/architecture/interface/delay.hpp>
#include <modm/architecture/interface/interrupt.hpp>
#include "tusb.h"
%% for irq, port, _ in irqs | sort
MODM_ISR({{irq}})
{
tusb_int_handler({{port}}, true);
}
%% endfor
%% if "samg55" in target.string
// Read unique ID using the flash controller
// This function is placed in RAM, because code cannot be fetched from flash
// while reading the unique ID.
modm_ramcode void read_efc_uid(uint32_t *uid)
{
// Disable interrupts -- we can't fetch code
// This function was measured to take 660ns on a SAMG55 running at 100MHz
modm::atomic::Lock lck;
// Issue Start Read Unique Identifier command
EFC->EEFC_FCR = EEFC_FCR_FCMD_STUI | EEFC_FCR_FKEY_PASSWD;
// Wait for flash controller ready bit to go low
while(EFC->EEFC_FSR & EEFC_FSR_FRDY) {};
for(uint32_t i=0; i<4; i++) {
uid[i] = *(uint32_t*)(IFLASH_ADDR + i * 4);
}
// Issue Stop Read Unique Identifier command
EFC->EEFC_FCR = EEFC_FCR_FCMD_SPUI | EEFC_FCR_FKEY_PASSWD;
// Wait for flash controller ready bit to go high
while(!(EFC->EEFC_FSR & EEFC_FSR_FRDY)) {};
}
%% endif
extern "C" uint8_t
tusb_get_device_serial(uint16_t *serial_str)
{
constexpr uint8_t SERIAL_BYTE_LEN = 16;
uint8_t raw_id[SERIAL_BYTE_LEN];
%% if "samg55" in target.string
read_efc_uid((uint32_t*)raw_id);
%% else
uint32_t *id_addresses[4] =
{
%% if target.platform in ["stm32"]
((uint32_t *) UID_BASE), ((uint32_t *) UID_BASE) + 1,
((uint32_t *) UID_BASE) + 2, ((uint32_t *) UID_BASE) + 3
%% elif target.platform in ["sam"]
%% if "samd51" in target.string or "same5" in target.string
(uint32_t *)0x008061FC, (uint32_t *)0x00806010,
(uint32_t *)0x00806014, (uint32_t *)0x00806018
%% else
(uint32_t *)0x0080A00C, (uint32_t *)0x0080A040,
(uint32_t *)0x0080A044, (uint32_t *)0x0080A048
%% endif
%% elif target.platform in ["rp"]
/* sysinfo CHIP_ID and GIT_REV */
((uint32_t *)0x40000000),((uint32_t *)0x40000040),
((uint32_t *)0x40000000),((uint32_t *)0x40000040),
%% endif
};
for (int i = 0; i < 4; i++)
for (int k = 0; k < 4; k++)
raw_id[4 * i + (3 - k)] = (*(id_addresses[i]) >> k * 8) & 0xff;
%% endif
const auto fn_nibble = [](uint8_t nibble)
{
return nibble + (nibble > 9 ? 'A' - 10 : '0');
};
for (unsigned int i = 0; i < sizeof(raw_id); i++)
{
serial_str[i * 2 + 1] = fn_nibble(raw_id[i] & 0xf);
serial_str[i * 2] = fn_nibble(raw_id[i] >> 4 & 0xf);
}
return sizeof(raw_id) * 2;
}
extern "C" void
tusb_time_delay_ms_api(uint32_t ms)
{
modm::delay_ms(ms);
}
%% if with_debug
#ifdef CFG_TUSB_DEBUG
#include <modm/debug.hpp>
extern "C" int
tinyusb_debug_printf(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
modm::log::debug.vprintf(fmt, va);
va_end(va);
return 0;
}
#endif
%% endif