Hotboards_eeprom.cpp - Driver to control serial (spi) eeprom memories, The memories are compatibles amount the manufactures Microchip, Atmel and ST, and of course you can control Hotboards eeprom boards (http://hotboards.org)
Dependents: Hotboards_eeprom_write_byte Hotboards_eeprom_write_array
Hotboards_eeprom.cpp@0:38fad00cc45b, 2016-01-29 (annotated)
- Committer:
- Hotboards
- Date:
- Fri Jan 29 00:46:08 2016 +0000
- Revision:
- 0:38fad00cc45b
Initial commit for eeprom library, pretty functional
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hotboards | 0:38fad00cc45b | 1 | /* |
Hotboards | 0:38fad00cc45b | 2 | Hotboards_eeprom.cpp - Driver to control serial (spi) eeprom memories, The memories are |
Hotboards | 0:38fad00cc45b | 3 | compatibles amount the manufactures Microchip, Atmel and ST, and of course you can control |
Hotboards | 0:38fad00cc45b | 4 | Hotboards eeprom board (http://hotboards.org) |
Hotboards | 0:38fad00cc45b | 5 | Created by Diego Perez, January 16, 2016. |
Hotboards | 0:38fad00cc45b | 6 | Released into the public domain. |
Hotboards | 0:38fad00cc45b | 7 | |
Hotboards | 0:38fad00cc45b | 8 | Density: 1Kbit | 2Kbit | 4Kbit | 8Kbit | 16Kbit | 32Kbit | 64Kbit | 128Kbit | 256Kbit | 512Kbit | 1 Mbit |
Hotboards | 0:38fad00cc45b | 9 | Part: 25xx010 | 25xx020 | 25xx040 | 25xx080 | 25xx160 | 25xx320 | 25xx640 | 25xx128 | 25xx256 | 25xx512 | 25xx1024 |
Hotboards | 0:38fad00cc45b | 10 | Page/Byte: 16 | 16 | 16 | 16(32) | 16(32) | 32 | 32 | 64 | 64 | 128 | 256 |
Hotboards | 0:38fad00cc45b | 11 | Addr/Bits: 7 | 8 | 9 | 16 | 16 | 16 | 16 | 16 | 16 | 16 |
Hotboards | 0:38fad00cc45b | 12 | */ |
Hotboards | 0:38fad00cc45b | 13 | #include "Hotboards_eeprom.h" |
Hotboards | 0:38fad00cc45b | 14 | |
Hotboards | 0:38fad00cc45b | 15 | #define READ 0x03 |
Hotboards | 0:38fad00cc45b | 16 | #define WRITE 0x02 |
Hotboards | 0:38fad00cc45b | 17 | #define WRDI 0x04 |
Hotboards | 0:38fad00cc45b | 18 | #define WREN 0x06 |
Hotboards | 0:38fad00cc45b | 19 | #define RDSR 0x05 |
Hotboards | 0:38fad00cc45b | 20 | #define WRSR 0x01 |
Hotboards | 0:38fad00cc45b | 21 | |
Hotboards | 0:38fad00cc45b | 22 | const uint16_t PageSize[ ] = { 16, 16, 16, 16, 32, 32, 64, 64, 128, 256 }; |
Hotboards | 0:38fad00cc45b | 23 | const uint32_t EepromSize[ ] = { 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072 }; |
Hotboards | 0:38fad00cc45b | 24 | |
Hotboards | 0:38fad00cc45b | 25 | Hotboards_eeprom::Hotboards_eeprom( SPI &spi, PinName cs, uint8_t type ) |
Hotboards | 0:38fad00cc45b | 26 | : _spi(spi), _cs_pin(cs) |
Hotboards | 0:38fad00cc45b | 27 | { |
Hotboards | 0:38fad00cc45b | 28 | _cs_pin = cs; |
Hotboards | 0:38fad00cc45b | 29 | _type = type; |
Hotboards | 0:38fad00cc45b | 30 | _page = PageSize[ type ]; |
Hotboards | 0:38fad00cc45b | 31 | _density = EepromSize[ type ]; |
Hotboards | 0:38fad00cc45b | 32 | } |
Hotboards | 0:38fad00cc45b | 33 | |
Hotboards | 0:38fad00cc45b | 34 | /* |
Hotboards | 0:38fad00cc45b | 35 | * just make sure the Cs pin is not clear |
Hotboards | 0:38fad00cc45b | 36 | */ |
Hotboards | 0:38fad00cc45b | 37 | void Hotboards_eeprom::init( void ) |
Hotboards | 0:38fad00cc45b | 38 | { |
Hotboards | 0:38fad00cc45b | 39 | _cs_pin = 1; |
Hotboards | 0:38fad00cc45b | 40 | } |
Hotboards | 0:38fad00cc45b | 41 | |
Hotboards | 0:38fad00cc45b | 42 | /* |
Hotboards | 0:38fad00cc45b | 43 | * write a single byte in a given eeprom address, this operation will take 5ms |
Hotboards | 0:38fad00cc45b | 44 | * address: eeprom address where tha byte will be written |
Hotboards | 0:38fad00cc45b | 45 | * data: the byte that will be written at the given address |
Hotboards | 0:38fad00cc45b | 46 | */ |
Hotboards | 0:38fad00cc45b | 47 | void Hotboards_eeprom::write( uint32_t address, uint8_t data ) |
Hotboards | 0:38fad00cc45b | 48 | { |
Hotboards | 0:38fad00cc45b | 49 | write( address, &data, 1 ); |
Hotboards | 0:38fad00cc45b | 50 | } |
Hotboards | 0:38fad00cc45b | 51 | |
Hotboards | 0:38fad00cc45b | 52 | /* |
Hotboards | 0:38fad00cc45b | 53 | * write a given number of bytes into the eeprom atarting at a given address |
Hotboards | 0:38fad00cc45b | 54 | * address: eeprom address where tha byte will be written |
Hotboards | 0:38fad00cc45b | 55 | * data: pointer to array of bytes that need to be written |
Hotboards | 0:38fad00cc45b | 56 | * size: the number of bytes to write |
Hotboards | 0:38fad00cc45b | 57 | */ |
Hotboards | 0:38fad00cc45b | 58 | void Hotboards_eeprom::write( uint32_t address, uint8_t *data, uint16_t size ) |
Hotboards | 0:38fad00cc45b | 59 | { |
Hotboards | 0:38fad00cc45b | 60 | uint16_t temp; |
Hotboards | 0:38fad00cc45b | 61 | /* just to cover your ass, check if the address is a valid direction */ |
Hotboards | 0:38fad00cc45b | 62 | if( ( address < _density ) && ( size != 0 ) ) |
Hotboards | 0:38fad00cc45b | 63 | { |
Hotboards | 0:38fad00cc45b | 64 | /* check if the amount of bytes to be read from the requested address do not exced the eeprom |
Hotboards | 0:38fad00cc45b | 65 | memory map if so, then only read the bytes left*/ |
Hotboards | 0:38fad00cc45b | 66 | if( ( address + size ) > _density ) |
Hotboards | 0:38fad00cc45b | 67 | { |
Hotboards | 0:38fad00cc45b | 68 | size = _density - address; |
Hotboards | 0:38fad00cc45b | 69 | } |
Hotboards | 0:38fad00cc45b | 70 | |
Hotboards | 0:38fad00cc45b | 71 | /* Check how many bytes are left to be written within a single page */ |
Hotboards | 0:38fad00cc45b | 72 | temp = _page - ( address % _page ); |
Hotboards | 0:38fad00cc45b | 73 | if( temp >= size ) |
Hotboards | 0:38fad00cc45b | 74 | { |
Hotboards | 0:38fad00cc45b | 75 | /* Data lenght can be written witihn the current page */ |
Hotboards | 0:38fad00cc45b | 76 | writePage( address, data, size ); |
Hotboards | 0:38fad00cc45b | 77 | } |
Hotboards | 0:38fad00cc45b | 78 | else |
Hotboards | 0:38fad00cc45b | 79 | { |
Hotboards | 0:38fad00cc45b | 80 | /* Data can not be written within the current page, so write only the bytes left */ |
Hotboards | 0:38fad00cc45b | 81 | writePage( address, data, temp ); |
Hotboards | 0:38fad00cc45b | 82 | /* update pointer to data, lenght and addres */ |
Hotboards | 0:38fad00cc45b | 83 | data = data + temp; |
Hotboards | 0:38fad00cc45b | 84 | size = size - temp; |
Hotboards | 0:38fad00cc45b | 85 | address = address + temp; |
Hotboards | 0:38fad00cc45b | 86 | |
Hotboards | 0:38fad00cc45b | 87 | /* Check if the remaining data is bigger than a page size */ |
Hotboards | 0:38fad00cc45b | 88 | while( size > _page ) |
Hotboards | 0:38fad00cc45b | 89 | { |
Hotboards | 0:38fad00cc45b | 90 | /* Write an entire page into eeprom */ |
Hotboards | 0:38fad00cc45b | 91 | writePage( address, data, _page ); |
Hotboards | 0:38fad00cc45b | 92 | size = size - _page; |
Hotboards | 0:38fad00cc45b | 93 | data = data + _page; |
Hotboards | 0:38fad00cc45b | 94 | address = address + _page; |
Hotboards | 0:38fad00cc45b | 95 | } |
Hotboards | 0:38fad00cc45b | 96 | /* Write one last time if there is still data to be written */ |
Hotboards | 0:38fad00cc45b | 97 | if( size != 0 ) |
Hotboards | 0:38fad00cc45b | 98 | { |
Hotboards | 0:38fad00cc45b | 99 | writePage( address, data, size ); |
Hotboards | 0:38fad00cc45b | 100 | } |
Hotboards | 0:38fad00cc45b | 101 | } |
Hotboards | 0:38fad00cc45b | 102 | } |
Hotboards | 0:38fad00cc45b | 103 | } |
Hotboards | 0:38fad00cc45b | 104 | |
Hotboards | 0:38fad00cc45b | 105 | /* |
Hotboards | 0:38fad00cc45b | 106 | * read a single byte from a given eeprom address |
Hotboards | 0:38fad00cc45b | 107 | * address: eeprom address where the byte will be read |
Hotboards | 0:38fad00cc45b | 108 | */ |
Hotboards | 0:38fad00cc45b | 109 | uint8_t Hotboards_eeprom::read( uint32_t address ) |
Hotboards | 0:38fad00cc45b | 110 | { |
Hotboards | 0:38fad00cc45b | 111 | uint8_t data; |
Hotboards | 0:38fad00cc45b | 112 | read( address, &data, 1 ); |
Hotboards | 0:38fad00cc45b | 113 | return data; |
Hotboards | 0:38fad00cc45b | 114 | } |
Hotboards | 0:38fad00cc45b | 115 | |
Hotboards | 0:38fad00cc45b | 116 | /* |
Hotboards | 0:38fad00cc45b | 117 | * read a given number of bytes from the eeprom starting at a given address |
Hotboards | 0:38fad00cc45b | 118 | * address: eeprom address where the bytes will be read it |
Hotboards | 0:38fad00cc45b | 119 | * data: pointer to array where data will be stored |
Hotboards | 0:38fad00cc45b | 120 | * size: the number of bytes to read |
Hotboards | 0:38fad00cc45b | 121 | */ |
Hotboards | 0:38fad00cc45b | 122 | void Hotboards_eeprom::read( uint32_t address, uint8_t *data, uint16_t size ) |
Hotboards | 0:38fad00cc45b | 123 | { |
Hotboards | 0:38fad00cc45b | 124 | uint16_t i; |
Hotboards | 0:38fad00cc45b | 125 | /* just to cover your ass, check if the address is a valid direction */ |
Hotboards | 0:38fad00cc45b | 126 | if( ( address < _density ) && ( size != 0 ) ) |
Hotboards | 0:38fad00cc45b | 127 | { |
Hotboards | 0:38fad00cc45b | 128 | /* check if the amount of bytes to be read from the requested address do not exced the eeprom |
Hotboards | 0:38fad00cc45b | 129 | memory map if so, then only read the bytes left*/ |
Hotboards | 0:38fad00cc45b | 130 | if( ( address + size ) > _density ) |
Hotboards | 0:38fad00cc45b | 131 | { |
Hotboards | 0:38fad00cc45b | 132 | size = _density - address; |
Hotboards | 0:38fad00cc45b | 133 | } |
Hotboards | 0:38fad00cc45b | 134 | |
Hotboards | 0:38fad00cc45b | 135 | /* operation begins, select memory */ |
Hotboards | 0:38fad00cc45b | 136 | _cs_pin = 0; |
Hotboards | 0:38fad00cc45b | 137 | |
Hotboards | 0:38fad00cc45b | 138 | /* Send command Read and address, depend on device is the number of address bytes neccesary */ |
Hotboards | 0:38fad00cc45b | 139 | sendAddress( READ, address ); |
Hotboards | 0:38fad00cc45b | 140 | |
Hotboards | 0:38fad00cc45b | 141 | for( i = 0 ; i<size ; i++ ) |
Hotboards | 0:38fad00cc45b | 142 | { |
Hotboards | 0:38fad00cc45b | 143 | /* Read data from memory */ |
Hotboards | 0:38fad00cc45b | 144 | *data = _spi.write( 0xAA ); |
Hotboards | 0:38fad00cc45b | 145 | data++; |
Hotboards | 0:38fad00cc45b | 146 | }; |
Hotboards | 0:38fad00cc45b | 147 | /* operation ended, unselect memory */ |
Hotboards | 0:38fad00cc45b | 148 | _cs_pin = 1; |
Hotboards | 0:38fad00cc45b | 149 | } |
Hotboards | 0:38fad00cc45b | 150 | } |
Hotboards | 0:38fad00cc45b | 151 | |
Hotboards | 0:38fad00cc45b | 152 | void Hotboards_eeprom::sendAddress( uint8_t cmd, uint32_t address ) |
Hotboards | 0:38fad00cc45b | 153 | { |
Hotboards | 0:38fad00cc45b | 154 | if( _type < HT_EEPROM25xx_4Kb ) |
Hotboards | 0:38fad00cc45b | 155 | { |
Hotboards | 0:38fad00cc45b | 156 | /* Send the command and one byte address */ |
Hotboards | 0:38fad00cc45b | 157 | _spi.write( cmd ); |
Hotboards | 0:38fad00cc45b | 158 | _spi.write( address ); |
Hotboards | 0:38fad00cc45b | 159 | } |
Hotboards | 0:38fad00cc45b | 160 | else if( _type == HT_EEPROM25xx_4Kb ) |
Hotboards | 0:38fad00cc45b | 161 | { |
Hotboards | 0:38fad00cc45b | 162 | /* Send the command coded in the MSB of the address and one LSB address */ |
Hotboards | 0:38fad00cc45b | 163 | _spi.write( cmd | ( uint8_t )( address >> 8 ) ); |
Hotboards | 0:38fad00cc45b | 164 | _spi.write( ( uint8_t )address ); |
Hotboards | 0:38fad00cc45b | 165 | } |
Hotboards | 0:38fad00cc45b | 166 | else if( ( _type > HT_EEPROM25xx_4Kb ) && ( _type < HT_EEPROM25xx_1Mb ) ) |
Hotboards | 0:38fad00cc45b | 167 | { |
Hotboards | 0:38fad00cc45b | 168 | /* Send command plus two byte address */ |
Hotboards | 0:38fad00cc45b | 169 | _spi.write( cmd ); |
Hotboards | 0:38fad00cc45b | 170 | _spi.write( ( uint8_t )( address >> 8 ) ); |
Hotboards | 0:38fad00cc45b | 171 | _spi.write( ( uint8_t )address ); |
Hotboards | 0:38fad00cc45b | 172 | } |
Hotboards | 0:38fad00cc45b | 173 | else if( _type == HT_EEPROM25xx_1Mb ) |
Hotboards | 0:38fad00cc45b | 174 | { |
Hotboards | 0:38fad00cc45b | 175 | /* Send command plus three byte address */ |
Hotboards | 0:38fad00cc45b | 176 | _spi.write( cmd ); |
Hotboards | 0:38fad00cc45b | 177 | _spi.write( ( uint8_t )( address >> 16 ) ); |
Hotboards | 0:38fad00cc45b | 178 | _spi.write( ( uint8_t )( address >> 8 ) ); |
Hotboards | 0:38fad00cc45b | 179 | _spi.write( ( uint8_t )address ); |
Hotboards | 0:38fad00cc45b | 180 | } |
Hotboards | 0:38fad00cc45b | 181 | } |
Hotboards | 0:38fad00cc45b | 182 | |
Hotboards | 0:38fad00cc45b | 183 | void Hotboards_eeprom::writePage( uint32_t address, uint8_t *data, uint16_t size ) |
Hotboards | 0:38fad00cc45b | 184 | { |
Hotboards | 0:38fad00cc45b | 185 | uint16_t i; |
Hotboards | 0:38fad00cc45b | 186 | |
Hotboards | 0:38fad00cc45b | 187 | /* Select memory */ |
Hotboards | 0:38fad00cc45b | 188 | _cs_pin = 0; |
Hotboards | 0:38fad00cc45b | 189 | /* write eneable latch */ |
Hotboards | 0:38fad00cc45b | 190 | _spi.write( WREN ); |
Hotboards | 0:38fad00cc45b | 191 | /* Unselect eeprom */ |
Hotboards | 0:38fad00cc45b | 192 | _cs_pin = 1; |
Hotboards | 0:38fad00cc45b | 193 | |
Hotboards | 0:38fad00cc45b | 194 | /* Select memory */ |
Hotboards | 0:38fad00cc45b | 195 | _cs_pin = 0; |
Hotboards | 0:38fad00cc45b | 196 | /* Send comand write and address, depend on device is the number of address bytes neccesary */ |
Hotboards | 0:38fad00cc45b | 197 | sendAddress( WRITE, address ); |
Hotboards | 0:38fad00cc45b | 198 | /* Send data to eeprom */ |
Hotboards | 0:38fad00cc45b | 199 | for( i = 0 ; i<size ; i++ ) |
Hotboards | 0:38fad00cc45b | 200 | { |
Hotboards | 0:38fad00cc45b | 201 | /* Read data from memory */ |
Hotboards | 0:38fad00cc45b | 202 | _spi.write( *data ); |
Hotboards | 0:38fad00cc45b | 203 | data++; |
Hotboards | 0:38fad00cc45b | 204 | }; |
Hotboards | 0:38fad00cc45b | 205 | /* Unselect eeprom */ |
Hotboards | 0:38fad00cc45b | 206 | _cs_pin = 1; |
Hotboards | 0:38fad00cc45b | 207 | /* wait until data has been recorded */ |
Hotboards | 0:38fad00cc45b | 208 | wait( 0.006 ); |
Hotboards | 0:38fad00cc45b | 209 | } |