From 3032cf9a3c80f2f9d7fc0222e8869a589effaed8 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Fri, 4 Oct 2019 13:15:37 +0200 Subject: [PATCH] py converted to c --- snippets/test1/Makefile | 17 ++++++ snippets/test1/test1.c | 118 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 snippets/test1/Makefile diff --git a/snippets/test1/Makefile b/snippets/test1/Makefile new file mode 100644 index 0000000..5513847 --- /dev/null +++ b/snippets/test1/Makefile @@ -0,0 +1,17 @@ +CC=gcc + +CFLAGS=-Wall +LDFLAGS=-lwiringPi + +test1: test1.o + $(CC) -o $@ $(LDFLAGS) $^ + +.c.o: + $(CC) $(CFLAGS) -c $< + +.PHONY: all +all: test1 + +.PHONY: clean +clean: + -rm -f *.o test1 diff --git a/snippets/test1/test1.c b/snippets/test1/test1.c index 31a3069..31ac765 100644 --- a/snippets/test1/test1.c +++ b/snippets/test1/test1.c @@ -1,4 +1,63 @@ #include +#include +#include +#include + + +const int CTRL_OUT = 16; +const int INTR_IN = 19; +const int SPI_CHAN = 0; +const int SPI_SPEED = 1000000; + +const uint8_t REG_MDR0 = 0b00001000; +const uint8_t REG_MDR1 = 0b00010000; +const uint8_t REG_DTR = 0b00011000; +const uint8_t REG_CNTR = 0b00100000; +const uint8_t REG_OTR = 0b00101000; +const uint8_t REG_STR = 0b00110000; + +const uint8_t CMD_CLR = 0b00000000; +const uint8_t CMD_RD = 0b01000000; +const uint8_t CMD_WR = 0b10000000; +const uint8_t CMD_LOAD = 0b11000000; + +const uint8_t STR_CY = 0b10000000; +const uint8_t STR_BW = 0b01000000; +const uint8_t STR_CMP = 0b00100000; +const uint8_t STR_IDX = 0b00010000; +const uint8_t STR_CEN = 0b00001000; +const uint8_t STR_PLS = 0b00000100; +const uint8_t STR_UD = 0b00000010; +const uint8_t STR_S = 0b00000001; + +const uint8_t MDR0_NOQ = 0b00000000; +const uint8_t MDR0_Q1 = 0b00000001; +const uint8_t MDR0_Q2 = 0b00000010; +const uint8_t MDR0_Q4 = 0b00000011; +const uint8_t MDR0_FRC = 0b00000000; +const uint8_t MDR0_SCC = 0b00000100; +const uint8_t MDR0_RLC = 0b00001000; +const uint8_t MDR0_MNC = 0b00001100; +const uint8_t MDR0_DI = 0b00000000; +const uint8_t MDR0_ILC = 0b00010000; +const uint8_t MDR0_IRC = 0b00100000; +const uint8_t MDR0_ILO = 0b00110000; +const uint8_t MDR0_AI = 0b00000000; +const uint8_t MDR0_SI = 0b01000000; +const uint8_t MDR0_FC1 = 0b00000000; +const uint8_t MDR0_FC2 = 0b10000000; + +const uint8_t MDR1_4CM = 0b00000000; +const uint8_t MDR1_3CM = 0b00000001; +const uint8_t MDR1_2CM = 0b00000010; +const uint8_t MDR1_1CM = 0b00000011; +const uint8_t MDR1_EC = 0b00000000; +const uint8_t MDR1_DC = 0b00000100; +const uint8_t MDR1_F_IDX = 0b00010000; +const uint8_t MDR1_F_CMP = 0b00100000; +const uint8_t MDR1_F_BW = 0b01000000; +const uint8_t MDR1_F_CY = 0b10000000; + void isr() { static int t = 0; @@ -6,12 +65,61 @@ void isr() { t = ! t; } +void writeCmd(c) { + uint8_t buf[1]; + buf[0] = c; + wiringPiSPIDataRW(SPI_CHAN, buf, 1); +} + +void writeCmdData(c, d) { + uint8_t buf[2]; + buf[0] = c; + buf[1] = d; + wiringPiSPIDataRW(SPI_CHAN, buf, 2); +} + +uint8_t read8(c) { + uint8_t buf[2]; + buf[0] = c; + wiringPiSPIDataRW(SPI_CHAN, buf, 2); + return buf[1]; +} + +uint32_t read32(c) { + uint8_t buf[5]; + buf[0] = c; + wiringPiSPIDataRW(SPI_CHAN, buf, 5); + uint32_t r = ((uint32_t)buf[1] << 24) | ((uint32_t)buf[2] << 16) | ((uint32_t)buf[3] << 8) | ((uint32_t)buf[4]); + return r; +} + +void init() { + wiringPiSetupGpio(); + + wiringPiSPISetup(SPI_CHAN, SPI_SPEED); + + pinMode(CTRL_OUT, OUTPUT); + digitalWrite(CTRL_OUT, 0); + + pinMode(INTR_IN, INPUT); + wiringPiISR(INTR_IN, INT_EDGE_FALLING, isr); +} + +void initCounter() { + writeCmd(CMD_CLR | REG_STR); + writeCmd(CMD_CLR | REG_CNTR); +} int main (void) { - wiringPiSetupGpio() ; - pinMode(16, OUTPUT); - pinMode(19, INPUT); - wiringPiISR(19, INT_EDGE_FALLING, isr); + init(); + initCounter(); - while(1); + while (1) { + uint8_t str = read8(CMD_RD | REG_STR); + uint32_t cntr = read32(CMD_RD | REG_CNTR); + + printf("%02x %ud\n", str, cntr); + + sleep(1); + } }