diff --git a/ports/raspberrypi/bindings/picodvi/Framebuffer.c b/ports/raspberrypi/bindings/picodvi/Framebuffer.c index 45307081527c9..2c300b76678b9 100644 --- a/ports/raspberrypi/bindings/picodvi/Framebuffer.c +++ b/ports/raspberrypi/bindings/picodvi/Framebuffer.c @@ -55,6 +55,7 @@ //| * 4 - Each nibble is a pixels in RGB format. The fourth bit is ignored. (RP2350 only) //| * 8 - Each byte is a pixels in RGB332 format. //| * 16 - Each two bytes are a pixel in RGB565 format. +//| * 32 - Each four bytes are a pixel in RGB888 format. The top byte is ignored. //| //| Output resolution support varies between the RP2040 and RP2350. //| @@ -63,17 +64,17 @@ //| full resolution. Color framebuffers must be half resolution (320x240 //| or 400x240) and pixels will be duplicated to create the signal. //| -//| On RP2350, output resolution is always 640x480. Monochrome +//| On RP2350, output resolution is either 640x480 or 720x400. Monochrome //| framebuffers (color_depth=1 or 2) must be full resolution. 4-bit -//| color must also be full resolution. 8-bit color can be half or full -//| resolution. 16-bit color must be half resolution due to RAM -//| limitations. +//| color must also be full resolution. 8-bit color can be quarter, half +//| or full resolution. 16-bit color and 32-bit color must be quarter or +//| half resolution due to internal RAM limitations. //| //| A Framebuffer is often used in conjunction with a //| `framebufferio.FramebufferDisplay`. //| -//| :param int width: the width of the target display signal. Only 320, 400, 640 or 800 is currently supported depending on color_depth and chip set. -//| :param int height: the height of the target display signal. Only 240 or 480 is currently supported depending on color_depth and chip set. +//| :param int width: the width of the source framebuffer. Support varies with chipset. +//| :param int height: the height of the source framebuffer. Support varies with chipset. //| :param ~microcontroller.Pin clk_dp: the positive clock signal pin //| :param ~microcontroller.Pin clk_dn: the negative clock signal pin //| :param ~microcontroller.Pin red_dp: the positive red signal pin @@ -83,7 +84,7 @@ //| :param ~microcontroller.Pin blue_dp: the positive blue signal pin //| :param ~microcontroller.Pin blue_dn: the negative blue signal pin //| :param int color_depth: the color depth of the framebuffer in bits. 1, 2 for grayscale -//| and 4 (RP2350 only), 8 or 16 for color +//| and 4 (RP2350 only), 8 or 16 for color, 32 for color (RP2350 only) //| """ //| @@ -114,7 +115,7 @@ static mp_obj_t picodvi_framebuffer_make_new(const mp_obj_type_t *type, size_t n mp_uint_t width = (mp_uint_t)mp_arg_validate_int_min(args[ARG_width].u_int, 0, MP_QSTR_width); mp_uint_t height = (mp_uint_t)mp_arg_validate_int_min(args[ARG_height].u_int, 0, MP_QSTR_height); mp_uint_t color_depth = args[ARG_color_depth].u_int; - if (color_depth != 1 && color_depth != 2 && color_depth != 4 && color_depth != 8 && color_depth != 16) { + if (color_depth != 1 && color_depth != 2 && color_depth != 4 && color_depth != 8 && color_depth != 16 && color_depth != 32) { mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid %q"), MP_QSTR_color_depth); } common_hal_picodvi_framebuffer_construct(self, @@ -221,7 +222,7 @@ static int picodvi_framebuffer_get_bytes_per_cell_proto(mp_obj_t self_in) { } static int picodvi_framebuffer_get_native_frames_per_second_proto(mp_obj_t self_in) { - return 60; + return common_hal_picodvi_framebuffer_get_native_frames_per_second(self_in); } static bool picodvi_framebuffer_get_pixels_in_byte_share_row_proto(mp_obj_t self_in) { diff --git a/ports/raspberrypi/bindings/picodvi/Framebuffer.h b/ports/raspberrypi/bindings/picodvi/Framebuffer.h index 280c3c9b8bf92..7eb7780ee49b8 100644 --- a/ports/raspberrypi/bindings/picodvi/Framebuffer.h +++ b/ports/raspberrypi/bindings/picodvi/Framebuffer.h @@ -29,5 +29,6 @@ int common_hal_picodvi_framebuffer_get_width(picodvi_framebuffer_obj_t *self); int common_hal_picodvi_framebuffer_get_height(picodvi_framebuffer_obj_t *self); int common_hal_picodvi_framebuffer_get_row_stride(picodvi_framebuffer_obj_t *self); int common_hal_picodvi_framebuffer_get_color_depth(picodvi_framebuffer_obj_t *self); +int common_hal_picodvi_framebuffer_get_native_frames_per_second(picodvi_framebuffer_obj_t *self); bool common_hal_picodvi_framebuffer_get_grayscale(picodvi_framebuffer_obj_t *self); mp_int_t common_hal_picodvi_framebuffer_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags); diff --git a/ports/raspberrypi/boards/adafruit_fruit_jam/board.c b/ports/raspberrypi/boards/adafruit_fruit_jam/board.c new file mode 100644 index 0000000000000..111fc5175139c --- /dev/null +++ b/ports/raspberrypi/boards/adafruit_fruit_jam/board.c @@ -0,0 +1,36 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "common-hal/microcontroller/Pin.h" +#include "hardware/gpio.h" +#include "shared-bindings/usb_host/Port.h" +#include "supervisor/board.h" + +// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. + + +#if defined(DEFAULT_USB_HOST_5V_POWER) +bool board_reset_pin_number(uint8_t pin_number) { + if (pin_number == DEFAULT_USB_HOST_5V_POWER->number) { + // doing this (rather than gpio_init) in this specific order ensures no + // glitch if pin was already configured as a high output. gpio_init() temporarily + // configures the pin as an input, so the power enable value would potentially + // glitch. + gpio_put(pin_number, 1); + gpio_set_dir(pin_number, GPIO_OUT); + gpio_set_function(pin_number, GPIO_FUNC_SIO); + + return true; + } + return false; +} +#endif + +#if defined(DEFAULT_USB_HOST_DATA_PLUS) +void board_init(void) { + common_hal_usb_host_port_construct(DEFAULT_USB_HOST_DATA_PLUS, DEFAULT_USB_HOST_DATA_MINUS); +} +#endif diff --git a/ports/raspberrypi/boards/adafruit_fruit_jam/mpconfigboard.h b/ports/raspberrypi/boards/adafruit_fruit_jam/mpconfigboard.h new file mode 100644 index 0000000000000..0ff814d653acd --- /dev/null +++ b/ports/raspberrypi/boards/adafruit_fruit_jam/mpconfigboard.h @@ -0,0 +1,24 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2024 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#define MICROPY_HW_BOARD_NAME "Adafruit Fruit Jam" +#define MICROPY_HW_MCU_NAME "rp2350b" + +#define MICROPY_HW_NEOPIXEL (&pin_GPIO32) +#define MICROPY_HW_NEOPIXEL_COUNT (5) + +#define DEFAULT_I2C_BUS_SCL (&pin_GPIO21) +#define DEFAULT_I2C_BUS_SDA (&pin_GPIO20) + +#define DEFAULT_SPI_BUS_SCK (&pin_GPIO30) +#define DEFAULT_SPI_BUS_MOSI (&pin_GPIO31) +#define DEFAULT_SPI_BUS_MISO (&pin_GPIO28) + +#define DEFAULT_USB_HOST_DATA_PLUS (&pin_GPIO1) +#define DEFAULT_USB_HOST_DATA_MINUS (&pin_GPIO2) +#define DEFAULT_USB_HOST_5V_POWER (&pin_GPIO11) + +#define CIRCUITPY_PSRAM_CHIP_SELECT (&pin_GPIO47) diff --git a/ports/raspberrypi/boards/adafruit_fruit_jam/mpconfigboard.mk b/ports/raspberrypi/boards/adafruit_fruit_jam/mpconfigboard.mk new file mode 100644 index 0000000000000..21215164706e1 --- /dev/null +++ b/ports/raspberrypi/boards/adafruit_fruit_jam/mpconfigboard.mk @@ -0,0 +1,10 @@ +USB_VID = 0x239A +USB_PID = 0x816C +USB_PRODUCT = "Fruit Jam" +USB_MANUFACTURER = "Adafruit" + +CHIP_VARIANT = RP2350 +CHIP_PACKAGE = B +CHIP_FAMILY = rp2 + +EXTERNAL_FLASH_DEVICES = "W25Q128JVxQ" diff --git a/ports/raspberrypi/boards/adafruit_fruit_jam/pico-sdk-configboard.h b/ports/raspberrypi/boards/adafruit_fruit_jam/pico-sdk-configboard.h new file mode 100644 index 0000000000000..2d9283a9192f2 --- /dev/null +++ b/ports/raspberrypi/boards/adafruit_fruit_jam/pico-sdk-configboard.h @@ -0,0 +1,10 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// Put board-specific pico-sdk definitions here. This file must exist. + +// Allow extra time for xosc to start. +#define PICO_XOSC_STARTUP_DELAY_MULTIPLIER 64 diff --git a/ports/raspberrypi/boards/adafruit_fruit_jam/pins.c b/ports/raspberrypi/boards/adafruit_fruit_jam/pins.c new file mode 100644 index 0000000000000..dba8617d2946c --- /dev/null +++ b/ports/raspberrypi/boards/adafruit_fruit_jam/pins.c @@ -0,0 +1,87 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2024 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "shared-bindings/board/__init__.h" + +static const mp_rom_map_elem_t board_module_globals_table[] = { + CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS + + { MP_OBJ_NEW_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_GPIO41) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_GPIO42) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_GPIO43) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_GPIO44) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_GPIO45) }, + + // On-board switch reverses D0 and D1 connections to RX and TX. + + { MP_OBJ_NEW_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_GPIO6) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_GPIO7) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_GPIO8) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_GPIO9) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_GPIO10) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO29) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_BUTTON), MP_ROM_PTR(&pin_GPIO0) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_BOOT), MP_ROM_PTR(&pin_GPIO0) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_BUTTON1), MP_ROM_PTR(&pin_GPIO4) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_BUTTON2), MP_ROM_PTR(&pin_GPIO5) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_GPIO20) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_GPIO21) }, + + { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_GPIO30) }, + { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_GPIO31) }, + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_GPIO28) }, + + { MP_ROM_QSTR(MP_QSTR_TFT_CS), MP_ROM_PTR(&pin_GPIO46) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_GPIO32) }, + + { MP_ROM_QSTR(MP_QSTR_CKN), MP_ROM_PTR(&pin_GPIO12) }, + { MP_ROM_QSTR(MP_QSTR_CKP), MP_ROM_PTR(&pin_GPIO13) }, + { MP_ROM_QSTR(MP_QSTR_D0N), MP_ROM_PTR(&pin_GPIO14) }, + { MP_ROM_QSTR(MP_QSTR_D0P), MP_ROM_PTR(&pin_GPIO15) }, + { MP_ROM_QSTR(MP_QSTR_D1N), MP_ROM_PTR(&pin_GPIO16) }, + { MP_ROM_QSTR(MP_QSTR_D1P), MP_ROM_PTR(&pin_GPIO17) }, + { MP_ROM_QSTR(MP_QSTR_D2N), MP_ROM_PTR(&pin_GPIO18) }, + { MP_ROM_QSTR(MP_QSTR_D2P), MP_ROM_PTR(&pin_GPIO19) }, + + { MP_ROM_QSTR(MP_QSTR_I2S_RESET), MP_ROM_PTR(&pin_GPIO22) }, + { MP_ROM_QSTR(MP_QSTR_I2S_MCLK), MP_ROM_PTR(&pin_GPIO27) }, + { MP_ROM_QSTR(MP_QSTR_I2S_BCLK), MP_ROM_PTR(&pin_GPIO26) }, + { MP_ROM_QSTR(MP_QSTR_I2S_WS), MP_ROM_PTR(&pin_GPIO25) }, + { MP_ROM_QSTR(MP_QSTR_I2S_DIN), MP_ROM_PTR(&pin_GPIO24) }, + { MP_ROM_QSTR(MP_QSTR_I2S_GPIO1), MP_ROM_PTR(&pin_GPIO23) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_SD_SCK), MP_ROM_PTR(&pin_GPIO34) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_SDIO_CLOCK), MP_ROM_PTR(&pin_GPIO34) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_SD_MOSI), MP_ROM_PTR(&pin_GPIO35) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_SDIO_COMMAND), MP_ROM_PTR(&pin_GPIO35) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_SD_MISO), MP_ROM_PTR(&pin_GPIO36) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_SDIO_DATA0), MP_ROM_PTR(&pin_GPIO36) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_SDIO_DATA1), MP_ROM_PTR(&pin_GPIO37) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_SDIO_DATA2), MP_ROM_PTR(&pin_GPIO38) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_SD_CS), MP_ROM_PTR(&pin_GPIO39) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_SDIO_DATA3), MP_ROM_PTR(&pin_GPIO39) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_SD_CARD_DETECT), MP_ROM_PTR(&pin_GPIO33) }, + + { MP_ROM_QSTR(MP_QSTR_USB_HOST_DATA_PLUS), MP_ROM_PTR(&pin_GPIO1) }, + { MP_ROM_QSTR(MP_QSTR_USB_HOST_DATA_MINUS), MP_ROM_PTR(&pin_GPIO2) }, + { MP_ROM_QSTR(MP_QSTR_USB_HOST_5V_POWER), MP_ROM_PTR(&pin_GPIO11) }, + + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_STEMMA_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2040.c b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2040.c index 5b69610ace785..0bfb86b4c8a1f 100644 --- a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2040.c +++ b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2040.c @@ -141,7 +141,7 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, // If the width is > 400, then it must not be color frame buffer and vice // versa. - if ((width > 400) == color_framebuffer || color_depth == 4) { + if ((width > 400) == color_framebuffer || color_depth == 4 || color_depth == 32) { mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid %q"), MP_QSTR_color_depth); } @@ -385,6 +385,10 @@ int common_hal_picodvi_framebuffer_get_color_depth(picodvi_framebuffer_obj_t *se return self->color_depth; } +int common_hal_picodvi_framebuffer_get_native_frames_per_second(picodvi_framebuffer_obj_t *self) { + return 60; +} + bool common_hal_picodvi_framebuffer_get_grayscale(picodvi_framebuffer_obj_t *self) { return self->color_depth < 8; } diff --git a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c index efa5c6d0d8e8c..27aa26daeabd2 100644 --- a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c +++ b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.c @@ -53,25 +53,37 @@ #define SYNC_V1_H0 (TMDS_CTRL_10 | (TMDS_CTRL_00 << 10) | (TMDS_CTRL_00 << 20)) #define SYNC_V1_H1 (TMDS_CTRL_11 | (TMDS_CTRL_00 << 10) | (TMDS_CTRL_00 << 20)) -#define MODE_H_SYNC_POLARITY 0 -#define MODE_H_FRONT_PORCH 16 -#define MODE_H_SYNC_WIDTH 96 -#define MODE_H_BACK_PORCH 48 -#define MODE_H_ACTIVE_PIXELS 640 - -#define MODE_V_SYNC_POLARITY 0 -#define MODE_V_FRONT_PORCH 10 -#define MODE_V_SYNC_WIDTH 2 -#define MODE_V_BACK_PORCH 33 -#define MODE_V_ACTIVE_LINES 480 - -#define MODE_H_TOTAL_PIXELS ( \ - MODE_H_FRONT_PORCH + MODE_H_SYNC_WIDTH + \ - MODE_H_BACK_PORCH + MODE_H_ACTIVE_PIXELS \ +#define MODE_720_H_SYNC_POLARITY 0 +#define MODE_720_H_FRONT_PORCH 24 +#define MODE_720_H_SYNC_WIDTH 64 +#define MODE_720_H_BACK_PORCH 88 +#define MODE_720_H_ACTIVE_PIXELS 720 + +#define MODE_720_V_SYNC_POLARITY 0 +#define MODE_720_V_FRONT_PORCH 3 +#define MODE_720_V_SYNC_WIDTH 4 +#define MODE_720_V_BACK_PORCH 13 +#define MODE_720_V_ACTIVE_LINES 400 + +#define MODE_640_H_SYNC_POLARITY 0 +#define MODE_640_H_FRONT_PORCH 16 +#define MODE_640_H_SYNC_WIDTH 96 +#define MODE_640_H_BACK_PORCH 48 +#define MODE_640_H_ACTIVE_PIXELS 640 + +#define MODE_640_V_SYNC_POLARITY 0 +#define MODE_640_V_FRONT_PORCH 10 +#define MODE_640_V_SYNC_WIDTH 2 +#define MODE_640_V_BACK_PORCH 33 +#define MODE_640_V_ACTIVE_LINES 480 + +#define MODE_720_V_TOTAL_LINES ( \ + MODE_720_V_FRONT_PORCH + MODE_720_V_SYNC_WIDTH + \ + MODE_720_V_BACK_PORCH + MODE_720_V_ACTIVE_LINES \ ) -#define MODE_V_TOTAL_LINES ( \ - MODE_V_FRONT_PORCH + MODE_V_SYNC_WIDTH + \ - MODE_V_BACK_PORCH + MODE_V_ACTIVE_LINES \ +#define MODE_640_V_TOTAL_LINES ( \ + MODE_640_V_FRONT_PORCH + MODE_640_V_SYNC_WIDTH + \ + MODE_640_V_BACK_PORCH + MODE_640_V_ACTIVE_LINES \ ) #define HSTX_CMD_RAW (0x0u << 12) @@ -83,34 +95,67 @@ // ---------------------------------------------------------------------------- // HSTX command lists -static uint32_t vblank_line_vsync_off[] = { - HSTX_CMD_RAW_REPEAT | MODE_H_FRONT_PORCH, +#define VSYNC_LEN 6 +#define VACTIVE_LEN 9 + +static uint32_t vblank_line640_vsync_off[VSYNC_LEN] = { + HSTX_CMD_RAW_REPEAT | MODE_640_H_FRONT_PORCH, SYNC_V1_H1, - HSTX_CMD_RAW_REPEAT | MODE_H_SYNC_WIDTH, + HSTX_CMD_RAW_REPEAT | MODE_640_H_SYNC_WIDTH, SYNC_V1_H0, - HSTX_CMD_RAW_REPEAT | (MODE_H_BACK_PORCH + MODE_H_ACTIVE_PIXELS), + HSTX_CMD_RAW_REPEAT | (MODE_640_H_BACK_PORCH + MODE_640_H_ACTIVE_PIXELS), SYNC_V1_H1 }; -static uint32_t vblank_line_vsync_on[] = { - HSTX_CMD_RAW_REPEAT | MODE_H_FRONT_PORCH, +static uint32_t vblank_line640_vsync_on[VSYNC_LEN] = { + HSTX_CMD_RAW_REPEAT | MODE_640_H_FRONT_PORCH, SYNC_V0_H1, - HSTX_CMD_RAW_REPEAT | MODE_H_SYNC_WIDTH, + HSTX_CMD_RAW_REPEAT | MODE_640_H_SYNC_WIDTH, SYNC_V0_H0, - HSTX_CMD_RAW_REPEAT | (MODE_H_BACK_PORCH + MODE_H_ACTIVE_PIXELS), + HSTX_CMD_RAW_REPEAT | (MODE_640_H_BACK_PORCH + MODE_640_H_ACTIVE_PIXELS), SYNC_V0_H1 }; -static uint32_t vactive_line[] = { - HSTX_CMD_RAW_REPEAT | MODE_H_FRONT_PORCH, +static uint32_t vactive_line640[VACTIVE_LEN] = { + HSTX_CMD_RAW_REPEAT | MODE_640_H_FRONT_PORCH, SYNC_V1_H1, HSTX_CMD_NOP, - HSTX_CMD_RAW_REPEAT | MODE_H_SYNC_WIDTH, + HSTX_CMD_RAW_REPEAT | MODE_640_H_SYNC_WIDTH, SYNC_V1_H0, HSTX_CMD_NOP, - HSTX_CMD_RAW_REPEAT | MODE_H_BACK_PORCH, + HSTX_CMD_RAW_REPEAT | MODE_640_H_BACK_PORCH, + SYNC_V1_H1, + HSTX_CMD_TMDS | MODE_640_H_ACTIVE_PIXELS +}; + +static uint32_t vblank_line720_vsync_off[VSYNC_LEN] = { + HSTX_CMD_RAW_REPEAT | MODE_720_H_FRONT_PORCH, SYNC_V1_H1, - HSTX_CMD_TMDS | MODE_H_ACTIVE_PIXELS + HSTX_CMD_RAW_REPEAT | MODE_720_H_SYNC_WIDTH, + SYNC_V1_H0, + HSTX_CMD_RAW_REPEAT | (MODE_720_H_BACK_PORCH + MODE_720_H_ACTIVE_PIXELS), + SYNC_V1_H1 +}; + +static uint32_t vblank_line720_vsync_on[VSYNC_LEN] = { + HSTX_CMD_RAW_REPEAT | MODE_720_H_FRONT_PORCH, + SYNC_V0_H1, + HSTX_CMD_RAW_REPEAT | MODE_720_H_SYNC_WIDTH, + SYNC_V0_H0, + HSTX_CMD_RAW_REPEAT | (MODE_720_H_BACK_PORCH + MODE_720_H_ACTIVE_PIXELS), + SYNC_V0_H1 +}; + +static uint32_t vactive_line720[VACTIVE_LEN] = { + HSTX_CMD_RAW_REPEAT | MODE_720_H_FRONT_PORCH, + SYNC_V1_H1, + HSTX_CMD_NOP, + HSTX_CMD_RAW_REPEAT | MODE_720_H_SYNC_WIDTH, + SYNC_V1_H0, + HSTX_CMD_NOP, + HSTX_CMD_RAW_REPEAT | MODE_720_H_BACK_PORCH, + SYNC_V1_H1, + HSTX_CMD_TMDS | MODE_720_H_ACTIVE_PIXELS }; picodvi_framebuffer_obj_t *active_picodvi = NULL; @@ -133,12 +178,33 @@ bool common_hal_picodvi_framebuffer_preflight( mp_uint_t width, mp_uint_t height, mp_uint_t color_depth) { + // These modes don't duplicate pixels so we can do sub-byte colors. They + // take too much ram for more than 8bit color though. + bool full_resolution = color_depth == 1 || color_depth == 2 || color_depth == 4 || color_depth == 8; + // These modes rely on the memory transfer to duplicate values across bytes. + bool doubled = color_depth == 8 || color_depth == 16 || color_depth == 32; + // for each supported resolution, check the color depth is supported if (width == 640 && height == 480) { - return color_depth == 1 || color_depth == 2 || color_depth == 4 || color_depth == 8; + return full_resolution; } if (width == 320 && height == 240) { - return color_depth == 8 || color_depth == 16; + return doubled; + } + if (width == 160 && height == 120) { + return doubled; + } + + if (width == 720 && height == 400) { + return full_resolution; + } + + if (width == 360 && height == 200) { + return doubled; + } + + if (width == 180 && height == 100) { + return doubled; } return false; } @@ -158,7 +224,12 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid %q and %q"), MP_QSTR_width, MP_QSTR_height); } - bool pixel_doubled = width == 320 && height == 240; + if (width % 160 == 0) { + self->output_width = 640; + } else { + self->output_width = 720; + } + size_t output_scaling = self->output_width / width; size_t all_allocated = 0; int8_t pins[8] = { @@ -203,16 +274,22 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, m_malloc_fail(framebuffer_size * sizeof(uint32_t)); return; } + memset(self->framebuffer, 0, framebuffer_size * sizeof(uint32_t)); // We compute all DMA transfers needed for a single frame. This ensure we don't have any super // quick interrupts that we need to respond to. Each transfer takes two words, trans_count and // read_addr. Active pixel lines need two transfers due to different read addresses. When pixel // doubling, then we must also set transfer size. size_t dma_command_size = 2; - if (pixel_doubled) { + if (output_scaling > 1) { dma_command_size = 4; } - self->dma_commands_len = (MODE_V_FRONT_PORCH + MODE_V_SYNC_WIDTH + MODE_V_BACK_PORCH + 2 * MODE_V_ACTIVE_LINES + 1) * dma_command_size; + + if (self->output_width == 640) { + self->dma_commands_len = (MODE_640_V_FRONT_PORCH + MODE_640_V_SYNC_WIDTH + MODE_640_V_BACK_PORCH + 2 * MODE_640_V_ACTIVE_LINES + 1) * dma_command_size; + } else { + self->dma_commands_len = (MODE_720_V_FRONT_PORCH + MODE_720_V_SYNC_WIDTH + MODE_720_V_BACK_PORCH + 2 * MODE_720_V_ACTIVE_LINES + 1) * dma_command_size; + } self->dma_commands = (uint32_t *)port_malloc(self->dma_commands_len * sizeof(uint32_t), true); if (self->dma_commands == NULL || ((size_t)self->framebuffer & 0xf0000000) == 0x10000000) { port_free(self->framebuffer); @@ -235,20 +312,33 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, self->dma_pixel_channel = dma_pixel_channel_maybe; self->dma_command_channel = dma_command_channel_maybe; - size_t words_per_line; - if (self->color_depth > 8) { - words_per_line = (self->width * (self->color_depth / 8)) / sizeof(uint32_t); + size_t command_word = 0; + size_t frontporch_start; + if (self->output_width == 640) { + frontporch_start = MODE_640_V_TOTAL_LINES - MODE_640_V_FRONT_PORCH; } else { - words_per_line = (self->width / (8 / self->color_depth)) / sizeof(uint32_t); + frontporch_start = MODE_720_V_TOTAL_LINES - MODE_720_V_FRONT_PORCH; + } + size_t frontporch_end = frontporch_start; + if (self->output_width == 640) { + frontporch_end += MODE_640_V_FRONT_PORCH; + } else { + frontporch_end += MODE_720_V_FRONT_PORCH; } - - size_t command_word = 0; - size_t frontporch_start = MODE_V_TOTAL_LINES - MODE_V_FRONT_PORCH; - size_t frontporch_end = frontporch_start + MODE_V_FRONT_PORCH; size_t vsync_start = 0; - size_t vsync_end = vsync_start + MODE_V_SYNC_WIDTH; + size_t vsync_end = vsync_start; + if (self->output_width == 640) { + vsync_end += MODE_640_V_SYNC_WIDTH; + } else { + vsync_end += MODE_720_V_SYNC_WIDTH; + } size_t backporch_start = vsync_end; - size_t backporch_end = backporch_start + MODE_V_BACK_PORCH; + size_t backporch_end = backporch_start; + if (self->output_width == 640) { + backporch_end += MODE_640_V_BACK_PORCH; + } else { + backporch_end += MODE_720_V_BACK_PORCH; + } size_t active_start = backporch_end; uint32_t dma_ctrl = self->dma_command_channel << DMA_CH0_CTRL_TRIG_CHAIN_TO_LSB | @@ -257,10 +347,12 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, DMA_CH0_CTRL_TRIG_INCR_READ_BITS | DMA_CH0_CTRL_TRIG_EN_BITS; uint32_t dma_pixel_ctrl; - if (pixel_doubled) { + if (output_scaling > 1) { // We do color_depth size transfers when pixel doubling. The memory bus will - // duplicate the 16 bits to produce 32 bits for the HSTX. - if (color_depth == 16) { + // duplicate the bytes read to produce 32 bits for the HSTX. + if (color_depth == 32) { + dma_pixel_ctrl = dma_ctrl | DMA_SIZE_32 << DMA_CH0_CTRL_TRIG_DATA_SIZE_LSB; + } else if (color_depth == 16) { dma_pixel_ctrl = dma_ctrl | DMA_SIZE_16 << DMA_CH0_CTRL_TRIG_DATA_SIZE_LSB; } else { dma_pixel_ctrl = dma_ctrl | DMA_SIZE_8 << DMA_CH0_CTRL_TRIG_DATA_SIZE_LSB; @@ -277,34 +369,46 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, // Write ctrl and write_addr once when not pixel doubling because they don't // change. (write_addr doesn't change when pixel doubling either but we need // to rewrite it because it is after the ctrl register.) - if (!pixel_doubled) { + if (output_scaling == 1) { dma_channel_hw_addr(self->dma_pixel_channel)->al1_ctrl = dma_ctrl; dma_channel_hw_addr(self->dma_pixel_channel)->al1_write_addr = dma_write_addr; } - for (size_t v_scanline = 0; v_scanline < MODE_V_TOTAL_LINES; v_scanline++) { - if (pixel_doubled) { + + uint32_t *vblank_line_vsync_on = self->output_width == 640 ? vblank_line640_vsync_on : vblank_line720_vsync_on; + uint32_t *vblank_line_vsync_off = self->output_width == 640 ? vblank_line640_vsync_off : vblank_line720_vsync_off; + uint32_t *vactive_line = self->output_width == 640 ? vactive_line640 : vactive_line720; + + size_t mode_v_total_lines; + if (self->output_width == 640) { + mode_v_total_lines = MODE_640_V_TOTAL_LINES; + } else { + mode_v_total_lines = MODE_720_V_TOTAL_LINES; + } + + for (size_t v_scanline = 0; v_scanline < mode_v_total_lines; v_scanline++) { + if (output_scaling > 1) { self->dma_commands[command_word++] = dma_ctrl; self->dma_commands[command_word++] = dma_write_addr; } if (vsync_start <= v_scanline && v_scanline < vsync_end) { - self->dma_commands[command_word++] = count_of(vblank_line_vsync_on); + self->dma_commands[command_word++] = VSYNC_LEN; self->dma_commands[command_word++] = (uintptr_t)vblank_line_vsync_on; } else if (backporch_start <= v_scanline && v_scanline < backporch_end) { - self->dma_commands[command_word++] = count_of(vblank_line_vsync_off); + self->dma_commands[command_word++] = VSYNC_LEN; self->dma_commands[command_word++] = (uintptr_t)vblank_line_vsync_off; } else if (frontporch_start <= v_scanline && v_scanline < frontporch_end) { - self->dma_commands[command_word++] = count_of(vblank_line_vsync_off); + self->dma_commands[command_word++] = VSYNC_LEN; self->dma_commands[command_word++] = (uintptr_t)vblank_line_vsync_off; } else { - self->dma_commands[command_word++] = count_of(vactive_line); + self->dma_commands[command_word++] = VACTIVE_LEN; self->dma_commands[command_word++] = (uintptr_t)vactive_line; size_t row = v_scanline - active_start; - size_t transfer_count = words_per_line; - if (pixel_doubled) { + size_t transfer_count = self->pitch; + if (output_scaling > 1) { self->dma_commands[command_word++] = dma_pixel_ctrl; self->dma_commands[command_word++] = dma_write_addr; - row /= 2; - // When pixel doubling, we do one transfer per pixel and it gets + row /= output_scaling; + // When pixel scaling, we do one transfer per pixel and it gets // mirrored into the rest of the word. transfer_count = self->width; } @@ -314,7 +418,7 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, } } // Last command is NULL which will trigger an IRQ. - if (pixel_doubled) { + if (output_scaling > 1) { self->dma_commands[command_word++] = DMA_CH0_CTRL_TRIG_IRQ_QUIET_BITS | DMA_CH0_CTRL_TRIG_EN_BITS; self->dma_commands[command_word++] = 0; @@ -322,7 +426,16 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, self->dma_commands[command_word++] = 0; self->dma_commands[command_word++] = 0; - if (color_depth == 16) { + if (color_depth == 32) { + // Configure HSTX's TMDS encoder for RGB888 + hstx_ctrl_hw->expand_tmds = + 7 << HSTX_CTRL_EXPAND_TMDS_L2_NBITS_LSB | + 16 << HSTX_CTRL_EXPAND_TMDS_L2_ROT_LSB | + 7 << HSTX_CTRL_EXPAND_TMDS_L1_NBITS_LSB | + 8 << HSTX_CTRL_EXPAND_TMDS_L1_ROT_LSB | + 7 << HSTX_CTRL_EXPAND_TMDS_L0_NBITS_LSB | + 0 << HSTX_CTRL_EXPAND_TMDS_L0_ROT_LSB; + } else if (color_depth == 16) { // Configure HSTX's TMDS encoder for RGB565 hstx_ctrl_hw->expand_tmds = 4 << HSTX_CTRL_EXPAND_TMDS_L2_NBITS_LSB | @@ -360,18 +473,25 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, (color_depth - 1) << HSTX_CTRL_EXPAND_TMDS_L0_NBITS_LSB | rot << HSTX_CTRL_EXPAND_TMDS_L0_ROT_LSB; } - size_t shifts_before_empty = ((32 / color_depth) % 32); - if (pixel_doubled && color_depth == 8) { - // All but 320x240 at 8bits will shift through all 32 bits. We are only - // doubling so we only need 16 bits (2 x 8) to get our doubled pixel. - shifts_before_empty = 2; + size_t pixels_per_word; + if (output_scaling == 1) { + pixels_per_word = 32 / color_depth; + } else { + pixels_per_word = 1; } + size_t shifts_before_empty = (pixels_per_word % 32); + if (output_scaling > 1) { + shifts_before_empty *= output_scaling; + } + + size_t shift_amount = color_depth % 32; + // Pixels come in 32 bits at a time. color_depth dictates the number // of pixels per word. Control symbols (RAW) are an entire 32-bit word. hstx_ctrl_hw->expand_shift = shifts_before_empty << HSTX_CTRL_EXPAND_SHIFT_ENC_N_SHIFTS_LSB | - color_depth << HSTX_CTRL_EXPAND_SHIFT_ENC_SHIFT_LSB | + shift_amount << HSTX_CTRL_EXPAND_SHIFT_ENC_SHIFT_LSB | 1 << HSTX_CTRL_EXPAND_SHIFT_RAW_N_SHIFTS_LSB | 0 << HSTX_CTRL_EXPAND_SHIFT_RAW_SHIFT_LSB; @@ -424,7 +544,7 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, // This wraps the transfer back to the start of the write address. size_t wrap = 3; // 8 bytes because we write two DMA registers. volatile uint32_t *write_addr = &dma_hw->ch[self->dma_pixel_channel].al3_transfer_count; - if (pixel_doubled) { + if (output_scaling > 1) { wrap = 4; // 16 bytes because we write all four DMA registers. write_addr = &dma_hw->ch[self->dma_pixel_channel].al3_ctrl; } @@ -511,6 +631,10 @@ int common_hal_picodvi_framebuffer_get_color_depth(picodvi_framebuffer_obj_t *se return self->color_depth; } +int common_hal_picodvi_framebuffer_get_native_frames_per_second(picodvi_framebuffer_obj_t *self) { + return self->output_width == 640 ? 60 : 70; +} + bool common_hal_picodvi_framebuffer_get_grayscale(picodvi_framebuffer_obj_t *self) { return self->color_depth < 4; } @@ -518,7 +642,13 @@ bool common_hal_picodvi_framebuffer_get_grayscale(picodvi_framebuffer_obj_t *sel mp_int_t common_hal_picodvi_framebuffer_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { picodvi_framebuffer_obj_t *self = (picodvi_framebuffer_obj_t *)self_in; bufinfo->buf = self->framebuffer; - bufinfo->typecode = self->color_depth > 8 ? 'H' : 'B'; + if (self->color_depth == 32) { + bufinfo->typecode = 'I'; + } else if (self->color_depth == 16) { + bufinfo->typecode = 'H'; + } else { + bufinfo->typecode = 'B'; + } bufinfo->len = self->framebuffer_len * sizeof(uint32_t); return 0; } diff --git a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h index abb75dc27825e..3a17d16b7af2d 100644 --- a/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h +++ b/ports/raspberrypi/common-hal/picodvi/Framebuffer_RP2350.h @@ -36,6 +36,7 @@ typedef struct { size_t dma_commands_len; // in words mp_uint_t width; mp_uint_t height; + mp_uint_t output_width; uint16_t pitch; // Number of words between rows. (May be more than a width's worth.) uint8_t color_depth; uint8_t dma_pixel_channel; diff --git a/ports/raspberrypi/link-rp2040.ld b/ports/raspberrypi/link-rp2040.ld index 5f2798623db15..6e7bd15a7b644 100644 --- a/ports/raspberrypi/link-rp2040.ld +++ b/ports/raspberrypi/link-rp2040.ld @@ -84,7 +84,7 @@ SECTIONS *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a: *interp.o *divider.o *tusb_fifo.o *mem_ops_aeabi.o *usbh.o) .text*) /* Allow everything in usbh.o except tuh_task_event_ready because we read it from core 1. */ - *usbh.o (.text.[_uphc]* .text.tuh_[cmvied]* .text.tuh_task_ext*) + *usbh.o (.text.[_uphc]* .text.tuh_[cmved]* .text.tuh_task_ext*) *(.fini) /* Pull all c'tors into .text */ *crtbegin.o(.ctors) diff --git a/ports/raspberrypi/link-rp2350.ld b/ports/raspberrypi/link-rp2350.ld index 19f7f05253680..a2cc62909e638 100644 --- a/ports/raspberrypi/link-rp2350.ld +++ b/ports/raspberrypi/link-rp2350.ld @@ -65,7 +65,7 @@ SECTIONS *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a: *interp.o *divider.o *tusb_fifo.o *mem_ops_aeabi.o *usbh.o *string0.o) .text*) /* Allow everything in usbh.o except tuh_task_event_ready because we read it from core 1. */ - *usbh.o (.text.[_uphc]* .text.tuh_[cmvied]* .text.tuh_task_ext*) + *usbh.o (.text.[_uphc]* .text.tuh_[cmved]* .text.tuh_task_ext*) *(.fini) /* Pull all c'tors into .text */ *crtbegin.o(.ctors)