reworked adc and can

This commit is contained in:
gijsio
2020-08-12 17:17:16 +02:00
parent 75581df556
commit 4e8e7d03ce
2 changed files with 14 additions and 16 deletions

View File

@@ -36,7 +36,7 @@ Enable the ADC block. This method is automatically called on object creation.
Disable the ADC block.
### adc.channel(\* , pin, attn=ADC.ATTN_0DB)
### adc.channel(pin, attn=ADC.ATTN_0DB)
Create an analog pin.

View File

@@ -23,33 +23,35 @@ can.recv()
## Constructors
#### class machine.CAN(bus=0, ...)
### class machine.CAN([bus=0])
Create an CAN object. See init for parameters of initialisation.:
```python
# only 1 CAN peripheral is available, so the bus must always be 0
can = CAN(0, mode=CAN.NORMAL, baudrate=500000, pins=('P22', 'P23')) # pin order is Tx, Rx
```
## Methods
#### can.init(mode=CAN.NORMAL, baudrate=500000, \*, frame\_format=CAN.FORMAT\_STD, rx\_queue\_len=128, pins=('P22', 'P23'))
### can.init([mode=CAN.NORMAL, baudrate=500000, frame_format=CAN.FORMAT_STD, rx_queue_len=128, pins=('P22', 'P23')])
Initialize the CAN controller. The arguments are:
* `mode` can take either CAN.NORMAL or CAN.SILENT. Silent mode is useful for sniffing the bus.
* `baudrate` sets up the bus speed. Acceptable values are between 1 and 1000000.
* `frame_format` defines the frame format to be accepted by the receiver. Useful for filtering frames based on the identifier length. Can tale either `CAN.FORMAT_STD`, `CAN.FORMAT_EXT`, `CAN.FORMAT_BOTH`. If `CAN.FORMAT_STD` is selected, extended frames won't be received and vice-versa.
* `rx_queue_len` defines the number of messages than can be queued by the receiver. Due to CAN being a high traffic bus, large values are recommended (>= 128), otherwise messages will be dropped specially when no filtering is applied.
* `frame_format` defines the frame format to be accepted by the receiver. Useful for filtering frames based on the identifier length. Can tale either:
* `CAN.FORMAT_STD`: With this option, extended frames won't be received and vice-versa.
* `CAN.FORMAT_EXT`
* `CAN.FORMAT_BOTH`
* `rx_queue_len` defines the number of messages than can be queued by the receiver. Due to CAN being a high traffic bus, large values are recommended (>= 128), otherwise messages will be dropped specially when no filtering is applied.
* `pins` selects the `Tx` and `Rx` pins (in that order).
#### can.deinit()
### can.deinit()
Disables the CAN bus.
#### can.send(id, \* , data=None, rtr=False, extended=False)
### can.send(id, [data=None, rtr=False, extended=False])
Send a CAN frame on the bus
@@ -61,7 +63,6 @@ Send a CAN frame on the bus
Can be used like:
```python
can.send(id=0x0020, data=bytes([0x01, 0x02, 0x03, 0x04, 0x05]), extended=True) # sends 5 bytes with an extended identifier
can.send(id=0x010, data=bytes([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])) # sends 8 bytes with an standard identifier
@@ -69,19 +70,18 @@ can.send(id=0x010, data=bytes([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]))
can.send(id=0x012, rtr=True) # sends a remote request for message id=0x12
```
#### can.recv(timeout=0)
### can.recv([timeout=0])
Get a message from the receive queue, and optionally specify a timeout value in **s** (can be a floating point value e.g. `0.2`). This function returns `None` if no messages available. If a message is present, it will be returned as a named tuple with the following form:
`(id, data, rtr, extended)`
```python
>>> can.recv()
(id=0x012, data=b'123', rtr=False, extended=False)
```
#### can.soft\_filter(mode, filter\_list)
### can.soft_filter(mode, filter_list)
Specify a software filter accepting only the messages that pass the filter test.
@@ -96,7 +96,6 @@ With software filters all messages in the bus are received by the CAN controller
For example:
```python
can.soft_filter(CAN.FILTER_LIST, [0x100, 0x200, 0x300, 0x400]) # only accept identifiers from 0x100, 0x200, 0x300 and 0x400
can.soft_filter(CAN.FILTER_RANGE, [(0x001, 0x010), (0x020, 0x030), (0x040, 0x050)]) # only accept identifiers from 0x001 to 0x010, from 0x020 to 0x030 and from 0x040 to 0x050.
@@ -106,7 +105,7 @@ can.soft_filter(CAN.FILTER_MASK, [(0x100, 0x7FF), (0x200, 0x7FC)]) # more of the
can.soft_filter(None) # disable soft filters, all messages are accepted
```
#### can.callback(trigger, handler=None, arg=None)
### can.callback(trigger, [handler=None, arg=None])
Set a callback to be triggered when any of this 3 events are present:
@@ -123,7 +122,6 @@ The values can be OR-ed together, for instance `trigger=CAN.RX_FRAME | CAN.RX_FI
It can be used like this:
```python
from machine import CAN
can = CAN(mode=CAN.NORMAL, baudrate=500000, pins=('P22', 'P23'))
@@ -134,7 +132,7 @@ def can_cb(can_o):
can.callback(handler=can_cb, trigger=CAN.RX_FRAME)
```
#### can.events()
### can.events()
This method returns a value with bits sets (if any) indicating the events that have occurred in the bus. Please note that by calling this function the internal events registry is cleared automatically, therefore calling it immediately for a second time will most likely return a value of 0.