1、环境 STM32F107RC核心板 + Atollic TrueStudio + Win8.1 2、STM32F1系列单片机分类打开参考手册第三章,可以获知:不同储存容量的单片机有不同的FLASH分布,而主要分为以下几类。1、low-density,2、medium-density,3、high-density,4、connectivity line。我们需要根据使用的单片机容量,确认其FLASH分布。 下图中,Table 3是STM32F107RC规格书上的关于兼容性的描述。从这里以及规格书上文描述,我们可以知道: STM32F107RC属于connectivity line。 # x( M& W2 L( J6 t7 n/ N+ Q
% P- V& B4 l/ M* I R5 p; t, ^8 r8 `2 Y 3、硬石STM32F1开源的源代码3.1 stm32f107_flash.c- /**$ X9 s; x {, m: b0 ~ ^& }" F
- ******************************************************************************, d2 ~0 }; f' @. ]+ u
- * 文件名程: stm_flash.c
0 B. q( ~& T$ Q* h: N& Z1 T - * 作 者: 硬石嵌入式开发团队& d f; ?6 d3 X: c/ h7 o0 W0 H
- * 版 本: V1.0+ y6 Z/ |- C; L) E% } g- |
- * 编写日期: 2015-10-048 d. j7 m: v6 P: W* w5 y
- * 功 能: 内部Falsh读写实现
/ y3 C0 e, t( W - ******************************************************************************4 i# y) G: }4 N
- * 说明:' g3 [) X8 z: X9 n: Y- e
- * 本例程配套硬石stm32开发板YS-F1Pro使用。
* t# u& e! I5 y: H# ~ - *
$ R n" T/ Y) g7 M: j5 P# |2 \ - * 淘宝:
( {5 g% |( w- j8 a - * 论坛:http://www.ing10bbs.com
+ H5 O$ E1 z' K; i6 M; ^ - * 版权归硬石嵌入式开发团队所有,请勿商用。
9 r) H% a+ s: ?7 k9 O, u - ******************************************************************************. M n9 E4 h7 q, M! z
- */
7 V. \4 {7 V( q - /* 包含头文件 ----------------------------------------------------------------*/
# i2 \3 V0 A @5 t8 b& w% G; u - #include "stm32f107_flash.h"
9 E- k" P9 F% D$ K. ~9 a7 x -
! Q, n2 q! O1 p8 J - /* 私有类型定义 --------------------------------------------------------------*/& {/ v9 m4 T+ B/ |% S9 D
- /* 私有宏定义 ----------------------------------------------------------------*/
: v0 \, H8 o# f0 `, w! C2 f- g8 N' ` - #if STM32_FLASH_SIZE < 256 d* N0 ^) k! [& ?% z% M
- #define STM_SECTOR_SIZE 1024 //字节7 B3 v0 j q1 R
- #else
9 q2 ?) [: p& T7 A0 V* n - #define STM_SECTOR_SIZE 2048# `. f: g' S# r* s" @
- #endif
; J! T' \7 ?& x" C3 [5 C -
5 v- ^# i. C+ A0 x! R) J W5 W7 w4 o - : j1 w. I- @7 \; U) S7 V& S& |
- /* 私有变量 ------------------------------------------------------------------*/* q- M: G+ ~7 e$ x
- #if STM32_FLASH_WREN //如果使能了写
4 L7 \9 Z: f5 o, h9 |3 x - static uint16_t STMFLASH_BUF [ STM_SECTOR_SIZE / 2 ];//最多是2K字节
2 Z* U4 n) X# N/ A; W5 g& ] - static FLASH_EraseInitTypeDef EraseInitStruct;4 t0 V1 g1 R( c: i+ {0 Y+ ^
- #endif$ p d+ @% X0 J* }
- 3 l* E: o _6 f; |) r
- /* 扩展变量 ------------------------------------------------------------------*/
& r% M) }. z; _$ Z+ ^: q* U - /* 私有函数原形 --------------------------------------------------------------*/8 x/ U. x9 l* `" X* n" f4 D& n; h' V
- /* 函数体 --------------------------------------------------------------------*/& ~( _" ]6 e- R5 j1 B3 U' G+ g
- /**
$ S: U! I5 c% g; M9 P3 w$ y& Z - * 函数功能: 读取指定地址的半字(16位数据)8 e5 e/ l$ m. v% x7 e A
- * 输入参数: faddr:读地址(此地址必须为2的倍数!!)4 }5 z& p# {6 Y9 F% j
- * 返 回 值: 返回值:对应数据.
. {" _* r) R9 f2 H - * 说 明:无: t) N4 w/ g# v) f
- */
, Y6 D% N/ ?2 A" o# p8 r4 x+ k - uint16_t STMFLASH_ReadHalfWord ( uint32_t faddr )
6 G& ?# l, d. V - {
( N: Y7 W2 } P - return *(__IO uint16_t*)faddr;
5 |! L9 k' \2 s1 Z: q: v: g - }
0 |1 L# N, N. W, s) G -
5 X$ i/ X. I! _# s: B- k - #if STM32_FLASH_WREN //如果使能了写
/ @% y0 z0 ]. `1 Z+ o' B - /**
- v$ i3 H# p5 z& k/ p - * 函数功能: 不检查的写入1 I* k0 V7 G- }! j8 p* {
- * 输入参数: WriteAddr:起始地址
; v Y/ _$ e2 k0 C! {$ G - * pBuffer:数据指针
! K$ P* S' F9 r# B' K( C% x, k - * NumToWrite:半字(16位)数; z# N# S2 C/ o( P
- * 返 回 值: 无
4 [8 s; \) S O) @+ u. D - * 说 明:无
2 t: v0 ]/ n4 B7 { - */
! v$ f1 g1 c" {9 M* ~- n" W, d% Z ^ - void STMFLASH_Write_NoCheck ( uint32_t WriteAddr, uint16_t * pBuffer, uint16_t NumToWrite ) , n6 K$ l+ C8 o0 w; R
- {
3 K& l1 ?6 I2 A4 ]) l9 ` - uint16_t i;6 \5 v, E$ n( b t+ O
-
' @: Q- @0 Y. S8 \. } - for(i=0;i<NumToWrite;i++)
1 }1 K1 [5 b% |4 H - {) q4 D$ X7 a0 k! `
- HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,WriteAddr,pBuffer[i]);6 q) T$ ~ _0 v; F0 u
- WriteAddr+=2; //地址增加2.
" C) c9 d P6 {# U - }
' f6 t2 k+ \ {" L# ~- m. P - } ' W5 ?' k6 V5 `4 _: p
-
1 r. Z+ C6 ~- T) R' N. M/ \ Z& n - /**1 \) p& D# U- V2 C
- * 函数功能: 从指定地址开始写入指定长度的数据2 M2 i! A# \" {8 @# Z
- * 输入参数: WriteAddr:起始地址(此地址必须为2的倍数!!)
5 \/ W' @- n+ X) Y& Z6 u* g2 f - * pBuffer:数据指针+ q, C+ Z$ T3 J2 n9 x
- * NumToWrite:半字(16位)数(就是要写入的16位数据的个数.)
* u( P7 A: X% f% P( R& d - * 返 回 值: 无
. Y: U& o5 J+ K x+ p( @: F( B2 r - * 说 明:无
3 l1 V" {# O; ]: B/ h5 t - */
+ Q0 Z3 \$ I" b5 k3 V$ J' X - void STMFLASH_Write ( uint32_t WriteAddr, uint16_t * pBuffer, uint16_t NumToWrite )
: I4 U% t' c) w q* ] - {5 ]6 r6 Z% C3 c% c
- uint32_t SECTORError = 0;
$ m- |5 n# O4 k$ Y8 x8 v3 v6 c7 M7 m - uint16_t secoff; //扇区内偏移地址(16位字计算)
/ M% ~" E* H' W% \ - uint16_t secremain; //扇区内剩余地址(16位字计算)
9 B; p- k2 L8 y - uint16_t i;- Z. }3 R9 l4 E9 X; ]$ ]
- uint32_t secpos; //扇区地址
, x& }( V! U m. Z6 T - uint32_t offaddr; //去掉0X08000000后的地址
" V% o, P' x, o( I! w" N% K1 E5 E - - p- K6 g$ w7 ?( ?/ [
- if(WriteAddr<FLASH_BASE||(WriteAddr>=(FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址; o+ l( ~: o4 z" F+ J. M( E2 k
-
) M( i% J+ b* x% z8 W' \ - HAL_FLASH_Unlock(); //解锁
% b# G0 l$ W: @+ J5 D -
* g! w# q; P3 v, c - offaddr=WriteAddr-FLASH_BASE; //实际偏移地址.
( D% j' J( u+ z7 ` O - secpos=offaddr/STM_SECTOR_SIZE; //扇区地址 0~127 for STM32F103RBT6' p1 B9 c, ^# j9 `6 F6 ~' U; s7 Y
- secoff=(offaddr%STM_SECTOR_SIZE)/2; //在扇区内的偏移(2个字节为基本单位.): Y" Q; b$ ?* y3 n
- secremain=STM_SECTOR_SIZE/2-secoff; //扇区剩余空间大小 m0 Z9 j ]6 x4 }' y; y" I
- if(NumToWrite<=secremain)secremain=NumToWrite;//不大于该扇区范围 t: f9 `" Z: \: M Y
- * O/ C' e! A& `' T; S% v- a
- while(1)- d6 q" T9 X( K7 _# b
- {) { f1 n3 n! O7 w2 ~* O
- STMFLASH_Read(secpos*STM_SECTOR_SIZE+FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//读出整个扇区的内容
' m( X/ ^2 @% s - for(i=0;i<secremain;i++)//校验数据; a0 \' L& e; E" k$ c2 \& j0 N2 ^
- {# e z! Y, t5 u2 {- Y+ d( C$ ~7 L: e
- if(STMFLASH_BUF[secoff+i]!=0XFFFF)break;//需要擦除- q) Q3 A% V+ X3 C/ U2 L
- }
/ z7 M) o" r( M" l - if(i<secremain)//需要擦除
: E# v. O2 X/ E6 t3 X h0 {) `+ @# p - {
- @% x, @% Z: h# o/ D, K - //擦除这个扇区
/ v" l/ G4 _: g- W: f) G - /* Fill EraseInit structure*/
) r. w. Z7 ~ |! Y9 [8 F: x+ K - EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
; c Y! P) G0 y - EraseInitStruct.PageAddress = secpos*STM_SECTOR_SIZE+FLASH_BASE;
1 L0 [' Q8 |' N+ f - EraseInitStruct.NbPages = 1;9 T9 F' \( Z. b
- HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);0 D0 L5 R1 ~* C, n' B3 X, g# T- W
- 1 O' V8 S: U7 V. }1 {
- for(i=0;i<secremain;i++)//复制
- M5 O" |; X7 H3 Q1 K9 E" y9 W - {* W, Y* K9 S4 {3 D3 v: D+ S$ K
- STMFLASH_BUF[i+secoff]=pBuffer[i];
: E: k3 Z N/ p- @2 Q, M. Q8 v - }2 H' I8 x" L. A9 p/ w9 C
- STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//写入整个扇区
0 C) B$ [8 f" t - }
, u0 d" K/ c8 K2 h: M - else6 \/ k& v4 D" ~. H5 R3 M0 Z1 y" o
- {
# z4 N" i0 Z% W' E' q6 L, y# y - STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);//写已经擦除了的,直接写入扇区剩余区间.
$ P4 D# }& g! q - }2 d7 Q4 {6 O9 X1 C8 {
- if(NumToWrite==secremain)break;//写入结束了
}( L/ a: G1 g: U ~ - else//写入未结束
9 Q: M1 R9 k8 Y1 F R& r/ c* { - {
# t* k5 i' h" \4 Y3 o- n - secpos++; //扇区地址增1
- _5 a( U7 M& M& O. F% v& R6 v/ N$ { - secoff=0; //偏移位置为0, n1 N! i; e) _ K# O( {+ X
- pBuffer+=secremain; //指针偏移
' f- R( u% M1 E- H - WriteAddr+=secremain; //写地址偏移
0 k1 ~$ A2 m) ~( @) n - NumToWrite-=secremain; //字节(16位)数递减7 P- p3 F% J2 s7 S$ r
- if(NumToWrite>(STM_SECTOR_SIZE/2))secremain=STM_SECTOR_SIZE/2;//下一个扇区还是写不完
* C- r* Z5 d m6 `' B; \# _! e - else secremain=NumToWrite;//下一个扇区可以写完了
z% p$ J" K/ P$ _, E# h7 t - }" W# [; m7 u9 Z! [1 F+ L
- };
- d9 \. v" e( {: i2 G7 P - HAL_FLASH_Lock();//上锁1 S8 q% j" S2 V3 I
- }. F9 y* [# ?8 s; l3 i0 J' [- H0 h
- ; y! K0 S# h# T6 p) E. s Z0 P
-
( m/ i9 q! ~ G& h/ { - W4 m' N) G0 U& \+ b! w. s
- #endif
5 n2 g K* b T5 m -
; ^, r% k3 L, b4 Q3 p; }' R& e. D& x( }+ p - /**
) \$ |4 R! H5 j2 D. v& w - * 函数功能: 从指定地址开始读出指定长度的数据+ _9 E1 L3 H$ u: I) w
- * 输入参数: ReadAddr:起始地址. v0 l! [) V w8 z N
- * pBuffer:数据指针$ p) \, z! y* G. O$ R( p
- * NumToRead:半字(16位)数! j& P1 N6 J; M% T& p' e
- * 返 回 值: 无
" u/ R5 @ }% K; w - * 说 明:无) }' t& M9 S) B/ S" w0 o6 ?
- */
( t' |, v" X( [ - void STMFLASH_Read ( uint32_t ReadAddr, uint16_t *pBuffer, uint16_t NumToRead ) ' ]# u! `% z0 T
- {
% d9 n/ a) r1 Q. G& l5 I" Q7 V - uint16_t i;/ j8 b8 ]* I4 @- L: @# ]
- 2 m# Y c6 j+ t2 P6 `# l8 f
- for(i=0;i<NumToRead;i++)6 ?3 }' a1 R4 e# N4 |
- {( J4 X, Y' ~6 U* F3 @# t/ w
- pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.* g1 `! H0 n2 z
- ReadAddr+=2;//偏移2个字节.
' Q1 d! _% ^6 {& r( @# | - }
w0 ^& h* n" F - }
, M' z8 W! C% w) n8 j" [7 E/ L - * r: Z. }9 B9 ^0 C w. e7 e" u4 `3 J; U
- /*+ e) w5 i0 Q. V% [, k
- * 函数名:Buffercmp
' H! }- B8 Z9 J2 Q3 A. j( H - * 描述 :比较两个缓冲区中的数据是否相等1 H' c* f* p9 o/ V) E& J
- * 输入 :-pBuffer1 src缓冲区指针7 f$ f" h! p& |! A5 ?" F9 I
- * -pBuffer2 dst缓冲区指针. Z6 j; _' _! V$ E% E
- * -BufferLength 缓冲区长度: E( D! L8 F5 V5 Y0 }8 _* f
- * 输出 :无; ~) Y. @% t- K% o
- * 返回 :-PASSED pBuffer1 等于 pBuffer2
4 P# [0 M( k! H6 V$ w - * -FAILED pBuffer1 不同于 pBuffer2& H. N1 c+ r# F F8 n D
- */: D. B' `/ A, C. g7 w4 {1 S1 b
- TestStatus Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength)
# Y2 c5 B6 [8 K% s& c - {
! b" B* A0 c0 u9 d; e - while(BufferLength--)7 f, N, n* ~) f; I4 h6 Q
- {
7 u% U4 `% o4 e$ V - printf("data:%d - %d \n",*pBuffer1 , *pBuffer2);
/ ~3 T2 Q7 c& _4 q _* W/ w7 [ - if(*pBuffer1 != *pBuffer2)
, V1 R/ s) M5 e. F& v9 j8 o - {
. ]( W% q. w4 {* h1 j - return FAILED;7 r+ }! Z7 A# w
- }4 {* O" b0 C6 [3 n$ d1 g
-
+ ]8 x" z( d+ B# E4 R3 \ - pBuffer1++;
3 T. \3 U+ C! L: c - pBuffer2++;
" N5 x, E# O: w+ `% X1 O - }
+ w+ \/ C4 B4 f, ^. h0 V - return PASSED;/ e; E( ~( D1 e, R" G, _+ c
- }
5 W4 s) h# U" j6 h" u9 r - # T& |( k4 V5 i6 f0 I, S! z5 V( h
-
' l Q; p/ O2 ~* ?! g" i; E3 l - /******************* (C) COPYRIGHT 2015-2020 硬石嵌入式开发团队 *****END OF FILE****/
复制代码
6 G& ?1 {6 L% \# X, o5 l3.2 stm32f107_flash.h; E/ y4 u" m: V$ w2 D6 N% ^0 k
- #ifndef __STMFLASH_H__ ~! g [3 p& W' @
- #define __STMFLASH_H__
L8 P; W+ U8 y: D* B: I% y -
! _8 @8 l/ V- x1 Z: N; g8 S - /* 包含头文件 ----------------------------------------------------------------*/
m/ Z9 i; B$ V( E - #include "stm32f1xx_hal.h"+ { g# p% \ |$ I, v# n
- " @& w& H4 S5 O2 `% R
- /* 类型定义 ------------------------------------------------------------------*/
3 K# b* n$ y- i# d9 N) N6 B - /* 宏定义 --------------------------------------------------------------------*/
2 A( O% y" x. A- c% t5 W - /************************** STM32 内部 FLASH 配置 *****************************/
$ D* ~" y1 o9 g5 B) w6 W; x - #define STM32_FLASH_SIZE 256 // 所选STM32的FLASH容量大小(单位为K)1 F# q$ J- J( o6 s" p' M$ m
- #define STM32_FLASH_WREN 1 // stm32芯片内容FLASH 写入使能(0,禁用;1,使能)
; }2 Z9 y9 \4 c) Q - 0 W5 X/ o: H$ |/ H8 Y
- typedef enum- H. _3 T3 }8 b k+ i
- {" g1 r4 w2 C$ v. k r0 E* u1 p. I- X
- FAILED = 0,) H4 Z! ^" e! k4 h5 d
- PASSED = !FAILED
$ h7 p- Y1 Y( T5 ~+ ~9 ] - }TestStatus;
$ P8 u; ]: F$ @& P - 0 m: G; Z$ [2 D/ ~- `
- #define FLASH_WriteAddress 0x0803F800 // 写在靠后位置,防止破坏程序
0 b5 a3 b* j2 s; [8 z# f - #define FLASH_ReadAddress FLASH_WriteAddress
1 _% M: u$ Z3 w - #define FLASH_TESTSIZE 512 //实际是512*2=1024字节
' w( N+ D3 Q7 j; ? - 9 }7 i' d, M, i* r
-
3 Y6 h- H+ ]7 @+ d R( H - /* 扩展变量 ------------------------------------------------------------------*/$ G+ \& X2 S) x% c
- /* 函数声明 ------------------------------------------------------------------*/
* V3 ]% M1 y9 j I0 n% n - uint16_t STMFLASH_ReadHalfWord(uint32_t faddr); //读出半字
7 x3 G1 r9 U9 _7 Q+ w* O a' [ - % D* H7 T8 w+ W* y
- void STMFLASH_WriteLenByte(uint32_t WriteAddr, uint32_t DataToWrite, uint16_t Len ); //指定地址开始写入指定长度的数据
/ ?, t+ E9 ?2 X* w5 h; ^5 j - uint32_t STMFLASH_ReadLenByte(uint32_t ReadAddr, uint16_t Len ); //指定地址开始读取指定长度数据$ r7 {3 k. e3 H, u! e1 T9 F
- void STMFLASH_Write( uint32_t WriteAddr, uint16_t * pBuffer, uint16_t NumToWrite ); //从指定地址开始写入指定长度的数据 y& s, \* E- n, |; c
- void STMFLASH_Read( uint32_t ReadAddr, uint16_t * pBuffer, uint16_t NumToRead ); //从指定地址开始读出指定长度的数据+ } q9 K% G" Y; {" |* G
- //static TestStatus Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength);- G9 N' _7 F; W& U
- TestStatus Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength);+ w3 f2 F' L& @) z4 \
-
2 a3 O/ s& x9 k6 T# Y+ \, ?4 A _ - #endif /* __STMFLASH_H__ */
+ h- t6 t9 W$ \ - / {* Q( A: C7 I F
- /******************* (C) COPYRIGHT 2015-2020 硬石嵌入式开发团队 *****END OF FILE****/
复制代码 0 J$ f/ n1 j M0 A; k- W2 ]" Y
3.3 main.c$ o5 l7 a2 r4 r% \2 m* O
- /* USER CODE BEGIN Header */8 ^1 ~2 v5 Z6 r& o& d
- /**4 F+ V: q' b' {9 o
- ******************************************************************************
1 u# n' R, b5 i9 g' `8 g$ }. E' B - * @file : main.c$ a1 o8 k" b* F! D; I( E
- * @brief : Main program body4 D5 ]$ c0 E0 l. X/ ^5 |7 f
- ******************************************************************************, o9 t7 D% `5 F0 Y0 d. w C
- * @attention
. I7 p7 `6 A4 u5 [$ y: c - *
[8 E$ _. I* ?+ H - * <h2><center>© Copyright (c) 2019 STMicroelectronics.6 U% f8 `' B$ V5 C
- * All rights reserved.</center></h2>) ^5 M3 U4 Y' K7 w3 k0 V0 w
- *
$ E7 R0 k8 v0 Y, n4 W% ?6 _) ? - * This software component is licensed by ST under BSD 3-Clause license,) L& E4 t H" ?
- * the "License"; You may not use this file except in compliance with the
% j: O) u) E# f( t+ i1 n - * License. You may obtain a copy of the License at:1 N s. `& ?3 O# U% m; I; |
- * opensource.org/licenses/BSD-3-Clause
5 ?" Y$ g' F( q. \ - *
" w6 h( [! L4 t5 N - ******************************************************************************
" B# b2 h8 F/ L# l5 l - */
7 _4 [0 \0 ]5 Q: `- f - /* USER CODE END Header */" b `& L% q: j
- $ ~# n, U/ B' A% T$ h9 z
- /* Includes ------------------------------------------------------------------*/( @, w$ s+ z) m; I* F5 d
- #include "main.h"- F: I* W9 y8 g1 w& }, I* [
- #include "can.h"9 z, _2 s% m6 J
- #include "dma.h"6 T& x8 H- B; f% T7 K/ E
- #include "lwip.h"' w( \1 ]) L4 y+ E- O$ G3 [# x( a
- #include "usart.h"
6 G7 W$ H: c! ~8 x0 X8 t - #include "gpio.h"
% w3 [8 A3 F) o9 ^0 X -
8 b1 [) l' c, j! |+ ? - /* Private includes ----------------------------------------------------------*/
) W4 C7 v4 J' F9 o0 D - /* USER CODE BEGIN Includes */ n, P2 D4 K' J& m2 o* W2 x: X! }
- #include "string.h"
3 T& N# b" S4 Z6 E1 a8 s - #include "tcp_echoclient.h"
. Z8 @8 a9 S K* R/ P6 K -
5 k8 r5 B% q% N8 t8 B/ l3 o2 P - /* USER CODE END Includes */: v% _0 Z4 g* i a9 A3 _0 V5 n
-
/ }( x1 {5 F) n1 { Q0 ?; K3 h5 S - /* Private typedef -----------------------------------------------------------*/7 |( ]4 e! ], K- z7 ], a
- /* USER CODE BEGIN PTD */
6 {5 q+ M' v) a" u, q - 0 H7 `9 k" N7 I ~" X
- /* USER CODE END PTD */) B0 m. }4 A0 D/ R7 L1 P
- 7 {- d/ s, a; c9 |1 d
- /* Private define ------------------------------------------------------------*/& W; c& x( g$ k: B
- /* USER CODE BEGIN PD */# f5 @8 Q, a3 k7 h' g$ e
- #define COUNTOF(__BUFFER__) (sizeof(__BUFFER__) / sizeof(*(__BUFFER__)))
7 c% m* f# P, ?* M' B- T7 d" n2 E -
; V# ]2 a& ~: j4 Y1 d; V% ^$ [ - /* USER CODE END PD */
7 w- f2 Y2 Z8 w- i% h# K+ P - & |) v% P$ D7 y1 T3 L
- /* Private macro -------------------------------------------------------------*/
4 A6 O& g5 Z g7 d0 c0 |1 y; p - /* USER CODE BEGIN PM */0 c; w4 f3 o$ d) H8 k1 L6 T
-
9 U N$ e( I" ~7 ? - /* USER CODE END PM */
1 A4 e' R) u( U) F3 b$ A" a+ @ - 9 z2 j7 ]2 D" r; `8 U7 |
- /* Private variables ---------------------------------------------------------*/3 M+ Z( d( |! F4 `/ [$ M
-
: Q& q4 O3 M: \/ x/ P - /* USER CODE BEGIN PV */
& x1 }2 H0 F& K - uint32_t counter_10ms, counter_50ms, counter_100ms, counter_200ms, counter_500ms, counter_1000ms, counter_2000ms, counter_5000ms, counter_10s;
S) G" l6 \. o8 L - uint8_t flag_1ms, flag_10ms, flag_50ms, flag_100ms, flag_200ms, flag_500ms, flag_1000ms, flag_2000ms, flag_5000ms, flag_10s;
0 Q: R& n/ b/ V3 r% S2 k) }& A - uint8_t b_KeyNumber;- S. Z0 Y$ U7 v2 Q: f) p
- , x- O2 V: Q4 W4 y$ W
- uint8_t a_DtuInBuffer[100];
' v, g Q- i" [2 _4 m: t - uint16_t b_DtuInBufferRxCnt;
% `; X0 Z- H, c9 W; D& ^ -
# R1 i8 w, b+ n - uint8_t a_DtuOutBuffer[100];+ H9 c# Q) H$ @2 L; O
- uint16_t b_DtuOutBufferRxCnt;
$ X/ j J' p0 ^5 }' k3 h - ; y. h) ]1 X9 Z4 H
- struct netif gnetif;
9 R( M5 \" ^0 o# ? - extern struct tcp_pcb *echoclient_pcb;8 w+ }# ^- j( D# s* f5 n1 j
- uint32_t status_TCPProc;
5 N' d* r" U4 W - & W; M4 X1 ?# w. |& o5 J- l
-
( J8 U- \4 j; S% n$ @0 ` - uint8_t a_SocketConfig[7];
# }# D+ `3 c3 T$ y/ K# k! A! ]4 g: ` - uint8_t a_SocketHeartBeat[60];
; W$ p1 v! ^( u1 R! G( |! l, K - uint8_t a_SocketHeartBeatInterval[2];1 Y( J/ h+ c, H0 x
- uint8_t a_SocketRegister[60];- }# U/ L2 C# G
-
+ Z+ p3 B( G/ E& U3 j - uint8_t a_DtuInOutConfig[2];8 h0 ^" S5 E$ r Z5 z) A
-
; `' k, A0 `* T) t" x7 ~1 I - uint8_t b_WifiMode;2 K* ^( |7 u/ ~) w& D4 r) i3 m& t
- uint8_t a_RouterSsid[20];
, D; r4 J) q9 l! }+ o - uint8_t a_RouterPswd[20];/ F& @( d6 A# G- Q8 r3 P
-
2 K7 D6 x3 G' G - uint8_t a_Rs485Config[5];
2 }; ?- Q) p! a/ l - uint8_t a_CanConfig[5];
: A% A( L9 m" T - & S0 {3 k! I ?* S9 y* T9 E
- uint8_t flag_DtuConfigUpdate;
. d' g. ~3 L- z2 y1 E1 T# h% R. ^/ G' u - uint8_t flag_DtuEthReady = 0;
" f, m( V! O" z; x. y: X -
9 K5 L: t, b5 o - uint32_t b_HeartBeatPackageCnt = 0;
- M1 n, S L& w -
% r+ d! K) f1 J - uint32_t b_232STM32ParamConfigBusyCnt = 0; //尝试通过串口和STM32交互时候,会让出相应时间,优先处理
$ o6 n3 P6 U0 K, n3 b, i0 R -
8 }1 L4 ]3 T; i6 _( U1 {/ }% U - /* USER CODE END PV */) N- d& m0 X) Q- q o v
- ' W7 _* M0 U, k4 I
- /* Private function prototypes -----------------------------------------------*/6 @% L' d1 K" N. j% x3 ]( ?
- void SystemClock_Config(void);7 w% @6 P t: o) Q4 L8 K7 d
- /* USER CODE BEGIN PFP */
0 A0 o$ a% S9 n- h - ) F) @/ P" v: Q/ Z/ E' d7 W
- /* USER CODE END PFP */+ `# S' j8 V# O& n
- 9 t$ a& [/ Q& R' R, F1 i
- /* Private user code ---------------------------------------------------------*/
7 U" j, X$ p- E* j - /* USER CODE BEGIN 0 */- [% B/ c* v# d0 t
- void Task_HeartBeatPackSent(void)% X: \- i2 \% K& f* O. e
- {( o4 r' O! f+ Z: j. b
- uint16_t b_interval;
6 I/ }. G% V8 B: Q- b/ O+ P4 ~ -
u" l: L# c! d0 L9 F- N - b_interval = (a_SocketHeartBeatInterval[0]<<8) + a_SocketHeartBeatInterval[1];
2 l$ P' r! Q' K2 N( ~ - / H- e& Y5 U& F7 N1 M) G% c1 i
- b_HeartBeatPackageCnt++;3 l( u( y4 O3 e) S# Z1 w
- if(b_HeartBeatPackageCnt>=b_interval)
/ |# k( s% q, Q! X" l: h5 d - {/ o# E# n k% F$ C
- b_HeartBeatPackageCnt = 0;7 j5 K0 Q, u* i6 x
- ( ^* t/ D: L$ F3 j- _
- , j# G9 f9 e0 k7 V9 y" k h2 k
- if(a_DtuInOutConfig[1]==1)
& ~" K8 b. T9 ?# Y) K; {6 w - {; B* K+ y C, W$ U
- if(flag_8266DTUReady==1)& J8 }$ J2 }. A. i; L
- {
- h# A/ D4 ?; g, i4 ^1 \* I9 i+ R - HAL_UART_Transmit(&huart3, a_SocketHeartBeat, strlen((char*)a_SocketHeartBeat), 100);" S% }; U( w* s: e) |
- printf("\r\n");
: ], R: q- e( S3 g) U" j - printf("\r\nA heart beat package is sent.\r\n ");
; {- J9 W0 l* c$ J W - }
$ u- d) r1 N! ^2 K - }! j" z9 I" q) |
- else if(a_DtuInOutConfig[1]==2)
H/ g- t2 {8 o" } - {/ o" O! K4 d7 [
- if(status_TCPProc>=2)9 F& T/ V1 i! V+ S% I ^
- {' g4 D' _# Y! c) X& t
- tcp_senddata(1);( { F t% U+ B5 P# A! N
- printf("\r\n");- B* X) E& T" m0 ]# r/ T# P
- printf("\r\nA heart beat package is sent.\r\n ");
" {0 [# `8 M, O: c9 l - }
% w1 S' l' |# ]& M3 i' V - }- G* {8 y# |5 |& O! B+ l- j& J+ N, B
- }: ]' {3 ~' y( w' l" S' c' [' }0 B
- }
4 E4 k6 G& u5 `" y+ ]5 F) ]$ k - void cleanDtuInBuffer(void)" |8 K' L& R. h( M3 E5 a
- {- d: H2 j5 J, ^- v2 c3 k; Y/ }6 B' a
- uint32_t i;3 y% {5 j- h- X7 T1 f
- for(i=0;i<b_DtuInBufferRxCnt;i++)
% w! W* u. U( d$ j - {% a1 b8 S: S" o: i, ]( Z
- a_DtuInBuffer[i] = 0;& e L* g& e3 |& E6 o# b6 g
- }* C+ Z4 e8 y' A6 Z2 Y/ Z/ A* t
- b_DtuInBufferRxCnt = 0;
3 ~& ^0 D+ k$ g/ `! ^. H - }6 b$ i+ N% Q& S. S8 O9 W+ r# T
- void cleanDtuOutBuffer(void)4 h$ W5 A- z1 a$ S- E
- {
( a6 d# l) \) X& T - uint32_t i;
V1 o- I7 c, S' N: G: I) f& V; C - for(i=0;i<b_DtuOutBufferRxCnt;i++)6 d' L& }7 v: z- v( `" b! T7 y$ a
- {
1 X. I5 E8 T; x# S+ N* O - a_DtuOutBuffer[i] = 0;
3 d o0 v- ^6 A. w0 M/ T2 E - }
3 o5 L | P1 ^+ q5 r. K# `7 I - b_DtuOutBufferRxCnt = 0;
6 a( g+ |2 b+ H2 \2 }# f - }% ^! K, p4 N; Y% D) G
-
: v6 f; ?5 T! ^ - void Task_DirectTransfer(void)/ R. L( E; W) J% [4 O* T
- {% h) m& A1 J, i% E
- uint32_t i, k;
3 A7 j m7 K" b. H4 r8 Z - //把数据输出到以太网或者WIFI' J. u- F7 j4 [0 I9 M
- if(a_DtuInOutConfig[1]==1)
1 S2 C3 a4 q9 l" T - {! }% |& u' J j2 o' v
- if(flag_8266DTUReady==1)
& l5 Y- S. g- L) U2 M+ z - {
4 R0 ^4 S; {9 Q) H! i6 E5 G) Y - if(b_DtuInBufferRxCnt>0)* U- J+ P! V+ q+ }6 O
- {
2 \) I7 B* V. g* @% g& Z( U1 E - printf("\r\n");& f" Y" W: [. ?' v
- printf("\r\nData sent to Wifi: ");
4 q. I9 w2 G/ G/ `, v9 m - HAL_UART_Transmit(&huart3, (uint8_t*)a_DtuInBuffer, b_DtuInBufferRxCnt, 100);, o7 C5 e) D. C2 z) B
- HAL_UART_Transmit(&huart1, (uint8_t*)a_DtuInBuffer, b_DtuInBufferRxCnt, 100); //debug- N0 v0 | h+ k+ I( f7 w+ q' ]1 P
- cleanDtuInBuffer();
4 M' j5 |' u- N) p4 V - }: g- b" X; G3 a- N
- }: A, v. W' i, n0 ^
- }
- W( {* B( R% u, j6 X) A0 \ - else if(a_DtuInOutConfig[1]==2)
/ p0 E4 |6 _7 J6 G. q5 f - {: B/ \0 ^8 w; |. u* j0 h6 j
- if(status_TCPProc>=2)
H) g4 K( N0 H1 I1 X; h' } - {: k& t1 |$ f7 `
- if(status_TCPProc == 2)+ R/ L- c% {. ]2 ?) H
- {
- H) U. P- W6 @! B8 B0 x - tcp_senddata(0); //专门发送注册包
: A, X' J" {# f1 ]% J - }
( y5 U) U" T5 O1 }- \ O& M1 J5 H - if(b_DtuInBufferRxCnt>0)# F* W2 ^; O E' r( P
- {4 J$ p( A7 x% e" e
- if(status_TCPProc <= 3), {/ ?5 B4 f! i
- {8 f5 Q$ x9 C ~& @ h/ \
- tcp_senddata(0);
2 ]: _ ]- R% Q+ t - }
* r4 l, v/ z$ x9 J* W - else& M, ~! B# V+ m9 L, \" g3 |+ e
- { l$ y) s% p; q
- status_TCPProc = 0;
+ S& j9 F; k. h - }
( V V# C8 D9 a2 O1 t0 I - cleanDtuInBuffer();
3 a/ F* ?2 [4 P8 X5 b0 j" j - }
$ P; K1 Q; {% q! T, v1 y4 t - }% p1 \% i* @* t0 \1 o
- }9 F- I8 B7 L' z9 I& {
- : d: Y' C; b4 u0 o% V# ^
- $ s! V- q$ }# D
- //把数据输出到RS232,RS485或者CAN
3 p o; Z2 `2 i) n/ N2 t8 d - if(a_DtuInOutConfig[0]==1)
& K& m) F( A! {0 W+ S) | - {/ } W3 h6 L8 a& K$ {. Z/ D; I) @
- if(b_DtuOutBufferRxCnt>0)2 }; T: O; `! k# E/ Y+ r6 z
- {
: S0 M( j+ D: Y& u, x8 o! v - HAL_UART_Transmit(&huart1, (uint8_t*)a_DtuOutBuffer, b_DtuOutBufferRxCnt, 100);
; h& B; D: D7 u& ?, Z1 k - cleanDtuOutBuffer();
& U' y5 `; n7 z* j! V. e - }
z, C; ~! `2 K( P0 j' _9 [ - }
# S; d( X7 w, L, E6 n - else if(a_DtuInOutConfig[0]==2)
/ C* J9 Z1 b4 w9 e* [) }2 J - {3 f% n) ^% G2 E7 M" |1 G9 s
- if(b_DtuOutBufferRxCnt>0)# W: u- J; p- p! X' G
- {5 m7 e1 K2 X# s9 L/ Q- F
- printf("\r\n");
: U0 J8 N; y" ^4 T - printf("\r\nData sent to RS485: ");
+ g7 h: k6 c! n) I - HAL_UART_Transmit(&huart1, (uint8_t*)a_DtuOutBuffer, b_DtuOutBufferRxCnt, 100);//debug
! @. p6 X0 B" e5 O. x - RS485_TX_MODE();0 U# R3 X; t: S0 [! \
- HAL_UART_Transmit(&huart5, (uint8_t*)a_DtuOutBuffer, b_DtuOutBufferRxCnt, 100);& J; X1 I. _" K6 \. E/ l
- HAL_Delay(1);
+ O: `+ E2 X, [# O) t$ g l6 W - RS485_RX_MODE();
1 O4 O; o4 z$ k7 o/ {) g - cleanDtuOutBuffer();2 y1 z X" n% ~4 w7 F
- }
; g1 z5 h) i: {1 {! G4 W2 E& x - }
0 b n: \5 ^- M - else if(a_DtuInOutConfig[0]==3)! N, l7 N5 Y0 q6 l
- {
! ]( w! u& c! t' k3 L: ~" b4 { - if(b_DtuOutBufferRxCnt>0). z* c" Z# G' F& w6 O% R
- {0 \9 k Q& N) [3 d5 }
- k = 0;& E1 V; y! Z' J/ Q
- if(b_DtuOutBufferRxCnt>8)
% k0 [) p, L& T - {
- H T! F' z, ^ n& P, T" ~' X - for(i=0; i<8; i++)
& h4 L! t6 S4 a3 V - {
" \+ `" H( x7 g& f - Can_TxData[i] = a_DtuOutBuffer[i];
, u) b; {# I1 j. N5 _1 S% K - k += Can_TxData[i];
( z7 ?3 B! }( P" ] - }/ N2 {5 p0 w% D( `2 \) G+ o
- }
& O4 s( H3 v+ F# _1 ` - else
) B/ F- w$ [ X, C8 q/ L( ]; s$ E( C - {
- ]+ s0 P+ A& L - for(i=0; i<b_DtuOutBufferRxCnt; i++)
/ K* M1 e" S( f - {
( ]% w) Y1 a8 c( z2 D" J - Can_TxData[i] = a_DtuOutBuffer[i];
! c5 J, H+ v9 l1 m6 C* H$ J. s0 T3 Z - k += Can_TxData[i];# [+ A$ q( u6 T* q0 p6 w' V6 H
- }" z7 b1 A1 Q' }- m1 ?- o4 t
- }
: g" b; |; v( I" p: z e) R- I, s - if(k>0)5 U( e1 N+ F/ i' x$ G
- {; |; i( i9 Z" b/ R$ F
- if(b_DtuOutBufferRxCnt>8)
9 k4 {7 B, H m4 B3 G! V: z - {
: K9 u* A0 ~, X2 h- K e - Can_TxMessage(0, Can_TxHeader.StdId, 8, Can_TxData);
) A8 q( v8 D0 f - }! H, D& {! c4 v2 P4 t5 E+ L
- else
" r" H( q! E; v+ T$ v( P" y - {7 t% j' D# _, g" \* N
- Can_TxMessage(0, Can_TxHeader.StdId, b_DtuOutBufferRxCnt, Can_TxData);4 B) z+ m o/ M7 A; i3 P) p
- }5 }9 g3 Q! v j! N. \! J) c: n' H) |
- ArrayLeftShift8bits(a_DtuOutBuffer, &b_DtuOutBufferRxCnt);
7 U# h& t/ X1 G' J7 x" Y, s - }1 W! M' u( Z' D3 Y! l) a( m, n4 \% Y
- else# R8 L2 @ u6 G; e$ e& m, j9 O. P
- {
/ n c) O( u' |+ ? - b_DtuOutBufferRxCnt = 0;
5 M$ q+ O0 O& L: g! y$ J - }& {! s6 @* [7 P
- }
9 h( B' G2 B! ]5 l# Z4 Z - }
9 Y% W- g6 c8 Z - }
- i* ^, _8 D7 W- G4 R - void Task_DtuRedoConfig(void). N3 G2 m/ L) Y0 G! @+ l( |/ |
- {
8 M2 V& ~# X; F, { - if(flag_DtuConfigUpdate==0)* P T& F9 h, B% c9 v' A& ^" a Y
- return;
" q. A6 J# [( s3 U5 ? - % I' M+ X3 ^( N' |6 |
- HAL_Delay(10);
! C+ R; r T( a" B4 j: ^: [ - printf("Re-initialize ETH, WIFI module, RS485... ");8 }) a& u/ J/ W, W; I& o; N
- //ETH:) o/ t( R" u+ @
- if(status_TCPProc >= 2)
. h) m; X. t9 W p' K - { Y# o4 b' ~1 _( h; Z0 P: ]5 w* p
- tcp_echoclient_disconnect();! V' a: i, ~9 ?# B% T
- status_TCPProc = 0;2 `( S4 e m" f' N% S
- }3 p. E) \7 B( Z. S
- //ESP8266:: g- v7 g% I& B; P" n* x
- if(Status_NodeMCU>=6)
! A& i4 }3 N( U& a - {
; w0 A6 e& }! }9 ]3 L* c& M - sprintf((char *)a_8266TxBuffer, "+++"); //esp8266 接收到这个+++会停止透传模式2 O7 f) L' H N1 y( Y
- HAL_UART_Transmit(&huart3, (uint8_t *)a_8266TxBuffer, strlen((char *)a_8266TxBuffer), 100);
1 ~1 t% a/ _1 T# V% a - Status_NodeMCU = 0;3 P/ R( ?! A" @- ~* r9 c
- HAL_Delay(100);
# J, k Z& e6 M$ B, x7 d - }
, u( \6 Z Z9 {- E* T! T: B- e - //485
3 I% n. A* |8 W. r* ]( _0 F - //MX_RS485_Reconfigure();
5 C f- A. V" S/ V5 _7 t& D - printf("Completed.\r\n");
. h9 h/ ?3 n8 m2 C* S$ f9 I* g - //8 l) S! Z' j. v! Y- y0 d
- flag_DtuConfigUpdate = 0;$ k% a) r+ u! @
-
Y6 ?3 s- i/ l7 O2 i - HAL_UART_Receive_IT(&huart1, &b_232RxByte, 1);8 x+ t; A6 q+ L2 X
- }
/ t( D% X$ a/ Y$ | - /* USER CODE END 0 */# h2 R& K9 h" K7 \" {3 g
-
! z1 d" L/ |. f! n4 D# @6 B - /**/ F7 C4 w& I6 ]7 s% |& i4 P
- * @brief The application entry point.
/ M" B# M% E/ x5 ]' H3 P; d - * @retval int
' U1 i* l _# G+ I - */* {/ a+ G A7 ?# }! F. E6 u
- int main(void)8 h0 b' `/ M7 Q7 E1 _) R3 D
- {+ O# i/ A; W$ D4 K) s; G
- /* USER CODE BEGIN 1 */: _1 r3 l( N. S# t5 @
-
% N. z: x i6 ` - /* USER CODE END 1 */$ c1 l# A2 T" Z- e, b. i
- 3 J3 V9 H4 |; u; h6 c
- ) F I& B% E0 V* K+ g G* [
- /* MCU Configuration--------------------------------------------------------*/5 s0 T% |' @0 b4 p
-
% _. A- d' |: G4 I+ \( I1 n# L4 j - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */2 n+ }4 Q* _ T p* I) e
- HAL_Init();
5 M$ F" c; x# d$ x - # O4 q5 v- R j- p% s
- /* USER CODE BEGIN Init */
4 P3 n u7 @! A' R2 o - , M0 }9 w% U. \7 x
- /* USER CODE END Init */
7 m, b8 k, z! O5 w" }4 a4 I - ( O+ O9 Y1 Q& O: n' d7 B$ R1 w
- /* Configure the system clock */% Z: ]% d4 j! ~' |/ j3 ^7 W7 c4 k |
- SystemClock_Config();
) Q, U [7 b0 Z8 @( V, C/ E - , O9 f! L M6 Q% x7 d$ N
- /* USER CODE BEGIN SysInit */
5 R' k/ x9 i& @8 w$ l -
5 `0 N2 X4 _4 F, a7 F - /* USER CODE END SysInit */
1 c+ t/ d4 `3 y -
* V8 C* R |( Z6 ~" K' u! [( \ - /* Initialize all configured peripherals */
0 b5 z% r* u8 p8 N! Y. M. f - MX_GPIO_Init();
I& h# T* O4 y/ L8 Z - MX_DMA_Init();
* C& r% i% {% \0 [" r2 @; B' y - MX_USART1_UART_Init();2 `7 g/ ^* [* I9 n- [
- MX_USART3_UART_Init();! C' ^; b& L" x: M! m# t1 Q
- MX_CAN1_Init();2 b. Y% j- t- {$ N1 p% b \2 ^
- MX_UART5_Init();$ i7 a! |5 ]+ D( _+ C6 [& |" Q
- MX_LWIP_Init();, I2 j7 s: Z4 B8 R5 I- C0 X. c" L( c
- /* USER CODE BEGIN 2 */; ?% U7 ^$ M2 l$ `! c R6 @5 k
-
+ X+ f% [7 O' L - RS232Interact_Init();6 T, }: I) k# R
- printf("Hello. STM32F107_DTU project: DTU. All in one.\n");6 S. G |: c7 X. J5 u& i
- ( U& o$ c0 t; }' ` z
- HAL_UART_Receive_IT(&huart1, &b_232RxByte, 1);
, G# X- P/ ]9 T- b/ o6 t6 a# D - HAL_UART_Receive_IT(&huart5, &b_485RxByte, 1);
( p, h+ M* u- n4 z5 I9 p: l -
3 L* P: }4 Y- V! m% h4 a -
: u% g. V- N9 R5 y/ Z - sprintf((char *)a_8266TxBuffer, "+++"); //esp8266 接收到这个+++会停止透传模式4 @! r; j/ C. T( I; j
- HAL_UART_Transmit(&huart3, (uint8_t *)a_8266TxBuffer, strlen((char *)a_8266TxBuffer), 100);. ]$ x" l: ~( @0 @& p, q
- Status_NodeMCU = 0;
5 G0 ?2 V% O8 D+ b' b7 [ -
3 j4 x5 Z& Q. Y i3 b - status_TCPProc = 0;, E, }5 r6 o% D5 V
-
, U2 {) V8 s$ q+ j - RS485_RX_MODE();
) Y6 f( U7 a! S5 G: [/ x* y - * X9 g% H9 v. Z
- CAN_Config_User();
4 Y9 j$ f/ L+ d$ f - 4 U6 |0 | J( C5 w$ R
-
) F/ z* z" f* e. {" A - /* USER CODE END 2 */8 G4 ~2 d$ V5 e( \
-
9 m7 I& o* `' w6 X8 B - /* Infinite loop */& e; j' t, q1 }2 q) r" F% h* f! b
- /* USER CODE BEGIN WHILE */2 y) f/ G$ A( i# U2 n
- while (1)+ c$ H. {+ R& {4 u
- {" k `% p" U! v3 B. F/ g5 n5 M* v
- if(a_DtuInOutConfig[1]==2)5 v0 k. H, G( D7 { H
- {# }0 }+ A- n( W, P2 e9 Q) ^
- MX_LWIP_Process();
( Z+ x/ d5 \8 w" E - }3 Q4 {0 g$ I+ d3 `. |$ v3 _$ l7 P
- if(flag_1ms ==1)
" }5 }7 m; S( `) z) q - {
5 ]- `8 O2 j+ Y3 ` - flag_1ms = 0;+ g* ~' d* W+ ?& a- A# g9 H, F* ]
- Task_232PC_Interact();
+ ]& J: e2 N7 }0 B# _2 p; |9 D1 I# m - 7 g0 Y0 A* `! o s
- if(flag_DtuConfigUpdate ==1)( y8 _& ?9 q* Y1 Y5 r% ?
- {
$ d5 |$ X5 H5 O/ [; r - Task_DtuRedoConfig();* H" C6 V. x7 _3 r' \! N( v
- }
; c6 A9 F4 q/ x1 V8 F$ i - if(b_232STM32ParamConfigBusyCnt>6)( x* Q9 ^$ G4 u8 d# R! L2 ]
- {/ s* N- X1 c) i1 ]7 \) s
- Task_DirectTransfer();5 C2 M) y7 O# @7 g& t
- }. y- p! G+ K2 N3 D0 M
- }. \% C& h& c: N8 q9 V& s
- if(flag_10ms==1); [% ?4 Q3 f8 y" j+ N+ d
- {
( l8 D6 ]) A" q, x+ f' ^ - flag_10ms=0;; w( k% s2 E9 l+ s
-
& s+ }- F) z8 F7 d! v" B - }
! c! u8 { C' ~" x% V - if(flag_50ms==1) { flag_50ms=0; }1 t3 }- v/ a- x3 p- q
- if(flag_100ms==1)& ~# j" i$ l9 m% n4 m
- {
9 E/ Q. {* Q0 l; o6 i" V: a5 b - if(b_232STM32ParamConfigBusyCnt>6). W4 k. \* f* ~+ j
- {
2 R) R4 a% W5 a: @4 e) I - if(a_DtuInOutConfig[1]==1)! ?5 j6 ]. N/ @0 h8 C% u# ]" A
- {
( L# K( A3 g: p* s- ~$ A8 ? - if((flag_ProcNodeMCUActive ==1)&&(status_232PC<12))
A/ Q1 T, o" {! n* f1 z l' p, x - {
2 x h+ K9 Q1 z9 X+ B- _ - Proc_NodeMCU();
) w/ x8 Z# o6 a2 f1 c - }
' p, c; Z; [; R1 k* C: Y - }
3 M+ j9 b/ v. j: X5 g' W* }0 b' x5 \8 X - }* g' w( r7 m+ o1 a. q2 t
- flag_100ms=0;
7 h+ i* D& r# Y: |# o* H - }7 ?. C! ^% ^( i
- if(flag_200ms==1) { flag_200ms=0; }; y; c9 n+ }3 G. h6 k2 B
- if(flag_500ms==1) { flag_500ms=0; }* M5 P9 \. q$ g6 s- m
- if(flag_1000ms==1)/ ?1 s# @7 Y |! |
- { E5 [% N! U- B0 c2 }) x- i' b) n
- flag_1000ms=0;" `9 M; E( w4 P5 O8 E8 N9 w% Z1 \
- HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);: H4 ^6 l$ J3 y" V% t! o: d
- 6 ?& s2 j3 h& d. y" s- t2 l7 F
- if(b_232STM32ParamConfigBusyCnt>6): w' E% c" N; O; f, [; B
- {
) j# B% d2 k- ]. A' T" Q - if(a_DtuInOutConfig[1]==2) ]" I3 c0 U/ E2 p I# H9 k( i0 [, U
- {
: V; T( Y( O; Q4 U1 E% Q - if(status_TCPProc<=1)0 D" t- E8 L2 c2 i, }& B# p
- {4 X8 N& B$ u" m/ t
- printf("connecting to TCP Server...\r\n");* X( V( ]. i. z" N) b$ T( B; G6 I
- tcp_echoclient_connect();
9 ?/ T9 o! s: l - }
) F- k: P2 J. q% R - }
* y' R5 U, C2 o% u6 c& d; A, L - Task_HeartBeatPackSent();
/ S+ ]4 V2 S! ~! g$ X/ N - }0 Q0 m4 q! x) |6 `0 N S3 L
- if(flag_ProcNodeMCUActive ==0)
4 p) J% u/ p3 p& a- `2 F - {
& b! Q- E' `1 U6 j3 k - flag_ProcNodeMCUActive = 1;; Z2 i3 U) w8 m9 V h
- }
4 D" B' G( W1 Z+ ^- t - if(b_232STM32ParamConfigBusyCnt<80)! V% A" `7 G: [8 \% T: j: u3 Y
- {
0 X! |6 x9 X1 X9 D - b_232STM32ParamConfigBusyCnt++;
9 f/ O& H% j$ f: ? - }3 q1 X W+ ~! b9 ?# x
- }
F. E" S+ M3 M9 I6 K5 T# f& _8 ~ - if(flag_2000ms==1)' h% {+ E! e' B0 k R) ^# U" E' s+ y$ N
- {
4 x* z6 ^5 Z2 j$ B' H0 y - flag_2000ms=0;
& _9 w ]* L0 J -
( I9 m/ L9 t# X5 i$ A, m - }
6 E- t- Q: Q- T5 \( c - if(flag_5000ms==1)
+ { u: v9 _! ^! M: V# r% ] - {
7 }: O& F' @& |0 ` - flag_5000ms=0; N/ ^4 v, k/ I( i
- }( i% e6 b3 h4 A. k* x" b9 d9 K
-
) I- u: d2 ^! G$ m; D - if(flag_10s==1). |: i( L p, p* U" Q2 E
- {5 O( n# u5 I1 k5 ~& u
- flag_10s=0;
z$ ~# b/ Y o5 [1 b - }
9 w( y3 M0 Z$ P4 O - /* USER CODE END WHILE */; O3 P* m# |0 [$ K7 I
- * |& T; w' \+ s& Z4 {3 N
- /* USER CODE BEGIN 3 */( z i h% b% M d
- }1 i- u; K: b ^* `; `
- /* USER CODE END 3 */0 ?8 c! ~1 n/ a4 U2 B" {3 [
- }; N' C. ~0 S! u1 o' ~; B, y0 I
-
& o* w6 C2 f) a, q: ?4 ` - /**
( `% r7 R5 M4 Z) _ C7 |6 m5 a. m% R - * @brief System Clock Configuration
! y# `2 W3 T) M# j - * @retval None
3 D3 r6 J! E1 T8 a3 @ - */! L. c5 }) n' s/ F# f
- void SystemClock_Config(void)( y: r6 X5 Y6 R, ^* _* \7 l7 w) P: G
- {6 Y3 t* T$ p/ `
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
; x7 Z0 V5 Y2 ]5 Q, |) Q - RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};, h& @8 T8 e3 _
- 8 B/ v+ w# |1 c' B
- /** Initializes the CPU, AHB and APB busses clocks ) l1 |+ T/ M* p3 Y+ p
- */) H4 E4 m& }1 N! c: f0 Y
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
4 D5 d7 m0 [6 Z: v, r - RCC_OscInitStruct.HSEState = RCC_HSE_ON;5 ?" v1 z% c3 r/ n( [
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV5;5 z- M- X9 ~2 j$ V; y
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;1 L; B% L& S5 w; _% \ a
- RCC_OscInitStruct.Prediv1Source = RCC_PREDIV1_SOURCE_PLL2;
1 R- x! Y3 K# |: G - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;/ G% Z" ]* I2 O
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
4 R* s" \( i; l - RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;2 S* g' B$ f, _/ ?2 Q
- RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL2_ON;
% ^2 s9 {# `& M, H' b - RCC_OscInitStruct.PLL2.PLL2MUL = RCC_PLL2_MUL10;
6 v0 W) u7 ]" D5 p' P3 S/ F - RCC_OscInitStruct.PLL2.HSEPrediv2Value = RCC_HSE_PREDIV2_DIV2; C( @/ l' q) g3 d; T7 {' `
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
& E* i6 }$ ~! l+ i$ b0 E - {, }1 U1 |3 U, u! v& [9 V
- Error_Handler();& p# t3 j8 |2 ~# t
- }) G, A, k5 b0 ]( U. x, }
- /** Initializes the CPU, AHB and APB busses clocks
2 y8 Z# [% N& l; U0 T - */
H" }$ `0 n" m7 y* I& b - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
) Y3 L2 N- M8 h/ p, A - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- ] |+ \9 @- L$ g% ~* s - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;2 n! z0 I+ _( [7 i% u. o
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;: c# @: |! a& J" A2 ^9 R7 k% B
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;- P" I& S* J3 F- ^$ x
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
' m2 P- {, v5 u7 a -
2 n- S, v0 Z% q0 c - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)+ e4 ?6 G+ }4 z d2 [' f
- {
4 u( b i: T* o6 A! P - Error_Handler();8 L$ Z' K- X: V5 E
- }
* T4 ~0 Q6 X+ p; x0 R0 A - /** Enables the Clock Security System - C) o2 I, V: ~* Q8 f6 t
- *// J8 ^ Q2 }, \; _$ Z
- HAL_RCC_EnableCSS();
[3 v* y) ?- L H4 M - /** Configure the Systick interrupt time
. J9 T, V- i6 i - */
; B7 O/ i. J. J9 W$ h - __HAL_RCC_PLLI2S_ENABLE();% e' Q# c3 p" o) @2 C
- }% `4 K$ v: e- _: P2 p
-
f; h* z* Q" Q* a4 L+ ^5 r - /* USER CODE BEGIN 4 */$ r9 g* G0 v4 _ f
- # m$ s# h7 T# @" `& J! @
- /**/ t$ a8 u3 Q$ v3 ~' J+ M/ y0 C6 Q
- * @brief HAL_SYSTICK_Callback()
; N9 I5 J# Y& X, y- M. j - * SW timer triggered every 1ms.& Z3 s3 z; T9 O
- */
( a6 O! c/ N! Q! Z/ Y8 b- A1 f - void HAL_SYSTICK_Callback(void): n5 J; Z' O& T& V
- {
$ Z6 C1 z7 r2 D7 I3 r# M - flag_1ms = 1;
+ D* v/ @5 J" |0 m+ K' V3 t& R - /*update counters*/* C- j6 q6 K9 r0 `" g
- if(counter_10ms<9) { counter_10ms++; }
( R2 D7 T" R4 L* C. h- I9 _0 K5 d9 `6 P - else { counter_10ms = 0; flag_10ms = 1; }
- v5 P9 B! x. R - if(counter_50ms<49) { counter_50ms++; }, n7 }! Q( W, R2 T4 j( Z) U
- else { counter_50ms = 0; flag_50ms = 1; }# j) W. N; e ~" z
- ( |; \6 X$ d2 ~
- if(counter_100ms<99) { counter_100ms++; }) t( i% l6 q+ _1 N5 L
- else { counter_100ms = 0; flag_100ms = 1;
" Q& t9 }3 C, c; S7 N. }8 A - if(counter_200ms<1) { counter_200ms++; }
+ n. [- o% u* ]9 M- L! }; G - else { counter_200ms = 0; flag_200ms = 1; }+ H0 G6 Y! f2 G" f
- if(counter_500ms<4) { counter_500ms++; }
* ]3 {9 u; U7 X* ^9 g - else { counter_500ms = 0; flag_500ms = 1; }
1 } E4 d* F9 L5 ~ - if(counter_1000ms<9) { counter_1000ms++; }& |& [1 A1 ^ v* ?$ H
- else { counter_1000ms = 0; flag_1000ms = 1; }
9 z6 y$ B' c, U; t* h( Y, p - if(counter_2000ms<19) { counter_2000ms++; }
+ \5 }7 [; l, [4 T) t R9 w - else { counter_2000ms = 0; flag_2000ms = 1; }
: @/ q0 e' ~( E4 i9 t5 j, ]! ` - if(counter_5000ms<49) { counter_5000ms++; }1 k' N' U. H' b3 k
- else { counter_5000ms = 0; flag_5000ms = 1; }
$ `% r: H$ w5 U2 Z0 X( v$ W - if(counter_10s<99) { counter_10s++; }
: f% y# L7 @% J8 {6 ^$ N - else { counter_10s = 0; flag_10s = 1; }
$ K8 x4 d8 G: v s- d, V5 d* T3 n - }* Y1 ]1 ~8 ]) M
- }) v' B; X( b7 s/ A- F, ~
-
5 T7 q2 G2 C# O, n, O! A" P( j - void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
1 Z% Y* z$ _# }1 c5 g1 \ - {
6 h2 t% D! y9 g- ?/ [0 g% X - if(GPIO_Pin ==GPIO_PIN_13)
+ d! r5 g5 Q' D# }8 B - {
/ B5 }* G+ b5 X3 f4 N' e - b_KeyNumber++;6 \. }3 C- V5 U* [4 ]; c7 p
- if(b_KeyNumber>=2)0 S: ]! Y7 ]8 X' m$ K6 b( E% w
- { @- f) c* L% E& h; e
- b_KeyNumber = 0;
- i) t. |; H8 Y5 p8 o6 [1 U5 Y0 @ - }
O2 m0 _& D+ C5 F# A - 4 g% D3 Y) ] J. v) i6 G
- sprintf((char *)a_DtuInBuffer, "a simple package\r\n");" t- X# B8 B$ A/ e6 N
- b_DtuInBufferRxCnt = strlen((char *)a_DtuInBuffer);2 S! V L$ i1 m2 X% L1 O: l1 A
- , t2 V: H" Y w' K* w7 P
- }
" n* s. |( o. k3 G - }
' S$ M$ I9 x. @5 h+ O( @! e -
( ?. n4 @+ A( l& j0 S - /* USER CODE END 4 */
* J6 K- y+ C. R& G' m b - . i$ H s/ |+ l% E
- /**( J8 x. K3 s: t( @0 A w
- * @brief This function is executed in case of error occurrence.
: K$ L1 A) s6 e1 |; I - * @retval None
! x3 E" ~ O b% ? Z - */
8 j+ P& _2 c4 K* F. j# H - void Error_Handler(void)5 T) F* `( W/ N e
- {
! ?7 ^0 `& q7 g s$ R8 I" p - /* USER CODE BEGIN Error_Handler_Debug */
' f q) _+ q7 e4 J5 g - /* User can add his own implementation to report the HAL error return state */# d8 b. U7 l9 r: F5 M
- % y0 O1 e. }7 g& T' T% l/ @" y
- /* USER CODE END Error_Handler_Debug */
2 U ]& ]. t+ `/ G7 X - }& [ F; e" b" M# A) E
-
+ \6 F# x1 o: g- [, n9 R - #ifdef USE_FULL_ASSERT* Q, X$ A {) c+ N
- /**
1 \( S/ J% T6 s& p) g5 E - * @brief Reports the name of the source file and the source line number( M% E2 P2 F0 h" R( v
- * where the assert_param error has occurred./ I- I4 x1 |- T0 W0 U& R. n
- * @param file: pointer to the source file name
$ @' A& G- Y& n3 y7 K - * @param line: assert_param error line source number
$ w" Y! \( d3 O T - * @retval None5 U, ~% l9 h Z' S
- */; ?7 \. s$ V- G% J* S0 E/ f
- void assert_failed(uint8_t *file, uint32_t line)
* S6 y% K0 t, H' j - {
, o: H. x* A& N - /* USER CODE BEGIN 6 */) E( Z2 e- b1 Y' n* ?2 E' m" w
- /* User can add his own implementation to report the file name and line number,$ B" v0 \; E7 E$ x( A: f
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
+ W- E' z" _5 V7 ?; F - /* USER CODE END 6 */
+ a [3 m# C& h# x2 }0 y \ - }
, q" Y8 h% c6 [3 m/ P2 h3 A - #endif /* USE_FULL_ASSERT */6 g4 O5 ^' b' C; k
-
6 P6 k( u( H! @, {) G7 n( H - /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
( r3 b4 x( A8 r+ [* a* n4 试验结果开发板上面的指示灯亮了,同时使用仿真器观察接收数组的内容: % @4 {' z9 ]2 J# a' D7 J: w
% V* h D7 ?5 ]2 k$ c |