GlobLib
HAL and API libraries for MCUs and hardware.

This module contains low level functions GPIO access. More...

Files

file  attiny13a_gpio.h
 Header file for attiny13a GPIO.
 

Enumerations

Functions

mcu_error pinSetup (gpio_mode mode, gpio_port port, gpio_pin pin)
 Setup a GPIO pin for a given function. More...
 
void pinHigh (gpio_port port, gpio_pin pin)
 Set a given gpio pin on a port to logic high. More...
 
void pinLow (gpio_port port, gpio_pin pin)
 Set a given gpio pin on a port to logic low. More...
 
void pinWrite (gpio_port port, gpio_pin pin, gpio_state state)
 Set a given gpio pin on a port to a logic state. More...
 
void pinToggle (gpio_port port, gpio_pin pin)
 Toggle the output of a given pin on a port. More...
 
uint8_t pinRead (gpio_port port, gpio_pin pin)
 Read the current digital value of an input pin. More...
 
mcu_error GPIO_ISREnable (gpio_port port, gpio_pin pin, gpio_isr trigger, void(*handle)(void))
 Setup interrupts on a gpio port / pin. More...
 
mcu_error GPIO_ISRDisable (gpio_pin pin)
 Disable interrupts on a gpio port / pin. More...
 

Detailed Description

This module contains low level functions GPIO access.

The attiny13a only has a single GPIO port, port B, with pins numbered 0 - 5. Pin 5 is used for the reset (active low). To use this pin as a regular GPIO the correct fuses need to be set in the Makefile.

Author
Stuart Ianna
Version
0.1
Date
October 2018
Warning
Interrupts also need to be enabled in the Makefile, they are disabled by default to save space.
Bug:
None
Todo:
Verified Compilers
  • avr-gcc 4.9.2
Minimum working example (blinky)
//Change to include appropriate MCU target
#include <stm32f103cb_gpio.h>
int main(void){
//Setup port C pin 13 for digital output
pinSetup(GPIO_DO,PORTC,PIN13);
while(1){
//Toggle the pin's output value
pinToggle(PORTC,PIN13);
//Wait a bit
for(int i = 0; i < 0x7FFF; i++);
}
return 0;
}
Minimum working example (Pin input)
//Change to include appropriate MCU target
#include <stm32f103cb_gpio.h>
int main(void){
//Setup port C pin 13 for digital output
pinSetup(GPIO_DO,PORTC,PIN13);
//Setup port A pin 6 for digital input
pinSetup(GPIO_DI,PORTA,PIN6);
while(1){
//Set output to pin input value (Inverse logic);
pinWrite(PORTC,PIN13,pinRead(PORTA,PIN6));
//Wait a bit
for(int i = 0; i < 0x7FFF; i++);
}
return 0;
}
Minimum working example (interrupt)
//Change to include appropriate MCU target
#include <stm32f103cb_gpio.h>
//Function to call for interrupt event
void callme(void);
int main(void){
//Setup port C pin 13 for digital output
pinSetup(GPIO_DO,PORTC,PIN13);
//Setup the ISR
GPIO_ISREnable(PORTA, PIN6, GPIO_RISING, &callme);
while(1){
//Do nothing
}
return 0;
}
void callme(void){
//Toggle the pin's output value
pinToggle(PORTC,PIN13);
}

Enumeration Type Documentation

enum gpio_isr

GPIO interrupt options for pins.

corresponds with the memory address offset used.

Warning
Interrupts need to be enabled in the Makefile before they will work. They are disabled by default to save space.
Enumerator
GPIO_RISING 

Interrupt occurs on rising edge detection.

GPIO_FALLING 

Interrupt occurs on falling edge detection.

GPIO_BOTH 

Interrupt occurs on rising and falling edge detection.

GPIO_LEVEL_LOW 

Interrupt occurs on low level.

Definition at line 104 of file attiny13a_gpio.h.

enum gpio_mode

GPIO setup options.

These values are used when setting up the pin for use.

Enumerator
GPIO_DI 

Setup the port for digital input.

GPIO_DO 

Setup the port for digital output.

GPIO_UART_TX 

Setup the port for USART transmit.

GPIO_UART_RX 

Setup the port for USART receive.

GPIO_I2C 

Setup the port for I2C.

GPIO_PWM 

Setup the port for output PWM.

GPIO_IC 

Setup the port for input capture.

GPIO_ADC 

Setup the port for ADC.

