Version of Robotron arcade game using LPC1768, a Gameduino shield, a serial EEPROM (for high scores), two microswitch joysticks and two buttons plus a box to put it in. 20 levels of mayhem.
Dependencies: 25LCxxx_SPI CommonTypes Gameduino mbed
Diff: SPIEEPROM.cpp
- Revision:
- 2:bb0f631a6068
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SPIEEPROM.cpp Thu Jun 06 20:11:28 2013 +0000 @@ -0,0 +1,122 @@ +/* + * SOURCE FILE : SPIEEPROM.cpp + * + * Definition of class SPIEEPROM. + * Rountines for communicating with a serial EEPROM over an SPI link. + * EEPROM in question is a Microchip 24AA512. + * Remember you need pullup resistors on the SCL, SDA and WP lines. + * + */ + +#include "SPIEEPROM.h" + +// Instruction set codes for use with EEPROM. +#define READ 0x03 // read data beginning at selected address +#define WRITE 0x02 // write data beginning at selected address +#define WRDI 0x04 // disable write operations +#define WREN 0x06 // enable write operations +#define RDSR 0x05 // read status register +#define WRSR 0x01 // write status register + +/***************/ +/* CONSTRUCTOR */ +/***************/ +// Pass pointer to an SPI object in spiLink. +// Pass pointer to output used as chip select in chipSelect. +SPIEEPROM::SPIEEPROM( SPI *spiLink, DigitalOut *chipSelect ) : + spi( spiLink ), + cs( chipSelect ) +{ +} + +/**************/ +/* DESTRUCTOR */ +/**************/ +SPIEEPROM::~SPIEEPROM() { +} + +/*************************************/ +/* ATTEMPT TO READ A NUMBER OF BYTES */ +/*************************************/ +// Pass address within EEPROM in address. +// Pass pointer to buffer for bytes read in buffer. +// Returns false if a failure occurred whilst reading EEPROM. +// Pass number of bytes to read in count. +// Returns true if successful, false if not. +bool SPIEEPROM::ReadBytes( UInt16 address, UInt8 *buffer, UInt16 count ) { + *cs = 0; // Select the EEPROM. + spi->write( READ ); // Send read instruction. + spi->write( ( address >> 8 ) & 0xFF ); // Send MSB of address. + spi->write( address & 0xFF ); // Send LSB of address. + // Read back the correct number of bytes by writing zero bytes. + // and storing what comes back in the buffer. + while( count > 0 ) { + *buffer++ = (UInt8)( spi->write( 0 ) ); + count--; + } + *cs = 1; // Deselect the EEPROM. + return true; // Always succeeds. +} + +/**************************************/ +/* ATTEMPT TO WRITE A CHUNK TO EEPROM */ +/**************************************/ +// Pass address within EEPROM in address. +// Pass pointer to buffer of bytes to write in buffer. +// Pass number of bytes to write in count. +// Returns true if successful, false if not. +// The number of bytes written must not exceed CHUNK_SIZE. +bool SPIEEPROM::WriteChunk( UInt16 address, const UInt8 *buffer, UInt8 count ) { +#if 0 + // First send address you want to write to. + wire->beginTransmission( slaveAddress ); + // Add high byte of EEPROM address to write to. + wire->send( ( address >> 8 ) & 0xFF ); + // Add low byte of EEPROM address to write to. + wire->send( address & 0xFF ); + // Add buffer bytes. + wire->send( (uint8*)buffer, count ); + // Send the packet. + // Return true if successful. + return ( wire->endTransmission() == SUCCESS ); +#else + return false; +#endif +} + +/**************************************/ +/* ATTEMPT TO WRITE A NUMBER OF BYTES */ +/**************************************/ +// Pass address within EEPROM in address. +// Pass pointer to buffer of bytes to write in buffer. +// Pass number of bytes to write in count. +// Returns true if successful, false if not. +bool SPIEEPROM::WriteBytes( UInt16 address, const UInt8 *buffer, UInt16 count ) { +#if 0 + bool ok = true; + // Write enable EEPROM and wait a bit. + digitalWrite( wp, LOW ); + delay( 1 ); + // Keep writing chunks until all bytes written. + while( ( count > 0 ) && ok ) { + if( count > CHUNK_SIZE ) { + ok = WriteChunk( address, buffer, (UInt8)CHUNK_SIZE ); + address += CHUNK_SIZE; + buffer += CHUNK_SIZE; + count -= CHUNK_SIZE; + } + else { + ok = WriteChunk( address, buffer, (UInt8)count ); + count = 0; + } + // Wait a lot to allow page to be written. + delay( 6 ); + } + // Write protect EEPROM. + digitalWrite( wp, HIGH ); + // Return true on success. + return ok; +#else + return false; +#endif +}