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 194 of file can_hw.cpp.

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

Referenced by initCan().

Here is the caller graph for this function:

◆ canInfo()

static void canInfo ( )
static

Definition at line 107 of file can_hw.cpp.

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

296 {
297 return isCanEnabled;
298}

◆ initCan()

void initCan ( )

Definition at line 208 of file can_hw.cpp.

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

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

◆ startCanPins()

void startCanPins ( )

Definition at line 154 of file can_hw.cpp.

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

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

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

Go to the source code of this file.