#include #include #include #define BUS_NUM 0 static struct spi_device* adxl345_dev = NULL; /** * @brief This function is called, when the module is loaded into the kernel */ static int __init ModuleInit(void) { struct spi_master* master = NULL; u8 id; /* Parameters for SPI device */ struct spi_board_info spi_device_info = { .modalias = "adxl345", .max_speed_hz = 5000000, /* Mhz */ .bus_num = BUS_NUM, .chip_select = 0, .mode = 3, }; /* Get access to spi bus */ master = spi_busnum_to_master(BUS_NUM); /* Check if we could get the master */ if (master == NULL) { printk("There is no spi bus with Nr. %d\n", BUS_NUM); return -1; } /* Create new SPI device. instantiate one new SPI device. defined - include/linux/spi.spi.h */ adxl345_dev = spi_new_device(master, &spi_device_info); if (adxl345_dev == NULL) { printk("Could not create device!\n"); return -1; } adxl345_dev->bits_per_word = 8; /* Setup the bus for device's parameters. Setup SPI mode and clock rate defined - include/linux/spi/spi.h */ if (spi_setup(adxl345_dev) != 0) { printk("Could not change bus setup!\n"); spi_unregister_device(adxl345_dev); return -1; } /* Read Chip ID */ id = spi_w8r8(adxl345_dev, 0x00); printk("Chip ID: 0x%x\n", id); return 0; } /** * @brief This function is called, when the module is removed from the kernel */ static void __exit ModuleExit(void) { if (adxl345_dev) spi_unregister_device(adxl345_dev); } module_init(ModuleInit); module_exit(ModuleExit); /* Meta Information */ MODULE_AUTHOR("Jura"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("A driver for reading out ID ADXL345 Accelerometer");