help with stm32 lcd  libraries - Page 1
Forum section: electronics / general

author: vortex
author_rank: Contributor
timestamp: December 10, 2023, 08:07:14 pm
content: hi ihave this librarie thats work wtih stm32fxxx but when i try to use it with stm32g030 it give me error when compiling in this part of the codei have uploder the librarie in attachmentsi use plattformio CRL, CRH register is not used on stm32g i i tried my best and failed to figure the problemiam still newb so someone help me please#if(LCD_RST<#define _rst_Out() LCD_PORT->CRL &=~(0xF<<(4*LCD_RST));\LCD_PORT->CRL |= (0x3<<(4*LCD_RST));#else#define _rst_Out() LCD_PORT->CRH &=~(0xF<<(4*LCD_RST));\LCD_PORT->CRH |= (0x3<<(4*LCD_RST));#endif#if(LCD_CS<#define _cs_Out() LCD_PORT->CRL &=~(0xF<<(4*LCD_CS));\LCD_PORT->CRL |= (0x3<<(4*LCD_CS));#else#define _cs_Out() LCD_PORT->CRH &=~(0xF<<(4*LCD_CS));\LCD_PORT->CRH |= (0x3<<(4*LCD_CS));#endif#if(LCD_SDA<#define _sda_Out() LCD_PORT->CRL &=~(0xF<<(4*LCD_SDA));\LCD_PORT->CRL |= (0x3<<(4*LCD_SDA));#define _sda_In() LCD_PORT->CRL &=~(0xF<<(4*LCD_SDA));\LCD_PORT->CRL |= (0x8<<(4*LCD_SDA));#else#define _sda_Out() LCD_PORT->CRH &=~(0xF<<(4*LCD_SDA));\LCD_PORT->CRH |= (0x3<<(4*LCD_SDA));#define _sda_In() LCD_PORT->CRH &=~(0xF<<(4*LCD_SDA));\LCD_PORT->CRH |= (0x8<<(4*LCD_SDA));#endif#if(LCD_CLK<#define _clk_Out() LCD_PORT->CRL &=~(0xF<<(4*LCD_CLK));\LCD_PORT->CRL |= (0x3<<(4*LCD_CLK));#else#define _clk_Out() LCD_PORT->CRH &=~(0xF<<(4*LCD_CLK));\LCD_PORT->CRH |= (0x3<<(4*LCD_CLK));#endif#define _Init_GPIO() { _rst_Out(); _cs_Out(); _sda_Out(); _clk_Out();}#endif#endif /* NOKIA1661_HW_H_ */

