Skip to content

Commit 56af2e2

Browse files
committed
update peripheral docs for MaixCAM2
1 parent 115645e commit 56af2e2

File tree

24 files changed

+1139
-359
lines changed

24 files changed

+1139
-359
lines changed
143 KB
Loading

docs/doc/en/peripheral/adc.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ update:
77
content: Initial document
88
---
99

10+
11+
## Supported Devices
12+
13+
| Device | Supported |
14+
| -------- | --------- |
15+
| MaixCAM2 ||
16+
| MaixCAM / MaixCAM-Pro ||
17+
18+
1019
## ADC Introduction
1120

1221

docs/doc/en/peripheral/gpio.md

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,97 @@
1-
# MaixCAM MaixPy Using GPIO
1+
---
2+
title: MaixCAM MaixPy Using GPIO
3+
---
24

35
## Introduction
46

5-
Using GPIO allows you to control pins for input or output high and low levels, which is commonly used to read signals or output control signals.
7+
Using GPIOs allows you to control a pin to output a high or low voltage level or to read a signal from it. This is a very common way to read signals or output control signals.
68

7-
**Note:** The pins on the `MaixCAM` are tolerant to `3.3V`. Do not input `5V` voltage.
9+
**Note:** The pins on the `MaixCAM` are `3.3V` tolerant. **Do not** apply `5V` to them.
810

9-
## Using GPIO in MaixPy
11+
## Prerequisites
1012

