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

175 {
176#if defined(STM32F4XX) || defined(STM32F7XX)
177 if (isListenOnly) {
178 canConfig->btr |= CAN_BTR_SILM;
179 }
180#elif defined(STM32H7XX)
181 // TODO: move to ChibiOS stm32_fdcan.h
182 #define FDCAN_CONFIG_CCCR_MON (1u << 5)
183 if (isListenOnly) {
184 canConfig->CCCR |= FDCAN_CONFIG_CCCR_MON;
185 }
186#endif
187}

Referenced by initCan().

Here is the caller graph for this function:

◆ canInfo()

static void canInfo ( )
static

Definition at line 88 of file can_hw.cpp.

88 {
89 if (!isCanEnabled) {
90 efiPrintf("CAN is not enabled, please enable & restart");
91 return;
92 }
93
95 efiPrintf("CAN1 RX %s", hwPortname(engineConfiguration->canRxPin));
97
99 efiPrintf("CAN2 RX %s", hwPortname(engineConfiguration->can2RxPin));
101
102#if (EFI_CAN_BUS_COUNT >= 3)
104 efiPrintf("CAN3 RX %s", hwPortname(engineConfiguration->can3RxPin));
106#endif
107
108 efiPrintf("type=%d canReadEnabled=%s canWriteEnabled=%s period=%d", engineConfiguration->canNbcType,
111
112 efiPrintf("CAN rx_cnt=%d/tx_ok=%d/tx_not_ok=%d",
116}
CANDriver * detectCanDevice(brain_pin_e pinRx, brain_pin_e pinTx)
Definition at32_can.cpp:93
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
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:

◆ getIsCanEnabled()

bool getIsCanEnabled ( void  )

Definition at line 277 of file can_hw.cpp.

277 {
278 return isCanEnabled;
279}

◆ initCan()

void initCan ( )

Definition at line 189 of file can_hw.cpp.

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

118 {
120 canInfo();
121}
can_nbc_e
Here is the call graph for this function:

◆ startCanPins()

void startCanPins ( )

Definition at line 135 of file can_hw.cpp.

135 {
136 // nothing to do if we aren't enabled...
137 if (!isCanEnabled) {
138 return;
139 }
140
141 // Validate pins
144 // todo: smarter online change of settings, kill isCanEnabled with fire
145 return;
146 }
148 return;
149 }
150
153 // todo: smarter online change of settings, kill isCanEnabled with fire
154 return;
155 }
157 return;
158 }
159
160#if EFI_PROD_CODE
161 efiSetPadModeIfConfigurationChanged("CAN TX", canTxPin, PAL_MODE_ALTERNATE(EFI_CAN_TX_AF));
162 efiSetPadModeIfConfigurationChanged("CAN RX", canRxPin, PAL_MODE_ALTERNATE(EFI_CAN_RX_AF));
163
164 efiSetPadModeIfConfigurationChanged("CAN2 TX", can2TxPin, PAL_MODE_ALTERNATE(EFI_CAN_TX_AF));
165 efiSetPadModeIfConfigurationChanged("CAN2 RX", can2RxPin, PAL_MODE_ALTERNATE(EFI_CAN_RX_AF));
166
167#if (EFI_CAN_BUS_COUNT >= 3)
168 efiSetPadModeIfConfigurationChanged("CAN3 TX", can3TxPin, PAL_MODE_ALTERNATE(EFI_CAN3_TX_AF));
169 efiSetPadModeIfConfigurationChanged("CAN3 RX", can3RxPin, PAL_MODE_ALTERNATE(EFI_CAN3_RX_AF));
170#endif // EFI_CAN_BUS_COUNT >= 3
171#endif // EFI_PROD_CODE
172}
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 123 of file can_hw.cpp.

123 {
124 efiSetPadUnusedIfConfigurationChanged(canTxPin);
125 efiSetPadUnusedIfConfigurationChanged(canRxPin);
126 efiSetPadUnusedIfConfigurationChanged(can2TxPin);
127 efiSetPadUnusedIfConfigurationChanged(can2RxPin);
128#if (EFI_CAN_BUS_COUNT >= 3)
129 efiSetPadUnusedIfConfigurationChanged(can3TxPin);
130 efiSetPadUnusedIfConfigurationChanged(can3RxPin);
131#endif
132}

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.