加入大疆C板例程bsp

This commit is contained in:
2026-04-12 20:34:47 +08:00
parent 7475077b9e
commit b68ac54a34
51 changed files with 5950 additions and 1290 deletions

74
bsp/boards/bsp_adc.c Normal file
View File

@@ -0,0 +1,74 @@
#include "bsp_adc.h"
#include "main.h"
extern ADC_HandleTypeDef hadc1;
extern ADC_HandleTypeDef hadc2;
extern ADC_HandleTypeDef hadc3;
volatile fp32 voltage_vrefint_proportion = 8.0586080586080586080586080586081e-4f;
static uint16_t adcx_get_chx_value(ADC_HandleTypeDef *ADCx, uint32_t ch)
{
static ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ch;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;//ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(ADCx, &sConfig) != HAL_OK)
{
Error_Handler();
}
HAL_ADC_Start(ADCx);
HAL_ADC_PollForConversion(ADCx, 10);
return (uint16_t)HAL_ADC_GetValue(ADCx);
}
void init_vrefint_reciprocal(void)
{
uint8_t i = 0;
uint32_t total_adc = 0;
for(i = 0; i < 200; i++)
{
total_adc += adcx_get_chx_value(&hadc1, ADC_CHANNEL_VREFINT);
}
voltage_vrefint_proportion = 200 * 1.2f / total_adc;
}
fp32 get_temprate(void)
{
uint16_t adcx = 0;
fp32 temperate;
adcx = adcx_get_chx_value(&hadc1, ADC_CHANNEL_TEMPSENSOR);
temperate = (fp32)adcx * voltage_vrefint_proportion;
temperate = (temperate - 0.76f) * 400.0f + 25.0f;
return temperate;
}
fp32 get_battery_voltage(void)
{
fp32 voltage;
uint16_t adcx = 0;
adcx = adcx_get_chx_value(&hadc3, ADC_CHANNEL_8);
voltage = (fp32)adcx * voltage_vrefint_proportion * 10.090909090909090909090909090909f;
return voltage;
}
uint8_t get_hardware_version(void)
{
uint8_t hardware_version;
hardware_version = HAL_GPIO_ReadPin(HW0_GPIO_Port, HW0_Pin)
| (HAL_GPIO_ReadPin(HW1_GPIO_Port, HW1_Pin)<<1)
| (HAL_GPIO_ReadPin(HW2_GPIO_Port, HW2_Pin)<<2);
return hardware_version;
}

9
bsp/boards/bsp_adc.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef BSP_ADC_H
#define BSP_ADC_H
#include "struct_typedef.h"
extern void init_vrefint_reciprocal(void);
extern fp32 get_temprate(void);
extern fp32 get_battery_voltage(void);
extern uint8_t get_hardware_version(void);
#endif

46
bsp/boards/bsp_buzzer.c Normal file
View File

@@ -0,0 +1,46 @@
#include "bsp_buzzer.h"
#include "main.h"
extern TIM_HandleTypeDef htim4;
void buzzer_on(uint16_t psc, uint16_t pwm)
{
__HAL_TIM_PRESCALER(&htim4, psc);
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, pwm);
}
void buzzer_off(void)
{
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, 0);
}
/**
* @brief 用蜂鸣器发出音符
* @param note note
* @param volume 音量,范围[0.0\\~1.0],用百分比表示
*/
void buzzer_note(uint16_t note,float volume)
{
if(volume > 1.0f)
{
volume = 1.0f;
}else if(volume < 0.0f)
{
volume = 0.0f;
}
// 禁用定时器
__HAL_TIM_DISABLE(&htim4);
// 重置定时器计数器
htim4.Instance->CNT = 0;
// 设置自动重装载寄存器ARR以控制PWM信号的频率
htim4.Instance->ARR = (8*21000 / note - 1) * 1u;
// 设置比较寄存器CCR3以控制PWM信号的占空比
htim4.Instance->CCR3 = (8*10500 / note - 1) * volume * 1u;
// 重新启用定时器
__HAL_TIM_ENABLE(&htim4);
// 启动PWM信号
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
}

8
bsp/boards/bsp_buzzer.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef BSP_BUZZER_H
#define BSP_BUZZER_H
#include "struct_typedef.h"
extern void buzzer_on(uint16_t psc, uint16_t pwm);
extern void buzzer_off(void);
extern void buzzer_note(uint16_t note,float volume);
#endif

45
bsp/boards/bsp_can.c Normal file
View File

@@ -0,0 +1,45 @@
#include "bsp_can.h"
void can_filter_init(void)
{
CAN_FilterTypeDef can_filter_st;
can_filter_st.FilterActivation = ENABLE;
can_filter_st.FilterMode = CAN_FILTERMODE_IDMASK;
can_filter_st.FilterScale = CAN_FILTERSCALE_32BIT;
can_filter_st.FilterIdHigh = 0x0000;
can_filter_st.FilterIdLow = 0x0000;
can_filter_st.FilterMaskIdHigh = 0x0000;
can_filter_st.FilterMaskIdLow = 0x0000;
can_filter_st.FilterBank = 0;
can_filter_st.FilterFIFOAssignment = CAN_RX_FIFO0;
HAL_CAN_ConfigFilter(&hcan1, &can_filter_st);
HAL_CAN_Start(&hcan1);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
can_filter_st.SlaveStartFilterBank = 14;
can_filter_st.FilterBank = 14;
HAL_CAN_ConfigFilter(&hcan2, &can_filter_st);
HAL_CAN_Start(&hcan2);
HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
}
/**
* @brief 发送控制数据
* @param[in] can_handle 选择CAN1或CAN2
* @param[in] tx_header CAN发送数据header
* @param[in] tx_data 发送数据
* @return none
*/
void CAN_SendTxMessage(CanCtrlData_s * can_ctrl_data)
{
uint32_t send_mail_box;
uint8_t cnt = 20; // 重复检测次数
uint32_t free_TxMailbox =
HAL_CAN_GetTxMailboxesFreeLevel(can_ctrl_data->hcan); // 检测是否有空闲邮箱
while (free_TxMailbox < 3 && cnt--) { // 等待空闲邮箱数达到3
free_TxMailbox = HAL_CAN_GetTxMailboxesFreeLevel(can_ctrl_data->hcan);
}
HAL_CAN_AddTxMessage(
can_ctrl_data->hcan, &can_ctrl_data->tx_header, can_ctrl_data->tx_data, &send_mail_box);
}

