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: I2CEEPROM.cpp
- Revision:
- 0:5fa232ee5fdf
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2CEEPROM.cpp Tue Jun 04 20:16:33 2013 +0000 @@ -0,0 +1,191 @@ +/* + * SOURCE FILE : I2CEEPROM.cpp + * + * Definition of class I2CEEPROM. + * Rountines for communicating with a serial EEPROM over an I2C link. + * EEPROM in question is a Microchip 24AA512. + * Remember you need pullup resistors on the SCL, SDA and WP lines. + * + */ + +#include "I2CEEPROM.h" + +/***************/ +/* CONSTRUCTOR */ +/***************/ +I2CEEPROM::I2CEEPROM() : + isOpen( false ), + // wire( (TwoWire*)NULL ), + wp( 8 ) +{ +} + +/**************/ +/* DESTRUCTOR */ +/**************/ +I2CEEPROM::~I2CEEPROM() { +} + +/**************************************/ +/* PREPARE TO COMMUNICATE WITH EEPROM */ +/**************************************/ +// Pass pointer to TwoWire object to use for I2C communications +// which should have been initialised and opened before calling this. +// Pass 3 bit address of EEPROM (as set by A0, A1 and A2 pins) in ea. +// Pass pin number to use for write protect in wp. +#if 0 +void I2CEEPROM::Open( TwoWire *wire, UInt8 ea, UInt8 wp ) { + if( ! isOpen ) { + // Save pointer to TwoWire object. + this->wire = wire; + // Calculate slave address. + slaveAddress = (UInt8)( ( ea & 0x7 ) | 0x50 ); + // Save write protect pin number. + this->wp = wp; + // Configure write protect pin as open drain output. + pinMode( wp, OUTPUT_OPEN_DRAIN ); + // Write protect EEPROM. + digitalWrite( wp, HIGH ); + isOpen = true; + } +} +#endif + +/*********************************************/ +/* SHUT DOWN COMMUNICATIONS LINK WITH EEPROM */ +/*********************************************/ +void I2CEEPROM::Close( void ) { + isOpen = false; +} + +// Size of each chunk read or written. +#define CHUNK_SIZE ( WIRE_BUFSIZ - 2 ) + +/************************************/ +/* ATTEMPT TO READ A CHUNK 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. +// The number of bytes read must not exceed CHUNK_SIZE. +bool I2CEEPROM::ReadChunk( UInt16 address, UInt8 *buffer, UInt8 count ) { +#if 0 + // First send address you want to read from. + wire->beginTransmission( slaveAddress ); + // Add high byte of EEPROM address to read from. + wire->send( ( address >> 8 ) & 0xFF ); + // Add low byte of EEPROM address to read from. + wire->send( address & 0xFF ); + // Send the packet and return false on failure. + if( wire->endTransmission() != SUCCESS ) { + return false; + } + // Now read back data. + wire->requestFrom( slaveAddress, count ); + UInt16 readCount = 0; + while( ( wire->available() > 0 ) && ( readCount < count ) ) { + *buffer++ = wire->receive(); + readCount++; + } + return true; +#else + return false; +#endif +} + +/*************************************/ +/* 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 I2CEEPROM::ReadBytes( UInt16 address, UInt8 *buffer, UInt16 count ) { +#if 0 + bool ok = true; + // Keep reading chunks until all bytes read. + while( ( count > 0 ) && ok ) { + if( count > CHUNK_SIZE ) { + ok = ReadChunk( address, buffer, (UInt8)CHUNK_SIZE ); + address += CHUNK_SIZE; + buffer += CHUNK_SIZE; + count -= CHUNK_SIZE; + } + else { + ok = ReadChunk( address, buffer, (UInt8)count ); + count = 0; + } + } + // Return true on success. + return ok; +#else + return false; +#endif +} + +/**************************************/ +/* 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 I2CEEPROM::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 I2CEEPROM::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 +} +