|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Bjarke Vad Andersen |
| 4 | + * |
| 5 | + * @section LICENSE |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU General Public License as |
| 9 | + * published by the Free Software Foundation; either version 2 of |
| 10 | + * the License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, but |
| 13 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * General Public License for more details at |
| 16 | + * http://www.gnu.org/copyleft/gpl.html |
| 17 | + * |
| 18 | + * @section DESCRIPTION |
| 19 | + * |
| 20 | + * This kernel module is written to used on a DevKit8000, |
| 21 | + * and is supposed used with a PSoC5 device connected trough SPI. |
| 22 | + * This file contains the character driver properties of the SPI kernel module. |
| 23 | + * This includes init, exit, read, write, open and release methods. |
| 24 | + * |
| 25 | + */ |
1 | 26 | #include <linux/err.h>
|
2 | 27 | #include <linux/ctype.h>
|
3 | 28 | #include <linux/cdev.h>
|
|
6 | 31 | #include <linux/input.h>
|
7 | 32 | #include <linux/module.h>
|
8 | 33 |
|
| 34 | +/** |
| 35 | + * @brief The Major number of the node. |
| 36 | + */ |
9 | 37 | #define PSOC5_MAJOR 62
|
| 38 | +/** |
| 39 | + * @brief The Minor number of the node. |
| 40 | + */ |
10 | 41 | #define PSOC5_MINOR 0
|
| 42 | +/** |
| 43 | + * @brief The maximum length for data to write to the device node in one function call. |
| 44 | + * */ |
11 | 45 | #define MAXLEN 512
|
| 46 | +/** |
| 47 | + * @brief The GPIO pin which is used for receiving "word complete" interrupts from the PSoC5. |
| 48 | + */ |
| 49 | +#define INTGPIO 130 //7/137 |
12 | 50 |
|
| 51 | + |
| 52 | +/** |
| 53 | + * @brief The init method. |
| 54 | + * |
| 55 | + * This method is called whenever the module is inserted into the kernel. |
| 56 | + * Here the basic setup and registering of the character driver takes place, including requesting |
| 57 | + * GPIO resources for the interrupt pin, and setting said GPIO to input. |
| 58 | + * psoc5_spi_init() is called to initialize the SPI part of the module. |
| 59 | + * |
| 60 | + * @return 0 on success. |
| 61 | + */ |
13 | 62 | static int __init psoc5_cdrv_init(void);
|
| 63 | +/* |
| 64 | + * @brief The exit method. |
| 65 | + * |
| 66 | + * This method is called whenever the module is removed from the kernel. |
| 67 | + * The character device is unregistered and deleted, and the requested GPIO resources are freed. |
| 68 | + */ |
14 | 69 | static void __exit psoc5_cdrv_exit(void);
|
15 |
| - |
| 70 | +/** |
| 71 | + * @brief The open method. |
| 72 | + * |
| 73 | + * This method is called whenever a user is trying to read from or write to the device. |
| 74 | + * Here interrupt resources are requested. This is done here and not in the init method |
| 75 | + * to prevent allocating resources that are not used. |
| 76 | + * |
| 77 | + * @return 0 on success. |
| 78 | + */ |
16 | 79 | int psoc5_cdrv_open(struct inode *inodep, struct file *filep);
|
| 80 | +/** |
| 81 | + * @brief The release method. |
| 82 | + * |
| 83 | + * This method is called when a user is closing an open PSoC5 device, |
| 84 | + * e.g. after reading from the device. |
| 85 | + * Here interrupt line resources are freed. |
| 86 | + * |
| 87 | + * @return 0 on success. |
| 88 | + */ |
17 | 89 | int psoc5_cdrv_release(struct inode *inodep, struct file *filep);
|
18 |
| - |
| 90 | +/** |
| 91 | + * @brief The write method. |
| 92 | + * |
| 93 | + * This method is called when a user is writing to the device. |
| 94 | + * Prevents the user from sending strings longer than MAXLEN, |
| 95 | + * and sends the data received from userspace through SPI by calling "psoc5_spi_write()". |
| 96 | + * |
| 97 | + * @param ubuf The received data from userspace, represented as a character array. |
| 98 | + * @param count The length of the received character array. |
| 99 | + * |
| 100 | + * @return The length of the string written to the device. |
| 101 | + */ |
19 | 102 | ssize_t psoc5_cdrv_write(struct file *filep, const char __user *ubuf,
|
20 | 103 | size_t count, loff_t *f_pos);
|
| 104 | +/** |
| 105 | + * @brief The read method. |
| 106 | + * |
| 107 | + * This method is called when a user is trying to read from the device. |
| 108 | + * Waits for an interrupt, and then continues to read out the data from the SPI, |
| 109 | + * and copies them to userspace. |
| 110 | + * If no interrupt is received within 1000 jiffies 0 is sent to userspace. |
| 111 | + * |
| 112 | + * @param ubuf The address on which the user expects data to be copied to. |
| 113 | + * @return The length of the received character array. |
| 114 | + */ |
21 | 115 | ssize_t psoc5_cdrv_read(struct file *filep, char __user *ubuf,
|
22 | 116 | size_t count, loff_t *f_pos);
|
| 117 | + |
| 118 | +/** |
| 119 | + * @brief The interrupt service routine. |
| 120 | + * |
| 121 | + * This ISR is called whenever a interrupt is received on the assigned GPIO pin, |
| 122 | + * meaning that a new word is received, it sets newData to 1. |
| 123 | + * |
| 124 | + * @return IRQ_HANDLED. |
| 125 | + */ |
| 126 | +irqreturn_t psoc5_isr(int irq, void *dev_id); |
| 127 | + |
| 128 | +/** |
| 129 | + * @brief The character device struct. |
| 130 | + * |
| 131 | + * Used to register the device in the kernel. |
| 132 | + * Contains all information of the character driver characteristics of the module, |
| 133 | + * including the file_operations struct. |
| 134 | + */ |
| 135 | +static struct cdev psoc5Dev; |
| 136 | +/** |
| 137 | + * @brief The file operations struct |
| 138 | + * |
| 139 | + * Used to define which operations are called when the device |
| 140 | + * is used as a file/node. |
| 141 | + */ |
| 142 | +struct file_operations psoc5_Fops; |
| 143 | +/** |
| 144 | + * @brief A static in that contains the major/minor number characteristics returned from MKDEV. |
| 145 | + */ |
| 146 | +static int devno; |
| 147 | +/** |
| 148 | + * @brief The wait queue which psoc5_cdrv_read() waits for. |
| 149 | + * |
| 150 | + * This wait queue is used to signal the character driver read method |
| 151 | + * from the ISR, that new data is ready from the PSoC5 device. |
| 152 | + */ |
| 153 | +DECLARE_WAIT_QUEUE_HEAD(wait_queue); |
| 154 | +/** |
| 155 | + * @brief The variable that is checked to see if new data is ready. |
| 156 | + */ |
| 157 | +int newData = 0; |
0 commit comments