#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
#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)
/*
* 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_ */
#define LCD_GPIO_Enable() RCC->IOPENR |= (1<<2)
#define LCD_GPIO_Enable() RCC->IOPENR |= (1<<0)
#define LCD_GPIO_Enable() RCC->IOPENR |= RCC_IOPENR_GPIOAEN