1: postcore_initcall(spi_init); /* 註冊 */
說明:
1) 由postcore_initcall(spi_init);能夠看出,此宏在系統初始化時是先於module_init()執行的。less
2) 申請的buf空間用於在spi數據傳輸中。異步
3) 接下來是總線註冊和類註冊。async
二:函數
此函數是半雙工的形式寫then讀post
1: int spi_write_then_read(struct spi_device *spi,
2: const void *txbuf, unsigned n_tx,
3: void *rxbuf, unsigned n_rx)
4: {
5: static DEFINE_MUTEX(lock);
6:
7: int status;
8: struct spi_message message;
9: struct spi_transfer x[2];
10: u8 *local_buf;
11:
12: /* Use preallocated DMA-safe buffer. We can't avoid copying here,
13: * (as a pure convenience thing), but we can keep heap costs
14: * out of the hot path ...
15: */
16: if ((n_tx + n_rx) > SPI_BUFSIZ)
17: return -EINVAL;
18:
19: spi_message_init(&message);
20: memset(x, 0, sizeof x);
21: if (n_tx) {
22: x[0].len = n_tx;
23: spi_message_add_tail(&x[0], &message);
24: }
25: if (n_rx) {
26: x[1].len = n_rx;
27: spi_message_add_tail(&x[1], &message);
28: }
29:
30: /* ... unless someone else is using the pre-allocated buffer */
31: if (!mutex_trylock(&lock)) {
32: local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
33: if (!local_buf)
34: return -ENOMEM;
35: } else
36: local_buf = buf;
37:
38: memcpy(local_buf, txbuf, n_tx);
39: x[0].tx_buf = local_buf;
40: x[1].rx_buf = local_buf + n_tx;
41:
42: /* do the i/o */
43: status = spi_sync(spi, &message);
44: if (status == 0)
45: memcpy(rxbuf, x[1].rx_buf, n_rx);
46:
47: if (x[0].tx_buf == buf)
48: mutex_unlock(&lock);
49: else
50: kfree(local_buf);
51:
52: return status;
53: }
1: 對master操做的加鎖與解鎖
2: int spi_bus_lock(struct spi_master *master)
3: {
4: unsigned long flags;
5:
6: mutex_lock(&master->bus_lock_mutex);
7:
8: spin_lock_irqsave(&master->bus_lock_spinlock, flags);
9: master->bus_lock_flag = 1;
10: spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
11:
12: /* mutex remains locked until spi_bus_unlock is called */
13:
14: return 0;
15: }
16: int spi_bus_unlock(struct spi_master *master)
17: {
18: master->bus_lock_flag = 0;
19:
20: mutex_unlock(&master->bus_lock_mutex);
21:
22: return 0;
23: }
同步數據交互
1: int spi_sync(struct spi_device *spi, struct spi_message *message)
2: {
3: return __spi_sync(spi, message, 0);
4: }
5: int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
6: {
7: return __spi_sync(spi, message, 1);
8: }