mirror of
https://github.com/m5stack/m5-docs.git
synced 2026-05-20 10:23:01 -07:00
add api(timer, uart) faq
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
Timer API
|
||||
**********
|
||||
|
||||
-----------------------------
|
||||
|
||||
Description
|
||||
------------
|
||||
|
||||
The ESP32 chip contains two hardware timer groups. Each group has two general-purpose hardware timers. They are all 64-bit generic timers based on 16-bit prescalers and 64-bit auto-reload-capable up / down counters.
|
||||
|
||||
*Note: Due to MicroPython callback latency, some callbacks may not be executed if the timer period is less than 15 ms.
|
||||
The number of events and executed callbacks can be checked using tm.events() method.*
|
||||
|
||||
-----------------------------
|
||||
|
||||
Function
|
||||
---------
|
||||
|
||||
machine.Timer(timer_no)
|
||||
>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
| **Description:**
|
||||
| Create the Timer instance object
|
||||
|
||||
| **Parament:**
|
||||
| **timer_no:** the timer number to be used for the timer
|
||||
|
||||
| **Example:**
|
||||
.. code:: python
|
||||
|
||||
import machine
|
||||
|
||||
p1 = machine.Pin(27)
|
||||
|
||||
init(period, mode, callback, dbgpin)
|
||||
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
| **Description:**
|
||||
| initialize paraments of the timer
|
||||
|
||||
| **Parament:**
|
||||
| **period:** Timer period(Default: 10 ms)
|
||||
| *Note: Timer period in ms, only used for PERIODIC and ONE_SHOT timers*
|
||||
| **mode:** Timer mode of operation(Default: PERIODIC)
|
||||
| **callback:** The Python callback function to be executed on timer event(Default: None)
|
||||
| **dbgpin:** GPIO pin to be used as debug output(Default: -1 (not used))
|
||||
| *Note: If used, the gpio level will toggle on each timer event*
|
||||
|
||||
| **Return:**
|
||||
| None
|
||||
|
||||
| **Example:**
|
||||
.. code:: python
|
||||
|
||||
from machine import SPI, Pin
|
||||
|
||||
spi = SPI(
|
||||
spihost=SPI.HSPI,
|
||||
baudrate=2600000
|
||||
sck=Pin(18),
|
||||
mosi=Pin(23),
|
||||
miso=Pin(19),
|
||||
cs=Pin(4)
|
||||
)
|
||||
|
||||
spi.write(buf) #NOHEAP
|
||||
|
||||
|
||||
value()
|
||||
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
| **Description:**
|
||||
|
||||
| **Return:**
|
||||
| Returns the current timer counter value in **µs** if timer mode is CHRONO or in **ms** for other modes
|
||||
|
||||
| **Example:**
|
||||
.. code:: python
|
||||
|
||||
import machine
|
||||
|
||||
p1 = machine.Pin(27)
|
||||
p1.init(p1.OUT)
|
||||
p1.value(1)
|
||||
|
||||
|
||||
---------------------
|
||||
|
||||
Usage
|
||||
------
|
||||
|
||||
.. code:: python
|
||||
|
||||
import machine
|
||||
|
||||
tcounter = 0
|
||||
|
||||
p1 = machine.Pin(27)
|
||||
p1.init(p1.OUT)
|
||||
p1.value(1)
|
||||
|
||||
def tcb(timer):
|
||||
global tcounter
|
||||
if tcounter & 1:
|
||||
p1.value(0)
|
||||
else:
|
||||
p1.value(1)
|
||||
tcounter += 1
|
||||
if (tcounter % 10000) == 0:
|
||||
print("[tcb] timer: {} counter: {}".format(timer.timernum(), tcounter))
|
||||
|
||||
t1 = machine.Timer(2)
|
||||
t1.init(period=20, mode=t1.PERIODIC, callback=tcb)
|
||||
@@ -0,0 +1,108 @@
|
||||
UART API
|
||||
*********
|
||||
|
||||
-----------------------------
|
||||
|
||||
Description
|
||||
------------
|
||||
|
||||
An Universal Asynchronous Receiver/Transmitter (UART) is a component known to handle the timing requirements for a variety of widely-adapted protocols (RS232, RS485, RS422, …).
|
||||
An UART provides a widely adopted and cheap method to realize full-duplex data exchange among different devices.
|
||||
There are three UART controllers available on the ESP32 chip. They are compatible with UART-enabled devices from various manufacturers. All UART controllers integrated in the ESP32 feature an identical set of registers for ease of programming and flexibility. In this documentation, these controllers are referred to as UART0, UART1, and UART2.
|
||||
|
||||
|
||||
-----------------------------
|
||||
|
||||
Function
|
||||
---------
|
||||
|
||||
machine.UART(uart_num, tx=pin, rx=pin [,args])
|
||||
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
| **Description:**
|
||||
| Create the uart instance object
|
||||
|
||||
| **Parament:**
|
||||
| **uart_num:** The hardware SPI host. machine-SPI.HSPI (1) or machine.SPI.VSPI (2) can be used. Default: 1
|
||||
| **tx:** the gpio used for tx
|
||||
| **rx:** the gpio used for rx
|
||||
|
||||
|
||||
| **Example:**
|
||||
.. code:: python
|
||||
|
||||
from machine import UART
|
||||
|
||||
uart2 = UART(2, tx=17, rx=16)
|
||||
uart2.init(115200, bits=8, parity=None, stop=1)
|
||||
|
||||
write(buf [, len [, off]])
|
||||
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
| **Description:**
|
||||
| Write bytes from buffer object ḃuf to UART
|
||||
|
||||
| **Parament:**
|
||||
| **buf:** data buffer to be write
|
||||
| **len:** writes `ŀen` bytes
|
||||
| **off:** starts writting from `off` position in `buf`
|
||||
|
||||
| **Example:**
|
||||
.. code:: python
|
||||
|
||||
from machine import UART
|
||||
|
||||
uart2 = UART(2, tx=17, rx=16)
|
||||
uart2.init(115200, bits=8, parity=None, stop=1)
|
||||
uart2.write('abc') # write the 3 characters
|
||||
|
||||
read()
|
||||
>>>>>>>
|
||||
|
||||
| **Description:**
|
||||
| read all available characters
|
||||
|
||||
| **Example:**
|
||||
.. code:: python
|
||||
|
||||
from machine import UART
|
||||
|
||||
uart2 = UART(2, tx=17, rx=16)
|
||||
uart2.init(115200, bits=8, parity=None, stop=1)
|
||||
uart2.read() # read all available characters
|
||||
|
||||
readline([max_len])
|
||||
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
| **Description:**
|
||||
| Reads all bytes from the receive buffer up to the line end character **\n**
|
||||
|
||||
| **Parament:**
|
||||
| **max_len:** maximum length to be read
|
||||
|
||||
| **Example:**
|
||||
.. code:: python
|
||||
|
||||
from machine import UART
|
||||
|
||||
uart2 = UART(2, tx=17, rx=16)
|
||||
uart2.init(115200, bits=8, parity=None, stop=1)
|
||||
uart2.readline() # read a line
|
||||
|
||||
|
||||
---------------------
|
||||
|
||||
Usage
|
||||
------
|
||||
|
||||
.. code:: python
|
||||
|
||||
from machine import UART
|
||||
|
||||
uart2 = UART(2, tx=17, rx=16)
|
||||
uart2.init(115200, bits=8, parity=None, stop=1)
|
||||
uart2.read(10) # read 10 characters, returns a bytes object
|
||||
uart2.read() # read all available characters
|
||||
uart2.readline() # read a line
|
||||
uart2.readinto(buf) # read and store into the given buffer
|
||||
uart2.write('abc') # write the 3 characters
|
||||
Reference in New Issue
Block a user