diff --git a/src/energenie/drv/build_mac b/src/energenie/drv/build_mac index 1c48dba..948f30c 100755 --- a/src/energenie/drv/build_mac +++ b/src/energenie/drv/build_mac @@ -3,18 +3,13 @@ # build file for Mac use # build gpio_test -gcc gpio_test.c gpio_sim.c +gcc gpio_test.c gpio_sim.c delay_posix.c mv a.out gpio_test chmod u+x gpio_test - - - - - # build spis_test (soft SPI tester) -##gcc spi_test.c spis_rpi.c gpio_rpi.c +##gcc spi_test.c spis.c gpio_sim.c delay_posix.c ##mv a.out spis_test ##chmod u+x spis_test diff --git a/src/energenie/drv/delay.h b/src/energenie/drv/delay.h new file mode 100644 index 0000000..63bfe1e --- /dev/null +++ b/src/energenie/drv/delay.h @@ -0,0 +1,22 @@ +/* delay.h 04/04/2016 D.J.Whale + * + * Abstraction for delay routines, as they are often platform specific + */ + +#ifndef _DELAY_H +#define _DELAY_H + +#include "system.h" + +//#include +#include // Won't work on Arduino + +void delay(struct timespec time); + +void delaysec(uint8_t secs); + +void delayus(unsigned int us); + +#endif + +/***** END OF FILE *****/ diff --git a/src/energenie/drv/delay_posix.c b/src/energenie/drv/delay_posix.c new file mode 100644 index 0000000..a5f27a8 --- /dev/null +++ b/src/energenie/drv/delay_posix.c @@ -0,0 +1,55 @@ +/* delay_posix.c 04/04/2016 D.J.Whale + * + * Delay routines for posix compliant standard libraries (not Arduino) + */ + +//#include +#include + +#include "system.h" + +static struct timespec delay_1sec = {1, 0}; + + +void delay(struct timespec time) +{ + nanosleep(&time, NULL); +} + + +void delaysec(uint8_t secs) +{ + while (secs-- != 0) + { + delay(delay_1sec); + } +} + + +/* Based on code suggested by Gordon Henderson: + * https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPi.c + * + * Note that his trick of using the hardware timer just didn't work, + * and this is the best of a bad bunch. nanosleep() delays at least + * 100uS in some cases. + */ + + +void delayus(unsigned int us) +{ + struct timeval tNow, tLong, tEnd; + + gettimeofday(&tNow, NULL); + tLong.tv_sec = us / 1000000; + tLong.tv_usec = us % 1000000; + timeradd(&tNow, &tLong, &tEnd); + + while (timercmp(&tNow, &tEnd, <)) + { + gettimeofday(&tNow, NULL); + } +} + + +/***** END OF FILE *****/ + diff --git a/src/energenie/drv/gpio.h b/src/energenie/drv/gpio.h index eb3229f..2b261fe 100644 --- a/src/energenie/drv/gpio.h +++ b/src/energenie/drv/gpio.h @@ -6,7 +6,7 @@ #ifndef GPIO_H #define GPIO_H -typedef unsigned char uint8_t; //TODO which header file defines this? +#include "system.h" /***** FUNCTION PROTOTYPES *****/ diff --git a/src/energenie/drv/gpio_test.c b/src/energenie/drv/gpio_test.c index 2ef63ad..1736bb8 100644 --- a/src/energenie/drv/gpio_test.c +++ b/src/energenie/drv/gpio_test.c @@ -5,18 +5,14 @@ #include #include +#include "system.h" #include "gpio.h" - -//TODO this is platform specific (won't work on Arduino?) -static void delay(struct timespec time) -{ - nanosleep(&time, NULL); -} - -static struct timespec delay_1sec = {1, 0}; +#include "delay.h" -// To allow platform specific test harnesses +/* Allows platform specific test harness, + * different platforms will have different available GPIO numbers + */ #define GPIO_A 2 #define GPIO_B 3 @@ -33,15 +29,15 @@ { puts("GPIO A"); gpio_write(GPIO_A, 1); - delay(delay_1sec); + delaysec(1); gpio_write(GPIO_A, 0); - delay(delay_1sec); + delaysec(1); puts("GPIO B"); gpio_write(GPIO_B, 1); - delay(delay_1sec); + delaysec(1); gpio_write(GPIO_B, 0); - delay(delay_1sec); + delaysec(1); } return 0; } diff --git a/src/energenie/drv/spi.h b/src/energenie/drv/spi.h index 7534264..070fbff 100644 --- a/src/energenie/drv/spi.h +++ b/src/energenie/drv/spi.h @@ -7,7 +7,7 @@ /***** INCLUDES *****/ -//#include +#include "system.h" /***** CONSTANTS *****/ @@ -24,14 +24,14 @@ typedef struct { - unsigned char cs; - unsigned char sclk; - unsigned char mosi; - unsigned char miso; + uint8_t cs; + uint8_t sclk; + uint8_t mosi; + uint8_t miso; - unsigned char spol; - unsigned char cpol; - unsigned char cpha; + uint8_t spol; + uint8_t cpol; + uint8_t cpha; //struct timespec tSettle; //struct timespec tHold; @@ -53,9 +53,9 @@ void spi_deselect(void); -int spi_byte(int txbyte); +int spi_byte(uint8_t txbyte); -void spi_frame(unsigned char* pTx, unsigned char* pRx, unsigned char count); +void spi_frame(uint8_t* pTx, uint8_t* pRx, uint8_t count); void spi_finished(void); diff --git a/src/energenie/drv/spis_rpi.c b/src/energenie/drv/spis_rpi.c index f77b606..1b82e8a 100644 --- a/src/energenie/drv/spis_rpi.c +++ b/src/energenie/drv/spis_rpi.c @@ -7,9 +7,10 @@ #include #include //#include -#include +#include // Won't work on Arduino #include +#include "system.h" #include "spi.h" #include "gpio.h" @@ -35,7 +36,10 @@ * and this is the best of a bad bunch. nanosleep() delays at least * 100uS in some cases. */ - + +//TODO: This is raspberry pi specific +//Put it in a delay.h delay_rpi.h?? + static void delayus(unsigned int us) { struct timeval tNow, tLong, tEnd; @@ -118,11 +122,11 @@ } -int spi_byte(int txbyte) +int spi_byte(uint8_t txbyte) { - int rxbyte = 0; - int bitno; - int bit ; + uint8_t rxbyte = 0; + uint8_t bitno; + uint8_t bit ; //TODO: Implement CPHA1 @@ -148,10 +152,10 @@ } -void spi_frame(unsigned char* pTx, unsigned char* pRx, unsigned char count) +void spi_frame(uint8_t* pTx, uint8_t* pRx, uint8_t count) { - unsigned char tx = 0; - unsigned char rx; + uint8_t tx = 0; + uint8_t rx; while (count > 0) { diff --git a/src/energenie/drv/system.h b/src/energenie/drv/system.h new file mode 100644 index 0000000..24f3b2b --- /dev/null +++ b/src/energenie/drv/system.h @@ -0,0 +1,10 @@ +#ifndef _SYSTEM_H +#define _SYSTEM_H + +//TODO which header file defines these? + +typedef unsigned char uint8_t; + +#endif + +/***** END OF FILE *****/