26
bsp/boards/bsp_can.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef BSP_CAN_H
#define BSP_CAN_H
#include "struct_typedef.h"
#include "stm32f4xx_hal.h"
// clang-format off
#define BOARD_DATA_ANY ((uint16_t)0x500)
#define BOARD_DATA_UINT16 ((uint16_t)0x600)
// clang-format on
typedef CAN_HandleTypeDef hcan_t;
typedef struct __CanCtrlData
{
hcan_t * hcan;
CAN_TxHeaderTypeDef tx_header;
uint8_t tx_data[8];
} CanCtrlData_s;
extern hcan_t hcan1;
extern hcan_t hcan2;
extern void can_filter_init(void);
extern void CAN_SendTxMessage(CanCtrlData_s * can_ctrl_data);
#endif

22
bsp/boards/bsp_crc32.c Normal file
View File

@@ -0,0 +1,22 @@
#include "bsp_crc32.h"
#include "main.h"
extern CRC_HandleTypeDef hcrc;
uint32_t get_crc32_check_sum(uint32_t *data, uint32_t len)
{
return HAL_CRC_Calculate(&hcrc, data, len);
}
bool_t verify_crc32_check_sum(uint32_t *data, uint32_t len)
{
static uint32_t crc32;
crc32 = get_crc32_check_sum(data, len-1);
return (crc32 == data[len-1]);
}
void append_crc32_check_sum(uint32_t *data, uint32_t len)
{
uint32_t crc32;
crc32 = get_crc32_check_sum(data, len-1);
data[len-1] = crc32;
}

8
bsp/boards/bsp_crc32.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef BSP_CRC32_H
#define BSP_CRC32_H
#include "struct_typedef.h"
extern uint32_t get_crc32_check_sum(uint32_t *data, uint32_t len);
extern bool_t verify_crc32_check_sum(uint32_t *data, uint32_t len);
extern void append_crc32_check_sum(uint32_t *data, uint32_t len);
#endif

76
bsp/boards/bsp_delay.c Normal file
View File

@@ -0,0 +1,76 @@
#include "bsp_delay.h"
#include "main.h"
static uint8_t fac_us = 0;
static uint32_t fac_ms = 0;
void delay_init(void)
{
fac_us = SystemCoreClock / 1000000;
fac_ms = SystemCoreClock / 1000;
}
void delay_us(uint16_t nus)
{
uint32_t ticks = 0;
uint32_t told = 0;
uint32_t tnow = 0;
uint32_t tcnt = 0;
uint32_t reload = 0;
reload = SysTick->LOAD;
ticks = nus * fac_us;
told = SysTick->VAL;
while (1)
{
tnow = SysTick->VAL;
if (tnow != told)
{
if (tnow < told)
{
tcnt += told - tnow;
}
else
{
tcnt += reload - tnow + told;
}
told = tnow;
if (tcnt >= ticks)
{
break;
}
}
}
}
void delay_ms(uint16_t nms)
{
uint32_t ticks = 0;
uint32_t told = 0;
uint32_t tnow = 0;
uint32_t tcnt = 0;
uint32_t reload = 0;
reload = SysTick->LOAD;
ticks = nms * fac_ms;
told = SysTick->VAL;
while (1)
{
tnow = SysTick->VAL;
if (tnow != told)
{
if (tnow < told)
{
tcnt += told - tnow;
}
else
{
tcnt += reload - tnow + told;
}
told = tnow;
if (tcnt >= ticks)
{
break;
}
}
}
}

9
bsp/boards/bsp_delay.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef BSP_DELAY_H
#define BSP_DELAY_H
#include "struct_typedef.h"
extern void delay_init(void);
extern void delay_us(uint16_t nus);
extern void delay_ms(uint16_t nms);
#endif

298
bsp/boards/bsp_flash.c Normal file
View File

