Numworks Epsilon  1.4.1
Graphing Calculator Operating System
console.cpp
Go to the documentation of this file.
1 #include <ion.h>
2 #include "console.h"
3 
4 /* This file implements a serial console.
5  * We use a 115200 8N1 serial port */
6 
7 namespace Ion {
8 namespace Console {
9 
10 char readChar() {
11  while (Device::UARTPort.SR()->getRXNE() == 0) {
12  }
13  return (char)Device::UARTPort.DR()->get();
14 }
15 
16 void writeChar(char c) {
17  while (Device::UARTPort.SR()->getTXE() == 0) {
18  }
20 }
21 
22 }
23 }
24 
25 namespace Ion {
26 namespace Console {
27 namespace Device {
28 
29 void init() {
30  RCC.APB1ENR()->setUSART3EN(true);
31 
32  for(const GPIOPin & g : Pins) {
33  g.group().MODER()->setMode(g.pin(), GPIO::MODER::Mode::AlternateFunction);
34  g.group().AFR()->setAlternateFunction(g.pin(), GPIO::AFR::AlternateFunction::AF7);
35  }
36 
37  UARTPort.CR1()->setUE(true);
38  UARTPort.CR1()->setTE(true);
39  UARTPort.CR1()->setRE(true);
40 
41  /* We need to set the baud rate of the UART port.
42  * This is set relative to the APB1 clock, which runs at 48 MHz.
43  *
44  * The baud rate is set by the following equation:
45  * BaudRate = fAPB1/(16*USARTDIV), where USARTDIV is a divider.
46  * In other words, USARDFIV = fAPB1/(16*BaudRate). All frequencies in Hz.
47  *
48  * In our case, fAPB1 = 48 MHz, so USARTDIV = 26.0416667
49  * DIV_MANTISSA = 26
50  * DIV_FRAC = 16*0.0416667 = 1
51  */
52  UARTPort.BRR()->setDIV_MANTISSA(26);
53  UARTPort.BRR()->setDIV_FRAC(1);
54 }
55 
56 void shutdown() {
57  for(const GPIOPin & g : Pins) {
58  g.group().MODER()->setMode(g.pin(), GPIO::MODER::Mode::Analog);
59  g.group().PUPDR()->setPull(g.pin(), GPIO::PUPDR::Pull::None);
60  }
61 }
62 
63 bool peerConnected() {
64  RxPin.group().PUPDR()->setPull(RxPin.pin(), GPIO::PUPDR::Pull::Down);
65  RxPin.group().MODER()->setMode(RxPin.pin(), GPIO::MODER::Mode::Input);
66  msleep(1);
67  bool result = RxPin.group().IDR()->get(RxPin.pin());
68  RxPin.group().PUPDR()->setPull(RxPin.pin(), GPIO::PUPDR::Pull::None);
69  RxPin.group().MODER()->setMode(RxPin.pin(), GPIO::MODER::Mode::AlternateFunction);
70  return result;
71 }
72 
73 
74 }
75 }
76 }
char readChar()
Definition: console.cpp:10
constexpr USART UARTPort
Definition: console.h:21
void set(uint16_t v) volatile
Definition: usart.h:19
void msleep(long ms)
Definition: ion.cpp:4
Definition: rcc.h:6
c(generic_all_nodes)
uint16_t get() volatile
Definition: usart.h:16
void writeChar(char c)
Definition: console.cpp:16
Definition: gpio.h:95
Definition: backlight.h:6