forked from Barfell/modbus-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mb_port.c
145 lines (131 loc) · 3.34 KB
/
mb_port.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
******************************************************************************
* @file mb_port.c
* @author Derrick Wang
* @brief modebus移植接口
******************************************************************************
* @note
* 该文件为modbus移植接口的实现,根据不同的MCU平台进行移植
******************************************************************************
*/
#include "mb_include.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
void mb_port_uartInit(uint32_t baud,uint8_t parity)
{
/*串口部分初始化*/
huart1.Instance = USART1;
huart1.Init.BaudRate = baud;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
if(parity==MB_PARITY_ODD)
{
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_ODD;
}
else if(parity==MB_PARITY_EVEN)
{
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_EVEN;
}
else
{
huart1.Init.StopBits = UART_STOPBITS_2;
huart1.Init.Parity = UART_PARITY_NONE;
}
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
void mb_port_uartEnable(uint8_t txen,uint8_t rxen)
{
if(txen)
{
__HAL_UART_ENABLE_IT(&huart1,UART_IT_TXE);
}
else
{
__HAL_UART_DISABLE_IT(&huart1,UART_IT_TXE);
}
if(rxen)
{
__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);
}
else
{
__HAL_UART_DISABLE_IT(&huart1,UART_IT_RXNE);
}
}
void mb_port_putchar(uint8_t ch)
{
huart1.Instance->DR = ch; //直接操作寄存器比HAL封装的更高效
}
void mb_port_getchar(uint8_t *ch)
{
*ch= (uint8_t)(huart1.Instance->DR & (uint8_t)0x00FF);
}
void mb_port_timerInit(uint32_t baud)
{
/*定时器部分初始化*/
htim3.Instance = TIM3;
htim3.Init.Prescaler = 71;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
if(baud>19200) //波特率大于19200固定使用1800作为3.5T
{
htim3.Init.Period = 1800;
}
else //其他波特率的需要根据计算
{
/* us=1s/(baud/11)*1000000*3.5
* =(11*1000000*3.5)/baud
* =38500000/baud
*/
htim3.Init.Period = (uint32_t)38500000/baud;
}
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
}
void mb_port_timerEnable()
{
__HAL_TIM_DISABLE(&htim3);
__HAL_TIM_CLEAR_IT(&htim3,TIM_IT_UPDATE); //清除中断位
__HAL_TIM_ENABLE_IT(&htim3,TIM_IT_UPDATE); //使能中断位
__HAL_TIM_SET_COUNTER(&htim3,0); //设置定时器计数为0
__HAL_TIM_ENABLE(&htim3); //使能定时器
}
void mb_port_timerDisable()
{
__HAL_TIM_DISABLE(&htim3);
__HAL_TIM_SET_COUNTER(&htim3,0);
__HAL_TIM_DISABLE_IT(&htim3,TIM_IT_UPDATE);
__HAL_TIM_CLEAR_IT(&htim3,TIM_IT_UPDATE);
}
//串口中断服务函数
void USART1_IRQHandler()
{
HAL_NVIC_ClearPendingIRQ(USART1_IRQn);
if((__HAL_UART_GET_FLAG(&huart1,UART_FLAG_RXNE)!=RESET))
{
__HAL_UART_CLEAR_FLAG(&huart1,UART_FLAG_RXNE);
mbh_uartRxIsr();
}
if((__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TXE)!=RESET))
{
__HAL_UART_CLEAR_FLAG(&huart1,UART_FLAG_TXE);
mbh_uartTxIsr();
}
}
//定时器中断服务函数
void TIM3_IRQHandler()
{
__HAL_TIM_CLEAR_IT(&htim3,TIM_IT_UPDATE);
mbh_timer3T5Isr();
}