lo_os.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "cmsis_os.h"
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <pthread.h>
  5. #include <errno.h>
  6. #include <signal.h>
  7. void osDelay(uint32_t ms) {
  8. // printf("[DELAY] %d ms\n", ms);
  9. usleep(ms * 1000);
  10. }
  11. // temporary struct to pass function ptr and param to wrapper
  12. typedef struct {
  13. TaskFunction_t func;
  14. void * param;
  15. } PthreadTask;
  16. void* pthread_wrapper(void* p) {
  17. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0x00);
  18. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0x00);
  19. PthreadTask* task = (PthreadTask*)p;
  20. task->func(task->param);
  21. return NULL;
  22. }
  23. TaskHandle_t xTaskCreateStatic(
  24. TaskFunction_t pxTaskCode,
  25. const char * const pcName,
  26. const uint32_t ulStackDepth,
  27. void * const pvParameters,
  28. UBaseType_t uxPriority,
  29. StackType_t * const puxStackBuffer,
  30. StaticTask_t * const pxTaskBuffer
  31. ) {
  32. TaskHandle_t thread = malloc(sizeof(TaskHandle_t));
  33. PthreadTask* task = malloc(sizeof(PthreadTask));
  34. task->func = pxTaskCode;
  35. task->param = pvParameters;
  36. pthread_create(thread, NULL, pthread_wrapper, (void*)task);
  37. return thread;
  38. }
  39. void vTaskDelete(TaskHandle_t xTask) {
  40. if(xTask == NULL) {
  41. // kill itself
  42. pthread_exit(NULL);
  43. }
  44. // maybe thread already join
  45. if (pthread_kill(*xTask, 0) == ESRCH) return;
  46. // send thread_child signal to stop it сигнал, который ее завершает
  47. pthread_cancel(*xTask);
  48. // wait for join and close descriptor
  49. pthread_join(*xTask, 0x00);
  50. // cleanup thread handler
  51. *xTask = 0;
  52. }
  53. TaskHandle_t xTaskGetCurrentTaskHandle(void) {
  54. TaskHandle_t thread = malloc(sizeof(TaskHandle_t));
  55. *thread = pthread_self();
  56. return thread;
  57. }
  58. bool task_equal(TaskHandle_t a, TaskHandle_t b) {
  59. if(a == NULL || b == NULL) return false;
  60. return pthread_equal(*a, *b) != 0;
  61. }
  62. SemaphoreHandle_t xSemaphoreCreateMutexStatic(StaticSemaphore_t* pxMutexBuffer) {
  63. // TODO add posix mutex init
  64. return NULL;
  65. }