lib_rtos.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  4. #include "FreeRTOS.h"
  5. #include "message_buffer.h"
  6. #include "task.h"
  7. #include "rtos.h"
  8. #include "xm_base.h"
  9. #include "rtc.h"
  10. extern volatile uint32_t ulPortInterruptNesting;
  11. OS_GLOBAL OS_Global;
  12. void XM_lock (void)
  13. {
  14. OS_IncDI();
  15. }
  16. void XM_unlock (void)
  17. {
  18. OS_DecRI();
  19. }
  20. void OS_InitKern (void)
  21. {
  22. }
  23. void OS_INIT_SYS_LOCKS (void)
  24. {
  25. }
  26. void OS_CreateTaskEx ( OS_TASK * pTask,
  27. OS_CREATE_TASK_PARA_NAME
  28. OS_U8 Priority,
  29. void (*pRoutine)(void *),
  30. void OS_STACKPTR *pStack,
  31. OS_UINT StackSize
  32. OS_CREATE_TASK_PARA_TS,
  33. void * pContext
  34. )
  35. {
  36. int priority;
  37. TaskFunction_t pvTaskCode = (TaskFunction_t)pRoutine;
  38. TaskHandle_t handle;
  39. StackSize /= sizeof(StackType_t);
  40. priority = Priority;
  41. priority -= 224;
  42. if(priority < 0)
  43. priority = 0;
  44. else if(priority > configMAX_PRIORITIES )
  45. priority = configMAX_PRIORITIES - 1;
  46. vTaskSuspendAll ();
  47. //XM_printf ("sizeof(pTask->task) = %d\n", sizeof(pTask->task));
  48. handle = xTaskCreateStatic
  49. (pvTaskCode, // TaskFunction_t pvTaskCode,
  50. Name, // const char * const pcName,
  51. StackSize, // uint32_t ulStackDepth
  52. pContext, // void *pvParameters,
  53. priority, // UBaseType_t uxPriority,
  54. pStack, // StackType_t * const puxStackBuffer,
  55. &pTask->task // StaticTask_t * const pxTaskBuffer
  56. );
  57. if(handle)
  58. {
  59. pTask->id = RTOS_ID_TASK;
  60. pTask->handle = handle;
  61. vTaskSetApplicationTaskTag(handle, (TaskHookFunction_t)pTask);
  62. }
  63. xTaskResumeAll ();
  64. if(handle == NULL)
  65. {
  66. OS_Error (OS_ERR_STACK);
  67. }
  68. }
  69. void OS_CreateTask ( OS_TASK * pTask,
  70. OS_ROM_DATA const char* Name,
  71. OS_U8 Priority,
  72. void (*pRoutine)(void),
  73. void OS_STACKPTR *pStack,
  74. OS_UINT StackSize
  75. OS_CREATE_TASK_PARA_TS
  76. )
  77. {
  78. int priority;
  79. TaskFunction_t pvTaskCode = (TaskFunction_t)pRoutine;
  80. TaskHandle_t handle;
  81. StackSize /= sizeof(StackType_t);
  82. priority = Priority;
  83. priority -= 224;
  84. if(priority < 0)
  85. priority = 0;
  86. else if(priority > configMAX_PRIORITIES )
  87. priority = configMAX_PRIORITIES - 1;
  88. vTaskSuspendAll ();
  89. //XM_printf ("sizeof(pTask->task) = %d\n", sizeof(pTask->task));
  90. handle = xTaskCreateStatic
  91. (pvTaskCode, // TaskFunction_t pvTaskCode,
  92. Name, // const char * const pcName,
  93. StackSize, // uint32_t ulStackDepth
  94. NULL, // void *pvParameters,
  95. priority, // UBaseType_t uxPriority,
  96. pStack, // StackType_t * const puxStackBuffer,
  97. &pTask->task // StaticTask_t * const pxTaskBuffer
  98. );
  99. if(handle)
  100. {
  101. pTask->id = RTOS_ID_TASK;
  102. pTask->handle = handle;
  103. vTaskSetApplicationTaskTag(handle, (TaskHookFunction_t)pTask);
  104. }
  105. xTaskResumeAll ();
  106. if(handle == NULL)
  107. {
  108. OS_Error (OS_ERR_STACK);
  109. }
  110. }
  111. void OS_Terminate (OS_TASK* pTask)
  112. {
  113. TaskHandle_t handle;
  114. if(pTask == NULL)
  115. {
  116. pTask = OS_GetpCurrentTask();
  117. pTask->id = 0;
  118. pTask->handle = 0;
  119. vTaskDelete(NULL);
  120. }
  121. else
  122. {
  123. if(pTask->id != RTOS_ID_TASK)
  124. {
  125. OS_Error (OS_ERR_INV_TASK);
  126. return;
  127. }
  128. handle = pTask->handle;
  129. pTask->id = 0;
  130. pTask->handle = 0;
  131. vTaskDelete(handle);
  132. }
  133. }
  134. void OS_Delay (int ms)
  135. {
  136. vTaskDelay(ms/portTICK_RATE_MS);
  137. //while(ms--);
  138. }
  139. OS_TASK* OS_GetpCurrentTask (void)
  140. {
  141. OS_TASK *pTask = (OS_TASK *)xTaskGetApplicationTaskTag(NULL);
  142. if(pTask == NULL || pTask->id != RTOS_ID_TASK)
  143. {
  144. OS_Error (OS_ERR_INV_TASK);
  145. return NULL;
  146. }
  147. return pTask;
  148. }
  149. unsigned char OS_GetPriority (OS_TASK* pTask)
  150. {
  151. int priority;
  152. TaskHandle_t handle = 0;
  153. if(pTask && pTask->id != RTOS_ID_TASK)
  154. {
  155. OS_Error (OS_ERR_INV_TASK);
  156. return 0;
  157. }
  158. if(pTask)
  159. handle = pTask->handle;
  160. priority = (unsigned char)uxTaskPriorityGet (handle);
  161. priority += 224; // 0 ~ 32
  162. if(priority > 255)
  163. priority = 255;
  164. return (unsigned char)priority;
  165. }
  166. void OS_SetPriority (OS_TASK* pTask, unsigned char Priority)
  167. {
  168. int priority;
  169. TaskHandle_t handle = 0;
  170. if(pTask && pTask->id != RTOS_ID_TASK)
  171. {
  172. OS_Error (OS_ERR_INV_TASK);
  173. return;
  174. }
  175. priority = Priority;
  176. priority -= 224;
  177. if(priority < 0)
  178. priority = 0;
  179. else if(priority > configMAX_PRIORITIES )
  180. priority = configMAX_PRIORITIES - 1;
  181. if(pTask)
  182. handle = pTask->handle;
  183. vTaskPrioritySet(handle, priority);
  184. }
  185. #include <stdlib.h>
  186. void* OS_malloc(unsigned int n)
  187. {
  188. char *mem = (char *)pvPortMalloc(n);
  189. if(mem)
  190. {
  191. //memset (mem, 0, n);
  192. }
  193. return mem;
  194. }
  195. void OS_free (void* pMemBlock)
  196. {
  197. vPortFree (pMemBlock);
  198. }
  199. void* OS_realloc (void * pv, unsigned int n )
  200. {
  201. return pvPortRealloc (pv, n);
  202. }
  203. void OS_CREATERSEMA (OS_RSEMA* pRSema)
  204. {
  205. SemaphoreHandle_t handle;
  206. if(pRSema == NULL)
  207. {
  208. OS_Error (OS_ERR_INV_RSEMA);
  209. return;
  210. }
  211. if(pRSema->id == RTOS_ID_RSEMA)
  212. {
  213. OS_Error (OS_ERR_2USE_RSEMA);
  214. return;
  215. }
  216. vTaskSuspendAll ();
  217. handle = xSemaphoreCreateRecursiveMutexStatic(&pRSema->sema);
  218. if(handle)
  219. {
  220. pRSema->handle = handle;
  221. pRSema->id = RTOS_ID_RSEMA;
  222. }
  223. xTaskResumeAll ();
  224. if(handle == NULL)
  225. {
  226. OS_Error (OS_ERR_INV_RSEMA);
  227. return;
  228. }
  229. }
  230. void OS_DeleteRSema (OS_RSEMA* pRSema)
  231. {
  232. if(pRSema == NULL || pRSema->id != RTOS_ID_RSEMA || pRSema->handle == NULL)
  233. {
  234. OS_Error (OS_ERR_RSEMA_DELETE);
  235. return;
  236. }
  237. vTaskSuspendAll ();
  238. vSemaphoreDelete(pRSema->handle);
  239. pRSema->handle = NULL;
  240. pRSema->id = 0;
  241. xTaskResumeAll ();
  242. }
  243. int OS_Use (OS_RSEMA* pRSema)
  244. {
  245. if(pRSema == NULL || pRSema->id != RTOS_ID_RSEMA)
  246. {
  247. OS_Error (OS_ERR_INV_RSEMA);
  248. return -1;
  249. }
  250. if(xSemaphoreTakeRecursive( pRSema->handle, portMAX_DELAY) == pdPASS)
  251. return 1;
  252. else
  253. {
  254. OS_Error (OS_ERR_INV_RSEMA);
  255. return -1;
  256. }
  257. }
  258. void OS_Unuse (OS_RSEMA* pRSema)
  259. {
  260. if(pRSema == NULL || pRSema->id != RTOS_ID_RSEMA)
  261. {
  262. OS_Error (OS_ERR_INV_RSEMA);
  263. return;
  264. }
  265. if(xSemaphoreGiveRecursive(pRSema->handle) != pdPASS)
  266. {
  267. OS_Error (OS_ERR_UNUSE_BEFORE_USE);
  268. }
  269. }
  270. // Requests a specified semaphore and blocks it for other tasks if it is available. Continues execution in any case.
  271. // 1: Resource was available, now in use by calling task
  272. // 0: Resource was not available.
  273. char OS_Request (OS_RSEMA* pRSema)
  274. {
  275. if(pRSema == NULL || pRSema->id != RTOS_ID_RSEMA)
  276. {
  277. OS_Error (OS_ERR_INV_RSEMA);
  278. return 0;
  279. }
  280. if(xSemaphoreTakeRecursive(pRSema->handle, 0) == pdPASS)
  281. return 1;
  282. else
  283. return 0;
  284. }
  285. void OS_CreateCSema (OS_CSEMA* pCSema, OS_UINT InitValue)
  286. {
  287. SemaphoreHandle_t handle;
  288. if(pCSema == NULL)
  289. {
  290. OS_Error (OS_ERR_INV_CSEMA);
  291. return;
  292. }
  293. memset (pCSema, 0, sizeof(OS_CSEMA));
  294. // 禁止任务调度
  295. vTaskSuspendAll ();
  296. handle = xSemaphoreCreateCountingStatic( 0xFFFFFFFF, InitValue, &pCSema->sema);
  297. if(handle)
  298. {
  299. pCSema->handle = handle;
  300. pCSema->id = RTOS_ID_CSEMA;
  301. }
  302. // 使能任务调度
  303. xTaskResumeAll ();
  304. if(handle == NULL)
  305. {
  306. OS_Error (OS_ERR_INV_CSEMA);
  307. return;
  308. }
  309. }
  310. void OS_DeleteCSema (OS_CSEMA* pCSema)
  311. {
  312. SemaphoreHandle_t handle;
  313. if(pCSema == NULL)
  314. {
  315. OS_Error (OS_ERR_INV_CSEMA);
  316. return;
  317. }
  318. if(pCSema->id != RTOS_ID_CSEMA || pCSema->handle == NULL)
  319. {
  320. OS_Error (OS_ERR_INV_CSEMA);
  321. return;
  322. }
  323. // 禁止任务调度
  324. vTaskSuspendAll ();
  325. handle = pCSema->handle;
  326. pCSema->id = 0;
  327. pCSema->handle = 0;
  328. vSemaphoreDelete (handle);
  329. // 使能任务调度
  330. xTaskResumeAll ();
  331. }
  332. void OS_WaitCSema (OS_CSEMA* pCSema)
  333. {
  334. if(pCSema == NULL || pCSema->id != RTOS_ID_CSEMA || pCSema->handle == NULL)
  335. {
  336. OS_Error (OS_ERR_INV_CSEMA);
  337. return;
  338. }
  339. if(xSemaphoreTake(pCSema->handle, portMAX_DELAY) == pdFAIL)
  340. {
  341. OS_Error (OS_ERR_INV_CSEMA);
  342. return;
  343. }
  344. }
  345. int OS_WaitCSemaTimed (OS_CSEMA* pCSema, int TimeOut)
  346. {
  347. if(pCSema == NULL || pCSema->id != RTOS_ID_CSEMA || pCSema->handle == NULL)
  348. {
  349. OS_Error (OS_ERR_INV_CSEMA);
  350. return 0;
  351. }
  352. if(xSemaphoreTake(pCSema->handle, TimeOut) == pdFAIL)
  353. {
  354. //printf ("OS_WaitCSemaTimed failed\n");
  355. return 0;
  356. }
  357. // printf ("OS_WaitCSemaTimed 0x%08x OK\n", pCSema);
  358. return 1;
  359. }
  360. // Increments the counter of a semaphore.
  361. void OS_SignalCSema (OS_CSEMA * pCSema)
  362. {
  363. BaseType_t ret = pdFAIL;
  364. if(ulPortInterruptNesting)
  365. {
  366. // 从中断程序中调用
  367. if(pCSema && pCSema->id == RTOS_ID_CSEMA)
  368. {
  369. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  370. ret = xSemaphoreGiveFromISR(pCSema->handle, &xHigherPriorityTaskWoken);
  371. if(ret == pdTRUE)
  372. {
  373. //XM_printf ("OS_SignalCSema 0x%08x\n", pCSema);
  374. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  375. }
  376. else
  377. {
  378. //XM_printf ("OS_SignalCSema failed\n");
  379. }
  380. }
  381. else
  382. {
  383. //XM_printf ("OS_SignalCSema error\n");
  384. }
  385. }
  386. else
  387. {
  388. vTaskSuspendAll ();
  389. if(pCSema && pCSema->id == RTOS_ID_CSEMA)
  390. {
  391. ret = xSemaphoreGive(pCSema->handle);
  392. }
  393. xTaskResumeAll ();
  394. }
  395. if(ret == pdFAIL)
  396. {
  397. OS_Error (OS_ERR_INV_TASK);
  398. }
  399. }
  400. int OS_GetCSemaValue (OS_CSEMA* pCSema)
  401. {
  402. if(pCSema == NULL || pCSema->id != RTOS_ID_CSEMA || pCSema->handle == NULL)
  403. {
  404. OS_Error (OS_ERR_INV_CSEMA);
  405. return 0;
  406. }
  407. return uxSemaphoreGetCount (pCSema->handle);
  408. }
  409. // 0: If the value could be set.
  410. // != 0: In case of error.
  411. int OS_SetCSemaValue (OS_CSEMA* pCSema, OS_UINT Value)
  412. {
  413. while(Value)
  414. {
  415. OS_SignalCSema (pCSema);
  416. Value --;
  417. }
  418. return 0;
  419. }
  420. // Waits for one of the events specified in the bitmask and clears the event memory after an event occurs.
  421. char OS_WaitEvent (char EventMask)
  422. {
  423. uint32_t pulNotificationValue;
  424. while(1)
  425. {
  426. if(xTaskNotifyWait(0, (unsigned char)0xffffffff, &pulNotificationValue, portMAX_DELAY) == pdTRUE)
  427. {
  428. // 检查指定的事件是否产生
  429. if(pulNotificationValue & EventMask)
  430. break;
  431. }
  432. else
  433. {
  434. // 异常
  435. OS_Error (OS_ERR_INV_TASK);
  436. return 0;
  437. }
  438. }
  439. return (char)(pulNotificationValue & EventMask);
  440. }
  441. // Waits for one of the events specified by the bitmask and clears only that event after it occurs.
  442. char OS_WaitSingleEvent (char EventMask)
  443. {
  444. unsigned int mask;
  445. int i;
  446. uint32_t events = 0;
  447. uint32_t pulNotificationValue = 0;
  448. uint32_t event_to_write_back = 0; // 回写事件
  449. uint32_t event_to_write_back_mask = (uint8_t)(~EventMask);
  450. while(1)
  451. {
  452. if(xTaskNotifyWait(0, (unsigned char)0xffffffff, &pulNotificationValue, portMAX_DELAY) == pdTRUE)
  453. {
  454. pulNotificationValue |= pulNotificationValue & event_to_write_back_mask;
  455. // 检查指定的事件是否产生
  456. events = pulNotificationValue & EventMask;
  457. if(events)
  458. break;
  459. }
  460. else
  461. {
  462. // 异常
  463. OS_Error (OS_ERR_INV_TASK);
  464. return 0;
  465. }
  466. }
  467. // 从bit0开始扫描, bit0~bit7
  468. mask = 0x01;
  469. for (i = 0; i < 8; i ++)
  470. {
  471. if(events & mask)
  472. {
  473. events &= ~mask;
  474. break;
  475. }
  476. mask = mask << 1;
  477. }
  478. event_to_write_back |= events;
  479. // 检查是否存在回写的事件
  480. if(event_to_write_back)
  481. {
  482. xTaskNotify(xTaskGetCurrentTaskHandle(), event_to_write_back, eSetBits);
  483. }
  484. return (char)(pulNotificationValue & mask);
  485. }
  486. void OS_SignalEvent (char Event, OS_TASK* pTask)
  487. {
  488. BaseType_t ret = pdFAIL;
  489. if(ulPortInterruptNesting)
  490. {
  491. if(pTask && pTask->id == RTOS_ID_TASK)
  492. {
  493. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  494. ret = xTaskNotifyFromISR(pTask->handle, (unsigned char)Event, eSetBits, &xHigherPriorityTaskWoken);
  495. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  496. }
  497. }
  498. else
  499. {
  500. vTaskSuspendAll ();
  501. if(pTask && pTask->id == RTOS_ID_TASK)
  502. {
  503. ret = xTaskNotify(pTask->handle, (unsigned char)Event, eSetBits);
  504. }
  505. xTaskResumeAll ();
  506. }
  507. if(ret == pdFAIL)
  508. {
  509. OS_Error (OS_ERR_INV_TASK);
  510. }
  511. }
  512. // 以下函数的仿真非完整, 但不影响当前项目的使用.
  513. // 假定pTask为当前任务
  514. char OS_GetEventsOccurred (OS_TASK* pTask)
  515. {
  516. uint32_t ulNotifiedValue = 0;
  517. if(pTask)
  518. {
  519. if(pTask->id != RTOS_ID_TASK)
  520. {
  521. OS_Error (OS_ERR_INV_TASK);
  522. return 0;
  523. }
  524. // 假定必须为当前任务
  525. if(pTask->handle != xTaskGetCurrentTaskHandle())
  526. {
  527. OS_Error (OS_ERR_INV_TASK);
  528. return 0;
  529. }
  530. }
  531. xTaskNotifyWait(0, 0, &ulNotifiedValue, 0);
  532. return (char)ulNotifiedValue;
  533. }
  534. // 以下函数的仿真非完整, 但不影响当前项目的使用.
  535. // Returns the actual state of events and then clears the events of a specified task.
  536. char OS_ClearEvents (OS_TASK* pTask)
  537. {
  538. if(pTask == NULL || pTask->id != RTOS_ID_TASK)
  539. {
  540. OS_Error (OS_ERR_INV_TASK);
  541. return 0;
  542. }
  543. xTaskNotifyStateClear(pTask->handle);
  544. return 0;
  545. }
  546. void OS_Start (void)
  547. {
  548. configASSERT(OS_DICnt == 1);
  549. OS_DICnt = 0;
  550. vTaskStartScheduler ();
  551. }
  552. void OS_EnterRegion (void)
  553. {
  554. XM_lock ();
  555. //configASSERT(ulPortInterruptNesting == 0);
  556. //taskENTER_CRITICAL ();
  557. }
  558. void OS_LeaveRegion (void)
  559. {
  560. //configASSERT(ulPortInterruptNesting == 0);
  561. //taskEXIT_CRITICAL ();
  562. XM_unlock ();
  563. }
  564. int OS_GetTime (void)
  565. {
  566. return xTaskGetTickCount ();
  567. }
  568. unsigned int OS_GetTime32 (void)
  569. {
  570. return xTaskGetTickCount ();
  571. }
  572. void OS_CreateTimer (OS_TIMER* pTimer, OS_TIMERROUTINE* Callback, OS_TIME Timeout)
  573. {
  574. TimerHandle_t handle;
  575. if(pTimer == NULL)
  576. {
  577. OS_Error (OS_ERR_INV_TIMER);
  578. return;
  579. }
  580. if(pTimer->id == RTOS_ID_TIMER)
  581. {
  582. OS_Error (OS_ERR_2USE_TIMER);
  583. return;
  584. }
  585. handle = xTimerCreateStatic (NULL, Timeout, 0, 0, (TimerCallbackFunction_t)Callback, &pTimer->timer);
  586. if(handle)
  587. {
  588. pTimer->id = RTOS_ID_TIMER;
  589. pTimer->handle = handle;
  590. }
  591. else
  592. {
  593. OS_Error (OS_ERR_INV_TIMER);
  594. return;
  595. }
  596. }
  597. void OS_DeleteTimer (OS_TIMER* pTimer)
  598. {
  599. TimerHandle_t handle;
  600. if(pTimer == NULL)
  601. {
  602. OS_Error (OS_ERR_INV_TIMER);
  603. return;
  604. }
  605. if(pTimer->id != RTOS_ID_TIMER)
  606. {
  607. OS_Error (OS_ERR_INV_TIMER);
  608. return;
  609. }
  610. handle = pTimer->handle;
  611. pTimer->id = 0;
  612. pTimer->handle = 0;
  613. xTimerDelete (handle, portMAX_DELAY);
  614. }
  615. void OS_StartTimer (OS_TIMER* pTimer)
  616. {
  617. if(pTimer == NULL || pTimer->id != RTOS_ID_TIMER)
  618. {
  619. OS_Error (OS_ERR_INV_TIMER);
  620. return;
  621. }
  622. if(ulPortInterruptNesting)
  623. {
  624. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  625. if( xTimerStartFromISR( pTimer->handle, &xHigherPriorityTaskWoken ) == pdPASS )
  626. {
  627. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  628. }
  629. }
  630. else
  631. {
  632. xTimerStart (pTimer->handle, portMAX_DELAY);
  633. }
  634. }
  635. void OS_StopTimer (OS_TIMER* pTimer)
  636. {
  637. if(pTimer == NULL || pTimer->id != RTOS_ID_TIMER)
  638. {
  639. OS_Error (OS_ERR_INV_TIMER);
  640. return;
  641. }
  642. if(ulPortInterruptNesting)
  643. {
  644. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  645. if(xTimerStopFromISR( pTimer->handle, &xHigherPriorityTaskWoken) == pdPASS)
  646. {
  647. if(xHigherPriorityTaskWoken != pdFALSE)
  648. {
  649. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  650. }
  651. }
  652. else
  653. {
  654. OS_Error (OS_ERR_INV_TIMER);
  655. return;
  656. }
  657. }
  658. else
  659. {
  660. xTimerStop (pTimer->handle, portMAX_DELAY);
  661. }
  662. }
  663. void OS_RetriggerTimer (OS_TIMER* pTimer)
  664. {
  665. if(pTimer == NULL || pTimer->id != RTOS_ID_TIMER)
  666. {
  667. OS_Error (OS_ERR_INV_TIMER);
  668. return;
  669. }
  670. if(ulPortInterruptNesting)
  671. {
  672. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  673. if( xTimerStartFromISR( pTimer->handle, &xHigherPriorityTaskWoken ) == pdPASS )
  674. {
  675. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  676. }
  677. }
  678. else
  679. {
  680. xTimerStart (pTimer->handle, portMAX_DELAY);
  681. }
  682. }
  683. void OS_EVENT_Create (OS_EVENT* pEvent)
  684. {
  685. EventGroupHandle_t handle;
  686. if(pEvent == NULL || pEvent->id == RTOS_ID_EVENT)
  687. {
  688. OS_Error (OS_ERR_EVENTOBJ_INV);
  689. return;
  690. }
  691. handle = xEventGroupCreateStatic(&pEvent->event);
  692. pEvent->handle = handle;
  693. pEvent->id = RTOS_ID_EVENT;
  694. }
  695. void OS_EVENT_Delete (OS_EVENT* pEvent)
  696. {
  697. if(pEvent == NULL || pEvent->id != RTOS_ID_EVENT)
  698. {
  699. OS_Error (OS_ERR_EVENTOBJ_INV);
  700. return;
  701. }
  702. pEvent->id = 0;
  703. vEventGroupDelete (pEvent->handle);
  704. pEvent->handle = 0;
  705. }
  706. void OS_EVENT_Wait (OS_EVENT* pEvent)
  707. {
  708. if(pEvent == NULL || pEvent->id != RTOS_ID_EVENT)
  709. {
  710. OS_Error (OS_ERR_EVENTOBJ_INV);
  711. return;
  712. }
  713. // 使用bit0模拟OS_EVENT
  714. xEventGroupWaitBits( pEvent->handle, 0x01, pdTRUE, pdFALSE, portMAX_DELAY);
  715. }
  716. // 0: Success, the event was signaled within the specified time.
  717. // 1: The event was not signaled and a timeout occurred.
  718. char OS_EVENT_WaitTimed (OS_EVENT* pEvent, OS_TIME Timeout)
  719. {
  720. if(pEvent == NULL || pEvent->id != RTOS_ID_EVENT)
  721. {
  722. OS_Error (OS_ERR_EVENTOBJ_INV);
  723. return 1;
  724. }
  725. // 使用bit0模拟OS_EVENT
  726. if(xEventGroupWaitBits( pEvent->handle, 0x01, pdTRUE, pdFALSE, Timeout) & 0x01)
  727. return 0;
  728. else
  729. return 1;
  730. }
  731. void OS_EVENT_Reset (OS_EVENT* pEvent)
  732. {
  733. if(pEvent == NULL || pEvent->id != RTOS_ID_EVENT)
  734. {
  735. OS_Error (OS_ERR_EVENTOBJ_INV);
  736. return;
  737. }
  738. xEventGroupClearBits (pEvent->handle, 0x01);
  739. }
  740. void OS_EVENT_Set (OS_EVENT* pEvent)
  741. {
  742. if(pEvent == NULL || pEvent->id != RTOS_ID_EVENT)
  743. {
  744. OS_Error (OS_ERR_EVENTOBJ_INV);
  745. return;
  746. }
  747. if(ulPortInterruptNesting)
  748. {
  749. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  750. if(xEventGroupSetBitsFromISR( pEvent->handle, 0x01, &xHigherPriorityTaskWoken) == pdPASS)
  751. {
  752. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  753. }
  754. else
  755. {
  756. //XM_printf ("OS_EVENT_Set failed\n");
  757. }
  758. }
  759. else
  760. {
  761. xEventGroupSetBits (pEvent->handle, 0x01);
  762. }
  763. }
  764. void OS_CREATEMB (OS_MAILBOX* pMB, unsigned char sizeofMsg, unsigned int maxnofMsg, void* pMsg)
  765. {
  766. MessageBufferHandle_t handle;
  767. unsigned int size;
  768. if(pMB == NULL || pMsg == NULL || sizeofMsg*maxnofMsg == 0)
  769. {
  770. OS_Error (OS_ERR_INV_MAILBOX);
  771. return;
  772. }
  773. if(pMB->id == RTOS_ID_MAILBOX)
  774. {
  775. OS_Error (OS_ERR_2USE_MAILBOX);
  776. return;
  777. }
  778. size = sizeofMsg * maxnofMsg;
  779. if(size < 4)
  780. {
  781. // special one MB
  782. handle = xMessageBufferCreateStatic(sizeof(pMB->min_msg), (uint8_t *)pMB->min_msg, &pMB->message);
  783. }
  784. else
  785. {
  786. // 必须为4字节的倍数
  787. if(size & 3)
  788. {
  789. OS_Error (OS_ERR_INV_MAILBOX);
  790. return;
  791. }
  792. handle = xMessageBufferCreateStatic(sizeofMsg * maxnofMsg, pMsg, &pMB->message);
  793. }
  794. pMB->handle = handle;
  795. pMB->id = RTOS_ID_MAILBOX;
  796. pMB->size = sizeofMsg;
  797. }
  798. // Clears all messages in a specified mailbox
  799. void OS_ClearMB (OS_MAILBOX* pMB)
  800. {
  801. if(pMB == NULL || pMB->id != RTOS_ID_MAILBOX || pMB->handle == NULL)
  802. {
  803. OS_Error (OS_ERR_INV_MAILBOX);
  804. return;
  805. }
  806. if(xMessageBufferReset(pMB->handle) == pdFAIL)
  807. {
  808. //OS_Error (OS_ERR_INV_MAILBOX);
  809. return;
  810. }
  811. }
  812. char OS_GetMailTimed (OS_MAILBOX* pMB, void* pDest, OS_TIME Timeout)
  813. {
  814. size_t length;
  815. if(pMB == NULL || pMB->id != RTOS_ID_MAILBOX || pMB->handle == NULL || pDest == NULL)
  816. {
  817. OS_Error (OS_ERR_INV_MAILBOX);
  818. return 1;
  819. }
  820. length = xMessageBufferReceive(pMB->handle, pDest, pMB->size, Timeout);
  821. if(length == pMB->size)
  822. return 0;
  823. else
  824. return 1;
  825. }
  826. void OS_PutMail (OS_MAILBOX* pMB, void* pMail)
  827. {
  828. if(pMB == NULL || pMB->id != RTOS_ID_MAILBOX || pMB->handle == NULL || pMail == NULL)
  829. {
  830. OS_Error (OS_ERR_INV_MAILBOX);
  831. return;
  832. }
  833. configASSERT (ulPortInterruptNesting == 0);
  834. if(xMessageBufferSend(pMB->handle, pMail, pMB->size, portMAX_DELAY) == 0)
  835. {
  836. OS_Error (OS_ERR_INV_MAILBOX);
  837. return;
  838. }
  839. }
  840. void OS_PutMail1 (OS_MAILBOX* pMB, const char* pMail)
  841. {
  842. if(pMB == NULL || pMB->id != RTOS_ID_MAILBOX || pMB->handle == NULL || pMail == NULL)
  843. {
  844. OS_Error (OS_ERR_INV_MAILBOX);
  845. return;
  846. }
  847. if(pMB->size != 1)
  848. {
  849. OS_Error (OS_ERR_INV_MAILBOX);
  850. return;
  851. }
  852. if(xMessageBufferSend(pMB->handle, pMail, pMB->size, portMAX_DELAY) == 0)
  853. {
  854. OS_Error (OS_ERR_INV_MAILBOX);
  855. return;
  856. }
  857. }
  858. void OS_TICK_Config ( unsigned FractPerInt, unsigned FractPerTick )
  859. {
  860. }
  861. void OS_Error(int ErrCode)
  862. {
  863. #if (DEBUG == 1)
  864. // 输出错误码
  865. printf ("OS_Error CODE: 0x%02x\r\n", ErrCode);
  866. #endif
  867. OS_DICnt = 0; /* Allow interrupts so we can communicate */
  868. OS_EI();
  869. while (1);
  870. }
  871. void trace_error (char *fmt, ...)
  872. {
  873. }
  874. void xm_do_change_task (void)
  875. {
  876. }
  877. #define XM_PRINTF_SIZE 128
  878. void XM_printf (char *fmt, ...)
  879. {
  880. static char xm_info[XM_PRINTF_SIZE + 4];
  881. va_list ap;
  882. xm_info[XM_PRINTF_SIZE] = 0;
  883. va_start(ap, fmt);
  884. //vsprintf (xm_info, fmt, ap);
  885. vsnprintf (xm_info, XM_PRINTF_SIZE, fmt, ap);
  886. va_end(ap);
  887. printf ("%s", xm_info);
  888. }
  889. void XM_printf_ (char *fmt, ...)
  890. {
  891. va_list ap;
  892. va_start(ap, fmt);
  893. printf(fmt, ap);
  894. va_end(ap);
  895. }
  896. // 鏃堕棿璁剧疆/璇诲彇鍑芥暟
  897. // 杩斿洖1琛ㄧず绯荤粺鏃堕棿宸茶�缃�
  898. // 杩斿洖0琛ㄧず绯荤粺鏃堕棿鏈��缃�紝绯荤粺杩斿洖缂虹渷瀹氫箟鏃堕棿
  899. int XM_GetLocalTime (XMSYSTEMTIME* pSystemTime)
  900. {
  901. SystemTime_t t;
  902. iGetLocalTime (&t);
  903. pSystemTime->wDay = t.tm_mday;
  904. pSystemTime->wDayOfWeek = t.tm_wday;
  905. pSystemTime->wHour = t.tm_hour;
  906. pSystemTime->wMilliseconds = 0;
  907. pSystemTime->wMinute = t.tm_min;
  908. pSystemTime->wMonth = t.tm_mon;
  909. pSystemTime->wSecond = t.tm_sec;
  910. pSystemTime->wYear = t.tm_year;
  911. return 1;
  912. }
  913. // 杩斿洖1璁剧疆绯荤粺鏃堕棿鎴愬姛
  914. // 杩斿洖0璁剧疆绯荤粺鏃堕棿澶辫触
  915. int XM_SetLocalTime (const XMSYSTEMTIME *pSystemTime)
  916. {
  917. SystemTime_t t;
  918. t.tm_mday = pSystemTime->wDay;
  919. t.tm_wday = pSystemTime->wDayOfWeek ;
  920. t.tm_hour = pSystemTime->wHour;
  921. t.tm_min = pSystemTime->wMinute;
  922. t.tm_mon = pSystemTime->wMonth;
  923. t.tm_sec = pSystemTime->wSecond;
  924. t.tm_year = pSystemTime->wYear;
  925. iSetLocalTime (&t);
  926. return 1;
  927. }
  928. DWORD XM_GetTickCount (void)
  929. {
  930. return OS_GetTime();
  931. }