|
| 1 | +.. currentmodule:: machine |
| 2 | +.. _machine.CAN: |
| 3 | + |
| 4 | +class CAN -- Controller Area Network protocol |
| 5 | +============================================= |
| 6 | + |
| 7 | +.. warning:: Currently no MicroPython ports have ``machine.CAN`` controllers. |
| 8 | + This is a design document only. |
| 9 | + |
| 10 | +CAN is a two-wire serial protocol used for reliable real-time message delivery |
| 11 | +between one or more nodes connected to a common bus. CAN 2.0 was standardised in |
| 12 | +ISO-11898, and is now also known as CAN Classic. |
| 13 | + |
| 14 | +There is also a newer, backwards compatible, protocol named CAN FD (CAN with |
| 15 | +Flexible Data-Rate). |
| 16 | + |
| 17 | +CAN support requires a controller (often an internal microcontroller |
| 18 | +peripheral), and an external transceiver to level-shift the signals onto the CAN |
| 19 | +bus. |
| 20 | + |
| 21 | +The ``machine.CAN`` interface is a *low level basic* CAN messaging interface |
| 22 | +that abstracts a CAN controller as an outgoing priority queue for sending |
| 23 | +messages, an incoming queue for receiving messages, and mechanisms for reporting |
| 24 | +errors. |
| 25 | + |
| 26 | +.. note:: The forthcoming ``can`` and ``aiocan`` micropython-lib modules are the |
| 27 | + recommended way to use CAN with MicroPython. |
| 28 | + |
| 29 | +Constructor |
| 30 | +----------- |
| 31 | + |
| 32 | +.. class:: CAN(id, **kwargs) |
| 33 | + |
| 34 | + Construct a CAN controller object of the given id: |
| 35 | + |
| 36 | + - ``id`` identifies a particular CAN controller object; it is board and port specific. |
| 37 | + - ``**kwargs`` are hardware-specific identifiers for the particular |
| 38 | + board and port. For example, to set which GPIO pins to use for the |
| 39 | + CAN controller. |
| 40 | + |
| 41 | +Methods |
| 42 | +------- |
| 43 | + |
| 44 | +.. method:: CAN.init(bitrate, mode=CAN.Mode.NORMAL, sample_point=75, sjw=None, tseg1=None, tseg2=None, f_clock=None) |
| 45 | + |
| 46 | + Initialise the CAN bus with the given parameters: |
| 47 | + |
| 48 | + - ``bitrate`` is the desired bus bit rate in bits per second. |
| 49 | + - ``mode`` is one of :class:`CAN.Mode` enumerated values, indicating the |
| 50 | + desired mode of operation. |
| 51 | + - ``sample_point`` is a percentage of the data bit time. It specifies the |
| 52 | + position of the bit sample with respect to the whole nominal bit time. |
| 53 | + |
| 54 | + The remaining parameters all relate to CAN bit timings. If not specified, the CAN controller |
| 55 | + will pick nominal values based on the specified bit rate. |
| 56 | + |
| 57 | + Otherwise, they can be specified: |
| 58 | + |
| 59 | + - ``sjw`` is the resynchronisation jump width in units of time quanta for nominal bits; |
| 60 | + it can be a value between 1 and 4 inclusive for classic CAN, and between 1 and 128 inclusive for CAN FD. |
| 61 | + - ``tseg1`` defines the location of the sample point in units of the time quanta for nominal bits; |
| 62 | + it can be a value between 1 and 16 inclusive for classic CAN, and between 2 and 256 inclusive for CAN FD. |
| 63 | + - ``tseg2`` defines the location of the transmit point in units of the time quanta for nominal bits; |
| 64 | + it can be a value between 1 and 8 inclusive for classic CAN, and between 2 and 128 inclusive for CAN FD. |
| 65 | + - ``f_clock`` is the system clock frequency for the CAN peripheral. This |
| 66 | + can be omitted on many ports where the value is known to the hardware. |
| 67 | + |
| 68 | + In this case, the CAN controller is configured correctly for the desired |
| 69 | + ``bitrate`` and the specified total number of time quanta per bit. |
| 70 | + |
| 71 | +.. method:: CAN.init_fd(bitrate, sample_point=None, sjw=None, tseg1=None, tseg2=None, f_clock=None) |
| 72 | + |
| 73 | + Initialise CAN FD for controllers which support it. |
| 74 | + |
| 75 | + .. note:: This method is not present on the class for controllers that only |
| 76 | + support CAN Classic. |
| 77 | + |
| 78 | + This must be called after :func:`CAN.init()`. The parameters all have the |
| 79 | + same meanings and behaviour as that function, however they configure the |
| 80 | + Bit Rate Switch (BRS) feature of CAN FD. |
| 81 | + |
| 82 | + When specifying exact timing parameters, the accepted ranges are: |
| 83 | + |
| 84 | + - ``sjw`` can be a value between 1 and 16 inclusive. |
| 85 | + - ``tseg1`` can be a value between 1 and 32 inclusive |
| 86 | + - ``tseg2`` can be a value between 1 and 32 inclusive |
| 87 | + |
| 88 | +.. method:: CAN.detect_bitrate([timeout_ms]) |
| 89 | + |
| 90 | + Detect the CAN bit rate by monitoring the CAN bus. |
| 91 | + |
| 92 | + ..note:: This method is only present on the class for controllers that |
| 93 | + support this feature. |
| 94 | + |
| 95 | + If successful, returns the detected bit rate and also configures the hardware |
| 96 | + CAN controller for this rate. |
| 97 | + |
| 98 | + If the optional ``timeout`` parameter is specified then detection only runs |
| 99 | + for this many milliseconds before timing out and returning ``None``. In this |
| 100 | + case the CAN controller configuration will remain unchanged after returning. |
| 101 | + |
| 102 | +.. method:: CAN.set_filters(filters) |
| 103 | + |
| 104 | + Set receive filters in the CAN controller. ``filters`` should be an iterable, |
| 105 | + where each item is a tuple or list with three elements: |
| 106 | + |
| 107 | + - ``identifier`` is a CAN identifier (int). |
| 108 | + - ``bit_mask`` is a bit mask for bits in the CAN identifier field (int). |
| 109 | + - ``flags`` is one or more bitwise OR-ed values from |
| 110 | + :class:`CAN.MessageFlags` that the incoming message needs to match. Not |
| 111 | + all controllers support filtering on all flags, a ``ValueError`` is raised |
| 112 | + if an unsupported flag is requested. |
| 113 | + |
| 114 | + Incoming messages are accepted if the bits masked in ``bit_mask`` match between |
| 115 | + the message identifier and the filter ``identifier`` value, and flags set in the |
| 116 | + filter match the incoming message. |
| 117 | + |
| 118 | + All filters are ORed together in the controller. Passing an empty list (``[]``) |
| 119 | + for the filters argument disables the receive filter (all messages received.) |
| 120 | + |
| 121 | + .. note:: If the caller passes a list with more entries than :data:`CAN.filters_max`, |
| 122 | + ``ValueError`` will be raised. |
| 123 | + |
| 124 | +.. data:: CAN.filters_max |
| 125 | + |
| 126 | + Constant value that reads the maximum number of supported receive filters |
| 127 | + for this hardware controller. |
| 128 | + |
| 129 | +.. method:: CAN.send(identifier, data, flags=0, fifo_equal=True) |
| 130 | + |
| 131 | + - ``identifier`` is an integer CAN identifier value. |
| 132 | + - ``data`` is a bytes object (or similar) containing the CAN message data. |
| 133 | + - ``flags`` is OR-ed together flags :class:`CAN.MessageFlags` specifying |
| 134 | + properties of the outgoing CAN message (Extended ID, Remote request, etc.) |
| 135 | + - ``keep_equal`` is a flag for whether messages with the same identifiers |
| 136 | + should be kept in the queue or replaced (see below). |
| 137 | + |
| 138 | + Write a new CAN message into the controller's hardware transmit queue to be |
| 139 | + sent onto the bus. The transmit queue is a priority queue sorted first on CAN |
| 140 | + identifier priority (lower numeric identifiers have higher priority), and |
| 141 | + second on order of insertion. |
| 142 | + |
| 143 | + If the queue is full and the new message has higher priority than the lowest |
| 144 | + priority message in the queue then the lowest priority message will be |
| 145 | + de-queued and replaced. |
| 146 | + |
| 147 | + If the queue is full and the new message has equal priority to the lowest |
| 148 | + priority message in the queue, then the behaviour depends on the |
| 149 | + ``keep_equal`` argument: |
| 150 | + |
| 151 | + - If ``True`` (default), the queue remains unchanged and the new message is |
| 152 | + not queued. This allows reliable transmission of messages in FIFO order. |
| 153 | + - If ``False``, the oldest matching message in the transmit queue is |
| 154 | + de-queued and replaced with the new message. |
| 155 | + |
| 156 | + The function returns an integer index (1-based) which identifies the queue |
| 157 | + entry where the new message was written. This is a hardware-specific index |
| 158 | + value that does not reflect which message will be sent next. |
| 159 | + |
| 160 | + An additional value ``CAN.SEND_FLAG`` is bitwise ORed with the result if the |
| 161 | + previously queued message at this entry indexed was de-queued and replaced |
| 162 | + with the new message. |
| 163 | + |
| 164 | + If the hardware queue was full and the new message could not be written into |
| 165 | + the queue at all due to priorities, then the return value is ``0``. |
| 166 | + |
| 167 | + .. note:: This intentionally low-level implementation is designed so the |
| 168 | + caller tracks the messages in each hardware queue entry. See the ``can`` and |
| 169 | + ``aiocan`` modules for details. |
| 170 | + |
| 171 | +.. data:: CAN.SEND_FLAG |
| 172 | + |
| 173 | + Constant bit value that is ORed with the index result from :func:`CAN.send` |
| 174 | + if another message was de-queued and replaced with the new message. |
| 175 | + |
| 176 | +.. data:: CAN.send_max |
| 177 | + |
| 178 | + Constant, indicating the maximum number of entries in the hardware transmit |
| 179 | + queue (this is also the highest possible index returned from ``send``). |
| 180 | + |
| 181 | +.. method:: CAN.irq_send(callback, hard=False) |
| 182 | + |
| 183 | + Sets an interrupt ``callback`` function to be called when the controller sends |
| 184 | + a message on the bus, or has attempted to send. |
| 185 | + |
| 186 | + Callback arguments are a tuple or list with three elements: |
| 187 | + |
| 188 | + - ``send_id`` is the same index value returned from :func:`CAN.send` when the |
| 189 | + corresponding message was queued. |
| 190 | + - ``send_status`` is 0 if the message was sent successfully, or a bitwise OR |
| 191 | + of :class:`CAN.SendErrors` flags otherwise. |
| 192 | + - ``requeued`` is a ``bool`` that is set to ``True`` if the CAN controller |
| 193 | + hardware has already re-queued this message to retry sending. |
| 194 | + |
| 195 | + If ``hard`` is set to True then the callback will be called in "hard" |
| 196 | + interrupt context on ports which support this. The parameter is ignored |
| 197 | + otherwise. |
| 198 | + |
| 199 | +.. method:: CAN.irq_recv(callback, hard=False) |
| 200 | + |
| 201 | + Sets an interrupt ``callback`` function to be called when the controller receives |
| 202 | + a message on the bus, or detects an error condition while receiving. |
| 203 | + |
| 204 | + Callback arguments for a successful receive are a tuple or list with the following elements: |
| 205 | + |
| 206 | + - ``timestamp`` is a ``time.ticks_us`` timestamp indicating when the message was |
| 207 | + received. |
| 208 | + - ``identifier`` is the CAN identifier which was received. If receive filters |
| 209 | + are set, the identifier will be one that matches. |
| 210 | + - ``data`` is a bytes object (or equivalent) showing the data portion of the |
| 211 | + message. |
| 212 | + - ``msg_flags`` is an integer comprised of bitwise ORed values from |
| 213 | + :class:`CAN.MessageFlags`. This indicates metadata about the message. |
| 214 | + - ``error_flags`` is ``0`` if the message was received successfully, or |
| 215 | + bitwise ORed values of :func:`CAN.RecvErrors` otherwise. Note that if error |
| 216 | + flags are set, the values of ``identifier``, ``data``, and ``msg_flags`` |
| 217 | + may be ``None``. |
| 218 | + |
| 219 | + Argument values are only valid while the callback is running, they must be |
| 220 | + copied into another variable or buffer in order to retain them. |
| 221 | + |
| 222 | + If ``hard`` is set to True then the callback will be called in "hard" |
| 223 | + interrupt context on ports which support this. The parameter is ignored |
| 224 | + otherwise. |
| 225 | + |
| 226 | + Unless not possible due to hardware limitations, the controller will call the |
| 227 | + ``irq_recv()`` callback in the same order that messages were received on the |
| 228 | + bus. |
| 229 | + |
| 230 | + .. note:: As a low-level class, :class:`machine.CAN` does not contain a |
| 231 | + normal ``recv()`` method. Refer to (planned) ``can`` and ``iocan`` |
| 232 | + modules, instead. |
| 233 | + |
| 234 | +.. method:: CAN.get_state() |
| 235 | + |
| 236 | + Returns a :class:`CAN.State` value indicating the state of the controller. |
| 237 | + |
| 238 | +.. method:: CAN.irq_state(callback, hard=False) |
| 239 | + |
| 240 | + Sets a callback which is called whenever the controller state changes. The |
| 241 | + callback argument is the new :class:`CAN.State` value. |
| 242 | + |
| 243 | + If ``hard`` is set to True then the callback will be called in "hard" |
| 244 | + interrupt context on ports which support this. The parameter is ignored |
| 245 | + otherwise. |
| 246 | + |
| 247 | +.. method:: CAN.get_counters([list]) |
| 248 | + |
| 249 | + Returns controller's error counter values. The result is a list of 8 values. |
| 250 | + If the optional ``list`` parameter is specified then the provided list object |
| 251 | + is updated and returned as the result, to avoid an allocation. |
| 252 | + |
| 253 | + The list items are: |
| 254 | + |
| 255 | + - TEC (Transmit Error Counter) value |
| 256 | + - REC (Receive Error Counter) value |
| 257 | + - Number of times the controller entered the Error Warning state. |
| 258 | + - Number of times the controller entered the Error Passive state. |
| 259 | + - Number of times the controller entered the Bus Off state. |
| 260 | + - Total number of pending TX messages in the hardware queue. |
| 261 | + - Total number of pending RX messages in the hardware queue. |
| 262 | + - Number of times an RX overrun occurred. |
| 263 | + |
| 264 | + .. note:: Depending on the controller, these values may overflow back to 0 after |
| 265 | + a certain value. |
| 266 | + |
| 267 | + .. note:: If a controller doesn't support a particular counter, it will return |
| 268 | + ``None`` for that list element. |
| 269 | + |
| 270 | +.. method:: CAN.get_errors() |
| 271 | + |
| 272 | + Returns an integer which is a bitwise OR of :class:`CAN.ErrorFlags` representing |
| 273 | + error conditions tracked by the CAN controller. |
| 274 | + |
| 275 | +.. method:: CAN.reset(mode=CAN.Mode.NORMAL) |
| 276 | + |
| 277 | + Fully resets the CAN controller hardware. Restores values previously set by :func:`CAN.init()`. |
| 278 | + |
| 279 | + All hardware queues are emptied, error counters and flags reset, etc. |
| 280 | + |
| 281 | + The controller transitions into the ``mode`` specified by the argument (see :class:`CAN.Mode`). |
| 282 | + |
| 283 | +.. method:: CAN.restart() |
| 284 | + |
| 285 | + Restart the CAN controller without resetting its internal state, except for |
| 286 | + TEC and REC. Can be used to manually recover from the ``BUS_OFF`` state. |
| 287 | + |
| 288 | +.. method:: CAN.mode(mode) |
| 289 | + |
| 290 | + Transitions the CAN controller to a new mode of operation, without resetting it. |
| 291 | + Argument is one of :func:`CAN.Mode`. |
| 292 | + |
| 293 | + .. note:: Note all modes are supported by all CAN controller hardware. |
| 294 | + Passing an unsupported mode value will raise ``ValueError``. |
| 295 | + |
| 296 | +.. class:: CAN.Mode |
| 297 | + |
| 298 | + Enumeration class that holds the following constant values (integers) representing controller modes of operation: |
| 299 | + |
| 300 | + - ``NORMAL`` - CAN controller interacts normally on the bus. |
| 301 | + - ``SLEEP`` - CAN controller is asleep in a low power mode. Depending on the |
| 302 | + controller, this may support waking the controller and transitioning to |
| 303 | + ``NORMAL`` mode if CAN traffic is received. |
| 304 | + - ``LOOPBACK`` - A testing mode. The CAN controller is still connected to the |
| 305 | + external bus, but will also receive its own transmitted messages and ignore |
| 306 | + any ACK errors. |
| 307 | + - ``SILENT`` - CAN controller receives messages but does not interact with |
| 308 | + the CAN bus (including sending ACKs, errors, etc.) |
| 309 | + - ``SILENT_LOOPBACK`` - A testing mode that does not require a CAN bus. The |
| 310 | + CAN controller receives its own transmitted messages without interacting |
| 311 | + with the CAN bus at all. The CAN TX and RX pins remain idle. |
| 312 | + |
| 313 | +.. class:: CAN.State |
| 314 | + |
| 315 | + Enumeration class that holds the following constant values (integers) representing the state of the controller on the bus: |
| 316 | + |
| 317 | + - ``STOPPED`` - The controller is not interacting with the bus. |
| 318 | + - ``ERROR_ACTIVE`` - The controller is in the Error-Active and TEC and REC are both less than 96. |
| 319 | + - ``ERROR_WARNING`` - The controller is in the Error-Warning state, meaning |
| 320 | + at least one of ``TEC`` or ``REC`` is 96 or greater. |
| 321 | + - ``ERROR_PASSIVE`` - The controller is in the Error-Passive state, meaning |
| 322 | + at least one of ``TEC`` or ``REC`` is 128 or greater. |
| 323 | + - ``BUS_OFF`` - The controller is in the Bus-Off state, meaning ``TEC`` is |
| 324 | + greater than 255. It does not currently have any influence on Bus activity. |
| 325 | + |
| 326 | +.. class:: CAN.ErrorFlags |
| 327 | + |
| 328 | + Enumeration class representing error conditions that have been detected by the controller. Cleared on reset. :func:`CAN.get_errors()`. |
| 329 | + |
| 330 | + TODO |
| 331 | + |
| 332 | +.. class:: CAN.MessageFlags |
| 333 | + |
| 334 | + Enumeration class representing possible conditions of a CAN message, as bitwise ORed together flags. |
| 335 | + |
| 336 | + - ``RTR`` - Message is a remote transmission request. If this bit is set, the |
| 337 | + ``data`` value should be an integer (indicating the length from the ``DLC`` |
| 338 | + field), not a bytes object. |
| 339 | + - ``EXTENDED_ID`` - Message identifier is Extended (29-bit). If not set, message |
| 340 | + identifier is Standard (11-bit). |
| 341 | + - ``FD_F``- Message is CAN FD in the FD data format, meaning data payload |
| 342 | + can be up to 64 bytes long. Passing this flag to a controller which lacks |
| 343 | + CAN FD support will raise ``ValueError``. |
| 344 | + - ``BRS`` - For CAN FD controllers and FD data format messages, indicates the |
| 345 | + bitrate should be switched during the data phase. |
| 346 | + |
| 347 | +.. class:: CAN.RecvErrors |
| 348 | + |
| 349 | + Enumeration class representing possible error statuses when |
| 350 | + receiving a CAN message. Multiple values may be ORed together. |
| 351 | + |
| 352 | + - ``CRC`` - A CRC error occurred. |
| 353 | + - ``FORM`` - A form error occurred in a fixed-form bit field. |
| 354 | + - ``OVERRUN`` - One or more messages overran the receive hardware |
| 355 | + queue and were lost. |
| 356 | + - ``ESI`` - The ESI flag of a received CAN FD message was set. |
| 357 | + |
| 358 | +.. class:: CAN.SendErrors |
| 359 | + |
| 360 | + Enumeration class representing possible error statuses when |
| 361 | + sending a CAN message. Multiple values may be ORed together. |
| 362 | + |
| 363 | + - ``NACK`` - Sent message was not ACKed. |
| 364 | + - ``BIT`` - A bit error was detected. |
0 commit comments