Subversion Repositories idreammicro-avr

Compare Revisions

Ignore whitespace Rev 30 → Rev 31

/trunk/libraries/mcp23008/mcp23008.h
41,15 → 41,58
* Public macro definitions.
******************************************************************************/
 
#define MCP23008__GP0 0
#define MCP23008__GP1 1
#define MCP23008__GP2 2
#define MCP23008__GP3 3
#define MCP23008__GP4 4
#define MCP23008__GP5 5
#define MCP23008__GP6 6
#define MCP23008__GP7 7
/******************************************************************************
* MCP23008 GPIOs.
******************************************************************************/
 
/**************************************************************************//**
* \def MCP23008__GP0
* \brief I/O pin 0.
******************************************************************************/
#define MCP23008__GP0 0
 
/**************************************************************************//**
* \def MCP23008__GP1
* \brief I/O pin 1.
******************************************************************************/
#define MCP23008__GP1 1
 
/**************************************************************************//**
* \def MCP23008__GP2
* \brief I/O iin 2.
******************************************************************************/
#define MCP23008__GP2 2
 
/**************************************************************************//**
* \def MCP23008__GP3
* \brief I/O pin 3.
******************************************************************************/
#define MCP23008__GP3 3
 
/**************************************************************************//**
* \def MCP23008__GP4
* \brief I/O pin 4.
******************************************************************************/
#define MCP23008__GP4 4
 
/**************************************************************************//**
* \def MCP23008__GP5
* \brief I/O pin 5.
******************************************************************************/
#define MCP23008__GP5 5
 
/**************************************************************************//**
* \def MCP23008__GP6
* \brief I/O pin 6.
******************************************************************************/
#define MCP23008__GP6 6
 
/**************************************************************************//**
* \def MCP23008__GP7
* \brief I/O pin 7.
******************************************************************************/
#define MCP23008__GP7 7
 
/******************************************************************************
* Public type definitions.
******************************************************************************/
63,8 → 106,8
******************************************************************************/
typedef enum mcp23008__directions
{
MCP23008__DIRECTION__INPUT, /*!< Input. */
MCP23008__DIRECTION__OUTPUT /*!< Output. */
MCP23008__DIRECTION__INPUT, /*!< Input. */
MCP23008__DIRECTION__OUTPUT /*!< Output. */
} mcp23008__direction_t;
 
/**************************************************************************//**
76,8 → 119,8
******************************************************************************/
typedef enum mcp23008__levels
{
MCP23008__LEVEL__LOW, /*!< Low level. */
MCP23008__LEVEL__HIGH /*!< High level. */
MCP23008__LEVEL__LOW, /*!< Low level. */
MCP23008__LEVEL__HIGH /*!< High level. */
} mcp23008__level_t;
 
/**************************************************************************//**
95,7 → 138,7
*
* \brief Initialize MCP23008.
*
* \param hardware_address Hardware address defined by pins A0 to A2.
* \param hardware_address Hardware address defined by pins A0 to A2.
******************************************************************************/
void
mcp23008__initialize
104,7 → 147,14
);
 
/**************************************************************************//**
* \fn void mcp23008__configure pin(
* mcp23008__gpio_t pin,
* mcp23008__direction_t direction)
*
* \brief Configure pin direction.
*
* \param pin Pin to configure.
* \param direction Pin direction.
******************************************************************************/
void
mcp23008__configure_pin
114,7 → 164,11
);
 
/**************************************************************************//**
* \fn void mcp23008__configure_port(mcp23008__direction_t direction)
*
* \brief Configure port direction.
*
* \param direction Port direction.
******************************************************************************/
void
mcp23008__configure_port
123,7 → 177,13
);
 
/**************************************************************************//**
* \fn mcp23008__level_t mcp23008__get_pin_level(mcp23008__gpio_t pin)
*
* \brief Get pin level.
*
* \param pin Pin to get level.
*
* \return Pin level.
******************************************************************************/
mcp23008__level_t
mcp23008__get_pin_level
132,17 → 192,28
);
 
