diff --git a/firmware/module_io/Core/Inc/configuration.h b/firmware/module_io/Core/Inc/configuration.h index bcce57b..db18b4e 100644 --- a/firmware/module_io/Core/Inc/configuration.h +++ b/firmware/module_io/Core/Inc/configuration.h @@ -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 \ No newline at end of file +#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 diff --git a/firmware/module_io/Core/Inc/datapacket.h b/firmware/module_io/Core/Inc/datapacket.h index da39b16..ad459ec 100644 --- a/firmware/module_io/Core/Inc/datapacket.h +++ b/firmware/module_io/Core/Inc/datapacket.h @@ -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; @@ -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 { @@ -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 diff --git a/firmware/module_io/Core/Inc/loop.h b/firmware/module_io/Core/Inc/loop.h deleted file mode 100644 index 7fc1965..0000000 --- a/firmware/module_io/Core/Inc/loop.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef LOOP_H -#define LOOP_H - -#include "main.h" - -void setup(void); -void loop(void); - -extern CAN_HandleTypeDef hcan; -extern I2C_HandleTypeDef hi2c1; -extern SPI_HandleTypeDef hspi2; - -#endif \ No newline at end of file diff --git a/firmware/module_io/Core/Src/datapacket.c b/firmware/module_io/Core/Src/datapacket.c index 567bc80..3af7b6e 100644 --- a/firmware/module_io/Core/Src/datapacket.c +++ b/firmware/module_io/Core/Src/datapacket.c @@ -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])}") diff --git a/firmware/module_io/Core/Src/interface_module.c b/firmware/module_io/Core/Src/interface_module.c index 95c67ff..7d70da1 100644 --- a/firmware/module_io/Core/Src/interface_module.c +++ b/firmware/module_io/Core/Src/interface_module.c @@ -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. @@ -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 @@ -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(); @@ -107,7 +112,7 @@ 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!"); @@ -115,7 +120,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) { pk->err = 1; @@ -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: { @@ -173,7 +170,7 @@ 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; @@ -181,7 +178,7 @@ void getCanMessages(void) { 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); @@ -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); @@ -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); diff --git a/firmware/module_io/Core/Src/loop.c b/firmware/module_io/Core/Src/loop.c deleted file mode 100644 index 31a687a..0000000 --- a/firmware/module_io/Core/Src/loop.c +++ /dev/null @@ -1,125 +0,0 @@ -// Header for this file -#include "loop.h" -// uint types -#include -// USB serial printing -#include "uprintf.h" -// Other USB things -#include "usb_device.h" -#include "usbd_cdc_if.h" -// Data packets from CAN -#include "datapacket.h" - -// There's an hcan variable somewhere. Compiler - Please find it and let me use it. -extern CAN_HandleTypeDef hcan; - -void sendDemoCanMessage() { - CAN_TxHeaderTypeDef TxHeader; - uint8_t TxData[8]; - uint32_t TxMailbox = 0xDEADBEEF; - - // Set up a demo message - TxHeader.IDE = CAN_ID_STD; - TxHeader.StdId = 0x555; - TxHeader.RTR = CAN_RTR_DATA; - TxHeader.DLC = 8; - - TxData[0] = 0x00; - TxData[1] = 0x01; - TxData[2] = 0x02; - TxData[3] = 0x03; - TxData[4] = 0x04; - TxData[5] = 0x05; - TxData[6] = 0x06; - TxData[7] = 0x07; - - // uprintf("State: %d ", hcan.State); - HAL_StatusTypeDef s = HAL_CAN_AddTxMessage(&hcan, &TxHeader, TxData, &TxMailbox); - if(s != HAL_OK) { - uprintf("TX Error %d", s); - } - - // Wait a moment for the message to actually transmit - HAL_Delay(1); - // Receive the packet - uprintf("RX "); - int num = HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0); - uprintf("Size: %1d ", num); - CAN_RxHeaderTypeDef RxHeader; - uint8_t RxData[8]; - s = HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, RxData); - // uprintf("Status: %08x ", HAL_CAN_GetState(&hcan)); - if(s != HAL_OK) { - uprintf("RX Error %d", s); - } else { - uprintf("ID: %03x ", RxHeader.StdId); - uprintf("Length: %1d ", RxHeader.DLC); - uprintf("Data: ["); - for(int i = 0; i < RxHeader.DLC; i++) { - if(i!=0) { - uprintf(","); - } - uprintf("%02x", RxData[i]); - } - uprintf("]"); - } - - uprintf("\n"); - - -} - -// Run once at the beginning -void setup() { - // Demo data packet - -} - -// HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); - -int count = 0; -// Called repeatedly while running -void loop() { - // Do stuff on light off - - DataPacket dp; - dp.id = 0x5A; - dp.err = 0; - dp.reply = 1; - dp.data.seq = 0x47; - dp.data.cmd = 0x28; - dp.data.args[0] = 0x00; - dp.data.args[1] = 0x01; - dp.data.args[2] = 0x02; - dp.data.args[3] = 0x03; - dp.datasize = 6; - - HAL_Delay(250); - HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); - uprintf("Test %4d", count++); - // sendDemoCanMessage(); - int ts = writeDatapacketToCan(&dp); - uprintf("[%d]: ", ts); - printDataPacket(&dp); - - // Wait and turn light on - HAL_Delay(250); - HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET); - DataPacket rdp; - rdp.id = 0; - rdp.err = 0; - rdp.reply = 0; - rdp.data.seq = 0; - rdp.data.cmd = 0; - for(int i = 0; i < 6; i++) { - rdp.data.args[i] = 0; - } - rdp.datasize = 0; - int rs = readDataPacketFromCan(&rdp); - uprintf(" Reply [%d]: ", rs); - printDataPacket(&rdp); - uprintf(" FIFO:%d", HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0)); - uprintf("\n"); - - -} \ No newline at end of file diff --git a/firmware/module_io/Core/Src/main.c b/firmware/module_io/Core/Src/main.c index 70b8d4c..da13d6a 100644 --- a/firmware/module_io/Core/Src/main.c +++ b/firmware/module_io/Core/Src/main.c @@ -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" diff --git a/firmware/module_io/Makefile b/firmware/module_io/Makefile index dd1bd94..e1b07ae 100644 --- a/firmware/module_io/Makefile +++ b/firmware/module_io/Makefile @@ -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 \ diff --git a/firmware/module_io/module_io.bin b/firmware/module_io/module_io.bin index ac3cfb7..446e811 100755 Binary files a/firmware/module_io/module_io.bin and b/firmware/module_io/module_io.bin differ