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@11:e3cbdbe3d77b, 2018-07-07 (annotated)
- Committer:
- Xinda
- Date:
- Sat Jul 07 11:45:08 2018 +0000
- Revision:
- 11:e3cbdbe3d77b
- Parent:
- 10:96b5e7dcc91f
July 7th
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" |
| Xinda | 11:e3cbdbe3d77b | 3 | #define TNF 0x02 |
| Xinda | 11:e3cbdbe3d77b | 4 | #define TFE 0x01 |
| Xinda | 11:e3cbdbe3d77b | 5 | #define RNE 0x04 |
| ThomasSonderDesign | 0:c2e41d203cdb | 6 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 7 | Memory::Memory(PinName chipSelect) : _cs_mem(chipSelect) |
| ThomasSonderDesign | 0:c2e41d203cdb | 8 | { |
| ThomasSonderDesign | 0:c2e41d203cdb | 9 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 10 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 11 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 12 | * Reads 'length' elements into a char array starting at the 24bit Address. |
| ThomasSonderDesign | 0:c2e41d203cdb | 13 | *If length is greater than BufferSize (3840 bytes) the function will terminate |
| ThomasSonderDesign | 0:c2e41d203cdb | 14 | *and return the start address. |
| ThomasSonderDesign | 0:c2e41d203cdb | 15 | */ |
| Xinda | 11:e3cbdbe3d77b | 16 | /*** My method to mywrite to FeRAM ***/ |
| Xinda | 11:e3cbdbe3d77b | 17 | void MEMmywrite (unsigned char data) { |
| Xinda | 11:e3cbdbe3d77b | 18 | // First don't mywrite to the FIFO buffer if it is full |
| Xinda | 11:e3cbdbe3d77b | 19 | while (!(LPC_SSP0->SR & TNF)) // While TNF-Bit = 0 (FIFO full)... |
| Xinda | 11:e3cbdbe3d77b | 20 | ; // Wait |
| Xinda | 11:e3cbdbe3d77b | 21 | LPC_SSP0->DR = data; // mywrite to FIFO buffer |
| Xinda | 11:e3cbdbe3d77b | 22 | } |
| Xinda | 11:e3cbdbe3d77b | 23 | |
| Xinda | 11:e3cbdbe3d77b | 24 | int Memory::readData(short value [], int Address, int length) |
| ThomasSonderDesign | 0:c2e41d203cdb | 25 | { |
| ThomasSonderDesign | 8:6af25b9563df | 26 | //if(length>bufferSize) { |
| ThomasSonderDesign | 8:6af25b9563df | 27 | //printf("\nLength %i exceeds Max Length\n",length); |
| ThomasSonderDesign | 8:6af25b9563df | 28 | //return Address; |
| ThomasSonderDesign | 8:6af25b9563df | 29 | //} |
| ThomasSonderDesign | 8:6af25b9563df | 30 | int temp = 0; |
| ThomasSonderDesign | 8:6af25b9563df | 31 | int temp1 = 0; |
| ThomasSonderDesign | 8:6af25b9563df | 32 | int temp2 =0; |
| ThomasSonderDesign | 0:c2e41d203cdb | 33 | _cs_mem = 1; //Ensure cs is deselected |
| ThomasSonderDesign | 0:c2e41d203cdb | 34 | wait_us(10); |
| ThomasSonderDesign | 0:c2e41d203cdb | 35 | _cs_mem = 0; //memory is selected |
| Xinda | 11:e3cbdbe3d77b | 36 | MEMmywrite(0x03); //Send read command |
| Xinda | 11:e3cbdbe3d77b | 37 | MEMmywrite(Address>>16); //Send high address byte |
| Xinda | 11:e3cbdbe3d77b | 38 | MEMmywrite(Address>>8); //Send mid address byte |
| Xinda | 11:e3cbdbe3d77b | 39 | MEMmywrite(Address); //Send low address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 40 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 41 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 42 | for(int i =0; i <length; i++) { |
| Xinda | 11:e3cbdbe3d77b | 43 | MEMmywrite(dummy);//Send dummy byte to read out value ate Address |
| Xinda | 11:e3cbdbe3d77b | 44 | while (LPC_SSP0->SR & RNE) |
| Xinda | 11:e3cbdbe3d77b | 45 | temp = LPC_SSP0->DR; |
| ThomasSonderDesign | 8:6af25b9563df | 46 | value[i]= (temp); |
| ThomasSonderDesign | 8:6af25b9563df | 47 | //printf(" %X",value[i]); |
| ThomasSonderDesign | 0:c2e41d203cdb | 48 | Address++; |
| ThomasSonderDesign | 0:c2e41d203cdb | 49 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 50 | _cs_mem = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 51 | return Address; //Return the address of the next unread byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 52 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 53 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 54 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 55 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 56 | * Sector Erase, erases everything in the 4KB sector that includes Address |
| ThomasSonderDesign | 0:c2e41d203cdb | 57 | */ |
| Xinda | 11:e3cbdbe3d77b | 58 | void Memory::sectorErase(long Address) |
| ThomasSonderDesign | 0:c2e41d203cdb | 59 | { |
| Xinda | 11:e3cbdbe3d77b | 60 | int byte1 = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 61 | _cs_mem = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 62 | _cs_mem=0; |
| Xinda | 11:e3cbdbe3d77b | 63 | MEMmywrite(0x06); //Send Write enable command |
| ThomasSonderDesign | 0:c2e41d203cdb | 64 | _cs_mem= 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 65 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 66 | _cs_mem=0; |
| Xinda | 11:e3cbdbe3d77b | 67 | MEMmywrite(0x20); //Send sector erase comand |
| Xinda | 11:e3cbdbe3d77b | 68 | MEMmywrite(Address>>16); //Send high address byte |
| Xinda | 11:e3cbdbe3d77b | 69 | MEMmywrite(Address>>8); //Send mid address byte |
| Xinda | 11:e3cbdbe3d77b | 70 | MEMmywrite(Address); //Send low address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 71 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 72 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 73 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 74 | //Pol the status register untill the Write In Progress bit is no longer set |
| ThomasSonderDesign | 0:c2e41d203cdb | 75 | _cs_mem=0; |
| Xinda | 11:e3cbdbe3d77b | 76 | MEMmywrite(05); |
| Xinda | 11:e3cbdbe3d77b | 77 | MEMmywrite(dummy); |
| Xinda | 11:e3cbdbe3d77b | 78 | while (LPC_SSP0->SR & RNE) // While RNE-Bit = 1 (FIFO receive buffer not empty)... |
| Xinda | 11:e3cbdbe3d77b | 79 | byte1 = LPC_SSP0->DR; |
| ThomasSonderDesign | 0:c2e41d203cdb | 80 | while(byte1>0) { |
| Xinda | 11:e3cbdbe3d77b | 81 | MEMmywrite(dummy); |
| Xinda | 11:e3cbdbe3d77b | 82 | while (LPC_SSP0->SR & RNE) // While RNE-Bit = 1 (FIFO receive buffer not empty)... |
| Xinda | 11:e3cbdbe3d77b | 83 | byte1 = LPC_SSP0->DR; |
| ThomasSonderDesign | 0:c2e41d203cdb | 84 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 85 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 86 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 87 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 88 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 89 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 90 | * Block Erase, erases everything in a 4KB block that includes Address |
| ThomasSonderDesign | 0:c2e41d203cdb | 91 | */ |
| Xinda | 11:e3cbdbe3d77b | 92 | int Memory::blockErase(int Address) |
| ThomasSonderDesign | 0:c2e41d203cdb | 93 | { |
| Xinda | 11:e3cbdbe3d77b | 94 | int byte1 = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 95 | _cs_mem = 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 96 | _cs_mem=0; |
| Xinda | 11:e3cbdbe3d77b | 97 | MEMmywrite(0x06); //Send Write enable command |
| ThomasSonderDesign | 0:c2e41d203cdb | 98 | _cs_mem= 1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 99 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 100 | _cs_mem=0; |
| Xinda | 11:e3cbdbe3d77b | 101 | MEMmywrite(0xD8); //Send sector erase comand |
| Xinda | 11:e3cbdbe3d77b | 102 | MEMmywrite((Address>>16)&0xff); //Send high address byte |
| Xinda | 11:e3cbdbe3d77b | 103 | MEMmywrite((Address>>8)&0xff); //Send mid address byte |
| Xinda | 11:e3cbdbe3d77b | 104 | MEMmywrite((Address)&0xff); //Send low address byte |
| ThomasSonderDesign | 0:c2e41d203cdb | 105 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 106 | wait_us(5); |
| ThomasSonderDesign | 0:c2e41d203cdb | 107 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 108 | //Pol the status register untill the Write In Progress bit is no longer set |
| ThomasSonderDesign | 0:c2e41d203cdb | 109 | _cs_mem=0; |
| Xinda | 11:e3cbdbe3d77b | 110 | MEMmywrite(05); |
| Xinda | 11:e3cbdbe3d77b | 111 | MEMmywrite(dummy); |
| Xinda | 11:e3cbdbe3d77b | 112 | while (LPC_SSP0->SR & RNE) // While RNE-Bit = 1 (FIFO receive buffer not empty)... |
| Xinda | 11:e3cbdbe3d77b | 113 | byte1 = LPC_SSP0->DR; |
| ThomasSonderDesign | 0:c2e41d203cdb | 114 | while(byte1>0) { |
| Xinda | 11:e3cbdbe3d77b | 115 | MEMmywrite(dummy); |
| Xinda | 11:e3cbdbe3d77b | 116 | while (LPC_SSP0->SR & RNE) // While RNE-Bit = 1 (FIFO receive buffer not empty)... |
| Xinda | 11:e3cbdbe3d77b | 117 | byte1 = LPC_SSP0->DR; |
| ThomasSonderDesign | 0:c2e41d203cdb | 118 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 119 | _cs_mem=1; |
| ThomasSonderDesign | 2:5c70c0334ed0 | 120 | int returnVal = (Address/0x10000); |
| ThomasSonderDesign | 2:5c70c0334ed0 | 121 | returnVal*=0x10000; |
| ThomasSonderDesign | 2:5c70c0334ed0 | 122 | returnVal+=0x10000; |
| ThomasSonderDesign | 2:5c70c0334ed0 | 123 | return returnVal; |
| ThomasSonderDesign | 0:c2e41d203cdb | 124 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 125 | |
| ThomasSonderDesign | 0:c2e41d203cdb | 126 | /** |
| ThomasSonderDesign | 0:c2e41d203cdb | 127 | * Writes a char array containg 'length' elements to memory sarting at address. |
| ThomasSonderDesign | 0:c2e41d203cdb | 128 | *If length is greater than BufferSize (3840 bytes) the function will terminate |
| ThomasSonderDesign | 0:c2e41d203cdb | 129 | *and return the start address. |
| ThomasSonderDesign | 0:c2e41d203cdb | 130 | */ |
| Xinda | 11:e3cbdbe3d77b | 131 | int Memory::writeData(char buffer[], int address, int length) |
| ThomasSonderDesign | 0:c2e41d203cdb | 132 | { |
| Xinda | 11:e3cbdbe3d77b | 133 | int byte1 = 1; |
| ThomasSonderDesign | 9:2587f246bf48 | 134 | //printf("\n C0 "); |
| ThomasSonderDesign | 0:c2e41d203cdb | 135 | if(length>bufferSize) { |
| ThomasSonderDesign | 0:c2e41d203cdb | 136 | printf("\nLength %i exceeds Max Length\n",length); |
| ThomasSonderDesign | 0:c2e41d203cdb | 137 | return address; |
| ThomasSonderDesign | 0:c2e41d203cdb | 138 | } |
| ThomasSonderDesign | 9:2587f246bf48 | 139 | //printf("\n C1 "); |
| ThomasSonderDesign | 0:c2e41d203cdb | 140 | for(int i =0; i<length; i++) { |
| Xinda | 11:e3cbdbe3d77b | 141 | if(address%256==0) { //Handle start and end of pages |
| AdminSonderDesign | 3:339efdc5134f | 142 | _cs_mem=1; |
| AdminSonderDesign | 3:339efdc5134f | 143 | wait_us(10); |
| ThomasSonderDesign | 9:2587f246bf48 | 144 | //printf("\n C2 "); |
| ThomasSonderDesign | 9:2587f246bf48 | 145 | |
| AdminSonderDesign | 3:339efdc5134f | 146 | //wait for the WIP bit to go low |
| AdminSonderDesign | 3:339efdc5134f | 147 | _cs_mem=0; //Selet memory |
| Xinda | 11:e3cbdbe3d77b | 148 | MEMmywrite(0x05); //Send read status register command |
| Xinda | 11:e3cbdbe3d77b | 149 | MEMmywrite(dummy); |
| Xinda | 11:e3cbdbe3d77b | 150 | while (LPC_SSP0->SR & RNE) // While RNE-Bit = 1 (FIFO receive buffer not empty)... |
| Xinda | 11:e3cbdbe3d77b | 151 | byte1 = LPC_SSP0->DR; |
| ThomasSonderDesign | 9:2587f246bf48 | 152 | //printf("\n C3 "); |
| AdminSonderDesign | 3:339efdc5134f | 153 | while ((byte1&1)>0) { |
| AdminSonderDesign | 3:339efdc5134f | 154 | wait_us(10); |
| ThomasSonderDesign | 9:2587f246bf48 | 155 | printf("\n C4 "); |
| Xinda | 11:e3cbdbe3d77b | 156 | MEMmywrite(0x05); //Send read status register command |
| Xinda | 11:e3cbdbe3d77b | 157 | MEMmywrite(dummy); |
| Xinda | 11:e3cbdbe3d77b | 158 | while (LPC_SSP0->SR & RNE) // While RNE-Bit = 1 (FIFO receive buffer not empty)... |
| Xinda | 11:e3cbdbe3d77b | 159 | byte1 = LPC_SSP0->DR; |
| AdminSonderDesign | 3:339efdc5134f | 160 | } |
| AdminSonderDesign | 3:339efdc5134f | 161 | _cs_mem=1; |
| AdminSonderDesign | 3:339efdc5134f | 162 | |
| AdminSonderDesign | 3:339efdc5134f | 163 | _cs_mem=0; //Selet memory |
| Xinda | 11:e3cbdbe3d77b | 164 | MEMmywrite(06); //Set Write Enable flag in the status reg |
| AdminSonderDesign | 3:339efdc5134f | 165 | _cs_mem=1; |
| AdminSonderDesign | 3:339efdc5134f | 166 | wait_us(10); |
| ThomasSonderDesign | 9:2587f246bf48 | 167 | |
| ThomasSonderDesign | 9:2587f246bf48 | 168 | //printf("\n C5 "); |
| AdminSonderDesign | 3:339efdc5134f | 169 | _cs_mem=0; //Selet memory |
| Xinda | 11:e3cbdbe3d77b | 170 | MEMmywrite(02); //Send read comand |
| Xinda | 11:e3cbdbe3d77b | 171 | MEMmywrite(address>>16); //Send high address byte |
| Xinda | 11:e3cbdbe3d77b | 172 | MEMmywrite(address>>8); //Send middle adress byte |
| Xinda | 11:e3cbdbe3d77b | 173 | MEMmywrite(address); //Send low address |
| AdminSonderDesign | 3:339efdc5134f | 174 | |
| AdminSonderDesign | 3:339efdc5134f | 175 | } |
| ThomasSonderDesign | 9:2587f246bf48 | 176 | //printf("\n C6 "); |
| Xinda | 11:e3cbdbe3d77b | 177 | MEMmywrite(buffer[i]); //Write the calue of the buffer to memory |
| ThomasSonderDesign | 6:c11cb6709b8b | 178 | //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 | 179 | |
| AdminSonderDesign | 3:339efdc5134f | 180 | wait_us(5); |
| Xinda | 11:e3cbdbe3d77b | 181 | address=address++; //Increment address |
| AdminSonderDesign | 3:339efdc5134f | 182 | } |
| AdminSonderDesign | 3:339efdc5134f | 183 | _cs_mem=1; |
| ThomasSonderDesign | 0:c2e41d203cdb | 184 | return address; |
| ThomasSonderDesign | 0:c2e41d203cdb | 185 | } |
| ThomasSonderDesign | 0:c2e41d203cdb | 186 | |
| ThomasSonderDesign | 8:6af25b9563df | 187 |
