portevent.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * FreeModbus Libary: CMSIS-RTOS2 Port
  3. * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id: portevent.c,v 1.60 2013/08/13 15:07:05 Armink $
  20. * portevent.c,v 1.60 2022/07/17 quanghona <lyhonquang@gmail.com> CMSIS-RTOS2 port $
  21. */
  22. /* ----------------------- Modbus includes ----------------------------------*/
  23. #include "mb.h"
  24. #include "mbport.h"
  25. #include "cmsis_os2.h"
  26. /* ----------------------- Variables ----------------------------------------*/
  27. static osEventFlagsId_t xEventId;
  28. /* ----------------------- Start implementation -----------------------------*/
  29. BOOL
  30. xMBPortEventInit( void )
  31. {
  32. xEventId = osEventFlagsNew(NULL);
  33. return xEventId != NULL;
  34. }
  35. BOOL
  36. xMBPortEventPost( eMBEventType eEvent )
  37. {
  38. if (xEventId != NULL)
  39. {
  40. return (osEventFlagsSet(xEventId, eEvent) & 0x80000000) == 0;
  41. }
  42. else
  43. {
  44. return FALSE;
  45. }
  46. }
  47. BOOL
  48. xMBPortEventGet( eMBEventType * eEvent )
  49. {
  50. uint32_t receivedEvent;
  51. const uint32_t EVENTS = EV_READY | EV_FRAME_RECEIVED | EV_EXECUTE | EV_FRAME_SENT;
  52. /* waiting forever OS event */
  53. receivedEvent = osEventFlagsWait(xEventId, EVENTS, osFlagsWaitAny, osWaitForever);
  54. switch (receivedEvent)
  55. {
  56. case EV_READY:
  57. *eEvent = EV_READY;
  58. break;
  59. case EV_FRAME_RECEIVED:
  60. *eEvent = EV_FRAME_RECEIVED;
  61. break;
  62. case EV_EXECUTE:
  63. *eEvent = EV_EXECUTE;
  64. break;
  65. case EV_FRAME_SENT:
  66. *eEvent = EV_FRAME_SENT;
  67. break;
  68. default:
  69. return FALSE;
  70. }
  71. return TRUE;
  72. }