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_PORTINOUT_H
|
|
|
|
#define MBED_PORTINOUT_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_PORTINOUT || 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 */
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** A multiple pin digital in/out used to set/read multiple bi-directional pins
|
2016-06-08 12:52:14 +00:00
|
|
|
*
|
2017-04-04 17:40:09 +00:00
|
|
|
* @note Synchronization level: Interrupt safe
|
2017-04-04 19:21:53 +00:00
|
|
|
* @ingroup drivers
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
|
|
|
class PortInOut {
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** Create an PortInOut, connected to the specified port
|
|
|
|
*
|
|
|
|
* @param port Port to connect to (Port0-Port5)
|
|
|
|
* @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
|
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
PortInOut(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_INPUT);
|
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 port 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
|
|
|
|
* An integer with each bit corresponding to associated port pin setting
|
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
int read()
|
|
|
|
{
|
2013-02-18 15:32:11 +00:00
|
|
|
return port_read(&_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set as an output
|
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
void output()
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
core_util_critical_section_enter();
|
2013-02-18 15:32:11 +00:00
|
|
|
port_dir(&_port, PIN_OUTPUT);
|
2016-05-31 22:57:41 +00:00
|
|
|
core_util_critical_section_exit();
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set as an input
|
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
void input()
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
core_util_critical_section_enter();
|
2013-02-18 15:32:11 +00:00
|
|
|
port_dir(&_port, PIN_INPUT);
|
2016-05-31 22:57:41 +00:00
|
|
|
core_util_critical_section_exit();
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the input pin mode
|
|
|
|
*
|
|
|
|
* @param mode PullUp, PullDown, PullNone, OpenDrain
|
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
void mode(PinMode mode)
|
|
|
|
{
|
2016-05-31 22:57:41 +00:00
|
|
|
core_util_critical_section_enter();
|
2013-02-18 15:32:11 +00:00
|
|
|
port_mode(&_port, mode);
|
2016-05-31 22:57:41 +00:00
|
|
|
core_util_critical_section_exit();
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** A shorthand for write()
|
2017-06-08 04:30:43 +00:00
|
|
|
* \sa PortInOut::write()
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
PortInOut &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 write()
|
|
|
|
* \sa PortInOut::write()
|
|
|
|
*/
|
2018-05-24 15:58:14 +00:00
|
|
|
PortInOut &operator= (PortInOut &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 PortInOut::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
|