Keyboard firmwares for Atmel AVR and Cortex-M
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_SEMAPHORE.C
  5. * Purpose: Implements binary and counting semaphores
  6. * Rev.: V4.60
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. #include "rt_TypeDef.h"
  35. #include "RTX_Conf.h"
  36. #include "rt_System.h"
  37. #include "rt_List.h"
  38. #include "rt_Task.h"
  39. #include "rt_Semaphore.h"
  40. #include "rt_HAL_CM.h"
  41. /*----------------------------------------------------------------------------
  42. * Functions
  43. *---------------------------------------------------------------------------*/
  44. /*--------------------------- rt_sem_init -----------------------------------*/
  45. void rt_sem_init (OS_ID semaphore, U16 token_count) {
  46. /* Initialize a semaphore */
  47. P_SCB p_SCB = semaphore;
  48. p_SCB->cb_type = SCB;
  49. p_SCB->p_lnk = NULL;
  50. p_SCB->tokens = token_count;
  51. }
  52. /*--------------------------- rt_sem_delete ---------------------------------*/
  53. #ifdef __CMSIS_RTOS
  54. OS_RESULT rt_sem_delete (OS_ID semaphore) {
  55. /* Delete semaphore */
  56. P_SCB p_SCB = semaphore;
  57. P_TCB p_TCB;
  58. while (p_SCB->p_lnk != NULL) {
  59. /* A task is waiting for token */
  60. p_TCB = rt_get_first ((P_XCB)p_SCB);
  61. rt_ret_val(p_TCB, 0);
  62. rt_rmv_dly(p_TCB);
  63. p_TCB->state = READY;
  64. rt_put_prio (&os_rdy, p_TCB);
  65. }
  66. if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
  67. /* preempt running task */
  68. rt_put_prio (&os_rdy, os_tsk.run);
  69. os_tsk.run->state = READY;
  70. rt_dispatch (NULL);
  71. }
  72. p_SCB->cb_type = 0;
  73. return (OS_R_OK);
  74. }
  75. #endif
  76. /*--------------------------- rt_sem_send -----------------------------------*/
  77. OS_RESULT rt_sem_send (OS_ID semaphore) {
  78. /* Return a token to semaphore */
  79. P_SCB p_SCB = semaphore;
  80. P_TCB p_TCB;
  81. if (p_SCB->p_lnk != NULL) {
  82. /* A task is waiting for token */
  83. p_TCB = rt_get_first ((P_XCB)p_SCB);
  84. #ifdef __CMSIS_RTOS
  85. rt_ret_val(p_TCB, 1);
  86. #else
  87. rt_ret_val(p_TCB, OS_R_SEM);
  88. #endif
  89. rt_rmv_dly (p_TCB);
  90. rt_dispatch (p_TCB);
  91. }
  92. else {
  93. /* Store token. */
  94. p_SCB->tokens++;
  95. }
  96. return (OS_R_OK);
  97. }
  98. /*--------------------------- rt_sem_wait -----------------------------------*/
  99. OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) {
  100. /* Obtain a token; possibly wait for it */
  101. P_SCB p_SCB = semaphore;
  102. if (p_SCB->tokens) {
  103. p_SCB->tokens--;
  104. return (OS_R_OK);
  105. }
  106. /* No token available: wait for one */
  107. if (timeout == 0) {
  108. return (OS_R_TMO);
  109. }
  110. if (p_SCB->p_lnk != NULL) {
  111. rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
  112. }
  113. else {
  114. p_SCB->p_lnk = os_tsk.run;
  115. os_tsk.run->p_lnk = NULL;
  116. os_tsk.run->p_rlnk = (P_TCB)p_SCB;
  117. }
  118. rt_block(timeout, WAIT_SEM);
  119. return (OS_R_TMO);
  120. }
  121. /*--------------------------- isr_sem_send ----------------------------------*/
  122. void isr_sem_send (OS_ID semaphore) {
  123. /* Same function as "os_sem"send", but to be called by ISRs */
  124. P_SCB p_SCB = semaphore;
  125. rt_psq_enq (p_SCB, 0);
  126. rt_psh_req ();
  127. }
  128. /*--------------------------- rt_sem_psh ------------------------------------*/
  129. void rt_sem_psh (P_SCB p_CB) {
  130. /* Check if task has to be waken up */
  131. P_TCB p_TCB;
  132. if (p_CB->p_lnk != NULL) {
  133. /* A task is waiting for token */
  134. p_TCB = rt_get_first ((P_XCB)p_CB);
  135. rt_rmv_dly (p_TCB);
  136. p_TCB->state = READY;
  137. #ifdef __CMSIS_RTOS
  138. rt_ret_val(p_TCB, 1);
  139. #else
  140. rt_ret_val(p_TCB, OS_R_SEM);
  141. #endif
  142. rt_put_prio (&os_rdy, p_TCB);
  143. }
  144. else {
  145. /* Store token */
  146. p_CB->tokens++;
  147. }
  148. }
  149. /*----------------------------------------------------------------------------
  150. * end of file
  151. *---------------------------------------------------------------------------*/