@@ -0,0 +1,298 @@
#include "bsp_flash.h"
#include "main.h"
#include "string.h"
/**
* @brief get the sector number of flash
* @param[in] address: flash address
* @retval sector number
*/
/**
* @brief 获取flash的sector号
* @param[in] address: flash 地址
* @retval sector号
*/
static uint32_t ger_sector(uint32_t address);
/**
* @brief erase flash
* @param[in] address: flash address
* @param[in] len: page num
* @retval none
*/
/**
* @brief 擦除flash
* @param[in] address: flash 地址
* @param[in] len: 页数量
* @retval none
*/
void flash_erase_address(uint32_t address, uint16_t len)
{
FLASH_EraseInitTypeDef flash_erase;
uint32_t error;
flash_erase.Sector = ger_sector(address);
flash_erase.TypeErase = FLASH_TYPEERASE_SECTORS;
flash_erase.VoltageRange = FLASH_VOLTAGE_RANGE_3;
flash_erase.NbSectors = len;
HAL_FLASH_Unlock();
HAL_FLASHEx_Erase(&flash_erase, &error);
HAL_FLASH_Lock();
}
/**
* @brief write data to one page of flash
* @param[in] start_address: flash address
* @param[in] buf: data point
* @param[in] len: data num
* @retval success 0, fail -1
*/
/**
* @brief 往一页flash写数据
* @param[in] start_address: flash 地址
* @param[in] buf: 数据指针
* @param[in] len: 数据长度
* @retval success 0, fail -1
*/
int8_t flash_write_single_address(uint32_t start_address, uint32_t *buf, uint32_t len)
{
static uint32_t uw_address;
static uint32_t end_address;
static uint32_t *data_buf;
static uint32_t data_len;
HAL_FLASH_Unlock();
uw_address = start_address;
end_address = get_next_flash_address(start_address);
data_buf = buf;
data_len = 0;
while (uw_address <= end_address)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,uw_address, *data_buf) == HAL_OK)
{
uw_address += 4;
data_buf++;
data_len++;
if (data_len == len)
{
break;
}
}
else
{
HAL_FLASH_Lock();
return -1;
}
}
HAL_FLASH_Lock();
return 0;
}
/**
* @brief write data to some pages of flash
* @param[in] start_address: flash start address
* @param[in] end_address: flash end address
* @param[in] buf: data point
* @param[in] len: data num
* @retval success 0, fail -1
*/
/**
* @brief 往几页flash写数据
* @param[in] start_address: flash 开始地址
* @param[in] end_address: flash 结束地址
* @param[in] buf: 数据指针
* @param[in] len: 数据长度
* @retval success 0, fail -1
*/
int8_t flash_write_muli_address(uint32_t start_address, uint32_t end_address, uint32_t *buf, uint32_t len)
{
uint32_t uw_address = 0;
uint32_t *data_buf;
uint32_t data_len;
HAL_FLASH_Unlock();
uw_address = start_address;
data_buf = buf;
data_len = 0;
while (uw_address <= end_address)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,uw_address, *data_buf) == HAL_OK)
{
uw_address += 4;
data_buf++;
data_len++;
if (data_len == len)
{
break;
}
}
else
{
HAL_FLASH_Lock();
return -1;
}
}
HAL_FLASH_Lock();
return 0;
}
/**
* @brief read data for flash
* @param[in] address: flash address
* @param[out] buf: data point
* @param[in] len: data num
* @retval none
*/
/**
* @brief 从flash读数据
* @param[in] start_address: flash 地址
* @param[out] buf: 数据指针
* @param[in] len: 数据长度
* @retval none
*/
void flash_read(uint32_t address, uint32_t *buf, uint32_t len)
{
memcpy(buf, (void*)address, len *4);
}
/**
* @brief get the sector number of flash
* @param[in] address: flash address
* @retval sector number
*/
/**
* @brief 获取flash的sector号
* @param[in] address: flash 地址
* @retval sector号
*/
uint32_t ger_sector(uint32_t address)
{
uint32_t sector = 0;
if ((address < ADDR_FLASH_SECTOR_1) && (address >= ADDR_FLASH_SECTOR_0))
{
sector = FLASH_SECTOR_0;
}
else if ((address < ADDR_FLASH_SECTOR_2) && (address >= ADDR_FLASH_SECTOR_1))
{
sector = FLASH_SECTOR_1;
}
else if ((address < ADDR_FLASH_SECTOR_3) && (address >= ADDR_FLASH_SECTOR_2))
{
sector = FLASH_SECTOR_2;
}
else if ((address < ADDR_FLASH_SECTOR_4) && (address >= ADDR_FLASH_SECTOR_3))
{
sector = FLASH_SECTOR_3;
}
else if ((address < ADDR_FLASH_SECTOR_5) && (address >= ADDR_FLASH_SECTOR_4))
{
sector = FLASH_SECTOR_4;
}
else if ((address < ADDR_FLASH_SECTOR_6) && (address >= ADDR_FLASH_SECTOR_5))
{
sector = FLASH_SECTOR_5;
}
else if ((address < ADDR_FLASH_SECTOR_7) && (address >= ADDR_FLASH_SECTOR_6))
{
sector = FLASH_SECTOR_6;
}
else if ((address < ADDR_FLASH_SECTOR_8) && (address >= ADDR_FLASH_SECTOR_7))
{
sector = FLASH_SECTOR_7;
}
else if ((address < ADDR_FLASH_SECTOR_9) && (address >= ADDR_FLASH_SECTOR_8))
{
sector = FLASH_SECTOR_8;
}
else if ((address < ADDR_FLASH_SECTOR_10) && (address >= ADDR_FLASH_SECTOR_9))
{
sector = FLASH_SECTOR_9;
}
else if ((address < ADDR_FLASH_SECTOR_11) && (address >= ADDR_FLASH_SECTOR_10))
{
sector = FLASH_SECTOR_10;
}
else if ((address < ADDR_FLASH_SECTOR_12) && (address >= ADDR_FLASH_SECTOR_11))
{
sector = FLASH_SECTOR_11;
}
else
{
sector = FLASH_SECTOR_11;
}
return sector;
}
/**
* @brief get the next page flash address
* @param[in] address: flash address
* @retval next page flash address
*/
/**
* @brief 获取下一页flash地址
* @param[in] address: flash 地址
* @retval 下一页flash地址
*/
uint32_t get_next_flash_address(uint32_t address)
{
uint32_t sector = 0;
if ((address < ADDR_FLASH_SECTOR_1) && (address >= ADDR_FLASH_SECTOR_0))
{
sector = ADDR_FLASH_SECTOR_1;
}
else if ((address < ADDR_FLASH_SECTOR_2) && (address >= ADDR_FLASH_SECTOR_1))
{
sector = ADDR_FLASH_SECTOR_2;
}
else if ((address < ADDR_FLASH_SECTOR_3) && (address >= ADDR_FLASH_SECTOR_2))
{
sector = ADDR_FLASH_SECTOR_3;
}
else if ((address < ADDR_FLASH_SECTOR_4) && (address >= ADDR_FLASH_SECTOR_3))
{
sector = ADDR_FLASH_SECTOR_4;
}
else if ((address < ADDR_FLASH_SECTOR_5) && (address >= ADDR_FLASH_SECTOR_4))
{
sector = ADDR_FLASH_SECTOR_5;
}
else if ((address < ADDR_FLASH_SECTOR_6) && (address >= ADDR_FLASH_SECTOR_5))
{
sector = ADDR_FLASH_SECTOR_6;
}
else if ((address < ADDR_FLASH_SECTOR_7) && (address >= ADDR_FLASH_SECTOR_6))
{
sector = ADDR_FLASH_SECTOR_7;
}
else if ((address < ADDR_FLASH_SECTOR_8) && (address >= ADDR_FLASH_SECTOR_7))
{
sector = ADDR_FLASH_SECTOR_8;
}
else if ((address < ADDR_FLASH_SECTOR_9) && (address >= ADDR_FLASH_SECTOR_8))
{
sector = ADDR_FLASH_SECTOR_9;
}
else if ((address < ADDR_FLASH_SECTOR_10) && (address >= ADDR_FLASH_SECTOR_9))
{
sector = ADDR_FLASH_SECTOR_10;
}
else if ((address < ADDR_FLASH_SECTOR_11) && (address >= ADDR_FLASH_SECTOR_10))
{
sector = ADDR_FLASH_SECTOR_11;
}
else /*(address < FLASH_END_ADDR) && (address >= ADDR_FLASH_SECTOR_23))*/
{
sector = FLASH_END_ADDR;
}
return sector;
}

125
bsp/boards/bsp_flash.h Normal file
View File

