Fraise  2.1
FRAmework for Interfacing Software and Electronics
 All Classes Files Functions Typedefs Groups
core.h
1 /* ********************************************************************
2  *
3  * Fraise core
4  *
5  *********************************************************************
6  This program 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 Foundation,
18  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20 *********************************************************************
21  * Copyright (c) Antoine Rousseau nov 2011
22  ******************************************************************* */
23 
24 #ifndef CORE_H
25 #define CORE_H
26 
27 /* E X T E R N S *********************************************************** */
28 
29 /* P U B L I C P R O T O T Y P E S **************************************** */
30 #include <config.h>
31 #include <pic18fregs.h>
32 #include <boardconfig.h>
33 #include <boardio.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <typedefs.h>
38 
48 void coreInit();
49 
51 void highInterrupts();
53 void lowInterrupts();
54 
56 void eeWriteByte(unsigned char address, unsigned char value);
58 unsigned char eeReadByte(unsigned char address);
59 
60 
61 #define CALL_FUN(x,a) x(a)
62 #define CALL_FUN2(x,a,b) x(a,b)
63 #define CALL_FUN3(x,a,b,c) x(a,b,c)
64 #define CALL_FUN4(x,a,c,d) x(a,b,c,d)
65 //#define CALL_FUNX(x,args...) x(args)
66 
67 // connector macros :
68 
69 #define KPORT(k) k##PORT
70 #define KBIT(k) k##BIT
71 #define KAN(k) k##AN
72 
73 // PIN ROUTINES
74 //----- Digital Read :
75 #define DIGITALREAD_(connport,connbit) (PORT##connport##bits.R##connport##connbit)
76 //----- Digital Write :
77 #define DIGITALWRITE_(connport,connbit,val) do{ \
78  if((val) != 0) LAT##connport##bits.LAT##connport##connbit = 1; \
79  else LAT##connport##bits.LAT##connport##connbit = 0; \
80 }while(0)
81 #define DIGITALCLEAR_(connport,connbit) do{ LAT##connport##bits.LAT##connport##connbit = 0; }while(0)
82 #define DIGITALSET_(connport,connbit) do{ LAT##connport##bits.LAT##connport##connbit = 1; }while(0)
83 //----- Pin Mode :
84 #define PIN_MODE_DIGITAL_IN_(connport,connbit) do{\
85  TRIS##connport##bits.TRIS##connport##connbit=1;\
86  bitclr(*(__data unsigned char*)((int)&PORT##connport + (int)&ANSELA-(int)&PORTA),connbit);\
87  } while(0)
88 #define PIN_MODE_ANALOGIN_(connport,connbit) do{\
89  TRIS##connport##bits.TRIS##connport##connbit=1;\
90  bitset(*(__data unsigned char*)((int)&PORT##connport + (int)&ANSELA-(int)&PORTA),connbit);\
91  } while(0)
92 #define PIN_MODE_DIGITAL_OUT_(connport,connbit) do{\
93  TRIS##connport##bits.TRIS##connport##connbit=0;\
94  } while(0)
95 
98 
105 #define digitalRead(conn) CALL_FUN2(DIGITALREAD_,KPORT(conn),KBIT(conn))
106 
112 #define digitalWrite(conn,val) CALL_FUN3(DIGITALWRITE_,KPORT(conn),KBIT(conn),val)
113 
118 #define digitalClear(conn) CALL_FUN2(DIGITALCLEAR_,KPORT(conn),KBIT(conn))
119 
124 #define digitalSet(conn) CALL_FUN2(DIGITALSET_,KPORT(conn),KBIT(conn))
125 
129 #define pinModeDigitalIn(conn) CALL_FUN2(PIN_MODE_DIGITAL_IN_,KPORT(conn),KBIT(conn))
130 
134 #define pinModeAnalogIn(conn) CALL_FUN2(PIN_MODE_ANALOGIN_,KPORT(conn),KBIT(conn))
135 
139 #define pinModeDigitalOut(conn) CALL_FUN2(PIN_MODE_DIGITAL_OUT_,KPORT(conn),KBIT(conn))
140 
141 #define setPinAnsel(conn,val) CALL_FUN3(SET_PIN_ANSEL_,KPORT(conn),KBIT(conn),(val)!=0)*/
142 
143 
146 #define BIT_COPY(dest,src) do{ if((src)!=0) dest = 1 ; else dest = 0 ; } while(0)
147 
148 #define bitset(var,bitno) ((var) |= (1 << (bitno)))
149 #define bitclr(var,bitno) ((var) &= ~(1 << (bitno)))
150 #define bittst(var,bitno) (unsigned char)((var & (1 << (bitno)))!=0)
151 
152 
153 #ifndef abs
154 #define abs(x) ((x)<0?-(x):(x))
155 #endif
156 
157 //------------------------- Time : ----------------
158 extern volatile DWORD Now; // time at last high interrupt
159 #define elapsed(since) ((time()-(unsigned long)(since))&0x7FFFFFFF) // in time cycles
160 #define microToTime(T) (((unsigned long)T*(FOSC/64000UL))/1000) //microseconds to time cycles
161 #define timeToMicro(T) (((unsigned long)T*(FOSC/64000UL))/1000) //time cycles to microseconds
162 
165 unsigned unsigned long int time(void);
166 
167 #define timeISR() (Now._dword)
168 
169 typedef unsigned long t_delay;
171 
175 #define delayStart(delay, micros) delay = time() + microToTime(micros)
176 
180 #define delayFinished(delay) (elapsed(delay) < 0x3FFFFFFF)
181 
183 
184 //----------------------- fake port Z ---------------
185 typedef union
186  {
187  struct
188  {
189  unsigned RZ0 : 1;
190  unsigned RZ1 : 1;
191  unsigned RZ2 : 1;
192  unsigned RZ3 : 1;
193  unsigned RZ4 : 1;
194  unsigned RZ5 : 1;
195  unsigned RZ6 : 1;
196  unsigned RZ7 : 1;
197  };
198  } __PORTZbits_t;
199 
200 typedef union
201  {
202  struct
203  {
204  unsigned LATZ0 : 1;
205  unsigned LATZ1 : 1;
206  unsigned LATZ2 : 1;
207  unsigned LATZ3 : 1;
208  unsigned LATZ4 : 1;
209  unsigned LATZ5 : 1;
210  unsigned LATZ6 : 1;
211  unsigned LATZ7 : 1;
212  };
213  } __LATZbits_t;
214 
215 typedef union
216  {
217  struct
218  {
219  unsigned TRISZ0 : 1;
220  unsigned TRISZ1 : 1;
221  unsigned TRISZ2 : 1;
222  unsigned TRISZ3 : 1;
223  unsigned TRISZ4 : 1;
224  unsigned TRISZ5 : 1;
225  unsigned TRISZ6 : 1;
226  unsigned TRISZ7 : 1;
227  };
228 
229  struct
230  {
231  unsigned RZ0 : 1;
232  unsigned RZ1 : 1;
233  unsigned RZ2 : 1;
234  unsigned RZ3 : 1;
235  unsigned RZ4 : 1;
236  unsigned RZ5 : 1;
237  unsigned RZ6 : 1;
238  unsigned RZ7 : 1;
239  };
240  } __TRISZbits_t;
241 
242 
243 typedef struct
244  {
245  unsigned ANSZ0 : 1;
246  unsigned ANSZ1 : 1;
247  unsigned ANSZ2 : 1;
248  unsigned ANSZ3 : 1;
249  unsigned ANSZ4 : 1;
250  unsigned ANSZ5 : 1;
251  unsigned ANSZ6 : 1;
252  unsigned ANSZ7 : 1;
253  } __ANSELZbits_t;
254 
255 #define _PORTZ_ADDR 0x0480
256 #define _PORT_TO_LAT 0x09
257 #define _PORT_TO_TRIS 0x12
258 #define _PORT_TO_ANSEL (-0x48)
259 
260 extern __at(_PORTZ_ADDR) volatile unsigned char PORTZ;
261 extern __at(_PORTZ_ADDR) volatile __PORTZbits_t PORTZbits;
262 
263 extern __at(_PORTZ_ADDR + _PORT_TO_LAT) volatile unsigned char LATZ;
264 extern __at(_PORTZ_ADDR + _PORT_TO_LAT) volatile __LATZbits_t LATZbits;
265 
266 extern __at(_PORTZ_ADDR + _PORT_TO_TRIS) volatile unsigned char TRICZ;
267 extern __at(_PORTZ_ADDR + _PORT_TO_TRIS) volatile __TRISZbits_t TRISZbits;
268 
269 extern __at(_PORTZ_ADDR + _PORT_TO_ANSEL) volatile unsigned char ANSELZ;
270 extern __at(_PORTZ_ADDR + _PORT_TO_ANSEL) volatile __ANSELZbits_t ANSELZbits;
271 
272 //----------------------- fake port Z connectors ---------------
273 
274 #define KZ0PORT Z // PORTZ0 & LATZ0 are initialized to 0
275 #define KZ0BIT 0
276 
277 #define KZ1PORT Z // PORTZ1 & LATZ1 are initialized to 1
278 #define KZ1BIT 1
279 
280 #define KZ2PORT Z
281 #define KZ2BIT 2
282 
283 #define KZ3PORT Z
284 #define KZ3BIT 3
285 
286 #define KZ4PORT Z
287 #define KZ4BIT 4
288 
289 #define KZ5PORT Z
290 #define KZ5BIT 5
291 
292 #define KZ6PORT Z
293 #define KZ6BIT 6
294 
295 #define KZ7PORT Z
296 #define KZ7BIT 7
297 
301 #endif //FRAISE_H
unsigned unsigned long int time(void)
Get time since bootup in 64/FOSC steps (1us @ 64MHz, 8us @ 8MHz).
Definition: core.c:151
Definition: core.h:200
unsigned char eeReadByte(unsigned char address)
Write to eeprom.
Definition: core.c:59
Definition: core.h:243
Definition: core.h:185
Definition: core.h:215
void highInterrupts()
User defined high priority interrupt routine.
void coreInit()
Init core module.
Definition: core.c:75
void lowInterrupts()
User defined low priority interrupt routine.
void eeWriteByte(unsigned char address, unsigned char value)
Write to eeprom.
Definition: core.c:36
Definition: typedefs.h:90
unsigned long t_delay
Delay type.
Definition: core.h:170