00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "pic24_all.h"
00031
00040 #define CONFIG_SLAVE_ENABLE() CONFIG_RD12_AS_DIG_OUTPUT()
00041 #define SLAVE_ENABLE() _LATD12 = 0 //low true assertion
00042 #define SLAVE_DISABLE() _LATD12 = 1
00043
00044 #define EEPROM_RDSR 0x05 //read status register command
00045 #define EEPROM_READ 0x03 //read command
00046 #define EEPROM_WRITE 0x02 //write command
00047 #define EEPROM_WENABLE 0x06 //write command
00048
00049 #define BLKSIZE 64
00050
00051 void configSPI2(void) {
00052
00053 SPI2CON1 = SEC_PRESCAL_4_1 |
00054 PRI_PRESCAL_4_1 |
00055 CLK_POL_ACTIVE_HIGH |
00056 SPI_CKE_ON |
00057 SPI_MODE8_ON |
00058 MASTER_ENABLE_ON;
00059 SPI2STATbits.SPIEN = 1;
00060 CONFIG_SLAVE_ENABLE();
00061 SLAVE_DISABLE();
00062 }
00063
00064
00065 void waitForWriteCompletion() {
00066 uint8 u8_spidata,u8_savedSWDTEN;
00067 u8_savedSWDTEN = _SWDTEN;
00068 _SWDTEN = 1;
00069 do {
00070 SLAVE_ENABLE();
00071 u8_spidata = ioMasterSPI2(EEPROM_RDSR);
00072 u8_spidata = ioMasterSPI2(0);
00073 SLAVE_DISABLE();
00074 } while (u8_spidata & 0x01);
00075 _SWDTEN = u8_savedSWDTEN;
00076 }
00077
00078
00079 void writeEnable() {
00080 SLAVE_ENABLE();
00081 ioMasterSPI2(EEPROM_WENABLE);
00082 SLAVE_DISABLE();
00083 }
00084
00085 void memWrite25LC256(uint16 u16_MemAddr, uint8 *pu8_buf) {
00086 uint8 u8_AddrLo, u8_AddrHi;
00087 uint8 u8_i;
00088
00089 u8_AddrLo = u16_MemAddr & 0x00FF;
00090 u8_AddrHi = (u16_MemAddr >> 8);
00091
00092 waitForWriteCompletion();
00093 writeEnable();
00094 SLAVE_ENABLE();
00095 ioMasterSPI2(EEPROM_WRITE);
00096 ioMasterSPI2(u8_AddrHi);
00097 ioMasterSPI2(u8_AddrLo);
00098 for (u8_i=0; u8_i< BLKSIZE; u8_i++) {
00099 ioMasterSPI2(pu8_buf[u8_i]);
00100 }
00101 SLAVE_DISABLE();
00102 }
00103
00104 void memRead25LC256(uint16 u16_MemAddr, uint8 *pu8_buf) {
00105 uint8 u8_AddrLo, u8_AddrHi;
00106 uint8 u8_i;
00107
00108 waitForWriteCompletion();
00109 u8_AddrLo = u16_MemAddr & 0x00FF;
00110 u8_AddrHi = (u16_MemAddr >> 8);
00111 SLAVE_ENABLE();
00112 ioMasterSPI2(EEPROM_READ);
00113 ioMasterSPI2(u8_AddrHi);
00114 ioMasterSPI2(u8_AddrLo);
00115 for (u8_i=0; u8_i<BLKSIZE ; u8_i++) {
00116 pu8_buf[u8_i] = ioMasterSPI2(0) ;
00117 }
00118 SLAVE_DISABLE();
00119 }
00120
00121
00122 int main (void) {
00123 uint8 au8_buf[BLKSIZE];
00124 uint16 u16_MemAddr;
00125 uint8 u8_Mode;
00126
00127 configBasic(HELLO_MSG);
00128 configSPI2();
00129 outString("\nEnter 'w' for write mode, anything else reads: ");
00130 u8_Mode = inCharEcho();
00131 outString("\n");
00132 u16_MemAddr = 0;
00133 while (1) {
00134 uint8 u8_i;
00135 if (u8_Mode == 'w') {
00136 outString("Enter 64 chars.\n");
00137
00138 for (u8_i = 0;u8_i< BLKSIZE;u8_i++) {
00139 au8_buf[u8_i] = inCharEcho();
00140 }
00141 outString("\nDoing Write\n");
00142
00143 memWrite25LC256(u16_MemAddr, au8_buf);
00144 u16_MemAddr = u16_MemAddr + BLKSIZE;
00145 memWrite25LC256(u16_MemAddr,au8_buf);
00146 u16_MemAddr = u16_MemAddr + BLKSIZE;
00147 } else {
00148 memRead25LC256(u16_MemAddr,au8_buf);
00149 for (u8_i = 0;u8_i< BLKSIZE;u8_i++) outChar(au8_buf[u8_i]);
00150 outString("\nAny key continues read...\n");
00151 inChar();
00152 u16_MemAddr = u16_MemAddr + BLKSIZE;
00153 }
00154 }
00155 }