rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Functions | Variables
can_hw.cpp File Reference

Detailed Description

CAN bus low level code.

todo: this file should be split into two - one for CAN transport level ONLY and another one with actual messages

See also
can_verbose.cpp for higher level logic
obd2.cpp for OBD-II messages via CAN
Date
Dec 11, 2013
Author
Andrey Belomutskiy, (c) 2012-2020

Definition in file can_hw.cpp.

Functions

const CANConfig * findCanConfig (can_baudrate_e rate)
 
static CCM_OPTIONAL CanRead canRead1 (0)
 
static CCM_OPTIONAL CanRead canRead2 (1)
 
static CCM_OPTIONAL CanRead canRead3 (2)
 
static CANDriver * getCanDevice (size_t index)
 
static void canInfo ()
 
void setCanType (int type)
 
void stopCanPins ()
 
void startCanPins ()
 
static void applyListenOnly (CANConfig *canConfig, bool isListenOnly)
 
void initCan ()
 
bool getIsCanEnabled (void)
 

Variables

static bool isCanEnabled = false
 
static const CANConfig canConfig_dummy
 
static CanWrite canWrite CCM_OPTIONAL
 
int txErrorCount [EFI_CAN_BUS_COUNT] = {}
 

Function Documentation

◆ applyListenOnly()

static void applyListenOnly ( CANConfig *  canConfig,
bool  isListenOnly 
)
static

Definition at line 196 of file can_hw.cpp.

196 {
197#if defined(STM32F4XX) || defined(STM32F7XX)
198 if (isListenOnly) {
199 canConfig->btr |= CAN_BTR_SILM;
200 }
201#elif defined(STM32H7XX)
202 // TODO: move to ChibiOS stm32_fdcan.h
203 #define FDCAN_CONFIG_CCCR_MON (1u << 5)
204 if (isListenOnly) {
205 canConfig->CCCR |= FDCAN_CONFIG_CCCR_MON;
206 }
207#endif
208}

Referenced by initCan().

Here is the caller graph for this function:

◆ canInfo()

static void canInfo ( )
static

Definition at line 109 of file can_hw.cpp.

