The engineering of a register of a punctual atom, usually consisting of the following documents:
Source Group |
Startup_stm32f40_41_xxx.s |
Startup file |
USER |
Main.c |
User Program Master File |
SYSTEM |
DELAY.C usart.c SYS.C |
The atomic team wrote their own files. |
HARDWARE |
Xxx.c |
The declaration of some functions called in MAIN.C |
Next, analyze the following several intrinsic files.
First, delay.c.
1#include"Delay.h"2#include"Sys.h" - - StaticU8 fac_us=0;// Delay of US - StaticU16 fac_ms=0;//MS DelayAfter the Uc_os-related conditional compilation is removed, only these are left, in fact, the initialization and three delay functions are defined.
A delay x us (x as a parameter into the function), the other two delay is Ms Delay, just XMS this function, the basic count unit or Systick, and the back of the MS function, called the XMS function, the delay is longer. - voiddelay_init (U8 sysclk) $ { theSystick->ctrl&=~ (1<<2); -fac_us=sysclk/8;Wuyi AFac_ms= (U16) fac_us* +; the } SysTick is a struct pointer, defined in CORE_CM4.H: #define SysTick ((Systick_type *) systick_base)
The Systick has 4 registers to control, see as follows:
typedef struct
{
__io uint32_t CTRL; /*!< offset:0x000 (r/w) SysTick Control and Status Register */
__io uint32_t LOAD; /*!< offset:0x004 (r/w) SysTick Reload Value Register */
__io uint32_t VAL; /*!< offset:0x008 (r/w) SysTick current Value Register */
__i uint32_t Calib; /*!< offset:0x00c (r/) SysTick Calibration Register */
} Systick_type;
The systick_base is the base address of the register, and the call to the register is completed in the subsequent program through operations such as Systick->load. After the function of the implementation are relatively good understanding, it is not verbose. Specifically, you can query the CPU for the introduction of Systick
111 voidDelay_us (u32 NUS) the { 113 U32 temp; the if(nus==0)return; thesystick->load=nus*fac_us; theSystick->val=0x00;117Systick->ctrl=0x01;118 Do119 { -Temp=systick->CTRL;121} while((temp&0x01) &&! (temp& (1<< -)));122Systick->ctrl=0x00;123Systick->val =0x00;124 }131 voidDelay_xms (U16 NMS) the { 133 U32 temp; 134Systick->load= (u32) Nms*fac_ms;135Systick->val =0x00;136Systick->ctrl=0x01;137 Do138 {139Temp=systick->CTRL; $} while((temp&0x01) &&! (temp& (1<< -)));141Systick->ctrl=0x00;142Systick->val =0x00;143 } 146 voidDelay_ms (U16 NMS)147 { 148U8 repeat=nms/540; MaxU16 remain=nms%540;151 while(repeat) the {153DELAY_XMS (540);154repeat--;155 }156 if(remain) delay_xms (remain);157 }
The following ideas are similar, do not introduce these files separately, only in main directly or indirectly call these, and then go back to learn these functions.
intMainvoid) {Stm32_clock_init (336,8,2,7);//This program is written in the seller's SYS.C, this is the chip internal clock tree initialization, the RCC register has been configured. Delay_init (168);//already introducedLed_init (); Configuring Gpio, also in sys.c, setting the various modes of GPIO while(1) {LED0=0; The led1= of the Gpio1; Delay_ms ( -); LED0=1; led1=0; Delay_ms ( -); }}
A small example that leads to a lot of things, the next thing you need to know is the RCC section, the Gpio section.
Register Chapter Learning 1