usb_osal_rtx.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @file usb_osal_rtx.c
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #include "usb_osal.h"
  24. #include "usb_errno.h"
  25. #include "stdlib.h"
  26. #include "RTL.h"
  27. uint32_t enter_critical(void);
  28. uint32_t exit_critical(uint32_t sr);
  29. //延时=timeout/os_tick, os tick设置为1ms,延时=timeout
  30. //移植为arm9,其他版本可能需要调整汇编代码
  31. //按最大端点数量设置,这里设为8
  32. _declare_box8(sem_mpool, sizeof(OS_SEM), 8);
  33. _declare_box8(mut_mpool, sizeof(OS_MUT), 8);
  34. usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size, uint32_t prio, usb_thread_entry_t entry, void *args)
  35. {
  36. void *stk = malloc(stack_size);
  37. _init_box8(sem_mpool, sizeof(sem_mpool), sizeof(OS_SEM));
  38. _init_box8(mut_mpool, sizeof(mut_mpool), sizeof(OS_MUT));
  39. return (usb_osal_thread_t)os_tsk_create_user_ex (entry, prio,
  40. stk, stack_size,
  41. args);
  42. }
  43. void usb_osal_thread_suspend(usb_osal_thread_t thread)
  44. {
  45. os_suspend();
  46. }
  47. void usb_osal_thread_resume(usb_osal_thread_t thread)
  48. {
  49. os_resume(0);
  50. }
  51. usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
  52. {
  53. void *sem = _alloc_box(sem_mpool);
  54. if(sem != NULL) os_sem_init(sem, initial_count);
  55. return (usb_osal_sem_t)sem;
  56. }
  57. //删除os同步量,并不简单 //不能有无限等待的信号量,否则不能删除
  58. void usb_osal_sem_delete(usb_osal_sem_t sem)
  59. {
  60. _free_box(sem_mpool, sem);
  61. }
  62. int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
  63. {
  64. return (os_sem_wait(sem, timeout) != OS_R_TMO) ? 0 : -ETIMEDOUT;
  65. }
  66. //在线程,中断均调用
  67. int usb_osal_sem_give(usb_osal_sem_t sem)
  68. {
  69. uint32_t intstatus = 0;
  70. /* Obtain the number of the currently executing interrupt. */
  71. __asm volatile ( "mrs intstatus, cpsr" );
  72. if ((intstatus & 0xf) == 0) { //user mode
  73. os_sem_send(sem);
  74. } else {
  75. isr_sem_send(sem);
  76. }
  77. return 0;
  78. }
  79. usb_osal_mutex_t usb_osal_mutex_create(void)
  80. {
  81. void *mut = _alloc_box(mut_mpool);
  82. if(mut != NULL) os_mut_init(mut);
  83. return (usb_osal_mutex_t)mut;
  84. }
  85. void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
  86. {
  87. _free_box(mut_mpool, mutex);
  88. }
  89. int usb_osal_mutex_take(usb_osal_mutex_t mutex)
  90. {
  91. os_mut_wait(mutex, 0xffff);
  92. return 0;
  93. }
  94. int usb_osal_mutex_give(usb_osal_mutex_t mutex)
  95. {
  96. return (os_mut_release(mutex) == OS_R_OK) ? 0 : -EINVAL;
  97. }
  98. usb_osal_event_t usb_osal_event_create(void)
  99. {
  100. return (usb_osal_event_t)os_tsk_self();
  101. }
  102. void usb_osal_event_delete(usb_osal_event_t event)
  103. {
  104. return;
  105. }
  106. int usb_osal_event_recv(usb_osal_event_t event, uint32_t set, uint32_t *recved)
  107. {
  108. os_evt_wait_or(set, 0xffff);
  109. *recved = os_evt_get();
  110. return 0;
  111. }
  112. //在线程,中断均调用
  113. int usb_osal_event_send(usb_osal_event_t event, uint32_t set)
  114. {
  115. uint32_t intstatus = 0;
  116. /* Obtain the number of the currently executing interrupt. */
  117. __asm volatile ( "mrs intstatus, cpsr" );
  118. if ((intstatus & 0xf) == 0) { //user mode
  119. os_evt_set(set, (OS_TID)event);
  120. } else {
  121. isr_evt_set(set, (OS_TID)event);
  122. }
  123. return 0;
  124. }
  125. //这两个函数只在线程调用,可以改为tsk_lock和tsk_unlock版本
  126. size_t usb_osal_enter_critical_section(void)
  127. {
  128. return enter_critical();
  129. }
  130. void usb_osal_leave_critical_section(size_t flag)
  131. {
  132. exit_critical(flag);
  133. }
  134. void usb_osal_msleep(uint32_t delay)
  135. {
  136. os_dly_wait(delay);
  137. }
  138. #pragma arm
  139. __asm uint32_t __builtin_ctz(uint32_t val)
  140. {
  141. rsb r3, r0, #0
  142. and r0, r3, r0
  143. clz r0, r0
  144. rsb r0, r0, #31
  145. bx lr
  146. }
  147. __asm uint32_t enter_critical(void)
  148. {
  149. mrs R0, CPSR
  150. orr R1, R0, #0xC0 ; Disable IRQ and FIQ
  151. msr CPSR_c, R1
  152. bx lr
  153. }
  154. __asm uint32_t exit_critical(uint32_t sr)
  155. {
  156. msr CPSR_c, R0
  157. bx lr
  158. }