Bluetooth communication for flocking.
Fork of BeautifulMemeProject by
PsiSwarm/eprom.cpp@0:8a5497a2e366, 2015-10-03 (annotated)
- Committer:
- jah128
- Date:
- Sat Oct 03 22:48:50 2015 +0000
- Revision:
- 0:8a5497a2e366
- Child:
- 1:f6356cf1cefc
Initial commit of PsiSwarm API and example code
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 | 0:8a5497a2e366 | 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 | 0:8a5497a2e366 | 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 | 0:8a5497a2e366 | 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 | 0:8a5497a2e366 | 23 | |
jah128 | 0:8a5497a2e366 | 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 | 0:8a5497a2e366 | 31 | void write_eeprom_byte ( int address, char data ){ |
jah128 | 0:8a5497a2e366 | 32 | char write_array[3]; |
jah128 | 0:8a5497a2e366 | 33 | if(address > 65279){ |
jah128 | 0:8a5497a2e366 | 34 | debug("WARNING: Attempt to write to invalid EPROM address: %X",address); |
jah128 | 0:8a5497a2e366 | 35 | }else{ |
jah128 | 0:8a5497a2e366 | 36 | write_array[0] = address / 256; |
jah128 | 0:8a5497a2e366 | 37 | write_array[1] = address % 256; |
jah128 | 0:8a5497a2e366 | 38 | write_array[2] = data; |
jah128 | 0:8a5497a2e366 | 39 | primary_i2c.write(EEPROM_ADDRESS, write_array, 3, false); |
jah128 | 0:8a5497a2e366 | 40 | //Takes 5ms to write a page: ideally this could be done with a timer or RTOS |
jah128 | 0:8a5497a2e366 | 41 | wait(0.005); |
jah128 | 0:8a5497a2e366 | 42 | } |
jah128 | 0:8a5497a2e366 | 43 | } |
jah128 | 0:8a5497a2e366 | 44 | |
jah128 | 0:8a5497a2e366 | 45 | /** Read a single byte from the EPROM |
jah128 | 0:8a5497a2e366 | 46 | * |
jah128 | 0:8a5497a2e366 | 47 | * @param address The address to read from, range 0-65279 |
jah128 | 0:8a5497a2e366 | 48 | * @return The character stored at address |
jah128 | 0:8a5497a2e366 | 49 | */ |
jah128 | 0:8a5497a2e366 | 50 | char read_eeprom_byte ( int address ){ |
jah128 | 0:8a5497a2e366 | 51 | char address_array [2]; |
jah128 | 0:8a5497a2e366 | 52 | address_array[0] = address / 256; |
jah128 | 0:8a5497a2e366 | 53 | address_array[1] = address % 256; |
jah128 | 0:8a5497a2e366 | 54 | char data [1]; |
jah128 | 0:8a5497a2e366 | 55 | primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false); |
jah128 | 0:8a5497a2e366 | 56 | primary_i2c.read(EEPROM_ADDRESS, data, 1, false); |
jah128 | 0:8a5497a2e366 | 57 | return data [0]; |
jah128 | 0:8a5497a2e366 | 58 | } |
jah128 | 0:8a5497a2e366 | 59 | |
jah128 | 0:8a5497a2e366 | 60 | /** Read the next byte from the EPROM, to be called after read_eeprom_byte |
jah128 | 0:8a5497a2e366 | 61 | * |
jah128 | 0:8a5497a2e366 | 62 | * @return The character stored at address after the previous one read from |
jah128 | 0:8a5497a2e366 | 63 | */ |
jah128 | 0:8a5497a2e366 | 64 | char read_next_eeprom_byte (){ |
jah128 | 0:8a5497a2e366 | 65 | char data [1]; |
jah128 | 0:8a5497a2e366 | 66 | primary_i2c.read(EEPROM_ADDRESS, data, 1, false); |
jah128 | 0:8a5497a2e366 | 67 | return data [0]; |
jah128 | 0:8a5497a2e366 | 68 | } |
jah128 | 0:8a5497a2e366 | 69 | |
jah128 | 0:8a5497a2e366 | 70 | /** Read the data stored in the reserved firmware area of the EPROM |
jah128 | 0:8a5497a2e366 | 71 | * |
jah128 | 0:8a5497a2e366 | 72 | * @return 1 if a valid firmware is read, 0 otherwise |
jah128 | 0:8a5497a2e366 | 73 | */ |
jah128 | 0:8a5497a2e366 | 74 | char read_firmware (){ |
jah128 | 0:8a5497a2e366 | 75 | char address_array [2] = {255,0}; |
jah128 | 0:8a5497a2e366 | 76 | primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false); |
jah128 | 0:8a5497a2e366 | 77 | primary_i2c.read(EEPROM_ADDRESS, firmware_bytes, 20, false); |
jah128 | 0:8a5497a2e366 | 78 | if(firmware_bytes[0] == PSI_BYTE) return 1; |
jah128 | 0:8a5497a2e366 | 79 | return 0; |
jah128 | 0:8a5497a2e366 | 80 | } |