1.spi_write_then_read
static int s3c24xx_spi_setupxfer(struct spi_device *spi,
struct spi_transfer *t)
{
/*设置了每字长的位数,发送速度*/
bpw = t ? t->bits_per_word : spi->bits_per_word;
hz = t ? t->speed_hz : spi->max_speed_hz;
/*分频值*/
div = clk_get_rate(hw->clk) / hz;
}
int spi_write_then_read(struct spi_device *spi,
const u8 *txbuf, unsigned n_tx,
u8 *rxbuf, unsigned n_rx)
{
static DECLARE_MUTEX(lock);
int status;
struct spi_message message;
struct spi_transfer x[2];
u8 *local_buf;
/* 这里初始化message结构里面用于存放struct spi_transfer指针的链表头 */
spi_message_init(&message);//INIT_LIST_HEAD(&message->transfers);
memset(x, 0, sizeof x);
/* 留意到没有:tx和rx个占一个工作添加到message的struct spi_transfer链表里,稍后被bitbang_work从链表里提出来处理*/
if (n_tx) {
x[0].len = n_tx;
spi_message_add_tail(&x[0], &message);//list_add_tail(&t->transfer_list, &m->transfers);
}
if (n_rx) {
x[1].len = n_rx;
spi_message_add_tail(&x[1], &message);
}
/* ... unless someone else is using the pre-allocated buffer */
local_buf = buf;//采用预分配的缓存吧
/* local_buf的前部分用来存放要发送的数据,后部分用来存放接收到的数据 */
memcpy(local_buf, txbuf, n_tx);
x[0].tx_buf = local_buf;
x[1].rx_buf = local_buf + n_tx;
/* do the i/o */
status = spi_sync(spi, &message);//同步io,等待spi传输完成,然后返回用户所接收的数据和状态
}
int spi_sync(struct spi_device *spi, struct spi_message *message)
{
DECLARE_COMPLETION_ONSTACK(done);//声明一个完成变量
int status;
message->complete = spi_complete;//spi传输完成后的回调函数
message->context = &done;
status = spi_async(spi, message);
if (status == 0)
wait_for_completion(&done);//等待spi传输,调用spi_complete后返回
}
static inline int
spi_async(struct spi_device *spi, struct spi_message *message)
{
message->spi = spi;
return spi->master->transfer(spi, message);//调用spi_bitbang_transfer传输数据
}
int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
{
struct spi_bitbang *bitbang;
unsigned long flags;