Library for the PsiSwarm Robot - Version 0.7

Dependents:   PsiSwarm_V7_Blank

Fork of PsiSwarmLibrary by James Hilder

Committer:
jah128
Date:
Sat Oct 15 13:51:39 2016 +0000
Revision:
6:b340a527add9
Parent:
5:3cdd1a37cdd7
Added Apache license to files

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 6:b340a527add9 2 *
jah128 6:b340a527add9 3 * Copyright 2016 University of York
jah128 6:b340a527add9 4 *
jah128 6:b340a527add9 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
jah128 6:b340a527add9 6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
jah128 6:b340a527add9 7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS
jah128 6:b340a527add9 8 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jah128 6:b340a527add9 9 * See the License for the specific language governing permissions and limitations under the License.
jah128 0:d6269d17c8cf 10 *
jah128 0:d6269d17c8cf 11 * File: eprom.cpp
jah128 0:d6269d17c8cf 12 *
jah128 0:d6269d17c8cf 13 * (C) Dept. Electronics & Computer Science, University of York
jah128 0:d6269d17c8cf 14 * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
jah128 0:d6269d17c8cf 15 *
jah128 5:3cdd1a37cdd7 16 * PsiSwarm Library Version: 0.7
jah128 0:d6269d17c8cf 17 *
jah128 5:3cdd1a37cdd7 18 * October 2016
jah128 0:d6269d17c8cf 19 *
jah128 0:d6269d17c8cf 20 * Functions for accessing the 64Kb EPROM chip and reading the reserved firmware block
jah128 0:d6269d17c8cf 21 *
jah128 0:d6269d17c8cf 22 * Example:
jah128 0:d6269d17c8cf 23 * @code
jah128 0:d6269d17c8cf 24 * #include "psiswarm.h"
jah128 0:d6269d17c8cf 25 *
jah128 0:d6269d17c8cf 26 * int main() {
jah128 0:d6269d17c8cf 27 * init();
jah128 0:d6269d17c8cf 28 * write_eeprom_byte(0,0xDD); //Writes byte 0xDD in EPROM address 0
jah128 0:d6269d17c8cf 29 * char c = read_eeprom_byte(0); //c will hold 0xDD
jah128 0:d6269d17c8cf 30 * //Valid address range is from 0 to 65279
jah128 0:d6269d17c8cf 31 * }
jah128 0:d6269d17c8cf 32 * @endcode
jah128 0:d6269d17c8cf 33 */
jah128 0:d6269d17c8cf 34
jah128 0:d6269d17c8cf 35 #include "psiswarm.h"
jah128 0:d6269d17c8cf 36
jah128 0:d6269d17c8cf 37 /** Write a single byte to the EPROM
jah128 0:d6269d17c8cf 38 *
jah128 0:d6269d17c8cf 39 * @param address The address to store the data, range 0-65279
jah128 0:d6269d17c8cf 40 * @param data The character to store
jah128 0:d6269d17c8cf 41 */
jah128 0:d6269d17c8cf 42 void write_eeprom_byte ( int address, char data )
jah128 0:d6269d17c8cf 43 {
jah128 0:d6269d17c8cf 44 char write_array[3];
jah128 0:d6269d17c8cf 45 if(address > 65279) {
jah128 0:d6269d17c8cf 46 debug("WARNING: Attempt to write to invalid EPROM address: %X",address);
jah128 0:d6269d17c8cf 47 } else {
jah128 0:d6269d17c8cf 48 write_array[0] = address / 256;
jah128 0:d6269d17c8cf 49 write_array[1] = address % 256;
jah128 0:d6269d17c8cf 50 write_array[2] = data;
jah128 0:d6269d17c8cf 51 primary_i2c.write(EEPROM_ADDRESS, write_array, 3, false);
jah128 0:d6269d17c8cf 52 //Takes 5ms to write a page: ideally this could be done with a timer or RTOS
jah128 0:d6269d17c8cf 53 wait(0.005);
jah128 0:d6269d17c8cf 54 }
jah128 0:d6269d17c8cf 55 }
jah128 0:d6269d17c8cf 56
jah128 0:d6269d17c8cf 57 /** Read a single byte from the EPROM
jah128 0:d6269d17c8cf 58 *
jah128 0:d6269d17c8cf 59 * @param address The address to read from, range 0-65279
jah128 0:d6269d17c8cf 60 * @return The character stored at address
jah128 0:d6269d17c8cf 61 */
jah128 0:d6269d17c8cf 62 char read_eeprom_byte ( int address )
jah128 0:d6269d17c8cf 63 {
jah128 0:d6269d17c8cf 64 char address_array [2];
jah128 0:d6269d17c8cf 65 address_array[0] = address / 256;
jah128 0:d6269d17c8cf 66 address_array[1] = address % 256;
jah128 0:d6269d17c8cf 67 char data [1];
jah128 0:d6269d17c8cf 68 primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false);
jah128 0:d6269d17c8cf 69 primary_i2c.read(EEPROM_ADDRESS, data, 1, false);
jah128 0:d6269d17c8cf 70 return data [0];
jah128 0:d6269d17c8cf 71 }
jah128 0:d6269d17c8cf 72
jah128 0:d6269d17c8cf 73 /** Read the next byte from the EPROM, to be called after read_eeprom_byte
jah128 0:d6269d17c8cf 74 *
jah128 0:d6269d17c8cf 75 * @return The character stored at address after the previous one read from
jah128 0:d6269d17c8cf 76 */
jah128 0:d6269d17c8cf 77 char read_next_eeprom_byte ()
jah128 0:d6269d17c8cf 78 {
jah128 0:d6269d17c8cf 79 char data [1];
jah128 0:d6269d17c8cf 80 primary_i2c.read(EEPROM_ADDRESS, data, 1, false);
jah128 0:d6269d17c8cf 81 return data [0];
jah128 0:d6269d17c8cf 82 }
jah128 0:d6269d17c8cf 83
jah128 0:d6269d17c8cf 84 /** Read the data stored in the reserved firmware area of the EPROM
jah128 0:d6269d17c8cf 85 *
jah128 0:d6269d17c8cf 86 * @return 1 if a valid firmware is read, 0 otherwise
jah128 0:d6269d17c8cf 87 */
jah128 0:d6269d17c8cf 88 char read_firmware ()
jah128 0:d6269d17c8cf 89 {
jah128 0:d6269d17c8cf 90 char address_array [2] = {255,0};
jah128 0:d6269d17c8cf 91 primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false);
jah128 0:d6269d17c8cf 92 primary_i2c.read(EEPROM_ADDRESS, firmware_bytes, 21, false);
jah128 0:d6269d17c8cf 93
jah128 0:d6269d17c8cf 94 if(firmware_bytes[0] == PSI_BYTE) {
jah128 0:d6269d17c8cf 95 // Parse firmware
jah128 0:d6269d17c8cf 96 char firmware_string [8];
jah128 0:d6269d17c8cf 97 sprintf(firmware_string,"%d.%d",firmware_bytes[9],firmware_bytes[10]);
jah128 0:d6269d17c8cf 98 firmware_version = atof(firmware_string);
jah128 1:060690a934a9 99 char pcb_version_string [8];
jah128 1:060690a934a9 100 sprintf(pcb_version_string,"%d.%d",firmware_bytes[7],firmware_bytes[8]);
jah128 1:060690a934a9 101 pcb_version = atof(pcb_version_string);
jah128 1:060690a934a9 102 char serial_number_string [8];
jah128 1:060690a934a9 103 sprintf(serial_number_string,"%d.%d",firmware_bytes[5],firmware_bytes[6]);
jah128 1:060690a934a9 104 serial_number = atof(serial_number_string);
jah128 1:060690a934a9 105 has_compass = firmware_bytes[11];
jah128 1:060690a934a9 106 has_side_ir = firmware_bytes[12];
jah128 1:060690a934a9 107 has_base_ir = firmware_bytes[13];
jah128 1:060690a934a9 108 has_base_colour_sensor= firmware_bytes[14];
jah128 1:060690a934a9 109 has_top_colour_sensor= firmware_bytes[15];
jah128 1:060690a934a9 110 has_wheel_encoders= firmware_bytes[16];
jah128 1:060690a934a9 111 has_audio_pic= firmware_bytes[17];
jah128 1:060690a934a9 112 has_ultrasonic_sensor= firmware_bytes[18];
jah128 1:060690a934a9 113 has_temperature_sensor= firmware_bytes[19];
jah128 1:060690a934a9 114 has_recharging_circuit= firmware_bytes[20];
jah128 1:060690a934a9 115 has_433_radio= firmware_bytes[21];
jah128 4:1c621cb8cf0d 116 if(firmware_version > 1.0){
jah128 4:1c621cb8cf0d 117 motor_calibration_set = firmware_bytes[22];
jah128 4:1c621cb8cf0d 118 if(motor_calibration_set == 1){
jah128 4:1c621cb8cf0d 119 left_motor_calibration_value = (float) firmware_bytes[23] * 65536;
jah128 4:1c621cb8cf0d 120 left_motor_calibration_value += ((float) firmware_bytes[24] * 256);
jah128 4:1c621cb8cf0d 121 left_motor_calibration_value += firmware_bytes[25];
jah128 4:1c621cb8cf0d 122 left_motor_calibration_value /= 16777216;
jah128 4:1c621cb8cf0d 123 right_motor_calibration_value = (float) firmware_bytes[26] * 65536;
jah128 4:1c621cb8cf0d 124 right_motor_calibration_value += ((float) firmware_bytes[27] * 256);
jah128 4:1c621cb8cf0d 125 right_motor_calibration_value += firmware_bytes[28];
jah128 4:1c621cb8cf0d 126 right_motor_calibration_value /= 16777216;
jah128 4:1c621cb8cf0d 127 }
jah128 4:1c621cb8cf0d 128 } else motor_calibration_set = 0;
jah128 0:d6269d17c8cf 129 return 1;
jah128 0:d6269d17c8cf 130 }
jah128 0:d6269d17c8cf 131 return 0;
jah128 0:d6269d17c8cf 132 }