DipCortex EEprom IAP library
Dependents: DipCortex-USB-EEProm vfd_modular_clock_mbed
DipCortex EEprom library.
Example project :
Import programDipCortex-USB-EEProm
DipCortex USB CDC + EEprom
DipCortex-EEprom.cpp@0:53297f36733d, 2014-02-23 (annotated)
- Committer:
- SolderSplashLabs
- Date:
- Sun Feb 23 22:52:19 2014 +0000
- Revision:
- 0:53297f36733d
DipCortex EEprom, port of the NXP IAP code that allows reading and writing to the on chip 4KB eeprom.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SolderSplashLabs | 0:53297f36733d | 1 | //----------------------------------------------------------------------------- |
SolderSplashLabs | 0:53297f36733d | 2 | //* Software that is described herein is for illustrative purposes only |
SolderSplashLabs | 0:53297f36733d | 3 | //* which provides customers with programming information regarding the |
SolderSplashLabs | 0:53297f36733d | 4 | //* products. This software is supplied "AS IS" without any warranties. |
SolderSplashLabs | 0:53297f36733d | 5 | //* NXP Semiconductors assumes no responsibility or liability for the |
SolderSplashLabs | 0:53297f36733d | 6 | //* use of the software, conveys no license or title under any patent, |
SolderSplashLabs | 0:53297f36733d | 7 | //* copyright, or mask work right to the product. NXP Semiconductors |
SolderSplashLabs | 0:53297f36733d | 8 | //* reserves the right to make changes in the software without |
SolderSplashLabs | 0:53297f36733d | 9 | //* notification. NXP Semiconductors also make no representation or |
SolderSplashLabs | 0:53297f36733d | 10 | //* warranty that such application will be suitable for the specified |
SolderSplashLabs | 0:53297f36733d | 11 | //* use without further testing or modification. |
SolderSplashLabs | 0:53297f36733d | 12 | //* Permission to use, copy, modify, and distribute this software and its |
SolderSplashLabs | 0:53297f36733d | 13 | //* documentation is hereby granted, under NXP Semiconductors' |
SolderSplashLabs | 0:53297f36733d | 14 | //* relevant copyright in the software, without fee, provided that it |
SolderSplashLabs | 0:53297f36733d | 15 | //* is used in conjunction with NXP Semiconductors microcontrollers. This |
SolderSplashLabs | 0:53297f36733d | 16 | //* copyright, permission, and disclaimer notice must appear in all copies of |
SolderSplashLabs | 0:53297f36733d | 17 | //* this code. |
SolderSplashLabs | 0:53297f36733d | 18 | //----------------------------------------------------------------------------- |
SolderSplashLabs | 0:53297f36733d | 19 | |
SolderSplashLabs | 0:53297f36733d | 20 | #include <stdint.h> |
SolderSplashLabs | 0:53297f36733d | 21 | #include <string.h> |
SolderSplashLabs | 0:53297f36733d | 22 | #include "DipCortex-EEprom.h" |
SolderSplashLabs | 0:53297f36733d | 23 | #include "LPC13Uxx.h" |
SolderSplashLabs | 0:53297f36733d | 24 | |
SolderSplashLabs | 0:53297f36733d | 25 | unsigned param_table[5]; |
SolderSplashLabs | 0:53297f36733d | 26 | unsigned result_table[5]; |
SolderSplashLabs | 0:53297f36733d | 27 | |
SolderSplashLabs | 0:53297f36733d | 28 | unsigned cclk; |
SolderSplashLabs | 0:53297f36733d | 29 | char flash_buf[FLASH_BUF_SIZE]; |
SolderSplashLabs | 0:53297f36733d | 30 | unsigned * flash_address; |
SolderSplashLabs | 0:53297f36733d | 31 | unsigned byte_ctr; |
SolderSplashLabs | 0:53297f36733d | 32 | |
SolderSplashLabs | 0:53297f36733d | 33 | #define iap_entry ((void (*)(unsigned [],unsigned []))(IAP_ADDRESS)) |
SolderSplashLabs | 0:53297f36733d | 34 | |
SolderSplashLabs | 0:53297f36733d | 35 | void IAP_WriteData(unsigned cclk,unsigned flash_address,unsigned * flash_data_buf, unsigned count); |
SolderSplashLabs | 0:53297f36733d | 36 | void IAP_PrepareEraseSector( unsigned flash_address ); |
SolderSplashLabs | 0:53297f36733d | 37 | void IAP_EraseSector(unsigned start_sector,unsigned end_sector,unsigned cclk); |
SolderSplashLabs | 0:53297f36733d | 38 | void IAP_PrepareSector(unsigned start_sector,unsigned end_sector,unsigned cclk); |
SolderSplashLabs | 0:53297f36733d | 39 | |
SolderSplashLabs | 0:53297f36733d | 40 | |
SolderSplashLabs | 0:53297f36733d | 41 | void IAP_WriteData(unsigned cclk,unsigned flash_address,unsigned * flash_data_buf, unsigned count) |
SolderSplashLabs | 0:53297f36733d | 42 | { |
SolderSplashLabs | 0:53297f36733d | 43 | param_table[0] = COPY_RAM_TO_FLASH; |
SolderSplashLabs | 0:53297f36733d | 44 | param_table[1] = flash_address; |
SolderSplashLabs | 0:53297f36733d | 45 | param_table[2] = (unsigned)flash_data_buf; |
SolderSplashLabs | 0:53297f36733d | 46 | param_table[3] = count; |
SolderSplashLabs | 0:53297f36733d | 47 | param_table[4] = cclk; |
SolderSplashLabs | 0:53297f36733d | 48 | iap_entry(param_table,result_table); |
SolderSplashLabs | 0:53297f36733d | 49 | } |
SolderSplashLabs | 0:53297f36733d | 50 | |
SolderSplashLabs | 0:53297f36733d | 51 | void IAP_EraseSector(unsigned start_sector,unsigned end_sector,unsigned cclk) |
SolderSplashLabs | 0:53297f36733d | 52 | { |
SolderSplashLabs | 0:53297f36733d | 53 | param_table[0] = ERASE_SECTOR; |
SolderSplashLabs | 0:53297f36733d | 54 | param_table[1] = start_sector; |
SolderSplashLabs | 0:53297f36733d | 55 | param_table[2] = end_sector; |
SolderSplashLabs | 0:53297f36733d | 56 | param_table[3] = cclk; |
SolderSplashLabs | 0:53297f36733d | 57 | iap_entry(param_table,result_table); |
SolderSplashLabs | 0:53297f36733d | 58 | } |
SolderSplashLabs | 0:53297f36733d | 59 | |
SolderSplashLabs | 0:53297f36733d | 60 | void IAP_PrepareSector(unsigned start_sector,unsigned end_sector,unsigned cclk) |
SolderSplashLabs | 0:53297f36733d | 61 | { |
SolderSplashLabs | 0:53297f36733d | 62 | param_table[0] = PREPARE_SECTOR_FOR_WRITE; |
SolderSplashLabs | 0:53297f36733d | 63 | param_table[1] = start_sector; |
SolderSplashLabs | 0:53297f36733d | 64 | param_table[2] = end_sector; |
SolderSplashLabs | 0:53297f36733d | 65 | param_table[3] = cclk; |
SolderSplashLabs | 0:53297f36733d | 66 | iap_entry(param_table,result_table); |
SolderSplashLabs | 0:53297f36733d | 67 | } |
SolderSplashLabs | 0:53297f36733d | 68 | |
SolderSplashLabs | 0:53297f36733d | 69 | int IAP_ErasePage ( unsigned startPageNo, unsigned endPageNo, unsigned cclk ) |
SolderSplashLabs | 0:53297f36733d | 70 | { |
SolderSplashLabs | 0:53297f36733d | 71 | param_table[0] = ERASE_PAGE; |
SolderSplashLabs | 0:53297f36733d | 72 | param_table[1] = startPageNo; |
SolderSplashLabs | 0:53297f36733d | 73 | param_table[2] = endPageNo; |
SolderSplashLabs | 0:53297f36733d | 74 | param_table[3] = cclk; |
SolderSplashLabs | 0:53297f36733d | 75 | iap_entry(param_table,result_table); |
SolderSplashLabs | 0:53297f36733d | 76 | |
SolderSplashLabs | 0:53297f36733d | 77 | if( result_table[0] == CMD_SUCCESS ) |
SolderSplashLabs | 0:53297f36733d | 78 | { |
SolderSplashLabs | 0:53297f36733d | 79 | return (1); |
SolderSplashLabs | 0:53297f36733d | 80 | } |
SolderSplashLabs | 0:53297f36733d | 81 | else |
SolderSplashLabs | 0:53297f36733d | 82 | { |
SolderSplashLabs | 0:53297f36733d | 83 | return (0); |
SolderSplashLabs | 0:53297f36733d | 84 | } |
SolderSplashLabs | 0:53297f36733d | 85 | } |
SolderSplashLabs | 0:53297f36733d | 86 | |
SolderSplashLabs | 0:53297f36733d | 87 | void IAP_PrepareEraseSector( unsigned flash_address ) |
SolderSplashLabs | 0:53297f36733d | 88 | { |
SolderSplashLabs | 0:53297f36733d | 89 | unsigned i; |
SolderSplashLabs | 0:53297f36733d | 90 | unsigned end_sector; |
SolderSplashLabs | 0:53297f36733d | 91 | |
SolderSplashLabs | 0:53297f36733d | 92 | end_sector = MAX_USER_SECTOR; |
SolderSplashLabs | 0:53297f36733d | 93 | |
SolderSplashLabs | 0:53297f36733d | 94 | for(i=0; i<=end_sector; i++) |
SolderSplashLabs | 0:53297f36733d | 95 | { |
SolderSplashLabs | 0:53297f36733d | 96 | if(flash_address < (SECTOR_0_START_ADDR + ((i + 1) * SECTOR_SIZE))) |
SolderSplashLabs | 0:53297f36733d | 97 | { |
SolderSplashLabs | 0:53297f36733d | 98 | LPC_GPIO->NOT[ 1 ] = 1<<16; |
SolderSplashLabs | 0:53297f36733d | 99 | |
SolderSplashLabs | 0:53297f36733d | 100 | // if its the start of a sector, erase it |
SolderSplashLabs | 0:53297f36733d | 101 | if( flash_address == SECTOR_0_START_ADDR + (SECTOR_SIZE * i)) |
SolderSplashLabs | 0:53297f36733d | 102 | { |
SolderSplashLabs | 0:53297f36733d | 103 | IAP_PrepareSector(i,i,cclk); |
SolderSplashLabs | 0:53297f36733d | 104 | IAP_EraseSector(i,i,cclk); |
SolderSplashLabs | 0:53297f36733d | 105 | } |
SolderSplashLabs | 0:53297f36733d | 106 | IAP_PrepareSector(i,i,cclk); |
SolderSplashLabs | 0:53297f36733d | 107 | break; |
SolderSplashLabs | 0:53297f36733d | 108 | } |
SolderSplashLabs | 0:53297f36733d | 109 | } |
SolderSplashLabs | 0:53297f36733d | 110 | } |
SolderSplashLabs | 0:53297f36733d | 111 | |
SolderSplashLabs | 0:53297f36733d | 112 | int IAP_WritePage (unsigned * dst, char * src, unsigned no_of_bytes) |
SolderSplashLabs | 0:53297f36733d | 113 | { |
SolderSplashLabs | 0:53297f36733d | 114 | unsigned enabled_irqs; |
SolderSplashLabs | 0:53297f36733d | 115 | |
SolderSplashLabs | 0:53297f36733d | 116 | // A copy of any enabled interrupts |
SolderSplashLabs | 0:53297f36733d | 117 | enabled_irqs = NVIC->ISER[0]; |
SolderSplashLabs | 0:53297f36733d | 118 | NVIC->ICER[0] = enabled_irqs; |
SolderSplashLabs | 0:53297f36733d | 119 | |
SolderSplashLabs | 0:53297f36733d | 120 | memcpy(&flash_buf[0], src, no_of_bytes); |
SolderSplashLabs | 0:53297f36733d | 121 | |
SolderSplashLabs | 0:53297f36733d | 122 | // If the address is the start of a sector then, prepare and erase it |
SolderSplashLabs | 0:53297f36733d | 123 | IAP_PrepareEraseSector((unsigned)dst); |
SolderSplashLabs | 0:53297f36733d | 124 | //IAP_ErasePage (1, 1, cclk); |
SolderSplashLabs | 0:53297f36733d | 125 | |
SolderSplashLabs | 0:53297f36733d | 126 | IAP_WriteData(cclk, (unsigned)dst, (unsigned *)flash_buf, FLASH_BUF_SIZE); |
SolderSplashLabs | 0:53297f36733d | 127 | |
SolderSplashLabs | 0:53297f36733d | 128 | NVIC->ISER[0] = enabled_irqs; |
SolderSplashLabs | 0:53297f36733d | 129 | |
SolderSplashLabs | 0:53297f36733d | 130 | return( result_table[0] ); |
SolderSplashLabs | 0:53297f36733d | 131 | } |
SolderSplashLabs | 0:53297f36733d | 132 | |
SolderSplashLabs | 0:53297f36733d | 133 | int IAP_CheckForUserCode(void) |
SolderSplashLabs | 0:53297f36733d | 134 | { |
SolderSplashLabs | 0:53297f36733d | 135 | unsigned *pmem, checksum,i; |
SolderSplashLabs | 0:53297f36733d | 136 | |
SolderSplashLabs | 0:53297f36733d | 137 | param_table[0] = BLANK_CHECK_SECTOR; |
SolderSplashLabs | 0:53297f36733d | 138 | param_table[1] = USER_START_SECTOR; |
SolderSplashLabs | 0:53297f36733d | 139 | param_table[2] = USER_START_SECTOR; |
SolderSplashLabs | 0:53297f36733d | 140 | iap_entry(param_table,result_table); |
SolderSplashLabs | 0:53297f36733d | 141 | |
SolderSplashLabs | 0:53297f36733d | 142 | if( result_table[0] == CMD_SUCCESS ) |
SolderSplashLabs | 0:53297f36733d | 143 | { |
SolderSplashLabs | 0:53297f36733d | 144 | // it's blank |
SolderSplashLabs | 0:53297f36733d | 145 | return (0); |
SolderSplashLabs | 0:53297f36733d | 146 | } |
SolderSplashLabs | 0:53297f36733d | 147 | else |
SolderSplashLabs | 0:53297f36733d | 148 | { |
SolderSplashLabs | 0:53297f36733d | 149 | /* |
SolderSplashLabs | 0:53297f36733d | 150 | * The reserved Cortex-M3 exception vector location 7 (offset 0x001C |
SolderSplashLabs | 0:53297f36733d | 151 | * in the vector table) should contain the 2’s complement of the |
SolderSplashLabs | 0:53297f36733d | 152 | * checksum of table entries 0 through 6. This causes the checksum |
SolderSplashLabs | 0:53297f36733d | 153 | * of the first 8 table entries to be 0. This code checksums the |
SolderSplashLabs | 0:53297f36733d | 154 | * first 8 locations of the start of user flash. If the result is 0, |
SolderSplashLabs | 0:53297f36733d | 155 | * then the contents is deemed a 'valid' image. |
SolderSplashLabs | 0:53297f36733d | 156 | */ |
SolderSplashLabs | 0:53297f36733d | 157 | checksum = 0; |
SolderSplashLabs | 0:53297f36733d | 158 | pmem = (unsigned *)USER_START_SECTOR; |
SolderSplashLabs | 0:53297f36733d | 159 | for (i = 0; i <= 7; i++) |
SolderSplashLabs | 0:53297f36733d | 160 | { |
SolderSplashLabs | 0:53297f36733d | 161 | checksum += *pmem; |
SolderSplashLabs | 0:53297f36733d | 162 | pmem++; |
SolderSplashLabs | 0:53297f36733d | 163 | } |
SolderSplashLabs | 0:53297f36733d | 164 | if (checksum != 0) |
SolderSplashLabs | 0:53297f36733d | 165 | { |
SolderSplashLabs | 0:53297f36733d | 166 | // Failed to checksum, not valid |
SolderSplashLabs | 0:53297f36733d | 167 | } |
SolderSplashLabs | 0:53297f36733d | 168 | else |
SolderSplashLabs | 0:53297f36733d | 169 | { |
SolderSplashLabs | 0:53297f36733d | 170 | // Checksum passed |
SolderSplashLabs | 0:53297f36733d | 171 | return (1); |
SolderSplashLabs | 0:53297f36733d | 172 | } |
SolderSplashLabs | 0:53297f36733d | 173 | } |
SolderSplashLabs | 0:53297f36733d | 174 | |
SolderSplashLabs | 0:53297f36733d | 175 | return (0); |
SolderSplashLabs | 0:53297f36733d | 176 | } |
SolderSplashLabs | 0:53297f36733d | 177 | |
SolderSplashLabs | 0:53297f36733d | 178 | void IAP_Init( void ) |
SolderSplashLabs | 0:53297f36733d | 179 | { |
SolderSplashLabs | 0:53297f36733d | 180 | cclk = CCLK; |
SolderSplashLabs | 0:53297f36733d | 181 | byte_ctr = 0; |
SolderSplashLabs | 0:53297f36733d | 182 | flash_address = (unsigned *)UPDATE_REQD; |
SolderSplashLabs | 0:53297f36733d | 183 | } |
SolderSplashLabs | 0:53297f36733d | 184 | |
SolderSplashLabs | 0:53297f36733d | 185 | void IAP_Eeprom_Write ( uint32_t eeAddress, uint8_t* buffAddress, uint32_t byteCount ) |
SolderSplashLabs | 0:53297f36733d | 186 | { |
SolderSplashLabs | 0:53297f36733d | 187 | unsigned int command[5], result[4]; |
SolderSplashLabs | 0:53297f36733d | 188 | |
SolderSplashLabs | 0:53297f36733d | 189 | command[0] = EEPROM_WRITE; |
SolderSplashLabs | 0:53297f36733d | 190 | command[1] = (uint32_t) eeAddress; |
SolderSplashLabs | 0:53297f36733d | 191 | command[2] = (uint32_t) buffAddress; |
SolderSplashLabs | 0:53297f36733d | 192 | command[3] = byteCount; |
SolderSplashLabs | 0:53297f36733d | 193 | command[4] = cclk; |
SolderSplashLabs | 0:53297f36733d | 194 | |
SolderSplashLabs | 0:53297f36733d | 195 | /* Invoke IAP call...*/ |
SolderSplashLabs | 0:53297f36733d | 196 | iap_entry(command, result); |
SolderSplashLabs | 0:53297f36733d | 197 | } |
SolderSplashLabs | 0:53297f36733d | 198 | |
SolderSplashLabs | 0:53297f36733d | 199 | void IAP_Eeprom_Read ( uint32_t eeAddress, uint8_t* buffAddress, uint32_t byteCount ) |
SolderSplashLabs | 0:53297f36733d | 200 | { |
SolderSplashLabs | 0:53297f36733d | 201 | unsigned int command[5], result[4]; |
SolderSplashLabs | 0:53297f36733d | 202 | |
SolderSplashLabs | 0:53297f36733d | 203 | command[0] = EEPROM_READ; |
SolderSplashLabs | 0:53297f36733d | 204 | command[1] = (uint32_t) eeAddress; |
SolderSplashLabs | 0:53297f36733d | 205 | command[2] = (uint32_t) buffAddress; |
SolderSplashLabs | 0:53297f36733d | 206 | command[3] = byteCount; |
SolderSplashLabs | 0:53297f36733d | 207 | command[4] = cclk; |
SolderSplashLabs | 0:53297f36733d | 208 | |
SolderSplashLabs | 0:53297f36733d | 209 | /* Invoke IAP call...*/ |
SolderSplashLabs | 0:53297f36733d | 210 | iap_entry( command, result); |
SolderSplashLabs | 0:53297f36733d | 211 | } |