Update sipo api.
[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  *
34  * \brief SIPO Module
35  *
36  * The SIPO module transforms a serial input in a parallel output. Please check hw_sipo.h
37  * file to customize hardware related parameters.
38  *
39  * \author Andrea Grandi <andrea@develer.com>
40  * \author Daniele Basile <asterix@develer.com>
41  */
42
43 #include "sipo.h"
44
45 #include "hw/hw_sipo.h"
46 #include "cfg/cfg_sipo.h"
47
48 #define LOG_LEVEL  SIPO_LOG_LEVEL
49 #define LOG_FORMAT SIPO_LOG_FORMAT
50 #include <cfg/log.h>
51 #include <cfg/compiler.h>
52
53 #include <io/kfile.h>
54
55 #include <string.h>
56
57
58 #define SIPO_DATAORDER_START(order)          (order ? SIPO_DATAORDER_START_LSB : SIPO_DATAORDER_START_MSB)
59 #define SIPO_DATAORDER_SHIFT(shift, order)   (order ?  ((shift) <<= 1) : ((shift) >>= 1))
60
61 /**
62  * Write a char in sipo shift register
63  */
64 INLINE void sipo_putchar(uint8_t c, uint8_t bit_order, uint8_t clock_pol)
65 {
66         uint8_t shift = SIPO_DATAORDER_START(bit_order);
67
68         for(int i = 0; i < 8; i++)
69         {
70                 if((c & shift) == 0)
71                         SIPO_SI_LOW();
72                 else
73                         SIPO_SI_HIGH();
74
75                 SIPO_SI_CLOCK(clock_pol);
76
77                 SIPO_DATAORDER_SHIFT(shift, bit_order);
78         }
79 }
80
81
82 #if !CONFIG_SIPO_DISABLE_OLD_API
83 /**
84  * Write a buffer into the sipo register and, when finished, give a load pulse.
85  */
86 static size_t sipo_write(struct KFile *_fd, const void *_buf, size_t size)
87 {
88         const uint8_t *buf = (const uint8_t *)_buf;
89         Sipo *fd = SIPO_CAST(_fd);
90         size_t write_len = size;
91
92         ASSERT(buf);
93
94         SIPO_SET_SI_LEVEL();
95         SIPO_SET_CLK_LEVEL(fd->clock_pol);
96         SIPO_SET_LD_LEVEL(fd->load_device, fd->load_pol);
97
98         // Load into the shift register all the buffer bytes
99         while(size--)
100                 sipo_putchar(*buf++, fd->bit_order, fd->clock_pol);
101
102         // We finsh to load bytes, so load it.
103         SIPO_LOAD(fd->load_device, fd->load_pol);
104
105         return write_len;
106 }
107
108 #else /* New api */
109
110 /**
111  * Write a buffer into the sipo register and, when finished, give a load pulse.
112  */
113 static size_t sipo_write(struct KFile *_fd, const void *_buf, size_t size)
114 {
115         const uint8_t *buf = (const uint8_t *)_buf;
116         Sipo *fd = SIPO_CAST(_fd);
117         size_t write_len = size;
118
119         ASSERT(buf);
120
121         SIPO_SET_SI_LEVEL();
122         SIPO_SET_CLK_LEVEL(fd->settings & SIPO_CLOCK_POL);
123         SIPO_SET_LD_LEVEL(fd->device, fd->settings & SIPO_LOAD_LEV);
124
125         // Load into the shift register all the buffer bytes
126         while(size--)
127                 sipo_putchar(*buf++, fd->settings & SIPO_DATAORDER,
128                         fd->settings & SIPO_CLOCK_POL);
129
130         // We finsh to load bytes, so load it.
131         SIPO_LOAD(fd->device, fd->settings & SIPO_LOAD_LEV);
132
133         return write_len;
134 }
135 #endif
136
137
138 INLINE void init(Sipo *fd)
139 {
140         ASSERT(fd);
141
142         //Set kfile struct type as a generic kfile structure.
143         DB(fd->fd._type = KFT_SIPO);
144
145         // Set up SIPO writing functions.
146         fd->fd.write = sipo_write;
147
148         SIPO_INIT_PIN();
149
150         /* Enable sipo output */
151         SIPO_ENABLE();
152 }
153
154 /**
155  * Initialize the SIPO
156  */
157 #if !CONFIG_SIPO_DISABLE_OLD_API
158
159 void sipo_init_1(Sipo *fd)
160 {
161         init(fd);
162 }
163 #else /* New api */
164
165 void sipo_init_3(Sipo *fd, SipoMap dev, uint8_t settings)
166 {
167         memset(fd, 0, sizeof(fd));
168         fd->settings = settings;
169         fd->device = dev;
170         init(fd);
171 }
172 #endif