diff options
| author | Physick <96335032+DegustatorPonos@users.noreply.github.com> | 2026-01-02 22:41:25 +0500 |
|---|---|---|
| committer | Physick <96335032+DegustatorPonos@users.noreply.github.com> | 2026-01-02 22:41:25 +0500 |
| commit | 9477bf2b6cce08f648b1d9b3c37a2bf697854e70 (patch) | |
| tree | 7c94f575961e41e50d65052d473735b6daa11f0e /lib/ili9341.c | |
Not working version
Diffstat (limited to 'lib/ili9341.c')
| -rw-r--r-- | lib/ili9341.c | 253 |
1 files changed, 253 insertions, 0 deletions
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 <libopencm3/stm32/f1/spi.h> +#include <stdint.h> +#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"); + } +} |
