40 #define CONFIG_SLAVE_ENABLE() CONFIG_RD12_AS_DIG_OUTPUT()
41 #define SLAVE_ENABLE() _LATD12 = 0 //low true assertion
42 #define SLAVE_DISABLE() _LATD12 = 1
44 #define EEPROM_RDSR 0x05 //read status register command
45 #define EEPROM_READ 0x03 //read command
46 #define EEPROM_WRITE 0x02 //write command
47 #define EEPROM_WENABLE 0x06 //write command
51 void configSPI2(
void) {
53 SPI2CON1 = SEC_PRESCAL_4_1 |
59 SPI2STATbits.SPIEN = 1;
60 CONFIG_SLAVE_ENABLE();
65 void waitForWriteCompletion() {
66 uint8_t u8_spidata,u8_savedSWDTEN;
67 u8_savedSWDTEN = _SWDTEN;
71 u8_spidata = ioMasterSPI2(EEPROM_RDSR);
72 u8_spidata = ioMasterSPI2(0);
74 }
while (u8_spidata & 0x01);
75 _SWDTEN = u8_savedSWDTEN;
81 ioMasterSPI2(EEPROM_WENABLE);
89 u8_AddrLo = u16_MemAddr & 0x00FF;
90 u8_AddrHi = (u16_MemAddr >> 8);
92 waitForWriteCompletion();
95 ioMasterSPI2(EEPROM_WRITE);
96 ioMasterSPI2(u8_AddrHi);
97 ioMasterSPI2(u8_AddrLo);
98 for (u8_i=0; u8_i< BLKSIZE; u8_i++) {
99 ioMasterSPI2(pu8_buf[u8_i]);
108 waitForWriteCompletion();
109 u8_AddrLo = u16_MemAddr & 0x00FF;
110 u8_AddrHi = (u16_MemAddr >> 8);
112 ioMasterSPI2(EEPROM_READ);
113 ioMasterSPI2(u8_AddrHi);
114 ioMasterSPI2(u8_AddrLo);
115 for (u8_i=0; u8_i<BLKSIZE ; u8_i++) {
116 pu8_buf[u8_i] = ioMasterSPI2(0) ;
129 outString(
"\nEnter 'w' for write mode, anything else reads: ");
135 if (u8_Mode ==
'w') {
138 for (u8_i = 0; u8_i< BLKSIZE; u8_i++) {
143 memWrite25LC256(u16_MemAddr, au8_buf);
144 u16_MemAddr = u16_MemAddr + BLKSIZE;
145 memWrite25LC256(u16_MemAddr,au8_buf);
146 u16_MemAddr = u16_MemAddr + BLKSIZE;
148 memRead25LC256(u16_MemAddr,au8_buf);
149 for (u8_i = 0; u8_i< BLKSIZE; u8_i++)
outChar(au8_buf[u8_i]);
150 outString(
"\nAny key continues read...\n");
152 u16_MemAddr = u16_MemAddr + BLKSIZE;