Reading serial number of G120

Here is a way of reading the unique serial number from a G120 in native mode.


 #define IAP_LOCATION              (0x1FFF1FF1UL)
typedef enum
{
    IAP_PREPARE = 50,       // Prepare sector(s) for write operation
    IAP_COPY_RAM2FLASH = 51,     // Copy RAM to Flash
    IAP_ERASE = 52,              // Erase sector(s)
    IAP_BLANK_CHECK = 53,        // Blank check sector(s)
    IAP_READ_PART_ID = 54,       // Read chip part ID
    IAP_READ_BOOT_VER = 55,      // Read chip boot code version
    IAP_COMPARE = 56,            // Compare memory areas
    IAP_REINVOKE_ISP = 57,       // Reinvoke ISP
    IAP_READ_SERIAL_NUMBER = 58, // Read serial number
}  IAP_COMMAND_CODE;

typedef enum
{
    CMD_SUCCESS,	             // Command is executed successfully.
    INVALID_COMMAND,             // Invalid command.
    SRC_ADDR_ERROR,              // Source address is not on a word boundary.
    DST_ADDR_ERROR,              // Destination address is not on a correct boundary.
    SRC_ADDR_NOT_MAPPED,         // Source address is not mapped in the memory map.
    DST_ADDR_NOT_MAPPED,         // Destination address is not mapped in the memory map.
    COUNT_ERROR,	               // Byte count is not multiple of 4 or is not a permitted value.
    INVALID_SECTOR,	           // Sector number is invalid.
    SECTOR_NOT_BLANK,	           // Sector is not blank.
    SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION,	// Command to prepare sector for write operation was not executed.
    COMPARE_ERROR,               // Source and destination data is not same.
    BUSY,		                   // Flash programming hardware interface is busy.
} IAP_STATUS_CODE;

typedef struct {
    uint32_t cmd;   // Command
    uint32_t param[4];      // Parameters
    uint32_t status;        // status code
    uint32_t result[4];     // Result
} IAP_COMMAND_Type;


//  IAP Command
typedef void (*IAP)(uint32_t *cmd,uint32_t *result);
IAP iap_entry = (IAP) IAP_LOCATION;
 #define IAP_Call 	iap_entry

IAP_STATUS_CODE ReadDeviceSerialNumLpc17xx(uint32_t *uid)
{
   IAP_COMMAND_Type iapCmd;
   iapCmd.cmd = IAP_READ_SERIAL_NUMBER;
   IAP_Call (&iapCmd.cmd, &iapCmd.status);

   if(iapCmd.status == CMD_SUCCESS)
   {
      if(uid != NULL)
	  {
	    uint32_t i = 0;
		for(i = 0; i < 4; i++)
        {
	        uid[i] =  iapCmd.result[i];
        }
	  }
   }

   return (IAP_STATUS_CODE)iapCmd.status;
}

If you call ReadDeviceSerialNumLpc17xx you will get the serial number in a uint32[4] array.

Convert this to a 16 byte array an run CRC32 and you will get a 32bit serial number out of the 128bit.

:slight_smile:

4 Likes

Very helpful, you should put this on CodeShare so it’s easy to find. :wink:

Thanks, this is going into my next release :slight_smile: