annotate packages/devs/adc/synth/current/src/adc_synth.c @ 2844:defb62be5b44

* cdl/adc_synth.cdl: * cdl/adc_synth.c: Fixed some typos.
author nickg
date Fri, 06 Mar 2009 13:49:31 +0000
parents 8fe6d83b6d48
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2842
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
1 //==========================================================================
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
2 //
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
3 // adc_synth.c
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
4 //
2844
defb62be5b44 * cdl/adc_synth.cdl:
nickg
parents: 2842
diff changeset
5 // ADC driver for Synthetic ADC
2842
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
6 //
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
7 //==========================================================================
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
8 // ####ECOSGPLCOPYRIGHTBEGIN####
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
9 // -------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
10 // This file is part of eCos, the Embedded Configurable Operating System.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
11 // Copyright (C) 2009 Free Software Foundation, Inc.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
12 //
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
13 // eCos is free software; you can redistribute it and/or modify it under
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
14 // the terms of the GNU General Public License as published by the Free
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
15 // Software Foundation; either version 2 or (at your option) any later
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
16 // version.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
17 //
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
18 // eCos is distributed in the hope that it will be useful, but WITHOUT
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
19 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
21 // for more details.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
22 //
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
23 // You should have received a copy of the GNU General Public License
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
24 // along with eCos; if not, write to the Free Software Foundation, Inc.,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
25 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
26 //
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
27 // As a special exception, if other files instantiate templates or use
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
28 // macros or inline functions from this file, or you compile this file
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
29 // and link it with other works to produce a work based on this file,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
30 // this file does not by itself cause the resulting work to be covered by
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
31 // the GNU General Public License. However the source code for this file
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
32 // must still be made available in accordance with section (3) of the GNU
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
33 // General Public License v2.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
34 //
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
35 // This exception does not invalidate any other reasons why a work based
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
36 // on this file might be covered by the GNU General Public License.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
37 // -------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
38 // ####ECOSGPLCOPYRIGHTEND####
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
39 //==========================================================================
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
40 //#####DESCRIPTIONBEGIN####
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
41 //
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
42 // Author(s): Simon Kallweit <simon.kallweit@intefo.ch>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
43 // Contributors:
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
44 // Date: 2009-02-27
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
45 // Purpose:
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
46 // Description:
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
47 //
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
48 //####DESCRIPTIONEND####
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
49 //
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
50 //==========================================================================
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
51
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
52 #include <pkgconf/kernel.h>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
53 #include <pkgconf/devs_adc_synth.h>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
54
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
55 #include <cyg/infra/cyg_type.h>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
56 #include <cyg/infra/cyg_ass.h>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
57 #include <cyg/io/adc.h>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
58 #include <cyg/hal/hal_arch.h>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
59 #include <cyg/hal/hal_io.h>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
60 #include <cyg/hal/hal_intr.h>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
61 #include <cyg/hal/drv_api.h>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
62
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
63 //-----------------------------------------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
64 // Diagnostic support
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
65 // Switch the #if to 1 to generate some diagnostic messages.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
66
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
67 #if 0
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
68 #include <cyg/infra/diag.h>
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
69 #define adc_diag( __fmt, ... ) diag_printf("ADC: %30s[%4d]: " __fmt, __FUNCTION__, __LINE__, ## __VA_ARGS__ );
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
70 #else
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
71 #define adc_diag( __fmt, ... )
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
72 #endif
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
73
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
74 #define NUM_CHANNELS 16
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
75
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
76 #define MODE_CONST 0
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
77 #define MODE_RANDOM 1
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
78 #define MODE_FILE 2
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
79
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
80 #define SAMPLE_BITS ((1 << CYGNUM_DEVS_ADC_SYNTH_SAMPLE_SIZE) - 1)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
81
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
82 //-----------------------------------------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
83 // Synthetic ADC channel
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
84
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
85 typedef struct synth_adc_channel_info {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
86 cyg_uint32 mode; // Channel mode
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
87 cyg_uint32 const_value; // Const sample value
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
88 char *filename; // Sampling data filename
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
89
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
90 int fd; // File descriptor of sample file
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
91 cyg_uint32 num_samples; // Number of samples in the file
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
92 cyg_adc_sample_t *base; // Base address of mapped sample file
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
93 cyg_adc_sample_t *sample; // Current sample
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
94
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
95 cyg_adc_sample_t (*get_sample)(cyg_adc_channel *chan);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
96 } synth_adc_channel_info;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
97
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
98 //-----------------------------------------------------------------------------
2844
defb62be5b44 * cdl/adc_synth.cdl:
nickg
parents: 2842
diff changeset
99 // Synthetic ADC device
2842
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
100
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
101 typedef struct synth_adc_info {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
102 synth_adc_channel_info *chan_info; // Channel infos
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
103 cyg_adc_channel *chan[NUM_CHANNELS]; // Channel references
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
104 cyg_uint32 chan_mask; // Active channels
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
105 cyg_handle_t alarm_handle; // Alarm handle
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
106 cyg_alarm alarm_data; // Alarm data
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
107 cyg_tick_count_t alarm_interval; // Alarm interval in ticks
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
108 cyg_uint32 alarm_samples; // Number of samples per tick
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
109 } synth_adc_info;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
110
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
111 //-----------------------------------------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
112 // API function call forward references
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
113
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
114 static bool synth_adc_init(struct cyg_devtab_entry *tab);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
115 static Cyg_ErrNo synth_adc_lookup(struct cyg_devtab_entry **tab,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
116 struct cyg_devtab_entry *sub_tab,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
117 const char *name);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
118
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
119 static void synth_adc_enable(cyg_adc_channel *chan);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
120 static void synth_adc_disable(cyg_adc_channel *chan);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
121 static void synth_adc_set_rate(cyg_adc_channel *chan, cyg_uint32 rate);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
122
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
123 static void alarm_handler(cyg_handle_t alarm, cyg_addrword_t data);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
124
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
125 static cyg_adc_sample_t synth_adc_get_sample_const(cyg_adc_channel *chan);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
126 static cyg_adc_sample_t synth_adc_get_sample_random(cyg_adc_channel *chan);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
127 static cyg_adc_sample_t synth_adc_get_sample_file(cyg_adc_channel *chan);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
128
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
129 static cyg_uint32 rand(void);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
130
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
131 CYG_ADC_FUNCTIONS(synth_adc_funs,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
132 synth_adc_enable,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
133 synth_adc_disable,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
134 synth_adc_set_rate);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
135
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
136 //-----------------------------------------------------------------------------
2844
defb62be5b44 * cdl/adc_synth.cdl:
nickg
parents: 2842
diff changeset
137 // Synthetic ADC channel info macro
2842
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
138
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
139 #define SYNTH_ADC_CHANNEL_INFO(_chan_) \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
140 { \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
141 .mode = CYGNUM_DEVS_ADC_SYNTH_CHANNEL##_chan_##_MODE, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
142 .const_value = CYGNUM_DEVS_ADC_SYNTH_CHANNEL##_chan_##_CONST_VALUE, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
143 .filename = CYGDAT_DEVS_ADC_SYNTH_CHANNEL##_chan_##_FILENAME, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
144 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
145
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
146 //-----------------------------------------------------------------------------
2844
defb62be5b44 * cdl/adc_synth.cdl:
nickg
parents: 2842
diff changeset
147 // Synthetic ADC channel instance macro
2842
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
148
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
149 #define SYNTH_ADC_CHANNEL(_chan_) \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
150 CYG_ADC_CHANNEL( \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
151 synth_adc_channel##_chan_, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
152 _chan_, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
153 CYGNUM_DEVS_ADC_SYNTH_CHANNEL##_chan_##_BUFSIZE, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
154 &synth_adc_device \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
155 ); \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
156 DEVTAB_ENTRY( \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
157 synth_adc_channel##_chan_##_device, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
158 CYGDAT_DEVS_ADC_SYNTH_CHANNEL##_chan_##_NAME, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
159 0, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
160 &cyg_io_adc_devio, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
161 synth_adc_init, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
162 synth_adc_lookup, \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
163 &synth_adc_channel##_chan_ \
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
164 );
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
165
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
166 //-----------------------------------------------------------------------------
2844
defb62be5b44 * cdl/adc_synth.cdl:
nickg
parents: 2842
diff changeset
167 // Synthetic ADC device instance
2842
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
168
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
169 static synth_adc_channel_info synth_adc_channel_infos[NUM_CHANNELS] = {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
170 SYNTH_ADC_CHANNEL_INFO(0),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
171 SYNTH_ADC_CHANNEL_INFO(1),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
172 SYNTH_ADC_CHANNEL_INFO(2),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
173 SYNTH_ADC_CHANNEL_INFO(3),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
174 SYNTH_ADC_CHANNEL_INFO(4),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
175 SYNTH_ADC_CHANNEL_INFO(5),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
176 SYNTH_ADC_CHANNEL_INFO(6),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
177 SYNTH_ADC_CHANNEL_INFO(7),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
178 SYNTH_ADC_CHANNEL_INFO(8),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
179 SYNTH_ADC_CHANNEL_INFO(9),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
180 SYNTH_ADC_CHANNEL_INFO(10),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
181 SYNTH_ADC_CHANNEL_INFO(11),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
182 SYNTH_ADC_CHANNEL_INFO(12),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
183 SYNTH_ADC_CHANNEL_INFO(13),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
184 SYNTH_ADC_CHANNEL_INFO(14),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
185 SYNTH_ADC_CHANNEL_INFO(15),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
186 };
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
187
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
188 static synth_adc_info synth_adc_info0 = {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
189 .chan_info = synth_adc_channel_infos,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
190 };
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
191
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
192 CYG_ADC_DEVICE(synth_adc_device,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
193 &synth_adc_funs,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
194 &synth_adc_info0,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
195 CYGNUM_DEVS_ADC_SYNTH_DEFAULT_RATE);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
196
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
197 SYNTH_ADC_CHANNEL(0)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
198 SYNTH_ADC_CHANNEL(1)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
199 SYNTH_ADC_CHANNEL(2)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
200 SYNTH_ADC_CHANNEL(3)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
201 SYNTH_ADC_CHANNEL(4)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
202 SYNTH_ADC_CHANNEL(5)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
203 SYNTH_ADC_CHANNEL(6)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
204 SYNTH_ADC_CHANNEL(7)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
205 SYNTH_ADC_CHANNEL(8)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
206 SYNTH_ADC_CHANNEL(9)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
207 SYNTH_ADC_CHANNEL(10)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
208 SYNTH_ADC_CHANNEL(11)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
209 SYNTH_ADC_CHANNEL(12)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
210 SYNTH_ADC_CHANNEL(13)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
211 SYNTH_ADC_CHANNEL(14)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
212 SYNTH_ADC_CHANNEL(15)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
213
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
214
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
215 //-----------------------------------------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
216 // This function is called from the device IO infrastructure to initialize the
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
217 // device. It should perform any work needed to start up the device, short of
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
218 // actually starting the generation of samples. This function will be called
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
219 // for each channel, so if there is initialization that only needs to be done
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
220 // once, such as creating and interrupt object, then care should be taken to do
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
221 // this. This function should also call cyg_adc_device_init() to initialize the
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
222 // generic parts of the driver.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
223
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
224 static bool
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
225 synth_adc_init(struct cyg_devtab_entry *tab)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
226 {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
227 static cyg_bool initialized = false;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
228 cyg_adc_channel *chan = (cyg_adc_channel *) tab->priv;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
229 cyg_adc_device *device = chan->device;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
230 synth_adc_info *info = device->dev_priv;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
231 synth_adc_channel_info *chan_info = &info->chan_info[chan->channel];
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
232 cyg_handle_t counter;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
233
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
234 adc_diag("Initializing device\n");
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
235
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
236 // Initialize channel
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
237 info->chan[chan->channel] = chan;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
238 switch (chan_info->mode) {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
239 case MODE_CONST:
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
240 chan_info->get_sample = synth_adc_get_sample_const;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
241 break;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
242 case MODE_RANDOM:
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
243 chan_info->get_sample = synth_adc_get_sample_random;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
244 break;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
245 case MODE_FILE:
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
246 chan_info->get_sample = synth_adc_get_sample_file;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
247 break;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
248 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
249
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
250 // Set default rate
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
251 if (!initialized) {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
252 // Initialize alarm
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
253 cyg_clock_to_counter(cyg_real_time_clock(), &counter);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
254 cyg_alarm_create(counter, alarm_handler, (cyg_addrword_t) device,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
255 &info->alarm_handle, &info->alarm_data);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
256
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
257 synth_adc_set_rate(chan, chan->device->config.rate);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
258 initialized = true;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
259 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
260
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
261 // Initialize generic parts of ADC device
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
262 cyg_adc_device_init(device);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
263
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
264 return true;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
265 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
266
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
267 //-----------------------------------------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
268 // This function is called when a client looks up or opens a channel. It should
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
269 // call cyg_adc_channel_init() to initialize the generic part of the channel.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
270 // It should also perform any operations needed to start the channel generating
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
271 // samples.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
272
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
273 static Cyg_ErrNo
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
274 synth_adc_lookup(struct cyg_devtab_entry **tab,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
275 struct cyg_devtab_entry *sub_tab,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
276 const char *name)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
277 {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
278 cyg_adc_channel *chan = (cyg_adc_channel *) (*tab)->priv;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
279 synth_adc_info *info = chan->device->dev_priv;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
280 synth_adc_channel_info *chan_info = &info->chan_info[chan->channel];
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
281
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
282 adc_diag("Opening device\n");
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
283
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
284 // When this channel is in file mode, initialize file access
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
285 if (chan_info->mode == MODE_FILE) {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
286 struct cyg_hal_sys_new_stat stat;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
287
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
288 // Open the file
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
289 chan_info->fd = cyg_hal_sys_open(chan_info->filename,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
290 CYG_HAL_SYS_O_RDONLY, 0);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
291 if (chan_info->fd == -ENOENT) {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
292 adc_diag("Cannot open sampling file '%s' for channel '%s'\n",
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
293 chan_info->filename, (*tab)->name);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
294 CYG_FAIL("Cannot open sampling file\n");
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
295 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
296
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
297 // Get file size
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
298 if (cyg_hal_sys_newfstat(chan_info->fd, &stat) != 0) {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
299 CYG_FAIL("Cannot stat sampling file\n");
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
300 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
301 chan_info->num_samples = stat.st_size / sizeof(cyg_adc_sample_t);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
302 if (chan_info->num_samples <= 0)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
303 CYG_FAIL("Sampling file too small\n");
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
304
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
305 // Memory map
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
306 chan_info->base = (cyg_adc_sample_t *) cyg_hal_sys_mmap(
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
307 NULL,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
308 chan_info->num_samples * sizeof(cyg_adc_sample_t),
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
309 CYG_HAL_SYS_PROT_READ,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
310 CYG_HAL_SYS_MAP_SHARED,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
311 chan_info->fd,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
312 0);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
313 if (chan_info->base == (void *) -1)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
314 CYG_FAIL("Cannot memory map sampling file\n");
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
315 chan_info->sample = chan_info->base;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
316
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
317 adc_diag("Mapped to %p\n", chan_info->base);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
318 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
319
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
320 // Initialize generic parts of the channel
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
321 cyg_adc_channel_init(chan);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
322
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
323 // The generic ADC manual says: When a channel is first looked up or
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
324 // opened, then it is automatically enabled and samples start to
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
325 // accumulate - so we start the channel now
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
326 chan->enabled = true;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
327 synth_adc_enable(chan);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
328
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
329 return ENOERR;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
330 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
331
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
332 //-----------------------------------------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
333 // This function is called from the generic ADC package to enable the channel
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
334 // in response to a CYG_IO_SET_CONFIG_ADC_ENABLE config operation. It should
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
335 // take any steps needed to start the channel generating samples
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
336
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
337 static void
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
338 synth_adc_enable(cyg_adc_channel *chan)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
339 {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
340 synth_adc_info *info = chan->device->dev_priv;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
341 cyg_bool start;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
342
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
343 adc_diag("Enabling channel\n");
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
344
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
345 start = !info->chan_mask;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
346 info->chan_mask |= (1 << chan->channel);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
347
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
348 // Start scanning when first channel was activated
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
349 if (start) {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
350 // Enable timer
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
351 adc_diag("Starting scanning\n");
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
352 cyg_alarm_initialize(info->alarm_handle,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
353 cyg_current_time() + info->alarm_interval,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
354 info->alarm_interval);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
355 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
356 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
357
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
358 //-----------------------------------------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
359 // This function is called from the generic ADC package to enable the channel
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
360 // in response to a CYG_IO_SET_CONFIG_ADC_DISABLE config operation. It should
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
361 // take any steps needed to stop the channel generating samples.
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
362
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
363 static void
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
364 synth_adc_disable(cyg_adc_channel *chan)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
365 {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
366 synth_adc_info *info = chan->device->dev_priv;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
367
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
368 adc_diag("Disabling channel\n");
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
369
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
370 info->chan_mask &= ~(1 << chan->channel);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
371
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
372 // Stop scanning when no channel is active
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
373 if (!info->chan_mask) {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
374 // Disable timer
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
375 adc_diag("Stopping scanning\n");
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
376 cyg_alarm_disable(info->alarm_handle);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
377 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
378 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
379
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
380 //-----------------------------------------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
381 // This function is called from the generic ADC package to enable the channel
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
382 // in response to a CYG_IO_SET_CONFIG_ADC_RATE config operation. It should take
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
383 // any steps needed to change the sample rate of the channel, or of the entire
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
384 // device. We use a timer channel to generate the interrupts for sampling the
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
385 // analog channels
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
386
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
387 static void
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
388 synth_adc_set_rate(cyg_adc_channel *chan, cyg_uint32 rate)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
389 {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
390 cyg_adc_device *device = chan->device;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
391 synth_adc_info *info = device->dev_priv;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
392 cyg_uint64 interval;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
393
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
394 adc_diag("Setting rate to %d\n", rate);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
395
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
396 interval = 1000000000000LL / rate;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
397 interval /= (CYGNUM_HAL_RTC_NUMERATOR / CYGNUM_HAL_RTC_DENOMINATOR);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
398
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
399 if (interval > 1000) {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
400 info->alarm_interval = interval / 1000;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
401 info->alarm_samples = 1;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
402 } else {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
403 info->alarm_interval = 1;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
404 info->alarm_samples = 1000 / interval;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
405 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
406
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
407 if (info->chan_mask)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
408 cyg_alarm_initialize(info->alarm_handle,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
409 cyg_current_time() + info->alarm_interval,
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
410 info->alarm_interval);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
411
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
412 device->config.rate = rate;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
413 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
414
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
415 static void
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
416 alarm_handler(cyg_handle_t alarm, cyg_addrword_t data)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
417 {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
418 cyg_adc_device *device = (cyg_adc_device *) data;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
419 synth_adc_info *info = device->dev_priv;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
420 cyg_adc_channel *chan;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
421 synth_adc_channel_info *chan_info;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
422 cyg_uint32 active_mask;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
423 int i, j;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
424
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
425 if (!info->chan_mask)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
426 return;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
427
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
428 active_mask = info->chan_mask;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
429
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
430 for (i = 0; i < NUM_CHANNELS; i++) {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
431 if (active_mask & 0x01) {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
432 chan = info->chan[i];
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
433 chan_info = &info->chan_info[chan->channel];
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
434 for (j = 0; j < info->alarm_samples; j++)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
435 cyg_adc_receive_sample(chan, chan_info->get_sample(chan));
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
436 cyg_adc_wakeup(info->chan[i]);
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
437 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
438 active_mask >>= 1;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
439 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
440 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
441
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
442 static cyg_adc_sample_t
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
443 synth_adc_get_sample_const(cyg_adc_channel *chan)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
444 {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
445 synth_adc_info *info = chan->device->dev_priv;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
446 synth_adc_channel_info *chan_info = &info->chan_info[chan->channel];
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
447
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
448 return chan_info->const_value;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
449 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
450
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
451 static cyg_adc_sample_t
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
452 synth_adc_get_sample_random(cyg_adc_channel *chan)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
453 {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
454 return rand() & SAMPLE_BITS;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
455 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
456
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
457 static cyg_adc_sample_t
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
458 synth_adc_get_sample_file(cyg_adc_channel *chan)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
459 {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
460 synth_adc_info *info = chan->device->dev_priv;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
461 synth_adc_channel_info *chan_info = &info->chan_info[chan->channel];
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
462 cyg_adc_sample_t sample;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
463
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
464 sample = *chan_info->sample++;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
465 if (chan_info->sample >= chan_info->base + chan_info->num_samples)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
466 chan_info->sample = chan_info->base;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
467
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
468 return sample;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
469 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
470
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
471 //-----------------------------------------------------------------------------
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
472 // Simple random number generator
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
473
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
474 static cyg_uint32 rand(void)
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
475 {
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
476 static cyg_uint32 seed;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
477
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
478 seed = (seed * 1103515245) + 12345; // permutate seed
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
479
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
480 return seed;
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
481 }
8fe6d83b6d48 Add STM32 and synthetic target ADC drivers.
nickg
parents:
diff changeset
482