Skip to content

Commit bd96db4

Browse files
committed
Changed stepper i/o configuration to depend on EEPROM contents instead of environment variable.
1 parent 7f4cb73 commit bd96db4

File tree

4 files changed

+150
-3
lines changed

4 files changed

+150
-3
lines changed

Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ SOURCES = \
6363
thermistor.c \
6464
traject.c \
6565
comm.c \
66+
eeprom.c \
6667
$(PROGRAM).c
6768

6869
CC = $(CROSS_COMPILE)gcc
@@ -141,7 +142,7 @@ install: all
141142
# DO NOT DELETE THIS LINE -- make depend depends on it.
142143
analog.o: analog.c analog.h beaglebone.h mendel.h debug.h
143144
bebopr_r2.o: bebopr_r2.c analog.h beaglebone.h temp.h thermistor.h \
144-
bebopr.h heater.h pwm.h traject.h
145+
bebopr.h heater.h pwm.h traject.h eeprom.h
145146
debug.o: debug.c debug.h
146147
gcode_parse.o: gcode_parse.c gcode_parse.h debug.h gcode_process.h \
147148
bebopr.h
@@ -163,6 +164,7 @@ thermistor.o: thermistor.c beaglebone.h thermistor.h
163164
traject.o: traject.c bebopr.h traject.h pruss_stepper.h algo2cmds.h \
164165
debug.h beaglebone.h mendel.h
165166
comm.o: comm.c comm.h mendel.h bebopr.h debug.h beaglebone.h
167+
eeprom.o: eeprom.c beaglebone.h eeprom.h
166168
mendel.o: mendel.c heater.h temp.h beaglebone.h pwm.h bebopr.h mendel.h \
167169
gcode_process.h gcode_parse.h limit_switches.h traject.h pruss_stepper.h \
168170
algo2cmds.h comm.h

bebopr_r2.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "heater.h"
1313
#include "pwm.h"
1414
#include "traject.h"
15+
#include "eeprom.h"
1516

1617
/*
1718
* Here one defines where the kernel puts the analog inputs,
@@ -146,11 +147,13 @@ int bebopr_pre_init( void)
146147
fprintf( stderr, "heater_config failed!\n");
147148
goto done;
148149
}
149-
char* s = getenv( "TB6560");
150-
if (s && strcmp( s, "yes") == 0) {
150+
result = get_step_io_config( EEPROM_PATH);
151+
// Only differentiate between Pololu and TB6560, default to Pololu
152+
if (result == TB6560_DRIVERS) {
151153
use_pololu_drivers = 0;
152154
}
153155
fprintf( stderr, "Using stepper driver configuration: '%s'\n", (use_pololu_drivers) ? "Pololu" : "TB6560");
156+
result = 0;
154157
done:
155158
return result;
156159
}

eeprom.c

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

eeprom.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _EEPROM_H
2+
#define _EEPROM_H
3+
4+
#define EEPROM_PATH "/sys/class/i2c-adapter/i2c-3/3-0054/eeprom"
5+
6+
#define POLOLU_DRIVERS 7
7+
#define TB6560_DRIVERS 8
8+
9+
extern int get_step_io_config( const char* eeprom_path);
10+
extern int set_step_io_config( const char* eeprom_path, uint8_t value);
11+
12+
#endif

0 commit comments

Comments
 (0)