C++ Library for the PsiSwarm Robot - Version 0.8

Dependents:   PsiSwarm_V8_Blank_CPP Autonomia_RndmWlk

Fork of PsiSwarmV7_CPP by Psi Swarm Robot

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  * Copyright 2016 University of York
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
00007  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS
00008  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00009  * See the License for the specific language governing permissions and limitations under the License.
00010  *
00011  * File: eprom.cpp
00012  *
00013  * (C) Dept. Electronics & Computer Science, University of York
00014  * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
00015  *
00016  * PsiSwarm Library Version: 0.8
00017  *
00018  * October 2016
00019  *
00020  * Functions for accessing the 64Kb EPROM chip and reading the reserved firmware block
00021  *
00022  * Example:
00023  * @code
00024  * #include "psiswarm.h"
00025  *
00026  * int main() {
00027  *     init();
00028  *     eprom.write_eeprom_byte(0,0xDD);    //Writes byte 0xDD in EPROM address 0
00029  *     char c = eprom.read_eeprom_byte(0); //c will hold 0xDD
00030  *     //Valid address range is from 0 to 65279
00031  * }
00032  * @endcode
00033  */
00034 
00035 #include "psiswarm.h"
00036 
00037 /** Write a single byte to the EPROM
00038  *
00039  * @param address The address to store the data, range 0-65279
00040  * @param data The character to store
00041  */
00042 void Eprom::write_eeprom_byte ( int address, char data )
00043 {
00044     char write_array[3];
00045     if(address > 65279) {
00046         psi.debug("WARNING: Attempt to write to invalid EPROM address: %X",address);
00047     } else {
00048         write_array[0] = address / 256;
00049         write_array[1] = address % 256;
00050         write_array[2] = data;
00051         primary_i2c.write(EEPROM_ADDRESS, write_array, 3, false);
00052         //Takes 5ms to write a page: ideally this could be done with a timer or RTOS
00053         wait(0.005);
00054     }
00055 }
00056 
00057 /** Read a single byte from the EPROM
00058  *
00059  * @param address The address to read from, range 0-65279
00060  * @return The character stored at address
00061  */
00062 char Eprom::read_eeprom_byte ( int address )
00063 {
00064     char address_array [2];
00065     address_array[0] = address / 256;
00066     address_array[1] = address % 256;
00067     char data [1];
00068     primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false);
00069     primary_i2c.read(EEPROM_ADDRESS, data, 1, false);
00070     return data [0];
00071 }
00072 
00073 /** Read the next byte from the EPROM, to be called after read_eeprom_byte
00074  *
00075  * @return The character stored at address after the previous one read from
00076  */
00077 char Eprom::read_next_eeprom_byte ()
00078 {
00079     char data [1];
00080     primary_i2c.read(EEPROM_ADDRESS, data, 1, false);
00081     return data [0];
00082 }
00083 
00084 /** Read the data stored in the reserved firmware area of the EPROM
00085  *
00086  * @return 1 if a valid firmware is read, 0 otherwise
00087  */
00088 char Eprom::read_firmware ()
00089 {
00090     char address_array [2] = {255,0};
00091     primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false);
00092     primary_i2c.read(EEPROM_ADDRESS, firmware_bytes, 21, false);
00093 
00094     if(firmware_bytes[0] == PSI_BYTE) {
00095         // Parse firmware
00096         char firmware_string [8];
00097         sprintf(firmware_string,"%d.%d",firmware_bytes[9],firmware_bytes[10]);
00098         firmware_version = atof(firmware_string);
00099         char pcb_version_string [8];
00100         sprintf(pcb_version_string,"%d.%d",firmware_bytes[7],firmware_bytes[8]);
00101         pcb_version = atof(pcb_version_string);
00102         char serial_number_string [8];
00103         sprintf(serial_number_string,"%d.%d",firmware_bytes[5],firmware_bytes[6]);
00104         serial_number = atof(serial_number_string);
00105         has_compass = firmware_bytes[11];
00106         has_side_ir = firmware_bytes[12];
00107         has_base_ir = firmware_bytes[13];
00108         has_base_colour_sensor= firmware_bytes[14];
00109         has_top_colour_sensor= firmware_bytes[15];
00110         has_wheel_encoders= firmware_bytes[16];
00111         has_audio_pic= firmware_bytes[17];
00112         has_ultrasonic_sensor= firmware_bytes[18];
00113         has_temperature_sensor= firmware_bytes[19];
00114         has_recharging_circuit= firmware_bytes[20];
00115         has_433_radio= firmware_bytes[21];
00116         if(firmware_version > 1.0) {
00117             motor_calibration_set = firmware_bytes[22];
00118             if(motor_calibration_set == 1) {
00119                 left_motor_calibration_value = (float) firmware_bytes[23] * 65536;
00120                 left_motor_calibration_value += ((float) firmware_bytes[24] * 256);
00121                 left_motor_calibration_value += firmware_bytes[25];
00122                 left_motor_calibration_value /= 16777216;
00123                 right_motor_calibration_value = (float) firmware_bytes[26] * 65536;
00124                 right_motor_calibration_value += ((float) firmware_bytes[27] * 256);
00125                 right_motor_calibration_value += firmware_bytes[28];
00126                 right_motor_calibration_value /= 16777216;
00127             }
00128         } else motor_calibration_set = 0;
00129         return 1;
00130     }
00131     return 0;
00132 }