refactoring
This commit is contained in:
81
src/counter.c
Normal file
81
src/counter.c
Normal file
@ -0,0 +1,81 @@
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "LS7366R.h"
|
||||
|
||||
const int CTRL_OUT = 16;
|
||||
const int INTR_IN = 19;
|
||||
const int SPI_CHAN = 0;
|
||||
const int SPI_SPEED = 1000000;
|
||||
|
||||
|
||||
uint32_t ec = 0;
|
||||
uint32_t counter = 0;
|
||||
uint32_t diff = 0;
|
||||
|
||||
pthread_mutex_t counterMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_t eventMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_cond_t eventSignal = PTHREAD_COND_INITIALIZER;
|
||||
|
||||
|
||||
void isr() {
|
||||
static uint32_t lastCounter = 0;
|
||||
|
||||
uint32_t currentCounter = read32(CMD_RD | REG_OTR);
|
||||
|
||||
pthread_mutex_lock(&counterMutex);
|
||||
diff = currentCounter - lastCounter;
|
||||
counter = currentCounter;
|
||||
ec++;
|
||||
pthread_mutex_unlock(&counterMutex);
|
||||
|
||||
|
||||
lastCounter = currentCounter;
|
||||
|
||||
pthread_mutex_lock(&eventMutex);
|
||||
pthread_cond_signal(&eventSignal);
|
||||
pthread_mutex_unlock(&eventMutex);
|
||||
}
|
||||
|
||||
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_RISING, isr);
|
||||
}
|
||||
|
||||
int main (void) {
|
||||
static uint32_t lastDiff = 0;
|
||||
uint32_t my_ec = 0;
|
||||
uint32_t my_counter = 0;
|
||||
uint32_t my_diff = 0;
|
||||
|
||||
init();
|
||||
initCounter();
|
||||
|
||||
while (1) {
|
||||
pthread_mutex_lock(&eventMutex);
|
||||
pthread_cond_wait(&eventSignal, &eventMutex);
|
||||
pthread_mutex_unlock(&eventMutex);
|
||||
|
||||
pthread_mutex_lock(&counterMutex);
|
||||
my_ec = ec;
|
||||
my_counter = counter;
|
||||
my_diff = diff;
|
||||
pthread_mutex_unlock(&counterMutex);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user