rustuino 0.1.0

A library to for programming the stm32f446re
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! This module contains everything that is used for UART communication.
//! 
//! For information on whitch pins have UART capabilities, check [`UART_MAP`](crate::include::UART_MAP)
//! 
//! # Examples
//! 
//! ```no_run
//! #![no_std]
//! #![no_main]
//! 
//! use rustuino::*;
//! 
//! #[entry]
//! fn main() -> ! {
//!   // Configure the serial connection
//!   let uart = UART::new(2, PA2, PA3, 115200).unwrap();
//! 
//!   loop {
//!     // Send Message
//!     uart.println_str("Hello World!");
//!     delay(1000);   
//!   }
//! }
//! ```

use crate::include::{SerialError, ProgError, UART_MAP, PIN_CONF};
use crate::gpio::{pinmode_alternate_function, Pin, AlternateFunction};
use stm32f4::stm32f446::{NVIC, Interrupt};
use rtt_target::rprintln;

/// This struct represents a configured UART peripheral.
pub struct UART {
  #[doc(hidden)]
  core: u8,
  #[doc(hidden)]
  _tx_pin: Pin<AlternateFunction>,
  #[doc(hidden)]
  _rx_pin: Pin<AlternateFunction>
}

impl UART {
  /// Configure a serial connection with one of the internal UART peripherals.
  /// 
  /// This Method expects the used UART core, two [pin identifiers](crate::include::pins) for the tx and rx-pins
  /// and a baudrate as parameters and returns the [UART Struct](crate::uart::UART). Panics if the core or pins
  /// are already used or invalid.
  pub fn new(core: u8, tx_pin: (char, u8), rx_pin: (char, u8), baud: u32) -> Result<Self, ProgError> {
    let peripheral_ptr;
    unsafe {peripheral_ptr = stm32f4::stm32f446::Peripherals::steal();}
    let rcc = &peripheral_ptr.RCC;

    let af = if core == 1 || core == 2 || core == 3 {7}
    else {8};
    
    if !UART_MAP.tx_pins.iter().zip(UART_MAP.rx_pins.iter())
    .zip(UART_MAP.cores.iter()).any(|i| i == ((&tx_pin, &rx_pin), &core)) {
      rprintln!("These pins are not available for UART communication! | UART::new()");
      return Err(ProgError::InvalidConfiguration);
    }

    unsafe {
      if PIN_CONF.contains(&tx_pin) || PIN_CONF.contains(&rx_pin) {
        rprintln!("These pins are already configured for another function! | UART::new()");
        return Err(ProgError::InvalidConfiguration);
      }
      else {
        PIN_CONF.push(tx_pin).expect("Could not store pin number! | UART::new()");
        PIN_CONF.push(rx_pin).expect("Could not store pin number! | UART::new()");
      }
    }

    let tx = match pinmode_alternate_function(tx_pin, af) {
      Ok(value) => value,
      Err(_) => return Err(ProgError::Internal)
    };

    let rx = match pinmode_alternate_function(rx_pin, af) {
      Ok(value) => value,
      Err(_) => return Err(ProgError::Internal)
    };
    
    match core {
      1 => {
        let uart1 = &peripheral_ptr.USART1;
        if rcc.apb2enr.read().usart1en().is_enabled() {
          rprintln!("U(S)ART{} is already configured! | UART::new()", core);
          return Err(ProgError::InvalidConfiguration);
        }
        rcc.apb2enr.modify(|_, w| w.usart1en().enabled());
        set_baud(core, baud);
        uart1.cr1.modify(|_, w| {
          w.te().enabled();
          w.re().enabled();
          w.ue().enabled()
        });
      },
      2 => {
        let uart2 = &peripheral_ptr.USART2;
        if rcc.apb1enr.read().usart2en().is_enabled() {
          rprintln!("U(S)ART{} is already configured! | UART::new()", core);
          return Err(ProgError::InvalidConfiguration);
        }
        rcc.apb1enr.modify(|_, w| w.usart2en().enabled());
        set_baud(core, baud);
        uart2.cr1.modify(|_, w| {
          w.te().enabled();
          w.re().enabled();
          w.ue().enabled()
        });
      },
      3 => {
        let uart3 = &peripheral_ptr.USART3;
        if rcc.apb1enr.read().usart3en().is_enabled() {
          rprintln!("U(S)ART{} is already configured! | UART::new()", core);
          return Err(ProgError::InvalidConfiguration);
        }
        rcc.apb1enr.modify(|_, w| w.usart3en().enabled());
        set_baud(core, baud);
        uart3.cr1.modify(|_, w| {
          w.te().enabled();
          w.re().enabled();
          w.ue().enabled()
        });
      },
      4 => {
        let uart4 = &peripheral_ptr.UART4;
        if rcc.apb1enr.read().uart4en().is_enabled() {
          rprintln!("U(S)ART{} is already configured! | UART::new()", core);
          return Err(ProgError::InvalidConfiguration);
        }
        rcc.apb1enr.modify(|_, w| w.uart4en().enabled());
        set_baud(core, baud);
        uart4.cr1.modify(|_, w| {
          w.te().enabled();
          w.re().enabled();
          w.ue().enabled()
        });
      },
      5 => {
        let uart5 = &peripheral_ptr.UART5;
        if rcc.apb1enr.read().uart5en().is_enabled() {
          rprintln!("U(S)ART{} is already configured! | UART::new()", core);
          return Err(ProgError::InvalidConfiguration);
        }
        rcc.apb1enr.modify(|_, w| w.uart5en().enabled());
        set_baud(core, baud);
        uart5.cr1.modify(|_, w| {
          w.te().enabled();
          w.re().enabled();
          w.ue().enabled()
        });
      },
      6 => {
        let uart6 = &peripheral_ptr.USART6;
        if rcc.apb2enr.read().usart6en().is_enabled() {
          rprintln!("U(S)ART{} is already configured! | UART::new()", core);
          return Err(ProgError::InvalidConfiguration);
        }
        rcc.apb2enr.modify(|_, w| w.usart6en().enabled());
        set_baud(core, baud);
        uart6.cr1.modify(|_, w| {
          w.te().enabled();
          w.re().enabled();
          w.ue().enabled()
        });
      },
      _ => {
        rprintln!("U(S)ART{} is not a valid U(S)ART peripheral! | UART::new()", core);
        return Err(ProgError::InvalidConfiguration);
      }
    };

    return Ok(Self {
      core,
      _tx_pin: tx,
      _rx_pin: rx
    });
  }

