Modified version of the UKESF lab source which can be carried out with no knowledge of C

Fork of PsiSwarm-Headstart by UKESF Headstart Summer School

Committer:
YRL50
Date:
Fri Sep 14 16:00:48 2018 +0000
Revision:
5:f6be169e465b
Parent:
4:dc77a25f29de
Fixing compile warnings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 0:d6269d17c8cf 1 /** University of York Robotics Laboratory PsiSwarm Library: Eprom Functions Source File
jah128 0:d6269d17c8cf 2 *
jah128 0:d6269d17c8cf 3 * File: eprom.cpp
jah128 0:d6269d17c8cf 4 *
jah128 0:d6269d17c8cf 5 * (C) Dept. Electronics & Computer Science, University of York
jah128 0:d6269d17c8cf 6 * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
jah128 0:d6269d17c8cf 7 *
jah128 4:dc77a25f29de 8 * PsiSwarm Library Version: 0.5
jah128 0:d6269d17c8cf 9 *
jah128 4:dc77a25f29de 10 * April 2016
jah128 0:d6269d17c8cf 11 *
jah128 0:d6269d17c8cf 12 * Functions for accessing the 64Kb EPROM chip and reading the reserved firmware block
jah128 0:d6269d17c8cf 13 *
jah128 0:d6269d17c8cf 14 * Example:
jah128 0:d6269d17c8cf 15 * @code
jah128 0:d6269d17c8cf 16 * #include "psiswarm.h"
jah128 0:d6269d17c8cf 17 *
jah128 0:d6269d17c8cf 18 * int main() {
jah128 0:d6269d17c8cf 19 * init();
jah128 0:d6269d17c8cf 20 * write_eeprom_byte(0,0xDD); //Writes byte 0xDD in EPROM address 0
jah128 0:d6269d17c8cf 21 * char c = read_eeprom_byte(0); //c will hold 0xDD
jah128 0:d6269d17c8cf 22 * //Valid address range is from 0 to 65279
jah128 0:d6269d17c8cf 23 * }
jah128 0:d6269d17c8cf 24 * @endcode
jah128 0:d6269d17c8cf 25 */
jah128 0:d6269d17c8cf 26
jah128 0:d6269d17c8cf 27 #include "psiswarm.h"
jah128 0:d6269d17c8cf 28
jah128 0:d6269d17c8cf 29 /** Write a single byte to the EPROM
jah128 0:d6269d17c8cf 30 *
jah128 0:d6269d17c8cf 31 * @param address The address to store the data, range 0-65279
jah128 0:d6269d17c8cf 32 * @param data The character to store
jah128 0:d6269d17c8cf 33 */
jah128 0:d6269d17c8cf 34 void write_eeprom_byte ( int address, char data )
jah128 0:d6269d17c8cf 35 {
jah128 0:d6269d17c8cf 36 char write_array[3];
jah128 0:d6269d17c8cf 37 if(address > 65279) {
jah128 0:d6269d17c8cf 38 debug("WARNING: Attempt to write to invalid EPROM address: %X",address);
jah128 0:d6269d17c8cf 39 } else {
jah128 0:d6269d17c8cf 40 write_array[0] = address / 256;
jah128 0:d6269d17c8cf 41 write_array[1] = address % 256;
jah128 0:d6269d17c8cf 42 write_array[2] = data;
jah128 0:d6269d17c8cf 43 primary_i2c.write(EEPROM_ADDRESS, write_array, 3, false);
jah128 0:d6269d17c8cf 44 //Takes 5ms to write a page: ideally this could be done with a timer or RTOS
jah128 0:d6269d17c8cf 45 wait(0.005);
jah128 0:d6269d17c8cf 46 }
jah128 0:d6269d17c8cf 47 }
jah128 0:d6269d17c8cf 48
jah128 0:d6269d17c8cf 49 /** Read a single byte from the EPROM
jah128 0:d6269d17c8cf 50 *
jah128 0:d6269d17c8cf 51 * @param address The address to read from, range 0-65279
jah128 0:d6269d17c8cf 52 * @return The character stored at address
jah128 0:d6269d17c8cf 53 */
jah128 0:d6269d17c8cf 54 char read_eeprom_byte ( int address )
jah128 0:d6269d17c8cf 55 {
jah128 0:d6269d17c8cf 56 char address_array [2];
jah128 0:d6269d17c8cf 57 address_array[0] = address / 256;
jah128 0:d6269d17c8cf 58 address_array[1] = address % 256;
jah128 0:d6269d17c8cf 59 char data [1];
jah128 0:d6269d17c8cf 60 primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false);
jah128 0:d6269d17c8cf 61 primary_i2c.read(EEPROM_ADDRESS, data, 1, false);
jah128 0:d6269d17c8cf 62 return data [0];
jah128 0:d6269d17c8cf 63 }
jah128 0:d6269d17c8cf 64
jah128 0:d6269d17c8cf 65 /** Read the next byte from the EPROM, to be called after read_eeprom_byte
jah128 0:d6269d17c8cf 66 *
jah128 0:d6269d17c8cf 67 * @return The character stored at address after the previous one read from
jah128 0:d6269d17c8cf 68 */
jah128 0:d6269d17c8cf 69 char read_next_eeprom_byte ()
jah128 0:d6269d17c8cf 70 {
jah128 0:d6269d17c8cf 71 char data [1];
jah128 0:d6269d17c8cf 72 primary_i2c.read(EEPROM_ADDRESS, data, 1, false);
jah128 0:d6269d17c8cf 73 return data [0];
jah128 0:d6269d17c8cf 74 }
jah128 0:d6269d17c8cf 75
jah128 0:d6269d17c8cf 76 /** Read the data stored in the reserved firmware area of the EPROM
jah128 0:d6269d17c8cf 77 *
jah128 0:d6269d17c8cf 78 * @return 1 if a valid firmware is read, 0 otherwise
jah128 0:d6269d17c8cf 79 */
jah128 0:d6269d17c8cf 80 char read_firmware ()
jah128 0:d6269d17c8cf 81 {
jah128 0:d6269d17c8cf 82 char address_array [2] = {255,0};
jah128 0:d6269d17c8cf 83 primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false);
jah128 0:d6269d17c8cf 84 primary_i2c.read(EEPROM_ADDRESS, firmware_bytes, 21, false);
jah128 0:d6269d17c8cf 85
jah128 0:d6269d17c8cf 86 if(firmware_bytes[0] == PSI_BYTE) {
jah128 0:d6269d17c8cf 87 // Parse firmware
jah128 0:d6269d17c8cf 88 char firmware_string [8];
jah128 0:d6269d17c8cf 89 sprintf(firmware_string,"%d.%d",firmware_bytes[9],firmware_bytes[10]);
jah128 0:d6269d17c8cf 90 firmware_version = atof(firmware_string);
jah128 1:060690a934a9 91 char pcb_version_string [8];
jah128 1:060690a934a9 92 sprintf(pcb_version_string,"%d.%d",firmware_bytes[7],firmware_bytes[8]);
jah128 1:060690a934a9 93 pcb_version = atof(pcb_version_string);
jah128 1:060690a934a9 94 char serial_number_string [8];
jah128 1:060690a934a9 95 sprintf(serial_number_string,"%d.%d",firmware_bytes[5],firmware_bytes[6]);
jah128 1:060690a934a9 96 serial_number = atof(serial_number_string);
jah128 1:060690a934a9 97 has_compass = firmware_bytes[11];
jah128 1:060690a934a9 98 has_side_ir = firmware_bytes[12];
jah128 1:060690a934a9 99 has_base_ir = firmware_bytes[13];
jah128 1:060690a934a9 100 has_base_colour_sensor= firmware_bytes[14];
jah128 1:060690a934a9 101 has_top_colour_sensor= firmware_bytes[15];
jah128 1:060690a934a9 102 has_wheel_encoders= firmware_bytes[16];
jah128 1:060690a934a9 103 has_audio_pic= firmware_bytes[17];
jah128 1:060690a934a9 104 has_ultrasonic_sensor= firmware_bytes[18];
jah128 1:060690a934a9 105 has_temperature_sensor= firmware_bytes[19];
jah128 1:060690a934a9 106 has_recharging_circuit= firmware_bytes[20];
jah128 1:060690a934a9 107 has_433_radio= firmware_bytes[21];
jah128 4:dc77a25f29de 108 if(firmware_version > 1.0){
jah128 4:dc77a25f29de 109 motor_calibration_set = firmware_bytes[22];
jah128 4:dc77a25f29de 110 if(motor_calibration_set == 1){
jah128 4:dc77a25f29de 111 left_motor_calibration_value = (float) firmware_bytes[23] * 65536;
jah128 4:dc77a25f29de 112 left_motor_calibration_value += ((float) firmware_bytes[24] * 256);
jah128 4:dc77a25f29de 113 left_motor_calibration_value += firmware_bytes[25];
jah128 4:dc77a25f29de 114 left_motor_calibration_value /= 16777216;
jah128 4:dc77a25f29de 115 right_motor_calibration_value = (float) firmware_bytes[26] * 65536;
jah128 4:dc77a25f29de 116 right_motor_calibration_value += ((float) firmware_bytes[27] * 256);
jah128 4:dc77a25f29de 117 right_motor_calibration_value += firmware_bytes[28];
jah128 4:dc77a25f29de 118 right_motor_calibration_value /= 16777216;
jah128 4:dc77a25f29de 119 }
jah128 4:dc77a25f29de 120 } else motor_calibration_set = 0;
jah128 0:d6269d17c8cf 121 return 1;
jah128 0:d6269d17c8cf 122 }
jah128 0:d6269d17c8cf 123 return 0;
jah128 0:d6269d17c8cf 124 }