Skip to content

Commit

Permalink
Removed all compiletime dependancy on BBB define. Only bebopr_r2.c st…
Browse files Browse the repository at this point in the history
…ill needs fixing.
  • Loading branch information
modmaker committed Jun 13, 2013
1 parent 861b8a1 commit ea4aa71
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 109 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ eeprom-tool-install: eeprom-tool
.PHONY: all build elf hex eep lss sym program coff extcoff clean depend applet_files install

# DO NOT DELETE THIS LINE -- make depend depends on it.
analog.o: analog.c analog.h beaglebone.h mendel.h debug.h
analog.o: analog.c analog.h beaglebone.h mendel.h debug.h bebopr.h
bebopr_r2.o: bebopr_r2.c analog.h beaglebone.h temp.h thermistor.h \
bebopr.h heater.h pwm.h traject.h eeprom.h gpio.h
debug.o: debug.c debug.h
Expand All @@ -173,10 +173,10 @@ home.o: home.c beaglebone.h home.h bebopr.h limit_switches.h traject.h \
pruss_stepper.h algo2cmds.h gcode_process.h gcode_parse.h debug.h
limit_switches.o: limit_switches.c limit_switches.h traject.h bebopr.h \
mendel.h gpio.h debug.h beaglebone.h
pruss.o: pruss.c pruss.h algo2cmds.h beaglebone.h debug.h
pruss.o: pruss.c pruss.h algo2cmds.h beaglebone.h debug.h bebopr.h
pruss_stepper.o: pruss_stepper.c pruss_stepper.h algo2cmds.h pruss.h \
beaglebone.h debug.h bebopr.h timestamp.h eeprom.h
pwm.o: pwm.c pwm.h beaglebone.h debug.h
pwm.o: pwm.c pwm.h beaglebone.h debug.h bebopr.h
temp.o: temp.c temp.h beaglebone.h analog.h debug.h mendel.h
thermistor.o: thermistor.c beaglebone.h thermistor.h
traject.o: traject.c bebopr.h traject.h pruss_stepper.h algo2cmds.h \
Expand Down
19 changes: 12 additions & 7 deletions analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "beaglebone.h"
#include "mendel.h"
#include "debug.h"
#include "bebopr.h"


