Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mailbox for thread-safe data transfer, Improve SPI reliability #107

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions documentation/masterboard_communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,22 @@ On the 4 pairs of wires, only 2 are used by the ethernet 100Mbps and cables are

It is possible to make the ethernet cables lighter by using only 2 twisted pairs of wire. The wires can be attached to the power cables without any issues of packet loss.

For reference, here is the wiring:

On a Straight cable, keep the orange wires (1,2) and green wires (3,6).

Do not untwist the wires!

![straight-through-cable](https://www.fs.com/images/ckfinder/ftp_images/tutorial/straight-through-cable.png)

Do not untwist the wires.

WiFi
----
The WiFi connection is also happening at the lowest level of the 802.11 specification, using vendor specific action frame. No base station or rooter are needed. No identification is needed.

Ideally a free channel should be used, with a minimum of 2.4Ghz devices surrounding. TODO setup a procedure to change the master board channel, currently hard-coded in the firmware.
Ideally a free channel should be used, with a minimum of 2.4Ghz devices surrounding. The master board will automaticaly detect the chanel used at startup.

The interface on the PC needs to support monitor mode and injection. ASUS PCE-AC51 has been tested with a RT Preempt patched OS.


Data packet
-----------

### Current protocol version: **3**
### Current protocol version: **4**

Both WiFi and Ethernet use the same data packet format.

Expand All @@ -58,17 +52,21 @@ Session ID | SPI connected
--- | ---
2 bytes | 1 byte

The **SPI connected** field contains an 8 bit integer, each bit of which tells whether or not the corresponding SPI slave is connected (Least significant bit: SPI0, most significant bit: SPI7).


The **SPI connected** field contains an 8 bit integer, each bit of which tells whether or not the corresponding SPI slave is connected (Least significant bit: SPI0, most significant bit: SPI7).
### Packets
Both **Command** and **Sensor** packets encapsulate 6 BLMC µDriver SPI interface packets, without the **Index** and **CRC** fields. Additional, sensor packets also include IMU measurement and AHRS estimation.

### Sensor packet (200 Bytes)
Session ID | µDriver0 | µDriver1 | µDriver2 | µDriver3 | µDriver4 | µDriver5 | IMU | Sensor Index | Packet Loss | Last Command Index
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
2 Bytes | 28 Bytes | 28 Bytes | 28 Bytes | 28 Bytes | 28 Bytes | 28 Bytes | 24 Bytes | 2 Bytes | 2 Bytes | 2 Bytes
### Sensor packet (212 Bytes)
Session ID | µDriver0 | µDriver1 | µDriver2 | µDriver3 | µDriver4 | µDriver5 | IMU | Power Board |Sensor Index | Packet Loss | Last Command Index
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
2 Bytes | 28 Bytes | 28 Bytes | 28 Bytes | 28 Bytes | 28 Bytes | 28 Bytes | 24 Bytes | 12 Bytes | 2 Bytes | 2 Bytes | 2 Bytes

**µDriverX** corresponds to a BLMC µDriver SPI interface sensor packet without the CRC and index fields.

**Power Board** correspond the powerboard measurment data composed of bus current, voltage and energy.

**IMU** is composed of Accelerometer, Gyroscope, AHRS, and estimation of Linear Acceleration (without gravity) data:

AccX | AccY | AccZ | GyrX | GyrY | GyrZ | AHRS Roll | AHRS Pitch | AHRS Yaw | LinAccX | LinAccY | LinAccZ
Expand Down
21 changes: 21 additions & 0 deletions firmware/components/quad_crc/quad_crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,25 @@ uint32_t CRC_compute(uint8_t *bytes, int len) {

bool CRC_check(uint8_t *bytes, int len) {
return (CRC_compute(bytes, len) == 0);
}


uint16_t crc16_ccitt(uint8_t * data, int len)
{
uint16_t crc = 0xFFFF;
for (unsigned int i = 0; i < len; ++i) {
uint16_t dbyte = data[i];
crc ^= dbyte << 8;
for (uint8_t j = 0; j < 8; ++j) {
uint16_t mix = crc & 0x8000;
crc = (crc << 1);
if (mix)
crc = crc ^ 0x1021;
}
}
return crc;
}

bool crc16_ccitt_check(uint8_t * data, int len){
return (crc16_ccitt(data, len) == 0);
}
3 changes: 2 additions & 1 deletion firmware/components/quad_crc/quad_crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

bool CRC_check(uint8_t *bytes, int len);
uint32_t CRC_compute(uint8_t *bytes, int len);

uint16_t crc16_ccitt(uint8_t * data, int len);
bool crc16_ccitt_check(uint8_t * data, int len);
#endif
13 changes: 12 additions & 1 deletion firmware/main/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

#include <stdint.h>

#define PROTOCOL_VERSION 3
#define PROTOCOL_VERSION 4

union floatbytes{
float f;
uint8_t b[4];
};
struct sensor_data {
uint16_t status;
uint16_t timestamp;
Expand Down Expand Up @@ -32,6 +36,12 @@ struct imu_data {
int16_t linear_acceleration[3];
} __attribute__((packed));

struct powerboard_data {
uint16_t vbus;
int16_t vshunt;
float energy;
} __attribute__((packed));

struct wifi_eth_packet_init {
uint16_t protocol_version; // used to ensure both the interface and the firmware use the same protocol
uint16_t session_id;
Expand All @@ -53,6 +63,7 @@ struct wifi_eth_packet_sensor {
uint16_t session_id;
struct sensor_data sensor[CONFIG_N_SLAVES];
struct imu_data imu;
struct powerboard_data powerboard;
uint16_t sensor_index;
uint16_t packet_loss;
uint16_t last_cmd_index;
Expand Down
16 changes: 16 additions & 0 deletions firmware/main/masterboard_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,22 @@ static void periodic_timer_callback(void *arg)
wifi_eth_tx_data.sensor[i].status = 0xf; // specifying that the transaction failed in the sensor packet
}
}

//Slave 7 is always the power board
uint8_t tx_spi_powerboard[12]={0xaa,0xbb,0xcc,0xdd,0xee,0xff,0xaa,0xbb,0xcc,0xdd,0xee,0xff}; //Fake packet, cmd to the power board are not yet implemented
uint8_t rx_spi_powerboard[12]={0};
spi_send(7, tx_spi_powerboard,rx_spi_powerboard, 12);
if (crc16_ccitt_check(rx_spi_powerboard,12)) //Packet valid?
{
wifi_eth_tx_data.powerboard.vbus = (rx_spi_powerboard[2]<<8) | rx_spi_powerboard[3];
wifi_eth_tx_data.powerboard.vshunt = (rx_spi_powerboard[4]<<8) | rx_spi_powerboard[5];
union floatbytes fb;
fb.b[3] = rx_spi_powerboard[6];
fb.b[2] = rx_spi_powerboard[7];
fb.b[1] = rx_spi_powerboard[8];
fb.b[0] = rx_spi_powerboard[9];
wifi_eth_tx_data.powerboard.energy = fb.f;
}
}

/* Get IMU latest data*/
Expand Down
Loading