SSL_Demo.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*******************************************************************************
  2. Class B Library Implementation Test File
  3. Summary:
  4. This file contains the test program for the
  5. the Class B Safety Software Library on PIC32MX MCUs.
  6. *******************************************************************************/
  7. /*******************************************************************************
  8. FileName: ClassB_main.c
  9. Processor: PIC32MX
  10. Compiler: Microchip MPLAB® C32 v1.04 or higher
  11. Copyright © 2008-2009 released Microchip Technology Inc. All rights reserved.
  12. Microchip licenses to you the right to use, modify, copy and distribute
  13. the accompanying software only when embedded on a Microchip microcontroller or
  14. digital signal controller that is integrated into your product or third party product.
  15. If the accompanying software required your consent to the terms of Microchip's
  16. click-wrap license agreement, then you should also refer to such license agreement
  17. for additional information regarding your rights and obligations.
  18. Your acceptance and/or use of this software constitutes your agreement to the terms
  19. and conditions of this notice and applicable click-wrap license, if any.
  20. You agree that you are solely responsible for testing the code and determining its suitability.
  21. Microchip has no obligation to modify, test, certify, or support the code.
  22. SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND,
  23. EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  24. MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
  25. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
  26. CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
  27. OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  28. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
  29. CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST dataC, COST OF PROCUREMENT OF
  30. SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  31. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  32. *******************************************************************************/
  33. /*********************************************************************
  34. *
  35. * Include Files
  36. *
  37. ********************************************************************/
  38. #include <stdlib.h>
  39. #include <P18F45K80.h>
  40. #include "SSL_CpuRegisterTest.h"
  41. #include "SSL_CheckerBoardTest.h"
  42. #include "SSL_ProgCounterTest.h"
  43. #include "SSL_MarchCMinusRamTest.h"
  44. #include "SSL_MarchBRamTest.h"
  45. #include "SSL_MarchCRamTest.h"
  46. #include "SSL_Flash_CRC.h"
  47. /*********************************************************************
  48. *
  49. * Pragmas
  50. *
  51. *********************************************************************/
  52. #pragma config XINST = OFF
  53. #pragma config FOSC = HS1
  54. #pragma config WDTEN = SWDTDIS //OFF // SWDTDIS
  55. #pragma config WDTPS = 256 // WD TimeOut 1024ms
  56. #pragma config SOSCSEL = DIG // Port C, Pin 0 & 1 => Digital
  57. #pragma config PLLCFG = ON
  58. /*********************************************************************
  59. *
  60. * Typdefs
  61. *
  62. *********************************************************************/
  63. volatile struct ClassB_Test_Flags {
  64. unsigned cpuRegister_TestResult : 1;
  65. unsigned programCounter_TestResult:1;
  66. unsigned checkerboardRam_TestResult:1;
  67. unsigned marchCRam_TestResult:1;
  68. unsigned marchCMinusRam_TestResult:1;
  69. unsigned marchCRamStack_TestResult:1;
  70. unsigned marchBRam_TestResult:1;
  71. unsigned flash_TestResult:1;
  72. unsigned clock_TestResult:1;
  73. unsigned clockLine_TestResult:1;
  74. } testFlag;
  75. /*+++++++ Main SSL Demo +++++++++++++++++++++++++++++++++++++++++++*/
  76. int main(void)
  77. {
  78. /* ini()? */
  79. /**********************************************************************************/
  80. /* CPU REGISTER TEST */
  81. /**********************************************************************************/
  82. if (SSL_PCtest()==CPU_REGISTER_TEST_PASS)
  83. {
  84. // the CPU registers don't have stuck bits
  85. testFlag.cpuRegister_TestResult = 1;
  86. }
  87. else
  88. {
  89. testFlag.cpuRegister_TestResult = 0;
  90. }
  91. /**********************************************************************************/
  92. /* PROGRAM COUNTER TEST */
  93. /* This requires a special linker script (elf32pic32mx.ld) to be added */
  94. /* as part of the project. See the description in SSL_PcTest.h */
  95. /**********************************************************************************/
  96. if (SSL_PCtest()==PC_TEST_PASS)
  97. {
  98. // the PC register does not have stuck bits
  99. testFlag.programCounter_TestResult = 1;
  100. }
  101. else
  102. {
  103. testFlag.programCounter_TestResult = 0;
  104. }
  105. /**********************************************************************************/
  106. /* RAM TESTS */
  107. /**********************************************************************************/
  108. /*************************/
  109. /* Checker Board RAM test*/
  110. /*************************/
  111. // We'll test 1KB chunk at the middle of the RAM
  112. // Note that this test is not destructive
  113. // It has to NOT overlap the stack space!
  114. if (SSL_RAMtest_CheckerBoard( )==CB_TEST_PASS)
  115. {
  116. // the test succeeded and we are confident that the RAM area can be used
  117. testFlag.checkerboardRam_TestResult = 1;
  118. }
  119. else
  120. {
  121. testFlag.checkerboardRam_TestResult = 0;
  122. }
  123. /****************************/
  124. /* March B Ram Test */
  125. /****************************/
  126. // Suppose we need to allocate and want to use a 1 KB chunk of memory
  127. // We'll test it using the Checker Board test before we use it
  128. // to make sure there is no problem with the chunk of RAM we selected.
  129. // Note that this test is destructive.
  130. // Note that the size of the RAM to test has to be multiple of 4.
  131. { // allocation succeeded and we can perform the test
  132. if (SSL_RAMtest_MarchB() ==MARCHB_TEST_PASS)
  133. {
  134. // the test succeeded and we can go ahead and use the allocated memory
  135. testFlag.marchBRam_TestResult = 1;
  136. }
  137. else
  138. {
  139. testFlag.marchBRam_TestResult = 0;
  140. }
  141. //free(ramTestStartAddress); // we're done with this memory block
  142. // return it to the heap
  143. }
  144. /*************************/
  145. /* MarchC RAM tests */
  146. /*************************/
  147. // We allocate and want to use a 2 KB chunk of memory
  148. // We'll test it using the March C and March C Minus tests before we use it
  149. // Note that the size of the RAM to test has to be multiple of 4.
  150. // This test is destructive too.
  151. if (SSL_RAMtest_MarchC() == MARCHC_TEST_PASS)
  152. {
  153. testFlag.marchCRam_TestResult = 1;
  154. }
  155. else
  156. {
  157. testFlag.marchCRam_TestResult = 0;
  158. }
  159. /**************************/
  160. /* MarchC Minus RAM test */
  161. /**************************/
  162. if (SSL_RAMtest_MarchCMinus() == MARCHC_TEST_PASS)
  163. {
  164. // if both tests succeeded we can go ahead and use the allocated memory
  165. testFlag.marchCMinusRam_TestResult = 1;
  166. }
  167. else
  168. {
  169. testFlag.marchCMinusRam_TestResult = 0;
  170. }
  171. /**********************************************************************************/
  172. /* FLASH CRC TEST */
  173. /**********************************************************************************/
  174. // This function can be called at startup to generate the Reference checksum.
  175. // The same function can be called periodically and the generated checksum can be
  176. // compared with the reference checksum.
  177. // If both are the same the "flash_TestResult" status bit can be set.
  178. unsigned int flashCrcRef, flashCrc; // reference and current CRC values
  179. unsigned int crcSeed=0xffff; // initial CRC register value
  180. // this is the recommended CRC seed
  181. // for checking properly long 0 streams.
  182. // calculate the CRC16 of the whole program flash (K0)
  183. char* flashStartAddress = (char*)0x9d000000; // fixed start K0 address on PIC32MX devices
  184. //char* flashEndAddress = flashStartAddress+BMXPFMSZ-1; // size of the flash on this device
  185. // the BMX register stores the Flash size for this part
  186. // first we calculate the reference Flash CRC value
  187. flashCrcRef = SSL_FLASHtest_CRC( );
  188. // at some time later we calculate again the CRC of the Flash
  189. flashCrc = SSL_FLASHtest_CRC( );
  190. // make sure that the periodic check is equal to the reference one
  191. if ( flashCrc==flashCrcRef)
  192. {
  193. testFlag.flash_TestResult=1;
  194. // we are confident that the data programmed in Flash
  195. // has not been altered in any way
  196. }
  197. else
  198. {
  199. testFlag.flash_TestResult=0;
  200. }
  201. }