/**************************************************************************//**
* \fn void mcp23008__set_pin_level(
* mcp23008__gpio_t pin,
* mcp23008__level_t level)
*
* \brief Set pin level.
*
* \param pin Pin to set level.
* \param level Level to set.
******************************************************************************/
void
mcp23008__set_pin_level
(
mcp23008__gpio_t pin,
mcp23008__level_t level
mcp23008__gpio_t pin,
mcp23008__level_t level
);
 
/**************************************************************************//**
* \fn uin8_t mcp23008__get_port_value(void)
*
* \brief Get port value.
*
* \return Port value.
******************************************************************************/
uint8_t
mcp23008__get_port_value
151,7 → 222,11
);
 
/**************************************************************************//**
* \fn void mcp23008__set_port_value(uint8_t value)
*
* \brief Set port value.
*
* \param value Value to set.
******************************************************************************/
void
mcp23008__set_port_value
/trunk/libraries/mcp23008/src/mcp23008.c
33,36 → 33,102
#include <twi/twi.h>
#include <useful/bits.h>
 
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
 
/******************************************************************************
* Private macros.
******************************************************************************/
 
#define MCP23008__ADDRESS 0x20
/**************************************************************************//**
* \def MCP23008__ADDRESS
* \brief MCP23008 address on TWI bus.
* This value is given by the datasheet.
******************************************************************************/
#define MCP23008__ADDRESS 0x20
 
#define MCP23008__REG_IODIR 0x00
#define MCP23008__REG_IPOL 0x01
#define MCP23008__REG_GPINTEN 0x02
#define MCP23008__REG_DEFVAL 0x03
#define MCP23008__REG_INTCON 0x04
#define MCP23008__REG_IOCON 0x05
#define MCP23008__REG_GPPU 0x06
#define MCP23008__REG_INTF 0x07
#define MCP23008__REG_INTCAP 0x08
#define MCP23008__REG_GPIO 0x09
#define MCP23008__REG_OLAT 0x0A
/**************************************************************************//**
* \def MCP23008__REG_IODIR
* \brief I/O direction register.
******************************************************************************/
#define MCP23008__REG_IODIR 0x00
 
#define MCP23008__CLOCK_RATE 400000
/**************************************************************************//**
* \def MCP23008__REG_IPOL
* \brief Input polarity register.
******************************************************************************/
#define MCP23008__REG_IPOL 0x01
 
/**************************************************************************//**
* \def MCP23008__REG_GPINTEN
* \brief Interrupt on change control register.
******************************************************************************/
#define MCP23008__REG_GPINTEN 0x02
 
/**************************************************************************//**
* \def MCP23008__REG_DEFVAL
* \brief Default compare register for interrupt on change.
******************************************************************************/
#define MCP23008__REG_DEFVAL 0x03
 
/**************************************************************************//**
* \def MCP23008__REG_INTCON
* \brief Interrupt control register.
******************************************************************************/
#define MCP23008__REG_INTCON 0x04
 
/**************************************************************************//**
* \def MCP23008__REG_IOCON
* \brief Configuration register.
******************************************************************************/
#define MCP23008__REG_IOCON 0x05
 
/**************************************************************************//**
* \def MCP23008__REG_GPPU
* \brief Pull-up resistor configuration register.
******************************************************************************/
#define MCP23008__REG_GPPU 0x06
 
/**************************************************************************//**
* \def MCP23008__REG_INTF
* \brief Interrupt flag register.
******************************************************************************/
#define MCP23008__REG_INTF 0x07
 
/**************************************************************************//**
* \def MCP23008__REG_INTCAP
* \brief Interrupt capture register.
******************************************************************************/
#define MCP23008__REG_INTCAP 0x08
 
/**************************************************************************//**
* \def MCP23008__REG_GPIO
* \brief Port register.
******************************************************************************/
#define MCP23008__REG_GPIO 0x09
 
/**************************************************************************//**
* \def MCP23008__REG_OLAT
* \brief Output latch register.
******************************************************************************/
#define MCP23008__REG_OLAT 0x0A
 
