move all highlevel code into the protocol folder

master
Julian Daube 3 years ago committed by Julian Daube
parent 917fac440f
commit fee54a77eb

3
.gitmodules vendored

@ -1,3 +0,0 @@
[submodule "firmware/slider/SliderCommunication"]
path = firmware/slider/SliderCommunication
url = https://gitlab.tubit.tu-berlin.de/dsp_labor_ws1819/SliderCommunication.git

@ -0,0 +1,4 @@
This folder contains template implementations for the communication master and slaves.
It also defines the data packet that is exchanged on each poll. This definition is
used in the actual slave firmware.

@ -0,0 +1,29 @@
/*
* communication.h
*
* Describes the data packet sent for each channel
*/
#pragma once
#include "interface.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
uint16_t poti[4];
uint16_t slider;
uint8_t button:1;
} message_t;
/// END OF MESSAGE == MSB is set
#define IS_EOM(b) (b & (0x80))
// defines the message length of a slave message in bytes
// NOTE: sizeof(message_t) is 12 because of the 4-byte alignment criteria
#define SLAVE_MESSAGE_LEN (5*2+1)
#ifdef __cplusplus
} // end extern "C"
#endif

@ -0,0 +1,24 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef uint8_t byte_t;
// this struct describes a communication interface
typedef struct iface {
// callback, gets called when data arrives at interface
void (*on_read)(struct iface *, byte_t, void*ptr);
void *callback_data;
void (*write)(struct iface *, byte_t);
} iface_t;
#ifdef __cplusplus
} // end extern "C"
#endif

@ -0,0 +1,62 @@
/*
* master.c
*
* Created on: 06.12.2018
* Author: julian
*/
#include "master.h"
#include "../communication.h"
void on_receive(iface_t * me, byte_t b, void*ptr)
{
master_t * this = (master_t*)(ptr);
if (this->msg_byte_counter >= 0) {
if (this->msg_byte_counter <=8) {
static uint16_t temp = 0;
if (this->msg_byte_counter%2) {
temp |= (b<<7);
switch(this->msg_byte_counter) {
case 1:
case 3:
case 5:
this->buffer[this->chain_len].poti[this->msg_byte_counter/2] = temp;
break;
case 7:
this->buffer[this->chain_len].slider = temp;
break;
}
}else {
temp = b&0x7F;
}
} else {
this->buffer[this->chain_len].button = b&0x01;
}
}
this->msg_byte_counter++;
if (!(this->msg_byte_counter = (this->msg_byte_counter+1)%SLAVE_MESSAGE_LEN)) {
this->chain_len++;
}
// decode package
if (IS_EOM(b)) {
return;
}
// keep communication running
me->write(me, 0x00);
}
void master_init(master_t *master, iface_t * dev)
{
dev->on_read = on_receive;
dev->callback_data = master;
// initialize master values
*master = struct {
.iface = dev,
};
}

@ -0,0 +1,36 @@
/*
* master.h
*
* Created on: 06.12.2018
* Author: julian
*/
#ifndef __DSPLAB_COMMON_MASTER_H__
#define __DSPLAB_COMMON_MASTER_H__
#include "../slave/slave.h"
#ifdef __cplusplus
extern "C" {
#endif
// how many channels we can encounter AT MOST
#define MAX_CHAIN_LEN 10
// this structure defines one complete chain
typedef struct {
iface_t * iface;
message_t buffer[MAX_CHAIN_LEN];
int chain_len; //< measured chainlength
int msg_byte_counter; //< bytecounter of receiver
} master_t;
void master_init(master_t *master, iface_t * dev);
#ifdef __cplusplus
} // end extern "C"
#endif
#endif /* __DSPLAB_COMMON_MASTER_H__ */

@ -0,0 +1,48 @@
/*
* slave.c
*
* this is a default implementation for a slave
* it will continously encode its state on request
*/
#include "slave.h"
#include "../communication.h"
// implementation for slave_encode
void slave_encode(iface_t *iface, int16_t s) {
iface->write(iface, (s>>0)&0x7F);
iface->write(iface, (s>>7)&0x7F);
}
void slave_on_receive(iface_t * me, byte_t buffer, void *ptr)
{
slave_t * slave = (slave_t*)ptr;
if (!IS_EOM(buffer)) {
me->write(me, buffer);
return;
}
me->write(me, buffer & 0x7F);
// write state
// write potis
for (int i = 0; i < 4; i++)
slave_encode(me, slave->state.poti[i]);
// write slider
slave_encode(me, slave->state.slider);
me->write(me, 0x80 | (slave->state.button & 0x01));
}
void slave_init(slave_t *slave, iface_t *iface)
{
iface->on_read = slave_on_receive;
iface->callback_data = slave;
slave->iface = iface;
slave->state = (message_t){};
}

@ -0,0 +1,36 @@
/*
* slave.h
*
* contains a slave definition
*/
#pragma once
#include "../communication.h"
#include "../interface.h"
#ifdef __cplusplus
extern "C" {
#endif
// this struct describes one chain in the link
typedef struct
{
iface_t * iface;
message_t state;
} slave_t;
// The slave will use this method to encode
// one adc sample
void slave_encode(iface_t *iface, int16_t s);
// initialize the slave
void slave_init(slave_t *slave, iface_t *iface);
#ifdef __cplusplus
} // end extern "C"
#endif
Loading…
Cancel
Save