aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2019-11-25 19:30:02 -0500
committerKevin O'Connor <kevin@koconnor.net>2019-11-26 08:42:14 -0500
commit401f7a879cf80c744203a53aed17d05356ddc4fc (patch)
tree6107f4307b4857dcc5527871f6cd3931f88eaf29
parentd7ec5505a6b517e927c93b95b13480eb85e42740 (diff)
downloadkutter-401f7a879cf80c744203a53aed17d05356ddc4fc.tar.gz
kutter-401f7a879cf80c744203a53aed17d05356ddc4fc.tar.xz
kutter-401f7a879cf80c744203a53aed17d05356ddc4fc.zip
atsam: Enable chipid as usb serial number
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
-rw-r--r--docs/Config_Changes.md6
-rw-r--r--src/atsam/Kconfig1
-rw-r--r--src/atsam/Makefile2
-rw-r--r--src/atsam/chipid.c74
4 files changed, 79 insertions, 4 deletions
diff --git a/docs/Config_Changes.md b/docs/Config_Changes.md
index fed5f79d..57cf4176 100644
--- a/docs/Config_Changes.md
+++ b/docs/Config_Changes.md
@@ -6,9 +6,9 @@ All dates in this document are approximate.
# Changes
-20191124: The USB names have changed on lpc176x, stm32, and atsamd.
-They now use the unique chip id by default. Update the "serial"
-setting in the "mcu" config section accordingly.
+20191124: The USB names have changed on lpc176x, stm32, atsamd, and
+atsam. They now use the unique chip id by default. Update the
+"serial" setting in the "mcu" config section accordingly.
20191121: The pressure_advance_lookahead_time parameter has been
removed. See example.cfg for alternate configuration settings.
diff --git a/src/atsam/Kconfig b/src/atsam/Kconfig
index 8b5c426e..d8b8d17c 100644
--- a/src/atsam/Kconfig
+++ b/src/atsam/Kconfig
@@ -11,6 +11,7 @@ config ATSAM_SELECT
select HAVE_GPIO_SPI
select HAVE_GPIO_HARD_PWM
select HAVE_GPIO_BITBANGING
+ select HAVE_CHIPID
config BOARD_DIRECTORY
string
diff --git a/src/atsam/Makefile b/src/atsam/Makefile
index 872fcdde..359cfc3d 100644
--- a/src/atsam/Makefile
+++ b/src/atsam/Makefile
@@ -28,7 +28,7 @@ src-y += generic/armcm_boot.c generic/armcm_irq.c generic/armcm_timer.c
src-y += generic/crc16_ccitt.c
usb-src-$(CONFIG_MACH_SAM3X) := atsam/sam3_usb.c
usb-src-$(CONFIG_MACH_SAM4) := atsam/sam4_usb.c
-src-$(CONFIG_USBSERIAL) += $(usb-src-y) generic/usb_cdc.c
+src-$(CONFIG_USBSERIAL) += $(usb-src-y) atsam/chipid.c generic/usb_cdc.c
src-$(CONFIG_SERIAL) += atsam/serial.c generic/serial_irq.c
src-$(CONFIG_MACH_SAM3X) += atsam/adc.c
src-$(CONFIG_MACH_SAM4S) += atsam/adc.c
diff --git a/src/atsam/chipid.c b/src/atsam/chipid.c
new file mode 100644
index 00000000..b3e58983
--- /dev/null
+++ b/src/atsam/chipid.c
@@ -0,0 +1,74 @@
+// Support for extracting the hardware chip id on sam3/sam4
+//
+// Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include "generic/irq.h" // irq_disable
+#include "generic/usb_cdc.h" // usb_fill_serial
+#include "generic/usbstd.h" // usb_string_descriptor
+#include "internal.h" // EFC0
+#include "sched.h" // DECL_INIT
+
+#define CHIP_UID_LEN 16
+
+static struct {
+ struct usb_string_descriptor desc;
+ uint16_t data[CHIP_UID_LEN * 2];
+} cdc_chipid;
+
+struct usb_string_descriptor *
+usbserial_get_serialid(void)
+{
+ return &cdc_chipid.desc;
+}
+
+// Compatibility definitions for sam4e8e
+#ifndef EFC0
+#define EFC0 EFC
+#define IFLASH0_ADDR IFLASH_ADDR
+#endif
+
+void noinline __section(".ramfunc.read_chip_id")
+read_chip_id(uint32_t *id)
+{
+ // Workaround sam3 errata
+ uint32_t fmr = EFC0->EEFC_FMR;
+ EFC0->EEFC_FMR = fmr | EEFC_FMR_SCOD;
+
+ // Send the STUI command
+ while (!(EFC0->EEFC_FSR & EEFC_FSR_FRDY))
+ ;
+ EFC0->EEFC_FCR = EEFC_FCR_FKEY_PASSWD | EEFC_FCR_FCMD_STUI;
+ while (EFC0->EEFC_FSR & EEFC_FSR_FRDY)
+ ;
+
+ // Copy the id
+ id[0] = *(uint32_t*)(IFLASH0_ADDR + 0x00);
+ id[1] = *(uint32_t*)(IFLASH0_ADDR + 0x04);
+ id[1] = *(uint32_t*)(IFLASH0_ADDR + 0x08);
+ id[1] = *(uint32_t*)(IFLASH0_ADDR + 0x0c);
+
+ // Send the SPUI command
+ EFC0->EEFC_FCR = EEFC_FCR_FKEY_PASSWD | EEFC_FCR_FCMD_SPUI;
+ while (!(EFC0->EEFC_FSR & EEFC_FSR_FRDY))
+ ;
+
+ // Restore fmr
+ EFC0->EEFC_FMR = fmr;
+}
+
+void
+chipid_init(void)
+{
+ if (!CONFIG_USB_SERIAL_NUMBER_CHIPID)
+ return;
+
+ uint32_t id[4];
+ irq_disable();
+ read_chip_id(id);
+ irq_enable();
+
+ usb_fill_serial(&cdc_chipid.desc, ARRAY_SIZE(cdc_chipid.data), id);
+}
+DECL_INIT(chipid_init);