  /// Deacitivates the UART connection and destroys the struct, freeing the core and pins.
  pub fn end(self) {
    let peripheral_ptr;
    unsafe {peripheral_ptr = stm32f4::stm32f446::Peripherals::steal();}
    let rcc = &peripheral_ptr.RCC;

    match self.core {
      1 => {
        let uart1 = &peripheral_ptr.USART1;
        rcc.apb2enr.modify(|_, w| w.usart1en().disabled());
        uart1.cr1.reset();
        uart1.cr2.reset();
        NVIC::mask(Interrupt::USART1);
      },
      2 => {
        let uart2 = &peripheral_ptr.USART2;
        rcc.apb1enr.modify(|_, w| w.usart2en().disabled());
        uart2.cr1.reset();
        uart2.cr2.reset();
        NVIC::mask(Interrupt::USART2);
      },
      3 => {
        let uart3 = &peripheral_ptr.USART3;
        rcc.apb1enr.modify(|_, w| w.usart3en().disabled());
        uart3.cr1.reset();
        uart3.cr2.reset();
        NVIC::mask(Interrupt::USART3);
      },
      4 => {
        let uart4 = &peripheral_ptr.UART4;
        rcc.apb1enr.modify(|_, w| w.uart4en().disabled());
        uart4.cr1.reset();
        uart4.cr2.reset();
        NVIC::mask(Interrupt::UART4);
      },
      5 => {
        let uart5 = &peripheral_ptr.UART5;
        rcc.apb1enr.modify(|_, w| w.uart5en().disabled());
        uart5.cr1.reset();
        uart5.cr2.reset();
        NVIC::mask(Interrupt::UART5);
      },
      6 => {
        let uart6 = &peripheral_ptr.USART6;
        rcc.apb2enr.modify(|_, w| w.usart6en().disabled());
        uart6.cr1.reset();
        uart6.cr2.reset();
        NVIC::mask(Interrupt::USART6);
      },
      _ => unreachable!()
    };

    drop(self);
  }

