zlg9518S spi驅動


static int zlg9518s_read( struct zlg9518s *flash, char cmd, char para, 
size_t len, char *buf )//讀 命令cmd和數據para ,buf  spi 讀出的內容存放的地方
{
int r_count = 0, i;


flash->cmd[0] = cmd;
flash->cmd[1] = para;

struct spi_transfer st[2];
struct spi_message  msg;

spi_message_init( &msg );
memset( st, 0, sizeof(st) );


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 (ren爲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 );




return 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 zlg9518s_write( struct zlg9518s *flash, char cmd,
size_t len, const char *buf )//寫 命令cmd 和 數據buf
{
int w_count = 0;
struct spi_transfer st[2];
struct spi_message  msg;

spi_message_init( &msg );
memset( st, 0, sizeof(st) );


flash->cmd[0] = cmd;


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 );
spi_sync( flash->spi, &msg );
w_count = msg.actual_length - CMD_SZ-1;

printk( "in (%s): write %d bytes to flash in total\n", __func__, w_count );
mutex_unlock( &flash->lock );
return 0;
}

發佈了27 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章