Compare commits

...

16 Commits

Author SHA1 Message Date
8cbd64034b prevent multi include 2025-05-25 16:19:52 +02:00
99c2f4f0d4 prevent multi include 2025-05-25 16:19:28 +02:00
095fca6b3d cleard to much? I am confused 2025-05-25 16:03:37 +02:00
69d537bddd cleard and moved includes to header 2025-05-25 16:01:05 +02:00
11146fb632 make ringbuffer thread safety optional 2025-05-25 15:27:00 +02:00
52e2c96e0e move DATA_SIZE to global 2025-05-25 15:07:39 +02:00
17a5b457d6 required libs 2025-05-25 14:03:12 +02:00
73c59c732a moved global.h to new controller implementation 2025-05-25 12:24:26 +02:00
2543ac1336 sleep if not buzy (end of loop) 2025-05-22 20:56:29 +02:00
6af2ab8b1c move mutex into ringbuffer
also make buffer void*
2025-05-22 20:49:02 +02:00
f34380bd01 i2c read and write ring buffer 2025-05-21 22:39:32 +02:00
b8c7b84746 implemented ringbuffer 2025-05-20 22:23:19 +02:00
c8984d8765 begin of threaded controller 2025-05-19 20:53:09 +02:00
4e33b053f4 something 2025-05-18 21:33:59 +02:00
ac6f851b68 dont stress device on boot 2025-05-18 21:22:32 +02:00
d62548d3e1 raspberry pi based controller
working i2c communication
2025-05-18 21:05:44 +02:00
11 changed files with 373 additions and 13 deletions

1
controller/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
controller

View File

@ -1,12 +1,22 @@
# Controller
## links
* https://www.kernel.org/doc/Documentation/i2c/dev-interface
* https://forum.arduino.cc/t/solved-cannot-get-correct-i2c-data-from-atmega8-into-raspberry-pi/1368337
## PINs
* interal = physical = usage
* 5 = 29 = reset peripheral devices
* 0 = 27 = I2C SDA
* 1 = 28 = I2C SCL
* 0 = 10 = I2C SDA
* 1 = 9 = I2C SCL
## I2C
* somehow the buildin I2C does not work anymore (maybe already damaged?)
* add additional bus by using GPIOs:
```
$ fgrep i2c /boot/firmware/config.txt
dtparam=i2c_arm=on
dtoverlay=i2c-gpio,bus=7,i2c_gpio_sda=10,i2c_gpio_scl=9
```
## commands
* reset peripheral devices: `gpioset gpiochip0 5=0`

1
controller/build.sh Executable file
View File

@ -0,0 +1 @@
gcc -o controller ringbuffer.c controller.c -li2c

236
controller/controller.c Normal file
View File