@@ -0,0 +1,125 @@
#ifndef BSP_FLASH_H
#define BSP_FLASH_H
#include "struct_typedef.h"
/* Base address of the Flash sectors */
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base address of Sector 0, 16 Kbytes */
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base address of Sector 1, 16 Kbytes */
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base address of Sector 2, 16 Kbytes */
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base address of Sector 3, 16 Kbytes */
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base address of Sector 4, 64 Kbytes */
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base address of Sector 5, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base address of Sector 6, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base address of Sector 7, 128 Kbytes */
#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base address of Sector 8, 128 Kbytes */
#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base address of Sector 9, 128 Kbytes */
#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base address of Sector 10, 128 Kbytes */
#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base address of Sector 11, 128 Kbytes */
#define FLASH_END_ADDR ((uint32_t)0x08100000) /* Base address of Sector 23, 128 Kbytes */
#define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000) /* Base address of Sector 12, 16 Kbytes */
#define ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000) /* Base address of Sector 13, 16 Kbytes */
#define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000) /* Base address of Sector 14, 16 Kbytes */
#define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000) /* Base address of Sector 15, 16 Kbytes */
#define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000) /* Base address of Sector 16, 64 Kbytes */
#define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000) /* Base address of Sector 17, 128 Kbytes */
#define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000) /* Base address of Sector 18, 128 Kbytes */
#define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000) /* Base address of Sector 19, 128 Kbytes */
#define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000) /* Base address of Sector 20, 128 Kbytes */
#define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000) /* Base address of Sector 21, 128 Kbytes */
#define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000) /* Base address of Sector 22, 128 Kbytes */
#define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) /* Base address of Sector 23, 128 Kbytes */
/**
* @brief erase flash
* @param[in] address: flash address
* @param[in] len: page num
* @retval none
*/
/**
* @brief 擦除flash
* @param[in] address: flash 地址
* @param[in] len: 页数量
* @retval none
*/
extern void flash_erase_address(uint32_t address, uint16_t len);
/**
* @brief write data to one page of flash
* @param[in] start_address: flash address
* @param[in] buf: data point
* @param[in] len: data num
* @retval success 0, fail -1
*/
/**
* @brief 往一页flash写数据
* @param[in] start_address: flash 地址
* @param[in] buf: 数据指针
* @param[in] len: 数据长度
* @retval success 0, fail -1
*/
extern int8_t flash_write_single_address(uint32_t start_address, uint32_t *buf, uint32_t len);
/**
* @brief write data to some pages of flash
* @param[in] start_address: flash start address
* @param[in] end_address: flash end address
* @param[in] buf: data point
* @param[in] len: data num
* @retval success 0, fail -1
*/
/**
* @brief 往几页flash写数据
* @param[in] start_address: flash 开始地址
* @param[in] end_address: flash 结束地址
* @param[in] buf: 数据指针
* @param[in] len: 数据长度
* @retval success 0, fail -1
*/
extern int8_t flash_write_muli_address(uint32_t start_address, uint32_t end_address, uint32_t *buf, uint32_t len);
/**
* @brief read data for flash
* @param[in] address: flash address
* @param[out] buf: data point
* @param[in] len: data num
* @retval none
*/
/**
* @brief 从flash读数据
* @param[in] start_address: flash 地址
* @param[out] buf: 数据指针
* @param[in] len: 数据长度
* @retval none
*/
extern void flash_read(uint32_t address, uint32_t *buf, uint32_t len);
/**
* @brief get the sector number of flash
* @param[in] address: flash address
* @retval sector number
*/
/**
* @brief 获取flash的sector号
* @param[in] address: flash 地址
* @retval sector号
*/
extern uint32_t ger_sector(uint32_t address);
/**
* @brief get the next page flash address
* @param[in] address: flash address
* @retval next page flash address
*/
/**
* @brief 获取下一页flash地址
* @param[in] address: flash 地址
* @retval 下一页flash地址
*/
extern uint32_t get_next_flash_address(uint32_t address);
#endif

18
bsp/boards/bsp_fric.c Normal file
View File

@@ -0,0 +1,18 @@
#include "bsp_fric.h"
#include "main.h"
extern TIM_HandleTypeDef htim1;
void fric_off(void)
{
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, FRIC_OFF);
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2, FRIC_OFF);
}
void fric1_on(uint16_t cmd)
{
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, cmd);
}
void fric2_on(uint16_t cmd)
{
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2, cmd);
}

12
bsp/boards/bsp_fric.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef BSP_FRIC_H
#define BSP_FRIC_H
#include "struct_typedef.h"
#define FRIC_UP 1400
#define FRIC_DOWN 1320
#define FRIC_OFF 1000
extern void fric_off(void);
extern void fric1_on(uint16_t cmd);
extern void fric2_on(uint16_t cmd);
#endif

269
bsp/boards/bsp_i2c.c Normal file
View File

