n-Blocks-HALL / RM25C512C-L_driver
Committer:
ianaherne
Date:
Wed Sep 12 12:14:39 2018 +0000
Revision:
1:67d99e4ce310
Parent:
0:890f322344f2
Child:
2:183d1d96f917
initial functionality of driver is tested and working, it stores a byte and reads a byte

Who changed what in which revision?

UserRevisionLine numberNew 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 1:67d99e4ce310 6 _cs = 1; // 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 1:67d99e4ce310 28 _cs = 0;
ianaherne 1:67d99e4ce310 29
ianaherne 1:67d99e4ce310 30 _spi.write(&cmd[0],1,&data_buffer[0],0);
ianaherne 1:67d99e4ce310 31
ianaherne 1:67d99e4ce310 32 _cs = 1;
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 1:67d99e4ce310 62 _cs = 0;
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 1:67d99e4ce310 70 _cs = 1;
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 1:67d99e4ce310 86 * @return NA
ianaherne 1:67d99e4ce310 87 * @warning A page write(128 bytes) can take up to 5 ms to complete
ianaherne 1:67d99e4ce310 88 *
ianaherne 1:67d99e4ce310 89 */
ianaherne 1:67d99e4ce310 90
ianaherne 1:67d99e4ce310 91 void rm25c512cl :: write_bytes(uint16_t address, char* data){
ianaherne 1:67d99e4ce310 92
ianaherne 1:67d99e4ce310 93 char cmd[3];
ianaherne 1:67d99e4ce310 94 char data_buffer[1];// not used here but required for spi.write() as a parameter
ianaherne 0:890f322344f2 95
ianaherne 0:890f322344f2 96
ianaherne 0:890f322344f2 97
ianaherne 1:67d99e4ce310 98 cmd[0] = WR;
ianaherne 1:67d99e4ce310 99 cmd[1] = address >> 8;
ianaherne 1:67d99e4ce310 100 cmd[2] = address & 0x00FF;
ianaherne 1:67d99e4ce310 101
ianaherne 1:67d99e4ce310 102 _spi.lock();
ianaherne 1:67d99e4ce310 103
ianaherne 1:67d99e4ce310 104 _cs = 0;
ianaherne 1:67d99e4ce310 105
ianaherne 1:67d99e4ce310 106 _spi.write(&cmd[0],3,&data_buffer[0],0);
ianaherne 1:67d99e4ce310 107 _spi.write(&data[0],sizeof(data),&data_buffer[0],0);
ianaherne 1:67d99e4ce310 108
ianaherne 1:67d99e4ce310 109 _cs = 1;
ianaherne 1:67d99e4ce310 110
ianaherne 1:67d99e4ce310 111 _spi.unlock();
ianaherne 1:67d99e4ce310 112
ianaherne 1:67d99e4ce310 113 wait_ms(5);
ianaherne 0:890f322344f2 114
ianaherne 0:890f322344f2 115
ianaherne 0:890f322344f2 116 }
ianaherne 0:890f322344f2 117
ianaherne 1:67d99e4ce310 118 /**
ianaherne 1:67d99e4ce310 119 * @brief write_byte()
ianaherne 1:67d99e4ce310 120 * @details Writes a single byte to EEprom at the given address
ianaherne 1:67d99e4ce310 121 * @param Address to write to
ianaherne 1:67d99e4ce310 122 * @param Value to write in given address
ianaherne 1:67d99e4ce310 123 * @return NA
ianaherne 1:67d99e4ce310 124 * @warning Byte write can take up to 100 us to complete
ianaherne 1:67d99e4ce310 125 *
ianaherne 1:67d99e4ce310 126 */
ianaherne 1:67d99e4ce310 127
ianaherne 1:67d99e4ce310 128 void rm25c512cl :: write_byte(uint16_t address,char value){
ianaherne 1:67d99e4ce310 129
ianaherne 1:67d99e4ce310 130 char cmd[4];
ianaherne 1:67d99e4ce310 131 char data_buffer[1]; // not used here but required for spi.write() as a parameter
ianaherne 1:67d99e4ce310 132
ianaherne 1:67d99e4ce310 133 cmd[0] = WR;
ianaherne 1:67d99e4ce310 134 cmd[1] = address >> 8;
ianaherne 1:67d99e4ce310 135 cmd[2] = address & 0x00FF;
ianaherne 1:67d99e4ce310 136 cmd[3] = value;
ianaherne 1:67d99e4ce310 137
ianaherne 1:67d99e4ce310 138 _spi.lock();
ianaherne 1:67d99e4ce310 139
ianaherne 1:67d99e4ce310 140 _cs = 0;
ianaherne 1:67d99e4ce310 141
ianaherne 1:67d99e4ce310 142 _spi.write(&cmd[0],4,&data_buffer[0],0);
ianaherne 1:67d99e4ce310 143
ianaherne 1:67d99e4ce310 144 _cs =1;
ianaherne 1:67d99e4ce310 145
ianaherne 1:67d99e4ce310 146 _spi.unlock();
ianaherne 1:67d99e4ce310 147
ianaherne 1:67d99e4ce310 148 wait_us(100);
ianaherne 0:890f322344f2 149
ianaherne 0:890f322344f2 150 }
ianaherne 0:890f322344f2 151
ianaherne 1:67d99e4ce310 152 /**
ianaherne 1:67d99e4ce310 153 * @brief read_bytes()
ianaherne 1:67d99e4ce310 154 * @details Reads page of data(128 bytes) at given address.
ianaherne 1:67d99e4ce310 155 * @param Page Address to start reading from.
ianaherne 1:67d99e4ce310 156 * @param Pointer to data buffer to store multiple bytes read from EEprom
ianaherne 1:67d99e4ce310 157 * @return
ianaherne 1:67d99e4ce310 158 * @warning Data buffer must be of size 128 bytes to read entire page.
ianaherne 1:67d99e4ce310 159 *
ianaherne 1:67d99e4ce310 160 */
ianaherne 1:67d99e4ce310 161
ianaherne 1:67d99e4ce310 162 void rm25c512cl :: read_bytes(uint16_t address, char* data_buffer){
ianaherne 1:67d99e4ce310 163
ianaherne 1:67d99e4ce310 164 char cmd[3];
ianaherne 1:67d99e4ce310 165
ianaherne 1:67d99e4ce310 166
ianaherne 1:67d99e4ce310 167 cmd[0] = READ;
ianaherne 1:67d99e4ce310 168 cmd[1] = address >> 8;
ianaherne 1:67d99e4ce310 169 cmd[2] = address & 0x00FF;
ianaherne 1:67d99e4ce310 170
ianaherne 1:67d99e4ce310 171
ianaherne 1:67d99e4ce310 172 _spi.lock();
ianaherne 1:67d99e4ce310 173
ianaherne 1:67d99e4ce310 174 _cs = 0;
ianaherne 1:67d99e4ce310 175
ianaherne 1:67d99e4ce310 176 _spi.write(&cmd[0],3,&data_buffer[0],0);
ianaherne 1:67d99e4ce310 177
ianaherne 1:67d99e4ce310 178 cmd[0] = 0xFF;
ianaherne 1:67d99e4ce310 179
ianaherne 1:67d99e4ce310 180 _spi.write(&cmd[0],1,&data_buffer[0],sizeof(data_buffer));
ianaherne 1:67d99e4ce310 181
ianaherne 1:67d99e4ce310 182 _cs = 1;
ianaherne 1:67d99e4ce310 183
ianaherne 1:67d99e4ce310 184 _spi.unlock();
ianaherne 0:890f322344f2 185
ianaherne 0:890f322344f2 186
ianaherne 0:890f322344f2 187 }
ianaherne 0:890f322344f2 188
ianaherne 1:67d99e4ce310 189 /**
ianaherne 1:67d99e4ce310 190 * @brief read_byte()
ianaherne 1:67d99e4ce310 191 * @details Reads a byte of data at given address.
ianaherne 1:67d99e4ce310 192 * @param Address to read byte from.
ianaherne 1:67d99e4ce310 193 * @return Byte of data read from address.
ianaherne 1:67d99e4ce310 194 * @warning
ianaherne 1:67d99e4ce310 195 *
ianaherne 1:67d99e4ce310 196 */
ianaherne 1:67d99e4ce310 197
ianaherne 1:67d99e4ce310 198 char rm25c512cl :: read_byte(uint16_t address){
ianaherne 1:67d99e4ce310 199
ianaherne 1:67d99e4ce310 200 char cmd[3];
ianaherne 1:67d99e4ce310 201 char data_buffer[1];// To store data byte returned from EEprom
ianaherne 1:67d99e4ce310 202
ianaherne 1:67d99e4ce310 203 cmd[0] = READ;
ianaherne 1:67d99e4ce310 204 cmd[1] = address >> 8;
ianaherne 1:67d99e4ce310 205 cmd[2] = address & 0x00FF;
ianaherne 1:67d99e4ce310 206
ianaherne 1:67d99e4ce310 207 _spi.lock();
ianaherne 1:67d99e4ce310 208
ianaherne 1:67d99e4ce310 209 _cs = 0;
ianaherne 1:67d99e4ce310 210
ianaherne 1:67d99e4ce310 211 _spi.write(&cmd[0],3,&data_buffer[0],0);
ianaherne 1:67d99e4ce310 212
ianaherne 1:67d99e4ce310 213 cmd[0] = 0xFF;
ianaherne 1:67d99e4ce310 214
ianaherne 1:67d99e4ce310 215 _spi.write(&cmd[0],1,&data_buffer[0],1);
ianaherne 1:67d99e4ce310 216
ianaherne 1:67d99e4ce310 217 _cs = 1;
ianaherne 1:67d99e4ce310 218
ianaherne 1:67d99e4ce310 219 _spi.unlock();
ianaherne 1:67d99e4ce310 220
ianaherne 1:67d99e4ce310 221 return data_buffer[0];
ianaherne 0:890f322344f2 222
ianaherne 0:890f322344f2 223 }