author: DavidAlfa
author_rank: Super Contributor
timestamp: December 10, 2023, 10:57:32 pm
content: CheckSTM32F10x RM008page 171 andSTM32FG0x0 RM0454page 182.The GPIO registers are different, so that function needs to be properly ported.This is what original code does:Code:#if(LCD_RST<8) // CRL is for pins 0-7, CRH for pins 8-15#define _rst_Out() LCD_PORT->CRL &=~(0xF<<(4*LCD_RST));\ // Clear all 4 bits in CNF / MODELCD_PORT->CRL |= (0x3<<(4*LCD_RST)); // CNF=00 (General PP output mode), MODE=11 (Speed=50MHz)(...)#define _sda_In() LCD_PORT->CRL &=~(0xF<<(4*LCD_SDA));\ // Clear all 4 bits in CNF / MODELCD_PORT->CRL |= (0x8<<(4*LCD_SDA)); // CNF=10 (Input with pull resistor enabled), MODER=00 (Input mode). Assume it's defaulting to pulldown?(...)#endifSo this is it.Since all inputs/outputs are initialized with the same settings, we can simplify it a lot by adding higher macro levels and using CMSIS macros:Code:#define _out(P) MODIFY_REG(LCD_PORT->MODER, 3<<(2*P), 1<<(2*P)); \ // Clear MODER bits, set MODER=01 (General output mode)CLEAR_BIT(LCD_PORT->OTYPER, 1<<P); \ // Clear OTYPER bit = output push-pull.SET_BIT(LCD_PORT->OSPEEDR, 3<<(2*P)) // Set OSPEEDR bits to 11 = max speed#define _in(P) CLEAR_BIT(LCD_PORT->MODER, 3<<(2*P)); \ // Clear MODER bits (Input mode)MODIFY_REG(LCD_PORT->PUPDR, 3<<(2*P), 2<<(2*P)) // Enable pull-down#define _rst_Out() _out(LCD_RST)#define _cs_Out() _out(LCD_CS)#define _sda_Out() _out(LCD_SDA)#define _sda_In() _in(LCD_SDA)#define _clk_Out() _out(LCD_CLK)Final file, note that I also replaced replace the include "stm32f10x.h" with "stm32g0xx.h".Code:/** nokia1661_Hw.h** Created on: Sep 11, 2019* Author: zeus*/#ifndef NOKIA1661_HW_H_#define NOKIA1661_HW_H_#define LCD_AVR_HW1//#define LCD_STM_HW1#if defined(LCD_AVR_HW) && defined(LCD_STM_HW)#error Only Select One Hardware AVR/STM#endif#if !defined(LCD_AVR_HW) && !defined(LCD_STM_HW)#error You need to define Hardware Type for LCD usage#endif#ifdef LCD_AVR_HW#define LCD_PORTPORTB#define LCD_PINPINB#define LCD_DDRDDRB#define LCD_RSTPINB1#define LCD_CSPINB2#define LCD_SDAPINB3#define LCD_CLKPINB5#include <avr/io.h>#include <util/delay.h>#include <avr/pgmspace.h>#define SBI(port,bit)asm("sbi %0, %1" : : "I" (_SFR_IO_ADDR(port)), "I" (bit))#define CBI(port,bit)asm("cbi %0, %1" : : "I" (_SFR_IO_ADDR(port)), "I" (bit))#define LCD_RSDA()(LCD_PIN&(1 << LCD_SDA))#define LCD_GPIO_Enable()#define _sda_In()LCD_DDR &=~(1<<LCD_SDA)#define _sda_Out()LCD_DDR |= (1<<LCD_SDA)#define _rst_set()SBI(LCD_PORT,LCD_RST)#define _rst_clr()CBI(LCD_PORT,LCD_RST)#define _cs_set()SBI(LCD_PORT,LCD_CS)#define _cs_clr()CBI(LCD_PORT,LCD_CS)#define _sda_set()SBI(LCD_PORT,LCD_SDA)#define _sda_clr()CBI(LCD_PORT,LCD_SDA)#define _clk_set()SBI(LCD_PORT,LCD_CLK)#define _clk_clr()CBI(LCD_PORT,LCD_CLK)#define _Init_GPIO()LCD_DDR |= (1 << LCD_RST) | (1 << LCD_CS) | (1 << LCD_SDA) | (1 << LCD_CLK)#elif defined(LCD_STM_HW)#include <stm32g0xx.h>#include <string.h>#define LCD_PORTGPIOA#define LCD_GPIO_Enable()RCC->APB2ENR |= (1<<2)#define LCD_RST7#define LCD_CS6#define LCD_SDA5#define LCD_CLK4#define LCD_RSDA()(LCD_PORT->IDR&(1<<LCD_SDA))#define SBI(port,bit)port->BSRR = (1<<bit)#define CBI(port,bit)port->BRR = (1<<bit)#define memcpy_Pmemcpy#define PROGMEM#define PSTR(x)(x)#define pgm_read_byte(x)*(x)#define _delay_ms(x)delay_ms(x) /*Define this Function in External Source*/#define _rst_set()SBI(LCD_PORT,LCD_RST)#define _rst_clr()CBI(LCD_PORT,LCD_RST)#define _cs_set()SBI(LCD_PORT,LCD_CS)#define _cs_clr()CBI(LCD_PORT,LCD_CS)#define _sda_set()SBI(LCD_PORT,LCD_SDA)#define _sda_clr()CBI(LCD_PORT,LCD_SDA)#define _clk_set()SBI(LCD_PORT,LCD_CLK)#define _clk_clr()CBI(LCD_PORT,LCD_CLK)#define _out(P) MODIFY_REG(LCD_PORT->MODER, 3<<(2*P), 1<<(2*P)); \CLEAR_BIT(LCD_PORT->OTYPER, 1<<P); \SET_BIT(LCD_PORT->OSPEEDR, 3<<(2*P))#define _in(P) CLEAR_BIT(LCD_PORT->MODER, 3<<(2*P)); \MODIFY_REG(LCD_PORT->PUPDR, 3<<(2*P), 2<<(2*P))#define _rst_Out() _out(LCD_RST)#define _cs_Out() _out(LCD_CS)#define _sda_Out() _out(LCD_SDA)#define _sda_In() _in(LCD_SDA)#define _clk_Out() _out(LCD_CLK)#define _Init_GPIO() { _rst_Out(); _cs_Out(); _sda_Out(); _clk_Out();}#endif#endif /* NOKIA1661_HW_H_ */

author: vortex
author_rank: Contributor
timestamp: December 11, 2023, 12:48:29 am
content: thank you very much DavidAlfa for your help i'm still learning i used avr before now convert to stm32 with better capability and price. RM0454 Reference manual with 990 page make you hisitate to read itlast thing to do change APB2ENR#define LCD_GPIO_Enable() RCC->APB2ENR |= (1<<2)to#define LCD_GPIO_Enable() RCC->APBENR2 |= (1<<2)

author: DavidAlfa
author_rank: Super Contributor
timestamp: December 11, 2023, 01:53:50 am
content: GPIO is enabled in RCC_IOPENR.Bits 0-5 enable GPIO A-F ports.(Read RM0454 page 151).This would enable GPIOC clock:Code:#define LCD_GPIO_Enable() RCC->IOPENR |= (1<<2)But it seems you're using GPIOA, then:Code:#define LCD_GPIO_Enable() RCC->IOPENR |= (1<<0)But even better by using the cmsis headers:Code:#define LCD_GPIO_Enable() RCC->IOPENR |= RCC_IOPENR_GPIOAEN