8000 samd: Refactor to use shared tinyusb, implement unique id (with cross-port refactor) by projectgus · Pull Request #12908 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

samd: Refactor to use shared tinyusb, implement unique id (with cross-port refactor) #12908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions ports/renesas-ra/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,8 @@

void mp_usbd_port_get_serial_number(char *serial_buf) {
const bsp_unique_id_t *id = R_BSP_UniqueIdGet();
// convert to hex
int hexlen = sizeof(id->unique_id_bytes) * 2;
MP_STATIC_ASSERT(hexlen <= MICROPY_HW_USB_DESC_STR_MAX);
for (int i = 0; i < hexlen; i += 2) {
static const char *hexdig = "0123456789abcdef";
serial_buf[i] = hexdig[id->unique_id_bytes[i / 2] >> 4];
serial_buf[i + 1] = hexdig[id->unique_id_bytes[i / 2] & 0x0f];
}
serial_buf[hexlen] = 0;
MP_STATIC_ASSERT(sizeof(id->unique_id_bytes) * 2 <= MICROPY_HW_USB_DESC_STR_MAX);
mp_usbd_hex_str(serial_buf, id->unique_id_bytes, sizeof(id->unique_id_bytes));
}

#endif
11 changes: 2 additions & 9 deletions ports/rp2/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,8 @@
void mp_usbd_port_get_serial_number(char *serial_buf) {
pico_unique_board_id_t id;
pico_get_unique_board_id(&id);
// convert to hex
int hexlen = sizeof(id.id) * 2;
MP_STATIC_ASSERT(hexlen <= MICROPY_HW_USB_DESC_STR_MAX);
for (int i = 0; i < hexlen; i += 2) {
static const char *hexdig = "0123456789abcdef";
serial_buf[i] = hexdig[id.id[i / 2] >> 4];
serial_buf[i + 1] = hexdig[id.id[i / 2] & 0x0f];
}
serial_buf[hexlen] = 0;
MP_STATIC_ASSERT(sizeof(id.id) * 2 <= MICROPY_HW_USB_DESC_STR_MAX);
mp_usbd_hex_str(serial_buf, id.id, sizeof(id.id));
}

#endif
6 changes: 4 additions & 2 deletions ports/samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/hpl/tc
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/include
INC += -I$(TOP)/lib/asf4/$(MCU_SERIES_LOWER)/include/pio
INC += -I$(TOP)/lib/tinyusb/src
INC += -I$(TOP)/shared/tinyusb/

MAKE_PINS = boards/make-pins.py
BOARD_PINS = $(BOARD_DIR)/pins.csv
Expand Down Expand Up @@ -117,7 +118,7 @@ SRC_C += \
samd_qspiflash.c \
samd_soc.c \
samd_spiflash.c \
tusb_port.c \
usbd.c \

SHARED_SRC_C += \
drivers/dht/dht.c \
Expand All @@ -133,7 +134,8 @@ SHARED_SRC_C += \
shared/runtime/sys_stdio_mphal.c \
shared/timeutils/timeutils.c \
shared/tinyusb/mp_cdc_common.c \
shared/tinyusb/mp_usbd.c
shared/tinyusb/mp_usbd.c \
shared/tinyusb/mp_usbd_descriptor.c \

ASF4_SRC_C += $(addprefix lib/asf4/$(MCU_SERIES_LOWER)/,\
hal/src/hal_atomic.c \
Expand Down
2 changes: 2 additions & 0 deletions ports/samd/mcu/samd21/mpconfigmcu.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
CFLAGS_MCU += -mtune=cortex-m0plus -mcpu=cortex-m0plus -msoft-float

CFLAGS_MCU += -DCFG_TUSB_MCU=OPT_MCU_SAMD21

MPY_CROSS_MCU_ARCH = armv6m

MICROPY_HW_CODESIZE ?= 184K
Expand Down
2 changes: 2 additions & 0 deletions ports/samd/mcu/samd51/mpconfigmcu.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
CFLAGS_MCU += -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard

CFLAGS_MCU += -DCFG_TUSB_MCU=OPT_MCU_SAMD51

MPY_CROSS_MCU_ARCH = armv7m

MICROPY_HW_CODESIZE ?= 368K
Expand Down
42 changes: 3 additions & 39 deletions ports/samd/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,45 +92,9 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq);

