2021-09-27 00:46:51 -04:00
|
|
|
/*
|
2021-10-22 13:23:03 -04:00
|
|
|
* SPDX-FileCopyrightText: 2013 Armink
|
2018-10-19 09:51:27 -04:00
|
|
|
*
|
2021-11-25 06:45:44 -05:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2021-10-22 13:23:03 -04:00
|
|
|
*
|
|
|
|
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
|
2018-10-19 09:51:27 -04:00
|
|
|
*/
|
|
|
|
/*
|
2021-11-25 06:45:44 -05:00
|
|
|
* FreeModbus Libary: ESP32 Port
|
2018-10-19 09:51:27 -04:00
|
|
|
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
|
|
|
*
|
2021-11-25 06:45:44 -05:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
2018-10-19 09:51:27 -04:00
|
|
|
*
|
2021-11-25 06:45:44 -05:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2018-10-19 09:51:27 -04:00
|
|
|
*
|
|
|
|
* File: $Id: porttimer_m.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------------- Platform includes --------------------------------*/
|
|
|
|
#include "port.h"
|
|
|
|
|
|
|
|
/* ----------------------- Modbus includes ----------------------------------*/
|
|
|
|
#include "mb_m.h"
|
|
|
|
#include "mbport.h"
|
2019-06-05 22:57:29 -04:00
|
|
|
#include "sdkconfig.h"
|
2018-10-19 09:51:27 -04:00
|
|
|
|
2021-10-12 11:06:13 -04:00
|
|
|
static const char *TAG = "MBM_TIMER";
|
2018-10-19 09:51:27 -04:00
|
|
|
|
|
|
|
/* ----------------------- Variables ----------------------------------------*/
|
2021-10-12 11:06:13 -04:00
|
|
|
static xTimerContext_t* pxTimerContext = NULL;
|
2018-10-19 09:51:27 -04:00
|
|
|
|
2021-10-12 11:06:13 -04:00
|
|
|
/* ----------------------- Start implementation -----------------------------*/
|
|
|
|
static void IRAM_ATTR vTimerAlarmCBHandler(void *param)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
2021-10-12 11:06:13 -04:00
|
|
|
pxMBMasterPortCBTimerExpired(); // Timer expired callback function
|
|
|
|
pxTimerContext->xTimerState = TRUE;
|
|
|
|
ESP_EARLY_LOGD(TAG, "Timer mode: (%d) triggered", xMBMasterGetCurTimerMode());
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
|
|
|
|
{
|
|
|
|
MB_PORT_CHECK((usTimeOut50us > 0), FALSE,
|
|
|
|
"Modbus timeout discreet is incorrect.");
|
2021-10-12 11:06:13 -04:00
|
|
|
MB_PORT_CHECK(!pxTimerContext, FALSE,
|
|
|
|
"Modbus timer is already created.");
|
|
|
|
pxTimerContext = calloc(1, sizeof(xTimerContext_t));
|
|
|
|
if (!pxTimerContext) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
pxTimerContext->xTimerIntHandle = NULL;
|
2018-10-19 09:51:27 -04:00
|
|
|
// Save timer reload value for Modbus T35 period
|
2021-10-12 11:06:13 -04:00
|
|
|
pxTimerContext->usT35Ticks = usTimeOut50us;
|
|
|
|
esp_timer_create_args_t xTimerConf = {
|
|
|
|
.callback = vTimerAlarmCBHandler,
|
|
|
|
.arg = NULL,
|
|
|
|
#if CONFIG_FMB_TIMER_USE_ISR_DISPATCH_METHOD
|
|
|
|
.dispatch_method = ESP_TIMER_ISR,
|
|
|
|
#else
|
|
|
|
.dispatch_method = ESP_TIMER_TASK,
|
|
|
|
#endif
|
|
|
|
.name = "MBM_T35timer"
|
2021-09-27 00:46:51 -04:00
|
|
|
};
|
2021-10-12 11:06:13 -04:00
|
|
|
// Create Modbus timer
|
|
|
|
esp_err_t xErr = esp_timer_create(&xTimerConf, &(pxTimerContext->xTimerIntHandle));
|
|
|
|
if (xErr) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-10-19 09:51:27 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-10-12 11:06:13 -04:00
|
|
|
// Set timer alarm value
|
|
|
|
static BOOL xMBMasterPortTimersEnable(uint64_t xToutUs)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
2021-10-12 11:06:13 -04:00
|
|
|
MB_PORT_CHECK(pxTimerContext && (pxTimerContext->xTimerIntHandle), FALSE,
|
|
|
|
"timer is not initialized.");
|
|
|
|
MB_PORT_CHECK((xToutUs > 0), FALSE,
|
|
|
|
"incorrect tick value for timer = (0x%llu).", xToutUs);
|
|
|
|
esp_timer_stop(pxTimerContext->xTimerIntHandle);
|
|
|
|
esp_timer_start_once(pxTimerContext->xTimerIntHandle, xToutUs);
|
|
|
|
pxTimerContext->xTimerState = FALSE;
|
2018-10-19 09:51:27 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void vMBMasterPortTimersT35Enable(void)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
2021-10-12 11:06:13 -04:00
|
|
|
#if CONFIG_FMB_TIMER_PORT_ENABLED
|
|
|
|
uint64_t xToutUs = (pxTimerContext->usT35Ticks * MB_TIMER_TICK_TIME_US);
|
2018-10-19 09:51:27 -04:00
|
|
|
|
|
|
|
// Set current timer mode, don't change it.
|
|
|
|
vMBMasterSetCurTimerMode(MB_TMODE_T35);
|
2021-10-12 11:06:13 -04:00
|
|
|
// Set timer alarm
|
|
|
|
(void)xMBMasterPortTimersEnable(xToutUs);
|
|
|
|
#endif
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void vMBMasterPortTimersConvertDelayEnable(void)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
|
|
|
// Covert time in milliseconds into ticks
|
2021-10-12 11:06:13 -04:00
|
|
|
uint64_t xToutUs = (MB_MASTER_DELAY_MS_CONVERT * 1000);
|
2018-10-19 09:51:27 -04:00
|
|
|
|
|
|
|
// Set current timer mode
|
|
|
|
vMBMasterSetCurTimerMode(MB_TMODE_CONVERT_DELAY);
|
|
|
|
ESP_LOGD(MB_PORT_TAG,"%s Convert delay enable.", __func__);
|
2021-10-12 11:06:13 -04:00
|
|
|
(void)xMBMasterPortTimersEnable(xToutUs);
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void vMBMasterPortTimersRespondTimeoutEnable(void)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
2021-10-12 11:06:13 -04:00
|
|
|
uint64_t xToutUs = (MB_MASTER_TIMEOUT_MS_RESPOND * 1000);
|
2018-10-19 09:51:27 -04:00
|
|
|
|
|
|
|
vMBMasterSetCurTimerMode(MB_TMODE_RESPOND_TIMEOUT);
|
|
|
|
ESP_LOGD(MB_PORT_TAG,"%s Respond enable timeout.", __func__);
|
2021-10-12 11:06:13 -04:00
|
|
|
(void)xMBMasterPortTimersEnable(xToutUs);
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
2019-11-08 03:55:42 -05:00
|
|
|
void MB_PORT_ISR_ATTR
|
|
|
|
vMBMasterPortTimersDisable()
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
2021-10-12 11:06:13 -04:00
|
|
|
// Disable timer alarm
|
|
|
|
esp_timer_stop(pxTimerContext->xTimerIntHandle);
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void vMBMasterPortTimerClose(void)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
2021-10-12 11:06:13 -04:00
|
|
|
// Delete active timer
|
|
|
|
if (pxTimerContext) {
|
|
|
|
if (pxTimerContext->xTimerIntHandle) {
|
|
|
|
esp_timer_stop(pxTimerContext->xTimerIntHandle);
|
|
|
|
esp_timer_delete(pxTimerContext->xTimerIntHandle);
|
|
|
|
}
|
|
|
|
free(pxTimerContext);
|
|
|
|
pxTimerContext = NULL;
|
|
|
|
}
|
2019-12-10 01:27:09 -05:00
|
|
|
}
|