Skip to content

Commit 93c4805

Browse files
committed
[uart] Fiberize UART implementations
1 parent aeee675 commit 93c4805

File tree

14 files changed

+43
-34
lines changed

14 files changed

+43
-34
lines changed

src/modm/platform/uart/at90_tiny_mega/module.lb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class Instance(Module):
4242
module.description = "Instance {}".format(self.instance)
4343

4444
def prepare(self, module, options):
45-
module.depends(":platform:uart")
4645
load_options(module)
4746
return True
4847

@@ -77,6 +76,7 @@ def prepare(module, options):
7776
":architecture:atomic",
7877
":architecture:interrupt",
7978
":architecture:uart",
79+
":architecture:fiber",
8080
":math:algorithm",
8181
":platform:gpio")
8282

src/modm/platform/uart/at90_tiny_mega/uart_tx.cpp.in

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <modm/architecture/driver/atomic/queue.hpp>
2525
#include <modm/architecture/interface/atomic_lock.hpp>
2626
#include <modm/architecture/interface/interrupt.hpp>
27+
#include <modm/processing/fiber.hpp>
2728

2829
#include "uart_defines.h"
2930

@@ -50,12 +51,7 @@ void
5051
modm::platform::Uart{{ id }}::writeBlocking(uint8_t data)
5152
{
5253
// wait until there is some place in the buffer
53-
while (!write(data))
54-
;
55-
56-
// wait until everything has been sent
57-
while (!isWriteFinished())
58-
;
54+
modm::this_fiber::poll([&]{ return write(data); });
5955
}
6056

6157
void
@@ -67,18 +63,13 @@ modm::platform::Uart{{ id }}::writeBlocking(const uint8_t *data, std::size_t len
6763
while (!write(*data++))
6864
;
6965
}
70-
71-
// then wait
72-
while (!isWriteFinished())
73-
;
7466
}
7567

7668
void
7769
modm::platform::Uart{{ id }}::flushWriteBuffer()
7870
{
7971
// just wait until the last byte has been sent
80-
while (!isWriteFinished())
81-
;
72+
modm::this_fiber::poll([&]{ return isWriteFinished(); });
8273
}
8374

8475
// MARK: - write

src/modm/platform/uart/cortex/itm.cpp.in

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// ----------------------------------------------------------------------------
1111

1212
#include <modm/platform/device.hpp>
13+
#include <modm/processing/fiber.hpp>
1314
#include "itm.hpp"
1415

1516
%% if options["buffer.tx"]
@@ -74,14 +75,18 @@ Itm::enable(uint8_t prescaler)
7475
void
7576
Itm::writeBlocking(uint8_t data)
7677
{
77-
while(not write(data)) ;
78+
modm::this_fiber::poll([&]{ return write(data); });
7879
}
7980

