From 71e35413b32c40c1b0e79c5be468c7d44da1529d Mon Sep 17 00:00:00 2001 From: RPi-B27 Date: Wed, 13 Dec 2017 16:10:52 +0100 Subject: [PATCH 1/5] first tests with python --- gpiolib.py | 614 +++++++++++++++++++++++++++++++++++++++++++++++++ testgpiolib.py | 27 +++ 2 files changed, 641 insertions(+) create mode 100644 gpiolib.py create mode 100644 testgpiolib.py diff --git a/gpiolib.py b/gpiolib.py new file mode 100644 index 0000000..41d3137 --- /dev/null +++ b/gpiolib.py @@ -0,0 +1,614 @@ +import libbcm2835._bcm2835 as bcm +import os + +class gpiolib : + MODE_READ=0 + MODE_WRITE=1 + MODE_SET=1 # redundant + MODE_CLR=2 + MODE_INPUT_READ=3 + PULL_UP=0 + PULL_DOWN=1 + NO_PULL=2 + GPIO_BEGIN=0 + GPIO_END=1 + NO_ACTION=2 + + STpin=bcm.RPI_V2_GPIO_P1_12 + RWpin=bcm.RPI_V2_GPIO_P1_11 + AD0pin=bcm.RPI_V2_GPIO_P1_07 + AD1pin=bcm.RPI_V2_GPIO_P1_13 + AD2pin=bcm.RPI_V2_GPIO_P1_15 + AD3pin=bcm.RPI_V2_GPIO_P1_29 + D0pin=bcm.RPI_V2_GPIO_P1_37 + D1pin=bcm.RPI_V2_GPIO_P1_36 + D2pin=bcm.RPI_V2_GPIO_P1_22 + D3pin=bcm.RPI_V2_GPIO_P1_18 + D4pin=bcm.RPI_V2_GPIO_P1_38 + D5pin=bcm.RPI_V2_GPIO_P1_40 + D6pin=bcm.RPI_V2_GPIO_P1_33 + D7pin=bcm.RPI_V2_GPIO_P1_35 + ACKpin=bcm.RPI_V2_GPIO_P1_16 + + #probably useless to kepp all of them here + CMD_IDLE = 0x80 + CMD_RESETPULSE = 0x88 + CMD_WRPRBITS = 0x90 + CMDH_WRPRBITS = 0x12 + CMD_SETSTARTACQ = 0x98 + CMD_STARTCONPUL = 0xA0 + CMD_STARTROPUL = 0xA8 + CMD_SETSELECT = 0xB0 + CMD_RSTBPULSE = 0xD8 + CMD_READSTATUS = 0xC0 + CMDH_READSTATUS = 0x18 + CMD_LOOPBRFIFO = 0xF0 + CMDH_LOOPBRFIFO = 0x1E + CMD_LOOPBACK = 0xF8 + CMDH_LOOPBACK = 0x1F + + BusMode="" + def __init__(self) : + bcm.bcm2835_init() + self.BusMode="" + + ###################### LOW LEVEL ROUTINES ###################### + def set_bus_init(self): + bcm.bcm2835_gpio_fsel(self.STpin, bcm.BCM2835_GPIO_FSEL_OUTP) # set pin direction + bcm.bcm2835_gpio_fsel(self.RWpin, bcm.BCM2835_GPIO_FSEL_OUTP) # set pin direction + bcm.bcm2835_gpio_fsel(self.ACKpin, bcm.BCM2835_GPIO_FSEL_INPT) # set pin direction + bcm.bcm2835_gpio_fsel(self.D7pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D6pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D5pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D4pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D3pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D2pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D1pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D0pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.AD0pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_fsel(self.AD1pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_fsel(self.AD2pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_fsel(self.AD3pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_write(self.AD0pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.LOW) + self.BusMode = self.MODE_READ # start in Read mode + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is HIGH + if lev == bcm.HIGH: + return 0 + else: + return -1 + + def set_output_pin(self): + bcm.bcm2835_gpio_fsel(self.RWpin, bcm.BCM2835_GPIO_FSEL_OUTP) + + + def set_bus_read_mode(self): + bcm.bcm2835_gpio_write(self.RWpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_fsel(self.D7pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D6pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D5pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D4pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D3pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D2pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D1pin, bcm.BCM2835_GPIO_FSEL_INPT) + bcm.bcm2835_gpio_fsel(self.D0pin, bcm.BCM2835_GPIO_FSEL_INPT) + self.BusMode = self.MODE_READ + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is HIGH + if lev == bcm.HIGH: + return 0 + else: + return -1 + + def set_bus_write_mode(self): + bcm.bcm2835_gpio_write(self.RWpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_fsel(self.D7pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_fsel(self.D6pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_fsel(self.D5pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_fsel(self.D4pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_fsel(self.D3pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_fsel(self.D2pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_fsel(self.D1pin, bcm.BCM2835_GPIO_FSEL_OUTP) + bcm.bcm2835_gpio_fsel(self.D0pin, bcm.BCM2835_GPIO_FSEL_OUTP) + self.BusMode = self.MODE_WRITE + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is HIGH + if lev == bcm.HIGH: + return 0 + else: + return -1 + + + def send_command(self,cmd): + NoAck = False + bcm.bcm2835_gpio_write(self.AD0pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.LOW) + if self.BusMode == self.MODE_READ: + self.set_bus_write_mode() + bcm.bcm2835_gpio_write(self.D0pin, (cmd &1)) + bcm.bcm2835_gpio_write(self.D1pin, ((cmd >> 1)&1)) + bcm.bcm2835_gpio_write(self.D2pin, ((cmd >> 2)&1)) + bcm.bcm2835_gpio_write(self.D3pin, ((cmd >> 3)&1)) + bcm.bcm2835_gpio_write(self.D4pin, ((cmd >> 4)&1)) + bcm.bcm2835_gpio_write(self.D5pin, ((cmd >> 5)&1)) + bcm.bcm2835_gpio_write(self.D6pin, ((cmd >> 6)&1)) + bcm.bcm2835_gpio_write(self.D7pin, ((cmd >> 7)&1)) + bcm.bcm2835_gpio_write(self.RWpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + print("\n Send Cmd, No ACK = 0") + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.RWpin, bcm.HIGH) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + print("\n Send Cmd, No ACK = 1") + if NoAck==True: + return -1 + else: + return 0 + + def fixed_acquisition(self): + cmd = CMD_SETSTARTACQ | 1 + NoAck = False + bcm.bcm2835_gpio_write(self.AD0pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.HIGH) + if self.BusMode == self.MODE_READ: + self.set_bus_write_mode() + bcm.bcm2835_gpio_write(self.D0pin, (cmd &1)) + bcm.bcm2835_gpio_write(self.D1pin, ((cmd >> 1)&1)) + bcm.bcm2835_gpio_write(self.D2pin, ((cmd >> 2)&1)) + bcm.bcm2835_gpio_write(self.D3pin, ((cmd >> 3)&1)) + bcm.bcm2835_gpio_write(self.D4pin, ((cmd >> 4)&1)) + bcm.bcm2835_gpio_write(self.D5pin, ((cmd >> 5)&1)) + bcm.bcm2835_gpio_write(self.D6pin, ((cmd >> 6)&1)) + bcm.bcm2835_gpio_write(self.D7pin, ((cmd >> 7)&1)) + bcm.bcm2835_gpio_write(self.RWpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + print("\n Send Cmd, No ACK = 0") + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.RWpin, bcm.HIGH) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + print("\n Send Cmd, No ACK = 1") + if NoAck==True: + return -1 + else: + return 0 + + def set_dac_high_word(self,val): + NoAck = False + bcm.bcm2835_gpio_write(self.AD0pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.LOW) + if self.BusMode == self.MODE_READ: + self.set_bus_write_mode() + bcm.bcm2835_gpio_write(self.D0pin, (val &1)) + bcm.bcm2835_gpio_write(self.D1pin, ((val >> 1)&1)) + bcm.bcm2835_gpio_write(self.D2pin, ((val >> 2)&1)) + bcm.bcm2835_gpio_write(self.D3pin, ((val >> 3)&1)) + bcm.bcm2835_gpio_write(self.D4pin, ((val >> 4)&1)) + bcm.bcm2835_gpio_write(self.D5pin, ((val >> 5)&1)) + bcm.bcm2835_gpio_write(self.D6pin, ((val >> 6)&1)) + bcm.bcm2835_gpio_write(self.D7pin, ((val >> 7)&1)) + bcm.bcm2835_gpio_write(self.RWpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.RWpin, bcm.HIGH) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + if NoAck==True: + return -1 + else: + return 0 + + + def set_dac_low_word(self,val): + NoAck = False + bcm.bcm2835_gpio_write(self.AD0pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.LOW) + if self.BusMode == self.MODE_READ: + self.set_bus_write_mode() + bcm.bcm2835_gpio_write(self.D0pin, (val &1)) + bcm.bcm2835_gpio_write(self.D1pin, ((val >> 1)&1)) + bcm.bcm2835_gpio_write(self.D2pin, ((val >> 2)&1)) + bcm.bcm2835_gpio_write(self.D3pin, ((val >> 3)&1)) + bcm.bcm2835_gpio_write(self.D4pin, ((val >> 4)&1)) + bcm.bcm2835_gpio_write(self.D5pin, ((val >> 5)&1)) + bcm.bcm2835_gpio_write(self.D6pin, ((val >> 6)&1)) + bcm.bcm2835_gpio_write(self.D7pin, ((val >> 7)&1)) + bcm.bcm2835_gpio_write(self.RWpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.RWpin, bcm.HIGH) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + if NoAck==True: + return -1 + else: + return 0 + + def set_trigger_delay(self,val): + NoAck = False + bcm.bcm2835_gpio_write(self.AD0pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.LOW) + if self.BusMode == self.MODE_READ: + self.set_bus_write_mode() + bcm.bcm2835_gpio_write(self.D0pin, (val &1)) + bcm.bcm2835_gpio_write(self.D1pin, ((val >> 1)&1)) + bcm.bcm2835_gpio_write(self.D2pin, ((val >> 2)&1)) + bcm.bcm2835_gpio_write(self.D3pin, ((val >> 3)&1)) + bcm.bcm2835_gpio_write(self.D4pin, ((val >> 4)&1)) + bcm.bcm2835_gpio_write(self.D5pin, ((val >> 5)&1)) + bcm.bcm2835_gpio_write(self.D6pin, ((val >> 6)&1)) + bcm.bcm2835_gpio_write(self.D7pin, ((val >> 7)&1)) + bcm.bcm2835_gpio_write(self.RWpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.RWpin, bcm.HIGH) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + if NoAck==True: + return -1 + else: + return 0 + + + def read_command(self): + NoAck = False + bcm.bcm2835_gpio_write(self.AD0pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.LOW) + if self.BusMode == self.MODE_WRITE: + self.set_bus_read_mode() + bcm.bcm2835_gpio_write(self.RWpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + print("\n Send Cmd, No ACK = 0") + r = 0 + l = bcm.bcm2835_gpio_lev(self.D0pin) + r = r | l + l = bcm.bcm2835_gpio_lev(self.D1pin) + r = r | (l << 1) + l = bcm.bcm2835_gpio_lev(self.D2pin) + r = r | (l << 2) + l = bcm.bcm2835_gpio_lev(self.D3pin) + r = r | (l << 3) + l = bcm.bcm2835_gpio_lev(self.D4pin) + r = r | (l << 4) + l = bcm.bcm2835_gpio_lev(self.D5pin) + r = r | (l << 5) + l = bcm.bcm2835_gpio_lev(self.D6pin) + r = r | (l << 6) + l = bcm.bcm2835_gpio_lev(self.D7pin) + r = r | (l << 7) + + result = int(r) + bcm.bcm2835_gpio_write(self.STpin, self.bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, self.bcm.HIGH) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + print("\n Send Cmd, No ACK = 1") + if NoAck==True: + return -1 + else: + return result + + # Read used word counter on Max10 FIFO, low part + def read_usedwl(self): + NoAck = False + bcm.bcm2835_gpio_write(self.AD0pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.LOW) + if self.BusMode == self.MODE_WRITE: + self.set_bus_read_mode() + bcm.bcm2835_gpio_write(self.RWpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + print("\n Send Cmd, No ACK = 0") + r = 0 + l = bcm.bcm2835_gpio_lev(self.D0pin) + r = r | l + l = bcm.bcm2835_gpio_lev(self.D1pin) + r = r | (l << 1) + l = bcm.bcm2835_gpio_lev(self.D2pin) + r = r | (l << 2) + l = bcm.bcm2835_gpio_lev(self.D3pin) + r = r | (l << 3) + l = bcm.bcm2835_gpio_lev(self.D4pin) + r = r | (l << 4) + l = bcm.bcm2835_gpio_lev(self.D5pin) + r = r | (l << 5) + l = bcm.bcm2835_gpio_lev(self.D6pin) + r = r | (l << 6) + l = bcm.bcm2835_gpio_lev(self.D7pin) + r = r | (l << 7) + result = int(r) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + print("\n Send Cmd, No ACK = 1") + if NoAck==True: + return -1 + else: + return result + + # Read used word counter on Max10 FIFO, high part + def read_usedwh(self): + NoAck = False + bcm.bcm2835_gpio_write(self.AD0pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.LOW) + if self.BusMode == self.MODE_WRITE: + self.set_bus_read_mode() + bcm.bcm2835_gpio_write(self.RWpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + print("\n Send Cmd, No ACK = 0") + r = 0 + l = bcm.bcm2835_gpio_lev(self.D0pin) + r = r | l + l = bcm.bcm2835_gpio_lev(self.D1pin) + r = r | (l << 1) + l = bcm.bcm2835_gpio_lev(self.D2pin) + r = r | (l << 2) + l = bcm.bcm2835_gpio_lev(self.D3pin) + r = r | (l << 3) + l = bcm.bcm2835_gpio_lev(self.D4pin) + r = r | (l << 4) + l = bcm.bcm2835_gpio_lev(self.D5pin) + r = r | (l << 5) + l = bcm.bcm2835_gpio_lev(self.D6pin) + r = r | (l << 6) + l = bcm.bcm2835_gpio_lev(self.D7pin) + r = r | (l << 7) + result = int(r) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + print("\n Send Cmd, No ACK = 1") + if NoAck==True: + return -1 + else: + return result + + + + # Write into the local FIFO + def write_local_fifo(self,val): + NoAck = False + bcm.bcm2835_gpio_write(self.AD0pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.LOW) + if self.BusMode == self.MODE_READ: + self.set_bus_write_mode() + bcm.bcm2835_gpio_write(self.D0pin, ( val &1)) + bcm.bcm2835_gpio_write(self.D1pin, ((val >> 1)&1)) + bcm.bcm2835_gpio_write(self.D2pin, ((val >> 2)&1)) + bcm.bcm2835_gpio_write(self.D3pin, ((val >> 3)&1)) + bcm.bcm2835_gpio_write(self.D4pin, ((val >> 4)&1)) + bcm.bcm2835_gpio_write(self.D5pin, ((val >> 5)&1)) + bcm.bcm2835_gpio_write(self.D6pin, ((val >> 6)&1)) + bcm.bcm2835_gpio_write(self.D7pin, ((val >> 7)&1)) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + print("\n Send Cmd, No ACK = 0") + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + print("\n Send Cmd, No ACK = 1") + if NoAck==True: + return -1 + else: + return 0 + + # Read from the local FIFO + def read_local_fifo(self): + NoAck = False + bcm.bcm2835_gpio_write(self.AD0pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.LOW) + if self.BusMode == self.MODE_WRITE: + self.set_bus_read_mode() + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + print("\n Send Cmd, No ACK = 0") + r = 0 + l = bcm.bcm2835_gpio_lev(self.D0pin) + r = r | l + l = bcm.bcm2835_gpio_lev(self.D1pin) + r = r | (l << 1) + l = bcm.bcm2835_gpio_lev(self.D2pin) + r = r | (l << 2) + l = bcm.bcm2835_gpio_lev(self.D3pin) + r = r | (l << 3) + l = bcm.bcm2835_gpio_lev(self.D4pin) + r = r | (l << 4) + l = bcm.bcm2835_gpio_lev(self.D5pin) + r = r | (l << 5) + l = bcm.bcm2835_gpio_lev(self.D6pin) + r = r | (l << 6) + l = bcm.bcm2835_gpio_lev(self.D7pin) + r = r | (l << 7) + result = int(r) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + print("\n Send Cmd, No ACK = 1") + if NoAck==True: + return -1 + else: + return result + + + # converts the programming sequence of 48 bytes into 384 single bytes where the LSB is + # the bit to be programmed + def ConvertProgrStrBytetoBit(self,thebytes): + bits=[] + for i in range(0,48): + byte=thebytes[i] + for j in range(0,8): + bits.append(1&(byte >>(7-j))) + return bits + + + def ConvertProgrStrBittoByte(self,bits): + thebytes=[] + for i in range(0,48): + b = 0; + for j in range(0,8): + b = b | (bits[i*8+j] << (7 - j)) + thebytes.append(b) + return thebytes + + +## program the 48 bytes configuration string into the SK2 3 bits at a time +## for all 4 chips on Hexaboard +## and return pointer to previous configuration string, assumes pointing to bit sequence +#int prog384(unsigned char * pNew, unsigned char * pPrevious) +#{ +# int chip, bit, j, byte_index, bit_index +# unsigned char bit2, bit1, bit0, bits, cmd +# unsigned char dout +# for(chip = 0 chip < 4 chip=chip+1){ +# for(bit = 0 bit < 384 bit = bit + 3){ +# bit2 = *(pNew + sizeof(unsigned char) * bit + 0) +# bit1 = *(pNew + sizeof(unsigned char) * bit + 1) +# bit0 = *(pNew + sizeof(unsigned char) * bit + 2) +# bits = (bit2 << 2) | (bit1 << 1) | bit0 +# cmd = CMD_WRPRBITS | bits +# send_command(cmd) +# dout = read_command() +# bits = dout & 7 +# bit2 = (bits >> 2) & 1 +# bit1 = (bits >> 1) & 1 +# bit0 = bits & 1 +# *(pPrevious + sizeof(unsigned char) * bit + 0) = bit2 +# *(pPrevious + sizeof(unsigned char) * bit + 1) = bit1 +# *(pPrevious + sizeof(unsigned char) * bit + 2) = bit0 +# } +# } +# return(0) +#} +# +#int progandverify384(unsigned char * pNew, unsigned char * pPrevious) +#{ +# prog384(pNew, pPrevious) +# prog384(pNew, pPrevious) +# return(0) +#} +# +# +#int progandverify48(unsigned char * pConfBytes, unsigned char * pPrevious) +#{ +# unsigned char *pNewConfBits +# unsigned char *pOldConfBits +# pNewConfBits = (unsigned char *) malloc(sizeof(unsigned char) * 384) +# pOldConfBits = (unsigned char *) malloc(sizeof(unsigned char) * 384) +# ConvertProgrStrBytetoBit( pConfBytes, pNewConfBits) +# prog384(pNewConfBits, pOldConfBits) +# prog384(pNewConfBits, pOldConfBits) +# ConvertProgrStrBittoByte(pOldConfBits, pPrevious) +# free(pNewConfBits) +# free(pOldConfBits) +# return(0) +#} +# +#int calib_gen(){ +# bool NoAck +# unsigned char lev +# bcm.bcm2835_gpio_write(AD0pin, bcm.LOW) +# bcm.bcm2835_gpio_write(AD1pin, bcm.LOW) +# bcm.bcm2835_gpio_write(AD2pin, bcm.LOW) +# bcm.bcm2835_gpio_write(AD3pin, bcm.HIGH) +# bcm.bcm2835_gpio_write(RWpin, bcm.LOW) +# bcm.bcm2835_gpio_write(STpin, bcm.LOW) +# bcm.bcm2835_gpio_write(STpin, bcm.LOW) +# lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW +# if(lev == bcm.HIGH) { +# NoAck = True +# } +# bcm.bcm2835_gpio_write(STpin, bcm.HIGH) +# bcm.bcm2835_gpio_write(STpin, bcm.HIGH) +# lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH +# if(lev == bcm.LOW) { +# NoAck = True +# } +# if(NoAck){ +# return(-1) +# } +# else { +# return(0) +# } +# bcm.bcm2835_gpio_write(RWpin, bcm.HIGH) +# +#} diff --git a/testgpiolib.py b/testgpiolib.py new file mode 100644 index 0000000..833e45e --- /dev/null +++ b/testgpiolib.py @@ -0,0 +1,27 @@ +import gpiolib + +CMD_IDLE = 0x80 +CMD_RESETPULSE = 0x88 +CMD_WRPRBITS = 0x90 +CMDH_WRPRBITS = 0x12 +CMD_SETSTARTACQ = 0x98 +CMD_STARTCONPUL = 0xA0 +CMD_STARTROPUL = 0xA8 +CMD_SETSELECT = 0xB0 +CMD_RSTBPULSE = 0xD8 +CMD_READSTATUS = 0xC0 +CMDH_READSTATUS = 0x18 +CMD_LOOPBRFIFO = 0xF0 +CMDH_LOOPBRFIFO = 0x1E +CMD_LOOPBACK = 0xF8 +CMDH_LOOPBACK = 0x1F + +gpio=gpiolib.gpiolib() + +res=gpio.set_bus_init() +print(res) +gpio.set_output_pin() +gpio.set_bus_read_mode() +res = gpio.send_command(CMD_RESETPULSE); +print(res) +gpio.set_bus_write_mode() From e0ea472d200a474bff5b8377d417e0cd4a2802b2 Mon Sep 17 00:00:00 2001 From: RPi-B27 Date: Wed, 13 Dec 2017 17:06:43 +0100 Subject: [PATCH 2/5] first version of gpiolib.py completed --- gpiolib.py | 141 +++++++++++++++++++++++------------------------------ 1 file changed, 60 insertions(+), 81 deletions(-) diff --git a/gpiolib.py b/gpiolib.py index 41d3137..f017e65 100644 --- a/gpiolib.py +++ b/gpiolib.py @@ -531,84 +531,63 @@ def ConvertProgrStrBittoByte(self,bits): return thebytes -## program the 48 bytes configuration string into the SK2 3 bits at a time -## for all 4 chips on Hexaboard -## and return pointer to previous configuration string, assumes pointing to bit sequence -#int prog384(unsigned char * pNew, unsigned char * pPrevious) -#{ -# int chip, bit, j, byte_index, bit_index -# unsigned char bit2, bit1, bit0, bits, cmd -# unsigned char dout -# for(chip = 0 chip < 4 chip=chip+1){ -# for(bit = 0 bit < 384 bit = bit + 3){ -# bit2 = *(pNew + sizeof(unsigned char) * bit + 0) -# bit1 = *(pNew + sizeof(unsigned char) * bit + 1) -# bit0 = *(pNew + sizeof(unsigned char) * bit + 2) -# bits = (bit2 << 2) | (bit1 << 1) | bit0 -# cmd = CMD_WRPRBITS | bits -# send_command(cmd) -# dout = read_command() -# bits = dout & 7 -# bit2 = (bits >> 2) & 1 -# bit1 = (bits >> 1) & 1 -# bit0 = bits & 1 -# *(pPrevious + sizeof(unsigned char) * bit + 0) = bit2 -# *(pPrevious + sizeof(unsigned char) * bit + 1) = bit1 -# *(pPrevious + sizeof(unsigned char) * bit + 2) = bit0 -# } -# } -# return(0) -#} -# -#int progandverify384(unsigned char * pNew, unsigned char * pPrevious) -#{ -# prog384(pNew, pPrevious) -# prog384(pNew, pPrevious) -# return(0) -#} -# -# -#int progandverify48(unsigned char * pConfBytes, unsigned char * pPrevious) -#{ -# unsigned char *pNewConfBits -# unsigned char *pOldConfBits -# pNewConfBits = (unsigned char *) malloc(sizeof(unsigned char) * 384) -# pOldConfBits = (unsigned char *) malloc(sizeof(unsigned char) * 384) -# ConvertProgrStrBytetoBit( pConfBytes, pNewConfBits) -# prog384(pNewConfBits, pOldConfBits) -# prog384(pNewConfBits, pOldConfBits) -# ConvertProgrStrBittoByte(pOldConfBits, pPrevious) -# free(pNewConfBits) -# free(pOldConfBits) -# return(0) -#} -# -#int calib_gen(){ -# bool NoAck -# unsigned char lev -# bcm.bcm2835_gpio_write(AD0pin, bcm.LOW) -# bcm.bcm2835_gpio_write(AD1pin, bcm.LOW) -# bcm.bcm2835_gpio_write(AD2pin, bcm.LOW) -# bcm.bcm2835_gpio_write(AD3pin, bcm.HIGH) -# bcm.bcm2835_gpio_write(RWpin, bcm.LOW) -# bcm.bcm2835_gpio_write(STpin, bcm.LOW) -# bcm.bcm2835_gpio_write(STpin, bcm.LOW) -# lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW -# if(lev == bcm.HIGH) { -# NoAck = True -# } -# bcm.bcm2835_gpio_write(STpin, bcm.HIGH) -# bcm.bcm2835_gpio_write(STpin, bcm.HIGH) -# lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH -# if(lev == bcm.LOW) { -# NoAck = True -# } -# if(NoAck){ -# return(-1) -# } -# else { -# return(0) -# } -# bcm.bcm2835_gpio_write(RWpin, bcm.HIGH) -# -#} + # program the 48 bytes configuration string into the SK2 3 bits at a time + # for all 4 chips on Hexaboard + # and return pointer to previous configuration string, assumes pointing to bit sequence + def prog384(self,newBits): + returnBits=[] + for chip in range(0,4): + returnBits=[] + for i in range(0,128): #128=384/3 + bit=i*3 + bit2 = newBits[bit + 0] + bit1 = newBits[bit + 1] + bit0 = newBits[bit + 2] + bits = (bit2 << 2) | (bit1 << 1) | bit0 + cmd = self.CMD_WRPRBITS | bits + self.send_command(cmd) + dout = self.read_command() + bits = dout & 7 + bit2 = (bits >> 2) & 1 + bit1 = (bits >> 1) & 1 + bit0 = bits & 1 + returnBits.append(bit2) + returnBits.append(bit1) + returnBits.append(bit0) + return returnBits + + def progandverify384(self,inputBits): + outputBits=self.prog384(inputBits) + outputBits=self.prog384(inputBits) #why do we need to do it twice + return outputBits + + + + def progandverify48(self,inputBytes): + inputBits=self.ConvertProgrStrBytetoBit(inputBytes) + outputBits=self.prog384(inputBits) + outputBits=self.prog384(inputBits) + outputBytes=self.ConvertProgrStrBittoByte(outputBits) + return outputBytes + + + def calib_gen(self): + bcm.bcm2835_gpio_write(self.AD0pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD1pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD2pin, bcm.LOW) + bcm.bcm2835_gpio_write(self.AD3pin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.RWpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + bcm.bcm2835_gpio_write(self.STpin, bcm.LOW) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.LOW + if lev == bcm.HIGH: + NoAck = True + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH + if lev == bcm.LOW: + NoAck = True + if NoAck==True: + return -1 + else: + return 0 From 02ff4dd22bb52523934d13c85f7227b0fade634c Mon Sep 17 00:00:00 2001 From: asteencern Date: Wed, 13 Dec 2017 23:41:09 +0100 Subject: [PATCH 3/5] debug gpiolib.py + update testgpiolib.py --- gpiolib.py | 4 +-- testgpiolib.py | 93 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 93 insertions(+), 4 deletions(-) diff --git a/gpiolib.py b/gpiolib.py index f017e65..1afc475 100644 --- a/gpiolib.py +++ b/gpiolib.py @@ -327,8 +327,8 @@ def read_command(self): r = r | (l << 7) result = int(r) - bcm.bcm2835_gpio_write(self.STpin, self.bcm.HIGH) - bcm.bcm2835_gpio_write(self.STpin, self.bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) + bcm.bcm2835_gpio_write(self.STpin, bcm.HIGH) lev = bcm.bcm2835_gpio_lev( self.ACKpin ) # check that ACK is bcm.HIGH if lev == bcm.LOW: NoAck = True diff --git a/testgpiolib.py b/testgpiolib.py index 833e45e..626d6ce 100644 --- a/testgpiolib.py +++ b/testgpiolib.py @@ -1,4 +1,32 @@ import gpiolib +from optparse import OptionParser +from time import sleep + +parser = OptionParser() + +parser.add_option("-r", "--saveRawData",dest="saveRawData", + help="save raw data",default=True) + +parser.add_option("-m", "--moduleNumber", dest="moduleNumber",type="int", + help="moduleNumber", default=63) + +parser.add_option("-n", "--nEvent", dest="nEvent",type="int", + help="number of event",default=100) + +parser.add_option("-e", "--externalChargeInjection", dest="externalChargeInjection", + help="set to use external injection",default=False) + + +parser.add_option("-c", "--channelIds", dest="channelIds", + help="channel Ids for charge injection", default=[36]) + +parser.add_option("-i", "--acquisitionType", dest="acquisitionType",choices=["standard","sweep","fixed"], + help="method for injection", default="standard") + + +(options, args) = parser.parse_args() + +print(options) CMD_IDLE = 0x80 CMD_RESETPULSE = 0x88 @@ -15,13 +43,74 @@ CMDH_LOOPBRFIFO = 0x1E CMD_LOOPBACK = 0xF8 CMDH_LOOPBACK = 0x1F +DAC_HIGH_WORD = 0x42 +DAC_LOW_WORD = 0x0A +TRIGGER_DELAY = 0x07# 0x00 to 0x07 + +prog_string_no_sign = [ 0xDA,0xA0,0xF9,0x32,0xE0,0xC1,0x2E,0x10,0x98,0xB0, + 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF, + 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, + 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, + 0xFF,0xFF,0xE9,0xD7,0xAE,0xBA,0x80,0x25 ] gpio=gpiolib.gpiolib() +#Initialize RPI res=gpio.set_bus_init() print(res) + gpio.set_output_pin() -gpio.set_bus_read_mode() res = gpio.send_command(CMD_RESETPULSE); print(res) -gpio.set_bus_write_mode() + +# gpio.set_bus_read_mode() +# gpio.set_bus_write_mode() + + +# empty local fifo by forcing extra reads, ignore results +for i in range(0,1): + res = gpio.read_local_fifo() + +res = gpio.set_trigger_delay(TRIGGER_DELAY); + +res = gpio.send_command(CMD_RSTBPULSE); +res = gpio.send_command(CMD_SETSELECT | 1); +print("\n\n####################################################\n") +print("Save Raw Data: %s\nExternal Pulse Injection: %s\nFixed Acquisition type: %s\n\n", options.saveRawData, options.externalChargeInjection, options.acquisitionType); +print("####################################################\n"); +if options.externalChargeInjection!=True: + #fprintf(fout,"Configuration used for SK2\n"); + #for i in range(0,48) + #fprintf(fout,"%02x ",prog_string_no_sign[i]); + + #fprintf(fout,"\nConfiguration read from SK2\n"); + print("\nConfiguration read from SK2\n"); + outputBytes=gpio.progandverify48(prog_string_no_sign); + print([hex(outputBytes[i]) for i in range(0,48)]) + #fprintf(fout,"\n");} +else: + print(fout,"\nConfiguration read from SK2\n"); + outputBytes=gpio.progandverify48(prog_string_no_sign); + print([hex(outputBytes[i]) for i in range(0,48)]) + # for(i = 0; i < 48; i = i + 1){ + # prog_string_base[i]=prog_string_no_sign[i]; + # } + # add_channel(ch,prog_string_base); + + # fprintf(fout,"Configuration used for SK2\n"); + # for(i = 0; i < 48; i = i + 1){ + # //fprintf(fout,"%02x ",prog_string_sign_inj[i]); + # fprintf(fout,"%02x ",prog_string_base[i]); + # } + # fprintf(fout,"\nConfiguration read from SK2\n"); + # //progandverify48(prog_string_sign_inj, return_string); + # progandverify48(prog_string_base, return_string); + # for(i = 0; i < 48; i = i + 1){ + # fprintf(fout,"%02x ",return_string[i]); + # } + # fprintf(fout,"\n"); + # } + +res = gpio.send_command(CMD_SETSELECT); +sleep(0.01); +print("\nFinished Configuration\n"); From e51eccb355e006c7fab4a8b5961eb95467637b8d Mon Sep 17 00:00:00 2001 From: RPi-B27 Date: Thu, 14 Dec 2017 09:52:12 +0100 Subject: [PATCH 4/5] update testgpiolib.py --- testgpiolib.py | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/testgpiolib.py b/testgpiolib.py index 626d6ce..6626192 100644 --- a/testgpiolib.py +++ b/testgpiolib.py @@ -78,6 +78,8 @@ print("\n\n####################################################\n") print("Save Raw Data: %s\nExternal Pulse Injection: %s\nFixed Acquisition type: %s\n\n", options.saveRawData, options.externalChargeInjection, options.acquisitionType); print("####################################################\n"); + +outputBytes=[] if options.externalChargeInjection!=True: #fprintf(fout,"Configuration used for SK2\n"); #for i in range(0,48) @@ -110,7 +112,44 @@ # } # fprintf(fout,"\n"); # } - -res = gpio.send_command(CMD_SETSELECT); -sleep(0.01); -print("\nFinished Configuration\n"); + +outputFile = open('Data/filename.raw','wb') +byteArray = bytearray(outputBytes) +outputFile.write(byteArray) + +res = gpio.send_command(CMD_SETSELECT) +sleep(0.1) +print("\nFinished Configuration\n") + +print("\nStart events acquisition\n") +rawData=[] +for i in range(0,options.nEvent): + print("event number ",i) + if options.acquisitionType=="sweep": + dac_ctrl = dac_fs * float(i) / float(maxevents) + res = gpio.set_dac_high_word((dac_ctrl & 0xFF0)>>4) + res = gpio.set_dac_low_word(dac_ctrl & 0x00F) + else: + res = gpio.set_dac_high_word(DAC_HIGH_WORD) + res = gpio.set_dac_low_word(DAC_LOW_WORD) + res = gpio.send_command(CMD_RESETPULSE) + sleep(0.0001); + if options.acquisitionType=="fixed": + res = gpio.fixed_acquisition() + else: + res = gpio.send_command(CMD_SETSTARTACQ | 1) + if options.externalChargeInjection: + res = gpio.send_command(CMD_SETSTARTACQ) ## <<<+++ THIS IS THE TRIGGER ## + else: + gpio.calib_gen() + res = gpio.send_command(CMD_STARTCONPUL) + sleep(0.003) + res = gpio.send_command(CMD_STARTROPUL) + sleep(0.0001) + #read the raw data + for i in range(0,30785): + t = gpio.read_local_fifo() + rawData.append(t & 255) + + byteArray = bytearray(rawData) + outputFile.write(byteArray) From c80fcb9a9ea39cb41c3b70548f425df8c8b0a143 Mon Sep 17 00:00:00 2001 From: RPi-B27 Date: Thu, 14 Dec 2017 17:42:40 +0100 Subject: [PATCH 5/5] minor update gpiolib.c; update testgpiolib.py: now compatible with gpiolib.c; minor update gpiolib.py -> to be removed --- gpiolib.c | 6 +++ gpiolib.py | 1 + testgpiolib.py | 106 ++++++++++++++++++++++++------------------------- 3 files changed, 59 insertions(+), 54 deletions(-) diff --git a/gpiolib.c b/gpiolib.c index 32bb2ca..dbe9194 100644 --- a/gpiolib.c +++ b/gpiolib.c @@ -62,6 +62,10 @@ static unsigned char BusMode; // global to remember status of gpio lines ////////////////////////////// LOW LEVEL ROUTINES ////////////////////////////// int set_bus_init() { + if(!bcm2835_init()){ + printf("problem in gpiolib.c method set_but_init(): bcm2835_init() failed -> exit code"); + exit(1); + } unsigned char lev; bcm2835_gpio_fsel(STpin, BCM2835_GPIO_FSEL_OUTP); // set pin direction bcm2835_gpio_fsel(RWpin, BCM2835_GPIO_FSEL_OUTP); // set pin direction @@ -85,6 +89,8 @@ int set_bus_init() BusMode = MODE_READ; // start in Read mode lev = bcm2835_gpio_lev( ACKpin ); // check that ACK is HIGH if(lev == HIGH) { + // Set the pin to be an output + bcm2835_gpio_fsel(RWpin, BCM2835_GPIO_FSEL_OUTP); return(0); } else { diff --git a/gpiolib.py b/gpiolib.py index 1afc475..5695d40 100644 --- a/gpiolib.py +++ b/gpiolib.py @@ -572,6 +572,7 @@ def progandverify48(self,inputBytes): def calib_gen(self): + NoAck = False bcm.bcm2835_gpio_write(self.AD0pin, bcm.LOW) bcm.bcm2835_gpio_write(self.AD1pin, bcm.LOW) bcm.bcm2835_gpio_write(self.AD2pin, bcm.LOW) diff --git a/testgpiolib.py b/testgpiolib.py index 6626192..f621c7c 100644 --- a/testgpiolib.py +++ b/testgpiolib.py @@ -1,11 +1,12 @@ -import gpiolib +##GPIOLIB.SO : gcc -c -I ./RPi_software/bcm2835-1.52/src ./RPi_software/bcm2835-1.52/src/bcm2835.c -fPIC gpiolib.c; gcc -shared -o gpiolib.so gpiolib.o +import ctypes from optparse import OptionParser from time import sleep parser = OptionParser() -parser.add_option("-r", "--saveRawData",dest="saveRawData", - help="save raw data",default=True) +parser.add_option("-r", "--compressRawData",dest="compressRawData",action="store_true", + help="option to compress (ie remove the '1000' before each word) raw data",default=False) parser.add_option("-m", "--moduleNumber", dest="moduleNumber",type="int", help="moduleNumber", default=63) @@ -13,7 +14,7 @@ parser.add_option("-n", "--nEvent", dest="nEvent",type="int", help="number of event",default=100) -parser.add_option("-e", "--externalChargeInjection", dest="externalChargeInjection", +parser.add_option("-e", "--externalChargeInjection", dest="externalChargeInjection",action="store_true", help="set to use external injection",default=False) @@ -25,9 +26,20 @@ (options, args) = parser.parse_args() - print(options) +compressRawData=options.compressRawData +moduleNumber=options.moduleNumber +nEvent=options.nEvent +externalChargeInjection=options.externalChargeInjection +channelIds=options.channelIds +acquisitionType=options.acquisitionType + +bcmlib=ctypes.CDLL("/usr/local/lib/libbcm2835.so", mode = ctypes.RTLD_GLOBAL) +gpio=ctypes.CDLL("/home/pi/arnaud-dev/rpi-daq/gpiolib.so") +res=bcmlib.bcm2835_init() +print("coucou",res) + CMD_IDLE = 0x80 CMD_RESETPULSE = 0x88 CMD_WRPRBITS = 0x90 @@ -47,28 +59,26 @@ DAC_LOW_WORD = 0x0A TRIGGER_DELAY = 0x07# 0x00 to 0x07 -prog_string_no_sign = [ 0xDA,0xA0,0xF9,0x32,0xE0,0xC1,0x2E,0x10,0x98,0xB0, - 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF, - 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, - 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, - 0xFF,0xFF,0xE9,0xD7,0xAE,0xBA,0x80,0x25 ] +prog_string_no_sign=[ 0xDA,0xA0,0xF9,0x32,0xE0,0xC1,0x2E,0x10,0x98,0xB0, + 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF, + 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, + 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, + 0xFF,0xFF,0xE9,0xD7,0xAE,0xBA,0x80,0x25 ] -gpio=gpiolib.gpiolib() +prog_string_to_c_uchar_p = (ctypes.c_ubyte*48)() +for i in range(0,48): + prog_string_to_c_uchar_p[i]=prog_string_no_sign[i] #Initialize RPI res=gpio.set_bus_init() print(res) -gpio.set_output_pin() res = gpio.send_command(CMD_RESETPULSE); print(res) -# gpio.set_bus_read_mode() -# gpio.set_bus_write_mode() - # empty local fifo by forcing extra reads, ignore results -for i in range(0,1): +for i in range(0,10): res = gpio.read_local_fifo() res = gpio.set_trigger_delay(TRIGGER_DELAY); @@ -76,42 +86,18 @@ res = gpio.send_command(CMD_RSTBPULSE); res = gpio.send_command(CMD_SETSELECT | 1); print("\n\n####################################################\n") -print("Save Raw Data: %s\nExternal Pulse Injection: %s\nFixed Acquisition type: %s\n\n", options.saveRawData, options.externalChargeInjection, options.acquisitionType); +print("Compress Raw Data: ",compressRawData,"\nExternal Pulse Injection: ", externalChargeInjection,"\nFixed Acquisition type: ",acquisitionType,"\n"); print("####################################################\n"); -outputBytes=[] -if options.externalChargeInjection!=True: - #fprintf(fout,"Configuration used for SK2\n"); - #for i in range(0,48) - #fprintf(fout,"%02x ",prog_string_no_sign[i]); - - #fprintf(fout,"\nConfiguration read from SK2\n"); +outputBytes=(ctypes.c_ubyte*48)() +if externalChargeInjection!=True: print("\nConfiguration read from SK2\n"); - outputBytes=gpio.progandverify48(prog_string_no_sign); + gpio.progandverify48(prog_string_to_c_uchar_p,outputBytes); print([hex(outputBytes[i]) for i in range(0,48)]) - #fprintf(fout,"\n");} else: - print(fout,"\nConfiguration read from SK2\n"); - outputBytes=gpio.progandverify48(prog_string_no_sign); + print("\nConfiguration read from SK2\n"); + gpio.progandverify48(prog_string_to_c_uchar_p,outputBytes); print([hex(outputBytes[i]) for i in range(0,48)]) - # for(i = 0; i < 48; i = i + 1){ - # prog_string_base[i]=prog_string_no_sign[i]; - # } - # add_channel(ch,prog_string_base); - - # fprintf(fout,"Configuration used for SK2\n"); - # for(i = 0; i < 48; i = i + 1){ - # //fprintf(fout,"%02x ",prog_string_sign_inj[i]); - # fprintf(fout,"%02x ",prog_string_base[i]); - # } - # fprintf(fout,"\nConfiguration read from SK2\n"); - # //progandverify48(prog_string_sign_inj, return_string); - # progandverify48(prog_string_base, return_string); - # for(i = 0; i < 48; i = i + 1){ - # fprintf(fout,"%02x ",return_string[i]); - # } - # fprintf(fout,"\n"); - # } outputFile = open('Data/filename.raw','wb') byteArray = bytearray(outputBytes) @@ -123,10 +109,12 @@ print("\nStart events acquisition\n") rawData=[] -for i in range(0,options.nEvent): +dac_ctrl=0 +dac_fs=0xFFF +for i in range(0,nEvent): print("event number ",i) - if options.acquisitionType=="sweep": - dac_ctrl = dac_fs * float(i) / float(maxevents) + if acquisitionType=="sweep": + dac_ctrl = int(dac_fs * float(i) / float(nEvent)) res = gpio.set_dac_high_word((dac_ctrl & 0xFF0)>>4) res = gpio.set_dac_low_word(dac_ctrl & 0x00F) else: @@ -134,11 +122,11 @@ res = gpio.set_dac_low_word(DAC_LOW_WORD) res = gpio.send_command(CMD_RESETPULSE) sleep(0.0001); - if options.acquisitionType=="fixed": + if acquisitionType=="fixed": res = gpio.fixed_acquisition() else: res = gpio.send_command(CMD_SETSTARTACQ | 1) - if options.externalChargeInjection: + if externalChargeInjection: res = gpio.send_command(CMD_SETSTARTACQ) ## <<<+++ THIS IS THE TRIGGER ## else: gpio.calib_gen() @@ -147,9 +135,19 @@ res = gpio.send_command(CMD_STARTROPUL) sleep(0.0001) #read the raw data - for i in range(0,30785): - t = gpio.read_local_fifo() - rawData.append(t & 255) + if compressRawData==True: + for i in range(0,30785): + t = gpio.read_local_fifo() + if i%2==0: + rawData.append( (t & 0xf)<<4 ) + else: + rawData[int(i/2)] |= (t & 0xf) + else: + for i in range(0,30785): + t = gpio.read_local_fifo() + rawData.append(t & 0xff) + rawData.append(dac_ctrl&0xff) + rawData.append((dac_ctrl>>8)&0xff) byteArray = bytearray(rawData) outputFile.write(byteArray)