11-
> MaixPy Firmware should > 4.1.2(not include)
13+
Please learn how to use the [pinmap](https://www.google.com/search?q=./pinmap.md) module to set pin functions first.
1214

13-
First, we need to know which pins and GPIOs the device has. For MaixCAM, each pin corresponds to a GPIO controller, as shown in the figure:
15+
To enable a pin for `GPIO` functionality, first use `pinmap` to set the function of the corresponding pin to `GPIO`.
1416

15-
![](https://wiki.sipeed.com/hardware/zh/lichee/assets/RV_Nano/intro/RV_Nano_3.jpg)
16-
![maixcam_pro_io](/static/image/maixcam_pro_io.png)
17+
## Choosing the Right GPIO to Use
1718

18-
It is important to note that pins can be used not only as GPIOs but also for other functions like PWM. Before using them, we need to set the pin function to GPIO.
19+
First, you need to know which pins on your device are available as GPIOs, as shown in the figure:
1920

20-
For example, on MaixCAM, **some pins are already occupied by other functions by default, such as UART0 and WiFi (SDIO1 + A26), so it is not recommended to use them.**
21+
| Device Model | Pinout Diagram | Pin Multiplexing Description |
22+
| ------- | ------- | --- |
23+
| MaixCAM | | The silkscreen on the board, e.g., `A19`, is the pin name, and `GPIOA19/PWM7` is the function name |
24+
| MaixCAM-Pro | | The first name, e.g., `A19`, is the pin name, and `GPIOA19/PWM7` is the corresponding function name |
25+
| MaixCAM2 | | The first name, e.g., `IO0_A2`, is the pin name, and `GPIO0_A2/SPI1_CS0` is the corresponding function name |
2126

22-
Other pins can be used, and the A14 pin is connected to the onboard LED, which is used as a system load indicator by default. If initialized, it will automatically disable the system indicator function and can be used as a regular GPIO (note that `A14` can only be used as an output). This way, you can control the LED's on and off state.
27+
Note that pins may have other default uses. It's best to avoid these pins. Please refer to the [pinmap](https://www.google.com/search?q=./pinmap.md) documentation for details.
2328

24-
Here is the English translation of your text:
29+
## Circuit Considerations
2530

26-
The circuit diagram of the LED is shown in the figure. Therefore, we only need to provide a high signal to pin A14, and the LED will conduct and light up:
27-
![](../../assets/gpio_led.png)
31+
Be aware that the voltage tolerance and load capacity of the pins are limited. You need to be careful when designing circuits to avoid basic mistakes, like asking "why can't the pin directly power a motor?"
32+
33+
* **Pin Voltage Tolerance**: Unless otherwise specified, the pins operate at `3.3v`. Do not connect an external `5v` voltage.
34+
* **Pin Input/Output Current**: The input/output current of the chip pins is limited. They are generally only used for control signals. For devices with high current requirements, please use a conversion circuit.
35+
36+
**Example**: To control an LED, a simple circuit is as follows:
37+
The `LED` is directly powered by a high-level output from the pin. This is the most intuitive way to use it, but you must be very careful. The maximum output and input current of a chip pin (the driving capability) is limited and is usually described in the chip's datasheet.
38+
Here, the current is `3.3v/(LED+resistor resistance)` \< `0.64mA`, so it can be driven directly. However, if your circuit's current is too high, it may fail to drive the device or even cause the chip to malfunction.
39+
40+
The **correct approach** is to connect an external conversion circuit so the pin only acts as a control signal. This can be done using a transistor, optocoupler, or relay. This document won't go into details; please do your own research.
41+
42+
## GPIO Output Mode
43+
44+
As shown in the LED circuit diagram, we only need to provide a high voltage level to the `A14` (`MaixCAM2` is `IO0_A6`) pin for the LED to turn on:
2845

2946
```python
30-
from maix import gpio, pinmap, time
47+
from maix import gpio, pinmap, time, sys, err
48+
49+
pin_name = "IO0_A6" if sys.device_id() == "maixcam2" else "A14"
50+
gpio_name = "GPIO0_A6" if sys.device_id() == "maixcam2" else "GPIOA14"
3151

32-
pinmap.set_pin_function("A14", "GPIOA14")
33-
led = gpio.GPIO("GPIOA14", gpio.Mode.OUT)
52+
err.check_raise(pinmap.set_pin_function(pin_name, gpio_name), "set pin failed")
53+
led = gpio.GPIO(gpio_name, gpio.Mode.OUT)
3454
led.value(0)
3555

3656
while 1:
3757
led.toggle()
3858
time.sleep_ms(500)
3959
```
4060

41-
Here, we first use `pinmap` to set the function of the `A14` pin to `GPIO`. Of course, for `A14`, since it only has the `GPIO` function, it can be omitted. For the sake of generality, other pins may need to be set, so it is set in this example.
61+
* First, get the pin and function name based on the board model.
62+
* Use `pinmap` to set the pin's function to `GPIO`.
63+
* `err.check_raise` is used to check the return value of `set_pin_function`. If there's an error, it raises an exception to prevent mistakes.
64+
* Initialize the `GPIO` object and set it to output mode.
65+
* The output value is toggled every `0.5s`, causing the LED to blink.
4266

43-
For more APIs, please refer to the [GPIO API Documentation](https://wiki.sipeed.com/maixpy/api/maix/peripheral/gpio.html)
67+
For more API information, please see the [GPIO API documentation](https://wiki.sipeed.com/maixpy/api/maix/peripheral/gpio.html).
4468

45-
## GPIO in Input Mode
69+
## GPIO Input Mode
4670

4771
```python
48-
from maix import gpio, pinmap, time
72+
from maix import gpio, pinmap, time, err
4973

50-
pinmap.set_pin_function("A19", "GPIOA19")
74+
err.check_raise(pinmap.set_pin_function("A19", "GPIOA19"), "set pin failed")
5175
led = gpio.GPIO("GPIOA19", gpio.Mode.IN)
5276

5377
while 1:
5478
print(led.value())
5579
time.sleep_ms(1) # sleep to make cpu free
5680
```
5781

58-
Here is the English translation of the text:
82+
## Using the Illumination LED on MaixCAM-Pro
5983

60-
## MaixCAM-Pro Uses Illumination LED
61-
62-
Both MaixCAM and MaixCAM-Pro have a small LED light connected to pin `A14`. Additionally, the MaixCAM-Pro has an onboard illumination LED connected to pin `B3`, which is turned on by a high signal and off by a low signal:
84+
Both `MaixCAM / MaixCAM-Pro` and `MaixCAM2` have a small LED connected to pins `A14` and `IO0_A6`.
85+
Additionally, the `MaixCAM-Pro` and `MaixCAM2` also have an onboard **illumination LED** connected to pins `B3` and `IO0_A25`, respectively, which turns on with a high voltage and off with a low voltage:
6386

6487
```python
65-
from maix import gpio, pinmap, time
88+
from maix import gpio, pinmap, time, sys, err
89+
90+
pin_name = "IO1_A25" if sys.device_id() == "maixcam2" else "B3"
91+
gpio_name = "GPIO1_A25" if sys.device_id() == "maixcam2" else "GPIOB3"
6692

67-
pinmap.set_pin_function("B3", "GPIOB3")
68-
led = gpio.GPIO("GPIOB3", gpio.Mode.OUT)
93+
err.check_raise(pinmap.set_pin_function(pin_name, gpio_name), "set pin failed")
94+
led = gpio.GPIO(gpio_name, gpio.Mode.OUT)
6995
led.value(0)
7096

7197
while 1:
@@ -74,4 +100,11 @@ while 1:
74100

75101
```
76102

103+
## More Examples
104+
105+
See [MaixPy examples](https://github.com/sipeed/MaixPy/tree/main/examples/peripheral/gpio).
106+
107+
## API Documentation
108+
109+
For more API information, see the [GPIO API documentation](https://wiki.sipeed.com/maixpy/api/maix/peripheral/gpio.html).
77110

docs/doc/en/peripheral/i2c.md

Lines changed: 102 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,122 @@
11
---
2-
title: Using I2C with MaixCAM MaixPy
2+
title: MaixCAM MaixPy Using I2C
3+
update:
4+
- date: 2025-08-08
5+
author: Neucrack
6+
version: 1.1.0
7+
content: Refactored the documentation to be more understandable for beginners
38
---
49

5-
> Note: Requires MaixPy image and firmware >= 4.2.1
10+
## Prerequisites
611

7-
The `I2C` and corresponding pins of `MaixCAM` can be seen in the diagram:
12+
Please learn how to use the [pinmap](https://www.google.com/search?q=./pinmap.md) module to set pin functions first.
813

9-
![](https://wiki.sipeed.com/hardware/zh/lichee/assets/RV_Nano/intro/RV_Nano_3.jpg)
10-
![maixcam_pro_io](/static/image/maixcam_pro_io.png)
14+
To enable an pin for `I2C` functionality, first use `pinmap` to set the function of the corresponding pin to `I2C`.
1115

12-
For MaixCAM, due to limited pin resources, the pins for `I2C1` and `I2C3` overlap with those of the WiFi module (SDIO1). Therefore, you can only use either WiFi or hardware I2C, but not both. Additionally, there is an `I2C5`, which is simulated by software at the lower driver level. It is recommended to use this one, as the drivers are already set up, and its use is the same as using hardware `I2C`.
16+
## I2C Introduction
1317

14-
By default, the pins for `I2C5` are configured as `GPIO`. Therefore, before using the `i2c` module, you should first use the `pinmap` module to set the pin functions to `I2C5` as follows:
18+
`I2C` uses only two pins, `SCL` and `SDA`, to achieve bus communication. This allows one master to connect to multiple slaves, enabling convenient one-to-many communication.
19+
It is commonly used for:
20+
21+
* Reading sensor data, such as temperature and humidity, IMU, and touchscreens.
22+
* Controlling devices, such as setting camera parameters.
23+
* Communication between two devices.
24+
25+
There are many good tutorials on the basics of I2C online. This article will not go into detail; please search and learn on your own.
26+
27+
## Choosing the Right I2C to Use
28+
29+
First, we need to know which pins and I2C interfaces the device has, as shown in the figure:
30+
31+
| Device Model | Pinout Diagram | Pin Multiplexing Description |
32+
| ------- | ------- | --- |
33+
| MaixCAM | | The silkscreen on the board, e.g., `A15`, is the pin name, and `I2C5_SCL` is the function name |
34+
| MaixCAM-Pro | | The first name, e.g., `A15`, is the pin name, and `I2C5_SCL` is the corresponding function name |
35+
| MaixCAM2 | | The first name, e.g., `IO0_A1`, is the pin name, and `I2C6_SCL` is the corresponding function name |
36+
37+
It's important to note that pins might have other default uses. It's best to avoid these pins. Please refer to the [pinmap](https://www.google.com/search?q=./pinmap.md) documentation for details.
38+
39+
For example:
40+
* For `MaixCAM / MaixCAM-Pro`, the `I2C1` and `I2C3` pins overlap with the WiFi module (SDIO1), so their use is not recommended. There is also an `I2C5`, which is a software-simulated driver at the low level. It is recommended to use it as the underlying driver has been configured, and it works the same as a hardware `I2C`.
41+
* The `I2C6 / I2C7` pins on `MaixCAM2` are idle and can be used once `pinmap` is configured.
42+
43+
## Using I2C in MaixPy
44+
45+
First, use `pinmap` to set the pin function to `I2C`, then initialize the `I2C` object:
1546

1647
```python
17-
from maix import i2c, pinmap
48+
from maix import i2c, pinmap, sys, err
49+
1850

19-
pinmap.set_pin_function("A15", "I2C5_SCL")
20-
pinmap.set_pin_function("A27", "I2C5_SDA")
51+
# get pin and i2c number according to device id
52+
device_id = sys.device_id()
53+
if device_id == "maixcam2":
54+
scl_pin_name = "IO0_A1"
55+
scl_i2c_name = "I2C6_SCL"
56+
sda_pin_name = "IO0_A0"
57+
sda_i2c_name = "I2C6_SDA"
58+
i2c_id = 6
59+
else:
60+
scl_pin_name = "A15"
61+
scl_i2c_name = "I2C5_SCL"
62+
sda_pin_name = "A27"
63+
sda_i2c_name = "I2C5_SDA"
64+
i2c_id = 5
2165

22-
bus1 = i2c.I2C(5, i2c.Mode.MASTER)
23-
slaves = bus1.scan()
24-
print("find slaves:", slaves)
66+
# set pinmap
67+
err.check_raise(pinmap.set_pin_function(scl_pin_name, scl_i2c_name), "set pin failed")
68+
err.check_raise(pinmap.set_pin_function(sda_pin_name, sda_i2c_name), "set pin failed")
69+
70+
bus = i2c.I2C(i2c_id, i2c.Mode.MASTER)
71+
slaves = bus.scan()
72+
print("find slaves:")
73+
for s in slaves:
74+
print(f"{s}[0x{s:02x}]")
2575

2676
```
2777

28-
For more APIs, see [i2c API documentation](https://wiki.sipeed.com/maixpy/api/maix/peripheral/i2c.html).
78+
## More Examples
79+
80+
See [MaixPy examples](https://github.com/sipeed/MaixPy/tree/main/examples/peripheral/i2c).
81+
82+
## API Documentation
83+
84+
For more API information, see [i2c API documentation](https://wiki.sipeed.com/maixpy/api/maix/peripheral/i2c.html).
85+
86+
## Using Other Libraries
87+
88+
Additionally, since it is a standard Linux driver, besides using the APIs provided by MaixPy, you can also use general Linux libraries like `smbus / smbus2`.
89+
90+
Steps to use:
91+
92+
* Run `pip install smbus` in the development board's terminal to install the library.
93+
* Use `pinmap` to map the pin functions.
94+
* Call `smbus` APIs to read from and write to `i2c`.
2995

30-
As mentioned above, for the `MaixCAM`, you must choose between using hardware `I2C` and `WiFi`. If you need to use `I2C`, you must disable `WiFi` and use the `pinmap` module to set the pin functions for `I2C`, then operate using the `maix.i2c` module.
31-
> TODO: Provide a method to disable WiFi (requires disabling the WiFi driver in the system, which is more complex).
3296

3397
```python
34-
from maix import i2c, pinmap
98+
from maix import i2c, pinmap, sys, err
99+
import smbus
35100

36-
pinmap.set_pin_function("P18", "I2C1_SCL")
37-
pinmap.set_pin_function("P21", "I2C1_SDA")
101+
# get pin and i2c number according to device id
102+
device_id = sys.device_id()
103+
if device_id == "maixcam2":
104+
scl_pin_name = "IO0_A1"
105+
scl_i2c_name = "I2C6_SCL"
106+
sda_pin_name = "IO0_A0"
107+
sda_i2c_name = "I2C6_SDA"
108+
i2c_id = 6
109+
else:
110+
scl_pin_name = "A15"
111+
scl_i2c_name = "I2C5_SCL"
112+
sda_pin_name = "A27"
113+
sda_i2c_name = "I2C5_SDA"
114+
i2c_id = 5
38115

39-
bus1 = i2c.I2C(1, i2c.Mode.MASTER)
40-
slaves = bus1.scan()
41-
print("find slaves:", slaves)
116+
# set pinmap
117+
err.check_raise(pinmap.set_pin_function(scl_pin_name, scl_i2c_name), "set pin failed")
118+
err.check_raise(pinmap.set_pin_function(sda_pin_name, sda_i2c_name), "set pin failed")
42119

43-
```
120+
121+
bus = smbus.SMBus(i2c_id)
122+
```

0 commit comments

Comments
 (0)