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 #include "pic24_all.h"
00030
00037 #define EEPROM 0xA0 //LC515 address assuming both address pins tied low.
00038 #define BLKSIZE 64
00039
00040
00041 void waitForWriteCompletion(uint8 u8_i2cAddr) {
00042 uint8 u8_ack, u8_savedSWDTEN;
00043 u8_savedSWDTEN = _SWDTEN;
00044 _SWDTEN = 1;
00045 u8_i2cAddr = I2C_WADDR(u8_i2cAddr);
00046 do {
00047 startI2C1();
00048 u8_ack = putNoAckCheckI2C1(u8_i2cAddr);
00049 stopI2C1();
00050 } while (u8_ack == I2C_NAK);
00051 _SWDTEN = u8_savedSWDTEN;
00052 }
00053
00054
00055 void memWriteLC515(uint8 u8_i2cAddr, uint16 u16_MemAddr, uint8 *pu8_buf) {
00056 uint8 u8_AddrLo, u8_AddrHi;
00057 uint16 u16_i;
00058 u8_AddrLo = u16_MemAddr & 0x00FF;
00059 u8_AddrHi = (u16_MemAddr >> 8);
00060 if (u16_MemAddr & 0x8000) {
00061
00062 u8_i2cAddr = u8_i2cAddr | 0x08;
00063 }
00064 waitForWriteCompletion(u8_i2cAddr);
00065
00066 startI2C1();
00067 putI2C1(I2C_WADDR(u8_i2cAddr));
00068 putI2C1(u8_AddrHi);
00069 putI2C1(u8_AddrLo);
00070 for (u16_i=0; u16_i < BLKSIZE; u16_i++) {
00071 putI2C1(*pu8_buf);
00072 pu8_buf++;
00073 }
00074 stopI2C1();
00075 }
00076
00077 void memReadLC515(uint8 u8_i2cAddr, uint16 u16_MemAddr, uint8 *pu8_buf) {
00078
00079 uint8 u8_AddrLo, u8_AddrHi;
00080
00081 u8_AddrLo = u16_MemAddr & 0x00FF;
00082 u8_AddrHi = (u16_MemAddr >> 8);
00083
00084 if (u16_MemAddr & 0x8000) {
00085
00086 u8_i2cAddr = u8_i2cAddr | 0x08;
00087 }
00088 waitForWriteCompletion(u8_i2cAddr);
00089
00090 write2I2C1(u8_i2cAddr,u8_AddrHi, u8_AddrLo);
00091
00092 readNI2C1(u8_i2cAddr,pu8_buf, BLKSIZE);
00093 }
00094
00095 #define DMA_TRANSFER_SIZE BLKSIZE
00096
00097 uint8 au8_bufferA[DMA_TRANSFER_SIZE] __attribute__((space(dma)));
00098 uint8 au8_bufferB[DMA_TRANSFER_SIZE] __attribute__((space(dma)));
00099
00100 typedef struct tagFLAGBITS {
00101 unsigned u1_activeBuffer:
00102 1;
00103 unsigned u1_writeFlag:
00104 1;
00105 }FLAGBITS;
00106 volatile FLAGBITS flags;
00107
00108 void configDMA0() {
00109 DMA0PAD = (unsigned int) &U1RXREG;
00110 DMA0REQ = DMA_IRQ_U1RX;
00111 _U1RXIF = 0;
00112
00113 DMA0STA = __builtin_dmaoffset(au8_bufferA);
00114 DMA0STB = __builtin_dmaoffset(au8_bufferB);
00115
00116 DMA0CNT = DMA_TRANSFER_SIZE -1;
00117 DMA0CON =
00118 (DMA_MODULE_ON |
00119 DMA_SIZE_BYTE |
00120 DMA_DIR_READ_PERIPHERAL |
00121 DMA_INTERRUPT_FULL |
00122 DMA_NULLW_OFF |
00123 DMA_AMODE_REGISTER_POSTINC |
00124 DMA_MODE_CONTINUOUS_PING_PONG);
00125
00126 _U1EIF = 0;
00127 _U1EIP = 1;
00128 _U1EIE = 1;
00129
00130 _DMA0IF = 0;
00131 _DMA0IP = 2;
00132 _DMA0IE = 1;
00133 }
00134
00135
00136 void _ISRFAST _U1ErrInterrupt(void) {
00137 _U1EIF = 0;
00138 checkRxErrorUART1();
00139 }
00140
00141
00142 void _ISRFAST _DMA0Interrupt(void) {
00143 _DMA0IF = 0;
00144 flags.u1_activeBuffer = !flags.u1_activeBuffer;
00145 flags.u1_writeFlag = 1;
00146 }
00147
00148 int main (void) {
00149 uint8 au8_eepromBuf[BLKSIZE];
00150 uint16 u16_MemAddr;
00151 uint8 u8_mode, u8_pause;
00152 configBasic(HELLO_MSG);
00153 configI2C1(400);
00154 outString("\nEnter 'w' for write mode, anything else reads: ");
00155 u8_mode = inCharEcho();
00156 outString("\n");
00157 u16_MemAddr = 0;
00158 u8_pause = 0;
00159 while (1) {
00160 uint8 u8_i;
00161 if (u8_mode == 'w') {
00162 configDMA0();
00163 while (1) {
00164 if (flags.u1_writeFlag) reportError("DMA overflow!\n");
00165 while (!flags.u1_writeFlag) doHeartbeat();
00166 flags.u1_writeFlag = 0;
00167 if (flags.u1_activeBuffer)
00168 memWriteLC515(EEPROM,u16_MemAddr,au8_bufferA);
00169 else
00170 memWriteLC515(EEPROM,u16_MemAddr,au8_bufferB);
00171 u16_MemAddr = u16_MemAddr +64;
00172 outString("*");
00173 }
00174 } else {
00175 while (1) {
00176 if (u8_pause) {
00177 inChar();
00178 u8_pause = 0;
00179 }
00180 memReadLC515(EEPROM,u16_MemAddr,au8_eepromBuf);
00181 for (u8_i = 0;u8_i< 64;u8_i++) outChar(au8_eepromBuf[u8_i]);
00182 if (isCharReady()) {
00183 inChar();
00184 u8_pause = 1;
00185 }
00186 u16_MemAddr = u16_MemAddr + 64;
00187 }
00188 }
00189 }
00190 }