Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/spi/spi.h>
  4.  
  5. int ask_2272_1527_pulse_duty_factor[4]={13,5,7,11};
  6.  
  7. struct spi_device *spi;
  8.  
  9. u16 word;
  10. //unsigned char wordx[2] = "xy";
  11. //unsigned char *word = (unsigned char *)wordx;
  12.  
  13. void
  14. rfm12_ask_encode_tribit(int *code, int append, int byte, int cnt)
  15. {
  16.     int i;
  17.     for (i=0;i<cnt;i++)
  18.     {
  19.         if (byte & (1<<(cnt-i-1))) {
  20.             code[append+(i*2)]=ask_2272_1527_pulse_duty_factor[0];
  21.             code[append+(i*2)+1]=ask_2272_1527_pulse_duty_factor[1];
  22.         } else {
  23.             code[append+(i*2)]=ask_2272_1527_pulse_duty_factor[2];
  24.             code[append+(i*2)+1]=ask_2272_1527_pulse_duty_factor[3];
  25.         }
  26.     }
  27. }
  28.  
  29. void
  30. rfm12_ask_trigger(int level, int us)
  31. {
  32.     if (level)
  33.     {
  34.         word = 0x8200|(1<<5)|(1<<4)|(1<<3);
  35.         spi_write(spi, (const u8 *)&word, 2); // 2. PwrMngt TX on
  36.         for(;us>0;us--)
  37.             udelay(1);
  38.     }
  39.     else
  40.     {
  41.         word = 0x8208;
  42.         spi_write(spi, (const u8 *)&word, 2);                                           // 2. PwrMngt TX off
  43.         for(;us>0;us--)
  44.             udelay(1);
  45.     }
  46. }
  47.  
  48. void
  49. rfm12_ask_2272_send(int *command, int delay, int cnt)
  50. {
  51.     int code[49];
  52.  
  53.     int i;
  54.     printk(KERN_ALERT "encoding\n");
  55.     for(i=0;i<3;i++)
  56.     {
  57.         rfm12_ask_encode_tribit(code, i*16, command[i], 8);
  58.     }
  59.     code[48]=7; //sync
  60. //  rfm12_prologue ();
  61.     word = 0x8200|(1<<5)|(1<<4)|(1<<3);
  62.     printk(KERN_ALERT "??\n");
  63.     spi_write(spi, (const u8 *)&word, 2);    // 2. PwrMngt TX on
  64.     int ii;
  65.     for(ii=cnt;ii>0;ii--)                            // Sequenz cnt send
  66.     {
  67.         int rfm12_trigger_level=0;
  68.         int i;
  69.         for(i=0;i<49;i++)
  70.         {
  71.         printk(KERN_ALERT "running trigger\n");
  72.             rfm12_ask_trigger(rfm12_trigger_level^=1,code[i]*delay);
  73.         }
  74.         printk(KERN_ALERT "running trigger (2)\n");
  75.         rfm12_ask_trigger(0,24*delay);
  76.     }
  77.     word = 0x8208;
  78.     printk(KERN_ALERT "set <0x8208>\n");
  79.     spi_write(spi, (const u8 *)&word, 2);                                               // 2. PwrMngt TX off
  80. //  rfm12_epilogue ();
  81. }
  82.  
  83. #define RFM12BAND(freq) (freq<800000?0x80D7:0x80E7)
  84.  
  85. static int __devinit rfm12_probe(struct spi_device *spi_)
  86. {
  87.     printk(KERN_ALERT "probing\n");
  88.     spi = spi_;
  89.     printk(KERN_ALERT "setting mode\n");
  90.     spi->mode = SPI_MODE_0;
  91.     printk(KERN_ALERT "setting word size\n");
  92.     spi->bits_per_word = 16;
  93.     printk(KERN_ALERT "setup spi device\n");
  94.     spi_setup(spi);
  95.  
  96.     //word = 0x0000;
  97.     //spi_write(spi, (const u8 *)&word, 2);
  98.  
  99.     word = 0x0000;
  100.     printk(KERN_ALERT "get status\n");
  101.     spi_write(spi, (const u8 *)&word, 2);
  102.     spi_read(spi, (u8 *)&word, 2);
  103.     printk(KERN_ALERT "got %x\n", word);
  104.  
  105.     word = 0xC0E0;
  106.     printk(KERN_ALERT "set <1>\n");
  107.     spi_write(spi, (const u8 *)&word, 2);       /* AVR CLK: 10MHz */
  108.     word = RFM12BAND(433920);
  109.     printk(KERN_ALERT "set <2>\n");
  110.     spi_write(spi, (const u8 *)&word, 2);   /* Select BAND, Enable FIFO */
  111.     word = 0xC2AB;
  112.     printk(KERN_ALERT "set <3>\n");
  113.     spi_write(spi, (const u8 *)&word, 2);       /* Data Filter: internal */
  114.     word = 0xCA81;
  115.     printk(KERN_ALERT "set <4>\n");
  116.     spi_write(spi, (const u8 *)&word, 2);       /* Set FIFO mode */
  117.     word = 0xE000;
  118.     printk(KERN_ALERT "set <5>\n");
  119.     spi_write(spi, (const u8 *)&word, 2);       /* disable wakeuptimer */
  120.     word = 0xC800;
  121.     printk(KERN_ALERT "set <6>\n");
  122.     spi_write(spi, (const u8 *)&word, 2);       /* disable low duty cycle */
  123.     word = 0xC4F7;
  124.     printk(KERN_ALERT "set <7>\n");
  125.     spi_write(spi, (const u8 *)&word, 2);       /* AFC settings: autotuning: -10kHz...+7,5kHz */
  126.  
  127.     int command[3];
  128.     command[0] = 0;
  129.     command[1] = 5;
  130.     command[2] = 81;
  131.     printk(KERN_ALERT "sending (0,5,81), 76, 10\n");
  132.     rfm12_ask_2272_send(command, 76, 10);
  133.     printk(KERN_ALERT "finished\n");
  134.  
  135.     return 0;
  136. }
  137.  
  138. static struct spi_driver rfm12_driver = {
  139.     .driver = {
  140.         .name = "rfm12",
  141.         .owner = THIS_MODULE,
  142.     },
  143.     .probe = rfm12_probe,
  144. };
  145.  
  146. int init_module(void)
  147. {
  148.     printk(KERN_ALERT "init module\n");
  149.     printk(KERN_ALERT "register spi driver\n");
  150.     int res_spi_register_driver = spi_register_driver(&rfm12_driver);
  151.     //int res_setup_sysfs = setup_sysfs();
  152.     //return res_spi_register_driver | res_setup_sysfs;
  153.     return res_spi_register_driver;
  154. }
  155.  
  156. void cleanup_module(void)
  157. {
  158.     spi_unregister_driver(&rfm12_driver);
  159. }
  160.  
  161. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement