提交 db6aa746 编写于 作者: wuyangyong's avatar wuyangyong

完善触摸和SPI_FLASH驱动

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@448 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 fb7ecf70
......@@ -313,7 +313,7 @@ void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
ili9325_SetCursor(x,y);
ili9325_WriteRAM_Prepare();
ili9325_WriteRAM(p);
ili9325_RAM = p ;
}
/* 获取像素点颜色 */
......@@ -339,7 +339,7 @@ void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_
ili9325_WriteRAM_Prepare(); /* Prepare to write GRAM */
while (x1 < x2)
{
ili9325_WriteRAM(p);
ili9325_RAM = p ;
x1++;
}
}
......@@ -359,7 +359,7 @@ void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t
ili9325_WriteRAM_Prepare(); /* Prepare to write GRAM */
while (y1 < y2)
{
ili9325_WriteRAM(p);
ili9325_RAM = p ;
y1++;
}
}
......@@ -379,7 +379,7 @@ void rt_hw_lcd_draw_raw_hline(rt_uint8_t *pixels, rt_base_t x1, rt_base_t x2, rt
ili9325_WriteRAM_Prepare(); /* Prepare to write GRAM */
while (x1 < x2)
{
ili9325_WriteRAM( *ptr );
ili9325_RAM = *ptr ;
x1 ++;
ptr ++;
}
......@@ -414,7 +414,7 @@ rt_err_t rt_hw_lcd_init(void)
ili9325_WriteRAM_Prepare();
for(test_y=0; test_y<76800; test_y++)
{
ili9325_WriteRAM(temp++);
ili9325_RAM = temp++ ;
}
/* read */
......
#include <stm32f10x.h>
#include "spi_flash.h"
extern unsigned char SPI_WriteByte(unsigned char data);
/********************** hardware *************************************/
/* SPI_FLASH_CS PA4 */
/* SPI_FLASH_RST PA3 */
#define FLASH_RST_0() GPIO_ResetBits(GPIOA,GPIO_Pin_3)
#define FLASH_RST_1() GPIO_SetBits(GPIOA,GPIO_Pin_3)
#define FLASH_CS_0() GPIO_ResetBits(GPIOA,GPIO_Pin_4)
#define FLASH_CS_1() GPIO_SetBits(GPIOA,GPIO_Pin_4)
/********************** hardware *************************************/
static void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
#include <stm32f10x.h>
#include "spi_flash.h"
extern unsigned char SPI_WriteByte(unsigned char data);
/********************** hardware *************************************/
/* SPI_FLASH_CS PA4 */
/* SPI_FLASH_RST PA3 */
#define FLASH_RST_0() GPIO_ResetBits(GPIOA,GPIO_Pin_3)
#define FLASH_RST_1() GPIO_SetBits(GPIOA,GPIO_Pin_3)
#define FLASH_CS_0() GPIO_ResetBits(GPIOA,GPIO_Pin_4)
#define FLASH_CS_1() GPIO_SetBits(GPIOA,GPIO_Pin_4)
/********************** hardware *************************************/
static void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
FLASH_RST_0(); // RESET
FLASH_CS_1();
FLASH_RST_1();
}
static unsigned char SPI_HostReadByte(void)
{
return SPI_WriteByte(0x00);
}
static void SPI_HostWriteByte(unsigned char wByte)
{
SPI_WriteByte(wByte);
}
/*****************************************************************************/
/*Status Register Format: */
/* ------------------------------------------------------------------------- */
/* | bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 | */
/* |--------|--------|--------|--------|--------|--------|--------|--------| */
/* |RDY/BUSY| COMP | device density | X | X | */
/* ------------------------------------------------------------------------- */
/* 0:busy | | AT45DB041:0111 | protect|page size */
/* 1:ready | | AT45DB161:1011 | */
/* --------------------------------------------------------------------------*/
/*****************************************************************************/
static unsigned char AT45DB_StatusRegisterRead(void)
{
unsigned char i;
FLASH_CS_0();
SPI_HostWriteByte(AT45DB_READ_STATE_REGISTER);
i=SPI_HostReadByte();
FLASH_CS_1();
return i;
}
static void wait_busy(void)
{
unsigned int i=0;
while (i++<2000)
{
if (AT45DB_StatusRegisterRead()&0x80)
{
break;
}
}
}
static void read_page(unsigned int page,unsigned char * pHeader)
{
unsigned int i=0;
wait_busy();
FLASH_CS_0();
SPI_HostWriteByte(AT45DB_MM_PAGE_TO_B1_XFER);
SPI_HostWriteByte((unsigned char)(page >> 6));
SPI_HostWriteByte((unsigned char)(page << 2));
SPI_HostWriteByte(0x00);
FLASH_CS_1();
wait_busy();
FLASH_CS_0();
SPI_HostWriteByte(AT45DB_BUFFER_1_READ);
SPI_HostWriteByte(0x00);
SPI_HostWriteByte(0x00);
SPI_HostWriteByte(0x00);
SPI_HostWriteByte(0x00);
for (i=0; i<512; i++)
{
*pHeader++ = SPI_HostReadByte();
}
FLASH_CS_1();
}
static void write_page(unsigned int page,unsigned char * pHeader)
{
unsigned int i;
wait_busy();
FLASH_CS_0();
SPI_HostWriteByte(AT45DB_BUFFER_2_WRITE);
SPI_HostWriteByte(0);
SPI_HostWriteByte(0);
SPI_HostWriteByte(0);
for(i=0; i<512; i++)
{
SPI_HostWriteByte(*pHeader++);
}
FLASH_CS_1();
wait_busy();
FLASH_CS_0();
SPI_HostWriteByte(AT45DB_B2_TO_MM_PAGE_PROG_WITH_ERASE);
SPI_HostWriteByte((unsigned char)(page>>6));
SPI_HostWriteByte((unsigned char)(page<<2));
SPI_HostWriteByte(0x00);
FLASH_CS_1();
}
#include <rtthread.h>
/* SPI DEVICE */
static struct rt_device spi_flash_device;
/* RT-Thread Device Driver Interface */
static rt_err_t rt_spi_flash_init(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t rt_spi_flash_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t rt_spi_flash_close(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t rt_spi_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
{
return RT_EOK;
}
static rt_size_t rt_spi_flash_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
rt_uint8_t *ptr;
rt_uint32_t index, nr;
nr = size/512;
ptr = (rt_uint8_t*)buffer;
for (index = 0; index < nr; index ++)
{
/* only supply single block read: block size 512Byte */
read_page((pos + index * 512)/512, &ptr[index * 512]);
}
return nr * 512;
}
static rt_size_t rt_spi_flash_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
rt_uint8_t *ptr;
rt_uint32_t index, nr;
nr = size / 512;
ptr = (rt_uint8_t*)buffer;
for (index = 0; index < nr; index ++)
{
/* only supply single block write: block size 512Byte */
write_page((pos + index * 512)/512, &ptr[index * 512]);
}
return nr * 512;
}
void rt_hw_spi_flash_init(void)
{
GPIO_Configuration();
/* register spi_flash device */
spi_flash_device.type = RT_Device_Class_Block;
spi_flash_device.init = rt_spi_flash_init;
spi_flash_device.open = rt_spi_flash_open;
spi_flash_device.close = rt_spi_flash_close;
spi_flash_device.read = rt_spi_flash_read;
spi_flash_device.write = rt_spi_flash_write;
spi_flash_device.control = rt_spi_flash_control;
/* no private */
spi_flash_device.private = RT_NULL;
rt_device_register(&spi_flash_device, "spi0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
}
FLASH_CS_1();
FLASH_RST_1();
}
static unsigned char SPI_HostReadByte(void)
{
//return SPI_WriteByte(0x00);
//Wait until the transmit buffer is empty
//while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while( (SPI1->SR & SPI_I2S_FLAG_TXE) == RESET);
// Send the byte
SPI_I2S_SendData(SPI1, 0);
//Wait until a data is received
//while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
while( (SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);
// Get the received data
return SPI_I2S_ReceiveData(SPI1);
}
static void SPI_HostWriteByte(unsigned char wByte)
{
SPI_WriteByte(wByte);
}
/*****************************************************************************/
/*Status Register Format: */
/* ------------------------------------------------------------------------- */
/* | bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 | */
/* |--------|--------|--------|--------|--------|--------|--------|--------| */
/* |RDY/BUSY| COMP | device density | X | X | */
/* ------------------------------------------------------------------------- */
/* 0:busy | | AT45DB041:0111 | protect|page size */
/* 1:ready | | AT45DB161:1011 | */
/* --------------------------------------------------------------------------*/
/*****************************************************************************/
static unsigned char AT45DB_StatusRegisterRead(void)
{
unsigned char i;
FLASH_CS_0();
SPI_HostWriteByte(AT45DB_READ_STATE_REGISTER);
i=SPI_HostReadByte();
FLASH_CS_1();
return i;
}
static void wait_busy(void)
{
unsigned int i=0;
while (i++<2000)
{
if (AT45DB_StatusRegisterRead()&0x80)
{
break;
}
}
}
static void read_page(unsigned int page,unsigned char * pHeader)
{
unsigned int i=0;
wait_busy();
FLASH_CS_0();
SPI_HostWriteByte(AT45DB_MM_PAGE_TO_B1_XFER);
SPI_HostWriteByte((unsigned char)(page >> 6));
SPI_HostWriteByte((unsigned char)(page << 2));
SPI_HostWriteByte(0x00);
FLASH_CS_1();
wait_busy();
FLASH_CS_0();
SPI_HostWriteByte(AT45DB_BUFFER_1_READ);
SPI_HostWriteByte(0x00);
SPI_HostWriteByte(0x00);
SPI_HostWriteByte(0x00);
SPI_HostWriteByte(0x00);
for (i=0; i<512; i++)
{
*pHeader++ = SPI_HostReadByte();
}
FLASH_CS_1();
}
static void write_page(unsigned int page,unsigned char * pHeader)
{
unsigned int i;
wait_busy();
FLASH_CS_0();
SPI_HostWriteByte(AT45DB_BUFFER_2_WRITE);
SPI_HostWriteByte(0);
SPI_HostWriteByte(0);
SPI_HostWriteByte(0);
for(i=0; i<512; i++)
{
SPI_HostWriteByte(*pHeader++);
}
FLASH_CS_1();
wait_busy();
FLASH_CS_0();
SPI_HostWriteByte(AT45DB_B2_TO_MM_PAGE_PROG_WITH_ERASE);
SPI_HostWriteByte((unsigned char)(page>>6));
SPI_HostWriteByte((unsigned char)(page<<2));
SPI_HostWriteByte(0x00);
FLASH_CS_1();
}
#include <rtthread.h>
/* SPI DEVICE */
static struct rt_device spi_flash_device;
/* RT-Thread Device Driver Interface */
static rt_err_t rt_spi_flash_init(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t rt_spi_flash_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t rt_spi_flash_close(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t rt_spi_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
{
return RT_EOK;
}
static rt_size_t rt_spi_flash_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
rt_uint8_t *ptr;
rt_uint32_t index, nr;
nr = size/512;
ptr = (rt_uint8_t*)buffer;
for (index = 0; index < nr; index ++)
{
/* only supply single block read: block size 512Byte */
read_page((pos + index * 512)/512, &ptr[index * 512]);
}
return nr * 512;
}
static rt_size_t rt_spi_flash_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
rt_uint8_t *ptr;
rt_uint32_t index, nr;
nr = size / 512;
ptr = (rt_uint8_t*)buffer;
for (index = 0; index < nr; index ++)
{
/* only supply single block write: block size 512Byte */
write_page((pos + index * 512)/512, &ptr[index * 512]);
}
return nr * 512;
}
void rt_hw_spi_flash_init(void)
{
GPIO_Configuration();
/* register spi_flash device */
spi_flash_device.type = RT_Device_Class_Block;
spi_flash_device.init = rt_spi_flash_init;
spi_flash_device.open = rt_spi_flash_open;
spi_flash_device.close = rt_spi_flash_close;
spi_flash_device.read = rt_spi_flash_read;
spi_flash_device.write = rt_spi_flash_write;
spi_flash_device.control = rt_spi_flash_control;
/* no private */
spi_flash_device.private = RT_NULL;
rt_device_register(&spi_flash_device, "spi0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
}
#include "stm32f10x.h"
#include "rtthread.h"
#include "board.h"
static void Delay_Nus(unsigned int dt)
{
volatile unsigned int a;
while (--dt)
{
for (a=0; a<5000; a++);
}
}
#if (LCD_VERSION == 2)
/*
MISO PA6
MOSI PA7
......@@ -24,8 +17,8 @@ CS PC4
7 6 - 4 3 2 1-0
s A2-A0 MODE SER/DFR PD1-PD0
*/
#define TOUCH_MSR_X 0x90 //读X轴坐标指令 addr:1
#define TOUCH_MSR_Y 0xD0 //读Y轴坐标指令 addr:3
#define TOUCH_MSR_Y 0x90 //读X轴坐标指令 addr:1
#define TOUCH_MSR_X 0xD0 //读Y轴坐标指令 addr:3
extern unsigned char SPI_WriteByte(unsigned char data);
......@@ -36,15 +29,6 @@ static void WriteDataTo7843(unsigned char num)
SPI_WriteByte(num);
}
//SPI 读数据
static unsigned int ReadDataFrom7843(void)
{
unsigned int temp;
temp = SPI_WriteByte(0x00)<<4;
temp |= ( (SPI_WriteByte(0x00)>>4)&0x0F );
return temp;
}
//触摸处理
void Go_Touch(void)
{
......@@ -52,12 +36,11 @@ void Go_Touch(void)
unsigned int Y;
CS_0();
WriteDataTo7843(TOUCH_MSR_X); //送控制字 10010000 即用差分方式读X坐标 详细请见有关资料
Delay_Nus(100);
Y = ReadDataFrom7843(); //读X轴坐标
WriteDataTo7843(TOUCH_MSR_Y); //送控制字 11010000 即用差分方式读Y坐标 详细请见有关资料
Delay_Nus(50);
X = ReadDataFrom7843(); //读Y轴坐标
WriteDataTo7843(TOUCH_MSR_X | 1); /* 发送读X坐标命令并关闭中断 */
X = SPI_WriteByte(0x00)<<4; /* 读取第一字节MSB */
X |= ((SPI_WriteByte(TOUCH_MSR_Y | 1)>>4)&0x0F );/* 读取第二字节 同时发送读Y轴坐标命令行*/
Y = SPI_WriteByte(0x00)<<4; /* 读取第一字节MSB */
Y |= ((SPI_WriteByte(1<<7)>>4)&0x0F ); /* 读取第二字节并重新打开中断 */
CS_1();
rt_kprintf("\r\nX: %04d Y: %04d",X,Y);
......@@ -80,6 +63,15 @@ static void exti_int_config(void)
EXTI_InitTypeDef EXTI_InitStructure;
/* PB1 touch INT */
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
......@@ -96,10 +88,31 @@ static void exti_int_config(void)
#include <finsh.h>
void touch_test(void)
{
SPI_InitTypeDef SPI_InitStructure;
rt_kprintf("\r\ntouch testing....\r\n");
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
/* Enable SPI_MASTER */
SPI_Cmd(SPI1, DISABLE);
/*------------------------ SPI1 configuration ------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;/* 72M/64=1.125M */
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_I2S_DeInit(SPI1);
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI_MASTER */
SPI_Cmd(SPI1, ENABLE);
NVIC_Configuration();
exti_int_config();
......@@ -109,12 +122,6 @@ void touch_test(void)
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
......@@ -122,10 +129,8 @@ void touch_test(void)
GPIO_Init(GPIOC,&GPIO_InitStructure);
CS_1();
}
Delay_Nus( 500 );
CS_0();
WriteDataTo7843(0x00);
WriteDataTo7843( 1<<7 ); /* 打开中断 */
CS_1();
}
FINSH_FUNCTION_EXPORT(touch_test, touch_test)
......@@ -135,5 +140,4 @@ void EXTI1_IRQHandler(void)
EXTI_ClearITPendingBit(EXTI_Line1);
Go_Touch();
}
/******************* (C) COPYRIGHT 2008 STMicroelectronics */
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册