This commit is contained in:
刘可亮
2024-09-03 11:16:08 +08:00
parent cf270df8d6
commit 803cac77d5
2931 changed files with 614364 additions and 31222 deletions

View File

@@ -0,0 +1,40 @@
## **machine** 与硬件相关的功能
**machine** 模块包含与特定开发板上的硬件相关的特定函数。 在这个模块中的大多数功能允许实现直接和不受限制地访问和控制系统上的硬件块如CPU定时器总线等。如果使用不当会导致故障死机崩溃在极端的情况下硬件会损坏。
需要注意的是由于不同开发板的硬件资源不同MicroPython 移植所能控制的硬件也是不一样的。因此对于控制硬件的例程来说,在使用前需要修改相关的配置参数来适配不同的开发板,或者直接运行已经对某一开发板适配好的 MicroPython 示例程序。本文档中的例程都是基于 RT-Thread IoT Board 潘多拉开发板而讲解的。
### 函数
#### 复位相关函数
##### **machine.info**()
显示关于系统介绍和内存占用等信息。
##### **machine.reset**() 注:暂未实现
重启设备,类似于按下复位按钮。
##### **machine.reset_cause**() 注:暂未实现
获得复位的原因,查看可能的返回值的常量。
#### 中断相关函数
##### **machine.disable_irq**()
禁用中断请求。返回先前的 `IRQ` 状态,该状态应该被认为是一个未知的值。这个返回值应该在 `disable_irq` 函数被调用之前被传给 `enable_irq` 函数来重置中断到初始状态。
##### **machine.enable_irq**(state)
重新使能中断请求。状态参数应该是从最近一次禁用功能的调用中返回的值。
#### 功耗相关函数
##### **machine.freq**()
返回 `CPU` 的运行频率。
##### **machine.idle**() 注:暂未实现
阻断给 `CPU` 的时钟信号,在较短或者较长的周期里减少功耗。当中断发生时,外设将继续工作。
##### **machine.sleep**() 注:暂未实现
停止 `CPU` 并禁止除了 `WLAN` 之外的所有外设。系统会从睡眠请求的地方重新恢复工作。为了确保唤醒一定会发生,应当首先配置中断源。
##### **machine.deepsleep**() 注:暂未实现
停止 `CPU` 和所有外设(包括网络接口)。执行从主函数中恢复,就像被复位一样。复位的原因可以检查 `machine.DEEPSLEEP` 参数获得。为了确保唤醒一定会发生,应该首先配置中断源,比如一个引脚的变换或者 `RTC` 的超时。

View File

@@ -0,0 +1,44 @@
## machine.ADC
**machine.ADC** 类是 machine 模块下的一个硬件类,用于指定 ADC 设备的配置和控制,提供对 ADC 设备的操作方法。
- ADCAnalog-to-Digital Converter模数转换器用于将连续变化的模拟信号转化为离散的数字信号。
- ADC 设备两个重要参数:采样值、分辨率;
- 采样值:当前时间由模拟信号转化的数值信号的数值;
- 分辨率:以二进制(或十进制)数的位数来表示,一般有 8 位、10 位、12 位、16 位等,它说明模数转换器对输入信号的分辨能力,位数越多,表示分辨率越高,采样值会更精确。
### 构造函数
在 RT-Thread MicroPython 中 `ADC` 对象的构造函数如下:
#### **class machine.ADC**(id, channel)
- **id**:使用的 ADC 设备编号,`id = 1` 表示编号为 1 的 ADC 设备,或者表示使用的 ADC 设备名,如 `id = "adc"` 表示设备名为 `adc` 的 ADC 设备;
- **channel**:使用的 ADC 设备通道号,每个 ADC 设备对应多个通道;
例如:`ADC(1,4)` 表示当前使用编号为 1 的 ADC 设备的 4 通道。
### 方法
#### **ADC.init**(channel)
根据输入的层参数初始化 ADC 对象,入参为使用的 ADC 对象通道号;
#### **ADC.deinit**()
用于关闭 ADC 对象ADC 对象 deinit 之后需要重新 init 才能使用。
#### **ADC.read**()
用于获取并返回当前 ADC 对象的采样值。例如当前采样值为 2048对应设备的分辨率为 12位当前设备参考电压为 3.3V ,则该 ADC 对象通道上实际电压值的计算公式为:**采样值 * 参考电压 / 1 << 分辨率位数)**,即 `vol = 2048 / 4096 * 3.3 V = 1.15V`
### 示例
``` python
>>> from machine import ADC # 从 machine 导入 ADC 类
>>> adc = ADC(1, 13) # 创建 ADC 对象,当前使用编号为 1 的 ADC 设备的 13 通道
>>> adc.read() # 获取 ADC 对象采样值
4095
>>> adc.deinit() # 关闭 ADC 对象
>>> adc.init(13) # 开启并重新配置 ADC 对象
```

View File

