你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32F2位带操作

[复制链接]
STMCU小助手 发布时间:2021-12-1 21:47
手册中说:
! i+ `+ g/ `  E  w6 _In the STM32F20x and STM32F21x both the peripheral registers and the SRAM are mapped to a bit-band region, so that single bit-band write and read operations are allowed.
+ h, f( v3 X" n+ B在这两个系列中外设和SRAM都有各自映射的位带区,以实现对位的单独操作。
. f# v9 `2 G! R: lThe operations are only available for Cortex ® -M3 accesses, and not from other bus masters (e.g. DMA).
- K5 P. x0 K/ ]% g使用局限于M3内核。
- ~0 j( [0 p' M. yA mapping formula shows how to reference each word in the alias region to a corresponding bit in the bit-band region. The mapping formula is:
1 J7 N5 }/ ^% ?% R  {地址映射公式如下
. p8 J5 I4 W( S, Q4 D2 s2 ?                               bit_word_addr  =  bit_band_base  + ( byte_offset  x 32) + ( bit_number  × 4)
6 d7 {  t7 s$ P+ F# ]  v: ewhere:: m$ \5 q/ Y" v
–  bit_word_addr  is the address of the word in the alias memory region that maps to the targeted bit& `' l9 d" P, d" p1 Y0 y5 D0 `
一位扩展成了一个字。4 x7 Q% k6 o1 P
–  bit_band_base  is the starting address of the alias region$ T; r* j0 S" z5 v
位带基地址是对应位带的起始地址。; {* ?2 q8 {& K
–  byte_offset  is the number of the byte in the bit-band region that contains the targeted bit
. R: U' }3 N$ X" D# T$ k$ `  M这里的偏移值为包含操作位的寄存器偏移值。& I! x) S4 J: H6 O% ^
–  bit_number  is the bit position (0-7) of the targeted bit  + l! k' I% D# A0 q
这里的位就是目标位。  d2 g; A; X) j9 F
0 H; X( V; ]/ ~
位带区在 SRAM上的地址范围:0x20000000 ~ 0x200FFFFF(SRAM区中最低1MB)$ n/ L) z. @" D2 ?; \% ^( }$ G
位带识别区在SRAM上的地址范围:   0x22000000 ~ 0x220FFFFF8 M: p9 ^- v2 p1 @+ K$ U3 t
位带区在片上外设的地址范围:0x4000 0000-0x400F FFFF(片上外设区中的最低1MB),
; F  K; R. X7 I- s; `1 s' ^$ U2 b. W$ H位带识别区在片上外设的地址范围:0x4200 0000~0x42FF FFFF;3 h/ J2 k6 C; P# G) }) f( s
对应关系:位带区的每个bit位的值 对应 位带识别区1个 32位的地址的内容;
0 o5 u9 \; h0 a; {+ ^所以位带操作是:当你通过位带别名区访问这些32位的地址的内容时,就可以达到访
- L! N) ~+ L; n3 v2 M' O  s问位带区对应的比特位。
' z$ a; N& U+ K. X+ r
6 J4 M( ^# _8 s举例:
" B$ d8 L1 P$ s0 I3 d, m2 @要给GPIO PC15做拉高拉低操作。
# s& [, @: Y( d: U. q5 Y% _4 X  s首先找到操作寄存器的地址:
' d% m8 q. C# r! y2 @+ SGPIO为外设,故需用外设的基地址:                   PERIPH_BASE               ((uint32_t)0x40000000); R$ j0 k+ V" K$ \
GPIOC在AH1外设上,故在之前基础上再做偏移:AHB1PERIPH_BASE       (PERIPH_BASE + 0x00020000)
. O1 s- k, q" e% t! j同时需要再加上GPIOC的偏移:                         GPIOC_BASE                  (AHB1PERIPH_BASE + 0x0800)
% ~* Q2 K. y$ I0 d. v7 `2 ^& O3 q然后找到位设置寄存器:                                   GPIOC_BSRR                  (GPIOC_BASE + 0x18)
; t6 }! ?+ f2 N0 M  a8 p9 S8 l5 w8 H( O  o, v) g0 J$ v
最终得到的地址为 :0x40020818
4 d/ P" r: H( j+ z+ P通常情况下向这个地址赋值即可实现指定位拉高拉低操作:
1 y+ O7 n: w# Z' b0 S3 k& p, ^  [*((volatile unsigned long *) 0x40020818) = 0x80000000  //!<拉高
7 B" N& ~7 E! V! }3 m*((volatile unsigned long *) 0x40020818) = 0x00008000  //!<拉低7 y8 P* G5 g2 ^+ d. e- |

! s8 n8 |* ^% ~2 p7 R7 F* D5 w但通过位带,按照公式获取位带操作地址:9 m$ C( `+ c. U
/*这是拉高时寄存器地址*/4 m  ?5 G! G/ c/ U! _, L5 y
AddrH =  *((volatile unsigned long *)(( 0x40020818  & 0xF0000000)+0x2000000+(( 0x40020818  & 0xFFFFF)<<5)+(15 << 2)))  
, q+ |8 L! ?7 T9 MAddrH = 1;   //!<置1就拉高 9 K- g7 y6 Q) e- @4 W; ^
/*这是拉低时寄存器地址*/
5 b$ o0 f; y2 A0 H3 b4 C$ c AddrL =  *((volatile unsigned long *)(( 0x40020818  & 0xF0000000)+0x2000000+(( 0x40020818  & 0xFFFFF)<<5)+((15+16) << 2))) ! `! L/ p! o+ f
AddrL = 1;   //!<置1就拉低   
: `0 Q9 C6 {: {9 U6 Q& m
, ~* K* n# r# c9 l使用宏定义,即:(Addr为 GPIOC_BSRR  拉高时 BitNum为15 拉低时 BitNum是(15+16) )
, }3 V3 U' a9 c* Z- h* {/ W#define BitBand(Addr,BitNum) *((volatile unsigned long *)((Addr & 0xF0000000)+0x2000000+((Addr & 0xFFFFF)<<5)+(BitNum << 2))) 4 _) c; n$ M) d9 _  Y+ \
5 \; _8 m, P! p; M/ I6 j
精简之后 ,位带操作   :5 t! t! B$ s" {1 g0 p; x5 a# q4 n) }
#define BitBand(Addr,BitNum) *((volatile unsigned long *)(PERIPH_BB_BASE|((Addr-PERIPH_BASE)<<5)|(BitNum << 2)))
! [) C/ X$ Y$ W8 h2 }" L+ {3 N
+ Q- h5 b; p2 X+ l. C! ?5 p
/ z' ~9 y* f. n7 Z/ Z+ ]0 S
收藏 评论0 发布时间:2021-12-1 21:47

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版