csect.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "core/config.h" // DEBUG
  2. #include <intrinsics.h>
  3. #include "core/csect.h"
  4. #pragma pack( push, 1 )
  5. typedef struct __sCRITICALSECTION_int_nc
  6. {
  7. volatile unsigned long xStdLock_int_reg;
  8. union
  9. {
  10. uint16_t reserved;
  11. struct
  12. {
  13. uint8_t bInitialized;
  14. uint8_t bLocked;
  15. };
  16. };
  17. }
  18. sCSect_t;
  19. typedef struct __sCRITICALSECTION_int_c
  20. {
  21. volatile int32_t xStdLock_int_reg;
  22. union
  23. {
  24. uint16_t reserved;
  25. struct
  26. {
  27. #if CSECT_IMPLEMENTATION_SIMPLE == 0
  28. uint8_t bInitialized;
  29. #endif
  30. uint8_t nCounter;
  31. };
  32. };
  33. }
  34. sCSectCnt_t;
  35. #pragma pack( pop )
  36. #if CSECT_IMPLEMENTATION_SIMPLE
  37. static sCSectCnt_t sys_csect = {
  38. .nCounter = 0,
  39. .xStdLock_int_reg = 0
  40. };
  41. #endif
  42. #if CSECT_IMPLEMENTATION_SIMPLE == 0
  43. void csect_create( xCSect_t * pSectDescriptor )
  44. {
  45. sCSect_t * p = ((sCSect_t*)(pSectDescriptor));
  46. if( p != NULL )
  47. {
  48. p->bInitialized = (uint8_t)(~0);
  49. p->bLocked = (uint8_t)(0);
  50. p->xStdLock_int_reg = 0;
  51. }
  52. }
  53. void csect_delete( xCSect_t * pSectDescriptor )
  54. {
  55. sCSect_t * p = ((sCSect_t*)(pSectDescriptor));
  56. if( p != NULL )
  57. {
  58. p->bInitialized = (uint8_t)(0);
  59. p->bLocked = (uint8_t)(0);
  60. p->xStdLock_int_reg = 0;
  61. }
  62. }
  63. void csect_enter( xCSect_t * pSectDescriptor )
  64. {
  65. sCSect_t * p = ((sCSect_t*)(pSectDescriptor));
  66. if( p != NULL && (p->bInitialized != 0) )
  67. {
  68. if( p->bLocked == 0 )
  69. {
  70. p->xStdLock_int_reg = __get_interrupt_state();
  71. __disable_interrupt();
  72. p->bLocked = (uint8_t)(~0);
  73. }
  74. }
  75. }
  76. void csect_leave( xCSect_t * pSectDescriptor )
  77. {
  78. sCSect_t * p = ((sCSect_t*)(pSectDescriptor));
  79. if( p != NULL && (p->bInitialized != 0) )
  80. {
  81. if( p->bLocked != 0 )
  82. {
  83. p->bLocked = 0;
  84. __set_interrupt_state( p->xStdLock_int_reg );
  85. }
  86. }
  87. }
  88. void csectcnt_create( xCSectCnt_t * pSectDescriptor )
  89. {
  90. sCSectCnt_t * p = ((sCSectCnt_t*)(pSectDescriptor));
  91. if( p != NULL )
  92. {
  93. p->bInitialized = (uint8_t)(~0);
  94. p->nCounter = 0;
  95. p->xStdLock_int_reg = 0;
  96. }
  97. }
  98. void csectcnt_delete( xCSectCnt_t * pSectDescriptor )
  99. {
  100. sCSectCnt_t * p = ((sCSectCnt_t*)(pSectDescriptor));
  101. if( p != NULL )
  102. {
  103. p->bInitialized = (uint8_t)(0);
  104. p->nCounter = 0;
  105. p->xStdLock_int_reg = 0;
  106. }
  107. }
  108. #endif
  109. #if CSECT_IMPLEMENTATION_SIMPLE == 1
  110. static void csectcnt_enter()
  111. #else
  112. void csectcnt_enter( xCSectCnt_t * pSectDescriptor )
  113. #endif
  114. {
  115. #if CSECT_IMPLEMENTATION_SIMPLE == 1
  116. sCSectCnt_t * p = &sys_csect;
  117. #else
  118. sCSectCnt_t * p = ((sCSectCnt_t*)(pSectDescriptor));
  119. #endif
  120. #if CSECT_IMPLEMENTATION_SIMPLE == 0
  121. if( p != NULL && (p->bInitialized != 0) )
  122. #endif
  123. {
  124. if( p->nCounter == 0 )
  125. {
  126. p->xStdLock_int_reg = __get_interrupt_state();
  127. __disable_interrupt();
  128. #if CONFIG_CSECT_DEBUG
  129. CONFIG_CSECT_DEBUG_SETPIN
  130. #endif
  131. p->nCounter++;
  132. }
  133. else if( p->nCounter < 255 )
  134. {
  135. p->nCounter++;
  136. }
  137. }
  138. }
  139. #if CSECT_IMPLEMENTATION_SIMPLE == 1
  140. static void csectcnt_leave()
  141. #else
  142. void csectcnt_leave( xCSectCnt_t * pSectDescriptor )
  143. #endif
  144. {
  145. #if CSECT_IMPLEMENTATION_SIMPLE == 1
  146. sCSectCnt_t * p = &sys_csect;
  147. #else
  148. sCSectCnt_t * p = ((sCSectCnt_t*)(pSectDescriptor));
  149. #endif
  150. #if CSECT_IMPLEMENTATION_SIMPLE == 0
  151. if( p != NULL && (p->bInitialized != 0) )
  152. #endif
  153. {
  154. if( p->nCounter > 1 )
  155. {
  156. p->nCounter--;
  157. }
  158. else if( p->nCounter == 1 )
  159. {
  160. p->nCounter--;
  161. #if CONFIG_CSECT_DEBUG
  162. CONFIG_CSECT_DEBUG_CLRPIN
  163. #endif
  164. __set_interrupt_state( p->xStdLock_int_reg );
  165. }
  166. }
  167. }
  168. #if CSECT_IMPLEMENTATION_SIMPLE == 1
  169. void csect_enter()
  170. {
  171. csectcnt_enter();
  172. }
  173. void csect_leave()
  174. {
  175. csectcnt_leave();
  176. }
  177. #endif