David Kiliani | 3fedd14 | 2008-11-01 00:39:12 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file me4600_ai.h |
| 3 | * |
| 4 | * @brief Meilhaus ME-4000 analog input subdevice class. |
| 5 | * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de) |
| 6 | * @author Guenter Gebhardt |
| 7 | * @author Krzysztof Gantzke (k.gantzke@meilhaus.de) |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de) |
| 12 | * |
| 13 | * This file is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | */ |
| 27 | |
| 28 | #ifndef _ME4600_AI_H_ |
| 29 | #define _ME4600_AI_H_ |
| 30 | |
| 31 | #include <linux/version.h> |
| 32 | #include "mesubdevice.h" |
| 33 | #include "meioctl.h" |
| 34 | #include "mecirc_buf.h" |
| 35 | |
| 36 | #ifdef __KERNEL__ |
| 37 | |
| 38 | #define ME4600_AI_MAX_DATA 0xFFFF |
| 39 | |
| 40 | #ifdef ME_SYNAPSE |
| 41 | # define ME4600_AI_CIRC_BUF_SIZE_ORDER 8 // 2^n PAGES =>> Maximum value of 1MB for Synapse |
| 42 | #else |
| 43 | # define ME4600_AI_CIRC_BUF_SIZE_ORDER 5 // 2^n PAGES =>> 128KB |
| 44 | #endif |
| 45 | #define ME4600_AI_CIRC_BUF_SIZE PAGE_SIZE<<ME4600_AI_CIRC_BUF_SIZE_ORDER // Buffer size in bytes. |
| 46 | |
| 47 | #ifdef _CBUFF_32b_t |
| 48 | # define ME4600_AI_CIRC_BUF_COUNT ((ME4600_AI_CIRC_BUF_SIZE) / sizeof(uint32_t)) // Size in values |
| 49 | #else |
| 50 | # define ME4600_AI_CIRC_BUF_COUNT ((ME4600_AI_CIRC_BUF_SIZE) / sizeof(uint16_t)) // Size in values |
| 51 | #endif |
| 52 | |
| 53 | #define ME4600_AI_FIFO_HALF 1024 //ME4600_AI_FIFO_COUNT/2 //1024 |
| 54 | #define ME4600_AI_FIFO_MAX_SC 1352 //0.66*ME4600_AI_FIFO_COUNT //1352 |
| 55 | |
| 56 | typedef enum ME4600_AI_STATUS { |
| 57 | ai_status_none = 0, |
| 58 | ai_status_single_configured, |
| 59 | ai_status_stream_configured, |
| 60 | ai_status_stream_run_wait, |
| 61 | ai_status_stream_run, |
| 62 | ai_status_stream_end_wait, |
| 63 | ai_status_stream_end, |
| 64 | ai_status_stream_fifo_error, |
| 65 | ai_status_stream_buffer_error, |
| 66 | ai_status_stream_error, |
| 67 | ai_status_last |
| 68 | } ME4600_AI_STATUS; |
| 69 | |
| 70 | typedef struct me4600_single_config_entry { |
| 71 | unsigned short status; |
| 72 | uint32_t entry; |
| 73 | uint32_t ctrl; |
| 74 | } me4600_single_config_entry_t; |
| 75 | |
| 76 | typedef struct me4600_range_entry { |
| 77 | int min; |
| 78 | int max; |
| 79 | } me4600_range_entry_t; |
| 80 | |
| 81 | typedef struct me4600_ai_ISM { |
| 82 | volatile unsigned int global_read; /**< The number of data read in total. */ |
| 83 | volatile unsigned int read; /**< The number of data read for this chunck. */ |
| 84 | volatile unsigned int next; /**< The number of data request by user. */ |
| 85 | } me4600_ai_ISM_t; |
| 86 | |
| 87 | typedef struct me4600_ai_timeout { |
| 88 | unsigned long start_time; |
| 89 | unsigned long delay; |
| 90 | } me4600_ai_timeout_t; |
| 91 | |
| 92 | /** |
| 93 | * @brief The ME-4000 analog input subdevice class. |
| 94 | */ |
| 95 | typedef struct me4600_ai_subdevice { |
| 96 | /* Inheritance */ |
| 97 | me_subdevice_t base; /**< The subdevice base class. */ |
| 98 | |
| 99 | /* Attributes */ |
| 100 | spinlock_t subdevice_lock; /**< Spin lock to protect the subdevice from concurrent access. */ |
| 101 | spinlock_t *ctrl_reg_lock; /**< Spin lock to protect #ctrl_reg from concurrent access. */ |
| 102 | |
| 103 | /* Hardware feautres */ |
| 104 | unsigned int irq; /**< The interrupt request number assigned by the PCI BIOS. */ |
| 105 | int isolated; /**< Marks if this subdevice is on an optoisolated device. */ |
| 106 | int sh; /**< Marks if this subdevice has sample and hold devices. */ |
| 107 | |
| 108 | unsigned int channels; /**< The number of channels available on this subdevice. */ |
| 109 | me4600_single_config_entry_t single_config[32]; /**< The configuration set for single acquisition. */ |
| 110 | |
| 111 | unsigned int data_required; /**< The number of data request by user. */ |
| 112 | unsigned int fifo_irq_threshold; /**< The user adjusted FIFO high water interrupt level. */ |
| 113 | unsigned int chan_list_len; /**< The length of the user defined channel list. */ |
| 114 | |
| 115 | me4600_ai_ISM_t ISM; /**< The information request by Interrupt-State-Machine. */ |
| 116 | volatile enum ME4600_AI_STATUS status; /**< The current stream status flag. */ |
| 117 | me4600_ai_timeout_t timeout; /**< The timeout for start in blocking and non-blocking mode. */ |
| 118 | |
| 119 | /* Registers *//**< All registers are 32 bits long. */ |
| 120 | unsigned long ctrl_reg; |
| 121 | unsigned long status_reg; |
| 122 | unsigned long channel_list_reg; |
| 123 | unsigned long data_reg; |
| 124 | unsigned long chan_timer_reg; |
| 125 | unsigned long chan_pre_timer_reg; |
| 126 | unsigned long scan_timer_low_reg; |
| 127 | unsigned long scan_timer_high_reg; |
| 128 | unsigned long scan_pre_timer_low_reg; |
| 129 | unsigned long scan_pre_timer_high_reg; |
| 130 | unsigned long start_reg; |
| 131 | unsigned long irq_status_reg; |
| 132 | unsigned long sample_counter_reg; |
| 133 | |
| 134 | unsigned int ranges_len; |
| 135 | me4600_range_entry_t ranges[4]; /**< The ranges available on this subdevice. */ |
| 136 | |
| 137 | /* Software buffer */ |
| 138 | me_circ_buf_t circ_buf; /**< Circular buffer holding measurment data. */ |
| 139 | wait_queue_head_t wait_queue; /**< Wait queue to put on tasks waiting for data to arrive. */ |
| 140 | |
| 141 | struct workqueue_struct *me4600_workqueue; |
| 142 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) |
| 143 | struct work_struct ai_control_task; |
| 144 | #else |
| 145 | struct delayed_work ai_control_task; |
| 146 | #endif |
| 147 | |
| 148 | volatile int ai_control_task_flag; /**< Flag controling reexecuting of control task */ |
| 149 | |
| 150 | #ifdef MEDEBUG_DEBUG_REG |
| 151 | unsigned long reg_base; |
| 152 | #endif |
| 153 | } me4600_ai_subdevice_t; |
| 154 | |
| 155 | /** |
| 156 | * @brief The constructor to generate a ME-4000 analog input subdevice instance. |
| 157 | * |
| 158 | * @param reg_base The register base address of the device as returned by the PCI BIOS. |
| 159 | * @param channels The number of analog input channels available on this subdevice. |
| 160 | * @param channels The number of analog input ranges available on this subdevice. |
| 161 | * @param isolated Flag indicating if this device is opto isolated. |
| 162 | * @param sh Flag indicating if sample and hold devices are available. |
| 163 | * @param irq The irq number assigned by PCI BIOS. |
| 164 | * @param ctrl_reg_lock Pointer to spin lock protecting the control register from concurrent access. |
| 165 | * |
| 166 | * @return Pointer to new instance on success.\n |
| 167 | * NULL on error. |
| 168 | */ |
| 169 | me4600_ai_subdevice_t *me4600_ai_constructor(uint32_t reg_base, |
| 170 | unsigned int channels, |
| 171 | unsigned int ranges, |
| 172 | int isolated, |
| 173 | int sh, |
| 174 | int irq, |
| 175 | spinlock_t * ctrl_reg_lock, |
| 176 | struct workqueue_struct |
| 177 | *me4600_wq); |
| 178 | |
| 179 | #endif |
| 180 | #endif |