2013-11-06 21:47:49 +08:00
|
|
|
|
/*
|
|
|
|
|
* FreeModbus Libary: STM32 Port
|
|
|
|
|
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*
|
|
|
|
|
* 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.h"
|
|
|
|
|
#include "mb_m.h"
|
|
|
|
|
#include "mbport.h"
|
|
|
|
|
|
|
|
|
|
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED
|
|
|
|
|
/* ----------------------- Variables ----------------------------------------*/
|
|
|
|
|
static USHORT usT35TimeOut50us;
|
|
|
|
|
static USHORT usPrescalerValue = 0;
|
|
|
|
|
|
|
|
|
|
/* ----------------------- static functions ---------------------------------*/
|
|
|
|
|
static void prvvTIMERExpiredISR(void);
|
|
|
|
|
|
|
|
|
|
/* ----------------------- Start implementation -----------------------------*/
|
|
|
|
|
BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
|
|
|
|
|
{
|
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
|
//====================================ʱ<>ӳ<EFBFBD>ʼ<EFBFBD><CABC>===========================
|
|
|
|
|
//ʹ<>ܶ<EFBFBD>ʱ<EFBFBD><CAB1>2ʱ<32><CAB1>
|
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
|
|
|
|
//====================================<3D><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>===========================
|
|
|
|
|
//<2F><>ʱ<EFBFBD><CAB1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>
|
|
|
|
|
//HCLKΪ72MHz<48><7A>APB1<42><31><EFBFBD><EFBFBD>2<EFBFBD><32>ƵΪ36MHz
|
|
|
|
|
//TIM2<4D><32>ʱ<EFBFBD>ӱ<EFBFBD>Ƶ<EFBFBD><C6B5>Ϊ72MHz<48><7A>Ӳ<EFBFBD><D3B2><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>Ƶ,<2C>ﵽ<EFBFBD><EFB5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//TIM2<4D>ķ<EFBFBD>Ƶϵ<C6B5><CFB5>Ϊ3599<39><39>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>Ϊ72 / (1 + Prescaler) = 20KHz,<2C><>Ϊ50us
|
|
|
|
|
//TIM<49><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪusTim1Timerout50u
|
|
|
|
|
usPrescalerValue = (uint16_t) (SystemCoreClock / 20000) - 1;
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>T35<33><35>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
usT35TimeOut50us = usTimeOut50us;
|
|
|
|
|
|
|
|
|
|
//Ԥװ<D4A4><D7B0>ʹ<EFBFBD><CAB9>
|
|
|
|
|
TIM_ARRPreloadConfig(TIM2, ENABLE);
|
|
|
|
|
//====================================<3D>жϳ<D0B6>ʼ<EFBFBD><CABC>===========================
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>NVIC<49><43><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD>ΪGroup2<70><32>0-3<><33>ռʽ<D5BC><CABD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>0-3<><33><EFBFBD><EFBFBD>Ӧʽ<D3A6><CABD><EFBFBD>ȼ<EFBFBD>
|
|
|
|
|
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
|
|
|
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
|
|
|
|
//<2F><>ʱ<EFBFBD><CAB1>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD>жϹر<CFB9>
|
|
|
|
|
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
|
|
|
|
|
//<2F><>ʱ<EFBFBD><CAB1>3<EFBFBD><33><EFBFBD><EFBFBD>
|
|
|
|
|
TIM_Cmd(TIM2, DISABLE);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vMBMasterPortTimersT35Enable()
|
|
|
|
|
{
|
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|
|
|
|
|
|
|
|
|
/* Set current timer mode,don't change it.*/
|
|
|
|
|
vMBMasterSetCurTimerMode(MB_TMODE_T35);
|
|
|
|
|
|
|
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = usPrescalerValue;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_Period = (uint16_t) usT35TimeOut50us;
|
|
|
|
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
|
|
|
|
|
|
|
|
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
|
|
|
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
|
|
|
|
TIM_SetCounter(TIM2, 0);
|
|
|
|
|
TIM_Cmd(TIM2, ENABLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vMBMasterPortTimersConvertDelayEnable()
|
|
|
|
|
{
|
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|
|
|
|
|
|
|
|
|
/* Set current timer mode,don't change it.*/
|
|
|
|
|
vMBMasterSetCurTimerMode(MB_TMODE_CONVERT_DELAY);
|
|
|
|
|
|
|
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = usPrescalerValue;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_Period = (uint16_t)(MB_MASTER_DELAY_MS_CONVERT * 1000 / 50);
|
|
|
|
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
|
|
|
|
|
|
|
|
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
|
|
|
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
|
|
|
|
TIM_SetCounter(TIM2, 0);
|
|
|
|
|
TIM_Cmd(TIM2, ENABLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vMBMasterPortTimersRespondTimeoutEnable()
|
|
|
|
|
{
|
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|
|
|
|
|
|
|
|
|
/* Set current timer mode,don't change it.*/
|
|
|
|
|
vMBMasterSetCurTimerMode(MB_TMODE_RESPOND_TIMEOUT);
|
|
|
|
|
|
|
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = usPrescalerValue;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
|
|
|
TIM_TimeBaseStructure.TIM_Period = (uint16_t)(MB_MASTER_TIMEOUT_MS_RESPOND * 1000 / 50);
|
|
|
|
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
|
|
|
|
|
|
|
|
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
|
|
|
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
|
|
|
|
TIM_SetCounter(TIM2, 0);
|
|
|
|
|
TIM_Cmd(TIM2, ENABLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vMBMasterPortTimersDisable()
|
|
|
|
|
{
|
|
|
|
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
|
|
|
|
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
|
|
|
|
|
TIM_SetCounter(TIM2, 0);
|
|
|
|
|
TIM_Cmd(TIM2, DISABLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void prvvTIMERExpiredISR(void)
|
|
|
|
|
{
|
|
|
|
|
(void) pxMBMasterPortCBTimerExpired();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TIM2_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
TIM_ClearFlag(TIM2, TIM_FLAG_Update); //<2F><><EFBFBD>жϱ<D0B6><CFB1><EFBFBD>
|
|
|
|
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>TIM2<4D><32><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
|
|
|
|
|
prvvTIMERExpiredISR();
|
|
|
|
|
}
|
|
|
|
|
rt_interrupt_leave();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|