read SAM3S unique serial number at start and print it

This commit is contained in:
Harald Welte
2016-03-20 16:00:39 +01:00
parent 28772ebcdc
commit 8ee15dbc2a
5 changed files with 56 additions and 1 deletions

View File

@@ -146,7 +146,7 @@ VPATH += src_board src_sam3s cmsis $(USB_PATHS) src_simtrace
# Objects built from C source files
C_CMSIS = core_cm3.o
C_LOWLEVEL = board_cstartup_gnu.o board_lowlevel.o syscalls.o exceptions.o
C_LIBLEVEL = spi.o pio.o pmc.o usart.o pio_it.o pio_capture.o uart_console.o iso7816_4.o wdt.o led.o tc.o
C_LIBLEVEL = spi.o pio.o pmc.o usart.o pio_it.o pio_capture.o uart_console.o iso7816_4.o wdt.o led.o tc.o unique_id.o
C_CCID = cciddriver.o USBD.o USBDDriver.o USBD_HAL.o USBRequests.o USBDCallbacks.o USBDescriptors.o USBDDriverCallbacks.o
C_SIMTRACE = simtrace_iso7816.o usb.o ccid.o sniffer.o mitm.o ringbuffer.o host_communication.o iso7816_fidi.o tc_etu.o req_ctx.o card_emu.o mode_cardemu.o
C_APPLEVEL = main.o

View File

@@ -52,5 +52,6 @@
#include "trace.h"
#include "wdt.h"
#include "unique_id.h"
#endif /* _LIB_SAM3S_ */

View File

@@ -0,0 +1,3 @@
#pragma once
void EEFC_ReadUniqueID(unsigned int *pdwUniqueID);

View File

@@ -0,0 +1,43 @@
#include "chip.h"
#define EFC_FCMD_STUI 0x0E
#define EFC_FCMD_SPUI 0x0F
__attribute__ ((section(".ramfunc")))
void EEFC_ReadUniqueID(unsigned int *pdwUniqueID)
{
unsigned int status;
/* Errata / Workaround: Set bit 16 of EEFC Flash Mode Register
* to 1 */
EFC->EEFC_FMR |= (1 << 16);
/* Send the Start Read unique Identifier command (STUI) by
* writing the Flash Command Register with the STUI command. */
EFC->EEFC_FCR = (0x5A << 24) | EFC_FCMD_STUI;
/* Wait for the FRDY bit to fall */
do {
status = EFC->EEFC_FSR;
} while ((status & EEFC_FSR_FRDY) == EEFC_FSR_FRDY);
/* The Unique Identifier is located in the first 128 bits of the
* Flash memory mapping. So, at the address 0x400000-0x400003.
* */
pdwUniqueID[0] = *(uint32_t *) IFLASH_ADDR;
pdwUniqueID[1] = *(uint32_t *) (IFLASH_ADDR + 4);
pdwUniqueID[2] = *(uint32_t *) (IFLASH_ADDR + 8);
pdwUniqueID[3] = *(uint32_t *) (IFLASH_ADDR + 12);
/* To stop the Unique Identifier mode, the user needs to send
* the Stop Read unique Identifier command (SPUI) by writing the
* Flash Command Register with the SPUI command. */
EFC->EEFC_FCR = (0x5A << 24) | EFC_FCMD_SPUI;
/* When the Stop read Unique Unique Identifier command (SPUI)
* has been performed, the FRDY bit in the Flash Programming
* Status Register (EEFC_FSR) rises. */
do {
status = EFC->EEFC_FSR;
} while ((status & EEFC_FSR_FRDY) != EEFC_FSR_FRDY);
}

View File

@@ -8,6 +8,8 @@
#include "utils.h"
#include "req_ctx.h"
uint32_t g_unique_id[4];
/*------------------------------------------------------------------------------
* Internal variables
*------------------------------------------------------------------------------*/
@@ -102,11 +104,17 @@ extern int main(void)
SIMtrace_USB_Initialize();
EEFC_ReadUniqueID(g_unique_id);
printf("\r\n\r\n"
"=============================================================================\r\n"
"SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\r\n"
"=============================================================================\r\n");
TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n",
g_unique_id[0], g_unique_id[1],
g_unique_id[2], g_unique_id[3]);
TRACE_INFO("USB init...\r\n");
while (USBD_GetState() < USBD_STATE_CONFIGURED) {
if (i >= MAX_USB_ITER * 3) {