GPIO_SPI_OUT 

Setup the port for output SPI.

GPIO_SPI_IN 

Setup the port for input SPI.

Definition at line 65 of file attiny13a_gpio.h.

enum gpio_pin

GPIO pins available for each port.

These macros are used to access the hardware pins for each port. Each hexidecimal number corresponds with the memory address offset used.

Enumerator
PIN0 

Pin 0 of the port.

PIN1 

Pin 1 of the port.

PIN2 

Pin 2 of the port.

PIN3 

Pin 3 of the port.

PIN4 

Pin 4 of the port.

PIN5 

Pin 5 of the port.

Definition at line 116 of file attiny13a_gpio.h.

enum gpio_port

GPIO ports available for the MCU.

There is only one port available on the Attiny13a. This macro is used for combatability with other MCU

Enumerator
PORTB 

Port B of the MCU.

Definition at line 93 of file attiny13a_gpio.h.

enum gpio_state

GPIO pin states.

These values are used when writing a value to a pin with pinWrite().

Enumerator
GPIO_LOW 

Set the corresponding pin to logic low.

GPIO_HIGH 

Set the corresponding pin to logic high.

Definition at line 82 of file attiny13a_gpio.h.

Function Documentation

mcu_error GPIO_ISRDisable ( gpio_pin  pin)

Disable interrupts on a gpio port / pin.

This function clears all previous setting of the interrupt.

Parameters
pinThe pin of the port, defined by gpio_pin
Returns
none
mcu_error GPIO_ISREnable ( gpio_port  port,
gpio_pin  pin,
gpio_isr  trigger,
void(*)(void)  handle 
)

Setup interrupts on a gpio port / pin.

The MCU can only support one external interrupt. This interrupt is called for every pin which has a mask enabled. Good stradegy is to test which pin could meet the intterupt condition in the main program.

Parameters
portThe port of the MCU, defined by gpio_port
pinThe pin of the port, defined by gpio_pin
triggerThe interrupt trigger, define by gpio_isr
handlePointer to the function to be called on interrupt event.
Warning
Interrupts need to be enabled in the Makefile before they will work. They are disabled by default to save space.
Returns
none
void pinHigh ( gpio_port  port,
gpio_pin  pin 
)

Set a given gpio pin on a port to logic high.

Pin must be set for digital output to use this function

Parameters
portThe port of the MCU, defined by gpio_port
pinThe pin of the port, defined by gpio_pin
Returns
none.

Definition at line 47 of file attiny13a_gpio.c.

void pinLow ( gpio_port  port,
gpio_pin  pin 
)

Set a given gpio pin on a port to logic low.

Pin must be set for digital output to use this function

Parameters
portThe port of the MCU, defined by gpio_port
pinThe pin of the port, defined by gpio_pin
Returns
none.

Definition at line 53 of file attiny13a_gpio.c.

uint8_t pinRead ( gpio_port  port,
gpio_pin  pin 
)

Read the current digital value of an input pin.

Pin must be set for digital input to use this function

Parameters
portThe port of the MCU, defined by gpio_port
pinThe pin of the port, defined by gpio_pin
Returns
The value of the pin. 1 = Logic high, 0 = logic low.

Definition at line 78 of file attiny13a_gpio.c.

mcu_error pinSetup ( gpio_mode  mode,
gpio_port  port,
gpio_pin  pin 
)

Setup a GPIO pin for a given function.

This function is also employed by other modules to configure pins and ports.

Parameters
modeThe mode for the pin to operate, defined by gpio_mode
portThe port of the MCU, defined by gpio_port
pinThe pin of the port, defined by gpio_pin
Returns
none.

Definition at line 24 of file attiny13a_gpio.c.

void pinToggle ( gpio_port  port,
gpio_pin  pin 
)

Toggle the output of a given pin on a port.

Pin must be set for digital output to use this function

Parameters
portThe port of the MCU, defined by gpio_port
pinThe pin of the port, defined by gpio_pin
Returns
none.

Definition at line 72 of file attiny13a_gpio.c.

void pinWrite ( gpio_port  port,
gpio_pin  pin,
gpio_state  state 
)

Set a given gpio pin on a port to a logic state.

Pin must be set for digital output to use this function

Parameters
stateThe state to set the pin to, defined by gpio_state
portThe port of the MCU, defined by gpio_port
pinThe pin of the port, defined by gpio_pin
Returns
none.

Definition at line 59 of file attiny13a_gpio.c.