1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use core::default::Default;
use core::intrinsics::transmute;

pub const STATUS_SINGLEACT:u32 = 0x1;
pub const IEN_SINGLE: u32 = 0x1 << 0;

pub const IF_SINGLE: u32 = (0x1 << 0);

#[repr(C)]
#[derive(Copy, Clone)]
#[allow(non_snake_case)]
pub struct Adc {
    pub CTRL: u32,
    pub CMD: u32,
    pub STATUS: u32,
    pub SINGLECTRL: u32,
    pub SCANCTRL: u32,
    pub IEN: u32,
    pub IF: u32,
    pub IFS: u32,
    pub IFC: u32,
    pub SINGLEDATA: u32,
    pub SCANDATA: u32,
    pub SINGLEDATAP: u32,
    pub SCANDATAP: u32,
    pub CAL: u32,
    pub RESERVED0: u32,
    pub BIASPROG: u32,
}

impl Adc {

    pub fn adc0() -> &'static mut Adc {
        unsafe { transmute(GET_ADC0()) }
    }

    pub fn init(&self, init: &Init) {
        unsafe { ADC_Init(self, init) }
    }

    pub fn init_single(&self, init: &InitSingle) {
        unsafe { ADC_InitSingle(self, init) }
    }

    pub fn data_single_get(&self) -> u32 {
        unsafe { STATIC_INLINE_ADC_DataSingleGet(self) }
    }

    pub fn start(&self, cmd: Start) {
        unsafe { STATIC_INLINE_ADC_Start(self, cmd) }
    }

}

pub fn timebase_calc(hfper_freq: u32) -> u8 {
    unsafe { ADC_TimebaseCalc(hfper_freq) }
}

pub fn prescale_calc(adc_freq: u32, hfper_freq: u32) -> u8 {
    unsafe { ADC_PrescaleCalc(adc_freq, hfper_freq) }
}

#[repr(C)]
#[derive(Copy, Clone)]
#[allow(non_snake_case)]
pub struct Init {
    pub ovs_rate_sel: OvsRateSel,
    pub lpf_mode: LPFilter,
    pub warm_up_mode: Warmup,
    pub timebase: u8,
    pub prescale: u8,
    pub tailgate: bool,
}

#[repr(C)]
#[derive(Copy, Clone)]
#[allow(non_snake_case)]
pub struct InitSingle {
    pub prs_sel: PRSSEL,
    pub acq_time: AcqTime,
    pub reference: Ref,
    pub resolution: Res,
    pub input: SingleInput,
    pub diff: bool,
    pub prs_enable: bool,
    pub left_adjust: bool,
    pub rep: bool,
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum PRSSEL {
    Ch0 = 0x0,
    Ch1 = 0x1,
    Ch2 = 0x2,
    Ch3 = 0x3,
    Ch4 = 0x4,
    Ch5 = 0x5,
    Ch6 = 0x6,
    Ch7 = 0x7,
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum AcqTime {
    Time1   = 0x0,
    Time2   = 0x1,
    Time4   = 0x2,
    Time8   = 0x3,
    Time16  = 0x4,
    Time32  = 0x5,
    Time64  = 0x6,
    Time128 = 0x7,
    Time256 = 0x8
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum Ref {
    Ref1V25      = 0x0,
    Ref2V5       = 0x1,
    RefVDD       = 0x2,
    Ref5VDIFF    = 0x3,
    RefExtSingle = 0x4,
    Ref2xExtDiff = 0x5,
    Ref2xVDD     = 0x6
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum Res {
    Res12Bit = 0x0,
    Res8Bit  = 0x1,
    Res6Bit  = 0x2,
    ResOVS   = 0x3
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum OvsRateSel {
    Sel2    = 0x0,
    Sel4    = 0x1,
    Sel8    = 0x2,
    Sel16   = 0x3,
    Sel32   = 0x4,
    Sel64   = 0x5,
    Sel128  = 0x6,
    Sel256  = 0x7,
    Sel512  = 0x8,
    Sel1024 = 0x9,
    Sel2048 = 0xA,
    Sel4096 = 0xB
}



#[repr(u8)]
#[derive(Copy, Clone)]
pub enum LPFilter {
    Bypass = 0x0,
    RC     = 0x1,
    DeCap  = 0x2
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum Warmup {
    Normal          = 0x0,
    FastBG          = 0x1,
    KeepScanRefWarm = 0x2,
    KeepADCWarm     = 0x3
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum SingleInput
{
    Ch0      = 0x0,
    Ch1      = 0x1,
    Ch2      = 0x2,
    Ch3      = 0x3,
    Ch4      = 0x4,
    Ch5      = 0x5,
    Ch6      = 0x6,
    Ch7      = 0x7,
    Temp     = 0x8,
    VDDDiv3  = 0x9,
    VDD      = 0xA,
    VSS      = 0xB,
    VrefDiv2 = 0xC,
    DACOut0  = 0xD,
    DACOut1  = 0xE,
    ATEST    = 15,

    //Ch0Ch1   = 0x0,
    //Ch2Ch3   = 0x1,
    //Ch4Ch5   = 0x2,
    //Ch6Ch7   = 0x3,
    //Diff0    = 4
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum Start {
    Single = 0x1,
    Scan = 0x1 << 2,
    ScanAndSingle = 0x1 | (0x1 << 2),
}


impl Default for Init {

    fn default() -> Init {
        Init {
            ovs_rate_sel: OvsRateSel::Sel2,
            lpf_mode: LPFilter::Bypass,
            warm_up_mode: Warmup::Normal,
            timebase: 0x1F,
            prescale: 0x0,
            tailgate: false,
        }
    }
}

impl Default for InitSingle {

    fn default() -> InitSingle {
        InitSingle {
            prs_sel: PRSSEL::Ch0,
            acq_time: AcqTime::Time1,
            reference: Ref::Ref1V25,
            resolution: Res::Res12Bit,
            input: SingleInput::Ch0,
            diff: false,
            prs_enable: false,
            left_adjust: false,
            rep: false,
        }
    }
}


extern {
    fn GET_ADC0() -> *mut Adc;

    fn ADC_Init(adc: &Adc, init: &Init);
    fn ADC_InitSingle(adc: &Adc, init: &InitSingle);
    fn ADC_TimebaseCalc(hfper_freq: u32) -> u8;
    fn ADC_PrescaleCalc(adc_freq: u32, hfper_freq: u32) -> u8;
    fn STATIC_INLINE_ADC_DataSingleGet(adc: &Adc) -> u32;
    fn STATIC_INLINE_ADC_Start(adc: &Adc, cmd: Start);
}