  /// Sends an ASCII char over the serial connection. Returns an error-enum if problems with the connection are detected.
  pub fn print_char(&self, data: char) -> Result<(), SerialError> {
    let peripheral_ptr;
    unsafe {peripheral_ptr = stm32f4::stm32f446::Peripherals::steal();}
    
    match self.core {
      1 => {
        let uart1 = &peripheral_ptr.USART1;
        while uart1.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart1.sr.read().bits()) {return Err(error);}
        }
        uart1.dr.write(|w| w.dr().bits(data as u16));
      },
      2 => {
        let uart2 = &peripheral_ptr.USART2;
        while uart2.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart2.sr.read().bits()) {return Err(error);}
        }
        uart2.dr.write(|w| w.dr().bits(data as u16));
      },
      3 => {
        let uart3 = &peripheral_ptr.USART3;
        while uart3.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart3.sr.read().bits()) {return Err(error);}
        }
        uart3.dr.write(|w| w.dr().bits(data as u16));
      },
      4 => {
        let uart4 = &peripheral_ptr.UART4;
        while uart4.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart4.sr.read().bits()) {return Err(error);}
        }
        uart4.dr.write(|w| w.dr().bits(data as u16));
      },
      5 => {
        let uart5 = &peripheral_ptr.UART5;
        while uart5.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart5.sr.read().bits()) {return Err(error);}
        }
        uart5.dr.write(|w| w.dr().bits(data as u16));
      },
      6 => {
        let uart6 = &peripheral_ptr.USART6;
        while uart6.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart6.sr.read().bits()) {return Err(error);}
        }
        uart6.dr.write(|w| w.dr().bits(data as u16));
      },
      _ => unreachable!()
    };

    return Ok(());
  }

  /// Sends an ASCII string over the serial connection. Returns an error-enum if problems with the connection are detected.
  pub fn print_str(&self, data: &str) -> Result<(), SerialError> {
    let peripheral_ptr;
    unsafe {peripheral_ptr = stm32f4::stm32f446::Peripherals::steal();}
    
    let bytes = data.as_bytes();
    
    match self.core {
      1 => {
        let uart1 = &peripheral_ptr.USART1;
        for byte in bytes {
          while uart1.sr.read().txe().bit_is_clear() {
            if let Err(error) = check_uart_errors(uart1.sr.read().bits()) {return Err(error);}
          }
          uart1.dr.write(|w| w.dr().bits((*byte).into()));
        }
      },
      2 => {
        let uart2 = &peripheral_ptr.USART2;
        for byte in bytes {
          while uart2.sr.read().txe().bit_is_clear() {
            if let Err(error) = check_uart_errors(uart2.sr.read().bits()) {return Err(error);}
          }
          uart2.dr.write(|w| w.dr().bits((*byte).into()));
        }
      },
      3 => {
        let uart3 = &peripheral_ptr.USART3;
        for byte in bytes {
          while uart3.sr.read().txe().bit_is_clear() {
            if let Err(error) = check_uart_errors(uart3.sr.read().bits()) {return Err(error);}
          }
          uart3.dr.write(|w| w.dr().bits((*byte).into()));
        }
      },
      4 => {
        let uart4 = &peripheral_ptr.UART4;
        for byte in bytes {
          while uart4.sr.read().txe().bit_is_clear() {
            if let Err(error) = check_uart_errors(uart4.sr.read().bits()) {return Err(error);}
          }
          uart4.dr.write(|w| w.dr().bits((*byte).into()));
        }
      },
      5 => {
        let uart5 = &peripheral_ptr.UART5;
        for byte in bytes {
          while uart5.sr.read().txe().bit_is_clear() {
            if let Err(error) = check_uart_errors(uart5.sr.read().bits()) {return Err(error);}
          }
          uart5.dr.write(|w| w.dr().bits((*byte).into()));
        }
      },
      6 => {
        let uart6 = &peripheral_ptr.USART6;
        for byte in bytes {
          while uart6.sr.read().txe().bit_is_clear() {
            if let Err(error) = check_uart_errors(uart6.sr.read().bits()) {return Err(error);}
          }
          uart6.dr.write(|w| w.dr().bits((*byte).into()));
        }
      },
      _ => unreachable!()
    };

    return Ok(());
  }

  /// Acts like [print_char](crate::uart::UART::print_char) except it prints a newline at the end of the string.
  pub fn println_char(&self, data: char) -> Result<(), SerialError> {
    if let Err(error) = self.print_char(data) {return Err(error);}
    if let Err(error) = self.print_str("\r\n") {return Err(error);}

    return Ok(());
  }

  /// Acts like [print_str](crate::uart::UART::print_str) except it prints a newline at the end of the string.
  pub fn println_str(&self, data: &str) -> Result<(), SerialError> {
    if let Err(error) = self.print_str(data) {return Err(error);}
    if let Err(error) = self.print_str("\r\n") {return Err(error);}
    
    return Ok(());
  }

  /// Sends a raw byte over the serial connection. Returns an error-enum if problems with the connection are detected.
  pub fn write(&self, data: u8) -> Result<(), SerialError> {
    let peripheral_ptr;
    unsafe {peripheral_ptr = stm32f4::stm32f446::Peripherals::steal();}

    match self.core {
      1 => {
        let uart1 = &peripheral_ptr.USART1;
        while uart1.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart1.sr.read().bits()) {return Err(error);}
        }
        uart1.dr.write(|w| w.dr().bits(data.into()));
      },
      2 => {
        let uart2 = &peripheral_ptr.USART2;
        while uart2.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart2.sr.read().bits()) {return Err(error);}
        }
        uart2.dr.write(|w| w.dr().bits(data.into()));
      },
      3 => {
        let uart3 = &peripheral_ptr.USART3;
        while uart3.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart3.sr.read().bits()) {return Err(error);}
        }
        uart3.dr.write(|w| w.dr().bits(data.into()));
      },
      4 => {
        let uart4 = &peripheral_ptr.UART4;
        while uart4.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart4.sr.read().bits()) {return Err(error);}
        }
        uart4.dr.write(|w| w.dr().bits(data.into()));
      },
      5 => {
        let uart5 = &peripheral_ptr.UART5;
        while uart5.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart5.sr.read().bits()) {return Err(error);}
        }
        uart5.dr.write(|w| w.dr().bits(data.into()));
      },
      6 => {
        let uart6 = &peripheral_ptr.USART6;
        while uart6.sr.read().txe().bit_is_clear() {
          if let Err(error) = check_uart_errors(uart6.sr.read().bits()) {return Err(error);}
        }
        uart6.dr.write(|w| w.dr().bits(data.into()));
      },
      _ => unreachable!()
    };

    return Ok(());
  }

  /// Waits until it recieves an ASCII char. Returns an error-enum if problems with the connection are detected.
  pub fn read_char(&self) -> Option<char> {
    let peripheral_ptr;
    unsafe {peripheral_ptr = stm32f4::stm32f446::Peripherals::steal();}

    let buffer: u8;
    
    match self.core {
      1 => {
        let uart1 = &peripheral_ptr.USART1;
        while uart1.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart1.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart1.dr.read().dr().bits() as u8;
      },
      2 => {
        let uart2 = &peripheral_ptr.USART2;
        while uart2.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart2.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart2.dr.read().dr().bits() as u8;
      },
      3 => {
        let uart3 = &peripheral_ptr.USART3;
        while uart3.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart3.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart3.dr.read().dr().bits() as u8;
      },
      4 => {
        let uart4 = &peripheral_ptr.UART4;
        while uart4.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart4.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart4.dr.read().dr().bits() as u8;
      },
      5 => {
        let uart5 = &peripheral_ptr.UART5;
        while uart5.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart5.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart5.dr.read().dr().bits() as u8;
      },
      6 => {
        let uart6 = &peripheral_ptr.USART6;
        while uart6.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart6.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart6.dr.read().dr().bits() as u8;
      },
      _ => unreachable!()
    };

    return Some(buffer as char);
  }

  /// Waits until it recieves a byte. Returns an error-enum if problems with the connection are detected.
  pub fn read_byte(&self) -> Option<u8> {
    let peripheral_ptr;
    unsafe {peripheral_ptr = stm32f4::stm32f446::Peripherals::steal();}

    let buffer: u8;
    
    match self.core {
      1 => {
        let uart1 = &peripheral_ptr.USART1;
        while uart1.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart1.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart1.dr.read().dr().bits() as u8;
      },
      2 => {
        let uart2 = &peripheral_ptr.USART2;
        while uart2.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart2.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart2.dr.read().dr().bits() as u8;
      },
      3 => {
        let uart3 = &peripheral_ptr.USART3;
        while uart3.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart3.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart3.dr.read().dr().bits() as u8;
      },
      4 => {
        let uart4 = &peripheral_ptr.UART4;
        while uart4.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart4.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart4.dr.read().dr().bits() as u8;
      },
      5 => {
        let uart5 = &peripheral_ptr.UART5;
        while uart5.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart5.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart5.dr.read().dr().bits() as u8;
      },
      6 => {
        let uart6 = &peripheral_ptr.USART6;
        while uart6.sr.read().rxne().bit_is_clear() {
          if check_uart_errors(uart6.sr.read().bits()).is_err() {return None;}
        }
        buffer = uart6.dr.read().dr().bits() as u8;
      },
      _ => unreachable!()
    };

    return Some(buffer);
  }
}
  
  
// Private Functions ==============================================================================
fn check_uart_errors(sr: u32) -> Result<(), SerialError> {
  let bits = sr & 0xF;

  if bits & 0x8 == 8 {return Err(SerialError::Overrun);}
  else if bits & 0x4 == 4 {return Err(SerialError::Noise);}
  else if bits & 0x2 == 2 {return Err(SerialError::FrameFormat);}
  else if bits & 0x1 == 1 {return Err(SerialError::Parity);}

  return Ok(());
}

