From 9477bf2b6cce08f648b1d9b3c37a2bf697854e70 Mon Sep 17 00:00:00 2001 From: Physick <96335032+DegustatorPonos@users.noreply.github.com> Date: Fri, 2 Jan 2026 22:41:25 +0500 Subject: Not working version --- lib/colours.h | 7 ++ lib/ili9341.c | 253 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/ili9341.h | 73 +++++++++++++++++ lib/spi_sw.c | 32 ++++++++ lib/spi_sw.h | 22 +++++ 5 files changed, 387 insertions(+) create mode 100644 lib/colours.h create mode 100644 lib/ili9341.c create mode 100644 lib/ili9341.h create mode 100644 lib/spi_sw.c create mode 100644 lib/spi_sw.h (limited to 'lib') diff --git a/lib/colours.h b/lib/colours.h new file mode 100644 index 0000000..e7c637e --- /dev/null +++ b/lib/colours.h @@ -0,0 +1,7 @@ +#define PALET_16BIT + +#ifdef PALET_16BIT + +#define white 0xFFFF + +#endif //PALET_16BIT diff --git a/lib/ili9341.c b/lib/ili9341.c new file mode 100644 index 0000000..5aba148 --- /dev/null +++ b/lib/ili9341.c @@ -0,0 +1,253 @@ +#include "ili9341.h" +#include +#include +#include "libopencm3/stm32/f1/gpio.h" +#include "spi_sw.h" +#include "libopencm3/stm32/common/spi_common_v1.h" +#include "libopencm3/stm32/f1/rcc.h" + +#define SW_SPI_IMPL + +SPI_IMPL spi_sw; + +void spi_transmit(int spi, uint8_t pl) { +#ifdef SW_SPI_IMPL + SpiSend(&spi_sw, pl); +#else + spi_write(spi, pl); +#endif +} + +// Hard resets the screen +void ILI_hard_reset(void) { + gpio_clear(GPIOA, ILI9341_RES); + delayCycles(100000); + gpio_set(GPIOA, ILI9341_RES); + delayCycles(100000); +} + +// Sets up GPIO for peripherals +void ILI_SetupGPIO() { + // setting up DC and RES + gpio_set_mode(GPIOA, + GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, + ILI9341_DC | ILI9341_RES); + + // Setting up MOSI, SS and SCK + gpio_set_mode(GPIOA, + GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, + ILI9341_CS | ILI9341_CLK | ILI9341_MOSI ); + + // Setting up MISO + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, ILI9341_MISO); + +#ifdef SW_SPI_IMPL + spi_sw = SpiInit(ILI9341_MOSI, ILI9341_CLK); +#else + // Setting up a SPI timer + rcc_periph_reset_pulse(RST_SPI1); + + spi_init_master(SPI_ADDR, SPI_CR1_BAUDRATE_FPCLK_DIV_4, SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE, + SPI_CR1_CPHA_CLK_TRANSITION_1, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST); + + spi_enable_software_slave_management(SPI_ADDR); + spi_set_nss_high(SPI_ADDR); + spi_enable(SPI_ADDR); + spi_set_clock_phase_1(SPI_ADDR); +#endif +} + +// Sends a comand to display +void ILI_sendComand(uint8_t cmd) { + gpio_set(GPIOA, ILI9341_CS); + gpio_clear(GPIOC, GPIO13); // Set the transmission LED + gpio_clear(GPIOA, ILI9341_DC); + spi_transmit(SPI_ADDR, cmd); + gpio_set(GPIOC, GPIO13); // Clear the reansmission LED + gpio_clear(GPIOA, ILI9341_CS); +} + +// Writes a bit to SPI +void ILI_sendData(uint8_t data) { + gpio_set(GPIOA, ILI9341_CS); + gpio_clear(GPIOC, GPIO13); // Set the transmission LED + gpio_set(GPIOA, ILI9341_DC); + spi_transmit(SPI_ADDR, data); + gpio_set(GPIOC, GPIO13); // Clear the reansmission LED + gpio_clear(GPIOA, ILI9341_CS); +} + +// Starts up an initialization sequence +void ILI_Setup(void) { + ILI_sendComand(ILI9341_SOFTRESET); + ILI_hard_reset(); + ILI_sendComand(ILI9341_SOFTRESET); + delayCycles(500000); + + ILI_sendComand(0xEF); + ILI_sendData(0x03); + ILI_sendData(0x80); + ILI_sendData(0x02); + + ILI_sendComand(0xCF); + ILI_sendData(0x00); + ILI_sendData(0XC1); + ILI_sendData(0X30); + + ILI_sendComand(0xED); + ILI_sendData(0x64); + ILI_sendData(0x03); + ILI_sendData(0X12); + ILI_sendData(0X81); + + ILI_sendComand(0xE8); + ILI_sendData(0x85); + ILI_sendData(0x00); + ILI_sendData(0x78); + + // Power control A + ILI_sendComand(ILI9341_POWERCONTROL_A); + ILI_sendData(0x39); + ILI_sendData(0x2C); + ILI_sendData(0x00); + ILI_sendData(0x34); + ILI_sendData(0x02); + + // Power control B + ILI_sendComand(ILI9341_POWERCONTROL_B); + ILI_sendData(0x00); + ILI_sendData(0xC1); + ILI_sendData(0x30); + + // Driver timings A + ILI_sendComand(ILI9341_DRIVER_TIMINGS_A); + ILI_sendData(0x85); + ILI_sendData(0x00); + ILI_sendData(0x78); + + // Driver timings B + ILI_sendComand(ILI9341_DRIVER_TIMINGS_B); + ILI_sendData(0x00); + ILI_sendData(0x00); + + // POS control + ILI_sendComand(ILI9341_POS_CONTROL); + ILI_sendData(0x64); + ILI_sendData(0x03); + ILI_sendData(0x12); + ILI_sendData(0x81); + + // Pump ratio + ILI_sendComand(ILI9341_PUMP_RATIO_CONTROL); + ILI_sendData(0x20); + + // Power control, VHR[5:0] + ILI_sendComand(ILI9341_POWERCONTROL1); + ILI_sendData(0x23); + + // Power control, SAP[2:0];BT[3:0] + ILI_sendComand(ILI9341_POWERCONTROL2); + ILI_sendData(0x10); + + // VCOM control 1 + ILI_sendComand(ILI9341_VCOM_CONTROL1); + ILI_sendData(0x3E); + ILI_sendData(0x28); + + // VCOM control 2 + ILI_sendComand(ILI9341_VCOM_CONTROL2); + ILI_sendData(0x86); + + // Memory access control + ILI_sendComand(ILI9341_MEM_ACCESS_CONTROL); + ILI_sendData(0x48); + + // Pixel format set + ILI_sendComand(ILI9341_PIXEL_FORMAT_CONTROL); + ILI_sendData(ILI9341_PIXEL_FORMAT_16BIT); + + // Frame ratio control + ILI_sendComand(ILI9341_FRAME_RATIO_CONTROL); + ILI_sendData(0x00); + ILI_sendData(0x18); + + // Display ratio control + ILI_sendComand(ILI9341_DISPLAY_RATIO_CONTROL); + ILI_sendData(0x08); + ILI_sendData(0x82); + ILI_sendData(0x27); + + // 3 gamma control + ILI_sendComand(ILI9341_3G_SETUP); + ILI_sendData(0x00); + + // Selected gamma + ILI_sendComand(ILI9341_GAMMA_CURVE_SELECTED); + ILI_sendData(0x01); + + // Positive gamma + ILI_sendComand(ILI9341_SET_POS_GAMMA); + ILI_sendData(0x0F); + ILI_sendData(0x31); + ILI_sendData(0x2B); + ILI_sendData(0x0C); + ILI_sendData(0x0E); + ILI_sendData(0x08); + ILI_sendData(0x4E); + ILI_sendData(0xF1); + ILI_sendData(0x37); + ILI_sendData(0x07); + ILI_sendData(0x10); + ILI_sendData(0x03); + ILI_sendData(0x0E); + ILI_sendData(0x09); + ILI_sendData(0x00); + + // Positive gamma + ILI_sendComand(ILI9341_SET_NEG_GAMMA); + ILI_sendData(0x00); + ILI_sendData(0x0E); + ILI_sendData(0x14); + ILI_sendData(0x03); + ILI_sendData(0x11); + ILI_sendData(0x07); + ILI_sendData(0x31); + ILI_sendData(0xC1); + ILI_sendData(0x48); + ILI_sendData(0x08); + ILI_sendData(0x0F); + ILI_sendData(0x0C); + ILI_sendData(0x31); + ILI_sendData(0x36); + ILI_sendData(0x0F); + + ILI_sendComand(ILI9341_SLEEPOUT); + delayCycles(500000); + ILI_sendComand(ILI9341_DISPLAYON); + delayCycles(500000); +} + +// Set position to write to +void ILI_setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { + ILI_sendComand(ILI9341_SET_COLUMN); + ILI_sendData((x0 >> 8) & 0xFF); + ILI_sendData(x0 & 0xFF); + ILI_sendData((x1 >> 8) & 0xFF); + ILI_sendData(x1 & 0xFF); + + ILI_sendComand(ILI9341_SET_ROW); + ILI_sendData((y0 >> 8) & 0xFF); + ILI_sendData(y0 & 0xFF); + ILI_sendData((y1 >> 8) & 0xFF); + ILI_sendData(y1 & 0xFF); + + ILI_sendComand(ILI9341_WRITE_TO_RAM); +} + +void delayCycles(uint32_t nops) { + for (uint32_t i = 0; i < nops; i++) { + __asm__("nop"); + } +} diff --git a/lib/ili9341.h b/lib/ili9341.h new file mode 100644 index 0000000..af1d3ee --- /dev/null +++ b/lib/ili9341.h @@ -0,0 +1,73 @@ +#include +#define STM32F1 +#include + +#ifndef ili9341 +#define ili9341 + +#define PALET_16BIT + +#define SCREEN_HEIGHT 240 +#define SCREEN_WIDTH 320 + +#define SPI_ADDR SPI1 + +#define ILI9341_DC GPIO2 +#define ILI9341_RES GPIO3 +#define ILI9341_CS GPIO1 +#define ILI9341_CLK GPIO5 +#define ILI9341_MISO GPIO6 +#define ILI9341_MOSI GPIO7 + +// Init comands +#define ILI9341_SOFTRESET 0x01 +#define ILI9341_POWERCONTROL_A 0xCB +#define ILI9341_POWERCONTROL_B 0xCF +#define ILI9341_DRIVER_TIMINGS_A 0xE8 +#define ILI9341_DRIVER_TIMINGS_B 0xEA +#define ILI9341_POS_CONTROL 0xEA +#define ILI9341_PUMP_RATIO_CONTROL 0xF7 +#define ILI9341_POWERCONTROL1 0xC0 +#define ILI9341_POWERCONTROL2 0xC1 +#define ILI9341_VCOM_CONTROL1 0xC5 +#define ILI9341_VCOM_CONTROL2 0xC7 +#define ILI9341_MEM_ACCESS_CONTROL 0x36 +#define ILI9341_PIXEL_FORMAT_CONTROL 0x3A +#define ILI9341_FRAME_RATIO_CONTROL 0xB1 +#define ILI9341_DISPLAY_RATIO_CONTROL 0xB6 +#define ILI9341_3G_SETUP 0xF2 +#define ILI9341_GAMMA_CURVE_SELECTED 0x26 +#define ILI9341_SET_POS_GAMMA 0x0F +#define ILI9341_SET_NEG_GAMMA 0xE1 + +#define ILI9341_DISPLAYOFF 0x28 +#define ILI9341_PIXELFORMAT 0x3A +#define ILI9341_MEMCONTROL 0x36 +#define ILI9341_SLEEPOUT 0x11 +#define ILI9341_DISPLAYON 0x29 + +#define ILI9341_SET_COLUMN 0x2A +#define ILI9341_SET_ROW 0x2B +#define ILI9341_WRITE_TO_RAM 0x2C + +#define ILI9341_PIXEL_FORMAT_16BIT 0x55 + +// Sets up GPIO for peripherals +void ILI_SetupGPIO(void); +// Starts up an initialization sequence +void ILI_Setup(void); +// Hard resets the screen +void ILI_hard_reset(void); + +// Sends a comand to display +void ILI_sendComand(uint8_t cmd); +// Writes a bit to SPI +void ILI_sendData(uint8_t data); + +// Set position to write to +void ILI_setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); + +#endif // ili9341 + +// Performs a goven amount of NOPs +void delayCycles(uint32_t nops); diff --git a/lib/spi_sw.c b/lib/spi_sw.c new file mode 100644 index 0000000..1b5656d --- /dev/null +++ b/lib/spi_sw.c @@ -0,0 +1,32 @@ +#include "spi_sw.h" +#include +#include + +SPI_IMPL SpiInit(uint16_t MOSI_pin, uint16_t CLK_pin) { + SPI_IMPL outp = { + .MOSI_pin = MOSI_pin, + .CLK_pin = CLK_pin, + .delay = (uint16_t) 1000, + }; + return outp; +} + +void SpiSend(SPI_IMPL *spi, uint8_t playload) { + if (TRAILING_EDGE) { + gpio_clear(GPIOA, spi->CLK_pin); + } else { + gpio_set(GPIOA, spi->CLK_pin); + } + + for (int i = 0; i < 8; i++) { + gpio_toggle(GPIOA, spi->CLK_pin); + if ((playload >> i) & 1) { + gpio_clear(GPIOA, spi->MOSI_pin); + } else { + gpio_set(GPIOA, spi->MOSI_pin); + } + delayCycles(spi->delay); + gpio_toggle(GPIOA, spi->CLK_pin); + delayCycles(spi->delay); + } +} diff --git a/lib/spi_sw.h b/lib/spi_sw.h new file mode 100644 index 0000000..26af12d --- /dev/null +++ b/lib/spi_sw.h @@ -0,0 +1,22 @@ +#define STM32F1 +#include +#include +#include +#include +#include "ili9341.h" + +#ifndef SPI_SW +#define SPI_SW + +#define TRAILING_EDGE false + +typedef struct { + uint16_t MOSI_pin; + uint16_t CLK_pin; + uint16_t delay; +} SPI_IMPL; + +SPI_IMPL SpiInit(uint16_t MOSI_pin, uint16_t CLK_pin); +void SpiSend(SPI_IMPL *spi, uint8_t playload); + +#endif //SPI_SW -- cgit v1.3