@ -0,0 +1,236 @@
#define RINGBUFFER_THREAD_SAFE
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include "global.h"
#ifndef RINGBUFFER_H
#include "ringbuffer.h"
#endif
#define IIC_DEVICE "/dev/i2c-7"
#define BUFFER_BLOCKS 128
#define HEADING_SIM_UP "sim/autopilot/heading_up"
#define HEADING_SIM_DOWN "sim/autopilot/heading_down"
#define ALTITUDE_SIM_UP "sim/autopilot/altitude_up"
#define ALTITUDE_SIM_DOWN "sim/autopilot/altitude_down"
#define AIRSPEED_SIM_UP "sim/autopilot/airspeed_up"
#define AIRSPEED_SIM_DOWN "sim/autopilot/airspeed_down"
#define ERROR_IIC_OPEN "open failed"
struct ringBuffer i2cDataRead;
struct ringBuffer i2cDataWrite;
struct blockData {
char data[DATA_SIZE];
int triggerBit;
};
struct device {
uint8_t address; // I2C address of device
char *name; // name of this device
char name1[3]; // first name to send on init of device
char *highCommand1; // command to use if TRIGGER_BIT_1 is set and HIGH_BIT_1 is set
char *lowCommand1; // command to use if TRIGGER_BIT_1 is set and HIGH_BIT_1 is not set
int rrefRefresh1; // refresh rate of subscribing rref
char *rref1; // rref to subscribe to
char name2[3]; // second name to send on init of device
char *highCommand2; // command to use if TRIGGER_BIT_2 is set and HIGH_BIT_2 is set
char *lowCommand2; // command to use if TRIGGER_BIT_2 is set and HIGH_BIT_2 is not set
int rrefRefresh2; // refresh rate of subscribing rref
char *rref2; // rref to subscribe to
};
struct rrefSubscription {
uint8_t address;
uint8_t triggerBit;
};
const struct device devices[] = {
{
0x08,
"Heading Speed",
{'H', 'D', 'G'},
HEADING_SIM_UP,
HEADING_SIM_DOWN,
200,
"sim/cockpit/autopilot/heading_mag",
{'S', 'P', 'D'},
AIRSPEED_SIM_UP,
AIRSPEED_SIM_DOWN,
200,
"sim/cockpit/autopilot/airspeed"
}
};
const int numDevices = sizeof(devices) / sizeof(struct device);
struct rrefSubscription *rrefSubscriptions;
void _log(char *format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
int setI2CAddress(int i2c, uint8_t address) {
if (ioctl(i2c, I2C_SLAVE, address) < 0) {
_log("setting i2c address 0x%02X failed\n", address);
return 0;
} else {
return 1;
}
}
void sendI2CInit(int i2c, struct device d) {
_log("Sending init to %s\n", d.name);
int bufLen = 1 + 1 + sizeof(d.name1) + 1 + sizeof(d.name2);
char buf[bufLen];
int i = 0;
buf[i++] = DATA_RESET_BYTE;
buf[i++] = DATA_INIT_BYTE_1;
buf[i++] = d.name1[0];
buf[i++] = d.name1[1];
buf[i++] = d.name1[2];
buf[i++] = DATA_INIT_BYTE_2;
buf[i++] = d.name2[0];
buf[i++] = d.name2[1];
buf[i++] = d.name2[2];
setI2CAddress(i2c, d.address);
i2c_smbus_write_block_data(i2c, 0, bufLen, buf);
}
void sendI2CData(int i2c, struct device d, uint8_t triggerBit, char *data, uint8_t dataLen) {
int bufLen = 1 + 1 + 1 + dataLen;
char buf[bufLen];
int i = 0;
buf[i++] = DATA_BYTE;
buf[i++] = triggerBit;
buf[i++] = dataLen;
for (int j = 0; j < dataLen; j++) {
buf[i++] = data[j];
}
setI2CAddress(i2c, d.address);
i2c_smbus_write_block_data(i2c, 0, bufLen, buf);
}
int tempCounter = 0;
int tempGauge = 0;
void* mqttHandler(void* arg) {
struct blockData *block = malloc(sizeof(struct blockData));
while (1) {
sprintf(block->data, "%03d", tempCounter);
block->triggerBit = TRIGGER_BIT_1;
ringBufferWrite(&i2cDataWrite, block);
tempCounter++;
sleep(1);
}
free(block);
}
void* i2cHandler(void* arg) {
int i2c, tmp, buzy;
__s32 i2cResponse;
struct blockData *block = malloc(sizeof(struct blockData));
i2c = open(IIC_DEVICE, O_RDWR);
if (i2c < 0) {
_log("Unable to open I2C device %s\n", IIC_DEVICE);
}
while (1) {
buzy = 0;
for (int i = 0; i < numDevices; i++) {
setI2CAddress(i2c, devices[i].address);
i2cResponse = i2c_smbus_read_byte_data(i2c, 0);
if (i2cResponse < 0) {
_log("I2C read from device %s failed: %s (%d)\n", devices[i].name, strerror(errno), errno);
continue;
}
if (i2cResponse == DATA_RESET_BYTE) {
sendI2CInit(i2c, devices[i]);
buzy = 1;
} else if (i2cResponse == DATA_STOP_BYTE) {
// expect no forther data from device
// send data to device
do {
tmp = ringBufferRead(&i2cDataWrite, block);
if (tmp == 0) {
// we got something to send
sendI2CData(i2c, devices[i], block->triggerBit, block->data, 3);
buzy = 1;
}
} while (tmp == 0);
continue;
} else {
// real data
_log("data: 0x%02x\n", i2cResponse);
if (i2cResponse & TRIGGER_BIT_2) {
if (i2cResponse & HIGH_BIT_2) {
tempGauge++;
if (tempGauge > 999) {
tempGauge = 999;
}
} else {
tempGauge--;
if (tempGauge < 0) {
tempGauge = 0;
}
}
sprintf(block->data, "%03d", tempGauge);
block->triggerBit = TRIGGER_BIT_2;
ringBufferWrite(&i2cDataWrite, block);
}
buzy = 1;
}
}
if (buzy == 0) {
usleep(50 * 1000);
}
}
close(i2c);
free(block);
pthread_exit(NULL);
}
int main() {
pthread_t i2cThread, mqttThread;
// create the ring buffers for i2c devices
ringBufferCreate(BUFFER_BLOCKS, sizeof(struct blockData), &i2cDataRead);
ringBufferCreate(BUFFER_BLOCKS, sizeof(struct blockData), &i2cDataWrite);
// alloc
rrefSubscriptions = calloc(sizeof(struct device), numDevices);
// start and join threads
pthread_create(&i2cThread, NULL, i2cHandler, NULL);
pthread_create(&mqttThread, NULL, mqttHandler, NULL);
pthread_join(i2cThread, NULL);
pthread_join(mqttThread, NULL);
// free resources
free(rrefSubscriptions);
ringBufferDestroy(&i2cDataRead);
ringBufferDestroy(&i2cDataWrite);
return 0;
}

11
controller/global.h Normal file
View File

@ -0,0 +1,11 @@
#define DATA_STOP_BYTE 0x00
#define DATA_INIT_BYTE_1 0x01
#define DATA_INIT_BYTE_2 0x02
#define DATA_BYTE 0x03
#define DATA_RESET_BYTE 0xFF
#define TRIGGER_BIT_1 1
#define HIGH_BIT_1 2
#define TRIGGER_BIT_2 4
#define HIGH_BIT_2 8
#define I2C_ADDRESS_START 0x08
#define DATA_SIZE 8

75
controller/ringbuffer.c Normal file
View File

@ -0,0 +1,75 @@
#ifndef RINGBUFFER_H
#include "ringbuffer.h"
#endif
void _ringBufferIncReader(struct ringBuffer *buf) {
buf->reader += buf->blockSize;
if (buf->reader >= buf->buffer + buf->blocks * buf->blockSize) {
buf->reader = buf->buffer;
}
}
void _ringBufferIncWriter(struct ringBuffer *buf) {
buf->writer += buf->blockSize;
if (buf->writer == buf->buffer + buf->blocks * buf->blockSize) {
buf->writer = buf->buffer;
}
if (buf->writer == buf->reader) {
// we incremented the writer and now it is equal to reader
// this means, the writer is overtaking the reader
// so push the reader forward, even if we are
// loosing data but this is how ring buffers work, eh?
_ringBufferIncReader(buf);
}
}
void ringBufferCreate(int blocks, size_t blockSize, struct ringBuffer *out) {
out->buffer = malloc(blocks * blockSize);
out->blocks = blocks;
out->blockSize = blockSize;
out->reader = out->buffer;
out->writer = out->buffer;
#ifdef RINGBUFFER_THREAD_SAFE
pthread_mutex_init(&out->mutex, NULL);
#endif
}
void ringBufferDestroy(struct ringBuffer *buf) {
free(buf->buffer);
#ifdef RINGBUFFER_THREAD_SAFE
pthread_mutex_destroy(&buf->mutex);
#endif
}
int ringBufferRead(struct ringBuffer *buf, void *out) {
#ifdef RINGBUFFER_THREAD_SAFE
pthread_mutex_lock(&buf->mutex);
#endif
if (buf->reader != buf->writer) {
// we have data to read
memcpy(out, buf->reader, buf->blockSize);
_ringBufferIncReader(buf);
#ifdef RINGBUFFER_THREAD_SAFE
pthread_mutex_unlock(&buf->mutex);
#endif
return 0;
} else {
// nothing to read
#ifdef RINGBUFFER_THREAD_SAFE
pthread_mutex_unlock(&buf->mutex);
#endif
return 1;
}
}
void ringBufferWrite(struct ringBuffer *buf, void *in) {
#ifdef RINGBUFFER_THREAD_SAFE
pthread_mutex_lock(&buf->mutex);
#endif
memcpy(buf->writer, in, buf->blockSize);
_ringBufferIncWriter(buf);
#ifdef RINGBUFFER_THREAD_SAFE
pthread_mutex_unlock(&buf->mutex);
#endif
}

25
controller/ringbuffer.h Normal file
View File

@ -0,0 +1,25 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#ifdef RINGBUFFER_THREAD_SAFE
#include <pthread.h>
#endif
#define RINGBUFFER_H
struct ringBuffer {
void *buffer;
int blocks;
size_t blockSize;
void *reader;
void *writer;
#ifdef RINGBUFFER_THREAD_SAFE
pthread_mutex_t mutex;
#endif
};
void ringBufferCreate(int blocks, size_t blockSize, struct ringBuffer *out);
void ringBufferDestroy(struct ringBuffer *buf);
int ringBufferRead(struct ringBuffer *buf, void *out);
void ringBufferWrite(struct ringBuffer *buf, void *in);

View File

@ -1,10 +0,0 @@
#define DATA_STOP_BYTE 0x00
#define DATA_INIT_BYTE_1 0x01
#define DATA_INIT_BYTE_2 0x02
#define DATA_BYTE 0x03
#define DATA_RESET_BYTE 0xFF
#define TRIGGER_BIT_1 1
#define HIGH_BIT_1 2
#define TRIGGER_BIT_2 4
#define HIGH_BIT_2 8
#define I2C_ADDRESS_START 0x08

1
master/global.h Symbolic link
View File

@ -0,0 +1 @@
../controller/global.h

View File

@ -2,6 +2,11 @@
* two rotators
* two displays
## required libraries
* https://github.com/adafruit/Adafruit_SSD1306/
* https://github.com/adafruit/Adafruit-GFX-Library
* https://github.com/adafruit/Adafruit_BusIO
## PINs
* IDE = Physical = Usage
* x = 1 = reset (connecto to master)

View File

@ -1 +1 @@
../master/global.h
../controller/global.h

View File

@ -51,6 +51,11 @@ uint8_t lastClk = HIGH;
byte valueBuffer[VALUE_BUFFER] = { 0 };
uint8_t readerPos, writerPos = 0;
// ringbuffer of display data
struct {
}
void addValue(uint8_t value) {
valueBuffer[writerPos++] = value;