STATIC mp_obj_t machine_unique_id(void) {
// Each device has a unique 128-bit serial number which is a concatenation of four 32-bit
// words contained at the following addresses. The uniqueness of the serial number is
// guaranteed only when using all 128 bits.
// Atmel SAM D21E / SAM D21G / SAM D21J
// SMART ARM-Based Microcontroller
// DATASHEET
// 9.6 (SAMD51) or 9.3.3 (or 10.3.3 depending on which manual)(SAMD21) Serial Number
//
// EXAMPLE (SAMD21)
// ----------------
// OpenOCD:
// Word0:
// > at91samd21g18.cpu mdw 0x0080A00C 1
// 0x0080a00c: 6e27f15f
// Words 1-3:
// > at91samd21g18.cpu mdw 0x0080A040 3
// 0x0080a040: 50534b54 332e3120 ff091645
//
// MicroPython (this code and same order as shown in Arduino IDE)
// >>> binascii.hexlify(machine.unique_id())
// b'6e27f15f50534b54332e3120ff091645'

#if defined(MCU_SAMD21)
uint32_t *id_addresses[4] = {(uint32_t *)0x0080A00C, (uint32_t *)0x0080A040,
(uint32_t *)0x0080A044, (uint32_t *)0x0080A048};
#elif defined(MCU_SAMD51)
uint32_t *id_addresses[4] = {(uint32_t *)0x008061FC, (uint32_t *)0x00806010,
(uint32_t *)0x00806014, (uint32_t *)0x00806018};
#endif
uint8_t raw_id[16];

for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
// 'Reverse' the read bytes into a 32 bit word (Consistent with Arduino)
raw_id[4 * i + k] = (*(id_addresses[i]) >> (24 - k * 8)) & 0xff;
}
}

return mp_obj_new_bytes((byte *)&raw_id, sizeof(raw_id));
samd_unique_id_t id;
samd_get_unique_id(&id);
return mp_obj_new_bytes((byte *)&id.bytes, sizeof(id.bytes));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id);

Expand Down
21 changes: 21 additions & 0 deletions ports/samd/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@
#define MICROPY_HW_ENABLE_USBDEV (1)
#define MICROPY_HW_USB_CDC_1200BPS_TOUCH (1)

#if MICROPY_HW_ENABLE_USBDEV
// Enable USB-CDC serial port
#ifndef MICROPY_HW_USB_CDC
#define MICROPY_HW_USB_CDC (1)
#endif
// SAMD unique ID is 16 bytes (hex string == 32)
#ifndef MICROPY_HW_USB_DESC_STR_MAX
#define MICROPY_HW_USB_DESC_STR_MAX (32)
#endif

#endif

// Control over Python builtins
#define MICROPY_PY_BUILTINS_BYTES_HEX (1)
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
Expand Down Expand Up @@ -141,6 +153,15 @@

#define MP_STATE_PORT MP_STATE_VM

// Miscellaneous settings

#ifndef MICROPY_HW_USB_VID
#define MICROPY_HW_USB_VID (0xf055)
#endif
#ifndef MICROPY_HW_USB_PID
#define MICROPY_HW_USB_PID (0x9802)
#endif

// Additional entries for use with pendsv_schedule_dispatch.
#ifndef MICROPY_BOARD_PENDSV_ENTRIES
#define MICROPY_BOARD_PENDSV_ENTRIES
Expand Down
36 changes: 36 additions & 0 deletions ports/samd/samd_soc.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,39 @@ void sercom_deinit_all(void) {
}

#endif

