can.rst 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. Controller Area Network (CAN)
  2. =============================
  3. .. -------------------------------- Overview -----------------------------------
  4. Overview
  5. --------
  6. The ESP32's peripherals contains a CAN Controller that supports Standard Frame
  7. Format (16-bit ID) and Extended Frame Format (29-bit ID) of the CAN2.0B specification.
  8. .. warning::
  9. The ESP32 CAN controller is not compatible with CAN FD frames and will interpret
  10. such frames as errors.
  11. This programming guide is split into the following sections:
  12. 1. :ref:`basic-can-concepts`
  13. 2. :ref:`signals-lines-and-transceiver`
  14. 3. :ref:`configuration`
  15. 4. :ref:`driver-operation`
  16. 5. :ref:`examples`
  17. .. --------------------------- Basic CAN Concepts ------------------------------
  18. .. _basic-can-concepts:
  19. Basic CAN Concepts
  20. ------------------
  21. .. note::
  22. The following section only covers the basic aspects of CAN. For full details,
  23. see the CAN2.0B specification
  24. The CAN protocol is a multi-master, multi-cast communication protocol with error
  25. detection/signalling and inbuilt message prioritization. The CAN protocol is
  26. commonly used as a communication bus in automotive applications.
  27. **Multi-master:** Any node in a CAN bus is allowed initiate the transfer of data.
  28. **Multi-cast:** When a node transmits a message, all nodes are able to receive
  29. the message (broadcast). However some nodes can selective choose which messages
  30. to accept via the use of acceptance filtering (multi-cast).
  31. **Error Detection and Signalling:** Every CAN node will constantly monitor the
  32. CAN bus. When any node detects an error, it will signal the error by transmitting an error
  33. frame. Other nodes will receive the error frame and transmit their own error frames
  34. in response. This will result in an error detection being propagated to all nodes on
  35. the bus.
  36. **Message Priorities:** If two nodes attempt to transmit simultaneously, the
  37. node transmitting the message with the lower ID will win arbitration. All other
  38. nodes will become receivers ensuring there is at most one transmitter at any time.
  39. CAN Message Frames
  40. ^^^^^^^^^^^^^^^^^^
  41. The CAN2.0B specification contains two frame formats known as **Extended Frame**
  42. and **Standard Frame** which contain 29-bit IDs and 11-bit IDs respectively.
  43. A CAN message consists of the following components
  44. - 29-bit or 11-bit ID
  45. - Data Length Code (DLC) between 0 to 8
  46. - Up to 8 bytes of data (should match DLC)
  47. Error States and Counters
  48. ^^^^^^^^^^^^^^^^^^^^^^^^^
  49. The CAN2.0B specification implements fault confinement by requiring every CAN node
  50. to maintain two internal error counters known as the **Transmit Error Counter (TEC)**
  51. and the **Receive Error Counter (REC)**. The two error counters are used to determine
  52. a CAN node's **error state**, and the counters are incremented and decremented
  53. following a set of rules (see CAN2.0B specification). These error states are known
  54. as **Error Active**, **Error Passive**, and **Bus-Off**.
  55. **Error Active:** A CAN node is Error Active when **both TEC and REC are less
  56. than 128** and indicates a CAN node is operating normally. Error Active nodes are
  57. allowed to participate in CAN bus activities, and will actively signal any error
  58. conditions it detects by transmitting an **Active Error Flag** over the CAN bus.
  59. **Error Passive:** A CAN node is Error Passive when **either the TEC or REC becomes
  60. greater than or equal to 128**. Error Passive nodes are still able to take part in
  61. CAN bus activities, but will instead transmit a **Passive Error Flag** upon
  62. detection of an error.
  63. **Bus-Off:** A CAN node becomes Bus-Off when the **TEC becomes greater than or equal
  64. to 256**. A Bus-Off node is unable take part in CAN bus activity and will remain so
  65. until it undergoes bus recovery.
  66. .. ---------------------- Signal Lines and Transceiver -------------------------
  67. .. _signals-lines-and-transceiver:
  68. Signals Lines and Transceiver
  69. -----------------------------
  70. The CAN controller does not contain a internal transceiver and therefore
  71. **requires an external transceiver** to operate. The type of external transceiver will
  72. depend on the application's physical layer specification (e.g. using SN65HVD23X
  73. transceivers for ISO 11898-2 compatibility).
  74. The CAN controller's interface consists of 4 signal lines known as **TX, RX, BUS-OFF,
  75. and CLKOUT**. These four signal lines can be routed through the GPIO Matrix to GPIOs.
  76. .. blockdiag:: ../../../_static/diagrams/can/can_controller_signals.diag
  77. :caption: Signal lines of the CAN controller
  78. :align: center
  79. **TX and RX:** The TX and RX signal lines are required to interface with an
  80. external CAN transceiver. Both signal lines represent/interpret a dominant bit
  81. as a low logic level (0V), and a recessive bit as a high logic level (3.3V).
  82. **BUS-OFF:** The BUS-OFF signal line is **optional** and is set to a low logic level
  83. (0V) whenever the CAN controller reaches a bus-off state. The BUS-OFF signal line
  84. is set to a high logic level (3.3V) otherwise.
  85. **CLKOUT:** The CLKOUT signal line is **optional** and outputs a prescaled version
  86. of the CAN controller's source clock (APB Clock).
  87. .. note::
  88. An external transceiver **must internally tie the TX input and the RX output**
  89. such that a change in logic level to the TX signal line can be observed on the
  90. RX line. Failing to do so will cause the CAN controller to interpret differences
  91. in logic levels between the two signal lines as a lost in arbitration or a
  92. bit error.
  93. .. ------------------------------ Configuration --------------------------------
  94. .. _configuration:
  95. Configuration
  96. -------------
  97. Operating Modes
  98. ^^^^^^^^^^^^^^^
  99. The CAN driver supports the following modes of operations:
  100. **Normal Mode:** The normal operating mode allows the CAN controller to take part
  101. in bus activities such as transmitting and receiving messages/error frames.
  102. Acknowledgement from another CAN node is required when transmitting message frames.
  103. **No Ack Mode:** The No Acknowledgement mode is similar to normal mode, however
  104. acknowledgements are not required when transmitting message frames. This mode is
  105. useful when self testing the CAN controller.
  106. **Listen Only Mode:** This mode will prevent the CAN controller from taking part
  107. in bus activities. Therefore transmissions of messages/acknowledgement/error frames
  108. will be disabled. However the the CAN controller will still be able to receive
  109. messages (without acknowledging). This mode is suited for applications such as
  110. CAN bus monitoring.
  111. Alerts
  112. ^^^^^^
  113. The CAN driver contains an alert feature which is used to notify the application
  114. level of certain CAN driver events. Alerts are selectively enabled when the
  115. CAN driver is installed, but can be reconfigured during runtime by calling
  116. :cpp:func:`can_reconfigure_alerts`. The application can then wait for any enabled
  117. alerts to occur by calling :cpp:func:`can_read_alerts`. The CAN driver supports
  118. the following alerts:
  119. +------------------------------------+------------------------------------------------------------------------+
  120. | Alert | Description |
  121. +====================================+=============================================+==========================+
  122. | ``CAN_ALERT_TX_IDLE`` | No more messages queued for transmission |
  123. +------------------------------------+------------------------------------------------------------------------+
  124. | ``CAN_ALERT_TX_SUCCESS`` | The previous transmission was successful |
  125. +------------------------------------+------------------------------------------------------------------------+
  126. | ``CAN_ALERT_BELOW_ERR_WARN`` | Both error counters have dropped below error warning limit |
  127. +------------------------------------+------------------------------------------------------------------------+
  128. | ``CAN_ALERT_ERR_ACTIVE`` | CAN controller has become error active |
  129. +------------------------------------+------------------------------------------------------------------------+
  130. | ``CAN_ALERT_RECOVERY_IN_PROGRESS`` | CAN controller is undergoing bus recovery |
  131. +------------------------------------+------------------------------------------------------------------------+
  132. | ``CAN_ALERT_BUS_RECOVERED`` | CAN controller has successfully completed bus recovery |
  133. +------------------------------------+------------------------------------------------------------------------+
  134. | ``CAN_ALERT_ARB_LOST`` | The previous transmission lost arbitration |
  135. +------------------------------------+------------------------------------------------------------------------+
  136. | ``CAN_ALERT_ABOVE_ERR_WARN`` | One of the error counters have exceeded the error warning limit |
  137. +------------------------------------+------------------------------------------------------------------------+
  138. | ``CAN_ALERT_BUS_ERROR`` | A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus |
  139. +------------------------------------+------------------------------------------------------------------------+
  140. | ``CAN_ALERT_TX_FAILED`` | The previous transmission has failed |
  141. +------------------------------------+------------------------------------------------------------------------+
  142. | ``CAN_ALERT_RX_QUEUE_FULL`` | The RX queue is full causing a received frame to be lost |
  143. +------------------------------------+------------------------------------------------------------------------+
  144. | ``CAN_ALERT_ERR_PASS`` | CAN controller has become error passive |
  145. +------------------------------------+------------------------------------------------------------------------+
  146. | ``CAN_ALERT_BUS_OFF`` | Bus-off condition occurred. CAN controller can no longer influence bus |
  147. +------------------------------------+------------------------------------------------------------------------+
  148. .. note::
  149. The **error warning limit** can be used to preemptively warn the application
  150. of bus errors before the error passive state is reached. By default the CAN
  151. driver sets the **error warning limit** to **96**. The ``CAN_ALERT_ABOVE_ERR_WARN``
  152. is raised when the TEC or REC becomes larger then or equal to the error warning
  153. limit. The ``CAN_ALERT_BELOW_ERR_WARN`` is raised when both TEC and REC return
  154. back to values below **96**.
  155. .. note::
  156. When enabling alerts, the ``CAN_ALERT_AND_LOG`` flag can be used to cause the
  157. CAN driver to log any raised alerts to UART. The ``CAN_ALERT_ALL`` and
  158. ``CAN_ALERT_NONE`` macros can also be used to enable/disable all alerts during
  159. configuration/reconfiguration.
  160. Bit Timing
  161. ^^^^^^^^^^
  162. The operating bit rate of the CAN controller is configured using the
  163. :cpp:type:`can_timing_config_t` structure. The period of each bit is made up of
  164. multiple **time quanta**, and the period of a **time quanta** is determined by a
  165. prescaled version of the CAN controller's source clock. A single bit contains the
  166. following segments in the following order:
  167. 1. The **Synchronization Segment** consists of a single time quanta
  168. 2. **Timing Segment 1** consists of 1 to 16 time quanta before sample point
  169. 3. **Timing Segment 2** consists of 1 to 8 time quanta after sample point
  170. The **Baudrate Prescaler** is used to determine the period of each time quanta by
  171. dividing the CAN controller's source clock (80 MHz APB clock). The ``brp`` can be
  172. **any even number from 2 to 128**.
  173. .. packetdiag:: ../../../_static/diagrams/can/can_bit_timing.diag
  174. :caption: Bit timing configuration for 500kbit/s given BRP = 8
  175. :align: center
  176. The sample point of a bit is located on the intersection of Timing Segment 1 and
  177. 2. Enabling **Triple Sampling** will cause 3 time quanta to be sampled per bit
  178. instead of 1 (extra samples are located at the tail end of Timing Segment 1).
  179. The **Synchronization Jump Width** is used to determined the maximum number of
  180. time quanta a single bit time can be lengthened/shortened for synchronization
  181. purposes. ``sjw`` can **range from 1 to 4**.
  182. .. note::
  183. Multiple combinations of ``brp``, ``tseg_1``, ``tseg_2``, and ``sjw`` can
  184. achieve the same bit rate. Users should tune these values to the physical
  185. characteristics of their CAN bus by taking into account factors such as
  186. **propagation delay, node information processing time, and phase errors**.
  187. Bit timing **macro initializers** are also available for commonly used CAN bus bit rates.
  188. The following macro initiliazers are provided by the CAN driver.
  189. - ``CAN_TIMING_CONFIG_25KBITS()``
  190. - ``CAN_TIMING_CONFIG_50KBITS()``
  191. - ``CAN_TIMING_CONFIG_100KBITS()``
  192. - ``CAN_TIMING_CONFIG_125KBITS()``
  193. - ``CAN_TIMING_CONFIG_250KBITS()``
  194. - ``CAN_TIMING_CONFIG_500KBITS()``
  195. - ``CAN_TIMING_CONFIG_800KBITS()``
  196. - ``CAN_TIMING_CONFIG_1MBITS()``
  197. Acceptance Filter
  198. ^^^^^^^^^^^^^^^^^
  199. The CAN controller contains a hardware acceptance filter which can be used to
  200. filter CAN messages of a particular ID. A node that filters out a message
  201. **will not receive the message, but will still acknowledge it**. Acceptances
  202. filters can make a node more efficient by filtering out messages sent over the
  203. CAN bus that are irrelevant to the CAN node in question. The CAN controller's
  204. acceptance filter is configured using two 32-bit values within :cpp:type:`can_filter_config_t`
  205. known as the **acceptance code** and the **acceptance mask**.
  206. The **acceptance code** specifies the bit sequence which a message's ID, RTR, and
  207. data bytes must match in order for the message to be received by the CAN
  208. controller. The **acceptance mask** is a bit sequence specifying which bits of
  209. the acceptance code can be ignored. This allows for a messages of different IDs
  210. to be accepted by a single acceptance code.
  211. The acceptance filter can be used under **Single or Dual Filter Mode**.
  212. Single Filter Mode will use the acceptance code and mask to define a single
  213. filter. This allows for the first two data bytes of a standard frame to be filtered,
  214. or the entirety of an extended frame's 29-bit ID. The following diagram illustrates
  215. how the 32-bit acceptance code and mask will be interpreted under Single Filter Mode
  216. (Note: The yellow and blue fields represent standard and extended CAN frames respectively).
  217. .. packetdiag:: ../../../_static/diagrams/can/can_acceptance_filter_single.diag
  218. :caption: Bit layout of single filter mode (Right side MSBit)
  219. :align: center
  220. **Dual Filter Mode** will use the acceptance code and mask to define two separate
  221. filters allowing for increased flexibility of ID's to accept, but does not allow
  222. for all 29-bits of an extended ID to be filtered. The following diagram illustrates
  223. how the 32-bit acceptance code and mask will be interpreted under **Dual Filter Mode**
  224. (Note: The yellow and blue fields represent standard and extended CAN frames respectively).
  225. .. packetdiag:: ../../../_static/diagrams/can/can_acceptance_filter_dual.diag
  226. :caption: Bit layout of dual filter mode (Right side MSBit)
  227. :align: center
  228. Disabling TX Queue
  229. ^^^^^^^^^^^^^^^^^^
  230. The TX queue can be disabled during configuration by setting the ``tx_queue_len``
  231. member of :cpp:type:`can_general_config_t` to ``0``. This will allow applications
  232. that do not require message transmission to save a small amount of memory when
  233. using the CAN driver.
  234. .. -------------------------------- CAN Driver ---------------------------------
  235. .. _driver-operation:
  236. Driver Operation
  237. ----------------
  238. The CAN driver is designed with distinct states and strict rules regarding the
  239. functions or conditions that trigger a state transition. The following diagram
  240. illustrates the various states and their transitions.
  241. .. blockdiag:: ../../../_static/diagrams/can/can_state_transition.diag
  242. :caption: State transition diagram of the CAN driver (see table below)
  243. :align: center
  244. +-------+------------------------+------------------------------------+
  245. | Label | Transition | Action/Condition |
  246. +=======+========================+====================================+
  247. | A | Uninstalled -> Stopped | :cpp:func:`can_driver_install` |
  248. +-------+------------------------+------------------------------------+
  249. | B | Stopped -> Uninstalled | :cpp:func:`can_driver_uninstall` |
  250. +-------+------------------------+------------------------------------+
  251. | C | Stopped -> Running | :cpp:func:`can_start` |
  252. +-------+------------------------+------------------------------------+
  253. | D | Running -> Stopped | :cpp:func:`can_stop` |
  254. +-------+------------------------+------------------------------------+
  255. | E | Running -> Bus-Off | Transmit Error Counter >= 256 |
  256. +-------+------------------------+------------------------------------+
  257. | F | Bus-Off -> Uninstalled | :cpp:func:`can_driver_uninstall` |
  258. +-------+------------------------+------------------------------------+
  259. | G | Bus-Off -> Recovering | :cpp:func:`can_initiate_recovery` |
  260. +-------+------------------------+------------------------------------+
  261. | H | Recovering -> Stopped | 128 occurrences of bus-free signal |
  262. +-------+------------------------+------------------------------------+
  263. Driver States
  264. ^^^^^^^^^^^^^
  265. **Uninstalled**: In the uninstalled state, no memory is allocated for the driver
  266. and the CAN controller is powered OFF.
  267. **Stopped**: In this state, the CAN controller is powered ON and the CAN driver
  268. has been installed. However the CAN controller will be unable to take part in
  269. any CAN bus activities such as transmitting, receiving, or acknowledging messages.
  270. **Running**: In the running state, the CAN controller is able to take part in
  271. bus activities. Therefore messages can be transmitted/received/acknowledged.
  272. Furthermore the CAN controller will be able to transmit error frames upon detection
  273. of errors on the CAN bus.
  274. **Bus-Off**: The bus-off state is automatically entered when the CAN controller's
  275. Transmit Error Counter becomes greater than or equal to 256 (see CAN2.0B specification
  276. regarding error counter rules). The bus-off state indicates the occurrence of severe
  277. errors on the CAN bus or in the CAN controller. Whilst in the bus-off state, the
  278. CAN controller will be unable to take part in any CAN bus activities. To exit
  279. the bus-off state, the CAN controller must undergo the bus recovery process.
  280. **Recovering**: The recovering state is entered when the CAN driver undergoes
  281. bus recovery. The CAN driver/controller will remain in the recovering state until
  282. the 128 occurrences of the bus-free signal (see CAN2.0B specification) is observed
  283. on the CAN bus.
  284. Message Flags
  285. ^^^^^^^^^^^^^
  286. The CAN driver distinguishes different types of CAN messages by using the message
  287. flags in the ``flags`` field of :cpp:type:`can_message_t`. These flags help
  288. distinguish whether a message is in standard or extended format, an RTR, and the
  289. type of transmission to use when transmitting such a message. The CAN driver
  290. supports the following flags:
  291. +-------------------------------+---------------------------------------------------------------+
  292. | Flag | Description |
  293. +===============================+===============================================================+
  294. | ``CAN_MSG_FLAG_EXTD`` | Message is in Extended Frame Format (29bit ID) |
  295. +-------------------------------+---------------------------------------------------------------+
  296. | ``CAN_MSG_FLAG_RTR`` | Message is a Remote Transmit Request |
  297. +-------------------------------+---------------------------------------------------------------+
  298. | ``CAN_MSG_FLAG_SS`` | Transmit message using Single Shot Transmission (Message will |
  299. | | note be retransmitted upon error or loss of arbitration) |
  300. +-------------------------------+---------------------------------------------------------------+
  301. | ``CAN_MSG_FLAG_SELF`` | Transmit message using Self Reception Request (Transmitted |
  302. | | message will also received by the same node) |
  303. +-------------------------------+---------------------------------------------------------------+
  304. | ``CAN_MSG_FLAG_DLC_NON_COMP`` | Message's Data length code is larger than 8. This |
  305. | | will break compliance with CAN2.0B |
  306. +-------------------------------+---------------------------------------------------------------+
  307. .. note::
  308. The ``CAN_MSG_FLAG_NONE`` flag can be used for Standard Frame Format messages
  309. .. -------------------------------- Examples -----------------------------------
  310. .. _examples:
  311. Examples
  312. --------
  313. Configuration & Installation
  314. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  315. The following code snippet demonstrates how to configure, install, and start the
  316. CAN driver via the use of the various configuration structures, macro initializers,
  317. the :cpp:func:`can_driver_install` function, and the :cpp:func:`can_start` function.
  318. .. code-block:: c
  319. #include "driver/gpio.h"
  320. #include "driver/can.h"
  321. void app_main()
  322. {
  323. //Initialize configuration structures using macro initializers
  324. can_general_config_t g_config = CAN_GENERAL_CONFIG_DEFAULT(GPIO_NUM_21, GPIO_NUM_22, CAN_MODE_NORMAL);
  325. can_timing_config_t t_config = CAN_TIMING_CONFIG_500KBITS();
  326. can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL();
  327. //Install CAN driver
  328. if (can_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
  329. printf("Driver installed\n");
  330. } else {
  331. printf("Failed to install driver\n");
  332. return;
  333. }
  334. //Start CAN driver
  335. if (can_start() == ESP_OK) {
  336. printf("Driver started\n");
  337. } else {
  338. printf("Failed to start driver\n");
  339. return;
  340. }
  341. ...
  342. }
  343. The usage of macro initializers are not mandatory and each of the configuration
  344. structures can be manually.
  345. Message Transmission
  346. ^^^^^^^^^^^^^^^^^^^^
  347. The following code snippet demonstrates how to transmit a message via the usage
  348. of the :cpp:type:`can_message_t` type and :cpp:func:`can_transmit` function.
  349. .. code-block:: c
  350. #include "driver/can.h"
  351. ...
  352. //Configure message to transmit
  353. can_message_t message;
  354. message.identifier = 0xAAAA;
  355. message.flags = CAN_MSG_FLAG_EXTD;
  356. message.data_length_code = 4;
  357. for (int i = 0; i < 4; i++) {
  358. message.data[i] = 0;
  359. }
  360. //Queue message for transmission
  361. if (can_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
  362. printf("Message queued for transmission\n");
  363. } else {
  364. printf("Failed to queue message for transmission\n");
  365. }
  366. Message Reception
  367. ^^^^^^^^^^^^^^^^^
  368. The following code snippet demonstrates how to receive a message via the usage
  369. of the :cpp:type:`can_message_t` type and :cpp:func:`can_receive` function.
  370. .. code-block:: c
  371. #include "driver/can.h"
  372. ...
  373. //Wait for message to be received
  374. can_message_t message;
  375. if (can_receive(&message, pdMS_TO_TICKS(10000)) == ESP_OK) {
  376. printf("Message received\n");
  377. } else {
  378. printf("Failed to receive message\n");
  379. return;
  380. }
  381. //Process received message
  382. if (message.flags & CAN_MSG_FLAG_EXTD) {
  383. printf("Message is in Extended Format\n");
  384. } else {
  385. printf("Message is in Standard Format\n");
  386. }
  387. printf("ID is %d\n", message.identifier);
  388. if (!(message.flags & CAN_MSG_FLAG_RTR)) {
  389. for (int i = 0; i < message.data_length_code; i++) {
  390. printf("Data byte %d = %d\n", i, message.data[i]);
  391. }
  392. }
  393. Reconfiguring and Reading Alerts
  394. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  395. The following code snippet demonstrates how to reconfigure and read CAN driver
  396. alerts via the use of the :cpp:func:`can_reconfigure_alerts` and
  397. :cpp:func:`can_read_alerts` functions.
  398. .. code-block:: c
  399. #include "driver/can.h"
  400. ...
  401. //Reconfigure alerts to detect Error Passive and Bus-Off error states
  402. uint32_t alerts_to_enable = CAN_ALERT_ERR_PASS | CAN_ALERT_BUS_OFF;
  403. if (can_reconfigure_alerts(alerts_to_enable, NULL) == ESP_OK) {
  404. printf("Alerts reconfigured\n");
  405. } else {
  406. printf("Failed to reconfigure alerts");
  407. }
  408. //Block indefinitely until an alert occurs
  409. uint32_t alerts_triggered;
  410. can_read_alerts(&alerts_triggered, portMAX_DELAY);
  411. Stop and Uninstall
  412. ^^^^^^^^^^^^^^^^^^
  413. The following code demonstrates how to stop and uninstall the CAN driver via the
  414. use of the :cpp:func:`can_stop` and :cpp:func:`can_driver_uninstall` functions.
  415. .. code-block:: c
  416. #include "driver/can.h"
  417. ...
  418. //Stop the CAN driver
  419. if (can_stop() == ESP_OK) {
  420. printf("Driver stopped\n");
  421. } else {
  422. printf("Failed to stop driver\n");
  423. return;
  424. }
  425. //Uninstall the CAN driver
  426. if (can_driver_uninstall() == ESP_OK) {
  427. printf("Driver uninstalled\n");
  428. } else {
  429. printf("Failed to uninstall driver\n");
  430. return;
  431. }
  432. Multiple ID Filter Configuration
  433. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  434. The acceptance mask in :cpp:type:`can_filter_config_t` can be configured such that
  435. two or more IDs will be accepted for a single filter. For a particular filter to
  436. accept multiple IDs, the conflicting bit positions amongst the IDs must be set
  437. in the acceptance mask. The acceptance code can be set to any one of the IDs.
  438. The following example shows how the calculate the acceptance mask given multiple
  439. IDs::
  440. ID1 = 11'b101 1010 0000
  441. ID2 = 11'b101 1010 0001
  442. ID3 = 11'b101 1010 0100
  443. ID4 = 11'b101 1010 1000
  444. //Acceptance Mask
  445. MASK = 11'b000 0000 1101
  446. Application Examples
  447. ^^^^^^^^^^^^^^^^^^^^
  448. **Network Example:** The CAN Network example demonstrates communication between
  449. two ESP32s using the CAN driver API. One CAN node acts as a network master initiate
  450. and ceasing the transfer of a data from another CAN node acting as a network slave.
  451. The example can be found via :example:`peripherals/can/can_network`.
  452. **Alert and Recovery Example:** This example demonstrates how to use the CAN driver's
  453. alert and bus recovery API. The example purposely introduces errors on the CAN
  454. bus to put the CAN controller into the Bus-Off state. An alert is used to detect
  455. the Bus-Off state and trigger the bus recovery process. The example can be found
  456. via :example:`peripherals/can/can_alert_and_recovery`.
  457. **Self Test Example:** This example uses the No Acknowledge Mode and Self Reception
  458. Request to cause the CAN controller to send and simultaneously receive a series
  459. of messages. This example can be used to verify if the connections between the CAN
  460. controller and the external transceiver are working correctly. The example can be
  461. found via :example:`peripherals/can/can_self_test`.
  462. .. ---------------------------- API Reference ----------------------------------
  463. API Reference
  464. -------------
  465. .. include:: /_build/inc/can.inc