emlib 0.0.1

A SiLabs EMlib bindings for Happy Gecko MCU (EFM32HG222F64)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#![allow(dead_code)]

use core::intrinsics::transmute;
use core::default::Default;

pub const TIMER_IF_OF: u32     = (0x1 << 0);
pub const TIMER_IF_UF: u32     = (0x1 << 1);
pub const TIMER_IF_CC0: u32    = (0x1 << 4);
pub const TIMER_IF_CC1: u32    = (0x1 << 5);
pub const TIMER_IF_CC2: u32    = (0x1 << 6);
pub const TIMER_IF_ICBOF0: u32 = (0x1 << 8);
pub const TIMER_IF_ICBOF1: u32 = (0x1 << 9);
pub const TIMER_IF_ICBOF2: u32 = (0x1 << 10);

#[repr(C)]
#[derive(Copy, Clone)]
#[allow(non_snake_case)]
pub struct CC {
    pub CTRL: u32,
    pub CCV: u32,
    pub CCVP: u32,
    pub Ccvn: u32,
}

#[repr(C)]
#[derive(Copy, Clone)]
#[allow(non_snake_case)]
pub struct Timer {
    pub CTRL: u32,
    pub CMD: u32,
    pub STATUS: u32,
    pub IEN: u32,
    pub IF: u32,
    pub IFS: u32,
    pub IFC: u32,
    pub TOP: u32,
    pub TOPB: u32,
    pub CNT: u32,
    pub ROUTE: u32,
    pub RESERVED0: u32,
    pub CC: [CC; 3],
    pub RESERVED1: [u32; 4],
    pub DTCTRL: u32,
    pub DTTIME: u32,
    pub DTFC: u32,
    pub DTOGEN: u32,
    pub DTFAULT: u32,
    pub DTFAULTC: u32,
    pub DTLOCK: u32,
}

impl Timer {

