core.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 2009-2014
  19. ********************************************************************/
  20. #include "core.h"
  21. //------------- time ---------------------------------------------
  22. volatile unsigned int TMR0U=0; // upper word of TMR0
  23. volatile DWORD Now;// time at last high interrupt
  24. //------------- eeprom -------------------------------------------
  25. void eeWriteByte(unsigned char address, unsigned char value){
  26. EEDATA = value;
  27. EEADR = address;
  28. // start write sequence as described in datasheet, page 91
  29. EECON1bits.EEPGD = 0;
  30. EECON1bits.CFGS = 0;
  31. EECON1bits.WREN = 1; // enable writes to data EEPROM
  32. INTCONbits.GIEH = 0; // disable interrupts
  33. INTCONbits.GIEL = 0; // disable interrupts
  34. EECON2 = 0x55;
  35. EECON2 = 0x0AA;
  36. EECON1bits.WR = 1; // start writing
  37. while(EECON1bits.WR){
  38. __asm nop __endasm;}
  39. if(EECON1bits.WRERR){
  40. //printf("ERROR: writing to EEPROM failed!\n");
  41. }
  42. EECON1bits.WREN = 0;
  43. INTCONbits.GIEH = 1; // enable interrupts
  44. INTCONbits.GIEL = 1; // enable interrupts
  45. }
  46. unsigned char eeReadByte(unsigned char address){
  47. EEADR = address;
  48. EECON1bits.CFGS = 0;
  49. EECON1bits.EEPGD = 0;
  50. EECON1bits.RD = 1;
  51. return EEDATA;
  52. }
  53. #if CONFIG_SETUP==1
  54. extern void Setup();
  55. #endif
  56. #ifndef FRAISE_NONE
  57. extern void fraiseISR();
  58. #endif
  59. void coreInit(void)
  60. {
  61. #if CONFIG_SETUP==1
  62. Setup();
  63. #endif
  64. // Fake port init :
  65. PORTZ = LATZ = 0;
  66. PORTZbits.RZ1 = LATZbits.LATZ1 = 1;
  67. // Master clock on Timer0 :
  68. T0CONbits.TMR0ON = 1;
  69. T0CONbits.T08BIT = 0; // 16 bit timer
  70. T0CONbits.T0CS = 0; // Use internal clock
  71. T0CONbits.T0SE = 1; // Hi to low
  72. T0CONbits.PSA = 0; // Use the prescaler
  73. T0CONbits.T0PS2 = 0; //
  74. T0CONbits.T0PS1 = 1; // 1/16 prescaler:
  75. T0CONbits.T0PS0 = 1; //
  76. INTCONbits.TMR0IE=1; // enable TMR0 interrupt
  77. INTCON2bits.TMR0IP=1; // high priority
  78. //T0CON=0b10010111;
  79. RCONbits.IPEN = 1; // enable interrupts priority mode
  80. INTCONbits.GIEH = 1; // enable interrupts
  81. INTCONbits.GIEL = 1; // enable interrupts
  82. // --------- init TMR2 as default PWM source -----------
  83. // fPWM = fOSC / (TMR2prescale * 4 * (PR2+1))
  84. // = 64000000Hz/(4 * 4 * 256) = 15.625 kHz
  85. T2CONbits.T2CKPS0=1; //prescaler 4
  86. PR2=255;
  87. T2CONbits.TMR2ON=1;
  88. }
  89. //--------------------- Main : -------------------------------
  90. void main() {
  91. #ifdef UD_SETUP
  92. setup();
  93. #endif
  94. #ifdef UD_LOOP
  95. while(1) loop();
  96. #endif
  97. }
  98. //--------------------- Interrupts : -------------------------------
  99. void high_ISR(void)
  100. //#ifdef SDCC
  101. __shadowregs __interrupt 1
  102. //#endif
  103. {
  104. Now.word1= TMR0U;
  105. LOWER_LSB(Now)=TMR0L;
  106. LOWER_MSB(Now)=TMR0H;
  107. if(INTCONbits.TMR0IF) {
  108. TMR0U++;
  109. INTCONbits.TMR0IF=0;
  110. Now.word1= TMR0U;
  111. LOWER_LSB(Now)=TMR0L;
  112. LOWER_MSB(Now)=TMR0H;
  113. }
  114. #ifdef UD_HIGH
  115. highInterrupts();
  116. #endif
  117. }
  118. void low_ISR(void)
  119. //#ifdef SDCC
  120. __interrupt 2
  121. //#endif
  122. {
  123. fraiseISR();
  124. #ifdef UD_LOW
  125. lowInterrupts();
  126. #endif
  127. }
  128. //------------------------- Time : ----------------
  129. unsigned unsigned long int time()
  130. {
  131. DWORD now;
  132. now.word1= TMR0U;
  133. LOWER_LSB(now)=TMR0L;
  134. LOWER_MSB(now)=TMR0H;
  135. if(now.word1 != TMR0U) { //a rollover occured : do read again
  136. now.word1= TMR0U;
  137. LOWER_LSB(now)=TMR0L;
  138. LOWER_MSB(now)=TMR0H;
  139. }
  140. return now._dword;
  141. }
  142. //------------- fake port Z -------------------------------------
  143. __at(_PORTZ_ADDR) volatile unsigned char PORTZ;
  144. __at(_PORTZ_ADDR) volatile __PORTZbits_t PORTZbits;
  145. __at(_PORTZ_ADDR + _PORT_TO_LAT) volatile unsigned char LATZ;
  146. __at(_PORTZ_ADDR + _PORT_TO_LAT) volatile __LATZbits_t LATZbits;
  147. __at(_PORTZ_ADDR + _PORT_TO_TRIS) volatile unsigned char TRICZ;
  148. __at(_PORTZ_ADDR + _PORT_TO_TRIS) volatile __TRISZbits_t TRISZbits;
  149. __at(_PORTZ_ADDR + _PORT_TO_ANSEL) volatile unsigned char ANSELZ;
  150. __at(_PORTZ_ADDR + _PORT_TO_ANSEL) volatile __ANSELZbits_t ANSELZbits;