/**************************************************************************//**
* \def MCP23008__CLOCK_RATE
* \brief MCP23008 frequency in Hertz.
* On ATmega, can be 100 kHz or 400 Khz.
******************************************************************************/
#define MCP23008__CLOCK_RATE 400000
 
/******************************************************************************
* Private function prototypes.
******************************************************************************/
 
/**************************************************************************//**
* \fn static uint8_t mcp23008__read_register(uint8_t address)
*
* \brief Read MCP23008 register value.
*
* \param address Address of the register to read value.
******************************************************************************/
static
uint8_t
72,14 → 138,19
);
 
/**************************************************************************//**
* \fn static void mcp23008__write_register(uint8_t address, uint8_t value)
*
* \brief Write MCP23008 register value.
*
* \param address Address of the register to write value.
* \param value Value to write.
******************************************************************************/
static
void
mcp23008__write_register
(
uint8_t address,
uint8_t value
uint8_t address,
uint8_t value
);
 
/******************************************************************************
97,7 → 168,7
*
* \brief Initialize MCP23008.
*
* \param hardware_address Hardware address defined by pins A0 to A2.
* \param hardware_address Hardware address defined by pins A0 to A2.
******************************************************************************/
void
mcp23008__initialize
127,14 → 198,21
}
 
/**************************************************************************//**
* \fn void mcp23008__configure pin(
* mcp23008__gpio_t pin,
* mcp23008__direction_t direction)
*
* \brief Configure pin direction.
*
* \param pin Pin to configure.
* \param direction Pin direction.
******************************************************************************/
void
mcp23008__configure_pin
(
 
mcp23008__gpio_t pin,
mcp23008__direction_t direction
mcp23008__gpio_t pin,
mcp23008__direction_t direction
){
uint8_t port_configuration = mcp23008__read_register(MCP23008__REG_IODIR);
 
151,7 → 229,11
}
 
/**************************************************************************//**
* \fn void mcp23008__configure_port(mcp23008__direction_t direction)
*
* \brief Configure port direction.
*
* \param direction Port direction.
******************************************************************************/
void
mcp23008__configure_port
162,7 → 244,13
}
 
/**************************************************************************//**
* \fn mcp23008__level_t mcp23008__get_pin_level(mcp23008__gpio_t pin)
*
* \brief Get pin level.
*
* \param pin Pin to get level.
*
* \return Pin level.
******************************************************************************/
mcp23008__level_t
mcp23008__get_pin_level
177,7 → 265,14
}
 
/**************************************************************************//**
* \fn void mcp23008__set_pin_level(
* mcp23008__gpio_t pin,
* mcp23008__level_t level)
*
* \brief Set pin level.
*
* \param pin Pin to set level.
* \param level Level to set.
******************************************************************************/
void
mcp23008__set_pin_level
200,7 → 295,11
}
 
/**************************************************************************//**
* \fn uin8_t mcp23008__get_port_value(void)
*
* \brief Get port value.
*
* \return Port value.
******************************************************************************/
uint8_t
mcp23008__get_port_value
213,7 → 312,11
}
 
/**************************************************************************//**
* \fn void mcp23008__set_port_value(uint8_t value)
*
* \brief Set port value.
*
* \param value Value to set.
******************************************************************************/
void
mcp23008__set_port_value
228,7 → 331,11
******************************************************************************/
 
/**************************************************************************//**
* \fn static uint8_t mcp23008__read_register(uint8_t address)
*
* \brief Read MCP23008 register value.
*
* \param address Address of the register to read value.
******************************************************************************/
static
uint8_t
252,14 → 359,19
}
 
/**************************************************************************//**
* \fn static void mcp23008__write_register(uint8_t address, uint8_t value)
*
* \brief Write MCP23008 register value.
*
* \param address Address of the register to write value.
* \param value Value to write.
******************************************************************************/
static
void
mcp23008__write_register
(
uint8_t address,
uint8_t value
uint8_t address,
uint8_t value
){
twi__start();
twi__send_slaw(mcp23008__twi_address);