From c22f33b299c475e12d1525a82a24f724d0bd68aa Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 20 Sep 2006 20:38:51 +0000 Subject: [PATCH] Added MCP41xxx digital potentiometer git-svn-id: https://src.develer.com/svnoss/bertos/trunk@723 38d2e660-2303-0410-9eaa-f027e97ec537 --- drv/mcp41.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ drv/mcp41.h | 34 ++++++++++++++++++++++++++++++ hw/hw_mcp41.c | 30 ++++++++++++++++++++++++++ hw/hw_mcp41.h | 46 ++++++++++++++++++++++++++++++++++++++++ hw/mcp41_map.h | 30 ++++++++++++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100755 drv/mcp41.c create mode 100755 drv/mcp41.h create mode 100755 hw/hw_mcp41.c create mode 100755 hw/hw_mcp41.h create mode 100755 hw/mcp41_map.h diff --git a/drv/mcp41.c b/drv/mcp41.c new file mode 100755 index 00000000..b9bcf05b --- /dev/null +++ b/drv/mcp41.c @@ -0,0 +1,57 @@ +/** + * \file + * + * + * \version $Id$ + * + * \brief MCP41xxx digital potentiomenter driver (implementation) + * + * \version $Id$ + * \author Francesco Sacchi + */ + +#include + +#include +#include +#include + +#include +#include +#include + +static Serial *spi_ser; + +/** + * Set resitance for digital pot \a dev + */ +void mcp41_setResistance(Mcp41Dev dev, mcp41_res_t res) +{ + MCP41_ON(dev); + /* send command byte */ + ser_putchar(MCP41_WRITE_DATA, spi_ser); + ser_putchar(((MCP41_HW_MAX * (uint32_t)res) + (MCP41_MAX / 2)) / MCP41_MAX, spi_ser); + ser_drain(spi_ser); + + MCP41_OFF(dev); +} + + +/** + * Initialize mcp41 potentiometer driver + */ +void mcp41_init(Serial *spi_port) +{ + Mcp41Dev dev; + + /* Init all potentiometer ports */ + for (dev = 0; dev < MCP41_CNT; dev++) + { + SET_MCP41_DDR(dev); + MCP41_OFF(dev); + } + + spi_ser = spi_port; +} diff --git a/drv/mcp41.h b/drv/mcp41.h new file mode 100755 index 00000000..b5217b0e --- /dev/null +++ b/drv/mcp41.h @@ -0,0 +1,34 @@ +/** + * \file + * + * + * \version $Id$ + * + * \brief MCP41xxx digital potentiomenter driver (interface) + * + * \version $Id$ + * \author Francesco Sacchi + */ + +#ifndef DRV_MCP41_H +#define DRV_MCP41_H + +#include +#include +#include + +#define MCP41_WRITE_DATA 0x11 +#define MCP41_HW_MAX 255 +#define MCP41_MAX 999 + +/** + * Type for digital potentiometer resistance. + */ +typedef uint16_t mcp41_res_t; + +void mcp41_setResistance(Mcp41Dev dev, mcp41_res_t res); +void mcp41_init(Serial *spi_port); + +#endif /* DRV_MCP41_H */ diff --git a/hw/hw_mcp41.c b/hw/hw_mcp41.c new file mode 100755 index 00000000..4455e2be --- /dev/null +++ b/hw/hw_mcp41.c @@ -0,0 +1,30 @@ +/** + * \file + * + * + * \version $Id$ + * + * \brief MCP41 hardware-specific definitions + * + * \version $Id$ + * \author Francesco Sacchi + */ + +#include + +#include +#include + +#include + + +const uint16_t mcp41_ports[MCP41_CNT] = { + _SFR_IO_ADDR(PORTB), /** Led pot */ + _SFR_IO_ADDR(PORTB), /** Photometer pot */ + }; +const uint8_t mcp41_pins [MCP41_CNT] = { + BV(PB7), + BV(PB6), + }; diff --git a/hw/hw_mcp41.h b/hw/hw_mcp41.h new file mode 100755 index 00000000..d2ef6900 --- /dev/null +++ b/hw/hw_mcp41.h @@ -0,0 +1,46 @@ +/** + * \file + * + * + * \version $Id$ + * + * \brief MCP41 hardware-specific definitions + * + * \version $Id$ + * \author Francesco Sacchi + */ + +#ifndef HW_MCP41_H +#define HW_MCP41_H + +#include +#include +#include +#include + +extern const uint16_t mcp41_ports[MCP41_CNT]; +extern const uint8_t mcp41_pins[MCP41_CNT]; + +/** + * Set MCP41 port associated with \a dev to output. + */ +INLINE void SET_MCP41_DDR(Mcp41Dev dev) +{ + /* DDR port is 1 address before PORT */ + ATOMIC(_SFR_IO8(mcp41_ports[dev] - 1) |= mcp41_pins[dev]); +} + +INLINE void MCP41_ON(Mcp41Dev i) +{ + ATOMIC(_SFR_IO8(mcp41_ports[i]) &= ~mcp41_pins[i]); +} + +INLINE void MCP41_OFF(Mcp41Dev i) +{ + ATOMIC(_SFR_IO8(mcp41_ports[i]) |= mcp41_pins[i]); +} + + +#endif /* HW_MCP41_H */ diff --git a/hw/mcp41_map.h b/hw/mcp41_map.h new file mode 100755 index 00000000..a0170dee --- /dev/null +++ b/hw/mcp41_map.h @@ -0,0 +1,30 @@ +/** + * \file + * + * + * \brief MCP41 digital potentiometer map definitions. + * + * \version $Id$ + * + * \author Francesco Sacchi + */ + +#ifndef MCP41_MAP_H +#define MCP41_MAP_H + +/** \name Enum for mcp41 pot evices. + * \{ + */ +typedef enum Mcp41Dev +{ + MCP41_LED, + MCP41_PHOTO_GAIN, + + MCP41_CNT, +} Mcp41Dev; +/* \} */ + +#endif /* MCP41_MAP_H */ -- 2.25.1