From c80fcb9a9ea39cb41c3b70548f425df8c8b0a143 Mon Sep 17 00:00:00 2001 From: RPi-B27 Date: Thu, 14 Dec 2017 17:42:40 +0100 Subject: [PATCH] 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)