portevent.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /* ----------------------- Start implementation -----------------------------*/
  28. BOOL
  29. xMBPortEventInit(eMBRTUSlaveObj *obj) {
  30. obj->xEventFlagsId = osEventFlagsNew(NULL);
  31. return obj->xEventFlagsId != NULL;
  32. }
  33. BOOL
  34. xMBPortEventPost(eMBRTUSlaveObj *obj, eMBEventType eEvent) {
  35. if (obj->xEventFlagsId != NULL) {
  36. return (osEventFlagsSet(obj->xEventFlagsId, eEvent) & 0x80000000) == 0;
  37. } else {
  38. return FALSE;
  39. }
  40. }
  41. BOOL
  42. xMBPortEventGet(eMBRTUSlaveObj *obj, eMBEventType *eEvent) {
  43. uint32_t receivedEvent;
  44. const uint32_t EVENTS = EV_READY | EV_FRAME_RECEIVED | EV_EXECUTE | EV_FRAME_SENT;
  45. /* waiting forever OS event */
  46. receivedEvent = osEventFlagsWait(obj->xEventFlagsId, EVENTS, osFlagsWaitAny, osWaitForever);
  47. switch (receivedEvent) {
  48. case EV_READY:
  49. *eEvent = EV_READY;
  50. break;
  51. case EV_FRAME_RECEIVED:
  52. *eEvent = EV_FRAME_RECEIVED;
  53. break;
  54. case EV_EXECUTE:
  55. *eEvent = EV_EXECUTE;
  56. break;
  57. case EV_FRAME_SENT:
  58. *eEvent = EV_FRAME_SENT;
  59. break;
  60. default:
  61. return FALSE;
  62. }
  63. return TRUE;
  64. }