Llosa de Viango Tollos – tollosBit.c

Introduction

Features

Getting started

Download


Libraries

  Tollos libraries

  Device libraries

    boards

    microcontrollers

    peripherals


Background

  Sample application

  Troubleshooting

tollosBit.c – bit (digital pin) functions for Tollos

These functions take a port (GPIO register) number and a bit mask; all those bits that have a 1 set in the mask will be included in the operation.

The definitions in tollosBit.h can be used for naming ports and bits, for example:

bitSet(PORT2, BIT5 | BIT7, BIT_ON);

Interrupts can be enabled for one or more bits at once using the bitEnable function, and can be disabled using bitDisable.
Functions
 bitDisable   disable interrupts for a bit or bits
 bitEnable   enable interrupt for a bit or bits
 bitExist   test whether a bit or bits exist
 bitQuery   query the state of an input bit or bits
 bitSet   effect an action on a bit or bits

bitDisable – disable interrupts for a bit or bits

int bitDisable(uint port, uint mask);

port – port in which the bit or bits reside

mask – mask of which bit(s) to disable

returns 0 if OK, negative if error (i.e., if the port is not valid or a bit is not an interrupt bit)

Disables interrupts for the specified bits (see also bitEnable). This also frees up the handler in use for a subsequent bitEnable.

bitEnable – enable interrupt for a bit or bits

int bitEnable(uint port, uint mask, uint action, vector handler);

port – port in which the bit or bits reside

mask – mask of which bit(s) to enable

action – one of BIT_RISE, BIT_FALL, or BIT_RISEFALL which indicates whether the interrupt should occur on rising edges, falling edges, or both

handler – the interrupt handler that will be called when any of the specified bits causes the interrupt action

returns 0 if OK, negative if error (i.e., if the port is not valid, a bit is not an interrupt bit, or the interrupt vector needed for the interrupts is already in use)

Once enabled, if the specified event occurs to any of the bits that are 1 in the mask word then the interrupt handler will be called.

A later call to bitEnable for a bit overrides any earlier call completely (it is not cumulative).

Interrupts for a bit or bits can be disabled by a call to bitDisable().

A limited range of interrupt handlers are available, depending on the processor:

LPC processors can set interrupts and handlers for bits 0...11 and 15...30 in PORT0 and bits 0...13 in PORT2 (42 in all).

STM32 processors can set interrupts on any port bits (0...15 in any port) but only one port for any one bit (e.g., PORTA bit 4 and PORTB bit 4 cannot both be enabled.

Bit interrupts may share resources with other interrupt mechanisms, and may interfere with them. On LPC ARM devices EINT3 (external interrupt 3) is shared with bit interrupts.

Tollos handles all bit interrupts, calls the user handler as specified on this call, and on its return clears the interrupt for the bit.

bitExist – test whether a bit or bits exist

int bitExist(uint port, uint mask);

port – port in which the bit or bits reside

mask – mask of which bit(s) to test

returns 1 if all the bits in the mask exist in the specified port, or 0 otherwise (including invalid port cases)

Note: this reports whether the bit exists in the microcontroller, not whether it is brought out to a board pin or whether it supports interrupts.

bitQuery – query the state of an input bit or bits

int bitQuery(uint port, uint mask, uint *bits);

port – port in which the bit or bits reside

mask – mask of which bit(s) to query

bits – receives the state of the selected bits (other bits will be 0)

returns 0 if OK, −1 if error

The result will be −1 if port is not a valid port number. Bits are undefined if the selected bits do not exist. Bits may also be undefined or unpredictable if the pin corresponding to a bit is in alternative use (e.g., for ADC).

bitSet – effect an action on a bit or bits

int bitSet(uint port, uint mask, uint action);

port – port in which the bit or bits reside

mask – mask of which bit(s) to affect

action – one of BIT_ON, BIT_OFF, or BIT_INVERT for bit output, or one of BIT_PULLUP, BIT_TRISTATE, or BIT_PULLDOWN for bit input, or BIT_OPENDRAIN to set output not push-pull

returns 0 if OK, −1 if error

BIT_ON, BIT_OFF, or BIT_INVERT set the specified bit(s) to push-pull output mode (unless already in output mode) then sets the bit(s) as appropriate for the request. BIT_OFF sets each bit to 0, BIT_ON sets them to 1, BIT_INVERT flips each bit.

The action BIT_OPENDRAIN sets the specified bit(s) to open drain output mode with level high (=1).

The actions BIT_PULLUP, BIT_TRISTATE, and BIT_PULLDOWN set the specified bit(s) to input mode with pull-up, neither pull-up nor pull-down, or pull-down.

After reset, bits are BIT_TRISTATE.

For example:

rc=bitSet(PORT1, BIT26 | BIT27, BIT_PULLUP);

The result is −1 if port is not a valid port number. Bits are undefined if the selected bits do not exist.

Tollos and these web pages were written by Mike Cowlishaw; Please send me any corrections, suggestions, etc.
All content © Mike Cowlishaw, 2010–2012, except where marked otherwise. All rights reserved. The pages here are for non-commercial use only (see the separate licence for Tollos source code). Privacy policy: the Speleotrove website records no personal information and sets no ‘cookies’. However, statistics, etc. might be recorded by the web hosting service.

This page was last updated on 2010-12-11 by c2wiki.