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