109 {
110 if (!isCanEnabled) {
111 efiPrintf("CAN is not enabled, please enable & restart");
112 return;
113 }
114
116 efiPrintf("CAN1 RX %s", hwPortname(engineConfiguration->canRxPin));
118
120 efiPrintf("CAN2 RX %s", hwPortname(engineConfiguration->can2RxPin));
122
123#if (EFI_CAN_BUS_COUNT >= 3)
125 efiPrintf("CAN3 RX %s", hwPortname(engineConfiguration->can3RxPin));
127#endif
128
129 efiPrintf("type=%d canReadEnabled=%s canWriteEnabled=%s period=%d", engineConfiguration->canNbcType,
132
133 efiPrintf("CAN rx_cnt=%d/tx_ok=%d/tx_not_ok=%d",
137}
void canHwInfo(CANDriver *cand)
Definition at32_can.cpp:129
const char * getCan_baudrate_e(can_baudrate_e value)
static bool isCanEnabled
Definition can_hw.cpp:25
static CANDriver * getCanDevice(size_t index)
Definition can_hw.cpp:90
int txErrorCount[EFI_CAN_BUS_COUNT]
Definition can_hw.cpp:107
TunerStudioOutputChannels outputChannels
Definition engine.h:113
const char * boolToString(bool value)
Definition efilib.cpp:19
static EngineAccessor engine
Definition engine.h:415
static constexpr engine_configuration_s * engineConfiguration
const char * hwPortname(brain_pin_e brainPin)

Referenced by initCan(), and setCanType().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ canRead1()

static CCM_OPTIONAL CanRead canRead1 ( )
static

Referenced by initCan().

Here is the caller graph for this function:

◆ canRead2()

static CCM_OPTIONAL CanRead canRead2 ( )
static

Referenced by initCan().

Here is the caller graph for this function:

◆ canRead3()

static CCM_OPTIONAL CanRead canRead3 ( )
static

Referenced by initCan().

Here is the caller graph for this function:

◆ findCanConfig()

const CANConfig * findCanConfig ( can_baudrate_e  rate)
extern

Definition at line 36 of file can_hw.cpp.

37{
38 return &canConfig_dummy;
39}
static const CANConfig canConfig_dummy
Definition can_hw.cpp:34

Referenced by initCan().

Here is the caller graph for this function:

◆ getCanDevice()

static CANDriver * getCanDevice ( size_t  index)
static

Definition at line 90 of file can_hw.cpp.

91{
92 switch (index) {
93 case 0:
95 case 1:
97#if (EFI_CAN_BUS_COUNT >= 3)
98 case 2:
100#endif
101 }
102
103 return nullptr;
104}
CANDriver * detectCanDevice(brain_pin_e pinRx, brain_pin_e pinTx)
Definition at32_can.cpp:93

Referenced by canInfo(), and initCan().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getIsCanEnabled()

bool getIsCanEnabled ( void  )

Definition at line 298 of file can_hw.cpp.

298 {
299 return isCanEnabled;
300}

◆ initCan()

void initCan ( )

Definition at line 210 of file can_hw.cpp.

210 {
211 addConsoleAction("caninfo", canInfo);
212
213 isCanEnabled = false;
214
215 // No CAN features enabled, nothing more to do.
217 return;
218 }
219
220 // Determine physical CAN peripherals based on selected pins
221 auto device1 = getCanDevice(0);
222 auto device2 = getCanDevice(1);
223#if (EFI_CAN_BUS_COUNT >= 3)
224 auto device3 = getCanDevice(2);
225#endif
226
227 // If all devices are null, a firmware error was already thrown by detectCanDevice, but we shouldn't continue
228 if (!device1 && !device2) {
229#if (EFI_CAN_BUS_COUNT >= 3)
230 if (!device3)
231#endif
232 return;
233 }
234
235 // Devices can't be the same!
236 if (((device1 == device2) && device1) ||
237#if (EFI_CAN_BUS_COUNT >= 3)
238 ((device2 == device3) && device2) ||
239 ((device3 == device1) && device3) ||
240#endif
241 0) {
242 criticalError("CAN pins must be set to different devices");
243 return;
244 }
245
246 // Initialize peripherals
247 if (device1) {
248 // Config based on baud rate
249 // Pointer to this local canConfig is stored inside CANDriver
250 // even it is used only during canStart this is wierd
251 CANConfig canConfig;
252 memcpy(&canConfig, findCanConfig(engineConfiguration->canBaudRate), sizeof(canConfig));
254 canStart(device1, &canConfig);
255
256 // Plumb CAN devices to tx system
257 CanTxMessage::setDevice(0, device1);
258 }
259
260 if (device2) {
261 CANConfig canConfig;
262 memcpy(&canConfig, findCanConfig(engineConfiguration->can2BaudRate), sizeof(canConfig));
264 canStart(device2, &canConfig);
265
266 // Plumb CAN devices to tx system
267 CanTxMessage::setDevice(1, device2);
268 }
269
270#if (EFI_CAN_BUS_COUNT >= 3)
271 if (device3) {
272 CANConfig canConfig;
273 memcpy(&canConfig, findCanConfig(engineConfiguration->can3BaudRate), sizeof(canConfig));
275 canStart(device3, &canConfig);
276
277 // Plumb CAN devices to tx system
278 CanTxMessage::setDevice(2, device3);
279 }
280#endif
281
282 // fire up threads, as necessary
284 canWrite.start();
285 }
286
288 canRead1.start(device1);
289 canRead2.start(device2);
290#if (EFI_CAN_BUS_COUNT >= 3)
291 canRead3.start(device3);
292#endif
293 }
294
295 isCanEnabled = true;
296}
static void applyListenOnly(CANConfig *canConfig, bool isListenOnly)
Definition can_hw.cpp:196
static void canInfo()
Definition can_hw.cpp:109
static CCM_OPTIONAL CanRead canRead1(0)
static CCM_OPTIONAL CanRead canRead3(2)
static CCM_OPTIONAL CanRead canRead2(1)
const CANConfig * findCanConfig(can_baudrate_e rate)
Definition can_hw.cpp:36
static void setDevice(size_t idx, CANDriver *device)
void addConsoleAction(const char *token, Void callback)
Register console action without parameters.

