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