struct analog_channel_record {
Expand Down Expand Up @@ -116,16 +117,20 @@ void* analog_worker( void* arg)
}
lseek( fd[ i], 0, SEEK_SET);
} else if (ret < 0) {
#ifdef BBB
/* FIXME: for now ignore these recoverable errors! */
if (debug_flags & DEBUG_ANALOG) {
fprintf( stderr, "analog read failed: fd=%d, buf=%p, size=%u, ret=%d\n", fd[ i], buf, sizeof( buf), ret);
if (get_kernel_type() == e_kernel_3_8) {
/*
* The 3.8.13 kernel ADC driver returns an error once every 10-100 reads.
* This seems to be a glich / recoverable error.
* FIXME: for now we just ignore these errors (and the read).
*/
if (debug_flags & DEBUG_ANALOG) {
fprintf( stderr, "analog thread: ADC read failed: fd=%d, buf=%p, size=%u, ret=%d\n",
fd[ i], buf, sizeof( buf), ret);
}
continue;
}
continue;
#else
perror( "analog thread: ADC read failed -");
goto failure;
#endif
}
++i;
if (i == num_analog_channels) {
Expand Down
6 changes: 6 additions & 0 deletions bebopr.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,10 @@ extern int config_set_e_axis_mode( int relative);
#define power_on() do { /* void */ } while (0)
#define power_off() do { /* void */ } while (0)

// return the kernel type running
typedef enum {
e_kernel_3_2, e_kernel_3_8
} kernel_type;
extern kernel_type get_kernel_type( void);

#endif
13 changes: 13 additions & 0 deletions bebopr_r2.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,16 @@ void bebopr_exit( void)
gpio_write_value_to_pin_file( 34, "value", "1");
fprintf( stderr, "Turned BEBOPR I/O power off\n");
}

kernel_type get_kernel_type( void)
{
/*
* FIXME: determine at runtime !
* also remove the BBB #ifdefs in this file!
*/
#ifdef BBB
return e_kernel_3_8;
#else
return e_kernel_3_2;
#endif
}
75 changes: 40 additions & 35 deletions pruss.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "algo2cmds.h"
#include "beaglebone.h"
#include "debug.h"
#include "bebopr.h"

#define MAX_UIO_MAPS 5 // TODO: should be taken from the right UIO .h file!

Expand Down Expand Up @@ -232,11 +233,13 @@ static int locate_pruss_device_l2( const char* device_name, char* uio_name, int
int found = 0;
const char uio_dev_name[] = "uio";

#ifdef BBB
snprintf( buffer, sizeof( buffer), "/sys/devices/ocp.2/4a300000.pruss/%s", uio_dev_name);
#else
snprintf( buffer, sizeof( buffer), "/sys/bus/platform/devices/%s/%s", device_name, uio_dev_name);
#endif
if (get_kernel_type() == e_kernel_3_8) {
snprintf( buffer, sizeof( buffer), "/sys/devices/ocp.2/4a300000.pruss/%s", uio_dev_name);
}
if (get_kernel_type() == e_kernel_3_2) {
snprintf( buffer, sizeof( buffer), "/sys/bus/platform/devices/%s/%s", device_name, uio_dev_name);
}

dir = opendir( buffer);
if (dir <= 0) {
perror( "Platform device not found");
Expand Down Expand Up @@ -269,9 +272,6 @@ int locate_pruss_device( const char* driver_name, char* drv_name, int drv_name_l
{
char buffer[ NAME_MAX];
DIR* dir;
#ifndef BBB
struct dirent* de;
#endif
int found = 0;

// for each driver instance of type 'driver_name', scan all subdirs of
Expand All @@ -289,37 +289,42 @@ int locate_pruss_device( const char* driver_name, char* drv_name, int drv_name_l
exit( EXIT_FAILURE);
}
}
#ifdef BBB
if (locate_pruss_device_l2( driver_name, uio_name, uio_name_len)) {
if (drv_name) {

if (get_kernel_type() == e_kernel_3_8) {
if (locate_pruss_device_l2( driver_name, uio_name, uio_name_len)) {
if (drv_name) {
strncpy( drv_name, driver_name, drv_name_len);
}
found = 1;
}
#else
for (de = readdir( dir) ; de ; de = readdir( dir)) {
if (de < 0) {
perror( "Problem reading directory");
exit( EXIT_FAILURE);
}
if (debug && debug_level > 3) {
printf( "found driver device file '%s'\n", de->d_name);
}
found = 1;
}
// find entries that are instances of the driver
if (strncmp( driver_name, de->d_name, strlen( driver_name)) == 0) {
if (debug && debug_level > 1) {
printf( "found driver device instance '%s'\n", de->d_name);
}

if (get_kernel_type() == e_kernel_3_2) {
struct dirent* de;
for (de = readdir( dir) ; de ; de = readdir( dir)) {
if (de < 0) {
perror( "Problem reading directory");
exit( EXIT_FAILURE);
}
if (debug && debug_level > 3) {
printf( "found driver device file '%s'\n", de->d_name);
}
if (locate_pruss_device_l2( de->d_name, uio_name, uio_name_len)) {
if (drv_name) {
strncpy( drv_name, de->d_name, drv_name_len);
// find entries that are instances of the driver
if (strncmp( driver_name, de->d_name, strlen( driver_name)) == 0) {
if (debug && debug_level > 1) {
printf( "found driver device instance '%s'\n", de->d_name);
}
if (locate_pruss_device_l2( de->d_name, uio_name, uio_name_len)) {
if (drv_name) {
strncpy( drv_name, de->d_name, drv_name_len);
}
found = 1;
break;
}
found = 1;
break;
}
}
}
#endif

return found;
}

Expand Down Expand Up @@ -503,8 +508,8 @@ int pruss_init( const char* ucodename, unsigned int offset, struct ucode_signatu
}
int status = pruss_rd32( MM_PRUSS_SYSCFG);
if ((status & 0x10) != 0) {
pruss_wr32( MM_PRUSS_SYSCFG, status & ~(1 << 4));
printf( "PRUSS enabled OCP master ports.\n");
pruss_wr32( MM_PRUSS_SYSCFG, status & ~(1 << 4));
printf( "PRUSS enabled OCP master ports.\n");
}
} else {
fprintf( stderr, "PRUSS ID is not found.\n");
Expand Down Expand Up @@ -532,7 +537,7 @@ int pruss_init( const char* ucodename, unsigned int offset, struct ucode_signatu
// Reset PRUSS counters
if (debug_flags & DEBUG_PRUSS) {
printf( "Clearing PRUSS counters, old: cycle = %u, stall = %u\n",
pruss_rd32( PRUSS_PRU_CTRL_CYCLE), pruss_rd32( PRUSS_PRU_CTRL_STALL));
pruss_rd32( PRUSS_PRU_CTRL_CYCLE), pruss_rd32( PRUSS_PRU_CTRL_STALL));
}
pruss_wr32( PRUSS_PRU_CTRL_CYCLE, 0);
pruss_wr32( PRUSS_PRU_CTRL_STALL, 0);
Expand Down
146 changes: 82 additions & 64 deletions pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "pwm.h"
#include "debug.h"
#include "bebopr.h"


struct pwm_channel_record {
channel_tag id;
Expand Down Expand Up @@ -125,9 +127,9 @@ void pwm_exit( void)
struct pwm_channel_record* pd = &pwm_channels[ ch];
pwm_set_output( pd->id, 0);
pwm_write_int_to_file( pd->device_path, "run", 0);
#ifndef BBB
pwm_write_int_to_file( pd->device_path, "request", 0);
#endif
if (get_kernel_type() == e_kernel_3_2) {
pwm_write_int_to_file( pd->device_path, "request", 0);
}
close (pd->duty_fd);
pwm_channels[ ch].duty_fd = -1;
}
Expand All @@ -152,54 +154,66 @@ int pwm_init( void)

++num_pwm_channels;

#ifndef BBB
pwm_write_int_to_file( pd->device_path, "request", 1);
#endif
if (get_kernel_type() == e_kernel_3_2) {
pwm_write_int_to_file( pd->device_path, "request", 1);
}

pwm_write_int_to_file( pd->device_path, "polarity", 0);

#ifdef BBB
// FIXME: quick hack to work around differences between ehrpwm implementations
pwm_write_int_to_file( pd->device_path, "run", 0);
unsigned int duty;
if (pwm_read_int_from_file( pd->device_path, "duty", (int*)&duty) >= 0) {
fprintf( stderr, "pwm_init: '%s' has duty set to %d\n", pd->id, duty);
} else {
fprintf( stderr, "pwm_init: '%s' could not read duty file\n", pd->id);
}
fprintf( stderr, "pwm_init: '%s' disabled pwm, setting duty to 0\n", pd->id);
pwm_write_int_to_file( pd->device_path, "duty", 0);
pd->duty = 0;
// if a frequency is specified in the configuration, use it
if (pd->frequency > 0) {
pd->period = 1000000000 / pd->frequency;
fprintf( stderr, "pwm_init: '%s' set frequency to %d Hz, period of %u [ns]\n",
pd->id, pd->frequency, pd->period);
pwm_write_int_to_file( pd->device_path, "period", pd->period);
}
if (pwm_read_int_from_file( pd->device_path, "period", (int*)&pd->period) >= 0 /*&& period > 0*/) {
fprintf( stderr, "pwm_init: '%s' has period set to %u\n", pd->id, pd->period);
pd->frequency = 1000000000 / pd->period;
fprintf( stderr, "pwm_init: '%s' set frequency for to %d Hz\n", pd->id, pd->frequency);
} else {
fprintf( stderr, "pwm_init: '%s' could not read period file\n", pd->id);
}
// now open duty file for further reference
snprintf( s, sizeof( s), "%s/duty", pd->device_path);
pd->duty_fd = open( s, O_WRONLY);
if (pd->duty_fd < 0) {
perror( "pwm_init: failed to open 'duty' file");
if (get_kernel_type() == e_kernel_3_8) {
// FIXME: quick hack to work around differences between ehrpwm implementations
pwm_write_int_to_file( pd->device_path, "run", 0);
unsigned int duty;
if (pwm_read_int_from_file( pd->device_path, "duty", (int*)&duty) >= 0) {
if (debug_flags & DEBUG_PWM) {
fprintf( stderr, "pwm_init: '%s' has duty set to %d\n", pd->id, duty);
}
} else {
fprintf( stderr, "pwm_init: '%s' could not read duty file\n", pd->id);
}
if (debug_flags & DEBUG_PWM) {
fprintf( stderr, "pwm_init: '%s' disabled pwm, setting duty to 0\n", pd->id);
}
pwm_write_int_to_file( pd->device_path, "duty", 0);
pd->duty = 0;
// if a frequency is specified in the configuration, use it
if (pd->frequency > 0) {
pd->period = 1000000000 / pd->frequency;
if (debug_flags & DEBUG_PWM) {
fprintf( stderr, "pwm_init: '%s' set frequency to %d Hz, period of %u [ns]\n",
pd->id, pd->frequency, pd->period);
}
pwm_write_int_to_file( pd->device_path, "period", pd->period);
}
if (pwm_read_int_from_file( pd->device_path, "period", (int*)&pd->period) >= 0 /*&& period > 0*/) {
pd->frequency = 1000000000 / pd->period;
if (debug_flags & DEBUG_PWM) {
fprintf( stderr, "pwm_init: '%s' has period set to %u\n", pd->id, pd->period);
fprintf( stderr, "pwm_init: '%s' set frequency for to %d Hz\n", pd->id, pd->frequency);
}
} else {
fprintf( stderr, "pwm_init: '%s' could not read period file\n", pd->id);
}
// now open duty file for further reference
snprintf( s, sizeof( s), "%s/duty", pd->device_path);
pd->duty_fd = open( s, O_WRONLY);
if (pd->duty_fd < 0) {
perror( "pwm_init: failed to open 'duty' file");
}
}
#else
pwm_write_int_to_file( pd->device_path, "duty_percent", 0);
if (pd->frequency) {
pwm_write_int_to_file( pd->device_path, "period_freq", pd->frequency);
}
snprintf( s, sizeof( s), "%s/duty_percent", pd->device_path);
pd->duty_fd = open( s, O_WRONLY);
if (pd->duty_fd < 0) {
perror( "pwm_init: failed to open 'duty_percent' file");

if (get_kernel_type() == e_kernel_3_2) {
pwm_write_int_to_file( pd->device_path, "duty_percent", 0);
if (pd->frequency) {
pwm_write_int_to_file( pd->device_path, "period_freq", pd->frequency);
}
snprintf( s, sizeof( s), "%s/duty_percent", pd->device_path);
pd->duty_fd = open( s, O_WRONLY);
if (pd->duty_fd < 0) {
perror( "pwm_init: failed to open 'duty_percent' file");
}
}
#endif

pwm_set_output( pd->id, 0);
pwm_write_int_to_file( pd->device_path, "run", 1);
}
Expand All @@ -221,27 +235,31 @@ int pwm_set_output( channel_tag pwm_channel, unsigned int percentage)
char s[ 30];
int result = 0;
int count = 0;
#ifdef BBB
// Black uses cycletime [ns] instread of percentage
struct pwm_channel_record* pd = &pwm_channels[ ix];
unsigned int duty = (percentage * (1000000000UL / 100)) / pd->frequency;
if (pd->duty != duty) {
pd->duty = duty;
if (debug_flags & DEBUG_PWM) {
fprintf( stderr, "pwm_set_output: '%s' changing duty into %u (%u%% of %u)\n",
pd->id, pd->duty, percentage, pd->period);

if (get_kernel_type() == e_kernel_3_8) {
// Black uses cycletime [ns] instread of percentage
struct pwm_channel_record* pd = &pwm_channels[ ix];
unsigned int duty = (percentage * (1000000000UL / 100)) / pd->frequency;
if (pd->duty != duty) {
pd->duty = duty;
if (debug_flags & DEBUG_PWM) {
fprintf( stderr, "pwm_set_output: '%s' changing duty into %u (%u%% of %u)\n",
pd->id, pd->duty, percentage, pd->period);
}
snprintf( s, sizeof( s), "%u", pd->duty);
count = strlen( s);
lseek( fd, 0, SEEK_SET);
result = write( fd, s, count);
}
snprintf( s, sizeof( s), "%u", pd->duty);
}

if (get_kernel_type() == e_kernel_3_2) {
// White uses percentage
snprintf( s, sizeof( s), "%u", percentage);
count = strlen( s);
lseek( fd, 0, SEEK_SET);
result = write( fd, s, count);
}
#else
// White uses percentage
snprintf( s, sizeof( s), "%u", percentage);
count = strlen( s);
result = write( fd, s, count);
#endif

if (result < 0) {
perror( "pwm_set_output: error writing to duty_fd file");
} else if (result == count) {
Expand Down

0 comments on commit ea4aa71

Please sign in to comment.