hal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include <xc.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include "pic24f_hal.h"
  5. // CONFIG4
  6. #pragma config DSWDTPS = DSWDTPS1F // Deep Sleep Watchdog Timer Postscale Select bits (1:68719476736 (25.7 Days))
  7. #pragma config DSWDTOSC = LPRC // DSWDT Reference Clock Select (DSWDT uses LPRC as reference clock)
  8. #pragma config DSBOREN = OFF // Deep Sleep BOR Enable bit (DSBOR Disabled)
  9. #pragma config DSWDTEN = OFF // Deep Sleep Watchdog Timer Enable (DSWDT Disabled)
  10. #pragma config DSSWEN = ON // DSEN Bit Enable (Deep Sleep is controlled by the register bit DSEN)
  11. #pragma config PLLDIV = DISABLED // USB 96 MHz PLL Prescaler Select bits (PLL Disabled)
  12. #pragma config I2C1SEL = DISABLE // Alternate I2C1 enable bit (I2C1 uses SCL1 and SDA1 pins)
  13. #pragma config IOL1WAY = ON // PPS IOLOCK Set Only Once Enable bit (Once set, the IOLOCK bit cannot be cleared)
  14. // CONFIG3
  15. #pragma config WPFP = WPFP127 // Write Protection Flash Page Segment Boundary (Page 127 (0x1FC00))
  16. #pragma config SOSCSEL = ON // SOSC Selection bits (SOSC circuit selected)
  17. #pragma config WDTWIN = PS25_0 // Window Mode Watchdog Timer Window Width Select (Watch Dog Timer Window Width is 25 percent)
  18. #pragma config PLLSS = PLL_PRI // PLL Secondary Selection Configuration bit (PLL is fed by the Primary oscillator)
  19. #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset Enable)
  20. #pragma config WPDIS = WPDIS // Segment Write Protection Disable (Disabled)
  21. #pragma config WPCFG = WPCFGDIS // Write Protect Configuration Page Select (Disabled)
  22. #pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select (Write Protect from WPFP to the last page of memory)
  23. // CONFIG2
  24. #pragma config POSCMD = EC // Primary Oscillator Select (External-Clock Mode Enabled)
  25. #pragma config WDTCLK = LPRC // WDT Clock Source Select bits (WDT uses LPRC)
  26. #pragma config OSCIOFCN = OFF // OSCO Pin Configuration (OSCO/CLKO/RA3 functions as CLKO (FOSC/2))
  27. #pragma config FCKSM = CSECMD // Clock Switching and Fail-Safe Clock Monitor Configuration bits (Clock switching is enabled, Fail-Safe Clock Monitor is disabled)
  28. #pragma config FNOSC = PRI // Initial Oscillator Select (Primary Oscillator (XT, HS, EC))
  29. #pragma config ALTCMPI = CxINC_RB // Alternate Comparator Input bit (C1INC is on RB13, C2INC is on RB9 and C3INC is on RA0)
  30. #pragma config WDTCMX = WDTCLK // WDT Clock Source Select bits (WDT clock source is determined by the WDTCLK Configuration bits)
  31. #pragma config IESO = OFF // Internal External Switchover (Disabled)
  32. // CONFIG1
  33. #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler Select (1:32,768)
  34. #pragma config FWPSA = PR128 // WDT Prescaler Ratio Select (1:128)
  35. #pragma config WINDIS = OFF // Windowed WDT Disable (Standard Watchdog Timer)
  36. #pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT disabled in hardware; SWDTEN bit disabled)
  37. #pragma config ICS = PGx1 // Emulator Pin Placement Select bits (Emulator functions are shared with PGEC1/PGED1)
  38. #pragma config LPCFG = OFF // Low power regulator control (Disabled - regardless of RETEN)
  39. #pragma config GWRP = OFF // General Segment Write Protect (Write to program memory allowed)
  40. #pragma config GCP = OFF // General Segment Code Protect (Code protection is disabled)
  41. #pragma config JTAGEN = OFF // JTAG Port Enable (Disabled)
  42. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _OscillatorFail ( void ) ;
  43. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _AddressError ( void ) ;
  44. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _StackError ( void ) ;
  45. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _MathError ( void ) ;
  46. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _AltOscillatorFail ( void ) ;
  47. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _AltAddressError ( void ) ;
  48. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _AltStackError ( void ) ;
  49. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _AltMathError ( void ) ;
  50. /* Init hardware */
  51. void platform_init(void)
  52. {
  53. /* The following code block shows an example of swtiching oscillator source, but we use fuse bits
  54. to come up in external oscillator mode. So it's not needed to switch modes
  55. */
  56. #if 0
  57. /* Check clock lock bit */
  58. if (OSCCONL & 0x80){
  59. //If system gets here, the fuse bits are set wrong as cannot change oscillator
  60. while(1);
  61. }
  62. char a , b , c , *p ;
  63. a = 0x02; //New source: external clock
  64. b = 0x78 ; /* Unlock sequence for high, 78, 9A */
  65. c = 0x9A ;
  66. p = (char *) &OSCCONH ;
  67. asm volatile ("mov.b %1,[%0] \n"
  68. "mov.b %2,[%0] \n"
  69. "mov.b %3,[%0] \n" : /* no outputs */ : "r"( p ) , "r"( b ) , "r"( c ) ,
  70. "r"( a ) ) ;
  71. a = 1 ; /* Switch oscillator bit */
  72. b = 0x46 ; /* Unlock sequence for low, 46, 57 */
  73. c = 0x57 ;
  74. p = (char *) &OSCCONL ;
  75. asm volatile ("mov.b %1,[%0] \n"
  76. "mov.b %2,[%0] \n"
  77. "mov.b %3,[%0] \n" : /* no outputs */ : "r"( p ) , "r"( b ) , "r"( c ) ,
  78. "r"( a ) ) ;
  79. #endif
  80. return;
  81. }
  82. #if HWCRYPTO
  83. /* AES Hardware Peripheral */
  84. void HW_AES128_Init(void)
  85. {
  86. //TODO: Following assumes reset state - possibly should write all bits instead
  87. CRYCONLbits.CRYON = 1;
  88. CRYCONLbits.OPMOD = 0x0; // Encrypt
  89. CRYCONLbits.CPHRSEL = 1;
  90. CRYCONLbits.CPHRMOD = 0;
  91. CRYCONHbits.KEYSRC = 0; //Key source - this is just SRAM
  92. }
  93. void HW_AES128_LoadKey(uint8_t * key)
  94. {
  95. //Load the key into CRYKEY
  96. memcpy((void *)&CRYKEY0, key, 16);
  97. }
  98. void HW_AES128_Enc_pretrigger(uint8_t * pt)
  99. {
  100. memcpy((void*)&CRYTXTA0, pt, 16);
  101. }
  102. void HW_AES128_Enc(uint8_t * pt)
  103. {
  104. CRYCONLbits.CRYGO = 1;
  105. // Wait for completion
  106. while (CRYCONLbits.CRYGO == 1)
  107. }
  108. void HW_AES128_Enc_posttrigger(uint8_t * pt)
  109. {
  110. memcpy(pt, (void*)&CRYTXTB0, 16);
  111. }
  112. #endif
  113. // *****************************************************************************
  114. // *****************************************************************************
  115. // Section: Primary Exception Vector handlers
  116. // *****************************************************************************
  117. // *****************************************************************************
  118. // *****************************************************************************
  119. /* void __attribute__((__interrupt__,auto_psv)) _OscillatorFail(void)
  120. Summary:
  121. Provides the required exception vector handlers for Oscillator trap
  122. Description:
  123. This routine is used if INTCON2bits.ALTIVT = 0 and it handles the oscillator
  124. trap.
  125. Remarks:
  126. All trap service routines in this file simply ensure that device
  127. continuously executes code within the trap service routine. Users
  128. may modify the basic framework provided here to suit to the needs
  129. of their application.
  130. */
  131. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _OscillatorFail ( void )
  132. {
  133. INTCON1bits.OSCFAIL = 0 ; //Clear the trap flag
  134. while (1) ;
  135. }
  136. // *****************************************************************************
  137. /* void __attribute__((__interrupt__,auto_psv)) _AddressError(void)
  138. Summary:
  139. Provides the required exception vector handlers for Address Error trap
  140. Description:
  141. This routine is used if INTCON2bits.ALTIVT = 0 and it handles the address
  142. error trap.
  143. Remarks:
  144. All trap service routines in this file simply ensure that device
  145. continuously executes code within the trap service routine. Users
  146. may modify the basic framework provided here to suit to the needs
  147. of their application.
  148. */
  149. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _AddressError ( void )
  150. {
  151. INTCON1bits.ADDRERR = 0 ; //Clear the trap flag
  152. while (1) ;
  153. }
  154. // *****************************************************************************
  155. /* void __attribute__((__interrupt__,auto_psv)) _StackError(void))
  156. Summary:
  157. Provides the required exception vector handlers for Stack Error trap
  158. Description:
  159. This routine is used if INTCON2bits.ALTIVT = 0 and it handles the stack
  160. error trap.
  161. Remarks:
  162. All trap service routines in this file simply ensure that device
  163. continuously executes code within the trap service routine. Users
  164. may modify the basic framework provided here to suit to the needs
  165. of their application.
  166. */
  167. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _StackError ( void )
  168. {
  169. INTCON1bits.STKERR = 0 ; //Clear the trap flag
  170. while (1) ;
  171. }
  172. // *****************************************************************************
  173. /* void __attribute__((__interrupt__,auto_psv)) _MathError(void))
  174. Summary:
  175. Provides the required exception vector handlers for Math Error trap
  176. Description:
  177. This routine is used if INTCON2bits.ALTIVT = 0 and it handles the math
  178. error trap.
  179. Remarks:
  180. All trap service routines in this file simply ensure that device
  181. continuously executes code within the trap service routine. Users
  182. may modify the basic framework provided here to suit to the needs
  183. of their application.
  184. */
  185. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _MathError ( void )
  186. {
  187. INTCON1bits.MATHERR = 0 ; //Clear the trap flag
  188. while (1) ;
  189. }
  190. // *****************************************************************************
  191. // *****************************************************************************
  192. // Section: Alternate Exception Vector handlers
  193. // *****************************************************************************
  194. // *****************************************************************************
  195. // *****************************************************************************
  196. /* void __attribute__((__interrupt__,auto_psv)) _AltOscillatorFail(void)
  197. Summary:
  198. Provides the required exception vector handlers for Oscillator trap
  199. Description:
  200. This routine is used if INTCON2bits.ALTIVT = 1 and it handles the oscillator
  201. trap.
  202. Remarks:
  203. All trap service routines in this file simply ensure that device
  204. continuously executes code within the trap service routine. Users
  205. may modify the basic framework provided here to suit to the needs
  206. of their application.
  207. */
  208. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _AltOscillatorFail ( void )
  209. {
  210. INTCON1bits.OSCFAIL = 0 ;
  211. while (1) ;
  212. }
  213. // *****************************************************************************
  214. /* void __attribute__((__interrupt__,auto_psv)) _AltAddressError(void)
  215. Summary:
  216. Provides the required exception vector handlers for Address Error trap
  217. Description:
  218. This routine is used if INTCON2bits.ALTIVT = 1 and it handles the address
  219. error trap.
  220. Remarks:
  221. All trap service routines in this file simply ensure that device
  222. continuously executes code within the trap service routine. Users
  223. may modify the basic framework provided here to suit to the needs
  224. of their application.
  225. */
  226. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _AltAddressError ( void )
  227. {
  228. INTCON1bits.ADDRERR = 0 ;
  229. while (1) ;
  230. }
  231. // *****************************************************************************
  232. /* void __attribute__((__interrupt__,auto_psv)) _AltStackError(void))
  233. Summary:
  234. Provides the required exception vector handlers for Stack Error trap
  235. Description:
  236. This routine is used if INTCON2bits.ALTIVT = 1 and it handles the stack
  237. error trap.
  238. Remarks:
  239. All trap service routines in this file simply ensure that device
  240. continuously executes code within the trap service routine. Users
  241. may modify the basic framework provided here to suit to the needs
  242. of their application.
  243. */
  244. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _AltStackError ( void )
  245. {
  246. INTCON1bits.STKERR = 0 ;
  247. while (1) ;
  248. }
  249. // *****************************************************************************
  250. /* void __attribute__((__interrupt__,auto_psv)) _AltMathError(void))
  251. Summary:
  252. Provides the required exception vector handlers for Math Error trap
  253. Description:
  254. This routine is used if INTCON2bits.ALTIVT = 1 and it handles the math
  255. error trap.
  256. Remarks:
  257. All trap service routines in this file simply ensure that device
  258. continuously executes code within the trap service routine. Users
  259. may modify the basic framework provided here to suit to the needs
  260. of their application.
  261. */
  262. void __attribute__ ( ( __interrupt__ , auto_psv ) ) _AltMathError ( void )
  263. {
  264. INTCON1bits.MATHERR = 0 ;
  265. while (1) ;
  266. }