From 96a9c1585a17bce4abf3b393e1c307388878adbb Mon Sep 17 00:00:00 2001 From: asterix Date: Wed, 3 Jun 2009 09:37:02 +0000 Subject: [PATCH] Generalize sipo module. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2700 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/drv/sipo.c | 36 ++++++++++++++++++++++++---------- bertos/drv/sipo.h | 50 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/bertos/drv/sipo.c b/bertos/drv/sipo.c index 44bbffe5..c3026274 100644 --- a/bertos/drv/sipo.c +++ b/bertos/drv/sipo.c @@ -34,8 +34,7 @@ * * \brief SIPO Module * - * The SIPO module transforms a serial input in - * a parallel output. Please check hw_sipo.h + * The SIPO module transforms a serial input in a parallel output. Please check hw_sipo.h * file to customize hardware related parameters. * * \author Andrea Grandi @@ -47,42 +46,57 @@ #include "hw/hw_sipo.h" #include +#include #include #include + +#define SIPO_DATAORDER_START(order) (order ? SIPO_DATAORDER_START_LSB : SIPO_DATAORDER_START_MSB) +#define SIPO_DATAORDER_SHIFT(shift, order) (order ? ((shift) <<= 1) : ((shift) >>= 1)) + /** * Write a char in sipo shift register */ -INLINE void sipo_putchar(uint8_t c) +INLINE void sipo_putchar(uint8_t c, uint8_t bit_order, uint8_t clock_pol) { + uint8_t shift = SIPO_DATAORDER_START(bit_order); + for(int i = 0; i < 8; i++) { - if((c & BV(i)) == 0) + if((c & shift) == 0) SIPO_SI_LOW(); else SIPO_SI_HIGH(); - SIPO_SI_CLOCK(); + SIPO_SI_CLOCK(clock_pol); + + SIPO_DATAORDER_SHIFT(shift, bit_order); } } /** * Write a buffer into the sipo register and, when finished, give a load pulse. */ - static size_t sipo_write(UNUSED_ARG(struct KFile *, fd), const void *_buf, size_t size) + static size_t sipo_write(struct KFile *_fd, const void *_buf, size_t size) { const uint8_t *buf = (const uint8_t *)_buf; + Sipo *fd = SIPO_CAST(_fd); size_t write_len = size; + ASSERT(buf); + SIPO_SET_SI_LEVEL(); + SIPO_SET_CLK_LEVEL(fd->clock_pol); + SIPO_SET_LD_LEVEL(fd->load_device, fd->load_pol); + // Load into the shift register all the buffer bytes while(size--) - sipo_putchar(*buf++); + sipo_putchar(*buf++, fd->bit_order, fd->clock_pol); - // We have just finished to shift the bytes into the register, now load them. - SIPO_LOAD(); + // We finsh to load bytes, so load it. + SIPO_LOAD(fd->load_device, fd->load_pol); return write_len; } @@ -96,7 +110,7 @@ void sipo_init(Sipo *fd) memset(fd, 0, sizeof(Sipo)); - //Set kfile struct type as a Sipo structure. + //Set kfile struct type as a generic kfile structure. DB(fd->fd._type = KFT_SIPO); // Set up SIPO writing functions. @@ -104,4 +118,6 @@ void sipo_init(Sipo *fd) SIPO_INIT_PIN(); + /* Enable sipo output */ + SIPO_ENABLE(); } diff --git a/bertos/drv/sipo.h b/bertos/drv/sipo.h index be2c6c1b..940f8537 100644 --- a/bertos/drv/sipo.h +++ b/bertos/drv/sipo.h @@ -31,32 +31,72 @@ * --> * * \brief Generic Serial-in, Parallel-out implementation (SIPO). - * - * This module use kfile interface. * * * \version $Id$ * * \author Andrea Grandi * \author Daniele Basile - * + * * $WIZ$ module_name = "sipo" * $WIZ$ module_depends = "kfile" * $WIZ$ module_hw = "bertos/hw/hw_sipo.h" - * */ + + + + #ifndef DRV_SIPO_H #define DRV_SIPO_H +#include "hw/hw_sipo.h" + #include +#define SIPO_DATAORDER_START_LSB 1 +#define SIPO_DATAORDER_START_MSB 0x80 + +/** + * Define enum to set sipo data order. + */ +typedef enum SipoBitOrder +{ + SIPO_DATAORDER_MSB = 0, + SIPO_DATAORDER_LSB = 1 +} SipoBitOrder; + +/** + * Define enum to set the start level of clock. + */ +typedef enum SipoClockPol +{ + SIPO_START_LOW = 0, + SIPO_START_HIGH = 1 + +} SipoClkPol; + +/** + * Define enum to set load signal level. + */ +typedef enum SipoLoadPol +{ + SIPO_LOW_TO_HIGH = 0, + SIPO_HIGH_TO_LOW = 1 + +} SipoLoadPol; + /** * Sipo KFile context structure. */ typedef struct Sipo { - KFile fd; ///< File descriptor. + KFile fd; ///< File descriptor. + SipoMap load_device; ///< Descriptor of the device that we want drive. + SipoLoadPol load_pol; ///< Set polarity of load signal. + SipoClkPol clock_pol; ///< Set polarity of data clock. + SipoBitOrder bit_order; ///< Set the order of pushed bits in sipo. + } Sipo; /** -- 2.25.1