EPPL  0.1 alpha
 All Files Typedefs Groups Pages
eppl_avr8_internals.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013, Radoslaw Koppel
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  - Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10 
11  - Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14 
15  - Neither the name of the copyright holders nor the
16  names of its contributors may be used to endorse or promote products
17  derived from this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
23  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
40 #ifndef EPPL_AVR8_INTERNALS_H_INCLUDED
41 #define EPPL_AVR8_INTERNALS_H_INCLUDED
42 
67 #define EPPL_avr8_CPORTREG(reg, cportnr) (xEPPL_avr8_CPORTREG(reg, cportnr))
68 
72 #define xEPPL_avr8_CPORTREG(reg, cportnr) EPPL_p##reg##cportnr
73 
101 #define EPPL_avr8_PORTREG(reg, portnr) \
102  (((portnr) == 0) ? EPPL_avr8_CPORTREG(reg, 0) : \
103  (((portnr) == 1) ? EPPL_avr8_CPORTREG(reg, 1) : \
104  (((portnr) == 2) ? EPPL_avr8_CPORTREG(reg, 2) : \
105  (((portnr) == 3) ? EPPL_avr8_CPORTREG(reg, 3) : \
106  (((portnr) == 4) ? EPPL_avr8_CPORTREG(reg, 4) : \
107  (((portnr) == 5) ? EPPL_avr8_CPORTREG(reg, 5) : \
108  (((portnr) == 6) ? EPPL_avr8_CPORTREG(reg, 6) : \
109  (((portnr) == 7) ? EPPL_avr8_CPORTREG(reg, 7) : \
110  (((portnr) == 8) ? EPPL_avr8_CPORTREG(reg, 8) : \
111  (((portnr) == 9) ? EPPL_avr8_CPORTREG(reg, 9) : \
112  (((portnr) == 10) ? EPPL_avr8_CPORTREG(reg, 10) : \
113  (((portnr) == 11) ? EPPL_avr8_CPORTREG(reg, 11) : \
114  ((volatile uint8_t*)NULL) \
115  ))))))))))))
116 
124  #ifdef PORTA
125  #define EPPL_pPORT0 &PORTA
126  #define EPPL_pPIN0 &PINA
127  #define EPPL_pDDR0 &DDRA
128  #else
129  #define EPPL_pPORT0 ((uint8_t*)NULL)
130  #define EPPL_pPIN0 ((uint8_t*)NULL)
131  #define EPPL_pDDR0 ((uint8_t*)NULL)
132  #endif
133 
134  #ifdef PORTB
135  #define EPPL_pPORT1 &PORTB
136  #define EPPL_pPIN1 &PINB
137  #define EPPL_pDDR1 &DDRB
138  #else
139  #define EPPL_pPORT1 ((uint8_t*)NULL)
140  #define EPPL_pPIN1 ((uint8_t*)NULL)
141  #define EPPL_pDDR1 ((uint8_t*)NULL)
142  #endif
143 
144  #ifdef PORTC
145  #define EPPL_pPORT2 &PORTC
146  #define EPPL_pPIN2 &PINC
147  #define EPPL_pDDR2 &DDRC
148  #else
149  #define EPPL_pPORT2 ((uint8_t*)NULL)
150  #define EPPL_pPIN2 ((uint8_t*)NULL)
151  #define EPPL_pDDR2 ((uint8_t*)NULL)
152  #endif
153 
154  #ifdef PORTD
155  #define EPPL_pPORT3 &PORTD
156  #define EPPL_pPIN3 &PIND
157  #define EPPL_pDDR3 &DDRD
158  #else
159  #define EPPL_pPORT3 ((uint8_t*)NULL)
160  #define EPPL_pPIN3 ((uint8_t*)NULL)
161  #define EPPL_pDDR3 ((uint8_t*)NULL)
162  #endif
163 
164  #ifdef PORTE
165  #define EPPL_pPORT4 &PORTE
166  #define EPPL_pPIN4 &PINE
167  #define EPPL_pDDR4 &DDRE
168  #else
169  #define EPPL_pPORT4 ((uint8_t*)NULL)
170  #define EPPL_pPIN4 ((uint8_t*)NULL)
171  #define EPPL_pDDR4 ((uint8_t*)NULL)
172  #endif
173 
174  #ifdef PORTF
175  #define EPPL_pPORT5 &PORTF
176  #define EPPL_pPIN5 &PINF
177  #define EPPL_pDDR5 &DDRF
178  #else
179  #define EPPL_pPORT5 ((uint8_t*)NULL)
180  #define EPPL_pPIN5 ((uint8_t*)NULL)
181  #define EPPL_pDDR5 ((uint8_t*)NULL)
182  #endif
183 
184  #ifdef PORTG
185  #define EPPL_pPORT6 &PORTG
186  #define EPPL_pPIN6 &PING
187  #define EPPL_pDDR6 &DDRG
188  #else
189  #define EPPL_pPORT6 ((uint8_t*)NULL)
190  #define EPPL_pPIN6 ((uint8_t*)NULL)
191  #define EPPL_pDDR6 ((uint8_t*)NULL)
192  #endif
193 
194  #ifdef PORTH
195  #define EPPL_pPORT7 &PORTH
196  #define EPPL_pPIN7 &PINH
197  #define EPPL_pDDR7 &DDRH
198  #else
199  #define EPPL_pPORT7 ((uint8_t*)NULL)
200  #define EPPL_pPIN7 ((uint8_t*)NULL)
201  #define EPPL_pDDR7 ((uint8_t*)NULL)
202  #endif
203 
204  #ifdef PORTI
205  #define EPPL_pPORT8 &PORTI
206  #define EPPL_pPIN8 &PINI
207  #define EPPL_pDDR8 &DDRI
208  #else
209  #define EPPL_pPORT8 ((uint8_t*)NULL)
210  #define EPPL_pPIN8 ((uint8_t*)NULL)
211  #define EPPL_pDDR8 ((uint8_t*)NULL)
212  #endif
213 
214  #ifdef PORTJ
215  #define EPPL_pPORT9 &PORTJ
216  #define EPPL_pPIN9 &PINJ
217  #define EPPL_pDDR9 &DDRJ
218  #else
219  #define EPPL_pPORT9 ((uint8_t*)NULL)
220  #define EPPL_pPIN9 ((uint8_t*)NULL)
221  #define EPPL_pDDR9 ((uint8_t*)NULL)
222  #endif
223 
224  #ifdef PORTK
225  #define EPPL_pPORT10 &PORTK
226  #define EPPL_pPIN10 &PINK
227  #define EPPL_pDDR10 &DDRK
228  #else
229  #define EPPL_pPORT10 ((uint8_t*)NULL)
230  #define EPPL_pPIN10 ((uint8_t*)NULL)
231  #define EPPL_pDDR10 ((uint8_t*)NULL)
232  #endif
233 
234  #ifdef PORTL
235  #define EPPL_pPORT11 &PORTL
236  #define EPPL_pPIN11 &PINL
237  #define EPPL_pDDR11 &DDRL
238  #else
239  #define EPPL_pPORT11 ((uint8_t*)NULL)
240  #define EPPL_pPIN11 ((uint8_t*)NULL)
241  #define EPPL_pDDR11 ((uint8_t*)NULL)
242  #endif
243 
253 #define EPPL_avr8_CheckRegSupportsBit(regptr) ( ((int)(regptr)) - __SFR_OFFSET < 32 )
254 
255 
256 
267  #define EPPL_avr8_modeGet(ms) ((ms) & EPPL_mode_mask)
268 
272  #define EPPL_avr8_stateGet(ms) ((ms) & EPPL_outSet_mask)
273 
279  #define EPPL_avr8_mode_knownDDR(m) ( \
280  \
281  (EPPL_avr8_modeGet(m) == EPPL_mode_outPP) || \
282  \
283  ( ((m) & EPPL_mode_flag) && \
284  ( !((m) & EPPL_mode_out) || \
285  \
286  ((m) & EPPL_outSet_flag) ) \
287  ) \
288  )
289 
295  #define EPPL_avr8_mode_knownPORT(m) ( \
296  \
297  (EPPL_avr8_modeGet(m) == EPPL_mode_outOC) || \
298  \
299  ((m) & EPPL_outSet_flag) )
300 
308  #define EPPL_avr8_mode_valueDDR(m) ( ( \
309  (EPPL_avr8_modeGet(m) == EPPL_mode_outPP) || \
310  (EPPL_avr8_mapOutModify_DDR(m) && ( \
311  (EPPL_avr8_mapOutNegate_DDR(m) && (EPPL_avr8_stateGet(m) == EPPL_outSet_0)) || \
312  (!EPPL_avr8_mapOutNegate_DDR(m) && (EPPL_avr8_stateGet(m) == EPPL_outSet_1)) \
313  ) \
314  ) \
315  ) ? 1 : 0 )
316 
324  #define EPPL_avr8_mode_valuePORT(m) ( ( \
325  ((EPPL_avr8_mapOutModify_PORT(m) || !(EPPL_avr8_modeGet(m) & EPPL_mode_out)) && ( \
326  (EPPL_avr8_mapOutNegate_PORT(m) && (EPPL_avr8_stateGet(m) == EPPL_outSet_0)) || \
327  (!EPPL_avr8_mapOutNegate_PORT(m) && (EPPL_avr8_stateGet(m) == EPPL_outSet_1)) \
328  ) \
329  )\
330  ) ? 1 : 0 )
331 
342  #define EPPL_avr8_mode_modifiedReg(reg, from, to) ( \
343  ( EPPL_avr8_mode_known##reg(from) && EPPL_avr8_mode_known##reg(to) && (EPPL_avr8_mode_value##reg(from) != EPPL_avr8_mode_value##reg(to)) ) || \
344  ( !EPPL_avr8_mode_known##reg(from) && EPPL_avr8_mode_known##reg(to) ) \
345  )
346 
362 #define eppl_avr8_asm_sbi(regptr, bit) \
363  __asm__ __volatile__ ( \
364  "sbi %0, %1" "\n\t" \
365  : \
366  : "I" ( ((int)(regptr)) - __SFR_OFFSET ), \
367  "I" (bit) \
368  )
369 
379 #define eppl_avr8_asm_cbi(regptr, bit) \
380  __asm__ __volatile__ ( \
381  "cbi %0, %1" "\n\t" \
382  : \
383  : "I" ( ((int)(regptr)) - __SFR_OFFSET ), \
384  "I" (bit) \
385  )
386 
394 #define EPPL_avr8_checkPinsDescending(...) xEPPL_avr8_checkPinsDescending(__VA_ARGS__, )
395 #define xEPPL_avr8_checkPinsDescending(pin1, pin2, ...) ( \
396  (pin1) >= 0 && (pin2) >= 0 && \
397  EPPL_PORTNR(pin1) == EPPL_PORTNR(pin2) && \
398  EPPL_PINNR(pin1) == (EPPL_PINNR(pin2) + 1) )
399 
415 #define EPPL_avr8_LRSH(v, sl, sr) \
416  ((uint8_t)(((sl) > (sr)) ? \
417  (uint8_t)((v) << ((sl)-(sr))) \
418  : \
419  (uint8_t)((v) >> ((sr)-(sl))) \
420  ))
421 
434 #define eppl_avr8_asm_bitcpy(dst, dstpos, src, srcpos) \
435  __asm__( \
436  "bst %[s], %[sb]" "\n\t" \
437  "bld %[d], %[db]" "\n\t" \
438  :[d]"+r"(dst) \
439  :[s]"r"(src), \
440  [db]"I" (dstpos), \
441  [sb]"I" (srcpos) \
442  )
443 
444 
446 #endif /* EPPL_AVR8_INTERNALS_H_INCLUDED */