board_lowlevel.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "FreeRTOS.h"
  2. #include "board.h"
  3. #include "sysinfo.h"
  4. #include "chip.h"
  5. static uint32_t chip_hversion = CHIP_HVER_NONE;
  6. static const char* abort_status[][2]=
  7. {
  8. // IFSR status , DFSR status
  9. {"Unknown(reserved status)", "Unknown(reserved status)" },//0
  10. {"Unknown(reserved status)", "Alignment Fault" },//1
  11. {"Debug Event", "Debug Event" },//2
  12. {"Access flag - section", "Access flag - section" },//3
  13. {"Unknown(reserved status)", "Instruction cache maintenance" },//4
  14. {"Translation fault - section", "Translation fault - section" },//5
  15. {"Access flag - Page", "Access flag - Page" },//6
  16. {"Translation fault -Page", "Translation fault -Page" },//7
  17. {"Synchronous external abort", "Synchronous external abort, nontranslation" },//8
  18. {"Domain fault - Section", "Domain fault - Section" },//9
  19. {"Unknown(reserved status)", "Unknown(reserved status)" },//10
  20. {"Domain fault - Page", "Domain fault - Page" },//11
  21. {"Synchronous external abort - L1 Translation", "Synchronous external abort - L1 Translation" },//12
  22. {"Permission fault - Section", "Permission fault - Section" },//13
  23. {"Synchronous external abort - L2 Translation", "Synchronous external abort - L2 Translation" },//14
  24. {"Permission fault - Page", "Permission fault - Page" },//15
  25. {"Unknown(reserved status)", "Unknown(reserved status)" },//16
  26. {"Unknown(reserved status)", "Unknown(reserved status)" },//17
  27. {"Unknown(reserved status)", "Unknown(reserved status)" },//18
  28. {"Unknown(reserved status)", "Unknown(reserved status)" },//19
  29. {"Unknown(reserved status)", "Unknown(reserved status)" },//20
  30. {"Unknown(reserved status)", "Unknown(reserved status)" },//21
  31. {"Unknown(reserved status)", "Asynchronous external abort"}
  32. };
  33. void LowLevelInit( void )
  34. {
  35. }
  36. void Abort_C_Handler( void)
  37. {
  38. uint32_t v1,v2, dfsr, lr;
  39. v1= 0;
  40. v2= 0;
  41. lr = 0;
  42. asm("mov %0,lr" : : "r"(lr));
  43. printf("abort lr=0x%x.\n", lr);
  44. asm("mrc p15, 0, %0, c5, c0, 0" : : "r"(v1));
  45. asm("mrc p15, 0, %0, c6, c0, 0" : : "r"(v2));
  46. dfsr = ((v1 >> 4) & 0x0F);
  47. printf("\n\r######################################################################\n\r");
  48. printf("Data Abort occured in %x domain\n\r", (unsigned int)dfsr);
  49. dfsr = (((v1 & 0x400) >> 6) | (v1 & 0x0F));
  50. printf("Data abort fault reason is: %s\n\r", (char*)abort_status[dfsr][1]);
  51. printf("Data fault occured at Address = 0x%08x\n\n\r",(unsigned int)v2);
  52. printf("-[Info]-Data fault status register value = 0x%x\n\r",(unsigned int)v1);
  53. while(1);
  54. }
  55. void Prefetch_C_Handler( void)
  56. {
  57. uint32_t v1,v2, ifsr;
  58. v1= 0;
  59. v2= 0;
  60. asm("mrc p15, 0, %0, c5, c0, 1" : : "r"(v1));
  61. asm("mrc p15, 0, %0, c6, c0, 2" : : "r"(v2));
  62. ifsr = (((v1 & 0x400) >> 6) | (v1 & 0x0F));
  63. printf("\n\r######################################################################\n\r");
  64. printf("Instruction prefetch abort reason is: %s\n\r", (char*)abort_status[ifsr][0]);
  65. printf("Instruction prefetch Fault occured at Address = 0x%08x\n\n\r",(unsigned int)v2);
  66. printf("-[INFO]- Prefetch Fault status register value by = 0x%x\n\r",(unsigned int)v1);
  67. while(1);
  68. }
  69. void Undefined_C_Handler( void)
  70. {
  71. printf("Undefined abort \n\r");
  72. while(1);
  73. }
  74. /*-----------------------------------------------------------*/
  75. void vApplicationMallocFailedHook( size_t size )
  76. {
  77. /* Called if a call to pvPortMalloc() fails because there is insufficient
  78. free memory available in the FreeRTOS heap. pvPortMalloc() is called
  79. internally by FreeRTOS API functions that create tasks, queues, software
  80. timers, and semaphores. The size of the FreeRTOS heap is set by the
  81. configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
  82. HeapStats_t heapStat;
  83. vPortGetHeapStats(&heapStat);
  84. printf("ERROR! Malloc %d bytes fail.\n", size);
  85. printf("xAvailableHeapSpaceInBytes %d.\n", heapStat.xAvailableHeapSpaceInBytes);
  86. printf("xSizeOfLargestFreeBlockInBytes %d.\n", heapStat.xSizeOfLargestFreeBlockInBytes);
  87. configASSERT( ( volatile void * ) NULL );
  88. }
  89. /*-----------------------------------------------------------*/
  90. void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
  91. {
  92. ( void ) pcTaskName;
  93. ( void ) pxTask;
  94. /* Run time stack overflow checking is performed if
  95. configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
  96. function is called if a stack overflow is detected. */
  97. printf("ERROR! task %s stack over flow.\n", pcTaskName);
  98. /* Force an assert. */
  99. configASSERT( ( volatile void * ) NULL );
  100. }
  101. /*-----------------------------------------------------------*/
  102. void vApplicationIdleHook( void )
  103. {
  104. static uint32_t idletick = 0;
  105. volatile size_t xFreeHeapSpace;
  106. /* This is just a trivial example of an idle hook. It is called on each
  107. cycle of the idle task. It must *NOT* attempt to block. In this case the
  108. idle task just queries the amount of FreeRTOS heap that remains. See the
  109. memory management section on the http://www.FreeRTOS.org web site for memory
  110. management options. If there is a lot of heap memory free then the
  111. configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
  112. RAM. */
  113. xFreeHeapSpace = xPortGetFreeHeapSize();
  114. /* Remove compiler warning about xFreeHeapSpace being set but never used. */
  115. ( void ) xFreeHeapSpace;
  116. if (xTaskGetTickCount() - idletick > configTICK_RATE_HZ * 10) {
  117. printf("Memory free: %d bytes.\n", xFreeHeapSpace);
  118. idletick = xTaskGetTickCount();
  119. }
  120. }
  121. /*-----------------------------------------------------------*/
  122. void vAssertCalled( const char * pcFile, unsigned long ulLine )
  123. {
  124. volatile unsigned long ul = 0;
  125. ( void ) pcFile;
  126. ( void ) ulLine;
  127. printf("crash %s : %d", pcFile, ulLine);
  128. taskENTER_CRITICAL();
  129. {
  130. /* Set ul to a non-zero value using the debugger to step out of this
  131. function. */
  132. while( ul == 0 )
  133. {
  134. portNOP();
  135. }
  136. }
  137. taskEXIT_CRITICAL();
  138. }
  139. /*-----------------------------------------------------------*/
  140. void vApplicationTickHook( void )
  141. {
  142. }
  143. /*-----------------------------------------------------------*/
  144. /* The function called by the RTOS port layer after it has managed interrupt
  145. entry. */
  146. void vApplicationIRQHandler( void )
  147. {
  148. GIC_IrqHandler();
  149. __DSB();
  150. __ISB();
  151. }
  152. /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
  153. implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
  154. used by the Idle task. */
  155. void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
  156. {
  157. /* If the buffers to be provided to the Idle task are declared inside this
  158. function then they must be declared static - otherwise they will be allocated on
  159. the stack and so not exists after this function exits. */
  160. static StaticTask_t xIdleTaskTCB;
  161. static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
  162. /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
  163. state will be stored. */
  164. *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
  165. /* Pass out the array that will be used as the Idle task's stack. */
  166. *ppxIdleTaskStackBuffer = uxIdleTaskStack;
  167. /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
  168. Note that, as the array is necessarily of type StackType_t,
  169. configMINIMAL_STACK_SIZE is specified in words, not bytes. */
  170. *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  171. }
  172. /*-----------------------------------------------------------*/
  173. /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
  174. application must provide an implementation of vApplicationGetTimerTaskMemory()
  175. to provide the memory that is used by the Timer service task. */
  176. void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
  177. {
  178. /* If the buffers to be provided to the Timer task are declared inside this
  179. function then they must be declared static - otherwise they will be allocated on
  180. the stack and so not exists after this function exits. */
  181. static StaticTask_t xTimerTaskTCB;
  182. static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
  183. /* Pass out a pointer to the StaticTask_t structure in which the Timer
  184. task's state will be stored. */
  185. *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
  186. /* Pass out the array that will be used as the Timer task's stack. */
  187. *ppxTimerTaskStackBuffer = uxTimerTaskStack;
  188. /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
  189. Note that, as the array is necessarily of type StackType_t,
  190. configMINIMAL_STACK_SIZE is specified in words, not bytes. */
  191. *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
  192. }
  193. uint32_t GetChipHWVeriosn(void)
  194. {
  195. ChipInfo *info = (ChipInfo *)(0x8000 - sizeof(ChipInfo));
  196. if (chip_hversion != CHIP_HVER_NONE)
  197. return chip_hversion;
  198. chip_hversion = CHIP_HVER_0; //default chip version is CHIP_HVER_0.
  199. if (info->crc == xcrc32((void*)info, sizeof(ChipInfo) - 4, 0xffffffff, HARD_CALC_CRC)) {
  200. if (info->hver >= CHIP_HVER_COUNT) {
  201. printf("Invalie chip version info.\n");
  202. return chip_hversion;
  203. }
  204. chip_hversion = info->hver;
  205. return chip_hversion;
  206. }
  207. printf("chip version info invalid.\n");
  208. return chip_hversion;
  209. }