rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
serial_can.cpp
Go to the documentation of this file.
1/**
2 * @file serial_can.cpp
3 *
4 * This code is a bridge between a serial streaming used by TS and a packet-frame CAN-bus, using the ISO-TP protocol.
5 * ISO 15765-2, or ISO-TP (Transport Layer), which is an international standard for sending data packets over a CAN-Bus.
6 * https://en.wikipedia.org/wiki/ISO_15765-2
7 *
8 * @date Aug 1, 2020
9 * @author andreika <promethePRINTus.pcb@gmail.com>
10 * @author Andrey Belomutskiy, (c) 2012-2020
11 */
12
13#include "pch.h"
14
15// todo: this file is asking to improve conditional compilation. unit_tests and cypress/kinetis are both special cases
16#if HAL_USE_CAN || EFI_UNIT_TEST
17#include "serial_can.h"
18#include "can.h"
19#include "can_msg_tx.h"
20#endif // HAL_USE_CAN || EFI_UNIT_TEST
21
22
23#if HAL_USE_CAN
24// this one installs itself as top level CAN bus listener with static frame ID
26// for RX, this one delegates to above FIFO via global field
28
29static CanStreamerState state(&transport, /*bus*/0, CAN_ECU_SERIAL_TX_ID);
30#endif // HAL_USE_CAN
31
32#if HAL_USE_CAN || EFI_UNIT_TEST
33
34static int isoTpPacketCounter = 0;
35
36/**
37 * incoming data main entry point
38 */
39void CanTsListener::decodeFrame(const CANRxFrame& frame, efitick_t /*nowNt*/) {
40 // CAN ID filtering happens in base class, by the time we are here we know it's the CAN_ECU_SERIAL_RX_ID packet
41 // todo: what if the FIFO is full?
42 CanRxMessage msg(frame);
44 PRINT("*** INFO: CanTsListener decodeFrame %d" PRINT_EOL, isoTpPacketCounter++);
45 }
46 if (!rxFifo.put(msg)) {
47 warning(ObdCode::CUSTOM_ERR_CAN_COMMUNICATION, "CAN sendDataTimeout() problems");
48 }
49}
50
51#if HAL_USE_CAN
52
56
58 // we do nothing here - see CanTxMessage::~CanTxMessage()
59 return CAN_MSG_OK;
60}
61
63 // see CanTsListener and processCanRxMessage()
64 CanRxMessage msg;
65 if (this->source->get(msg, timeout)) {
66 *crfp = msg.frame;
67 return CAN_MSG_OK;
68 }
69 return CAN_MSG_TIMEOUT;
70}
71
73 transport.init();
74}
75
76msg_t canStreamAddToTxTimeout(size_t *np, const uint8_t *txbuf, sysinterval_t timeout) {
77 return state.streamAddToTxTimeout(np, txbuf, timeout);
78}
79
80msg_t canStreamFlushTx(sysinterval_t timeout) {
81 return state.streamFlushTx(timeout);
82}
83
84 // np uses in/out parameter approach. Yes ChibiOS does same but still evil!
85 // in entry: number of data frames to receive
86 // on exit the number of frames actually received
87msg_t canStreamReceiveTimeout(size_t *np, uint8_t *rxbuf, sysinterval_t timeout) {
88 return state.streamReceiveTimeout(np, rxbuf, timeout);
89}
90
91#endif /* HAL_USE_CAN */
92
93
94
95#endif // HAL_USE_CAN || EFI_UNIT_TEST
void registerCanListener(CanListener &listener)
Definition can_rx.cpp:84
int32_t can_msg_t
Definition can_mocks.h:6
int32_t can_sysinterval_t
Definition can_mocks.h:7
CANRxFrame frame
Definition isotp.h:66
virtual bool get(CanRxMessage &item, int timeout)=0
can_msg_t streamAddToTxTimeout(size_t *np, const uint8_t *txbuf, can_sysinterval_t timeout)
Definition isotp.cpp:248
CanRxMessageSource * source
Definition serial_can.h:40
virtual can_msg_t transmit(const CanTxMessage *ctfp, can_sysinterval_t timeout) override
virtual can_msg_t receive(CANRxFrame *crfp, can_sysinterval_t timeout) override
fifo_buffer_sync< CanRxMessage, CAN_FIFO_FRAME_SIZE > rxFifo
Definition serial_can.h:28
virtual void decodeFrame(const CANRxFrame &frame, efitick_t nowNt)
static constexpr engine_configuration_s * engineConfiguration
bool warning(ObdCode code, const char *fmt,...)
@ CUSTOM_ERR_CAN_COMMUNICATION
state("state", SensorCategory.SENSOR_INPUTS, FieldType.INT8, 1879, 1.0, -1.0, -1.0, "")
msg_t canStreamReceiveTimeout(size_t *np, uint8_t *rxbuf, sysinterval_t timeout)
static CanStreamerState state & transport
static int isoTpPacketCounter
void tsOverCanInit()
static CanTsListener g_listener
msg_t canStreamAddToTxTimeout(size_t *np, const uint8_t *txbuf, sysinterval_t timeout)
msg_t canStreamFlushTx(sysinterval_t timeout)