@@ -0,0 +1,269 @@
#include "bsp_i2c.h"
#include "main.h"
extern I2C_HandleTypeDef hi2c1;
extern I2C_HandleTypeDef hi2c2;
void bsp_I2C_master_transmit(I2C_TypeDef *I2C, uint16_t I2C_address, uint8_t *data, uint16_t len)
{
if(I2C == I2C1)
{
HAL_I2C_Master_Transmit(&hi2c1, I2C_address, data, len, 100);
}
else if(I2C == I2C2)
{
HAL_I2C_Master_Transmit(&hi2c2, I2C_address, data, len, 100);
}
}
void bsp_I2C_reset(I2C_TypeDef *I2C)
{
I2C_HandleTypeDef *hi2c;
if(I2C == I2C1)
{
hi2c = &hi2c1;
}
else if(I2C == I2C2)
{
hi2c = &hi2c2;
}
SET_BIT(hi2c->Instance->CR1, I2C_CR1_SWRST);
CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_SWRST);
if (HAL_I2C_Init(hi2c) != HAL_OK)
{
Error_Handler();
}
}
bool_t bsp_I2C_check_ack(I2C_TypeDef *I2C, uint16_t I2C_address)
{
I2C_HandleTypeDef *hi2c;
if(I2C == I2C1)
{
hi2c = &hi2c1;
}
else if(I2C == I2C2)
{
hi2c = &hi2c2;
}
if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) && ((hi2c->hdmatx != NULL && hi2c->hdmatx->Instance->NDTR != 0) || (hi2c->hdmarx != NULL && hi2c->hdmarx->Instance->NDTR != 0)))
{
return I2C_ACK;
}
else
{
uint16_t timeout = 0;
timeout = 0;
while(hi2c->Instance->SR2 & 0x02)
{
timeout ++;
if(timeout > 100)
{
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
return I2C_NO_ACK;
}
}
CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
timeout = 0;
while(!(hi2c->Instance->SR1 & 0x01))
{
timeout ++;
if(timeout > 100)
{
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
return I2C_NO_ACK;
}
}
hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(I2C_address);
timeout = 0;
while(!(hi2c->Instance->SR1 & 0x02))
{
timeout ++;
if(timeout > 500)
{
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
return I2C_NO_ACK;
}
}
do{
__IO uint32_t tmpreg = 0x00U;
tmpreg = hi2c->Instance->SR1;
tmpreg = hi2c->Instance->SR2;
UNUSED(tmpreg);
} while(0);
timeout = 0;
while(!(hi2c->Instance->SR1 & 0x80))
{
timeout ++;
if(timeout > 500)
{
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
return I2C_NO_ACK;
}
}
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
return I2C_ACK;
}
}
void DMA1_Stream7_IRQHandler(void)
{
if(DMA1->HISR & (1<<27))
{
__HAL_DMA_CLEAR_FLAG(hi2c2.hdmatx, DMA_HISR_TCIF7);
SET_BIT(hi2c2.Instance->CR1, I2C_CR1_STOP);
}
}
void I2C2_tx_DMA_init(void)
{
//disable DMA
//ʧЧDMA
__HAL_DMA_DISABLE(hi2c2.hdmatx);
while(hi2c2.hdmatx->Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(hi2c2.hdmatx);
}
hi2c2.hdmatx->Instance->PAR = (uint32_t)(&I2C2->DR);
__HAL_DMA_CLEAR_FLAG(hi2c2.hdmatx, DMA_HISR_TCIF7);
__HAL_DMA_ENABLE_IT(hi2c2.hdmatx, DMA_IT_TC);
}
void I2C2_tx_DMA_enable(uint32_t tx_buf, uint16_t ndtr)
{
//disable DMA
//ʧЧDMA
__HAL_DMA_DISABLE(hi2c2.hdmatx);
while(hi2c2.hdmatx->Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(hi2c2.hdmatx);
}
__HAL_DMA_CLEAR_FLAG(hi2c2.hdmatx, DMA_HISR_TCIF7);
hi2c2.hdmatx->Instance->M0AR = tx_buf;
__HAL_DMA_SET_COUNTER(hi2c2.hdmatx, ndtr);
__HAL_DMA_ENABLE(hi2c2.hdmatx);
}
HAL_StatusTypeDef I2C_TX_DMA_START(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
{
uint16_t timeout = 0;
if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
{
hi2c->Instance->CR1 |= I2C_CR1_PE;
}
timeout = 0;
while(hi2c->Instance->SR2 & 0x02)
{
timeout ++;
if(timeout > 100)
{
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
return HAL_BUSY;
}
}
CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
timeout = 0;
while(!(hi2c->Instance->SR1 & 0x01))
{
timeout ++;
if(timeout > 100)
{
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
return HAL_ERROR;
}
}
hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
timeout = 0;
while(!(hi2c->Instance->SR1 & 0x02))
{
timeout ++;
if(timeout > 500)
{
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
return HAL_ERROR;
}
}
do{
__IO uint32_t tmpreg = 0x00U;
tmpreg = hi2c->Instance->SR1;
tmpreg = hi2c->Instance->SR2;
UNUSED(tmpreg);
} while(0);
timeout = 0;
while(!(hi2c->Instance->SR1 & 0x80))
{
timeout ++;
if(timeout > 500)
{
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
return HAL_ERROR;
}
}
return HAL_OK;
}
void I2C2_DMA_transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size)
{
if( I2C_TX_DMA_START(&hi2c2, DevAddress) == HAL_OK)
{
I2C2_tx_DMA_enable((uint32_t)pData, Size);
}
}

19
bsp/boards/bsp_i2c.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef BSP_I2C_H
#define BSP_I2C_H
#include "struct_typedef.h"
#include "main.h"
#define I2C_ACK 1
#define I2C_NO_ACK 0
extern void bsp_I2C_reset(I2C_TypeDef *I2C);
extern void bsp_I2C_master_transmit(I2C_TypeDef *I2C, uint16_t I2C_address, uint8_t *data, uint16_t len);
extern bool_t bsp_I2C_check_ack(I2C_TypeDef *I2C, uint16_t I2C_address);
extern void I2C2_tx_DMA_init(void);
extern void I2C2_DMA_transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size);
#endif

8
bsp/boards/bsp_imu_pwm.c Normal file
View File

@@ -0,0 +1,8 @@
#include "bsp_imu_pwm.h"
#include "main.h"
extern TIM_HandleTypeDef htim10;
void imu_pwm_set(uint16_t pwm)
{
__HAL_TIM_SetCompare(&htim10, TIM_CHANNEL_1, pwm);
}

7
bsp/boards/bsp_imu_pwm.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef BSP_IMU_PWM_H
#define BSP_IMU_PWM_H
#include "struct_typedef.h"
extern void imu_pwm_set(uint16_t pwm);
#endif

12
bsp/boards/bsp_laser.c Normal file
View File

@@ -0,0 +1,12 @@
#include "bsp_laser.h"
#include "main.h"
extern TIM_HandleTypeDef htim3;
void laser_on(void)
{
__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_3, 8399);
}
void laser_off(void)
{
__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_3, 0);
}

8
bsp/boards/bsp_laser.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef BSP_LASER_H
#define BSP_LASER_H
#include "struct_typedef.h"
extern void laser_configuration(void);
extern void laser_on(void);
extern void laser_off(void);
#endif

30
bsp/boards/bsp_led.c Normal file
View File

