Skip to content

static pin-map: patch for SerialBase class #12033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions drivers/SerialBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ class SerialBase : private NonCopyable<SerialBase> {
/** Initialize serial port
*/
void _init();
void _init_direct();
/* Pointer to serial init function */
void (SerialBase::*_init_func)();

/** Deinitialize serial port
*/
Expand All @@ -345,18 +348,22 @@ class SerialBase : private NonCopyable<SerialBase> {
bool _rx_asynch_set = false;
#endif

serial_t _serial {};
Callback<void()> _irq[IrqCnt];
int _baud;
bool _rx_enabled = true;
bool _tx_enabled = true;
const PinName _tx_pin;
const PinName _rx_pin;
serial_t _serial {};
Callback<void()> _irq[IrqCnt];
int _baud;
bool _rx_enabled = true;
bool _tx_enabled = true;
const PinName _tx_pin;
const PinName _rx_pin;
const serial_pinmap_t *_static_pinmap = NULL;
void (SerialBase::*_set_flow_control_dp_func)(Flow, PinName, PinName) = NULL;

#if DEVICE_SERIAL_FC
Flow _flow_type = Disabled;
PinName _flow1 = NC;
PinName _flow2 = NC;
Flow _flow_type = Disabled;
PinName _flow1 = NC;
PinName _flow2 = NC;
const serial_fc_pinmap_t *_static_pinmap_fc = NULL;
void (SerialBase::*_set_flow_control_sp_func)(Flow, const serial_fc_pinmap_t &) = NULL;
#endif

#endif
Expand Down
41 changes: 31 additions & 10 deletions drivers/source/SerialBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
#endif
_baud(baud),
_tx_pin(tx),
_rx_pin(rx)
_rx_pin(rx),
_init_func(&SerialBase::_init)
{
// No lock needed in the constructor

for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
_irq[i] = NULL;
}

_init();
(this->*_init_func)();
}

SerialBase::SerialBase(const serial_pinmap_t &static_pinmap, int baud) :
Expand All @@ -50,17 +51,17 @@ SerialBase::SerialBase(const serial_pinmap_t &static_pinmap, int baud) :
_serial(),
_baud(baud),
_tx_pin(static_pinmap.tx_pin),
_rx_pin(static_pinmap.rx_pin)
_rx_pin(static_pinmap.rx_pin),
_static_pinmap(&static_pinmap),
_init_func(&SerialBase::_init_direct)
{
// No lock needed in the constructor

for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
_irq[i] = NULL;
}

serial_init_direct(&_serial, &static_pinmap);
serial_baud(&_serial, _baud);
serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
(this->*_init_func)();
}

void SerialBase::baud(int baudrate)
Expand Down Expand Up @@ -150,7 +151,21 @@ void SerialBase::_init()
{
serial_init(&_serial, _tx_pin, _rx_pin);
#if DEVICE_SERIAL_FC
set_flow_control(_flow_type, _flow1, _flow2);
if (_set_flow_control_dp_func) {
(this->*_set_flow_control_dp_func)(_flow_type, _flow1, _flow2);
}
#endif
serial_baud(&_serial, _baud);
serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
}

void SerialBase::_init_direct()
{
serial_init_direct(&_serial, _static_pinmap);
#if DEVICE_SERIAL_FC
if (_static_pinmap_fc && _set_flow_control_dp_func) {
(this->*_set_flow_control_sp_func)(_flow_type, *_static_pinmap_fc);
}
#endif
serial_baud(&_serial, _baud);
serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
Expand All @@ -166,7 +181,7 @@ void SerialBase::enable_input(bool enable)
lock();
if (_rx_enabled != enable) {
if (enable && !_tx_enabled) {
_init();
(this->*_init_func)();
}

core_util_critical_section_enter();
Expand Down Expand Up @@ -203,7 +218,7 @@ void SerialBase::enable_output(bool enable)
lock();
if (_tx_enabled != enable) {
if (enable && !_rx_enabled) {
_init();
(this->*_init_func)();
}

core_util_critical_section_enter();
Expand Down Expand Up @@ -289,6 +304,8 @@ SerialBase::~SerialBase()
#if DEVICE_SERIAL_FC
void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2)
{
MBED_ASSERT(_static_pinmap == NULL); // this function must be used when serial object has been created using dynamic pin-map constructor
_set_flow_control_dp_func = &SerialBase::set_flow_control;
lock();

_flow_type = type;
Expand Down Expand Up @@ -318,9 +335,13 @@ void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2)

void SerialBase::set_flow_control(Flow type, const serial_fc_pinmap_t &static_pinmap)
{
MBED_ASSERT(_static_pinmap != NULL); // this function must be used when serial object has been created using static pin-map constructor
_set_flow_control_sp_func = &SerialBase::set_flow_control;
lock();
_static_pinmap_fc = &static_pinmap;
_flow_type = type;
FlowControl flow_type = (FlowControl)type;
serial_set_flow_control_direct(&_serial, flow_type, &static_pinmap);
serial_set_flow_control_direct(&_serial, flow_type, _static_pinmap_fc);
unlock();
}
#endif
Expand Down