STM32 study notes (a) --- Basics

STM32 learning Notes (a) - the basics

common sense

1. All pins of the chip sequence are in reverse order

2. Chip by the kernel and is mainly composed of on-chip peripherals.

Memory Map

Itself does not have a memory address, the memory allocated to the process is called address memory map .

To a memory reallocation process is called address memory remapping .

Memory Map

Register map

When a peripheral chip design, in four bytes as a unit, a total of 32bit, each cell corresponding to a different function, we can control the drive unit peripherals work.

Depending on the function of each unit to the memory unit to the function name aliases, the alias register is.

Has been assigned to the process-addressed memory cell has a specific function is called the alias register map

Examples: GPIOF output data register ODR memory address port is 0x4002 1414 (ODR 32-bit registers, 16-bit low-active, controlling the IO port 16 outputs high and low)

  1. Access memory unit via absolute addresses:

     *(unsigned int *)0x40021414 = 0xFFFF;
  2. Memory access means by way of the register

     #define GPIOF_BASE   0x40021400
     #define GPIOF_ODR   (unsigned int *)(0x40021400 + 0x14)
    
    *GPIOF_ODR = 0xFFFF;

Package c language register

/* 外设基地址 */
#define PERIPH_BASE (unsigned int)0x40000000

/* 总线基地址 */
#define APB1PERIPH_BASE PERIPH_BASE
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define AHB1PERIPH_BASE (PERIPH_BASE + 0x20000)
#define AHB2PERIPH_BASE (PERIPH_BASE + 0x10000000)

/* GPIO外设基地址 */
#define GPIOA_BASE AHB1PERIPH_BASE
#define GPIOB_BASE (AHB1PERIPH_BASE + 0x400)
#define GPIOC_BASE (AHB1PERIPH_BASE + 0x800)
#define GPIOD_BASE (AHB1PERIPH_BASE + 0xC00)
#define GPIOE_BASE (AHB1PERIPH_BASE + 0x1000)
#define GPIOF_BASE (AHB1PERIPH_BASE + 0x1400)
#define GPIOG_BASE (AHB1PERIPH_BASE + 0x1800)
#define GPIOH_BASE (AHB1PERIPH_BASE + 0x1C00)
#define GPIOI_BASE (AHB1PERIPH_BASE + 0x2000)

C language syntax predetermined storage space within the structure is a continuous variable.

typedef unsigned int uint32_t; /*无符号 32 位变量*/
typedef unsigned short int uint16_t; /*无符号 16 位变量*/

/* GPIO 寄存器列表 */
typedef struct 
{
 uint32_t MODER; /*GPIO 模式寄存器 地址偏移: 0x00 */
 uint32_t OTYPER; /*GPIO 输出类型寄存器 地址偏移: 0x04 */
 uint32_t OSPEEDR; /*GPIO 输出速度寄存器 地址偏移: 0x08 */
 uint32_t PUPDR; /*GPIO 上拉/下拉寄存器 地址偏移: 0x0C */
 uint32_t IDR; /*GPIO 输入数据寄存器 地址偏移: 0x10 */
 uint32_t ODR; /*GPIO 输出数据寄存器 地址偏移: 0x14 */
 uint16_t BSRRL; /*GPIO 置位/复位寄存器低 16 位部分 地址偏移: 0x18 */
 uint16_t BSRRH; /*GPIO 置位/复位寄存器高 16 位部分 地址偏移: 0x1A */
 uint32_t LCKR; /*GPIO 配置锁定寄存器 地址偏移: 0x1C */
 uint32_t AFR[2]; /*GPIO 复用功能配置寄存器 地址偏移: 0x20-0x24 */
 } GPIO_TypeDef;
GPIO_TypeDef * GPIOx;//GPIOx为指针,存储的是变量的地址
GPIOx = GPIOA_BASE; //将GPIOx指向GPIOA的基地址
GPIOx->MODER = oxFFFFFFFF; //通过结构体成员访问GPIOA的MODER寄存器
GPIOx->BSRRL = oxFFFF;

The method of operation of register bits

Clear operation: & = ~

Set operations: | =

The inversion: ^ =

  1. The variables are cleared by:
//x&=~(1<<n)   将变量x的第n位清零
uint8_t x = 0xff; //1111 1111 //bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
//将变量x的第三位清零
x &=~(1<<3); //1111 0111
  1. The variables a bit:
//x |= (1<<n)   将变量x的第n位置位
uint8_t x = 0xf0; //1111 0000
//将变量x的第零位置位
x |= (1<<0)  //1111 0001
  1. The negated by variables:
// x ^= (1<<n)   将变量x的第n位取反
uint8_t x = 0xff; //1111 1111
//将变量x的第2位取反
x ^= (1<<2); // 1111 1011
Published 48 original articles · won praise 78 · views 60000 +

Guess you like

Origin blog.csdn.net/xiaoyuanwuhui/article/details/98608173