diff --git a/src/energenie/drv/build b/src/energenie/drv/build deleted file mode 100755 index 5979933..0000000 --- a/src/energenie/drv/build +++ /dev/null @@ -1,43 +0,0 @@ -#! /bin/bash - - -# build gpio_test -gcc gpio_test.c gpio.c -mv a.out gpio_test -chmod u+x gpio_test - - -# build spis_test (soft SPI tester) -gcc spi_test.c spis.c gpio.c -mv a.out spis_test -chmod u+x spis_test - -# build spih_test (hard SPI tester) -gcc spi_test.c spih.c gpio.c -mv a.out spih_test -chmod u+x spish_test - - -# build hrf69_test -## gcc hrf69_test.c hrf69.c spis.c gpio.c -## mv a.out hrf69_test -## chmod u+x hrf69_test - - -# build radio_test -## gcc radio_test.c radio.c hrf69.c spis.c gpio.c -## mv a.out radio_test -## chmod u+x radio_test - - -# build spi .so library on Raspberry Pi -gcc -Wall -shared -o spi_rpi.so -fPIC spis.c gpio.c -# nm -D spi_rpi.so -cp spi_rpi.so .. - -# radio spi .so library on Raspberry Pi -## gcc -Wall -shared -o radio_rpi.so -fPIC radio.c hrf69.c spis.c gpio.c -## nm -D radio_rpi.so -## cp radio_rpi.so .. - -# END diff --git a/src/energenie/drv/build_rpi b/src/energenie/drv/build_rpi new file mode 100755 index 0000000..428b10d --- /dev/null +++ b/src/energenie/drv/build_rpi @@ -0,0 +1,44 @@ +#! /bin/bash + +# build file for Raspberry Pi use + +# build gpio_test +gcc gpio_test.c gpio.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.c +mv a.out spis_test +chmod u+x spis_test + +# build spih_test (hard SPI tester) +gcc spi_test.c spih_rpi.c gpio.c +mv a.out spih_test +chmod u+x spish_test + + +# build hrf69_test +## gcc hrf69_test.c hrf69.c spis_rpi.c gpio.c +## mv a.out hrf69_test +## chmod u+x hrf69_test + + +# build radio_test +## gcc radio_test.c radio.c hrf69.c spis_rpi.c gpio.c +## mv a.out radio_test +## chmod u+x radio_test + + +# build spi .so library on Raspberry Pi +gcc -Wall -shared -o spi_rpi.so -fPIC spis_rpi.c gpio.c +# nm -D spi_rpi.so +cp spi_rpi.so .. + +# radio spi .so library on Raspberry Pi +## gcc -Wall -shared -o radio_rpi.so -fPIC radio.c hrf69.c spis_rpi.c gpio.c +## nm -D radio_rpi.so +## cp radio_rpi.so .. + +# END diff --git a/src/energenie/drv/spih.c b/src/energenie/drv/spih.c deleted file mode 100644 index 622afd5..0000000 --- a/src/energenie/drv/spih.c +++ /dev/null @@ -1,65 +0,0 @@ -/* spih.c (Hard SPI) D.J.Whale 19/07/2014 - */ - - -/***** INCLUDES *****/ - -//#include -//#include -//#include -//#include -//#include - -//#include "spi.h" -//#include "gpio.h" - - - -/***** VARIABLES *****/ - -static SPI_CONFIG config; - - -void spi_init_defaults(void) -{ - //TODO -} - - -void spi_init(SPI_CONFIG* pConfig) -{ - //TODO -} - - -void spi_finished(void) -{ - //TODO -} - - -void spi_select(void) -{ - //TODO -} - - -void spi_deselect(void) -{ - //TODO -} - - -int spi_byte(int txbyte) -{ - //TODO -} - - -void spi_frame(unsigned char* pTx, unsigned char* pRx, unsigned char count) -{ - //TODO -} - - -/***** END OF FILE *****/ diff --git a/src/energenie/drv/spih_arduino.c b/src/energenie/drv/spih_arduino.c new file mode 100644 index 0000000..74643ce --- /dev/null +++ b/src/energenie/drv/spih_arduino.c @@ -0,0 +1,6 @@ +// placeholder for arduino SPI hardware driver +// +// This will be used to aid porting to the arduino. +// The arduino has more predictable timing than the Raspberry Pi, +// and thus it is a much better test suite platform. + diff --git a/src/energenie/drv/spih_rpi.c b/src/energenie/drv/spih_rpi.c new file mode 100644 index 0000000..622afd5 --- /dev/null +++ b/src/energenie/drv/spih_rpi.c @@ -0,0 +1,65 @@ +/* spih.c (Hard SPI) D.J.Whale 19/07/2014 + */ + + +/***** INCLUDES *****/ + +//#include +//#include +//#include +//#include +//#include + +//#include "spi.h" +//#include "gpio.h" + + + +/***** VARIABLES *****/ + +static SPI_CONFIG config; + + +void spi_init_defaults(void) +{ + //TODO +} + + +void spi_init(SPI_CONFIG* pConfig) +{ + //TODO +} + + +void spi_finished(void) +{ + //TODO +} + + +void spi_select(void) +{ + //TODO +} + + +void spi_deselect(void) +{ + //TODO +} + + +int spi_byte(int txbyte) +{ + //TODO +} + + +void spi_frame(unsigned char* pTx, unsigned char* pRx, unsigned char count) +{ + //TODO +} + + +/***** END OF FILE *****/ diff --git a/src/energenie/drv/spis.c b/src/energenie/drv/spis.c deleted file mode 100644 index f77b606..0000000 --- a/src/energenie/drv/spis.c +++ /dev/null @@ -1,172 +0,0 @@ -/* spis.c (soft SPI) D.J.Whale 19/07/2014 - */ - - -/***** INCLUDES *****/ - -#include -#include -//#include -#include -#include - -#include "spi.h" -#include "gpio.h" - - -/***** MACROS *****/ - -#define CLOCK_ACTIVE() gpio_write(config.sclk, config.cpol?0:1) -#define CLOCK_IDLE() gpio_write(config.sclk, config.cpol?1:0) - -#define SELECTED() gpio_write(config.cs, config.spol?1:0) -#define NOT_SELECTED() gpio_write(config.cs, config.spol?0:1) - - -/***** VARIABLES *****/ - -static SPI_CONFIG config; - - -/* 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. - */ - -static 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); - } -} - - -void spi_init_defaults(void) -{ -#define CS 7 //CE1 -#define SCLK 11 -#define MOSI 10 -#define MISO 9 - -/* ms */ -#define TSETTLE (1) /* us settle */ -#define THOLD (1) /* us hold */ -#define TFREQ (1) /* us half clock */ - - SPI_CONFIG defaultConfig = {CS, SCLK, MOSI, MISO, SPI_SPOL0, SPI_CPOL0, SPI_CPHA0, - TSETTLE, THOLD, TFREQ}; - - spi_init(&defaultConfig); -} - - -void spi_init(SPI_CONFIG* pConfig) -{ - /* It's a standalone library, so init GPIO also */ - gpio_init(); - memcpy(&config, pConfig, sizeof(SPI_CONFIG)); - - //TODO: Implement CPHA1 - if (config.cpha != 0) - { - fprintf(stderr, "error: CPHA 1 not yet supported"); - exit(-1); - } - - gpio_setout(config.sclk); - CLOCK_IDLE(); - - gpio_setout(config.mosi); - gpio_low(config.mosi); - gpio_setin(config.miso); - - gpio_setout(config.cs); - NOT_SELECTED(); -} - - -void spi_finished(void) -{ - gpio_setin(config.mosi); - gpio_setin(config.sclk); - gpio_setin(config.cs); -} - - -void spi_select(void) -{ - SELECTED(); - delayus(config.tSettle); -} - - -void spi_deselect(void) -{ - NOT_SELECTED(); - delayus(config.tSettle); -} - - -int spi_byte(int txbyte) -{ - int rxbyte = 0; - int bitno; - int bit ; - - //TODO: Implement CPHA1 - - for (bitno=0; bitno<8; bitno++) - { - /* Transmit MSB first */ - bit = ((txbyte & 0x80) != 0x00); - txbyte <<= 1; - gpio_write(config.mosi, bit); - delayus(config.tSettle); - CLOCK_ACTIVE(); - delayus(config.tHold); - delayus(config.tFreq); - - /* Read MSB first */ - bit = gpio_read(config.miso); - rxbyte = (rxbyte<<1) | bit; - - CLOCK_IDLE(); - delayus(config.tFreq); - } - return rxbyte; -} - - -void spi_frame(unsigned char* pTx, unsigned char* pRx, unsigned char count) -{ - unsigned char tx = 0; - unsigned char rx; - - while (count > 0) - { - if (pTx != NULL) - { - tx = *(pTx++); - } - rx = spi_byte(tx); - if (pRx != NULL) - { - *(pRx++) = rx; - } - count--; - } -} - - -/***** END OF FILE *****/ diff --git a/src/energenie/drv/spis_arduino.c b/src/energenie/drv/spis_arduino.c new file mode 100644 index 0000000..bd41ac0 --- /dev/null +++ b/src/energenie/drv/spis_arduino.c @@ -0,0 +1,6 @@ +// placeholder for arduino SPI software driver +// +// This will be used to aid porting to the arduino. +// The arduino has more predictable timing than the Raspberry Pi, +// and thus it is a much better test suite platform. + diff --git a/src/energenie/drv/spis_rpi.c b/src/energenie/drv/spis_rpi.c new file mode 100644 index 0000000..f77b606 --- /dev/null +++ b/src/energenie/drv/spis_rpi.c @@ -0,0 +1,172 @@ +/* spis.c (soft SPI) D.J.Whale 19/07/2014 + */ + + +/***** INCLUDES *****/ + +#include +#include +//#include +#include +#include + +#include "spi.h" +#include "gpio.h" + + +/***** MACROS *****/ + +#define CLOCK_ACTIVE() gpio_write(config.sclk, config.cpol?0:1) +#define CLOCK_IDLE() gpio_write(config.sclk, config.cpol?1:0) + +#define SELECTED() gpio_write(config.cs, config.spol?1:0) +#define NOT_SELECTED() gpio_write(config.cs, config.spol?0:1) + + +/***** VARIABLES *****/ + +static SPI_CONFIG config; + + +/* 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. + */ + +static 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); + } +} + + +void spi_init_defaults(void) +{ +#define CS 7 //CE1 +#define SCLK 11 +#define MOSI 10 +#define MISO 9 + +/* ms */ +#define TSETTLE (1) /* us settle */ +#define THOLD (1) /* us hold */ +#define TFREQ (1) /* us half clock */ + + SPI_CONFIG defaultConfig = {CS, SCLK, MOSI, MISO, SPI_SPOL0, SPI_CPOL0, SPI_CPHA0, + TSETTLE, THOLD, TFREQ}; + + spi_init(&defaultConfig); +} + + +void spi_init(SPI_CONFIG* pConfig) +{ + /* It's a standalone library, so init GPIO also */ + gpio_init(); + memcpy(&config, pConfig, sizeof(SPI_CONFIG)); + + //TODO: Implement CPHA1 + if (config.cpha != 0) + { + fprintf(stderr, "error: CPHA 1 not yet supported"); + exit(-1); + } + + gpio_setout(config.sclk); + CLOCK_IDLE(); + + gpio_setout(config.mosi); + gpio_low(config.mosi); + gpio_setin(config.miso); + + gpio_setout(config.cs); + NOT_SELECTED(); +} + + +void spi_finished(void) +{ + gpio_setin(config.mosi); + gpio_setin(config.sclk); + gpio_setin(config.cs); +} + + +void spi_select(void) +{ + SELECTED(); + delayus(config.tSettle); +} + + +void spi_deselect(void) +{ + NOT_SELECTED(); + delayus(config.tSettle); +} + + +int spi_byte(int txbyte) +{ + int rxbyte = 0; + int bitno; + int bit ; + + //TODO: Implement CPHA1 + + for (bitno=0; bitno<8; bitno++) + { + /* Transmit MSB first */ + bit = ((txbyte & 0x80) != 0x00); + txbyte <<= 1; + gpio_write(config.mosi, bit); + delayus(config.tSettle); + CLOCK_ACTIVE(); + delayus(config.tHold); + delayus(config.tFreq); + + /* Read MSB first */ + bit = gpio_read(config.miso); + rxbyte = (rxbyte<<1) | bit; + + CLOCK_IDLE(); + delayus(config.tFreq); + } + return rxbyte; +} + + +void spi_frame(unsigned char* pTx, unsigned char* pRx, unsigned char count) +{ + unsigned char tx = 0; + unsigned char rx; + + while (count > 0) + { + if (pTx != NULL) + { + tx = *(pTx++); + } + rx = spi_byte(tx); + if (pRx != NULL) + { + *(pRx++) = rx; + } + count--; + } +} + + +/***** END OF FILE *****/