Science Memeseum / Mbed 2 deprecated BeaconDemo_RobotCode

Dependencies:   mbed

Fork of PsiSwarm-BeaconDemo_Bluetooth by James Wilson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers eprom.cpp Source File

eprom.cpp

00001 /** University of York Robotics Laboratory PsiSwarm Library: Eprom Functions Source File
00002  *
00003  * File: eprom.cpp
00004  *
00005  * (C) Dept. Electronics & Computer Science, University of York
00006  * James Hilder, Alan Millard, Homero Elizondo, Jon Timmis
00007  *
00008  * PsiSwarm Library Version: 0.3, October 2015
00009  *
00010  * Functions for accessing the 64Kb EPROM chip and reading the reserved firmware block
00011  *
00012  * Example:
00013  * @code
00014  * #include "psiswarm.h"
00015  *
00016  * int main() {
00017  *     init();
00018  *     write_eeprom_byte(0,0xDD);    //Writes byte 0xDD in EPROM address 0
00019  *     char c = read_eeprom_byte(0); //c will hold 0xDD
00020  *     //Valid address range is from 0 to 65279
00021  * }
00022  * @endcode
00023  */
00024 
00025 #include "psiswarm.h"
00026 
00027 /** Write a single byte to the EPROM
00028  *
00029  * @param address The address to store the data, range 0-65279
00030  * @param data The character to store
00031  */
00032 void write_eeprom_byte ( int address, char data )
00033 {
00034     char write_array[3];
00035     if(address > 65279) {
00036         debug("WARNING: Attempt to write to invalid EPROM address: %X",address);
00037     } else {
00038         write_array[0] = address / 256;
00039         write_array[1] = address % 256;
00040         write_array[2] = data;
00041         primary_i2c.write(EEPROM_ADDRESS, write_array, 3, false);
00042         //Takes 5ms to write a page: ideally this could be done with a timer or RTOS
00043         wait(0.005);
00044     }
00045 }
00046 
00047 /** Read a single byte from the EPROM
00048  *
00049  * @param address The address to read from, range 0-65279
00050  * @return The character stored at address
00051  */
00052 char read_eeprom_byte ( int address )
00053 {
00054     char address_array [2];
00055     address_array[0] = address / 256;
00056     address_array[1] = address % 256;
00057     char data [1];
00058     primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false);
00059     primary_i2c.read(EEPROM_ADDRESS, data, 1, false);
00060     return data [0];
00061 }
00062 
00063 /** Read the next byte from the EPROM, to be called after read_eeprom_byte
00064  *
00065  * @return The character stored at address after the previous one read from
00066  */
00067 char read_next_eeprom_byte ()
00068 {
00069     char data [1];
00070     primary_i2c.read(EEPROM_ADDRESS, data, 1, false);
00071     return data [0];
00072 }
00073 
00074 /** Read the data stored in the reserved firmware area of the EPROM
00075  *
00076  * @return 1 if a valid firmware is read, 0 otherwise
00077  */
00078 char read_firmware ()
00079 {
00080     char address_array [2] = {255,0};
00081     primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false);
00082     primary_i2c.read(EEPROM_ADDRESS, firmware_bytes, 21, false);
00083 
00084     if(firmware_bytes[0] == PSI_BYTE) {
00085         // Parse firmware
00086         char firmware_string [8];
00087         sprintf(firmware_string,"%d.%d",firmware_bytes[9],firmware_bytes[10]);
00088         firmware_version = atof(firmware_string);
00089         return 1;
00090     }
00091     return 0;
00092 }