Skip to content

Commit ce3df82

Browse files
authored
Merge pull request #8118 from n0xa/M5StickCPlus
Adding support for M5Stack Stick C Plus
2 parents 25bacc3 + 75dbb7d commit ce3df82

File tree

5 files changed

+391
-0
lines changed

5 files changed

+391
-0
lines changed
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 n0xa
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "supervisor/board.h"
28+
#include "mpconfigboard.h"
29+
#include "shared-bindings/busio/SPI.h"
30+
#include "shared-bindings/busio/I2C.h"
31+
#include "shared-bindings/displayio/FourWire.h"
32+
#include "shared-module/displayio/__init__.h"
33+
#include "shared-module/displayio/mipi_constants.h"
34+
#include "shared-bindings/board/__init__.h"
35+
#include "shared-bindings/microcontroller/Pin.h"
36+
#include "components/driver/include/driver/gpio.h"
37+
#include "components/hal/include/hal/gpio_hal.h"
38+
#include "common-hal/microcontroller/Pin.h"
39+
40+
#include "../../pmic/axp192/axp192.h"
41+
42+
// display init sequence according to adafruit_st7735r.py library
43+
uint8_t display_init_sequence[] = {
44+
0x01,0x80,0x96, // SWRESET and Delay 150ms
45+
0x11,0x80,0xff, // SLPOUT and Delay
46+
0xb1,0x03,0x01,0x2C,0x2D, // _FRMCTR1
47+
0xb2,0x03,0x01,0x2C,0x2D, // _FRMCTR2
48+
0xb3,0x06,0x01,0x2C,0x2D,0x01,0x2C,0x2D, // _FRMCTR3
49+
0xb4,0x01,0x07, // _INVCTR line inversion
50+
0xc0,0x03,0xa2,0x02,0x84, // _PWCTR1 GVDD = 4.7V, 1.0uA
51+
0xc1,0x01,0xc5, // _PWCTR2 VGH=14.7V, VGL=-7.35V
52+
0xc2,0x02,0x0a,0x00, // _PWCTR3 Opamp current small, Boost frequency
53+
0xc3,0x02,0x8a,0x2a,
54+
0xc4,0x02,0x8a,0xee,
55+
0xc5,0x01,0x0e, // _VMCTR1 VCOMH = 4V, VOML = -1.1V
56+
0x36,0x01,0xc8, // MADCTL Rotate display
57+
0x21,0x00, // _INVON
58+
0x3a,0x01,0x05, // COLMOD - 16bit color
59+
0xe0,0x10,0x02,0x1c,0x07,0x12,0x37,0x32,0x29,0x2d,0x29,0x25,0x2B,0x39,0x00,0x01,0x03,0x10, // _GMCTRP1 Gamma
60+
0xe1,0x10,0x03,0x1d,0x07,0x06,0x2E,0x2C,0x29,0x2D,0x2E,0x2E,0x37,0x3F,0x00,0x00,0x02,0x10, // _GMCTRN1
61+
0x13,0x80,0x0a, // _NORON
62+
0x29,0x80,0x64 // _DISPON
63+
};
64+
65+
static bool pmic_init(busio_i2c_obj_t *i2c) {
66+
int rc;
67+
uint8_t write_buf[2];
68+
69+
if (!pmic_common_init(i2c)) {
70+
return false;
71+
}
72+
73+
// Reg: 30h
74+
// The VBUS-IPSOUT path can be selected to be opened regardless of the status of N_VBUSEN
75+
// VBUS VHOLD pressure limit control disabled
76+
// VBUS current limit control disabled
77+
write_buf[0] = AXP192_VBUS_IPSOUT;
78+
write_buf[1] = AXP192_VBUS_IPSOUT_IGNORE_VBUSEN;
79+
rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf));
80+
if (rc != 0) {
81+
return false;
82+
}
83+
84+
// Reg: 33h
85+
// Charge function enable control bit, including internal and external channels
86+
// Charging target voltage: 4.2V
87+
// Charging end current: End charging when charging current is less than 10% setting
88+
// Internal path charging current: 100mA
89+
write_buf[0] = AXP192_CHARGING_CTRL1;
90+
write_buf[1] = AXP192_CHARGING_CTRL1_ENABLE |
91+
AXP192_CHARGING_CTRL1_VOLTAGE_4_20V |
92+
AXP192_CHARGING_CTRL1_CURRENT_100mA;
93+
rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf));
94+
if (rc != 0) {
95+
return false;
96+
}
97+
98+
// Reg: 90h
99+
// GPIO0(LDOio0) floating
100+
write_buf[0] = AXP192_GPIO0_FUNCTION;
101+
write_buf[1] = AXP192_GPIO0_FUNCTION_FLOATING;
102+
rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf));
103+
if (rc != 0) {
104+
return false;
105+
}
106+
107+
// Reg: 91h
108+
// GPIO0(LDOio0) 2.8V
109+
write_buf[0] = AXP192_GPIO0_LDO_VOLTAGE;
110+
write_buf[1] = AXP192_GPIO0_LDO_VOLTAGE_2_8V;
111+
rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf));
112+
if (rc != 0) {
113+
return false;
114+
}
115+
116+
// Reg: 28h
117+
// LDO2 (TFT backlight): 2.8V
118+
// LDO3 (TFT logic): 3.0V
119+
write_buf[0] = AXP192_LDO23_OUT_VOLTAGE;
120+
write_buf[1] = AXP192_LDO23_OUT_VOLTAGE_LDO2_2_8V |
121+
AXP192_LDO23_OUT_VOLTAGE_LDO3_3_0V;
122+
rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf));
123+
if (rc != 0) {
124+
return false;
125+
}
126+
127+
// Reg: 12h
128+
// Enable CTRL_EXTEN, DCDC1, LDO2 and LDO3
129+
write_buf[0] = AXP192_DCDC13_LDO23_CTRL;
130+
write_buf[1] = AXP192_DCDC13_LDO23_CTRL_EXTEN |
131+
AXP192_DCDC13_LDO23_CTRL_LDO3 |
132+
AXP192_DCDC13_LDO23_CTRL_LDO2 |
133+
AXP192_DCDC13_LDO23_CTRL_DCDC1;
134+
rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf));
135+
if (rc != 0) {
136+
return false;
137+
}
138+
139+
// Reg: 26h
140+
// DCDC1 (ESP32 VDD): 3.350V
141+
write_buf[0] = AXP192_DCDC1_OUT_VOLTAGE;
142+
write_buf[1] = AXP192_DCDC1_OUT_VOLTAGE_3_350V;
143+
rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf));
144+
if (rc != 0) {
145+
return false;
146+
}
147+
148+
if (!pmic_disable_all_irq(i2c)) {
149+
return false;
150+
}
151+
152+
if (!pmic_clear_all_irq(i2c)) {
153+
return false;
154+
}
155+
156+
if (!pmic_enable_power_key_press_irq(i2c)) {
157+
return false;
158+
}
159+
160+
if (!pmic_enable_low_battery_irq(i2c)) {
161+
return false;
162+
}
163+
164+
return true;
165+
}
166+
167+
static bool display_init(void) {
168+
displayio_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus;
169+
busio_spi_obj_t *spi = &bus->inline_bus;
170+
common_hal_busio_spi_construct(spi, &pin_GPIO13, &pin_GPIO15, NULL, false);
171+
common_hal_busio_spi_never_reset(spi);
172+
173+
bus->base.type = &displayio_fourwire_type;
174+
175+
common_hal_displayio_fourwire_construct(
176+
bus,
177+
spi,
178+
&pin_GPIO23, // DC
179+
&pin_GPIO5, // CS
180+
&pin_GPIO18, // RST
181+
10000000, // baudrate
182+
0, // polarity
183+
0 // phase
184+
);
185+
186+
displayio_display_obj_t *display = &allocate_display()->display;
187+
display->base.type = &displayio_display_type;
188+
189+
common_hal_displayio_display_construct(
190+
display,
191+
bus,
192+
135, // width (after rotation)
193+
240, // height (after rotation)
194+
40, // column start
195+
52, // row start
196+
1, // rotation
197+
16, // color depth
198+
false, // grayscale
199+
false, // pixels in a byte share a row. Only valid for depths < 8
200+
1, // bytes per cell. Only valid for depths < 8
201+
false, // reverse_pixels_in_byte. Only valid for depths < 8
202+
true, // reverse_pixels_in_word
203+
MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command
204+
MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command
205+
MIPI_COMMAND_WRITE_MEMORY_START, // write memory command
206+
display_init_sequence,
207+
sizeof(display_init_sequence),
208+
NULL, // backlight pin
209+
NO_BRIGHTNESS_COMMAND,
210+
1.0f, // brightness
211+
false, // single_byte_bounds
212+
false, // data_as_commands
213+
true, // auto_refresh
214+
80, // native_frames_per_second
215+
false, // backlight_on_high
216+
false, // SH1107_addressing
217+
50000 // backlight pwm frequency
218+
);
219+
220+
return true;
221+
}
222+
223+
void board_init(void) {
224+
busio_i2c_obj_t *internal_i2c = common_hal_board_create_i2c(0);
225+
226+
if (!pmic_init(internal_i2c)) {
227+
mp_printf(&mp_plat_print, "could not initialize axp192 pmic\n");
228+
return;
229+
}
230+
231+
if (!display_init()) {
232+
mp_printf(&mp_plat_print, "could not initialize the display");
233+
return;
234+
}
235+
}
236+
237+
bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
238+
// Set IR led gpio high to prevent power drain from the led
239+
if (pin_number == 9) {
240+
gpio_set_direction(pin_number, GPIO_MODE_DEF_OUTPUT);
241+
gpio_set_level(pin_number, true);
242+
return true;
243+
}
244+
return false;
245+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 n0xa
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// Micropython setup
28+
29+
#define MICROPY_HW_BOARD_NAME "M5Stack Stick C Plus"
30+
#define MICROPY_HW_MCU_NAME "ESP32"
31+
32+
#define MICROPY_HW_LED_STATUS (&pin_GPIO10)
33+
#define MICROPY_HW_LED_STATUS_INVERTED (1)
34+
35+
#define CIRCUITPY_BOARD_I2C (2)
36+
#define CIRCUITPY_BOARD_I2C_PIN {{.scl = &pin_GPIO22, .sda = &pin_GPIO21}, \
37+
{.scl = &pin_GPIO33, .sda = &pin_GPIO32}}
38+
39+
// For entering safe mode
40+
#define CIRCUITPY_BOOT_BUTTON (&pin_GPIO37)
41+
42+
// Explanation of how a user got into safe mode
43+
#define BOARD_USER_SAFE_MODE_ACTION translate("You pressed button A at start up.")
44+
45+
// UART pins attached to the USB-serial converter chip
46+
#define CIRCUITPY_CONSOLE_UART_TX (&pin_GPIO1)
47+
#define CIRCUITPY_CONSOLE_UART_RX (&pin_GPIO3)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CIRCUITPY_CREATOR_ID = 0x10151015
2+
CIRCUITPY_CREATION_ID = 0x0032000A
3+
4+
IDF_TARGET = esp32
5+
6+
CIRCUITPY_ESP_FLASH_MODE = qio
7+
CIRCUITPY_ESP_FLASH_FREQ = 80m
8+
CIRCUITPY_ESP_FLASH_SIZE = 4MB
9+
CIRCUITPY_ESPCAMERA = 0
10+
11+
SRC_C += pmic/axp192/axp192.c
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "shared-bindings/board/__init__.h"
2+
#include "shared-module/displayio/__init__.h"
3+
4+
CIRCUITPY_BOARD_BUS_SINGLETON(porta_i2c, i2c, 1)
5+
6+
STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
7+
CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS
8+
9+
// External pins are in silkscreen order, from top to bottom, left side, then right side
10+
11+
{ MP_ROM_QSTR(MP_QSTR_A26), MP_ROM_PTR(&pin_GPIO26) },
12+
{ MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_GPIO26) },
13+
14+
{ MP_ROM_QSTR(MP_QSTR_A36), MP_ROM_PTR(&pin_GPIO36) },
15+
{ MP_ROM_QSTR(MP_QSTR_D36), MP_ROM_PTR(&pin_GPIO36) },
16+
17+
{ MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_GPIO0) },
18+
{ MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_GPIO0) },
19+
20+
{ MP_ROM_QSTR(MP_QSTR_PORTA_SDA), MP_ROM_PTR(&pin_GPIO32) },
21+
{ MP_ROM_QSTR(MP_QSTR_D32), MP_ROM_PTR(&pin_GPIO32) },
22+
23+
{ MP_ROM_QSTR(MP_QSTR_PORTA_SCL), MP_ROM_PTR(&pin_GPIO33) },
24+
{ MP_ROM_QSTR(MP_QSTR_D33), MP_ROM_PTR(&pin_GPIO33) },
25+
26+
{ MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO10) },
27+
{ MP_ROM_QSTR(MP_QSTR_IR_LED), MP_ROM_PTR(&pin_GPIO9) },
28+
29+
// buttons
30+
{ MP_ROM_QSTR(MP_QSTR_BTN_A), MP_ROM_PTR(&pin_GPIO37) },
31+
{ MP_ROM_QSTR(MP_QSTR_BTN_B), MP_ROM_PTR(&pin_GPIO39) },
32+
33+
// internal i2c bus
34+
{ MP_ROM_QSTR(MP_QSTR_SYS_SDA), MP_ROM_PTR(&pin_GPIO21) },
35+
{ MP_ROM_QSTR(MP_QSTR_SYS_SCL), MP_ROM_PTR(&pin_GPIO22) },
36+
37+
// internal devices interrupt
38+
{ MP_ROM_QSTR(MP_QSTR_SYS_INT), MP_ROM_PTR(&pin_GPIO35) },
39+
40+
// pmu AXP192
41+
{ MP_ROM_QSTR(MP_QSTR_PMU_N_VBUSEN), MP_ROM_PTR(&pin_GPIO27) },
42+
43+
// pdm microphone
44+
{ MP_ROM_QSTR(MP_QSTR_PDM_MIC_CLK), MP_ROM_PTR(&pin_GPIO0) },
45+
{ MP_ROM_QSTR(MP_QSTR_PDM_MIC_DATA), MP_ROM_PTR(&pin_GPIO34) },
46+
47+
// buzzer
48+
{ MP_ROM_QSTR(MP_QSTR_SPEAKER), MP_ROM_PTR(&pin_GPIO2) },
49+
50+
// lcd spi bus
51+
{ MP_ROM_QSTR(MP_QSTR_LCD_MOSI), MP_ROM_PTR(&pin_GPIO15) },
52+
{ MP_ROM_QSTR(MP_QSTR_LCD_CLK), MP_ROM_PTR(&pin_GPIO13) },
53+
{ MP_ROM_QSTR(MP_QSTR_LCD_DC), MP_ROM_PTR(&pin_GPIO23) },
54+
{ MP_ROM_QSTR(MP_QSTR_LCD_RST), MP_ROM_PTR(&pin_GPIO18) },
55+
{ MP_ROM_QSTR(MP_QSTR_LCD_CS), MP_ROM_PTR(&pin_GPIO5) },
56+
57+
{ MP_ROM_QSTR(MP_QSTR_SYS_I2C), MP_ROM_PTR(&board_i2c_obj) },
58+
{ MP_ROM_QSTR(MP_QSTR_PORTA_I2C), MP_ROM_PTR(&board_porta_i2c_obj) },
59+
60+
{ MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display)}
61+
};
62+
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
CONFIG_ESP32_SPIRAM_SUPPORT=n
2+
3+
# CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY is not set
4+
#
5+
# LWIP
6+
#
7+
CONFIG_LWIP_LOCAL_HOSTNAME="M5StaskStickCPlus"
8+
# end of LWIP
9+
10+
# Uncomment (remove ###) to send ESP_LOG output to TX/RX pins
11+
### #
12+
### # ESP System Settings
13+
### #
14+
### CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y
15+
### # CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT is not set
16+
### CONFIG_ESP_CONSOLE_UART_CUSTOM=y
17+
### CONFIG_ESP_CONSOLE_NONE is not set
18+
### CONFIG_ESP_CONSOLE_UART=y
19+
### CONFIG_ESP_CONSOLE_UART_CUSTOM_NUM_0=y
20+
### # CONFIG_ESP_CONSOLE_UART_CUSTOM_NUM_1 is not set
21+
### CONFIG_ESP_CONSOLE_UART_NUM=0
22+
### CONFIG_ESP_CONSOLE_UART_TX_GPIO=17
23+
### CONFIG_ESP_CONSOLE_UART_RX_GPIO=16
24+
### CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200
25+
### # CONFIG_ESP_SYSTEM_CHECK_INT_LEVEL_5 is not set
26+
### # end of ESP System Settings

0 commit comments

Comments
 (0)