2013-02-18 15:32:11 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2006-2013 ARM Limited
|
2018-11-09 11:22:52 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#ifndef MBED_PORTOUT_H
|
|
|
|
#define MBED_PORTOUT_H
|
|
|
|
|
2016-10-01 07:11:36 +00:00
|
|
|
#include "platform/platform.h"
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2018-12-04 02:44:27 +00:00
|
|
|
#if DEVICE_PORTOUT || defined(DOXYGEN_ONLY)
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2016-10-01 07:11:36 +00:00
|
|
|
#include "hal/port_api.h"
|
2017-01-27 11:10:28 +00:00
|
|
|
#include "platform/mbed_critical.h"
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
namespace mbed {
|
2016-10-04 20:02:44 +00:00
|
|
|
/** \addtogroup drivers */
|
2018-10-12 22:11:06 +00:00
|
|
|
/** A multiple pin digital output
|
2016-06-08 12:52:14 +00:00
|
|
|
*
|
2017-04-04 17:40:09 +00:00
|
|
|
* @note Synchronization level: Interrupt safe
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* @code
|
|
|
|
* // Toggle all four LEDs
|
|
|
|
*
|
|
|
|
* #include "mbed.h"
|
|
|
|
*
|
|
|
|
* // LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED4 = P1.23
|
|
|
|
* #define LED_MASK 0x00B40000
|
|
|
|
*
|
|
|
|
* PortOut ledport(Port1, LED_MASK);
|
|
|
|
*
|
|
|
|
* int main() {
|
|
|
|
* while(1) {
|
|
|
|
* ledport = LED_MASK;
|
|
|
|
* wait(1);
|
|
|
|
* ledport = 0;
|
|
|
|
* wait(1);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* @endcode
|
2017-04-04 19:21:53 +00:00
|
|
|
* @ingroup drivers
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
|
|
|
class PortOut {
|
|
|
|
public:
|
|
|
|
|
2018-10-12 22:11:06 +00:00
|
|
|
/** Create a PortOut, connected to the specified port
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
2018-10-12 22:11:06 +00:00
|
|
|
* @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)
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
PortOut(PortName port, int mask = 0xFFFFFFFF)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
core_util_critical_section_enter();
|
2013-02-18 15:32:11 +00:00
|
|
|
port_init(&_port, port, mask, PIN_OUTPUT);
|
2016-05-31 22:57:41 +00:00
|
|
|
core_util_critical_section_exit();
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Write the value to the output port
|
|
|
|
*
|
|
|
|
* @param value An integer specifying a bit to write for every corresponding PortOut pin
|
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
void write(int value)
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
port_write(&_port, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Read the value currently output on the port
|
|
|
|
*
|
|
|
|
* @returns
|
2018-10-12 22:11:06 +00:00
|
|
|
* An integer with each bit corresponding to associated pin value
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
int read()
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
return port_read(&_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A shorthand for write()
|
2017-06-08 04:30:43 +00:00
|
|
|
* \sa PortOut::write()
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
PortOut &operator= (int value)
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
write(value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-06-08 04:30:43 +00:00
|
|
|
/** A shorthand for read()
|
|
|
|
* \sa PortOut::read()
|
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
PortOut &operator= (PortOut &rhs)
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
write(rhs.read());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A shorthand for read()
|
2017-06-08 04:30:43 +00:00
|
|
|
* \sa PortOut::read()
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
operator int()
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
return read();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
port_t _port;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mbed
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|