Author Topic: help with stm32 lcd libraries  (Read 897 times)

0 Members and 1 Guest are viewing this topic.

Offline vortexTopic starter

  • Contributor
  • Posts: 11
  • Country: ma
help with stm32 lcd libraries
« on: December 10, 2023, 08:07:14 pm »
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 code
i have uploder the librarie in attachments
i use plattformio CRL,  CRH  register is not used on stm32g i i tried my best and failed to figure the problem
iam still newb so someone help me please


#if(LCD_RST<8)
#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<8)
#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<8)
#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<8)
#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_ */
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5914
  • Country: es
Re: help with stm32 lcd libraries
« Reply #1 on: December 10, 2023, 10:57:32 pm »
Check STM32F10x RM008 page 171 and STM32FG0x0 RM0454 page 182.
The GPIO registers are different, so that function needs to be properly ported.

This is what original code does:
Code: [Select]
#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 / MODE
                     LCD_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 / MODE
                    LCD_PORT->CRL |= (0x8<<(4*LCD_SDA));                     // CNF=10 (Input with pull resistor enabled), MODER=00 (Input mode). Assume it's defaulting to pulldown?
(...)
#endif

So 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: [Select]
#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: [Select]
/*
 * nokia1661_Hw.h
 *
 *  Created on: Sep 11, 2019
 *      Author: zeus
 */

#ifndef NOKIA1661_HW_H_
#define NOKIA1661_HW_H_

#define LCD_AVR_HW 1
//#define LCD_STM_HW 1

#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_PORT PORTB
#define LCD_PIN PINB
#define LCD_DDR DDRB

#define LCD_RST PINB1
#define LCD_CS PINB2
#define LCD_SDA PINB3
#define LCD_CLK PINB5

#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_PORT GPIOA
#define LCD_GPIO_Enable() RCC->APB2ENR |= (1<<2)

#define LCD_RST 7
#define LCD_CS 6
#define LCD_SDA 5
#define LCD_CLK 4

#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_P memcpy
#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_ */
« Last Edit: December 11, 2023, 12:01:04 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: vortex

Offline vortexTopic starter

  • Contributor
  • Posts: 11
  • Country: ma
Re: help with stm32 lcd libraries
« Reply #2 on: December 11, 2023, 12:48:29 am »
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 it


last thing to do change APB2ENR
#define LCD_GPIO_Enable()   RCC->APB2ENR |= (1<<2)
 to
#define LCD_GPIO_Enable()   RCC->APBENR2 |= (1<<2)
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5914
  • Country: es
Re: help with stm32 lcd libraries
« Reply #3 on: December 11, 2023, 01:53:50 am »
GPIO is enabled in RCC_IOPENR.
Bits 0-5 enable GPIO A-F ports.
(Read RM0454 page 151).

This would enable GPIOC clock:
Code: [Select]
#define LCD_GPIO_Enable()   RCC->IOPENR |= (1<<2)

But it seems you're using GPIOA, then:
Code: [Select]
#define LCD_GPIO_Enable()   RCC->IOPENR |= (1<<0)

But even better by using the cmsis headers:
Code: [Select]
#define LCD_GPIO_Enable()   RCC->IOPENR |= RCC_IOPENR_GPIOAEN
« Last Edit: December 11, 2023, 02:06:37 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: vortex


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf