Skip to content

Commit

Permalink
Merge pull request #827 from Daft-Freak/pico-config-defs
Browse files Browse the repository at this point in the history
More pico config
  • Loading branch information
Gadgetoid authored Jul 14, 2023
2 parents ca63486 + 6f443b3 commit eaf25f8
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 89 deletions.
18 changes: 14 additions & 4 deletions 32blit-pico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ target_compile_options(BlitHalPico INTERFACE
if(DEFINED PICO_ADDON)
# for boards that don't have a board in the pico sdk
# (usually because they are an add-on for a regular pico)
set(CONFIG_PATH ${CMAKE_CURRENT_LIST_DIR}/board/${PICO_ADDON}/config.cmake)
set(CONFIG_PATH ${CMAKE_CURRENT_LIST_DIR}/board/${PICO_ADDON})
set(BOARD_ID ${PICO_ADDON})
else()
set(CONFIG_PATH ${CMAKE_CURRENT_LIST_DIR}/board/${PICO_BOARD}/config.cmake)
set(CONFIG_PATH ${CMAKE_CURRENT_LIST_DIR}/board/${PICO_BOARD})
set(BOARD_ID ${PICO_BOARD})
endif()

if(EXISTS ${CONFIG_PATH})
include(${CONFIG_PATH})
if(EXISTS ${CONFIG_PATH}/config.cmake)
include(${CONFIG_PATH}/config.cmake)
message("Using board config \"${BLIT_BOARD_NAME}\"")
else()
include(${CMAKE_CURRENT_LIST_DIR}/board/pico/config.cmake)
Expand All @@ -79,6 +81,14 @@ else()
endif()
endif()

if(EXISTS ${CONFIG_PATH}/config.h)
list(APPEND BLIT_BOARD_DEFINITIONS "BLIT_BOARD_CONFIG=\"${CONFIG_PATH}/config.h\"")
endif()

# board id definition
string(TOUPPER ${BOARD_ID} BOARD_ID)
list(APPEND BLIT_BOARD_DEFINITIONS BLIT_BOARD_${BOARD_ID})

# default drivers
if(NOT BLIT_AUDIO_DRIVER)
set(BLIT_AUDIO_DRIVER "none")
Expand Down
9 changes: 0 additions & 9 deletions 32blit-pico/board/display_pack/config.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
set(BLIT_BOARD_NAME "Display Pack")

set(BLIT_BOARD_DEFINITIONS
DISPLAY_HEIGHT=135

LED_INVERTED
LED_R_PIN=6
LED_G_PIN=7
LED_B_PIN=8
)

blit_driver(display st7789)
blit_driver(input gpio_abxy)
8 changes: 8 additions & 0 deletions 32blit-pico/board/display_pack/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#define DISPLAY_HEIGHT 135

#define LED_INVERTED
#define LED_R_PIN 6
#define LED_G_PIN 7
#define LED_B_PIN 8
9 changes: 0 additions & 9 deletions 32blit-pico/board/display_pack_2/config.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
set(BLIT_BOARD_NAME "Display Pack 2.0")

set(BLIT_BOARD_DEFINITIONS
DISPLAY_WIDTH=320

LED_INVERTED
LED_R_PIN=6
LED_G_PIN=7
LED_B_PIN=8
)

blit_driver(display st7789)
blit_driver(input gpio_abxy)
8 changes: 8 additions & 0 deletions 32blit-pico/board/display_pack_2/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#define DISPLAY_WIDTH 320

#define LED_INVERTED
#define LED_R_PIN 6
#define LED_G_PIN 7
#define LED_B_PIN 8
6 changes: 0 additions & 6 deletions 32blit-pico/board/pimoroni_picosystem/config.cmake
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
set(BLIT_BOARD_NAME "PicoSystem")

set(BLIT_BOARD_DEFINITIONS
AUDIO_BEEP_PIN=PICOSYSTEM_AUDIO_PIN
LED_R_PIN=PICOSYSTEM_LED_R_PIN
LED_G_PIN=PICOSYSTEM_LED_G_PIN
LED_B_PIN=PICOSYSTEM_LED_B_PIN
USB_PRODUCT_STR="PicoSystem"

# non-default PWM audio support
PICO_AUDIO_PWM_MONO_PIN=PICOSYSTEM_AUDIO_PIN
PICO_AUDIO_PWM_PIO=1
Expand Down
17 changes: 17 additions & 0 deletions 32blit-pico/board/pimoroni_picosystem/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#define AUDIO_BEEP_PIN PICOSYSTEM_AUDIO_PIN

#define LED_R_PIN PICOSYSTEM_LED_R_PIN
#define LED_G_PIN PICOSYSTEM_LED_G_PIN
#define LED_B_PIN PICOSYSTEM_LED_B_PIN

#define LCD_CS_PIN PICOSYSTEM_LCD_CSN_PIN
#define LCD_DC_PIN PICOSYSTEM_LCD_DC_PIN
#define LCD_SCK_PIN PICOSYSTEM_LCD_SCLK_PIN
#define LCD_MOSI_PIN PICOSYSTEM_LCD_MOSI_PIN
#define LCD_BACKLIGHT_PIN PICOSYSTEM_BACKLIGHT_PIN
#define LCD_VSYNC_PIN PICOSYSTEM_LCD_VSYNC_PIN
#define LCD_RESET_PIN PICOSYSTEM_LCD_RESET_PIN

#define USB_PRODUCT_STR "PicoSystem"
5 changes: 5 additions & 0 deletions 32blit-pico/board/vgaboard/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#ifndef ALLOW_HIRES
#define ALLOW_HIRES 0 // disable by default, mode switching isn't supported
#endif
27 changes: 23 additions & 4 deletions 32blit-pico/config.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#pragma once

#include BLIT_BOARD_CONFIG

// these are the defaults

#ifndef ALLOW_HIRES
#ifdef DISPLAY_ST7789
#define ALLOW_HIRES 1
#else
#define ALLOW_HIRES 0 // hires is currently not supported for VGA/DVI
#endif
#endif

#ifndef DISPLAY_WIDTH
Expand All @@ -21,6 +20,26 @@
#define DISPLAY_HEIGHT 240
#endif

#ifndef LCD_CS_PIN
#define LCD_CS_PIN PICO_DEFAULT_SPI_CSN_PIN
#endif

#ifndef LCD_DC_PIN
#define LCD_DC_PIN 16
#endif

#ifndef LCD_SCK_PIN
#define LCD_SCK_PIN PICO_DEFAULT_SPI_SCK_PIN
#endif

#ifndef LCD_MOSI_PIN
#define LCD_MOSI_PIN PICO_DEFAULT_SPI_TX_PIN
#endif

#ifndef LCD_BACKLIGHT_PIN
#define LCD_BACKLIGHT_PIN 20
#endif

#ifndef OVERCLOCK_250
#define OVERCLOCK_250 1
#endif
Expand Down
99 changes: 42 additions & 57 deletions 32blit-pico/st7789.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,6 @@ namespace st7789 {

static uint16_t win_w, win_h; // window size

#ifdef PIMORONI_PICOSYSTEM
#define CS PICOSYSTEM_LCD_CSN_PIN
#define DC PICOSYSTEM_LCD_DC_PIN
#define SCK PICOSYSTEM_LCD_SCLK_PIN
#define MOSI PICOSYSTEM_LCD_MOSI_PIN
#define BACKLIGHT PICOSYSTEM_BACKLIGHT_PIN
#define VSYNC PICOSYSTEM_LCD_VSYNC_PIN
#define RESET PICOSYSTEM_LCD_RESET_PIN
#else
#define CS PICO_DEFAULT_SPI_CSN_PIN
#define DC 16
#define SCK PICO_DEFAULT_SPI_SCK_PIN
#define MOSI PICO_DEFAULT_SPI_TX_PIN
#define BACKLIGHT 20
#endif

static bool write_mode = false; // in RAMWR
static bool pixel_double = false;
static uint16_t *upd_frame_buffer = nullptr;
Expand Down Expand Up @@ -126,44 +110,44 @@ namespace st7789 {

void init(bool auto_init_sequence) {
// configure pins
gpio_set_function(DC, GPIO_FUNC_SIO);
gpio_set_dir(DC, GPIO_OUT);
gpio_set_function(LCD_DC_PIN, GPIO_FUNC_SIO);
gpio_set_dir(LCD_DC_PIN, GPIO_OUT);

gpio_set_function(CS, GPIO_FUNC_SIO);
gpio_set_dir(CS, GPIO_OUT);
gpio_set_function(LCD_CS_PIN, GPIO_FUNC_SIO);
gpio_set_dir(LCD_CS_PIN, GPIO_OUT);

bi_decl_if_func_used(bi_1pin_with_name(DC, "Display D/C"));
bi_decl_if_func_used(bi_1pin_with_name(CS, "Display CS"));
bi_decl_if_func_used(bi_1pin_with_name(LCD_DC_PIN, "Display D/C"));
bi_decl_if_func_used(bi_1pin_with_name(LCD_CS_PIN, "Display CS"));

// if supported by the display then the vsync pin is
// toggled high during vertical blanking period
#ifdef VSYNC
gpio_set_function(VSYNC, GPIO_FUNC_SIO);
gpio_set_dir(VSYNC, GPIO_IN);
gpio_set_pulls(VSYNC, false, true);
#ifdef LCD_VSYNC_PIN
gpio_set_function(LCD_VSYNC_PIN, GPIO_FUNC_SIO);
gpio_set_dir(LCD_VSYNC_PIN, GPIO_IN);
gpio_set_pulls(LCD_VSYNC_PIN, false, true);

bi_decl_if_func_used(bi_1pin_with_name(VSYNC, "Display TE/VSync"));
bi_decl_if_func_used(bi_1pin_with_name(LCD_VSYNC_PIN, "Display TE/VSync"));
#endif

// if a backlight pin is provided then set it up for
// pwm control
#ifdef BACKLIGHT
#ifdef LCD_BACKLIGHT_PIN
pwm_config pwm_cfg = pwm_get_default_config();
pwm_set_wrap(pwm_gpio_to_slice_num(BACKLIGHT), 65535);
pwm_init(pwm_gpio_to_slice_num(BACKLIGHT), &pwm_cfg, true);
gpio_set_function(BACKLIGHT, GPIO_FUNC_PWM);
pwm_set_wrap(pwm_gpio_to_slice_num(LCD_BACKLIGHT_PIN), 65535);
pwm_init(pwm_gpio_to_slice_num(LCD_BACKLIGHT_PIN), &pwm_cfg, true);
gpio_set_function(LCD_BACKLIGHT_PIN, GPIO_FUNC_PWM);

bi_decl_if_func_used(bi_1pin_with_name(BACKLIGHT, "Display Backlight"));
bi_decl_if_func_used(bi_1pin_with_name(LCD_BACKLIGHT_PIN, "Display Backlight"));
#endif

#ifdef RESET
gpio_set_function(RESET, GPIO_FUNC_SIO);
gpio_set_dir(RESET, GPIO_OUT);
gpio_put(RESET, 0);
#ifdef LCD_RESET_PIN
gpio_set_function(LCD_RESET_PIN, GPIO_FUNC_SIO);
gpio_set_dir(LCD_RESET_PIN, GPIO_OUT);
gpio_put(LCD_RESET_PIN, 0);
sleep_ms(100);
gpio_put(RESET, 1);
gpio_put(LCD_RESET_PIN, 1);

bi_decl_if_func_used(bi_1pin_with_name(RESET, "Display Reset"));
bi_decl_if_func_used(bi_1pin_with_name(LCD_RESET_PIN, "Display Reset"));
#endif

// setup PIO
Expand All @@ -176,21 +160,22 @@ namespace st7789 {
#if OVERCLOCK_250
sm_config_set_clkdiv(&cfg, 2); // back to 62.5MHz from overclock
#endif

sm_config_set_out_shift(&cfg, false, true, 8);
sm_config_set_out_pins(&cfg, MOSI, 1);
sm_config_set_out_pins(&cfg, LCD_MOSI_PIN, 1);
sm_config_set_fifo_join(&cfg, PIO_FIFO_JOIN_TX);
sm_config_set_sideset_pins(&cfg, SCK);
sm_config_set_sideset_pins(&cfg, LCD_SCK_PIN);

pio_gpio_init(pio, MOSI);
pio_gpio_init(pio, SCK);
pio_sm_set_consecutive_pindirs(pio, pio_sm, MOSI, 1, true);
pio_sm_set_consecutive_pindirs(pio, pio_sm, SCK, 1, true);
pio_gpio_init(pio, LCD_MOSI_PIN);
pio_gpio_init(pio, LCD_SCK_PIN);
pio_sm_set_consecutive_pindirs(pio, pio_sm, LCD_MOSI_PIN, 1, true);
pio_sm_set_consecutive_pindirs(pio, pio_sm, LCD_SCK_PIN, 1, true);

pio_sm_init(pio, pio_sm, pio_offset, &cfg);
pio_sm_set_enabled(pio, pio_sm, true);

bi_decl_if_func_used(bi_1pin_with_name(MOSI, "Display TX"));
bi_decl_if_func_used(bi_1pin_with_name(SCK, "Display SCK"));
bi_decl_if_func_used(bi_1pin_with_name(LCD_MOSI_PIN, "Display TX"));
bi_decl_if_func_used(bi_1pin_with_name(LCD_SCK_PIN, "Display SCK"));

// if auto_init_sequence then send initialisation sequence
// for our standard displays based on the width and height
Expand Down Expand Up @@ -293,21 +278,21 @@ namespace st7789 {
write_mode = false;
}

gpio_put(CS, 0);
gpio_put(LCD_CS_PIN, 0);

gpio_put(DC, 0); // command mode
gpio_put(LCD_DC_PIN, 0); // command mode
pio_put_byte(pio, pio_sm, command);

if(data) {
pio_wait(pio, pio_sm);
gpio_put(DC, 1); // data mode
gpio_put(LCD_DC_PIN, 1); // data mode

for(size_t i = 0; i < len; i++)
pio_put_byte(pio, pio_sm, data[i]);
}

pio_wait(pio, pio_sm);
gpio_put(CS, 1);
gpio_put(LCD_CS_PIN, 1);
}

void update(bool dont_block) {
Expand All @@ -331,18 +316,18 @@ namespace st7789 {
}

void set_backlight(uint8_t brightness) {
#ifdef BACKLIGHT
#ifdef LCD_BACKLIGHT_PIN
// gamma correct the provided 0-255 brightness value onto a
// 0-65535 range for the pwm counter
float gamma = 2.8;
uint16_t value = (uint16_t)(pow((float)(brightness) / 255.0f, gamma) * 65535.0f + 0.5f);
pwm_set_gpio_level(BACKLIGHT, value);
pwm_set_gpio_level(LCD_BACKLIGHT_PIN, value);
#endif
}

bool vsync_callback(gpio_irq_callback_t callback) {
#ifdef VSYNC
gpio_set_irq_enabled_with_callback(VSYNC, GPIO_IRQ_EDGE_RISE, true, callback);
#ifdef LCD_VSYNC_PIN
gpio_set_irq_enabled_with_callback(LCD_VSYNC_PIN, GPIO_IRQ_EDGE_RISE, true, callback);
return true;
#else
return false;
Expand Down Expand Up @@ -394,13 +379,13 @@ namespace st7789 {

// setup for writing
uint8_t r = reg::RAMWR;
gpio_put(CS, 0);
gpio_put(LCD_CS_PIN, 0);

gpio_put(DC, 0); // command mode
gpio_put(LCD_DC_PIN, 0); // command mode
pio_put_byte(pio, pio_sm, r);
pio_wait(pio, pio_sm);

gpio_put(DC, 1); // data mode
gpio_put(LCD_DC_PIN, 1); // data mode

pio_sm_set_enabled(pio, pio_sm, false);
pio_sm_restart(pio, pio_sm);
Expand Down

0 comments on commit eaf25f8

Please sign in to comment.