#10604 - mash - Wed Sep 10, 2003 2:43 pm
I'm in big trouble. I would like to save my game data on a 4kbit (512 byte) EEPROM. Could you send me source examples how can I do that?
Thanks!
Mash
Thanks!
Mash
Code: |
/** Setup the waitstate for an EEPROM. This must be done before any transfer operations with the EEPROM. */ void initializeEEPROM () { REG_WAITCNT &= ~(7 << 8); /* Clear bits 8, 9, 10. */ REG_WAITCNT |= 3 << 8; /* Set bits 8 and 9. */ } /** Attempt to write an eight-byte packet to EEPROM. * @param size The EEPROM size. This must be 512 or 8192. * @param offset The EEPROM offset to store into. These are in pages of eight, so zero and one indicate separate pages. * @param data The data to store. * @return Returns zero on success or negative on failure. -1 means that the chip was initially not OK. -2 means that the chip timed out after the write. */ int writeToEEPROM (int size, int offset, u8 data [8]) { volatile u8 *eeprom = (u8 *) 0xD000000; /* EEPROM pointer. */ u8 packet [12]; /* Packet content to transfer. */ int start; /* Starting offset for the data. */ int c; /* Loop counter. */ assert (size == 512 || size == 8192); /* Initially check that the EEPROM is ready. */ if (!(*eeprom & 1)) return -1; /* Write the address into the packet. */ if (size == 512) { assert (offset >= 0 && offset < 64); packet [0] = 2 | (offset << 2); start = 1; } else { assert (offset >= 0 && offset < 1024); packet [0] = 2 | (offset << 2); packet [1] = offset >> 6; start = 2; } packet [start + 8] = 0; /* Store the end-of-transfer indicator. */ /* Copy the data over. */ for (c = 0; c < 8; c ++) packet [start + c] = data [c]; /* Make the DMA transfer. */ REG_DM3SAD = (u32) &packet [0]; /* Starting address. */ REG_DM3DAD = (u32) eeprom; /* Destination address. */ REG_DM3CNT_L = (start + 8 + 2) / 2; /* Number of 16-bit words to transfer, either 5 or 6. */ REG_DM3CNT_H = 1 << 15; /* Control bits and fire the DMA. */ REG_DISPCNT = REG_DISPCNT; /* Waste some clock cycles. */ while (REG_DM3CNT_H & (1 << 15)); /* Wait out the transfer. */ /* Now wait until the EEPROM is back to ready. * Each cycle here must take at least one clock cycle. */ for (c = 0; c < 150000; c ++) if (*eeprom & 1) return 1; return -2; /* Ass! */ } /** Read a packet from EEPROM. * * @param size The EEPROM size. This must be 512 or 8192. * @param offset The EEPROM offset to read from. These are in pages of eight, so zero and one indicate separate pages. * @param data The data pointer to read into. * @return Returns zero if the operation is successful, or -1 if the EEPROM initially reported that it was not ready. */ int readFromEEPROM (int size, int offset, u8 *data) { volatile u8 *eeprom = (u8 *) 0xD000000; /* EEPROM pointer. */ u8 packet [10]; /* Transfer packet to and from. */ int packetSize; /* Size of the send packet in 16-bit words. */ int c; assert (size == 512 || size == 8192); /* Check that the EEPROM is good to go. */ if (!(*eeprom & 1)) return -1; /* Write the address into the packet. */ if (size == 512) { assert (offset >= 0 && offset < 64); packet [0] = 3 | (offset << 2); packet [1] = 0; packetSize = 1; } else { assert (offset >= 0 && offset < 1024); packet [0] = 3 | (offset << 2); packet [1] = offset >> 6; packet [2] = 0; packetSize = 2; } /* Send the packet through DMA. */ REG_DM3SAD = (u32) &packet [0]; /* Starting address. */ REG_DM3DAD = (u32) eeprom; /* Destination address. */ REG_DM3CNT_L = packetSize; /* Number of 16-bit words to transfer. */ REG_DM3CNT_H = 1 << 15; /* Control bits and fire the DMA. */ REG_DISPCNT = REG_DISPCNT; /* Waste some clock cycles. */ while (REG_DM3CNT_H & (1 << 15)); /* Wait out the transfer. */ /* Get the return packet. */ REG_DM3SAD = (u32) eeprom; /* Starting address. */ REG_DM3DAD = (u32) &packet [0]; /* Destination address. */ REG_DM3CNT_L = 5; /* Number of 16-bit words to transfer. */ REG_DM3CNT_H = 1 << 15; /* Control bits and fire the DMA. */ REG_DISPCNT = REG_DISPCNT; /* Waste some clock cycles. */ while (REG_DM3CNT_H & (1 << 15)); /* Wait out the transfer. */ /* Unpack the return packet. */ for (c = 0; c < 8; c ++) data [c] = (packet [c] >> 4) | (packet [c + 1] << 4); return 0; /* Woot. */ } |
Code: |
EEPROMInit (); u8 data[8]; if (EEPROMreadData (EEPROM_SIZE_64KB, 1, (u8*)&data) == -1) { DrawText (0, 0, "ERROR1_"); } if (data[0] == 10) { DrawText (0, 0, "Hello!!!_"); } data[0] = 10; if (EEPROMwriteData (EEPROM_SIZE_64KB, 1, data) == 1) { DrawText (0, 5, "ERROR2_"); } DrawText (0, 1, "GoodBye_"); while (1) { } |