44#include <driver/gpio.h>
55#include <driver/ledc.h>
66
7- #if defined(RG_SCREEN_ROTATE ) && RG_SCREEN_ROTATE != 0
8- #error "RG_SCREEN_ROTATE doesn't do anything on this driver, you have to use the 0x36 command during init!"
9- #endif
10-
117static spi_device_handle_t spi_dev ;
128static QueueHandle_t spi_transactions ;
139static QueueHandle_t spi_buffers ;
@@ -16,6 +12,14 @@ static QueueHandle_t spi_buffers;
1612#define SPI_BUFFER_COUNT (5)
1713#define SPI_BUFFER_LENGTH (LCD_BUFFER_LENGTH * 2)
1814
15+ #define ILI9341_CMD (cmd , data ...) \
16+ { \
17+ const uint8_t c = cmd, x[] = {data}; \
18+ spi_queue_transaction(&c, 1, 0); \
19+ if (sizeof(x)) \
20+ spi_queue_transaction(&x, sizeof(x), 1); \
21+ }
22+
1923static inline uint16_t * spi_take_buffer (void )
2024{
2125 uint16_t * buffer ;
@@ -141,24 +145,13 @@ static void spi_deinit(void)
141145 RG_LOGE ("Failed to properly terminate SPI driver!" );
142146}
143147
144- #define ILI9341_CMD (cmd , data ...) \
145- { \
146- const uint8_t c = cmd, x[] = {data}; \
147- spi_queue_transaction(&c, 1, 0); \
148- if (sizeof(x)) \
149- spi_queue_transaction(&x, sizeof(x), 1); \
150- }
151-
152148static void lcd_set_backlight (float percent )
153149{
154150 float level = RG_MIN (RG_MAX (percent / 100.f , 0 ), 1.f );
155151 int error_code = 0 ;
156152
157153#if defined(RG_GPIO_LCD_BCKL )
158154 error_code = ledc_set_fade_time_and_start (LEDC_LOW_SPEED_MODE , LEDC_CHANNEL_0 , 0x1FFF * level , 50 , 0 );
159- #elif defined(RG_TARGET_QTPY_GAMER )
160- rg_i2c_gpio_set_direction (AW_TFT_BACKLIGHT , RG_GPIO_ANALOG_OUTPUT );
161- rg_i2c_gpio_set_level (AW_TFT_BACKLIGHT , level * 255 );
162155#endif
163156
164157 if (error_code )
@@ -204,17 +197,20 @@ static void lcd_init(void)
204197#ifdef RG_GPIO_LCD_BCKL
205198 // Initialize backlight at 0% to avoid the lcd reset flash
206199 ledc_timer_config (& (ledc_timer_config_t ){
207- .duty_resolution = LEDC_TIMER_13_BIT ,
208- .freq_hz = 5000 ,
209200 .speed_mode = LEDC_LOW_SPEED_MODE ,
201+ .duty_resolution = LEDC_TIMER_13_BIT ,
210202 .timer_num = LEDC_TIMER_0 ,
203+ .freq_hz = 5000 ,
211204 });
212205 ledc_channel_config (& (ledc_channel_config_t ){
213- .channel = LEDC_CHANNEL_0 ,
214- .duty = 0 ,
215206 .gpio_num = RG_GPIO_LCD_BCKL ,
216207 .speed_mode = LEDC_LOW_SPEED_MODE ,
208+ .channel = LEDC_CHANNEL_0 ,
217209 .timer_sel = LEDC_TIMER_0 ,
210+ .duty = 0 ,
211+ #ifdef RG_GPIO_LCD_BCKL_INVERT
212+ .flags .output_invert = 1 ,
213+ #endif
218214 });
219215 ledc_fade_func_install (0 );
220216#endif
@@ -231,25 +227,24 @@ static void lcd_init(void)
231227 rg_usleep (100 * 1000 );
232228 gpio_set_level (RG_GPIO_LCD_RST , 1 );
233229 rg_usleep (10 * 1000 );
234- #elif defined(RG_TARGET_QTPY_GAMER )
235- rg_i2c_gpio_set_direction (AW_TFT_RESET , RG_GPIO_OUTPUT );
236- rg_i2c_gpio_set_level (AW_TFT_RESET , 0 );
237- rg_usleep (100 * 1000 );
238- rg_i2c_gpio_set_level (AW_TFT_RESET , 1 );
239- rg_usleep (10 * 1000 );
240230#endif
241231
242- ILI9341_CMD (0x01 ); // Reset
243- rg_usleep (5 * 1000 ); // Wait 5ms after reset
244- ILI9341_CMD (0x3A , 0X05 ); // Pixel Format Set RGB565
245- #ifdef RG_SCREEN_INIT
246- RG_SCREEN_INIT ();
247- #else
248- #warning "LCD init sequence is not defined for this device!"
249- #endif
250- ILI9341_CMD (0x11 ); // Exit Sleep
251- rg_usleep (10 * 1000 );// Wait 10ms after sleep out
252- ILI9341_CMD (0x29 ); // Display on
232+ ILI9341_CMD (0x01 ); // Reset
233+ rg_usleep (5 * 1000 ); // Wait 5ms after reset
234+ ILI9341_CMD (0x3A , 0X55 ); // COLMOD (Pixel Format Set RGB565 65k)
235+ #if defined(RG_SCREEN_ROTATION ) && defined(RG_SCREEN_RGB_BGR )
236+ // The rotation is designed so that the user can simply try all values 0-7 to find what works.
237+ // It's simpler than trying to explain the MADCTL register bits, combined with hardware variations...
238+ ILI9341_CMD (0x36 , (RG_SCREEN_RGB_BGR ? 0x08 : 0x00 ) | (RG_SCREEN_ROTATION << 5 )); // MADCTL (0x08=BGR, 0x20=MV, 0x40=MX, 0x80=MY)
239+ #endif
240+ #ifdef RG_SCREEN_INIT
241+ RG_SCREEN_INIT ();
242+ #else
243+ #warning "LCD init sequence is not defined for this device!"
244+ #endif
245+ ILI9341_CMD (0x11 ); // Exit Sleep
246+ rg_usleep (10 * 1000 ); // Wait 10ms after sleep out
247+ ILI9341_CMD (0x29 ); // Display on
253248}
254249
255250static void lcd_deinit (void )
0 commit comments