summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bus.c58
-rw-r--r--flash.c52
2 files changed, 75 insertions, 35 deletions
diff --git a/bus.c b/bus.c
index da5243a..29958b0 100644
--- a/bus.c
+++ b/bus.c
@@ -58,6 +58,14 @@
static int devmemfd = -1;
+static bool util_isbeaglebone()
+{
+ bool ret = false;
+ int fd = open("/sys/kernel/debug/omap_mux/board/core", O_RDONLY);
+ close(fd);
+ return fd > -1;
+}
+
static void* util_mapmemoryblock(off_t offset, size_t len)
{
devmemfd = open("/dev/mem", O_RDWR | O_SYNC);
@@ -192,7 +200,7 @@ static void gpmc_setup(int chipselect, int accesscycles, int size, bool enablecs
*(registers + displacement + GPMC_CONFIG3) = 0x0; // not using ADV so we can ignore this guy
*(registers + displacement + GPMC_CONFIG4) = (accesscycles << OEOFFTIME);
*(registers + displacement + GPMC_CONFIG5) = (accesscycles << RDACCESSTIME) | (accesscycles << WRCYCLETIME)
- | (accesscycles << RDCYCLETIME);
+ | (accesscycles << RDCYCLETIME);
*(registers + displacement + GPMC_CONFIG6) = (accesscycles << WRACCESSTIME);
*(registers + displacement + GPMC_CONFIG7) = size << 8 | (enablecs ? 1 << 6 : 0) | baseaddress;
@@ -296,43 +304,63 @@ static unsigned bases[] = { GPIOPIN0BASE, GPIOPIN1BASE, GPIOPIN2BASE, GPIOPIN3BA
static unsigned pins[] = { GPIOPIN0PIN, GPIOPIN1PIN, GPIOPIN2PIN, GPIOPIN3PIN, GPIOPIN4PIN, GPIOPIN5PIN };
static volatile uint8_t* extbus;
+static bool isbb = false;
void bus_init()
{
- gpmc_setup(0, GPMCACCESSTIME, GPMC_SIZE_16MB, true, 1);
- extbus = (uint8_t*) util_mapmemoryblock(0x01000000, 0x100);
+ isbb = util_isbeaglebone();
+
+ if (!isbb) {
+ printf("This doesn't seem to be a beaglebone.. bus stuff disabled!\n");
+ }
- //gpmc_printinfo();
+ if (isbb) {
+ gpmc_setup(0, GPMCACCESSTIME, GPMC_SIZE_16MB, true, 1);
- int i;
- for (i = 0; i < SIZEOFARRAY(bases); i++) {
- gpio_export(bases[i], pins[i]);
- gpio_changedirection(bases[i], pins[i], true);
+ extbus = (uint8_t*) util_mapmemoryblock(0x01000000, 0x100);
+
+ //gpmc_printinfo();
+
+ int i;
+ for (i = 0; i < SIZEOFARRAY(bases); i++) {
+ gpio_export(bases[i], pins[i]);
+ gpio_changedirection(bases[i], pins[i], true);
+ }
}
}
void bus_setpin(int pin, int value)
{
- gpio_writevalue(bases[pin], pins[pin], value & 0x1);
+ if (isbb) {
+ gpio_writevalue(bases[pin], pins[pin], value & 0x1);
+ }
}
void bus_writebyte(uint8_t address, uint8_t data)
{
- *(extbus + address) = data;
+ if (isbb) {
+ *(extbus + address) = data;
+ }
}
uint8_t bus_readbyte(uint8_t address)
{
- return *(extbus + address);
+ if (isbb) {
+ return *(extbus + address);
+ } else {
+ return 0;
+ }
}
void bus_shutdown()
{
- util_unmapmemoryblock((void*) extbus, 0x100);
- int i;
- for (i = 0; i < SIZEOFARRAY(bases); i++) {
- gpio_unexport(bases[i], pins[i]);
+ if (isbb) {
+ util_unmapmemoryblock((void*) extbus, 0x100);
+ int i;
+ for (i = 0; i < SIZEOFARRAY(bases); i++) {
+ gpio_unexport(bases[i], pins[i]);
+ }
}
}
diff --git a/flash.c b/flash.c
index 3113388..719b4b1 100644
--- a/flash.c
+++ b/flash.c
@@ -27,7 +27,8 @@
// crc32 routine
-static uint32_t crc32(uint8_t* buf, int count) {
+static uint32_t crc32(uint8_t* buf, int count)
+{
MHASH context = mhash_init(MHASH_CRC32B);
mhash(context, buf, count);
uint32_t hash;
@@ -42,19 +43,23 @@ typedef struct {
uint32_t crc32;
} persistancehdr;
-static void persistance_printheader(persistancehdr* hdr) {
-printf("Frozen struct has version %d, is %d bytes long and has the CRC32 0x%08"PRIx32"\n", hdr->version,
- hdr->length, hdr->crc32);
+static void persistance_printheader(persistancehdr* hdr)
+{
+ printf("Frozen struct has version %d, is %d bytes long and has the CRC32 0x%08"PRIx32"\n", hdr->version,
+ hdr->length, hdr->crc32);
}
// copy a file from one place to another.. this is not portable, linux only
-bool persistance_copyfile(char* source, char* dest) {
+bool persistance_copyfile(char* source, char* dest)
+{
mode_t filemode = S_IRUSR | S_IWUSR;
- int src = open(source, O_RDONLY);
+ int src = open(source, O_RDONLY | O_CREAT); // should create the file for us if this is the first run
+ // You would think that O_RDONLY would stop the file creation, but it seems to work
int dst = open(dest, O_SYNC | O_RDWR | O_CREAT, filemode);
- if (src < 0 || dst < 0)
+ if (src < 0 || dst < 0) {
return false;
+ }
struct stat s;
fstat(src, &s);
@@ -68,7 +73,8 @@ bool persistance_copyfile(char* source, char* dest) {
// store an object
bool persistance_freeze(char* dest, void* data, unsigned int offset, unsigned int len, unsigned int total,
- uint32_t version) {
+ uint32_t version)
+{
// don't write past the end of the file..
if (offset + len > total) {
@@ -151,7 +157,7 @@ bool persistance_freeze(char* dest, void* data, unsigned int offset, unsigned in
// write the data to disk
ftruncate(fd, sizeof(hdr) + total); // not really needed but if we did have a file thats bigger than it
- // should be put a stop to that
+ // should be put a stop to that
// write the header
if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
@@ -162,7 +168,7 @@ bool persistance_freeze(char* dest, void* data, unsigned int offset, unsigned in
// seek to the offset.. which could mean not seeking at all
if (lseek(fd, offset, SEEK_CUR) < 0) {
errno = PERSIST_ERR_COULDNTSEEK; // shouldn't ever happen really because if we're actually
- // seeking the file should already be the total size.
+ // seeking the file should already be the total size.
return false;
}
@@ -180,7 +186,8 @@ bool persistance_freeze(char* dest, void* data, unsigned int offset, unsigned in
}
// try to load an object from disk
-bool persistance_unfreeze(char* dest, void* result, unsigned int len, uint32_t version) {
+bool persistance_unfreeze(char* dest, void* result, unsigned int len, uint32_t version)
+{
int fd = open(dest, O_RDONLY);
@@ -225,11 +232,13 @@ bool persistance_unfreeze(char* dest, void* result, unsigned int len, uint32_t v
}
-int readUserBlock(FlashStruct *mem) {
+int readUserBlock(FlashStruct *mem)
+{
// try to unfreeze the main file
- if (persistance_unfreeze(MAINFILE, mem, sizeof(*mem), 0))
+ if (persistance_unfreeze(MAINFILE, mem, sizeof(*mem), 0)) {
return sizeof(*mem);
+ }
// something went wrong
else {
printf("Error unfreezing %d.. trying backup\n", errno);
@@ -247,7 +256,8 @@ int readUserBlock(FlashStruct *mem) {
return 0;
}
-void writeUserBlock(FlashStruct *mem, int addr, int numbytes) {
+void writeUserBlock(FlashStruct *mem, int addr, int numbytes)
+{
// *** There is a potential issue here.. if the mainfile is corrupt ***
// *** and this gets called before readUserBlock then the ***
@@ -258,20 +268,22 @@ void writeUserBlock(FlashStruct *mem, int addr, int numbytes) {
// backup the main copy of the file
if (persistance_copyfile(MAINFILE, BACKUPFILE)) {
if (!persistance_freeze(MAINFILE, mem, addr, numbytes, sizeof(*mem), 0)) {
- if (errno != PERSIST_ERR_COULDNTWRITE)
+ if (errno != PERSIST_ERR_COULDNTWRITE) {
printf("Error while trying to write, %d. **Write did not happen!!!**\n", errno);
- else
+ } else {
printf("Error while writing data to disk. **File is potentially corrupt!**\n");
+ }
}
- }
- else {
+ } else {
printf("Could not backup current file. **Write did not happen!!!**\n");
}
}
-void initFlash(FlashStruct *mem) {
- if (readUserBlock(mem) > 0)
+void initFlash(FlashStruct *mem)
+{
+ if (readUserBlock(mem) > 0) {
return;
+ }
// uninitialized device!
mem->flash_start = (char) 99;