Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/i2c.h>
  4. #include <linux/delay.h>
  5.  
  6. /* Add your code here */
  7.  
  8. static int nunchuk_probe(struct i2c_client *client, const struct i2c_device_id *id);
  9. static int nunchuk_remove(struct i2c_client *client);
  10. MODULE_LICENSE("GPL");
  11.  
  12. static const struct i2c_device_id nunchuk_id[] = {
  13. { "nunchuk", 0 },
  14. { }
  15. };
  16. MODULE_DEVICE_TABLE(i2c, nunchuk_id);
  17. #ifdef CONFIG_OF
  18. static const struct of_device_id nunchuk_dt_ids[] = {
  19. { .compatible = "nintendo,nunchuk", },
  20. { }
  21. };
  22. MODULE_DEVICE_TABLE(of, nunchuk_dt_ids);
  23. #endif
  24. static struct i2c_driver nunchuk_driver = {
  25. .probe = nunchuk_probe,
  26. .remove = nunchuk_remove,
  27. .id_table = nunchuk_id,
  28. .driver = {
  29. .name = "nunchuk",
  30. .owner = THIS_MODULE,
  31. .of_match_table = of_match_ptr(nunchuk_dt_ids),
  32. },
  33. };
  34. module_i2c_driver(nunchuk_driver);
  35.  
  36.  
  37.  
  38. static char nunchuk_read_registers(struct i2c_client *client)
  39. {
  40. const char read_buffer[6];
  41. mdelay(10);
  42. i2c_smbus_write_byte(client,0x00);
  43. mdelay(10);
  44. i2c_master_recv(client,&read_buffer,6);
  45. return read_buffer[5];
  46.  
  47.  
  48. }
  49.  
  50.  
  51.  
  52. static int nunchuk_probe(struct i2c_client *client, const struct i2c_device_id *id)
  53. {
  54. int ret;
  55. int i=0,zpressed,cpressed,nulti_bit,prvi_bit;
  56. char prvi,drugi;
  57. const char buffer[4] = {0xf0,0x55,0xfb,0x00};
  58. pr_info();
  59. pr_alert("usao");
  60.  
  61. ret = i2c_master_send(client,&buffer[i],2);
  62. udelay(1000);
  63. if (ret < 0)
  64. {
  65. dev_err(&client->dev, "I2C write error\n");
  66. return ret;
  67. }
  68. ret = i2c_master_send(client,&buffer[i+2],2);
  69. if (ret < 0)
  70. {
  71. dev_err(&client->dev, "I2C write error\n");
  72. return ret;
  73. }
  74.  
  75. //citanje
  76. prvi = nunchuk_read_registers(client);
  77. drugi = nunchuk_read_registers(client);
  78.  
  79. nulti_bit = drugi & 0b00000001;
  80. prvi_bit = drugi & 0b00000010;
  81.  
  82. if (nulti_bit == 0)
  83. {
  84. zpressed = 1;
  85. }else
  86. {
  87. zpressed = 0;
  88. }
  89.  
  90. if (prvi_bit == 0)
  91. {
  92. cpressed = 1;
  93. }else
  94. {
  95. cpressed = 0;
  96. }
  97.  
  98.  
  99. if (cpressed==1)
  100. pr_alert("Stisnut c");
  101. if (cpressed==0)
  102. pr_alert("Pusten c");
  103.  
  104. if (zpressed==1)
  105. pr_alert("Stisnut z");
  106. if (zpressed==0)
  107. pr_alert("Pusten z");
  108.  
  109. return 0;
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. static int nunchuk_remove(struct i2c_client *client)
  118. {
  119. pr_info();
  120. pr_alert("izasao");
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement