From 083645700424a51aa1a8d8cc24bcdae45e627f18 Mon Sep 17 00:00:00 2001 From: asterix Date: Tue, 31 Mar 2009 09:13:20 +0000 Subject: [PATCH] Refactory and generalize sipo module. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2445 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cpu/avr/drv/sipo.c | 79 +++++++++++++++++++++++++-------------- bertos/cpu/avr/drv/sipo.h | 35 +++++++++++++---- bertos/hw/hw_sipo.h | 46 ++++++++++++++++------- 3 files changed, 109 insertions(+), 51 deletions(-) diff --git a/bertos/cpu/avr/drv/sipo.c b/bertos/cpu/avr/drv/sipo.c index db4ec87e..cb821692 100644 --- a/bertos/cpu/avr/drv/sipo.c +++ b/bertos/cpu/avr/drv/sipo.c @@ -32,55 +32,76 @@ * * \version $Id$ * - * \author Andrea Grandi - * * \brief SIPO Module * * The SIPO module trasform a serial input in * a parallel output. Please check hw_sipo.h * file to customize hardware relative parameters. * + * \author Daniele Basile */ #include "sipo.h" + #include "hw/hw_sipo.h" -#include +#include -Serial *sipo_port; +#include -/** Initialize the SIPO port */ -void sipo_init(void) -{ - CLOCK_LOW; - SET_SOUT_LOW; - LOAD_LOW; - SET_SCK_OUT; - SET_SOUT_OUT; - LOAD_INIT; - sipo_putchar(0x0); - OE_OUT; - OE_LOW; -} +#include -/** Write a char in the SIPO port and manage the LOAD pin */ -void sipo_putchar(uint8_t c) +/** + * Write a char in sipo shift register + */ +INLINE void sipo_putchar(uint8_t c) { - for(int i = 0; i <= 7; i++) + for(int i = 0; i < 8; i++) { if((c & BV(i)) == 0) - { - SET_SOUT_LOW; - } + SIPO_SI_LOW(); else - { - SET_SOUT_HIGH; - } + SIPO_SI_HIGH(); - CLOCK_PULSE; + SIPO_SI_CLOCK(); } +} + +/** + * Write a buffer into sipo register and when finish to + * we load it. + */ + static size_t sipo_write(UNUSED_ARG(struct KFile *, fd), const void *_buf, size_t size) +{ + const uint8_t *buf = (const uint8_t *)_buf; + size_t write_len = size; + ASSERT(buf); - LOAD_HIGH; - LOAD_LOW; + // Load into shift register all byte in buffer + while(size--) + sipo_putchar(*buf++); + + // We finsh to load bytes into shift register, load it. + SIPO_LOAD(); + + return write_len; } +/** + * Initialize the SIPO + */ +void sipo_init(Sipo *fd) +{ + ASSERT(fd); + + memset(fd, 0, sizeof(Sipo)); + + //Set kfile struct type as a generic kfile structure. + DB(fd->fd._type = KFT_SIPO); + + // Set up data flash programming functions. + fd->fd.write = sipo_write; + + SIPO_INIT_PIN(); + +} diff --git a/bertos/cpu/avr/drv/sipo.h b/bertos/cpu/avr/drv/sipo.h index ab57908b..ab5100f4 100644 --- a/bertos/cpu/avr/drv/sipo.h +++ b/bertos/cpu/avr/drv/sipo.h @@ -35,18 +35,37 @@ * * \version $Id$ * - * \author Andrea Grandi + * \author Daniele Basile */ -#ifndef SIPO_H -#define SIPO_H +#ifndef DRV_SIPO_H +#define DRV_SIPO_H -#include "hw/hw_sipo.h" +#include -#include +/** + * Sipo KFile context structure. + */ +typedef struct Sipo +{ + KFile fd; ///< File descriptor. +} Sipo; + +/** + * ID for sipo. + */ +#define KFT_SIPO MAKE_ID('S', 'I', 'P', 'O') + +/** + * Convert + ASSERT from generic KFile to Sipo. + */ +INLINE Sipo * SIPO_CAST(KFile *fd) +{ + ASSERT(fd->_type == KFT_SIPO); + return (Sipo *)fd; +} -void sipo_init(void); -void sipo_putchar(uint8_t c); +void sipo_init(Sipo *fd); -#endif // SIPO_H +#endif // DRV_SIPO_H diff --git a/bertos/hw/hw_sipo.h b/bertos/hw/hw_sipo.h index aa77783f..0ac56ba7 100644 --- a/bertos/hw/hw_sipo.h +++ b/bertos/hw/hw_sipo.h @@ -27,7 +27,7 @@ * the GNU General Public License. * * Copyright 2003, 2004, 2006, 2008 Develer S.r.l. (http://www.develer.com/) - * Copyright 2000 Bernie Innocenti + * Copyright 2000 Bernardo Innocenti * All Rights Reserved. * --> * @@ -36,7 +36,7 @@ * * \version $Id$ * - * \author Andrea Grandi + * \author Daniele Basile */ #ifndef HW_SIPO_H @@ -44,19 +44,37 @@ #warning TODO:This is an example implementation, you must implement it! -#define LOAD_HIGH /* Implement me! */ -#define LOAD_LOW /* Implement me! */ -#define LOAD_INIT /* Implement me! */ -#define SET_SCK_OUT /* Implement me! */ -#define SET_SOUT_OUT /* Implement me! */ -#define CLOCK_HIGH /* Implement me! */ -#define CLOCK_LOW /* Implement me! */ -#define SET_SOUT_HIGH /* Implement me! */ -#define SET_SOUT_LOW /* Implement me! */ -#define CLOCK_PULSE /* Implement me! */ -#define OE_OUT -#define OE_LOW +/** + * Define the procedure to set one bit low/hight to + * serial input in sipo device. + */ +#define SIPO_SI_HIGH()/* Implement me! */ +#define SIPO_SI_LOW() /* Implement me! */ + +/** + * Drive pin to load the bit, presented in serial-in pin, + * into sipo shift register. + */ +#define SIPO_SI_CLOCK() /* Implement me! */ + +/** + * Clock the content of shift register to output. + */ +#define SIPO_LOAD() /* Implement me! */ + +/** + * Enable the shift register output. + */ +#define SIPO_ENABLE() /* Implement me! */ +/** + * Do anything that needed to init sipo pins. + */ +#define SIPO_INIT_PIN() \ + do { \ + /* Implement me! */ \ + } while(0) + #endif /* HW_SIPO_H */ -- 2.25.1