1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-24 23:38:09 +00:00
trezor-firmware/embed/extmod/modtrezorui/display-stm32.h

312 lines
12 KiB
C
Raw Normal View History

2016-03-27 21:10:31 +00:00
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
2016-05-28 12:37:32 +00:00
* Licensed under TREZOR License
* see LICENSE file for details
2016-03-27 21:10:31 +00:00
*/
#include STM32_HAL_H
#ifndef DISPLAY_ILI9341V
2016-09-24 13:20:41 +00:00
#define DISPLAY_ILI9341V 0
#endif
#ifndef DISPLAY_ST7789V
2016-09-24 13:20:41 +00:00
#define DISPLAY_ST7789V 1
#endif
#ifndef DISPLAY_VSYNC
#define DISPLAY_VSYNC 1
#endif
2016-09-24 13:20:41 +00:00
// FSMC/FMC Bank 1 - NOR/PSRAM 1
#define DISPLAY_MEMORY_BASE 0x60000000
#define DISPLAY_MEMORY_PIN 16
2017-03-17 16:43:22 +00:00
#define CMD(X) (*((__IO uint8_t *)((uint32_t)(DISPLAY_MEMORY_BASE))) = (X))
#define DATA(X) (*((__IO uint8_t *)((uint32_t)(DISPLAY_MEMORY_BASE | (1 << DISPLAY_MEMORY_PIN)))) = (X))
2016-03-27 21:10:31 +00:00
2016-06-06 12:26:08 +00:00
static TIM_HandleTypeDef TIM1_Handle;
#define LED_PWM_TIM_PERIOD (10000)
static uint32_t timer1_get_source_freq() {
uint32_t source;
// TIM1 is on APB2
source = HAL_RCC_GetPCLK2Freq();
if ((uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3) != RCC_HCLK_DIV1) {
source *= 2;
}
return source;
}
2017-03-08 16:36:11 +00:00
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);
#endif
#if DISPLAY_ST7789V
DATA(RGB | MX | MY);
2017-03-08 16:36:11 +00:00
#endif
BUFFER_OFFSET_X = 0;
BUFFER_OFFSET_Y = MAX_DISPLAY_RESY - DISPLAY_RESY;
2017-03-08 16:36:11 +00:00
break;
case 90:
CMD(0x36);
#if DISPLAY_ILI9341V
DATA(BGR | MV | MY);
2017-03-08 16:36:11 +00:00
#endif
#if DISPLAY_ST7789V
DATA(RGB | MV | MY);
2017-03-08 16:36:11 +00:00
#endif
BUFFER_OFFSET_X = MAX_DISPLAY_RESY - DISPLAY_RESX;
BUFFER_OFFSET_Y = 0;
2017-03-08 16:36:11 +00:00
break;
case 180:
CMD(0x36);
#if DISPLAY_ILI9341V
DATA(BGR);
#endif
#if DISPLAY_ST7789V
DATA(RGB);
#endif
2017-03-08 16:36:11 +00:00
BUFFER_OFFSET_X = 0;
BUFFER_OFFSET_Y = 0;
break;
case 270:
CMD(0x36);
#if DISPLAY_ILI9341V
DATA(BGR | MV | MX);
2017-03-08 16:36:11 +00:00
#endif
#if DISPLAY_ST7789V
DATA(RGB | MV | MX);
#endif
2017-03-08 16:36:11 +00:00
BUFFER_OFFSET_X = 0;
BUFFER_OFFSET_Y = 0;
break;
}
}
void display_set_backlight(int val)
{
__HAL_TIM_SET_COMPARE(&TIM1_Handle, TIM_CHANNEL_1, LED_PWM_TIM_PERIOD * val / 255);
2017-03-08 16:36:11 +00:00
}
2017-09-08 01:04:23 +00:00
void display_pwm_init(void)
{
// enable PWM timer
TIM1_Handle.Instance = TIM1;
TIM1_Handle.Init.Period = LED_PWM_TIM_PERIOD - 1;
TIM1_Handle.Init.Prescaler = timer1_get_source_freq() / 1000000 - 1; // TIM runs at 1MHz
TIM1_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TIM1_Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
TIM1_Handle.Init.RepetitionCounter = 0;
HAL_TIM_PWM_Init(&TIM1_Handle);
TIM_OC_InitTypeDef TIM_OC_InitStructure;
TIM_OC_InitStructure.Pulse = 0;
TIM_OC_InitStructure.OCMode = TIM_OCMODE_PWM2;
TIM_OC_InitStructure.OCPolarity = TIM_OCPOLARITY_HIGH;
TIM_OC_InitStructure.OCFastMode = TIM_OCFAST_DISABLE;
TIM_OC_InitStructure.OCNPolarity = TIM_OCNPOLARITY_HIGH;
TIM_OC_InitStructure.OCIdleState = TIM_OCIDLESTATE_SET;
TIM_OC_InitStructure.OCNIdleState = TIM_OCNIDLESTATE_SET;
HAL_TIM_PWM_ConfigChannel(&TIM1_Handle, &TIM_OC_InitStructure, TIM_CHANNEL_1);
2017-10-06 09:11:59 +00:00
display_backlight(0);
2017-09-08 01:04:23 +00:00
HAL_TIM_PWM_Start(&TIM1_Handle, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&TIM1_Handle, TIM_CHANNEL_1);
}
int display_init(void) {
2017-03-08 16:36:11 +00:00
// init peripherials
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_TIM1_CLK_ENABLE();
__HAL_RCC_FMC_CLK_ENABLE();
2016-03-27 21:10:31 +00:00
2016-09-28 11:37:05 +00:00
GPIO_InitTypeDef GPIO_InitStructure;
// LCD_RST/PC14
GPIO_InitStructure.Pin = GPIO_PIN_14;
2016-03-27 21:10:31 +00:00
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
2016-09-28 11:37:05 +00:00
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, GPIO_PIN_SET);
2016-03-27 21:10:31 +00:00
// LCD_FMARK/PD12 (tearing effect)
GPIO_InitStructure.Pin = GPIO_PIN_12;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
// LCD_PWM/PA7 (backlight control)
2016-03-27 21:10:31 +00:00
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
2016-06-06 12:26:08 +00:00
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
2016-06-06 12:26:08 +00:00
GPIO_InitStructure.Alternate = GPIO_AF1_TIM1;
GPIO_InitStructure.Pin = GPIO_PIN_7;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
2016-06-06 12:26:08 +00:00
GPIO_InitStructure.Alternate = GPIO_AF12_FMC;
2016-03-27 21:10:31 +00:00
// LCD_CS/PD7 LCD_RS/PD11 LCD_RD/PD4 LCD_WR/PD5
GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_11 | GPIO_PIN_4 | GPIO_PIN_5;
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
// LCD_D0/PD14 LCD_D1/PD15 LCD_D2/PD0 LCD_D3/PD1
GPIO_InitStructure.Pin = GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_0 | GPIO_PIN_1;
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
// LCD_D4/PE7 LCD_D5/PE8 LCD_D6/PE9 LCD_D7/PE10
GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
2017-09-08 01:04:23 +00:00
display_pwm_init();
2016-06-06 12:26:08 +00:00
2016-03-27 21:10:31 +00:00
// timing values from:
// http://ele-tech.com/html/it-is-developed-that-embedded-stm32-fsmc-interface-drives-tft-lcd-to-be-designed.html
FMC_NORSRAM_InitTypeDef FMC_NORSRAMInitStructure;
FMC_NORSRAM_TimingTypeDef FMC_NORSRAMTimingStructure;
FMC_NORSRAMTimingStructure.AddressSetupTime = 2;
FMC_NORSRAMTimingStructure.AddressHoldTime = 0;
FMC_NORSRAMTimingStructure.DataSetupTime = 5;
FMC_NORSRAMTimingStructure.BusTurnAroundDuration = 0;
FMC_NORSRAMTimingStructure.CLKDivision = 0;
FMC_NORSRAMTimingStructure.DataLatency = 0;
FMC_NORSRAMTimingStructure.AccessMode = FMC_ACCESS_MODE_B;
FMC_NORSRAMInitStructure.NSBank = FMC_NORSRAM_BANK1;
FMC_NORSRAMInitStructure.DataAddressMux = FMC_DATA_ADDRESS_MUX_DISABLE;
FMC_NORSRAMInitStructure.MemoryType = FMC_MEMORY_TYPE_NOR;
FMC_NORSRAMInitStructure.MemoryDataWidth = FMC_NORSRAM_MEM_BUS_WIDTH_8;
FMC_NORSRAMInitStructure.BurstAccessMode = FMC_BURST_ACCESS_MODE_DISABLE;
FMC_NORSRAMInitStructure.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW;
FMC_NORSRAMInitStructure.WrapMode = FMC_WRAP_MODE_DISABLE;
FMC_NORSRAMInitStructure.WaitSignalActive = FMC_WAIT_TIMING_BEFORE_WS;
FMC_NORSRAMInitStructure.WriteOperation = FMC_WRITE_OPERATION_ENABLE;
FMC_NORSRAMInitStructure.WaitSignal = FMC_WAIT_SIGNAL_DISABLE;
FMC_NORSRAMInitStructure.ExtendedMode = FMC_EXTENDED_MODE_DISABLE;
FMC_NORSRAMInitStructure.AsynchronousWait = FMC_ASYNCHRONOUS_WAIT_DISABLE;
FMC_NORSRAMInitStructure.WriteBurst = FMC_WRITE_BURST_DISABLE;
FMC_NORSRAMInitStructure.PageSize = FMC_PAGE_SIZE_NONE;
FMC_NORSRAM_Init(FMC_NORSRAM_DEVICE, &FMC_NORSRAMInitStructure);
FMC_NORSRAM_Timing_Init(FMC_NORSRAM_DEVICE, &FMC_NORSRAMTimingStructure, FMC_NORSRAMInitStructure.NSBank);
// FMC_NORSRAM_Extended_Timing_Init(FMC_NORSRAM_EXTENDED_DEVICE, &FMC_NORSRAMTimingStructure, FMC_NORSRAMInitStructure.NSBank, FMC_NORSRAMInitStructure.ExtendedMode);
__FMC_NORSRAM_ENABLE(FMC_NORSRAM_DEVICE, FMC_NORSRAMInitStructure.NSBank);
2016-03-27 21:10:31 +00:00
2016-09-28 11:37:05 +00:00
// 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);
2016-10-05 22:23:57 +00:00
#if DISPLAY_ILI9341V
2017-03-08 16:36:11 +00:00
CMD(0x01); // software reset
2016-03-27 21:10:31 +00:00
HAL_Delay(20);
2017-03-08 16:36:11 +00:00
CMD(0x28); // display off
CMD(0xCF); DATA(0x00); DATA(0xC1); DATA(0x30);
2017-03-08 16:36:11 +00:00
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);
2016-03-27 21:10:31 +00:00
CMD(0xF7); DATA(0x20);
2017-03-08 16:36:11 +00:00
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(0x20); // don't invert colors
2016-09-24 13:20:41 +00:00
#endif
2016-09-28 12:33:38 +00:00
#if DISPLAY_ST7789V
2017-03-08 16:36:11 +00:00
CMD(0x01); // software reset
2016-10-05 22:23:57 +00:00
HAL_Delay(20);
2017-03-08 16:36:11 +00:00
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)
2017-03-08 16:36:11 +00:00
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
2016-09-24 13:20:41 +00:00
#endif
2017-09-08 01:04:23 +00:00
display_backlight(0);
2016-09-28 15:13:46 +00:00
display_clear();
2017-09-08 01:04:23 +00:00
display_orientation(0);
2016-03-27 21:10:31 +00:00
display_unsleep();
return 0;
2016-03-27 21:10:31 +00:00
}
2017-09-08 01:04:23 +00:00
static void display_set_window_raw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
2016-09-24 13:20:41 +00:00
#if DISPLAY_ILI9341V || DISPLAY_ST7789V
CMD(0x2A); DATA(x0 >> 8); DATA(x0 & 0xFF); DATA(x1 >> 8); DATA(x1 & 0xFF); // column addr set
CMD(0x2B); DATA(y0 >> 8); DATA(y0 & 0xFF); DATA(y1 >> 8); DATA(y1 & 0xFF); // row addr set
2016-03-27 21:10:31 +00:00
CMD(0x2C);
2016-09-24 13:20:41 +00:00
#endif
2016-03-27 21:10:31 +00:00
}
2017-09-08 01:04:23 +00:00
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;
display_set_window_raw(x0, y0, x1, y1);
}
2016-10-03 09:52:19 +00:00
void display_refresh(void) {
#if DISPLAY_VSYNC
// synchronize with the panel synchronization signal in order to avoid visual tearing effects
while (GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_12)) { }
while (GPIO_PIN_SET == HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_12)) { }
#endif
2016-03-27 21:10:31 +00:00
}
2017-03-17 13:14:00 +00:00
void display_save(const char *filename)
{
}