8081
void
8182
Itm::flushWriteBuffer()
8283
{
8384
%% if options["buffer.tx"]
84-
while(!isWriteFinished()) update();
85+
while(!isWriteFinished())
86+
{
87+
update();
88+
modm::this_fiber::yield();
89+
}
8590
%% else
8691
return;
8792
%% endif
@@ -91,6 +96,7 @@ bool
9196
Itm::write(uint8_t data)
9297
{
9398
%% if options["buffer.tx"]
99+
update();
94100
if (txBuffer.push(data)) return true;
95101
update();
96102
return txBuffer.push(data);
@@ -109,16 +115,6 @@ Itm::write(const uint8_t *data, std::size_t length)
109115
return sent;
110116
}
111117

112-
bool
113-
Itm::isWriteFinished()
114-
{
115-
%% if options["buffer.tx"]
116-
return txBuffer.isEmpty();
117-
%% else
118-
return true;
119-
%% endif
120-
}
121-
122118
std::size_t
123119
Itm::discardTransmitBuffer()
124120
{
@@ -138,6 +134,16 @@ Itm::discardTransmitBuffer()
138134
#undef PORT
139135
%% endif
140136

137+
bool
138+
Itm::isWriteFinished()
139+
{
140+
%% if options["buffer.tx"]
141+
return txBuffer.isEmpty() and ITM->PORT[0].u32 != 0;
142+
%% else
143+
return ITM->PORT[0].u32 != 0;
144+
%% endif
145+
}
146+
141147
bool
142148
Itm::write_itm(uint32_t data, uint8_t size)
143149
{

src/modm/platform/uart/cortex/module.lb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def prepare(module, options):
2323

2424
module.depends(
2525
":architecture:uart",
26+
":architecture:fiber",
2627
":cmsis:device")
2728

2829
module.add_option(

src/modm/platform/uart/rp/module.lb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def prepare(module, options):
6161
":platform:gpio",
6262
":platform:clockgen",
6363
":architecture:uart",
64+
":architecture:fiber",
6465
":architecture:interrupt")
6566

6667
for instance in listify(device.get_driver("uart")["instance"]):

src/modm/platform/uart/rp/uart.cpp.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <modm/architecture/interface/atomic_lock.hpp>
1818
#include <modm/architecture/interface/interrupt.hpp>
1919
#include <modm/platform/core/resets.hpp>
20+
#include <modm/processing/fiber.hpp>
2021

2122
#include "../device.hpp"
2223

@@ -60,7 +61,7 @@ void modm::platform::Uart{{ id }}::unreset()
6061
void
6162
modm::platform::Uart{{ id }}::writeBlocking(uint8_t data)
6263
{
63-
while (not write(data));
64+
modm::this_fiber::poll([&]{ return write(data); });
6465
}
6566

6667
void
@@ -72,7 +73,7 @@ modm::platform::Uart{{ id }}::writeBlocking(const uint8_t *data, std::size_t len
7273
void
7374
modm::platform::Uart{{ id }}::flushWriteBuffer()
7475
{
75-
while(!isWriteFinished());
76+
modm::this_fiber::poll([&]{ return isWriteFinished(); });
7677
}
7778

7879
// ----------------------------------------------------------------------------

src/modm/platform/uart/sam-sercom/module.lb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def prepare(module, options):
5050

5151
module.depends(
5252
":architecture:uart",
53+
":architecture:fiber",
5354
":math:algorithm",
5455
":cmsis:device",
5556
":platform:gpio",

src/modm/platform/uart/sam-sercom/uart.cpp.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
#include "../device.hpp"
1616
#include "uart_hal_{{ id }}.hpp"
1717
#include "uart_{{ id }}.hpp"
18+
#include <modm/processing/fiber.hpp>
1819

1920
namespace modm::platform
2021
{
2122

2223
void
2324
{{ name }}::writeBlocking(uint8_t data)
2425
{
25-
while(!{{ hal }}::isTransmitRegisterEmpty());
26+
modm::this_fiber::poll([&]{ return {{ hal }}::isTransmitRegisterEmpty(); });
2627
{{ hal }}::write(data);
2728
}
2829

@@ -37,7 +38,7 @@ void
3738
void
3839
{{ name }}::flushWriteBuffer()
3940
{
40-
return;
41+
modm::this_fiber::poll([&]{ return isWriteFinished(); });
4142
}
4243

4344
bool

src/modm/platform/uart/sam/uart.cpp.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "{{ type }}_{{ id }}.hpp"
1414

1515
#include <modm/architecture/driver/atomic/queue.hpp>
16+
#include <modm/processing/fiber.hpp>
1617

1718
%% set name="{}{}".format(type.capitalize(), id)
1819
%% set reg=name.upper()
@@ -111,7 +112,7 @@ bool
111112
void
112113
{{ name }}::flushWriteBuffer()
113114
{
114-
while(!isWriteFinished());
115+
modm::this_fiber::poll([&]{ return isWriteFinished(); });
115116
}
116117

117118
void

src/modm/platform/uart/sam/uart/module.lb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def prepare(module, options):
6969

7070
module.depends(
7171
":architecture:uart",
72+
":architecture:fiber",
7273
":math:algorithm",
7374
":cmsis:device",
7475
":platform:gpio",

0 commit comments

Comments
 (0)