/*----------------------------------------------------------------------------
* Name: GLCD.c
* Purpose: MCBSTM32C low level Graphic LCD (320x240 pixels) functions
* Version: V1.00
* Note(s):
*----------------------------------------------------------------------------
* This file is part of the uVision/ARM development tools.
* This software may only be used under the terms of a valid, current,
* end user licence from KEIL for a compatible version of KEIL software
* development tools. Nothing else gives you the right to use this software.
*
* This software is supplied "AS IS" without warranties of any kind.
*
* Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
*----------------------------------------------------------------------------
* History:
* V1.00 Initial Version
*----------------------------------------------------------------------------*/
#pragma diag_suppress=550
#include "stm32f10x.h"
#include "GLCD.h"
#include "font.h"
/* SPI_SR - bit definitions. */
#define RXNE 0x01
#define TXE 0x02
#define BSY 0x80
/*********************** Hardware specific configuration **********************/
/*------------------------- Speed dependant settings -------------------------*/
/* If processor works on high frequency delay has to be increased, it can be
increased by factor 2^N by this constant */
#define DELAY_2N 18
/*---------------------- Graphic LCD size definitions ------------------------*/
#define WIDTH 320 /* Screen Width (in pixels) */
#define HEIGHT 240 /* Screen Hight (in pixels) */
#define BPP 16 /* Bits per pixel */
#define BYPP ((BPP+7)/8) /* Bytes per pixel */
/*--------------- Graphic LCD interface hardware definitions -----------------*/
#define SPI_START (0x70) /* Start byte for SPI transfer */
#define SPI_RD (0x01) /* WR bit 1 within start */
#define SPI_WR (0x00) /* WR bit 0 within start */
#define SPI_DATA (0x02) /* RS bit 1 within start byte */
#define SPI_INDEX (0x00) /* RS bit 0 within start byte */
/*---------------------------- Global variables ------------------------------*/
/******************************************************************************/
static volatile unsigned short TextColor = Black, BackColor = White;
/************************ Local auxiliary functions ***************************/
/*******************************************************************************
* Delay in while loop cycles *
* Parameter: cnt: number of while cycles to delay *
* Return: *
*******************************************************************************/
static void delay (int cnt) {
cnt <<= DELAY_2N;
while (cnt--);
}
/*******************************************************************************
* Write data to LCD controller *
* Parameter: c: data to be written *
* Return: *
*******************************************************************************/
__inline void wr_dat (unsigned short c) {
Set_Rs;
Set_nRd;
GPIOE->ODR = c;
Clr_nWr;
Set_nWr;
}
/*******************************************************************************
* Write command to LCD controller *
* Parameter: c: command to be written *
* Return: *
*******************************************************************************/
__inline void wr_cmd (unsigned char c) {
Clr_Rs;
Set_nRd;
GPIOE->ODR = c;
Clr_nWr;
Set_nWr;
}
/*******************************************************************************
* Read data from LCD controller *
* Parameter: *
* Return: read data *
*******************************************************************************/
__inline unsigned short rd_dat (void) {
unsigned short val = 0;
Set_Rs;
Set_nWr;
Clr_nRd;
GPIOE->CRH = 0x44444444;
GPIOE->CRL = 0x44444444;
val = GPIOE->IDR;
val = GPIOE->IDR;
GPIOE->CRH = 0x33333333; //配置GPIOE->ODR 8~15位位推挽输出,且最大速度为50M
GPIOE->CRL = 0x33333333; //配置GPIOE->ODR 0~7位位推挽输出,且最大速度为50M
Set_nRd;
return val;
}
/*******************************************************************************
* Write to LCD register *
* Parameter: reg: register to be write *
* val: value to write to register *
*******************************************************************************/
__inline void wr_reg (unsigned char reg, unsigned short val) {
Clr_Cs;
wr_cmd(reg);
wr_dat(val);
Set_Cs;
}
/*******************************************************************************
* Read from LCD register *
* Parameter: reg: register to be read *
* Return: value read from register *
*******************************************************************************/
unsigned short rd_reg (unsigned short reg) {
Clr_Cs;
wr_cmd(reg);
reg = rd_dat();
Set_Cs;
return reg;
}
/************************ Exported functions **********************************/
/*******************************************************************************
* Initialize the Graphic LCD controller *
* Parameter: *
* Return: *
*******************************************************************************/
void GLCD_init (void) {
static unsigned short DriverCode;
/* Enable clock for GPIOA,B,C,D,E AFIO and SPI3. */
RCC->APB2ENR |= 0x0000007D;
/* NCS is PB2, GPIO output set to high. */
GPIOE->CRL = 0x33333333; //设置PE口为50M通用推挽输出模式
GPIOE->CRH = 0x33333333;
GPIOC->CRL &= 0xf0ffffff; //PC6设置为50M通用推挽输出模式
GPIOC->CRL |= 0x03000000;
GPIOD->CRH &= 0x000fffff; //PD13,14,15设置为50M通用推挽输出模式
GPIOD->CRH |= 0x33300000;
GPIOC->BSRR = 0x00000040; //cs = 1;
GPIOD->BSRR = 0x0000E000; //rs,wr,rd = 1;
delay(5); /* Delay 50 ms */
DriverCode = rd_reg(0x00); //Read from LCD register
if(DriverCode == 0x9320)
{
/* Start Initial Sequence --------------------------------------------------*/
wr_reg(0xE5, 0x8000); /* Set the internal vcore voltage */
wr_reg(0x00, 0x0001); /* Start internal OSC */
wr_reg(0x01, 0x0100); //wr_reg(0x01, 0x0100); /* Set SS and SM bit */
wr_reg(0x02, 0x0700); /* Set 1 line inversion */
//wr_reg(0x03, 0x1030); //wr_reg(0x03, 0x1030); /* Set GRAM write direction and BGR=1 */
wr_reg(0x03, 0x1018); /*竖屏*/
wr_reg(0x04, 0x00