Sonder Design Team / Memory
Committer:
ThomasSonderDesign
Date:
Tue Apr 17 03:20:30 2018 +0000
Revision:
10:96b5e7dcc91f
againg?;

Who changed what in which revision?

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