Skip to content

Commit 80486ca

Browse files
committed
Review request changes
1 parent 42e4f5e commit 80486ca

File tree

5 files changed

+91
-38
lines changed

5 files changed

+91
-38
lines changed

TESTS/mbed_drivers/unbuffered_serial/main.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,27 @@ using namespace utest::v1;
4141
#define MSG_KEY_ECHO_MESSAGE "echo_message"
4242
#define MSG_VALUE_HELLO_WORLD "Hello, world!"
4343

44-
#define STRING_TO_SEND "{{" MSG_KEY_ECHO_MESSAGE ";" MSG_VALUE_HELLO_WORLD "}}\r\n"
4544
#define EXPECTED_ECHOED_STRING "{{" MSG_KEY_ECHO_MESSAGE ";" MSG_VALUE_HELLO_WORLD "}}"
45+
// The target is expected to transmit Greentea messages with \n (or \r\n) or they are not detected by the host
46+
#define STRING_TO_SEND EXPECTED_ECHOED_STRING "\n"
47+
4648

4749
static UnbufferedSerial unbuffered_serial_obj(
4850
USBTX, USBRX, MBED_CONF_PLATFORM_STDIO_BAUD_RATE
4951
);
5052

51-
ssize_t greentea_unbuffered_serial_get_kv(void *buffer, ssize_t length)
53+
static ssize_t unbuffered_serial_read(void *buffer, ssize_t length)
5254
{
5355
if (length == 0) {
5456
return 0;
5557
}
5658

57-
// Ignore the character previously sent to the host that was not read
59+
// Ignore the `\n` character previously sent to the host in the previous
60+
// key-value pair that was not removed from the FIFO.
5861
unsigned char *buf = static_cast<unsigned char *>(buffer);
5962
unbuffered_serial_obj.read(buf, 1);
6063

6164
// Get the message sent by the host
62-
// unsigned char *buf = static_cast<unsigned char *>(buffer);
6365
for (ssize_t i = 0; i < length; i++) {
6466
TEST_ASSERT_EQUAL_UINT(1, unbuffered_serial_obj.read(buf+i, 1));
6567
}
@@ -68,6 +70,13 @@ ssize_t greentea_unbuffered_serial_get_kv(void *buffer, ssize_t length)
6870
}
6971

7072

73+
// Test that data sent using an UnbufferedSerial object is correctly sent.
74+
// The test case sends a Greentea key-value pair message from the target to the
75+
// host using an UnbufferedSerial object and expects the message
76+
// to be echoed back by the host. The host response is received via the Greentea
77+
// framework usual route using greentea_parse_kv(). Success is determined upon
78+
// reception of the echoed message which indicates that the message was received
79+
// by the host as it was sent by the target.
7180
static void test_serial_write()
7281
{
7382
char tx_msg[] = STRING_TO_SEND;
@@ -85,6 +94,13 @@ static void test_serial_write()
8594
TEST_ASSERT_EQUAL_STRING(MSG_VALUE_HELLO_WORLD, rx_value);
8695
}
8796

97+
98+
// Test that data received using an UnbufferedSerial object is correctly received.
99+
// The test case sends a Greentea key-value pair message from the target to the
100+
// host via the Greentea framework usual route using greentea_send_kv().
101+
// It expects the message to be echoed back to the target. An UnbufferedSerial
102+
// object is used to handle the received message. Succes is determined upon
103+
// reception of a key-value pair matching the key-value pair sent by the target.
88104
static void test_serial_read()
89105
{
90106
greentea_send_kv(MSG_KEY_ECHO_MESSAGE, MSG_VALUE_HELLO_WORLD);
@@ -93,11 +109,12 @@ static void test_serial_read()
93109
// Exclude the null terminator which is not read
94110
ssize_t expected_rx_msg_length = sizeof(EXPECTED_ECHOED_STRING)-1;
95111

96-
greentea_unbuffered_serial_get_kv(rx_msg, expected_rx_msg_length);
112+
unbuffered_serial_read(rx_msg, expected_rx_msg_length);
97113

98114
TEST_ASSERT_EQUAL_STRING(EXPECTED_ECHOED_STRING, rx_msg);
99115
}
100116

