5 GPIO144 DRDY signal from PIC24@CPU2010
6 SPI2 AD data receive and CMD send for PIC24@CPU2010
8 Copyright Naoya Takamura, 2011
9 This program based on spike.c
10 ---------------------------------------------
13 Copyright Scott Ellis, 2010
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <linux/init.h>
31 #include <linux/module.h>
33 #include <linux/device.h>
34 #include <linux/mutex.h>
35 #include <linux/slab.h>
36 #include <linux/cdev.h>
37 #include <linux/spi/spi.h>
38 #include <linux/string.h>
39 #include <asm/uaccess.h>
40 #include <linux/dma-mapping.h>
42 #include <linux/gpio.h>
43 #include <linux/interrupt.h>
44 #include <linux/poll.h>
45 #include <linux/wait.h>
46 #include <linux/sched.h>
47 #include <asm/atomic.h>
51 #define SPI_BUFF_SIZE 2048 // for spike_ctl
52 #define USER_BUFF_SIZE 128
56 #define SPI_BUS_SPEED 500000 // Hz
58 #define SPI_DATA_SIZE (965+1) // $含む PICから受信するデータ長 DMA問題のために+1byteしている
60 #define GPIO_DRDY_IN 144 // DRDY Input = GPIO144
62 //#define DEBUG_TOGGLE_OUT // デバッグ時定義する
63 #ifdef DEBUG_TOGGLE_OUT
64 #define GPIO_TOGGLE_OUT 145 // Debug用toggle出力 = GPIO145
69 #define MODULE_NAME "spike-ad"
71 const char this_driver_name[] = MODULE_NAME;
73 #define SPI_CMD_MAX (64) // SPI送信データの最大長
75 // 送信データバッファ(CMD for PIC)
79 struct spike_control {
80 struct spi_message msg;
81 struct spi_transfer transfer;
82 u32 busy; // 1=spi_async()終了待ち
88 int irq; // DRDY信号割り込み
90 struct semaphore cmd_sem;
91 u8 cmd_buf[SPI_CMD_MAX]; // 送信するコマンド
92 int cmd_len; // SPIで送信するコマンド長
95 static struct spike_control spike_ctl;
99 struct semaphore fop_sem;
103 struct spi_device *spi_device;
108 static struct spike_dev spike_dev;
112 unsigned long f_version; // 識別用 f_version
113 wait_queue_head_t wait; /* read and write queues */
114 int sleep_mode; // 0: 起きてる 1: 待ち
115 int intflag; // 割り込みフラグ 1=割り込み入った
118 static FileInfo finfo;
120 void spike_tasklet_func(unsigned long data);
121 DECLARE_TASKLET(spike_tasklet, spike_tasklet_func, 0);
124 /**** SPI受信データ リングバッファ ******************************************
126 #define RING_NUM (30) // リングバッファ個数
127 #define SPI_MAX (SPI_DATA_SIZE+256) // 1回のデータバイト数max
128 static unsigned char spibuf[RING_NUM][SPI_MAX];
136 static atomic_t ring_read;
138 static atomic_t ring_write;
142 atomic_set(&ring_read, 0);
143 atomic_set(&ring_write, 0);
144 memset(spibuf, 0, sizeof(spibuf));
146 #define ring_clear() ring_read_set(ring_write_get())
149 int ring_read_get(void)
151 return atomic_read(&ring_read);
153 void ring_read_set(int i)
155 atomic_set(&ring_read, i);
157 void ring_read_plus(void)
159 atomic_inc(&ring_read);
160 if (atomic_read(&ring_read) >= RING_NUM) atomic_set(&ring_read, 0);
164 int ring_write_get(void)
166 return atomic_read(&ring_write);
168 void ring_write_plus(void)
170 atomic_inc(&ring_write);
171 if (atomic_read(&ring_write) >= RING_NUM) atomic_set(&ring_write, 0);
174 int ring_num_get(void)
178 i = atomic_read(&ring_write) - atomic_read(&ring_read);
179 if (i < 0) i += RING_NUM;
184 unsigned char* ring_get(int ptr)
193 memset(spibuf[atomic_read(&ring_write)], 0, SPI_MAX);
202 if (ring_num_get() >= RING_NUM-1) return 1;
208 spike_queue_spi_write()で開始した
212 static void spike_spi_completion_handler(void *arg)
216 spike_ctl.spi_callbacks++;
219 // 受信したデータをリングバッファに保存
220 p = ring_get(ring_write_get());
221 memcpy(p, spike_ctl.rx_buff, SPI_DATA_SIZE);
223 spike_ctl.received_len = spike_ctl.msg.actual_length;
229 if (finfo.sleep_mode) {
231 wake_up_interruptible(&(finfo.wait)); // 起こす
232 //printk(KERN_INFO "intsel_interrupt: wakeup %ld\n", fi[i].f_version);
233 finfo.sleep_mode = 0;
239 受信データはcallback funcで受ける
241 static int spike_queue_spi_write(void)
246 // struct spi_messageを初期化 ゼロクリア
247 spi_message_init(&spike_ctl.msg);
250 spike_ctl.msg.complete = spike_spi_completion_handler;
251 spike_ctl.msg.context = NULL;
253 memset(spike_ctl.tx_buff, 0, SPI_BUFF_SIZE);
254 memset(spike_ctl.rx_buff, 0, SPI_BUFF_SIZE);
256 if (down_interruptible(&spike_ctl.cmd_sem))
259 memcpy(spike_ctl.tx_buff, spike_ctl.cmd_buf, spike_ctl.cmd_len);
261 memset(spike_ctl.cmd_buf, 0, SPI_CMD_MAX);
262 up(&spike_ctl.cmd_sem);
264 spike_ctl.transfer.tx_buf = spike_ctl.tx_buff;
265 spike_ctl.transfer.rx_buf = spike_ctl.rx_buff;
266 spike_ctl.transfer.len = SPI_DATA_SIZE;
268 spi_message_add_tail(&spike_ctl.transfer, &spike_ctl.msg);
270 spin_lock_irqsave(&spike_dev.spi_lock, flags);
272 if (spike_dev.spi_device)
273 status = spi_async(spike_dev.spi_device, &spike_ctl.msg);
277 spin_unlock_irqrestore(&spike_dev.spi_lock, flags);
289 void spike_tasklet_func(unsigned long data)
293 status = spike_queue_spi_write();
301 DRDY Interrupt Handler
303 static irqreturn_t irq_handler(int irq, void *dev_id)
305 #ifdef DEBUG_TOGGLE_OUT
306 if (gpio_get_value(GPIO_TOGGLE_OUT)) {
307 gpio_set_value(GPIO_TOGGLE_OUT, 0);
309 gpio_set_value(GPIO_TOGGLE_OUT, 1);
313 tasklet_schedule(&spike_tasklet);
318 static ssize_t spike_file_read(struct file *filp, char __user *buff, size_t count,
330 if (down_interruptible(&spike_dev.fop_sem))
333 if (spike_ctl.busy) {
334 sprintf(spike_dev.user_buff, "spike_ctl.busy==1\n");
335 count = strlen(spike_dev.user_buff);
336 up(&spike_dev.fop_sem);
339 //sprintf(spike_dev.user_buff, "DMA\n");
341 sprintf(spike_dev.user_buff,
342 "Status: %d\nTX: %d %d %d %d\nRX: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\nCallback=%d DRDY=%d\n",
343 spike_ctl.msg.status,
344 spike_ctl.tx_buff[0], spike_ctl.tx_buff[1],
345 spike_ctl.tx_buff[2], spike_ctl.tx_buff[3],
346 spike_ctl.rx_buff[0], spike_ctl.rx_buff[1],
347 spike_ctl.rx_buff[2], spike_ctl.rx_buff[3],
348 spike_ctl.rx_buff[4], spike_ctl.rx_buff[5],
349 spike_ctl.rx_buff[6], spike_ctl.rx_buff[7],
350 spike_ctl.rx_buff[8], spike_ctl.rx_buff[9],
351 spike_ctl.rx_buff[10], spike_ctl.rx_buff[11],
352 spike_ctl.rx_buff[12], spike_ctl.rx_buff[13],
353 spike_ctl.rx_buff[14], spike_ctl.rx_buff[15],
354 spike_ctl.spi_callbacks, gpio_get_value(GPIO_DRDY_IN));
357 // status = spike_do_one_message();
358 //status = spike_send_cmd();
359 //status = spike_rcv_data();
363 status = spike_queue_spi_write();
366 sprintf(spike_dev.user_buff,
367 "spike_do_one_message failed : %d\n",
374 len = strlen(spike_dev.user_buff);
379 if (copy_to_user(buff, spike_dev.user_buff, count)) {
380 printk(KERN_ALERT "spike_read(): copy_to_user() failed\n");
387 up(&spike_dev.fop_sem);
393 static int spike_file_open(struct inode *inode, struct file *filp)
397 // printk(KERN_INFO "spike_open: (%Lu)\n", filp->f_version);
399 if (down_interruptible(&spike_dev.fop_sem))
402 if (!spike_dev.user_buff) {
403 spike_dev.user_buff = kmalloc(USER_BUFF_SIZE, GFP_KERNEL);
404 if (!spike_dev.user_buff)
408 if (finfo.f_version != 0) {
409 printk(KERN_INFO "spike_open: busy\n");
413 finfo.f_version = filp->f_version;
414 finfo.sleep_mode = 0;
416 init_waitqueue_head(&(finfo.wait));
417 filp->private_data = (void*)&finfo; // プライベートデータに構造体ポインタ設定
421 up(&spike_dev.fop_sem);
426 static int spike_file_close(struct inode * inode, struct file * file)
428 // printk(KERN_INFO "spike_close: (%Lu)\n",file->f_version);
430 ((FileInfo *)(file->private_data))->f_version = 0;
431 ((FileInfo *)(file->private_data))->sleep_mode = 0;
432 ((FileInfo *)(file->private_data))->intflag = 0;
436 static unsigned int spike_file_poll(struct file *file, struct poll_table_struct *ptab)
438 //printk(KERN_INFO "spike_file_poll: (%ld)\n",file->f_version);
440 if ( ((FileInfo *)(file->private_data))->intflag ) {
442 ((FileInfo *)(file->private_data))->intflag = 0;
443 //printk(KERN_INFO "spike_file_poll: (%ld) interrupted\n",file->f_version);
444 return POLLIN | POLLRDNORM; // 読み込みOK
446 // 割り込み入っていないので待ち行列に追加する 割り込みハンドラが起こす
447 poll_wait(file, &(((FileInfo *)(file->private_data))->wait), ptab);
448 //printk(KERN_INFO "spike_file_poll: (%ld) poll_wait\n",file->f_version);
451 ((FileInfo *)(file->private_data))->sleep_mode = 1;
456 static long spike_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
461 // printk(KERN_INFO "spike_file_ioctl: (%ld)\n",file->f_version);
465 if (down_interruptible(&spike_ctl.cmd_sem))
467 if (copy_from_user(&spike_ctl.cmd_len, (void *)arg, sizeof(int))) {
468 printk(KERN_ALERT "spike_file_ioctl(): copy_from_user() failed\n");
469 up(&spike_ctl.cmd_sem);
472 //printk(KERN_INFO "spike_file_ioctl: CMD_TX_LEN %d\n", spike_ctl.cmd_len);
473 if (spike_ctl.cmd_len > SPI_CMD_MAX) spike_ctl.cmd_len = SPI_CMD_MAX;
474 up(&spike_ctl.cmd_sem);
478 if (down_interruptible(&spike_ctl.cmd_sem))
480 if (copy_from_user(&spike_ctl.cmd_buf, (void *)arg, spike_ctl.cmd_len)) {
481 printk(KERN_ALERT "spike_file_ioctl(): copy_from_user() failed\n");
482 up(&spike_ctl.cmd_sem);
485 //printk(KERN_INFO "spike_file_ioctl: CMD_TX_SET %02X %02X %02X %02X\n", spike_ctl.cmd_buf[0], spike_ctl.cmd_buf[1], spike_ctl.cmd_buf[2], spike_ctl.cmd_buf[3]);
486 up(&spike_ctl.cmd_sem);
491 p = ring_get(ring_read_get());
494 if (copy_to_user((void *)arg, p, SPI_DATA_SIZE)) {
495 printk(KERN_ALERT "spike_file_ioctl(): copy_to_user() failed\n");
499 // SPIで実際に受信しているデータ長を返す
500 case CMD_RECEIVED_LEN_GET:
501 i = spike_ctl.received_len;
502 if (copy_to_user((void *)arg, &i, sizeof(int))) {
503 printk(KERN_ALERT "spike_file_ioctl(): copy_to_user() failed\n");
510 if (copy_to_user((void *)arg, &i, sizeof(int))) {
511 printk(KERN_ALERT "spike_file_ioctl(): copy_to_user() failed\n");
520 printk(KERN_INFO "spike_file_ioctl: unknown cmd=%d\n", cmd);
525 static const struct file_operations spike_fops = {
526 .owner = THIS_MODULE,
527 // .read = spike_file_read,
528 .open = spike_file_open,
529 .release = spike_file_close,
530 .poll = spike_file_poll,
531 .unlocked_ioctl = spike_file_ioctl,
535 static int spike_probe(struct spi_device *spi_device)
539 spin_lock_irqsave(&spike_dev.spi_lock, flags);
540 spike_dev.spi_device = spi_device;
541 spin_unlock_irqrestore(&spike_dev.spi_lock, flags);
546 static int spike_remove(struct spi_device *spi_device)
550 spin_lock_irqsave(&spike_dev.spi_lock, flags);
551 spike_dev.spi_device = NULL;
552 spin_unlock_irqrestore(&spike_dev.spi_lock, flags);
559 static int __init add_spike_device_to_bus(void)
561 struct spi_master *spi_master;
562 struct spi_device *spi_device;
567 spi_master = spi_busnum_to_master(SPI_BUS);
569 printk(KERN_ALERT "spi_busnum_to_master(%d) returned NULL\n",
571 printk(KERN_ALERT "Missing modprobe omap2_mcspi?\n");
575 spi_device = spi_alloc_device(spi_master);
577 put_device(&spi_master->dev);
578 printk(KERN_ALERT "spi_alloc_device() failed\n");
582 spi_device->chip_select = SPI_BUS_CS0;
584 /* Check whether this SPI bus.cs is already claimed */
585 snprintf(buff, sizeof(buff), "%s.%u",
586 dev_name(&spi_device->master->dev),
587 spi_device->chip_select);
589 pdev = bus_find_device_by_name(spi_device->dev.bus, NULL, buff);
591 /* We are not going to use this spi_device, so free it */
592 spi_dev_put(spi_device);
595 * There is already a device configured for this bus.cs
596 * It is okay if it us, otherwise complain and fail.
598 if (pdev->driver && pdev->driver->name &&
599 strcmp(this_driver_name, pdev->driver->name)) {
601 "Driver [%s] already registered for %s\n",
602 pdev->driver->name, buff);
606 spi_device->max_speed_hz = SPI_BUS_SPEED;
607 spi_device->mode = SPI_MODE_0;
608 spi_device->bits_per_word = 8;
609 spi_device->irq = -1;
610 spi_device->controller_state = NULL;
611 spi_device->controller_data = NULL;
612 strlcpy(spi_device->modalias, this_driver_name, SPI_NAME_SIZE);
614 status = spi_add_device(spi_device);
616 spi_dev_put(spi_device);
617 printk(KERN_ALERT "spi_add_device() failed: %d\n",
622 put_device(&spi_master->dev);
627 static struct spi_driver spike_driver = {
629 .name = this_driver_name,
630 .owner = THIS_MODULE,
632 .probe = spike_probe,
633 .remove = __devexit_p(spike_remove),
636 static int __init spike_init_spi(void)
640 spike_ctl.tx_buff = kmalloc(SPI_BUFF_SIZE, GFP_KERNEL | GFP_DMA);
641 if (!spike_ctl.tx_buff) {
643 goto spike_init_error;
646 spike_ctl.rx_buff = kmalloc(SPI_BUFF_SIZE, GFP_KERNEL | GFP_DMA);
647 if (!spike_ctl.rx_buff) {
649 goto spike_init_error;
652 error = spi_register_driver(&spike_driver);
654 printk(KERN_ALERT "spi_register_driver() failed %d\n", error);
655 goto spike_init_error;
658 error = add_spike_device_to_bus();
660 printk(KERN_ALERT "add_spike_to_bus() failed\n");
661 spi_unregister_driver(&spike_driver);
662 goto spike_init_error;
666 spike_dev.spi_device->dev.coherent_dma_mask = 0xFFFFFFFF;
668 spike_ctl.tx_buff = dma_alloc_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, &spike_ctl.tx_dma, GFP_ATOMIC);
669 if (!spike_ctl.tx_buff) {
671 goto spike_init_error;
674 // spike_ctl.rx_buff = kmalloc(SPI_BUFF_SIZE, GFP_KERNEL | GFP_DMA);
675 spike_ctl.rx_buff = dma_alloc_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, &spike_ctl.rx_dma, GFP_ATOMIC);
676 if (!spike_ctl.rx_buff) {
678 goto spike_init_error;
686 if (spike_ctl.tx_buff) {
687 kfree(spike_ctl.tx_buff);
688 spike_ctl.tx_buff = 0;
689 //dma_free_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, spike_ctl.tx_buff, spike_ctl.tx_dma);
690 //spike_ctl.tx_dma = 0;
693 if (spike_ctl.rx_buff) {
694 kfree(spike_ctl.rx_buff);
695 spike_ctl.rx_buff = 0;
696 //dma_free_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, spike_ctl.rx_buff, spike_ctl.rx_dma);
697 //spike_ctl.rx_dma = 0;
704 static int __init spike_init_cdev(void)
708 spike_dev.devt = MKDEV(0, 0);
710 error = alloc_chrdev_region(&spike_dev.devt, 0, 1, this_driver_name);
712 printk(KERN_ALERT "alloc_chrdev_region() failed: %d \n",
717 cdev_init(&spike_dev.cdev, &spike_fops);
718 spike_dev.cdev.owner = THIS_MODULE;
720 error = cdev_add(&spike_dev.cdev, spike_dev.devt, 1);
722 printk(KERN_ALERT "cdev_add() failed: %d\n", error);
723 unregister_chrdev_region(spike_dev.devt, 1);
730 static int __init spike_init_class(void)
732 spike_dev.class = class_create(THIS_MODULE, this_driver_name);
734 if (!spike_dev.class) {
735 printk(KERN_ALERT "class_create() failed\n");
739 if (!device_create(spike_dev.class, NULL, spike_dev.devt, NULL,
741 printk(KERN_ALERT "device_create(..., %s) failed\n",
743 class_destroy(spike_dev.class);
749 int spike_init_gpio(void)
751 if (gpio_request(GPIO_DRDY_IN, "GPIO_DRDY_IN")) {
752 printk(KERN_ALERT "gpio_request(GPIO_DRDY_IN) failed\n");
753 goto init_gpio_fail_1;
756 if (gpio_direction_input(GPIO_DRDY_IN)) {
757 printk(KERN_ALERT "gpio_direction_input(GPIO_DRDY_IN) failed\n");
758 goto init_gpio_fail_2;
760 #ifdef DEBUG_TOGGLE_OUT
761 if (gpio_request(GPIO_TOGGLE_OUT, "GPIO_TOGGLE_OUT")) {
762 printk(KERN_ALERT "gpio_request(GPIO_TOGGLE_OUT) failed\n");
763 goto init_gpio_fail_2;
766 if (gpio_direction_output(GPIO_TOGGLE_OUT, 0)) {
767 printk(KERN_ALERT "gpio_direction_output(GPIO_TOGGLE_OUT) failed\n");
768 goto init_gpio_fail_3;
774 #ifdef DEBUG_TOGGLE_OUT
776 gpio_free(GPIO_TOGGLE_OUT);
780 gpio_free(GPIO_DRDY_IN);
786 void spike_free_gpio(void)
788 gpio_free(GPIO_DRDY_IN);
789 #ifdef DEBUG_TOGGLE_OUT
790 gpio_free(GPIO_TOGGLE_OUT);
793 int spike_init_irq(void)
797 spike_ctl.irq = OMAP_GPIO_IRQ(GPIO_DRDY_IN);
798 result = request_irq(spike_ctl.irq,
800 IRQF_TRIGGER_FALLING,
805 printk(KERN_ALERT "request_irq failed: %d\n", result);
811 void spike_free_irq(void)
813 free_irq(spike_ctl.irq, &spike_ctl);
816 static int __init spike_init(void)
818 memset(&spike_dev, 0, sizeof(spike_dev));
819 memset(&spike_ctl, 0, sizeof(spike_ctl));
821 sema_init(&spike_dev.fop_sem, 1);
822 spin_lock_init(&spike_dev.spi_lock);
824 sema_init(&spike_ctl.cmd_sem, 1);
826 finfo.f_version = 0; // 未使用マーク
830 if (spike_init_cdev() < 0)
833 if (spike_init_class() < 0)
836 if (spike_init_spi() < 0)
838 // DRDY GPIO144 Input config
839 if (spike_init_gpio() < 0)
842 // DRDY GPIO144 Interrupt config
843 if (spike_init_irq() < 0)
847 printk(KERN_INFO "%s %s initialized\n", MODULE_NAME, VERSION);
854 if (spike_ctl.tx_buff)
855 kfree(spike_ctl.tx_buff);
856 if (spike_ctl.rx_buff)
857 kfree(spike_ctl.rx_buff);
860 device_destroy(spike_dev.class, spike_dev.devt);
861 class_destroy(spike_dev.class);
864 cdev_del(&spike_dev.cdev);
865 unregister_chrdev_region(spike_dev.devt, 1);
870 module_init(spike_init);
872 static void __exit spike_exit(void)
874 spi_unregister_device(spike_dev.spi_device);
875 spi_unregister_driver(&spike_driver);
882 device_destroy(spike_dev.class, spike_dev.devt);
883 class_destroy(spike_dev.class);
885 cdev_del(&spike_dev.cdev);
886 unregister_chrdev_region(spike_dev.devt, 1);
888 if (spike_ctl.tx_buff)
889 kfree(spike_ctl.tx_buff);
890 //dma_free_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, spike_ctl.tx_buff, spike_ctl.tx_dma);
892 if (spike_ctl.rx_buff)
893 kfree(spike_ctl.rx_buff);
894 //dma_free_coherent(&(spike_dev.spi_device->dev), SPI_BUFF_SIZE, spike_ctl.rx_buff, spike_ctl.rx_dma);
896 if (spike_dev.user_buff)
897 kfree(spike_dev.user_buff);
899 printk(KERN_INFO "%s %s removed\n", MODULE_NAME, VERSION);
901 module_exit(spike_exit);
903 MODULE_AUTHOR("Naoya Takamura");
904 MODULE_DESCRIPTION("sciLog AD SPI driver based on spike");
905 MODULE_LICENSE("GPL");
906 MODULE_VERSION("1.0");