![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ft. button press reset
Fork of BeaconDemo_RobotCode by
PsiSwarm/eprom.cpp@2:a6214fd156ff, 2015-10-05 (annotated)
- Committer:
- jah128
- Date:
- Mon Oct 05 14:42:16 2015 +0000
- Revision:
- 2:a6214fd156ff
- Parent:
- 1:f6356cf1cefc
- Child:
- 6:ff3c66f7372b
Changed nothing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 0:8a5497a2e366 | 1 | /** University of York Robotics Laboratory PsiSwarm Library: Eprom Functions Source File |
jah128 | 1:f6356cf1cefc | 2 | * |
jah128 | 0:8a5497a2e366 | 3 | * File: eprom.cpp |
jah128 | 0:8a5497a2e366 | 4 | * |
jah128 | 0:8a5497a2e366 | 5 | * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York |
jah128 | 1:f6356cf1cefc | 6 | * |
jah128 | 0:8a5497a2e366 | 7 | * PsiSwarm Library Version: 0.2, October 2015 |
jah128 | 0:8a5497a2e366 | 8 | * |
jah128 | 0:8a5497a2e366 | 9 | * Functions for accessing the 64Kb EPROM chip and reading the reserved firmware block |
jah128 | 0:8a5497a2e366 | 10 | * |
jah128 | 0:8a5497a2e366 | 11 | * Example: |
jah128 | 0:8a5497a2e366 | 12 | * @code |
jah128 | 0:8a5497a2e366 | 13 | * #include "psiswarm.h" |
jah128 | 1:f6356cf1cefc | 14 | * |
jah128 | 0:8a5497a2e366 | 15 | * int main() { |
jah128 | 0:8a5497a2e366 | 16 | * init(); |
jah128 | 0:8a5497a2e366 | 17 | * write_eeprom_byte(0,0xDD); //Writes byte 0xDD in EPROM address 0 |
jah128 | 0:8a5497a2e366 | 18 | * char c = read_eeprom_byte(0); //c will hold 0xDD |
jah128 | 0:8a5497a2e366 | 19 | * //Valid address range is from 0 to 65279 |
jah128 | 0:8a5497a2e366 | 20 | * } |
jah128 | 0:8a5497a2e366 | 21 | * @endcode |
jah128 | 0:8a5497a2e366 | 22 | */ |
jah128 | 1:f6356cf1cefc | 23 | |
jah128 | 1:f6356cf1cefc | 24 | #include "psiswarm.h" |
jah128 | 0:8a5497a2e366 | 25 | |
jah128 | 0:8a5497a2e366 | 26 | /** Write a single byte to the EPROM |
jah128 | 0:8a5497a2e366 | 27 | * |
jah128 | 0:8a5497a2e366 | 28 | * @param address The address to store the data, range 0-65279 |
jah128 | 0:8a5497a2e366 | 29 | * @param data The character to store |
jah128 | 0:8a5497a2e366 | 30 | */ |
jah128 | 1:f6356cf1cefc | 31 | void write_eeprom_byte ( int address, char data ) |
jah128 | 1:f6356cf1cefc | 32 | { |
jah128 | 0:8a5497a2e366 | 33 | char write_array[3]; |
jah128 | 1:f6356cf1cefc | 34 | if(address > 65279) { |
jah128 | 1:f6356cf1cefc | 35 | debug("WARNING: Attempt to write to invalid EPROM address: %X",address); |
jah128 | 1:f6356cf1cefc | 36 | } else { |
jah128 | 1:f6356cf1cefc | 37 | write_array[0] = address / 256; |
jah128 | 1:f6356cf1cefc | 38 | write_array[1] = address % 256; |
jah128 | 1:f6356cf1cefc | 39 | write_array[2] = data; |
jah128 | 1:f6356cf1cefc | 40 | primary_i2c.write(EEPROM_ADDRESS, write_array, 3, false); |
jah128 | 1:f6356cf1cefc | 41 | //Takes 5ms to write a page: ideally this could be done with a timer or RTOS |
jah128 | 1:f6356cf1cefc | 42 | wait(0.005); |
jah128 | 0:8a5497a2e366 | 43 | } |
jah128 | 0:8a5497a2e366 | 44 | } |
jah128 | 1:f6356cf1cefc | 45 | |
jah128 | 0:8a5497a2e366 | 46 | /** Read a single byte from the EPROM |
jah128 | 0:8a5497a2e366 | 47 | * |
jah128 | 0:8a5497a2e366 | 48 | * @param address The address to read from, range 0-65279 |
jah128 | 0:8a5497a2e366 | 49 | * @return The character stored at address |
jah128 | 1:f6356cf1cefc | 50 | */ |
jah128 | 1:f6356cf1cefc | 51 | char read_eeprom_byte ( int address ) |
jah128 | 1:f6356cf1cefc | 52 | { |
jah128 | 0:8a5497a2e366 | 53 | char address_array [2]; |
jah128 | 0:8a5497a2e366 | 54 | address_array[0] = address / 256; |
jah128 | 0:8a5497a2e366 | 55 | address_array[1] = address % 256; |
jah128 | 0:8a5497a2e366 | 56 | char data [1]; |
jah128 | 0:8a5497a2e366 | 57 | primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false); |
jah128 | 0:8a5497a2e366 | 58 | primary_i2c.read(EEPROM_ADDRESS, data, 1, false); |
jah128 | 0:8a5497a2e366 | 59 | return data [0]; |
jah128 | 0:8a5497a2e366 | 60 | } |
jah128 | 0:8a5497a2e366 | 61 | |
jah128 | 0:8a5497a2e366 | 62 | /** Read the next byte from the EPROM, to be called after read_eeprom_byte |
jah128 | 0:8a5497a2e366 | 63 | * |
jah128 | 0:8a5497a2e366 | 64 | * @return The character stored at address after the previous one read from |
jah128 | 0:8a5497a2e366 | 65 | */ |
jah128 | 1:f6356cf1cefc | 66 | char read_next_eeprom_byte () |
jah128 | 1:f6356cf1cefc | 67 | { |
jah128 | 0:8a5497a2e366 | 68 | char data [1]; |
jah128 | 0:8a5497a2e366 | 69 | primary_i2c.read(EEPROM_ADDRESS, data, 1, false); |
jah128 | 0:8a5497a2e366 | 70 | return data [0]; |
jah128 | 0:8a5497a2e366 | 71 | } |
jah128 | 0:8a5497a2e366 | 72 | |
jah128 | 0:8a5497a2e366 | 73 | /** Read the data stored in the reserved firmware area of the EPROM |
jah128 | 0:8a5497a2e366 | 74 | * |
jah128 | 0:8a5497a2e366 | 75 | * @return 1 if a valid firmware is read, 0 otherwise |
jah128 | 0:8a5497a2e366 | 76 | */ |
jah128 | 1:f6356cf1cefc | 77 | char read_firmware () |
jah128 | 1:f6356cf1cefc | 78 | { |
jah128 | 0:8a5497a2e366 | 79 | char address_array [2] = {255,0}; |
jah128 | 0:8a5497a2e366 | 80 | primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false); |
jah128 | 2:a6214fd156ff | 81 | primary_i2c.read(EEPROM_ADDRESS, firmware_bytes, 21, false); |
jah128 | 1:f6356cf1cefc | 82 | |
jah128 | 1:f6356cf1cefc | 83 | if(firmware_bytes[0] == PSI_BYTE) { |
jah128 | 1:f6356cf1cefc | 84 | // Parse firmware |
jah128 | 1:f6356cf1cefc | 85 | char firmware_string [8]; |
jah128 | 1:f6356cf1cefc | 86 | sprintf(firmware_string,"%d.%d",firmware_bytes[9],firmware_bytes[10]); |
jah128 | 1:f6356cf1cefc | 87 | firmware_version = atof(firmware_string); |
jah128 | 1:f6356cf1cefc | 88 | return 1; |
jah128 | 1:f6356cf1cefc | 89 | } |
jah128 | 0:8a5497a2e366 | 90 | return 0; |
jah128 | 0:8a5497a2e366 | 91 | } |