Referenced by initHardware().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setCanType()

void setCanType ( int  type)

Definition at line 139 of file can_hw.cpp.

139 {
141 canInfo();
142}
can_nbc_e
Here is the call graph for this function:

◆ startCanPins()

void startCanPins ( )

Definition at line 156 of file can_hw.cpp.

156 {
157 // nothing to do if we aren't enabled...
158 if (!isCanEnabled) {
159 return;
160 }
161
162 // Validate pins
165 // todo: smarter online change of settings, kill isCanEnabled with fire
166 return;
167 }
169 return;
170 }
171
174 // todo: smarter online change of settings, kill isCanEnabled with fire
175 return;
176 }
178 return;
179 }
180
181#if EFI_PROD_CODE
182 efiSetPadModeIfConfigurationChanged("CAN TX", canTxPin, PAL_MODE_ALTERNATE(EFI_CAN_TX_AF));
183 efiSetPadModeIfConfigurationChanged("CAN RX", canRxPin, PAL_MODE_ALTERNATE(EFI_CAN_RX_AF));
184
185 efiSetPadModeIfConfigurationChanged("CAN2 TX", can2TxPin, PAL_MODE_ALTERNATE(EFI_CAN_TX_AF));
186 efiSetPadModeIfConfigurationChanged("CAN2 RX", can2RxPin, PAL_MODE_ALTERNATE(EFI_CAN_RX_AF));
187
188#if (EFI_CAN_BUS_COUNT >= 3)
189 efiSetPadModeIfConfigurationChanged("CAN3 TX", can3TxPin, PAL_MODE_ALTERNATE(EFI_CAN3_TX_AF));
190 efiSetPadModeIfConfigurationChanged("CAN3 RX", can3RxPin, PAL_MODE_ALTERNATE(EFI_CAN3_RX_AF));
191#endif // EFI_CAN_BUS_COUNT >= 3
192#endif // EFI_PROD_CODE
193}
bool isValidCanRxPin(brain_pin_e pin)
Definition at32_can.cpp:89
bool isValidCanTxPin(brain_pin_e pin)
Definition at32_can.cpp:85
void firmwareError(ObdCode code, const char *fmt,...)
@ CUSTOM_OBD_70
bool isBrainPinValid(brain_pin_e brainPin)

Referenced by startHardware().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stopCanPins()

void stopCanPins ( )

Definition at line 144 of file can_hw.cpp.

144 {
145 efiSetPadUnusedIfConfigurationChanged(canTxPin);
146 efiSetPadUnusedIfConfigurationChanged(canRxPin);
147 efiSetPadUnusedIfConfigurationChanged(can2TxPin);
148 efiSetPadUnusedIfConfigurationChanged(can2RxPin);
149#if (EFI_CAN_BUS_COUNT >= 3)
150 efiSetPadUnusedIfConfigurationChanged(can3TxPin);
151 efiSetPadUnusedIfConfigurationChanged(can3RxPin);
152#endif
153}

Referenced by applyNewHardwareSettings().

Here is the caller graph for this function:

Variable Documentation

◆ canConfig_dummy

const CANConfig canConfig_dummy
static

Definition at line 34 of file can_hw.cpp.

Referenced by findCanConfig().

◆ CCM_OPTIONAL

CanWrite canWrite CCM_OPTIONAL
static

Definition at line 87 of file can_hw.cpp.

◆ isCanEnabled

bool isCanEnabled = false
static

Definition at line 25 of file can_hw.cpp.

Referenced by canInfo(), getIsCanEnabled(), initCan(), and startCanPins().

◆ txErrorCount

int txErrorCount[EFI_CAN_BUS_COUNT] = {}

Definition at line 107 of file can_hw.cpp.

107{};

Referenced by canInfo(), and CanTxMessage::~CanTxMessage().

Go to the source code of this file.