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.
Memory.cpp@4:bb4fd1147054, 2017-02-09 (annotated)
- Committer:
- ThomasSonderDesign
- Date:
- Thu Feb 09 00:51:57 2017 +0000
- Revision:
- 4:bb4fd1147054
- Parent:
- 3:339efdc5134f
- Child:
- 5:2fa79108a29b
Updated so that you can write to any address in a page, not just the start.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ThomasSonderDesign | 0:c2e41d203cdb | 1 | #include "mbed.h" |
| ThomasSonderDesign | 0:c2e41d203cdb | 2 | #include "Memory.h" |
| ThomasSonderDesign | 0:c2e41d203cdb | 3 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 4 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 5 | Memory::Memory(PinName chipSelect) : _cs_mem(chipSelect) |
| ThomasSonderDesign | 0:c2e41d203cdb | 6 | { |
| ThomasSonderDesign | 0:c2e41d203cdb | 7 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 8 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 9 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 10 | * Reads 'length' elements into a char array starting at the 24bit Address. |
| ThomasSonderDesign | 0:c2e41d203cdb | 11 | *If length is greater than BufferSize (3840 bytes) the function will terminate |
| ThomasSonderDesign | 0:c2e41d203cdb | 12 | *and return the start address. |
| ThomasSonderDesign | 0:c2e41d203cdb | 13 | */ |
| ThomasSonderDesign | 0:c2e41d203cdb | 14 | int Memory::readData(SPI my_spi, char value [], int Address, int length) |
| ThomasSonderDesign | 0:c2e41d203cdb | 15 | { |
| ThomasSonderDesign | 0:c2e41d203cdb | 16 | if(length>bufferSize) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 17 | printf("\nLength %i exceeds Max Length\n",length); |
| ThomasSonderDesign | 0:c2e41d203cdb | 18 | return Address; |
| ThomasSonderDesign | 0:c2e41d203cdb | 19 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 20 | _cs_mem = 1; //Ensure cs is deselected |
| ThomasSonderDesign | 0:c2e41d203cdb | 21 | wait_us(10); |
| ThomasSonderDesign | 0:c2e41d203cdb | 22 | _cs_mem = 0; //memory is selected |
| ThomasSonderDesign | 0:c2e41d203cdb | 23 | my_spi.write(0x03); //Send read command |
| ThomasSonderDesign | 0:c2e41d203cdb | 24 | my_spi.write(Address>>16); //Send high address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 25 | my_spi.write(Address>>8); //Send mid address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 26 | my_spi.write(Address); //Send low address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 27 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 28 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 29 | for(int i =0; i <length; i++) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 30 | value[i]= my_spi.write(dummy);//Send dummy byte to read out value ate Address |
| ThomasSonderDesign | 0:c2e41d203cdb | 31 | Address++; |
| ThomasSonderDesign | 0:c2e41d203cdb | 32 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 33 | _cs_mem = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 34 | return Address; //Return the address of the next unread byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 35 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 36 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 37 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 38 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 39 | * Sector Erase, erases everything in the 4KB sector that includes Address |
| ThomasSonderDesign | 0:c2e41d203cdb | 40 | */ |
| ThomasSonderDesign | 0:c2e41d203cdb | 41 | void Memory::sectorErase(SPI my_spi, long Address) |
| ThomasSonderDesign | 0:c2e41d203cdb | 42 | { |
| ThomasSonderDesign | 0:c2e41d203cdb | 43 | _cs_mem = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 44 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 45 | my_spi.write(0x06); //Send Write enable command |
| ThomasSonderDesign | 0:c2e41d203cdb | 46 | _cs_mem= 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 47 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 48 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 49 | my_spi.write(0x20); //Send sector erase comand |
| ThomasSonderDesign | 0:c2e41d203cdb | 50 | my_spi.write(Address>>16); //Send high address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 51 | my_spi.write(Address>>8); //Send mid address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 52 | my_spi.write(Address); //Send low address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 53 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 54 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 55 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 56 | //Pol the status register untill the Write In Progress bit is no longer set |
| ThomasSonderDesign | 0:c2e41d203cdb | 57 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 58 | my_spi.write(05); |
| ThomasSonderDesign | 0:c2e41d203cdb | 59 | int byte1 = my_spi.write(dummy); |
| ThomasSonderDesign | 0:c2e41d203cdb | 60 | while(byte1>0) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 61 | byte1 = my_spi.write(dummy); |
| ThomasSonderDesign | 0:c2e41d203cdb | 62 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 63 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 64 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 65 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 66 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 67 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 68 | * Block Erase, erases everything in a 4KB block that includes Address |
| ThomasSonderDesign | 0:c2e41d203cdb | 69 | */ |
| ThomasSonderDesign | 0:c2e41d203cdb | 70 | void Memory::blockErase(SPI my_spi, int Address) |
| ThomasSonderDesign | 0:c2e41d203cdb | 71 | { |
| ThomasSonderDesign | 0:c2e41d203cdb | 72 | _cs_mem = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 73 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 74 | my_spi.write(0x06); //Send Write enable command |
| ThomasSonderDesign | 0:c2e41d203cdb | 75 | _cs_mem= 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 76 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 77 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 78 | my_spi.write(0xD8); //Send sector erase comand |
| ThomasSonderDesign | 0:c2e41d203cdb | 79 | my_spi.write((Address>>16)&0xff); //Send high address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 80 | my_spi.write((Address>>8)&0xff); //Send mid address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 81 | my_spi.write((Address)&0xff); //Send low address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 82 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 83 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 84 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 85 | //Pol the status register untill the Write In Progress bit is no longer set |
| ThomasSonderDesign | 0:c2e41d203cdb | 86 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 87 | my_spi.write(05); |
| ThomasSonderDesign | 0:c2e41d203cdb | 88 | int byte1 = my_spi.write(dummy); |
| ThomasSonderDesign | 0:c2e41d203cdb | 89 | while(byte1>0) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 90 | byte1 = my_spi.write(dummy); |
| ThomasSonderDesign | 0:c2e41d203cdb | 91 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 92 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 93 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 94 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 95 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 96 | * Writes a char array containg 'length' elements to memory sarting at address. |
| ThomasSonderDesign | 0:c2e41d203cdb | 97 | *If length is greater than BufferSize (3840 bytes) the function will terminate |
| ThomasSonderDesign | 0:c2e41d203cdb | 98 | *and return the start address. |
| ThomasSonderDesign | 0:c2e41d203cdb | 99 | */ |
| ThomasSonderDesign | 0:c2e41d203cdb | 100 | int Memory::writeData(SPI my_spi, char buffer[], int address, int length) |
| ThomasSonderDesign | 0:c2e41d203cdb | 101 | { |
| ThomasSonderDesign | 0:c2e41d203cdb | 102 | if(length>bufferSize) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 103 | printf("\nLength %i exceeds Max Length\n",length); |
| ThomasSonderDesign | 0:c2e41d203cdb | 104 | return address; |
| ThomasSonderDesign | 0:c2e41d203cdb | 105 | } |
| ThomasSonderDesign | 4:bb4fd1147054 | 106 | |
| ThomasSonderDesign | 4:bb4fd1147054 | 107 | //Enable the memory for wiring. This segment is only required if not writing to the start of a page. |
| ThomasSonderDesign | 4:bb4fd1147054 | 108 | if(address%256!=0){ |
| ThomasSonderDesign | 4:bb4fd1147054 | 109 | _cs_mem=1; |
| ThomasSonderDesign | 4:bb4fd1147054 | 110 | _cs_mem=0; //Selet memory |
| ThomasSonderDesign | 4:bb4fd1147054 | 111 | my_spi.write(06); //Set Write Enable flag in the status reg |
| ThomasSonderDesign | 4:bb4fd1147054 | 112 | _cs_mem=1; //Deslect memory |
| ThomasSonderDesign | 4:bb4fd1147054 | 113 | wait_us(10); |
| ThomasSonderDesign | 4:bb4fd1147054 | 114 | _cs_mem=0; //Selet memory |
| ThomasSonderDesign | 4:bb4fd1147054 | 115 | my_spi.write(0x02); //Send write comand |
| ThomasSonderDesign | 4:bb4fd1147054 | 116 | my_spi.write(address>>16); //Send high address byte |
| ThomasSonderDesign | 4:bb4fd1147054 | 117 | my_spi.write(address>>8); //Send middle adress byte |
| ThomasSonderDesign | 4:bb4fd1147054 | 118 | my_spi.write(address); //Send low address |
| ThomasSonderDesign | 4:bb4fd1147054 | 119 | } |
| ThomasSonderDesign | 4:bb4fd1147054 | 120 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 121 | for(int i =0; i<length; i++) { |
| ThomasSonderDesign | 4:bb4fd1147054 | 122 | //Handle start and end of pages. aAt the page boundry the memory must be deselected and and re-enabled for writng to the next page. |
| ThomasSonderDesign | 4:bb4fd1147054 | 123 | if(address%256==0) { |
| ThomasSonderDesign | 4:bb4fd1147054 | 124 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 125 | wait_us(10); |
| ThomasSonderDesign | 4:bb4fd1147054 | 126 | //wait for the WIP bit to go low |
| ThomasSonderDesign | 4:bb4fd1147054 | 127 | _cs_mem=0; //Selet memory |
| ThomasSonderDesign | 4:bb4fd1147054 | 128 | my_spi.write(0x05); //Send read status register command |
| ThomasSonderDesign | 4:bb4fd1147054 | 129 | int byte1 = my_spi.write(dummy);//Send dummy byte to read status reg |
| ThomasSonderDesign | 4:bb4fd1147054 | 130 | while ((byte1&1)>0) { |
| ThomasSonderDesign | 4:bb4fd1147054 | 131 | wait_us(10); |
| ThomasSonderDesign | 4:bb4fd1147054 | 132 | my_spi.write(0x05); //Send read status register command |
| ThomasSonderDesign | 4:bb4fd1147054 | 133 | byte1 = my_spi.write(dummy);//Send dummy byte to read status reg |
| ThomasSonderDesign | 4:bb4fd1147054 | 134 | } |
| ThomasSonderDesign | 4:bb4fd1147054 | 135 | _cs_mem=1; |
| ThomasSonderDesign | 4:bb4fd1147054 | 136 | _cs_mem=0; //Selet memory |
| ThomasSonderDesign | 4:bb4fd1147054 | 137 | my_spi.write(06); //Set Write Enable flag in the status reg |
| ThomasSonderDesign | 4:bb4fd1147054 | 138 | _cs_mem=1; //Deslect memory |
| ThomasSonderDesign | 4:bb4fd1147054 | 139 | wait_us(10); |
| ThomasSonderDesign | 4:bb4fd1147054 | 140 | _cs_mem=0; //Selet memory |
| ThomasSonderDesign | 4:bb4fd1147054 | 141 | my_spi.write(0x02); //Send write comand |
| ThomasSonderDesign | 4:bb4fd1147054 | 142 | my_spi.write(address>>16); //Send high address byte |
| ThomasSonderDesign | 4:bb4fd1147054 | 143 | my_spi.write(address>>8); //Send middle adress byte |
| ThomasSonderDesign | 4:bb4fd1147054 | 144 | my_spi.write(address); //Send low address |
| ThomasSonderDesign | 0:c2e41d203cdb | 145 | } |
| ThomasSonderDesign | 4:bb4fd1147054 | 146 | |
| ThomasSonderDesign | 4:bb4fd1147054 | 147 | my_spi.write(buffer[i]); //Write the value of the buffer to memory |
| ThomasSonderDesign | 0:c2e41d203cdb | 148 | wait_us(5); |
| AdminSonderDesign | 3:339efdc5134f | 149 | address=address+1; //Increment address |
| ThomasSonderDesign | 0:c2e41d203cdb | 150 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 151 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 152 | return address; |
| ThomasSonderDesign | 0:c2e41d203cdb | 153 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 154 | |
| AdminSonderDesign | 3:339efdc5134f | 155 | /** |
| AdminSonderDesign | 3:339efdc5134f | 156 | * Arrange the data to the NEW address for icon refreshing |
| AdminSonderDesign | 3:339efdc5134f | 157 | */ |
| AdminSonderDesign | 3:339efdc5134f | 158 | int Memory::arrangeData(SPI my_spi, int Address, int addressNew, int length) |
| AdminSonderDesign | 3:339efdc5134f | 159 | { |
| AdminSonderDesign | 3:339efdc5134f | 160 | char value[length]; |
| AdminSonderDesign | 3:339efdc5134f | 161 | _cs_mem = 1; //Ensure cs is deselected |
| AdminSonderDesign | 3:339efdc5134f | 162 | wait_us(10); |
| AdminSonderDesign | 3:339efdc5134f | 163 | _cs_mem = 0; //memory is selected |
| AdminSonderDesign | 3:339efdc5134f | 164 | my_spi.write(0x03); //Send read command |
| AdminSonderDesign | 3:339efdc5134f | 165 | my_spi.write(Address>>16); //Send high address byte |
| AdminSonderDesign | 3:339efdc5134f | 166 | my_spi.write(Address>>8); //Send mid address byte |
| AdminSonderDesign | 3:339efdc5134f | 167 | my_spi.write(Address); //Send low address byte |
| AdminSonderDesign | 3:339efdc5134f | 168 | |
| AdminSonderDesign | 3:339efdc5134f | 169 | |
| AdminSonderDesign | 3:339efdc5134f | 170 | for(int i =0; i <length; i++) { |
| AdminSonderDesign | 3:339efdc5134f | 171 | value[i]= my_spi.write(dummy);//Send dummy byte to read out value ate Address |
| AdminSonderDesign | 3:339efdc5134f | 172 | Address++; |
| AdminSonderDesign | 3:339efdc5134f | 173 | } |
| AdminSonderDesign | 3:339efdc5134f | 174 | _cs_mem = 1; |
| AdminSonderDesign | 3:339efdc5134f | 175 | |
| AdminSonderDesign | 3:339efdc5134f | 176 | wait_ms(50);//wait for command transfer |
| AdminSonderDesign | 3:339efdc5134f | 177 | |
| AdminSonderDesign | 3:339efdc5134f | 178 | for(int i =0; i<length; i++) { |
| AdminSonderDesign | 3:339efdc5134f | 179 | if(addressNew%256==0) { //Handle start and end of pages |
| AdminSonderDesign | 3:339efdc5134f | 180 | _cs_mem=1; |
| AdminSonderDesign | 3:339efdc5134f | 181 | wait_us(10); |
| AdminSonderDesign | 3:339efdc5134f | 182 | //wait for the WIP bit to go low |
| AdminSonderDesign | 3:339efdc5134f | 183 | _cs_mem=0; //Selet memory |
| AdminSonderDesign | 3:339efdc5134f | 184 | my_spi.write(0x05); //Send read status register command |
| AdminSonderDesign | 3:339efdc5134f | 185 | int byte1 = my_spi.write(dummy);//Send dummy byte to read status reg |
| AdminSonderDesign | 3:339efdc5134f | 186 | while ((byte1&1)>0) { |
| AdminSonderDesign | 3:339efdc5134f | 187 | wait_us(10); |
| AdminSonderDesign | 3:339efdc5134f | 188 | my_spi.write(0x05); //Send read status register command |
| AdminSonderDesign | 3:339efdc5134f | 189 | byte1 = my_spi.write(dummy);//Send dummy byte to read status reg |
| AdminSonderDesign | 3:339efdc5134f | 190 | } |
| AdminSonderDesign | 3:339efdc5134f | 191 | _cs_mem=1; |
| AdminSonderDesign | 3:339efdc5134f | 192 | |
| AdminSonderDesign | 3:339efdc5134f | 193 | _cs_mem=0; //Selet memory |
| AdminSonderDesign | 3:339efdc5134f | 194 | my_spi.write(06); //Set Write Enable flag in the status reg |
| AdminSonderDesign | 3:339efdc5134f | 195 | _cs_mem=1; |
| AdminSonderDesign | 3:339efdc5134f | 196 | wait_us(10); |
| AdminSonderDesign | 3:339efdc5134f | 197 | |
| AdminSonderDesign | 3:339efdc5134f | 198 | _cs_mem=0; //Selet memory |
| AdminSonderDesign | 3:339efdc5134f | 199 | my_spi.write(0x02); //Send read comand |
| AdminSonderDesign | 3:339efdc5134f | 200 | my_spi.write(addressNew>>16); //Send high address byte |
| AdminSonderDesign | 3:339efdc5134f | 201 | my_spi.write(addressNew>>8); //Send middle adress byte |
| AdminSonderDesign | 3:339efdc5134f | 202 | my_spi.write(addressNew); //Send low address |
| AdminSonderDesign | 3:339efdc5134f | 203 | |
| AdminSonderDesign | 3:339efdc5134f | 204 | } |
| AdminSonderDesign | 3:339efdc5134f | 205 | my_spi.write(value[i]); //Write the calue of the buffer to memory |
| AdminSonderDesign | 3:339efdc5134f | 206 | wait_us(5); |
| AdminSonderDesign | 3:339efdc5134f | 207 | addressNew=addressNew+1; //Increment address |
| AdminSonderDesign | 3:339efdc5134f | 208 | } |
| AdminSonderDesign | 3:339efdc5134f | 209 | _cs_mem=1; |
| AdminSonderDesign | 3:339efdc5134f | 210 | wait_ms(10); //Need to wait register response, otherwise will cause the output full of dummies |
| AdminSonderDesign | 3:339efdc5134f | 211 | return addressNew; |
| AdminSonderDesign | 3:339efdc5134f | 212 | } |