core.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *********************************************************************
  18. * Copyright (c) Antoine Rousseau nov 2011
  19. ******************************************************************* */
  20. #ifndef CORE_H
  21. #define CORE_H
  22. /* E X T E R N S *********************************************************** */
  23. /* P U B L I C P R O T O T Y P E S **************************************** */
  24. #include <config.h>
  25. #include <pic18fregs.h>
  26. #include <boardconfig.h>
  27. #include <boardio.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <typedefs.h>
  32. /** @defgroup core Core module
  33. * Core module implements eeprom, pins and time.
  34. * @{
  35. */
  36. /** @name Initialization
  37. @{ */
  38. /** @brief Init core module. */
  39. /** Init processor, configure TIMER0 for time() use.
  40. * Normally coreInit() is called by fruitInit() (see module fruit), so you don't have to call it by yourself.
  41. */
  42. void coreInit();
  43. /**@}*/
  44. /**@name User defined functions
  45. @{*/
  46. /** @brief User defined initialization. */
  47. void setup();
  48. /** @brief User defined forever loop. */
  49. void loop();
  50. /** @brief Optional user defined high priority interrupt routine. */
  51. void highInterrupts();
  52. /** @brief Optional user defined low priority interrupt routine. */
  53. void lowInterrupts();
  54. /**@}*/
  55. /* @brief Write to eeprom. */
  56. void eeWriteByte(unsigned char address, unsigned char value);
  57. /* @brief Write to eeprom. */
  58. unsigned char eeReadByte(unsigned char address);
  59. #define CALL_FUN(x,a) x(a)
  60. #define CALL_FUN2(x,a,b) x(a,b)
  61. #define CALL_FUN3(x,a,b,c) x(a,b,c)
  62. #define CALL_FUN4(x,a,b,c,d) x(a,b,c,d)
  63. //#define CALL_FUNX(x,args...) x(args)
  64. // connector macros :
  65. /*#define KPORT(k) k##PORT
  66. #define KBIT(k) k##BIT
  67. #define KAN(k) k##AN
  68. #define KINT(k) k##INT*/
  69. #define KPROP(k, prop) k##prop
  70. #define KPORT(k) CALL_FUN2(KPROP, k, PORT)
  71. #define KBIT(k) CALL_FUN2(KPROP, k, BIT)
  72. #define KAN(k) CALL_FUN2(KPROP, k, AN)
  73. #define KINT(k) CALL_FUN2(KPROP, k, INT)
  74. // PIN ROUTINES
  75. //----- Digital Read :
  76. #define DIGITALREAD_(connport,connbit) (PORT##connport##bits.R##connport##connbit)
  77. //----- Digital Write :
  78. #define DIGITALWRITE_(connport,connbit,val) do{ \
  79. if((val) != 0) LAT##connport##bits.LAT##connport##connbit = 1; \
  80. else LAT##connport##bits.LAT##connport##connbit = 0; \
  81. }while(0)
  82. #define DIGITALCLEAR_(connport,connbit) do{ LAT##connport##bits.LAT##connport##connbit = 0; }while(0)
  83. #define DIGITALSET_(connport,connbit) do{ LAT##connport##bits.LAT##connport##connbit = 1; }while(0)
  84. //----- Pin Mode :
  85. #define PIN_MODE_DIGITAL_IN_(connport,connbit) do{\
  86. TRIS##connport##bits.TRIS##connport##connbit=1;\
  87. bitclr(*(__data unsigned char*)((int)&PORT##connport + (int)&ANSELA-(int)&PORTA),connbit);\
  88. } while(0)
  89. #define PIN_MODE_ANALOGIN_(connport,connbit) do{\
  90. TRIS##connport##bits.TRIS##connport##connbit=1;\
  91. bitset(*(__data unsigned char*)((int)&PORT##connport + (int)&ANSELA-(int)&PORTA),connbit);\
  92. } while(0)
  93. #define PIN_MODE_DIGITAL_OUT_(connport,connbit) do{\
  94. TRIS##connport##bits.TRIS##connport##connbit=0;\
  95. } while(0)
  96. /** \name Pin routines */
  97. //@{
  98. /** @brief Digital read from pin.
  99. Get the voltage (0/1) seen from the pin.
  100. @param conn Symbol of the pin (example : K1 for connector 1)
  101. @return 0 if voltage was LOW
  102. @return 1 if voltage was HIGH
  103. */
  104. #define digitalRead(conn) CALL_FUN2(DIGITALREAD_,KPORT(conn),KBIT(conn))
  105. /** @brief Digital write to pin.
  106. Set the pin output voltage to LOW or HIGH.
  107. @param conn Symbol of the pin (example : K1 for connector 1)
  108. @param val 0 or 1
  109. */
  110. #define digitalWrite(conn,val) CALL_FUN3(DIGITALWRITE_,KPORT(conn),KBIT(conn),val)
  111. /** @brief Digital clear pin.
  112. Clear the pin output voltage.
  113. @param conn Symbol of the pin (example : K1 for connector 1)
  114. */
  115. #define digitalClear(conn) CALL_FUN2(DIGITALCLEAR_,KPORT(conn),KBIT(conn))
  116. /** @brief Digital set pin.
  117. Set the pin output voltage ot HIGH.
  118. @param conn Symbol of the pin (example : K1 for connector 1)
  119. */
  120. #define digitalSet(conn) CALL_FUN2(DIGITALSET_,KPORT(conn),KBIT(conn))
  121. /** @brief Set pin to Digital Input.
  122. @param conn Symbol of the pin (example : K1 for connector 1)
  123. */
  124. #define pinModeDigitalIn(conn) CALL_FUN2(PIN_MODE_DIGITAL_IN_,KPORT(conn),KBIT(conn))
  125. /** @brief Set pin to Analog Input.
  126. @param conn Symbol of the pin (example : K1 for connector 1)
  127. */
  128. #define pinModeAnalogIn(conn) CALL_FUN2(PIN_MODE_ANALOGIN_,KPORT(conn),KBIT(conn))
  129. /** @brief Set pin to Digital Output.
  130. @param conn Symbol of the pin (example : K1 for connector 1)
  131. */
  132. #define pinModeDigitalOut(conn) CALL_FUN2(PIN_MODE_DIGITAL_OUT_,KPORT(conn),KBIT(conn))
  133. //@}
  134. #define setPinAnsel(conn,val) CALL_FUN3(SET_PIN_ANSEL_,KPORT(conn),KBIT(conn),(val)!=0)*/
  135. /** \name Bit macro */
  136. //@{
  137. #define BIT_COPY(dest,src) do{ if((src)!=0) dest = 1 ; else dest = 0 ; } while(0) ///< Copy bit **src** to **dest**.
  138. #define bitset(var,bitno) ((var) |= (1 << (bitno))) ///< Set bit **bitno** in variable **var**.
  139. #define bitclr(var,bitno) ((var) &= ~(1 << (bitno))) ///< Clear bit **bitno** in variable **var**.
  140. #define bittst(var,bitno) (unsigned char)((var & (1 << (bitno)))!=0) ///< Get value of bit **bitno** in variable **var**.
  141. //@}
  142. #ifndef abs
  143. #define abs(x) ((x)<0?-(x):(x))
  144. #endif
  145. //------------------------- Time : ----------------
  146. extern volatile DWORD Now; // time at last high interrupt
  147. #define elapsed(since) ((time()-(unsigned long)(since))&0x7FFFFFFF) // in time cycles
  148. #define microToTime(T) (((unsigned long)T*(FOSC/64000UL))/1000) //microseconds to time cycles
  149. #define timeToMicro(T) (((unsigned long)T*(FOSC/64000UL))/1000) //time cycles to microseconds
  150. /** \name Time functions */
  151. //@{
  152. unsigned unsigned long int time(void); ///< Get time since bootup in 64/FOSC steps (1us @ 64MHz, 8us @ 8MHz).
  153. #define timeISR() (Now._dword) ///< time() equivalent to be used inside of interrupts routines.
  154. /// Time type
  155. typedef unsigned long t_time;
  156. /// Delay type
  157. typedef unsigned long t_delay;
  158. /// @brief Start delay
  159. /// @param delay Delay to start (use t_delay type)
  160. /// @param micros Timeout in microseconds
  161. #define delayStart(delay, micros) delay = time() + microToTime(micros)
  162. /// @brief Test delay timeout
  163. /// @param delay Delay to test (use t_delay type)
  164. /// @return TRUE if timeout
  165. #define delayFinished(delay) (elapsed(delay) < 0x3FFFFFFF)
  166. //@}
  167. //----------------------- fake port Z ---------------
  168. typedef union
  169. {
  170. struct
  171. {
  172. unsigned RZ0 : 1;
  173. unsigned RZ1 : 1;
  174. unsigned RZ2 : 1;
  175. unsigned RZ3 : 1;
  176. unsigned RZ4 : 1;
  177. unsigned RZ5 : 1;
  178. unsigned RZ6 : 1;
  179. unsigned RZ7 : 1;
  180. };
  181. } __PORTZbits_t;
  182. typedef union
  183. {
  184. struct
  185. {
  186. unsigned LATZ0 : 1;
  187. unsigned LATZ1 : 1;
  188. unsigned LATZ2 : 1;
  189. unsigned LATZ3 : 1;
  190. unsigned LATZ4 : 1;
  191. unsigned LATZ5 : 1;
  192. unsigned LATZ6 : 1;
  193. unsigned LATZ7 : 1;
  194. };
  195. } __LATZbits_t;
  196. typedef union
  197. {
  198. struct
  199. {
  200. unsigned TRISZ0 : 1;
  201. unsigned TRISZ1 : 1;
  202. unsigned TRISZ2 : 1;
  203. unsigned TRISZ3 : 1;
  204. unsigned TRISZ4 : 1;
  205. unsigned TRISZ5 : 1;
  206. unsigned TRISZ6 : 1;
  207. unsigned TRISZ7 : 1;
  208. };
  209. struct
  210. {
  211. unsigned RZ0 : 1;
  212. unsigned RZ1 : 1;
  213. unsigned RZ2 : 1;
  214. unsigned RZ3 : 1;
  215. unsigned RZ4 : 1;
  216. unsigned RZ5 : 1;
  217. unsigned RZ6 : 1;
  218. unsigned RZ7 : 1;
  219. };
  220. } __TRISZbits_t;
  221. typedef struct
  222. {
  223. unsigned ANSZ0 : 1;
  224. unsigned ANSZ1 : 1;
  225. unsigned ANSZ2 : 1;
  226. unsigned ANSZ3 : 1;
  227. unsigned ANSZ4 : 1;
  228. unsigned ANSZ5 : 1;
  229. unsigned ANSZ6 : 1;
  230. unsigned ANSZ7 : 1;
  231. } __ANSELZbits_t;
  232. #define _PORTZ_ADDR 0x0480
  233. #define _PORT_TO_LAT 0x09
  234. #define _PORT_TO_TRIS 0x12
  235. #define _PORT_TO_ANSEL (-0x48)
  236. extern __at(_PORTZ_ADDR) volatile unsigned char PORTZ;
  237. extern __at(_PORTZ_ADDR) volatile __PORTZbits_t PORTZbits;
  238. extern __at(_PORTZ_ADDR + _PORT_TO_LAT) volatile unsigned char LATZ;
  239. extern __at(_PORTZ_ADDR + _PORT_TO_LAT) volatile __LATZbits_t LATZbits;
  240. extern __at(_PORTZ_ADDR + _PORT_TO_TRIS) volatile unsigned char TRICZ;
  241. extern __at(_PORTZ_ADDR + _PORT_TO_TRIS) volatile __TRISZbits_t TRISZbits;
  242. extern __at(_PORTZ_ADDR + _PORT_TO_ANSEL) volatile unsigned char ANSELZ;
  243. extern __at(_PORTZ_ADDR + _PORT_TO_ANSEL) volatile __ANSELZbits_t ANSELZbits;
  244. //----------------------- fake port Z connectors ---------------
  245. #define KZ0PORT Z // PORTZ0 & LATZ0 are initialized to 0
  246. #define KZ0BIT 0
  247. #define KZ1PORT Z // PORTZ1 & LATZ1 are initialized to 1
  248. #define KZ1BIT 1
  249. #define KZ2PORT Z
  250. #define KZ2BIT 2
  251. #define KZ3PORT Z
  252. #define KZ3BIT 3
  253. #define KZ4PORT Z
  254. #define KZ4BIT 4
  255. #define KZ5PORT Z
  256. #define KZ5BIT 5
  257. #define KZ6PORT Z
  258. #define KZ6BIT 6
  259. #define KZ7PORT Z
  260. #define KZ7BIT 7
  261. /** @}
  262. */
  263. #endif //FRAISE_H