    #[inline]
    pub fn timer0() -> &'static mut Timer {
        unsafe { transmute(GET_TIMER0()) }
    }

    #[inline]
    pub fn timer1() -> &'static mut Timer {
        unsafe { transmute(GET_TIMER1()) }
    }

    #[inline]
    pub fn timer2() -> &'static mut Timer {
        unsafe { transmute(GET_TIMER2()) }
    }

    pub fn capture_get(&self, ch: u32) -> u32 {
        unsafe { STATIC_INLINE_TIMER_CaptureGet(self, ch) }
    }

    pub fn compare_buf_set(&self, ch: u32, val: u32) {
        unsafe { STATIC_INLINE_TIMER_CompareBufSet(self, ch, val) }
    }

    pub fn compare_set(&self, ch: u32, val: u32) {
        unsafe { STATIC_INLINE_TIMER_CompareSet(self, ch, val) }
    }

    pub fn counter_get(&self) -> u32 {
        unsafe { STATIC_INLINE_TIMER_CounterGet(self) }
    }

    pub fn counter_set(&self, val: u32) {
        unsafe { STATIC_INLINE_TIMER_CounterSet(self, val) }
    }

    pub fn enable(&self, enable: bool) {
        unsafe { STATIC_INLINE_TIMER_Enable(self, enable) }
    }

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

    pub fn init_cc(&self, ch: u32, init: &InitCC) {
        unsafe { TIMER_InitCC(self, ch, init) }
    }

    pub fn init_dti(&self, init: &InitDTI) {
        unsafe { TIMER_InitDTI(self, init) }
    }

    pub fn enable_dti(&self, enable: bool) {
        unsafe { STATIC_INLINE_TIMER_EnableDTI(self, enable) }
    }

    pub fn get_dti_fault(&self) -> u32 {
        unsafe { STATIC_INLINE_TIMER_GetDTIFault(self) }
    }

    pub fn clear_dti_fault(&self, flags: u32) {
        unsafe { STATIC_INLINE_TIMER_ClearDTIFault(self, flags) }
    }

    pub fn int_clear(&self, flags: u32) {
        unsafe { STATIC_INLINE_TIMER_IntClear(self, flags) }
    }

    pub fn int_disable(&self, flags: u32) {
        unsafe { STATIC_INLINE_TIMER_IntDisable(self, flags) }
    }

    pub fn int_enable(&self, flags: u32) {
        unsafe { STATIC_INLINE_TIMER_IntEnable(self, flags)}
    }

    pub fn int_get(&self) -> u32 {
        unsafe { STATIC_INLINE_TIMER_IntGet(self) }
    }

    pub fn int_get_enabled(&self) -> u32 {
        unsafe { STATIC_INLINE_TIMER_IntGetEnabled(self) }
    }

    pub fn int_set(&self, flags: u32) {
        unsafe { STATIC_INLINE_TIMER_IntSet(self, flags) }
    }

    pub fn lock(&self) {
        unsafe { STATIC_INLINE_TIMER_Lock(self) }
    }

    pub fn reset(&self) {
        unsafe { TIMER_Reset(self) }
    }

    pub fn top_buf_set(&self, val: u32) {
        unsafe { STATIC_INLINE_TIMER_TopBufSet(self, val) }
    }

    pub fn top_get(&self) -> u32 {
        unsafe { STATIC_INLINE_TIMER_TopGet(self) }
    }

    pub fn top_set(&self, val: u32) {
        unsafe { STATIC_INLINE_TIMER_TopSet(self, val) }
    }

    pub fn unlock(&self) {
        unsafe { STATIC_INLINE_TIMER_Unlock(self) }
    }

}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum CCMode {
    Off = 0x0,
    Capture = 0x1,
    Compare = 0x2,
    PWM = 0x3,
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum ClkSel {
    HFPerClk = 0x0,
    CC1 = 0x1,
    Cascade = 0x2,
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum Edge {
    Rising = 0x0,
    Falling = 0x1,
    Both = 0x2,
    None = 0x3,
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum Event {
    EveryEdge = 0x0,
    Every2ndEdge = 0x1,
    Rising = 0x2,
    Falling = 0x3,
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum InputAction {
    None = 0x0,
    Start = 0x1,
    Stop = 0x2,
    ReloadStart = 0x3,
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum Mode {
    Up = 0x0,
    Down = 0x1,
    UpDown = 0x2,
    QDec = 0x3,
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum OutputAction {
    None = 0x0,
    Toggle = 0x1,
    Clear = 0x2,
    Set = 0x3,
}

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum Prescale {
    Prescale1    = 0x0,
    Prescale2    = 0x1,
    Prescale4    = 0x2,
    Prescale8    = 0x3,
    Prescale16   = 0x4,
    Prescale32   = 0x5,
    Prescale64   = 0x6,
    Prescale128  = 0x7,
    Prescale256  = 0x8,
    Prescale512  = 0x9,
    Prescale1024 = 0xA,
}

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

#[repr(u8)]
#[derive(Copy, Clone)]
pub enum DtiFaultAction {
    None = 0x0,
    Inactive = 0x1,
    Clear = 0x2,
    Tristate = 0x3,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Init {
    pub enable: bool,
    pub debug_run: bool,
    pub prescale: Prescale,
    pub clk_sel: ClkSel,
    pub count_2x: bool,
    pub ati: bool,
    pub fall_action: InputAction,
    pub rise_action: InputAction,
    pub mode: Mode,
    pub dma_clr_act: bool,
    pub quad_mode_x4: bool,
    pub one_shot: bool,
    pub sync: bool,
}

impl Default for Init {
    fn default() -> Init {
        Init {
            enable:       true,
            debug_run:    false,
            prescale:     Prescale::Prescale1,
            clk_sel:      ClkSel::HFPerClk,
            count_2x:     false,
            ati:          false,
            fall_action:  InputAction::None,
            rise_action:  InputAction::None,
            mode:         Mode::Up,
            dma_clr_act:  false,
            quad_mode_x4: false,
            one_shot:     false,
            sync:         false
        }
    }
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct InitCC {
    event_ctrl: Event,
    edge: Edge,
    prs_sel: PRSSEL,
    cufoa: OutputAction,
    cofoa: OutputAction,
    cmoa: OutputAction,
    mode: CCMode,
    filter: bool,
    prs_input: bool,
    coist: bool,
    out_invert: bool,
}

impl Default for InitCC {
    fn default() -> InitCC {
        InitCC {
            event_ctrl: Event::EveryEdge,
            edge:       Edge::Rising,
            prs_sel:    PRSSEL::Ch0,
            cufoa:      OutputAction::None,
            cofoa:      OutputAction::None,
            cmoa:       OutputAction::None,
            mode:       CCMode::Off,
            filter:     false,
            prs_input:  false,
            coist:      false,
            out_invert: false,
        }
    }
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct InitDTI {
    enable: bool,
    active_low_out: bool,
    invert_complementary_out: bool,
    auto_restart: bool,
    enable_prs_source: bool,
    prs_sel: PRSSEL,
    prescale: Prescale,
    rise_time: u32,
    fall_time: u32,
    outputs_enable_mask: u32,
    enable_fault_source_core_lockup: bool,
    enable_fault_source_debugger: bool,
    enable_fault_source_prs_sel_0: bool,
    fault_source_prs_sel_0: PRSSEL,
    enable_fault_source_prs_sel_1: bool,
    fault_source_prs_sel_1: PRSSEL,
    fault_action: DtiFaultAction,
}

impl Default for InitDTI {
    fn default() -> InitDTI {
        InitDTI {
            enable:                          true,
            active_low_out:                  false,
            invert_complementary_out:        false,
            auto_restart:                    false,
            enable_prs_source:               false,
            prs_sel:                         PRSSEL::Ch0,
            prescale:                        Prescale::Prescale1,
            rise_time:                       0,
            fall_time:                       0,
            outputs_enable_mask:             (0x1 << 0 | 0x1<<3),
            enable_fault_source_core_lockup: true,
            enable_fault_source_debugger:    true,
            enable_fault_source_prs_sel_0:   false,
            fault_source_prs_sel_0:          PRSSEL::Ch0,
            enable_fault_source_prs_sel_1:   false,
            fault_source_prs_sel_1:          PRSSEL::Ch0,
            fault_action:                    DtiFaultAction::Inactive,
        }
    }
}

#[link(name = "emlib")]
extern {

    #[allow(dead_code)] #[inline] fn GET_TIMER0() -> *mut Timer;
    #[allow(dead_code)] #[inline] fn GET_TIMER1() -> *mut Timer;
    #[allow(dead_code)] #[inline] fn GET_TIMER2() -> *mut Timer;
    #[allow(dead_code)] #[inline] fn GET_TIMER3() -> *mut Timer;

    #[allow(dead_code)] fn STATIC_INLINE_TIMER_CaptureGet(timer: &Timer, ch: u32) -> u32;
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_CompareBufSet(timer: &Timer, ch: u32, val: u32);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_CompareSet(timer: &Timer, ch: u32, val: u32);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_CounterGet(timer: &Timer) -> u32;
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_CounterSet(timer: &Timer, val: u32);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_Enable(timer: &Timer, enable: bool);
    #[allow(dead_code)] fn TIMER_Init(timer: &Timer, init: &Init);
    #[allow(dead_code)] fn TIMER_InitCC(timer: &Timer, ch: u32, init: &InitCC);
    #[allow(dead_code)] fn TIMER_InitDTI(timer: &Timer, init: &InitDTI);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_EnableDTI(timer: &Timer, enable: bool);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_GetDTIFault(timer: &Timer) -> u32;
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_ClearDTIFault(timer: &Timer, flags: u32);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_IntClear(timer: &Timer, flags: u32);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_IntDisable(timer: &Timer, flags: u32);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_IntEnable(timer: &Timer, flags: u32);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_IntGet(timer: &Timer) -> u32;
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_IntGetEnabled(timer: &Timer) -> u32;
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_IntSet(timer: &Timer, flags: u32);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_Lock(timer: &Timer);
    #[allow(dead_code)] fn TIMER_Reset(timer: &Timer);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_TopBufSet(timer: &Timer, val: u32);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_TopGet(timer: &Timer) -> u32;
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_TopSet(timer: &Timer, val: u32);
    #[allow(dead_code)] fn STATIC_INLINE_TIMER_Unlock(timer: &Timer);

}