@@ -0,0 +1,111 @@
## machine.I2C
**machine.I2C** 类是 `machine` 模块下面的一个硬件类,用于对 `I2C` 的配置和控制,提供对 `I2C` 设备的操作方法。
- `I2C` 是一种用于设备间通信的两线协议。在物理层上,它由两根线组成:`SCL``SDA` ,即时钟和数据线。
- `I2C` 对象被创建到一个特定的总线上,它们可以在创建时被初始化,也可以之后再来初始化。
- 打印 `I2C` 对象会打印出配置时的信息。
### 构造函数
在 RT-Thread MicroPython 中 `I2C` 对象的构造函数如下:
#### **class machine.I2C**(id= -1, scl, sda, freq=400000)
使用下面的参数构造并返回一个新的 `I2C` 对象:
- **id** :标识特定的 `I2C` 外设。如果填入 id = -1即选择软件模拟的方式实现 `I2C`,这时可以使用任意引脚来模拟 `I2C` 总线 ,这样在初始化时就必须指定 `scl``sda`
软件 I2C 的初始化方式可参考 [软件 I2C 示例](#i2c_2)。
硬件 I2C 的初始化方式可参考 [硬件 I2C 示例](#i2c_3)。
- **scl** : 应该是一个 `Pin` 对象,指定为一个用于 `scl``Pin` 对象。
- **sda** : 应该是一个 `Pin` 对象,指定为一个用于 `sda``Pin` 对象。
- **freq** :应该是为 `scl` 设置的最大频率。
### 方法
#### **I2C.init**(scl, sda, freq=400000)
初始化 `I2C` 总线,参数介绍可以参考构造函数中的参数。
#### **I2C.deinit**()
关闭 `I2C` 总线。
#### **I2C.scan**()
扫描所有 0x08 和 0x77 之间的 `I2C` 地址,然后返回一个有响应地址的列表。如果一个设备在总线上收到了他的地址,就会通过拉低 `SDA` 的方式来响应。
### I2C 基础方法
下面的方法实现了基本的 `I2C` 总线操作,可以组合成任何的 `I2C` 通信操作,如果需要对总线进行更多的控制,可以可以使用他们,否则可以使用后面介绍的标准使用方法。
#### **I2C.start**()
在总线上产生一个启动信号。(`SCL` 为高时,`SDA` 转换为低)
#### **I2C.stop**()
在总线上产生一个停止信号。(`SCL` 为高时,`SDA` 转换为高)
#### **I2C.readinto**(buf, nack=True)
从总线上读取字节并将他们存储到 `buf` 中,读取的字节数时 `buf` 的长度。在收到最后一个字节以外的所有内容后,将在总线上发送 `ACK`。在收到最后一个字节之后,如果 `NACK` 是正确的,那么就会发送一个 `NACK`,否则将会发送 `ACK`
#### **I2C.write**(buf)
`buf` 中的数据接入到总线,检查每个字节之后是否收到 `ACK`,并在收到 `NACK` 时停止传输剩余的字节。这个函数返回接收到的 `ACK` 的数量。
### I2C 标准总线操作
下面的方法实现了标准 `I2C` 主设备对一个给定从设备的读写操作。
#### **I2C.readfrom**(addr, nbytes, stop=True)
`addr` 指定的从设备中读取 n 个字节,如果 `stop = True`,那么在传输结束时会产生一个停止信号。函数会返回一个存储着读到数据的字节对象。
#### **I2C.readfrom_into**(addr, buf, stop=True)
`addr` 指定的从设备中读取数据存储到 `buf` 中,读取的字节数将是 `buf` 的长度,如果 `stop = True`,那么在传输结束时会产生一个停止信号。
这个方法没有返回值。
#### **I2C.writeto**(addr, buf, stop=True)
`buf` 中的数据写入到 `addr` 指定的的从设备中,如果在写的过程中收到了 `NACK` 信号,那么就不会发送剩余的字节。如果 `stop = True`,那么在传输结束时会产生一个停止信号,即使收到一个 `NACK`。这个函数返回接收到的 `ACK` 的数量。
### 内存操作
一些 `I2C` 设备充当一个内存设备,可以读取和写入。在这种情况下,有两个与 `I2C` 相关的地址,从机地址和内存地址。下面的方法是与这些设备进行通信的便利函数。
#### **I2C.readfrom_mem**(addr, memaddr, nbytes, \*, addrsize=8)
`addr` 指定的从设备中 `memaddr` 地址开始读取 n 个字节。`addrsize` 参数指定地址的长度。返回一个存储读取数据的字节对象。
#### **I2C.readfrom_mem_into**(addr, memaddr, buf, \*, addrsize=8)
`addr` 指定的从设备中 `memaddr` 地址读取数据到 `buf` 中,,读取的字节数是 `buf` 的长度。
这个方法没有返回值。
#### **I2C.writeto_mem**(addr, memaddr, buf, \*, addrsize=8)
`buf` 里的数据写入 `addr` 指定的从机的 `memaddr` 地址中。
这个方法没有返回值。
### 示例
#### 软件模拟 I2C
```python
>>> from machine import Pin, I2C
>>> clk = Pin(("clk", 29), Pin.OUT_OD) # Select the 29 pin device as the clock
>>> sda = Pin(("sda", 30), Pin.OUT_OD) # Select the 30 pin device as the data line
>>> i2c = I2C(-1, clk, sda, freq=100000) # create I2C peripheral at frequency of 100kHz
>>> i2c.scan() # scan for slaves, returning a list of 7-bit addresses
[81] # Decimal representation
>>> i2c.writeto(0x51, b'123') # write 3 bytes to slave with 7-bit address 42
3
>>> i2c.readfrom(0x51, 4) # read 4 bytes from slave with 7-bit address 42
b'\xf8\xc0\xc0\xc0'
>>> i2c.readfrom_mem(0x51, 0x02, 1) # read 1 bytes from memory of slave 0x51(7-bit),
b'\x12' # starting at memory-address 8 in the slave
>>> i2c.writeto_mem(0x51, 2, b'\x10') # write 1 byte to memory of slave 42,
# starting at address 2 in the slave
```
#### 硬件 I2C
需要先开启 `I2C` 设备驱动,查找设备可以在 `msh` 中输入`list_device` 命令。
在构造函数的第一个参数传入 `0`,系统就会搜索名为 `i2c0` 的设备,找到之后使用这个设备来构建 `I2C` 对象:
```python
>>> from machine import Pin, I2C
>>> i2c = I2C(0) # create I2C peripheral at frequency of 100kHz
>>> i2c.scan() # scan for slaves, returning a list of 7-bit addresses
[81] # Decimal representation
```
更多内容可参考 [machine.I2C](http://docs.micropython.org/en/latest/pyboard/library/machine.I2C.html) 。

View File

@@ -0,0 +1,76 @@
## machine.LCD
**machine.LCD** 类是 machine 模块下面的一个硬件类,用于对 LCD 的配置和控制,提供对 LCD 设备的操作方法。
IoT board 板载一块 1.3 寸,分辨率为 `240*240` 的 LCD 显示屏,因此对该屏幕操作时,(x, y) 坐标的范围是 `0 - 239`
### 构造函数
在 RT-Thread MicroPython 中 `LCD` 对象的构造函数如下:
#### **class machine.LCD**()
在给定总线上构造一个 `LCD` 对象,无入参,初始化的对象取决于特定硬件,初始化方式可参考 [示例](#_3)。
### 方法
#### **LCD.light**(value)
控制是否开启 LCD 背光,入参为 True 则打开 LCD 背光,入参为 False 则关闭 LCD 背光。
#### **LCD.fill**(color)
根据给定的颜色填充整个屏幕,支持多种颜色,可以传入的参数有:
```
WHITE BLACK BLUE BRED GRED GBLUE RED MAGENTA GREEN CYAN YELLOW BROWN BRRED GRAY GRAY175 GRAY151 GRAY187 GRAY240
```
详细的使用方法可参考[示例](#_3)。
#### **LCD.pixel**(x, y, color)
向指定的位置x, y画点点的颜色为 color 指定的颜色,可指定的颜色和上一个功能相同。
> 注意:(x, y) 坐标不要超过实际范围,使用下面的方法对坐标进行操作时同样需要遵循此限制。
#### **LCD.text**(str, x, y, size)
在指定的位置x,y写入字符串字符串由 str 指定,字体的大小由 size 指定size 的大小可为 162432。
#### **LCD.line**(x1, y1, x2, y2)
在 LCD 上画一条直线,起始地址为 x1, y1终点为x2, y2
#### **LCD.rectangle**(x1, y1, x2, y2)
在 LCD 上画一个矩形左上角的位置为x1, y1右下角的位置为x2, y2
#### **LCD.circle**(x1, y1, r)
在 LCD 上画一个圆形圆心的位置为x1, y1半径长度为 r。
#### **LCD.show_bmp**( x, y, pathname)
在 LCD 指定位置上显示 32-bit bmp 格式的图片信息,注意显示 bmp 图片时,(x, y) 坐标是图片的左下角。
### 示例
```python
from machine import LCD # 从 machine 导入 LCD 类
lcd = LCD() # 创建一个 lcd 对象
lcd.light(False) # 关闭背光
lcd.light(True) # 打开背光
lcd.fill(lcd.BLACK) # 将整个 LCD 填充为黑色
lcd.fill(lcd.RED) # 将整个 LCD 填充为红色
lcd.fill(lcd.GRAY) # 将整个 LCD 填充为灰色
lcd.fill(lcd.WHITE) # 将整个 LCD 填充为白色
lcd.pixel(50, 50, lcd.BLUE) # 将50,50位置的像素填充为蓝色
lcd.text("hello RT-Thread", 0, 0, 16) # 在0, 0 位置以 16 字号打印字符串
lcd.text("hello RT-Thread", 0, 16, 24) # 在0, 16位置以 24 字号打印字符串
lcd.text("hello RT-Thread", 0, 48, 32) # 在0, 48位置以 32 字号打印字符串
lcd.line(0, 50, 239, 50) # 以起点050终点23950画一条线
lcd.line(0, 50, 239, 50) # 以起点050终点23950画一条线
lcd.rectangle(100, 100, 200, 200) # 以左上角为100,100右下角200,200画矩形
lcd.circle(150, 150, 80) # 以圆心位置150,150半径为 80 画圆
lcd.show_bmp(180, 50, "sun.bmp") # 以位置180,50为图片左下角坐标显示文件系统中的 bmp 图片 "sun.bmp"
```

View File

@@ -0,0 +1,57 @@
## machine.PWM
**machine.PWM** 类是 machine 模块下的一个硬件类,用于指定 PWM 设备的配置和控制,提供对 PWM 设备的操作方法。
- PWM (Pulse Width Modulation脉冲宽度调制) 是一种对模拟信号电平进行数字编码的方式;
- PWM 设备可以通过调节有效电平在一个周期信号中的比例时间来操作设备;
- PWM 设备两个重要的参数频率freq和占空比duty
- 频率:从一个上升沿(下降沿)到下一个上升沿(下降沿)的时间周期,单位为 Hz
- 占空比:有效电平(通常为电平)在一个周期内的时间比例;
### 构造函数
在 RT-Thread MicroPython 中 `PWM` 对象的构造函数如下:
#### **class machine.PWM**(id, channel, freq, duty)
在给定的总线上构建一个 `PWM` 对象,参数介绍如下:
- **id**:使用的 PWM 设备编号,如 `id = 1` 表示编号为 1 的 PWM 设备,或者表示使用的 PWM 设备名,如 `id = "pwm"` 表示设备名为 `pwm` 的 PWM 设备;
- **channel**:使用的 PWM 设备通道号,每个 PWM 设备包含多个通道,范围为 [0, 4]
- **freq**:初始化频率,范围 [1, 156250]
- **duty**:初始化占空比数值,范围 [0 255]
例如:`PWM(1,4,100,100)` 表示当前使用 编号为 1 的 PWM 设备的 4 通道,初始化频率为 1000 Hz初始化占空比的数值为 100。
### 方法
#### **PWM.init**(channel, freq, duty)
根据输入的参数初始化 PWM 对象,参数说明同上。
#### **PWM.deinit**()
用于关闭 PWM 对象,对象 deinit 之后需要重新 init 才能使用。
#### **PWM.freq**(freq)
用于获取或者设置 PWM 对象的频率,频率的范围为 [1, 156250]。如果参数为空,返回当前 PWM 对象的频率;如果参数非空,则使用该参数设置当前 PWM 对象的频率。
#### **PWM.duty**(duty)
用于获取或者设置 PWM 对象的占空比数值,占空比数值的范围为 [0, 255],例如 `duty = 100`,表示当前设备占空比为 `100/255 = 39.22%` 。如果参数为空,返回当前 PWM 对象的占空比数值;如果参数非空,则使用该参数设置当前 PWM 对象的占空比数值。
### 示例
``` python
>>> from machine import PWM # 从 machine 导入 PWM 类
>>> pwm = PWM(3, 3, 1000, 100) # 创建 PWM 对象,当前使用编号为 3 的 PWM 设备的 3 通道,初始化的频率为 1000Hz占空比数值为 100占空比为 100/255 = 39.22%
>>> pwm.freq(2000) # 设置 PWM 对象频率
>>> pwm.freq() # 获取 PWM 对象频率
2000
>>> pwm.duty(200) # 设置 PWM 对象占空比数值
>>> pwm.duty() # 获取 PWM 对象占空比数值
200
>>> pwm.deinit() # 关闭 PWM 对象
>>> pwm.init(3, 1000, 100) # 开启并重新配置 PWM 对象
```

View File

@@ -0,0 +1,110 @@
## machine.Pin
**machine.Pin** 类是 machine 模块下面的一个硬件类,用于对引脚的配置和控制,提供对 `Pin` 设备的操作方法。
`Pin` 对象用于控制输入/输出引脚(也称为 `GPIO`)。`Pin` 对象通常与一个物理引脚相关联他可以驱动输出电压和读取输入电压。Pin 类中有设置引脚模式(输入/输出)的方法,也有获取和设置数字逻辑(`0``1`)的方法。
一个 `Pin` 对象是通过一个标识符来构造的,它明确地指定了一个特定的输入输出。标识符的形式和物理引脚的映射是特定于一次移植的。标识符可以是整数,字符串或者是一个带有端口和引脚号码的元组。在 RT-Thread MicroPython 中,引脚标识符是一个由代号和引脚号组成的元组,如 `Pin(("PB15", 31), Pin.OUT_PP)` 中的` ("PB15", 31)`
### 构造函数
在 RT-Thread MicroPython 中 `Pin` 对象的构造函数如下:
#### **class machine.Pin**( id, mode = -1, pull = -1value)
- **id** :由用户自定义的引脚名和 `Pin` 设备引脚号组成,如 ("PB15", 31)"PB15" 为用户自定义的引脚名,`31``RT-Thread Pin` 设备驱动在本次移植中的引脚号。
- **mode** 指定引脚模式,可以是以下几种:
- **Pin.IN** :输入模式
- **Pin.OUT_PP** :输出模式
- **Pin.OUT_OD** :开漏模式
- **pull** 如果指定的引脚连接了上拉下拉电阻,那么可以配置成下面的状态:
- **None** :没有上拉或者下拉电阻。
- **Pin.PULL_UP** :使能上拉电阻。
- **Pin.PULL_DOWN** :使能下拉电阻。
- **value** `value` 的值只对输出模式和开漏输出模式有效,用来设置初始输出值。
### 方法
#### **Pin.init**(mode= -1, pull= -1, \*, value, drive, alt)
根据输入的参数重新初始化引脚。只有那些被指定的参数才会被设置,其余引脚的状态将保持不变,详细的参数可以参考上面的构造函数。
#### **Pin.value**([x])
如果没有给定参数 `x` ,这个方法可以获得引脚的值。
如果给定参数 `x` ,如 `0``1`,那么设置引脚的值为 逻辑 `0` 或 逻辑 `1`
#### **Pin.name**()
返回引脚对象在构造时用户自定义的引脚名。
#### **Pin.irq**(handler=None, trigger=(Pin.IRQ_RISING))
配置在引脚的触发源处于活动状态时调用的中断处理程序。如果引脚模式是, `Pin.IN` 则触发源是引脚上的外部值。 如果引脚模式是, `Pin.OUT` 则触发源是引脚的输出缓冲器。 否则,如果引脚模式是, `Pin.OPEN_DRAIN` 那么触发源是状态'0'的输出缓冲器和状态'1'的外部引脚值。
参数:
- `handler` 是一个可选的函数,在中断触发时调用
- `trigger` 配置可以触发中断的事件。可能的值是:
- `Pin.IRQ_FALLING` 下降沿中断
- `Pin.IRQ_RISING` 上升沿中断
- `Pin.IRQ_RISING_FALLING` 上升沿或下降沿中断
- `Pin.IRQ_LOW_LEVEL` 低电平中断
- `Pin.IRQ_HIGH_LEVEL` 高电平中断
### 常量
下面的常量用来配置 `Pin` 对象。
#### 选择引脚模式:
##### **Pin.IN**
##### **Pin.OUT_PP**
##### **Pin.OUT_OD**
#### 选择上/下拉模式:
##### **Pin.PULL_UP**
##### **Pin.PULL_DOWN**
##### **None**
使用值 `None` 代表不进行上下拉。
#### 选择中断触发模式:
##### **Pin.IRQ_FALLING**
##### **Pin.IRQ_RISING**
##### **Pin.IRQ_RISING_FALLING**
##### **Pin.IRQ_LOW_LEVEL**
##### **Pin.IRQ_HIGH_LEVEL**
### 示例一
控制引脚输出高低电平信号,并读取按键引脚电平信号。
```
from machine import Pin
PIN_OUT = 31
PIN_IN = 58
p_out = Pin(("PB15", PIN_OUT), Pin.OUT_PP)
p_out.value(1) # set io high
p_out.value(0) # set io low
p_in = Pin(("key_0", PIN_IN), Pin.IN, Pin.PULL_UP)
print(p_in.value() ) # get value, 0 or 1
```
### 示例二
上升沿信号触发引脚中断后执行中断处理函数。
```
from machine import Pin
PIN_KEY0 = 58 # PD10
key_0 = Pin(("key_0", PIN_KEY0), Pin.IN, Pin.PULL_UP)
def func(v):
print("Hello rt-thread!")
key_0.irq(trigger=Pin.IRQ_RISING, handler=func)
```
更多内容可参考 [machine.Pin](http://docs.micropython.org/en/latest/pyboard/library/machine.Pin.html) 。

View File

@@ -0,0 +1,56 @@
## machine.RTC
**machine.RTC** 类是 machine 模块下面的一个硬件类,用于对指定 RTC 设备的配置和控制,提供对 RTC 设备的操作方法。
- RTCReal-Time Clock )实时时钟可以提供精确的实时时间,它可以用于产生年、月、日、时、分、秒等信息。
### 构造函数
在 RT-Thread MicroPython 中 `RTC` 对象的构造函数如下:
#### **class machine.RTC**()
所以在给定的总线上构造一个 `RTC` 对象,无入参对象,使用方式可参考 [示例](#_3)。
### 方法
#### **RTC.init**(datetime)
根据传入的参数初始化 RTC 设备起始时间。入参 `datetime` 为一个时间元组,格式如下:
```
(year, month, day, wday, hour, minute, second, yday)
```
参数介绍如下所示:
- **year**:年份;
- **month**:月份,范围 [1, 12]
- **day**:日期,范围 [1, 31]
- **wday**:星期,范围 [0, 6]0 表示星期一,以此类推;
- **hour**:小时,范围 [0, 23]
- **minute**:分钟,范围[0, 59]
- **second**:秒,范围[0, 59]
- **yday**:从当前年份 1 月 1 日开始的天数,范围 [0, 365],一般置位 0 未实现。
使用的方式可参考 [示例](#_3)。
#### **RTC.deinit**()
重置 RTC 设备时间到 2015 年 1 月 1日重新运行 RTC 设备。
#### **RTC.now**()
获取当前时间,返回值为上述 `datetime` 时间元组格式。
### 示例
```python
>>> from machine import RTC
>>> rtc = RTC() # 创建 RTC 设备对象
>>> rtc.init((2019,6,5,2,10,22,30,0)) # 设置初始化时间
>>> rtc.now() # 获取当前时间
(2019, 6, 5, 2, 10, 22, 40, 0)
>>> rtc.deinit() # 重置时间到2015年1月1日
>>> rtc.now() # 获取当前时间
(2015, 1, 1, 3, 0, 0, 1, 0)
```

View File

@@ -0,0 +1,96 @@
## machine.SPI
**machine.SPI** 类是 machine 模块下面的一个硬件类,用于对 SPI 的配置和控制,提供对 SPI 设备的操作方法。
- `SPI` 是一个由主机驱动的同步串行协议。在物理层,总线有三根:`SCK``MOSI``MISO`。多个设备可以共享同一总线,每个设备都由一个单独的信号 `SS` 来选中,也称片选信号。
- 主机通过片选信号选定一个设备进行通信。`SS` 信号的管理应该由用户代码负责。(通过 [machine.Pin](Pin.md)
### 构造函数
在 RT-Thread MicroPython 中 `SPI` 对象的构造函数如下:
#### **class machine.SPI**(id, ...)
在给定总线上构造一个 `SPI` 对象,`id` 取决于特定的移植。
如果想要使用软件 `SPI` , 即使用引脚模拟 `SPI` 总线,那么初始化的第一个参数需要设置为 `-1` ,可参考 [软件 SPI 示例](#spi) 。
使用硬件 `SPI` 在初始化时只需传入 `SPI` 设备的编号即可,如 '50' 表示 `SPI5` 总线上的第 0 个设备。初始化方式可参考 [硬件 SPI 示例](#spi_1)。
如果没有额外的参数,`SPI` 对象会被创建,但是不会被初始化,如果给出额外的参数,那么总线将被初始化,初始化参数可以参考下面的 `SPI.init` 方法。
### 方法
#### **SPI.init**(baudrate=1000000, \*, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None)
用给定的参数初始化`SPI`总线:
- **baudrate** `SCK` 时钟频率。
- **polarity** :极性可以是 `0``1`,是时钟空闲时所处的电平。
- **phase** :相位可以是 `0``1`,分别在第一个或者第二个时钟边缘采集数据。
- **bits** :每次传输的数据长度,一般是 8 位。
- **firstbit** :传输数据从高位开始还是从低位开始,可以是 `SPI.MSB` 或者 `SPI.LSB`
- **sck** :用于 `sck``machine.Pin` 对象。
- **mosi** :用于 `mosi``machine.Pin` 对象。
- **miso** :用于`miso``machine.Pin` 对象。
#### **SPI.deinit**()
关闭 `SPI` 总线。
#### **SPI.read**(nbytes, write=0x00)
读出 n 字节的同时不断的写入 `write` 给定的单字节。返回一个存放着读出数据的字节对象。
#### **SPI.readinto**(buf, write=0x00)
读出 n 字节到 `buf` 的同时不断地写入 `write` 给定的单字节。
这个方法返回读入的字节数。
#### **SPI.write**(buf)
写入 `buf` 中包含的字节。返回`None`
#### **SPI.write_readinto**(write_buf, read_buf)
在读出数据到 `readbuf` 时,从 `writebuf` 中写入数据。缓冲区可以是相同的或不同,但是两个缓冲区必须具有相同的长度。返回 `None`
### 常量
#### **SPI.MSB**
设置从高位开始传输数据。
#### **SPI.LSB**
设置从低位开始传输数据。
### 示例
#### 软件模拟 SPI
```
>>> from machine import Pin, SPI
>>> clk = Pin(("clk", 26), Pin.OUT_PP)
>>> mosi = Pin(("mosi", 27), Pin.OUT_PP)
>>> miso = Pin(("miso", 28), Pin.IN)
>>> spi = SPI(-1, 500000, polarity = 0, phase = 0, bits = 8, firstbit = 0, sck = clk, mosi = mosi, miso = miso)
>>> print(spi)
SoftSPI(baudrate=500000, polarity=0, phase=0, sck=clk, mosi=mosi, miso=miso)
>>> spi.write("hello rt-thread!")
>>> spi.read(10)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
```
#### 硬件 SPI
需要先开启 `SPI` 设备驱动,查找设备可以在 `msh` 中输入`list_device` 命令。
在构造函数的第一个参数传入 `50`,系统就会搜索名为 `spi50` 的设备,找到之后使用这个设备来构建 `SPI` 对象:
```
>>> from machine import SPI
>>> spi = SPI(50)
>>> print(spi)
SPI(device port : spi50)
>>> spi.write(b'\x9f')
>>> spi.read(5)
b'\xff\xff\xff\xff\xff'
>>> buf = bytearray(1)
>>> spi.write_readinto(b"\x9f",buf)
>>> buf
bytearray(b'\xef')
>>> spi.init(100000,0,0,8,1) # Resetting SPI parameter
```
更多内容可参考 [machine.SPI](http://docs.micropython.org/en/latest/pyboard/library/machine.SPI.html) 。

View File

@@ -0,0 +1,73 @@
## machine.Timer
**machine.Timer** 类是 machine 模块下的一个硬件类,用于 Timer 设备的配置和控制,提供对 Timer 设备的操作方法。
- Timer硬件定时器是一种用于处理周期性和定时性事件的设备。
- Timer 硬件定时器主要通过内部计数器模块对脉冲信号进行计数,实现周期性设备控制的功能。
- Timer 硬件定时器可以自定义**超时时间**和**超时回调函数**,并且提供两种**定时器模式**
- `ONE_SHOT`:定时器只执行一次设置的回调函数;
- `PERIOD`:定时器会周期性执行设置的回调函数;
- 打印 Timer 对象会打印出配置的信息。
### 构造函数
在 RT-Thread MicroPython 中 `Timer` 对象的构造函数如下:
#### **class machine.Timer**(id)
- **id**:使用的 Timer 设备编号,`id = 1` 表示编号为 1 的 Timer 设备,或者表示使用的 timer 设备名,如 `id = "timer"` 表示设备名为 `timer` 的 Timer 设备;
该函数主要用于通过设备编号创建 Timer 设备对象。
### 方法
#### **Timer.init**(mode = Timer.PERIODIC, period = 0, callback = None)
- **mode**:设置 Timer 定时器模式,可以设置两种模式:`ONE_SHOT`(执行一次)、`PERIOD`(周期性执行),默认设置的模式为 `PERIOD` 模式;
- **period**:设置 Timer 定时器定时周期单位毫秒ms
- **callback**:设置 Timer 定义器超时回调函数,默认设置的函数为 None 空函数,设置的函数格式如下所示:
```python
def callback_test(device): # 回调函数有且只有一个入参,为创建的 Timer 对象
print("Timer callback test")
print(device) # 打印 Timer 对象配置信息
```
该函数使用方式如下示例所示:
```python
timer.init(wdt.PERIOD, 5000, callback_test) # 设置定时器模式为周期性执行,超时时间为 5 秒, 超时函数为 callback_test
```
#### **Timer.deinit**()
该函数用于停止并关闭 Timer 设备。
### 常量
下面的常量用来配置 `Timer` 对象。
#### 选择定时器模式:
##### **Timer.PERIODIC**
##### **Timer.ONE_SHOT**
### 示例
```python
>>> from machine import Timer # 从 machine 导入 Timer 类
>>> timer = Timer(15) # 创建 Timer 对象,当前设备编号为 11
>>> # 进入粘贴模式
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== def callback_test(device): # 定义超时回调函数
=== print("Timer callback test")
>>> timer.init(timer.PERIODIC, 5000, callback_test) # 初始化 Timer 对象,设置定时器模式为循环执行,超时时间为 5 秒,超时回调函数 callback_test
>>> Timer callback test # 5 秒超时循环执行回调函数,打印日志
>>> Timer callback test
>>> Timer callback test
>>> timer.init(timer.ONE_SHOT, 5000, callback_test) # 设置定时器模式为只执行一次,超时时间为 5 秒,超时回调函数为 callback_test
>>> Timer callback test # 5 秒超时后执行一次回调函数,打印日志
>>> timer.deinit() # 停止并关闭 Timer 定时器
```
更多内容可参考 [machine.Timer](http://docs.micropython.org/en/latest/library/machine.Timer.html)。

View File

@@ -0,0 +1,60 @@
## machine.UART
**machine.UART** 类是 machine 模块下面的一个硬件类,用于对 UART 的配置和控制,提供对 UART 设备的操作方法。
`UART` 实现了标准的 `uart/usart` 双工串行通信协议,在物理层上,他由两根数据线组成:`RX``TX`。通信单元是一个字符,它可以是 8 或 9 位宽。
### 构造函数
在 RT-Thread MicroPython 中 `UART` 对象的构造函数如下:
#### **class machine.UART**(id, ...)
在给定总线上构造一个 `UART` 对象,`id` 取决于特定的移植。
初始化参数可以参考下面的 `UART.init` 方法。
使用硬件 UART 在初始化时只需传入 `UART` 设备的编号即可,如传入 `1` 表示 `uart1` 设备。
初始化方式可参考 [示例](#_3)。
### 方法
#### **UART.init**(baudrate = 9600, bits=8, parity=None, stop=1)
- **baudrate** `SCK` 时钟频率。
- **bits** :每次发送数据的长度。
- **parity** :校验方式。
- **stop** :停止位的长度。
#### **UART.deinit**()
关闭串口总线。
#### **UART.read**([nbytes])
读取字符,如果指定读 n 个字节,那么最多读取 n 个字节,否则就会读取尽可能多的数据。
返回值:一个包含读入数据的字节对象。如果如果超时则返回 `None`
#### **UART.readinto**(buf[, nbytes])
读取字符到 `buf` 中,如果指定读 n 个字节,那么最多读取 n 个字节,否则就读取尽可能多的数据。另外读取数据的长度不超过 `buf` 的长度。
返回值:读取和存储到 `buf` 中的字节数。如果超时则返回 `None`
#### **UART.readline**()
读一行数据,以换行符结尾。
返回值:读入的行数,如果超时则返回 `None`
#### **UART.write**(buf)
`buf` 中的数据写入总线。
返回值:写入的字节数,如果超时则返回 `None`
### 示例
在构造函数的第一个参数传入`1`,系统就会搜索名为 `uart1` 的设备,找到之后使用这个设备来构建 `UART` 对象:
```python
from machine import UART
uart = UART(1, 115200) # init with given baudrate
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
uart.read(10) # read 10 characters, returns a bytes object
uart.read() # read all available characters
uart.readline() # read a line
uart.readinto(buf) # read and store into the given buffer
uart.write('abc') # write the 3 characters
```
更多内容可参考 [machine.UART](http://docs.micropython.org/en/latest/pyboard/library/machine.UART.html) 。

View File

@@ -0,0 +1,42 @@
## machine.WDT
**machine.WDT** 类是 machine 模块下的一个硬件类,用于 WDT 设备的配置和控制,提供对 WDT 设备的操作方法。
如下为 WDT 设备基本介绍:
- WDTWatchDog Timer硬件看门狗是一个定时器设备用于系统程序结束或出错导致系统进入不可恢复状态时重启系统。
- WDT 启动之后,计数器开始计数,在计数器溢出前没有被复位,会对 CPU 产生一个复位信号使设备重启(简称 “被狗咬”);
- 系统正常运行时,需要在 WDT 设备允许的时间间隔内对看门狗计数清零简称“喂狗”WDT 设备一旦启动,需要定时“喂狗”以确保设备正常运行。
### 构造函数
在 RT-Thread MicroPython 中 `WDT` 对象的构造函数如下:
#### **class machine.WDT**(id = "wdt", timeout=5)
- **id**: 使用的 WDT 设备编号,`id = 1` 表示编号为 1 的 WDT 设备,或者表示使用的 WDT 设备名,如 `id = "wdt"` 表示设备名为 `wdt` 的 WDT 设备;
- **timeout**设置看门狗超时时间单位s
用于创建一个 WDT 对象并且启动 WDT 功能。一旦启动设置的超时时间无法改动WDT 功能无法停止。
如果该函数入参为空,则设置超时时间为 5 秒;如果入参非空,则使用该入参设置 WDT 超时时间,超时时间最小设置为 1 秒。
### 方法
#### **WDT.feed**()
用于执行“喂狗”操作,清空看门狗设备计数。应用程序应该合理的周期性调用该函数,以防止系统重启。
### 示例
``` python
>>> from machine import WDT # 从 machine 导入 WDT 类
>>> wdt = WDT() # 创建 WDT 对象,默认超时时间为 5 秒
>>> wdt = WDT(10) # 创建 WDT 对象,设置超时时间为 10 秒
>>> wdt.feed() # 在 10 秒超时时间内需要执行“喂狗”操作,清空看门狗设备计数,否则系统将重启
```
更多内容可参考 [machine.WDT](http://docs.micropython.org/en/latest/library/machine.WDT.html) 。

View File

@@ -0,0 +1,72 @@
# micropython 内部功能访问与控制模块
## Functions
### micropython.const(expr)
Used to declare that the expression is a constant so that the compile can optimise it. The use of this function should be as follows:
```python
from micropython import const
CONST_X = const(123)
CONST_Y = const(2 * CONST_X + 1)
```
Constants declared this way are still accessible as global variables from outside the module they are declared in. On the other hand, if a constant begins with an underscore then it is hidden, it is not available as a global variable, and does not take up any memory during execution.
This const function is recognised directly by the MicroPython parser and is provided as part of the micropython module mainly so that scripts can be written which run under both CPython and MicroPython, by following the above pattern.
### micropython.opt_level([level])
If level is given then this function sets the optimisation level for subsequent compilation of scripts, and returns None. Otherwise it returns the current optimisation level.
- The optimisation level controls the following compilation features:
- Assertions: at level 0 assertion statements are enabled and compiled into the bytecode; at levels 1 and higher assertions are not compiled.
- Built-in __debug__ variable: at level 0 this variable expands to True; at levels 1 and higher it expands to False.
Source-code line numbers: at levels 0, 1 and 2 source-code line number are stored along with the bytecode so that exceptions can report the line number they occurred at; at levels 3 and higher line numbers are not stored.
- The default optimisation level is usually level 0.
### micropython.alloc_emergency_exception_buf(size)
Allocate size bytes of RAM for the emergency exception buffer (a good size is around 100 bytes). The buffer is used to create exceptions in cases when normal RAM allocation would fail (eg within an interrupt handler) and therefore give useful traceback information in these situations.
A good way to use this function is to put it at the start of your main script (eg boot.py or main.py) and then the emergency exception buffer will be active for all the code following it.
### micropython.mem_info([verbose])
Print information about currently used memory. If the verbose argument is given then extra information is printed.
The information that is printed is implementation dependent, but currently includes the amount of stack and heap used. In verbose mode it prints out the entire heap indicating which blocks are used and which are free.
### micropython.qstr_info([verbose])
Print information about currently interned strings. If the verbose argument is given then extra information is printed.
The information that is printed is implementation dependent, but currently includes the number of interned strings and the amount of RAM they use. In verbose mode it prints out the names of all RAM-interned strings.
### micropython.stack_use()
Return an integer representing the current amount of stack that is being used. The absolute value of this is not particularly useful, rather it should be used to compute differences in stack usage at different points.
### micropython.heap_lock()
### micropython.heap_unlock()
Lock or unlock the heap. When locked no memory allocation can occur and a MemoryError will be raised if any heap allocation is attempted.
These functions can be nested, ie heap_lock() can be called multiple times in a row and the lock-depth will increase, and then heap_unlock() must be called the same number of times to make the heap available again.
If the REPL becomes active with the heap locked then it will be forcefully unlocked.
### micropython.kbd_intr(chr)
Set the character that will raise a KeyboardInterrupt exception. By default this is set to 3 during script execution, corresponding to Ctrl-C. Passing -1 to this function will disable capture of Ctrl-C, and passing 3 will restore it.
This function can be used to prevent the capturing of Ctrl-C on the incoming stream of characters that is usually used for the REPL, in case that stream is used for other purposes.
### micropython.schedule(func, arg)
Schedule the function func to be executed “very soon”. The function is passed the value arg as its single argument. “Very soon” means that the MicroPython runtime will do its best to execute the function at the earliest possible time, given that it is also trying to be efficient, and that the following conditions hold:
- A scheduled function will never preempt another scheduled function.
- Scheduled functions are always executed “between opcodes” which means that all fundamental Python operations (such as appending to a list) are guaranteed to be atomic.
- A given port may define “critical regions” within which scheduled functions will never be executed. Functions may be scheduled within a critical region but they will not be executed until that region is exited. An example of a critical region is a preempting interrupt handler (an IRQ).
A use for this function is to schedule a callback from a preempting IRQ. Such an IRQ puts restrictions on the code that runs in the IRQ (for example the heap may be locked) and scheduling a function to call later will lift those restrictions.
Note: If schedule() is called from a preempting IRQ, when memory allocation is not allowed and the callback to be passed to schedule() is a bound method, passing this directly will fail. This is because creating a reference to a bound method causes memory allocation. A solution is to create a reference to the method in the class constructor and to pass that reference to schedule(). This is discussed in detail here reference documentation under “Creation of Python objects”.
There is a finite queue to hold the scheduled functions and schedule() will raise a RuntimeError if the queue is full.

View File

@@ -0,0 +1,10 @@
## network 网络配置
此模块提供网络驱动程序和路由配置。特定硬件的网络驱动程序在此模块中可用,用于配置硬件网络接口。然后,配置接口提供的网络服务可以通过 `usocket` 模块使用。
### 专用的网络类配置
下面具体的类实现了抽象网卡的接口,并提供了一种控制各种网络接口的方法。
- [class WLAN control built-in WiFi interfaces](network/wlan.md)

View File

@@ -0,0 +1,109 @@
## class WLAN 控制内置的 WiFi 网络接口
该类为 WiFi 网络处理器提供一个驱动程序。使用示例:
```python
import network
# enable station interface and connect to WiFi access point
nic = network.WLAN(network.STA_IF)
nic.active(True)
nic.connect('your-ssid', 'your-password')
# now use sockets as usual
```
### 构造函数
在 RT-Thread MicroPython 中 `WLAN` 对象的构造函数如下:
#### class network.WLAN(interface_id)
创建一个 WLAN 网络接口对象。支持的接口是 ` network.STA_IF`STA 模式,可以连接到上游的 WiFi 热点上) 和 `network.AP_IF`AP 模式,允许其他 WiFi 客户端连接到自身的热点)。下面方法的可用性取决于接口的类型。例如只有STA 接口可以使用 `WLAN.connect()` 方法连接到 AP 热点上。
### 方法
#### **WLAN.active**([is_active])
如果向该方法传入布尔数值,传入 True 则使能卡,传入 False 则禁止网卡。否则,如果不传入参数,则查询当前网卡的状态。
#### **WLAN.connect**(ssid, password)
使用指定的账号和密码链接指定的无线热点。
#### **WLAN.disconnect**()
从当前链接的无线网络中断开。
#### **WLAN.scan**()
扫描当前可以连接的无线网络。
只能在 STA 模式下进行扫描,使用元组列表的形式返回 WiFi 接入点的相关信息。
ssid, bssid, channel, rssi, authmode, hidden
#### **WLAN.status**([param])
返回当前无线连接的状态。
当调用该方法时没有附带参数,就会返回值描述当前网络连接的状态。如果还没有从热点连接中获得 IP 地址,此时的状态为 `STATION_IDLE`。如果已经从连接的无线网络中获得 IP 地址,此时的状态为 `STAT_GOT_IP`
当调用该函数使用的参数为 `rssi` 时,则返回 `rssi` 的值,该函数目前只支持这一个参数。
#### **WLAN.isconnected**()
在 STA 模式时,如果已经连接到 WiFi 网络,并且获得了 IP 地址,则返回 True。如果处在 AP 模式,此时已经与客户端建立连接,则返回 True。其他情况下都返回 False。
#### WLAN.ifconfig([(ip, subnet, gateway, dns)])
获取或者设置网络接口的参数IP 地址子网掩码网关DNS 服务器。当调用该方法不附带参数时,该方法会返回一个包含四个元素的元组来描述上面的信息。想要设置上面的值,传入一个包含上述四个元素的元组,例如:
```python
nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
```
#### **WLAN.config**('param')
#### WLAN.config(param=value, ...)
获取或者设置一般网络接口参数,这些方法允许处理标准的 ip 配置之外的其他参数,如 `WLAN. ifconfig()` 函数处理的参数。这些参数包括特定网络和特定硬件的参数。对于参数的设置,应该使用关键字的语法,可以一次性设置多个参数。
当查询参数时,参数名称的引用应该为字符串,且每次只能查询一个参数。
```python
# Set WiFi access point name (formally known as ESSID) and WiFi password
ap.config(essid='My_AP', password="88888888")
# Query params one by one
print(ap.config('essid'))
print(ap.config('channel'))
```
下面是目前支持的参数:
| Parameter | Description |
| --------- | --------------------------------- |
| mac | MAC address (bytes) |
| essid | WiFi access point name (string) |
| channel | WiFi channel (integer) |
| hidden | Whether ESSID is hidden (boolean) |
| password | Access password (string) |
### 示例
STA 模式下:
```python
import network
wlan = network.WLAN(network.STA_IF)
wlan.scan()
wlan.connect("rtthread","02188888888")
wlan.isconnected()
```
AP 模式下:
```python
import network
ap = network.WLAN(network.AP_IF)
ap.config(essid="hello_rt-thread", password="88888888")
ap.active(True)
ap.config("essid")
```

View File

@@ -0,0 +1,23 @@
## **rtthread** 系统相关函数
**rtthread** 模块提供了与 RT-Thread 操作系统相关的功能,如查看栈使用情况等。
### 函数
#### rtthread.current_tid()
返回当前线程的 id 。
#### rtthread.is_preempt_thread()
返回是否是可抢占线程。
### 示例
```
>>> import rtthread
>>>
>>> rtthread.is_preempt_thread() # determine if it's a preemptible thread
True
>>> rtthread.current_tid() # current thread id
268464956
>>>
```