None
bool32 Button 1 Status
bool32 Button 2 Status
bool32 Button 3 Status
bool32 Button 4 Status
bool32 Button 5 Status
bool32 Button 6 Status
bool32 Button 7 Status
bool32 Button 8 Status
uint8_t *txbuf;
uint8_t *rxbuf;
bool btn1;
bool btn2;
bool btn3;
bool btn4;
bool btn5;
bool btn6;
bool btn7;
bool btn8;
// SETUP
// ------------------
void setup() {
static uint8_t _txbuf[8] __attribute__((section(".sram2")));
static uint8_t _rxbuf[8] __attribute__((section(".sram2")));
txbuf = _txbuf;
rxbuf = _rxbuf;
}
// LOOP
// ------------------
void loop() {
chThdSleepMilliseconds(1);
txbuf[0] = 0x42;
txbuf[1] = 0;
txbuf[2] = 0;
txbuf[3] = 0;
txbuf[4] = 0;
txbuf[5] = 0;
txbuf[6] = 0;
txbuf[7] = 0;
spiSelect(&SPID1);
spiSend(&SPID1, 1, &txbuf[0]);
palSetPadMode(GPIOA, 7, PAL_MODE_INPUT); // MOSI -> tristate
spiReceive(&SPID1, 4, &rxbuf[0]);
spiUnselect(&SPID1);
palSetPadMode(GPIOA, 7, PAL_MODE_ALTERNATE(5)); // MOSI -> output
btn1 = rxbuf[0] & 0x01 ? true : false;
btn2 = rxbuf[1] & 0x01 ? true : false;
btn3 = rxbuf[2] & 0x01 ? true : false;
btn4 = rxbuf[3] & 0x01 ? true : false;
btn5 = rxbuf[0] & 0x10 ? true : false;
btn6 = rxbuf[1] & 0x10 ? true : false;
btn7 = rxbuf[2] & 0x10 ? true : false;
btn8 = rxbuf[3] & 0x10 ? true : false;
}
// ------------------------------------------------------------------------------------------------
// THREADS
// ------------------------------------------------------------------------------------------------
msg_t ThreadX2() {
setup();
while (!chThdShouldTerminate()) {
loop();
chThdSleepMilliseconds(1);
}
chThdExit((msg_t)0);
}
static msg_t ThreadX(void *arg) { ((attr_parent *)arg)->ThreadX2(); }
WORKING_AREA(waThreadX, 1024);
Thread *Thd;
Thd = chThdCreateStatic(waThreadX, sizeof(waThreadX), NORMALPRIO, ThreadX,
(void *)this);
btn1 = false;
btn2 = false;
btn3 = false;
btn4 = false;
btn5 = false;
btn6 = false;
btn7 = false;
btn8 = false;
outlet_button1 = btn1;
outlet_button2 = btn2;
outlet_button3 = btn3;
outlet_button4 = btn4;
outlet_button5 = btn5;
outlet_button6 = btn6;
outlet_button7 = btn7;
outlet_button8 = btn8;
chThdTerminate(Thd);
chThdWait(Thd);