last backups of BBB and GUI s/w

This commit is contained in:
2022-04-22 14:43:58 -07:00
commit 011d4431fd
276 changed files with 121700 additions and 0 deletions
Binary file not shown.
+578
View File
@@ -0,0 +1,578 @@
.origin 0 // start of program in PRU memory
.entrypoint START // program entry point for the debugger
#define CLOCK_CNTR_ADDR 0x00000000
#define ADC_STATE_ADDR 0x00000004
#define ADC_CNTRL_STR_ADDR 0x00000008
#define ADC_CNTRL_CHANGE 0x0000000C
#define ADC_ADDR 0x00000010
#define ADC_SIZE 0x00000014
#define ADC_SAMPLE_DIVISOR 0x00000018
#define ADC_NUM_BLOCKS 0x0000001C
#define BUFSIZE 0x20000 // 128K is optimal flash write size for bbone.
#define NUMBUFS 8 // 1Mb shared memory space divided into 8 buffers
#define SHARED_MEM_SIZE 0x100000
#define BUF_HDR_SIZE 32 // 32 Byte header contained at start of 128k buffer
#define SAMPLE_SIZE 24
#define PRU0_R31_VEC_VALID 32 // allows notification of program completion
#define PRU_EVTOUT_0 3 // the event number that is sent back
// This map documents pin mapping to the ADS1287, and the associated registers
// r30.t0 Power Down 8 Pin 8.45
// r30.t1 Power Down 4 Pin 8.46
// r30.t2 Power Down 7 Pin 8.43
// r30.t3 Power Down 3 Pin 8.44
// r30.t4 Power Down 6 Pin 8.41
// r30.t5 Power Down 2 Pin 8.42
// r30.t6 Power Down 5 Pin 8.39
// r30.t7 Power Down 1 Pin 8.40
// r30.t8 ADS1278 Board Enable Pin 8.27
// r30.t9 SPI_CLK Pin 8.29
// r31.t10 SPI DATA IN Pin 8.28
// r31.t11 SPI DATA READY Pin 8.30
// Register Use
// R0 General Purpose / Indexing
// R1 General Purpose
#define CLOCK_COUNT r2 // Clock Counter used to determine SCLOCK frequency
#define RUN_STATE r3 // Run State - can turn off data acq via this
#define ADC_CNTRL_STR r4 // ADC Control string used to enable/disable daq channels and daq
#define ADC_CHNG_FLG r5 // ADC Control Change flag used to indicate a settings change
#define SMPL_BIT_CNTR r6 // Sample bit counter, used to count number of bits read
#define CHAN_BITMASK r7 // Channel bitmask used to append channel in MSB of the sample
#define SHR_MEM_PTR r8 // Pointer to the shared memory buffer used to store data
#define SHR_MEM_SZ r9 // Size of shared memory buffer
#define CURRENT_SAMPLE r14
#define CURRENT_BUF r15 // Current 128K buffer we are writing to (0 - 9)
#define CUR_BUF_ADRS r16 // Memory address of current 128K buffer
#define CUR_BUF_LEFT r17 // This is the amount left of the current buffer in bytes
#define _128K r18 // 128k stored in register
#define NUM_BLOCKS r19 // Number of 128k blocks we are going to record
#define SAMPLE1 r21
#define SAMPLE2 r22
#define SAMPLE3 r23
#define SAMPLE4 r24
#define SAMPLE5 r25
#define SAMPLE6 r26
#define SAMPLE7 r27
#define SAMPLE8 r28
START:
// Clear all outputs to the ADS 1278
CLR r30.t0
CLR r30.t1
CLR r30.t2
CLR r30.t3
CLR r30.t4
CLR r30.t5
CLR r30.t6
CLR r30.t7
CLR r30.t8
// Clear all registers we are going to use, since we know that they can come up
// with garbage in them
MOV r0, 0
MOV r1, 0
MOV CLOCK_COUNT, 0 // R2
MOV RUN_STATE, 0 // R3
MOV ADC_CNTRL_STR, 0 // R4
MOV ADC_CHNG_FLG, 0 // R5
MOV SMPL_BIT_CNTR, 0 // R6
MOV CHAN_BITMASK, 0 // R7
MOV SHR_MEM_PTR, 0 // R8
MOV SHR_MEM_SZ, 0 // R9
MOV SKIP_CHN_BM, 0 // R10
MOV CURRENT_SAMPLE, 0 // R11
MOV DROP_SMPL_CNTR, 0 // R12
MOV CURRENT_CHAN, 0 // R13
MOV SAMPLE_DIVISOR, 0 // R14
MOV SKIP_WRITE, 0 // R15
MOV CURRENT_BUF, 0 // R16
MOV CUR_BUF_ADRS, 0 // R17
MOV CUR_BUF_LEFT, 0 // R18
MOV _128K, 0 // R19
MOV NUM_BLOCKS, 0 // R20
MOV SAMPLE1 , 0 // R21
MOV SAMPLE2 , 0 // R21
MOV SAMPLE3 , 0 // R21
MOV SAMPLE4 , 0 // R21
MOV SAMPLE5 , 0 // R21
MOV SAMPLE6 , 0 // R21
MOV SAMPLE7 , 0 // R21
MOV SAMPLE8 , 0 // R21
// Enable the OCP master port -- allows transfer of data to Linux userspace
LBCO r0, C4, 4, 4 // load PRU-ICSS CFG reg into r0
CLR r0, r0, 4 // clear bit 4 (STANDBY_INIT)
SBCO r0, C4, 4, 4 // store the modified r0 back at the load addr
START_LOOP: // This is an easy place to halt the debugger
MOV r1,ADC_STATE_ADDR
LBBO RUN_STATE, r1, 0, 4 // the daq state is now loaded into r3.
QBEQ START_LOOP, RUN_STATE, 0 // We hang out in a loop until told to read the adc
MOV r1,ADC_ADDR //
LBBO SHR_MEM_PTR, r1, 0, 4 // load the Linux address that is passed into r8 -- to store sample values
MOV r1,ADC_SIZE //
LBBO SHR_MEM_SZ, r1, 0, 4 // load the size that is passed into r9 -- the number of samples to take
MOV r1,ADC_NUM_BLOCKS //
LBBO NUM_BLOCKS, r1, 0, 4 // load the count of how many 128K blocks we are going to record
MOV _128K, BUFSIZE
MOV r1, SHARED_MEM_SIZE // Check to see that the shared mem size is correct.
QBEQ MEM_BUF_ALLOCATED, SHR_MEM_SZ, r1
HALT // If the buffer is not allocated, halt.
MEM_BUF_ALLOCATED:
MOV CUR_BUF_LEFT, _128K
MOV r1,ADC_CNTRL_CHANGE // load the base address into r1
LBBO ADC_CHNG_FLG, r1, 0, 4 // the ADC_CNTL_CHANGE is now loaded into R5
QBBS CONFIG_CHANGE, ADC_CHNG_FLG.t0 // If bit 1 of ADC_CTRL_CHANGE is set, there is a config
//change
QBA CONFIG_DONE // Else no changes, goto CONFIG_DONE
CONFIG_CHANGE:
MOV r1, CLOCK_CNTR_ADDR
LBBO CLOCK_COUNT, r1, 0, 4 // the clock delay is now loaded into r2.
MOV r1, ADC_SAMPLE_DIVISOR // load the sample divisor into R14
LBBO SAMPLE_DIVISOR, r1, 0, 4
MOV r1,ADC_CNTRL_STR_ADDR
LBBO ADC_CNTRL_STR, r1, 0, 4 // the ADC_CNTL_STR is now loaded into R4
MOV r30.b0, 0 // Clear all of the power down pins
CLR ADC_CHNG_FLG.t1 // Clear the change flag so we dont do the config every loop
ENABLE_ADC_BOARD:
QBBS ENABLE_ADC, ADC_CNTRL_STR.t8 //
QBA RESET_DONE
ENABLE_ADC:
CLR r30.t8 // We turn off the DAQ to make sure that we clear it.
MOV r0, 0xFF // Load a delay val into R0
RESET_WAIT:
SUB r0, r0, 1
QBNE RESET_WAIT, r0, 0 // loop until the delay has expired (equals 0)
SET r30.t8
RESET_DONE:
QBBS SET_PWDN1, ADC_CNTRL_STR.t0 // If bit 1 of ADC_CTRL_STR is 1, set PWDN4, Pin 46.
QBA PWDN2
SET_PWDN1:
SET r30.t1
PWDN2:
QBBS SET_PWDN2, ADC_CNTRL_STR.t1 // If bit 2 of ADC_CTRL_STR is 1, set PWDN8, pin 45
QBA PWDN3
SET_PWDN2:
SET r30.t0
PWDN3:
QBBS SET_PWDN3, ADC_CNTRL_STR.t2 // If bit 3 of ADC_CTRL_STR is 1, set PWDN3 , pin 44
QBA PWDN4
SET_PWDN3:
SET r30.t3
PWDN4:
QBBS SET_PWDN4, ADC_CNTRL_STR.t3 // If bit 4 of ADC_CTRL_STR is 1, set PWDN7, pin 43
QBA PWDN5
SET_PWDN4:
SET r30.t2
PWDN5:
QBBS SET_PWDN5, ADC_CNTRL_STR.t4 // If bit 5 of ADC_CTRL_STR is 1, set PWDN2, pin 42
QBA PWDN6
SET_PWDN5:
SET r30.t5
PWDN6:
QBBS SET_PWDN6, ADC_CNTRL_STR.t5 // If bit 6 of ADC_CTRL_STR is 1, set PWDN6, Pin 41
QBA PWDN7
SET_PWDN6:
SET r30.t4
PWDN7:
QBBS SET_PWDN7, ADC_CNTRL_STR.t6 // If bit 7 of ADC_CTRL_STR is 1, set PWDN1, Pin 40
QBA PWDN8
SET_PWDN7:
SET r30.t7
PWDN8:
QBBS SET_PWDN8, ADC_CNTRL_STR.t7 // If bit 8 of ADC_CTRL_STR is 1, set PWDN5, Pin 39
QBA CONFIG_DONE
SET_PWDN8:
SET r30.t6
CONFIG_DONE:
// We have just reset and then enabled the ADC, and enabled channels`. Wait a little bit before we try to read samples.
MOV r0, 0x0F // Load a delay val into R0
INIT_WAIT:
SUB r0, r0, 1
QBNE INIT_WAIT, r0, 0 // loop until the delay has expired (equals 0)
//We kick off a clock pulse so we can see it on the logic analyzer for debug purposes
SET r30.t9 // set the clock to be low
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
DELAY_ON1:
SUB r0, r0, 1
QBNE DELAY_ON1, r0, 0 // loop until the delay has expired (equals 0)
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
CLR r30.t9 // set the clock to be low
DELAY_OFF1:
SUB r0, r0, 1
QBNE DELAY_OFF1, r0, 0 // loop until the delay has expired (equals 0)
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
//We want to look at the first bit in the config bitmask.
READ_NEXT_SAMPLE:
MOV r1,ADC_STATE_ADDR //Check to see if we are still need to be running
LBBO RUN_STATE, r1, 0, 4
QBEQ CONTINUE_DAQ, RUN_STATE, 1
HALT // We've been told to stop.
CONTINUE_DAQ:
MOV CHAN_BITMASK, 1
MOV CURRENT_CHAN, 0
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
MOV SAMPLE1, 0
MOV SAMPLE2, 0
MOV SAMPLE3, 0
MOV SAMPLE4, 0
MOV SAMPLE5, 0
MOV SAMPLE6, 0
MOV SAMPLE7, 0
MOV SAMPLE8, 0
// We check data ready to make sure the ADC has had a chance to initialize and set dready high before sampling.
DATA_READY_HIGH:
QBBS DATA_READY_WAIT, R31.t11
QBA DATA_READY_HIGH
DATA_READY_WAIT:
QBBC DATA_READY, r31.t11 // If Data Ready line clear, read a sample
QBA DATA_READY_WAIT // Else hang out in a tight loop ...
DATA_READY:
READ_CHANNEL1:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET r30.t9 // set the clock line to be high
DELAYON1:
SUB r0, r0, 1
QBNE DELAYON1, r0, 0
CLR r30.t9 // set the clock to be low
// Read Data In
QBBC ZERO_BIT1, r31.t10 // Check to see whether Data In is a 0 or 1
OR SAMPLE1, SAMPLE1, 0x00000001
ZERO_BIT1:
LSL SAMPLE1, SAMPLE1, 1 // Shift current sample contents left by one
MOV r0, CLOCK_COUNT
DELAYOFF1:
SUB r0, r0, 1
QBNE DELAYOFF, r0, 0 // loop until the delay has expired (equals 0)
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
QBNE READ_CHANNEL1, SMPL_BIT_CNTR, 0
//////////////////////////
READ_CHANNEL2:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET r30.t9 // set the clock to be high
DELAYON2:
SUB r0, r0, 1
QBNE DELAYON2, r0, 0
// Reload the clock delay val into R0
CLR r30.t9 // set the clock to be low
// Read Data In
QBBC ZERO_BIT2, r31.t10 // Check to see whether Data In is a 0 or 1
OR SAMPLE2, SAMPLE2, 0x00000001
ZERO_BIT2:
LSL SAMPLE2, SAMPLE2, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF21:
SUB r0, r0, 1
QBNE DELAYOFF, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL2, SMPL_BIT_CNTR, 0
//////////////////////
READ_CHANNEL3:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET r30.t9 // set the clock to be high
DELAYON3:
SUB r0, r0, 1
QBNE DELAYON3, r0, 0
// Reload the clock delay val into R0
CLR r30.t9 // set the clock to be low
// Read Data In
QBBC ZERO_BIT3, r31.t10 // Check to see whether Data In is a 0 or 1
OR SAMPLE3, SAMPLE3, 0x00000001
ZERO_BIT3:
LSL SAMPLE3, SAMPLE3, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF3:
SUB r0, r0, 1
QBNE DELAYOFF, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL3, SMPL_BIT_CNTR, 0
/////////////////////////
READ_CHANNEL4:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET r30.t9 // set the clock to be high
DELAYON4:
SUB r0, r0, 1
QBNE DELAYON4, r0, 0
// Reload the clock delay val into R0
CLR r30.t9 // set the clock to be low
// Read Data In
QBBC ZERO_BIT4, r31.t10 // Check to see whether Data In is a 0 or 1
OR SAMPLE4, SAMPLE4, 0x00000001
ZERO_BIT4:
LSL SAMPLE4, SAMPLE4, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF4:
SUB r0, r0, 1
QBNE DELAYOFF, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL4, SMPL_BIT_CNTR, 0
/////////////////////////
READ_CHANNEL5:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET r30.t9 // set the clock to be high
DELAYON5:
SUB r0, r0, 1
QBNE DELAYON5, r0, 0
// Reload the clock delay val into R0
CLR r30.t9 // set the clock to be low
// Read Data In
QBBC ZERO_BIT5, r31.t10 // Check to see whether Data In is a 0 or 1
OR SAMPLE5, SAMPLE5, 0x00000001
ZERO_BIT5:
LSL SAMPLE5, SAMPLE5, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF5:
SUB r0, r0, 1
QBNE DELAYOFF, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL5, SMPL_BIT_CNTR, 0
/////////////////////////
READ_CHANNEL6:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET r30.t9 // set the clock to be high
DELAYON6:
SUB r0, r0, 1
QBNE DELAYON6, r0, 0
// Reload the clock delay val into R0
CLR r30.t9 // set the clock to be low
// Read Data In
QBBC ZERO_BIT6, r31.t10 // Check to see whether Data In is a 0 or 1
OR SAMPLE6, SAMPLE6, 0x00000001
ZERO_BIT6:
LSL SAMPLE6, SAMPLE6, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF6:
SUB r0, r0, 1
QBNE DELAYOFF, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL6, SMPL_BIT_CNTR, 0
/////////////////////////
READ_CHANNEL7:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET r30.t9 // set the clock to be high
DELAYON7:
SUB r0, r0, 1
QBNE DELAYON7, r0, 0
// Reload the clock delay val into R0
CLR r30.t9 // set the clock to be low
// Read Data In
QBBC ZERO_BIT7, r31.t10 // Check to see whether Data In is a 0 or 1
OR SAMPLE7, SAMPLE7, 0x00000001
ZERO_BIT7
LSL SAMPLE7, SAMPLE7, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF7:
SUB r0, r0, 1
QBNE DELAYOFF, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL7, SMPL_BIT_CNTR, 0
/////////////////////////
READ_CHANNEL8:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET r30.t9 // set the clock to be high
DELAYON8:
SUB r0, r0, 1
QBNE DELAYON8, r0, 0
// Reload the clock delay val into R0
CLR r30.t9 // set the clock to be low
// Read Data In
QBBC ZERO_BIT8, r31.t10 // Check to see whether Data In is a 0 or 1
OR SAMPLE8, SAMPLE8, 0x00000001
ZERO_BIT8:
LSL SAMPLE8, SAMPLE8, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF8:
SUB r0, r0, 1
QBNE DELAYOFF, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL8, SMPL_BIT_CNTR, 0
/////////////////////////
STORE_SAMPLE:
LSR SAMPLE1, SAMPLE1, 1 // Need to shift the sample word back to the right by one
LSR SAMPLE2, SAMPLE2, 1
LSR SAMPLE3, SAMPLE3, 1
LSR SAMPLE4, SAMPLE4, 1
LSR SAMPLE5, SAMPLE5, 1
LSR SAMPLE6, SAMPLE6, 1
LSR SAMPLE7, SAMPLE7, 1
LSR SAMPLE8, SAMPLE8, 1
MOV SAMPLE1.b3, 0x01 // Stash the channel bitmask in the upper byte used to store the sample
MOV SAMPLE2.b3, 0X02
MOV SAMPLE3.b3, 0x03
MOV SAMPLE4.b3, 0x04
MOV SAMPLE5.b3, 0x05
MOV SAMPLE6.b3, 0x06
MOV SAMPLE7.b3, 0x07
MOV SAMPLE8.b3, 0x08
SBBO SAMPLE1, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE2, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE3, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE4, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE5, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE6, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE7, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE8, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SUB CUR_BUF_LEFT, CUR_BUF_LEFT, 32 // reducing the number of samples - 4 bytes per sample, 8 channels
QBEQ BUFFER_DONE, CUR_BUF_LEFT, 0 // See if we have taken 128kb of samples
QBA READ_NEXT_SAMPLE // If we've looped thru all channels in the sample. wait for the next sample
BUFFER_DONE:
// Generate an interrupt to the Linux process to write the buffer to flash
MOV r31.b0, PRU0_R31_VEC_VALID | PRU_EVTOUT_0
HALT // Debug
MOV CUR_BUF_LEFT, _128K
// If we have recorded all of the buffers that we need to, then halt.
SUB NUM_BLOCKS, NUM_BLOCKS, 1
QBEQ END, NUM_BLOCKS, 0
ADD CUR_BUF_ADRS, CUR_BUF_ADRS, _128K
MOV r1, SHARED_MEM_SIZE
QBEQ WRAP_TIME, CUR_BUF_ADRS, r1
QBA READ_NEXT_SAMPLE
WRAP_TIME:
// Always wanted to be a rapper ;-0)
// We have used all the shared memory to write samples, wrap back to the beginning.
MOV CUR_BUF_ADRS, 0
MOV r31.b0, PRU0_R31_VEC_VALID | PRU_EVTOUT_0
HALT //DEBUG
QBA READ_NEXT_SAMPLE
// halt the pru program -- we reach here when the file is full.
END:
HALT
View File
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
pasm -b pruadc1.p
pasm -b pruadc0.p
gcc -v -g pruadc.c -o pruadc -lpthread -lprussdrv
+3
View File
@@ -0,0 +1,3 @@
Temporary command to change the ethernet ip address
sudo ifconfig eth0 10.72.152.2 netmask 255.255.255.0
Binary file not shown.
+966
View File
@@ -0,0 +1,966 @@
// Copyright Doug Lewis Sept 2017
#include <stdio.h>
#include <stdlib.h>
#include <prussdrv.h>
#include <pruss_intc_mapping.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include </home/erps/daq2/pruSrc/pruadc.h>
unsigned int adc_pru_data0[ADC_PRU_DATA_SIZE];
unsigned int adc_pru_data1[ADC_PRU_DATA_SIZE];
typedef struct adc_conf_t {
unsigned int cycles_per_clock;
unsigned int down_sample;
unsigned int control_string;
unsigned int num_blocks; // File size in number of 128k blocks to record
unsigned int block_size;
unsigned int mode;
unsigned int addr;
unsigned int size;
} adc_conf;
unsigned int readFileValue(char filename[]){
FILE* fp;
unsigned int value = 0;
fp = fopen(filename, "rt");
fscanf(fp, "%x", &value);
fclose(fp);
return value;
}
unsigned int adc_conf_set_address(adc_conf *conf) {
//conf->addr = ADC_PRU_DATA_SIZE + readFileValue(MMAP1_LOC "addr");
conf->addr = readFileValue(MMAP1_LOC "addr");
}
unsigned int adc_conf_get_address(adc_conf *conf) {
return (conf->addr);
}
unsigned int adc_conf_set_size(adc_conf *conf) {
conf->size = readFileValue(MMAP1_LOC "size");
}
unsigned int adc_conf_get_size(adc_conf *conf) {
return(conf->size);
}
int adc_conf_set_cycles_per_clock (adc_conf *conf, unsigned int cycles) {
conf->cycles_per_clock = cycles;
}
unsigned int adc_conf_get_cycles_per_clock (adc_conf *conf) {
return (conf->cycles_per_clock) ;
}
int adc_conf_set_mode(adc_conf *conf, unsigned int mode){
conf->mode = mode;
}
int adc_conf_set_block_size(adc_conf *conf, unsigned int block_size){
conf->block_size = block_size;
}
unsigned int adc_conf_get_mode (adc_conf *conf) {
return (conf->mode) ;
}
int adc_conf_set_down_sample(adc_conf *conf, unsigned int down_sample){
conf->down_sample = down_sample;
}
unsigned int adc_conf_get_down_sample(adc_conf *conf){
return (conf->down_sample);
}
int adc_conf_set_control_string(adc_conf *conf, unsigned int control_string){
conf->control_string = control_string;
}
unsigned int adc_conf_get_control_string(adc_conf *conf){
return (conf->control_string);
}
int adc_conf_set_data_file_size(adc_conf *conf, unsigned int data_file_size){
conf->num_blocks = data_file_size;
}
unsigned int adc_conf_get_data_file_size(adc_conf *conf){
return (conf->num_blocks);
}
int default_daq_init(adc_conf *conf) {
adc_conf_set_cycles_per_clock(conf, ADS_CLOCKS);
adc_conf_set_mode(conf, STOP);
adc_conf_set_down_sample(conf, ADS_SAMPLE_DIVISOR);
adc_conf_set_control_string(conf, ADS_CONTROL_STRING);
adc_conf_set_block_size(conf, BUFSIZE); // Default block size is 128k
adc_conf_set_data_file_size(conf, DAQ_FILE_SIZE);
adc_conf_set_address(conf);
adc_conf_set_size(conf);
}
int adc_conf_settings(adc_conf *conf) {
return (OK);
}
int main(int argc, char **argv)
{
char in_char = '0';
struct adc_conf *conf;
unsigned int *sample;
void *addr;
int read_fd;
int client_sock_fd = 0;
int i = 0;
conf = malloc(sizeof (adc_conf));
sample = malloc(sizeof(unsigned int) * 8);
if(getuid()!=0){
printf("You must run this program as root. Exiting.\n");
exit(EXIT_FAILURE);
}
// Initialize structure used by prussdrv_pruintc_intc
// PRUSS_INTC_INITDATA is found in pruss_intc_mapping.h
tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
// Read in the location and address of PRU shared memory. This value changes
// each time a new block of memory is allocated.
default_daq_init((adc_conf *) conf);
// data for setting up the ADC on PRU0.
adc_pru_data0[0] = adc_conf_get_cycles_per_clock ((adc_conf *)conf);
adc_pru_data0[1] = adc_conf_get_mode ((adc_conf *)conf);
adc_pru_data0[2] = adc_conf_get_control_string ((adc_conf *)conf);
adc_pru_data0[3] = CHANGE; // First time through, set to reflect changed.
adc_pru_data0[4] = adc_conf_get_address((adc_conf *)conf);
adc_pru_data0[5] = adc_conf_get_size((adc_conf *)conf);
adc_pru_data0[6] = adc_conf_get_down_sample ((adc_conf *)conf);
adc_pru_data0[7] = adc_conf_get_data_file_size ((adc_conf *)conf);
// data for setting up the ADC on PRU1.
adc_pru_data1[0] = adc_conf_get_cycles_per_clock ((adc_conf *)conf);
adc_pru_data1[1] = adc_conf_get_mode ((adc_conf *)conf);
adc_pru_data1[2] = adc_conf_get_control_string ((adc_conf *)conf);
adc_pru_data1[3] = CHANGE; // First time through, set to reflect changed.
adc_pru_data1[4] = adc_conf_get_address((adc_conf *)conf);
adc_pru_data1[5] = adc_conf_get_size((adc_conf *)conf);
adc_pru_data1[6] = adc_conf_get_down_sample ((adc_conf *)conf);
adc_pru_data1[7] = adc_conf_get_data_file_size ((adc_conf *)conf);
for (i = 8; i < 32; i ++)
adc_pru_data1[i] = 0;
printf("PRU1 Clock Counter %d \n", adc_pru_data1[0]);
printf("PRU1 ADC State %d \n", adc_pru_data1[1]);
printf("ADC1 Control String %x \n", adc_pru_data1[2]);
printf("ADC1 Control String Modified %d \n", adc_pru_data1[3]);
printf("PRU Shared Memory Address Pool: 0x%x\n", adc_pru_data1[4]);
printf("PRU Shared Memory Pool Size: 0x%x\n", adc_pru_data1[5]);
printf("Sample Divisor: 0x%x\n", adc_pru_data1[6]);
printf("Number of 128k blocks to record 0x%x\n", adc_pru_data1[7]);
// Allocate and initialize memory
prussdrv_init ();
prussdrv_open (PRU_EVTOUT_0);
// Write the address and size into PRU0 Data RAM0.
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, 32);
// Map PRU's interrupts
prussdrv_pruintc_init(&pruss_intc_initdata);
// Load and execute the PRU program on the PRU
prussdrv_exec_program (PRU1_ADC_CONTROL_PRU, "./pruadc1.bin");
printf("ADC Control PRU1 program running on PRU %x \n", PRU1_ADC_CONTROL_PRU);
//prussdrv_exec_program (PRU0_ADC_CONTROL_PRU, "./pruadc0.bin");
//printf("ADC Control PRU0 program running on PRU %x \n", PRU0_ADC_CONTROL_PRU);
if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
printf("Failed to open memory!");
return (ERR);
}
while (in_char != 'q') {
printf("Enter Command : \n");
scanf( " %c", &in_char);
switch (in_char) {
// Write the file to a binary file. It cannot be opened to look at
// and will require postprocessing.
case 'b' :
printf("Binary data collection to file \n");
printf("Data collection started \n");
adc_pru_data0[1] = RUN;
adc_pru_data0[3] = CHANGE;
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
// prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, adc_pru_data0, ADC_PRU_DATA_SIZE);
data_to_bin_file (conf, client_sock_fd);
break;
case 'c' :
generate_cal_data( );
break;
case 'e':
client_sock_fd = create_bind_accept_sock();
if (client_sock_fd != ERR)
printf("Socket Successfully created \n");
printf("Entering client control mode \n");
client_main(conf, client_sock_fd, adc_pru_data0, adc_pru_data1);
break;
// Write the file in a formatted hex value, with space separation
// between each of the channels in a sample so that the file is
// human readable for quick looks.
case 'f' :
printf("Formatted data collection \n");
printf("Data collection started \n");
adc_pru_data1[1] = RUN;
adc_pru_data1[3] = CHANGE;
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
//prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, adc_pru_data0, ADC_PRU_DATA_SIZE);
write_file_formatted ((adc_conf *)conf, 1);
//write_file_formatted ((adc_conf *)conf, 0);
break;
case 'h' :
printf("Data collection halted \n");
adc_pru_data1[1] = STOP;
adc_pru_data1[3] = CHANGE;
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, adc_pru_data0, ADC_PRU_DATA_SIZE);
break;
// Get a single sample, print it to the screen
case 'o':
printf("getting a single sample \n");
adc_pru_data1[1] = RUN;
adc_pru_data1[3] = CHANGE;
adc_conf_set_block_size((adc_conf *)conf, 32);
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
sampleGet((adc_conf *)conf, 1, sample);
break;
// Write the file as binary. Indicate to PRU1 that this is a throughput test.
// The data from PRU1 will contain a 24 bit counter which is incremented for
// each sample.`
case 't' :
printf("Throughput Test Mode, Binary data collection to file \n");
printf("Data generation started \n");
adc_pru_data1[1] = TEST;
adc_pru_data1[3] = CHANGE;
addr = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, adc_pru_data1[4] & ~MAP_MASK);
bzero((void *) addr, (size_t) (PRU1_START_OFFSET + BUF_HDR_SIZE));
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
data_to_bin_file ((adc_conf *)conf, client_sock_fd);
break;
case 's' :
printf("Start PRU Data collection, and return \n");
adc_pru_data1[1] = TEST;
adc_pru_data1[3] = CHANGE;
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
break;
case 'p' :
get_buff_stats((adc_conf *)conf);
break;
default :
printf("Command not recognized %c \n", in_char);
}
}
prussdrv_exit ();
}
int client_main(adc_conf *conf, int clientSockFd)
{
char sendMessage[256] = {'\0'};
char recvMessage[256];
char exit[256];
char timeString[32];
int readSize = 0;
char recStartMessage[256];
size_t length = 0;
strcpy(exit, "exit");
while (strncmp(exit, recvMessage, 4) != 0)
{
readSize = recv(clientSockFd, recvMessage, sizeof(recvMessage) , 0);
printf("Received message: %s from client \n", recvMessage);
if (strncmp("record", recvMessage, 6) == 0)
{
strncpy (sendMessage, "recstart,2000 \0 ", 13);
length = strlen(sendMessage);
send(clientSockFd, sendMessage, length, 0);
strncpy(timeString, &recvMessage[6], 14);
recording_start(conf,
clientSockFd,
timeString);
strncpy (sendMessage, "recdone ", 16);
length = strlen(sendMessage);
send(clientSockFd, sendMessage, length, 0);
}
}
close(clientSockFd);
}
int recording_start( adc_conf *conf,
int client_sock_fd,
char timeString[])
{
char fileString[128] = "/home/erps/data/dataFile";
printf("Binary data collection to file \n");
printf("Data collection started \n");
//adc_pru_data1[1] = TEST;
adc_pru_data1[1] = RUN;
adc_pru_data1[3] = CHANGE;
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
// prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, adc_pru_data0, ADC_PRU_DATA_SIZE);
strncat(fileString, timeString, 14);
strncat(fileString, ".bin\0", 6);
printf("Recording File = %s \n", fileString);
data_to_bin_file (conf, client_sock_fd, fileString);
}
// Write data to a file so that the hex info can be looked at raw.
// Formatting makes it easy to look at hex values of each channel per sample.
// Also, this operation takes a relatively small set of samples, as opposed to
// a longer duration continuous recording.
int write_file_formatted (adc_conf *conf, int pru_num)
{
int read_fd;
FILE *write_fd;
void *map_base, *virt_addr;
unsigned long read_result;
int result;
unsigned int addr = conf->addr;
unsigned int size = conf->size;
if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
printf("Failed to open memory!");
return (ERR);
}
printf("Memory file open successfully \n");
if (pru_num == 1) {
if((write_fd = fopen("/home/erps/data/outfile1.dat", "w")) == NULL){
printf("Failed to open data file \n");
return (ERR);
addr += PRU1_START_OFFSET;
printf("waiting on interrupt \n");
prussdrv_pru_wait_event (PRU_EVTOUT_0);
}
} else if (pru_num == 0) {
if((write_fd = fopen("/home/erps/data/outfile0.dat", "w")) == NULL){
printf("Failed to open data file \n");
return (ERR);
addr += PRU0_START_OFFSET;
}
} else {
printf("Invalid PRU %d \n", pru_num);
return(ERR);
}
printf("Data file open successfully \n");
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, addr & ~MAP_MASK);
if(map_base == (void *) -1) {
printf("Failed to map base address");
return (ERR);
}
printf("Reading data \n");
int i = 0;
int j = 0;
for(i = 0; i < size; i+= (8 * BYTES_PER_SAMPLE)){
for (j = 0; j < 8; j++) {
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
fprintf( write_fd, "0x%08x ", read_result);
addr += BYTES_PER_SAMPLE;
}
fprintf(write_fd, "\n");
}
if ((result = fclose (write_fd)) != 0) {
printf("Error closing data file %d \n", result);
}
return (OK);
}
int sampleGet (adc_conf *conf, int pru_num, uint32_t *sample)
{
int read_fd;
FILE *write_fd;
void *map_base, *virt_addr;
unsigned long read_result;
int result;
unsigned int addr = conf->addr;
if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
printf("Failed to open memory!");
return (ERR);
}
printf("Memory file open successfully \n");
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, conf->addr & ~MAP_MASK);
if(map_base == (void *) -1) {
printf("Failed to map base address");
return (ERR);
}
printf("Reading data \n");
int i = 0;
int j = 0;
for(i = 0; i < conf->size; i+= (8 * BYTES_PER_SAMPLE)){
for (j = 0; j < 8; j++) {
virt_addr = map_base + (addr & MAP_MASK);
//read_result = *((uint32_t *) virt_addr);
*sample = *((uint32_t *) virt_addr);
sample ++;
printf( "0x%08x ", sample);
addr += BYTES_PER_SAMPLE;
}
fprintf(write_fd, "\n");
}
return (OK);
}
int get_buff_stats(adc_conf *conf)
{
int i, buffs, buff_head_ptr;
int read_fd;
void *map_base, *virt_addr;
unsigned long read_result;
unsigned int addr = conf->addr;
unsigned int pru1_wr_flag = addr;
unsigned int pru1_buff_count = addr + 4;
unsigned int pru1_head = addr + 8;
unsigned int pru1_wrap_count = addr +12;
unsigned int pru0_wr_flag = addr + 0x80;
unsigned int pru0_buff_count = addr + 0x84;
unsigned int pru0_head = addr + 0x88;
unsigned int pru0_wrap_count = addr + 0x92;
if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
printf("Failed to open memory!");
return (ERR);
}
printf("Memory file open successfully \n");
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, addr & ~MAP_MASK);
if(map_base == (void *) -1) {
printf("Failed to map base address");
return (ERR);
}
virt_addr = map_base + (pru1_wr_flag & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU1_WR_FLAG = %x ", read_result);
virt_addr = map_base + (pru1_buff_count & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU1_BUFF_COUNT = %x ", read_result);
addr += 4;
virt_addr = map_base + (pru1_head & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU1_HEAD = %x ", read_result);
virt_addr = map_base + (pru1_wrap_count & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU1_WRAP_COUNT = %x \n", read_result);
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU0_WR_FLAG = %x ", read_result);
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU0_BUFF_COUNT = %x ", read_result);
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU0_HEAD = %x ", read_result);
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU0_WRAP_COUNT = %x \n\n", read_result);
return (OK);
}
int tail_increment(int tail, int num)
{
tail += num;
if (tail == QUEUE_SIZE)
tail = 0;
return (tail);
}
int create_bind_accept_sock()
{
#define PORT 8888
struct sockaddr_in server, client;
int addr_len = sizeof(server);
int sock_descriptor, client_sock;
int backlog = 5;
sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);
if(sock_descriptor == ERR)
{
printf("Error opening socket\n");
return -1;
}
printf("Socket Created \n");
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
server.sin_family = AF_INET;
if (bind(sock_descriptor, (struct sockaddr *)&server, sizeof(struct sockaddr_in) ) == -1)
{
printf("Error binding socket\n");
return (ERR);
}
printf("Socket Successfully bound to port %u\n", PORT);
listen(sock_descriptor, backlog);
addr_len = sizeof(server);
client_sock = accept(sock_descriptor, (struct sockaddr *) &client, (socklen_t*) &addr_len);
if (client_sock < 0)
{
error("ERROR on accept");
}
else
{
printf("Client socket connection accepted \n");
}
return(client_sock);
}
int send_buffs_to_host (void *virt_addr, int num_buffs_to_write, int sock_fd) {
int32_t bytesToSend = num_buffs_to_write * BUFSIZE;
int32_t sent = 0;
uint8_t *addr = virt_addr;
struct timeval timeout;
timeout.tv_sec = 2;
timeout.tv_usec = 0;
while (bytesToSend > 0 || (setsockopt(sock_fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) > 0))
//while (bytesToSend > 0)
{
if ((sent = send(sock_fd, (uint8_t*)addr, bytesToSend, 0)) < 0)
{
printf("ERROR: Failed to send buffer contents across TCPIP.\n");
system("ifconfig eth0 192.168.8.2");
return(ERR);
}
bytesToSend -= sent;
addr += sent;
}
}
process_buffers(void *virt_addr, int num_buffs_to_write, int sock_fd, FILE *write_fd, int8_t flash_record, int8_t network_send)
{
uint32_t wr_num;
if (flash_record == ENABLED)
wr_num = write_buffs_to_flash (virt_addr, num_buffs_to_write, write_fd);
if (network_send == ENABLED)
send_buffs_to_host (virt_addr, num_buffs_to_write, sock_fd);
}
int write_buffs_to_flash (void *virt_addr, int num_buffs_to_write, FILE * write_fd) {
int wr_num = 0;
wr_num = fwrite ( virt_addr, sizeof(uint8_t), num_buffs_to_write * BUFSIZE, write_fd);
if (wr_num != num_buffs_to_write * BUFSIZE)
{
printf ("Error writing flash file %d \n", errno);
strerror(errno);
}
return (wr_num);
}
int data_to_bin_file (adc_conf *conf, int client_sock_fd, char dataFile[])
{
int i, j, k;
int buffs = 0;
unsigned long read_result;
int read_fd;
FILE *write_fd;
void *map_base, *virt_addr;
void *pru1_wr_flag_addr;
void *pru1_buff_count_addr;
void *pru1_head_addr;
void *pru1_wrap_count_addr;
int wr_num = 0;
int tail = 0;
int num_buffs = 0;
int num_buffs_to_write = 0;
int last_count = 0;
int num_buffs_processed = 0;
volatile unsigned long pru1_wr_flag = 0;
volatile unsigned long pru1_buff_count = 0;
volatile unsigned long pru1_head = 0;
volatile unsigned long pru1_wrap_count = 0;
unsigned int addr = conf->addr;
unsigned int pru1_conf_addr = conf->addr;
int iteration = 0;
int count = 0;
int network_send = TRUE;
//int network_send = FALSE;
int flash_record = TRUE;
if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
printf("Failed to open memory!");
return (ERR);
}
printf("Memory file open successfully \n");
write_fd = fopen(dataFile, "wb");
if( write_fd == NULL){
printf("Failed to open data file \n");
return (ERR);
}
printf("Data file open successfully \n");
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, addr & ~MAP_MASK);
if(map_base == (void *) -1) {
printf("Failed to map base address");
return (ERR);
}
else {
printf("Base Address = %x \n", map_base);
}
#if DEBUG
printf("Dumping header data \n");
i = 0;
j = 0;
for(i = 0; i < (PRU1_START_OFFSET + 256); i+= (8 * BYTES_PER_SAMPLE)){
for (j = 0; j < 8; j++) {
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf( "0x%08x ", read_result);
addr += BYTES_PER_SAMPLE;
}
printf("\n");
}
#endif
pru1_conf_addr = conf->addr;
addr = conf->addr + PRU1_START_OFFSET;
pru1_wr_flag_addr = map_base + ((pru1_conf_addr + PRU1_WR_FLAG) & MAP_MASK);
pru1_wr_flag = *((uint32_t *) pru1_wr_flag_addr);
pru1_buff_count_addr = map_base + ((pru1_conf_addr + PRU1_BUFF_COUNT) & MAP_MASK);
pru1_buff_count = *((uint32_t *) pru1_buff_count_addr);
pru1_head_addr = map_base + ((pru1_conf_addr + PRU1_HEAD) & MAP_MASK);
pru1_head = *((uint32_t *) pru1_head_addr);
pru1_wrap_count_addr = map_base + ((pru1_conf_addr + PRU1_WRAP_COUNT) & MAP_MASK);
pru1_wrap_count = *((uint32_t *) pru1_wrap_count_addr);
tail = 0;
while (num_buffs_processed < DAQ_FILE_SIZE)
{
virt_addr = map_base + ((pru1_conf_addr + PRU1_WR_FLAG) & MAP_MASK);
pru1_wr_flag = *((uint32_t *) pru1_wr_flag_addr);
pru1_buff_count_addr = map_base + ((pru1_conf_addr + PRU1_BUFF_COUNT) & MAP_MASK);
pru1_buff_count = *((uint32_t *) pru1_buff_count_addr);
pru1_head_addr = map_base + ((pru1_conf_addr + PRU1_HEAD) & MAP_MASK);
pru1_head = *((uint32_t *) pru1_head_addr);
pru1_wrap_count_addr = map_base + ((pru1_conf_addr + PRU1_WRAP_COUNT) & MAP_MASK);
pru1_wrap_count = *((uint32_t *) pru1_wrap_count_addr);
if (pru1_wr_flag == 1)
{
num_buffs = pru1_buff_count - last_count;
if (num_buffs > 0)
{
// printf("h = %d, t = %d, num_buffs = %d ", pru1_head, tail, num_buffs);
}
if (num_buffs <= QUEUE_SIZE)
{
if (tail < pru1_head)
{
num_buffs_to_write = pru1_head - tail;
virt_addr = map_base + (addr & MAP_MASK);
process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
num_buffs_processed += num_buffs_to_write;
addr += num_buffs_to_write * BUFSIZE;
tail = tail_increment(tail, num_buffs_to_write);
pru1_wr_flag = 0;
}
else if (tail > pru1_head)
{
// Write the buffers from the tail to the end of the queue.
virt_addr = map_base + (addr & MAP_MASK);
num_buffs_to_write = QUEUE_SIZE - tail;
process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
//addr += num_buffs_to_write * BUFSIZE;
tail = tail_increment(tail, num_buffs_to_write);
num_buffs_processed += num_buffs_to_write;
// Write the buffers from the beginning of the queue (zeroth element) to the head
// We have to remember to set the pointer back to the beginning of shared memory
addr = conf->addr + PRU1_START_OFFSET;
virt_addr = map_base + (addr & MAP_MASK);
num_buffs_to_write = pru1_head;
process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
addr += num_buffs_to_write * BUFSIZE;
num_buffs_processed += num_buffs_to_write;
tail = tail_increment(tail, num_buffs_to_write);
}
else if ((tail == pru1_head) && (num_buffs == QUEUE_SIZE))
{
// Write the buffers from the tail to the end of the queue.
virt_addr = map_base + (addr & MAP_MASK);
num_buffs_to_write += QUEUE_SIZE - tail;
process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
num_buffs_processed += num_buffs_to_write;
addr += num_buffs_to_write * BUFSIZE;
tail = tail_increment(tail, num_buffs_to_write);
// Write the buffers from the beginning of the queue to the head
addr = conf->addr + PRU1_START_OFFSET;
virt_addr = map_base + (addr & MAP_MASK);
num_buffs_to_write = pru1_head;
process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
addr += num_buffs_to_write * BUFSIZE;
num_buffs_processed += num_buffs_to_write;
tail = tail_increment(tail, num_buffs_to_write); // This may look funny, but we are incrementing tail by tail.
}
pru1_wr_flag = 0;
if (num_buffs > 0)
{
count += num_buffs;
if (count >= 30)
{
printf("h = %d, t = %d, num_buffs = %d ", pru1_head, tail, num_buffs);
printf("num_procd = %d, addr = %x \n", num_buffs_processed, addr);
count = 0;
}
// if (num_buffs_processed >= 180)
// {
// printf("num_procd = %d, addr = %x \n", num_buffs_processed, addr);
// // this is a place to set a breakpoint
// }
}
last_count = pru1_buff_count;
usleep(100);
}
else if (num_buffs > QUEUE_SIZE)
{
// We have detected an overflow condition, likely due to some linux related activity.
// For now we throw out the buffers that have accumulated. Later we can explore saving
// those buffers.
printf("Queue overflow %d \n", num_buffs);
last_count = pru1_buff_count;
num_buffs_processed += num_buffs;
usleep(100);
}
}
}
printf("TOTAL num_buffs_processed = %d \n", num_buffs_processed);
fclose ((FILE *)write_fd);
return (OK);
}
generate_cal_data( ) {
FILE *read_5v_fd;
FILE *read_0v_fd;
FILE *write_fd;
uint32_t average_5v[8];
uint32_t average_0v[8];
float ma[8];
uint32_t b[8];
int i;
uint32_t temp;
read_5v_fd = fopen("/home/erps/caldata/board1_5v_golden_063018.dat", "r");
if( read_5v_fd == NULL){
printf("Failed to open 5v cal data file \n");
return (ERR);
}
else
printf("Cal File Opened \n");
read_and_average_cal_data(read_5v_fd, &average_5v[0]);
printf("5V Averages %x %x %x %x %x %x %x %x \n\n",average_5v[0], average_5v[1],
average_5v[2], average_5v[3], average_5v[4], average_5v[5], average_5v[6], average_5v[7]);
read_0v_fd = fopen("/home/erps/caldata/board1_0v_golden_063018.dat", "r");
if( read_0v_fd == NULL){
printf("Failed to open 0v cal data file \n");
return (ERR);
}
else
printf("Cal File Opened \n");
write_fd = fopen("/home/erps/caldata/board1_cal_golden_063018.dat", "w");
if( write_fd == NULL){
printf("Failed to open cal result file \n");
return (ERR);
}
else
printf("Cal Result File Opened \n");
read_and_average_cal_data(read_0v_fd, &average_0v[0]);
printf("0V Averages %x %x %x %x %x %x %x %x \n\n",average_0v[0], average_0v[1],
average_0v[2], average_0v[3], average_0v[4], average_0v[5], average_0v[6], average_0v[7]);
for (i = 0; i < 8; i++)
{
// y = mx + b
//ma = (yH - yL) / (xH - xL)
if (average_0v[i] & 0x800000)
{
temp = ~average_0v[i] & 0x00FFFFFF;
ma[i] = (float)(average_5v[i] + temp) / 0x7FFFFF ;
b[i] = temp * ma[i];
}
else
{
ma[i] = (float)(average_5v[i] - average_0v[i]) / 0x7FFFFF ;
b[i] = average_0v[i] * ma[i];
}
}
printf("\n\n");
printf(" ma:%f %f %f %f %f %f %f %f \n", ma[0], ma[1], ma[2], ma[3], ma[4], ma[5], ma[6], ma[7]);
printf(" b :%x %x %x %x %x %x %x %x \n", b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
fprintf(write_fd, "%f %f %f %f %f %f %f %f \n", ma[0], ma[1], ma[2], ma[3], ma[4], ma[5], ma[6], ma[7]);
fprintf(write_fd, "%x %x %x %x %x %x %x %x \n", b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
fclose ((FILE *)read_5v_fd);
fclose ((FILE *)read_0v_fd);
fclose ((FILE *)write_fd);
}
read_and_average_cal_data(FILE *read_fd, uint32_t *average[]) {
int i = 0;
int j = 0;
uint32_t s[8];
uint64_t total[8];
int64_t tot[8];
for (i = 0; i < 9; i ++){
fscanf(read_fd, "%x %x %x %x %x %x %x %x \n", &s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6], &s[7]);
// printf("%x %x %x %x %x %x %x %x \n", s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
}
for (i = 0; i < 8; i ++) {
total[i] = 0;
s[i] = 0;
average[i] = 0;
}
for (i = 0; i < 8; i ++) {
for (j = 0; j < 8; j++) {
fscanf(read_fd, "%x", &s[j]);
s[j] &= s[j] & 0x0FFFFFF;
if (s[j] & 0x800000)
{
s[j] = ~s[j] & 0xFFFFFF;
tot[j] -= s[j];
}
tot[j] += s[j];
total[j] += s[j];
}
fscanf(read_fd, "\n");
}
printf("Totals %x %x %x %x %x %x %x %x \n \n",total[0], total[1], total[2], total[3], total[4], total[5], total[6], total[7]);
printf("Totals %x %x %x %x %x %x %x %x \n \n",(uint32_t)tot[0], (uint32_t) tot[1], (uint32_t) tot[2], (uint32_t) tot[3],
(uint32_t) tot[4], (uint32_t) tot[5], (uint32_t)tot[6], (uint32_t)tot[7]);
for (i = 0; i < 8; i ++)
{
average[i] = total[i] / 8;
}
}
+807
View File
@@ -0,0 +1,807 @@
// Copyright Doug Lewis Sept 2017
#include <stdio.h>
#include <stdlib.h>
#include <prussdrv.h>
#include <pruss_intc_mapping.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include </home/erps/daq2/pruSrc/pruadc.h>
unsigned int adc_pru_data0[ADC_PRU_DATA_SIZE];
unsigned int adc_pru_data1[ADC_PRU_DATA_SIZE];
typedef struct adc_conf_t {
unsigned int cycles_per_clock;
unsigned int down_sample;
unsigned int control_string;
unsigned int num_blocks; // File size in number of 128k blocks to record
unsigned int block_size;
unsigned int mode;
unsigned int addr;
unsigned int size;
} adc_conf;
unsigned int readFileValue(char filename[]){
FILE* fp;
unsigned int value = 0;
fp = fopen(filename, "rt");
fscanf(fp, "%x", &value);
fclose(fp);
return value;
}
unsigned int adc_conf_set_address(adc_conf *conf) {
//conf->addr = ADC_PRU_DATA_SIZE + readFileValue(MMAP1_LOC "addr");
conf->addr = readFileValue(MMAP1_LOC "addr");
}
unsigned int adc_conf_get_address(adc_conf *conf) {
return (conf->addr);
}
unsigned int adc_conf_set_size(adc_conf *conf) {
conf->size = readFileValue(MMAP1_LOC "size");
}
unsigned int adc_conf_get_size(adc_conf *conf) {
return(conf->size);
}
int adc_conf_set_cycles_per_clock (adc_conf *conf, unsigned int cycles) {
conf->cycles_per_clock = cycles;
}
unsigned int adc_conf_get_cycles_per_clock (adc_conf *conf) {
return (conf->cycles_per_clock) ;
}
int adc_conf_set_mode(adc_conf *conf, unsigned int mode){
conf->mode = mode;
}
int adc_conf_set_block_size(adc_conf *conf, unsigned int block_size){
conf->block_size = block_size;
}
unsigned int adc_conf_get_mode (adc_conf *conf) {
return (conf->mode) ;
}
int adc_conf_set_down_sample(adc_conf *conf, unsigned int down_sample){
conf->down_sample = down_sample;
}
unsigned int adc_conf_get_down_sample(adc_conf *conf){
return (conf->down_sample);
}
int adc_conf_set_control_string(adc_conf *conf, unsigned int control_string){
conf->control_string = control_string;
}
unsigned int adc_conf_get_control_string(adc_conf *conf){
return (conf->control_string);
}
int adc_conf_set_data_file_size(adc_conf *conf, unsigned int data_file_size){
conf->num_blocks = data_file_size;
}
unsigned int adc_conf_get_data_file_size(adc_conf *conf){
return (conf->num_blocks);
}
int default_daq_init(adc_conf *conf) {
adc_conf_set_cycles_per_clock(conf, ADS_CLOCKS);
adc_conf_set_mode(conf, STOP);
adc_conf_set_down_sample(conf, ADS_SAMPLE_DIVISOR);
adc_conf_set_control_string(conf, ADS_CONTROL_STRING);
adc_conf_set_block_size(conf, BUFSIZE); // Default block size is 128k
adc_conf_set_data_file_size(conf, DAQ_FILE_SIZE);
adc_conf_set_address(conf);
adc_conf_set_size(conf);
}
int adc_conf_settings(adc_conf *conf) {
return (OK);
}
int main(int argc, char **argv)
{
char in_char = '0';
struct adc_conf *conf;
unsigned int *sample;
void *addr;
int read_fd;
int i = 0;
conf = malloc(sizeof (adc_conf));
sample = malloc(sizeof(unsigned int) * 8);
if(getuid()!=0){
printf("You must run this program as root. Exiting.\n");
exit(EXIT_FAILURE);
}
// Initialize structure used by prussdrv_pruintc_intc
// PRUSS_INTC_INITDATA is found in pruss_intc_mapping.h
tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
// Read in the location and address of PRU shared memory. This value changes
// each time a new block of memory is allocated.
default_daq_init((adc_conf *) conf);
// data for setting up the ADC on PRU0.
adc_pru_data0[0] = adc_conf_get_cycles_per_clock ((adc_conf *)conf);
adc_pru_data0[1] = adc_conf_get_mode ((adc_conf *)conf);
adc_pru_data0[2] = adc_conf_get_control_string ((adc_conf *)conf);
adc_pru_data0[3] = CHANGE; // First time through, set to reflect changed.
adc_pru_data0[4] = adc_conf_get_address((adc_conf *)conf);
adc_pru_data0[5] = adc_conf_get_size((adc_conf *)conf);
adc_pru_data0[6] = adc_conf_get_down_sample ((adc_conf *)conf);
adc_pru_data0[7] = adc_conf_get_data_file_size ((adc_conf *)conf);
// data for setting up the ADC on PRU1.
adc_pru_data1[0] = adc_conf_get_cycles_per_clock ((adc_conf *)conf);
adc_pru_data1[1] = adc_conf_get_mode ((adc_conf *)conf);
adc_pru_data1[2] = adc_conf_get_control_string ((adc_conf *)conf);
adc_pru_data1[3] = CHANGE; // First time through, set to reflect changed.
adc_pru_data1[4] = adc_conf_get_address((adc_conf *)conf);
adc_pru_data1[5] = adc_conf_get_size((adc_conf *)conf);
adc_pru_data1[6] = adc_conf_get_down_sample ((adc_conf *)conf);
adc_pru_data1[7] = adc_conf_get_data_file_size ((adc_conf *)conf);
for (i = 8; i < 32; i ++)
adc_pru_data1[i] = 0;
printf("PRU1 Clock Counter %d \n", adc_pru_data1[0]);
printf("PRU1 ADC State %d \n", adc_pru_data1[1]);
printf("ADC1 Control String %x \n", adc_pru_data1[2]);
printf("ADC1 Control String Modified %d \n", adc_pru_data1[3]);
printf("PRU Shared Memory Address Pool: 0x%x\n", adc_pru_data1[4]);
printf("PRU Shared Memory Pool Size: 0x%x\n", adc_pru_data1[5]);
printf("Sample Divisor: 0x%x\n", adc_pru_data1[6]);
printf("Number of 128k blocks to record 0x%x\n", adc_pru_data1[7]);
// Allocate and initialize memory
prussdrv_init ();
prussdrv_open (PRU_EVTOUT_0);
// Write the address and size into PRU0 Data RAM0.
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, 32);
// Map PRU's interrupts
prussdrv_pruintc_init(&pruss_intc_initdata);
// Load and execute the PRU program on the PRU
prussdrv_exec_program (PRU1_ADC_CONTROL_PRU, "./pruadc1.bin");
printf("ADC Control PRU1 program running on PRU %x \n", PRU1_ADC_CONTROL_PRU);
//prussdrv_exec_program (PRU0_ADC_CONTROL_PRU, "./pruadc0.bin");
//printf("ADC Control PRU0 program running on PRU %x \n", PRU0_ADC_CONTROL_PRU);
if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
printf("Failed to open memory!");
return (ERR);
}
while (in_char != 'q') {
printf("Enter Command : \n");
scanf( " %c", &in_char);
switch (in_char) {
// Write the file to a binary file. It cannot be opened to look at
// and will require postprocessing.
case 'b' :
printf("Binary data collection to file \n");
printf("Data collection started \n");
adc_pru_data0[1] = RUN;
adc_pru_data0[3] = CHANGE;
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
// prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, adc_pru_data0, ADC_PRU_DATA_SIZE);
data_to_bin_file (conf);
break;
case 'c' :
read_and_average_cal_data( );
break;
// Write the file in a formatted hex value, with space separation
// between each of the channels in a sample so that the file is
// human readable for quick looks.
case 'f' :
printf("Formatted data collection \n");
printf("Data collection started \n");
adc_pru_data1[1] = RUN;
adc_pru_data1[3] = CHANGE;
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
//prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, adc_pru_data0, ADC_PRU_DATA_SIZE);
write_file_formatted ((adc_conf *)conf, 1);
//write_file_formatted ((adc_conf *)conf, 0);
break;
case 'h' :
printf("Data collection halted \n");
adc_pru_data1[1] = STOP;
adc_pru_data1[3] = CHANGE;
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, adc_pru_data0, ADC_PRU_DATA_SIZE);
break;
// Get a single sample, print it to the screen
case 'o':
printf("getting a single sample \n");
adc_pru_data1[1] = RUN;
adc_pru_data1[3] = CHANGE;
adc_conf_set_block_size((adc_conf *)conf, 32);
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
sampleGet((adc_conf *)conf, 1, sample);
break;
// Write the file as binary. Indicate to PRU1 that this is a throughput test.
// The data from PRU1 will contain a 24 bit counter which is incremented for
// each sample.`
case 't' :
printf("Throughput Test Mode, Binary data collection to file \n");
printf("Data generation started \n");
adc_pru_data1[1] = TEST;
adc_pru_data1[3] = CHANGE;
addr = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, adc_pru_data1[4] & ~MAP_MASK);
bzero((void *) addr, (size_t) (PRU1_START_OFFSET + BUF_HDR_SIZE));
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
data_to_bin_file ((adc_conf *)conf);
break;
case 's' :
printf("Start PRU Data collection, and return \n");
adc_pru_data1[1] = TEST;
adc_pru_data1[3] = CHANGE;
prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
break;
case 'p' :
get_buff_stats((adc_conf *)conf);
break;
default :
printf("Command not recognized %c \n", in_char);
}
}
prussdrv_exit ();
}
// Write data to a file so that the hex info can be looked at raw.
// Formatting makes it easy to look at hex values of each channel per sample.
// Also, this operation takes a relatively small set of samples, as opposed to
// a longer duration continuous recording.
int write_file_formatted (adc_conf *conf, int pru_num)
{
int read_fd;
FILE *write_fd;
void *map_base, *virt_addr;
unsigned long read_result;
int result;
unsigned int addr = conf->addr;
unsigned int size = conf->size;
if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
printf("Failed to open memory!");
return (ERR);
}
printf("Memory file open successfully \n");
if (pru_num == 1) {
if((write_fd = fopen("/home/erps/data/outfile1.dat", "w")) == NULL){
printf("Failed to open data file \n");
return (ERR);
addr += PRU1_START_OFFSET;
printf("waiting on interrupt \n");
prussdrv_pru_wait_event (PRU_EVTOUT_0);
}
} else if (pru_num == 0) {
if((write_fd = fopen("/home/erps/data/outfile0.dat", "w")) == NULL){
printf("Failed to open data file \n");
return (ERR);
addr += PRU0_START_OFFSET;
}
} else {
printf("Invalid PRU %d \n", pru_num);
return(ERR);
}
printf("Data file open successfully \n");
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, addr & ~MAP_MASK);
if(map_base == (void *) -1) {
printf("Failed to map base address");
return (ERR);
}
printf("Reading data \n");
int i = 0;
int j = 0;
for(i = 0; i < size; i+= (8 * BYTES_PER_SAMPLE)){
for (j = 0; j < 8; j++) {
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
fprintf( write_fd, "0x%08x ", read_result);
addr += BYTES_PER_SAMPLE;
}
fprintf(write_fd, "\n");
}
if ((result = fclose (write_fd)) != 0) {
printf("Error closing data file %d \n", result);
}
return (OK);
}
int sampleGet (adc_conf *conf, int pru_num, uint32_t *sample)
{
int read_fd;
FILE *write_fd;
void *map_base, *virt_addr;
unsigned long read_result;
int result;
unsigned int addr = conf->addr;
if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
printf("Failed to open memory!");
return (ERR);
}
printf("Memory file open successfully \n");
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, conf->addr & ~MAP_MASK);
if(map_base == (void *) -1) {
printf("Failed to map base address");
return (ERR);
}
printf("Reading data \n");
int i = 0;
int j = 0;
for(i = 0; i < conf->size; i+= (8 * BYTES_PER_SAMPLE)){
for (j = 0; j < 8; j++) {
virt_addr = map_base + (addr & MAP_MASK);
//read_result = *((uint32_t *) virt_addr);
*sample = *((uint32_t *) virt_addr);
sample ++;
printf( "0x%08x ", sample);
addr += BYTES_PER_SAMPLE;
}
fprintf(write_fd, "\n");
}
return (OK);
}
int get_buff_stats(adc_conf *conf)
{
int i, buffs, buff_head_ptr;
int read_fd;
void *map_base, *virt_addr;
unsigned long read_result;
unsigned int addr = conf->addr;
unsigned int pru1_wr_flag = addr;
unsigned int pru1_buff_count = addr + 4;
unsigned int pru1_head = addr + 8;
unsigned int pru1_wrap_count = addr +12;
unsigned int pru0_wr_flag = addr + 0x80;
unsigned int pru0_buff_count = addr + 0x84;
unsigned int pru0_head = addr + 0x88;
unsigned int pru0_wrap_count = addr + 0x92;
if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
printf("Failed to open memory!");
return (ERR);
}
printf("Memory file open successfully \n");
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, addr & ~MAP_MASK);
if(map_base == (void *) -1) {
printf("Failed to map base address");
return (ERR);
}
virt_addr = map_base + (pru1_wr_flag & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU1_WR_FLAG = %x ", read_result);
virt_addr = map_base + (pru1_buff_count & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU1_BUFF_COUNT = %x ", read_result);
addr += 4;
virt_addr = map_base + (pru1_head & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU1_HEAD = %x ", read_result);
virt_addr = map_base + (pru1_wrap_count & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU1_WRAP_COUNT = %x \n", read_result);
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU0_WR_FLAG = %x ", read_result);
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU0_BUFF_COUNT = %x ", read_result);
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU0_HEAD = %x ", read_result);
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf ("PRU0_WRAP_COUNT = %x \n\n", read_result);
return (OK);
}
int tail_increment(int tail, int num)
{
tail += num;
if (tail == QUEUE_SIZE)
tail = 0;
return (tail);
}
int create_bind_accept_sock()
{
#define PORT 12345
struct sockaddr_in addr;
int addr_len = sizeof(addr);
int new_sock_fd, server_fd;
int backlog = 5;
new_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if(server_fd == ERR)
{
printf("Error opening socket\n");
return -1;
}
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = 0;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
if(bind(server_fd, (struct sockaddr *)&addr,sizeof(struct sockaddr_in) ) == -1)
{
printf("Error binding socket\n");
return (ERR);
}
printf("Socket Successfully bound to port %u\n", PORT);
listen(server_fd, backlog);
addr_len = sizeof(addr);
new_sock_fd = accept(server_fd, (struct sockaddr *) &addr, (socklen_t*) &addr_len);
if (new_sock_fd < 0)
error("ERROR on accept");
return(new_sock_fd);
}
int send_buffs_to_host (void *virt_addr, int num_buffs_to_write, int sock_fd) {
int32_t bytesToSend = num_buffs_to_write * BUFSIZE;
int32_t send_left = 0;
uint8_t *addr = virt_addr;
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
while (bytesToSend > 0 || (setsockopt(sock_fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) > 0))
{
if ((send_left = send(sock_fd, (uint8_t*)addr, bytesToSend, 0)) < 0)
{
printf("ERROR: Failed to send buffer contents across TCPIP.\n");
return(ERR);
}
bytesToSend -= send_left;
addr += send_left;
}
}
process_buffers(void *virt_addr, int num_buffs_to_write, int sock_fd, FILE *write_fd, int8_t flash_record, int8_t network_send)
{
uint32_t wr_num;
if (flash_record == ENABLED)
wr_num = write_buffs_to_flash (virt_addr, num_buffs_to_write, write_fd);
if (network_send == ENABLED)
send_buffs_to_host (virt_addr, num_buffs_to_write, sock_fd);
}
int write_buffs_to_flash (void *virt_addr, int num_buffs_to_write, FILE * write_fd) {
int wr_num = 0;
wr_num = fwrite ( virt_addr, sizeof(uint8_t), num_buffs_to_write * BUFSIZE, write_fd);
if (wr_num != num_buffs_to_write * BUFSIZE)
{
printf ("Error writing flash file %d \n", errno);
strerror(errno);
}
return (wr_num);
}
int data_to_bin_file (adc_conf *conf)
{
int i, j, k;
int buffs = 0;
unsigned long read_result;
int read_fd;
FILE *write_fd;
void *map_base, *virt_addr;
void *pru1_wr_flag_addr;
void *pru1_buff_count_addr;
void *pru1_head_addr;
void *pru1_wrap_count_addr;
int wr_num = 0;
int tail = 0;
int num_buffs = 0;
int num_buffs_to_write = 0;
int last_count = 0;
int num_buffs_processed = 0;
volatile unsigned long pru1_wr_flag = 0;
volatile unsigned long pru1_buff_count = 0;
volatile unsigned long pru1_head = 0;
volatile unsigned long pru1_wrap_count = 0;
unsigned int addr = conf->addr;
unsigned int pru1_conf_addr = conf->addr;
int iteration = 0;
int client_sock_fd = 0;
int network_send = TRUE;
int flash_record = TRUE;
if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
printf("Failed to open memory!");
return (ERR);
}
printf("Memory file open successfully \n");
write_fd = fopen("/home/erps/data/outfile.bin", "wb");
if( write_fd == NULL){
printf("Failed to open data file \n");
return (ERR);
}
printf("Data file open successfully \n");
client_sock_fd = create_bind_accept_sock();
if (client_sock_fd != ERR)
printf("Socket Successfully created \n");
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, addr & ~MAP_MASK);
if(map_base == (void *) -1) {
printf("Failed to map base address");
return (ERR);
}
else {
printf("Base Address = %x \n", map_base);
}
#if DEBUG
printf("Dumping header data \n");
i = 0;
j = 0;
for(i = 0; i < (PRU1_START_OFFSET + 256); i+= (8 * BYTES_PER_SAMPLE)){
for (j = 0; j < 8; j++) {
virt_addr = map_base + (addr & MAP_MASK);
read_result = *((uint32_t *) virt_addr);
printf( "0x%08x ", read_result);
addr += BYTES_PER_SAMPLE;
}
printf("\n");
}
#endif
pru1_conf_addr = conf->addr;
addr = conf->addr + PRU1_START_OFFSET;
pru1_wr_flag_addr = map_base + ((pru1_conf_addr + PRU1_WR_FLAG) & MAP_MASK);
pru1_wr_flag = *((uint32_t *) pru1_wr_flag_addr);
pru1_buff_count_addr = map_base + ((pru1_conf_addr + PRU1_BUFF_COUNT) & MAP_MASK);
pru1_buff_count = *((uint32_t *) pru1_buff_count_addr);
pru1_head_addr = map_base + ((pru1_conf_addr + PRU1_HEAD) & MAP_MASK);
pru1_head = *((uint32_t *) pru1_head_addr);
pru1_wrap_count_addr = map_base + ((pru1_conf_addr + PRU1_WRAP_COUNT) & MAP_MASK);
pru1_wrap_count = *((uint32_t *) pru1_wrap_count_addr);
tail = 0;
//tail = 4; // this is for test purposes only
while (num_buffs_processed < DAQ_FILE_SIZE)
{
virt_addr = map_base + ((pru1_conf_addr + PRU1_WR_FLAG) & MAP_MASK);
pru1_wr_flag = *((uint32_t *) pru1_wr_flag_addr);
pru1_buff_count_addr = map_base + ((pru1_conf_addr + PRU1_BUFF_COUNT) & MAP_MASK);
pru1_buff_count = *((uint32_t *) pru1_buff_count_addr);
pru1_head_addr = map_base + ((pru1_conf_addr + PRU1_HEAD) & MAP_MASK);
pru1_head = *((uint32_t *) pru1_head_addr);
pru1_wrap_count_addr = map_base + ((pru1_conf_addr + PRU1_WRAP_COUNT) & MAP_MASK);
pru1_wrap_count = *((uint32_t *) pru1_wrap_count_addr);
if (pru1_wr_flag == 1)
{
num_buffs = pru1_buff_count - last_count;
if (num_buffs > 0)
{
printf("h = %d, t = %d, num_buffs = %d ", pru1_head, tail, num_buffs);
}
if (num_buffs <= QUEUE_SIZE)
{
if (tail < pru1_head)
{
num_buffs_to_write = pru1_head - tail;
virt_addr = map_base + (addr & MAP_MASK);
process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
num_buffs_processed += num_buffs_to_write;
addr += num_buffs_to_write * BUFSIZE;
tail = tail_increment(tail, num_buffs_to_write);
pru1_wr_flag = 0;
}
else if (tail > pru1_head)
{
// Write the buffers from the tail to the end of the queue.
virt_addr = map_base + (addr & MAP_MASK);
num_buffs_to_write = QUEUE_SIZE - tail;
process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
addr += num_buffs_to_write * BUFSIZE;
tail = tail_increment(tail, num_buffs_to_write);
num_buffs_processed += num_buffs_to_write;
// Write the buffers from the beginning of the queue (zeroth element) to the head
virt_addr = map_base + (addr & MAP_MASK);
num_buffs_to_write = pru1_head;
process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
addr += num_buffs_to_write * BUFSIZE;
num_buffs_processed += num_buffs_to_write;
tail = tail_increment(tail, num_buffs_to_write);
}
else if ((tail == pru1_head) && (num_buffs == QUEUE_SIZE))
{
// Write the buffers from the tail to the end of the queue.
virt_addr = map_base + (addr & MAP_MASK);
num_buffs_to_write += QUEUE_SIZE - tail;
process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
num_buffs_processed += num_buffs_to_write;
addr += num_buffs_to_write * BUFSIZE;
tail = tail_increment(tail, num_buffs_to_write);
// Write the buffers from the beginning of the queue to the head
virt_addr = map_base + (addr & MAP_MASK);
num_buffs_to_write = pru1_head;
process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
addr += num_buffs_to_write * BUFSIZE;
num_buffs_processed += num_buffs_to_write;
tail = tail_increment(tail, num_buffs_to_write); // This may look funny, but we are incrementing tail by tail.
}
pru1_wr_flag = 0;
if (num_buffs > 0)
{
printf("num_buffs_processed = %d \n", num_buffs_processed);
}
last_count = pru1_buff_count;
usleep(1000);
}
else if (num_buffs > QUEUE_SIZE)
{
printf("Queue overflow %d \n", num_buffs);
}
}
}
printf("TOTAL num_buffs_processed = %d \n", num_buffs_processed);
fclose ((FILE *)write_fd);
return (OK);
}
read_and_average_cal_data( ) {
FILE *read_fd;
int i = 0;
int j = 0;
uint32_t s[8];
uint32_t total[8];
uint32_t average[8];
read_fd = fopen("/home/erps/caldata/5Vcal_with2p2res.dat", "r");
if( read_fd == NULL){
printf("Failed to open data file \n");
return (ERR);
}
else
printf("Cal File Opened \n");
for (i = 0; i < 8; i ++){
fscanf(read_fd, "%x %x %x %x %x %x %x %x \n", &s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6], &s[7]);
// printf("%x %x %x %x %x %x %x %x \n", s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
}
for (i = 0; i < 8; i ++) {
total[i] = 0;
s[i] = 0;
average[i] = 0;
}
for (i = 0; i < 100; i ++) {
for (j = 0; j < 8; j++) {
fscanf(read_fd, "%x", &s[j]);
s[j] &= s[j] & 0x0FFFFFF;
total[j] += s[j];
}
fscanf(read_fd, "\n");
printf("%x %x %x %x %x %x %x %x \n",s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
printf("%x %x %x %x %x %x %x %x \n",total[0], total[1], total[2], total[3], total[4], total[5], total[6], total[7]);
}
for (i = 0; i < 8; i ++)
{
average[i] = total[i] / 100;
}
printf("%x %x %x %x %x %x %x %x \n",average[0], average[1], average[2], average[3], average[4], average[5], average[6], average[7]);
fclose ((FILE *)read_fd);
}
+148
View File
@@ -0,0 +1,148 @@
#define CLOCK_CNTR_ADDR 0x00000000
#define ADC_STATE_ADDR 0x00000004
#define ADC_CNTRL_STR_ADDR 0x00000008
#define ADC_CNTRL_CHANGE 0x0000000C
#define ADC_ADDR 0x00000010
#define ADC_SIZE 0x00000014
#define ADC_SAMPLE_DIVISOR 0x00000018
#define PRU1_NUM_BLOCKS 0x0000001C
#define PRU0_NUM_BLOCKS 0x00000020
#define BLOCK_SIZE_ADDR 0x00000024
#define PRU1_WR_FLAG 0x00000028
#define PRU1_BUFF_COUNT 0x0000002C
#define PRU1_HEAD 0x00000030
#define PRU1_WRAP_COUNT 0x00000034
#define PRU0_WR_FLAG 0x00000038
#define PRU0_BUFF_COUNT 0x0000003C
#define PRU0_HEAD 0x00000040
#define PRU0_WRAP_COUNT 0x00000044
// This map documents pin mapping to the ADS1287, and the associated registers
#define PWDN_B_CH_1_4 r30.t6 // Pin 8.39
#define PWDN_C_CH_1_4 r30.t7 // Pin 8.40
#define PWDN_B_CH_5_8 r30.t4 // Pin 8.41
#define PWDN_D_CH_1_4 r30.t5 // Pin 8.42
#define PWDN_A_CH_5_8 r30.t2 // Pin 8.43
#define PWDN_D_CH_5_8 r30.t3 // Pin 8.44
#define PWDN_A_CH_1_4 r30.t0 // Pin 8.45
#define PWDN_C_CH_5_8 r30.t1 // Pin 8.46
#define DELAY_PULSES 0x10000
// SPI Interface to AD1278 B & D vi PRU1
#define SPI_CLK_B_D_PIN R30.t8 // Pin 8.27
#define SPI_DSR_B_D_PIN R31.t10 // Pin 8.28
#define SPI_DATA_IN_B_D_PIN R31.t11 // Pin 8.30
#define SPI_SYNC_PIN R30.t9 // Pin 8.29
// SPI Interface to AD1278 A & C vi PRU0
#define SPI_CLK_A_C_PIN R30.t1 // Pin 9.29
#define SPI_DSR_A_C_PIN R31.t5 // Pin 9.27
#define SPI_DATA_IN_A_C_PIN R31.t3 // Pin 9.28
#define PWDN_B_1_4_BIT 0x1 // Pin 8.39
#define PWDN_C_1_4_BIT 0x2 // Pin 8.40
#define PWDN_B_5_8_BIT 0x4 // Pin 8.41
#define PWDN_D_1_4_BIT 0x8 // Pin 8.42
#define PWDN_A_5_8_BIT 0x10 // Pin 8.43
#define PWDN_D_5_8_BIT 0x20 // Pin 8.44
#define PWDN_A_1_4_BIT 0x40 // Pin 8.45
#define PWDN_C_5_8_BIT 0x80 // Pin 8.46
// Register Use
// R0 General Purpose / Indexing
// R1 General Purpose
#define CLOCK_COUNT r2 // Clock Counter used to determine SCLOCK frequency
#define RUN_STATE r3 // Run State - can turn off data acq via this
#define ADC_CNTRL_STR r4 // ADC Control string used to enable/disable daq channels and daq
#define ADC_CHNG_FLG r5 // ADC Control Change flag used to indicate a settings change
#define SMPL_BIT_CNTR r6 // Sample bit counter, used to count number of bits read
#define CHAN_BITMASK r7 // Channel bitmask used to append channel in MSB of the sample
#define SHR_MEM_PTR r8 // Pointer to the shared memory buffer used to store data
#define SHR_MEM_SZ r9 // Size of shared memory buffer
#define COUNT r10 // Sample count
#define HEAD r11 // Buffer head
#define TAIL r12 // Buffer Tail
#define PRU_DATA_STRT r13 // We store a pointer to the beginning of buffer space here
#define CURRENT_SAMPLE r14
#define CURRENT_BUF r15 // Current 128K buffer we are writing to (0 - 9)
#define CUR_BUF_ADRS r16 // Memory address of current 128K buffer
#define CUR_BUF_LEFT r17 // This is the amount left of the current buffer in bytes
#define SHR_MEM_START r18 // Address of shared memory region
#define NUM_BLOCKS r19 // Number of 128k blocks we are going to record
#define BUFF_COUNT r20
#define SAMPLE1 r21
#define SAMPLE2 r22
#define SAMPLE3 r23
#define SAMPLE4 r24
#define SAMPLE5 r25
#define SAMPLE6 r26
#define SAMPLE7 r27
#define SAMPLE8 r28
#define WRAP_COUNT r29
// R30 and R31 are input/output registers, do not use for general purpose regs.
#define BUFSIZE 0x20000 // 128K is optimal flash write size for bbone.
#define SHARED_MEM_SIZE 0x100000
#define QUEUE_SIZE 8 // Number of buffers of BUFSIZE in the queue
#define DAQ_FILE_SIZE 2000 // Number of 128k blocks.
//#define DAQ_FILE_SIZE 0x40 // Number of 128k blocks.
#define PRU1_START_OFFSET 0x00000100
#define PRU0_START_OFFSET 0x00100000
#define FULL_COUNT 0x00FFFFFF
#define BUF_HDR_SIZE 32 // 32 Byte header contained at start of 128k buffer
#define SAMPLE_SIZE 24
#define ADS_CONTROL_STRING 0x1FF // Banks A enabled
#define ADS_SAMPLE_DIVISOR 0x1
#define ADS_CLOCKS 0x0E /// Number of instructions to wait between toggling clock high to low.
//#define ADS_CLOCKS 0x6 /// Number of instructions to wait between toggling clock high to low.
#define PRU0_ADC_CONTROL_PRU 0 // PRU 0 controls ADC2 and ADC3
#define PRU1_ADC_CONTROL_PRU 1 // PRU 1 controls power up/down of ads1278 channels, ADC0 and ADC1
#define BYTES_PER_SAMPLE 4
#define ADS_BOARD_ENABLE 0x100
#define ADC_PRU_DATA_SIZE 128
#define INSTRUCTIONS_PER_SAMPLE 0x02509 //Roughly the number of delay count to be equal to one minute
#define OK 1
#define ERR -1
#define MAP_SIZE 0x0FFFFFFF
#define MAP_MASK (MAP_SIZE - 1)
#define MMAP0_LOC "/sys/class/uio/uio0/maps/map0/"
#define MMAP1_LOC "/sys/class/uio/uio0/maps/map1/"
#define PRU0_R31_VEC_VALID 32 // allows notification of program completion
#define PRU_EVTOUT_0 0 // the event number that is sent back
#define STOP 0
#define RUN 1
#define TEST 2
#define CHANGE 1
#define NO_CHANGE 0
#define TRUE 1
#define FALSE 0
#define ENABLED 1
Binary file not shown.
+425
View File
@@ -0,0 +1,425 @@
.origin 0 // start of program in PRU memory
.entrypoint START // program entry point for the debugger
//Copyright Doug Lewis Sept 2017
#include <./pruadc.h>
START:
// Clear clock outputs to the ADS 1278
CLR SPI_CLK_A_C_PIN
// Clear all registers we are going to use, since we know that they can come up
// with garbage in them
MOV r0, 0
MOV r1, 0
MOV CLOCK_COUNT, 0 // R2
MOV RUN_STATE, 0 // R3
MOV ADC_CNTRL_STR, 0 // R4
MOV ADC_CHNG_FLG, 0 // R5
MOV SMPL_BIT_CNTR, 0 // R6
MOV CHAN_BITMASK, 0 // R7
MOV SHR_MEM_PTR, 0 // R8
MOV SHR_MEM_SZ, 0 // R9
MOV COUNT, 0 // R10
MOV HEAD, 0 // R11
MOV TAIL, 0 // R12
MOV PRU_DATA_STRT, 0 // R13
MOV CURRENT_SAMPLE, 0 // R14
MOV CURRENT_BUF, 0 // R15
MOV CUR_BUF_ADRS, 0 // R16
MOV CUR_BUF_LEFT, 0 // R17
MOV SHR_MEM_START, 0 // R18
MOV NUM_BLOCKS, 0 // R19
MOV BUFF_COUNT, 0 // R20
MOV SAMPLE1 , 0 // R21
MOV SAMPLE2 , 0 // R22
MOV SAMPLE3 , 0 // R23
MOV SAMPLE4 , 0 // R24
MOV SAMPLE5 , 0 // R25
MOV SAMPLE6 , 0 // R26
MOV SAMPLE7 , 0 // R27
MOV SAMPLE8 , 0 // R28
MOV WRAP_COUNT, 0 // R29
// Enable the OCP master port -- allows transfer of data to Linux userspace
LBCO r0, C4, 4, 4 // load PRU-ICSS CFG reg into r0
CLR r0, r0, 4 // clear bit 4 (STANDBY_INIT)
SBCO r0, C4, 4, 4 // store the modified r0 back at the load addr
START_LOOP: // This is an easy place to halt the debugger
MOV r1,ADC_STATE_ADDR
LBBO RUN_STATE, r1, 0, 4 // the daq state is now loaded into r3.
QBEQ START_LOOP, RUN_STATE, 0 // We hang out in a loop until told to read the adc
MOV r1,ADC_ADDR //
LBBO SHR_MEM_PTR, r1, 0, 4 // load the Linux address that is passed into r8 -- to store sample values
MOV R1, PRU0_START_OFFSET
ADD PRU_DATA_STRT, SHR_MEM_START, R1
MOV SHR_MEM_PTR, PRU_DATA_STRT
MOV r1,ADC_SIZE //
LBBO SHR_MEM_SZ, r1, 0, 4 // load the size that is passed into r9 -- the number of samples to take
MOV r1,PRU0_NUM_BLOCKS //
LBBO NUM_BLOCKS, r1, 0, 4 // load the count of how many 128K blocks we are going to record
MEM_BUF_ALLOCATED:
MOV CUR_BUF_LEFT, BUFSIZE
MOV r1,ADC_CNTRL_CHANGE // load the base address into r1
LBBO ADC_CHNG_FLG, r1, 0, 4 // the ADC_CNTL_CHANGE is now loaded into R5
QBBS CONFIG_CHANGE, ADC_CHNG_FLG.t0 // If bit 1 of ADC_CTRL_CHANGE is set, there is a config
//change
QBA CONFIG_DONE // Else no changes, goto CONFIG_DONE
CONFIG_CHANGE:
MOV r1, CLOCK_CNTR_ADDR
LBBO CLOCK_COUNT, r1, 0, 4 // the clock delay is now loaded into r2.
MOV r1,ADC_CNTRL_STR_ADDR
LBBO ADC_CNTRL_STR, r1, 0, 4 // the ADC_CNTL_STR is now loaded into R4
CLR ADC_CHNG_FLG.t1 // Clear the change flag so we dont do the config every loop
CONFIG_DONE:
//We want to look at the first bit in the config bitmask.
READ_NEXT_SAMPLE:
MOV r1,ADC_STATE_ADDR //Check to see if we are still need to be running
LBBO RUN_STATE, r1, 0, 4
QBEQ CONTINUE_DAQ, RUN_STATE, 1
HALT // We've been told to stop.
CONTINUE_DAQ:
MOV SAMPLE1, 0
MOV SAMPLE2, 0
MOV SAMPLE3, 0
MOV SAMPLE4, 0
MOV SAMPLE5, 0
MOV SAMPLE6, 0
MOV SAMPLE7, 0
MOV SAMPLE8, 0
// We check data ready to make sure the ADC has had a chance to initialize and set dready high before sampling.
DATA_READY_HIGH:
QBBS DATA_READY_WAIT, SPI_DSR_A_C_PIN
QBA DATA_READY_HIGH
DATA_READY_WAIT:
QBBC DATA_READY, SPI_DSR_A_C_PIN // If Data Ready line clear, read a sample
QBA DATA_READY_WAIT // Else hang out in a tight loop ...
DATA_READY:
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL1:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_A_C_PIN // set the clock line to be high
DELAYON1:
SUB r0, r0, 1
QBNE DELAYON1, r0, 0
CLR SPI_CLK_A_C_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT1, SPI_DATA_IN_A_C_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE1, SAMPLE1, 0x00000001
ZERO_BIT1:
LSL SAMPLE1, SAMPLE1, 1 // Shift current sample contents left by one
MOV r0, CLOCK_COUNT
DELAYOFF1:
SUB r0, r0, 1
QBNE DELAYOFF1, r0, 0 // loop until the delay has expired (equals 0)
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
QBNE READ_CHANNEL1, SMPL_BIT_CNTR, 0
//////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL2:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_A_C_PIN // set the clock to be high
DELAYON2:
SUB r0, r0, 1
QBNE DELAYON2, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_A_C_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT2, SPI_DATA_IN_A_C_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE2, SAMPLE2, 0x00000001
ZERO_BIT2:
LSL SAMPLE2, SAMPLE2, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF2:
SUB r0, r0, 1
QBNE DELAYOFF2, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL2, SMPL_BIT_CNTR, 0
//////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL3:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_A_C_PIN // set the clock to be high
DELAYON3:
SUB r0, r0, 1
QBNE DELAYON3, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_A_C_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT3, SPI_DATA_IN_A_C_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE3, SAMPLE3, 0x00000001
ZERO_BIT3:
LSL SAMPLE3, SAMPLE3, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF3:
SUB r0, r0, 1
QBNE DELAYOFF3, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL3, SMPL_BIT_CNTR, 0
/////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL4:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_A_C_PIN // set the clock to be high
DELAYON4:
SUB r0, r0, 1
QBNE DELAYON4, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_A_C_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT4, SPI_DATA_IN_A_C_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE4, SAMPLE4, 0x00000001
ZERO_BIT4:
LSL SAMPLE4, SAMPLE4, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF4:
SUB r0, r0, 1
QBNE DELAYOFF4, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL4, SMPL_BIT_CNTR, 0
/////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL5:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_A_C_PIN // set the clock to be high
DELAYON5:
SUB r0, r0, 1
QBNE DELAYON5, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_A_C_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT5, SPI_DATA_IN_A_C_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE5, SAMPLE5, 0x00000001
ZERO_BIT5:
LSL SAMPLE5, SAMPLE5, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF5:
SUB r0, r0, 1
QBNE DELAYOFF5, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL5, SMPL_BIT_CNTR, 0
/////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL6:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_A_C_PIN // set the clock to be high
DELAYON6:
SUB r0, r0, 1
QBNE DELAYON6, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_A_C_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT6, SPI_DATA_IN_A_C_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE6, SAMPLE6, 0x00000001
ZERO_BIT6:
LSL SAMPLE6, SAMPLE6, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF6:
SUB r0, r0, 1
QBNE DELAYOFF6, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL6, SMPL_BIT_CNTR, 0
/////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL7:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_A_C_PIN // set the clock to be high
DELAYON7:
SUB r0, r0, 1
QBNE DELAYON7, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_A_C_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT7, SPI_DATA_IN_A_C_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE7, SAMPLE7, 0x00000001
ZERO_BIT7:
LSL SAMPLE7, SAMPLE7, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF7:
SUB r0, r0, 1
QBNE DELAYOFF7, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL7, SMPL_BIT_CNTR, 0
/////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL8:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_A_C_PIN // set the clock to be high
DELAYON8:
SUB r0, r0, 1
QBNE DELAYON8, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_A_C_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT8, SPI_DATA_IN_A_C_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE8, SAMPLE8, 0x00000001
ZERO_BIT8:
LSL SAMPLE8, SAMPLE8, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
MOV r0, CLOCK_COUNT
DELAYOFF8:
SUB r0, r0, 1
QBNE DELAYOFF8, r0, 0 // loop until the delay has expired (equals 0)
QBNE READ_CHANNEL8, SMPL_BIT_CNTR, 0
/////////////////////////
STORE_SAMPLE:
LSR SAMPLE1, SAMPLE1, 1 // Need to shift the sample word back to the right by one
LSR SAMPLE2, SAMPLE2, 1
LSR SAMPLE3, SAMPLE3, 1
LSR SAMPLE4, SAMPLE4, 1
LSR SAMPLE5, SAMPLE5, 1
LSR SAMPLE6, SAMPLE6, 1
LSR SAMPLE7, SAMPLE7, 1
LSR SAMPLE8, SAMPLE8, 1
MOV SAMPLE1.b3, 0x01 // Stash the channel bitmask in the upper byte used to store the sample
MOV SAMPLE2.b3, 0x02
MOV SAMPLE3.b3, 0x04
MOV SAMPLE4.b3, 0x08
MOV SAMPLE5.b3, 0x10
MOV SAMPLE6.b3, 0x20
MOV SAMPLE7.b3, 0x40
MOV SAMPLE8.b3, 0x80
SBBO SAMPLE1, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE2, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE3, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE4, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE5, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE6, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE7, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE8, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SUB CUR_BUF_LEFT, CUR_BUF_LEFT, 32 // reducing the number of samples - 4 bytes per sample, 8 channels
QBEQ BUFFER_DONE, CUR_BUF_LEFT, 0 // See if we have taken 128kb of samples
QBA READ_NEXT_SAMPLE // If we've looped thru all channels in the sample. wait for the next sample
BUFFER_DONE:
MOV CUR_BUF_LEFT, BUFSIZE
// If we have recorded all of the buffers that we need to, then halt.
SUB NUM_BLOCKS, NUM_BLOCKS, 1
QBEQ END, NUM_BLOCKS, 0
MOV R1, BUFSIZE
ADD CUR_BUF_ADRS, CUR_BUF_ADRS, R1
MOV r1, SHARED_MEM_SIZE
QBEQ WRAP_TIME, CUR_BUF_ADRS, r1
QBA READ_NEXT_SAMPLE
WRAP_TIME:
// Always wanted to be a rapper ;-0)
// We have used all the shared memory to write samples, wrap back to the beginning.
MOV CUR_BUF_ADRS, 0
MOV r31.b0, PRU0_R31_VEC_VALID | PRU_EVTOUT_0
HALT //DEBUG
QBA READ_NEXT_SAMPLE
// halt the pru program -- we reach here when the file is full.
END:
HALT
Binary file not shown.
+752
View File
@@ -0,0 +1,752 @@
.origin 0 // start of program in PRU memory
.entrypoint START // program entry point for the debugger
// Copyright Doug Lewis Sept 2017
//#include </usr/include/prussdrv.h>
//#include </usr/include/pruss_intc_mapping.h>
#include <./pruadc.h>
START:
// Clear all outputs to the ADS 1278
CLR r30.t0
CLR r30.t1
CLR r30.t2
CLR r30.t3
CLR r30.t4
CLR r30.t5
CLR r30.t6
CLR r30.t7
CLR r30.t8
// Clear all registers we are going to use, since we know that they can come up
// with garbage in them
MOV r0, 0
MOV r1, 0
MOV CLOCK_COUNT, 0 // R2
MOV RUN_STATE, 0 // R3
MOV ADC_CNTRL_STR, 0 // R4
MOV ADC_CHNG_FLG, 0 // R5
MOV SMPL_BIT_CNTR, 0 // R6
MOV CHAN_BITMASK, 0 // R7
MOV SHR_MEM_PTR, 0 // R8
MOV SHR_MEM_SZ, 0 // R9
MOV COUNT, 0 // R10
MOV HEAD, 0 // R11
MOV TAIL, 0 // R12
MOV PRU_DATA_STRT, 0 // R13
MOV CURRENT_SAMPLE, 0 // R14
MOV CURRENT_BUF, 0 // R15
MOV CUR_BUF_ADRS, 0 // R16
MOV CUR_BUF_LEFT, 0 // R17
MOV SHR_MEM_START, 0 // R18
MOV NUM_BLOCKS, 0 // R19
MOV BUFF_COUNT, 0 // R20
MOV SAMPLE1 , 0 // R21
MOV SAMPLE2 , 0 // R22
MOV SAMPLE3 , 0 // R23
MOV SAMPLE4 , 0 // R24
MOV SAMPLE5 , 0 // R25
MOV SAMPLE6 , 0 // R26
MOV SAMPLE7 , 0 // R27
MOV SAMPLE8 , 0 // R28
MOV R29, 0
MOV R30, 0
MOV R31, 0
// Enable the OCP master port -- allows transfer of data to Linux userspace
LBCO r0, C4, 4, 4 // load PRU-ICSS CFG reg into r0
CLR r0, r0, 4 // clear bit 4 (STANDBY_INIT)
SBCO r0, C4, 4, 4 // store the modified r0 back at the load addr
START_LOOP: // This is an easy place to halt the debugger
MOV r1,ADC_STATE_ADDR
LBBO RUN_STATE, r1, 0, 4 // the daq state is now loaded into r3.
QBEQ START_LOOP, RUN_STATE, 0 // We hang out in a loop until told to read the adc
MOV r1,ADC_ADDR
LBBO SHR_MEM_START, r1, 0, 4 // load the Linux address that is passed into r8 -- to store sample values
//Skip the offset bytes that we use for adc_conf at the beginning of the shared memory region
MOV r1, PRU1_START_OFFSET
ADD PRU_DATA_STRT, SHR_MEM_START, R1
MOV SHR_MEM_PTR, PRU_DATA_STRT
MOV r1,ADC_SIZE
LBBO SHR_MEM_SZ, r1, 0, 4 // load the size that is passed into r9 -- the number of samples to take
MOV r1,PRU1_NUM_BLOCKS
LBBO NUM_BLOCKS, r1, 0, 4 // load the count of how many 128K blocks we are going to record
MEM_BUF_ALLOCATED:
MOV CUR_BUF_LEFT, BUFSIZE
MOV r1,ADC_CNTRL_CHANGE // load the base address into r1
LBBO ADC_CHNG_FLG, r1, 0, 4 // the ADC_CNTL_CHANGE is now loaded into R5
QBBS CONFIG_CHANGE, ADC_CHNG_FLG.t0 // If bit 1 of ADC_CTRL_CHANGE is set, there is a config
//change
QBA CONFIG_DONE // Else no changes, goto CONFIG_DONE
CONFIG_CHANGE:
MOV r1, CLOCK_CNTR_ADDR
LBBO CLOCK_COUNT, r1, 0, 4 // the clock delay is now loaded into r2.
MOV r1,ADC_CNTRL_STR_ADDR
LBBO ADC_CNTRL_STR, r1, 0, 4 // the ADC_CNTL_STR is now loaded into R4
CLR ADC_CHNG_FLG.t1 // Clear the change flag so we dont do the config every loop
QBBS SET_PWDN1, ADC_CNTRL_STR.t0
QBA PWDN2
SET_PWDN1:
SET r30.t6 // Pin 39, CH B 1-4
PWDN2:
QBBS SET_PWDN2, ADC_CNTRL_STR.t1
QBA PWDN3
SET_PWDN2:
SET r30.t7 // Pin 40, CH C 1-4
PWDN3:
QBBS SET_PWDN3, ADC_CNTRL_STR.t2
QBA PWDN4
SET_PWDN3:
SET r30.t4 // Pin 41, CH B 5-8
PWDN4:
QBBS SET_PWDN4, ADC_CNTRL_STR.t3
QBA PWDN5
SET_PWDN4:
SET r30.t5 // Pin 42, CH D 1-4
PWDN5:
QBBS SET_PWDN5, ADC_CNTRL_STR.t4
QBA PWDN6
SET_PWDN5:
SET r30.t2 // Pin 43, CH A 5-8
PWDN6:
QBBS SET_PWDN6, ADC_CNTRL_STR.t5
QBA PWDN7
SET_PWDN6:
SET r30.t3 // Pin 44, CHD 5-8
PWDN7:
QBBS SET_PWDN7, ADC_CNTRL_STR.t6
QBA PWDN8
SET_PWDN7:
SET r30.t0 // Pin 45 CH A 1-4
PWDN8:
QBBS SET_PWDN8, ADC_CNTRL_STR.t7
QBA CONFIG_DONE
SET_PWDN8:
SET r30.t1 // Pin 46, CH C 5-8
CONFIG_DONE:
QBEQ THROUGH_TEST, RUN_STATE, TEST // If RUN_STATE = 1, start data collection, If 2, test mode.
// Set the SYNC Pin High
SET SPI_SYNC_PIN
MOV r1, DELAY_PULSES // We are going to delay here for a little bit in order for the 5V and 10V
// rail voltages to ramp after enabling the daqs.
DELAY_START:
// Generate one clock pulse for test purposes
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_B_D_PIN // set the clock line to be high
DELAY_CLOCK_TEST_PULSE:
SUB r0, r0, 1
QBNE DELAY_CLOCK_TEST_PULSE, r0, 0
CLR SPI_CLK_B_D_PIN
SUB r1, r1, 1
QBNE DELAY_START, r1, 0
// sync is active low, so we clear it
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
CLR SPI_SYNC_PIN // CLR the sync pin. Once it goes low, the ADC will start to
// convert data, and the DSR should go high after this.
DELAY_SYNC_PULSE:
SUB r0, r0, 1
QBNE DELAY_SYNC_PULSE, r0, 0
SET SPI_SYNC_PIN
NEXT_BUFFER:
MOV CUR_BUF_LEFT, BUFSIZE
// Move a well known cow into first four bytes of the buffer header.
MOV R1, 0xDEADBEEF
SBBO R1, SHR_MEM_PTR, 0, 4
// Copy the buffer count into the second 4 bytes of the header
SBBO BUFF_COUNT, SHR_MEM_PTR, 4, 4 // store the sample value into shared memory space
//Skip 32 bytes that we will use for the 128k buffer header.
ADD SHR_MEM_PTR, SHR_MEM_PTR, BUF_HDR_SIZE
SUB CUR_BUF_LEFT, CUR_BUF_LEFT, BUF_HDR_SIZE
//We want to look at the first bit in the config bitmask.
READ_NEXT_SAMPLE:
MOV r1,ADC_STATE_ADDR //Check to see if we are still need to be running
LBBO RUN_STATE, r1, 0, 4
QBEQ CONTINUE_DAQ, RUN_STATE, 1
QBA END // We've been told to stop.
CONTINUE_DAQ:
MOV SAMPLE1, 0
MOV SAMPLE2, 0
MOV SAMPLE3, 0
MOV SAMPLE4, 0
MOV SAMPLE5, 0
MOV SAMPLE6, 0
MOV SAMPLE7, 0
MOV SAMPLE8, 0
// We look for DSR to be high
DATA_READY_HIGH:
QBBS DATA_READY_WAIT, SPI_DSR_B_D_PIN
QBA DATA_READY_HIGH
DATA_READY_WAIT:
QBBC DATA_READY, SPI_DSR_B_D_PIN // If Data Ready line clear, read a sample
QBA DATA_READY_WAIT // Else hang out in a tight loop ...
MOV r0, 0
MOV r0, 0
DATA_READY:
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL1:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_B_D_PIN // set the clock line to be high
DELAYON1:
SUB r0, r0, 1
QBNE DELAYON1, r0, 0
CLR SPI_CLK_B_D_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT1, SPI_DATA_IN_B_D_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE1, SAMPLE1, 0x00000001
MOV r0, CLOCK_COUNT
DELAYOFF1:
SUB r0, r0, 1
QBNE DELAYOFF1, r0, 0 // loop until the delay has expired (equals 0)
ZERO_BIT1:
LSL SAMPLE1, SAMPLE1, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
QBNE READ_CHANNEL1, SMPL_BIT_CNTR, 0
//////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL2:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_B_D_PIN // set the clock to be high
DELAYON2:
SUB r0, r0, 1
QBNE DELAYON2, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_B_D_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT2, SPI_DATA_IN_B_D_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE2, SAMPLE2, 0x00000001
MOV r0, CLOCK_COUNT
DELAYOFF2:
SUB r0, r0, 1
QBNE DELAYOFF2, r0, 0 // loop until the delay has expired (equals 0)
ZERO_BIT2:
LSL SAMPLE2, SAMPLE2, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
QBNE READ_CHANNEL2, SMPL_BIT_CNTR, 0
//////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL3:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_B_D_PIN // set the clock to be high
DELAYON3:
SUB r0, r0, 1
QBNE DELAYON3, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_B_D_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT3, SPI_DATA_IN_B_D_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE3, SAMPLE3, 0x00000001
MOV r0, CLOCK_COUNT
DELAYOFF3:
SUB r0, r0, 1
QBNE DELAYOFF3, r0, 0 // loop until the delay has expired (equals 0)
ZERO_BIT3:
LSL SAMPLE3, SAMPLE3, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
QBNE READ_CHANNEL3, SMPL_BIT_CNTR, 0
/////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL4:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_B_D_PIN // set the clock to be high
DELAYON4:
SUB r0, r0, 1
QBNE DELAYON4, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_B_D_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT4, SPI_DATA_IN_B_D_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE4, SAMPLE4, 0x00000001
MOV r0, CLOCK_COUNT
DELAYOFF4:
SUB r0, r0, 1
QBNE DELAYOFF4, r0, 0 // loop until the delay has expired (equals 0)
ZERO_BIT4:
LSL SAMPLE4, SAMPLE4, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
QBNE READ_CHANNEL4, SMPL_BIT_CNTR, 0
/////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL5:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_B_D_PIN // set the clock to be high
DELAYON5:
SUB r0, r0, 1
QBNE DELAYON5, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_B_D_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT5, SPI_DATA_IN_B_D_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE5, SAMPLE5, 0x00000001
MOV r0, CLOCK_COUNT
DELAYOFF5:
SUB r0, r0, 1
QBNE DELAYOFF5, r0, 0 // loop until the delay has expired (equals 0)
ZERO_BIT5:
LSL SAMPLE5, SAMPLE5, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
QBNE READ_CHANNEL5, SMPL_BIT_CNTR, 0
/////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL6:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_B_D_PIN // set the clock to be high
DELAYON6:
SUB r0, r0, 1
QBNE DELAYON6, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_B_D_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT6, SPI_DATA_IN_B_D_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE6, SAMPLE6, 0x00000001
MOV r0, CLOCK_COUNT
DELAYOFF6:
SUB r0, r0, 1
QBNE DELAYOFF6, r0, 0 // loop until the delay has expired (equals 0)
ZERO_BIT6:
LSL SAMPLE6, SAMPLE6, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
QBNE READ_CHANNEL6, SMPL_BIT_CNTR, 0
/////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL7:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_B_D_PIN // set the clock to be high
DELAYON7:
SUB r0, r0, 1
QBNE DELAYON7, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_B_D_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT7, SPI_DATA_IN_B_D_PIN // Check to see whether Data In is a 0 or 1
OR SAMPLE7, SAMPLE7, 0x00000001
MOV r0, CLOCK_COUNT
DELAYOFF7:
SUB r0, r0, 1
QBNE DELAYOFF7, r0, 0 // loop until the delay has expired (equals 0)
ZERO_BIT7:
LSL SAMPLE7, SAMPLE7, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
QBNE READ_CHANNEL7, SMPL_BIT_CNTR, 0
/////////////////////////
MOV SMPL_BIT_CNTR, SAMPLE_SIZE // We're going to read 24 bits
READ_CHANNEL8:
MOV r0, CLOCK_COUNT // Reload the clock delay val into R0
SET SPI_CLK_B_D_PIN // set the clock to be high
DELAYON8:
SUB r0, r0, 1
QBNE DELAYON8, r0, 0
// Reload the clock delay val into R0
CLR SPI_CLK_B_D_PIN // set the clock to be low
// Read Data In
QBBC ZERO_BIT8, SPI_DATA_IN_B_D_PIN //to see whether Data In is a 0 or 1
OR SAMPLE8, SAMPLE8, 0x00000001
MOV r0, CLOCK_COUNT
DELAYOFF8:
SUB r0, r0, 1
QBNE DELAYOFF8, r0, 0 // loop until the delay has expired (equals 0)
ZERO_BIT8:
LSL SAMPLE8, SAMPLE8, 1 // Shift current sample contents left by one
SUB SMPL_BIT_CNTR, SMPL_BIT_CNTR, 1 // See if we have read all 24 bits of the sample
QBNE READ_CHANNEL8, SMPL_BIT_CNTR, 0
/////////////////////////
STORE_SAMPLE:
LSR SAMPLE1, SAMPLE1, 1 // Need to shift the sample word back to the right by one
LSR SAMPLE2, SAMPLE2, 1
LSR SAMPLE3, SAMPLE3, 1
LSR SAMPLE4, SAMPLE4, 1
LSR SAMPLE5, SAMPLE5, 1
LSR SAMPLE6, SAMPLE6, 1
LSR SAMPLE7, SAMPLE7, 1
LSR SAMPLE8, SAMPLE8, 1
MOV SAMPLE1.b3, 0x01 // Stash the channel bitmask in the upper byte used to store the sample
MOV SAMPLE2.b3, 0x02
MOV SAMPLE3.b3, 0x04
MOV SAMPLE4.b3, 0x08
MOV SAMPLE5.b3, 0x10
MOV SAMPLE6.b3, 0x20
MOV SAMPLE7.b3, 0x40
MOV SAMPLE8.b3, 0x80
SBBO SAMPLE1, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE2, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE3, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE4, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE5, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE6, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE7, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE8, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SUB CUR_BUF_LEFT, CUR_BUF_LEFT, 32 // reducing the number of samples - 4 bytes per sample, 8 channels
QBEQ BUFFER_DONE, CUR_BUF_LEFT, 0 // See if we have taken 128kb of samples
QBA READ_NEXT_SAMPLE // If we've looped thru all channels in the sample. wait for the next sample
BUFFER_DONE:
MOV CUR_BUF_LEFT, BUFSIZE
// Subtract 32 bytes that are the header
SUB CUR_BUF_LEFT, CUR_BUF_LEFT, 32 // reducing the number of samples - 4 bytes per sample, 8 channels
// Set the amount of the current buffer left to be 128k
ADD BUFF_COUNT, BUFF_COUNT, 1
MOV R1, 1
SBBO R1, SHR_MEM_START, PRU1_WR_FLAG, 4
SBBO BUFF_COUNT, SHR_MEM_START, PRU1_BUFF_COUNT, 4
ADD HEAD, HEAD, 1
SBBO HEAD, SHR_MEM_START, PRU1_HEAD, 4
QBNE NO_WRAP, HEAD, QUEUE_SIZE
MOV HEAD, 0
SBBO HEAD, SHR_MEM_START, PRU1_HEAD, 4
QBA WRAP_TIME
NO_WRAP:
// If we have recorded all of the buffers that we need to, then halt.
SUB NUM_BLOCKS, NUM_BLOCKS, 1
QBEQ END, NUM_BLOCKS, 0
QBA NEXT_BUFFER
WRAP_TIME:
// Always wanted to be a rapper ;-0)
// We have used all the buffers in the shmem queue, wrap to the beginning.
SUB NUM_BLOCKS, NUM_BLOCKS, 1
QBEQ END, NUM_BLOCKS, 0
ADD WRAP_COUNT, WRAP_COUNT, 1
// We've wrapped, so we need to point back to the start of data buffer start
MOV SHR_MEM_PTR, PRU_DATA_STRT
// We want to write WRAP_COUNT to shared memory adc_pru_data0[12] here.
SBBO WRAP_COUNT, SHR_MEM_START, PRU1_WRAP_COUNT, 4
QBA NEXT_BUFFER
//////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Throughput test
//
//////////////////////////////////////////////////////////////////////////////////////////////////////
THROUGH_TEST:
MOV r1,ADC_ADDR
LBBO SHR_MEM_PTR, r1, 0, 4 // load the Linux address that is passed into r8 -- to store sample values
//Skip the offset bytes that we use for adc_conf at the beginning of the shared memory region
MOV r1, PRU1_START_OFFSET
ADD SHR_MEM_PTR, SHR_MEM_PTR, r1
MOV r1,ADC_SIZE
LBBO SHR_MEM_SZ, r1, 0, 4 // load the size that is passed into r9 -- the number of samples to take
NEXT_TEST_BUFFER:
MOV CUR_BUF_LEFT, BUFSIZE
// Move a well done hamburger into the first four bytes of the buffer header.
MOV R1, 0xDEADBEEF
SBBO R1, SHR_MEM_PTR, 0, 4
// Copy the buffer count into the second 4 bytes of the header
SBBO BUFF_COUNT, SHR_MEM_PTR, 4, 4 // store the sample value into shared memory space
//Skip 32 bytes that we will use for the 128k buffer header.
ADD SHR_MEM_PTR, SHR_MEM_PTR, BUF_HDR_SIZE
SUB CUR_BUF_LEFT, CUR_BUF_LEFT, BUF_HDR_SIZE
CREATE_NEXT_TEST_SAMPLE:
// Move a counter value into each of the eight 32 bit sample words.
// We are going to do this brute force (instead of a small) loop to be as fast as possible.
MOV SAMPLE1, COUNT
ADD COUNT, COUNT, 1
MOV SAMPLE2, COUNT
ADD COUNT, COUNT, 1
MOV SAMPLE3, COUNT
ADD COUNT, COUNT, 1
MOV SAMPLE4, COUNT
ADD COUNT, COUNT, 1
MOV SAMPLE5, COUNT
ADD COUNT, COUNT, 1
MOV SAMPLE6, COUNT
ADD COUNT, COUNT, 1
MOV SAMPLE7, COUNT
ADD COUNT, COUNT, 1
MOV SAMPLE8, COUNT
ADD COUNT, COUNT, 1
// We look to see if COUNT > FFFFFF, if so, set to zero.
MOV R1, FULL_COUNT
QBEQ ZERO_COUNT, R1, COUNT
QBA WRITE_CHAN_BYTE
ZERO_COUNT:
MOV COUNT, 0
WRITE_CHAN_BYTE:
MOV SAMPLE1.b3, 0x01 // Stash the channel bitmask in the upper byte used to store the sample
MOV SAMPLE2.b3, 0x02
MOV SAMPLE3.b3, 0x04
MOV SAMPLE4.b3, 0x08
MOV SAMPLE5.b3, 0x10
MOV SAMPLE6.b3, 0x20
MOV SAMPLE7.b3, 0x40
MOV SAMPLE8.b3, 0x80
SBBO SAMPLE1, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE2, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE3, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE4, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE5, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE6, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE7, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SBBO SAMPLE8, SHR_MEM_PTR, 0, 4 // store the sample value into shared memory space
ADD SHR_MEM_PTR, SHR_MEM_PTR, 4 // Add 4 bytes per sample to the address pointer
SUB CUR_BUF_LEFT, CUR_BUF_LEFT, 32 // reducing the number of samples - 4 bytes per sample, 8 channels
// We have created one sample, wait roughly 1/10547 secs to simulate sample time from DAQ
MOV r0, INSTRUCTIONS_PER_SAMPLE
DELAY:
SUB r0, r0, 1
QBNE DELAY, r0, 0 // loop until the delay has expired (equals 0)
QBEQ TEST_BUFFER_DONE, CUR_BUF_LEFT, 0 // See if we have taken 128kb of samples
QBA CREATE_NEXT_TEST_SAMPLE // If we've looped thru all channels in the sample. wait for the next sample
TEST_BUFFER_DONE:
// Set the amount of the current buffer left to be 128k
MOV CUR_BUF_LEFT, BUFSIZE
// Subtract 32 bytes that are the header
SUB CUR_BUF_LEFT, CUR_BUF_LEFT, 32 // reducing the number of samples - 4 bytes per sample, 8 channels
ADD BUFF_COUNT, BUFF_COUNT, 1
MOV R1, 1
SBBO R1, SHR_MEM_START, PRU1_WR_FLAG, 4
SBBO BUFF_COUNT, SHR_MEM_START, PRU1_BUFF_COUNT, 4
ADD HEAD, HEAD, 1
SBBO HEAD, SHR_MEM_START, PRU1_HEAD, 4
QBNE NO_TEST_WRAP, HEAD, QUEUE_SIZE
MOV HEAD, 0
SBBO HEAD, SHR_MEM_START, PRU1_HEAD, 4
QBA TEST_WRAP_TIME
NO_TEST_WRAP:
// If we have recorded all of the buffers that we need to, then halt.
SUB NUM_BLOCKS, NUM_BLOCKS, 1
QBEQ END, NUM_BLOCKS, 0
QBA NEXT_TEST_BUFFER
TEST_WRAP_TIME:
// Always wanted to be a rapper ;-0)
// We have used all the buffers in the shmem queue, wrap to the beginning.
SUB NUM_BLOCKS, NUM_BLOCKS, 1
QBEQ END, NUM_BLOCKS, 0
ADD WRAP_COUNT, WRAP_COUNT, 1
// We've wrapped, so we need to point back to the start of data buffer start
MOV SHR_MEM_PTR, PRU_DATA_STRT
// We want to write WRAP_COUNT to shared memory adc_pru_data0[12] here.
SBBO WRAP_COUNT, SHR_MEM_START, PRU1_WRAP_COUNT, 4
QBA NEXT_TEST_BUFFER
// halt the pru program -- we reach here when we have read all of the intended buffers.
END:
CLR r30.t6 // Pin 39, CH B 1-4
CLR r30.t7 // Pin 40, CH C 1-4
CLR r30.t4 // Pin 41, CH B 5-8
CLR r30.t5 // Pin 42, CH D 1-4
CLR r30.t2 // Pin 43, CH A 5-8
CLR r30.t3 // Pin 44, CHD 5-8
CLR r30.t0 // Pin 45 CH A 1-4
CLR r30.t1 // Pin 46, CH C 5-8
CLR r30.t8
MOV r31.b0, PRU0_R31_VEC_VALID | PRU_EVTOUT_0
HALT
+18
View File
@@ -0,0 +1,18 @@
cd /lib/firmware
echo PRU-ADC > $SLOTS
cat $SLOTS
lsmod
rmmod uio_pruss
lsmod
modprobe uio_pruss extram_pool_sz=0x400000
lsmod
cd /sys/class/uio/uio0/maps/map1
cat addr
cat size
cd /sys/class/uio/uio0/maps/map0
cat addr
cat size
cd /home/erps/daq2/pruSrc
./pruadc