FreeModbus_Slave-Master-RTT.../FreeModbus/port/porttimer.c

108 lines
3.7 KiB
C
Raw Normal View History

/*
* 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.c,v 1.60 2013/08/13 15:07:05 Armink $
*/
/* ----------------------- Platform includes --------------------------------*/
#include "port.h"
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
/* ----------------------- static functions ---------------------------------*/
static void prvvTIMERExpiredISR(void);
/* ----------------------- Start implementation -----------------------------*/
BOOL xMBPortTimersInit(USHORT usTim1Timerout50us)
{
uint16_t PrescalerValue = 0;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//====================================ʱ<>ӳ<EFBFBD>ʼ<EFBFBD><CABC>===========================
//ʹ<>ܶ<EFBFBD>ʱ<EFBFBD><CAB1><33><CAB1>
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, 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
//TIM3<4D><33>ʱ<EFBFBD>ӱ<EFBFBD>Ƶ<EFBFBD><C6B5>Ϊ72MHz<48><7A>Ӳ<EFBFBD><D3B2><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>Ƶ,<2C><EFBFBD><EFB5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//TIM3<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
PrescalerValue = (uint16_t) (SystemCoreClock / 20000) - 1;
//<2F><>ʱ<EFBFBD><CAB1>1<EFBFBD><31>ʼ<EFBFBD><CABC>
TIM_TimeBaseStructure.TIM_Period = (uint16_t) usTim1Timerout50us;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//Ԥװ<D4A4><D7B0>ʹ<EFBFBD><CAB9>
TIM_ARRPreloadConfig(TIM3, 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 = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
//<2F><>ʱ<EFBFBD><CAB1>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD>жϹر<CFB9>
TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
//<2F><>ʱ<EFBFBD><CAB1>3<EFBFBD><33><EFBFBD><EFBFBD>
TIM_Cmd(TIM3, DISABLE);
return TRUE;
}
void vMBPortTimersEnable()
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
}
void vMBPortTimersDisable()
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, DISABLE);
}
void prvvTIMERExpiredISR(void)
{
(void) pxMBPortCBTimerExpired();
}
void TIM3_IRQHandler(void)
{
rt_interrupt_enter();
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearFlag(TIM3, TIM_FLAG_Update); //<2F><><EFBFBD>жϱ<D0B6><CFB1><EFBFBD>
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>T3<54><33><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
prvvTIMERExpiredISR();
}
rt_interrupt_leave();
}