Skip to content

Commit

Permalink
Implement MinimalSerial class
Browse files Browse the repository at this point in the history
  • Loading branch information
hugueskamba committed Oct 28, 2019
1 parent e2578db commit c95e193
Show file tree
Hide file tree
Showing 9 changed files with 708 additions and 105 deletions.
123 changes: 123 additions & 0 deletions drivers/MinimalSerial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* mbed Microcontroller Library
* Copyright (c) 2019 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_MINIMAL_SERIAL_H
#define MBED_MINIMAL_SERIAL_H

#include "platform/platform.h"

#if DEVICE_SERIAL || defined(DOXYGEN_ONLY)
#include "hal/serial_api.h"
#include "platform/NonCopyable.h"
#include "PinNames.h"

namespace mbed {

/**
* \defgroup drivers_MinimalSerial MinimalSerial class
* \ingroup drivers-public-api-uart
* @{
*/

/** A abstract class for serial port implementations
*/
class MinimalSerial : private NonCopyable<MinimalSerial> {

public:

enum Flow {
Disabled = 0,
RTS,
CTS,
RTSCTS
};

#if DEVICE_SERIAL_FC
/** Set the flow control type on the serial port
*
* @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
* @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
* @param flow2 the second flow control pin (CTS for RTSCTS)
*/
void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
#endif // DEVICE_SERIAL_FC

/** Set the baud rate of the serial port
*
* @param baudrate The baudrate of the serial port.
*/
void baud(int baudrate);

/** Write a char to the serial port
*
* @param c The char to write
*
* @returns The written char
*/
int putc(int c);

/** Read a char to the serial port
*
* @returns The char read from the serial port
*/
int getc();

protected:
MinimalSerial(
PinName tx,
PinName rx,
int baud
);

MinimalSerial(
serial_t &serial_ref,
bool serial_inited,
PinName tx,
PinName rx,
int baud
);

virtual ~MinimalSerial();

int _base_getc();

int _base_putc(int c);

int _baud;

serial_t _serial;

#if !defined(DOXYGEN_ONLY)
/** Acquire exclusive access to this serial port
*/
virtual void lock(void){};

/** Release exclusive access to this serial port
*/
virtual void unlock(void){};
#endif // !defined(DOXYGEN_ONLY)

private:
void init(PinName tx, PinName rx);
};

/** @}*/

} // namespace mbed

#endif // DEVICE_SERIAL || defined(DOXYGEN_ONLY)

#endif // MBED_MINIMAL_SERIAL_H
46 changes: 3 additions & 43 deletions drivers/SerialBase.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
* Copyright (c) 2006-2019 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -25,6 +25,7 @@
#include "hal/serial_api.h"
#include "platform/mbed_toolchain.h"
#include "platform/NonCopyable.h"
#include "drivers/MinimalSerial.h"

#if DEVICE_SERIAL_ASYNCH
#include "platform/CThunk.h"
Expand All @@ -43,15 +44,9 @@ namespace mbed {
*
* @note Synchronization level: Set by subclass
*/
class SerialBase : private NonCopyable<SerialBase> {
class SerialBase : public MinimalSerial, private NonCopyable<SerialBase> {

public:
/** Set the baud rate of the serial port
*
* @param baudrate The baudrate of the serial port (default = 9600).
*/
void baud(int baudrate);

enum Parity {
None = 0,
Odd,
Expand All @@ -67,13 +62,6 @@ class SerialBase : private NonCopyable<SerialBase> {
IrqCnt
};

enum Flow {
Disabled = 0,
RTS,
CTS,
RTSCTS
};

/** Set the transmission format used by the serial port
*
* @param bits The number of bits in a word (5-8; default = 8)
Expand Down Expand Up @@ -155,29 +143,8 @@ class SerialBase : private NonCopyable<SerialBase> {
*/
void send_break();

#if !defined(DOXYGEN_ONLY)
protected:

/** Acquire exclusive access to this serial port
*/
virtual void lock(void);

/** Release exclusive access to this serial port
*/
virtual void unlock(void);
#endif
public:

#if DEVICE_SERIAL_FC
/** Set the flow control type on the serial port
*
* @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
* @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
* @param flow2 the second flow control pin (CTS for RTSCTS)
*/
void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
#endif

static void _irq_handler(uint32_t id, SerialIrq irq_type);

#if DEVICE_SERIAL_ASYNCH
Expand Down Expand Up @@ -291,10 +258,6 @@ class SerialBase : private NonCopyable<SerialBase> {
SerialBase(PinName tx, PinName rx, int baud);
virtual ~SerialBase();

int _base_getc();

int _base_putc(int c);

#if DEVICE_SERIAL_ASYNCH
CThunk<SerialBase> _thunk_irq;
DMAUsage _tx_usage;
Expand All @@ -304,10 +267,7 @@ class SerialBase : private NonCopyable<SerialBase> {
bool _tx_asynch_set;
bool _rx_asynch_set;
#endif

serial_t _serial;
Callback<void()> _irq[IrqCnt];
int _baud;
#endif
};

Expand Down
Loading

0 comments on commit c95e193

Please sign in to comment.