Skip to content

Commit

Permalink
minor update gpiolib.c; update testgpiolib.py: now compatible with gp…
Browse files Browse the repository at this point in the history
…iolib.c; minor update gpiolib.py -> to be removed
  • Loading branch information
RPi-B27 authored and RPi-B27 committed Dec 14, 2017
1 parent e51eccb commit c80fcb9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
6 changes: 6 additions & 0 deletions gpiolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions gpiolib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
106 changes: 52 additions & 54 deletions testgpiolib.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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)

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)


Expand All @@ -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
Expand All @@ -47,71 +59,45 @@
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);

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)
Expand All @@ -123,22 +109,24 @@

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:
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":
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()
Expand All @@ -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)

0 comments on commit c80fcb9

Please sign in to comment.