Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
rm25c512cl.cpp@4:17e602b21ac5, 2018-09-12 (annotated)
- Committer:
- ianaherne
- Date:
- Wed Sep 12 16:09:01 2018 +0000
- Revision:
- 4:17e602b21ac5
- Parent:
- 3:e846980a1536
- Child:
- 6:0434e89c2d68
added enumerator for pin states
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ianaherne | 0:890f322344f2 | 1 | #include "rm25c512cl.h" |
| ianaherne | 0:890f322344f2 | 2 | |
| ianaherne | 0:890f322344f2 | 3 | |
| ianaherne | 0:890f322344f2 | 4 | rm25c512cl::rm25c512cl(PinName MOSI,PinName MISO,PinName SCLK,PinName CS): _spi(MOSI,MISO,SCLK),_cs(CS){ |
| ianaherne | 1:67d99e4ce310 | 5 | |
| ianaherne | 4:17e602b21ac5 | 6 | _cs = HIGH; // always set chip select to initial state of high |
| ianaherne | 0:890f322344f2 | 7 | |
| ianaherne | 0:890f322344f2 | 8 | } |
| ianaherne | 0:890f322344f2 | 9 | |
| ianaherne | 1:67d99e4ce310 | 10 | /** |
| ianaherne | 1:67d99e4ce310 | 11 | * @brief write_enable() |
| ianaherne | 1:67d99e4ce310 | 12 | * @details Sets write enable latch to 1 to enable writing to eeprom uses the WREN opcode |
| ianaherne | 1:67d99e4ce310 | 13 | * @param NA |
| ianaherne | 1:67d99e4ce310 | 14 | * @return NA |
| ianaherne | 1:67d99e4ce310 | 15 | * @warning Write or erase instructions will be ignored if this instruction is not issued first |
| ianaherne | 1:67d99e4ce310 | 16 | * |
| ianaherne | 1:67d99e4ce310 | 17 | */ |
| ianaherne | 1:67d99e4ce310 | 18 | |
| ianaherne | 0:890f322344f2 | 19 | void rm25c512cl :: write_enable(){ |
| ianaherne | 0:890f322344f2 | 20 | |
| ianaherne | 1:67d99e4ce310 | 21 | char cmd[1]; |
| ianaherne | 1:67d99e4ce310 | 22 | char data_buffer[1];// not used here but required for spi.write() as a parameter |
| ianaherne | 1:67d99e4ce310 | 23 | |
| ianaherne | 1:67d99e4ce310 | 24 | cmd[0] = WREN; |
| ianaherne | 1:67d99e4ce310 | 25 | |
| ianaherne | 1:67d99e4ce310 | 26 | _spi.lock(); |
| ianaherne | 1:67d99e4ce310 | 27 | |
| ianaherne | 4:17e602b21ac5 | 28 | _cs = LOW; |
| ianaherne | 1:67d99e4ce310 | 29 | |
| ianaherne | 1:67d99e4ce310 | 30 | _spi.write(&cmd[0],1,&data_buffer[0],0); |
| ianaherne | 1:67d99e4ce310 | 31 | |
| ianaherne | 4:17e602b21ac5 | 32 | _cs = HIGH; |
| ianaherne | 1:67d99e4ce310 | 33 | |
| ianaherne | 1:67d99e4ce310 | 34 | _spi.unlock(); |
| ianaherne | 1:67d99e4ce310 | 35 | |
| ianaherne | 1:67d99e4ce310 | 36 | wait_us(20); |
| ianaherne | 1:67d99e4ce310 | 37 | |
| ianaherne | 0:890f322344f2 | 38 | |
| ianaherne | 0:890f322344f2 | 39 | |
| ianaherne | 0:890f322344f2 | 40 | } |
| ianaherne | 0:890f322344f2 | 41 | |
| ianaherne | 1:67d99e4ce310 | 42 | /** |
| ianaherne | 1:67d99e4ce310 | 43 | * @brief read_status_reg() |
| ianaherne | 1:67d99e4ce310 | 44 | * @details Reads the status register of EEprom using the RDSR opcode, the register |
| ianaherne | 1:67d99e4ce310 | 45 | * read can be used to determine if a write has completed, or the write protection status |
| ianaherne | 1:67d99e4ce310 | 46 | * @param NA |
| ianaherne | 1:67d99e4ce310 | 47 | * @return Current eight bit value of register |
| ianaherne | 1:67d99e4ce310 | 48 | * @warning |
| ianaherne | 1:67d99e4ce310 | 49 | * |
| ianaherne | 1:67d99e4ce310 | 50 | */ |
| ianaherne | 1:67d99e4ce310 | 51 | |
| ianaherne | 1:67d99e4ce310 | 52 | char rm25c512cl :: read_status_reg(){ |
| ianaherne | 1:67d99e4ce310 | 53 | |
| ianaherne | 1:67d99e4ce310 | 54 | char cmd[1]; |
| ianaherne | 1:67d99e4ce310 | 55 | char data_buffer[1]; // to store value returned from EEprom status register |
| ianaherne | 1:67d99e4ce310 | 56 | |
| ianaherne | 1:67d99e4ce310 | 57 | |
| ianaherne | 1:67d99e4ce310 | 58 | cmd[0] = RDSR; |
| ianaherne | 1:67d99e4ce310 | 59 | |
| ianaherne | 1:67d99e4ce310 | 60 | _spi.lock(); |
| ianaherne | 1:67d99e4ce310 | 61 | |
| ianaherne | 4:17e602b21ac5 | 62 | _cs = LOW; |
| ianaherne | 1:67d99e4ce310 | 63 | |
| ianaherne | 1:67d99e4ce310 | 64 | _spi.write(&cmd[0],1,&data_buffer[0],0); |
| ianaherne | 1:67d99e4ce310 | 65 | |
| ianaherne | 1:67d99e4ce310 | 66 | cmd[0] = 0xFF; // write garbage to read something back |
| ianaherne | 1:67d99e4ce310 | 67 | |
| ianaherne | 1:67d99e4ce310 | 68 | _spi.write(&cmd[0],1,&data_buffer[0],1); |
| ianaherne | 1:67d99e4ce310 | 69 | |
| ianaherne | 4:17e602b21ac5 | 70 | _cs = HIGH; |
| ianaherne | 1:67d99e4ce310 | 71 | |
| ianaherne | 1:67d99e4ce310 | 72 | _spi.unlock(); |
| ianaherne | 1:67d99e4ce310 | 73 | |
| ianaherne | 1:67d99e4ce310 | 74 | wait_us(20); |
| ianaherne | 1:67d99e4ce310 | 75 | |
| ianaherne | 1:67d99e4ce310 | 76 | return data_buffer[0]; |
| ianaherne | 1:67d99e4ce310 | 77 | |
| ianaherne | 1:67d99e4ce310 | 78 | |
| ianaherne | 1:67d99e4ce310 | 79 | } |
| ianaherne | 1:67d99e4ce310 | 80 | |
| ianaherne | 1:67d99e4ce310 | 81 | /** |
| ianaherne | 1:67d99e4ce310 | 82 | * @brief write_bytes() |
| ianaherne | 1:67d99e4ce310 | 83 | * @details Writes multiple bytes to the specified address given. |
| ianaherne | 1:67d99e4ce310 | 84 | * @param Starting address to write to. |
| ianaherne | 1:67d99e4ce310 | 85 | * @param Pointer to the data array containg bytes to be stored. |
| ianaherne | 2:183d1d96f917 | 86 | * @param Size of the data array in bytes. |
| ianaherne | 1:67d99e4ce310 | 87 | * @return NA |
| ianaherne | 1:67d99e4ce310 | 88 | * @warning A page write(128 bytes) can take up to 5 ms to complete |
| ianaherne | 1:67d99e4ce310 | 89 | * |
| ianaherne | 1:67d99e4ce310 | 90 | */ |
| ianaherne | 1:67d99e4ce310 | 91 | |
| ianaherne | 2:183d1d96f917 | 92 | void rm25c512cl :: write_bytes(uint16_t address, char* data, uint16_t data_size){ |
| ianaherne | 1:67d99e4ce310 | 93 | |
| ianaherne | 1:67d99e4ce310 | 94 | char cmd[3]; |
| ianaherne | 1:67d99e4ce310 | 95 | char data_buffer[1];// not used here but required for spi.write() as a parameter |
| ianaherne | 0:890f322344f2 | 96 | |
| ianaherne | 0:890f322344f2 | 97 | |
| ianaherne | 0:890f322344f2 | 98 | |
| ianaherne | 1:67d99e4ce310 | 99 | cmd[0] = WR; |
| ianaherne | 1:67d99e4ce310 | 100 | cmd[1] = address >> 8; |
| ianaherne | 1:67d99e4ce310 | 101 | cmd[2] = address & 0x00FF; |
| ianaherne | 1:67d99e4ce310 | 102 | |
| ianaherne | 1:67d99e4ce310 | 103 | _spi.lock(); |
| ianaherne | 1:67d99e4ce310 | 104 | |
| ianaherne | 4:17e602b21ac5 | 105 | _cs = LOW; |
| ianaherne | 1:67d99e4ce310 | 106 | |
| ianaherne | 1:67d99e4ce310 | 107 | _spi.write(&cmd[0],3,&data_buffer[0],0); |
| ianaherne | 2:183d1d96f917 | 108 | _spi.write(&data[0],data_size,&data_buffer[0],0); |
| ianaherne | 1:67d99e4ce310 | 109 | |
| ianaherne | 4:17e602b21ac5 | 110 | _cs = HIGH; |
| ianaherne | 1:67d99e4ce310 | 111 | |
| ianaherne | 1:67d99e4ce310 | 112 | _spi.unlock(); |
| ianaherne | 1:67d99e4ce310 | 113 | |
| ianaherne | 1:67d99e4ce310 | 114 | wait_ms(5); |
| ianaherne | 0:890f322344f2 | 115 | |
| ianaherne | 0:890f322344f2 | 116 | |
| ianaherne | 0:890f322344f2 | 117 | } |
| ianaherne | 0:890f322344f2 | 118 | |
| ianaherne | 1:67d99e4ce310 | 119 | /** |
| ianaherne | 1:67d99e4ce310 | 120 | * @brief write_byte() |
| ianaherne | 1:67d99e4ce310 | 121 | * @details Writes a single byte to EEprom at the given address |
| ianaherne | 1:67d99e4ce310 | 122 | * @param Address to write to |
| ianaherne | 1:67d99e4ce310 | 123 | * @param Value to write in given address |
| ianaherne | 1:67d99e4ce310 | 124 | * @return NA |
| ianaherne | 1:67d99e4ce310 | 125 | * @warning Byte write can take up to 100 us to complete |
| ianaherne | 1:67d99e4ce310 | 126 | * |
| ianaherne | 1:67d99e4ce310 | 127 | */ |
| ianaherne | 1:67d99e4ce310 | 128 | |
| ianaherne | 1:67d99e4ce310 | 129 | void rm25c512cl :: write_byte(uint16_t address,char value){ |
| ianaherne | 1:67d99e4ce310 | 130 | |
| ianaherne | 1:67d99e4ce310 | 131 | char cmd[4]; |
| ianaherne | 1:67d99e4ce310 | 132 | char data_buffer[1]; // not used here but required for spi.write() as a parameter |
| ianaherne | 1:67d99e4ce310 | 133 | |
| ianaherne | 1:67d99e4ce310 | 134 | cmd[0] = WR; |
| ianaherne | 1:67d99e4ce310 | 135 | cmd[1] = address >> 8; |
| ianaherne | 1:67d99e4ce310 | 136 | cmd[2] = address & 0x00FF; |
| ianaherne | 1:67d99e4ce310 | 137 | cmd[3] = value; |
| ianaherne | 1:67d99e4ce310 | 138 | |
| ianaherne | 1:67d99e4ce310 | 139 | _spi.lock(); |
| ianaherne | 1:67d99e4ce310 | 140 | |
| ianaherne | 4:17e602b21ac5 | 141 | _cs = LOW; |
| ianaherne | 1:67d99e4ce310 | 142 | |
| ianaherne | 1:67d99e4ce310 | 143 | _spi.write(&cmd[0],4,&data_buffer[0],0); |
| ianaherne | 1:67d99e4ce310 | 144 | |
| ianaherne | 4:17e602b21ac5 | 145 | _cs = HIGH; |
| ianaherne | 1:67d99e4ce310 | 146 | |
| ianaherne | 1:67d99e4ce310 | 147 | _spi.unlock(); |
| ianaherne | 1:67d99e4ce310 | 148 | |
| ianaherne | 1:67d99e4ce310 | 149 | wait_us(100); |
| ianaherne | 0:890f322344f2 | 150 | |
| ianaherne | 0:890f322344f2 | 151 | } |
| ianaherne | 0:890f322344f2 | 152 | |
| ianaherne | 1:67d99e4ce310 | 153 | /** |
| ianaherne | 1:67d99e4ce310 | 154 | * @brief read_bytes() |
| ianaherne | 3:e846980a1536 | 155 | * @details Reads multiple bytes starting at given address. |
| ianaherne | 1:67d99e4ce310 | 156 | * @param Page Address to start reading from. |
| ianaherne | 2:183d1d96f917 | 157 | * @param Pointer to data buffer to store multiple bytes read from EEprom. |
| ianaherne | 2:183d1d96f917 | 158 | * @param Size of the data buffer in bytes. |
| ianaherne | 1:67d99e4ce310 | 159 | * @return |
| ianaherne | 1:67d99e4ce310 | 160 | * @warning Data buffer must be of size 128 bytes to read entire page. |
| ianaherne | 1:67d99e4ce310 | 161 | * |
| ianaherne | 1:67d99e4ce310 | 162 | */ |
| ianaherne | 1:67d99e4ce310 | 163 | |
| ianaherne | 2:183d1d96f917 | 164 | void rm25c512cl :: read_bytes(uint16_t address, char* data_buffer, uint16_t data_buffer_size){ |
| ianaherne | 1:67d99e4ce310 | 165 | |
| ianaherne | 1:67d99e4ce310 | 166 | char cmd[3]; |
| ianaherne | 1:67d99e4ce310 | 167 | |
| ianaherne | 1:67d99e4ce310 | 168 | |
| ianaherne | 1:67d99e4ce310 | 169 | cmd[0] = READ; |
| ianaherne | 1:67d99e4ce310 | 170 | cmd[1] = address >> 8; |
| ianaherne | 1:67d99e4ce310 | 171 | cmd[2] = address & 0x00FF; |
| ianaherne | 1:67d99e4ce310 | 172 | |
| ianaherne | 1:67d99e4ce310 | 173 | |
| ianaherne | 1:67d99e4ce310 | 174 | _spi.lock(); |
| ianaherne | 1:67d99e4ce310 | 175 | |
| ianaherne | 4:17e602b21ac5 | 176 | _cs = LOW; |
| ianaherne | 1:67d99e4ce310 | 177 | |
| ianaherne | 1:67d99e4ce310 | 178 | _spi.write(&cmd[0],3,&data_buffer[0],0); |
| ianaherne | 1:67d99e4ce310 | 179 | |
| ianaherne | 1:67d99e4ce310 | 180 | cmd[0] = 0xFF; |
| ianaherne | 1:67d99e4ce310 | 181 | |
| ianaherne | 2:183d1d96f917 | 182 | _spi.write(&cmd[0],1,&data_buffer[0],data_buffer_size); |
| ianaherne | 1:67d99e4ce310 | 183 | |
| ianaherne | 4:17e602b21ac5 | 184 | _cs = HIGH; |
| ianaherne | 1:67d99e4ce310 | 185 | |
| ianaherne | 1:67d99e4ce310 | 186 | _spi.unlock(); |
| ianaherne | 0:890f322344f2 | 187 | |
| ianaherne | 0:890f322344f2 | 188 | |
| ianaherne | 0:890f322344f2 | 189 | } |
| ianaherne | 0:890f322344f2 | 190 | |
| ianaherne | 1:67d99e4ce310 | 191 | /** |
| ianaherne | 1:67d99e4ce310 | 192 | * @brief read_byte() |
| ianaherne | 1:67d99e4ce310 | 193 | * @details Reads a byte of data at given address. |
| ianaherne | 1:67d99e4ce310 | 194 | * @param Address to read byte from. |
| ianaherne | 1:67d99e4ce310 | 195 | * @return Byte of data read from address. |
| ianaherne | 1:67d99e4ce310 | 196 | * @warning |
| ianaherne | 1:67d99e4ce310 | 197 | * |
| ianaherne | 1:67d99e4ce310 | 198 | */ |
| ianaherne | 1:67d99e4ce310 | 199 | |
| ianaherne | 1:67d99e4ce310 | 200 | char rm25c512cl :: read_byte(uint16_t address){ |
| ianaherne | 1:67d99e4ce310 | 201 | |
| ianaherne | 1:67d99e4ce310 | 202 | char cmd[3]; |
| ianaherne | 1:67d99e4ce310 | 203 | char data_buffer[1];// To store data byte returned from EEprom |
| ianaherne | 1:67d99e4ce310 | 204 | |
| ianaherne | 1:67d99e4ce310 | 205 | cmd[0] = READ; |
| ianaherne | 1:67d99e4ce310 | 206 | cmd[1] = address >> 8; |
| ianaherne | 1:67d99e4ce310 | 207 | cmd[2] = address & 0x00FF; |
| ianaherne | 1:67d99e4ce310 | 208 | |
| ianaherne | 1:67d99e4ce310 | 209 | _spi.lock(); |
| ianaherne | 1:67d99e4ce310 | 210 | |
| ianaherne | 4:17e602b21ac5 | 211 | _cs = LOW; |
| ianaherne | 1:67d99e4ce310 | 212 | |
| ianaherne | 1:67d99e4ce310 | 213 | _spi.write(&cmd[0],3,&data_buffer[0],0); |
| ianaherne | 1:67d99e4ce310 | 214 | |
| ianaherne | 1:67d99e4ce310 | 215 | cmd[0] = 0xFF; |
| ianaherne | 1:67d99e4ce310 | 216 | |
| ianaherne | 1:67d99e4ce310 | 217 | _spi.write(&cmd[0],1,&data_buffer[0],1); |
| ianaherne | 1:67d99e4ce310 | 218 | |
| ianaherne | 4:17e602b21ac5 | 219 | _cs = HIGH; |
| ianaherne | 1:67d99e4ce310 | 220 | |
| ianaherne | 1:67d99e4ce310 | 221 | _spi.unlock(); |
| ianaherne | 1:67d99e4ce310 | 222 | |
| ianaherne | 1:67d99e4ce310 | 223 | return data_buffer[0]; |
| ianaherne | 0:890f322344f2 | 224 | |
| ianaherne | 3:e846980a1536 | 225 | } |
| ianaherne | 3:e846980a1536 | 226 | |
| ianaherne | 3:e846980a1536 | 227 | /** |
| ianaherne | 3:e846980a1536 | 228 | * @brief page_erase() |
| ianaherne | 3:e846980a1536 | 229 | * @details 128 byte erase at page address |
| ianaherne | 3:e846980a1536 | 230 | * @param Page address |
| ianaherne | 3:e846980a1536 | 231 | * @return NA |
| ianaherne | 3:e846980a1536 | 232 | * @warning write enable must be called prior to calling this function or or |
| ianaherne | 3:e846980a1536 | 233 | * instruction will be ignored. |
| ianaherne | 3:e846980a1536 | 234 | * |
| ianaherne | 3:e846980a1536 | 235 | */ |
| ianaherne | 3:e846980a1536 | 236 | |
| ianaherne | 3:e846980a1536 | 237 | |
| ianaherne | 3:e846980a1536 | 238 | void rm25c512cl :: page_erase(uint16_t address){ |
| ianaherne | 3:e846980a1536 | 239 | |
| ianaherne | 3:e846980a1536 | 240 | char cmd[3]; |
| ianaherne | 3:e846980a1536 | 241 | char data_buffer[1];// not used here but required as function parameter |
| ianaherne | 3:e846980a1536 | 242 | |
| ianaherne | 3:e846980a1536 | 243 | cmd[0] = PERS; |
| ianaherne | 3:e846980a1536 | 244 | cmd[1] = address >> 8; |
| ianaherne | 3:e846980a1536 | 245 | cmd[2] = address & 0x00FF; |
| ianaherne | 3:e846980a1536 | 246 | |
| ianaherne | 3:e846980a1536 | 247 | _spi.lock(); |
| ianaherne | 3:e846980a1536 | 248 | |
| ianaherne | 4:17e602b21ac5 | 249 | _cs = LOW; |
| ianaherne | 3:e846980a1536 | 250 | |
| ianaherne | 3:e846980a1536 | 251 | _spi.write(&cmd[0],3,&data_buffer[0],0); |
| ianaherne | 3:e846980a1536 | 252 | |
| ianaherne | 4:17e602b21ac5 | 253 | _cs = HIGH; |
| ianaherne | 3:e846980a1536 | 254 | |
| ianaherne | 3:e846980a1536 | 255 | _spi.unlock(); |
| ianaherne | 3:e846980a1536 | 256 | |
| ianaherne | 3:e846980a1536 | 257 | |
| ianaherne | 3:e846980a1536 | 258 | |
| ianaherne | 3:e846980a1536 | 259 | } |
| ianaherne | 3:e846980a1536 | 260 | |
| ianaherne | 3:e846980a1536 | 261 | /** |
| ianaherne | 3:e846980a1536 | 262 | * @brief chip_erase() |
| ianaherne | 3:e846980a1536 | 263 | * @details erases all memory on chip |
| ianaherne | 3:e846980a1536 | 264 | * @param NA |
| ianaherne | 3:e846980a1536 | 265 | * @return NA |
| ianaherne | 3:e846980a1536 | 266 | * @warning write enable must be called prior to calling this function or or |
| ianaherne | 3:e846980a1536 | 267 | * instruction will be ignored. |
| ianaherne | 3:e846980a1536 | 268 | * |
| ianaherne | 3:e846980a1536 | 269 | */ |
| ianaherne | 3:e846980a1536 | 270 | |
| ianaherne | 3:e846980a1536 | 271 | void rm25c512cl :: chip_erase(){ |
| ianaherne | 3:e846980a1536 | 272 | |
| ianaherne | 3:e846980a1536 | 273 | char cmd[1]; |
| ianaherne | 3:e846980a1536 | 274 | char data_buffer[1];// not used here but required as function parameter |
| ianaherne | 3:e846980a1536 | 275 | |
| ianaherne | 3:e846980a1536 | 276 | cmd[0] = CERS; |
| ianaherne | 3:e846980a1536 | 277 | |
| ianaherne | 3:e846980a1536 | 278 | _spi.lock(); |
| ianaherne | 3:e846980a1536 | 279 | |
| ianaherne | 4:17e602b21ac5 | 280 | _cs = LOW; |
| ianaherne | 3:e846980a1536 | 281 | |
| ianaherne | 3:e846980a1536 | 282 | _spi.write(&cmd[0],1,&data_buffer[0],0); |
| ianaherne | 3:e846980a1536 | 283 | |
| ianaherne | 4:17e602b21ac5 | 284 | _cs = HIGH; |
| ianaherne | 3:e846980a1536 | 285 | |
| ianaherne | 3:e846980a1536 | 286 | _spi.unlock(); |
| ianaherne | 3:e846980a1536 | 287 | |
| ianaherne | 3:e846980a1536 | 288 | |
| ianaherne | 3:e846980a1536 | 289 | |
| ianaherne | 3:e846980a1536 | 290 | } |
| ianaherne | 3:e846980a1536 | 291 |