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