hsk-libs-dev  270
High Speed Karlsruhe XC878 library collection
hsk_io.h
Go to the documentation of this file.
1 /** \file
2  * HSK I/O headers
3  *
4  * This file contains macro definitions to use and initialize I/O ports
5  * and variables bitwise.
6  *
7  * All the macros take a port and a mask to select the affected pins. All
8  * operations are masked with the selected pins so it is safe to define
9  * 0xff (every bit 1) to activate a certain property.
10  *
11  * Set and get macros take a bit field to define the value that represents
12  * the \c on or \c true state, so the logic code can always use a \c 1 for
13  * <tt>true</tt>/<tt>on</tt>.
14  *
15  * The macros are grouped as:
16  * - \ref IO_PORT_IN
17  * - \ref IO_PORT_OUT
18  * - \ref IO_VAR
19  *
20  * @author kami
21  *
22  * \section ports I/O Port Pull-Up/-Down Table
23  *
24  * The device boots with all parallel ports configured as inputs.
25  * The following table lists the pins that come up with activated internal
26  * pull up:
27  *
28  * | Port\\Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
29  * |-----------|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:
30  * | P0 | 1 | 1 | x | x | x | 1 | x | x
31  * | P1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
32  * | P3 | x | 1 | x | x | x | x | x | x
33  * | P4 | x | x | x | x | x | 1 | x | x
34  * | P5 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
35  */
36 
37 #ifndef _HSK_IO_H_
38 #define _HSK_IO_H_
39 
40 /**
41  * \defgroup IO_PORT_IN Input Port Access
42  *
43  * This group contains defines and macros to initialize port pins as inputs
44  * and read them.
45  *
46  * @{
47  */
48 
49 /**
50  * Initializes a set of port pins as inputs.
51  *
52  * @warning
53  * Expects port page 0 and RMAP 0, take care in ISRs
54  * @param port
55  * The parallel port to configure
56  * @param pins
57  * A bit mask of the pins to select
58  */
59 #define IO_PORT_IN_INIT(port, pins) { \
60  port##_DIR &= ~(pins); \
61 }
62 
63 /**
64  * Bit mask to set the logical 1 to GND level for all selected pins.
65  *
66  * @note
67  * Can also be used for \ref IO_PORT_OUT
68  */
69 #define IO_PORT_ON_GND 0
70 
71 /**
72  * Bit mask to set the logical 1 to high level for all selected pins.
73  *
74  * @note
75  * Can also be used for \ref IO_PORT_OUT
76  */
77 #define IO_PORT_ON_HIGH 0xff
78 
79 
80 /**
81  * Evaluates to a bit mask of logical pin states of a port.
82  *
83  * @note
84  * Can also be used for \ref IO_PORT_OUT
85  * @warning
86  * Expects port page 0 and RMAP 0, take care in ISRs
87  * @param port
88  * The parallel port to access
89  * @param pins
90  * A bit mask of the pins to select
91  * @param on
92  * A bit mask of pins that defines the states which represent on
93  */
94 #define IO_PORT_GET(port, pins, on) ( \
95  (port##_DATA ^ ~(on)) & (pins) \
96 )
97 
98 /**
99  * @}
100  */
101 
102 /**
103  * \defgroup IO_PORT_OUT Output Port Access
104  *
105  * This group contains macros and defines to initialize port pins for output
106  * and safely set output states.
107  *
108  * @{
109  */
110 
111 /**
112  * Bit mask to set weak drive strength for all selected pins.
113  */
114 #define IO_PORT_STRENGTH_WEAK 0
115 
116 /**
117  * Bit mask to set strong drive strength for all selected pins.
118  */
119 #define IO_PORT_STRENGTH_STRONG 0xff
120 
121 /**
122  * Bit mask to disable drain mode for all selected pins.
123  */
124 #define IO_PORT_DRAIN_DISABLE 0
125 
126 /**
127  * Bit mask to enable drain mode for all selected pins.
128  */
129 #define IO_PORT_DRAIN_ENABLE 0xff
130 
131 /**
132  * Initializes a set of port pins as outputs.
133  *
134  * @warning
135  * Expects port page 0 and RMAP 0, take care in ISRs
136  * @param port
137  * The parallel port to configure
138  * @param pins
139  * A bit mask of the pins to select
140  * @param strength
141  * A bit mask of pins with strong drive strength
142  * @param drain
143  * A bit mask of pins that only drive GND
144  * @param on
145  * A bit mask of pins that defines the states which represent on
146  * @see IO_PORT_ON_GND
147  * @see IO_PORT_ON_HIGH
148  * @param set
149  * Initial logical values for the defined outputs
150  */
151 #define IO_PORT_OUT_INIT(port, pins, strength, drain, on, set) { \
152  port##_DIR |= pins; \
153  SFR_PAGE(_pp3, noSST); \
154  port##_OD &= (drain) | ~(pins); \
155  port##_OD |= (drain) & (pins); \
156  port##_DS &= (strength) | ~(pins); \
157  port##_DS |= (strength) & (pins); \
158  SFR_PAGE(_pp0, noSST); \
159  port##_DATA &= ((set) ^ ~(on)) | ~(pins); \
160  port##_DATA |= ((set) ^ ~(on)) & (pins); \
161 }
162 
163 /**
164  * Set a set of output port pins.
165  *
166  * @warning
167  * Expects port page 0 and RMAP 0, take care in ISRs
168  * @param port
169  * The parallel port to set
170  * @param pins
171  * A bit mask of the pins to select
172  * @param on
173  * A bit mask of pins that defines the states which represent on
174  * @see IO_PORT_ON_GND
175  * @see IO_PORT_ON_HIGH
176  * @param set
177  * Set logical values for the defined outputs
178  */
179 #define IO_PORT_OUT_SET(port, pins, on, set) {\
180  port##_DATA &= ((set) ^ ~(on)) | ~(pins); \
181  port##_DATA |= ((set) ^ ~(on)) & (pins); \
182 }
183 
184 /**
185  * @}
186  */
187 
188 /**
189  * \defgroup IO_PORT_PULL I/O Port Pull-Up/-Down Setup
190  *
191  * This group contains macros and defines to initialize the pull-up/-down
192  * devices of port pins.
193  *
194  * @{
195  */
196 
197 /**
198  * Bit mask to disable pull up/down for all selected pins.
199  */
200 #define IO_PORT_PULL_DISABLE 0
201 
202 /**
203  * Bit mask to enable pull up/down for all selected pins.
204  */
205 #define IO_PORT_PULL_ENABLE 0xff
206 
207 /**
208  * Bit mask to select pull down for all selected pins.
209  */
210 #define IO_PORT_PULL_DOWN 0
211 
212 /**
213  * Bit mask to select pull up for all selected pins.
214  */
215 #define IO_PORT_PULL_UP 0xff
216 
217 /**
218  * Sets the pull-up/-down properties of port pins.
219  *
220  * @warning
221  * Expects port page 0 and RMAP 0, take care in ISRs
222  * @param port
223  * The parallel port to configure
224  * @param pins
225  * A bit mask of the pins to select
226  * @param pull
227  * A bit mask of pins to activate the internal pull up/down device for
228  * @param dir
229  * A bit mask of pins to set the pull direction
230  */
231 #define IO_PORT_PULL_INIT(port, pins, pull, dir) { \
232  SFR_PAGE(_pp1, noSST); \
233  port##_PUDSEL &= (dir) | ~(pins); \
234  port##_PUDSEL |= (dir) & (pins); \
235  port##_PUDEN &= (pull) | ~(pins); \
236  port##_PUDEN |= (pull) & (pins); \
237  SFR_PAGE(_pp0, noSST); \
238 }
239 
240 /**
241  * @}
242  */
243 
244 /**
245  * \defgroup IO_VAR Variable Access
246  *
247  * This group specifies macros to access bits of a variable. Their value lies
248  * in the seperation of encoded \c on state and logical \c on (1), as well as
249  * the safe bit masking.
250  *
251  * @{
252  */
253 
254 /**
255  * Set a set of variable bits.
256  *
257  * @param var
258  * The variable to set
259  * @param bits
260  * A bit mask of the bits to select
261  * @param on
262  * A bit mask that defines the states which represent true
263  * @param set
264  * Set logical values for the defined bits
265  */
266 #define IO_VAR_SET(var, bits, on, set) {\
267  (var) &= ((set) ^ ~(on)) | ~(bits); \
268  (var) |= ((set) ^ ~(on)) & (bits); \
269 }
270 
271 /**
272  * Evaluates to a bit mask of logical states of a variable.
273  *
274  * @param var
275  * The variable to access
276  * @param bits
277  * A bit mask of the bits to select
278  * @param on
279  * A bit mask that defines the states which represent true
280  */
281 #define IO_VAR_GET(var, bits, on) ( \
282  ((var) ^ ~(on)) & (bits) \
283 )
284 
285 /**
286  * @}
287  */
288 
289 #endif /* _HSK_IO_H_ */
290