SPI driver writing
In short, the writing of the SPI driver is divided:
1. Build and register the spi_device
Add in the Board FileSpi_board_info, which is called in the init function of the Board FileSpi_register_board_info (cloud_spi_devs, array_size (cloud_spi_devs ));
Spi_register_board_info(Cloud_spi_devs, array_size (cloud_spi_devs); // register spi_board_info. This code registers spi_board_info to the linked list board_list. Spi_device encapsulates a spi_master struct. In fact, the registration of spi_master will call scan_boardinfo to scan board_list and find the SPI device attached to it after spi_register_board_info, then create and register the spi_device.
2.Build and register spi_driver
(1)
Static struct spi_driver m25p80_driver = {
. Driver = {
. Name = "m25p80 ",
. Bus = & spi_bus_type,
. Owner = this_module,
},
. Probe = m25p_probe,
. Remove =__ devexit_p (m25p_remove ),
};
(2)// Register spi_driver
Spi_register_driver (& m25p80_driver );Spi_deviceCall probe later
(3) Implement the probe operation:
Spi_transfer (which integrates data Buf space addresses and other information)
Construction of spi_message (a collection of spi_transfer;
Spi_message_init (initialize spi_message)
Spi_message_add_tail (Add the new spi_transfer to the end of the spi_message Queue)
Spi_sync function call (call spi_master to send spi_message)
For example:
Struct spi_transfer ST = {
.........
};
// Fill in spi_transfer
Struct spi_message Meg;
// Define message
Spi_init_message (& MEG );
// Initialize Meg
Spi_message_add_tail (& St, & MEG );
// Put the st at the end of the message queue
Spi_sync (spi_device, & MEG );
// Associate message with spi_device and send Meg