一、W25Q32BV芯片簡介
W25X是一系列SPI接口Flash芯片的簡稱,它採用SPI接口和CPU通信,本文使用的W25Q32BV容量爲32M,具體特性如下:
1.1、基本特性
該芯片最大支持104MHz的時鐘,供電電壓範圍在2.7~3.6V,SPI的片選信號CS低有效,在操作芯片的時候,
需要將/WP和/HOLD管腳接電源。
發送地址或數據到設備時,MOSI管腳數據採樣在CLK的上升沿,從芯片讀數據或者狀態時,MISO管腳數據採樣在CLK
的下降沿,所以在設置SPI的工作模式時,必須設置爲MODE0或者MODE3,本文設置爲MODE3。
1.2、存儲空間簡介
W25Q32BV總共有16384頁(page),每頁有256bytes,每次最大可以編程一頁。在擦除上,可以一次擦除
4KB、32KB、64KB,或者擦除整個芯片。整個芯片的存儲空間如下圖:
W25Q32BV存儲空間分爲sector和block。一個sector共有4KB,一個block共有32KB。一個sector存儲空間如下圖:
本文共支持四種擦除方式,分別如下:
1) cmd = 0x20,sector擦除,一次可以擦除4KB。芯片共有1024個sector。
2) cmd = 0x52,半個block擦除,一次可以擦除32KB。芯片共有128個半block。
3) cmd = 0xd8,block擦除,一次可以擦除64KB。芯片共有64個block。
4) cmd = 0xC7,芯片擦除,擦除整個芯片。
1.3、狀態寄存器
W25Q32BV共有兩個字節的狀態寄存器,我們需要關心的就是BIT0和BIT1。
BIT0:busy flag,1:busy,0:free。
BIT1:write enable latch,1:write enable,0:write disable。
1.4、操作要求
在操作W25Q32BV時,如果是寫數據到芯片,則每寫一個字節,都需要讀取一個數據。
在從芯片接收數據時,首先往芯片寫一個字節的0xff,然後就是需要讀取的數據。
二、設備驅動
2.1、設備註冊
在系統啓動的時候,首先會對設備信息進行註冊,見《Linux
spi驅動分析(一)----總線驅動》中的3.1,所以編寫w25q的設備
驅動程序時,首先需要對設備信息進行註冊,具體內容如下:
點擊(此處)摺疊或打開
-
#if defined(CONFIG_SPI_FLASH_W25Q)
-
static struct gsc3280_spi_info w25q_spi1_dev_platdata = {
-
.cs_type = 1,
-
.pin_cs = 87,
-
.num_cs = 1,
-
.cs_value = 0,
-
.lsb_flg = 0,
-
.bits_per_word = 8,
-
};
-
#endif
-
static struct spi_board_info gsc3280_spi_devices[] = {
-
#if defined(CONFIG_SPI_FLASH_W25Q)
-
{
-
.modalias = "spi-w25q",
-
.bus_num = 1,
-
.chip_select = 2,
-
.mode = SPI_MODE_3,
-
.max_speed_hz = 5 * 1000 * 1000,
-
.controller_data = &w25q_spi1_dev_platdata,
-
},
-
#endif
-
-
};
-
static int __init gsc3280_spi_devices_init(void)
-
{
-
spi_register_board_info(gsc3280_spi_devices, ARRAY_SIZE(gsc3280_spi_devices));
-
return 0;
-
}
- device_initcall(gsc3280_spi_devices_init);
2.2、初始化函數
首先我們從設備註冊開始,程序如下:
點擊(此處)摺疊或打開
-
static struct spi_driver w25q_driver = {
-
.driver = {
-
.name = "spi-w25q",
-
.owner = THIS_MODULE,
-
},
-
//.id_table = w25q_ids,
-
.probe = w25q_probe,
-
.remove = __devexit_p(w25q_remove),
-
};
-
-
-
static int __init w25q_init(void)
-
{
-
return spi_register_driver(&w25q_driver);
-
}
-
static void __exit w25q_exit(void)
-
{
-
spi_unregister_driver(&w25q_driver);
-
}
-
module_init(w25q_init);
- module_exit(w25q_exit);
由於W25Q32BV使用SPI接口,所以將其註冊爲SPI驅動,接下來看下探測函數w25q_probe,程序如下:
點擊(此處)摺疊或打開
-
static int __devinit w25q_probe(struct spi_device *spi)
-
{
-
int ret = 0;
-
struct w25q_dev *w25q;
-
-
DBG("############\n");
-
DBG("w25q spi flash probe start.\n");
-
w25q = kzalloc(sizeof(struct w25q_dev), GFP_KERNEL);
-
if (!w25q) {
-
DBG("!!!!kzalloc error!\n");
-
return -ENOMEM;
-
}
-
ret = spi_setup(spi);
-
if (ret != 0) {
-
DBG("!!!!setup error!\n");
-
return ret;
-
}
-
w25q->spi = spi;
-
mutex_init(&w25q->mlock);
-
strlcpy(w25q->name, W25Q_SPI_FLASH_NAME, sizeof(w25q->name));
-
ret = alloc_chrdev_region(&w25q->devt, 0, W25Q_MAX_MINOR, "w25q");
-
if (ret < 0) {
-
DBG("!!!!%s: failed to allocate char dev region!\n", __FILE__);
-
goto err_kzall;
-
}
-
w25q->dev.devt = MKDEV(MAJOR(w25q->devt), 1);
-
cdev_init(&w25q->cdev, &w25q_fops);
-
w25q->cdev.owner = THIS_MODULE;
-
ret = cdev_add(&w25q->cdev, w25q->devt, 1);
-
if (ret) {
-
DBG("!!!!cdev add error!\n");
-
goto err_alloc;
-
}
-
w25q->class = class_create(THIS_MODULE, "w25q-spi");
-
if (IS_ERR(w25q->class)) {
-
DBG("!!!!failed in create w25q spi flash class!\n");
-
goto err_alloc;;
-
}
-
device_create(w25q->class, NULL, w25q->devt, NULL, "w25q");
-
dev_set_drvdata(&spi->dev, w25q);
-
DBG("w25q spi flash probe success.\n");
-
DBG("############\n");
-
return 0;
-
-
err_alloc:
-
unregister_chrdev_region(w25q->devt, W25Q_MAX_MINOR);
-
err_kzall:
-
kfree(w25q);
-
printk(KERN_ERR "!!!!!!w25q spi flash probe error.!!!!!!\n");
-
return ret;
- }
說明:
1) 首先申請設備驅動結構體。
2) 調用spi_setup(spi)函數對設備信息初始化。
3) 初始化設備驅動結構體成員變量。
4) 創建/dev目錄下操作文件,操作函數集爲w25q_fops。
5) 將設備驅動結構體中的鏈表插入本文件全局鏈表w25q_device_list中,以便在函數操作集的open函數中找到設備驅動結構體。
remove函數是探測函數的相反過程,具體程序如下:
點擊(此處)摺疊或打開
-
static int __devexit w25q_remove(struct spi_device *spi)
-
{
-
struct w25q_dev *w25q = dev_get_drvdata(&spi->dev);
-
-
cdev_del(&w25q->cdev);
-
unregister_chrdev_region(w25q->devt, W25Q_MAX_MINOR);
-
device_destroy(w25q->class, w25q->devt);
-
class_destroy(w25q->class);
-
kfree(w25q);
-
return 0;
- }
2.3、操作函數集w25q_fops
操作函數集結構體具體內容如下:
點擊(此處)摺疊或打開
-
static const struct file_operations w25q_fops = {
-
.owner = THIS_MODULE,
-
.open = w25q_open,
-
.write = w25q_write,
-
.unlocked_ioctl = w25q_ioctl,
-
.read = w25q_read,
-
.llseek = w25q_llseek,
-
.release = w25q_release,
- };
接下來我們一一講述。
首先看下open函數w25q_open,具體程序如下:
點擊(此處)摺疊或打開
-
static int w25q_open(struct inode *inode, struct
file *file)
-
{
-
struct w25q_dev *w25q = container_of(inode->i_cdev, struct
w25q_dev, char_cdev);
-
-
if (test_and_set_bit(W25Q_BIT_LOCK_OPEN, &w25q->bit_lock)) {
-
DBG("!!!!w25q open err, busy!\n");
-
return -EBUSY;
-
}
-
file->private_data = w25q;
-
return 0;
- }
說明:
1) 通過container_of找到在探測函數w25q_probe中定義的設備驅動結構體。
2) 測試並且設置忙標誌,如果測試忙,直接忙退出。
3) 將找到的設備驅動結構體指針指向file->private_data,在函數操作集的其他函數中就可以使用設備驅動結構體了。
接下來看下寫函數w25q_write(),程序如下:
點擊(此處)摺疊或打開
-
#define W25Q_BUF_LEN 4096
-
#define W25Q_PAGE_NUM 256
-
static ssize_t w25q_write(struct file *file, const char
__user *user_buf, size_t count, loff_t *ppos)
-
{
-
int ret = 0;
-
u8 *buf_start, *buf_tmp, *w25q_buf;
-
struct w25q_dev *w25q= file->private_data;
-
u32 buf_size = 0, page_num = W25Q_PAGE_NUM, len = 0;
-
-
DBG("@@@@w25q write start\n");
-
buf_start = buf_tmp = kzalloc(W25Q_BUF_LEN, GFP_KERNEL);
-
w25q_buf = w25q->buf = kzalloc(page_num + 4, GFP_KERNEL);
-
if (!buf_start || !w25q_buf) {
-
DBG("!!!!kzalloc error!\n");
-
return -ENOMEM;
-
}
-
ret = mutex_lock_interruptible(&w25q->mlock);
-
if (ret) {
-
DBG("!!!!mutex lock error!");
-
goto exit_kfree;
-
}
-
len = W25Q_BUF_LEN;
-
buf_size = min(count, len);
-
if (copy_from_user(buf_tmp, user_buf, buf_size)) {
-
DBG("!!!!copy_from_user() error!\n");
-
ret = -EFAULT;
-
goto exit_lock;
-
}
-
DBG("w25q->const_addr = 0x%x\n", w25q->const_addr);
-
buf_tmp = buf_start;
-
w25q->cmd = W25X_PAGE_PROG;
-
w25q->addr = w25q->const_addr;
-
while(buf_size) {
-
w25q->buf = w25q_buf;
-
w25q->len = min(buf_size, page_num);
-
memcpy(w25q->buf + 4, buf_tmp, w25q->len);
-
ret = w25q_write_date(w25q);
-
if (ret != 0) {
-
break;
-
}
-
buf_tmp += w25q->len;
-
w25q->addr += w25q->len;
-
buf_size -= w25q->len;
-
}
-
-
exit_lock:
-
mutex_unlock(&w25q->mlock);
-
exit_kfree:
-
kfree(buf_start);
-
kfree(w25q_buf);
-
if (ret != 0)
-
DBG("!!!!w25q write error!\n");
-
else
-
DBG("w25q write success\n");
-
return ret;
- }
說明:
1) 寫函數首先申請兩段內存,第一段內存用於存儲從應用層複製來的待寫數據,最大爲4KB。第二段內存用於存儲每次
往W25Q32BV寫的數據。由於W25Q32BV每次最大能寫256bytes,所以page_num = 256,加上4是由於每次
傳輸時,需要在最前面加上一個字節的命令和三個字節的地址。
2) 獲取本次可以傳輸的最大數據長度。
3) 設置好傳輸的cmd和起始地址,然後進入while循環。
4) 在while循環中,獲取本次可以傳輸的最大長度,最長爲256bytes,然後將其拷貝到buf中,加上4的目的是因爲buf
的前四個字節需要放置命令和地址。
5) 調用w25q_write_date(w25q)函數實現數據傳輸。
6) 更新變量,爲下一次傳輸做好準備。
w25q_write_date(w25q)函數具體內容如下:
點擊(此處)摺疊或打開
-
static void w25q_write_enable(struct w25q_dev *w25q)
-
{
-
u8 cmd = W25X_WRITE_ENABLE;
-
-
spi_w8r8(w25q->spi, cmd);
-
}
-
static int w25q_wait_null(struct w25q_dev *w25q)
-
{
-
uint8_t limit = 5;
-
-
/* wait BUSY bit clear */
-
while(((w25q_read_stat_reg(w25q) & 0x01) == 0x01) && (limit != 0)) {
-
limit--;
-
mdelay(50);
-
}
-
if (limit == 0) {
-
DBG("!!!!w25q_wait_null:time out!\n");
-
return -EBUSY;
-
}
-
else
-
return 0;
-
}
-
/*
-
* when you call this function,
-
* the w25q->cmd, w25q->len(tx date len),
-
* w25q->addr and w25q->buf(date) are
OK
-
*
-
*/
-
static int w25q_write_date(struct w25q_dev *w25q)
-
{
-
int ret = 0;
-
u8 i = 0, rx = 0;
-
struct spi_message message;
-
struct spi_transfer x[(w25q->len + 4) * 2];
-
-
w25q_write_enable(w25q); //SET WEL
-
ret = w25q_wait_null(w25q);
-
if (ret != 0) {
-
DBG("!!!!w25q_write_date: wait null err!\n");
-
return ret;
-
}
-
if((w25q_read_stat_reg(w25q) & 0x02) != 0x02) {
-
DBG("!!!!state register write able is 0\n");
-
return -EBUSY; //disable
write
-
}
-
-
DBG("cmd = 0x%x, addr = 0x%x\n", w25q->cmd, w25q->addr);
-
w25q->buf[0] = w25q->cmd;
-
w25q->buf[1] = ((u8)(w25q->addr >> 16));
-
w25q->buf[2] = ((u8)(w25q->addr >> 8));
-
w25q->buf[3] = ((u8)w25q->addr);
-
-
spi_message_init(&message);
-
memset(x, 0, sizeof x);
-
for (i = 0; i < (w25q->len + 4) * 2; i++) {
-
x[i].len = 1;
-
spi_message_add_tail(&x[i], &message);
-
if ((i % 2) == 0) {
-
x[i].tx_buf = w25q->buf++;
-
} else {
-
x[i].rx_buf = ℞
-
}
-
}
-
/* do the i/o */
-
ret = spi_sync(w25q->spi, &message);
-
if (ret != 0) {
-
DBG("!!!!w25q_write_date: spi_sync() error!");
-
return ret;
-
}
-
ret = w25q_wait_null(w25q);
-
if (ret != 0)
-
DBG("!!!!w25q_write_date: w25q_wait_null() error!");
-
return ret;
- }
說明:
1) 在調用w25q_write_date(w25q)函數之前,需要首先設置好w25q->cmd, w25q->len(tx date len),
w25q->addr和w25q->buf(date)變量。
2) 設置芯片狀態寄存器,使其可寫。
3) 等待芯片不忙。
4) 讀取芯片狀態寄存器,查看其是否可寫。
5) 配置發送buf,調用spi_sync(w25q->spi, &message);函數實現寫數據。
6) 等待芯片不忙,退出。
接下來看下函數操作集中的ioctl函數,程序如下:
點擊(此處)摺疊或打開
-
static long w25q_ioctl(struct file *file, unsigned int cmd, unsigned
long arg)
-
{
-
int ret = 0;
-
u32 get_value = 0;
-
struct w25q_dev *w25q= file->private_data;
-
void __user *argp = (void __user *)arg;
-
int __user *p = argp;
-
-
DBG("@@@@w25q ioctl start.\n");
-
ret = mutex_lock_interruptible(&w25q->mlock);
-
if (ret) {
-
DBG("!!!!mutex lock error!\n");
-
return ret;
-
}
-
if ((_IOC_TYPE(cmd) != W25Q_IOC_MAGIC) || (_IOC_NR(cmd) > W25Q_IOC_MAXNR)) {
-
DBG("!!!!ioc type or ioc nr error!\n");
-
ret = -ENOTTY;
-
goto exit;
-
}
-
switch(cmd) {
-
case W25Q_SECTOR_ERASE:
-
case W25Q_HALF_BLOCK_ERASE:
-
case W25Q_BLOCK_ERASE:
-
if (get_user(get_value, p)) {
-
DBG("!!!!get value error!\n");
-
ret = -EFAULT;
-
goto exit;
-
}
-
ret = w25q_erase(w25q, get_value, cmd);
-
break;
-
case W25Q_CHIP_ERASE:
-
ret = w25q_chip_erase(w25q);
-
break;
-
case W25Q_READ_DEVICE_ID:
-
ret = w25q_read_id(w25q);
-
if (ret == 0)
-
put_user(w25q->result, p);
-
break;
-
default:
-
DBG("!!!!cmd error!\n");
-
ret = -ENOTTY;
-
break;
-
}
-
-
exit:
-
mutex_unlock(&w25q->mlock);
-
if (ret != 0)
-
DBG("!!!!w25q ioctl error!\n");
-
else
-
DBG("w25q ioctl success.\n");
-
return ret;
- }
說明:
1) 目前共支持5個命令,包括sector擦除,half block擦除,block擦除,芯片擦除和讀取芯片ID。
2) 前三種擦除方式共用一個函數w25q_erase(w25q, get_value, cmd);,程序如下:
點擊(此處)摺疊或打開
-
static int w25q_erase(struct w25q_dev *w25q, u32
num, unsigned int cmd)
-
{
-
int ret = 0;
-
u8 *buf_start;
-
-
switch(cmd) {
-
case W25Q_SECTOR_ERASE:
-
DBG("sector erase cmd\n");
-
if (num > W25Q_SECTOR_MAX) {
-
DBG("!!!!sector max is over\n");
-
return -EFAULT;
-
}
-
w25q->const_addr = num * W25Q_ONE_SECTOR_ADDR;
-
w25q->cmd = W25X_SECTOR_ERASE_CMD;
-
break;
-
case W25Q_HALF_BLOCK_ERASE:
-
DBG("half block erase cmd\n");
-
if (num > W25Q_HALF_BLOCK_MAX) {
-
DBG("!!!!half block max is over\n");
-
return -EFAULT;
-
}
-
w25q->const_addr = num * W25Q_HALF_BLOCK_ADDR;
-
w25q->cmd = W25X_HALF_BLOCK_ERASE_CMD;
-
break;
-
case W25Q_BLOCK_ERASE:
-
DBG("block erase cmd\n");
-
if (num > W25Q_BLOCK_MAX) {
-
DBG("!!!!block max is over\n");
-
return -EFAULT;
-
}
-
w25q->const_addr = num * W25Q_ONE_BLOCK_ADDR;
-
w25q->cmd = W25X_BLOCK_ERASE_CMD;
-
break;
-
}
-
DBG("w25q->const_addr = 0x%x\n", w25q->const_addr);
-
w25q->len = 0;
-
buf_start = w25q->buf = kzalloc(w25q->len + 4, GFP_KERNEL);
-
if (!buf_start) {
-
DBG("!!!!kzalloc is error\n");
-
return -ENOMEM;
-
}
-
w25q->addr = w25q->const_addr;
-
ret = w25q_write_date(w25q);
-
kfree(buf_start);
-
if (ret != 0) {
-
DBG("!!!!w25q_erase: spi write err!\n");
-
return ret;
-
}
-
DBG("w25q_erase: erase OK\n");
-
return ret;
- }
說明:
1) 首先根據不同的擦除方式,設置命令和地址兩個變量。
2) 調用w25q_write_date(w25q);函數實現數據傳輸。
芯片擦除函數w25q_chip_erase()如下:
點擊(此處)摺疊或打開
-
static int w25q_chip_erase(struct w25q_dev *w25q)
-
{
-
int ret = 0;
-
u8 cmd = W25X_CHIP_ERASE;
-
-
DBG("w25q_chip_erase\n");
-
w25q_write_enable(w25q); //SET WEL
-
ret = w25q_wait_null(w25q);
-
if (ret != 0) {
-
DBG("!!!!chip_erase: wait null err!\n");
-
return ret;
-
}
-
if((w25q_read_stat_reg(w25q) & 0x02) != 0x02) {
-
DBG("!!!!state register write able is 0\n");
-
return -EBUSY; //disable
write
-
}
-
spi_w8r8(w25q->spi, cmd);
-
return w25q_wait_null(w25q);
- }
讀取設備ID函數w25q_read_id()如下:
點擊(此處)摺疊或打開
-
static int w25q_read_id(struct w25q_dev *w25q)
-
{
-
int ret = 0;
-
u8 *buf_start;
-
-
DBG("w25q_read_id\n");
-
w25q->len = 2;
-
w25q->addr = 0;
-
w25q->cmd = W25X_READ_ID_CMD;
-
buf_start = w25q->buf = kzalloc(w25q->len, GFP_KERNEL);
-
if (!buf_start) {
-
DBG("!!!!kzalloc is error\n");
-
return -ENOMEM;
-
}
-
ret = w25q_read_data(w25q);
-
w25q->buf = buf_start;
-
w25q->result = *w25q->buf << 8;
-
w25q->buf++;
-
w25q->result |= *w25q->buf;
-
kfree(buf_start);
-
if (ret != 0) {
-
DBG("!!!!w25q_read_id: w25q_read_data error!\n");
-
return ret;
-
}
-
DBG("w25q_read_id: read id OK\n");
-
return ret;
- }
說明:
1) 首先設置好變量,申請內存
2) 調用w25q_read_data()函數實現讀取數據。
w25q_read_data函數如下:
點擊(此處)摺疊或打開
-
/*
-
* when you call this function,
-
* the w25q->cmd, w25q->len(receive len)
-
* w25q->buf(kzalloc
receive) and w25q->addr
are OK
-
*
-
*/
-
static int w25q_read_data(struct w25q_dev *w25q)
-
{
-
int ret = 0;
-
struct spi_message message;
-
struct spi_transfer x[(w25q->len + 4) * 2];
-
u8 i = 0, rx = 0, dumy_value = 0xff, tx_buff[4] = {0};
-
-
w25q_write_enable(w25q); //SET WEL
-
ret = w25q_wait_null(w25q);
-
if (ret != 0) {
-
DBG("!!!!chip_erase: wait null err!\n");
-
return ret;
-
}
-
if((w25q_read_stat_reg(w25q) & 0x02) != 0x02) {
-
DBG("!!!!state register write able is 0\n");
-
return -EBUSY; //disable
write
-
}
-
-
DBG("cmd = 0x%x, addr = 0x%x\n", w25q->cmd, w25q->addr);
-
tx_buff[0] = w25q->cmd;
-
tx_buff[1] = ((uint8_t)(w25q->addr >> 16));
-
tx_buff[2] = ((uint8_t)(w25q->addr >> 8));
-
tx_buff[3] = ((uint8_t)(w25q->addr));
-
-
spi_message_init(&message);
-
memset(x, 0, sizeof x);
-
for (i = 0; i < 8; i++) { //cmd
-
x[i].len = 1;
-
spi_message_add_tail(&x[i], &message);
-
if ((i % 2) == 0) {
-
x[i].tx_buf = &tx_buff[i / 2];
-
} else {
-
x[i].rx_buf = ℞
-
}
-
}
-
for (i = 8; i < (w25q->len + 4) * 2; i++) {
-
x[i].len = 1;
-
spi_message_add_tail(&x[i], &message);
-
if ((i % 2) == 0) {
-
x[i].tx_buf = &dumy_value;
-
} else {
-
x[i].rx_buf = w25q->buf++;
-
}
-
}
-
/* do the i/o */
-
return spi_sync(w25q->spi, &message);
- }
說明:
1) 在從芯片讀取數據時的格式爲:首先發送一個字節命令+三個字節讀取地址,然後就可以接收數據了。
2) 第一個for循環發送的是命令和地址,第二個for循環是接收數據。
3) 調用此函數之前,需要設置好w25q->cmd, w25q->len(receive len),w25q->buf(kzalloc
receive)和w25q->addr。
接下來看下函數操作集中的讀數據函數w25q_read(),程序如下:
點擊(此處)摺疊或打開
-
static ssize_t w25q_read(struct file *file, char
__user *user_buf, size_t count, loff_t *ppos)
-
{
-
int ret = 0;
-
u8 *buf_start, *buf_tmp, *w25q_buf;
-
struct w25q_dev *w25q = file->private_data;
-
u32 buf_size = 0, read_len = 0, page_num = W25Q_PAGE_NUM;
-
-
DBG("@@@@w25q read start\n");
-
buf_start = buf_tmp = kzalloc(W25Q_BUF_LEN, GFP_KERNEL);
-
w25q_buf = w25q->buf = kzalloc(page_num, GFP_KERNEL);
-
if (!buf_start || !w25q_buf ) {
-
DBG("!!!!kzalloc error!\n");
-
return -ENOMEM;
-
}
-
ret = mutex_lock_interruptible(&w25q->mlock);
-
if (ret) {
-
DBG("!!!!mutex lock error!\n");
-
goto exit_kfree;
-
}
-
read_len = W25Q_BUF_LEN;
-
buf_size = min(count, read_len);
-
read_len = buf_size;
-
-
w25q->cmd = W25X_READ_DATA;
-
w25q->addr = w25q->const_addr;
-
DBG("w25q->addr = 0x%x\n", w25q->addr);
-
while (buf_size) {
-
w25q->buf = w25q_buf;
-
w25q->len = min(buf_size, page_num);
-
ret = w25q_read_data(w25q);
-
if (ret != 0) {
-
goto exit_lock;
-
}
-
memcpy(buf_tmp, w25q_buf, w25q->len);
-
buf_tmp += w25q->len;
-
buf_size -= w25q->len;
-
w25q->addr += w25q->len;
-
}
-
ret = copy_to_user(user_buf, buf_start, read_len);
-
ret = read_len -ret;
-
-
exit_lock:
-
mutex_unlock(&w25q->mlock);
-
exit_kfree:
-
kfree(buf_start);
-
kfree(w25q_buf);
-
DBG("w25q read stop, ret = %d\n", ret);
-
return ret;
- }
說明:
1) 此函數需要申請兩段內存空間,第一段用於存放從W25Q32BV接收到的數據,第二段用於存放每次接收的數據。
2) 設置好變量後,調用w25q_read_data(w25q)讀取數據。
3) 讀取完成後,將讀取到的數據拷貝到應用層。
三、應用層測試
應用層測試程序如下:
點擊(此處)摺疊或打開
-
/*
-
* first you must erase,
-
* then write, then read
-
* or you can read only
-
*
-
*/
-
#include "w25q.h"
-
-
-
int main(int argc, char **argv)
-
{
-
char str[10] = {0};
-
int fd = 0, ret = 0;
-
unsigned char buffer[BUFSIZE] = {0};
-
unsigned int i = 0, idCmd = 0, num = 0;
-
-
fd = open("/dev/w25q", O_RDWR);
-
if (fd < 0) {
-
printf("Open ADC Device Faild!\n");
-
exit(1);
-
}
-
while(1) {
-
idCmd = 0;
-
printf("please enter the cmd and num :\n");
-
scanf("%s%x", str, &num);
-
//printf("cmd
= %s, idFreq = %d\n", str, idFreq);
-
if (num >= 0) {
-
if (strcmp(str, "SECTOR") == 0) {
-
idCmd = W25Q_SECTOR_ERASE;
-
ret = ioctl(fd, idCmd, &num);
-
if (ret != 0) {
-
printf("sector erase Faild!\n");
-
}
-
} else if(strcmp(str, "HALF") == 0) {
-
idCmd = W25Q_HALF_BLOCK_ERASE;
-
ret = ioctl(fd, idCmd, &num);
-
if (ret != 0) {
-
printf("half block erase Faild!\n");
-
}
-
} else if(strcmp(str, "BLOCK") == 0) {
-
idCmd = W25Q_BLOCK_ERASE;
-
ret = ioctl(fd, idCmd, &num);
-
if (ret != 0) {
-
printf("block erase Faild!\n");
-
}
-
} else if(strcmp(str, "CHIP") == 0) {
-
idCmd = W25Q_CHIP_ERASE;
-
ret = ioctl(fd, idCmd, &num);
-
if (ret != 0) {
-
printf("chip erase Faild!\n");
-
}
-
} else if(strcmp(str, "ID") == 0) {
-
idCmd = W25Q_READ_DEVICE_ID;
-
ret = ioctl(fd, idCmd, &num);
-
if (ret != 0) {
-
printf("read ID Faild!\n");
-
} else {
-
printf("ID = 0x%x\n", num);
-
}
-
} else if(strcmp(str, "READ") == 0) {
-
memset(buffer, 0, BUFSIZE);
-
printf("------------\n");
-
for (i = 0; i < WRITE_NUM; i++) {
-
if((i != 0) && ((i % 8) == 0)) {
-
printf("\n");
-
}
-
printf("0x%x ", buffer[i]);
-
}
-
printf("\n------------\n");
-
ret = read(fd, buffer, WRITE_NUM);
-
printf("\n------------\n");
-
for (i = 0; i < WRITE_NUM; i++) {
-
if((i != 0) && ((i % 8) == 0)) {
-
printf("\n");
-
}
-
printf("0x%x ", buffer[i]);
-
}
-
printf("\n------------\n");
-
} else if(strcmp(str, "WRITE") == 0) {
-
for (i = 0; i < WRITE_NUM; i++) {
-
buffer[i] = i;
-
}
-
ret = write(fd, buffer, WRITE_NUM);
-
if (ret != 0) {
-
printf("w25q write oper Faild!\n");
-
}
-
} else if(strcmp(str, "QUIT") == 0) {
-
break;
-
} else {
-
printf("wrong string\n");
-
}
-
} else {
-
printf("wrong input num(< 0)\n");
-
}
-
}/* end while(1) */
-
close(fd);
-
return 0;
- }
說明:
1) 首先從終端接收命令內容。
2) 比較命令,然後進入不同的處理流程。