|
ChibiOS/RT
2.5.1 |
|
I/O Ports Abstraction Layer.
This module defines an abstract interface for digital I/O ports. Note that most I/O ports functions are just macros. The macros have default software implementations that can be redefined in a PAL Low Level Driver if the target hardware supports special features like, for example, atomic bit set/reset/masking. Please refer to the ports specific documentation for details.
The PAL Driver has the advantage to make the access to the I/O ports platform independent and still be optimized for the specific architectures.
Note that the PAL Low Level Driver may also offer non standard macro and functions in order to support specific features but, of course, the use of such interfaces would not be portable. Such interfaces shall be marked with the architecture name inside the function names.
HAL_USE_PAL option must be enabled in halconf.h.In implementing a PAL Low Level Driver there are some rules/behaviors that should be respected.
The behavior is not specified but there are implementations better than others, this is the list of possible implementations, preferred options are on top:
The behavior is not specified but there are implementations better than others, this is the list of possible implementations, preferred options are on top:
The behavior is not specified.
The behavior is not specified.
The behavior is not specified.
Data Structures | |
| struct | IOBus |
| I/O bus descriptor. More... | |
| struct | PALConfig |
| Generic I/O ports static initializer. More... | |
Functions | |
| ioportmask_t | palReadBus (IOBus *bus) |
| Read from an I/O bus. | |
| void | palWriteBus (IOBus *bus, ioportmask_t bits) |
| Write to an I/O bus. | |
| void | palSetBusMode (IOBus *bus, iomode_t mode) |
| Programs a bus with the specified mode. | |
| void | _pal_lld_setgroupmode (ioportid_t port, ioportmask_t mask, iomode_t mode) |
| Pads mode setup. | |
Pads mode constants | |
| #define | PAL_MODE_RESET 0 |
| After reset state. | |
| #define | PAL_MODE_UNCONNECTED 1 |
| Safe state for unconnected pads. | |
| #define | PAL_MODE_INPUT 2 |
| Regular input high-Z pad. | |
| #define | PAL_MODE_INPUT_PULLUP 3 |
| Input pad with weak pull up resistor. | |
| #define | PAL_MODE_INPUT_PULLDOWN 4 |
| Input pad with weak pull down resistor. | |
| #define | PAL_MODE_INPUT_ANALOG 5 |
| Analog input mode. | |
| #define | PAL_MODE_OUTPUT_PUSHPULL 6 |
| Push-pull output pad. | |
| #define | PAL_MODE_OUTPUT_OPENDRAIN 7 |
| Open-drain output pad. | |
Logic level constants | |
| #define | PAL_LOW 0 |
| Logical low state. | |
| #define | PAL_HIGH 1 |
| Logical high state. | |
Macro Functions | |
| #define | palInit(config) pal_lld_init(config) |
| PAL subsystem initialization. | |
| #define | palReadPort(port) ((void)(port), 0) |
| Reads the physical I/O port states. | |
| #define | palReadLatch(port) ((void)(port), 0) |
| Reads the output latch. | |
| #define | palWritePort(port, bits) ((void)(port), (void)(bits)) |
| Writes a bits mask on a I/O port. | |
| #define | palSetPort(port, bits) palWritePort(port, palReadLatch(port) | (bits)) |
| Sets a bits mask on a I/O port. | |
| #define | palClearPort(port, bits) palWritePort(port, palReadLatch(port) & ~(bits)) |
| Clears a bits mask on a I/O port. | |
| #define | palTogglePort(port, bits) palWritePort(port, palReadLatch(port) ^ (bits)) |
| Toggles a bits mask on a I/O port. | |
| #define | palReadGroup(port, mask, offset) ((palReadPort(port) >> (offset)) & (mask)) |
| Reads a group of bits. | |
| #define | palWriteGroup(port, mask, offset, bits) |
| Writes a group of bits. | |
| #define | palSetGroupMode(port, mask, offset, mode) |
| Pads group mode setup. | |
| #define | palReadPad(port, pad) ((palReadPort(port) >> (pad)) & 1) |
| Reads an input pad logical state. | |
| #define | palWritePad(port, pad, bit) |
| Writes a logical state on an output pad. | |
| #define | palSetPad(port, pad) palSetPort(port, PAL_PORT_BIT(pad)) |
Sets a pad logical state to PAL_HIGH. | |
| #define | palClearPad(port, pad) palClearPort(port, PAL_PORT_BIT(pad)) |
Clears a pad logical state to PAL_LOW. | |
| #define | palTogglePad(port, pad) palTogglePort(port, PAL_PORT_BIT(pad)) |
| Toggles a pad logical state. | |
| #define | palSetPadMode(port, pad, mode) palSetGroupMode(port, PAL_PORT_BIT(pad), 0, mode) |
| Pad mode setup. | |
Defines | |
| #define | PAL_PORT_BIT(n) ((ioportmask_t)(1 << (n))) |
| Port bit helper macro. | |
| #define | PAL_GROUP_MASK(width) ((ioportmask_t)(1 << (width)) - 1) |
| Bits group mask helper. | |
| #define | _IOBUS_DATA(name, port, width, offset) {port, PAL_GROUP_MASK(width), offset} |
| Data part of a static I/O bus initializer. | |
| #define | IOBUS_DECL(name, port, width, offset) IOBus name = _IOBUS_DATA(name, port, width, offset) |
| Static I/O bus initializer. | |
| #define | PAL_MODE_OUTPUT_PUSHPULL_SLOW 16 |
| STM8S specific alternate push-pull slow output mode. | |
| #define | PAL_MODE_OUTPUT_OPENDRAIN_SLOW 17 |
| STM8S specific alternate open-drain slow output mode. | |
| #define | PAL_IOPORTS_WIDTH 8 |
| Width, in bits, of an I/O port. | |
| #define | PAL_WHOLE_PORT ((ioportmask_t)0xFF) |
| Whole port mask. | |
| #define | IOPORTS ((PALConfig *)0x5000) |
| GPIO ports as a whole. | |
| #define | IOPORT1 GPIOA |
| GPIO port A identifier. | |
| #define | IOPORT2 GPIOB |
| GPIO port B identifier. | |
| #define | IOPORT3 GPIOC |
| GPIO port C identifier. | |
| #define | IOPORT4 GPIOD |
| GPIO port D identifier. | |
| #define | IOPORT5 GPIOE |
| GPIO port E identifier. | |
| #define | IOPORT6 GPIOF |
| GPIO port F identifier. | |
| #define | IOPORT7 GPIOG |
| GPIO port G identifier. | |
| #define | IOPORT8 GPIOH |
| GPIO port H identifier. | |
| #define | IOPORT9 GPIOI |
| GPIO port I identifier. | |
| #define | pal_lld_init(config) (*IOPORTS = *(config)) |
| Low level PAL subsystem initialization. | |
| #define | pal_lld_readport(port) ((port)->IDR) |
| Reads the physical I/O port states. | |
| #define | pal_lld_readlatch(port) ((port)->ODR) |
| Reads the output latch. | |
| #define | pal_lld_writeport(port, bits) ((port)->ODR = (bits)) |
| Writes a bits mask on a I/O port. | |
| #define | pal_lld_setgroupmode(port, mask, offset, mode) _pal_lld_setgroupmode(port, mask << offset, mode) |
| Pads group mode setup. | |
Typedefs | |
| typedef uint8_t | ioportmask_t |
| Digital I/O port sized unsigned type. | |
| typedef uint8_t | iomode_t |
| Digital I/O modes. | |
| typedef GPIO_TypeDef * | ioportid_t |
| Port Identifier. | |
| ioportmask_t palReadBus | ( | IOBus * | bus | ) |
Read from an I/O bus.
chSysLock() and chSysUnlock(). palReadGroup() macro. The use of this function is preferred when you value code size, readability and error checking over speed.| [in] | bus | the I/O bus, pointer to a IOBus structure |
Definition at line 69 of file pal.c.
References IOBus::mask, IOBus::offset, PAL_IOPORTS_WIDTH, palReadGroup, and IOBus::portid.
| void palWriteBus | ( | IOBus * | bus, |
| ioportmask_t | bits | ||
| ) |
Write to an I/O bus.
chSysLock() and chSysUnlock(). | [in] | bus | the I/O bus, pointer to a IOBus structure |
| [in] | bits | the bits to be written on the I/O bus. Values exceeding the bus width are masked so most significant bits are lost. |
Definition at line 94 of file pal.c.
References IOBus::mask, IOBus::offset, PAL_IOPORTS_WIDTH, palWriteGroup, and IOBus::portid.
Programs a bus with the specified mode.
chSysLock() and chSysUnlock(). | [in] | bus | the I/O bus, pointer to a IOBus structure |
| [in] | mode | the mode |
Definition at line 117 of file pal.c.
References IOBus::mask, IOBus::offset, PAL_IOPORTS_WIDTH, palSetGroupMode, and IOBus::portid.
| void _pal_lld_setgroupmode | ( | ioportid_t | port, |
| ioportmask_t | mask, | ||
| iomode_t | mode | ||
| ) |
Pads mode setup.
This function programs a pads group belonging to the same port with the specified mode.
PAL_MODE_UNCONNECTED is implemented as push pull output at 2MHz.| [in] | port | the port identifier |
| [in] | mask | the group mask |
| [in] | mode | the mode |
Definition at line 66 of file pal_lld.c.
References PAL_MODE_INPUT, PAL_MODE_INPUT_ANALOG, PAL_MODE_INPUT_PULLUP, PAL_MODE_OUTPUT_OPENDRAIN, PAL_MODE_OUTPUT_OPENDRAIN_SLOW, PAL_MODE_OUTPUT_PUSHPULL, PAL_MODE_OUTPUT_PUSHPULL_SLOW, PAL_MODE_RESET, and PAL_MODE_UNCONNECTED.
| #define PAL_MODE_RESET 0 |
After reset state.
The state itself is not specified and is architecture dependent, it is guaranteed to be equal to the after-reset state. It is usually an input state.
Definition at line 48 of file pal.h.
Referenced by _pal_lld_setgroupmode().
| #define PAL_MODE_UNCONNECTED 1 |
Safe state for unconnected pads.
The state itself is not specified and is architecture dependent, it may be mapped on PAL_MODE_INPUT_PULLUP, PAL_MODE_INPUT_PULLDOWN or PAL_MODE_OUTPUT_PUSHPULL as example.
Definition at line 57 of file pal.h.
Referenced by _pal_lld_setgroupmode().
| #define PAL_MODE_INPUT 2 |
Regular input high-Z pad.
Definition at line 62 of file pal.h.
Referenced by _pal_lld_setgroupmode().
| #define PAL_MODE_INPUT_PULLUP 3 |
Input pad with weak pull up resistor.
Definition at line 67 of file pal.h.
Referenced by _pal_lld_setgroupmode().
| #define PAL_MODE_INPUT_PULLDOWN 4 |
| #define PAL_MODE_INPUT_ANALOG 5 |
| #define PAL_MODE_OUTPUT_PUSHPULL 6 |
| #define PAL_MODE_OUTPUT_OPENDRAIN 7 |
| #define PAL_PORT_BIT | ( | n | ) | ((ioportmask_t)(1 << (n))) |
| #define PAL_GROUP_MASK | ( | width | ) | ((ioportmask_t)(1 << (width)) - 1) |
| #define _IOBUS_DATA | ( | name, | |
| port, | |||
| width, | |||
| offset | |||
| ) | {port, PAL_GROUP_MASK(width), offset} |
Data part of a static I/O bus initializer.
This macro should be used when statically initializing an I/O bus that is part of a bigger structure.
| [in] | name | name of the IOBus variable |
| [in] | port | I/O port descriptor |
| [in] | width | bus width in bits |
| [in] | offset | bus bit offset within the port |
| #define IOBUS_DECL | ( | name, | |
| port, | |||
| width, | |||
| offset | |||
| ) | IOBus name = _IOBUS_DATA(name, port, width, offset) |
| #define palInit | ( | config | ) | pal_lld_init(config) |
PAL subsystem initialization.
halInit(), there is no need to explicitly initialize the driver.| [in] | config | pointer to an architecture specific configuration structure. This structure is defined in the low level driver header. |
Definition at line 208 of file pal.h.
Referenced by halInit().
| #define palReadPort | ( | port | ) | ((void)(port), 0) |
Reads the physical I/O port states.
| [in] | port | port identifier |
| #define palReadLatch | ( | port | ) | ((void)(port), 0) |
Reads the output latch.
The purpose of this function is to read back the latched output value.
| [in] | port | port identifier |
| #define palWritePort | ( | port, | |
| bits | |||
| ) | ((void)(port), (void)(bits)) |
Writes a bits mask on a I/O port.
| [in] | port | port identifier |
| [in] | bits | bits to be written on the specified port |
| #define palSetPort | ( | port, | |
| bits | |||
| ) | palWritePort(port, palReadLatch(port) | (bits)) |
Sets a bits mask on a I/O port.
chSysLock() and chSysUnlock(). | [in] | port | port identifier |
| [in] | bits | bits to be ORed on the specified port |
| #define palClearPort | ( | port, | |
| bits | |||
| ) | palWritePort(port, palReadLatch(port) & ~(bits)) |
Clears a bits mask on a I/O port.
chSysLock() and chSysUnlock(). | [in] | port | port identifier |
| [in] | bits | bits to be cleared on the specified port |
| #define palTogglePort | ( | port, | |
| bits | |||
| ) | palWritePort(port, palReadLatch(port) ^ (bits)) |
Toggles a bits mask on a I/O port.
chSysLock() and chSysUnlock(). | [in] | port | port identifier |
| [in] | bits | bits to be XORed on the specified port |
| #define palReadGroup | ( | port, | |
| mask, | |||
| offset | |||
| ) | ((palReadPort(port) >> (offset)) & (mask)) |
Reads a group of bits.
| [in] | port | port identifier |
| [in] | mask | group mask, a logical AND is performed on the input data |
| [in] | offset | group bit offset within the port |
Definition at line 338 of file pal.h.
Referenced by palReadBus().
| #define palWriteGroup | ( | port, | |
| mask, | |||
| offset, | |||
| bits | |||
| ) |
palWritePort(port, (palReadLatch(port) & ~((mask) << (offset))) | \ (((bits) & (mask)) << (offset)))
Writes a group of bits.
| [in] | port | port identifier |
| [in] | mask | group mask, a logical AND is performed on the output data |
| [in] | offset | group bit offset within the port |
| [in] | bits | bits to be written. Values exceeding the group width are masked. |
Definition at line 357 of file pal.h.
Referenced by palWriteBus().
| #define palSetGroupMode | ( | port, | |
| mask, | |||
| offset, | |||
| mode | |||
| ) |
Pads group mode setup.
This function programs a pads group belonging to the same port with the specified mode.
| [in] | port | port identifier |
| [in] | mask | group mask |
| [in] | offset | group bit offset within the port |
| [in] | mode | group mode |
Definition at line 380 of file pal.h.
Referenced by palSetBusMode().
| #define palReadPad | ( | port, | |
| pad | |||
| ) | ((palReadPort(port) >> (pad)) & 1) |
Reads an input pad logical state.
palReadPort().| [in] | port | port identifier |
| [in] | pad | pad number within the port |
| PAL_LOW | low logical state. |
| PAL_HIGH | high logical state. |
| #define palWritePad | ( | port, | |
| pad, | |||
| bit | |||
| ) |
palWritePort(port, (palReadLatch(port) & ~PAL_PORT_BIT(pad)) | \ (((bit) & 1) << pad))
Writes a logical state on an output pad.
chSysLock() and chSysUnlock(). palReadLatch() and palWritePort().| [in] | port | port identifier |
| [in] | pad | pad number within the port |
| [in] | bit | logical value, the value must be PAL_LOW or PAL_HIGH |
| #define palSetPad | ( | port, | |
| pad | |||
| ) | palSetPort(port, PAL_PORT_BIT(pad)) |
Sets a pad logical state to PAL_HIGH.
chSysLock() and chSysUnlock(). palSetPort().| [in] | port | port identifier |
| [in] | pad | pad number within the port |
Definition at line 451 of file pal.h.
Referenced by spi_lld_unselect().
| #define palClearPad | ( | port, | |
| pad | |||
| ) | palClearPort(port, PAL_PORT_BIT(pad)) |
Clears a pad logical state to PAL_LOW.
chSysLock() and chSysUnlock(). palClearPort().| [in] | port | port identifier |
| [in] | pad | pad number within the port |
Definition at line 473 of file pal.h.
Referenced by spi_lld_select().
| #define palTogglePad | ( | port, | |
| pad | |||
| ) | palTogglePort(port, PAL_PORT_BIT(pad)) |
Toggles a pad logical state.
chSysLock() and chSysUnlock(). palTogglePort().| [in] | port | port identifier |
| [in] | pad | pad number within the port |
| #define palSetPadMode | ( | port, | |
| pad, | |||
| mode | |||
| ) | palSetGroupMode(port, PAL_PORT_BIT(pad), 0, mode) |
Pad mode setup.
This function programs a pad with the specified mode.
| [in] | port | port identifier |
| [in] | pad | pad number within the port |
| [in] | mode | pad mode |
| #define PAL_MODE_OUTPUT_PUSHPULL_SLOW 16 |
STM8S specific alternate push-pull slow output mode.
Definition at line 43 of file pal_lld.h.
Referenced by _pal_lld_setgroupmode().
| #define PAL_MODE_OUTPUT_OPENDRAIN_SLOW 17 |
STM8S specific alternate open-drain slow output mode.
Definition at line 48 of file pal_lld.h.
Referenced by _pal_lld_setgroupmode().
| #define PAL_IOPORTS_WIDTH 8 |
Width, in bits, of an I/O port.
Definition at line 74 of file pal_lld.h.
Referenced by palReadBus(), palSetBusMode(), and palWriteBus().
| #define PAL_WHOLE_PORT ((ioportmask_t)0xFF) |
| #define IOPORTS ((PALConfig *)0x5000) |
| #define pal_lld_init | ( | config | ) | (*IOPORTS = *(config)) |
| #define pal_lld_readport | ( | port | ) | ((port)->IDR) |
| #define pal_lld_readlatch | ( | port | ) | ((port)->ODR) |
| #define pal_lld_writeport | ( | port, | |
| bits | |||
| ) | ((port)->ODR = (bits)) |
| #define pal_lld_setgroupmode | ( | port, | |
| mask, | |||
| offset, | |||
| mode | |||
| ) | _pal_lld_setgroupmode(port, mask << offset, mode) |
Pads group mode setup.
This function programs a pads group belonging to the same port with the specified mode.
| [in] | port | port identifier |
| [in] | mask | group mask |
| [in] | offset | group bit offset within the port |
| [in] | mode | group mode |
| typedef uint8_t ioportmask_t |
| typedef GPIO_TypeDef* ioportid_t |