@@ -0,0 +1,30 @@
#include "bsp_led.h"
#include "main.h"
extern TIM_HandleTypeDef htim5;
/**
* @brief aRGB show
* @param[in] aRGB: 0xaaRRGGBB, 'aa' is alpha, 'RR' is red, 'GG' is green, 'BB' is blue
* @retval none
*/
/**
* @brief <20><>ʾRGB
* @param[in] aRGB:0xaaRRGGBB,'aa' <20><>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>,'RR'<27>Ǻ<EFBFBD>ɫ,'GG'<27><><EFBFBD><EFBFBD>ɫ,'BB'<27><><EFBFBD><EFBFBD>ɫ
* @retval none
*/
void aRGB_led_show(uint32_t aRGB)
{
static uint8_t alpha;
static uint16_t red,green,blue;
alpha = (aRGB & 0xFF000000) >> 24;
red = ((aRGB & 0x00FF0000) >> 16) * alpha;
green = ((aRGB & 0x0000FF00) >> 8) * alpha;
blue = ((aRGB & 0x000000FF) >> 0) * alpha;
__HAL_TIM_SetCompare(&htim5, TIM_CHANNEL_1, blue);
__HAL_TIM_SetCompare(&htim5, TIM_CHANNEL_2, green);
__HAL_TIM_SetCompare(&htim5, TIM_CHANNEL_3, red);
}

18
bsp/boards/bsp_led.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef BSP_LED_H
#define BSP_LED_H
#include "struct_typedef.h"
/**
* @brief aRGB show
* @param[in] aRGB: 0xaaRRGGBB, 'aa' is alpha, 'RR' is red, 'GG' is green, 'BB' is blue
* @retval none
*/
/**
* @brief <20><>ʾRGB
* @param[in] aRGB:0xaaRRGGBB,'aa' <20><>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>,'RR'<27>Ǻ<EFBFBD>ɫ,'GG'<27><><EFBFBD><EFBFBD>ɫ,'BB'<27><><EFBFBD><EFBFBD>ɫ
* @retval none
*/
extern void aRGB_led_show(uint32_t aRGB);
#endif

42
bsp/boards/bsp_pwm.c Normal file
View File

@@ -0,0 +1,42 @@
#include "bsp_pwm.h"
#include "main.h"
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim8;
void servo_pwm_set(uint16_t pwm, uint8_t i)
{
switch(i)
{
case 0:
{
__HAL_TIM_SetCompare(&htim8, TIM_CHANNEL_1, pwm);
}break;
case 1:
{
__HAL_TIM_SetCompare(&htim8, TIM_CHANNEL_2, pwm);
}break;
}
}
void pump_pwm_set(uint16_t pwm, uint8_t i)
{
switch(i)
{
case 1:
{
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, pwm);
}break;
case 2:
{
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2, pwm);
}break;
case 3:
{
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_3, pwm);
}break;
case 4:
{
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_4, pwm);
}break;
}
}

8
bsp/boards/bsp_pwm.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef BSP_SERVO_PWM_H
#define BSP_SERVO_PWM_H
#include "struct_typedef.h"
extern void servo_pwm_set(uint16_t pwm, uint8_t i);
extern void pump_pwm_set(uint16_t pwm, uint8_t i);
#endif

67
bsp/boards/bsp_rc.c Normal file
View File

@@ -0,0 +1,67 @@
#include "bsp_rc.h"
#include "main.h"
extern UART_HandleTypeDef huart3;
extern DMA_HandleTypeDef hdma_usart3_rx;
void RC_Init(uint8_t *rx1_buf, uint8_t *rx2_buf, uint16_t dma_buf_num)
{
//enable the DMA transfer for the receiver request
//使能DMA串口接收
SET_BIT(huart3.Instance->CR3, USART_CR3_DMAR);
//enalbe idle interrupt
//使能空闲中断
__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
//disable DMA
//失效DMA
__HAL_DMA_DISABLE(&hdma_usart3_rx);
while(hdma_usart3_rx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_usart3_rx);
}
hdma_usart3_rx.Instance->PAR = (uint32_t) & (USART3->DR);
//memory buffer 1
//内存缓冲区1
hdma_usart3_rx.Instance->M0AR = (uint32_t)(rx1_buf);
//memory buffer 2
//内存缓冲区2
hdma_usart3_rx.Instance->M1AR = (uint32_t)(rx2_buf);
//data length
//数据长度
hdma_usart3_rx.Instance->NDTR = dma_buf_num;
//enable double memory buffer
//使能双缓冲区
SET_BIT(hdma_usart3_rx.Instance->CR, DMA_SxCR_DBM);
//enable DMA
//使能DMA
__HAL_DMA_ENABLE(&hdma_usart3_rx);
}
void RC_unable(void)
{
__HAL_UART_DISABLE(&huart3);
}
void RC_restart(uint16_t dma_buf_num)
{
__HAL_UART_DISABLE(&huart3);
__HAL_DMA_DISABLE(&hdma_usart3_rx);
hdma_usart3_rx.Instance->NDTR = dma_buf_num;
__HAL_DMA_ENABLE(&hdma_usart3_rx);
__HAL_UART_ENABLE(&huart3);
}

8
bsp/boards/bsp_rc.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef BSP_RC_H
#define BSP_RC_H
#include "struct_typedef.h"
extern void RC_Init(uint8_t *rx1_buf, uint8_t *rx2_buf, uint16_t dma_buf_num);
extern void RC_unable(void);
extern void RC_restart(uint16_t dma_buf_num);
#endif

21
bsp/boards/bsp_rng.c Normal file
View File

@@ -0,0 +1,21 @@
#include "bsp_rng.h"
#include "main.h"
extern RNG_HandleTypeDef hrng;
uint32_t RNG_get_random_num(void)
{
static uint32_t rng;
HAL_RNG_GenerateRandomNumber(&hrng, &rng);
return rng;
}
int32_t RNG_get_random_rangle(int min, int max)
{
static int32_t random;
random = (RNG_get_random_num() % (max - min + 1)) + min;
return random;
}

7
bsp/boards/bsp_rng.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef BSP_RNG_H
#define BSP_RNG_H
#include "struct_typedef.h"
extern uint32_t RNG_get_random_num(void);
extern int32_t RNG_get_random_rangle(int min, int max);
#endif

101
bsp/boards/bsp_spi.c Normal file
View File

