linux下spi的驱动开发实例M25P10
来源:互联网 发布:简述域名的含义 编辑:程序博客网 时间:2024/05/24 03:06
m25p10驱动测试
目标:S5PC100平台上编写一个简单的spi驱动模块,在probe阶段实现对m25p10的ID号探测、flash擦除、flash状态读取、flash写入、flash读取等操作。代码已经经过测试,运行于2.6.35内核。理解下面代码需要参照m25p10的芯片手册。其实下面的代码和处理器没有太大关系,这也是spi子系统的分层特点。
urning its value in the location * Return the status register value. * Returns negative if error occurred. */static int read_sr(struct m25p10a *flash){ssize_t retval;u8 code = CMD_RDSR;u8 val;retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);if (retval < 0) {dev_err(&flash->spi->dev, "error %d reading SR\n",(int) retval);return retval;}return val;}/* * Service routine to read status register until ready, or timeout occurs. * Returns non-zero if error. */static int wait_till_ready(struct m25p10a *flash){int count;int sr; /* one chip guarantees max 5 msec wait here after page writes, * but potentially three seconds (!) after page erase. */for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {if ((sr = read_sr(flash)) < 0)break;else if (!(sr & SR_WIP))return 0;/* REVISIT sometimes sleeping would be best */} printk( "in (%s): count = %d\n", count );return 1;}/* * Set write enable latch with Write Enable command. * Returns negative if error occurred. */static inline int write_enable( struct m25p10a *flash ){flash->cmd[0] = CMD_WRITE_ENABLE;return spi_write( flash->spi, flash->cmd, 1 );}/* * Erase the whole flash memory * * Returns 0 if successful, non-zero otherwise. */static int erase_chip( struct m25p10a *flash ){/* Wait until finished previous write command. */if (wait_till_ready(flash))return -1;/* Send write enable, then erase commands. */write_enable( flash );flash->cmd[0] = CMD_BULK_ERASE;return spi_write( flash->spi, flash->cmd, 1 );}/* * Read an address range from the flash chip. The address range * may be any size provided it is within the physical boundaries. */static int m25p10a_read( struct m25p10a *flash, loff_t from, size_t len, char *buf ){int r_count = 0, i;flash->cmd[0] = CMD_READ_BYTES;flash->cmd[1] = from >> 16;flash->cmd[2] = from >> 8;flash->cmd[3] = from;#if 1struct spi_transfer st[2];struct spi_message msg;spi_message_init( &msg );memset( st, 0, sizeof(st) );flash->cmd[0] = CMD_READ_BYTES;flash->cmd[1] = from >> 16;flash->cmd[2] = from >> 8;flash->cmd[3] = from;st[ 0 ].tx_buf = flash->cmd;st[ 0 ].len = CMD_SZ;spi_message_add_tail( &st[0], &msg );st[ 1 ].rx_buf = buf;st[ 1 ].len = len;spi_message_add_tail( &st[1], &msg );mutex_lock( &flash->lock );/* Wait until finished previous write command. */if (wait_till_ready(flash)) {mutex_unlock( &flash->lock );return -1;}spi_sync( flash->spi, &msg );r_count = msg.actual_length - CMD_SZ;printk( "in (%s): read %d bytes\n", __func__, r_count );for( i = 0; i < r_count; i++ ) {printk( "0x%02x\n", buf[ i ] );}mutex_unlock( &flash->lock );#endifreturn 0;}/* * Write an address range to the flash chip. Data must be written in * FLASH_PAGE_SIZE chunks. The address range may be any size provided * it is within the physical boundaries. */static int m25p10a_write( struct m25p10a *flash, loff_t to, size_t len, const char *buf ){int w_count = 0, i, page_offset;struct spi_transfer st[2];struct spi_message msg;#if 1if (wait_till_ready(flash)) { //读状态,等待readymutex_unlock( &flash->lock );return -1;}#endifwrite_enable( flash ); //写使能spi_message_init( &msg );memset( st, 0, sizeof(st) );flash->cmd[0] = CMD_PAGE_PROGRAM;flash->cmd[1] = to >> 16;flash->cmd[2] = to >> 8;flash->cmd[3] = to;st[ 0 ].tx_buf = flash->cmd;st[ 0 ].len = CMD_SZ;spi_message_add_tail( &st[0], &msg );st[ 1 ].tx_buf = buf;st[ 1 ].len = len;spi_message_add_tail( &st[1], &msg );mutex_lock( &flash->lock );/* get offset address inside a page */page_offset = to % FLASH_PAGE_SIZE; /* do all the bytes fit onto one page? */if( page_offset + len <= FLASH_PAGE_SIZE ) {// yesst[ 1 ].len = len; printk("%d, cmd = %d\n", st[ 1 ].len, *(char *)st[0].tx_buf);//while(1){spi_sync( flash->spi, &msg );}w_count = msg.actual_length - CMD_SZ;}else {// no}printk( "in (%s): write %d bytes to flash in total\n", __func__, w_count );mutex_unlock( &flash->lock );return 0;}static int check_id( struct m25p10a *flash ) { char buf[10] = {0}; flash->cmd[0] = CMD_READ_ID;spi_write_then_read( flash->spi, flash->cmd, 1, buf, 3 ); printk( "Manufacture ID: 0x%x\n", buf[0] );printk( "Device ID: 0x%x\n", buf[1] | buf[2] << 8 );return buf[2] << 16 | buf[1] << 8 | buf[0]; } static int m25p10a_probe(struct spi_device *spi) { int ret = 0;struct m25p10a*flash;char buf[ 256 ];printk( "%s was called\n", __func__ );flash = kzalloc( sizeof(struct m25p10a), GFP_KERNEL );if( !flash ) {return -ENOMEM;}flash->spi = spi;mutex_init( &flash->lock );/* save flash as driver's private data */spi_set_drvdata( spi, flash );check_id( flash ); //读取ID#if 1ret = erase_chip( flash ); //擦除if( ret < 0 ) {printk( "erase the entirely chip failed\n" );}printk( "erase the whole chip done\n" );memset( buf, 0x7, 256 );m25p10a_write( flash, 0, 20, buf); //0地址写入20个7memset( buf, 0, 256 );m25p10a_read( flash, 0, 25, buf ); //0地址读出25个数#endifreturn 0; } static int m25p10a_remove(struct spi_device *spi) { return 0; } static struct spi_driver m25p10a_driver = { .probe = m25p10a_probe, .remove = m25p10a_remove, .driver = { .name = "m25p10a", }, }; static int __init m25p10a_init(void) { return spi_register_driver(&m25p10a_driver); } static void __exit m25p10a_exit(void) { spi_unregister_driver(&m25p10a_driver); } module_init(m25p10a_init); module_exit(m25p10a_exit); MODULE_DESCRIPTION("m25p10a driver for FS_S5PC100"); MODULE_LICENSE("GPL");
有兴趣的朋友可以试试
- linux下spi的驱动开发实例M25P10
- Linux下spi驱动开发
- Linux下spi驱动开发
- linux下SPI驱动开发
- Linux下spi驱动开发
- Linux下spi驱动开发
- Linux下spi驱动开发
- linux下spi驱动开发
- Linux下spi驱动开发
- Linux下spi驱动开发
- Linux下spi驱动开发
- Linux下spi驱动开发
- Linux下spi驱动开发
- Linux下的spi驱动
- linux下的spi驱动及测试程序移植开发
- Linux下spi驱动开发(1)
- Linux下spi驱动开发(2)
- Linux下spi驱动开发(1)
- HTML5 游戏示例,只贴图,贴代码,不说话。
- 孙鑫VC学习笔记:第二讲 掌握C++
- setTimeout和setInterval的区别
- ArcSDE数据被锁定后的解锁方法
- 再谈哈希函数与线性探测再散列
- linux下spi的驱动开发实例M25P10
- 代码格式规范工具 astyle 或 indent
- linux fork 精解
- SEO优化中网站主页百度快照消失的最佳解决方法
- 在android c代码中加log
- 代码静态分析工具 PC-Lint
- spring的事务的传播方式
- 跨域访问实现依据
- 逻辑思维能力测试题归纳总结之计算题