1 /* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 /*
16 * FreeModbus Libary: ESP32 Port Demo Application
17 * Copyright (C) 2010 Christian Walter <cwalter@embedded-solutions.at>
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 *
27 * documentation and/or other materials provided with the distribution.
28 * 3. The name of the author may not be used to endorse or promote products
29 * derived from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * File: $Id: portevent.c,v 1.1 2010/06/06 13:07:20 wolti Exp $
43 */
44
45 /* ----------------------- System includes ----------------------------------*/
46 #include <freertos/FreeRTOS.h>
47 #include <freertos/task.h>
48 #include <freertos/queue.h>
49
50 /* ----------------------- Modbus includes ----------------------------------*/
51 #include "mb.h"
52 #include "mbport.h"
53 #include "port.h"
54 #include "sdkconfig.h"
55 #include "port_serial_slave.h"
56 /* ----------------------- Variables ----------------------------------------*/
57 static xQueueHandle xQueueHdl;
58
59 #define MB_EVENT_QUEUE_SIZE (6)
60 #define MB_EVENT_QUEUE_TIMEOUT (pdMS_TO_TICKS(CONFIG_FMB_EVENT_QUEUE_TIMEOUT))
61
62 /* ----------------------- Start implementation -----------------------------*/
63 BOOL
xMBPortEventInit(void)64 xMBPortEventInit( void )
65 {
66 BOOL bStatus = FALSE;
67 if((xQueueHdl = xQueueCreate(MB_EVENT_QUEUE_SIZE, sizeof(eMBEventType))) != NULL)
68 {
69 vQueueAddToRegistry(xQueueHdl, "MbPortEventQueue");
70 bStatus = TRUE;
71 }
72 return bStatus;
73 }
74
75 void
vMBPortEventClose(void)76 vMBPortEventClose( void )
77 {
78 if(xQueueHdl != NULL)
79 {
80 vQueueDelete(xQueueHdl);
81 xQueueHdl = NULL;
82 }
83 }
84
85 BOOL MB_PORT_ISR_ATTR
xMBPortEventPost(eMBEventType eEvent)86 xMBPortEventPost( eMBEventType eEvent )
87 {
88 BaseType_t xStatus, xHigherPriorityTaskWoken = pdFALSE;
89 assert(xQueueHdl != NULL);
90
91 if( (BOOL)xPortInIsrContext() == TRUE )
92 {
93 xStatus = xQueueSendFromISR(xQueueHdl, (const void*)&eEvent, &xHigherPriorityTaskWoken);
94 if ( xHigherPriorityTaskWoken )
95 {
96 portYIELD_FROM_ISR();
97 }
98 if (xStatus != pdTRUE) {
99 ESP_EARLY_LOGV(MB_PORT_TAG, "%s: Post message failure = %d.", __func__, xStatus);
100 return FALSE;
101 }
102 }
103 else
104 {
105 xStatus = xQueueSend(xQueueHdl, (const void*)&eEvent, MB_EVENT_QUEUE_TIMEOUT);
106 MB_PORT_CHECK((xStatus == pdTRUE), FALSE, "%s: Post message failure.", __func__);
107 }
108 return TRUE;
109 }
110
111 BOOL
xMBPortEventGet(eMBEventType * peEvent)112 xMBPortEventGet(eMBEventType * peEvent)
113 {
114 assert(xQueueHdl != NULL);
115 BOOL xEventHappened = FALSE;
116
117 if (xQueueReceive(xQueueHdl, peEvent, portMAX_DELAY) == pdTRUE) {
118 xEventHappened = TRUE;
119 }
120 return xEventHappened;
121 }
122
123 xQueueHandle
xMBPortEventGetHandle(void)124 xMBPortEventGetHandle(void)
125 {
126 if(xQueueHdl != NULL)
127 {
128 return xQueueHdl;
129 }
130 return NULL;
131 }
132