164 lines
4.0 KiB
C
Raw Normal View History

2019-10-04 07:28:32 +01:00
#include <wiringPi.h>
2019-10-04 08:32:10 +01:00
#include <wiringPiSPI.h>
2019-10-04 13:15:37 +02:00
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
2019-10-04 13:55:58 +02:00
#include <pthread.h>
2019-10-04 13:15:37 +02:00
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;
2019-10-04 07:28:32 +01:00
2019-10-04 14:29:10 +02:00
uint32_t ec = 0;
uint32_t counter = 0;
uint32_t diff = 0;
2019-10-04 13:55:58 +02:00
2019-10-04 14:48:57 +02:00
pthread_mutex_t counterMutex = PTHREAD_MUTEX_INITIALIZER;
2019-10-04 13:55:58 +02:00
pthread_mutex_t eventMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t eventSignal = PTHREAD_COND_INITIALIZER;
2019-10-04 07:28:32 +01:00
2019-10-04 13:17:03 +02:00
void writeCmd(uint8_t c) {
2019-10-04 13:28:56 +02:00
uint8_t buf[1];
buf[0] = c;
wiringPiSPIDataRW(SPI_CHAN, buf, 1);
2019-10-04 13:15:37 +02:00
}
2019-10-04 13:17:03 +02:00
void writeCmdData(uint8_t c, uint8_t d) {
2019-10-04 13:28:56 +02:00
uint8_t buf[2];
buf[0] = c;
buf[1] = d;
wiringPiSPIDataRW(SPI_CHAN, buf, 2);
2019-10-04 13:15:37 +02:00
}
2019-10-04 13:17:03 +02:00
uint8_t read8(uint8_t c) {
2019-10-04 13:15:37 +02:00
uint8_t buf[2];
2019-10-04 13:28:56 +02:00
buf[0] = c;
2019-10-04 13:15:37 +02:00
wiringPiSPIDataRW(SPI_CHAN, buf, 2);
2019-10-04 13:28:56 +02:00
return buf[1];
2019-10-04 13:15:37 +02:00
}
2019-10-04 13:17:03 +02:00
uint32_t read32(uint8_t c) {
2019-10-04 13:28:56 +02:00
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;
2019-10-04 13:15:37 +02:00
}
2019-10-04 13:33:43 +02:00
void isr() {
2019-10-04 13:55:58 +02:00
static uint32_t lastCounter = 0;
uint32_t currentCounter = read32(CMD_RD | REG_OTR);
2019-10-04 14:48:57 +02:00
pthread_mutex_lock(&counterMutex);
2019-10-04 13:55:58 +02:00
diff = currentCounter - lastCounter;
2019-10-04 14:29:10 +02:00
counter = currentCounter;
2019-10-04 08:51:15 +01:00
ec++;
2019-10-04 14:48:57 +02:00
pthread_mutex_unlock(&counterMutex);
lastCounter = currentCounter;
2019-10-04 13:55:58 +02:00
pthread_mutex_lock(&eventMutex);
pthread_cond_signal(&eventSignal);
pthread_mutex_unlock(&eventMutex);
2019-10-04 13:33:43 +02:00
}
2019-10-04 13:15:37 +02:00
void init() {
2019-10-04 13:28:56 +02:00
wiringPiSetupGpio();
2019-10-04 13:15:37 +02:00
2019-10-04 13:28:56 +02:00
wiringPiSPISetup(SPI_CHAN, SPI_SPEED);
2019-10-04 13:15:37 +02:00
2019-10-04 13:28:56 +02:00
pinMode(CTRL_OUT, OUTPUT);
digitalWrite(CTRL_OUT, 0);
2019-10-04 13:15:37 +02:00
2019-10-04 13:28:56 +02:00
pinMode(INTR_IN, INPUT);
2019-10-04 14:15:05 +02:00
wiringPiISR(INTR_IN, INT_EDGE_RISING, isr);
2019-10-04 13:15:37 +02:00
}
void initCounter() {
2019-10-04 13:28:56 +02:00
writeCmd(CMD_CLR | REG_STR);
writeCmd(CMD_CLR | REG_CNTR);
2019-10-04 13:33:43 +02:00
writeCmdData(CMD_WR | REG_MDR0, MDR0_ILO);
2019-10-04 13:15:37 +02:00
}
2019-10-04 07:28:32 +01:00
int main (void) {
2019-10-04 09:48:04 +01:00
static uint32_t lastDiff = 0;
2019-10-04 14:48:57 +02:00
uint32_t my_ec = 0;
uint32_t my_counter = 0;
uint32_t my_diff = 0;
2019-10-04 09:48:04 +01:00
2019-10-04 13:28:56 +02:00
init();
initCounter();
2019-10-04 13:15:37 +02:00
2019-10-04 13:28:56 +02:00
while (1) {
2019-10-04 13:55:58 +02:00
pthread_mutex_lock(&eventMutex);
pthread_cond_wait(&eventSignal, &eventMutex);
pthread_mutex_unlock(&eventMutex);
2019-10-04 07:28:32 +01:00
2019-10-04 14:48:57 +02:00
pthread_mutex_lock(&counterMutex);
my_ec = ec;
my_counter = counter;
my_diff = diff;
pthread_mutex_unlock(&counterMutex);
2019-10-04 14:50:08 +02:00
if (my_diff != lastDiff) {
lastDiff = my_diff;
double f = 1.0 / (((double) my_diff) / 1000000.0);
printf("%d %d %d %f\n", my_ec, my_counter, my_diff, f);
2019-10-04 09:48:04 +01:00
}
2019-10-04 13:28:56 +02:00
}
2019-10-04 07:28:32 +01:00
}