@@ -0,0 +1,101 @@
#include "bsp_spi.h"
#include "main.h"
extern SPI_HandleTypeDef hspi1;
extern DMA_HandleTypeDef hdma_spi1_rx;
extern DMA_HandleTypeDef hdma_spi1_tx;
void SPI1_DMA_init(uint32_t tx_buf, uint32_t rx_buf, uint16_t num)
{
SET_BIT(hspi1.Instance->CR2, SPI_CR2_TXDMAEN);
SET_BIT(hspi1.Instance->CR2, SPI_CR2_RXDMAEN);
__HAL_SPI_ENABLE(&hspi1);
//disable DMA
//失效DMA
__HAL_DMA_DISABLE(&hdma_spi1_rx);
while(hdma_spi1_rx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_spi1_rx);
}
__HAL_DMA_CLEAR_FLAG(&hdma_spi1_rx, DMA_LISR_TCIF2);
hdma_spi1_rx.Instance->PAR = (uint32_t) & (SPI1->DR);
//memory buffer 1
//内存缓冲区1
hdma_spi1_rx.Instance->M0AR = (uint32_t)(rx_buf);
//data length
//数据长度
__HAL_DMA_SET_COUNTER(&hdma_spi1_rx, num);
__HAL_DMA_ENABLE_IT(&hdma_spi1_rx, DMA_IT_TC);
//disable DMA
//失效DMA
__HAL_DMA_DISABLE(&hdma_spi1_tx);
while(hdma_spi1_tx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_spi1_tx);
}
__HAL_DMA_CLEAR_FLAG(&hdma_spi1_tx, DMA_LISR_TCIF3);
hdma_spi1_tx.Instance->PAR = (uint32_t) & (SPI1->DR);
//memory buffer 1
//内存缓冲区1
hdma_spi1_tx.Instance->M0AR = (uint32_t)(tx_buf);
//data length
//数据长度
__HAL_DMA_SET_COUNTER(&hdma_spi1_tx, num);
}
void SPI1_DMA_enable(uint32_t tx_buf, uint32_t rx_buf, uint16_t ndtr)
{
__HAL_DMA_DISABLE(&hdma_spi1_rx);
__HAL_DMA_DISABLE(&hdma_spi1_tx);
while(hdma_spi1_rx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_spi1_rx);
}
while(hdma_spi1_tx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_spi1_tx);
}
__HAL_DMA_CLEAR_FLAG (hspi1.hdmarx, __HAL_DMA_GET_TC_FLAG_INDEX(hspi1.hdmarx));
__HAL_DMA_CLEAR_FLAG (hspi1.hdmarx, __HAL_DMA_GET_HT_FLAG_INDEX(hspi1.hdmarx));
__HAL_DMA_CLEAR_FLAG (hspi1.hdmarx, __HAL_DMA_GET_TE_FLAG_INDEX(hspi1.hdmarx));
__HAL_DMA_CLEAR_FLAG (hspi1.hdmarx, __HAL_DMA_GET_DME_FLAG_INDEX(hspi1.hdmarx));
__HAL_DMA_CLEAR_FLAG (hspi1.hdmarx, __HAL_DMA_GET_FE_FLAG_INDEX(hspi1.hdmarx));
__HAL_DMA_CLEAR_FLAG (hspi1.hdmatx, __HAL_DMA_GET_TC_FLAG_INDEX(hspi1.hdmatx));
__HAL_DMA_CLEAR_FLAG (hspi1.hdmatx, __HAL_DMA_GET_HT_FLAG_INDEX(hspi1.hdmatx));
__HAL_DMA_CLEAR_FLAG (hspi1.hdmatx, __HAL_DMA_GET_TE_FLAG_INDEX(hspi1.hdmatx));
__HAL_DMA_CLEAR_FLAG (hspi1.hdmatx, __HAL_DMA_GET_DME_FLAG_INDEX(hspi1.hdmatx));
__HAL_DMA_CLEAR_FLAG (hspi1.hdmatx, __HAL_DMA_GET_FE_FLAG_INDEX(hspi1.hdmatx));
hdma_spi1_rx.Instance->M0AR = rx_buf;
hdma_spi1_tx.Instance->M0AR = tx_buf;
__HAL_DMA_SET_COUNTER(&hdma_spi1_rx, ndtr);
__HAL_DMA_SET_COUNTER(&hdma_spi1_tx, ndtr);
__HAL_DMA_ENABLE(&hdma_spi1_rx);
__HAL_DMA_ENABLE(&hdma_spi1_tx);
}

11
bsp/boards/bsp_spi.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef BSP_SPI_H
#define BSP_SPI_H
#include "struct_typedef.h"
#include "stm32f4xx_hal.h"
extern SPI_HandleTypeDef hspi2;
extern void SPI1_DMA_init(uint32_t tx_buf, uint32_t rx_buf, uint16_t num);
extern void SPI1_DMA_enable(uint32_t tx_buf, uint32_t rx_buf, uint16_t ndtr);
#endif

28
bsp/boards/bsp_uart.c Normal file
View File

@@ -0,0 +1,28 @@
/**
* @brief uart板级支持包
* @author Penguin
*/
#include "bsp_uart.h"
/**
* @brief 使用uart发送数据
* @param huart
* @param pData
* @param Size
* @param Timeout
* @return
*/
UartSendState_e UartSendTxMessage(
UART_HandleTypeDef * huart, uint8_t * pData, uint16_t Size, uint32_t Timeout)
{
uint8_t cnt = 5; //最大重发次数
HAL_StatusTypeDef status = HAL_UART_Transmit(huart, pData, Size, Timeout);
while (cnt-- && status != HAL_OK) {
status = HAL_UART_Transmit(huart, pData, Size, Timeout);
}
if (status == HAL_OK) {
return UART_SEND_OK;
}
return UART_SEND_FAIL;
}

19
bsp/boards/bsp_uart.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef BSP_UART_H
#define BSP_UART_H
#include "stm32f4xx_hal.h"
#include "struct_typedef.h"
#include "usart.h"
typedef UART_HandleTypeDef huart_t;
#define UART1 huart6 //(3pin)与板上的标识相对应
#define UART2 huart1 //(4pin)与板上的标识相对应
typedef enum __UartSendState {
UART_SEND_FAIL = 0,
UART_SEND_OK,
} UartSendState_e;
extern UartSendState_e UartSendTxMessage(
UART_HandleTypeDef * huart, uint8_t * pData, uint16_t Size, uint32_t Timeout);
#endif // BSP_UART_H

193
bsp/boards/bsp_usart.c Normal file
View File

