|
1.新建项目
- y" K$ b) h! P4 q# b
在PIO的Home页面新建项目,项目名称freertos_modbus,选择开发板为 MonkeyPi_STM32_G070RB,开发框架选择libopencm3; - 1upload_protocol = cmsis-dap6 k$ t6 \* r) Z+ Q3 @7 x
- 2debug_tool = cmsis-dap
复制代码 + ~( G8 c, c, B% b0 S
! A) Q& |6 ~" _. e: c9 A* _2 ?) K* z
2 编写程序2.1 添加 freeModbus 库然后从FreeModbus源码中的 demo\BARE\port中复制文件到工程的src\modbus_port文件夹下,最后的文件夹结构如下: 这里使用RS485,因此需要对RS485使能端口进行配置,其他为串口的配置,然后在发送和接收中断时候调用modbus相关接口进行处理: 3 J* A' T7 `8 Q! J
- 1{
3 O2 n4 K9 l- D% b8 ] - 2 "name": "FreeModbus",' C6 j. T1 L. M. j2 e
- 3 "version": "master",
6 m- {5 y2 K' y! {( t - 4 "repository":{6 I `4 F4 T+ z3 K4 L: c, e
- 5 "type":"git"," n2 C' p- e* ^( V9 F( W0 X: w
- 6 "url":"https://github.com/cwalter-at/freemodbus"
! D$ r# @! N" ~: @5 P3 j E - 7 },
, ~+ I" f4 ~! o* o- c! b% i! {6 w - 8 "build": {
% l* W' A$ d* A; f a+ q C; M - 9 "flags": [ U) ]7 _7 P* w1 ~- o
- 10 "-Iascii",
8 E- A5 e+ c0 i - 11 "-Ifunctions",: f' x/ W9 R S$ _% @8 T. Q
- 12 "-Iinclude",
: x; c4 _6 i6 s4 A+ T( n. q - 13 "-Irtu",) k8 l, G' i6 S* C: \( O: ~* b7 t
- 14 "-Itcp"
2 y! \ g% k7 K# W8 l - 15 ],
8 W0 k% D! v) T, N# m! c" R - 16 "srcFilter": [; O/ u& p1 I" B/ s# e( u
- 17 "+<*>"! y9 V$ E9 x. \) _3 [' A
- 18 ]
, h8 |+ k: x# c1 ?( g) o. y - 19 }1 ` F' b$ ?" `7 ^2 f) m8 N
- 20}
复制代码
1 a! V% l1 N9 G" \% U$ J7 V+ o* Y) T: v& y
然后从FreeModbus源码中的 demo\BARE\port中复制文件到工程的src\modbus_port文件夹下,最后的文件夹结构如下:! g/ w# }/ n: E# c5 R
, K" M6 R1 J; V9 \5 J
* K0 |6 A2 r. e
/ D3 \& d z, z, J8 @
' L3 ^0 q% V9 T/ l+ W: f& g& D2.2移植
2 i+ o- G5 L. ?% Zportevent:
, T2 U+ T2 v* B1 w, ?" ]6 u. p- 1/* ----------------------- Modbus includes ----------------------------------*/
; H" e2 D* r; d* ^ - 2#include "mb.h". I. X8 m8 P/ h ?2 z+ o
- 3#include "mbport.h"
: U# o1 N% T( H* R/ I, a - 4#include "FreeRTOS.h"
' m+ C+ K! s1 K% @5 i) y8 _ - 5#include "task.h"( @! H' S; W4 u1 H+ f
- 6
! i$ L; y" o; H/ X A' N - 7/* ----------------------- Variables ----------------------------------------*/3 O; j' n0 E# u* Z, r
- 8static eMBEventType eQueuedEvent;4 G& {5 i* ]1 ^
- 9static BOOL xEventInQueue; J `. Y( k4 {! e# C7 S; s# X+ L
- 10static uint32_t modbus_last_active_time = 0;/ T) V" E- u2 X- O( k" ^
- 11
8 e/ c( W m5 A - 12uint32_t get_modbus_last_active_time(void)$ A2 X+ h2 y/ j/ l! ?/ |" x7 L
- 13{: W1 Z; I; {7 S$ I) Z# G" T, o8 d
- 14 return modbus_last_active_time;
. z: N2 A* f! g7 K4 n q& F - 15}, A4 B9 n- x" J
- 16
}7 ~# B; y5 k" ?! T, Y - 17/* ----------------------- Start implementation -----------------------------*/; W# y5 q6 X# ]: p; B, }
- 18BOOL
5 T+ O6 H7 z5 M! c' Y - 19xMBPortEventInit( void )
H# B+ g( ^( x. e7 p2 I- D8 N6 v - 20{
+ Y" ?1 O5 N5 I$ i* w - 21 xEventInQueue = FALSE;6 ] |" c. d5 j, i- k
- 22 return TRUE;$ G6 Z4 [( p( h+ f6 m' u
- 23}3 F$ m7 W! M2 ^! y, |
- 24
# W7 S& }( {/ c* d% B: F - 25BOOL
1 v+ M2 N( D: Y2 ?7 S - 26xMBPortEventPost( eMBEventType eEvent )
7 J$ g5 {3 l! a* f/ K0 ~/ B& _4 D( ^ - 27{' v- T" U4 _4 C" ?1 H& o. ]
- 28 xEventInQueue = TRUE;
. B5 M; I& t, m. v - 29 eQueuedEvent = eEvent;' C" Y5 ~+ i% n" W/ M
- 30
2 x5 z" J2 Q0 c9 v/ _ - 31 if (eEvent == EV_EXECUTE) {0 X; W4 e: T& P |; y$ U+ ~! a
- 32 modbus_last_active_time = xTaskGetTickCount();
! X. ?' `( z: e* W - 33 }( @9 W, g! } r4 x5 V
- 34 return TRUE;* p& v. r* a* H1 [9 k
- 35}
8 f/ g/ N9 C) M; g - 36
: X7 _9 k/ a. W9 q* [ @3 Z - 37BOOL
* O ?2 `) l q' _) r - 38xMBPortEventGet( eMBEventType * eEvent )
6 g* Z. X$ Q0 F( C7 f - 39{9 C! p/ ~1 G# i: m( F6 \* R
- 40 BOOL xEventHappened = FALSE;' C1 a0 [! }9 m3 [: [$ W% ]
- 41
' w3 d" l% r/ R# X - 42 if( xEventInQueue )
4 p, H5 z0 v2 k# P - 43 {% c5 t2 a* D0 y0 M1 m; i
- 44 *eEvent = eQueuedEvent;; k- v2 v6 L- R6 x4 m6 p4 m3 k+ y
- 45 xEventInQueue = FALSE;
# n" w& u1 W6 C, q$ E7 H6 y) x5 _ - 46 xEventHappened = TRUE;' g& R$ W( j1 y5 t
- 47 }; e4 _2 g+ ?( O* O* m
- 48 return xEventHappened;
& ~! p# F6 r% h* Q8 j - 49}
复制代码
# H" U# d, F' D3 e& u1 B+ \# @) L9 U3 s. ^! y/ i1 k
portserial
- X7 |9 k) F. y3 W这里使用RS485,因此需要对RS485使能端口进行配置,其他为串口的配置,然后在发送和接收中断时候调用modbus相关接口进行处理:
% u+ x; H. {) f8 S5 [0 c4 w4 h
3 ^0 H3 P2 [' J8 M. v- 1#include "port.h"
- V; a; [ ^/ B4 J - 2
1 [: d# b5 k+ e) P6 |) Y# `4 g - 3#include "FreeRTOS.h"2 Z- h% T T2 {# y% E. U
- 4#include "queue.h"
" f1 g- p6 Q) y) p7 E' |7 ?2 h - 51 Z! ^: J' H/ d0 ~! K. E
- 6#include <libopencm3/cm3/nvic.h>
" [. `) ^; ?+ E z) { - 7#include <libopencm3/stm32/usart.h>
1 P+ G: U" e& I7 ?' ~* A. x! ~ - 8#include <libopencm3/stm32/rcc.h>
; _$ U ^' ^" x - 9#include <libopencm3/stm32/gpio.h>0 Q B3 V$ {/ L% l& `' K
- 10
( Z% i! ^6 B. { - 11/* ----------------------- Modbus includes ----------------------------------*/8 `% R6 _' B8 ~- H
- 12#include "mb.h"
! `+ W2 N, S( o8 b/ q - 13#include "mbport.h"6 ?* K" v/ ^% e K& e9 Y/ F1 ?
- 14) R ?& y$ n; {- x1 K L
- 15/* ----------------------- static functions ---------------------------------*/
6 q0 |& L. [& ~9 M6 n - 16" f+ F! T9 c8 j# q6 S
- 17xQueueHandle uart_queue;% E5 h5 }, g3 l
- 18
3 U7 K6 j4 S$ k4 g# W. ? - 19#define RS485_1_CLOCK RCC_GPIOB
- M# R( G" M; K' p, @ - 20#define RS485_1_EN_PORT GPIOB
2 W8 X" L% n/ J# D0 M - 21#define RS485_1_EN_PIN GPIO8
+ o& @7 j3 U0 C$ F2 T - 22# y" {4 [: O3 x# _/ J0 _: K& Z
- 23static void rs485_delay(int n)
6 a% e, ?' C: P* L. u ^ - 24{
) ]' J8 \: K# P& a4 q" h - 25 while (--n) {/ y- N1 d q, H$ l/ }3 ~ T( T- K1 [
- 26 __asm__ volatile ("nop");
0 D/ `3 d/ d+ p; Y - 27 }
6 n/ A3 Y6 c# x. I" L, Z8 n; I - 28}( v" H) w. h3 e D+ Q" w# i" L, ?& K
- 29% B6 n4 P2 [& [7 o
- 30static inline void rs485_1_rx_mode(void)
, n! p; B0 G. O0 O2 \ - 31{
; j0 q! U/ @ S% E - 32 gpio_clear(RS485_1_EN_PORT, RS485_1_EN_PIN);- F' }7 N. K& ?( j- C! W c
- 33}
$ U) L; e, a8 v - 348 [* p1 e- n) X' u* ~
- 35static inline void rs485_1_tx_mode(void)% T3 u8 k- Y1 k/ e; C, o) P( Y, B
- 36{
K0 z& Q$ b' T9 h D4 f' f; l - 37 gpio_set(RS485_1_EN_PORT, RS485_1_EN_PIN);/ r- l6 Q6 P5 E* y1 ]: b% b- N
- 38}8 v& T, T( b' g: S( @1 d0 B, \
- 39
$ Y# K; m7 J& I6 M! s - 40static inline void rs485_gpio_init(void)
" B8 ?$ G7 O* [1 E - 41{
' U, o4 m8 E3 S; F' O3 V9 O0 Y - 42 rcc_periph_clock_enable(RS485_1_CLOCK);
+ H% t" ]- C$ C, V( s* [3 q - 43 gpio_mode_setup(RS485_1_EN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_1_EN_PIN);
% K$ S0 s6 p/ v4 O% f - 445 ]2 {/ S, w3 c7 T6 z# e& Q7 c# }
- 45 rs485_1_rx_mode();
5 x* O3 w) e* v0 P& A) ] - 46}9 B0 }/ ]2 n U( c, \
- 47
6 W9 t; C/ ^% T' F - 48/* ----------------------- Start implementation -----------------------------*/& [( {* z* j9 S9 e% y
- 49void
" ^3 s. R0 ~# I - 50vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
+ s8 z9 ` ?1 t$ i - 51{$ B# ?# \- l6 K1 ~1 V
- 52 /* If xRXEnable enable serial receive interrupts. If xTxENable enable' m3 V4 }4 t4 N5 D5 x
- 53 * transmitter empty interrupts.
+ m* i* d) R' m+ \6 p - 54 */
! H& L0 K( ^8 f- J% F& h - 55 if (xRxEnable) {
B A2 q+ L: d3 n0 w U - 56 rs485_delay(10000);9 T1 S/ C9 x9 I7 e* k0 X6 k, l: F# I
- 57 rs485_1_rx_mode(); q7 u9 B# ?0 x1 t4 D( U
- 58 rs485_delay(10000);; ?/ G4 I" k* |& N( c L$ v
- 59 usart_enable_rx_interrupt(USART1);+ Y$ E0 ?% L& u- r7 x( Y
- 60 }. F& ^8 p* ^' O" n; G+ [
- 61 else {5 O3 G9 P' w' d5 x' H7 F( _3 }& [
- 62 usart_disable_rx_interrupt(USART1);! E$ M. I d3 S. ^# g7 _. t" I& `
- 63 }
1 m( ]2 s( T, r0 j4 R3 E - 64
( L4 ?; J7 H/ ?+ R( P# M - 65 if (xTxEnable) {1 D/ U Q% c3 V% g
- 66 rs485_delay(10000);
0 l. [0 x5 V9 z8 N6 i, @ - 67 rs485_1_tx_mode();' f, P' T: t3 o+ V" [' M( Z
- 68 rs485_delay(10000);8 o- o. B" I* v, U
- 69 usart_enable_tx_interrupt(USART1);
- G, K$ P" `7 k+ r/ |! T - 70 } _0 ?( k. t$ { d
- 71 else {, Y O' G: C3 o* x5 ]+ D
- 72 usart_disable_tx_interrupt(USART1);
0 w' X4 V. ~7 c3 P/ a- P4 q" A0 K - 73) \6 j/ V' F7 d
- 74 }8 O2 @3 w( f9 |" g" u
- 75}9 ~0 R0 Z& Q/ W* I) O4 q
- 767 j' `8 K" A! ?( Y9 h) [! i
- 77BOOL
4 N2 Y8 i0 a, }& E4 J - 78xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
/ e- O8 t+ X$ l3 W% P - 79{/ x: Y- H* P e8 \) h
- 80 nvic_enable_irq(NVIC_USART1_IRQ);# J5 r# Q! Q7 |: t, H) v- d
- 81
# R8 r2 C" m5 x5 z- ` - 82 rcc_periph_clock_enable(RCC_GPIOB);$ i; y! j. o# T
- 83 gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO6 | GPIO7);
+ n4 G2 C+ g* N B$ A b2 A - 84 gpio_set_af(GPIOB, GPIO_AF0, GPIO6 | GPIO7);* g( J7 E+ ^$ E+ M) [
- 85# _* ~$ [7 v9 B6 V
- 86 rcc_periph_clock_enable(RCC_USART1);" e# X) y$ D) i0 K8 \) z: s
- 87
+ Q* \5 j2 ]/ B, x. q5 T+ k - 88 /* Set up USART/UART parameters using the libopencm3 helper functions */
- X* _: r7 J. b/ h3 I: V2 {) c# p0 c - 89 usart_set_baudrate(USART1, ulBaudRate);# c! Z, p4 s* b2 d0 h
- 90 usart_set_databits(USART1, ucDataBits);3 T! x+ _. @5 m; s$ g
- 91 usart_set_stopbits(USART1, USART_STOPBITS_1);
u$ `* u$ P! S% d! i, ` - 92 usart_set_mode(USART1, USART_MODE_TX_RX);& u9 ?7 G5 B; i: B$ g! a
- 933 k$ i# M6 L& _
- 94 switch (eParity) {
, k8 ?6 @% C2 b/ o6 C - 95 case MB_PAR_ODD:
& s3 d* ~: h# E% s8 [8 S' U - 96 usart_set_parity(USART1, USART_PARITY_ODD);" R3 r8 ?8 u0 ?8 p
- 97 break;
. |- f1 _# N [* H) p0 d3 A - 98 case MB_PAR_EVEN:' k t0 }+ ^1 P( t
- 99 usart_set_parity(USART1, USART_PARITY_EVEN);
/ ?3 ?- L, o& ~5 t% Q9 y' ` - 100 break;+ v y7 `' T, s% }
- 101 default:
. Y2 b5 Y! `. |" x& y - 102 usart_set_parity(USART1, USART_PARITY_NONE);
8 z# Y% o; B: x3 P - 103 break;$ @) f8 s+ Q* [ j) F
- 104 }5 s9 i. N, `+ T8 D& Z1 B
- 105/ U3 X: F$ n# }$ y6 I% _4 j
- 106 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);% ~( I9 ]6 ~5 I, o( u" U
- 1074 I" A' a& O4 X) [1 w9 |% t
- 108 usart_enable(USART1);
& |9 M |& L, E& w1 H! A; m - 109 Z( c1 b! y2 T
- 110 rs485_gpio_init();
, l0 C. u' ?. D: Y - 111$ Q+ A) q: P! E3 b, w8 d9 T7 Z! a
- 112 return TRUE;
: ~0 T5 l; ]$ w - 113}. B: v+ @' _% G- Y: x
- 1147 ?+ C4 g9 n4 ^, }
- 115BOOL
4 i i \0 f6 d% q6 h - 116xMBPortSerialPutByte( CHAR ucByte )
. h6 V( j' L! y) ? - 117{. y$ ]2 P) z) W. Z- Q
- 118' F: {6 @" X8 X
- 119 usart_send_blocking(USART1, (uint16_t) ucByte); 3 Q `( }1 \6 U7 \ [* P# ^
- 1207 d0 K; h3 n [7 Y
- 121 return TRUE;! x/ p, }7 s% x' Y* W. v, c
- 122}
# l6 F; N' j0 P6 H Y" A* V - 123
* l" K3 g* K( | - 124BOOL
: O/ n9 A. ~1 e; j' B - 125xMBPortSerialGetByte( CHAR * pucByte )
5 T( P8 B. y _5 _& O" b - 126{
/ I' m0 h* a' J* ^+ j - 127 *pucByte = usart_recv(USART1);
) @3 K; y4 @% g& z - 128
H& Y6 K: }/ O - 129 return TRUE;. Q5 S8 {: E8 n+ Y' A
- 130}
3 F/ B. N! @: r w - 131* S2 D& C8 ]- C2 r1 A7 H" H4 Z9 b" j
- 132
& p0 J3 }9 K3 C" q. d! l - 133uint32_t uart1_isr, uart1_icr;
, [, g8 \# n$ Y, c H' M( L - 1349 L" V& m ~; c% B
- 135void usart1_isr(void)
! f' n$ q B+ W' U7 p - 136{
: |& i! k8 Z' n- a. t/ U - 137
& E( I0 j4 b9 E v2 ` - 138 /* Check if we were called because of RXNE. */8 G& M' N7 m( Z+ b$ [) X
- 139 if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) &&
' Y7 c7 Z; E5 f. ]6 ?9 K; R - 140 ((USART_ISR(USART1) & USART_ISR_RXNE) != 0)) {
# @1 ]; P/ X9 L* ?% y+ L R - 141
& L3 Y) C2 \: [4 R - 142 /* Retrieve the data from the peripheral. */
; f4 Q) X* v, ^9 k! ?5 D' S - 143 // usart_recv(USART1);+ m3 R" h) s! p; l2 n, D4 G
- 144; Q7 [# L W/ p) Y+ a5 S! H
- 145 pxMBFrameCBByteReceived();" E& A: w0 b7 R) K
- 146% P4 W4 j) a) d* w
- 147 }* }% D6 t7 `, x3 v% J
- 148
' v4 v# j I+ f8 k" a# `# A - 149. a; l5 K) O7 b6 X
- 150 /* Check if we were called because of TXE. */2 V$ t3 P9 o' p g3 q- u w
- 151 if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) &&3 B* Y* B! H# M" [) M
- 152 ((USART_ISR(USART1) & USART_ISR_TXE) != 0)) {
" _2 N/ D+ F3 x' [$ B - 153* Z8 m7 r& m# ?7 S- ~/ P! d
- 154 /* Put data into the transmit register. */5 d; K" V% h9 T6 w+ n
- 155 //usart_send(USART1, data);
+ H- N8 F% R7 L1 A3 z) E - 156
* I, d5 e m+ U' A - 157 pxMBFrameCBTransmitterEmpty();& o% s" ^8 X& c( m& X2 Q' k7 W0 R
- 158! z$ C" l0 }; |9 N' A
- 159 }
0 u3 \5 Q1 C7 L7 [/ j- Y - 160
r) k% r. [4 p6 }* h/ U - 161}
复制代码 porttimer
" L; w2 i! j" @, L9 C* p- 1#include "port.h"
' h* k' [0 O Y+ f+ J0 h' n - 2
* Z# K) H/ N: t1 O - 3#include <libopencm3/cm3/nvic.h>- ]$ ~( j! q0 m* N# B M6 c
- 4#include <libopencm3/stm32/rcc.h>
9 K5 Z J' a( P - 5#include <libopencm3/stm32/timer.h>
8 ~! l6 Y) `+ N* l1 M- F - 6' X3 o a$ @ _6 j3 M' j5 B
- 7/* ----------------------- Modbus includes ----------------------------------*/2 c5 ^4 q% g; M, I2 f) s
- 8#include "mb.h"
" C* n. ?3 w N# P; u* `8 ` - 9#include "mbport.h"
6 D/ H4 l0 y+ E3 A |5 C# [$ Q - 10
$ {" E( T6 ?5 \' y+ C - 11/* ----------------------- static functions ---------------------------------*/
U$ w1 E7 C. G" q% [0 b - 12static void prvvTIMERExpiredISR( void );
6 r# @$ v9 _- k1 m - 13
& n- ]% G' i$ W3 r/ X. h, Y - 14/* ----------------------- Start implementation -----------------------------*/5 A/ E2 [( ~4 |: f% O
- 15BOOL6 G. f3 i N4 M! Y9 T+ r
- 16xMBPortTimersInit( USHORT usTim1Timerout50us )4 H8 X p8 s# m4 Y' m$ M) h
- 17{: V) U: g6 i" ^0 t
- 18 rcc_periph_clock_enable(RCC_TIM2);0 j! ^9 f/ c3 s
- 19 nvic_enable_irq(NVIC_TIM2_IRQ);
: ?# a& y9 \ l0 g - 20 rcc_periph_reset_pulse(RST_TIM2);5 _8 d1 A/ V1 f, @6 k
- 216 J3 D8 G# r" t: i3 W
- 22 timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
- d- j) A( G1 v+ b - 23* ~6 L5 j6 A; o+ M/ K- T5 ~
- 24 timer_set_prescaler(TIM2, (rcc_apb1_frequency/ 20000));
( y* c& N- @+ P; U9 @1 M% F, p - 25. V s( ~/ @ k; J9 Q4 ?) E
- 26 timer_disable_preload(TIM2);
% o2 L/ t9 N0 }( @' T! D h - 27 timer_continuous_mode(TIM2);9 R( D$ k# Z# b& U1 f f2 u
- 28
) w6 {$ r+ E, h5 @: U6 Q. G! H( H l - 29 timer_set_period(TIM2, usTim1Timerout50us);
; P6 F, \1 B) P2 c \ - 30 timer_enable_counter(TIM2);+ K2 J# J, A+ |, T, ~ f) x% o; [
- 31. Z& U/ T, ^7 c
- 32 timer_enable_irq(TIM2, TIM_DIER_UIE);0 {. x% l% V4 @/ t! D2 D4 y" P1 w- ~2 D1 r
- 33( x% J/ `7 Y$ H* j; q/ G
- 34 return TRUE;; G1 f( l0 w: h7 g$ ?
- 35}6 S! F& s1 v) C9 ~
- 368 }$ l# E2 T& B6 b
- 37
3 i- v! Z3 R: m7 r$ I - 38inline void
0 d6 q( X/ M- S; C - 39vMBPortTimersEnable( )3 `1 V/ o) L) s& i1 v3 G
- 40{ E* n2 w* a- k. M" v0 P
- 41 timer_set_counter(TIM2, 0);
( ^0 l+ B/ u2 I6 ~# K - 42 timer_enable_counter(TIM2);, @6 G& J9 F* _: E: D) _
- 43}
1 ~9 _0 a; A& \0 i- A - 44* {" M8 P, r: o
- 45inline void1 k0 M, M2 I6 ^) Y* K6 N$ r f. h5 t
- 46vMBPortTimersDisable( ), A9 N/ o0 t( B0 E. ?! [( q! r
- 47{
& e, l8 l5 P0 Z3 @3 H# w - 48 timer_disable_counter(TIM2);' W4 r( Y! X0 | r# T. T z' L
- 49}6 J0 `5 m" l$ D
- 50
" ?6 P; j4 ?1 M W4 s- y - 51/* Create an ISR which is called whenever the timer has expired. This function
! n; k& Y% y) c. l/ I; q - 52 * must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that5 X8 |: X, \9 J. q* J( e# e
- 53 * the timer has expired.& Z: J5 m6 A; y! ?
- 54 */) }8 ]. ?' z% g) B8 f" B
- 55static void prvvTIMERExpiredISR( void ) t# b' K! w; k: o) Q$ v
- 56{ u* m8 ~& P! A' U
- 57 ( void )pxMBPortCBTimerExpired( );
; V# r, }, C& S9 ^) d/ c( x, Z: p - 58}4 U0 L, A1 h5 a; S/ G2 k6 ~
- 59
& Y. J' `( L6 J) ~ - 60void6 @% L$ {' f3 e0 I; [: J
- 61vMBPortTimersDelay( USHORT usTimeOutMS )) a* v3 Y; I7 u6 A6 j
- 62{
6 r, r8 t$ a" N. p' Y - 63 vTaskDelay(pdMS_TO_TICKS(usTimeOutMS));
. r( ]! L+ r6 N# v, U- }: H2 N3 } S9 \ - 64}
$ G* b1 F5 i* A" C - 65
9 A4 X# m' D. n/ e- R6 W - 66void tim2_isr(void)
/ L% d* D3 O+ B8 l8 N - 67{% l e* b! t5 e' c+ |. S
- 68 if (timer_get_flag(TIM2, TIM_SR_UIF)) {
5 N8 ~/ y5 q; @" w8 q - 69
9 v% q+ r; N7 _3 S9 k% ^ - 70 /* Clear compare interrupt flag. */
: @" g6 k1 o, A5 h0 x - 71 timer_clear_flag(TIM2, TIM_SR_UIF);
3 w2 o& o+ i7 { ?. L+ Z; t! V* E - 72& d5 q1 R$ R/ w9 s' f$ b0 h, x! `
- 73 prvvTIMERExpiredISR();
" a% w. E8 A) ?/ h8 B - 744 D& b( h$ L: k5 R' v
- 75 }$ H. g( H; l K' w/ ^+ D$ ?! s
- 76}
复制代码 开启定时器和中断,用于modbus时序控制;+ g/ Q" I3 S( E
, k7 L; a; D3 y
4 F8 t; c3 i1 q- w6 }
2.2使用
% ~, V. C$ i: X9 R$ a e0 v: G在src目录新建 modbus_cb.h 和 modbus_cb.c 两个文件,实现寄存器、线圈的读写回调:
2 h; @6 w5 d; [3 E7 D6 g- 1/// CMD4
, p+ t3 j$ ?5 k% v - 2eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs );
3 D/ P1 u& B, r# x. W. m, q' p* b7 ] - 3
: ]# ]4 M( B* f - 4/// CMD6、3、16
9 b+ Q9 m$ G" L3 \ j( y9 i - 5eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode );9 L2 H$ T% W& u$ I
- 6
9 n, x, k- B$ @. n# R8 J$ Y - 7/// CMD1、5、155 a( ]: b* i0 q
- 8eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode );. A1 ~: J9 r* c3 z3 o& p' ?8 T0 `* v
- 9
, X9 I! U- `$ @3 l( a! N - 10/// CMD4
7 ]% @' T; P/ g. o# s+ F1 O - 11eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete );
复制代码
( F1 ~0 H% t+ n% W' N
4 s& \5 H7 e0 k
3 a+ U4 w/ M# H8 \5 }基本的实现示例如下:2 s1 ~8 v3 }8 `* @
- 1#include "modbus_cb.h"( D& v3 A: A; Z4 l8 K
- 2#include "stdbool.h"
7 q4 `9 X9 q' c" ]" b3 |0 |2 [ - 3
5 r) {* P6 L/ j g- j& i+ S; J - 4extern log(const char* fmt, ...);
; v8 g3 V" b; r6 t/ | - 5
" L$ I: ~2 m% {$ g1 o2 e- L - 6// 输入寄存器
) y, E1 \5 z* k. I d* w - 7#define REG_INPUT_SIZE 32: D# V/ X4 ], [3 ^1 g! T
- 8uint16_t REG_INPUT_BUF[REG_INPUT_SIZE];1 E+ H- H6 a6 G1 V& P% @ C
- 9" d5 r `: V0 J4 j' ^
- 10// 保持寄存器9 D; m: l* E! h6 L6 c) ?5 E
- 11#define REG_HOLD_SIZE 32
5 C. T3 c1 X+ ~2 R* u - 12uint16_t REG_HOLD_BUF[REG_HOLD_SIZE];
5 u3 o2 D( M$ j# U" @1 X - 13
0 Q$ W( U- |2 a: K7 U3 B/ W, x. q - 14// 线圈寄存器
. r% F: V, x6 I - 15#define REG_COILS_SIZE 16
5 W- b4 t$ O9 u! X8 _ - 16uint8_t REG_COILS_BUF[REG_COILS_SIZE];. l# J! t6 ]* B
- 17
; a1 Q: |2 Y5 }0 e - 18// 离散量8 f+ O$ e5 }; P# O; _* J
- 19#define REG_DISC_SIZE 8$ n q4 V5 e* _# N7 H5 |
- 20uint8_t REG_DISC_BUF[REG_DISC_SIZE];9 x# Q& Y! X* ^' }" z
- 21
3 s+ D# I# H7 B, r4 W - 22/// CMD4
6 W; W, W, ^* M4 l o& p* ?1 @; q - 23eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
! \ C4 c _- v, }* w - 24{5 K3 [& H# y: _% ~% o3 @
- 25 USHORT usRegIndex = usAddress - 1; ! v1 `: n8 j/ Y& Q' m+ j3 x0 \' B5 o$ g
- 26
* v' B$ \' X" J - 27 // 非法检测
: F% Z/ F& D q% E4 ^0 P7 X - 28 if((usRegIndex + usNRegs) > REG_INPUT_SIZE)0 b& i6 q( o/ z$ Y! t
- 29 {
! G' c* |( B# [- Q/ V - 30 return MB_ENOREG;7 |' O, N4 s+ T* O- b- l k
- 31 }; X3 [" L" u2 C& u+ v1 I8 g
- 32
7 Z1 z/ H3 T' p% D - 33 log(" CMD4, 寄存器输入.");
6 G2 h9 Y7 R3 W7 V1 J1 i - 343 N2 w8 O1 \) A5 @
- 35 // 填充数据3 B% D% I8 T# _+ @
- 36 REG_INPUT_BUF[0] = 0x01;
8 I' p2 c: ?3 R# X. W - 37 REG_INPUT_BUF[1] = 0x02;; u, G5 l3 Z. a+ M
- 38/ `1 W# c' X/ O/ H' D7 t( B
- 39 // 循环读取
- U+ F' J6 C8 ]1 l% n6 ^ - 40 while ( usNRegs > 0 ) {# @. L% d( [4 {$ H! M
- 41 *pucRegBuffer++ = ( unsigned char )( REG_INPUT_BUF[usRegIndex] >> 8 );& r* H% j" z, B
- 42 *pucRegBuffer++ = ( unsigned char )( REG_INPUT_BUF[usRegIndex] & 0xFF );
+ L1 K1 Y- p B8 ` - 43 usRegIndex++;* w& E; o* s+ R. A, B: i
- 44 usNRegs--;: P& t2 r" m @- {; I" {7 x; ]; T! Q' ?
- 45 }& {: r) S% g2 C7 t
- 462 v% l% r# G3 E5 [0 |% o" r
- 47 return MB_ENOERR;1 H# m- O6 _1 b) g' M
- 48}
% h( k) Z: Z) `6 k; `+ m% s. _ - 49
* J; M( K+ w- g+ B - 50/// CMD6、3、16
3 R+ M# F& j: p2 L8 d% _' n: B# n+ x - 51eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
: u$ f! L! R) W5 h! H& l - 52{! c9 `' M( {9 A( m
- 53 USHORT usRegIndex = usAddress - 1;
. ]3 R& U% j4 l3 E1 t - 54
# M# g6 [3 L, [+ j& c - 55 // 非法检测- ^- ?# F x6 }( y& ~! w
- 56 if((usRegIndex + usNRegs) > REG_HOLD_SIZE) {+ K- R+ A- G3 K# Q& N$ t& ^
- 57 return MB_ENOREG;
/ y8 v/ _4 |0 t. w# b( p4 l( I - 58 }9 ~, _( D/ X' F: s9 o
- 59$ J6 U( Q3 c* N! Y/ N
- 60 log(" CMD3,6,16, 保持寄存器读写.");
5 }# ?% b5 _; a- Y) R1 D - 616 Q8 T; [5 ~, C
- 62 // 写寄存器" o7 U" r; Q! b' G9 `# F
- 63 if (eMode == MB_REG_WRITE) {& w" _# t" n3 q
- 64 while ( usNRegs > 0 ) {
+ }: D. G8 J+ }# [, s - 65 uint16_t value;0 s% v, D9 ^& |
- 66 |: ]' ?2 M4 H5 d, S' Z
- 67 value = (pucRegBuffer[0] << 8) | pucRegBuffer[1];
& M4 G! w& i3 d ? e7 D: N i - 68
5 P* L. [0 m, P( L - 69 log(" 写寄存器值:%d", value);" M0 W" G! y2 q- V+ K" ~9 t3 S
- 70! i# g u! i# L$ ^9 k
- 71 pucRegBuffer += 2;
; F2 W2 J% O. B/ [. i6 I j% j - 72 usRegIndex++;4 u5 F( K; T P4 Q7 {, g( r
- 73 usNRegs--;' }1 }+ V- P: t2 p" U- t7 s5 i
- 74
' P3 m& v! M# t6 | - 75 }
* t8 g4 y6 |0 b1 Q3 D. w e+ u) s - 76" |( ~3 x0 V# y& [
- 77 }
8 o0 h K3 Q' x" Y - 78 // 读寄存器1 y; O# i8 i" G% b e
- 79 else {0 h4 M* f$ [+ v' O! B9 L! H; g
- 80
% |0 S6 v; Y5 j6 u, S - 81 log(" 读寄存器.");8 V/ Y% D" b7 G5 n; Q/ V# d" d
- 82 ^2 _# b+ a( ^: T
- 83 REG_HOLD_BUF[0] = 0x32;
1 j2 J) j, q5 z - 84 REG_HOLD_BUF[1] = 0x33;0 v' K+ I2 M2 d/ T
- 854 |6 H1 _% I( t( `2 l% N3 I9 S/ q* ]
- 86 while ( usNRegs > 0 ) {
( r5 \: V( _* m. _+ ?* A) w [ - 87 *pucRegBuffer++ = ( unsigned char )( REG_HOLD_BUF[usRegIndex] >> 8 );
$ ~, `& H: V8 Z - 88 *pucRegBuffer++ = ( unsigned char )( REG_HOLD_BUF[usRegIndex] & 0xFF );% T; Q C1 d& y( K/ v- A- K
- 89 usRegIndex++;
7 J7 Z$ Q1 v7 |( [ - 90 usNRegs--;4 s1 y6 R- ^& L) h; r
- 91 }
q* A8 v" j+ H- V. C7 }% E - 92 }1 u3 X7 n6 w' F+ C7 E1 Q
- 93
$ }' g9 t. q0 k9 t/ M9 U - 94 return MB_ENOERR;% @3 N" [- ^; a- a
- 95}; ]$ t4 H0 |/ U& J
- 96
/ F$ E& a* z' p( w2 P/ c' y - 97/// CMD1、5、15
0 Y3 Q1 B9 d( V& g4 J; w& F - 98eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
0 l* {) q9 N, w$ d; S( n* M0 [6 Q - 99{
% j2 ?) j( X) A5 { - 100+ G1 U% ]3 e7 w) T# }" n. [
- 101 USHORT usRegIndex = usAddress - 1;
' |3 b! g0 c r+ f4 V - 102 USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);8 g/ Y7 V4 D8 x9 J8 M* _" v0 {0 Q- a
- 103 UCHAR ucStatus = 0;
2 y1 }3 x ~6 a }" y" X& p8 b) X - 104 UCHAR ucBits = 0;
# J0 I1 v3 X0 M3 n - 105 UCHAR ucDisp = 0;
6 d- p: E8 @6 Y: E" e - 106
# x8 ~6 @9 A# C: H" h: h - 107 // 非法检测
2 f( V6 T* ^% p - 108 if ((usRegIndex + usNCoils) > REG_COILS_SIZE) {% ?7 j5 Q u0 T4 O, b: P. L* [
- 109 return MB_ENOREG;- P% V8 m2 e( q* `- ~
- 110 }% N# o% c3 i4 e8 E
- 111 @* z2 `5 ^' j
- 112 log(" CMD1,5,15, 线圈读写.");' P; E, `, X. e7 m
- 113
: Q4 F$ ^8 Z% l6 I% `1 ]) ]! ~ - 114 // 写线圈 e }3 O/ p5 E' f5 z# p1 `
- 115 if (eMode == MB_REG_WRITE) {
; ?" S7 P+ C6 p$ n/ u$ ~ - 116
) Q% s C% r0 ?4 u- ]0 f2 ~ - 117 while (usCoilGroups--) {
! e* g# M7 ^+ I5 } - 118
# O' \% K2 k. e% t R7 V: I2 d; L2 [ - 119 ucStatus = *pucRegBuffer++;
" p) N" H% v P* n) V - 120 ucBits = 8;
@2 B7 Q { I2 X2 E( {# D - 121
2 m" ~$ W2 M/ E" F& e3 F0 ? - 122 while((usNCoils) != 0 && (ucBits) != 0) {% V3 ]$ J U+ f2 U. T# k
- 123 bool flag = ucStatus & 0x01;* O! K( K) B$ v% q
- 124, n$ a0 P2 _. ^' u/ f' \
- 125 switch (usRegIndex) {' X3 H3 e! e9 m1 p
- 126( Q* [1 |* |0 B8 @' N
- 127 case 0:0 m9 g1 X4 X% [% T0 t9 g8 ]" ~) H
- 128 log(" 线圈0 : %d", flag);//
* Y' X9 T4 j& |) k+ E3 f) I& e3 g - 129 break;
; G! s* ^* a4 f" D' z$ ] - 130
# x0 {, U& l3 M; E: l - 131 case 1:0 K1 a2 |. D& s- o- y5 [
- 132 log(" 线圈1 : %d", flag);) G7 |' t7 j2 p3 @! Z
- 133 break;0 ]* h% j& o1 e' V7 _) S
- 134) M+ o( v3 U3 j7 Z! Y
- 135 default:
( H! {$ g/ V1 s - 136
) d6 t# ]% r. w* h3 L1 ? - 137 break;
* J. W- x7 B+ i# G+ b) a - 1387 y8 q( J& H5 u: z+ y
- 139 }. X5 ^* Z. L c" n
- 140
: b4 {; H# F, d, _. M7 A - 141 usRegIndex++;
G6 e( {( q1 j* u" B - 142 ucStatus >>= 1;% m9 O$ T& E; h, w
- 143 usNCoils--;
& d5 V! {3 m3 E/ \" p1 @# i - 144 ucBits--;
6 w' K; Q% {/ r2 c& G* y9 k! ` - 145 }8 U$ W4 n1 ~, ^. W$ ]5 K
- 146' v) g% W0 R1 n+ ^2 E, n+ ^
- 147 }
0 m( ]) n& G8 }4 d7 @ - 148 }
$ f% L2 i- A% P0 g - 149 // 读线圈
- c! k# v7 r1 K - 150 else {
& m5 \; `: } s6 O8 A: X' ` - 151
9 G' I, _1 q S; Y2 { - 152 REG_COILS_BUF[0] = 1;
7 O% F4 l# k+ n6 k, r - 153 REG_COILS_BUF[1] = 0;; D! h2 b& L9 o3 N8 _% z# |, a
- 154
$ D7 e; h1 _7 b1 P0 { - 155 while (usCoilGroups--) {
7 D% l9 z7 U* v# f) J: X - 156 ucDisp = 0;
8 w; R( W9 u$ d - 157 ucBits = 8;5 w' g2 Z2 X5 c7 E, ?: |7 a
- 158 ucStatus = 0;" S; T$ d7 J& `' a6 w( |: B; ]
- 1596 P8 x+ ]& k1 ]3 M
- 160 while((usNCoils) != 0 && (ucBits) != 0) {
7 ^5 ^7 `1 E) _$ O- z* ?1 T - 161 ucStatus |= (REG_COILS_BUF[usRegIndex++] << (ucDisp++));
7 o0 q4 X& q; E# {+ a - 162 usNCoils--;# e9 {. P2 y; {( I( X
- 163 ucBits--;3 b5 y/ O! k* n: m( C' o$ ~. }
- 164 }3 q8 B' z( u1 Y4 D. K6 ^: ?
- 165# D, L& q& y& T5 {
- 166 *pucRegBuffer++ = ucStatus;
5 v6 Q; S% p2 {) n - 167 }
. s; r+ [0 `. t+ z3 x - 168 }
! J9 U: M q0 c2 ? - 169
6 o% n3 t, y3 Z# \5 b - 170 return MB_ENOERR;
+ F* x) T. b, `. l0 x - 171}
3 i k& d* @ e; j) H% u& R - 172
; l- U+ Q: o4 h6 X Q# ^ - 173/// CMD4$ Z, O" W2 J8 ^5 @. i2 R* [
- 174eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )5 m/ W/ H1 h" R! G! @2 o
- 175{
r5 |+ W- i0 n, t; B; l% O3 Y- x - 176 USHORT usRegIndex = usAddress - 1;
7 J, y2 B7 N) a9 g. S& N! z: V - 177 USHORT usCoilGroups = ((usNDiscrete - 1) / 8 + 1);4 d8 P, B2 ]6 K& @6 R# T: S2 h4 H
- 178 UCHAR ucStatus = 0;
% ~2 `7 y1 O+ z/ z& d/ `& ^1 y - 179 UCHAR ucBits = 0;
5 N" ^9 y* }8 I5 I - 180 UCHAR ucDisp = 0;2 w* f3 f" y1 N% Z# v2 }
- 181
! g( g3 K9 T6 h - 182 // 非法检测1 _: N) r9 L N: ^' M$ C
- 183 if ((usRegIndex + usNDiscrete) > REG_DISC_SIZE) {
; s! t ~ i: ] - 184 return MB_ENOREG;! D5 P5 Z7 a% k* y3 v! x* w4 v
- 185 }
0 J/ d9 K& i% Q% C6 N - 186" X! T. k" n5 L
- 187 log(" CMD4, 离散寄存器写入.");
& p T3 f6 d' f - 188
& p0 f/ J0 s* o5 ^0 J) t' W - 189 // 读离散输入- A" L- ^$ p* S6 c5 R6 V% b2 V. B/ K
- 190 while (usCoilGroups--) {, O* f6 E& N) R8 r) U5 a: [* C
- 191 ucDisp = 0;# P5 ]* l0 }: V8 }: H4 _7 U
- 192 ucBits = 8;
; f' W& v6 _6 C( q% t) B - 193 ucStatus = 0;4 D3 Q( }$ W6 b3 I+ k2 x2 S5 J
- 194
/ M7 L u) O5 [6 O - 195 while((usNDiscrete != 0) && (ucBits != 0))5 y6 x/ h. R3 r) B. s: n
- 196 {# ]& r" _1 b; D5 \7 S/ x0 }* O
- 197 switch (usRegIndex) {
$ E: b e! q0 J& Z% H* S: } - 198 case 0:
7 X; ~, N8 V5 b' ~9 |$ B - 199 ucStatus = 0x10;
5 m' D( Z4 X! ]: e - 200 break;0 o: N% V9 a/ }
- 201 }
u% \9 W* f3 h) |3 M, E - 202
' }8 r' P& A. Q( P2 e/ A/ |: { - 203 usRegIndex++;( T- b. \( b4 Q* r8 Z% e& M
- 204 ucDisp++;2 z2 C8 L' h& a; L
- 205 usNDiscrete--;
2 i x/ t2 N, l+ e - 206 ucBits--;) M5 L' x, ~+ E! R
- 207 }: R, k0 v5 _$ u) G
- 208 *pucRegBuffer++ = ucStatus;' W+ ?2 n2 R; y: J2 ]$ z
- 209 }1 r/ B- Z+ g Z4 P9 c' u: a$ ?# q
- 210
F: J! [0 n, v5 ?( [% j7 ` - 211 return MB_ENOERR;8 a8 c3 U5 B1 ~
- 212}
复制代码 0 M6 N( ^! F; U& w7 |
在main中创建modbus任务:
/ A6 [/ }4 D9 \1 v5 n' H$ Z5 s: x- 1static void task_modbus_handle(void *param)9 @- }2 X$ G7 |7 k: P' k7 ^
- 2{0 z4 b6 e, n- p3 d
- 3
* F3 B4 W7 }/ ^, l2 x, a" ^& b - 4 eMBErrorCode eStatus;: k. @: S4 m3 h; o
- 5
. s0 i7 C+ ?- _; z - 6 log(" task modbus start."); B9 U5 b# q+ W% J# y2 o* ?
- 73 R. K3 N! Q6 r3 S# z. i
- 8 eStatus = eMBInit( MB_RTU, 0x01, 0, 9600, MB_PAR_NONE );- e' b& W6 ]9 o3 c& v
- 9
; f& F% k* H- p) N. n; i - 10 /* Enable the Modbus Protocol Stack. */
4 h& ?0 j% U' p5 q3 T* b - 11 eStatus = eMBEnable();
9 |6 {# h$ Z! x `' F# { - 12
3 i. Y5 _# T Q* |+ I. L - 13 (void)eStatus;
* k4 J* h8 P' h1 t6 {2 A3 R - 14
/ |( L% t7 c0 E4 ]) }; p - 15 for( ;; ) {
! E2 q: p% Z: H' [ - 16 ( void )eMBPoll();
T5 x+ \+ z( K - 17 vTaskDelay(pdMS_TO_TICKS(10));
_$ l6 I! x( z2 y" V8 C - 18 }: ~, Y' w$ S: q9 [6 ?4 g0 J. i
- 19
- T* w; w, d. q- B. x2 K - 20}
复制代码 7 c7 A% ~( w" q8 l+ F) m
% b( k. r/ U6 q
3 烧写测试9 j: x5 @# @- u* b* l- [$ w
将开发板连接到USB转485模块,然后使用modbus poll程序进行测试:4 v b; G4 Q4 V4 `' c
$ @, o x- V$ ^/ C5 R
1 `7 B, X, u/ N9 t# ]转载自: MakerInChina.cn
& `) e* _8 L% A7 C/ K
: l7 T R6 S- C7 ]. s$ Z% D5 k: ?- ]1 w
|