Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

103 řádky
3.3 KiB

  1. /*
  2. * Lua RTOS, mutex api implementation over FreeRTOS
  3. *
  4. * Copyright (C) 2015 - 2017
  5. * IBEROXARXA SERVICIOS INTEGRALES, S.L. & CSS IBÉRICA, S.L.
  6. *
  7. * Author: Jaume Olivé (jolive@iberoxarxa.com / jolive@whitecatboard.org)
  8. *
  9. * All rights reserved.
  10. *
  11. * Permission to use, copy, modify, and distribute this software
  12. * and its documentation for any purpose and without fee is hereby
  13. * granted, provided that the above copyright notice appear in all
  14. * copies and that both that the copyright notice and this
  15. * permission notice and warranty disclaimer appear in supporting
  16. * documentation, and that the name of the author not be used in
  17. * advertising or publicity pertaining to distribution of the
  18. * software without specific, written prior permission.
  19. *
  20. * The author disclaim all warranties with regard to this
  21. * software, including all implied warranties of merchantability
  22. * and fitness. In no event shall the author be liable for any
  23. * special, indirect or consequential damages or any damages
  24. * whatsoever resulting from loss of use, data or profits, whether
  25. * in an action of contract, negligence or other tortious action,
  26. * arising out of or in connection with the use or performance of
  27. * this software.
  28. *
  29. * Modified by: LoBo (loboris@gmail.com / https://github.com/loboris)
  30. *
  31. */
  32. #include "freertos/FreeRTOS.h"
  33. #include "esp_attr.h"
  34. #include "mutex.h"
  35. #define portEND_SWITCHING_ISR(xSwitchRequired) \
  36. if (xSwitchRequired) { \
  37. _frxt_setup_switch(); \
  38. }
  39. extern unsigned port_interruptNesting[portNUM_PROCESSORS];
  40. void _mtx_init() {
  41. }
  42. void mtx_init(struct mtx *mutex, const char *name, const char *type, int opts) {
  43. mutex->sem = xSemaphoreCreateBinary();
  44. if (mutex->sem) {
  45. if (port_interruptNesting[xPortGetCoreID()] != 0) {
  46. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  47. xSemaphoreGiveFromISR( mutex->sem, &xHigherPriorityTaskWoken);
  48. portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
  49. } else {
  50. xSemaphoreGive( mutex->sem );
  51. }
  52. }
  53. }
  54. void IRAM_ATTR mtx_lock(struct mtx *mutex) {
  55. if (port_interruptNesting[xPortGetCoreID()] != 0) {
  56. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  57. xSemaphoreTakeFromISR( mutex->sem, &xHigherPriorityTaskWoken );
  58. portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
  59. } else {
  60. xSemaphoreTake( mutex->sem, portMAX_DELAY );
  61. }
  62. }
  63. int mtx_trylock(struct mtx *mutex) {
  64. if (xSemaphoreTake( mutex->sem, 0 ) == pdTRUE) {
  65. return 1;
  66. } else {
  67. return 0;
  68. }
  69. }
  70. void IRAM_ATTR mtx_unlock(struct mtx *mutex) {
  71. if (port_interruptNesting[xPortGetCoreID()] != 0) {
  72. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  73. xSemaphoreGiveFromISR( mutex->sem, &xHigherPriorityTaskWoken );
  74. portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
  75. } else {
  76. xSemaphoreGive( mutex->sem );
  77. }
  78. }
  79. void mtx_destroy(struct mtx *mutex) {
  80. if (port_interruptNesting[xPortGetCoreID()] != 0) {
  81. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  82. xSemaphoreGiveFromISR( mutex->sem, &xHigherPriorityTaskWoken );
  83. portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
  84. } else {
  85. xSemaphoreGive( mutex->sem );
  86. }
  87. vSemaphoreDelete( mutex->sem );
  88. mutex->sem = 0;
  89. }