diff --git a/src/energenie/drv/build_mac b/src/energenie/drv/build_mac index e96ac74..379e8d4 100755 --- a/src/energenie/drv/build_mac +++ b/src/energenie/drv/build_mac @@ -10,9 +10,9 @@ # build spis_test (soft SPI tester) -##gcc spi_test.c spis.c gpio_sim.c delay_posix.c -##mv a.out spis_test -##chmod u+x spis_test +gcc spi_test.c spis.c gpio_sim.c delay_posix.c +mv a.out spis_test +chmod u+x spis_test # build hrf69_test @@ -40,13 +40,7 @@ chmod u+x radio_test -# build spi .so library on Raspberry Pi -##gcc -Wall -shared -o spi_rpi.so -fPIC spis_rpi.c gpio_rpi.c -##nm -D spi_rpi.so -##cp spi_rpi.so .. - - -# radio spi .so library on Mac +# radio .so library on Mac gcc -Wall -shared -o radio_mac.so -fPIC radio.c hrfm69.c spis.c gpio_sim.c delay_posix.c ##nm -g radio_mac.so ##cp radio_mac.so .. diff --git a/src/energenie/drv/build_rpi b/src/energenie/drv/build_rpi index b7076c2..bec3576 100755 --- a/src/energenie/drv/build_rpi +++ b/src/energenie/drv/build_rpi @@ -31,13 +31,8 @@ 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_rpi.c -# nm -D spi_rpi.so -##cp spi_rpi.so .. - -# radio spi .so library on Raspberry Pi +# radio .so library on Raspberry Pi gcc -Wall -shared -o radio_rpi.so -fPIC radio.c hrfm69.c spis.c gpio_rpi.c delay_posix.c ##nm -D radio_rpi.so cp radio_rpi.so .. diff --git a/src/energenie/drv/spi_test.c b/src/energenie/drv/spi_test.c index cef2dc1..a371957 100644 --- a/src/energenie/drv/spi_test.c +++ b/src/energenie/drv/spi_test.c @@ -14,6 +14,7 @@ #include "gpio.h" #include "spi.h" #include "trace.h" +#include "delay.h" /***** CONSTANTS *****/ diff --git a/src/energenie/drv/trace.h b/src/energenie/drv/trace.h index abf396a..20a0ca9 100644 --- a/src/energenie/drv/trace.h +++ b/src/energenie/drv/trace.h @@ -11,7 +11,7 @@ #ifndef _TRACE_H #define _TRACE_H -//#define TRACE_POSIX +#define TRACE_POSIX /* POSIX IMPLEMENTATION */ /* Printf is not available on some platforms, or not very efficient. diff --git a/src/energenie/radio.py b/src/energenie/radio.py index 83c6f6a..6932d24 100644 --- a/src/energenie/radio.py +++ b/src/energenie/radio.py @@ -1,6 +1,12 @@ -# test1.py 26/09/2015 D.J.Whale +# radio.py 26/09/2015 D.J.Whale # -# Simple low level test of the HopeRF interface +# **** DEPRECATED **** +# +# NOTE: This is being DEPRECATED. +# It is only here because the FSK physical layer has not been pushed +# into the new radio interface module. +# +# Simple low level test of the HopeRF interface # Uses direct SPI commands to exercise the interface. # # Receives and dumps payload buffers. @@ -8,11 +14,12 @@ # Eventually a lot of this will be pushed into a separate module, # and then pushed back into C once it is proved working. -import spi +import radio2 as r # A temporary adaptor layer to gain access to SPI directly def warning(msg): print("warning:" + str(msg)) + def trace(msg): print(str(msg)) @@ -120,17 +127,17 @@ def HRF_writereg(addr, data): """Write an 8 bit value to a register""" buf = [addr | MASK_WRITE_DATA, data] - spi.select() - spi.frame(buf) - spi.deselect() + r.spi_select() + r.spi_frame(buf) + r.spi_deselect() def HRF_readreg(addr): """Read an 8 bit value from a register""" buf = [addr, 0x00] - spi.select() - res = spi.frame(buf) - spi.deselect() + r.spi_select() + res = r.spi_frame(buf) + r.spi_deselect() #print(hex(res[1])) return res[1] # all registers are 8 bit @@ -143,27 +150,27 @@ txbuf.append(b) #print("write FIFO %s" % ashex(txbuf)) - spi.select() - spi.frame(txbuf) - spi.deselect() + r.spi_select() + r.spi_frame(txbuf) + r.spi_deselect() def HRF_readfifo_burst(): """Read bytes from the payload FIFO using burst read""" #first byte read is the length in remaining bytes buf = [] - spi.select() - spi.frame([ADDR_FIFO]) + r.spi_select() + r.spi_frame([ADDR_FIFO]) count = 1 # read at least the length byte while count > 0: - rx = spi.frame([ADDR_FIFO]) + rx = r.spi_frame([ADDR_FIFO]) data = rx[0] if len(buf) == 0: count = data else: count -= 1 buf.append(data) - spi.deselect() + r.spi_deselect() trace("readfifo:" + str(ashex(buf))) return buf @@ -392,22 +399,22 @@ def init(): """Initialise the module ready for use""" - spi.init_defaults() + r.spi_init_defaults() trace("RESET") # Note that if another program left GPIO pins in a different state # and did a dirty exit, the reset fails to work and the clear fifo hangs. - # Might have to make the spi.init() set everything to inputs first, + # Might have to make the r.spi_init() set everything to inputs first, # then set to outputs, to make sure that the # GPIO registers are in a deterministic start state. - spi.reset() # send a hardware reset to ensure radio in clean state + r.spi_reset() # send a hardware reset to ensure radio in clean state HRF_clear_fifo() def reset(): """Reset the radio chip""" - spi.reset() + r.spi_reset() def get_ver(): @@ -450,12 +457,12 @@ def transmit(payload): """Transmit a single payload using the present modulation scheme""" - spi.start_transaction() + r.spi_start_transaction() if not modulation_fsk: HRF_send_OOK_payload(payload) else: HRF_send_payload(payload) - spi.end_transaction() + r.spi_end_transaction() def receiver(fsk=None, ook=None): @@ -471,23 +478,23 @@ def isReceiveWaiting(): """Check to see if a payload is waiting in the receive buffer""" - spi.start_transaction() + r.spi_start_transaction() waiting = HRF_check_payload() - spi.end_transaction() + r.spi_end_transaction() return waiting def receive(): """Receive a single payload from the buffer using the present modulation scheme""" - spi.start_transaction() + r.spi_start_transaction() payload = HRF_receive_payload() - spi.end_transaction() + r.spi_end_transaction() return payload def finished(): """Close the library down cleanly when finished""" - spi.finished() + r.spi_finished() # END diff --git a/src/energenie/radio2.py b/src/energenie/radio2.py index 7e2439c..70dee9b 100644 --- a/src/energenie/radio2.py +++ b/src/energenie/radio2.py @@ -2,7 +2,7 @@ # # New version of the radio driver, with most of the fast stuff pushed into C. # -# NOTE 1: This is partially tested, and only used for OOK transmit at the moment. +# NOTE 1: This is only used for OOK transmit at the moment. # FSK transmit and receive is inside radio.py, as the underlying radio.c code # does not yet support FSK mode. @@ -16,8 +16,8 @@ #TODO: Should really add parameter validation here, so that C code doesn't have to. #although it will be faster in C (C could be made optional, like an assert?) -LIBNAME = "radio_rpi.so" -#LIBNAME = "drv/radio_mac.so" # testing +#LIBNAME = "drv/radio_rpi.so" +LIBNAME = "drv/radio_mac.so" # testing import time import ctypes @@ -174,4 +174,119 @@ radio_finished_fn() +#----- TEMPORARILY EXPOSE EMBEDDED SPI MODULE --------------------------------- + +# Temporarily expose the embedded spi/gpio interface. +# This is to allow older version of code to share the .so +# rather than us having to maintain both spi.so and radio_rpi.so +# +# This is a stepping stone towards a single unified radio_rpi.so +# that does both OOK and FSK physical layer. + +spi_init_defaults_fn = libradio["spi_init_defaults"] +spi_init_fn = libradio["spi_init"] +spi_select_fn = libradio["spi_select"] +spi_deselect_fn = libradio["spi_deselect"] +spi_byte_fn = libradio["spi_byte"] +spi_frame_fn = libradio["spi_frame"] +spi_finished_fn = libradio["spi_finished"] + +#gpio_init_fn = libradio["gpio_init"] +#gpio_setin_fn = libradio["gpio_setin"] +gpio_setout_fn = libradio["gpio_setout"] +gpio_high_fn = libradio["gpio_high"] +gpio_low_fn = libradio["gpio_low"] +#gpio_write_fn = libradio["gpio_write"] +#gpio_read_fn = libradio["gpio_read"] + +RESET = 25 # BCM GPIO +LED_GREEN = 27 # BCM GPIO (not B rev1) +LED_RED = 22 # BCM GPIO + + +def spi_trace(msg): + print(str(msg)) + + +def spi_reset(): + spi_trace("reset") + + reset = ctypes.c_int(RESET) + gpio_setout_fn(reset) + gpio_high_fn(reset) + time.sleep(0.1) + gpio_low_fn(reset) + time.sleep(0.1) + + # Put LEDs into known off state + led_red = ctypes.c_int(LED_RED) + led_green = ctypes.c_int(LED_GREEN) + gpio_setout_fn(led_red) + gpio_low_fn(led_red) + gpio_setout_fn(led_green) + gpio_low_fn(led_green) + + +def spi_init_defaults(): + spi_trace("calling init_defaults") + spi_init_defaults_fn() + + +def spi_init(): + spi_trace("calling init") + #TODO build a config structure + #TODO pass in pointer to config structure + #spi_init_fn() + + +def spi_start_transaction(): + """Start a transmit or receive, perhaps multiple bursts""" + # turn the GREEN LED on + led_green = ctypes.c_int(LED_GREEN) + gpio_high_fn(led_green) + + +def spi_end_transaction(): + """End a transmit or receive, perhaps multiple listens""" + # turn the GREEN LED off + led_green = ctypes.c_int(LED_GREEN) + gpio_low_fn(led_green) + + +def spi_select(): + spi_trace("calling select") + spi_select_fn() + + +def spi_deselect(): + spi_trace("calling deselect") + spi_deselect_fn() + + +def spi_byte(tx): + txbyte = ctypes.c_ubyte(tx) + #spi_trace("calling byte") + rxbyte = spi_byte_fn(txbyte) + return rxbyte + + +def spi_frame(txlist): + spi_trace("calling frame ") + framelen = len(txlist) + #spi_trace("len:" + str(framelen)) + Frame = ctypes.c_ubyte * framelen + txframe = Frame(*txlist) + rxframe = Frame() + + spi_frame_fn(ctypes.byref(txframe), ctypes.byref(rxframe), framelen) + rxlist = [] + for i in range(framelen): + rxlist.append(rxframe[i]) + return rxlist + + +def spi_finished(): + spi_trace("calling finished") + spi_finished_fn() + # END diff --git a/src/energenie/radio_rpi.so b/src/energenie/radio_rpi.so deleted file mode 100755 index 0129587..0000000 --- a/src/energenie/radio_rpi.so +++ /dev/null Binary files differ diff --git a/src/energenie/spi.py b/src/energenie/spi.py deleted file mode 100644 index 74f3a10..0000000 --- a/src/energenie/spi.py +++ /dev/null @@ -1,140 +0,0 @@ -# spi.py 19/07/2014 D.J.Whale -# -# a C based SPI driver, with a python wrapper - -LIBNAME = "spi_rpi.so" - -import ctypes -import time - -from os import path -mydir = path.dirname(path.abspath(__file__)) - -RESET = 25 # BCM GPIO -LED_GREEN = 27 # BCM GPIO (not B rev1) -LED_RED = 22 # BCM GPIO - -libspi = ctypes.cdll.LoadLibrary(mydir + "/" + LIBNAME) -spi_init_defaults_fn = libspi["spi_init_defaults"] -spi_init_fn = libspi["spi_init"] -spi_select_fn = libspi["spi_select"] -spi_deselect_fn = libspi["spi_deselect"] -spi_byte_fn = libspi["spi_byte"] -spi_frame_fn = libspi["spi_frame"] -spi_finished_fn = libspi["spi_finished"] - -# Might put this in a separate rpi_gpio.so and dynamically link both -# from rpi_spi and from python, but have to be careful with sharing -# memmap. For now, we only use this to control the radio RESET, -# so let's leave this embedded inside the rpi_spi.so until we -# need any more visibility -#gpio_init_fn = libspi["gpio_init"] -#gpio_setin_fn = libspi["gpio_setin"] -gpio_setout_fn = libspi["gpio_setout"] -gpio_high_fn = libspi["gpio_high"] -gpio_low_fn = libspi["gpio_low"] -#gpio_write_fn = libspi["gpio_write"] -#gpio_read_fn = libspi["gpio_read"] - - -def trace(msg): - pass #print("spi:" + msg) - - -# Note, this is radio specific, not SPI specific. hmm. -# fine for now to test the theory, but architecturally this is wrong. -# might have an optional reset line in spi_config, and an spi_reset() -# in spi.c that excercises it if required, as a more generic way -# to do this, as most SPI devices have a reset line. If value not -# defined, nothing happens. If value defined, it must have a -# polarity too, and spi.c will set it to an output inactive -# until reset is required. Also need to set up a reset active -# time and a 'after reset' guard time. That will cover most -# cases generically without having to surface the whole inner GPIO -# out to the python. - -def reset(): - trace("reset") - - reset = ctypes.c_int(RESET) - gpio_setout_fn(reset) - gpio_high_fn(reset) - time.sleep(0.1) - gpio_low_fn(reset) - time.sleep(0.1) - - # Put LEDs into known off state - led_red = ctypes.c_int(LED_RED) - led_green = ctypes.c_int(LED_GREEN) - gpio_setout_fn(led_red) - gpio_low_fn(led_red) - gpio_setout_fn(led_green) - gpio_low_fn(led_green) - - -def init_defaults(): - trace("calling init_defaults") - spi_init_defaults_fn() - - -def init(): - trace("calling init") - #TODO build a config structure - #TODO pass in pointer to config structure - #spi_init_fn() - - -def start_transaction(): - """Start a transmit or receive, perhaps multiple bursts""" - # turn the GREEN LED on - led_green = ctypes.c_int(LED_GREEN) - gpio_high_fn(led_green) - - -def end_transaction(): - """End a transmit or receive, perhaps multiple listens""" - # turn the GREEN LED off - led_green = ctypes.c_int(LED_GREEN) - gpio_low_fn(led_green) - - -def select(): - trace("calling select") - spi_select_fn() - - -def deselect(): - trace("calling deselect") - spi_deselect_fn() - - -def byte(tx): - txbyte = ctypes.c_ubyte(tx) - #trace("calling byte") - rxbyte = spi_byte_fn(txbyte) - return rxbyte - - -def frame(txlist): - trace("calling frame ") - framelen = len(txlist) - #print("len:" + str(framelen)) - Frame = ctypes.c_ubyte * framelen - txframe = Frame(*txlist) - rxframe = Frame() - - spi_frame_fn(ctypes.byref(txframe), ctypes.byref(rxframe), framelen) - rxlist = [] - for i in range(framelen): - rxlist.append(rxframe[i]) - return rxlist - - -def finished(): - trace("calling finished") - spi_finished_fn() - - -# END - - diff --git a/src/energenie/spi_rpi.so b/src/energenie/spi_rpi.so deleted file mode 100755 index 6f81455..0000000 --- a/src/energenie/spi_rpi.so +++ /dev/null Binary files differ