基于Acconeer的A121-60GHz毫米波雷达传感器SDK移植及测距示例(STM32L496为例)
工程:
Keil工程资源
参考资料:
A121 datasheet 1.3
A121 HAL Software Integration User Guide
A121 STM32CubeIDE User Guide
官方参考示例工程:
XM125_MDK-AC5_A121_v1_0_0
(XM125相当于A121的最小系统板 硬件连接与裸机相同)
文章目录
- A121
- 引脚配置
- SDK移植
- RSS的SDK移植
- SDK移植函数
- SPI传输函数
- 等待中断函数
- 延时函数修改
- 函数指针赋值
- SDK示例移植
- 堆栈配置
- 测试SDK
- 上电测试
- 测距测试
- 附录:Cortex-M架构的SysTick系统定时器精准延时和MCU位带操作
- SysTick系统定时器精准延时
- 延时函数
- 阻塞延时
- 非阻塞延时
- 位带操作
- 位带代码
- 位带宏定义
- 总线函数
- 一、位带操作理论及实践
- 二、如何判断MCU的外设是否支持位带
A121
该传感器与MCU连接只需要一组SPI和一个中断GPIO、一个使能控制GPIO
且SPI的CPOL和CPHA都为0(空闲时低电平 且在第一个变化沿进行采样)
SPI速度可以达到50MHz 但不建议
A121作为从机使用 所以输出引脚为MISO 输入引脚为MOSI
引脚配置
与A121 STM32CubeIDE User Guide中介绍的基本相同
但建议SPI的速度设置为10M以下 并且选择8位数据传输
同时选中软件片选
关闭NSSP
之所以要选中如下配置 可以参考:
【STM32】HAL库中的SPI传输(可利用中断或DMA进行连续传输)
同时开启中断
GPIO配置:
包括软件片选 使能和中断
同样得开启GPIO中断
SDK移植
主要参考A121 HAL Software Integration User Guide中的内容
详细介绍了该如何进行移植前的配置
如:
也可以直接参考XM125_MDK-AC5_A121_v1_0_0示例工程来进行配置
其被移植部分的工程结构如下:
RSS的SDK移植
如图 将相关头文件拷贝到工程目录后 建议新建一个头文件用于导入这些库
比如:
#ifndef __A121_H__
#define __A121_H__
#include "main.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <complex.h>
#include <math.h>#include "acc_config.h"
#include "acc_config_subsweep.h"
#include "acc_definitions_a121.h"
#include "acc_definitions_common.h"
#include "acc_detector_distance.h"
#include "acc_detector_distance_definitions.h"
#include "acc_detector_presence.h"
#include "acc_hal_definitions_a121.h"
#include "acc_processing.h"
#include "acc_rss_a121.h"
#include "acc_sensor.h"
#include "acc_version.h"#include "acc_hal_integration_a121.h"
#include "acc_integration.h"
#include "acc_integration_log.h"#include "acc_control_helper.h"
#include "acc_processing_helpers.h"#include "ref_app_smart_presence.h"
#include "ref_app_tank_level.h"#include "example_service_subsweeps.h"
#include "example_service_multiple_configurations.h"
#include "example_service_hibernate.h"
#include "example_service.h"
#include "example_processing_subtract_adaptive_bg.h"
#include "example_processing_peak_interpolation.h"
#include "example_processing_noncoherent_mean.h"
#include "example_processing_coherent_mean.h"
#include "example_processing_amplitude.h"
#include "example_diagnostic_test.h"
#include "example_detector_presence_multiple_configurations.h"
#include "example_detector_presence.h"
#include "example_detector_distance_recorded_threshold.h"
#include "example_detector_distance_close_range.h"
#include "example_detector_distance.h"
#include "example_control_helper.h"
#include "example_bring_up.h"void Init_A121(void);#endif
当然 rss/lib目录下的静态库也要导入
并且静态库要配置为library file
SDK移植函数
位于integration目录下
除了头文件外 需要覆写三个.c文件中的函数
在这里 需要把工程中不同的引脚名称重新定义以下
比如:
#define A121_SPI_HANDLE A121_SPI_Handle#define SPI_SS_GPIO_Port A121_SPI_CS_GPIO_Port
#define SPI_SS_Pin A121_SPI_CS_Pin#define ENABLE_GPIO_Port A121_EN_GPIO_Port
#define ENABLE_Pin A121_EN_Pin#define INTERRUPT_GPIO_Port A121_EXTI_GPIO_Port
#define INTERRUPT_Pin A121_EXTI_Pin
stm32.c中的不用改 只是一些基本延时、内存操作
log.c中注释掉fflush(stdout);
(这里是清空stdout的语句 其中printf需要进行重定向 如果重定向过了 就不需要这一句了)
重定向参考:
【STM32】HAL库UART串口配置及重定向(解决接收中断与scanf不能同时工作、重定向卡死、低功耗一直唤醒的问题)
xm125.c中的函数需要进行更改:
前文说到 xm125就是A121的最小系统板 所以硬件的连接是一样的 所以可以直接拿来移植
SPI传输函数
前文说到 我们配置的SPI是8位传输
所以这里需要建立一个8位SPI传输函数
示例工程上的函数是16位 直接改成8位即可
同样 我这里是用中断的方式来进行连续传输的(如果要使用10M以上的连续传输 则替换成DMA的方式)
static void acc_hal_integration_sensor_transfer8(acc_sensor_id_t sensor_id, uint8_t *buffer, size_t buffer_length){(void)sensor_id; // Ignore parameter sensor_id// Set SPI_SS LOW (Activate)HAL_GPIO_WritePin(SPI_SS_GPIO_Port, SPI_SS_Pin, GPIO_PIN_RESET);//const uint32_t SPI_TRANSMIT_RECEIVE_TIMEOUT = 5000;#ifdef A121_USE_SPI_DMAspi_transfer_complete = false;HAL_StatusTypeDef status = HAL_SPI_TransmitReceive_DMA(&A121_SPI_HANDLE, (uint8_t *)buffer, (uint8_t *)buffer, buffer_length);if (status != HAL_OK){return;}uint32_t start = HAL_GetTick();while (!spi_transfer_complete && (HAL_GetTick() - start) < SPI_TRANSMIT_RECEIVE_TIMEOUT){// Turn off interruptsdisable_interrupts();// Check once more so that the interrupt have not occurredif (!spi_transfer_complete){__WFI();}// Enable interrupt again, the ISR will execute directly after thisenable_interrupts();}
#elseHAL_SPI_TransmitReceive_IT(&A121_SPI_HANDLE, (uint8_t *)buffer, (uint8_t *)buffer, buffer_length);while(A121_SPI_HANDLE.State!=HAL_SPI_STATE_READY && A121_SPI_HANDLE.State!=HAL_SPI_STATE_ERROR);
#endif// Set SPI_SS HIGH (De-activate)HAL_GPIO_WritePin(SPI_SS_GPIO_Port, SPI_SS_Pin, GPIO_PIN_SET);
}
等待中断函数
这里是官方写法 但是SysTick在我的工程中会关闭 可以替换成其他的超时计算方式
bool acc_hal_integration_wait_for_sensor_interrupt(acc_sensor_id_t sensor_id, uint32_t timeout_ms)
{(void)sensor_id; // Ignore parameter sensor_idconst uint32_t wait_begin_ms = HAL_GetTick();while ((HAL_GPIO_ReadPin(INTERRUPT_GPIO_Port, INTERRUPT_Pin) != GPIO_PIN_SET) &&(HAL_GetTick() - wait_begin_ms < timeout_ms)){// Wait for the GPIO interruptdisable_interrupts();// Check again so that IRQ did not occurif (HAL_GPIO_ReadPin(INTERRUPT_GPIO_Port, INTERRUPT_Pin) != GPIO_PIN_SET){__WFI();}// Enable interrupts again to allow pending interrupt to be handledenable_interrupts();}return HAL_GPIO_ReadPin(INTERRUPT_GPIO_Port, INTERRUPT_Pin) == GPIO_PIN_SET;
}
延时函数修改
为了避免HAL库的延时函数出错 统一换成我们自己的延时函数
void acc_hal_integration_sensor_enable(acc_sensor_id_t sensor_id)
{(void)sensor_id; // Ignore parameter sensor_idHAL_GPIO_WritePin(ENABLE_GPIO_Port, ENABLE_Pin, GPIO_PIN_SET);HAL_GPIO_WritePin(SPI_SS_GPIO_Port, SPI_SS_Pin, GPIO_PIN_SET);// Wait 2 ms to make sure that the sensor crystal have time to stabilizedelay_ms(2);
}void acc_hal_integration_sensor_disable(acc_sensor_id_t sensor_id)
{(void)sensor_id; // Ignore parameter sensor_idHAL_GPIO_WritePin(SPI_SS_GPIO_Port, SPI_SS_Pin, GPIO_PIN_RESET);HAL_GPIO_WritePin(ENABLE_GPIO_Port, ENABLE_Pin, GPIO_PIN_RESET);// Wait after disable to leave the sensor in a known state// in case the application intends to enable the sensor directlydelay_ms(2);
}
函数指针赋值
最后把定义的函数指针全部赋值到结构体内即可
这里需要注意的是 8位SPI传输和16位SPI传输选其一即可 我们用的8位 所以16位对应的函数为NULL
const acc_hal_a121_t *acc_hal_rss_integration_get_implementation(void)
{static const acc_hal_a121_t val ={.max_spi_transfer_size = STM32_MAX_TRANSFER_SIZE,.mem_alloc = malloc,.mem_free = free,.transfer = acc_hal_integration_sensor_transfer8,.log = acc_integration_log,.optimization.transfer16 = NULL,};return &val;
}
SDK示例移植
直接移植即可
其中 acc_processing_helpers.c
这里用到了<complex.h>
头文件 用于复数操作 但多数AC5编译器不支持 可以换成AC6 不过我这里没用到相关函数 所以没有进行配置
堆栈配置
如图:
其中 在函数acc_example_bring_up
中 用于分配内存
雷达数组大小ACC_RSS_ASSEMBLY_TEST_MIN_BUFFER_SIZE
是2048 所以还是给大一点堆栈空间好
测试SDK
官方示例中给出了几个函数用于测试各种功能
acc_example_bring_up(0,NULL); //测试雷达与MCU的通讯是否正常,A121雷达外部电路是否正常。acc_example_service(0,NULL); //原始数据。高精度测距,复杂场景测距从此入手。acc_example_detector_distance(0,NULL); //距离检测器,适用于简单场景测距。acc_example_detector_presence(0,NULL); //存在检测器,可用于人体检测。acc_example_processing_amplitude(0,NULL); //从Sparse IQ服务的原始数据提取距离信息的处理方法。
上电测试
调用acc_example_bring_up(0,NULL);
函数即可
输出效果:
其中 A121开头的为SDK中的函数运行输出
测距测试
调用acc_example_detector_distance(0,NULL);
函数即可
输出效果:
其中 A121开头的为SDK中的函数运行输出
该示例可以输出多个目标
同时会在开始测距之前初始化相关配置:
[A121] Acconeer software version a121-v1.0.0
[A121] 00:00:00.000 (I) (detector_distance) Detector Distance Config
[A121] 00:00:00.000 (I) (detector_distance) sensor: 1
[A121] 00:00:00.000 (I) (detector_distance) start_m: 0.250000
[A121] 00:00:00.000 (I) (detector_distance) end_m: 3.000000
[A121] 00:00:00.000 (I) (detector_distance) max_step_length: 0
[A121] 00:00:00.000 (I) (detector_distance) max_profile: PROFILE_5
[A121] 00:00:00.000 (I) (detector_distance) signal_quality: 15.000000
[A121] 00:00:00.000 (I) (detector_distance) threshold_method: CFAR
[A121] 00:00:00.000 (I) (detector_distance) peak_sorting_method: STRONGEST
[A121] 00:00:00.000 (I) (detector_distance) num_frames_in_recorded_threshold: 100
[A121] 00:00:00.000 (I) (detector_distance) fixed_threshold_value: 100.000000
[A121] 00:00:00.000 (I) (detector_distance) threshold_sensitivity: 0.500000
[A121] 00:00:00.000 (I) (detector_distance) Offset Calibration Config
[A121] 00:00:00.000 (I) (config) sweep_rate: 0.000000
[A121] 00:00:00.000 (I) (config) frame_rate: 0.000000
[A121] 00:00:00.000 (I) (config) sweeps_per_frame: 1
[A121] 00:00:00.000 (I) (config) continuous_sweep_mode: false
[A121] 00:00:00.000 (I) (config) double_buffering: false
[A121] 00:00:00.000 (I) (config) inter_frame_idle_state: DEEP_SLEEP
[A121] 00:00:00.000 (I) (config) inter_sweep_idle_state: READY
[A121] 00:00:00.000 (I) (config) num_subsweeps: 1
[A121] 00:00:00.000 (I) (config) subsweep: 0
[A121] 00:00:00.000 (I) (config) start_point : -30
[A121] 00:00:00.000 (I) (config) num_points : 50
[A121] 00:00:00.000 (I) (config) step_length : 1
[A121] 00:00:00.000 (I) (config) hwaas : 64
[A121] 00:00:00.000 (I) (config) receiver_gain : 16
[A121] 00:00:00.000 (I) (config) enable_tx : true
[A121] 00:00:00.000 (I) (config) phase_enhancement: true
[A121] 00:00:00.000 (I) (config) enable_loopback : true
[A121] 00:00:00.000 (I) (config) prf : PROFILE_1
[A121] 00:00:00.000 (I) (config) profile : 13.0MHz
[A121] 00:00:00.000 (I) (detector_distance) Far Noise Calibration
[A121] 00:00:00.000 (I) (config) sweep_rate: 0.000000
[A121] 00:00:00.000 (I) (config) frame_rate: 0.000000
[A121] 00:00:00.000 (I) (config) sweeps_per_frame: 1
[A121] 00:00:00.000 (I) (config) continuous_sweep_mode: false
[A121] 00:00:00.000 (I) (config) double_buffering: false
[A121] 00:00:00.000 (I) (config) inter_frame_idle_state: DEEP_SLEEP
[A121] 00:00:00.000 (I) (config) inter_sweep_idle_state: READY
[A121] 00:00:00.000 (I) (config) num_subsweeps: 4
[A121] 00:00:00.000 (I) (config) subsweep: 0
[A121] 00:00:00.000 (I) (config) start_point : 0
[A121] 00:00:00.000 (I) (config) num_points : 220
[A121] 00:00:00.000 (I) (config) step_length : 1
[A121] 00:00:00.000 (I) (config) hwaas : 2
[A121] 00:00:00.000 (I) (config) receiver_gain : 10
[A121] 00:00:00.000 (I) (config) enable_tx : false
[A121] 00:00:00.000 (I) (config) phase_enhancement: true
[A121] 00:00:00.000 (I) (config) enable_loopback : false
[A121] 00:00:00.000 (I) (config) prf : PROFILE_1
[A121] 00:00:00.000 (I) (config) profile : 19.5MHz
[A121] 00:00:00.000 (I) (config) subsweep: 1
[A121] 00:00:00.000 (I) (config) start_point : 0
[A121] 00:00:00.000 (I) (config) num_points : 220
[A121] 00:00:00.000 (I) (config) step_length : 1
[A121] 00:00:00.000 (I) (config) hwaas : 5
[A121] 00:00:00.000 (I) (config) receiver_gain : 10
[A121] 00:00:00.000 (I) (config) enable_tx : false
[A121] 00:00:00.000 (I) (config) phase_enhancement: true
[A121] 00:00:00.000 (I) (config) enable_loopback : false
[A121] 00:00:00.000 (I) (config) prf : PROFILE_3
[A121] 00:00:00.000 (I) (config) profile : 15.6MHz
[A121] 00:00:00.000 (I) (config) subsweep: 2
[A121] 00:00:00.000 (I) (config) start_point : 0
[A121] 00:00:00.000 (I) (config) num_points : 220
[A121] 00:00:00.000 (I) (config) step_length : 1
[A121] 00:00:00.000 (I) (config) hwaas : 7
[A121] 00:00:00.000 (I) (config) receiver_gain : 10
[A121] 00:00:00.000 (I) (config) enable_tx : false
[A121] 00:00:00.000 (I) (config) phase_enhancement: true
[A121] 00:00:00.000 (I) (config) enable_loopback : false
[A121] 00:00:00.000 (I) (config) prf : PROFILE_5
[A121] 00:00:00.000 (I) (config) profile : 15.6MHz
[A121] 00:00:00.000 (I) (config) subsweep: 3
[A121] 00:00:00.000 (I) (config) start_point : 0
[A121] 00:00:00.000 (I) (config) num_points : 220
[A121] 00:00:00.000 (I) (config) step_length : 1
[A121] 00:00:00.000 (I) (config) hwaas : 15
[A121] 00:00:00.000 (I) (config) receiver_gain : 10
[A121] 00:00:00.000 (I) (config) enable_tx : false
[A121] 00:00:00.000 (I) (config) phase_enhancement: true
[A121] 00:00:00.000 (I) (config) enable_loopback : false
[A121] 00:00:00.000 (I) (config) prf : PROFILE_5
[A121] 00:00:00.000 (I) (config) profile : 15.6MHz
[A121] 00:00:00.000 (I) (detector_distance) Far Sensor
[A121] 00:00:00.000 (I) (config) sweep_rate: 0.000000
[A121] 00:00:00.000 (I) (config) frame_rate: 0.000000
[A121] 00:00:00.000 (I) (config) sweeps_per_frame: 1
[A121] 00:00:00.000 (I) (config) continuous_sweep_mode: false
[A121] 00:00:00.000 (I) (config) double_buffering: false
[A121] 00:00:00.000 (I) (config) inter_frame_idle_state: DEEP_SLEEP
[A121] 00:00:00.000 (I) (config) inter_sweep_idle_state: READY
[A121] 00:00:00.000 (I) (config) num_subsweeps: 4
[A121] 00:00:00.000 (I) (config) subsweep: 0
[A121] 00:00:00.000 (I) (config) start_point : 48
[A121] 00:00:00.000 (I) (config) num_points : 87
[A121] 00:00:00.000 (I) (config) step_length : 4
[A121] 00:00:00.000 (I) (config) hwaas : 2
[A121] 00:00:00.000 (I) (config) receiver_gain : 10
[A121] 00:00:00.000 (I) (config) enable_tx : true
[A121] 00:00:00.000 (I) (config) phase_enhancement: true
[A121] 00:00:00.000 (I) (config) enable_loopback : false
[A121] 00:00:00.000 (I) (config) prf : PROFILE_1
[A121] 00:00:00.000 (I) (config) profile : 19.5MHz
[A121] 00:00:00.000 (I) (config) subsweep: 1
[A121] 00:00:00.000 (I) (config) start_point : 120
[A121] 00:00:00.000 (I) (config) num_points : 68
[A121] 00:00:00.000 (I) (config) step_length : 12
[A121] 00:00:00.000 (I) (config) hwaas : 5
[A121] 00:00:00.000 (I) (config) receiver_gain : 10
[A121] 00:00:00.000 (I) (config) enable_tx : true
[A121] 00:00:00.000 (I) (config) phase_enhancement: true
[A121] 00:00:00.000 (I) (config) enable_loopback : false
[A121] 00:00:00.000 (I) (config) prf : PROFILE_3
[A121] 00:00:00.000 (I) (config) profile : 15.6MHz
[A121] 00:00:00.000 (I) (config) subsweep: 2
[A121] 00:00:00.000 (I) (config) start_point : 264
[A121] 00:00:00.000 (I) (config) num_points : 30
[A121] 00:00:00.000 (I) (config) step_length : 24
[A121] 00:00:00.000 (I) (config) hwaas : 7
[A121] 00:00:00.000 (I) (config) receiver_gain : 10
[A121] 00:00:00.000 (I) (config) enable_tx : true
[A121] 00:00:00.000 (I) (config) phase_enhancement: true
[A121] 00:00:00.000 (I) (config) enable_loopback : false
[A121] 00:00:00.000 (I) (config) prf : PROFILE_5
[A121] 00:00:00.000 (I) (config) profile : 15.6MHz
[A121] 00:00:00.000 (I) (config) subsweep: 3
[A121] 00:00:00.000 (I) (config) start_point : 984
[A121] 00:00:00.000 (I) (config) num_points : 25
[A121] 00:00:00.000 (I) (config) step_length : 24
[A121] 00:00:00.000 (I) (config) hwaas : 15
[A121] 00:00:00.000 (I) (config) receiver_gain : 10
[A121] 00:00:00.000 (I) (config) enable_tx : true
[A121] 00:00:00.000 (I) (config) phase_enhancement: true
[A121] 00:00:00.000 (I) (config) enable_loopback : false
[A121] 00:00:00.000 (I) (config) prf : PROFILE_5
[A121] 00:00:00.000 (I) (config) profile : 15.6MHz
输出多个目标点时 直接在后面进行打印:
附录:Cortex-M架构的SysTick系统定时器精准延时和MCU位带操作
SysTick系统定时器精准延时
延时函数
SysTick->LOAD中的值为计数值
计算方法为工作频率值/分频值
比如工作频率/1000 则周期为1ms
以ADuCM4050为例:
#include "ADuCM4050.h"void delay_ms(unsigned int ms)
{SysTick->LOAD = 26000000/1000-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能52MHz的系统定时器while(ms--){while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待}SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{SysTick->LOAD = 26000000/1000/1000-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能52MHz的系统定时器while(us--){while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待}SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
其中的52000000表示芯片的系统定时器频率 32系列一般为外部定时器频率的两倍
Cortex-M架构SysTick系统定时器阻塞和非阻塞延时
阻塞延时
首先是最常用的阻塞延时
void delay_ms(unsigned int ms)
{SysTick->LOAD = 50000000/1000-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器while(ms--){while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待}SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{SysTick->LOAD = 50000000/1000/1000-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器while(us--){while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待}SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
50000000表示工作频率
分频后即可得到不同的延时时间
以此类推
那么 不用两个嵌套while循环 也可以写成:
void delay_ms(unsigned int ms)
{SysTick->LOAD = 50000000/1000*ms-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{SysTick->LOAD = 50000000/1000/1000*us-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
但是这种写法有个弊端
那就是输入ms后,最大定时不得超过计数值,也就是不能超过LOAD的最大值,否则溢出以后,则无法正常工作
而LOAD如果最大是32位 也就是4294967295
晶振为50M的话 50M的计数值为1s 4294967295计数值约为85s
固最大定时时间为85s
但用嵌套while的话 最大可以支持定时4294967295*85s
非阻塞延时
如果采用非阻塞的话 直接改写第二种方法就好了:
void delay_ms(unsigned int ms)
{SysTick->LOAD = 50000000/1000*ms-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器//while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待//SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{SysTick->LOAD = 50000000/1000/1000*us-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器//while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待//SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
将等待和关闭定时器语句去掉
在使用时加上判断即可变为阻塞:
delay_ms(500);
while ((SysTick->CTRL & 0x00010000)==0);
SysTick->CTRL = 0;
在非阻塞状态下 可以提交定时器后 去做别的事情 然后再来等待
不过这样又有一个弊端 那就是定时器会自动重载 可能做别的事情以后 定时器跑过了 然后就要等85s才能停下
故可以通过内部定时器来进行非阻塞延时函数的编写
基本上每个mcu的内部定时器都可以配置自动重载等功能 网上资料很多 这里就不再阐述了
位带操作
位带代码
M3、M4架构的单片机 其输出口地址为端口地址+20 输入为+16
M0架构的单片机 其输出口地址为端口地址+12 输入为+8
以ADuCM4050为列:
位带宏定义
#ifndef __GPIO_H__
#define __GPIO_H__
#include "ADuCM4050.h"
#include "adi_gpio.h"#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))#define GPIO0_ODR_Addr (ADI_GPIO0_BASE+20) //0x40020014
#define GPIO0_IDR_Addr (ADI_GPIO0_BASE+16) //0x40020010#define GPIO1_ODR_Addr (ADI_GPIO1_BASE+20) //0x40020054
#define GPIO1_IDR_Addr (ADI_GPIO1_BASE+16) //0x40020050#define GPIO2_ODR_Addr (ADI_GPIO2_BASE+20) //0x40020094
#define GPIO2_IDR_Addr (ADI_GPIO2_BASE+16) //0x40020090#define GPIO3_ODR_Addr (ADI_GPIO3_BASE+20) //0x400200D4
#define GPIO3_IDR_Addr (ADI_GPIO3_BASE+16) //0x400200D0#define P0_O(n) BIT_ADDR(GPIO0_ODR_Addr,n) //输出
#define P0_I(n) BIT_ADDR(GPIO0_IDR_Addr,n) //输入 #define P1_O(n) BIT_ADDR(GPIO1_ODR_Addr,n) //输出
#define P1_I(n) BIT_ADDR(GPIO1_IDR_Addr,n) //输入 #define P2_O(n) BIT_ADDR(GPIO2_ODR_Addr,n) //输出
#define P2_I(n) BIT_ADDR(GPIO2_IDR_Addr,n) //输入 #define P3_O(n) BIT_ADDR(GPIO3_ODR_Addr,n) //输出
#define P3_I(n) BIT_ADDR(GPIO3_IDR_Addr,n) //输入 #define Port0 (ADI_GPIO_PORT0)
#define Port1 (ADI_GPIO_PORT1)
#define Port2 (ADI_GPIO_PORT2)
#define Port3 (ADI_GPIO_PORT3)#define Pin0 (ADI_GPIO_PIN_0)
#define Pin1 (ADI_GPIO_PIN_1)
#define Pin2 (ADI_GPIO_PIN_2)
#define Pin3 (ADI_GPIO_PIN_3)
#define Pin4 (ADI_GPIO_PIN_4)
#define Pin5 (ADI_GPIO_PIN_5)
#define Pin6 (ADI_GPIO_PIN_6)
#define Pin7 (ADI_GPIO_PIN_7)
#define Pin8 (ADI_GPIO_PIN_8)
#define Pin9 (ADI_GPIO_PIN_9)
#define Pin10 (ADI_GPIO_PIN_10)
#define Pin11 (ADI_GPIO_PIN_11)
#define Pin12 (ADI_GPIO_PIN_12)
#define Pin13 (ADI_GPIO_PIN_13)
#define Pin14 (ADI_GPIO_PIN_14)
#define Pin15 (ADI_GPIO_PIN_15)void GPIO_OUT(unsigned int port,unsigned int pin,unsigned int flag);
void GPIO_BUS_OUT(unsigned int port,unsigned int num);void P0_BUS_O(unsigned int num);
unsigned int P0_BUS_I(void);void P1_BUS_O(unsigned int num);
unsigned int P1_BUS_I(void);void P2_BUS_O(unsigned int num);
unsigned int P2_BUS_I(void);void P3_BUS_O(unsigned int num);
unsigned int P3_BUS_I(void);#endif
总线函数
#include "ADuCM4050.h"
#include "adi_gpio.h"
#include "GPIO.h"void GPIO_OUT(unsigned int port,unsigned int pin,unsigned int flag)
{switch(port){case 0:{switch(pin){case 0:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_0));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_0));};break;case 1:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_1));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_1));};break;case 2:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_2));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_2));};break;case 3:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_3));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_3));};break;case 4:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_4));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_4));};break;case 5:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_5));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_5));};break;case 6:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_6));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_6));};break;case 7:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_7));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_7));};break;case 8:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_8));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_8));};break;case 9:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_9));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_9));};break;case 10:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_10));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_10));};break;case 11:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_11));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_11));};break;case 12:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_12));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_12));};break;case 13:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_13));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_13));};break;case 14:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_14));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_14));};break;case 15:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_15));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_15));};break;default:pin=0;break;}}break;case 1:{switch(pin){case 0:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_0));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_0));};break;case 1:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_1));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_1));};break;case 2:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_2));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_2));};break;case 3:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_3));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_3));};break;case 4:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_4));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_4));};break;case 5:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_5));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_5));};break;case 6:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_6));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_6));};break;case 7:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_7));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_7));};break;case 8:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_8));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_8));};break;case 9:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_9));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_9));};break;case 10:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_10));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_10));};break;case 11:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_11));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_11));};break;case 12:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_12));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_12));};break;case 13:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_13));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_13));};break;case 14:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_14));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_14));};break;case 15:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_15));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_15));};break;default:pin=0;break;}}break;case 2:{switch(pin){case 0:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_0));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_0));};break;case 1:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_1));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_1));};break;case 2:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_2));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_2));};break;case 3:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_3));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_3));};break;case 4:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_4));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_4));};break;case 5:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_5));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_5));};break;case 6:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_6));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_6));};break;case 7:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_7));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_7));};break;case 8:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_8));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_8));};break;case 9:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_9));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_9));};break;case 10:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_10));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_10));};break;case 11:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_11));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_11));};break;case 12:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_12));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_12));};break;case 13:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_13));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_13));};break;case 14:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_14));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_14));};break;case 15:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_15));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_15));};break;default:pin=0;break;}}break;case 3:{switch(pin){case 0:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_0));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_0));};break;case 1:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_1));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_1));};break;case 2:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_2));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_2));};break;case 3:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_3));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_3));};break;case 4:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_4));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_4));};break;case 5:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_5));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_5));};break;case 6:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_6));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_6));};break;case 7:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_7));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_7));};break;case 8:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_8));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_8));};break;case 9:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_9));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_9));};break;case 10:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_10));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_10));};break;case 11:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_11));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_11));};break;case 12:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_12));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_12));};break;case 13:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_13));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_13));};break;case 14:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_14));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_14));};break;case 15:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_15));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_15));};break;default:pin=0;break;}}break;default:port=0;break;}
}void GPIO_BUS_OUT(unsigned int port,unsigned int num) //num最大为0xffff
{int i;for(i=0;i<16;i++){GPIO_OUT(port,i,(num>>i)&0x0001);}
}void P0_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){P0_O(i)=(num>>i)&0x0001;}
}
unsigned int P0_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(P0_I(i)<<i)&0xFFFF;}return num;
}void P1_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){P1_O(i)=(num>>i)&0x0001;}
}
unsigned int P1_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(P1_I(i)<<i)&0xFFFF;}return num;
}void P2_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){P2_O(i)=(num>>i)&0x0001;}
}
unsigned int P2_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(P2_I(i)<<i)&0xFFFF;}return num;
}void P3_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){P3_O(i)=(num>>i)&0x0001;}
}
unsigned int P3_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(P3_I(i)<<i)&0xFFFF;}return num;
}
一、位带操作理论及实践
位带操作的概念其实30年前就有了,那还是 CM3 将此能力进化,这里的位带操作是 8051 位寻址区的威力大幅加强版
位带区: 支持位带操作的地址区
位带别名: 对别名地址的访问最终作 用到位带区的访问上(注意:这中途有一个 地址映射过程)
位带操作对于硬件 I/O 密集型的底层程序最有用处
支持了位带操作后,可以使用普通的加载/存储指令来对单一的比特进行读写。在CM4中,有两个区中实现了位带。其中一个是SRAM区的最低1MB范围,第二个则是片内外设区的最低1MB范围。这两个区中的地址除了可以像普通的RAM一样使用外,它们还都有自己的“位带别名区”,位带别名区把每个比特膨胀成一个32位的字。当你通过位带别名区访问这些字时,就可以达到访问原始比特的目的。
位操作就是可以单独的对一个比特位读和写,类似与51中sbit定义的变量,stm32中通过访问位带别名区来实现位操作的功能
STM32中有两个地方实现了位带,一个是SRAM,一个是片上外设。
(1)位带本质上是一块地址区(例如每一位地址位对应一个寄存器)映射到另一片地址区(实现每一位地址位对应一个寄存器中的一位),该区域就叫做位带别名区,将每一位膨胀成一个32位的字。
(2)位带区的4个字节对应实际寄存器或内存区的一个位,虽然变大到4个字节,但实际上只有最低位有效(代表0或1)
只有位带可以直接用=赋值的方式来操作寄存器 位带是把寄存器上的每一位 膨胀到32位 映射到位带区 比如0x4002 0000地址的第0个bit 映射到位带区的0地址 那么其对应的位带映射地址为0x00 - 0x04 一共32位 但只有LSB有效 采用位带的方式用=赋值时 就是把位带区对应的LSB赋值 然后MCU再转到寄存器对应的位里面 寄存器操作时 如果不改变其他位上面的值 那就只能通过&=或者|=的方式进行
要设置0x2000 0000这个字节的第二个位bit2为1,使用位带操作的步骤有:
1、将1写入位 带别名区对应的映射地址(即0x22000008,因为1bit对应4个byte);
2、将0x2000 0000的值 读取到内部的缓冲区(这一步骤是内核完成的,属于原子操作,不需要用户操作);
3、将bit2置1,再把值写 回到0x2000 0000(属于原子操作,不需要用户操作)。
关于GPIO引脚对应的访问地址,可以参考以下公式
寄存器位带别名 = 0x42000000 + (寄存器的地址-0x40000000)32 + 引脚编号4
如:端口F访问的起始地址GPIOF_BASE
#define GPIOF ((GPIO_TypeDef *)GPIOF_BASE)
但好在官方库里面都帮我们定义好了 只需要在BASE地址加上便宜即可
例如:
GPIOF的ODR寄存器的地址 = GPIOF_BASE + 0x14
寄存器位带别名 = 0x42000000 + (寄存器的地址-0x40000000)32 + 引脚编号4
设置PF9引脚的话:
uint32_t *PF9_BitBand =
*(uint32_t *)(0x42000000 + ((uint32_t )&GPIOF->ODR– 0x40000000) *32 + 9*4)
封装一下:
#define PFout(x) *(volatile uint32_t *)(0x42000000 + ((uint32_t )&GPIOF->ODR – 0x40000000) *32 + x*4)
现在 可以把通用部分封装成一个小定义:
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
那么 设置PF引脚的函数可以定义:
#define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414
#define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410 #define PF_O(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PF_I(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
若使PF9输入输出则:
PF_O(9)=1; //输出高电平
uint8_t dat = PF_I(9); //获取PF9引脚的值
总线输入输出:
void PF_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){PF_O(i)=(num>>i)&0x0001;}
}
unsigned int PF_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(PF_I(i)<<i)&0xFFFF;}return num;
}
STM32的可用下面的函数:
#ifndef __GPIO_H__
#define __GPIO_H__
#include "stm32l496xx.h"#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))#define GPIOA_ODR_Addr (GPIOA_BASE+20) //0x40020014
#define GPIOB_ODR_Addr (GPIOB_BASE+20) //0x40020414
#define GPIOC_ODR_Addr (GPIOC_BASE+20) //0x40020814
#define GPIOD_ODR_Addr (GPIOD_BASE+20) //0x40020C14
#define GPIOE_ODR_Addr (GPIOE_BASE+20) //0x40021014
#define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414
#define GPIOG_ODR_Addr (GPIOG_BASE+20) //0x40021814
#define GPIOH_ODR_Addr (GPIOH_BASE+20) //0x40021C14
#define GPIOI_ODR_Addr (GPIOI_BASE+20) //0x40022014 #define GPIOA_IDR_Addr (GPIOA_BASE+16) //0x40020010
#define GPIOB_IDR_Addr (GPIOB_BASE+16) //0x40020410
#define GPIOC_IDR_Addr (GPIOC_BASE+16) //0x40020810
#define GPIOD_IDR_Addr (GPIOD_BASE+16) //0x40020C10
#define GPIOE_IDR_Addr (GPIOE_BASE+16) //0x40021010
#define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410
#define GPIOG_IDR_Addr (GPIOG_BASE+16) //0x40021810
#define GPIOH_IDR_Addr (GPIOH_BASE+16) //0x40021C10
#define GPIOI_IDR_Addr (GPIOI_BASE+16) //0x40022010 #define PA_O(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PA_I(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入 #define PB_O(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
#define PB_I(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入 #define PC_O(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
#define PC_I(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入 #define PD_O(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
#define PD_I(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入 #define PE_O(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
#define PE_I(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入#define PF_O(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PF_I(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入#define PG_O(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
#define PG_I(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入#define PH_O(n) BIT_ADDR(GPIOH_ODR_Addr,n) //输出
#define PH_I(n) BIT_ADDR(GPIOH_IDR_Addr,n) //输入#define PI_O(n) BIT_ADDR(GPIOI_ODR_Addr,n) //输出
#define PI_I(n) BIT_ADDR(GPIOI_IDR_Addr,n) //输入void PA_BUS_O(unsigned int num);
unsigned int PA_BUS_I(void);void PB_BUS_O(unsigned int num);
unsigned int PB_BUS_I(void);void PC_BUS_O(unsigned int num);
unsigned int PC_BUS_I(void);void PD_BUS_O(unsigned int num);
unsigned int PD_BUS_I(void);void PE_BUS_O(unsigned int num);
unsigned int PE_BUS_I(void);void PF_BUS_O(unsigned int num);
unsigned int PF_BUS_I(void);void PG_BUS_O(unsigned int num);
unsigned int PG_BUS_I(void);void PH_BUS_O(unsigned int num);
unsigned int PH_BUS_I(void);void PI_BUS_O(unsigned int num);
unsigned int PI_BUS_I(void);#endif
#include "GPIO.h"void PA_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){PA_O(i)=(num>>i)&0x0001;}
}
unsigned int PA_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(PA_I(i)<<i)&0xFFFF;}return num;
}void PB_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){PB_O(i)=(num>>i)&0x0001;}
}
unsigned int PB_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(PB_I(i)<<i)&0xFFFF;}return num;
}void PC_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){PC_O(i)=(num>>i)&0x0001;}
}
unsigned int PC_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(PC_I(i)<<i)&0xFFFF;}return num;
}void PD_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){PD_O(i)=(num>>i)&0x0001;}
}
unsigned int PD_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(PD_I(i)<<i)&0xFFFF;}return num;
}void PE_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){PE_O(i)=(num>>i)&0x0001;}
}
unsigned int PE_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(PE_I(i)<<i)&0xFFFF;}return num;
}void PF_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){PF_O(i)=(num>>i)&0x0001;}
}
unsigned int PF_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(PF_I(i)<<i)&0xFFFF;}return num;
}void PG_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){PG_O(i)=(num>>i)&0x0001;}
}
unsigned int PG_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(PG_I(i)<<i)&0xFFFF;}return num;
}void PH_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){PH_O(i)=(num>>i)&0x0001;}
}
unsigned int PH_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(PH_I(i)<<i)&0xFFFF;}return num;
}void PI_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{int i;for(i=0;i<16;i++){PI_O(i)=(num>>i)&0x0001;}
}
unsigned int PI_BUS_I(void) //输出值num最大为0xFFFF
{unsigned int num;int i;for(i=0;i<16;i++){num=num+(PI_I(i)<<i)&0xFFFF;}return num;
}
二、如何判断MCU的外设是否支持位带
根据《ARM Cortex-M3与Cortex-M4权威指南(第3版)》中第6章第7节描述
也就是说 要实现对GPIO的位带操作 必须保证GPIO位于外设区域的第一个1MB中
第一个1MB应该是0x4010 0000之前 位带不是直接操作地址 而是操作地址映射 地址映射被操作以后 MCU自动会修改对应寄存器的值
位带区只有1MB 所以只能改0x4000 0000 - 0x400F FFFF的寄存器
像F4系列 GPIO的首地址为0x4002 0000 就可以用位带来更改
STM32L476的GPIO就不行:
AHB2的都不能用位带
ABP 还有AHB1都可以用
但是L476的寄存器里面 GPIO和ADC都是AHB2