Skip to content

Commit

Permalink
Fixed data format bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-C committed Jun 3, 2024
1 parent 12d9d59 commit 56ed0fc
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 173 deletions.
12 changes: 11 additions & 1 deletion firmware/module_io/Core/Inc/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@
|| (defined(CONFIG_OUTPUTS) && (defined(CONFIG_ADC) || defined(CONFIG_I2C))) \
|| (defined(CONFIG_ADC) && defined(CONFIG_I2C))
#error("You must specify exactly 1 of CONFIG_OUTPUTS, CONFIG_ADC, or CONFIG_I2C")
#endif
#endif

#ifdef CONFIG_OUTPUTS
#define BASE_ADDR_OFFSET 0
#endif
#ifdef CONFIG_ADC
#define BASE_ADDR_OFFSET 8
#endif
#ifdef CONFIG_I2C
#define BASE_ADDR_OFFSET 12
#endif
9 changes: 3 additions & 6 deletions firmware/module_io/Core/Inc/datapacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ self.cmd is the command
self.data is the array of 6 bytes of data
*/

#define BIGLITTLEDATA(pk) ((BigLittleData*) pk->data.bytes)
typedef struct BigLittleData_t {
uint32_t big;
uint16_t little;
Expand All @@ -30,10 +31,7 @@ typedef struct CanData_t {
// The command the packet is about
uint8_t cmd;
// Arguments or data for the packet
uint8_t args[6];
// union {
// BigLittleData bld;
// };
uint8_t bytes[6];
} CanData;

typedef struct DataPacket_t {
Expand All @@ -48,10 +46,9 @@ typedef struct DataPacket_t {
// The size of the packet. This includes the SEQ and CMD bytes. Max 8.
uint16_t datasize;
// Note: data must be aligned with the middle of a word, otherwise BigLittleData access will fail.

// The data of the packet.
CanData data;

} DataPacket;

#define DATAPACKET_RESVD_BIT 8
Expand Down
13 changes: 0 additions & 13 deletions firmware/module_io/Core/Inc/loop.h

This file was deleted.

2 changes: 1 addition & 1 deletion firmware/module_io/Core/Src/datapacket.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern CAN_HandleTypeDef hcan;
void printDataPacket(DataPacket* pkt) {
uprintf("%c%d %c%02X #%02x !%02x", (pkt->err)?'E':'.', pkt->reserved&1, (pkt->reply)?'<':'>', pkt->id, pkt->data.seq, pkt->data.cmd);
for(int i = 0; i < pkt->datasize-2; i++) {
uprintf(" %02X", pkt->data.args[i]);
uprintf(" %02X", pkt->data.bytes[i]);
}
// print(f"{'E' if self.err == 1 else '.'}{self.rsvd:01b} {'<' if self.reply else '>'}{self.id:02X} #{self.seq:02X} !{self.cmd:02x}: {' '.join([f'{b:02X}' for b in self.data])}")

Expand Down
46 changes: 21 additions & 25 deletions firmware/module_io/Core/Src/interface_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ uint8_t baseAddress = 0;
// Represents if a fatal error has occurred
int fatal = 0;

#ifdef CONFIG_OUTPUTS
GPIO_TypeDef* outputPorts[] = {LED_GPIO_Port, OUT1_GPIO_Port, OUT2_GPIO_Port, OUT3_GPIO_Port, OUT4_GPIO_Port, OUT5_GPIO_Port, OUT6_GPIO_Port, OUT7_GPIO_Port};
uint16_t outputPins[] = {LED_Pin, OUT1_Pin, OUT2_Pin, OUT3_Pin, OUT4_Pin, OUT5_Pin, OUT6_Pin, OUT7_Pin};
#endif

void onFatalError(void) {
fatal = 1;
// Do things here to set things to a safe state.
Expand Down Expand Up @@ -68,7 +73,7 @@ void compressUID(uint8_t* dest) {
}

int processPacket(DataPacket* pk) {
uint8_t subid = pk->id & 0xF;
uint8_t subid = (pk->id & 0xF) - BASE_ADDR_OFFSET;
uprintf("#%d ", subid);

// If the packet was an error, check if it's one we care about
Expand All @@ -80,7 +85,7 @@ int processPacket(DataPacket* pk) {
compressUID(tempid);
int f = 0;
for(int i = 0; i < 6; i++) {
f &= tempid[i] != pk->data.args[i];
f &= tempid[i] != pk->data.bytes[i];
}
if(f) {
onFatalError();
Expand All @@ -107,15 +112,15 @@ int processPacket(DataPacket* pk) {
uprintf("Command too short!");
return 3;
}
uprintf(" Exec time! ");
uprintf("Exec time! ");
switch(pk->data.cmd) {
case BUSCMD_CLAIM_ID: {
uprintf("Id claim commnd!");
uint8_t tempid[6];
compressUID(tempid);
int f = 0;
for(int i = 0; i < 6; i++) {
f &= tempid[i] != pk->data.args[i];
f &= tempid[i] != pk->data.bytes[i];
}
if(f) {
pk->err = 1;
Expand All @@ -129,39 +134,31 @@ int processPacket(DataPacket* pk) {
uprintf("UID low read");
pk->reply = 1;
pk->datasize = 8;
copyUID(pk->data.args, 6, 0);
copyUID(pk->data.bytes, 6, 0);
} break;
case BUSCMD_READ_ID_HIGH: {
uprintf("UID low high");
pk->reply = 1;
pk->datasize = 8;
copyUID(pk->data.args, 6, 6);
copyUID(pk->data.bytes, 6, 6);
writeDatapacketToCan(pk);
} break;
#ifdef CONFIG_OUTPUTS
case BUSCMD_READ_VALUE: {
uprintf("Read value command");
uint32_t val = HAL_GPIO_ReadPin(LED_GPIO_Port, LED_Pin);
uint32_t val = HAL_GPIO_ReadPin(outputPorts[subid], outputPins[subid]);
pk->datasize = 8;
BigLittleData* bld = (BigLittleData*) pk->data.args;
// pk->data.bld.big = val;
bld->big = val;
// BigLittleData* bld = BIGLITTLEDATA(pk);
BIGLITTLEDATA(pk)->big = val;
// bld->big = val;
pk->reply = 1;
writeDatapacketToCan(pk);
uprintf("\nSending reply ");
printDataPacket(pk);
writeDatapacketToCan(pk);
} break;
case BUSCMD_WRITE_VALUE: {
uprintf("Write value command");
BigLittleData* bld = (BigLittleData*) pk->data.args;
uprintf(" %08x %08x %08x %08x", bld, &(bld->little), &(pk->data.args[4]), pk->data.args);
uint32_t value2 = 758;
uprintf(" %08x %08x", &bld, &value2);
uprintf(" L:%x ", bld->little);
uprintf(" B:%x ", bld->big);
uint32_t value = (uint32_t) pk->data.args[0];
// uint32_t value = 1;
uprintf("I should set %d to %d", subid, value);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, value);
HAL_GPIO_WritePin(outputPorts[subid], outputPins[subid], BIGLITTLEDATA(pk)->big);
} break;
#endif
default: {
Expand All @@ -173,15 +170,15 @@ int processPacket(DataPacket* pk) {

void getCanMessages(void) {
while(!fatal && HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0)) {
uprintf(" Got message! ");
uprintf("Got message! ");
DataPacket pk;
pk.id = 0;
pk.err = 0;
pk.reply = 0;
pk.data.seq = 0;
pk.data.cmd = 0;
for(int i = 0; i < 6; i++) {
pk.data.args[i] = 0;
pk.data.bytes[i] = 0;
}
pk.datasize = 0;
int rs = readDataPacketFromCan(&pk);
Expand All @@ -190,7 +187,6 @@ void getCanMessages(void) {
return;
}
printDataPacket(&pk);
uprintf(" FIFO:%d", HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0));

uint8_t bid = pk.id & 0xF0;
uprintf(" BID:%2x", bid);
Expand Down Expand Up @@ -280,7 +276,7 @@ void doEverything(void) {
iddp.reply = 0; // Not a reply
iddp.data.seq = 0; // Don't care what it is
iddp.data.cmd = BUSCMD_CLAIM_ID; // Sets the ID to claim
compressUID(iddp.data.args);
compressUID(iddp.data.bytes);
iddp.datasize = 8; // Include the sequence number and command in this count
// Send ID claim command
int freeTX = HAL_CAN_GetTxMailboxesFreeLevel(&hcan);
Expand Down
125 changes: 0 additions & 125 deletions firmware/module_io/Core/Src/loop.c

This file was deleted.

1 change: 0 additions & 1 deletion firmware/module_io/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "loop.h"
#include "uprintf.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
Expand Down
1 change: 0 additions & 1 deletion firmware/module_io/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ BUILD_DIR = build
######################################
# C sources
C_SOURCES = \
Core/Src/loop.c \
Core/Src/main.c \
Core/Src/stm32f0xx_it.c \
Core/Src/stm32f0xx_hal_msp.c \
Expand Down
Binary file modified firmware/module_io/module_io.bin
Binary file not shown.

0 comments on commit 56ed0fc

Please sign in to comment.