Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 1 | #include "chip.h" |
| 2 | |
| 3 | #define EFC_FCMD_STUI 0x0E |
| 4 | #define EFC_FCMD_SPUI 0x0F |
| 5 | |
| 6 | __attribute__ ((section(".ramfunc"))) |
| 7 | void EEFC_ReadUniqueID(unsigned int *pdwUniqueID) |
| 8 | { |
| 9 | unsigned int status; |
| 10 | |
| 11 | /* Errata / Workaround: Set bit 16 of EEFC Flash Mode Register |
| 12 | * to 1 */ |
| 13 | EFC->EEFC_FMR |= (1 << 16); |
| 14 | |
| 15 | /* Send the Start Read unique Identifier command (STUI) by |
| 16 | * writing the Flash Command Register with the STUI command. */ |
| 17 | EFC->EEFC_FCR = (0x5A << 24) | EFC_FCMD_STUI; |
| 18 | |
| 19 | /* Wait for the FRDY bit to fall */ |
| 20 | do { |
| 21 | status = EFC->EEFC_FSR; |
| 22 | } while ((status & EEFC_FSR_FRDY) == EEFC_FSR_FRDY); |
| 23 | |
| 24 | /* The Unique Identifier is located in the first 128 bits of the |
| 25 | * Flash memory mapping. So, at the address 0x400000-0x400003. |
| 26 | * */ |
| 27 | pdwUniqueID[0] = *(uint32_t *) IFLASH_ADDR; |
| 28 | pdwUniqueID[1] = *(uint32_t *) (IFLASH_ADDR + 4); |
| 29 | pdwUniqueID[2] = *(uint32_t *) (IFLASH_ADDR + 8); |
| 30 | pdwUniqueID[3] = *(uint32_t *) (IFLASH_ADDR + 12); |
| 31 | |
| 32 | /* To stop the Unique Identifier mode, the user needs to send |
| 33 | * the Stop Read unique Identifier command (SPUI) by writing the |
| 34 | * Flash Command Register with the SPUI command. */ |
| 35 | EFC->EEFC_FCR = (0x5A << 24) | EFC_FCMD_SPUI; |
| 36 | |
| 37 | /* When the Stop read Unique Unique Identifier command (SPUI) |
| 38 | * has been performed, the FRDY bit in the Flash Programming |
| 39 | * Status Register (EEFC_FSR) rises. */ |
| 40 | do { |
| 41 | status = EFC->EEFC_FSR; |
| 42 | } while ((status & EEFC_FSR_FRDY) != EEFC_FSR_FRDY); |
| 43 | } |