From 0fea572babe4b1f1a64ed8df19fe6a0a64c6a7b5 Mon Sep 17 00:00:00 2001 From: Hugues Kamba Date: Tue, 25 Jun 2019 15:13:28 +0100 Subject: [PATCH] Drivers: Sanity cleanup for coherence and reducing size (#2) * Move implementations from headers to sources in order to remove header file inclusions that do not need to be there. * Move one-line methods that do not call other modules methods to the header files. * Replace implicit inclusion of header files by explicit inclusions of the header files needed. * Explicitly include header files in other modules that relied on implicit inclusion of said header files. * Update the year in the Copyright notice of modules modified. --- drivers/AnalogIn.cpp | 27 ++++++- drivers/AnalogIn.h | 26 ++----- drivers/AnalogOut.cpp | 62 ++++++++++++++++ drivers/AnalogOut.h | 39 ++-------- drivers/CAN.cpp | 48 ++++++++++--- drivers/CAN.h | 49 ++++--------- drivers/DigitalIn.cpp | 31 ++++++++ drivers/DigitalIn.h | 11 +-- drivers/DigitalInOut.cpp | 60 ++++++++++++++++ drivers/DigitalInOut.h | 39 ++-------- drivers/DigitalOut.cpp | 39 ++++++++++ drivers/DigitalOut.h | 18 +---- drivers/FlashIAP.cpp | 39 +--------- drivers/FlashIAP.h | 36 ++++++++-- drivers/I2C.cpp | 12 +--- drivers/I2C.h | 14 ++-- drivers/I2CSlave.cpp | 39 +--------- drivers/I2CSlave.h | 35 +++++++-- drivers/InterruptIn.cpp | 28 +------- drivers/InterruptIn.h | 23 ++++-- drivers/InterruptManager.cpp | 32 --------- drivers/InterruptManager.h | 35 +++++++-- drivers/MbedCRC.cpp | 1 - drivers/PortIn.cpp | 43 +++++++++++ drivers/PortIn.h | 17 +---- drivers/PortInOut.cpp | 72 +++++++++++++++++++ drivers/PortInOut.h | 43 ++--------- drivers/PortOut.cpp | 51 ++++++++++++++ drivers/PortOut.h | 22 ++---- drivers/PwmOut.cpp | 133 +++++++++++++++++++++++++++++++++++ drivers/PwmOut.h | 107 ++++------------------------ drivers/QSPI.cpp | 10 --- drivers/QSPI.h | 10 ++- drivers/RawSerial.cpp | 23 +----- drivers/RawSerial.h | 18 +++-- drivers/SPI.cpp | 51 +------------- drivers/SPI.h | 60 +++++++++++++--- drivers/SPISlave.cpp | 15 ---- drivers/SPISlave.h | 15 +++- drivers/Serial.cpp | 33 +-------- drivers/Serial.h | 34 ++++++--- drivers/SerialBase.cpp | 19 ----- drivers/SerialBase.h | 17 ++++- drivers/SerialWireOutput.cpp | 41 +++++++++++ drivers/SerialWireOutput.h | 18 ++--- drivers/Ticker.cpp | 12 +++- drivers/Ticker.h | 12 +--- drivers/Timeout.h | 3 +- drivers/Timer.cpp | 16 +---- drivers/Timer.h | 13 ++-- drivers/TimerEvent.cpp | 29 +------- drivers/TimerEvent.h | 30 ++++++-- drivers/UARTSerial.cpp | 68 +----------------- drivers/UARTSerial.h | 59 ++++++++++++---- hal/LowPowerTickerWrapper.h | 4 +- 55 files changed, 1016 insertions(+), 825 deletions(-) create mode 100644 drivers/AnalogOut.cpp create mode 100644 drivers/DigitalIn.cpp create mode 100644 drivers/DigitalInOut.cpp create mode 100644 drivers/DigitalOut.cpp create mode 100644 drivers/PortIn.cpp create mode 100644 drivers/PortInOut.cpp create mode 100644 drivers/PortOut.cpp create mode 100644 drivers/PwmOut.cpp create mode 100644 drivers/SerialWireOutput.cpp diff --git a/drivers/AnalogIn.cpp b/drivers/AnalogIn.cpp index dd9d22f306f2..97b1b8e13863 100644 --- a/drivers/AnalogIn.cpp +++ b/drivers/AnalogIn.cpp @@ -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"); @@ -23,6 +23,29 @@ namespace mbed { SingletonPtr AnalogIn::_mutex; -}; +AnalogIn::AnalogIn(PinName pin) +{ + lock(); + analogin_init(&_adc, pin); + unlock(); +} + +float AnalogIn::read() +{ + lock(); + float ret = analogin_read(&_adc); + unlock(); + return ret; +} + +unsigned short AnalogIn::read_u16() +{ + lock(); + unsigned short ret = analogin_read_u16(&_adc); + unlock(); + return ret; +} + +} // namespace mbed #endif diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h index 8f247e5975b6..633b3b0b4e36 100644 --- a/drivers/AnalogIn.h +++ b/drivers/AnalogIn.h @@ -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"); @@ -58,37 +58,20 @@ class AnalogIn { * * @param pin AnalogIn pin to connect to */ - AnalogIn(PinName pin) - { - lock(); - analogin_init(&_adc, pin); - unlock(); - } + AnalogIn(PinName pin); /** Read the input voltage, represented as a float in the range [0.0, 1.0] * * @returns A floating-point value representing the current input voltage, measured as a percentage */ - float read() - { - lock(); - float ret = analogin_read(&_adc); - unlock(); - return ret; - } + float read(); /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF] * * @returns * 16-bit unsigned short representing the current input voltage, normalized to a 16-bit value */ - unsigned short read_u16() - { - lock(); - unsigned short ret = analogin_read_u16(&_adc); - unlock(); - return ret; - } + unsigned short read_u16(); /** An operator shorthand for read() * @@ -120,7 +103,6 @@ class AnalogIn { { _mutex->lock(); } - virtual void unlock() { _mutex->unlock(); diff --git a/drivers/AnalogOut.cpp b/drivers/AnalogOut.cpp new file mode 100644 index 000000000000..0ca51c0f710d --- /dev/null +++ b/drivers/AnalogOut.cpp @@ -0,0 +1,62 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-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. + */ + +#include "drivers/AnalogOut.h" + +#if DEVICE_ANALOGOUT + +namespace mbed { + +void AnalogOut::write(float value) +{ + lock(); + analogout_write(&_dac, value); + unlock(); +} + +void AnalogOut::write_u16(unsigned short value) +{ + lock(); + analogout_write_u16(&_dac, value); + unlock(); +} + +float AnalogOut::read() +{ + lock(); + float ret = analogout_read(&_dac); + unlock(); + return ret; +} + +AnalogOut &AnalogOut::operator= (float percent) +{ + // Underlying write call is thread safe + write(percent); + return *this; +} + +AnalogOut &AnalogOut::operator= (AnalogOut &rhs) +{ + // Underlying write call is thread safe + write(rhs.read()); + return *this; +} + +} // namespace mbed + +#endif // DEVICE_ANALOGOUT diff --git a/drivers/AnalogOut.h b/drivers/AnalogOut.h index d12cbafcc4d9..ec75c7a333bb 100644 --- a/drivers/AnalogOut.h +++ b/drivers/AnalogOut.h @@ -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"); @@ -70,24 +70,14 @@ class AnalogOut { * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). * Values outside this range will be saturated to 0.0f or 1.0f. */ - void write(float value) - { - lock(); - analogout_write(&_dac, value); - unlock(); - } + void write(float value); /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF] * * @param value 16-bit unsigned short representing the output voltage, * normalized to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v) */ - void write_u16(unsigned short value) - { - lock(); - analogout_write_u16(&_dac, value); - unlock(); - } + void write_u16(unsigned short value); /** Return the current output voltage setting, measured as a percentage (float) * @@ -99,33 +89,17 @@ class AnalogOut { * @note * This value may not match exactly the value set by a previous write(). */ - float read() - { - lock(); - float ret = analogout_read(&_dac); - unlock(); - return ret; - } + float read(); /** An operator shorthand for write() * \sa AnalogOut::write() */ - AnalogOut &operator= (float percent) - { - // Underlying write call is thread safe - write(percent); - return *this; - } + AnalogOut &operator= (float percent); /** An operator shorthand for write() * \sa AnalogOut::write() */ - AnalogOut &operator= (AnalogOut &rhs) - { - // Underlying write call is thread safe - write(rhs.read()); - return *this; - } + AnalogOut &operator= (AnalogOut &rhs); /** An operator shorthand for read() * \sa AnalogOut::read() @@ -147,7 +121,6 @@ class AnalogOut { { _mutex.lock(); } - virtual void unlock() { _mutex.unlock(); diff --git a/drivers/CAN.cpp b/drivers/CAN.cpp index a3699e3919d7..78b6762f3437 100644 --- a/drivers/CAN.cpp +++ b/drivers/CAN.cpp @@ -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"); @@ -22,6 +22,42 @@ namespace mbed { +CANMessage::CANMessage() : CAN_Message() +{ + len = 8U; + type = CANData; + format = CANStandard; + id = 0U; + memset(data, 0, 8); +} + +CANMessage::CANMessage(unsigned int _id, const unsigned char *_data, unsigned char _len, CANType _type, CANFormat _format) +{ + len = _len & 0xF; + type = _type; + format = _format; + id = _id; + memcpy(data, _data, _len); +} + +CANMessage::CANMessage(unsigned int _id, const char *_data, unsigned char _len, CANType _type, CANFormat _format) +{ + len = _len & 0xF; + type = _type; + format = _format; + id = _id; + memcpy(data, _data, _len); +} + +CANMessage::CANMessage(unsigned int _id, CANFormat _format) +{ + len = 0; + type = CANRemote; + format = _format; + id = _id; + memset(data, 0, 8); +} + CAN::CAN(PinName rd, PinName td) : _can(), _irq() { // No lock needed in constructor @@ -157,16 +193,6 @@ void CAN::_irq_handler(uint32_t id, CanIrqType type) } } -void CAN::lock() -{ - _mutex.lock(); -} - -void CAN::unlock() -{ - _mutex.unlock(); -} - } // namespace mbed #endif diff --git a/drivers/CAN.h b/drivers/CAN.h index 665677efc0fd..05cb1ec25294 100644 --- a/drivers/CAN.h +++ b/drivers/CAN.h @@ -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"); @@ -39,14 +39,7 @@ class CANMessage : public CAN_Message { public: /** Creates empty CAN message. */ - CANMessage() : CAN_Message() - { - len = 8U; - type = CANData; - format = CANStandard; - id = 0U; - memset(data, 0, 8); - } + CANMessage(); /** Creates CAN message with specific content. * @@ -56,14 +49,7 @@ class CANMessage : public CAN_Message { * @param _type Type of Data: Use enum CANType for valid parameter values * @param _format Data Format: Use enum CANFormat for valid parameter values */ - CANMessage(unsigned int _id, const unsigned char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) - { - len = _len & 0xF; - type = _type; - format = _format; - id = _id; - memcpy(data, _data, _len); - } + CANMessage(unsigned int _id, const unsigned char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard); /** Creates CAN message with specific content. @@ -74,28 +60,14 @@ class CANMessage : public CAN_Message { * @param _type Type of Data: Use enum CANType for valid parameter values * @param _format Data Format: Use enum CANFormat for valid parameter values */ - CANMessage(unsigned int _id, const char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) - { - len = _len & 0xF; - type = _type; - format = _format; - id = _id; - memcpy(data, _data, _len); - } + CANMessage(unsigned int _id, const char *_data, unsigned char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard); /** Creates CAN remote message. * * @param _id Message ID * @param _format Data Format: Use enum CANType for valid parameter values */ - CANMessage(unsigned int _id, CANFormat _format = CANStandard) - { - len = 0; - type = CANRemote; - format = _format; - id = _id; - memset(data, 0, 8); - } + CANMessage(unsigned int _id, CANFormat _format = CANStandard); }; /** A can bus client, used for communicating with can devices @@ -313,8 +285,15 @@ class CAN : private NonCopyable { #if !defined(DOXYGEN_ONLY) protected: - virtual void lock(); - virtual void unlock(); + virtual void lock() + { + _mutex.lock(); + } + virtual void unlock() + { + _mutex.unlock(); + } + can_t _can; Callback _irq[IrqCnt]; PlatformMutex _mutex; diff --git a/drivers/DigitalIn.cpp b/drivers/DigitalIn.cpp new file mode 100644 index 000000000000..064503eee493 --- /dev/null +++ b/drivers/DigitalIn.cpp @@ -0,0 +1,31 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-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. + */ + +#include "drivers/DigitalIn.h" + +#include "platform/mbed_critical.h" + +namespace mbed { + +void DigitalIn::mode(PinMode pull) +{ + core_util_critical_section_enter(); + gpio_mode(&gpio, pull); + core_util_critical_section_exit(); +} + +} // namespace mbed diff --git a/drivers/DigitalIn.h b/drivers/DigitalIn.h index 348a87bf33a4..b2406ea8e78a 100644 --- a/drivers/DigitalIn.h +++ b/drivers/DigitalIn.h @@ -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"); @@ -20,7 +20,6 @@ #include "platform/platform.h" #include "hal/gpio_api.h" -#include "platform/mbed_critical.h" namespace mbed { /** \addtogroup drivers */ @@ -72,6 +71,7 @@ class DigitalIn { // No lock needed in the constructor gpio_init_in_ex(&gpio, pin, mode); } + /** Read the input, represented as 0 or 1 (int) * * @returns @@ -88,12 +88,7 @@ class DigitalIn { * * @param pull PullUp, PullDown, PullNone, OpenDrain */ - void mode(PinMode pull) - { - core_util_critical_section_enter(); - gpio_mode(&gpio, pull); - core_util_critical_section_exit(); - } + void mode(PinMode pull); /** Return the output setting, represented as 0 or 1 (int) * diff --git a/drivers/DigitalInOut.cpp b/drivers/DigitalInOut.cpp new file mode 100644 index 000000000000..7a9cfef82467 --- /dev/null +++ b/drivers/DigitalInOut.cpp @@ -0,0 +1,60 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-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. + */ + +#include "drivers/DigitalInOut.h" + +#include "platform/mbed_critical.h" + +namespace mbed { + +void DigitalInOut::output() +{ + core_util_critical_section_enter(); + gpio_dir(&gpio, PIN_OUTPUT); + core_util_critical_section_exit(); +} + +void DigitalInOut::input() +{ + core_util_critical_section_enter(); + gpio_dir(&gpio, PIN_INPUT); + core_util_critical_section_exit(); +} + +void DigitalInOut::mode(PinMode pull) +{ + core_util_critical_section_enter(); + gpio_mode(&gpio, pull); + core_util_critical_section_exit(); +} + +DigitalInOut &DigitalInOut::operator= (int value) +{ + // Underlying write is thread safe + write(value); + return *this; +} + +DigitalInOut &DigitalInOut::operator= (DigitalInOut &rhs) +{ + core_util_critical_section_enter(); + write(rhs.read()); + core_util_critical_section_exit(); + return *this; +} + +} // namespace mbed diff --git a/drivers/DigitalInOut.h b/drivers/DigitalInOut.h index 9ceb025aee8f..c21253bf89f4 100644 --- a/drivers/DigitalInOut.h +++ b/drivers/DigitalInOut.h @@ -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"); @@ -20,7 +20,6 @@ #include "platform/platform.h" #include "hal/gpio_api.h" -#include "platform/mbed_critical.h" namespace mbed { /** \addtogroup drivers */ @@ -81,32 +80,17 @@ class DigitalInOut { /** Set as an output */ - void output() - { - core_util_critical_section_enter(); - gpio_dir(&gpio, PIN_OUTPUT); - core_util_critical_section_exit(); - } + void output(); /** Set as an input */ - void input() - { - core_util_critical_section_enter(); - gpio_dir(&gpio, PIN_INPUT); - core_util_critical_section_exit(); - } + void input(); /** Set the input pin mode * * @param pull PullUp, PullDown, PullNone, OpenDrain */ - void mode(PinMode pull) - { - core_util_critical_section_enter(); - gpio_mode(&gpio, pull); - core_util_critical_section_exit(); - } + void mode(PinMode pull); /** Return the output setting, represented as 0 or 1 (int) * @@ -130,24 +114,13 @@ class DigitalInOut { * inout = button; // Equivalent to inout.write(button.read()) * @endcode */ - DigitalInOut &operator= (int value) - { - // Underlying write is thread safe - write(value); - return *this; - } + DigitalInOut &operator= (int value); /**A shorthand for write() using the assignment operator which copies the * state from the DigitalInOut argument. * \sa DigitalInOut::write() */ - DigitalInOut &operator= (DigitalInOut &rhs) - { - core_util_critical_section_enter(); - write(rhs.read()); - core_util_critical_section_exit(); - return *this; - } + DigitalInOut &operator= (DigitalInOut &rhs); /** A shorthand for read() * \sa DigitalInOut::read() diff --git a/drivers/DigitalOut.cpp b/drivers/DigitalOut.cpp new file mode 100644 index 000000000000..20ec03bac798 --- /dev/null +++ b/drivers/DigitalOut.cpp @@ -0,0 +1,39 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-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. + */ + +#include "drivers/DigitalOut.h" + +#include "platform/mbed_critical.h" + +namespace mbed { + +DigitalOut &DigitalOut::operator= (int value) +{ + // Underlying write is thread safe + write(value); + return *this; +} + +DigitalOut &DigitalOut::operator= (DigitalOut &rhs) +{ + core_util_critical_section_enter(); + write(rhs.read()); + core_util_critical_section_exit(); + return *this; +} + +} // namespace mbed diff --git a/drivers/DigitalOut.h b/drivers/DigitalOut.h index b9fe1f2d3ec1..41ba5c5460ed 100644 --- a/drivers/DigitalOut.h +++ b/drivers/DigitalOut.h @@ -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"); @@ -19,7 +19,6 @@ #include "platform/platform.h" #include "hal/gpio_api.h" -#include "platform/mbed_critical.h" namespace mbed { /** \addtogroup drivers */ @@ -111,24 +110,13 @@ class DigitalOut { * led = button; // Equivalent to led.write(button.read()) * @endcode */ - DigitalOut &operator= (int value) - { - // Underlying write is thread safe - write(value); - return *this; - } + DigitalOut &operator= (int value); /** A shorthand for write() using the assignment operator which copies the * state from the DigitalOut argument. * \sa DigitalOut::write() */ - DigitalOut &operator= (DigitalOut &rhs) - { - core_util_critical_section_enter(); - write(rhs.read()); - core_util_critical_section_exit(); - return *this; - } + DigitalOut &operator= (DigitalOut &rhs); /** A shorthand for read() * \sa DigitalOut::read() diff --git a/drivers/FlashIAP.cpp b/drivers/FlashIAP.cpp index ff015092593f..45031ce64508 100644 --- a/drivers/FlashIAP.cpp +++ b/drivers/FlashIAP.cpp @@ -1,5 +1,5 @@ /* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited + * Copyright (c) 2017-2019 ARM Limited * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -46,16 +46,6 @@ static inline bool is_aligned(uint32_t number, uint32_t alignment) } } -FlashIAP::FlashIAP() -{ - -} - -FlashIAP::~FlashIAP() -{ - -} - int FlashIAP::init() { int ret = 0; @@ -216,31 +206,6 @@ int FlashIAP::erase(uint32_t addr, uint32_t size) return ret; } -uint32_t FlashIAP::get_page_size() const -{ - return flash_get_page_size(&_flash); -} - -uint32_t FlashIAP::get_sector_size(uint32_t addr) const -{ - return flash_get_sector_size(&_flash, addr); -} - -uint32_t FlashIAP::get_flash_start() const -{ - return flash_get_start_address(&_flash); -} - -uint32_t FlashIAP::get_flash_size() const -{ - return flash_get_size(&_flash); -} - -uint8_t FlashIAP::get_erase_value() const -{ - return flash_get_erase_value(&_flash); -} - -} +} // namespace mbed #endif diff --git a/drivers/FlashIAP.h b/drivers/FlashIAP.h index 0c3c8e104952..47c9fc6939a8 100644 --- a/drivers/FlashIAP.h +++ b/drivers/FlashIAP.h @@ -58,8 +58,15 @@ namespace mbed { */ class FlashIAP : private NonCopyable { public: - FlashIAP(); - ~FlashIAP(); + FlashIAP() + { + + } + + ~FlashIAP() + { + + } /** Initialize a flash IAP device * @@ -114,33 +121,48 @@ class FlashIAP : private NonCopyable { * @param addr Address of or inside the sector to query * @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped */ - uint32_t get_sector_size(uint32_t addr) const; + uint32_t get_sector_size(uint32_t addr) const + { + return flash_get_sector_size(&_flash, addr); + } /** Get the flash start address * * @return Flash start address */ - uint32_t get_flash_start() const; + uint32_t get_flash_start() const + { + return flash_get_start_address(&_flash); + } /** Get the flash size * * @return Flash size */ - uint32_t get_flash_size() const; + uint32_t get_flash_size() const + { + return flash_get_size(&_flash); + } /** Get the program page size * * The page size defines the writable page size * @return Size of a program page in bytes */ - uint32_t get_page_size() const; + uint32_t get_page_size() const + { + return flash_get_page_size(&_flash); + } /** Get the flash erase value * * Get the value we read after erase operation * @return flash erase value */ - uint8_t get_erase_value() const; + uint8_t get_erase_value() const + { + return flash_get_erase_value(&_flash); + } #if !defined(DOXYGEN_ONLY) private: diff --git a/drivers/I2C.cpp b/drivers/I2C.cpp index 3e06825c17d0..8221f46ad9e5 100644 --- a/drivers/I2C.cpp +++ b/drivers/I2C.cpp @@ -1,5 +1,5 @@ /* mbed Microcontroller Library - * Copyright (c) 2006-2015 ARM Limited + * Copyright (c) 2006-2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -131,16 +131,6 @@ void I2C::stop(void) unlock(); } -void I2C::lock() -{ - _mutex->lock(); -} - -void I2C::unlock() -{ - _mutex->unlock(); -} - int I2C::recover(PinName sda, PinName scl) { DigitalInOut pin_sda(sda, PIN_INPUT, PullNone, 1); diff --git a/drivers/I2C.h b/drivers/I2C.h index 9a19bd124740..97ca24536bd1 100644 --- a/drivers/I2C.h +++ b/drivers/I2C.h @@ -1,5 +1,5 @@ /* mbed Microcontroller Library - * Copyright (c) 2006-2015 ARM Limited + * Copyright (c) 2006-2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -30,7 +30,7 @@ #if DEVICE_I2C_ASYNCH #include "platform/CThunk.h" #include "hal/dma_api.h" -#include "platform/FunctionPointer.h" +#include "platform/Callback.h" #endif namespace mbed { @@ -164,11 +164,17 @@ class I2C : private NonCopyable { /** Acquire exclusive access to this I2C bus */ - virtual void lock(void); + virtual void lock(void) + { + _mutex->lock(); + } /** Release exclusive access to this I2C bus */ - virtual void unlock(void); + virtual void unlock(void) + { + _mutex->unlock(); + } virtual ~I2C() { diff --git a/drivers/I2CSlave.cpp b/drivers/I2CSlave.cpp index f0697460be60..cb37e88ef5f5 100644 --- a/drivers/I2CSlave.cpp +++ b/drivers/I2CSlave.cpp @@ -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"); @@ -27,47 +27,12 @@ I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c() i2c_slave_mode(&_i2c, 1); } -void I2CSlave::frequency(int hz) -{ - i2c_frequency(&_i2c, hz); -} - void I2CSlave::address(int address) { int addr = (address & 0xFF) | 1; i2c_slave_address(&_i2c, 0, addr, 0); } -int I2CSlave::receive(void) -{ - return i2c_slave_receive(&_i2c); -} - -int I2CSlave::read(char *data, int length) -{ - return i2c_slave_read(&_i2c, data, length) != length; -} - -int I2CSlave::read(void) -{ - return i2c_byte_read(&_i2c, 0); -} - -int I2CSlave::write(const char *data, int length) -{ - return i2c_slave_write(&_i2c, data, length) != length; -} - -int I2CSlave::write(int data) -{ - return i2c_byte_write(&_i2c, data); -} - -void I2CSlave::stop(void) -{ - i2c_stop(&_i2c); -} - -} +} // namespace mbed #endif diff --git a/drivers/I2CSlave.h b/drivers/I2CSlave.h index 78715ba93452..529b5bc433f6 100644 --- a/drivers/I2CSlave.h +++ b/drivers/I2CSlave.h @@ -87,7 +87,10 @@ class I2CSlave { * * @param hz The bus frequency in Hertz. */ - void frequency(int hz); + void frequency(int hz) + { + i2c_frequency(&_i2c, hz); + } /** Check if this I2C Slave has been addressed. * @@ -97,7 +100,10 @@ class I2CSlave { * @retval WriteAddressed The master is writing to this slave. * @retval WriteGeneral The master is writing to all slave. */ - int receive(void); + int receive(void) + { + return i2c_slave_receive(&_i2c); + } /** Read specified number of bytes from an I2C master. * @@ -108,13 +114,19 @@ class I2CSlave { * @retval 0 If the number of bytes read is equal to length requested. * @retval nonzero On error or if the number of bytes read is less than requested. */ - int read(char *data, int length); + int read(char *data, int length) + { + return i2c_slave_read(&_i2c, data, length) != length; + } /** Read a single byte from an I2C master. * * @return The byte read. */ - int read(void); + int read(void) + { + return i2c_byte_read(&_i2c, 0); + } /** Write to an I2C master. * @@ -125,7 +137,10 @@ class I2CSlave { * @retval 0 If written all bytes successfully. * @retval nonzero On error or if the number of bytes written is less than requested. */ - int write(const char *data, int length); + int write(const char *data, int length) + { + return i2c_slave_write(&_i2c, data, length) != length; + } /** Write a single byte to an I2C master. * @@ -136,7 +151,10 @@ class I2CSlave { * @retval 1 If an ACK is received. * @retval 2 On timeout. */ - int write(int data); + int write(int data) + { + return i2c_byte_write(&_i2c, data); + } /** Set the I2C slave address. * @@ -149,7 +167,10 @@ class I2CSlave { /** Reset the I2C slave back into the known ready receiving state. */ - void stop(void); + void stop(void) + { + i2c_stop(&_i2c); + } #if !defined(DOXYGEN_ONLY) diff --git a/drivers/InterruptIn.cpp b/drivers/InterruptIn.cpp index a4ab5f0135a4..fef25f999072 100644 --- a/drivers/InterruptIn.cpp +++ b/drivers/InterruptIn.cpp @@ -24,10 +24,7 @@ namespace mbed { // compatibility. // If not for that, we could simplify by having only the 2-param // constructor, with a default value for the PinMode. -InterruptIn::InterruptIn(PinName pin) : gpio(), - gpio_irq(), - _rise(NULL), - _fall(NULL) +InterruptIn::InterruptIn(PinName pin) : gpio(), gpio_irq(), _rise(NULL), _fall(NULL) { // No lock needed in the constructor irq_init(pin); @@ -45,23 +42,6 @@ InterruptIn::InterruptIn(PinName pin, PinMode mode) : gpio_init_in_ex(&gpio, pin, mode); } -void InterruptIn::irq_init(PinName pin) -{ - gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); -} - -InterruptIn::~InterruptIn() -{ - // No lock needed in the destructor - gpio_irq_free(&gpio_irq); -} - -int InterruptIn::read() -{ - // Read only - return gpio_read(&gpio); -} - void InterruptIn::mode(PinMode pull) { core_util_critical_section_enter(); @@ -128,12 +108,6 @@ void InterruptIn::disable_irq() core_util_critical_section_exit(); } -InterruptIn::operator int() -{ - // Underlying call is atomic - return read(); -} - } // namespace mbed #endif diff --git a/drivers/InterruptIn.h b/drivers/InterruptIn.h index 32cc6619eece..991cc0b811b8 100644 --- a/drivers/InterruptIn.h +++ b/drivers/InterruptIn.h @@ -80,7 +80,11 @@ class InterruptIn : private NonCopyable { */ InterruptIn(PinName pin, PinMode mode); - virtual ~InterruptIn(); + virtual ~InterruptIn() + { + // No lock needed in the destructor + gpio_irq_free(&gpio_irq); + } /** Read the input, represented as 0 or 1 (int) * @@ -88,11 +92,19 @@ class InterruptIn : private NonCopyable { * An integer representing the state of the input pin, * 0 for logical 0, 1 for logical 1 */ - int read(); + int read() + { + // Read only + return gpio_read(&gpio); + } /** An operator shorthand for read() */ - operator int(); + operator int() + { + // Underlying call is atomic + return read(); + } /** Attach a function to call when a rising edge occurs on the input @@ -171,7 +183,10 @@ class InterruptIn : private NonCopyable { Callback _rise; Callback _fall; - void irq_init(PinName pin); + void irq_init(PinName pin) + { + gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); + } #endif }; diff --git a/drivers/InterruptManager.cpp b/drivers/InterruptManager.cpp index 179731b4199c..465b8c13c146 100644 --- a/drivers/InterruptManager.cpp +++ b/drivers/InterruptManager.cpp @@ -57,12 +57,6 @@ InterruptManager *InterruptManager::get() return _instance; } -InterruptManager::InterruptManager() -{ - // No mutex needed in constructor - memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain *)); -} - void InterruptManager::destroy() { // Not a good idea to call this unless NO interrupt at all @@ -127,32 +121,6 @@ bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq) return ret; } -void InterruptManager::irq_helper() -{ - _chains[__get_IPSR()]->call(); -} - -int InterruptManager::get_irq_index(IRQn_Type irq) -{ - // Pure function - no lock needed - return (int)irq + NVIC_USER_IRQ_OFFSET; -} - -void InterruptManager::static_irq_helper() -{ - InterruptManager::get()->irq_helper(); -} - -void InterruptManager::lock() -{ - _mutex.lock(); -} - -void InterruptManager::unlock() -{ - _mutex.unlock(); -} - } // namespace mbed #endif diff --git a/drivers/InterruptManager.h b/drivers/InterruptManager.h index 8b207f0c5ead..85463aad9d23 100644 --- a/drivers/InterruptManager.h +++ b/drivers/InterruptManager.h @@ -169,11 +169,23 @@ class InterruptManager : private NonCopyable { #if !defined(DOXYGEN_ONLY) private: - InterruptManager(); + InterruptManager() + { + // No mutex needed in constructor + memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain *)); + } + ~InterruptManager(); - void lock(); - void unlock(); + void lock() + { + _mutex.lock(); + } + + void unlock() + { + _mutex.unlock(); + } template pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front = false) @@ -192,10 +204,21 @@ class InterruptManager : private NonCopyable { pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front = false); bool must_replace_vector(IRQn_Type irq); - int get_irq_index(IRQn_Type irq); - void irq_helper(); + int get_irq_index(IRQn_Type irq) + { + // Pure function - no lock needed + return (int)irq + NVIC_USER_IRQ_OFFSET; + } + + void irq_helper() + { + _chains[__get_IPSR()]->call(); + } void add_helper(void (*function)(void), IRQn_Type irq, bool front = false); - static void static_irq_helper(); + static void static_irq_helper() + { + InterruptManager::get()->irq_helper(); + } CallChain *_chains[NVIC_NUM_VECTORS]; static InterruptManager *_instance; diff --git a/drivers/MbedCRC.cpp b/drivers/MbedCRC.cpp index 6353e052c3fa..4115da01ccf1 100644 --- a/drivers/MbedCRC.cpp +++ b/drivers/MbedCRC.cpp @@ -16,7 +16,6 @@ */ #include -#include "drivers/TableCRC.h" #include "drivers/MbedCRC.h" namespace mbed { diff --git a/drivers/PortIn.cpp b/drivers/PortIn.cpp new file mode 100644 index 000000000000..fc8577b84863 --- /dev/null +++ b/drivers/PortIn.cpp @@ -0,0 +1,43 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-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. + */ + +#include "drivers/PortIn.h" + + +#if DEVICE_PORTIN + +#include "platform/mbed_critical.h" + +namespace mbed { + +PortIn::PortIn(PortName port, int mask) +{ + core_util_critical_section_enter(); + port_init(&_port, port, mask, PIN_INPUT); + core_util_critical_section_exit(); +} + +void PortIn::mode(PinMode mode) +{ + core_util_critical_section_enter(); + port_mode(&_port, mode); + core_util_critical_section_exit(); +} + +} // namespace mbed + +#endif // #if DEVICE_PORTIN diff --git a/drivers/PortIn.h b/drivers/PortIn.h index f3ae250e98d4..d3fb7192429f 100644 --- a/drivers/PortIn.h +++ b/drivers/PortIn.h @@ -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"); @@ -22,7 +22,6 @@ #if DEVICE_PORTIN || defined(DOXYGEN_ONLY) #include "hal/port_api.h" -#include "platform/mbed_critical.h" namespace mbed { /** \addtogroup drivers */ @@ -61,12 +60,7 @@ class PortIn { * @param port Port to connect to (as defined in target's PortNames.h) * @param mask Bitmask defines which port pins should be an input (0 - ignore, 1 - include) */ - PortIn(PortName port, int mask = 0xFFFFFFFF) - { - core_util_critical_section_enter(); - port_init(&_port, port, mask, PIN_INPUT); - core_util_critical_section_exit(); - } + PortIn(PortName port, int mask = 0xFFFFFFFF); /** Read the value input to the port * @@ -82,12 +76,7 @@ class PortIn { * * @param mode PullUp, PullDown, PullNone, OpenDrain */ - void mode(PinMode mode) - { - core_util_critical_section_enter(); - port_mode(&_port, mode); - core_util_critical_section_exit(); - } + void mode(PinMode mode); /** A shorthand for read() */ diff --git a/drivers/PortInOut.cpp b/drivers/PortInOut.cpp new file mode 100644 index 000000000000..3b9671ad9654 --- /dev/null +++ b/drivers/PortInOut.cpp @@ -0,0 +1,72 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-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. + */ + +#include "drivers/PortInOut.h" + + +#if DEVICE_PORTINOUT + +#include "platform/mbed_critical.h" + +namespace mbed { + +PortInOut::PortInOut(PortName port, int mask) +{ + core_util_critical_section_enter(); + port_init(&_port, port, mask, PIN_INPUT); + core_util_critical_section_exit(); +} + +void PortInOut::output() +{ + core_util_critical_section_enter(); + port_dir(&_port, PIN_OUTPUT); + core_util_critical_section_exit(); +} + +void PortInOut::input() +{ + core_util_critical_section_enter(); + port_dir(&_port, PIN_INPUT); + core_util_critical_section_exit(); +} + +void PortInOut::mode(PinMode mode) +{ + core_util_critical_section_enter(); + port_mode(&_port, mode); + core_util_critical_section_exit(); +} + +PortInOut &PortInOut::operator= (int value) +{ + write(value); + return *this; +} + +/** A shorthand for write() + * \sa PortInOut::write() + */ +PortInOut &PortInOut::operator= (PortInOut &rhs) +{ + write(rhs.read()); + return *this; +} + +} // namespace mbed + +#endif // #if DEVICE_PORTINOUT diff --git a/drivers/PortInOut.h b/drivers/PortInOut.h index 2036eba25202..e725fa003f84 100644 --- a/drivers/PortInOut.h +++ b/drivers/PortInOut.h @@ -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"); @@ -22,7 +22,6 @@ #if DEVICE_PORTINOUT || defined(DOXYGEN_ONLY) #include "hal/port_api.h" -#include "platform/mbed_critical.h" namespace mbed { /** \addtogroup drivers */ @@ -40,12 +39,7 @@ class PortInOut { * @param port Port to connect to (Port0-Port5) * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) */ - PortInOut(PortName port, int mask = 0xFFFFFFFF) - { - core_util_critical_section_enter(); - port_init(&_port, port, mask, PIN_INPUT); - core_util_critical_section_exit(); - } + PortInOut(PortName port, int mask = 0xFFFFFFFF); /** Write the value to the output port * @@ -68,50 +62,27 @@ class PortInOut { /** Set as an output */ - void output() - { - core_util_critical_section_enter(); - port_dir(&_port, PIN_OUTPUT); - core_util_critical_section_exit(); - } + void output(); /** Set as an input */ - void input() - { - core_util_critical_section_enter(); - port_dir(&_port, PIN_INPUT); - core_util_critical_section_exit(); - } + void input(); /** Set the input pin mode * * @param mode PullUp, PullDown, PullNone, OpenDrain */ - void mode(PinMode mode) - { - core_util_critical_section_enter(); - port_mode(&_port, mode); - core_util_critical_section_exit(); - } + void mode(PinMode mode); /** A shorthand for write() * \sa PortInOut::write() */ - PortInOut &operator= (int value) - { - write(value); - return *this; - } + PortInOut &operator= (int value); /** A shorthand for write() * \sa PortInOut::write() */ - PortInOut &operator= (PortInOut &rhs) - { - write(rhs.read()); - return *this; - } + PortInOut &operator= (PortInOut &rhs); /** A shorthand for read() * \sa PortInOut::read() diff --git a/drivers/PortOut.cpp b/drivers/PortOut.cpp new file mode 100644 index 000000000000..62fd3a26482f --- /dev/null +++ b/drivers/PortOut.cpp @@ -0,0 +1,51 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-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. + */ + +#include "drivers/PortOut.h" + + +#if DEVICE_PORTOUT + +#include "platform/mbed_critical.h" + +namespace mbed { + +PortOut::PortOut(PortName port, int mask) +{ + core_util_critical_section_enter(); + port_init(&_port, port, mask, PIN_OUTPUT); + core_util_critical_section_exit(); +} + +PortOut &PortOut::operator= (int value) +{ + write(value); + return *this; +} + +/** A shorthand for write() + * \sa PortOut::write() + */ +PortOut &PortOut::operator= (PortOut &rhs) +{ + write(rhs.read()); + return *this; +} + +} // namespace mbed + +#endif // #if DEVICE_PORTOUT diff --git a/drivers/PortOut.h b/drivers/PortOut.h index 18a0bcd3c5df..609c229e0de8 100644 --- a/drivers/PortOut.h +++ b/drivers/PortOut.h @@ -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"); @@ -22,7 +22,6 @@ #if DEVICE_PORTOUT || defined(DOXYGEN_ONLY) #include "hal/port_api.h" -#include "platform/mbed_critical.h" namespace mbed { /** \addtogroup drivers */ @@ -60,12 +59,7 @@ class PortOut { * @param port Port to connect to (as defined in target's PortNames.h) * @param mask Bitmask defines which port pins are an output (0 - ignore, 1 - include) */ - PortOut(PortName port, int mask = 0xFFFFFFFF) - { - core_util_critical_section_enter(); - port_init(&_port, port, mask, PIN_OUTPUT); - core_util_critical_section_exit(); - } + PortOut(PortName port, int mask = 0xFFFFFFFF); /** Write the value to the output port * @@ -89,20 +83,12 @@ class PortOut { /** A shorthand for write() * \sa PortOut::write() */ - PortOut &operator= (int value) - { - write(value); - return *this; - } + PortOut &operator= (int value); /** A shorthand for read() * \sa PortOut::read() */ - PortOut &operator= (PortOut &rhs) - { - write(rhs.read()); - return *this; - } + PortOut &operator= (PortOut &rhs); /** A shorthand for read() * \sa PortOut::read() diff --git a/drivers/PwmOut.cpp b/drivers/PwmOut.cpp new file mode 100644 index 000000000000..12711658828d --- /dev/null +++ b/drivers/PwmOut.cpp @@ -0,0 +1,133 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-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. + */ + +#include "drivers/PwmOut.h" + + +#if DEVICE_PWMOUT + +#include "platform/mbed_critical.h" +#include "platform/mbed_power_mgmt.h" + +namespace mbed { + +PwmOut::PwmOut(PinName pin) : _deep_sleep_locked(false) +{ + core_util_critical_section_enter(); + pwmout_init(&_pwm, pin); + core_util_critical_section_exit(); +} + +PwmOut::~PwmOut() +{ + core_util_critical_section_enter(); + pwmout_free(&_pwm); + unlock_deep_sleep(); + core_util_critical_section_exit(); +} + +void PwmOut::write(float value) +{ + core_util_critical_section_enter(); + lock_deep_sleep(); + pwmout_write(&_pwm, value); + core_util_critical_section_exit(); +} + +float PwmOut::read() +{ + core_util_critical_section_enter(); + float val = pwmout_read(&_pwm); + core_util_critical_section_exit(); + return val; +} + +void PwmOut::period(float seconds) +{ + core_util_critical_section_enter(); + pwmout_period(&_pwm, seconds); + core_util_critical_section_exit(); +} + +void PwmOut::period_ms(int ms) +{ + core_util_critical_section_enter(); + pwmout_period_ms(&_pwm, ms); + core_util_critical_section_exit(); +} + +void PwmOut::period_us(int us) +{ + core_util_critical_section_enter(); + pwmout_period_us(&_pwm, us); + core_util_critical_section_exit(); +} + +void PwmOut::pulsewidth(float seconds) +{ + core_util_critical_section_enter(); + pwmout_pulsewidth(&_pwm, seconds); + core_util_critical_section_exit(); +} + +void PwmOut::pulsewidth_ms(int ms) +{ + core_util_critical_section_enter(); + pwmout_pulsewidth_ms(&_pwm, ms); + core_util_critical_section_exit(); +} + +void PwmOut::pulsewidth_us(int us) +{ + core_util_critical_section_enter(); + pwmout_pulsewidth_us(&_pwm, us); + core_util_critical_section_exit(); +} + +PwmOut &PwmOut::operator= (float value) +{ + // Underlying call is thread safe + write(value); + return *this; +} + +PwmOut &PwmOut::operator= (PwmOut &rhs) +{ + // Underlying call is thread safe + write(rhs.read()); + return *this; +} + +void PwmOut::lock_deep_sleep() +{ + if (_deep_sleep_locked == false) { + sleep_manager_lock_deep_sleep(); + _deep_sleep_locked = true; + } +} + +void PwmOut::unlock_deep_sleep() +{ + if (_deep_sleep_locked == true) { + sleep_manager_unlock_deep_sleep(); + _deep_sleep_locked = false; + } +} + +} // namespace mbed + +#endif // #if DEVICE_PWMOUT diff --git a/drivers/PwmOut.h b/drivers/PwmOut.h index ed8765594a99..7803eda61f98 100644 --- a/drivers/PwmOut.h +++ b/drivers/PwmOut.h @@ -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"); @@ -21,8 +21,6 @@ #if DEVICE_PWMOUT || defined(DOXYGEN_ONLY) #include "hal/pwmout_api.h" -#include "platform/mbed_critical.h" -#include "platform/mbed_power_mgmt.h" namespace mbed { /** \addtogroup drivers */ @@ -58,20 +56,9 @@ class PwmOut { * * @param pin PwmOut pin to connect to */ - PwmOut(PinName pin) : _deep_sleep_locked(false) - { - core_util_critical_section_enter(); - pwmout_init(&_pwm, pin); - core_util_critical_section_exit(); - } + PwmOut(PinName pin); - ~PwmOut() - { - core_util_critical_section_enter(); - pwmout_free(&_pwm); - unlock_deep_sleep(); - core_util_critical_section_exit(); - } + ~PwmOut(); /** Set the output duty-cycle, specified as a percentage (float) * @@ -80,13 +67,7 @@ class PwmOut { * 0.0f (representing on 0%) and 1.0f (representing on 100%). * Values outside this range will be saturated to 0.0f or 1.0f. */ - void write(float value) - { - core_util_critical_section_enter(); - lock_deep_sleep(); - pwmout_write(&_pwm, value); - core_util_critical_section_exit(); - } + void write(float value); /** Return the current output duty-cycle setting, measured as a percentage (float) * @@ -98,13 +79,7 @@ class PwmOut { * @note * This value may not match exactly the value set by a previous write(). */ - float read() - { - core_util_critical_section_enter(); - float val = pwmout_read(&_pwm); - core_util_critical_section_exit(); - return val; - } + float read(); /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same. * @@ -113,82 +88,42 @@ class PwmOut { * The resolution is currently in microseconds; periods smaller than this * will be set to zero. */ - void period(float seconds) - { - core_util_critical_section_enter(); - pwmout_period(&_pwm, seconds); - core_util_critical_section_exit(); - } + void period(float seconds); /** Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same. * @param ms Change the period of a PWM signal in milliseconds without modifying the duty cycle */ - void period_ms(int ms) - { - core_util_critical_section_enter(); - pwmout_period_ms(&_pwm, ms); - core_util_critical_section_exit(); - } + void period_ms(int ms); /** Set the PWM period, specified in microseconds (int), keeping the duty cycle the same. * @param us Change the period of a PWM signal in microseconds without modifying the duty cycle */ - void period_us(int us) - { - core_util_critical_section_enter(); - pwmout_period_us(&_pwm, us); - core_util_critical_section_exit(); - } + void period_us(int us); /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. * @param seconds Change the pulse width of a PWM signal specified in seconds (float) */ - void pulsewidth(float seconds) - { - core_util_critical_section_enter(); - pwmout_pulsewidth(&_pwm, seconds); - core_util_critical_section_exit(); - } + void pulsewidth(float seconds); /** Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same. * @param ms Change the pulse width of a PWM signal specified in milliseconds */ - void pulsewidth_ms(int ms) - { - core_util_critical_section_enter(); - pwmout_pulsewidth_ms(&_pwm, ms); - core_util_critical_section_exit(); - } + void pulsewidth_ms(int ms); /** Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same. * @param us Change the pulse width of a PWM signal specified in microseconds */ - void pulsewidth_us(int us) - { - core_util_critical_section_enter(); - pwmout_pulsewidth_us(&_pwm, us); - core_util_critical_section_exit(); - } + void pulsewidth_us(int us); /** A operator shorthand for write() * \sa PwmOut::write() */ - PwmOut &operator= (float value) - { - // Underlying call is thread safe - write(value); - return *this; - } + PwmOut &operator= (float value); /** A operator shorthand for write() * \sa PwmOut::write() */ - PwmOut &operator= (PwmOut &rhs) - { - // Underlying call is thread safe - write(rhs.read()); - return *this; - } + PwmOut &operator= (PwmOut &rhs); /** An operator shorthand for read() * \sa PwmOut::read() @@ -202,22 +137,10 @@ class PwmOut { #if !(DOXYGEN_ONLY) protected: /** Lock deep sleep only if it is not yet locked */ - void lock_deep_sleep() - { - if (_deep_sleep_locked == false) { - sleep_manager_lock_deep_sleep(); - _deep_sleep_locked = true; - } - } + void lock_deep_sleep(); /** Unlock deep sleep in case it is locked */ - void unlock_deep_sleep() - { - if (_deep_sleep_locked == true) { - sleep_manager_unlock_deep_sleep(); - _deep_sleep_locked = false; - } - } + void unlock_deep_sleep(); pwmout_t _pwm; bool _deep_sleep_locked; diff --git a/drivers/QSPI.cpp b/drivers/QSPI.cpp index c79c949e3b3c..49246fdedd5f 100644 --- a/drivers/QSPI.cpp +++ b/drivers/QSPI.cpp @@ -206,16 +206,6 @@ qspi_status_t QSPI::command_transfer(int instruction, int address, const char *t return ret_status; } -void QSPI::lock() -{ - _mutex->lock(); -} - -void QSPI::unlock() -{ - _mutex->unlock(); -} - // Note: Private helper function to initialize qspi HAL bool QSPI::_initialize() { diff --git a/drivers/QSPI.h b/drivers/QSPI.h index e247e8f65155..1c6b7e743d6a 100644 --- a/drivers/QSPI.h +++ b/drivers/QSPI.h @@ -187,11 +187,17 @@ class QSPI : private NonCopyable { protected: /** Acquire exclusive access to this SPI bus */ - virtual void lock(void); + virtual void lock(void) + { + _mutex->lock(); + } /** Release exclusive access to this SPI bus */ - virtual void unlock(void); + virtual void unlock(void) + { + _mutex->unlock(); + } qspi_t _qspi; diff --git a/drivers/RawSerial.cpp b/drivers/RawSerial.cpp index 1424c6c9afb9..8c289999e789 100644 --- a/drivers/RawSerial.cpp +++ b/drivers/RawSerial.cpp @@ -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"); @@ -15,9 +15,7 @@ * limitations under the License. */ #include "drivers/RawSerial.h" -#include "platform/mbed_wait_api.h" #include -#include #if DEVICE_SERIAL @@ -26,11 +24,6 @@ namespace mbed { -RawSerial::RawSerial(PinName tx, PinName rx, int baud) : SerialBase(tx, rx, baud) -{ - // No lock needed in the constructor -} - int RawSerial::getc() { lock(); @@ -91,20 +84,6 @@ int RawSerial::vprintf(const char *format, std::va_list arg) return len; } -/** Acquire exclusive access to this serial port - */ -void RawSerial::lock() -{ - // No lock used - external synchronization required -} - -/** Release exclusive access to this serial port - */ -void RawSerial::unlock() -{ - // No lock used - external synchronization required -} - } // namespace mbed #endif diff --git a/drivers/RawSerial.h b/drivers/RawSerial.h index 0e6c9c7c8ffa..215c271388c7 100644 --- a/drivers/RawSerial.h +++ b/drivers/RawSerial.h @@ -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"); @@ -23,7 +23,6 @@ #include "mbed_toolchain.h" #include "drivers/SerialBase.h" -#include "hal/serial_api.h" #include "platform/NonCopyable.h" #include @@ -65,7 +64,10 @@ class RawSerial: public SerialBase, private NonCopyable { * @note * Either tx or rx may be specified as NC if unused */ - RawSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); + RawSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) : SerialBase(tx, rx, baud) + { + // No lock needed in the constructor + } /** Write a char to the serial port * @@ -97,11 +99,17 @@ class RawSerial: public SerialBase, private NonCopyable { /* Acquire exclusive access to this serial port */ - virtual void lock(void); + virtual void lock(void) + { + // No lock used - external synchronization required + } /* Release exclusive access to this serial port */ - virtual void unlock(void); + virtual void unlock(void) + { + // No lock used - external synchronization required + } #endif }; diff --git a/drivers/SPI.cpp b/drivers/SPI.cpp index ffe696bd6db7..66c3b3ef41da 100644 --- a/drivers/SPI.cpp +++ b/drivers/SPI.cpp @@ -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"); @@ -28,32 +28,6 @@ namespace mbed { SPI::spi_peripheral_s SPI::_peripherals[SPI_PERIPHERALS_USED]; int SPI::_peripherals_used; -SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) : -#if DEVICE_SPI_ASYNCH - _irq(this), -#endif - _mosi(mosi), - _miso(miso), - _sclk(sclk), - _hw_ssel(ssel), - _sw_ssel(NC) -{ - _do_construct(); -} - -SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel, use_gpio_ssel_t) : -#if DEVICE_SPI_ASYNCH - _irq(this), -#endif - _mosi(mosi), - _miso(miso), - _sclk(sclk), - _hw_ssel(NC), - _sw_ssel(ssel, 1) -{ - _do_construct(); -} - void SPI::_do_construct() { // No lock needed in the constructor @@ -188,11 +162,6 @@ void SPI::_set_ssel(int val) } } -void SPI::lock() -{ - _peripheral->mutex->lock(); -} - void SPI::select() { lock(); @@ -202,11 +171,6 @@ void SPI::select() } } -void SPI::unlock() -{ - _peripheral->mutex->unlock(); -} - void SPI::deselect() { if (--_select_count == 0) { @@ -243,14 +207,6 @@ void SPI::abort_transfer() #endif } - -void SPI::clear_transfer_buffer() -{ -#if TRANSACTION_QUEUE_SIZE_SPI - _peripheral->transaction_buffer->reset(); -#endif -} - void SPI::abort_all_transfers() { clear_transfer_buffer(); @@ -323,11 +279,6 @@ void SPI::unlock_deep_sleep() #if TRANSACTION_QUEUE_SIZE_SPI -void SPI::start_transaction(transaction_t *data) -{ - start_transfer(data->tx_buffer, data->tx_length, data->rx_buffer, data->rx_length, data->width, data->callback, data->event); -} - void SPI::dequeue_transaction() { Transaction t; diff --git a/drivers/SPI.h b/drivers/SPI.h index 6b647a3adcac..ebb95f460988 100644 --- a/drivers/SPI.h +++ b/drivers/SPI.h @@ -1,5 +1,5 @@ /* mbed Microcontroller Library - * Copyright (c) 2006-2015 ARM Limited + * Copyright (c) 2006-2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -40,7 +40,7 @@ #include "platform/CThunk.h" #include "hal/dma_api.h" #include "platform/CircularBuffer.h" -#include "platform/FunctionPointer.h" +#include "platform/Callback.h" #include "platform/Transaction.h" #endif @@ -110,7 +110,18 @@ class SPI : private NonCopyable { * @param sclk SPI Clock pin. * @param ssel SPI Chip Select pin. */ - SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel = NC); + SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel = NC) : +#if DEVICE_SPI_ASYNCH + _irq(this), +#endif + _mosi(mosi), + _miso(miso), + _sclk(sclk), + _hw_ssel(ssel), + _sw_ssel(NC) + { + _do_construct(); + } /** Create a SPI master connected to the specified pins. * @@ -126,7 +137,18 @@ class SPI : private NonCopyable { * @param sclk SPI Clock pin. * @param ssel SPI Chip Select pin. */ - SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel, use_gpio_ssel_t); + SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel, use_gpio_ssel_t) : +#if DEVICE_SPI_ASYNCH + _irq(this), +#endif + _mosi(mosi), + _miso(miso), + _sclk(sclk), + _hw_ssel(NC), + _sw_ssel(ssel, 1) + { + _do_construct(); + } virtual ~SPI(); @@ -178,11 +200,17 @@ class SPI : private NonCopyable { /** Acquire exclusive access to this SPI bus. */ - virtual void lock(void); + virtual void lock(void) + { + _peripheral->mutex->lock(); + } /** Release exclusive access to this SPI bus. */ - virtual void unlock(void); + virtual void unlock(void) + { + _peripheral->mutex->unlock(); + } /** Assert the Slave Select line, acquiring exclusive access to this SPI bus. * @@ -239,7 +267,12 @@ class SPI : private NonCopyable { /** Clear the queue of transfers. */ - void clear_transfer_buffer(); + void clear_transfer_buffer() + { + #if TRANSACTION_QUEUE_SIZE_SPI + _peripheral->transaction_buffer->reset(); + #endif + } /** Clear the queue of transfers and abort the on-going transfer. */ @@ -325,7 +358,18 @@ class SPI : private NonCopyable { * * @param data Transaction data. */ - void start_transaction(transaction_t *data); + void start_transaction(transaction_t *data) + { + start_transfer( + data->tx_buffer, + data->tx_length, + data->rx_buffer, + data->rx_length, + data->width, + data->callback, + data->event + ); + } /** Dequeue a transaction and start the transfer if there was one pending. */ diff --git a/drivers/SPISlave.cpp b/drivers/SPISlave.cpp index db870cdea2aa..02127e6c899c 100644 --- a/drivers/SPISlave.cpp +++ b/drivers/SPISlave.cpp @@ -44,21 +44,6 @@ void SPISlave::frequency(int hz) spi_frequency(&_spi, _hz); } -int SPISlave::receive(void) -{ - return (spi_slave_receive(&_spi)); -} - -int SPISlave::read(void) -{ - return (spi_slave_read(&_spi)); -} - -void SPISlave::reply(int value) -{ - spi_slave_write(&_spi, value); -} - } // namespace mbed #endif diff --git a/drivers/SPISlave.h b/drivers/SPISlave.h index 878bc8d67215..72ebf0dcdd9e 100644 --- a/drivers/SPISlave.h +++ b/drivers/SPISlave.h @@ -96,20 +96,29 @@ class SPISlave : private NonCopyable { * @retval 0 No data waiting. * @retval 1 Data waiting. */ - int receive(void); + int receive(void) + { + return (spi_slave_receive(&_spi)); + } /** Retrieve data from receive buffer as slave. * * @return The data in the receive buffer. */ - int read(void); + int read(void) + { + return (spi_slave_read(&_spi)); + } /** Fill the transmission buffer with the value to be written out * as slave on the next received message from the master. * * @param value The data to be transmitted next. */ - void reply(int value); + void reply(int value) + { + spi_slave_write(&_spi, value); + } #if !defined(DOXYGEN_ONLY) diff --git a/drivers/Serial.cpp b/drivers/Serial.cpp index d0f87263b673..e3dccd0d955f 100644 --- a/drivers/Serial.cpp +++ b/drivers/Serial.cpp @@ -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"); @@ -15,42 +15,11 @@ * limitations under the License. */ #include "drivers/Serial.h" -#include "platform/mbed_wait_api.h" #if DEVICE_SERIAL namespace mbed { -Serial::Serial(PinName tx, PinName rx, const char *name, int baud) : SerialBase(tx, rx, baud), Stream(name) -{ -} - -Serial::Serial(PinName tx, PinName rx, int baud): SerialBase(tx, rx, baud), Stream(NULL) -{ -} - -int Serial::_getc() -{ - // Mutex is already held - return _base_getc(); -} - -int Serial::_putc(int c) -{ - // Mutex is already held - return _base_putc(c); -} - -void Serial::lock() -{ - _mutex.lock(); -} - -void Serial::unlock() -{ - _mutex.unlock(); -} - } // namespace mbed #endif diff --git a/drivers/Serial.h b/drivers/Serial.h index d19010808d57..b6cf4d4b17ed 100644 --- a/drivers/Serial.h +++ b/drivers/Serial.h @@ -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"); @@ -24,7 +24,6 @@ #include "platform/Stream.h" #include "SerialBase.h" #include "platform/PlatformMutex.h" -#include "hal/serial_api.h" #include "platform/NonCopyable.h" namespace mbed { @@ -69,7 +68,9 @@ class Serial : public SerialBase, public Stream, private NonCopyable { * @note * Either tx or rx may be specified as NC (Not Connected) if unused */ - Serial(PinName tx, PinName rx, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); + Serial(PinName tx, PinName rx, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) : SerialBase(tx, rx, baud), Stream(name) + { + } /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud @@ -81,7 +82,9 @@ class Serial : public SerialBase, public Stream, private NonCopyable { * @note * Either tx or rx may be specified as NC (Not Connected) if unused */ - Serial(PinName tx, PinName rx, int baud); + Serial(PinName tx, PinName rx,int baud) : SerialBase(tx, rx, baud), Stream(NULL) + { + } /* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through * the calls from the SerialBase instead for backwards compatibility. This problem is @@ -102,10 +105,25 @@ class Serial : public SerialBase, public Stream, private NonCopyable { #if !(DOXYGEN_ONLY) protected: - virtual int _getc(); - virtual int _putc(int c); - virtual void lock(); - virtual void unlock(); + virtual int _getc() + { + // Mutex is already held + return _base_getc(); + } + virtual int _putc(int c) + { + // Mutex is already held + return _base_putc(c); + } + virtual void lock() + { + _mutex.lock(); + } + virtual void unlock() + { + _mutex.unlock(); + } + PlatformMutex _mutex; #endif diff --git a/drivers/SerialBase.cpp b/drivers/SerialBase.cpp index 856944fd3470..09372d71dc03 100644 --- a/drivers/SerialBase.cpp +++ b/drivers/SerialBase.cpp @@ -66,7 +66,6 @@ int SerialBase::readable() return ret; } - int SerialBase::writeable() { lock(); @@ -107,12 +106,6 @@ void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) } } -int SerialBase::_base_getc() -{ - // Mutex is already held - return serial_getc(&_serial); -} - int SerialBase::_base_putc(int c) { // Mutex is already held @@ -151,16 +144,6 @@ void SerialBase::send_break() unlock(); } -void SerialBase::lock() -{ - // Stub -} - -void SerialBase:: unlock() -{ - // Stub -} - SerialBase::~SerialBase() { // No lock needed in destructor @@ -294,7 +277,6 @@ int SerialBase::read(uint8_t *buffer, int length, const event_callback_t &callba return result; } - int SerialBase::read(uint16_t *buffer, int length, const event_callback_t &callback, int event, unsigned char char_match) { int result = 0; @@ -308,7 +290,6 @@ int SerialBase::read(uint16_t *buffer, int length, const event_callback_t &callb return result; } - void SerialBase::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match) { _rx_asynch_set = true; diff --git a/drivers/SerialBase.h b/drivers/SerialBase.h index 99da7a9b45d1..5d081d680eaf 100644 --- a/drivers/SerialBase.h +++ b/drivers/SerialBase.h @@ -157,11 +157,17 @@ class SerialBase : private NonCopyable { /** Acquire exclusive access to this serial port */ - virtual void lock(void); + virtual void lock(void) + { + // Stub + } /** Release exclusive access to this serial port */ - virtual void unlock(void); + virtual void unlock(void) + { + // Stub + } #endif public: @@ -288,7 +294,12 @@ class SerialBase : private NonCopyable { SerialBase(PinName tx, PinName rx, int baud); virtual ~SerialBase(); - int _base_getc(); + int _base_getc() + { + // Mutex is already held + return serial_getc(&_serial); + } + int _base_putc(int c); #if DEVICE_SERIAL_ASYNCH diff --git a/drivers/SerialWireOutput.cpp b/drivers/SerialWireOutput.cpp new file mode 100644 index 000000000000..92d6bd3c0167 --- /dev/null +++ b/drivers/SerialWireOutput.cpp @@ -0,0 +1,41 @@ +/* mbed Microcontroller Library + * Copyright (c) 2017-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. + */ + +#include "drivers/SerialWireOutput.h" + +#if defined(DEVICE_ITM) + +#include "hal/itm_api.h" + +namespace mbed { + +SerialWireOutput::SerialWireOutput(void) +{ + /* Initialize ITM using internal init function. */ + mbed_itm_init(); +} + +ssize_t SerialWireOutput::write(const void *buffer, size_t size) +{ + mbed_itm_send_block(ITM_PORT_SWO, buffer, size); + + return size; +} + +} // namespace mbed + +#endif // DEVICE_ITM diff --git a/drivers/SerialWireOutput.h b/drivers/SerialWireOutput.h index 0379687c9baa..b9dfae79dfd7 100644 --- a/drivers/SerialWireOutput.h +++ b/drivers/SerialWireOutput.h @@ -1,5 +1,5 @@ /* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited + * Copyright (c) 2017-2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,9 +18,10 @@ #ifndef MBED_SERIALWIREOUTPUT_H #define MBED_SERIALWIREOUTPUT_H +#include "platform/platform.h" + #if defined(DEVICE_ITM) -#include "hal/itm_api.h" #include "platform/FileHandle.h" namespace mbed { @@ -29,18 +30,9 @@ class SerialWireOutput : public FileHandle { public: - SerialWireOutput(void) - { - /* Initialize ITM using internal init function. */ - mbed_itm_init(); - } - - virtual ssize_t write(const void *buffer, size_t size) - { - mbed_itm_send_block(ITM_PORT_SWO, buffer, size); + SerialWireOutput(void); - return size; - } + virtual ssize_t write(const void *buffer, size_t size); virtual ssize_t read(void *buffer, size_t size) { diff --git a/drivers/Ticker.cpp b/drivers/Ticker.cpp index b9540ec0fdd4..fada56cf646d 100644 --- a/drivers/Ticker.cpp +++ b/drivers/Ticker.cpp @@ -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"); @@ -20,9 +20,19 @@ #include "platform/FunctionPointer.h" #include "hal/ticker_api.h" #include "platform/mbed_critical.h" +#include "platform/mbed_power_mgmt.h" namespace mbed { +Ticker::Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) +{ +} + +// When low power ticker is in use, then do not disable deep sleep. +Ticker::Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(!data->interface->runs_in_deep_sleep) +{ +} + void Ticker::detach() { core_util_critical_section_enter(); diff --git a/drivers/Ticker.h b/drivers/Ticker.h index 8a68b4a00f66..87053b9ee881 100644 --- a/drivers/Ticker.h +++ b/drivers/Ticker.h @@ -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"); @@ -21,9 +21,7 @@ #include "platform/Callback.h" #include "platform/mbed_toolchain.h" #include "platform/NonCopyable.h" -#include "platform/mbed_power_mgmt.h" #include "hal/lp_ticker_api.h" -#include "platform/mbed_critical.h" namespace mbed { /** \addtogroup drivers */ @@ -67,14 +65,10 @@ namespace mbed { class Ticker : public TimerEvent, private NonCopyable { public: - Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) - { - } + Ticker(); // When low power ticker is in use, then do not disable deep sleep. - Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(!data->interface->runs_in_deep_sleep) - { - } + Ticker(const ticker_data_t *data); /** Attach a function to be called by the Ticker, specifying the interval in seconds * diff --git a/drivers/Timeout.h b/drivers/Timeout.h index c155c9bcecff..d86708cc3d70 100644 --- a/drivers/Timeout.h +++ b/drivers/Timeout.h @@ -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"); @@ -19,7 +19,6 @@ #include "drivers/Ticker.h" #include "platform/NonCopyable.h" -#include "platform/mbed_power_mgmt.h" namespace mbed { /** \addtogroup drivers */ diff --git a/drivers/Timer.cpp b/drivers/Timer.cpp index 072581087ae6..18120edd6266 100644 --- a/drivers/Timer.cpp +++ b/drivers/Timer.cpp @@ -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"); @@ -18,6 +18,7 @@ #include "hal/ticker_api.h" #include "hal/us_ticker_api.h" #include "platform/mbed_critical.h" +#include "platform/mbed_power_mgmt.h" namespace mbed { @@ -26,8 +27,7 @@ Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data( reset(); } -Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), - _lock_deepsleep(!data->interface->runs_in_deep_sleep) +Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), _lock_deepsleep(!data->interface->runs_in_deep_sleep) { reset(); } @@ -70,11 +70,6 @@ void Timer::stop() core_util_critical_section_exit(); } -int Timer::read_us() -{ - return read_high_resolution_us(); -} - float Timer::read() { return (float)read_high_resolution_us() / 1000000.0f; @@ -112,9 +107,4 @@ void Timer::reset() core_util_critical_section_exit(); } -Timer::operator float() -{ - return read(); -} - } // namespace mbed diff --git a/drivers/Timer.h b/drivers/Timer.h index 010f6019b5f2..d32b84a20890 100644 --- a/drivers/Timer.h +++ b/drivers/Timer.h @@ -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"); @@ -20,7 +20,6 @@ #include "platform/platform.h" #include "hal/ticker_api.h" #include "platform/NonCopyable.h" -#include "platform/mbed_power_mgmt.h" namespace mbed { /** \addtogroup drivers */ @@ -86,11 +85,17 @@ class Timer : private NonCopyable { * * @returns Time passed in microseconds */ - int read_us(); + int read_us() + { + return read_high_resolution_us(); + } /** An operator shorthand for read() */ - operator float(); + operator float() + { + return read(); + } /** Get in a high resolution type the time passed in microseconds. * Returns a 64 bit integer. diff --git a/drivers/TimerEvent.cpp b/drivers/TimerEvent.cpp index 9c77eb86f58c..a225d1fd1b19 100644 --- a/drivers/TimerEvent.cpp +++ b/drivers/TimerEvent.cpp @@ -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"); @@ -17,7 +17,6 @@ #include "drivers/TimerEvent.h" #include -#include "hal/ticker_api.h" #include "hal/us_ticker_api.h" namespace mbed { @@ -27,36 +26,10 @@ TimerEvent::TimerEvent() : event(), _ticker_data(get_us_ticker_data()) ticker_set_handler(_ticker_data, (&TimerEvent::irq)); } -TimerEvent::TimerEvent(const ticker_data_t *data) : event(), _ticker_data(data) -{ - ticker_set_handler(_ticker_data, (&TimerEvent::irq)); -} - void TimerEvent::irq(uint32_t id) { TimerEvent *timer_event = (TimerEvent *)id; timer_event->handler(); } -TimerEvent::~TimerEvent() -{ - remove(); -} - -// insert in to linked list -void TimerEvent::insert(timestamp_t timestamp) -{ - ticker_insert_event(_ticker_data, &event, timestamp, (uint32_t)this); -} - -void TimerEvent::insert_absolute(us_timestamp_t timestamp) -{ - ticker_insert_event_us(_ticker_data, &event, timestamp, (uint32_t)this); -} - -void TimerEvent::remove() -{ - ticker_remove_event(_ticker_data, &event); -} - } // namespace mbed diff --git a/drivers/TimerEvent.h b/drivers/TimerEvent.h index 88f4c3b750dc..0102e5289117 100644 --- a/drivers/TimerEvent.h +++ b/drivers/TimerEvent.h @@ -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"); @@ -18,7 +18,6 @@ #define MBED_TIMEREVENT_H #include "hal/ticker_api.h" -#include "hal/us_ticker_api.h" #include "platform/NonCopyable.h" namespace mbed { @@ -32,7 +31,12 @@ namespace mbed { class TimerEvent : private NonCopyable { public: TimerEvent(); - TimerEvent(const ticker_data_t *data); + + TimerEvent(const ticker_data_t *data) + : event(), _ticker_data(data) + { + ticker_set_handler(_ticker_data, (&TimerEvent::irq)); + } /** The handler registered with the underlying timer interrupt * @@ -42,7 +46,10 @@ class TimerEvent : private NonCopyable { /** Destruction removes it... */ - virtual ~TimerEvent(); + virtual ~TimerEvent() + { + remove(); + } #if !defined(DOXYGEN_ONLY) protected: @@ -61,7 +68,10 @@ class TimerEvent : private NonCopyable { * from the past the event is scheduled after ticker's overflow. * For reference @see convert_timestamp */ - void insert(timestamp_t timestamp); + void insert(timestamp_t timestamp) + { + ticker_insert_event(_ticker_data, &event, timestamp, (uint32_t)this); + } /** Set absolute timestamp of the internal event. * @param timestamp event's us timestamp @@ -70,11 +80,17 @@ class TimerEvent : private NonCopyable { * Do not insert more than one timestamp. * The same @a event object is used for every @a insert/insert_absolute call. */ - void insert_absolute(us_timestamp_t timestamp); + void insert_absolute(us_timestamp_t timestamp) + { + ticker_insert_event_us(_ticker_data, &event, timestamp, (uint32_t)this); + } /** Remove timestamp. */ - void remove(); + void remove() + { + ticker_remove_event(_ticker_data, &event); + } ticker_event_t event; diff --git a/drivers/UARTSerial.cpp b/drivers/UARTSerial.cpp index e9213555d767..628bc62da0aa 100644 --- a/drivers/UARTSerial.cpp +++ b/drivers/UARTSerial.cpp @@ -1,5 +1,5 @@ /* mbed Microcontroller Library - * Copyright (c) 2006-2017 ARM Limited + * Copyright (c) 2006-2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,34 +28,12 @@ namespace mbed { -UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) : - SerialBase(tx, rx, baud), - _blocking(true), - _tx_irq_enabled(false), - _rx_irq_enabled(false), - _tx_enabled(true), - _rx_enabled(true), - _dcd_irq(NULL) +UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) : SerialBase(tx, rx, baud), _blocking(true), _tx_irq_enabled(false), _rx_irq_enabled(false), _tx_enabled(true), _rx_enabled(true), _dcd_irq(NULL) { /* Attatch IRQ routines to the serial device. */ enable_rx_irq(); } -UARTSerial::~UARTSerial() -{ - delete _dcd_irq; -} - -void UARTSerial::dcd_irq() -{ - wake(); -} - -void UARTSerial::set_baud(int baud) -{ - SerialBase::baud(baud); -} - void UARTSerial::set_data_carrier_detect(PinName dcd_pin, bool active_high) { delete _dcd_irq; @@ -87,26 +65,6 @@ void UARTSerial::set_flow_control(Flow type, PinName flow1, PinName flow2) } #endif -int UARTSerial::close() -{ - /* Does not let us pass a file descriptor. So how to close ? - * Also, does it make sense to close a device type file descriptor*/ - return 0; -} - -int UARTSerial::isatty() -{ - return 1; - -} - -off_t UARTSerial::seek(off_t offset, int whence) -{ - /*XXX lseek can be done theoratically, but is it sane to mark positions on a dynamically growing/shrinking - * buffer system (from an interrupt context) */ - return -ESPIPE; -} - int UARTSerial::sync() { api_lock(); @@ -279,28 +237,6 @@ short UARTSerial::poll(short events) const return revents; } -void UARTSerial::lock() -{ - // This is the override for SerialBase. - // No lock required as we only use SerialBase from interrupt or from - // inside our own critical section. -} - -void UARTSerial::unlock() -{ - // This is the override for SerialBase. -} - -void UARTSerial::api_lock(void) -{ - _mutex.lock(); -} - -void UARTSerial::api_unlock(void) -{ - _mutex.unlock(); -} - void UARTSerial::rx_irq(void) { bool was_empty = _rxbuf.empty(); diff --git a/drivers/UARTSerial.h b/drivers/UARTSerial.h index 2959ce77e2c1..23cb869ca850 100644 --- a/drivers/UARTSerial.h +++ b/drivers/UARTSerial.h @@ -1,5 +1,5 @@ /* mbed Microcontroller Library - * Copyright (c) 2006-2017 ARM Limited + * Copyright (c) 2006-2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,7 +26,6 @@ #include "SerialBase.h" #include "InterruptIn.h" #include "platform/PlatformMutex.h" -#include "hal/serial_api.h" #include "platform/CircularBuffer.h" #include "platform/NonCopyable.h" @@ -57,7 +56,10 @@ class UARTSerial : private SerialBase, public FileHandle, private NonCopyable