digital-inputs: CAN-tx with FIFO

This commit is contained in:
andreika-git 2023-08-27 00:32:15 +03:00
parent ad847ed654
commit 0d99de62d7
4 changed files with 57 additions and 9 deletions

View File

@ -149,6 +149,7 @@ ASMXSRC = $(ALLXASMSRC)
INCDIR = $(CONFDIR) \ INCDIR = $(CONFDIR) \
$(ALLINC) \ $(ALLINC) \
../../ext/libfirmware \ ../../ext/libfirmware \
../../ext/libfirmware/util/include/rusefi \
source \ source \

View File

@ -1,3 +1,4 @@
#include <initializer_list>
#include "ch.h" #include "ch.h"
#include "hal.h" #include "hal.h"
#include "chprintf.h" #include "chprintf.h"
@ -5,6 +6,7 @@
#include "can.h" #include "can.h"
#include "test_logic.h" #include "test_logic.h"
#include "can/can_common.h" #include "can/can_common.h"
#include "containers/fifo_buffer.h"
#include "global.h" #include "global.h"
extern BaseSequentialStream *chp; extern BaseSequentialStream *chp;
@ -17,6 +19,9 @@ static const CANConfig cancfg = {
static bool isGoodCanPackets = true; static bool isGoodCanPackets = true;
static bool hasReceivedAnalog = false; static bool hasReceivedAnalog = false;
static int outputCount = -1;
static fifo_buffer_sync<CANTxFrame> txFifo;
static void canPacketError(const char *msg, ...) { static void canPacketError(const char *msg, ...) {
va_list vl; va_list vl;
@ -36,6 +41,10 @@ bool isHappyCanTest() {
return isGoodCanPackets && hasReceivedAnalog; return isGoodCanPackets && hasReceivedAnalog;
} }
int getOutputCount() {
return outputCount;
}
static bool wasBoardDetectError = false; static bool wasBoardDetectError = false;
static void receiveBoardStatus(const uint8_t msg[8]) { static void receiveBoardStatus(const uint8_t msg[8]) {
@ -61,6 +70,12 @@ static void receiveBoardStatus(const uint8_t msg[8]) {
} }
} }
static void receiveOutputMetaInfo(const uint8_t msg[8]) {
if (msg[0] == CAN_BENCH_HEADER) {
outputCount = msg[2];
}
}
static void receiveRawAnalog(const uint8_t msg[8]) { static void receiveRawAnalog(const uint8_t msg[8]) {
// wait for the BoardStatus package first // wait for the BoardStatus package first
if (currentBoard == nullptr) if (currentBoard == nullptr)
@ -103,25 +118,53 @@ void processCanRxMessage(const CANRxFrame& frame) {
printRxFrame(frame, "BENCH_TEST_EVENT_COUNTERS"); printRxFrame(frame, "BENCH_TEST_EVENT_COUNTERS");
} else if (CAN_EID(frame) == BENCH_TEST_BUTTON_COUNTERS) { } else if (CAN_EID(frame) == BENCH_TEST_BUTTON_COUNTERS) {
printRxFrame(frame, "BENCH_TEST_BUTTON_COUNTERS"); printRxFrame(frame, "BENCH_TEST_BUTTON_COUNTERS");
} else if (CAN_EID(frame) == BENCH_TEST_IO_META_INFO) {
printRxFrame(frame, "BENCH_TEST_IO_META_INFO");
receiveOutputMetaInfo(frame.data8);
} }
} }
static void sendCanTxMessage(const CANTxFrame & frame) {
if (!txFifo.put(frame)) {
chprintf(chp, "CAN sendCanTxMessage() problems");
}
}
static void sendCanTxMessage(int EID, std::initializer_list<uint8_t> data) {
CANTxFrame txmsg;
txmsg.IDE = CAN_IDE_EXT;
txmsg.EID = EID;
txmsg.RTR = CAN_RTR_DATA;
txmsg.DLC = 8;
size_t idx = 0;
for (uint8_t v : data) {
txmsg.data8[idx] = v;
idx++;
}
sendCanTxMessage(txmsg);
}
void sendCanPinState(uint8_t pinIdx, bool isSet) {
sendCanTxMessage(BENCH_TEST_IO_CONTROL, { (uint8_t)(isSet ? CAN_BENCH_GET_SET : CAN_BENCH_GET_CLEAR), pinIdx });
}
void setOutputCountRequest() {
sendCanTxMessage(BENCH_TEST_IO_CONTROL, { CAN_BENCH_GET_COUNT });
}
static THD_WORKING_AREA(can_tx_wa, THREAD_STACK); static THD_WORKING_AREA(can_tx_wa, THREAD_STACK);
static THD_FUNCTION(can_tx, p) { static THD_FUNCTION(can_tx, p) {
CANTxFrame txmsg; CANTxFrame txmsg;
(void)p; (void)p;
chRegSetThreadName("transmitter"); chRegSetThreadName("transmitter");
txmsg.IDE = CAN_IDE_EXT;
txmsg.EID = 0x01234567;
txmsg.RTR = CAN_RTR_DATA;
txmsg.DLC = 8;
txmsg.data32[0] = 0x55AA55AA;
txmsg.data32[1] = 0x00FF00FF;
while (true) { while (true) {
if (txFifo.get(txmsg, TIME_MS2I(100))) {
canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, TIME_MS2I(100)); canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, TIME_MS2I(100));
chThdSleepMilliseconds(500); }
} }
} }

View File

@ -17,4 +17,8 @@
void initCan(); void initCan();
void startNewCanTest(); void startNewCanTest();
bool isHappyCanTest(); bool isHappyCanTest();
int getOutputCount();
void setOutputCountRequest();
void sendCanPinState(uint8_t pinIdx, bool isSet);

@ -1 +1 @@
Subproject commit d85c4cdefdc7730b74b6ea4be2652a4564fb8503 Subproject commit aaafa2e3e1cff8e7cb3a0b15ce8ad443a91830e0