Some fix and paste. Use putchar instead a write in protocol module.
[bertos.git] / bertos / drv / sipo.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id$
34  *
35  * \brief SIPO Module
36  *
37  * The SIPO module transforms a serial input in
38  * a parallel output. Please check hw_sipo.h
39  * file to customize hardware related parameters.
40  *
41  * \author Andrea Grandi <andrea@develer.com>
42  * \author Daniele Basile <asterix@develer.com>
43  */
44
45 #include "sipo.h"
46
47 #include "hw/hw_sipo.h"
48
49 #include <cfg/compiler.h>
50
51 #include <kern/kfile.h>
52
53 #include <string.h>
54
55 /**
56  * Write a char in sipo shift register
57  */
58 INLINE void sipo_putchar(uint8_t c)
59 {
60         for(int i = 0; i < 8; i++)
61         {
62                 if((c & BV(i)) == 0)
63                         SIPO_SI_LOW();
64                 else
65                         SIPO_SI_HIGH();
66
67                 SIPO_SI_CLOCK();
68         }
69 }
70
71 /**
72  * Write a buffer into the sipo register and, when finished, give a load pulse.
73  */
74  static size_t sipo_write(UNUSED_ARG(struct KFile *, fd), const void *_buf, size_t size)
75 {
76         const uint8_t *buf = (const uint8_t *)_buf;
77         size_t write_len = size;
78         ASSERT(buf);
79
80         // Load into the shift register all the buffer bytes
81         while(size--)
82                 sipo_putchar(*buf++);
83
84         // We have just finished to shift the bytes into the register, now load them.
85         SIPO_LOAD();
86
87         return write_len;
88 }
89
90 /**
91  * Initialize the SIPO
92  */
93 void sipo_init(Sipo *fd)
94 {
95         ASSERT(fd);
96
97         memset(fd, 0, sizeof(Sipo));
98
99         //Set kfile struct type as a Sipo structure.
100         DB(fd->fd._type = KFT_SIPO);
101
102         // Set up SIPO writing functions.
103         fd->fd.write = sipo_write;
104
105         SIPO_INIT_PIN();
106
107 }