|
| 1 | +/* |
| 2 | + * compile with: |
| 3 | + * ARCH=arm arm-arago-linux-gnueabi-gcc eeprom-decode.c -o eeprom-decode |
| 4 | + */ |
| 5 | + |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdint.h> |
| 9 | +#include <string.h> |
| 10 | +#include <time.h> |
| 11 | +#include <fcntl.h> |
| 12 | +#include <unistd.h> |
| 13 | + |
| 14 | +#include "beaglebone.h" |
| 15 | +#include "eeprom.h" |
| 16 | + |
| 17 | + |
| 18 | +#define EEPROM_MAGIC 0xAA5533EE |
| 19 | + |
| 20 | +#define BIG_ENDIAN_8( x) ( (x) & 0xFF) |
| 21 | +#define BIG_ENDIAN_16( x) (( (BIG_ENDIAN_8( x) << 8) | BIG_ENDIAN_8( x >> 8) ) & 0xFFFF) |
| 22 | +#define BIG_ENDIAN_32( x) (( (BIG_ENDIAN_16( x) << 16) | BIG_ENDIAN_16( x >> 16) ) & 0xFFFFFFFF) |
| 23 | + |
| 24 | + |
| 25 | +union bone_cape_io_config { |
| 26 | + struct { |
| 27 | + uint32_t header; |
| 28 | + char header_version[ 2]; |
| 29 | + char cape_name[ 32]; |
| 30 | + char cape_revision[ 4]; |
| 31 | + char manufacturer[ 16]; |
| 32 | + char part_number[ 16]; |
| 33 | + uint16_t pins_used; |
| 34 | + char serial_number[ 12]; /* WWYY4P13nnnn */ |
| 35 | + uint16_t pin_config[ 74]; |
| 36 | + uint16_t vdd_3v3_current; |
| 37 | + uint16_t vdd_5v0_current; |
| 38 | + uint16_t sys_5v0_current; |
| 39 | + uint16_t dc_supplied; |
| 40 | + }; |
| 41 | + struct { |
| 42 | + uint8_t data[ 244]; |
| 43 | + }; |
| 44 | +}; |
| 45 | + |
| 46 | +struct eeprom { |
| 47 | + union bone_cape_io_config cape_io_config; |
| 48 | + uint8_t step_io_config; |
| 49 | + // put microcode here ? |
| 50 | +}; |
| 51 | + |
| 52 | +int get_step_io_config( const char* eeprom_path) |
| 53 | +{ |
| 54 | + uint8_t step_io_config; |
| 55 | + int fd = open( eeprom_path, O_RDONLY); |
| 56 | + int result; |
| 57 | + if (fd < 0) { |
| 58 | + perror( "Failed to open EEPROM for reading"); |
| 59 | + result = -1; |
| 60 | + goto done; |
| 61 | + } |
| 62 | + result = lseek( fd, sizeof( union bone_cape_io_config), SEEK_SET); |
| 63 | + if (result < 0) { |
| 64 | + perror( "Failed to lseek EEPROM"); |
| 65 | + result = -1; |
| 66 | + goto done; |
| 67 | + } |
| 68 | + int cnt = read( fd, &step_io_config, sizeof( step_io_config)); |
| 69 | + if (cnt < 0) { |
| 70 | + perror( "Failed to read EEPROM"); |
| 71 | + result = -1; |
| 72 | + goto done; |
| 73 | + } else if (cnt != sizeof( step_io_config)) { |
| 74 | + // short write ? |
| 75 | + result = -1; |
| 76 | + goto done; |
| 77 | + } |
| 78 | + result = step_io_config; |
| 79 | +done: |
| 80 | + close( fd); |
| 81 | + return result; |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +int set_step_io_config( const char* eeprom_path, uint8_t value) |
| 86 | +{ |
| 87 | + uint8_t step_io_config = value; |
| 88 | + int fd = open( eeprom_path, O_WRONLY); |
| 89 | + int result; |
| 90 | + if (fd < 0) { |
| 91 | + perror( "Failed to open EEPROM for writing"); |
| 92 | + result = -1; |
| 93 | + goto done; |
| 94 | + } |
| 95 | + result = lseek( fd, sizeof( union bone_cape_io_config), SEEK_SET); |
| 96 | + if (result < 0) { |
| 97 | + perror( "Failed to lseek EEPROM"); |
| 98 | + result = -1; |
| 99 | + goto done; |
| 100 | + } |
| 101 | + int cnt = write( fd, &step_io_config, sizeof( step_io_config)); |
| 102 | + if (cnt < 0) { |
| 103 | + perror( "Failed to read EEPROM"); |
| 104 | + result = -1; |
| 105 | + goto done; |
| 106 | + } else if (cnt != sizeof( step_io_config)) { |
| 107 | + // short write ? |
| 108 | + result = -1; |
| 109 | + goto done; |
| 110 | + } |
| 111 | + result = 0; |
| 112 | +done: |
| 113 | + close( fd); |
| 114 | + return result; |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +#ifdef STANDALONE |
| 119 | + |
| 120 | +int main( int argc, char* argv[]) |
| 121 | +{ |
| 122 | + int result = get_step_io_config( EEPROM_PATH); |
| 123 | + printf( "Current EEPROM step_io_config value is: 0x%02x (%d)\n", result, result); |
| 124 | + result = set_step_io_config( EEPROM_PATH, TB6560_DRIVERS); |
| 125 | + result = get_step_io_config( EEPROM_PATH); |
| 126 | + printf( "New EEPROM step_io_config value is: 0x%02x (%d)\n", result, result); |
| 127 | + return 0; |
| 128 | +} |
| 129 | + |
| 130 | +#endif |
0 commit comments