UKESF Headstart Summer School / PsiSwarm-Headstart

Dependents:   UKESF_Lab

Fork of PsiSwarmLibrary by James Hilder

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, Alexander Horsfield, Homero Elizondo, Jon Timmis
00007  *
00008  * PsiSwarm Library Version: 0.5
00009  *
00010  * April 2016
00011  *
00012  * Functions for accessing the 64Kb EPROM chip and reading the reserved firmware block
00013  *
00014  * Example:
00015  * @code
00016  * #include "psiswarm.h"
00017  *
00018  * int main() {
00019  *     init();
00020  *     write_eeprom_byte(0,0xDD);    //Writes byte 0xDD in EPROM address 0
00021  *     char c = read_eeprom_byte(0); //c will hold 0xDD
00022  *     //Valid address range is from 0 to 65279
00023  * }
00024  * @endcode
00025  */
00026 
00027 #include "psiswarm.h"
00028 
00029 /** Write a single byte to the EPROM
00030  *
00031  * @param address The address to store the data, range 0-65279
00032  * @param data The character to store
00033  */
00034 void write_eeprom_byte ( int address, char data )
00035 {
00036     char write_array[3];
00037     if(address > 65279) {
00038         debug("WARNING: Attempt to write to invalid EPROM address: %X",address);
00039     } else {
00040         write_array[0] = address / 256;
00041         write_array[1] = address % 256;
00042         write_array[2] = data;
00043         primary_i2c.write(EEPROM_ADDRESS, write_array, 3, false);
00044         //Takes 5ms to write a page: ideally this could be done with a timer or RTOS
00045         wait(0.005);
00046     }
00047 }
00048 
00049 /** Read a single byte from the EPROM
00050  *
00051  * @param address The address to read from, range 0-65279
00052  * @return The character stored at address
00053  */
00054 char read_eeprom_byte ( int address )
00055 {
00056     char address_array [2];
00057     address_array[0] = address / 256;
00058     address_array[1] = address % 256;
00059     char data [1];
00060     primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false);
00061     primary_i2c.read(EEPROM_ADDRESS, data, 1, false);
00062     return data [0];
00063 }
00064 
00065 /** Read the next byte from the EPROM, to be called after read_eeprom_byte
00066  *
00067  * @return The character stored at address after the previous one read from
00068  */
00069 char read_next_eeprom_byte ()
00070 {
00071     char data [1];
00072     primary_i2c.read(EEPROM_ADDRESS, data, 1, false);
00073     return data [0];
00074 }
00075 
00076 /** Read the data stored in the reserved firmware area of the EPROM
00077  *
00078  * @return 1 if a valid firmware is read, 0 otherwise
00079  */
00080 char read_firmware ()
00081 {
00082     char address_array [2] = {255,0};
00083     primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false);
00084     primary_i2c.read(EEPROM_ADDRESS, firmware_bytes, 21, false);
00085 
00086     if(firmware_bytes[0] == PSI_BYTE) {
00087         // Parse firmware
00088         char firmware_string [8];
00089         sprintf(firmware_string,"%d.%d",firmware_bytes[9],firmware_bytes[10]);
00090         firmware_version = atof(firmware_string);
00091         char pcb_version_string [8];
00092         sprintf(pcb_version_string,"%d.%d",firmware_bytes[7],firmware_bytes[8]);
00093         pcb_version = atof(pcb_version_string);
00094         char serial_number_string [8];
00095         sprintf(serial_number_string,"%d.%d",firmware_bytes[5],firmware_bytes[6]);
00096         serial_number = atof(serial_number_string);
00097         has_compass = firmware_bytes[11];
00098         has_side_ir = firmware_bytes[12];
00099         has_base_ir = firmware_bytes[13];
00100         has_base_colour_sensor= firmware_bytes[14];
00101         has_top_colour_sensor= firmware_bytes[15];
00102         has_wheel_encoders= firmware_bytes[16];
00103         has_audio_pic= firmware_bytes[17];
00104         has_ultrasonic_sensor= firmware_bytes[18];
00105         has_temperature_sensor= firmware_bytes[19];
00106         has_recharging_circuit= firmware_bytes[20];
00107         has_433_radio= firmware_bytes[21];
00108         if(firmware_version > 1.0){
00109         motor_calibration_set = firmware_bytes[22];
00110         if(motor_calibration_set == 1){
00111             left_motor_calibration_value = (float) firmware_bytes[23] * 65536;
00112             left_motor_calibration_value += ((float) firmware_bytes[24] * 256);
00113             left_motor_calibration_value += firmware_bytes[25];
00114             left_motor_calibration_value /= 16777216;
00115             right_motor_calibration_value = (float) firmware_bytes[26] * 65536;
00116             right_motor_calibration_value += ((float) firmware_bytes[27] * 256);
00117             right_motor_calibration_value += firmware_bytes[28];
00118             right_motor_calibration_value /= 16777216;
00119         }
00120         } else motor_calibration_set = 0;
00121         return 1;
00122     }
00123     return 0;
00124 }