Skip to content

Commit

Permalink
Drivers: Sanity cleanup for coherence and reducing size (#2)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
hugueskamba committed Jul 3, 2019
1 parent f4b986d commit 6a5d04e
Show file tree
Hide file tree
Showing 55 changed files with 1,016 additions and 825 deletions.
27 changes: 25 additions & 2 deletions drivers/AnalogIn.cpp
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 @@ -23,6 +23,29 @@ namespace mbed {

SingletonPtr<PlatformMutex> 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
26 changes: 4 additions & 22 deletions drivers/AnalogIn.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 Down Expand Up @@ -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()
*
Expand Down Expand Up @@ -120,7 +103,6 @@ class AnalogIn {
{
_mutex->lock();
}

virtual void unlock()
{
_mutex->unlock();
Expand Down
62 changes: 62 additions & 0 deletions drivers/AnalogOut.cpp
Original file line number Diff line number Diff line change
@@ -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
39 changes: 6 additions & 33 deletions drivers/AnalogOut.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 Down Expand Up @@ -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)
*
Expand All @@ -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()
Expand All @@ -147,7 +121,6 @@ class AnalogOut {
{
_mutex.lock();
}

virtual void unlock()
{
_mutex.unlock();
Expand Down
48 changes: 37 additions & 11 deletions drivers/CAN.cpp
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 @@ -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
Expand Down Expand Up @@ -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
49 changes: 14 additions & 35 deletions drivers/CAN.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 Down Expand Up @@ -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.
*
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -313,8 +285,15 @@ class CAN : private NonCopyable<CAN> {

#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<void()> _irq[IrqCnt];
PlatformMutex _mutex;
Expand Down
Loading

0 comments on commit 6a5d04e

Please sign in to comment.