void samd_get_unique_id(samd_unique_id_t *id) {
// Atmel SAM D21E / SAM D21G / SAM D21J
// SMART ARM-Based Microcontroller
// DATASHEET
// 9.6 (SAMD51) or 9.3.3 (or 10.3.3 depending on which manual)(SAMD21) Serial Number
//
// EXAMPLE (SAMD21)
// ----------------
// OpenOCD:
// Word0:
// > at91samd21g18.cpu mdw 0x0080A00C 1
// 0x0080a00c: 6e27f15f
// Words 1-3:
// > at91samd21g18.cpu mdw 0x0080A040 3
// 0x0080a040: 50534b54 332e3120 ff091645
//
// MicroPython (this code and same order as shown in Arduino IDE)
// >>> binascii.hexlify(machine.unique_id())
// b'6e27f15f50534b54332e3120ff091645'

#if defined(MCU_SAMD21)
uint32_t *id_addresses[4] = {(uint32_t *)0x0080A00C, (uint32_t *)0x0080A040,
(uint32_t *)0x0080A044, (uint32_t *)0x0080A048};
#elif defined(MCU_SAMD51)
uint32_t *id_addresses[4] = {(uint32_t *)0x008061FC, (uint32_t *)0x00806010,
(uint32_t *)0x00806014, (uint32_t *)0x00806018};
#endif

for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
// 'Reverse' the read bytes into a 32 bit word (Consistent with Arduino)
id->bytes[4 * i + k] = (*(id_addresses[i]) >> (24 - k * 8)) & 0xff;
}
}
}
8 changes: 8 additions & 0 deletions ports/samd/samd_soc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#include "sam.h"
#include "clock_config.h"

typedef struct _samd_unique_id_t {
uint8_t bytes[16];
} samd_unique_id_t;

extern Sercom *sercom_instance[];

void samd_init(void);
Expand All @@ -40,6 +44,10 @@ void USB_Handler_wrapper(void);
void sercom_enable(Sercom *spi, int state);
void sercom_register_irq(int sercom_id, void (*sercom_irq_handler));

// Each device has a unique 128-bit serial number. The uniqueness of the serial number is
// guaranteed only when using all 128 bits.
void samd_get_unique_id(samd_unique_id_t *id);

#define SERCOM_IRQ_TYPE_UART (0)
#define SERCOM_IRQ_TYPE_SPI (1)
#define SERCOM_IRQ_TYPE_I2C (2)
Expand Down
122 changes: 0 additions & 122 deletions ports/samd/tusb_port.c

This file was deleted.

36 changes: 18 additions & 18 deletions ports/samd/tusb_config.h → ports/samd/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Damien P. George
* Copyright (c) 2023 Angus Gratton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,25 +23,25 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SAMD_TUSB_CONFIG_H
#define MICROPY_INCLUDED_SAMD_TUSB_CONFIG_H

// Common configuration
#include "py/mpconfig.h"

#if defined(MCU_SAMD21)
#define CFG_TUSB_MCU OPT_MCU_SAMD21
#elif defined(MCU_SAMD51)
#define CFG_TUSB_MCU OPT_MCU_SAMD51
#endif
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#define CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
#if MICROPY_HW_ENABLE_USBDEV

#include "mp_usbd.h"
#include "string.h"
#include "tusb.h"
#include "samd_soc.h"

// Device configuration
void mp_usbd_port_get_serial_number(char *serial_buf) {
samd_unique_id_t id;
samd_get_unique_id(&id);
MP_STATIC_ASSERT(sizeof(id.bytes) * 2 <= MICROPY_HW_USB_DESC_STR_MAX);
mp_usbd_hex_str(serial_buf, id.bytes, sizeof(id.bytes));
}

#define CFG_TUD_ENDOINT0_SIZE (64)
#define CFG_TUD_CDC (1)
#define CFG_TUD_CDC_RX_BUFSIZE (64)
#define CFG_TUD_CDC_TX_BUFSIZE (64)
void USB_Handler_wrapper(void) {
tud_int_handler(0);
}

#endif // MICROPY_INCLUDED_SAMD_TUSB_CONFIG_H
#endif
Loading
0