1
0

uspi_glue.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include "devices/rpi2/raspi.h"
  5. #include <uspienv/timer.h>
  6. #include <uspienv/logger.h>
  7. #include <uspienv/interrupt.h>
  8. #include <uspienv/exceptionhandler.h>
  9. #include <uspienv/bcmpropertytags.h>
  10. typedef void TInterruptHandler (void *pParam);
  11. //#include "uspi/include/uspi/uspios.h"
  12. void format_string(char *fmt, va_list argptr, char *formatted_string);
  13. void LogWrite(const char *source, // short name of module
  14. unsigned severity, // see above
  15. const char *msg, ...) {
  16. //printf("[uspi-log] %s %d %s\r\n",source,severity,msg);
  17. va_list argptr;
  18. va_start(argptr,msg);
  19. vfprintf(stdout, msg, argptr);
  20. va_end(argptr);
  21. printf("\r\n");
  22. }
  23. TLogger *LoggerGet (void) {
  24. return NULL;
  25. }
  26. void LoggerWrite(TLogger *pThis, const char *pSource, TLogSeverity Severity, const char *pMessage, ...) {
  27. LogWrite(pSource, Severity, pMessage);
  28. }
  29. void uspi_assertion_failed() {
  30. }
  31. void InvalidateDataCache() {
  32. arm_invalidate_data_caches();
  33. }
  34. void CleanDataCache() {
  35. arm_clear_data_caches();
  36. }
  37. /*
  38. void MsDelay(unsigned int ms) {
  39. }
  40. void usDelay(unsigned int us) {
  41. }
  42. void StartKernelTimer() {
  43. }
  44. void CancelKernelTimer() {
  45. }
  46. int SetPowerStateOn() {
  47. return 1;
  48. }
  49. void ConnectInterrupt() {
  50. }*/
  51. void DebugHexdump() {
  52. printf("[uspi] DebugHexdump not implemented.\r\n");
  53. }
  54. void debug_stacktrace (const uint32_t *pStackPtr, const char *pSource)
  55. {
  56. for (unsigned i = 0; i < 64; i++, pStackPtr++)
  57. {
  58. extern unsigned char _bss_end;
  59. if (*pStackPtr >= MEM_KERNEL_START && *pStackPtr < (uint32_t)_bss_end)
  60. {
  61. printf("[trace] [%u] 0x%X\r\n", i, (unsigned) *pStackPtr);
  62. }
  63. }
  64. }
  65. int GetMACAddress(uint8_t buf[6]) {
  66. buf[0]=0xde;
  67. buf[1]=0xad;
  68. buf[2]=0xbe;
  69. buf[3]=0xef;
  70. buf[4]=0x02;
  71. buf[5]=0x42;
  72. return 1;
  73. }
  74. #define EnableInterrupts() __asm volatile ("cpsie i")
  75. #define DisableInterrupts() __asm volatile ("cpsid i")
  76. static volatile unsigned s_nCriticalLevel = 0;
  77. static volatile int s_bWereEnabled;
  78. void EnterCritical (void)
  79. {
  80. uint32_t nFlags;
  81. __asm volatile ("mrs %0, cpsr" : "=r" (nFlags));
  82. DisableInterrupts();
  83. if (s_nCriticalLevel++ == 0)
  84. {
  85. s_bWereEnabled = nFlags & 0x80 ? 0 : 1;
  86. }
  87. arm_dmb();
  88. }
  89. void LeaveCritical (void)
  90. {
  91. arm_dmb();
  92. //assert (s_nCriticalLevel > 0);
  93. if (--s_nCriticalLevel == 0)
  94. {
  95. if (s_bWereEnabled)
  96. {
  97. EnableInterrupts();
  98. }
  99. }
  100. }
  101. void MsDelay (unsigned nMilliSeconds)
  102. {
  103. TimerMsDelay (TimerGet (), nMilliSeconds);
  104. }
  105. void usDelay (unsigned nMicroSeconds)
  106. {
  107. TimerusDelay (TimerGet (), nMicroSeconds);
  108. }
  109. unsigned StartKernelTimer (unsigned nDelay, TKernelTimerHandler *pHandler, void *pParam, void *pContext)
  110. {
  111. return TimerStartKernelTimer (TimerGet (), nDelay, pHandler, pParam, pContext);
  112. }
  113. void CancelKernelTimer (unsigned hTimer)
  114. {
  115. TimerCancelKernelTimer (TimerGet (), hTimer);
  116. }
  117. void ConnectInterrupt (unsigned nIRQ, TInterruptHandler *pHandler, void *pParam)
  118. {
  119. InterruptSystemConnectIRQ (InterruptSystemGet (), nIRQ, pHandler, pParam);
  120. }
  121. int SetPowerStateOn (unsigned nDeviceId)
  122. {
  123. TBcmPropertyTags Tags;
  124. BcmPropertyTags (&Tags);
  125. TPropertyTagPowerState PowerState;
  126. PowerState.nDeviceId = nDeviceId;
  127. PowerState.nState = POWER_STATE_ON | POWER_STATE_WAIT;
  128. if ( !BcmPropertyTagsGetTag (&Tags, PROPTAG_SET_POWER_STATE, &PowerState, sizeof PowerState, 8)
  129. || (PowerState.nState & POWER_STATE_NO_DEVICE)
  130. || !(PowerState.nState & POWER_STATE_ON))
  131. {
  132. _BcmPropertyTags (&Tags);
  133. return 0;
  134. }
  135. _BcmPropertyTags (&Tags);
  136. return 1;
  137. }
  138. static TExceptionHandler uspi_exception_handler;
  139. static TInterruptSystem uspi_interrupts;
  140. static TTimer uspi_timer;
  141. void uspi_glue_init() {
  142. printf("uu ExceptionHandler2…\r\n");
  143. ExceptionHandler2(&uspi_exception_handler);
  144. printf("uu InterruptSystem…\r\n");
  145. InterruptSystem(&uspi_interrupts);
  146. printf("uu InterruptSystemInitialize…\r\n");
  147. InterruptSystemInitialize(&uspi_interrupts);
  148. printf("uu Timer…\r\n");
  149. Timer(&uspi_timer, &uspi_interrupts);
  150. printf("uu TimerInitialize…\r\n");
  151. TimerInitialize(&uspi_timer);
  152. }