armink a5528950d0 1、【修改】“UserModbusSlaver”相关文件前缀为“user_app”,主机为“user_app_m”,从机为“user_app_s”,主从机公用一个头文件,同时将相关文件移动到/FreeModbus/port下;
2、【修改】FreeModbus的port文件夹下相关文件的命名方式,从机保留FreeModbus原有的文件,主机则在文件名后面增加“_m”以区分,尽量保证FreeModbus源码的统一性;
3、【增加】FreeModbus主机初始化、使能、失能及定时器状态机相关方法,定时器超时状态机还需要后期完善,发送状态机及接收状态机还未实现;

Signed-off-by: armink <armink.ztl@gmail.com>
2013-08-17 17:13:52 +08:00

108 lines
3.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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 "mbport.h"
/* ----------------------- static functions ---------------------------------*/
static void prvvTIMERExpiredISR(void);
/* ----------------------- Start implementation -----------------------------*/
BOOL xMBMasterPortTimersInit(USHORT usTim1Timerout50us)
{
uint16_t PrescalerValue = 0;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//====================================ʱ<>ӳ<EFBFBD>ʼ<EFBFBD><CABC>===========================
//ʹ<>ܶ<EFBFBD>ʱ<EFBFBD><CAB1><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
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(TIM2, &TIM_TimeBaseStructure);
//Ԥװ<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 vMBMasterPortTimersEnable()
{
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();
}