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
 

Function Documentation

◆ applyListenOnly()

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

Definition at line 193 of file can_hw.cpp.

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

Referenced by initCan().

Here is the caller graph for this function:

◆ canInfo()

static void canInfo ( )
static

Definition at line 106 of file can_hw.cpp.

106 {
107 if (!isCanEnabled) {
108 efiPrintf("CAN is not enabled, please enable & restart");
109 return;
110 }
111
113 efiPrintf("CAN1 RX %s", hwPortname(engineConfiguration->canRxPin));
115
117 efiPrintf("CAN2 RX %s", hwPortname(engineConfiguration->can2RxPin));
119
120#if (EFI_CAN_BUS_COUNT >= 3)
122 efiPrintf("CAN3 RX %s", hwPortname(engineConfiguration->can3RxPin));
124#endif
125
126 efiPrintf("type=%d canReadEnabled=%s canWriteEnabled=%s period=%d", engineConfiguration->canNbcType,
129
130 efiPrintf("CAN rx_cnt=%d/tx_ok=%d/tx_not_ok=%d",
134}
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:89
TunerStudioOutputChannels outputChannels
Definition engine.h:109
const char * boolToString(bool value)
Definition efilib.cpp:19
static EngineAccessor engine
Definition engine.h:413
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 89 of file can_hw.cpp.

90{
91 switch (index) {
92 case 0:
94 case 1:
96#if (EFI_CAN_BUS_COUNT >= 3)
97 case 2:
99#endif
100 }
101
102 return nullptr;
103}
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 295 of file can_hw.cpp.

295 {
296 return isCanEnabled;
297}

◆ initCan()

void initCan ( )

Definition at line 207 of file can_hw.cpp.

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

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

◆ startCanPins()

void startCanPins ( )

Definition at line 153 of file can_hw.cpp.

153 {
154 // nothing to do if we aren't enabled...
155 if (!isCanEnabled) {
156 return;
157 }
158
159 // Validate pins
162 // todo: smarter online change of settings, kill isCanEnabled with fire
163 return;
164 }
166 return;
167 }
168
171 // todo: smarter online change of settings, kill isCanEnabled with fire
172 return;
173 }
175 return;
176 }
177
178#if EFI_PROD_CODE
179 efiSetPadModeIfConfigurationChanged("CAN TX", canTxPin, PAL_MODE_ALTERNATE(EFI_CAN_TX_AF));
180 efiSetPadModeIfConfigurationChanged("CAN RX", canRxPin, PAL_MODE_ALTERNATE(EFI_CAN_RX_AF));
181
182 efiSetPadModeIfConfigurationChanged("CAN2 TX", can2TxPin, PAL_MODE_ALTERNATE(EFI_CAN_TX_AF));
183 efiSetPadModeIfConfigurationChanged("CAN2 RX", can2RxPin, PAL_MODE_ALTERNATE(EFI_CAN_RX_AF));
184
185#if (EFI_CAN_BUS_COUNT >= 3)
186 efiSetPadModeIfConfigurationChanged("CAN3 TX", can3TxPin, PAL_MODE_ALTERNATE(EFI_CAN3_TX_AF));
187 efiSetPadModeIfConfigurationChanged("CAN3 RX", can3RxPin, PAL_MODE_ALTERNATE(EFI_CAN3_RX_AF));
188#endif // EFI_CAN_BUS_COUNT >= 3
189#endif // EFI_PROD_CODE
190}
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 141 of file can_hw.cpp.

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

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 86 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().

Go to the source code of this file.