Skip to content

Commit 6335789

Browse files
committed
initial working m5stack cardputer setup
1 parent c4e98e1 commit 6335789

File tree

5 files changed

+298
-0
lines changed

5 files changed

+298
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
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/fourwire/FourWire.h"
31+
#include "shared-bindings/microcontroller/Pin.h"
32+
#include "shared-module/displayio/__init__.h"
33+
#include "shared-module/displayio/mipi_constants.h"
34+
#include "shared-bindings/board/__init__.h"
35+
36+
fourwire_fourwire_obj_t board_display_obj;
37+
38+
#define DELAY 0x80
39+
40+
// display init sequence according to LilyGO example app
41+
uint8_t display_init_sequence[] = {
42+
// sw reset
43+
0x01, 0 | DELAY, 150,
44+
// sleep out
45+
0x11, 0 | DELAY, 255,
46+
// normal display mode on
47+
0x13, 0,
48+
// display and color format settings
49+
0x36, 1, 0x68,
50+
0xB6, 2, 0x0A, 0x82,
51+
0x3A, 1 | DELAY, 0x55, 10,
52+
// ST7789V frame rate setting
53+
0xB2, 5, 0x0C, 0x0C, 0x00, 0x33, 0x33,
54+
// voltages: VGH / VGL
55+
0xB7, 1, 0x35,
56+
// ST7789V power setting
57+
0xBB, 1, 0x28,
58+
0xC0, 1, 0x0C,
59+
0xC2, 2, 0x01, 0xFF,
60+
0xC3, 1, 0x10,
61+
0xC4, 1, 0x20,
62+
0xC6, 1, 0x0F,
63+
0xD0, 2, 0xA4, 0xA1,
64+
// ST7789V gamma setting
65+
0xE0, 14, 0xD0, 0x00, 0x02, 0x07, 0x0A, 0x28, 0x32, 0x44, 0x42, 0x06, 0x0E, 0x12, 0x14, 0x17,
66+
0xE1, 14, 0xD0, 0x00, 0x02, 0x07, 0x0A, 0x28, 0x31, 0x54, 0x47, 0x0E, 0x1C, 0x17, 0x1B, 0x1E,
67+
0x21, 0,
68+
// display on
69+
0x29, 0 | DELAY, 255,
70+
};
71+
72+
73+
void board_init(void) {
74+
busio_spi_obj_t *spi = common_hal_board_create_spi(0);
75+
fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus;
76+
bus->base.type = &fourwire_fourwire_type;
77+
78+
// see here for inspiration: https://github.com/m5stack/M5GFX/blob/33d7d3135e816a86a008fae8ab3757938cee95d2/src/M5GFX.cpp#L1350
79+
common_hal_fourwire_fourwire_construct(
80+
bus,
81+
spi,
82+
&pin_GPIO34, // DC
83+
&pin_GPIO37, // CS
84+
&pin_GPIO33, // RST
85+
40000000, // baudrate
86+
0, // polarity
87+
0 // phase
88+
);
89+
busdisplay_busdisplay_obj_t *display = &allocate_display()->display;
90+
display->base.type = &busdisplay_busdisplay_type;
91+
92+
common_hal_busdisplay_busdisplay_construct(
93+
display,
94+
bus,
95+
240, // width (after rotation)
96+
135, // height (after rotation)
97+
40, // column start
98+
53, // row start
99+
0, // rotation
100+
16, // color depth
101+
false, // grayscale
102+
false, // pixels in a byte share a row. Only valid for depths < 8
103+
1, // bytes per cell. Only valid for depths < 8
104+
false, // reverse_pixels_in_byte. Only valid for depths < 8
105+
true, // reverse_pixels_in_word
106+
MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command
107+
MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command
108+
MIPI_COMMAND_WRITE_MEMORY_START, // write memory command
109+
display_init_sequence,
110+
sizeof(display_init_sequence),
111+
&pin_GPIO38, // backlight pin
112+
NO_BRIGHTNESS_COMMAND,
113+
1.0f, // brightness
114+
false, // single_byte_bounds
115+
false, // data_as_commands
116+
true, // auto_refresh
117+
60, // native_frames_per_second
118+
true, // backlight_on_high
119+
false, // SH1107_addressing
120+
50000 // backlight pwm frequency
121+
);
122+
}
123+
124+
bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
125+
// Override the I2C/TFT power pin reset to prevent resetting the display.
126+
if (pin_number == 21) {
127+
// Turn on TFT and I2C
128+
gpio_set_direction(21, GPIO_MODE_DEF_OUTPUT);
129+
gpio_set_level(21, true);
130+
return true;
131+
}
132+
return false;
133+
}
134+
135+
// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here.
136+
137+
// TODO: Should we turn off the display when asleep, in board_deinit() ?
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Scott Shawcroft for Adafruit Industries
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 "M5 Stack Cardputer"
30+
#define MICROPY_HW_MCU_NAME "ESP32S3"
31+
32+
#define MICROPY_HW_NEOPIXEL (&pin_GPIO21)
33+
// #define CIRCUITPY_STATUS_LED_POWER (&pin_GPIO34)
34+
35+
// #define MICROPY_HW_LED_STATUS (&pin_GPIO13)
36+
37+
#define DEFAULT_I2C_BUS_SCL (&pin_GPIO41)
38+
#define DEFAULT_I2C_BUS_SDA (&pin_GPIO42)
39+
40+
// sd - do not use here, causes tft to stop working
41+
// #define DEFAULT_SPI_BUS_SCK (&pin_GPIO40)
42+
// #define DEFAULT_SPI_BUS_MOSI (&pin_GPIO14)
43+
// #define DEFAULT_SPI_BUS_MISO (&pin_GPIO39)
44+
// orig
45+
#define DEFAULT_SPI_BUS_SCK (&pin_GPIO36)
46+
#define DEFAULT_SPI_BUS_MOSI (&pin_GPIO35)
47+
#define DEFAULT_SPI_BUS_MISO (&pin_GPIO34)
48+
49+
#define DEFAULT_UART_BUS_RX (&pin_GPIO44)
50+
#define DEFAULT_UART_BUS_TX (&pin_GPIO43)
51+
// orig
52+
// #define DEFAULT_UART_BUS_RX (&pin_GPIO2)
53+
// #define DEFAULT_UART_BUS_TX (&pin_GPIO1)
54+
55+
// #define DOUBLE_TAP_PIN (&pin_GPIO38)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
USB_VID = 0x303A
2+
USB_PID = 0x811A
3+
USB_PRODUCT = "M5STACK CoreS3 - CircuitPython"
4+
USB_MANUFACTURER = "M5STACK"
5+
6+
IDF_TARGET = esp32s3
7+
8+
CIRCUITPY_ESP_FLASH_MODE = qio
9+
CIRCUITPY_ESP_FLASH_FREQ = 80m
10+
CIRCUITPY_ESP_FLASH_SIZE = 8MB
11+
CIRCUITPY_ESPCAMERA = 0
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "shared-bindings/board/__init__.h"
2+
3+
#include "shared-module/displayio/__init__.h"
4+
5+
STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
6+
CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS
7+
8+
// Port A
9+
{ MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_GPIO1) },
10+
{ MP_ROM_QSTR(MP_QSTR_PORTA1), MP_ROM_PTR(&pin_GPIO1) },
11+
{ MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_GPIO1) },
12+
{ MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_GPIO2) },
13+
{ MP_ROM_QSTR(MP_QSTR_PORTA2), MP_ROM_PTR(&pin_GPIO2) },
14+
{ MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_GPIO2) },
15+
16+
// Keyboard
17+
// { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_GPIO3) },
18+
{ MP_ROM_QSTR(MP_QSTR_KB_COL_2), MP_ROM_PTR(&pin_GPIO3) },
19+
// { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_GPIO4) },
20+
{ MP_ROM_QSTR(MP_QSTR_KB_COL_3), MP_ROM_PTR(&pin_GPIO4) },
21+
// { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_GPIO5) },
22+
{ MP_ROM_QSTR(MP_QSTR_KB_COL_4), MP_ROM_PTR(&pin_GPIO5) },
23+
// { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_GPIO6) },
24+
{ MP_ROM_QSTR(MP_QSTR_KB_COL_5), MP_ROM_PTR(&pin_GPIO6) },
25+
// { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_GPIO7) },
26+
{ MP_ROM_QSTR(MP_QSTR_KB_COL_6), MP_ROM_PTR(&pin_GPIO7) },
27+
// { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_GPIO8) },
28+
{ MP_ROM_QSTR(MP_QSTR_KB_A_0), MP_ROM_PTR(&pin_GPIO8) },
29+
// { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_GPIO9) },
30+
{ MP_ROM_QSTR(MP_QSTR_KB_A_1), MP_ROM_PTR(&pin_GPIO9) },
31+
// { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_GPIO11) },
32+
{ MP_ROM_QSTR(MP_QSTR_KB_A_2), MP_ROM_PTR(&pin_GPIO11) },
33+
// { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_GPIO13) },
34+
{ MP_ROM_QSTR(MP_QSTR_KB_COL_0), MP_ROM_PTR(&pin_GPIO13) },
35+
// { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_GPIO15) },
36+
{ MP_ROM_QSTR(MP_QSTR_KB_COL_1), MP_ROM_PTR(&pin_GPIO15) },
37+
38+
// Neopixel
39+
{ MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_GPIO21) },
40+
41+
// Speaker
42+
{ MP_ROM_QSTR(MP_QSTR_D41), MP_ROM_PTR(&pin_GPIO41) },
43+
{ MP_ROM_QSTR(MP_QSTR_D42), MP_ROM_PTR(&pin_GPIO42) },
44+
{ MP_ROM_QSTR(MP_QSTR_D43), MP_ROM_PTR(&pin_GPIO43) },
45+
46+
// IR
47+
{ MP_ROM_QSTR(MP_QSTR_D44), MP_ROM_PTR(&pin_GPIO44) },
48+
49+
// SD
50+
{ MP_ROM_QSTR(MP_QSTR_SD_MOSI), MP_ROM_PTR(&pin_GPIO14) },
51+
{ MP_ROM_QSTR(MP_QSTR_SD_SCK), MP_ROM_PTR(&pin_GPIO40) },
52+
{ MP_ROM_QSTR(MP_QSTR_SD_MISO), MP_ROM_PTR(&pin_GPIO39) },
53+
{ MP_ROM_QSTR(MP_QSTR_SD_CS), MP_ROM_PTR(&pin_GPIO12) },
54+
55+
56+
// Display
57+
{ MP_ROM_QSTR(MP_QSTR_TFT_RST), MP_ROM_PTR(&pin_GPIO33) },
58+
{ MP_ROM_QSTR(MP_QSTR_TFT_RESET), MP_ROM_PTR(&pin_GPIO33) },
59+
{ MP_ROM_QSTR(MP_QSTR_TFT_DC), MP_ROM_PTR(&pin_GPIO34) },
60+
{ MP_ROM_QSTR(MP_QSTR_TFT_RS), MP_ROM_PTR(&pin_GPIO34) },
61+
{ MP_ROM_QSTR(MP_QSTR_TFT_MOSI), MP_ROM_PTR(&pin_GPIO35) },
62+
{ MP_ROM_QSTR(MP_QSTR_TFT_DATA), MP_ROM_PTR(&pin_GPIO35) },
63+
{ MP_ROM_QSTR(MP_QSTR_TFT_SCK), MP_ROM_PTR(&pin_GPIO36) },
64+
{ MP_ROM_QSTR(MP_QSTR_TFT_CS), MP_ROM_PTR(&pin_GPIO37) },
65+
{ MP_ROM_QSTR(MP_QSTR_TFT_BACKLIGHT), MP_ROM_PTR(&pin_GPIO38) },
66+
{ MP_ROM_QSTR(MP_QSTR_TFT_BL), MP_ROM_PTR(&pin_GPIO38) },
67+
68+
// Button
69+
{ MP_ROM_QSTR(MP_QSTR_BUTTON), MP_ROM_PTR(&pin_GPIO0) },
70+
{ MP_ROM_QSTR(MP_QSTR_BOOT0), MP_ROM_PTR(&pin_GPIO0) },
71+
72+
// Other
73+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
74+
{ MP_ROM_QSTR(MP_QSTR_STEMMA_I2C), MP_ROM_PTR(&board_i2c_obj) },
75+
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) },
76+
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) },
77+
78+
{ MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display)}
79+
};
80+
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# Espressif IoT Development Framework Configuration
3+
#
4+
#
5+
# Component config
6+
#
7+
#
8+
# LWIP
9+
#
10+
CONFIG_LWIP_LOCAL_HOSTNAME="espressif-esp32s3"
11+
# end of LWIP
12+
13+
# end of Component config
14+
15+
# end of Espressif IoT Development Framework Configuration

0 commit comments

Comments
 (0)