stash
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
j3d1 2023-02-19 13:36:53 +01:00
parent 2b07102b69
commit 1cd16f0aad
9 changed files with 326 additions and 162 deletions

View file

@ -86,49 +86,100 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
}
}
typedef union {
struct {
unsigned dat: 12;
unsigned addr: 4;
} __attribute__((packed));
uint16_t _;
} cc48x6_frame;
typedef enum {
CMD_NONE = 0,
CMD_READ = 1,
CMD_WRITE = 2,
CMD_INIT = 3,
CMD_RESET = 4,
CMD_VERSION = 5,
CMD_CONFIG = 6,
CMD_PING = 7
} bus_cmd;
typedef union {
struct {
unsigned dat: 16;
unsigned addr: 8;
unsigned cmd: 8;
} __attribute__((packed));
uint32_t _;
} request_frame;
typedef union {
struct {
unsigned dat: 16;
unsigned addr: 8;
unsigned flags: 7;
unsigned: 1;
} __attribute__((packed));
uint32_t _;
} reply_frame;
uint8_t addr = 0;
void HAL_SPI_CpltCallback(SPI_HandleTypeDef *hspi) {
if(RX_Buffer[0]) {
union {
struct {
unsigned data: 12;
unsigned chan: 4;
} __attribute__((packed));
uint16_t raw;
request_frame req = {._ = ((uint32_t) RX_Buffer[0]) << 16 | (uint32_t) RX_Buffer[1]};
reply_frame rep = {._ = 0};
} frame = {.raw=RX_Buffer[0]};
toggle = !toggle;
counter++;
if(toggle) {
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_SET);
if(addr == 0) {
bool init = HAL_GPIO_ReadPin(INIT_IN_GPIO_Port, INIT_IN_Pin);
if(init && req.cmd == CMD_INIT) {
addr = req.addr;
} else if(init) {
rep.flags = 5;
} else {
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_RESET);
rep.flags = 7;
}
} else if(addr == req.addr) {
if(req.cmd == CMD_PING){
rep.dat = req.dat;
}else {
cc48x6_frame frame = {._=req.dat};
if(frame.chan <= 6 && frame.chan >= 1) {
frame.chan--;
if(frame.data > 0x300) {
mem[frame.chan * 2 + 1] = 0xFF;
mem[frame.chan * 2] = frame.data;
counter++;
toggle = !toggle;
if(toggle) {
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_SET);
} else {
mem[frame.chan * 2 + 1] = (frame.data * 0xFF) / 0x300;
mem[frame.chan * 2] = 0x300;
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_RESET);
}
dirty = true;
TX_Buffer[0] = RX_Buffer[0];
if(frame.addr <= 6 && frame.addr >= 1) {
frame.addr--;
if(frame.dat > 0x300) {
mem[frame.addr * 2 + 1] = 0xFF;
mem[frame.addr * 2] = frame.dat;
} else {
mem[frame.addr * 2 + 1] = (frame.dat * 0xFF) / 0x300;
mem[frame.addr * 2] = 0x300;
}
} else if(frame.chan == 15 && frame.data < 8) {
TX_Buffer[0] = conf[frame.data];
} else {
TX_Buffer[0] = 0;
dirty = true;
rep.dat = req.dat;
} else if(frame.addr == 15 && frame.dat < 8) {
TX_Buffer[0] = conf[frame.dat];
}
}
}
HAL_SPI_TransmitReceive_DMA(&hspi2, TX_Buffer, RX_Buffer, 1);
TX_Buffer[0] = rep._ >> 16;
TX_Buffer[1] = rep._ & 0xFFFF;
HAL_SPI_TransmitReceive_DMA(&hspi2, TX_Buffer, RX_Buffer, 2);
HAL_GPIO_WritePin(INIT_OUT_GPIO_Port, INIT_OUT_Pin, GPIO_PIN_SET);
for (volatile int i = 0; i < 3; i++);
@ -229,7 +280,7 @@ int main(void) {
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_SET);
HAL_SPI_Receive_DMA(&hspi2, (uint8_t *) RX_Buffer, 1);
HAL_SPI_Receive_DMA(&hspi2, (uint8_t *) RX_Buffer, 2);