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

Perfect the AT45DB driver

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@332 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 d088d601
#include "stm32f10x.h"
#include "usb_lib.h"
#include "usb_istr.h"
#include "hw_config.h"
#include "usb_pwr.h"
#include "rtthread.h"
extern uint32_t Mass_Memory_Size[3];
extern uint32_t Mass_Block_Size[3];
extern uint32_t Mass_Block_Count[3];
extern rt_device_t dev_sdio;
extern rt_device_t dev_spi_flash;
#include <finsh.h>
#include "sdcard.h"
void USB_cable(void)
{
rt_device_t dev = RT_NULL;
SD_CardInfo * sdio_info = RT_NULL;
dev = rt_device_find("sd0");
/* SPI_FLASH */
dev_spi_flash = rt_device_find("spi0");
Mass_Block_Size[1] = 512;
Mass_Block_Count[1] = 4096;
Mass_Memory_Size[1] = 4096*512;
if(dev != RT_NULL)
{
dev_sdio = dev;
sdio_info = (SD_CardInfo *)dev->private;
Mass_Memory_Size[0] = sdio_info->CardCapacity;
Mass_Block_Size[0] = sdio_info->CardBlockSize;
Mass_Block_Count[0] = Mass_Memory_Size[0] / Mass_Block_Size[0];
}
else
{
rt_kprintf("\r\nNo find the device sd0 !!!!");
}
/* 3:NAND */
/* usb msc up*/
Set_System();
Set_USBClock();
USB_Interrupts_Config();
USB_Init();
}
FINSH_FUNCTION_EXPORT(USB_cable, cable_the_usb);
#include "stm32f10x.h"
#include "usb_lib.h"
#include "usb_istr.h"
#include "hw_config.h"
#include "usb_pwr.h"
#include "rtthread.h"
extern uint32_t Mass_Memory_Size[3];
extern uint32_t Mass_Block_Size[3];
extern uint32_t Mass_Block_Count[3];
extern rt_device_t dev_sdio;
extern rt_device_t dev_spi_flash;
#include <finsh.h>
#include "sdcard.h"
void USB_cable(void)
{
rt_device_t dev = RT_NULL;
SD_CardInfo * sdio_info = RT_NULL;
dev = rt_device_find("sd0");
{
SPI_InitTypeDef SPI_InitStructure;
/*------------------------ 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_16;/* 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);
SPI_CalculateCRC(SPI1, DISABLE);
}
/* SPI_FLASH */
dev_spi_flash = rt_device_find("spi0");
Mass_Block_Size[1] = 512;
Mass_Block_Count[1] = 4096;
Mass_Memory_Size[1] = 4096*512;
if(dev != RT_NULL)
{
dev_sdio = dev;
sdio_info = (SD_CardInfo *)dev->private;
Mass_Memory_Size[0] = sdio_info->CardCapacity;
Mass_Block_Size[0] = sdio_info->CardBlockSize;
Mass_Block_Count[0] = Mass_Memory_Size[0] / Mass_Block_Size[0];
}
else
{
rt_kprintf("\r\nNo find the device sd0 !!!!");
}
/* 3:NAND */
/* usb msc up*/
Set_System();
Set_USBClock();
USB_Interrupts_Config();
USB_Init();
}
FINSH_FUNCTION_EXPORT(USB_cable, cable_the_usb);
此差异已折叠。
#include <stm32f10x.h>
//#include "spi_flash.h"
#include <string.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_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 | 0 | 1 | 1 | 1 | X | X | */
/* ----------------------------------------------------------------------- */
/* bit7 - 忙标记,0为忙1为不忙。 */
/* 当Status Register的位0移出之后,接下来的时钟脉冲序列将使SPI器件继续*/
/* 将最新的状态字节送出。 */
/* bit6 - 标记最近一次Main Memory Page和Buffer的比较结果,0相同,1不同。 */
/* bit5 */
/* bit4 */
/* bit3 */
/* bit2 - 这4位用来标记器件密度,对于AT45DB041B,这4位应该是0111,一共能标记 */
/* 16种不同密度的器件。 */
/* bit1 */
/* bit0 - 这2位暂时无效 */
/******************************************************************************/
static unsigned char AT45DB_StatusRegisterRead(void)
{
unsigned char i;
FLASH_CS_0();
SPI_HostWriteByte(0xd7);
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(0x53);
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(0xD4);
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(0x87);
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(0x86);
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)
{
/* only supply single block read: block size 512Byte */
read_page(pos/512,buffer);
return RT_EOK;
}
static rt_size_t rt_spi_flash_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
/* only supply single block write: block size 512Byte */
write_page(pos/512,(unsigned char*)buffer);
return RT_EOK;
}
void rt_hw_spi_flash_init(void)
{
GPIO_Configuration();
/* register spi_flash device */
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_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
}
#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_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)
{
/* only supply single block read: block size 512Byte */
read_page(pos/512,buffer);
return RT_EOK;
}
static rt_size_t rt_spi_flash_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
/* only supply single block write: block size 512Byte */
write_page(pos/512,(unsigned char*)buffer);
return RT_EOK;
}
void rt_hw_spi_flash_init(void)
{
GPIO_Configuration();
/* register spi_flash device */
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_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
}
#ifndef SPI_FLASH_H_INCLUDED
#define SPI_FLASH_H_INCLUDED
extern void rt_hw_spi_flash_init(void);
#endif // SPI_FLASH_H_INCLUDED
#ifndef SPI_FLASH_H_INCLUDED
#define SPI_FLASH_H_INCLUDED
/*
user for AT45DB161.
copy form : http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=737106
thanks to gxlujd.
*/
#define AT45DB_BUFFER_1_WRITE 0x84 /* 写入第一缓冲区 */
#define AT45DB_BUFFER_2_WRITE 0x87 /* 写入第二缓冲区 */
#define AT45DB_BUFFER_1_READ 0xD4 /* 读取第一缓冲区 */
#define AT45DB_BUFFER_2_READ 0xD6 /* 读取第二缓冲区 */
#define AT45DB_B1_TO_MM_PAGE_PROG_WITH_ERASE 0x83 /* 将第一缓冲区的数据写入主存储器(擦除模式)*/
#define AT45DB_B2_TO_MM_PAGE_PROG_WITH_ERASE 0x86 /* 将第二缓冲区的数据写入主存储器(擦除模式)*/
#define AT45DB_MM_PAGE_TO_B1_XFER 0x53 /* 将主存储器的指定页数据加载到第一缓冲区 */
#define AT45DB_MM_PAGE_TO_B2_XFER 0x55 /* 将主存储器的指定页数据加载到第二缓冲区 */
#define AT45DB_PAGE_ERASE 0x81 /* 页删除(每页512/528字节) */
#define AT45DB_SECTOR_ERASE 0x7C /* 扇区擦除(每扇区128K字节)*/
#define AT45DB_READ_STATE_REGISTER 0xD7 /* 读取状态寄存器 */
extern void rt_hw_spi_flash_init(void);
#endif /* SPI_FLASH_H_INCLUDED */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册