From 62c3f9c1e32d82430fb0e44ee197e0ceb4ccd745 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Thu, 5 Jan 2017 21:57:59 +0100 Subject: [PATCH] nrf5/drivers: Adding preliminary file for ili9341 lcd driver. --- nrf5/drivers/display/lcd_ili9341_driver.c | 195 ++++++++++++++++++++++ nrf5/drivers/display/lcd_ili9341_driver.h | 6 + nrf5/drivers/display/lcd_ili9341_obj.c | 5 + 3 files changed, 206 insertions(+) diff --git a/nrf5/drivers/display/lcd_ili9341_driver.c b/nrf5/drivers/display/lcd_ili9341_driver.c index 7bdc0a8405..bd232a34a2 100644 --- a/nrf5/drivers/display/lcd_ili9341_driver.c +++ b/nrf5/drivers/display/lcd_ili9341_driver.c @@ -24,10 +24,205 @@ * THE SOFTWARE. */ +#include "py/mphal.h" + #include "lcd_ili9341_driver.h" +#include "hal_spi.h" +#include "hal_time.h" #if MICROPY_PY_DISPLAY_LCD_ILI9341 +static pin_obj_t * mp_cs_pin; +static pin_obj_t * mp_dc_pin; +static NRF_SPI_Type * mp_instance; +static void raw_write(uint8_t value) +{ + hal_spi_master_tx_rx(mp_instance, 1, &value, NULL); +} + +static void cmd_write(uint8_t value) +{ + mp_hal_pin_low(mp_dc_pin); + mp_hal_pin_low(mp_cs_pin); + + hal_spi_master_tx_rx(mp_instance, 1, &value, NULL); + + mp_hal_pin_low(mp_cs_pin); +} + +static void data_write(uint8_t value) +{ + mp_hal_pin_high(mp_dc_pin); + mp_hal_pin_low(mp_cs_pin); + + hal_spi_master_tx_rx(mp_instance, 1, &value, NULL); + + mp_hal_pin_low(mp_cs_pin); +} + +void driver_ili9341_init(NRF_SPI_Type * p_instance, pin_obj_t * p_cs_pin, pin_obj_t * p_dc_pin) +{ + mp_instance = p_instance; + mp_cs_pin = p_cs_pin; + mp_dc_pin = p_dc_pin; + + // mp_hal_pin_high(enable_pin); + // mp_hal_pin_high(backlight_pin); + mp_hal_pin_high(mp_cs_pin); + mp_hal_pin_high(mp_dc_pin); + + // Read driver id + + mp_hal_delay_ms(500); + + cmd_write(0x01); + + mp_hal_delay_ms(200); + + cmd_write(0xCF); + data_write(0x00); + data_write(0x8B); + data_write(0X30); + + cmd_write(0xED); + data_write(0x67); + data_write(0x03); + data_write(0X12); + data_write(0X81); + + cmd_write(0xE8); + data_write(0x85); + data_write(0x10); + data_write(0x7A); + + cmd_write(0xCB); + data_write(0x39); + data_write(0x2C); + data_write(0x00); + data_write(0x34); + data_write(0x02); + + cmd_write(0xF7); + data_write(0x20); + + cmd_write(0xEA); + data_write(0x00); + data_write(0x00); + + cmd_write(0xC0); /* Power control */ + data_write(0x1B); /* VRH[5:0] */ + + cmd_write(0xC1); /* Power control */ + data_write(0x10); /* SAP[2:0];BT[3:0] */ + + cmd_write(0xC5); /* VCM control */ + data_write(0x3F); + data_write(0x3C); + + cmd_write(0xC7); /* VCM control2 */ + data_write(0XB7); + + cmd_write(0x36); /* Memory Access Control */ + data_write(0x08); + + cmd_write(0x3A); + data_write(0x55); + + cmd_write(0xB1); + data_write(0x00); + data_write(0x1B); + + cmd_write(0xB6); /* Display Function Control */ + data_write(0x0A); + data_write(0xA2); + + cmd_write(0xF2); /* 3Gamma Function Disable */ + data_write(0x00); + + cmd_write(0x26); /* Gamma curve selected */ + data_write(0x01); + + cmd_write(0xE0); /* Set Gamma */ + data_write(0x0F); + data_write(0x2A); + data_write(0x28); + data_write(0x08); + data_write(0x0E); + data_write(0x08); + data_write(0x54); + data_write(0XA9); + data_write(0x43); + data_write(0x0A); + data_write(0x0F); + data_write(0x00); + data_write(0x00); + data_write(0x00); + data_write(0x00); + + cmd_write(0XE1); /* Set Gamma */ + data_write(0x00); + data_write(0x15); + data_write(0x17); + data_write(0x07); + data_write(0x11); + data_write(0x06); + data_write(0x2B); + data_write(0x56); + data_write(0x3C); + data_write(0x05); + data_write(0x10); + data_write(0x0F); + data_write(0x3F); + data_write(0x3F); + data_write(0x0F); + + cmd_write(0x11); /* Exit Sleep */ + + mp_hal_delay_ms(120); + + cmd_write(0x29); /* Display on */ +} + +static void set_col(uint16_t start_col, uint16_t end_col) +{ + cmd_write(0x2A); /* Column Command address */ + data_write(start_col >> 8); + data_write(start_col & 0xFF ); + data_write(end_col >> 8); + data_write(end_col & 0xFF); +} + +static void set_page(uint16_t start_page, uint16_t end_page) +{ + cmd_write(0x2B); /* Column Command address */ + data_write(start_page >> 8); + data_write(start_page & 0xFF); + data_write(end_page >> 8); + data_write(end_page & 0xFF); +} + +void driver_ili9341_clear(uint16_t color) +{ + set_col(0, 239); + set_page(0, 319); + + cmd_write(0x2c); // start writing to the display ram + + mp_hal_pin_high(mp_dc_pin); + mp_hal_pin_low(mp_cs_pin); + + for(uint16_t i = 0; i < 38400; i++) + { + raw_write(color >> 10); + raw_write((color >> 6) & 0xFF ); + raw_write(color & 0xFF); + raw_write(0); + } + + mp_hal_pin_high(mp_cs_pin); + +} + #endif diff --git a/nrf5/drivers/display/lcd_ili9341_driver.h b/nrf5/drivers/display/lcd_ili9341_driver.h index ce0cbcc316..c81592daa6 100644 --- a/nrf5/drivers/display/lcd_ili9341_driver.h +++ b/nrf5/drivers/display/lcd_ili9341_driver.h @@ -27,4 +27,10 @@ #ifndef LCD_ILI9341_DRIVER_H__ #define LCD_ILI9341_DRIVER_H__ +#include "hal_spi.h" + +void driver_ili9341_init(NRF_SPI_Type * p_instance, pin_obj_t * cs_pin, pin_obj_t * dc_pin); + +void driver_ili9341_clear(uint16_t color); + #endif // LCD_ILI9341_DRIVER_H__ diff --git a/nrf5/drivers/display/lcd_ili9341_obj.c b/nrf5/drivers/display/lcd_ili9341_obj.c index ca7d2b066b..222442d17b 100644 --- a/nrf5/drivers/display/lcd_ili9341_obj.c +++ b/nrf5/drivers/display/lcd_ili9341_obj.c @@ -31,6 +31,7 @@ #include "py/mphal.h" #include "genhdr/pins.h" +#include "lcd_ili9341_driver.h" // For now PWM is only enabled for nrf52 targets. #if MICROPY_PY_DISPLAY_LCD_ILI9341 @@ -157,6 +158,8 @@ STATIC mp_obj_t lcd_ili9341_fill(mp_obj_t self_in, mp_obj_t color) { lcd_ili9341_obj_t *self = MP_OBJ_TO_PTR(self_in); (void)self; + driver_ili9341_clear(mp_obj_get_int(color)); + return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(lcd_ili9341_fill_obj, lcd_ili9341_fill); @@ -177,6 +180,8 @@ STATIC mp_obj_t lcd_ili9341_show(size_t n_args, const mp_obj_t *args) { num_of_refresh = mp_obj_get_int(args[1]); } + driver_ili9341_init(self->spi->pyb->spi->instance, self->pin_cs, self->pin_dc); + (void)num_of_refresh; (void)self;