libyggdrasil  v1.0.0
uart.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Asgard - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 
30 #include <string>
31 #include <string_view>
32 #include <array>
33 
34 namespace bsp::asg_coproc::drv {
35 
42  template<addr_t BaseAddress>
43  struct UART {
44 
50  static bool init() {
51  return true;
52  }
53 
59  static bool deinit() {
60  return true;
61  }
62 
69  static void receive(std::string &buffer) {
70  UE = true;
71  while (true) {
72  while (!RXNE);
73 
74  char c = RECV;
75 
76  if (c == '\n' || c == '\r')
77  break;
78  else
79  buffer.push_back(c);
80  }
81  UE = false;
82  }
83 
90  template<size_t N>
91  static void receive(std::array<u8, N> &buffer) {
92  u32 index = 0;
93  UE = true;
94  while (index < N) {
95  while (!RXNE);
96 
97  u8 c = RECV;
98 
99  buffer[index] = c;
100 
101  index++;
102  }
103  UE = false;
104  }
105 
111  static void transmit(std::string_view buffer) {
112  UE = true;
113  for (char c : buffer) {
114  while(!TXE);
115 
116  TRNS = c;
117  }
118  while(!TC);
119  UE = false;
120  }
121 
128  template<size_t N>
129  static void transmit(const std::array<u8, N> &buffer) {
130  UE = true;
131  for (char c : buffer) {
132  while(!TXE);
133 
134  TRNS = c;
135  }
136  while(!TC);
137  UE = false;
138  }
139 
140  private:
141  enum class RegisterMap {
142  CR1 = 0x00,
143  CR2 = 0x04,
144  CR3 = 0x08,
145  ISR = 0x1C,
146  ICR = 0x20,
147  RDR = 0x24,
148  TDR = 0x28,
149  };
150 
158 
159  static inline auto UE = typename CR1::template Field<0, 0>();
160  static inline auto RXNE = typename ISR::template Field<5, 5>();
161  static inline auto TXE = typename ISR::template Field<7, 7>();
162  static inline auto TC = typename ISR::template Field<6, 6>();
163 
164  static inline auto RECV = typename RDR::template Field<0, 7>();
165  static inline auto TRNS = typename TDR::template Field<0, 7>();
166  };
167 
168 }
bsp::Register
MMIO Register abstraction. Gives access to bitfields within the register as well as a reference to th...
Definition: registers.hpp:90
bsp::asg_coproc::drv::UART::transmit
static void transmit(const std::array< u8, N > &buffer)
UART transmit.
Definition: uart.hpp:129
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::asg_coproc::drv
Definition: adc.hpp:32
bsp::asg_coproc::drv::UART::receive
static void receive(std::string &buffer)
UART receive string.
Definition: uart.hpp:69
bsp::asg_coproc::drv::UART::init
static bool init()
Init function.
Definition: uart.hpp:50
bsp::asg_coproc::drv::UART::transmit
static void transmit(std::string_view buffer)
UART transmit string.
Definition: uart.hpp:111
u32
uint32_t u32
Definition: types.h:38
RegisterMap
RegisterMap
Register map.
Definition: hash.cpp:42
uart.hpp
Frontend for the UART abstraction.
bsp::asg_coproc::drv::UART::deinit
static bool deinit()
Deinit function.
Definition: uart.hpp:59
bsp::asg_coproc::drv::UART::receive
static void receive(std::array< u8, N > &buffer)
UART receive.
Definition: uart.hpp:91
bsp::asg_coproc::drv::UART
UART implementation for Asgard.
Definition: uart.hpp:43