fn set_baud(core: u8, baud: u32) {
  let peripheral_ptr;
  unsafe {peripheral_ptr = stm32f4::stm32f446::Peripherals::steal();}
  
  // (Mantisse, Fractal)
  let uartdiv: (f64, f64) = modf(16000000.0 / (16.0 * baud as f64));

  match core {
    1 => {
      let uart1 = &peripheral_ptr.USART1;
      uart1.brr.modify(|_, w| {
        w.div_mantissa().bits(uartdiv.1 as u16);
        w.div_fraction().bits((uartdiv.0 * 16.0) as u8)
      });
    },
    2 => {
      let uart2 = &peripheral_ptr.USART2;
      uart2.brr.modify(|_, w| {
        w.div_mantissa().bits(uartdiv.1 as u16);
        w.div_fraction().bits((uartdiv.0 * 16.0) as u8)
      });
    },
    3 => {
      let uart3 = &peripheral_ptr.USART3;
      uart3.brr.modify(|_, w| {
        w.div_mantissa().bits(uartdiv.1 as u16);
        w.div_fraction().bits((uartdiv.0 * 16.0) as u8)
      });
    },
    4 => {
      let uart4 = &peripheral_ptr.UART4;
      uart4.brr.modify(|_, w| {
        w.div_mantissa().bits(uartdiv.1 as u16);
        w.div_fraction().bits((uartdiv.0 * 16.0) as u8)
      });
    },
    5 => {
      let uart5 = &peripheral_ptr.UART5;
      uart5.brr.modify(|_, w| {
        w.div_mantissa().bits(uartdiv.1 as u16);
        w.div_fraction().bits((uartdiv.0 * 16.0) as u8)
      });
    },
    6 => {
      let uart6 = &peripheral_ptr.USART6;
      uart6.brr.modify(|_, w| {
        w.div_mantissa().bits(uartdiv.1 as u16);
        w.div_fraction().bits((uartdiv.0 * 16.0) as u8)
      });
    },
    _ => unreachable!()
  }
}


#[doc(hidden)]
pub fn modf(x: f64) -> (f64, f64) {
  let rv2: f64;
  let mut u = x.to_bits();
  let mask: u64;
  let e = ((u >> 52 & 0x7ff) as i32) - 0x3ff;

  // no fractional part
  if e >= 52 {
      rv2 = x;
      if e == 0x400 && (u << 12) != 0 {
          /* nan */
          return (x, rv2);
      }
      u &= 1 << 63;
      return (f64::from_bits(u), rv2);
  }

  // no integral part
  if e < 0 {
      u &= 1 << 63;
      rv2 = f64::from_bits(u);
      return (x, rv2);
  }

  mask = ((!0) >> 12) >> e;
  if (u & mask) == 0 {
      rv2 = x;
      u &= 1 << 63;
      return (f64::from_bits(u), rv2);
  }
  u &= !mask;
  rv2 = f64::from_bits(u);
  return (x - rv2, rv2);
}