Change the mem communication function. Now is xinda's fast read, not be tested may have some errors

Dependents:   BlackBoard_Firmware_Fast_read_not_test

Fork of Memory by Sonder Design Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Memory.cpp Source File

Memory.cpp

00001 #include "mbed.h"
00002 #include "Memory.h"
00003 #define TNF 0x02
00004 #define TFE 0x01
00005 #define RNE 0x04
00006 
00007 Memory::Memory(PinName chipSelect) : _cs_mem(chipSelect)
00008 {
00009     _cs_mem=1;
00010 }
00011 /**
00012  * Reads 'length' elements into a char array starting at the 24bit Address.
00013  *If length is greater than BufferSize (3840 bytes) the function will terminate
00014  *and return the start address.
00015  */
00016 /*** My method to mywrite to FeRAM ***/
00017 void MEMmywrite (unsigned char data) {
00018     // First don't mywrite to the FIFO buffer if it is full
00019     while (!(LPC_SSP0->SR & TNF))       // While TNF-Bit = 0 (FIFO full)...
00020         ;                               // Wait
00021     LPC_SSP0->DR = data;                // mywrite to FIFO buffer
00022 }
00023 
00024 int Memory::readData(short value [], int Address, int length)
00025 {
00026     //if(length>bufferSize) {
00027         //printf("\nLength %i exceeds Max Length\n",length);
00028         //return Address;
00029     //}
00030     int temp = 0;
00031     int temp1 = 0;
00032     int temp2 =0;
00033     _cs_mem = 1;             //Ensure cs is deselected
00034     wait_us(10);
00035     _cs_mem = 0;                     //memory is selected
00036     MEMmywrite(0x03);         //Send read command
00037     MEMmywrite(Address>>16);  //Send high address byte
00038     MEMmywrite(Address>>8);   //Send mid address byte
00039     MEMmywrite(Address);      //Send low address byte
00040 
00041 
00042     for(int i =0; i <length; i++) {
00043         MEMmywrite(dummy);//Send dummy byte to read out value ate Address
00044         while (LPC_SSP0->SR & RNE)
00045             temp = LPC_SSP0->DR;
00046         value[i]= (temp);
00047         //printf(" %X",value[i]);
00048         Address++;
00049     }
00050     _cs_mem = 1;
00051     return Address;     //Return the address of the next unread byte
00052 }
00053 
00054 
00055 /**
00056  * Sector  Erase, erases everything in the 4KB sector that includes Address
00057  */
00058 void Memory::sectorErase(long Address)
00059 {
00060     int byte1 = 1;
00061     _cs_mem = 1;
00062     _cs_mem=0;
00063     MEMmywrite(0x06);     //Send Write enable command
00064     _cs_mem= 1;
00065     wait_us(5);
00066     _cs_mem=0;
00067     MEMmywrite(0x20);     //Send sector erase comand
00068     MEMmywrite(Address>>16);  //Send high address byte
00069     MEMmywrite(Address>>8);   //Send mid address byte
00070     MEMmywrite(Address);      //Send low address byte
00071     _cs_mem=1;
00072     wait_us(5);
00073 
00074     //Pol the status register untill the Write In Progress bit is no longer set
00075     _cs_mem=0;
00076     MEMmywrite(05);
00077     MEMmywrite(dummy);
00078     while (LPC_SSP0->SR & RNE)          // While RNE-Bit = 1 (FIFO receive buffer not empty)...
00079         byte1 = LPC_SSP0->DR;
00080     while(byte1>0) {
00081         MEMmywrite(dummy);
00082         while (LPC_SSP0->SR & RNE)          // While RNE-Bit = 1 (FIFO receive buffer not empty)...
00083             byte1 = LPC_SSP0->DR;
00084     }
00085     _cs_mem=1;
00086 }
00087 
00088 
00089 /**
00090  * Block  Erase, erases everything in a 4KB block that includes Address
00091  */
00092 int Memory::blockErase(int Address)
00093 {
00094     int byte1 = 1;
00095     _cs_mem = 1;
00096     _cs_mem=0;
00097     MEMmywrite(0x06);     //Send Write enable command
00098     _cs_mem= 1;
00099     wait_us(5);
00100     _cs_mem=0;
00101     MEMmywrite(0xD8);     //Send sector erase comand
00102     MEMmywrite((Address>>16)&0xff);  //Send high address byte
00103     MEMmywrite((Address>>8)&0xff);   //Send mid address byte
00104     MEMmywrite((Address)&0xff);      //Send low address byte
00105     _cs_mem=1;
00106     wait_us(5);
00107 
00108     //Pol the status register untill the Write In Progress bit is no longer set
00109     _cs_mem=0;
00110     MEMmywrite(05);
00111     MEMmywrite(dummy);
00112     while (LPC_SSP0->SR & RNE)          // While RNE-Bit = 1 (FIFO receive buffer not empty)...
00113         byte1 = LPC_SSP0->DR;
00114     while(byte1>0) {
00115         MEMmywrite(dummy);
00116         while (LPC_SSP0->SR & RNE)          // While RNE-Bit = 1 (FIFO receive buffer not empty)...
00117             byte1 = LPC_SSP0->DR;
00118     }
00119     _cs_mem=1;
00120     int returnVal = (Address/0x10000);
00121     returnVal*=0x10000;
00122     returnVal+=0x10000;
00123     return returnVal;
00124 }
00125 
00126 /**
00127  * Writes a char array containg 'length' elements to memory sarting at address.
00128  *If length is greater than BufferSize (3840 bytes) the function will terminate
00129  *and return the start address.
00130  */
00131 int Memory::writeData(char buffer[], int address, int length)
00132 {
00133     int byte1 = 1;
00134     //printf("\n C0 ");
00135     if(length>bufferSize) {
00136         printf("\nLength %i exceeds Max Length\n",length);
00137         return address;
00138     }
00139     //printf("\n C1 ");
00140     for(int i =0; i<length; i++) {
00141         if(address%256==0) {                //Handle start and end of pages
00142             _cs_mem=1;
00143             wait_us(10);
00144             //printf("\n C2 ");
00145             
00146             //wait for the WIP bit to go low
00147             _cs_mem=0;                           //Selet memory
00148             MEMmywrite(0x05);             //Send read status register command
00149             MEMmywrite(dummy);
00150             while (LPC_SSP0->SR & RNE)          // While RNE-Bit = 1 (FIFO receive buffer not empty)...
00151                 byte1 = LPC_SSP0->DR;
00152             //printf("\n C3 ");
00153             while ((byte1&1)>0) {
00154                 wait_us(10);
00155                 printf("\n C4 ");
00156                 MEMmywrite(0x05);         //Send read status register command
00157                 MEMmywrite(dummy);
00158                 while (LPC_SSP0->SR & RNE)          // While RNE-Bit = 1 (FIFO receive buffer not empty)...
00159                     byte1 = LPC_SSP0->DR;
00160             }
00161             _cs_mem=1;
00162 
00163             _cs_mem=0;                           //Selet memory
00164             MEMmywrite(06);               //Set Write Enable flag in the status reg
00165             _cs_mem=1;
00166             wait_us(10);
00167             
00168             //printf("\n C5 ");
00169             _cs_mem=0;                           //Selet memory
00170             MEMmywrite(02);               //Send read comand
00171             MEMmywrite(address>>16);      //Send high address byte
00172             MEMmywrite(address>>8);       //Send middle adress byte
00173             MEMmywrite(address);          //Send low address
00174 
00175         }
00176         //printf("\n C6 ");
00177         MEMmywrite(buffer[i]);            //Write the calue of the buffer to memory
00178         //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);
00179             
00180         wait_us(5);
00181         address=address++;                  //Increment address
00182     }
00183     _cs_mem=1;
00184     return address;
00185 }
00186 
00187