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.
Dependents: BlackBoard_Firmware_Fast_read_not_test
Fork of Memory by
Memory.cpp@8:6af25b9563df, 2018-04-15 (annotated)
- Committer:
- ThomasSonderDesign
- Date:
- Sun Apr 15 23:19:56 2018 +0000
- Revision:
- 8:6af25b9563df
- Parent:
- 6:c11cb6709b8b
- Child:
- 9:2587f246bf48
Small update made to speed up read time.
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 | 8:6af25b9563df | 14 | int Memory::readData(SPI my_spi, short value [], int Address, int length) |
| ThomasSonderDesign | 0:c2e41d203cdb | 15 | { |
| ThomasSonderDesign | 8:6af25b9563df | 16 | //if(length>bufferSize) { |
| ThomasSonderDesign | 8:6af25b9563df | 17 | //printf("\nLength %i exceeds Max Length\n",length); |
| ThomasSonderDesign | 8:6af25b9563df | 18 | //return Address; |
| ThomasSonderDesign | 8:6af25b9563df | 19 | //} |
| ThomasSonderDesign | 8:6af25b9563df | 20 | int temp = 0; |
| ThomasSonderDesign | 8:6af25b9563df | 21 | int temp1 = 0; |
| ThomasSonderDesign | 8:6af25b9563df | 22 | int temp2 =0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 23 | _cs_mem = 1; //Ensure cs is deselected |
| ThomasSonderDesign | 0:c2e41d203cdb | 24 | wait_us(10); |
| ThomasSonderDesign | 0:c2e41d203cdb | 25 | _cs_mem = 0; //memory is selected |
| ThomasSonderDesign | 0:c2e41d203cdb | 26 | my_spi.write(0x03); //Send read command |
| ThomasSonderDesign | 0:c2e41d203cdb | 27 | my_spi.write(Address>>16); //Send high address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 28 | my_spi.write(Address>>8); //Send mid address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 29 | my_spi.write(Address); //Send low address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 30 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 31 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 32 | for(int i =0; i <length; i++) { |
| ThomasSonderDesign | 8:6af25b9563df | 33 | temp = my_spi.write(dummy);//Send dummy byte to read out value ate Address |
| ThomasSonderDesign | 8:6af25b9563df | 34 | value[i]= (temp); |
| ThomasSonderDesign | 8:6af25b9563df | 35 | //printf(" %X",value[i]); |
| ThomasSonderDesign | 0:c2e41d203cdb | 36 | Address++; |
| ThomasSonderDesign | 0:c2e41d203cdb | 37 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 38 | _cs_mem = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 39 | return Address; //Return the address of the next unread byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 40 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 41 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 42 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 43 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 44 | * Sector Erase, erases everything in the 4KB sector that includes Address |
| ThomasSonderDesign | 0:c2e41d203cdb | 45 | */ |
| ThomasSonderDesign | 0:c2e41d203cdb | 46 | void Memory::sectorErase(SPI my_spi, long Address) |
| ThomasSonderDesign | 0:c2e41d203cdb | 47 | { |
| ThomasSonderDesign | 0:c2e41d203cdb | 48 | _cs_mem = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 49 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 50 | my_spi.write(0x06); //Send Write enable command |
| ThomasSonderDesign | 0:c2e41d203cdb | 51 | _cs_mem= 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 52 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 53 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 54 | my_spi.write(0x20); //Send sector erase comand |
| ThomasSonderDesign | 0:c2e41d203cdb | 55 | my_spi.write(Address>>16); //Send high address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 56 | my_spi.write(Address>>8); //Send mid address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 57 | my_spi.write(Address); //Send low address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 58 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 59 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 60 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 61 | //Pol the status register untill the Write In Progress bit is no longer set |
| ThomasSonderDesign | 0:c2e41d203cdb | 62 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 63 | my_spi.write(05); |
| ThomasSonderDesign | 0:c2e41d203cdb | 64 | int byte1 = my_spi.write(dummy); |
| ThomasSonderDesign | 0:c2e41d203cdb | 65 | while(byte1>0) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 66 | byte1 = my_spi.write(dummy); |
| ThomasSonderDesign | 0:c2e41d203cdb | 67 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 68 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 69 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 70 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 71 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 72 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 73 | * Block Erase, erases everything in a 4KB block that includes Address |
| ThomasSonderDesign | 0:c2e41d203cdb | 74 | */ |
| ThomasSonderDesign | 2:5c70c0334ed0 | 75 | int Memory::blockErase(SPI my_spi, int Address) |
| ThomasSonderDesign | 0:c2e41d203cdb | 76 | { |
| ThomasSonderDesign | 0:c2e41d203cdb | 77 | _cs_mem = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 78 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 79 | my_spi.write(0x06); //Send Write enable command |
| ThomasSonderDesign | 0:c2e41d203cdb | 80 | _cs_mem= 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 81 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 82 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 83 | my_spi.write(0xD8); //Send sector erase comand |
| ThomasSonderDesign | 0:c2e41d203cdb | 84 | my_spi.write((Address>>16)&0xff); //Send high address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 85 | my_spi.write((Address>>8)&0xff); //Send mid address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 86 | my_spi.write((Address)&0xff); //Send low address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 87 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 88 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 89 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 90 | //Pol the status register untill the Write In Progress bit is no longer set |
| ThomasSonderDesign | 0:c2e41d203cdb | 91 | _cs_mem=0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 92 | my_spi.write(05); |
| ThomasSonderDesign | 0:c2e41d203cdb | 93 | int byte1 = my_spi.write(dummy); |
| ThomasSonderDesign | 0:c2e41d203cdb | 94 | while(byte1>0) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 95 | byte1 = my_spi.write(dummy); |
| ThomasSonderDesign | 0:c2e41d203cdb | 96 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 97 | _cs_mem=1; |
| ThomasSonderDesign | 2:5c70c0334ed0 | 98 | int returnVal = (Address/0x10000); |
| ThomasSonderDesign | 2:5c70c0334ed0 | 99 | returnVal*=0x10000; |
| ThomasSonderDesign | 2:5c70c0334ed0 | 100 | returnVal+=0x10000; |
| ThomasSonderDesign | 2:5c70c0334ed0 | 101 | return returnVal; |
| ThomasSonderDesign | 0:c2e41d203cdb | 102 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 103 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 104 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 105 | * Writes a char array containg 'length' elements to memory sarting at address. |
| ThomasSonderDesign | 0:c2e41d203cdb | 106 | *If length is greater than BufferSize (3840 bytes) the function will terminate |
| ThomasSonderDesign | 0:c2e41d203cdb | 107 | *and return the start address. |
| ThomasSonderDesign | 0:c2e41d203cdb | 108 | */ |
| ThomasSonderDesign | 0:c2e41d203cdb | 109 | int Memory::writeData(SPI my_spi, char buffer[], int address, int length) |
| ThomasSonderDesign | 0:c2e41d203cdb | 110 | { |
| ThomasSonderDesign | 0:c2e41d203cdb | 111 | if(length>bufferSize) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 112 | printf("\nLength %i exceeds Max Length\n",length); |
| ThomasSonderDesign | 0:c2e41d203cdb | 113 | return address; |
| ThomasSonderDesign | 0:c2e41d203cdb | 114 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 115 | for(int i =0; i<length; i++) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 116 | if(address%256==0) { //Handle start and end of pages |
| ThomasSonderDesign | 0:c2e41d203cdb | 117 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 118 | wait_us(10); |
| ThomasSonderDesign | 0:c2e41d203cdb | 119 | //wait for the WIP bit to go low |
| ThomasSonderDesign | 0:c2e41d203cdb | 120 | _cs_mem=0; //Selet memory |
| ThomasSonderDesign | 0:c2e41d203cdb | 121 | my_spi.write(0x05); //Send read status register command |
| ThomasSonderDesign | 0:c2e41d203cdb | 122 | int byte1 = my_spi.write(dummy);//Send dummy byte to read status reg |
| ThomasSonderDesign | 0:c2e41d203cdb | 123 | while ((byte1&1)>0) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 124 | wait_us(10); |
| ThomasSonderDesign | 0:c2e41d203cdb | 125 | my_spi.write(0x05); //Send read status register command |
| ThomasSonderDesign | 0:c2e41d203cdb | 126 | byte1 = my_spi.write(dummy);//Send dummy byte to read status reg |
| ThomasSonderDesign | 0:c2e41d203cdb | 127 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 128 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 129 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 130 | _cs_mem=0; //Selet memory |
| ThomasSonderDesign | 0:c2e41d203cdb | 131 | my_spi.write(06); //Set Write Enable flag in the status reg |
| ThomasSonderDesign | 0:c2e41d203cdb | 132 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 133 | wait_us(10); |
| ThomasSonderDesign | 0:c2e41d203cdb | 134 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 135 | _cs_mem=0; //Selet memory |
| ThomasSonderDesign | 0:c2e41d203cdb | 136 | my_spi.write(02); //Send read comand |
| ThomasSonderDesign | 0:c2e41d203cdb | 137 | my_spi.write(address>>16); //Send high address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 138 | my_spi.write(address>>8); //Send middle adress byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 139 | my_spi.write(address); //Send low address |
| ThomasSonderDesign | 0:c2e41d203cdb | 140 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 141 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 142 | my_spi.write(buffer[i]); //Write the calue of the buffer to memory |
| ThomasSonderDesign | 6:c11cb6709b8b | 143 | //printf("%i%i%i%i%i%i%i%i",sixtyBytes[i]>>7&&1,buffer[i]>>6&&1,buffer[i]>>5&&1,buffer[i]>>4&&1,buffer[i]>>3&&1,buffer[i]>>2&&1,buffer[i]>>1&&1,buffer[i]&&1); |
| ThomasSonderDesign | 6:c11cb6709b8b | 144 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 145 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 146 | address=address++; //Increment address |
| ThomasSonderDesign | 0:c2e41d203cdb | 147 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 148 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 149 | return address; |
| ThomasSonderDesign | 0:c2e41d203cdb | 150 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 151 | |
| ThomasSonderDesign | 8:6af25b9563df | 152 |
