ow-dash-cube/Core/Src/LCD_driver.c
Clément Grennerat aad3bc2280 Fixing orientation
2025-09-14 12:48:57 +02:00

604 lines
17 KiB
C

// Driver afficheur ADA 1983/chip ILI9341 pour Kit STM32L476 IUT1 de Grenoble Dpt GEII
//
// Adaptations L476, et fusion libs : V. GRENNERAT, IUT1 de Grenoble, Dpt GEII
// Correction bug coordonnees X et Y pour Draw_Char et Draw_Text : V. GRENNERAT
// Ajout fonction LCD_Draw_Image_XY : V. GRENNERAT
// Intégration des fonctions de configuration SPI3 et GPIO : V. GRENNERAT
// Version 2 (1/12/2019) :
// Suppression de l'utilisation de la HAL STM32 : V. GRENNERAT
// Diverses optimisations, dans les burst images.
// --------------------------------
// MIT License
//
// Copyright (c) 2017 Matej Artnak
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
/* Includes ------------------------------------------------------------------*/
#include "LCD_driver.h"
#include "5x5_font.h"
#include "stm32f1xx.h"
/* Global Variables ------------------------------------------------------------------*/
volatile uint16_t LCD_HEIGHT = LCD_SCREEN_HEIGHT;
volatile uint16_t LCD_WIDTH = LCD_SCREEN_WIDTH;
/* SPI3 & GPIOs init function */
//void LCD_SPI_Init(void) {
////__HAL_RCC_SPI3_CLK_ENABLE();
// RCC->APB1ENR1 |= RCC_APB1ENR1_SPI3EN;// SPI3 clock enable
// RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN;// GPIOC clock enable
//
// /** CS & DC GPIO signals configuration
// PC8 ------> LCD_CS_PIN
// PC11 ------> LCD_DC_PIN
// */
// GPIOC->BSRR = LCD_CS_PIN | (LCD_DC_PIN << 16);//CS à 1 et DC à 0
// GPIOC->MODER |= (1 << 16) | (1 << 22);//GPIO out
// GPIOC->MODER &= ~((1 << 17) | (1 << 23));//mise à 0
// GPIOC->OTYPER &= (GPIO_OTYPER_OT8 | GPIO_OTYPER_OT11);//PC8 & 11 en PP
// GPIOC->OSPEEDR |= (3 << 16) | (3 << 22);//High speed
//
// /**SPI3 GPIO Configuration
// PC10 ------> SPI3_SCK
// PC12 ------> SPI3_MOSI */
// GPIOC->AFR[1] |= (6 << 8) | (6 << 16);//PC10 en AF6 : SPI3_SCK, PC12 en AF6 : SPI3_MOSI
// GPIOC->AFR[1] &= 0xFFF6F6FF;//Mise à 0
// GPIOC->MODER |= (2 << 20) | (2 << 24);//MODE AF
// GPIOC->MODER &= 0xFEEFFFFF;//Mise à 0
// GPIOC->OTYPER &= (GPIO_OTYPER_OT10 | GPIO_OTYPER_OT12);//PC10 & 12 en PP
// GPIOC->OSPEEDR |= (3 << 24) | (3 << 20);//High speed
//
// /*Configure module SPI3*/
// SPI3->CR1 = SPI_CR1_SSM | SPI_CR1_SSI;//CS soft, SSI à 1 sinon decl. mode fault
// SPI3->CR1 |= SPI_CR1_MSTR;// 0 sauf SPE et mode Master, BR = 0 => /2=> Fsck=40M
// SPI3->CR2 = 0x0700 | SPI_CR2_FRXTH;// | SPI_CR2_NSSP; //mode 8 bits, ITs disabled, no DMA, FRXTH doit être à 1 en 8 bits
// SPI3->CR1 |= SPI_CR1_SPE;
//
//}
/* Send command (char) to LCD via SPI bus */
void LCD_Write_Command(uint8_t Command) {
while ((SPI1->SR & SPI_SR_BSY) != 0);
CMD
;
while ((SPI1->SR & SPI_SR_TXE) == 0);
SPI1->DR = Command;// Cast sur pointeur, pour ecriture 8 bits. Sinon l'acces 16 bits provoque un tfert 16 bits
}
/* Send Data (char) to LCD via SPI bus */
void LCD_Write_Data(uint8_t Data) {
while ((SPI1->SR & SPI_SR_BSY) != 0);//Attendre fin envoi trame
DATA
;
while ((SPI1->SR & SPI_SR_TXE) == 0);// Waiting for TX register to be available.
SPI1->DR = Data;
}
void LCD_Write_Data16(uint16_t data) {
while ((SPI1->SR & SPI_SR_BSY) != 0);// Wait until SPI is not busy
DATA
;
while ((SPI1->SR & SPI_SR_TXE) == 0);// Wait for TX buffer empty
SPI1->DR = (data >> 8) & 0xFF;// Send MSB
while ((SPI1->SR & SPI_SR_TXE) == 0);// Wait for TX buffer empty
SPI1->DR = data & 0xFF;// Send LSB
}
/* Set the frame to draw into and sends a write into frame command */
void LCD_Set_Address(uint16_t X1, uint16_t Y1, uint16_t X2, uint16_t Y2) {
LCD_Write_Command(0x2A);
LCD_Write_Data16(X1 + 10);
LCD_Write_Data16(X2 + 10);
LCD_Write_Command(0x2B);
LCD_Write_Data16(Y1 + 19);
LCD_Write_Data16(Y2 + 19);
LCD_Write_Command(0x2C);
}
//void LCD_Set_Rotation(uint8_t Rotation) {
//
// uint8_t screen_rotation = Rotation;
//
// LCD_Write_Command(0x36);
//
// switch (screen_rotation)
// case SCREEN_VERTICAL_1:
// LCD_Write_Data(0x40 | 0x08);
// LCD_WIDTH = 240;
// LCD_HEIGHT = 320;
// break;
// case SCREEN_HORIZONTAL_1:
// LCD_Write_Data(0x20 | 0x08);
// LCD_WIDTH = 320;
// LCD_HEIGHT = 240;
// break;
// case SCREEN_VERTICAL_2:
// LCD_Write_Data(0x80 | 0x08);
// LCD_WIDTH = 240;
// LCD_HEIGHT = 320;
// break;
// case SCREEN_HORIZONTAL_2:
// LCD_Write_Data(0x40 | 0x80 | 0x20 | 0x08);
// LCD_WIDTH = 320;
// LCD_HEIGHT = 240;
// break;
// default:
// break;
//}
void LCD_HardwareReset() {
HAL_GPIO_WritePin(DRESET_GPIO_Port, DRESET_Pin, GPIO_PIN_SET);
HAL_Delay(1);
HAL_GPIO_WritePin(DRESET_GPIO_Port, DRESET_Pin, GPIO_PIN_RESET);
HAL_Delay(20);
HAL_GPIO_WritePin(DRESET_GPIO_Port, DRESET_Pin, GPIO_PIN_SET);
HAL_Delay(150);
}
void LCD_Init(void) {
LCD_HardwareReset();
// enable spi1
SPI1->CR1 |= SPI_CR1_SPE;// NSS (CS) pin is automatically pulled low
HAL_Delay(300);
// Software reset
LCD_Write_Command(0x01);
HAL_Delay(150);
// Color mode: 16bit/pixels
LCD_Write_Command(0x3A);
LCD_Write_Data(0x55);
HAL_Delay(150);
// Enable color inversion (INVON)
LCD_Write_Command(0x21);
// Configure orientation stuff
LCD_Write_Command(0x36);
LCD_Write_Data(0b10110100);
// Exit sleep
LCD_Write_Command(0x11);
HAL_Delay(150);
// Turn on display
LCD_Write_Command(0x29);
HAL_Delay(400);
// Fill white
LCD_Fill_Screen(WHITE);
// Draw colors columns
LCD_Set_Address(30, 30, LCD_SCREEN_WIDTH - 30, LCD_SCREEN_HEIGHT - 30);
uint32_t size = (LCD_SCREEN_WIDTH - 59) * 20;
LCD_Draw_Colour_Burst(BLACK, size);
LCD_Draw_Colour_Burst(WHITE, size);
LCD_Draw_Colour_Burst(BLUE, size);
LCD_Draw_Colour_Burst(GREEN, size);
LCD_Draw_Colour_Burst(RED, size);
LCD_Draw_Colour_Burst(BLACK, 4 * size);
// Draw rectangles in the angles
LCD_Draw_Rectangle(1, 1, 20, 20, RED);
LCD_Draw_Rectangle(LCD_SCREEN_WIDTH - 21, 1, 20, 20, GREEN);
LCD_Draw_Rectangle(LCD_SCREEN_WIDTH - 21, LCD_SCREEN_HEIGHT - 21, 20, 20,
MAGENTA);
LCD_Draw_Rectangle(1, LCD_SCREEN_HEIGHT - 21, 20, 20, BLUE);
// STARTING ROTATION
//LCD_Set_Rotation(SCREEN_HORIZONTAL_1);
}
//INTERNAL FUNCTION OF LIBRARY
/*Sends block colour information to LCD*/
void LCD_Draw_Colour_Burst(uint16_t color, uint32_t size) {
while ((SPI1->SR & SPI_SR_BSY) != 0);// Wait until SPI is not busy
DATA
;
for (uint32_t j = 0; j < size; j++) {
while ((SPI1->SR & SPI_SR_TXE) == 0);// Waiting for TX register to be available.
SPI1->DR = (color >> 8) & 0xFF;
while ((SPI1->SR & SPI_SR_TXE) == 0);// Waiting for TX register to be available.
SPI1->DR = color & 0xFF;
}
}
//FILL THE ENTIRE SCREEN WITH SELECTED COLOUR (either #define-d ones or custom 16bit)
/*Sets address (entire screen) and Sends Height*Width ammount of colour information to LCD*/
void LCD_Fill_Screen(uint16_t color) {
LCD_Draw_Rectangle(0, 0, LCD_WIDTH, LCD_HEIGHT, color);
}
//DRAW PIXEL AT XY POSITION WITH SELECTED COLOUR
//
//Location is dependant on screen orientation. x0 and y0 locations change with orientations.
//Using pixels to draw big simple structures is not recommended as it is really slow
//Try using either rectangles or lines if possible
//
//void LCD_Draw_Pixel(uint16_t X, uint16_t Y, uint16_t Colour) {
// if ((X >= LCD_WIDTH) || (Y >= LCD_HEIGHT)) return;//OUT OF BOUNDS!
//
////ADDRESS
// LCD_Write_Command(0x2A);
//
////XDATA
// DATA
// ;
// CS_ON;
// SPI3->DR = (X >> 8) | (X << 8);//inversion MSB / LSB pour envoi des 2 mots 8 bits en 1W 16bits
////Pas d'att si FIFO full (TX buffer Empty=0) car juste 2 écriture 16 bits tiennent dans FIFO
// SPI3->DR = ((X + 1) >> 8) | ((X + 1) << 8);
// while ((SPI3->SR & SPI_SR_BSY) != 0);//Attendre fin envoi trame
// CS_OFF;
//
////ADDRESS
// LCD_Write_Command(0x2B);
//
////YDATA
// DATA
// ;
// CS_ON;
// SPI3->DR = (Y >> 8) | (Y << 8);
// SPI3->DR = ((Y + 1) >> 8) | ((Y + 1) << 8);
// while ((SPI3->SR & SPI_SR_BSY) != 0);//Attendre fin envoi trame
// CS_OFF;
//
////ADDRESS
// LCD_Write_Command(0x2C);
//
////COLOUR
// DATA
// ;
// CS_ON;
// SPI3->DR = (Colour >> 8) | (Colour << 8);
// while ((SPI3->SR & SPI_SR_BSY) != 0);//Attendre fin envoi trame
// CS_OFF;
//}
//DRAW RECTANGLE OF SET SIZE AND HEIGTH AT X and Y POSITION WITH CUSTOM COLOUR
//
//Rectangle is hollow. X and Y positions mark the upper left corner of rectangle
//As with all other draw calls x0 and y0 locations dependant on screen orientation
//
void LCD_Draw_Rectangle(
uint16_t X,
uint16_t Y,
uint16_t Width,
uint16_t Height,
uint16_t Colour) {
if ((X >= LCD_WIDTH) || (Y >= LCD_HEIGHT)) return;
if ((X + Width - 1) >= LCD_WIDTH) {
Width = LCD_WIDTH - X;
}
if ((Y + Height - 1) >= LCD_HEIGHT) {
Height = LCD_HEIGHT - Y;
}
LCD_Set_Address(X, Y, X + Width - 1, Y + Height - 1);
LCD_Draw_Colour_Burst(Colour, Height * Width);
}
//DRAW LINE FROM X,Y LOCATION to X+Width,Y LOCATION
void LCD_Draw_Horizontal_Line(
uint16_t X,
uint16_t Y,
uint16_t Width,
uint16_t Colour) {
if ((X >= LCD_WIDTH) || (Y >= LCD_HEIGHT)) return;
if ((X + Width - 1) >= LCD_WIDTH) {
Width = LCD_WIDTH - X;
}
LCD_Set_Address(X, Y, X + Width - 1, Y);
LCD_Draw_Colour_Burst(Colour, Width);
}
//DRAW LINE FROM X,Y LOCATION to X,Y+Height LOCATION
void LCD_Draw_Vertical_Line(
uint16_t X,
uint16_t Y,
uint16_t Height,
uint16_t Colour) {
if ((X >= LCD_WIDTH) || (Y >= LCD_HEIGHT)) return;
if ((Y + Height - 1) >= LCD_HEIGHT) {
Height = LCD_HEIGHT - Y;
}
LCD_Set_Address(X, Y, X, Y + Height - 1);
LCD_Draw_Colour_Burst(Colour, Height);
}
/*********************Partie de la Lib issue de LCD_GFX**************************/
/*Draw hollow circle at X,Y location with specified radius and colour. X and Y represent circles center */
void LCD_Draw_Hollow_Circle(
uint16_t X,
uint16_t Y,
uint16_t Radius,
uint16_t Colour) {
int x = Radius - 1;
int y = 0;
int dx = 1;
int dy = 1;
int err = dx - (Radius << 1);
while (x >= y) {
LCD_Draw_Pixel(X + x, Y + y, Colour);
LCD_Draw_Pixel(X + y, Y + x, Colour);
LCD_Draw_Pixel(X - y, Y + x, Colour);
LCD_Draw_Pixel(X - x, Y + y, Colour);
LCD_Draw_Pixel(X - x, Y - y, Colour);
LCD_Draw_Pixel(X - y, Y - x, Colour);
LCD_Draw_Pixel(X + y, Y - x, Colour);
LCD_Draw_Pixel(X + x, Y - y, Colour);
if (err <= 0) {
y++;
err += dy;
dy += 2;
}
if (err > 0) {
x--;
dx += 2;
err += (-Radius << 1) + dx;
}
}
}
/*Draw filled circle at X,Y location with specified radius and colour. X and Y represent circles center */
void LCD_Draw_Filled_Circle(
uint16_t X,
uint16_t Y,
uint16_t Radius,
uint16_t Colour) {
int x = Radius;
int y = 0;
int xChange = 1 - (Radius << 1);
int yChange = 0;
int radiusError = 0;
while (x >= y) {
for (int i = X - x; i <= X + x; i++) {
LCD_Draw_Pixel(i, Y + y, Colour);
LCD_Draw_Pixel(i, Y - y, Colour);
}
for (int i = X - y; i <= X + y; i++) {
LCD_Draw_Pixel(i, Y + x, Colour);
LCD_Draw_Pixel(i, Y - x, Colour);
}
y++;
radiusError += yChange;
yChange += 2;
if (((radiusError << 1) + xChange) > 0) {
x--;
radiusError += xChange;
xChange += 2;
}
}
//Really slow implementation, will require future overhaul
//TODO: https://stackoverflow.com/questions/1201200/fast-algorithm-for-drawing-filled-circles
}
/*Draw a hollow rectangle between positions X0,Y0 and X1,Y1 with specified colour*/
void LCD_Draw_Hollow_Rectangle_Coord(
uint16_t X0,
uint16_t Y0,
uint16_t X1,
uint16_t Y1,
uint16_t Colour) {
uint16_t X_length = 0;
uint16_t Y_length = 0;
uint8_t Negative_X = 0;
uint8_t Negative_Y = 0;
float Calc_Negative = 0;
Calc_Negative = X1 - X0;
if (Calc_Negative < 0) Negative_X = 1;
Calc_Negative = 0;
Calc_Negative = Y1 - Y0;
if (Calc_Negative < 0) Negative_Y = 1;
//DRAW HORIZONTAL!
if (!Negative_X) {
X_length = X1 - X0;
} else {
X_length = X0 - X1;
}
LCD_Draw_Horizontal_Line(X0, Y0, X_length, Colour);
LCD_Draw_Horizontal_Line(X0, Y1, X_length, Colour);
//DRAW VERTICAL!
if (!Negative_Y) {
Y_length = Y1 - Y0;
} else {
Y_length = Y0 - Y1;
}
LCD_Draw_Vertical_Line(X0, Y0, Y_length, Colour);
LCD_Draw_Vertical_Line(X1, Y0, Y_length, Colour);
if ((X_length > 0) || (Y_length > 0)) {
LCD_Draw_Pixel(X1, Y1, Colour);
}
}
/*Draw a filled rectangle between positions X0,Y0 and X1,Y1 with specified colour*/
void LCD_Draw_Filled_Rectangle_Coord(
uint16_t X0,
uint16_t Y0,
uint16_t X1,
uint16_t Y1,
uint16_t Colour) {
uint16_t X_length = 0;
uint16_t Y_length = 0;
uint8_t Negative_X = 0;
uint8_t Negative_Y = 0;
int32_t Calc_Negative = 0;
uint16_t X0_true = 0;
uint16_t Y0_true = 0;
Calc_Negative = X1 - X0;
if (Calc_Negative < 0) Negative_X = 1;
Calc_Negative = 0;
Calc_Negative = Y1 - Y0;
if (Calc_Negative < 0) Negative_Y = 1;
//DRAW HORIZONTAL!
if (!Negative_X) {
X_length = X1 - X0;
X0_true = X0;
} else {
X_length = X0 - X1;
X0_true = X1;
}
//DRAW VERTICAL!
if (!Negative_Y) {
Y_length = Y1 - Y0;
Y0_true = Y0;
} else {
Y_length = Y0 - Y1;
Y0_true = Y1;
}
LCD_Draw_Rectangle(X0_true, Y0_true, X_length, Y_length, Colour);
}
/*Draws a character (fonts imported from fonts.h) at X,Y location with specified font colour, size and Background colour*/
/*See fonts.h implementation of font on what is required for changing to a different font when switching fonts libraries*/
void LCD_Draw_Char(
char Character,
uint16_t X,
uint16_t Y,
uint16_t Colour,
uint16_t Size,
uint16_t Background_Colour) {
uint8_t function_char;
uint8_t i, j;
function_char = Character;
if (function_char < ' ') {
Character = 0;
} else {
function_char -= 32;
}
char temp[CHAR_WIDTH];
for (uint8_t k = 0; k < CHAR_WIDTH; k++) {
temp[k] = font[function_char][k];
}
// Draw pixels
LCD_Draw_Rectangle(X, Y, CHAR_WIDTH * Size, CHAR_HEIGHT * Size,
Background_Colour);
for (j = 0; j < CHAR_WIDTH; j++) {
for (i = 0; i < CHAR_HEIGHT; i++) {
if (temp[j] & (1 << i)) {
if (Size == 1) {
LCD_Draw_Pixel(X + j, Y + i, Colour);
} else {
LCD_Draw_Rectangle(X + (j * Size), Y + (i * Size), Size, Size,
Colour);
}
}
}
}
}
/*Draws an array of characters (fonts imported from fonts.h) at X,Y location with specified font colour, size and Background colour*/
/*See fonts.h implementation of font on what is required for changing to a different font when switching fonts libraries*/
void LCD_Draw_Text(
const char *Text,
uint16_t X,
uint16_t Y,
uint16_t Colour,
uint16_t Size,
uint16_t Background_Colour) {
while (*Text) {
LCD_Draw_Char(*Text++, X, Y, Colour, Size, Background_Colour);
X += CHAR_WIDTH * Size;
}
}
/*Dessine une image dans une zone de l'ecran, aux coordonnées X et Y*/
//CONVERTISSEUR: http://www.digole.com/tools/PicturetoC_Hex_converter.php
//65K colour (2Bytes / Pixel)
//void LCD_Draw_Image_XY(
// const char *Image_Array,
// uint16_t X,
// uint16_t Y,
// uint16_t Width,
// uint16_t Height) {
// LCD_Set_Address(X, Y, X + Width - 1, Y + Height - 1);
//
// DATA
// ;
// CS_ON;
//
// for (uint32_t i = 0; i < Width * Height * 2; i += 2) {
// while ((SPI3->SR & SPI_SR_TXE) == 0);//Si FIFO full (TX buffer Empty=0), on attend
//// on utilise l'ecriture 16 bits dans DR, pour des envois 8 bits
//// Le LSB doit etre place ds le MSB :
// SPI3->DR = ((short) Image_Array[i + 1]) << 8 | Image_Array[i];
// }
//
// while ((SPI3->SR & SPI_SR_BSY) != 0);//Attendre fin envoi trame
// CS_OFF;
//}
/*Draws a full screen picture from flash. Image converted from RGB .jpeg/other to C array using online converter*/
//USING CONVERTER: http://www.digole.com/tools/PicturetoC_Hex_converter.php
//65K colour (2Bytes / Pixel)
//void LCD_Draw_Image_Full(const char *Image_Array, uint8_t Orientation) {
// switch (Orientation) {
// case SCREEN_HORIZONTAL_1:
// LCD_Set_Rotation(SCREEN_HORIZONTAL_1);
// LCD_Set_Address(0, 0, LCD_SCREEN_WIDTH, LCD_SCREEN_HEIGHT);
// break;
//
// case SCREEN_HORIZONTAL_2:
// LCD_Set_Rotation(SCREEN_HORIZONTAL_2);
// LCD_Set_Address(0, 0, LCD_SCREEN_WIDTH, LCD_SCREEN_HEIGHT);
// break;
//
// case SCREEN_VERTICAL_1:
// LCD_Set_Rotation(SCREEN_VERTICAL_1);
// LCD_Set_Address(0, 0, LCD_SCREEN_HEIGHT, LCD_SCREEN_WIDTH);
// break;
//
// case SCREEN_VERTICAL_2:
// LCD_Set_Rotation(SCREEN_VERTICAL_2);
// LCD_Set_Address(0, 0, LCD_SCREEN_HEIGHT, LCD_SCREEN_WIDTH);
// break;
// }
//
// DATA
// ;
// CS_ON;
//
// for (uint32_t i = 0; i < LCD_SCREEN_WIDTH * LCD_SCREEN_HEIGHT * 2; i += 2) {
// while ((SPI3->SR & SPI_SR_TXE) == 0);//Si FIFO full (TX buffer Empty=0), on attend
//// on utilise l'ecriture 16 bits dans DR, pour des envois 8 bits
//// Le LSB doit etre place ds le MSB :
// SPI3->DR = ((short) Image_Array[i + 1]) << 8 | Image_Array[i];
// }
//
// while ((SPI3->SR & SPI_SR_BSY) != 0);//Attendre fin envoi trame
// CS_OFF;
//}