@@ -0,0 +1,193 @@
#include "bsp_usart.h"
#include "main.h"
extern UART_HandleTypeDef huart1;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern UART_HandleTypeDef huart6;
extern DMA_HandleTypeDef hdma_usart6_rx;
extern DMA_HandleTypeDef hdma_usart6_tx;
void usart1_init(uint8_t *rx1_buf, uint8_t *rx2_buf, uint16_t dma_buf_num)
{
//enable the DMA transfer for the receiver and tramsmit request
//使能DMA串口接收和发送
SET_BIT(huart1.Instance->CR3, USART_CR3_DMAR);
SET_BIT(huart1.Instance->CR3, USART_CR3_DMAT);
//enalbe idle interrupt
//使能空闲中断
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
//disable DMA
//失效DMA
__HAL_DMA_DISABLE(&hdma_usart1_rx);
while(hdma_usart1_rx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_usart1_rx);
}
__HAL_DMA_CLEAR_FLAG(&hdma_usart1_rx, DMA_LISR_TCIF1);
hdma_usart1_rx.Instance->PAR = (uint32_t) & (USART1->DR);
//memory buffer 1
//内存缓冲区1
hdma_usart1_rx.Instance->M0AR = (uint32_t)(rx1_buf);
//memory buffer 2
//内存缓冲区2
hdma_usart1_rx.Instance->M1AR = (uint32_t)(rx2_buf);
//data length
//数据长度
__HAL_DMA_SET_COUNTER(&hdma_usart1_rx, dma_buf_num);
//enable double memory buffer
//使能双缓冲区
SET_BIT(hdma_usart1_rx.Instance->CR, DMA_SxCR_DBM);
//enable DMA
//使能DMA
__HAL_DMA_ENABLE(&hdma_usart1_rx);
//disable DMA
//失效DMA
__HAL_DMA_DISABLE(&hdma_usart1_tx);
while(hdma_usart1_tx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_usart1_tx);
}
hdma_usart1_tx.Instance->PAR = (uint32_t) & (USART1->DR);
usart1_tx_dma_init();
}
void usart1_tx_dma_init(void)
{
//enable the DMA transfer for the receiver and tramsmit request
//使能DMA串口接收和发送
SET_BIT(huart1.Instance->CR3, USART_CR3_DMAR);
SET_BIT(huart1.Instance->CR3, USART_CR3_DMAT);
//disable DMA
//失效DMA
__HAL_DMA_DISABLE(&hdma_usart1_tx);
while(hdma_usart1_tx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_usart1_tx);
}
hdma_usart1_tx.Instance->PAR = (uint32_t) & (USART1->DR);
hdma_usart1_tx.Instance->M0AR = (uint32_t)(NULL);
hdma_usart1_tx.Instance->NDTR = 0;
}
void usart1_tx_dma_enable(uint8_t *data, uint16_t len)
{
//disable DMA
//失效DMA
__HAL_DMA_DISABLE(&hdma_usart1_tx);
while(hdma_usart1_tx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_usart1_tx);
}
__HAL_DMA_CLEAR_FLAG(&hdma_usart1_tx, DMA_HISR_TCIF7);
hdma_usart1_tx.Instance->M0AR = (uint32_t)(data);
__HAL_DMA_SET_COUNTER(&hdma_usart1_tx, len);
__HAL_DMA_ENABLE(&hdma_usart1_tx);
}
void usart6_init(uint8_t *rx1_buf, uint8_t *rx2_buf, uint16_t dma_buf_num)
{
//enable the DMA transfer for the receiver and tramsmit request
//使能DMA串口接收和发送
SET_BIT(huart6.Instance->CR3, USART_CR3_DMAR);
SET_BIT(huart6.Instance->CR3, USART_CR3_DMAT);
//enalbe idle interrupt
//使能空闲中断
__HAL_UART_ENABLE_IT(&huart6, UART_IT_IDLE);
//disable DMA
//失效DMA
__HAL_DMA_DISABLE(&hdma_usart6_rx);
while(hdma_usart6_rx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_usart6_rx);
}
__HAL_DMA_CLEAR_FLAG(&hdma_usart6_rx, DMA_LISR_TCIF1);
hdma_usart6_rx.Instance->PAR = (uint32_t) & (USART6->DR);
//memory buffer 1
//内存缓冲区1
hdma_usart6_rx.Instance->M0AR = (uint32_t)(rx1_buf);
//memory buffer 2
//内存缓冲区2
hdma_usart6_rx.Instance->M1AR = (uint32_t)(rx2_buf);
//data length
//数据长度
__HAL_DMA_SET_COUNTER(&hdma_usart6_rx, dma_buf_num);
//enable double memory buffer
//使能双缓冲区
SET_BIT(hdma_usart6_rx.Instance->CR, DMA_SxCR_DBM);
//enable DMA
//使能DMA
__HAL_DMA_ENABLE(&hdma_usart6_rx);
//disable DMA
//失效DMA
__HAL_DMA_DISABLE(&hdma_usart6_tx);
while(hdma_usart6_tx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_usart6_tx);
}
hdma_usart6_tx.Instance->PAR = (uint32_t) & (USART6->DR);
}
void usart6_tx_dma_enable(uint8_t *data, uint16_t len)
{
//disable DMA
//失效DMA
__HAL_DMA_DISABLE(&hdma_usart6_tx);
while(hdma_usart6_tx.Instance->CR & DMA_SxCR_EN)
{
__HAL_DMA_DISABLE(&hdma_usart6_tx);
}
__HAL_DMA_CLEAR_FLAG(&hdma_usart6_tx, DMA_HISR_TCIF6);
hdma_usart6_tx.Instance->M0AR = (uint32_t)(data);
__HAL_DMA_SET_COUNTER(&hdma_usart6_tx, len);
__HAL_DMA_ENABLE(&hdma_usart6_tx);
}

11
bsp/boards/bsp_usart.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef BSP_USART_H
#define BSP_USART_H
#include "struct_typedef.h"
extern void usart6_init(uint8_t *rx1_buf, uint8_t *rx2_buf, uint16_t dma_buf_num);
extern void usart1_init(uint8_t *rx1_buf, uint8_t *rx2_buf, uint16_t dma_buf_num);
extern void usart1_tx_dma_init(void);
extern void usart1_tx_dma_enable(uint8_t *data, uint16_t len);
#endif