117+
101118
utest::v1::status_t greentea_setup(const size_t number_of_cases)
102119
{
103120
GREENTEA_SETUP(12, "serial_comms");

drivers/SerialBase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ namespace mbed {
3838
* @{
3939
*/
4040

41-
/** An abstract base class for serial port implementations
42-
* Can't be instantiated in sub-classess
41+
/** A base class for serial port implementations
42+
* Can't be instantiated directly (use UnbufferedSerial or UARTSerial)
4343
*
4444
* @note Synchronization level: Set by subclass
4545
*/

drivers/UnbufferedSerial.h

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121

2222
#if DEVICE_SERIAL || defined(DOXYGEN_ONLY)
2323

24+
#include <cstdarg>
25+
2426
#include "drivers/SerialBase.h"
2527
#include "platform/FileHandle.h"
2628
#include "platform/mbed_toolchain.h"
2729
#include "platform/NonCopyable.h"
28-
#include <cstdarg>
30+
#include "platform/PlatformMutex.h"
31+
2932

3033
namespace mbed {
3134

@@ -64,36 +67,28 @@ class UnbufferedSerial:
6467

6568
/** Write the contents of a buffer to a file
6669
*
67-
* Follows POSIX semantics:
68-
*
69-
* * if blocking, block until all data is written
70-
* * if no data can be written, and non-blocking set, return -EAGAIN
71-
* * if some data can be written, and non-blocking set, write partial
70+
* Blocks until all data is written
7271
*
7372
* @param buffer The buffer to write from
74-
* @param length The number of bytes to write
75-
* @return The number of bytes written, negative error on failure
73+
* @param size The number of bytes to write
74+
* @return The number of bytes written
7675
*/
7776
virtual ssize_t write(const void *buffer, size_t size);
7877

7978
/** Read the contents of a file into a buffer
8079
*
81-
* Follows POSIX semantics:
82-
*
83-
* * if no data is available, and non-blocking set return -EAGAIN
84-
* * if no data is available, and blocking set, wait until data is available
85-
* * If any data is available, call returns immediately
80+
* Blocks and reads exactly one character
8681
*
8782
* @param buffer The buffer to read in to
88-
* @param length The number of bytes to read
89-
* @return The number of bytes read, 0 at end of file, negative error on failure
83+
* @param size The number of bytes to read
84+
* @return The number of bytes read
9085
*/
9186
virtual ssize_t read(void *buffer, size_t size);
9287

9388
/** Move the file position to a given offset from from a given location
9489
*
95-
* Not valid for a device type FileHandle like UARTSerial.
96-
* In case of UARTSerial, returns ESPIPE
90+
* Not valid for a device type FileHandle like UnbufferedSerial.
91+
* In case of UnbufferedSerial, returns ESPIPE
9792
*
9893
* @param offset The offset from whence to move to
9994
* @param whence The start of where to seek
@@ -136,12 +131,17 @@ class UnbufferedSerial:
136131
return 0;
137132
}
138133

139-
/**
140-
* Equivalent to POSIX poll(). Derived from FileHandle.
141-
* Provides a mechanism to multiplex input/output over a set of file
142-
* handles.
134+
135+
/** Check for poll event flags
136+
* Check the events listed in events to see if data can be read or written
137+
* without blocking.
138+
* Call is nonblocking - returns state of events.
139+
*
140+
* @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
141+
*
142+
* @returns bitmask of poll events that have occurred.
143143
*/
144-
virtual short poll(short events) const;
144+
virtual short poll(short events);
145145

146146
#if !(DOXYGEN_ONLY)
147147
protected:
@@ -152,6 +152,17 @@ class UnbufferedSerial:
152152
/* Release exclusive access to this serial port
153153
*/
154154
virtual void unlock(void);
155+
156+
private:
157+
158+
/** Acquire mutex */
159+
virtual void api_lock(void);
160+
161+
/** Release mutex */
162+
virtual void api_unlock(void);
163+
164+
165+
PlatformMutex _mutex;
155166
#endif // !(DOXYGEN_ONLY)
156167
};
157168

drivers/source/UnbufferedSerial.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
* limitations under the License.
1616
*/
1717

18-
#include "hal/serial_api.h"
19-
2018
#include "drivers/UnbufferedSerial.h"
2119

22-
2320
#if DEVICE_SERIAL
2421

22+
#include "platform/mbed_critical.h"
23+
2524
namespace mbed {
2625

2726
UnbufferedSerial::UnbufferedSerial(
@@ -36,9 +35,25 @@ UnbufferedSerial::UnbufferedSerial(
3635
ssize_t UnbufferedSerial::write(const void *buffer, size_t size)
3736
{
3837
const unsigned char *buf = static_cast<const unsigned char *>(buffer);
38+
39+
if (size == 0) {
40+
return 0;
41+
}
42+
43+
bool lock_api = !core_util_in_critical_section();
44+
45+
if (lock_api) {
46+
api_lock();
47+
}
48+
3949
for (size_t i = 0; i < size; i++) {
40-
serial_putc(&_serial, buf[i]);
50+
_base_putc(buf[i]);
51+
}
52+
53+
if (lock_api) {
54+
api_unlock();
4155
}
56+
4257
return size;
4358
}
4459

@@ -48,17 +63,17 @@ ssize_t UnbufferedSerial::read(void *buffer, size_t size)
4863
if (size == 0) {
4964
return 0;
5065
}
51-
buf[0] = serial_getc(&_serial);
66+
buf[0] = _base_getc();
5267
return 1;
5368
}
5469

55-
short UnbufferedSerial::poll(short events) const
70+
short UnbufferedSerial::poll(short events)
5671
{
5772
short revents = 0;
58-
if ((events & POLLIN) && serial_readable((serial_t*)&_serial)) {
73+
if ((events & POLLIN) && SerialBase::readable()) {
5974
revents |= POLLIN;
6075
}
61-
if ((events & POLLOUT) && serial_writable((serial_t*)&_serial)) {
76+
if ((events & POLLOUT) && SerialBase::writeable()) {
6277
revents |= POLLOUT;
6378
}
6479
return revents;
@@ -78,6 +93,16 @@ void UnbufferedSerial::unlock()
7893
// No lock used - external synchronization required
7994
}
8095

96+
void UnbufferedSerial::api_lock(void)
97+
{
98+
_mutex.lock();
99+
}
100+
101+
void UnbufferedSerial::api_unlock(void)
102+
{
103+
_mutex.unlock();
104+
}
105+
81106
} // namespace mbed
82107

83108
#endif // #if DEVICE_SERIAL

features/unsupported/tests/mbed/ticker/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "mbed.h"
22
#include "test_env.h"
33

4-
UnbufferedSerial pc(USBTX, USBRX);
4+
RawSerial pc(USBTX, USBRX);
55

66
Ticker flipper_1;
77
DigitalOut led1(LED1);

0 commit comments

Comments
 (0)