Page MenuHomec4science

gpio.h
No OneTemporary

File Metadata

Created
Tue, Mar 4, 20:33
/*
* File: gpio.h
* Author: tuleu
*
* Created on August 23, 2012, 12:26 PM
*/
#ifndef SBCP_UC_GPIO_H
#define SBCP_UC_GPIO_H
#include <p33Fxxxx.h>
/**
* \defgroup gpio_m GPIO module
*
* \brief a module to use GPIO pins
*
* Simply create anew gpio with gpio_create, and then you can gpio_set() or
* gpio_clear() it.
*
*/
/**
* \name GPIO port
* GPIO port available on dsPIC33
* \ingroup gpio_m
*/
///@{
/// Port A
#define GPIO_PORT_A (&TRISA)
/// Port B
#define GPIO_PORT_B (&TRISB)
/// Port C
#define GPIO_PORT_C (&TRISC)
///@}
typedef volatile unsigned int * gpio_port_t;
/**
* Pin available on a gpio
* \ingroup gpio_m
*/
typedef enum gpio_pin_t {
GPIO_PIN_0 = 0,
GPIO_PIN_1 = 1,
GPIO_PIN_2 = 2,
GPIO_PIN_3 = 3,
GPIO_PIN_4 = 4,
GPIO_PIN_5 = 5,
GPIO_PIN_6 = 6,
GPIO_PIN_7 = 7,
GPIO_PIN_8 = 8,
GPIO_PIN_9 = 9,
GPIO_PIN_10 = 10,
GPIO_PIN_11 = 11,
GPIO_PIN_12 = 12,
GPIO_PIN_13 = 13,
GPIO_PIN_14 = 14,
GPIO_PIN_15 = 15,
} gpio_pin_t;
/**
* direction of a gpio
* \ingroup gpio_m
*/
enum gpio_direction_t {
GPIO_OUTPUT = 0, //!> pin is an output
GPIO_INPUT = 1 //!> pin is an input
};
typedef enum gpio_direction_t gpio_direction_t;
/**
* Represents a GPIO pin as a variable.
* \ingroup gpio_m
*/
struct gpio {
/// port
gpio_port_t port;
/// pin
gpio_pin_t pin;
};
typedef struct gpio gpio;
// TAKEN FROM MOLOLE
/** Atomic and operation to prevent race conditions inside interrupts: *x = (*x) & y */
#define atomic_and(x,y) do { __asm__ volatile ("and.w %[yy], [%[xx]], [%[xx]]": : [xx] "r" (x), [yy] "r"(y): "cc","memory"); } while(0)
/** Atomic or operation to prevent race conditions inside interrupts: *x = (*x) | y */
#define atomic_or(x,y) do { __asm__ volatile ("ior.w %[yy], [%[xx]], [%[xx]]" : : [xx] "r" (x), [yy] "r"(y): "cc","memory"); } while(0)
// NOT TAKEN FROM MOLOLE
/**
* Creates a new gpio.
* \ingroup gpio_m
* @param port the gpio port to use
* @param pin the gpio_pin_t to use
* @param dir the gpio_direction_t of the port
* @return a newly created gpio
*/
gpio gpio_create(gpio_port_t port, gpio_pin_t pin, gpio_direction_t dir);
/**
* Sets the direction of the gpio.
* \ingroup gpio_m
* @param io the gpio to modify
* @param dir the gpio_direction_t to use
*/
void gpio_set_direction(gpio * io, gpio_direction_t dir);
/**
* Writes to the gpio port.
* \ingroup gpio_m
* @param io the gpio to use
* @param value the value to write (either 0 or 1)
*/
void gpio_write(gpio * io, int value);
#define GPIO_TRIS(io) ((io).port)
#define GPIO_PORT(io) ((io).port + 1)
#define GPIO_LAT(io) ((io).port + 2)
#define GPIO_ODC(io) ((io).port + 3)
/**
* Atomic macro to set a pin.
* \ingroup gpio_m
*/
#define gpio_set(io) do{\
atomic_or((volatile unsigned int * ) GPIO_LAT(io), 1 << (io).pin);\
}while(0)
/**
* Atomic macro to clear a pin.
* \ingroup gpio_m
*/
#define gpio_clear(io) do{\
atomic_and((volatile unsigned int *) GPIO_LAT(io), ~(1 << (io).pin ));\
}while(0)
/**
* reads a gpio.
* \ingroup gpio_m
*/
#define gpio_read(io) ( *(GPIO_PORT(io)) & (1 << (io).pin) )
/**
* \defgroup rpio_m Pin assignement module
*
* \brief module to define pin assignements.
*
* Simply use map_pin_to_periph_output()
*
* \code
//will map pin RP3 to UART 1 TX output
map_pin_to_periph_output(3,RP_U1TX);
\endcode
* \ingroup gpio_m
*/
/**
* RP pin function.
* \ingroup rpio_m
*/
typedef enum rp_function_t {
RP_NULL = 0x00,
RP_C1OUT = 0x01,
RP_C2OUT = 0x02,
RP_U1TX = 0x03,
RP_U1RTS = 0x04,
RP_U2TX = 0x05,
RP_U2RTS = 0x06,
RP_SDO1 = 0x07,
RP_SCK1 = 0x08,
RP_SS1 = 0x09,
RP_SDO2 = 0x0a,
RP_SCK2 = 0x0b,
RP_SS2 = 0x0c,
RP_C1TX = 0x10,
RP_OC1 = 0x12,
RP_OC2 = 0x13,
RP_OC3 = 0x14,
RP_OC4 = 0x15,
RP_UPDN1 = 0x1a,
RP_UPDN2 = 0x1b
} rp_dunction_t;
/**
* Maps a given rp_pin_t to a given rp_function_t
* \ingroup rpio_m
* \param pin the pin number to use.
* \param periph the rp_function_t to use
*/
#define map_pin_to_periph_output(pin,periph) do{\
_RP ## pin ## R = periph;\
}while(0)
/**
* RP available pins.
* \ingroup rpio_m
*/
typedef enum rp_pin_t {
RP_0 = 0,
RP_1 ,
RP_2 ,
RP_3 ,
RP_4 ,
RP_5 ,
RP_6 ,
RP_7 ,
RP_8 ,
RP_9 ,
RP_10 ,
RP_11 ,
RP_12 ,
RP_13 ,
RP_14 ,
RP_15 ,
RP_16 ,
RP_17 ,
RP_18 ,
RP_19 ,
RP_20 ,
RP_21 ,
RP_22 ,
RP_23 ,
RP_24 ,
RP_25 ,
RP_26
} rp_pin_t;
#endif /* SBCP_UC_GPIO_H */

Event Timeline