Bluetooth communication for flocking.
Fork of BeautifulMemeProject by
PsiSwarm/eprom.cpp@27:7eb032772bc2, 2016-01-31 (annotated)
- Committer:
- alanmillard
- Date:
- Sun Jan 31 15:14:54 2016 +0000
- Revision:
- 27:7eb032772bc2
- Parent:
- 6:ff3c66f7372b
Flocking seems to work better now, however the robot controller sometimes "crashes" (unsure why).
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 | 6:ff3c66f7372b | 5 | * (C) Dept. Electronics & Computer Science, University of York |
jah128 | 6:ff3c66f7372b | 6 | * James Hilder, Alan Millard, Homero Elizondo, Jon Timmis |
jah128 | 1:f6356cf1cefc | 7 | * |
jah128 | 6:ff3c66f7372b | 8 | * PsiSwarm Library Version: 0.3, October 2015 |
jah128 | 0:8a5497a2e366 | 9 | * |
jah128 | 0:8a5497a2e366 | 10 | * Functions for accessing the 64Kb EPROM chip and reading the reserved firmware block |
jah128 | 0:8a5497a2e366 | 11 | * |
jah128 | 0:8a5497a2e366 | 12 | * Example: |
jah128 | 0:8a5497a2e366 | 13 | * @code |
jah128 | 0:8a5497a2e366 | 14 | * #include "psiswarm.h" |
jah128 | 1:f6356cf1cefc | 15 | * |
jah128 | 0:8a5497a2e366 | 16 | * int main() { |
jah128 | 0:8a5497a2e366 | 17 | * init(); |
jah128 | 0:8a5497a2e366 | 18 | * write_eeprom_byte(0,0xDD); //Writes byte 0xDD in EPROM address 0 |
jah128 | 0:8a5497a2e366 | 19 | * char c = read_eeprom_byte(0); //c will hold 0xDD |
jah128 | 0:8a5497a2e366 | 20 | * //Valid address range is from 0 to 65279 |
jah128 | 0:8a5497a2e366 | 21 | * } |
jah128 | 0:8a5497a2e366 | 22 | * @endcode |
jah128 | 0:8a5497a2e366 | 23 | */ |
jah128 | 1:f6356cf1cefc | 24 | |
jah128 | 1:f6356cf1cefc | 25 | #include "psiswarm.h" |
jah128 | 0:8a5497a2e366 | 26 | |
jah128 | 0:8a5497a2e366 | 27 | /** Write a single byte to the EPROM |
jah128 | 0:8a5497a2e366 | 28 | * |
jah128 | 0:8a5497a2e366 | 29 | * @param address The address to store the data, range 0-65279 |
jah128 | 0:8a5497a2e366 | 30 | * @param data The character to store |
jah128 | 0:8a5497a2e366 | 31 | */ |
jah128 | 1:f6356cf1cefc | 32 | void write_eeprom_byte ( int address, char data ) |
jah128 | 1:f6356cf1cefc | 33 | { |
jah128 | 0:8a5497a2e366 | 34 | char write_array[3]; |
jah128 | 1:f6356cf1cefc | 35 | if(address > 65279) { |
jah128 | 1:f6356cf1cefc | 36 | debug("WARNING: Attempt to write to invalid EPROM address: %X",address); |
jah128 | 1:f6356cf1cefc | 37 | } else { |
jah128 | 1:f6356cf1cefc | 38 | write_array[0] = address / 256; |
jah128 | 1:f6356cf1cefc | 39 | write_array[1] = address % 256; |
jah128 | 1:f6356cf1cefc | 40 | write_array[2] = data; |
jah128 | 1:f6356cf1cefc | 41 | primary_i2c.write(EEPROM_ADDRESS, write_array, 3, false); |
jah128 | 1:f6356cf1cefc | 42 | //Takes 5ms to write a page: ideally this could be done with a timer or RTOS |
jah128 | 1:f6356cf1cefc | 43 | wait(0.005); |
jah128 | 0:8a5497a2e366 | 44 | } |
jah128 | 0:8a5497a2e366 | 45 | } |
jah128 | 1:f6356cf1cefc | 46 | |
jah128 | 0:8a5497a2e366 | 47 | /** Read a single byte from the EPROM |
jah128 | 0:8a5497a2e366 | 48 | * |
jah128 | 0:8a5497a2e366 | 49 | * @param address The address to read from, range 0-65279 |
jah128 | 0:8a5497a2e366 | 50 | * @return The character stored at address |
jah128 | 1:f6356cf1cefc | 51 | */ |
jah128 | 1:f6356cf1cefc | 52 | char read_eeprom_byte ( int address ) |
jah128 | 1:f6356cf1cefc | 53 | { |
jah128 | 0:8a5497a2e366 | 54 | char address_array [2]; |
jah128 | 0:8a5497a2e366 | 55 | address_array[0] = address / 256; |
jah128 | 0:8a5497a2e366 | 56 | address_array[1] = address % 256; |
jah128 | 0:8a5497a2e366 | 57 | char data [1]; |
jah128 | 0:8a5497a2e366 | 58 | primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false); |
jah128 | 0:8a5497a2e366 | 59 | primary_i2c.read(EEPROM_ADDRESS, data, 1, false); |
jah128 | 0:8a5497a2e366 | 60 | return data [0]; |
jah128 | 0:8a5497a2e366 | 61 | } |
jah128 | 0:8a5497a2e366 | 62 | |
jah128 | 0:8a5497a2e366 | 63 | /** Read the next byte from the EPROM, to be called after read_eeprom_byte |
jah128 | 0:8a5497a2e366 | 64 | * |
jah128 | 0:8a5497a2e366 | 65 | * @return The character stored at address after the previous one read from |
jah128 | 0:8a5497a2e366 | 66 | */ |
jah128 | 1:f6356cf1cefc | 67 | char read_next_eeprom_byte () |
jah128 | 1:f6356cf1cefc | 68 | { |
jah128 | 0:8a5497a2e366 | 69 | char data [1]; |
jah128 | 0:8a5497a2e366 | 70 | primary_i2c.read(EEPROM_ADDRESS, data, 1, false); |
jah128 | 0:8a5497a2e366 | 71 | return data [0]; |
jah128 | 0:8a5497a2e366 | 72 | } |
jah128 | 0:8a5497a2e366 | 73 | |
jah128 | 0:8a5497a2e366 | 74 | /** Read the data stored in the reserved firmware area of the EPROM |
jah128 | 0:8a5497a2e366 | 75 | * |
jah128 | 0:8a5497a2e366 | 76 | * @return 1 if a valid firmware is read, 0 otherwise |
jah128 | 0:8a5497a2e366 | 77 | */ |
jah128 | 1:f6356cf1cefc | 78 | char read_firmware () |
jah128 | 1:f6356cf1cefc | 79 | { |
jah128 | 0:8a5497a2e366 | 80 | char address_array [2] = {255,0}; |
jah128 | 0:8a5497a2e366 | 81 | primary_i2c.write(EEPROM_ADDRESS, address_array, 2, false); |
jah128 | 2:a6214fd156ff | 82 | primary_i2c.read(EEPROM_ADDRESS, firmware_bytes, 21, false); |
jah128 | 1:f6356cf1cefc | 83 | |
jah128 | 1:f6356cf1cefc | 84 | if(firmware_bytes[0] == PSI_BYTE) { |
jah128 | 1:f6356cf1cefc | 85 | // Parse firmware |
jah128 | 1:f6356cf1cefc | 86 | char firmware_string [8]; |
jah128 | 1:f6356cf1cefc | 87 | sprintf(firmware_string,"%d.%d",firmware_bytes[9],firmware_bytes[10]); |
jah128 | 1:f6356cf1cefc | 88 | firmware_version = atof(firmware_string); |
jah128 | 1:f6356cf1cefc | 89 | return 1; |
jah128 | 1:f6356cf1cefc | 90 | } |
jah128 | 0:8a5497a2e366 | 91 | return 0; |
jah128 | 0:8a5497a2e366 | 92 | } |