diff --git a/micropython/extmod/modtrezorui/display-stmhal.h b/micropython/extmod/modtrezorui/display-stmhal.h index b4c7f74e66..144c051f83 100644 --- a/micropython/extmod/modtrezorui/display-stmhal.h +++ b/micropython/extmod/modtrezorui/display-stmhal.h @@ -7,15 +7,12 @@ #include STM32_HAL_H - #define DISPLAY_ILI9341V 0 #define DISPLAY_ST7789V 1 #define CMD(X) (*((__IO uint8_t *)((uint32_t)(0x60000000))) = (X)) #define DATA(X) (*((__IO uint8_t *)((uint32_t)(0x60000000 | 0x10000))) = (X)) -void DATAS(const void *bytes, int len); - static TIM_HandleTypeDef TIM1_Handle; #define LED_PWM_TIM_PERIOD (10000) @@ -30,7 +27,95 @@ static uint32_t timer1_get_source_freq() { return source; } -void display_sram_init(void) { +static void __attribute__((unused)) display_sleep(void) { +#if DISPLAY_ILI9341V || DISPLAY_ST7789V + CMD(0x28); // display off + HAL_Delay(20); + CMD(0x10); // enter sleep +#endif +} + +static void display_unsleep(void) { +#if DISPLAY_ILI9341V || DISPLAY_ST7789V + CMD(0x11); // exit sleep + HAL_Delay(20); + CMD(0x29); // display on +#endif +} + +static uint16_t BUFFER_OFFSET_X = 0, BUFFER_OFFSET_Y = 0; + +void display_set_orientation(int degrees) +{ +#define RGB (0) +#define BGR (1 << 3) +#define MV (1 << 5) +#define MX (1 << 6) +#define MY (1 << 7) + // memory access control + switch (degrees) { + case 0: + CMD(0x36); +#if DISPLAY_ILI9341V + DATA(BGR | MX | MY); + BUFFER_OFFSET_X = 0; + BUFFER_OFFSET_Y = 80; +#endif +#if DISPLAY_ST7789V + DATA(RGB | MX | MY ); + BUFFER_OFFSET_X = 0; + BUFFER_OFFSET_Y = 80; +#endif + break; + case 90: + CMD(0x36); +#if DISPLAY_ILI9341V + DATA(BGR | MV | MX); + BUFFER_OFFSET_X = 0; + BUFFER_OFFSET_Y = 0; +#endif +#if DISPLAY_ST7789V + DATA(RGB | MV | MY ); + BUFFER_OFFSET_X = 80; + BUFFER_OFFSET_Y = 0; +#endif + break; + case 180: + CMD(0x36); +#if DISPLAY_ILI9341V + DATA(BGR); + BUFFER_OFFSET_X = 0; + BUFFER_OFFSET_Y = 0; +#endif +#if DISPLAY_ST7789V + DATA(RGB); + BUFFER_OFFSET_X = 0; + BUFFER_OFFSET_Y = 0; +#endif + break; + case 270: + CMD(0x36); +#if DISPLAY_ILI9341V + DATA(BGR | MV | MY); + BUFFER_OFFSET_X = 80; + BUFFER_OFFSET_Y = 0; +#endif +#if DISPLAY_ST7789V + DATA(RGB | MV | MX); + BUFFER_OFFSET_X = 0; + BUFFER_OFFSET_Y = 0; +#endif + break; + } +} + +void display_set_backlight(int val) +{ + __HAL_TIM_SetCompare(&TIM1_Handle, TIM_CHANNEL_1, LED_PWM_TIM_PERIOD * BACKLIGHT / 255); +} + +void display_init(void) { + // init peripherials __GPIOE_CLK_ENABLE(); __TIM1_CLK_ENABLE(); __FSMC_CLK_ENABLE(); @@ -71,7 +156,6 @@ void display_sram_init(void) { GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10; HAL_GPIO_Init(GPIOE, &GPIO_InitStructure); - // enable PWM timer TIM1_Handle.Instance = TIM1; TIM1_Handle.Init.Period = LED_PWM_TIM_PERIOD - 1; @@ -126,85 +210,68 @@ void display_sram_init(void) { FSMC_NORSRAM_Timing_Init(FSMC_NORSRAM_DEVICE, &FSMC_NORSRAMTimingStructure, FSMC_NORSRAMInitStructure.NSBank); // FSMC_NORSRAM_Extended_Timing_Init(FSMC_NORSRAM_EXTENDED_DEVICE, &FSMC_NORSRAMTimingStructure, FSMC_NORSRAMInitStructure.NSBank, FSMC_NORSRAMInitStructure.ExtendedMode); __FSMC_NORSRAM_ENABLE(FSMC_NORSRAM_DEVICE, FSMC_NORSRAMInitStructure.NSBank); -} -static void __attribute__((unused)) display_sleep(void) { -#if DISPLAY_ILI9341V || DISPLAY_ST7789V - CMD(0x28); // display off - HAL_Delay(20); - CMD(0x10); // enter sleep -#endif -} - -static void display_unsleep(void) { -#if DISPLAY_ILI9341V || DISPLAY_ST7789V - CMD(0x11); // exit sleep - HAL_Delay(20); - CMD(0x29); // display on -#endif -} - -void display_init(void) { - display_sram_init(); // hardware reset HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, GPIO_PIN_RESET); HAL_Delay(20); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, GPIO_PIN_SET); HAL_Delay(5); #if DISPLAY_ILI9341V - CMD(0x01); // software reset + CMD(0x01); // software reset HAL_Delay(20); - CMD(0x28); // display off - CMD(0xCF); DATAS("\x00\xC1\x30", 3); - CMD(0xED); DATAS("\x64\x03\x12\x81", 4); - CMD(0xE8); DATAS("\x85\x10\x7A", 3); - CMD(0xCB); DATAS("\x39\x2C\x00\x34\x02", 5); + CMD(0x28); // display off + CMD(0xCF); DATA(0x00); DATA(0xC1); DATA(0x30; + CMD(0xED); DATA(0x64); DATA(0x03); DATA(0x12); DATA(0x81); + CMD(0xE8); DATA(0x85); DATA(0x10); DATA(0x7A); + CMD(0xCB); DATA(0x39); DATA(0x2C); DATA(0x00); DATA(0x34); DATA(0x02); CMD(0xF7); DATA(0x20); - CMD(0xEA); DATAS("\x00\x00", 2); - CMD(0xC0); DATA(0x23); // power control VRH[5:0] - CMD(0xC1); DATA(0x12); // power control SAP[2:0] BT[3:0] - CMD(0xC5); DATAS("\x60\x44", 2); // vcm control 1 - CMD(0xC7); DATA(0x8A); // vcm control 2 - CMD(0x3A); DATA(0x55); // memory access control (16-bit 565) - CMD(0xB1); DATAS("\x00\x18", 2); // framerate - CMD(0xB6); DATAS("\x0A\xA2", 2); // display function control - CMD(0xF6); DATAS("\x01\x30\x00", 3); // interface control - CMD(0xF2); DATA(0x00); // 3 gamma func disable - CMD(0x26); DATA(0x01); // gamma func enable - CMD(0xE0); DATAS("\x0F\x2F\x2C\x0B\x0F\x09\x56\xD9\x4A\x0B\x14\x05\x0C\x06\x00", 15); // gamma curve 1 - CMD(0xE1); DATAS("\x00\x10\x13\x04\x10\x06\x25\x26\x3B\x04\x0B\x0A\x33\x39\x0F", 15); // gamma curve 2 - CMD(0x21); // invert colors + CMD(0xEA); DATA(0x00); DATA(0x00); + CMD(0xC0); DATA(0x23); // power control VRH[5:0] + CMD(0xC1); DATA(0x12); // power control SAP[2:0] BT[3:0] + CMD(0xC5); DATA(0x60); DATA(0x44); // vcm control 1 + CMD(0xC7); DATA(0x8A); // vcm control 2 + CMD(0x3A); DATA(0x55); // memory access control (16-bit 565) + CMD(0xB1); DATA(0x00); DATA(0x18); // framerate + CMD(0xB6); DATA(0x0A); DATA(0xA2); // display function control + CMD(0xF6); DATA(0x01); DATA(0x30); DATA(0x00); // interface control + CMD(0xF2); DATA(0x00); // 3 gamma func disable + CMD(0x26); DATA(0x01); // gamma func enable + // gamma curve 1 + CMD(0xE0); DATA(0x0F); DATA(0x2F); DATA(0x2C); DATA(0x0B); DATA(0x0F); DATA(0x09); DATA(0x56); DATA(0xD9); DATA(0x4A); DATA(0x0B); DATA(0x14); DATA(0x05); DATA(0x0C); DATA(0x06); DATA(0x00); + // gamma curve 2 + CMD(0xE1); DATA(0x00); DATA(0x10); DATA(0x13); DATA(0x04); DATA(0x10); DATA(0x06); DATA(0x25); DATA(0x26); DATA(0x3B); DATA(0x04); DATA(0x0B); DATA(0x0A); DATA(0x33); DATA(0x39); DATA(0x0F); + CMD(0x21); // invert colors #endif #if DISPLAY_ST7789V - CMD(0x01); // software reset + CMD(0x01); // software reset HAL_Delay(20); - CMD(0x28); // display off - CMD(0x3A); DATA(0x55); // COLMOD: Interface Pixel format - CMD(0xB2); DATAS("\x08\x08\x00\x22\x22", 5); // PORCTRK: Porch setting - CMD(0xB7); DATA(0x35); // GCTRL: Gate Control - CMD(0xC2); DATAS("\x01\xFF", 2); // VDVVRHEN: VDV and VRH Command Enable - CMD(0xC3); DATA(0x0B); // VRHS: VRH Set - CMD(0xC4); DATA(0x20); // VDVS: VDV Set - CMD(0xBB); DATA(0x20); // VCOMS: VCOM setting - CMD(0xC5); DATA(0x20); // VCMOFSET + CMD(0x28); // display off + CMD(0x3A); DATA(0x55); // COLMOD: Interface Pixel format + CMD(0xB2); DATA(0x08); DATA(0x08); DATA(0x00); DATA(0x22); DATA(0x22); // PORCTRK: Porch setting + CMD(0xB7); DATA(0x35); // GCTRL: Gate Control + CMD(0xC2); DATA(0x01); DATA(0xFF); // VDVVRHEN: VDV and VRH Command Enable + CMD(0xC3); DATA(0x0B); // VRHS: VRH Set + CMD(0xC4); DATA(0x20); // VDVS: VDV Set + CMD(0xBB); DATA(0x20); // VCOMS: VCOM setting + CMD(0xC5); DATA(0x20); // VCMOFSET // CMD(0xC6); DATA(0x0F); // FRCTRL2: Framerate Control (60 Hz) - CMD(0x35); DATA(0x00); // TEON: Tearing Effect Line On - CMD(0xD0); DATAS("\xA4\xA1", 2); // PWCTRL1: Power Control 1 - // CMD(0xE0); DATAS("\x70\x2C\x2E\x15\x10\x09\x48\x33\x53\x0B\x19\x18\x20\x25", 14); // gamma curve 1 - // CMD(0xE1); DATAS("\x70\x2C\x2E\x15\x10\x09\x48\x33\x53\x0B\x19\x18\x20\x25", 14); // gamma curve 2 - CMD(0x26); DATA(0x01); // gamma func (gc3) enable - CMD(0x20); // don't invert colors + CMD(0x35); DATA(0x00); // TEON: Tearing Effect Line On + CMD(0xD0); DATA(0xA4); DATA(0xA1); // PWCTRL1: Power Control 1 + // gamma curve 1 + // CMD(0xE0); DATA(0x70); DATA(0x2C); DATA(0x2E); DATA(0x15); DATA(0x10); DATA(0x09); DATA(0x48); DATA(0x33); DATA(0x53); DATA(0x0B); DATA(0x19); DATA(0x18); DATA(0x20); DATA(0x25); + // gamma curve 2 + // CMD(0xE1); DATA(0x70); DATA(0x2C); DATA(0x2E); DATA(0x15); DATA(0x10); DATA(0x09); DATA(0x48); DATA(0x33); DATA(0x53); DATA(0x0B); DATA(0x19); DATA(0x18); DATA(0x20); DATA(0x25); + CMD(0x26); DATA(0x01); // gamma func (gc3) enable + CMD(0x20); // don't invert colors #endif - display_orientation(0); - display_backlight(0); + display_set_orientation(0); + display_set_backlight(0); // clear buffer display_clear(); display_unsleep(); } -static uint16_t BUFFER_OFFSET_X = 0, BUFFER_OFFSET_Y = 0; - -void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { +static void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { x0 += BUFFER_OFFSET_X; y0 += BUFFER_OFFSET_Y; x1 += BUFFER_OFFSET_X; y1 += BUFFER_OFFSET_Y; #if DISPLAY_ILI9341V || DISPLAY_ST7789V @@ -218,85 +285,3 @@ void display_refresh(void) { while (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_12)) { } while (GPIO_PIN_SET == HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_12)) { } } - -int display_orientation(int degrees) -{ -#define RGB (0) -#define BGR (1 << 3) -#define MV (1 << 5) -#define MX (1 << 6) -#define MY (1 << 7) - // memory access control - switch (degrees) { - case 0: - CMD(0x36); -#if DISPLAY_ILI9341V - DATA(BGR | MX | MY); - BUFFER_OFFSET_X = 0; - BUFFER_OFFSET_Y = 80; -#endif -#if DISPLAY_ST7789V - DATA(RGB | MX | MY ); - BUFFER_OFFSET_X = 0; - BUFFER_OFFSET_Y = 80; -#endif - ORIENTATION = 0; - break; - case 90: - CMD(0x36); -#if DISPLAY_ILI9341V - DATA(BGR | MV | MX); - BUFFER_OFFSET_X = 0; - BUFFER_OFFSET_Y = 0; -#endif -#if DISPLAY_ST7789V - DATA(RGB | MV | MY ); - BUFFER_OFFSET_X = 80; - BUFFER_OFFSET_Y = 0; -#endif - ORIENTATION = 90; - break; - case 180: - CMD(0x36); -#if DISPLAY_ILI9341V - DATA(BGR); - BUFFER_OFFSET_X = 0; - BUFFER_OFFSET_Y = 0; -#endif -#if DISPLAY_ST7789V - DATA(RGB); - BUFFER_OFFSET_X = 0; - BUFFER_OFFSET_Y = 0; -#endif - ORIENTATION = 180; - break; - case 270: - CMD(0x36); -#if DISPLAY_ILI9341V - DATA(BGR | MV | MY); - BUFFER_OFFSET_X = 80; - BUFFER_OFFSET_Y = 0; -#endif -#if DISPLAY_ST7789V - DATA(RGB | MV | MX); - BUFFER_OFFSET_X = 0; - BUFFER_OFFSET_Y = 0; -#endif - ORIENTATION = 270; - break; - } - return ORIENTATION; -} - -int display_backlight(int val) -{ - if (val >= 0 && val <= 255) { - BACKLIGHT = val; - __HAL_TIM_SetCompare(&TIM1_Handle, TIM_CHANNEL_1, LED_PWM_TIM_PERIOD * BACKLIGHT / 255); - } - return BACKLIGHT; -} - -void display_save(const char *filename) -{ -} diff --git a/micropython/extmod/modtrezorui/display-unix-null.h b/micropython/extmod/modtrezorui/display-unix-null.h index daed75413d..b4db2c5570 100644 --- a/micropython/extmod/modtrezorui/display-unix-null.h +++ b/micropython/extmod/modtrezorui/display-unix-null.h @@ -17,7 +17,7 @@ void display_init(void) { } -void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) +static void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { } @@ -25,24 +25,10 @@ void display_refresh(void) { } -int display_orientation(int degrees) +static void display_set_orientation(int degrees) { - if (degrees != ORIENTATION) { - if (degrees == 0 || degrees == 90 || degrees == 180 || degrees == 270) { - ORIENTATION = degrees; - } - } - return ORIENTATION; } -int display_backlight(int val) -{ - if (BACKLIGHT != val && val >= 0 && val <= 255) { - BACKLIGHT = val; - } - return BACKLIGHT; -} - -void display_save(const char *filename) +static void display_set_backlight(int val) { } diff --git a/micropython/extmod/modtrezorui/display-unix-sdl.h b/micropython/extmod/modtrezorui/display-unix-sdl.h index aa5c303fb2..4af7548504 100644 --- a/micropython/extmod/modtrezorui/display-unix-sdl.h +++ b/micropython/extmod/modtrezorui/display-unix-sdl.h @@ -100,7 +100,7 @@ void display_init(void) SDL_SetTextureAlphaMod(TEXTURE, 0); } -void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) +static void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { SX = x0; SY = y0; EX = x1; EY = y1; @@ -117,26 +117,11 @@ void display_refresh(void) SDL_RenderPresent(RENDERER); } -int display_orientation(int degrees) +static void display_set_orientation(int degrees) { - if (degrees != ORIENTATION) { - if (degrees == 0 || degrees == 90 || degrees == 180 || degrees == 270) { - ORIENTATION = degrees; - } - } - return ORIENTATION; } -int display_backlight(int val) +static void display_set_backlight(int val) { - if (BACKLIGHT != val && val >= 0 && val <= 255) { - BACKLIGHT = val; - SDL_SetRenderDrawColor(RENDERER, BACKLIGHT, BACKLIGHT, BACKLIGHT, 255); - } - return BACKLIGHT; -} - -void display_save(const char *filename) -{ - IMG_SavePNG(BUFFER, filename); + SDL_SetRenderDrawColor(RENDERER, val, val, val, 255); } diff --git a/micropython/extmod/modtrezorui/display.c b/micropython/extmod/modtrezorui/display.c index 77322a54ab..c1d420f611 100644 --- a/micropython/extmod/modtrezorui/display.c +++ b/micropython/extmod/modtrezorui/display.c @@ -32,15 +32,6 @@ static int OFFSET[2] = {0, 0}; // common display functions -void DATAS(const void *bytes, int len) -{ - const uint8_t *c = (const uint8_t *)bytes; - while (len-- > 0) { - DATA(*c); - c++; - } -} - static void set_color_table(uint16_t colortable[16], uint16_t fgcolor, uint16_t bgcolor) { uint8_t cr, cg, cb; @@ -472,16 +463,6 @@ void display_loader(uint16_t progress, int yoffset, uint16_t fgcolor, uint16_t b } } -void display_raw(uint8_t reg, const uint8_t *data, int datalen) -{ - if (reg) { - CMD(reg); - } - if (data && datalen > 0) { - DATAS(data, datalen); - } -} - int *display_offset(int xy[2]) { if (xy) { @@ -490,3 +471,23 @@ int *display_offset(int xy[2]) } return OFFSET; } + +int display_orientation(int degrees) +{ + if (degrees != ORIENTATION) { + if (degrees == 0 || degrees == 90 || degrees == 180 || degrees == 270) { + ORIENTATION = degrees; + display_set_orientation(degrees); + } + } + return ORIENTATION; +} + +int display_backlight(int val) +{ + if (BACKLIGHT != val && val >= 0 && val <= 255) { + BACKLIGHT = val; + display_set_backlight(val); + } + return BACKLIGHT; +} diff --git a/micropython/extmod/modtrezorui/display.h b/micropython/extmod/modtrezorui/display.h index d0cfa856b9..75356b19eb 100644 --- a/micropython/extmod/modtrezorui/display.h +++ b/micropython/extmod/modtrezorui/display.h @@ -24,8 +24,6 @@ int display_orientation(int degrees); int display_backlight(int val); int *display_offset(int xy[2]); -void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); - void display_clear(void); void display_refresh(void); @@ -41,7 +39,4 @@ void display_text_center(int x, int y, const char *text, int textlen, uint8_t fo void display_text_right(int x, int y, const char *text, int textlen, uint8_t font, uint16_t fgcolor, uint16_t bgcolor); int display_text_width(const char *text, int textlen, uint8_t font); -void display_raw(uint8_t reg, const uint8_t *data, int datalen); -void display_save(const char *filename); - #endif diff --git a/micropython/extmod/modtrezorui/modtrezorui-display.h b/micropython/extmod/modtrezorui/modtrezorui-display.h index 1a1aad9755..b1b1cfbea5 100644 --- a/micropython/extmod/modtrezorui/modtrezorui-display.h +++ b/micropython/extmod/modtrezorui/modtrezorui-display.h @@ -352,37 +352,6 @@ STATIC mp_obj_t mod_TrezorUi_Display_offset(size_t n_args, const mp_obj_t *args) } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_offset_obj, 1, 2, mod_TrezorUi_Display_offset); -/// def trezor.ui.display.raw(reg: int, data: bytes=None) -> None: -/// ''' -/// Performs a raw command on the display. Read the datasheet to learn more. -/// ''' -STATIC mp_obj_t mod_TrezorUi_Display_raw(size_t n_args, const mp_obj_t *args) { - mp_int_t r = mp_obj_get_int(args[1]); - if (n_args > 2){ - mp_buffer_info_t raw; - mp_get_buffer_raise(args[2], &raw, MP_BUFFER_READ); - display_raw(r, raw.buf, raw.len); - } else { - display_raw(r, NULL, 0); - } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_raw_obj, 2, 3, mod_TrezorUi_Display_raw); - -/// def trezor.ui.display.save(filename: string) -> None: -/// ''' -/// Saves current display contents to file filename. -/// ''' -STATIC mp_obj_t mod_TrezorUi_Display_save(mp_obj_t self, mp_obj_t filename) { - mp_buffer_info_t fn; - mp_get_buffer_raise(filename, &fn, MP_BUFFER_READ); - if (fn.len > 0) { - display_save(fn.buf); - } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorUi_Display_save_obj, mod_TrezorUi_Display_save); - STATIC const mp_rom_map_elem_t mod_TrezorUi_Display_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&mod_TrezorUi_Display_clear_obj) }, { MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&mod_TrezorUi_Display_refresh_obj) }, @@ -400,8 +369,6 @@ STATIC const mp_rom_map_elem_t mod_TrezorUi_Display_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_orientation), MP_ROM_PTR(&mod_TrezorUi_Display_orientation_obj) }, { MP_ROM_QSTR(MP_QSTR_backlight), MP_ROM_PTR(&mod_TrezorUi_Display_backlight_obj) }, { MP_ROM_QSTR(MP_QSTR_offset), MP_ROM_PTR(&mod_TrezorUi_Display_offset_obj) }, - { MP_ROM_QSTR(MP_QSTR_raw), MP_ROM_PTR(&mod_TrezorUi_Display_raw_obj) }, - { MP_ROM_QSTR(MP_QSTR_save), MP_ROM_PTR(&mod_TrezorUi_Display_save_obj) }, { MP_ROM_QSTR(MP_QSTR_FONT_MONO), MP_OBJ_NEW_SMALL_INT(FONT_MONO) }, { MP_ROM_QSTR(MP_QSTR_FONT_NORMAL), MP_OBJ_NEW_SMALL_INT(FONT_NORMAL) }, { MP_ROM_QSTR(MP_QSTR_FONT_BOLD), MP_OBJ_NEW_SMALL_INT(FONT_BOLD) },