mirror of
https://github.com/goldenfishs/MRobot.git
synced 2026-04-01 05:17:13 +08:00
疯狂写bsp
This commit is contained in:
BIN
assets/User_code/bsp/.DS_Store
vendored
BIN
assets/User_code/bsp/.DS_Store
vendored
Binary file not shown.
@@ -1,19 +1,106 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "bsp\can.h"
|
||||
#include "bsp/can.h"
|
||||
#include "bsp/bsp.h"
|
||||
#include <can.h>
|
||||
#include <cmsis_os.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
typedef struct CAN_QueueNode {
|
||||
uint32_t can_id;
|
||||
osMessageQueueId_t queue;
|
||||
uint8_t queue_size;
|
||||
struct CAN_QueueNode *next;
|
||||
} CAN_QueueNode_t;
|
||||
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
static CAN_QueueNode_t *can_queue_list = NULL;
|
||||
static void (*CAN_Callback[BSP_CAN_NUM][BSP_CAN_CB_NUM])(void);
|
||||
|
||||
/* Private function prototypes ---------------------------------------------- */
|
||||
static BSP_CAN_t CAN_Get(CAN_HandleTypeDef *hcan);
|
||||
static osMessageQueueId_t find_queue(uint32_t can_id);
|
||||
static void can_rx_fifo0_handler(void);
|
||||
static void can_rx_fifo1_handler(void);
|
||||
|
||||
/* AUTO GENERATED CAN_RX_CALLBACKS */
|
||||
/* Private function -------------------------------------------------------- */
|
||||
|
||||
/* 查找指定CAN ID的队列 */
|
||||
static osMessageQueueId_t find_queue(uint32_t can_id) {
|
||||
CAN_QueueNode_t *node = can_queue_list;
|
||||
while (node) {
|
||||
if (node->can_id == can_id) return node->queue;
|
||||
node = node->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* FIFO0接收处理函数 */
|
||||
static void can_rx_fifo0_handler(void) {
|
||||
CAN_RxHeaderTypeDef rx_header;
|
||||
uint8_t rx_data[8];
|
||||
|
||||
// 遍历所有CAN接口处理FIFO0
|
||||
for (int can_idx = 0; can_idx < BSP_CAN_NUM; can_idx++) {
|
||||
CAN_HandleTypeDef *hcan = BSP_CAN_GetHandle((BSP_CAN_t)can_idx);
|
||||
if (hcan == NULL) continue;
|
||||
|
||||
while (HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO0) > 0) {
|
||||
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, rx_data) == HAL_OK) {
|
||||
uint32_t can_id = (rx_header.IDE == CAN_ID_STD) ? rx_header.StdId : rx_header.ExtId;
|
||||
osMessageQueueId_t queue = find_queue(can_id);
|
||||
if (queue) {
|
||||
CAN_Message_t msg;
|
||||
msg.header = rx_header;
|
||||
memcpy(msg.data, rx_data, 8);
|
||||
// 非阻塞发送,如果队列满了就丢弃
|
||||
osMessageQueuePut(queue, &msg, 0, 0);
|
||||
}
|
||||
// 如果没有找到对应的队列,消息被直接丢弃
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* FIFO1接收处理函数 */
|
||||
static void can_rx_fifo1_handler(void) {
|
||||
CAN_RxHeaderTypeDef rx_header;
|
||||
uint8_t rx_data[8];
|
||||
|
||||
// 遍历所有CAN接口处理FIFO1
|
||||
for (int can_idx = 0; can_idx < BSP_CAN_NUM; can_idx++) {
|
||||
CAN_HandleTypeDef *hcan = BSP_CAN_GetHandle((BSP_CAN_t)can_idx);
|
||||
if (hcan == NULL) continue;
|
||||
|
||||
while (HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO1) > 0) {
|
||||
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO1, &rx_header, rx_data) == HAL_OK) {
|
||||
uint32_t can_id = (rx_header.IDE == CAN_ID_STD) ? rx_header.StdId : rx_header.ExtId;
|
||||
osMessageQueueId_t queue = find_queue(can_id);
|
||||
if (queue) {
|
||||
CAN_Message_t msg;
|
||||
msg.header = rx_header;
|
||||
memcpy(msg.data, rx_data, 8);
|
||||
// 非阻塞发送,如果队列满了就丢弃
|
||||
osMessageQueuePut(queue, &msg, 0, 0);
|
||||
}
|
||||
// 如果没有找到对应的队列,消息被直接丢弃
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static BSP_CAN_t CAN_Get(CAN_HandleTypeDef *hcan) {
|
||||
/* AUTO GENERATED CAN_GET */
|
||||
else
|
||||
return BSP_CAN_ERR;
|
||||
}
|
||||
|
||||
/* HAL Callback Functions --------------------------------------------------- */
|
||||
void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan) {
|
||||
BSP_CAN_t bsp_can = CAN_Get(hcan);
|
||||
if (bsp_can != BSP_CAN_ERR) {
|
||||
@@ -130,6 +217,132 @@ CAN_HandleTypeDef *BSP_CAN_GetHandle(BSP_CAN_t can) {
|
||||
int8_t BSP_CAN_RegisterCallback(BSP_CAN_t can, BSP_CAN_Callback_t type,
|
||||
void (*callback)(void)) {
|
||||
if (callback == NULL) return BSP_ERR_NULL;
|
||||
if (can >= BSP_CAN_NUM) return BSP_ERR_INITED;
|
||||
if (type >= BSP_CAN_CB_NUM) return BSP_ERR;
|
||||
|
||||
CAN_Callback[can][type] = callback;
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
int BSP_CAN_GetMessage(uint32_t can_id, CAN_Message_t *msg, uint32_t timeout) {
|
||||
if (msg == NULL) return BSP_ERR_NULL;
|
||||
osMessageQueueId_t queue = find_queue(can_id);
|
||||
if (!queue) return BSP_ERR_NO_DEV; // 没有该队列
|
||||
if (osMessageQueueGet(queue, msg, NULL, timeout) == osOK) {
|
||||
return BSP_OK; // 成功
|
||||
}
|
||||
return BSP_ERR; // 超时或队列为空
|
||||
}
|
||||
|
||||
int8_t BSP_CAN_CreateIdQueue(uint32_t can_id, uint8_t queue_size) {
|
||||
// 检查是否已存在
|
||||
CAN_QueueNode_t *node = can_queue_list;
|
||||
while (node) {
|
||||
if (node->can_id == can_id) {
|
||||
return BSP_ERR; // 已存在
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
// 创建新节点
|
||||
CAN_QueueNode_t *new_node = malloc(sizeof(CAN_QueueNode_t));
|
||||
if (new_node == NULL) return BSP_ERR_NULL;
|
||||
new_node->can_id = can_id;
|
||||
new_node->queue_size = queue_size;
|
||||
new_node->queue = osMessageQueueNew(queue_size, sizeof(CAN_Message_t), NULL);
|
||||
if (new_node->queue == NULL) {
|
||||
free(new_node);
|
||||
return BSP_ERR;
|
||||
}
|
||||
new_node->next = can_queue_list;
|
||||
can_queue_list = new_node;
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
|
||||
int8_t BSP_CAN_DeleteIdQueue(uint32_t can_id) {
|
||||
CAN_QueueNode_t **current = &can_queue_list;
|
||||
while (*current) {
|
||||
if ((*current)->can_id == can_id) {
|
||||
CAN_QueueNode_t *to_delete = *current;
|
||||
*current = (*current)->next;
|
||||
osMessageQueueDelete(to_delete->queue);
|
||||
free(to_delete);
|
||||
return BSP_OK;
|
||||
}
|
||||
current = &(*current)->next;
|
||||
}
|
||||
return BSP_ERR; // 未找到
|
||||
}
|
||||
|
||||
|
||||
void BSP_CAN_RegisterId(uint32_t can_id, uint8_t queue_size) {
|
||||
BSP_CAN_CreateIdQueue(can_id, queue_size);
|
||||
}
|
||||
|
||||
|
||||
int8_t BSP_CAN_Transmit(BSP_CAN_t can, BSP_CAN_Format_t format,
|
||||
uint32_t id, uint8_t *data, uint8_t dlc) {
|
||||
if (can >= BSP_CAN_NUM) return BSP_ERR;
|
||||
if (data == NULL) return BSP_ERR_NULL;
|
||||
if (dlc > 8) return BSP_ERR;
|
||||
|
||||
CAN_HandleTypeDef *hcan = BSP_CAN_GetHandle(can);
|
||||
if (hcan == NULL) return BSP_ERR_NULL;
|
||||
|
||||
CAN_TxHeaderTypeDef header;
|
||||
uint32_t mailbox;
|
||||
|
||||
switch (format) {
|
||||
case CAN_FORMAT_STD_DATA:
|
||||
header.StdId = id;
|
||||
header.IDE = CAN_ID_STD;
|
||||
header.RTR = CAN_RTR_DATA;
|
||||
header.TransmitGlobalTime = DISABLE;
|
||||
header.DLC = dlc;
|
||||
break;
|
||||
case CAN_FORMAT_EXT_DATA:
|
||||
header.ExtId = id;
|
||||
header.IDE = CAN_ID_EXT;
|
||||
header.RTR = CAN_RTR_DATA;
|
||||
header.TransmitGlobalTime = DISABLE;
|
||||
header.DLC = dlc;
|
||||
break;
|
||||
case CAN_FORMAT_STD_REMOTE:
|
||||
header.StdId = id;
|
||||
header.IDE = CAN_ID_STD;
|
||||
header.RTR = CAN_RTR_REMOTE;
|
||||
header.TransmitGlobalTime = DISABLE;
|
||||
header.DLC = dlc;
|
||||
break;
|
||||
case CAN_FORMAT_EXT_REMOTE:
|
||||
header.ExtId = id;
|
||||
header.IDE = CAN_ID_EXT;
|
||||
header.RTR = CAN_RTR_REMOTE;
|
||||
header.TransmitGlobalTime = DISABLE;
|
||||
header.DLC = dlc;
|
||||
break;
|
||||
default:
|
||||
return BSP_ERR;
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef res = HAL_CAN_AddTxMessage(hcan, &header, data, &mailbox);
|
||||
|
||||
if (res == HAL_OK) {
|
||||
return BSP_OK;
|
||||
} else {
|
||||
return BSP_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
int8_t BSP_CAN_Init(void) {
|
||||
/* AUTO GENERATED CAN_INIT */
|
||||
|
||||
// 注册接收中断处理函数
|
||||
for (int can_idx = 0; can_idx < BSP_CAN_NUM; can_idx++) {
|
||||
BSP_CAN_RegisterCallback((BSP_CAN_t)can_idx, HAL_CAN_RX_FIFO0_MSG_PENDING_CB, can_rx_fifo0_handler);
|
||||
BSP_CAN_RegisterCallback((BSP_CAN_t)can_idx, HAL_CAN_RX_FIFO1_MSG_PENDING_CB, can_rx_fifo1_handler);
|
||||
}
|
||||
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,14 @@ extern "C" {
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <can.h>
|
||||
|
||||
#include "bsp/bsp.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <cmsis_os.h>
|
||||
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
|
||||
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
typedef enum {
|
||||
@@ -35,11 +39,85 @@ typedef enum {
|
||||
BSP_CAN_CB_NUM
|
||||
} BSP_CAN_Callback_t;
|
||||
|
||||
typedef enum {
|
||||
CAN_FORMAT_STD_DATA, /* 标准数据帧 */
|
||||
CAN_FORMAT_EXT_DATA, /* 扩展数据帧 */
|
||||
CAN_FORMAT_STD_REMOTE, /* 标准远程帧 */
|
||||
CAN_FORMAT_EXT_REMOTE, /* 扩展远程帧 */
|
||||
} BSP_CAN_Format_t;
|
||||
|
||||
typedef struct {
|
||||
CAN_RxHeaderTypeDef header;
|
||||
uint8_t data[8];
|
||||
} CAN_Message_t;
|
||||
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
/**
|
||||
* @brief 获取 CAN 句柄
|
||||
* @param can CAN 枚举类型
|
||||
* @return CAN_HandleTypeDef 指针,失败返回 NULL
|
||||
*/
|
||||
CAN_HandleTypeDef *BSP_CAN_GetHandle(BSP_CAN_t can);
|
||||
|
||||
/**
|
||||
* @brief 注册 CAN 回调
|
||||
* @param can CAN 枚举类型
|
||||
* @param type 回调类型
|
||||
* @param callback 回调函数指针
|
||||
* @return BSP_OK 成功,BSP_ERR_NULL/BSP_ERR_INVALID_PARAM 失败
|
||||
*/
|
||||
int8_t BSP_CAN_RegisterCallback(BSP_CAN_t can, BSP_CAN_Callback_t type,
|
||||
void (*callback)(void));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* @brief 发送 CAN 消息
|
||||
* @param can CAN 枚举类型
|
||||
* @param format 消息格式
|
||||
* @param id CAN ID
|
||||
* @param data 数据指针
|
||||
* @param dlc 数据长度
|
||||
* @return BSP_OK 成功,BSP_ERR/BSP_ERR_NULL/BSP_ERR_INVALID_PARAM 失败
|
||||
*/
|
||||
int8_t BSP_CAN_Transmit(BSP_CAN_t can, BSP_CAN_Format_t format,
|
||||
uint32_t id, uint8_t *data, uint8_t dlc);
|
||||
|
||||
/**
|
||||
* @brief 初始化 CAN
|
||||
* @return BSP_OK 成功,BSP_ERR/BSP_ERR_NULL/BSP_ERR_INITED 失败
|
||||
*/
|
||||
int8_t BSP_CAN_Init(void);
|
||||
|
||||
/**
|
||||
* @brief 获取 CAN 消息
|
||||
* @param can_id CAN ID
|
||||
* @param msg 存储消息的结构体指针
|
||||
* @param timeout 超时时间(毫秒)
|
||||
* @return BSP_OK 成功,BSP_ERR/BSP_ERR_NULL/BSP_ERR_NO_DEV 失败
|
||||
*/
|
||||
int BSP_CAN_GetMessage(uint32_t can_id, CAN_Message_t *msg, uint32_t timeout);
|
||||
|
||||
/**
|
||||
* @brief 注册 CAN ID
|
||||
* @param can_id CAN ID
|
||||
* @param queue_size 队列大小
|
||||
* @return 无
|
||||
*/
|
||||
void BSP_CAN_RegisterId(uint32_t can_id, uint8_t queue_size);
|
||||
|
||||
/**
|
||||
* @brief 创建指定 CAN ID 的队列
|
||||
* @param can_id CAN ID
|
||||
* @param queue_size 队列大小
|
||||
* @return BSP_OK 成功,BSP_ERR/BSP_ERR_NULL/BSP_ERR_INVALID_PARAM 失败
|
||||
*/
|
||||
int8_t BSP_CAN_CreateIdQueue(uint32_t can_id, uint8_t queue_size);
|
||||
|
||||
/**
|
||||
* @brief 删除指定 CAN ID 的队列
|
||||
* @param can_id CAN ID
|
||||
* @return BSP_OK 成功,BSP_ERR/BSP_ERR_INVALID_PARAM 失败
|
||||
*/
|
||||
int8_t BSP_CAN_DeleteIdQueue(uint32_t can_id);
|
||||
|
||||
/* USER CAN FUNCTIONS BEGIN */
|
||||
/* USER CAN FUNCTIONS END */
|
||||
@@ -3,4 +3,5 @@ can,要求开启can的中断要求开启can的中断要求开启can的中断要
|
||||
gpio,要求设置label开启中断
|
||||
spi,要求开启spi中断
|
||||
i2c,要求开始spi中断
|
||||
mm,这是套了一层的内存
|
||||
mm,这是套了一层的内存
|
||||
time,需要使用开启freerots
|
||||
|
@@ -7,7 +7,16 @@
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
typedef struct {
|
||||
uint16_t pin;
|
||||
GPIO_TypeDef *gpio;
|
||||
} BSP_GPIO_MAP_t;
|
||||
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
static const BSP_GPIO_MAP_t GPIO_Map[BSP_GPIO_NUM] = {
|
||||
/* AUTO GENERATED BSP_GPIO_MAP */
|
||||
};
|
||||
|
||||
static void (*GPIO_Callback[16])(void);
|
||||
|
||||
/* Private function -------------------------------------------------------- */
|
||||
@@ -36,7 +45,7 @@ int8_t BSP_GPIO_RegisterCallback(uint16_t pin, void (*callback)(void)) {
|
||||
|
||||
int8_t BSP_GPIO_EnableIRQ(uint16_t pin) {
|
||||
switch (pin) {
|
||||
/* AUTO GENERATED BSP_GPIO_ENABLE_IRQ */
|
||||
/* AUTO GENERATED BSP_GPIO_ENABLE_IRQ */
|
||||
default:
|
||||
return BSP_ERR;
|
||||
}
|
||||
@@ -45,9 +54,26 @@ int8_t BSP_GPIO_EnableIRQ(uint16_t pin) {
|
||||
|
||||
int8_t BSP_GPIO_DisableIRQ(uint16_t pin) {
|
||||
switch (pin) {
|
||||
/* AUTO GENERATED BSP_GPIO_DISABLE_IRQ */
|
||||
/* AUTO GENERATED BSP_GPIO_DISABLE_IRQ */
|
||||
default:
|
||||
return BSP_ERR;
|
||||
}
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
int8_t BSP_GPIO_WritePin(BSP_GPIO_t gpio, bool value){
|
||||
if (gpio >= BSP_GPIO_NUM) return BSP_ERR;
|
||||
HAL_GPIO_WritePin(GPIO_Map[gpio].gpio, GPIO_Map[gpio].pin, value);
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
int8_t BSP_GPIO_TogglePin(BSP_GPIO_t gpio){
|
||||
if (gpio >= BSP_GPIO_NUM) return BSP_ERR;
|
||||
HAL_GPIO_TogglePin(GPIO_Map[gpio].gpio, GPIO_Map[gpio].pin);
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
bool BSP_GPIO_ReadPin(BSP_GPIO_t gpio){
|
||||
if (gpio >= BSP_GPIO_NUM) return false;
|
||||
return HAL_GPIO_ReadPin(GPIO_Map[gpio].gpio, GPIO_Map[gpio].pin) == GPIO_PIN_SET;
|
||||
}
|
||||
@@ -6,18 +6,30 @@ extern "C" {
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "bsp/bsp.h"
|
||||
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
typedef enum {
|
||||
/* AUTO GENERATED BSP_GPIO_ENUM */
|
||||
BSP_GPIO_NUM,
|
||||
BSP_GPIO_ERR,
|
||||
} BSP_GPIO_t;
|
||||
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
int8_t BSP_GPIO_RegisterCallback(uint16_t pin, void (*callback)(void));
|
||||
|
||||
int8_t BSP_GPIO_EnableIRQ(uint16_t pin);
|
||||
int8_t BSP_GPIO_DisableIRQ(uint16_t pin);
|
||||
|
||||
int8_t BSP_GPIO_WritePin(BSP_GPIO_t gpio, bool value);
|
||||
int8_t BSP_GPIO_TogglePin(BSP_GPIO_t gpio);
|
||||
|
||||
bool BSP_GPIO_ReadPin(BSP_GPIO_t gpio);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -101,3 +101,76 @@ int8_t BSP_I2C_RegisterCallback(BSP_I2C_t i2c, BSP_I2C_Callback_t type,
|
||||
I2C_Callback[i2c][type] = callback;
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
int8_t BSP_I2C_Transmit(BSP_I2C_t i2c, uint16_t devAddr, uint8_t *data,
|
||||
uint16_t size, bool dma) {
|
||||
if (i2c >= BSP_I2C_NUM) return BSP_ERR;
|
||||
I2C_HandleTypeDef *hi2c = BSP_I2C_GetHandle(i2c);
|
||||
if (hi2c == NULL) return BSP_ERR;
|
||||
|
||||
if (dma) {
|
||||
return HAL_I2C_Master_Transmit_DMA(hi2c, devAddr, data, size);
|
||||
} else {
|
||||
return HAL_I2C_Master_Transmit_IT(hi2c, devAddr, data, size);
|
||||
}
|
||||
}
|
||||
|
||||
int8_t BSP_I2C_Receive(BSP_I2C_t i2c, uint16_t devAddr, uint8_t *data,
|
||||
uint16_t size, bool dma) {
|
||||
if (i2c >= BSP_I2C_NUM) return BSP_ERR;
|
||||
I2C_HandleTypeDef *hi2c = BSP_I2C_GetHandle(i2c);
|
||||
if (hi2c == NULL) return BSP_ERR;
|
||||
|
||||
if (dma) {
|
||||
return HAL_I2C_Master_Receive_DMA(hi2c, devAddr, data, size);
|
||||
} else {
|
||||
return HAL_I2C_Master_Receive_IT(hi2c, devAddr, data, size);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t BSP_I2C_MemReadByte(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr) {
|
||||
if (i2c >= BSP_I2C_NUM) return 0xFF;
|
||||
I2C_HandleTypeDef *hi2c = BSP_I2C_GetHandle(i2c);
|
||||
if (hi2c == NULL) return 0xFF;
|
||||
|
||||
uint8_t data;
|
||||
HAL_I2C_Mem_Read(hi2c, devAddr, memAddr, I2C_MEMADD_SIZE_16BIT, &data, 1, HAL_MAX_DELAY);
|
||||
return data;
|
||||
}
|
||||
|
||||
int8_t BSP_I2C_MemWriteByte(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr,
|
||||
uint8_t data) {
|
||||
if (i2c >= BSP_I2C_NUM) return BSP_ERR;
|
||||
I2C_HandleTypeDef *hi2c = BSP_I2C_GetHandle(i2c);
|
||||
if (hi2c == NULL) return BSP_ERR;
|
||||
|
||||
return HAL_I2C_Mem_Write(hi2c, devAddr, memAddr, I2C_MEMADD_SIZE_16BIT, &data, 1, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
int8_t BSP_I2C_MemRead(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr,
|
||||
uint8_t *data, uint16_t size, bool dma) {
|
||||
if (i2c >= BSP_I2C_NUM || data == NULL || size == 0) return BSP_ERR;
|
||||
I2C_HandleTypeDef *hi2c = BSP_I2C_GetHandle(i2c);
|
||||
if (hi2c == NULL) return BSP_ERR;
|
||||
|
||||
if (dma) {
|
||||
return HAL_I2C_Mem_Read_DMA(hi2c, devAddr, memAddr, I2C_MEMADD_SIZE_16BIT, data, size);
|
||||
}
|
||||
else {
|
||||
return HAL_I2C_Mem_Read(hi2c, devAddr, memAddr, I2C_MEMADD_SIZE_16BIT, data, size, HAL_MAX_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int8_t BSP_I2C_MemWrite(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr,
|
||||
uint8_t *data, uint16_t size, bool dma) {
|
||||
if (i2c >= BSP_I2C_NUM || data == NULL || size == 0) return BSP_ERR;
|
||||
I2C_HandleTypeDef *hi2c = BSP_I2C_GetHandle(i2c);
|
||||
if (hi2c == NULL) return BSP_ERR;
|
||||
|
||||
if (dma) {
|
||||
return HAL_I2C_Mem_Write_DMA(hi2c, devAddr, memAddr, I2C_MEMADD_SIZE_16BIT, data, size);
|
||||
} else {
|
||||
return HAL_I2C_Mem_Write(hi2c, devAddr, memAddr, I2C_MEMADD_SIZE_16BIT, data, size, HAL_MAX_DELAY);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ extern "C" {
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <i2c.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "bsp/bsp.h"
|
||||
|
||||
@@ -44,6 +46,22 @@ I2C_HandleTypeDef *BSP_I2C_GetHandle(BSP_I2C_t i2c);
|
||||
int8_t BSP_I2C_RegisterCallback(BSP_I2C_t i2c, BSP_I2C_Callback_t type,
|
||||
void (*callback)(void));
|
||||
|
||||
int8_t BSP_I2C_Transmit(BSP_I2C_t i2c, uint16_t devAddr, uint8_t *data,
|
||||
uint16_t size, bool dma);
|
||||
int8_t BSP_I2C_Receive(BSP_I2C_t i2c, uint16_t devAddr, uint8_t *data,
|
||||
uint16_t size, bool dma);
|
||||
|
||||
|
||||
uint8_t BSP_I2C_MemReadByte(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr);
|
||||
int8_t BSP_I2C_MemWriteByte(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr,
|
||||
uint8_t data);
|
||||
|
||||
int8_t BSP_I2C_MemRead(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr,
|
||||
uint8_t *data, uint16_t size, bool dma);
|
||||
int8_t BSP_I2C_MemWrite(BSP_I2C_t i2c, uint16_t devAddr, uint16_t memAddr,
|
||||
uint8_t *data, uint16_t size, bool dma);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
70
assets/User_code/bsp/pwm.c
Normal file
70
assets/User_code/bsp/pwm.c
Normal file
@@ -0,0 +1,70 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "bsp/pwm.h"
|
||||
#include "bsp.h"
|
||||
|
||||
#include <tim.h>
|
||||
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
typedef struct {
|
||||
TIM_HandleTypeDef *tim;
|
||||
uint16_t channel;
|
||||
} BSP_PWM_Config_t;
|
||||
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
static const BSP_PWM_Config_t PWM_Map[BSP_PWM_NUM] = {
|
||||
/* AUTO GENERATED BSP_PWM_MAP */
|
||||
};
|
||||
|
||||
/* Private function -------------------------------------------------------- */
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
|
||||
int8_t BSP_PWM_Start(BSP_PWM_Channel_t ch) {
|
||||
if (ch >= BSP_PWM_NUM) return BSP_ERR;
|
||||
|
||||
HAL_TIM_PWM_Start(PWM_Map[ch].tim, PWM_Map[ch].channel);
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
int8_t BSP_PWM_SetComp(BSP_PWM_Channel_t ch, float duty_cycle) {
|
||||
if (ch >= BSP_PWM_NUM) return BSP_ERR;
|
||||
|
||||
if (duty_cycle > 1.0f) {
|
||||
duty_cycle = 1.0f;
|
||||
}
|
||||
if (duty_cycle < 0.0f) {
|
||||
duty_cycle = 0.0f;
|
||||
}
|
||||
|
||||
// 获取ARR值(周期值)
|
||||
uint32_t arr = __HAL_TIM_GET_AUTORELOAD(PWM_Map[ch].tim);
|
||||
|
||||
// 计算比较值:CCR = duty_cycle * (ARR + 1)
|
||||
uint32_t ccr = (uint32_t)(duty_cycle * (arr + 1));
|
||||
|
||||
__HAL_TIM_SET_COMPARE(PWM_Map[ch].tim, PWM_Map[ch].channel, ccr);
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
int8_t BSP_PWM_SetFreq(BSP_PWM_Channel_t ch, float freq) {
|
||||
if (ch >= BSP_PWM_NUM) return BSP_ERR;
|
||||
|
||||
uint32_t timer_clock = HAL_RCC_GetPCLK1Freq(); // Get the timer clock frequency
|
||||
uint32_t prescaler = PWM_Map[ch].tim->Init.Prescaler;
|
||||
uint32_t period = (timer_clock / (prescaler + 1)) / freq - 1;
|
||||
|
||||
if (period > UINT16_MAX) {
|
||||
return BSP_ERR; // Frequency too low
|
||||
}
|
||||
__HAL_TIM_SET_AUTORELOAD(PWM_Map[ch].tim, period);
|
||||
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
int8_t BSP_PWM_Stop(BSP_PWM_Channel_t ch) {
|
||||
if (ch >= BSP_PWM_NUM) return BSP_ERR;
|
||||
|
||||
HAL_TIM_PWM_Stop(PWM_Map[ch].tim, PWM_Map[ch].channel);
|
||||
return BSP_OK;
|
||||
}
|
||||
30
assets/User_code/bsp/pwm.h
Normal file
30
assets/User_code/bsp/pwm.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bsp.h"
|
||||
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
/* PWM通道 */
|
||||
typedef enum {
|
||||
/* AUTO GENERATED BSP_PWM_ENUM */
|
||||
BSP_PWM_NUM,
|
||||
BSP_PWM_ERR,
|
||||
} BSP_PWM_Channel_t;
|
||||
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
int8_t BSP_PWM_Start(BSP_PWM_Channel_t ch);
|
||||
int8_t BSP_PWM_SetComp(BSP_PWM_Channel_t ch, float duty_cycle);
|
||||
int8_t BSP_PWM_SetFreq(BSP_PWM_Channel_t ch, float freq);
|
||||
int8_t BSP_PWM_Stop(BSP_PWM_Channel_t ch);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -94,3 +94,74 @@ int8_t BSP_SPI_RegisterCallback(BSP_SPI_t spi, BSP_SPI_Callback_t type,
|
||||
SPI_Callback[spi][type] = callback;
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
int8_t BSP_SPI_Transmit(BSP_SPI_t spi, uint8_t *data, uint16_t size, bool dma) {
|
||||
if (spi >= BSP_SPI_NUM) return BSP_ERR;
|
||||
SPI_HandleTypeDef *hspi = BSP_SPI_GetHandle(spi);
|
||||
if (hspi == NULL) return BSP_ERR;
|
||||
|
||||
if (dma) {
|
||||
return HAL_SPI_Transmit_DMA(hspi, data, size);
|
||||
} else {
|
||||
return HAL_SPI_Transmit_IT(hspi, data, size);
|
||||
}
|
||||
}
|
||||
|
||||
int8_t BSP_SPI_Receive(BSP_SPI_t spi, uint8_t *data, uint16_t size, bool dma) {
|
||||
if (spi >= BSP_SPI_NUM) return BSP_ERR;
|
||||
SPI_HandleTypeDef *hspi = BSP_SPI_GetHandle(spi);
|
||||
if (hspi == NULL) return BSP_ERR;
|
||||
|
||||
if (dma) {
|
||||
return HAL_SPI_Receive_DMA(hspi, data, size);
|
||||
} else {
|
||||
return HAL_SPI_Receive_IT(hspi, data, size);
|
||||
}
|
||||
}
|
||||
|
||||
int8_t BSP_SPI_TransmitReceive(BSP_SPI_t spi, uint8_t *txData, uint8_t *rxData,
|
||||
uint16_t size, bool dma) {
|
||||
if (spi >= BSP_SPI_NUM) return BSP_ERR;
|
||||
SPI_HandleTypeDef *hspi = BSP_SPI_GetHandle(spi);
|
||||
if (hspi == NULL) return BSP_ERR;
|
||||
|
||||
if (dma) {
|
||||
return HAL_SPI_TransmitReceive_DMA(hspi, txData, rxData, size);
|
||||
} else {
|
||||
return HAL_SPI_TransmitReceive_IT(hspi, txData, rxData, size);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t BSP_SPI_MemReadByte(BSP_SPI_t spi, uint8_t reg) {
|
||||
if (spi >= BSP_SPI_NUM) return 0xFF;
|
||||
SPI_HandleTypeDef *hspi = BSP_SPI_GetHandle(spi);
|
||||
if (hspi == NULL) return 0xFF;
|
||||
|
||||
uint8_t data = 0;
|
||||
HAL_SPI_Mem_Read(hspi, reg, &data, sizeof(data));
|
||||
return data;
|
||||
}
|
||||
|
||||
int8_t BSP_SPI_MemWriteByte(BSP_SPI_t spi, uint8_t reg, uint8_t data) {
|
||||
if (spi >= BSP_SPI_NUM) return BSP_ERR;
|
||||
SPI_HandleTypeDef *hspi = BSP_SPI_GetHandle(spi);
|
||||
if (hspi == NULL) return BSP_ERR;
|
||||
|
||||
return HAL_SPI_Mem_Write(hspi, reg, &data, sizeof(data));
|
||||
}
|
||||
|
||||
int8_t BSP_SPI_MemRead(BSP_SPI_t spi, uint8_t reg, uint8_t *data, uint16_t size) {
|
||||
if (spi >= BSP_SPI_NUM) return BSP_ERR;
|
||||
SPI_HandleTypeDef *hspi = BSP_SPI_GetHandle(spi);
|
||||
if (hspi == NULL || data == NULL || size == 0) return BSP_ERR_NULL;
|
||||
|
||||
return HAL_SPI_Mem_Read(hspi, reg, data, size);
|
||||
}
|
||||
|
||||
int8_t BSP_SPI_MemWrite(BSP_SPI_t spi, uint8_t reg, uint8_t *data, uint16_t size) {
|
||||
if (spi >= BSP_SPI_NUM) return BSP_ERR;
|
||||
SPI_HandleTypeDef *hspi = BSP_SPI_GetHandle(spi);
|
||||
if (hspi == NULL || data == NULL || size == 0) return BSP_ERR_NULL;
|
||||
|
||||
return HAL_SPI_Mem_Write(hspi, reg, data, size);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ extern "C" {
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <spi.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bsp/bsp.h"
|
||||
|
||||
@@ -40,6 +41,17 @@ SPI_HandleTypeDef *BSP_SPI_GetHandle(BSP_SPI_t spi);
|
||||
int8_t BSP_SPI_RegisterCallback(BSP_SPI_t spi, BSP_SPI_Callback_t type,
|
||||
void (*callback)(void));
|
||||
|
||||
|
||||
int8_t BSP_SPI_Transmit(BSP_SPI_t spi, uint8_t *data, uint16_t size, bool dma);
|
||||
int8_t BSP_SPI_Receive(BSP_SPI_t spi, uint8_t *data, uint16_t size, bool dma);
|
||||
int8_t BSP_SPI_TransmitReceive(BSP_SPI_t spi, uint8_t *txData, uint8_t *rxData,
|
||||
uint16_t size, bool dma);
|
||||
|
||||
uint8_t BSP_SPI_MemReadByte(BSP_SPI_t spi, uint8_t reg);
|
||||
int8_t BSP_SPI_MemWriteByte(BSP_SPI_t spi, uint8_t reg, uint8_t data);
|
||||
int8_t BSP_SPI_MemRead(BSP_SPI_t spi, uint8_t reg, uint8_t *data, uint16_t size);
|
||||
int8_t BSP_SPI_MemWrite(BSP_SPI_t spi, uint8_t reg, uint8_t *data, uint16_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,16 +1,35 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "bsp/delay.h"
|
||||
#include "bsp.h"
|
||||
|
||||
#include <cmsis_os2.h>
|
||||
#include <main.h>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "main.h"
|
||||
#include "task.h"
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
/* Private function -------------------------------------------------------- */
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
int8_t BSP_Delay(uint32_t ms) {
|
||||
|
||||
uint32_t BSP_TIME_Get_ms() { return xTaskGetTickCount(); }
|
||||
|
||||
uint64_t BSP_TIME_Get_us() {
|
||||
uint32_t ms_old = xTaskGetTickCount();
|
||||
uint32_t tick_value_old = SysTick->VAL;
|
||||
uint32_t ms_new = xTaskGetTickCount();
|
||||
uint32_t tick_value_new = SysTick->VAL;
|
||||
if (ms_old == ms_new) {
|
||||
return ms_new * 1000 + 1000 - tick_value_old * 1000 / (SysTick->LOAD + 1);
|
||||
} else {
|
||||
return ms_new * 1000 + 1000 - tick_value_new * 1000 / (SysTick->LOAD + 1);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t BSP_TIME_Get() __attribute__((alias("BSP_TIME_Get_us")));
|
||||
|
||||
int8_t BSP_TIME_Delay_ms(uint32_t ms) {
|
||||
uint32_t tick_period = 1000u / osKernelGetTickFreq();
|
||||
uint32_t ticks = ms / tick_period;
|
||||
|
||||
@@ -32,3 +51,14 @@ int8_t BSP_Delay(uint32_t ms) {
|
||||
}
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
/*阻塞us延迟*/
|
||||
int8_t BSP_TIME_Delay_us(uint32_t us) {
|
||||
uint64_t start = BSP_TIME_Get_us();
|
||||
while (BSP_TIME_Get_us() - start < us) {
|
||||
// 等待us时间
|
||||
}
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
int8_t BSP_TIME_Delay(uint32_t ms) __attribute__((alias("BSP_TIME_Delay_ms")));
|
||||
@@ -13,7 +13,18 @@ extern "C" {
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
int8_t BSP_Delay(uint32_t ms);
|
||||
uint32_t BSP_TIME_Get_ms();
|
||||
|
||||
uint64_t BSP_TIME_Get_us();
|
||||
|
||||
uint64_t BSP_TIME_Get();
|
||||
|
||||
int8_t BSP_TIME_Delay_ms(uint32_t ms);
|
||||
|
||||
/*微秒阻塞延时,一般别用*/
|
||||
int8_t BSP_TIME_Delay_us(uint32_t us);
|
||||
|
||||
int8_t BSP_TIME_Delay(uint32_t ms);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "bsp\uart.h"
|
||||
#include <uart.h>
|
||||
|
||||
#include "bsp/uart.h"
|
||||
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
@@ -107,6 +109,29 @@ UART_HandleTypeDef *BSP_UART_GetHandle(BSP_UART_t uart) {
|
||||
int8_t BSP_UART_RegisterCallback(BSP_UART_t uart, BSP_UART_Callback_t type,
|
||||
void (*callback)(void)) {
|
||||
if (callback == NULL) return BSP_ERR_NULL;
|
||||
if (uart >= BSP_UART_NUM || type >= BSP_UART_CB_NUM) return BSP_ERR;
|
||||
UART_Callback[uart][type] = callback;
|
||||
return BSP_OK;
|
||||
}
|
||||
|
||||
int8_t BSP_UART_Transmit(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma) {
|
||||
if (uart >= BSP_UART_NUM) return BSP_ERR;
|
||||
if (data == NULL || size == 0) return BSP_ERR_NULL;
|
||||
|
||||
if (dma) {
|
||||
return HAL_UART_Transmit_DMA(BSP_UART_GetHandle(uart), data, size);
|
||||
} else {
|
||||
return HAL_UART_Transmit_IT(BSP_UART_GetHandle(uart), data, size);
|
||||
}
|
||||
}
|
||||
|
||||
int8_t BSP_UART_Receive(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma) {
|
||||
if (uart >= BSP_UART_NUM) return BSP_ERR;
|
||||
if (data == NULL || size == 0) return BSP_ERR_NULL;
|
||||
|
||||
if (dma) {
|
||||
return HAL_UART_Receive_DMA(BSP_UART_GetHandle(uart), data, size);
|
||||
} else {
|
||||
return HAL_UART_Receive_IT(BSP_UART_GetHandle(uart), data, size);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ extern "C" {
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <usart.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bsp/bsp.h"
|
||||
|
||||
@@ -38,9 +39,13 @@ typedef enum {
|
||||
} BSP_UART_Callback_t;
|
||||
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
|
||||
UART_HandleTypeDef *BSP_UART_GetHandle(BSP_UART_t uart);
|
||||
int8_t BSP_UART_RegisterCallback(BSP_UART_t uart, BSP_UART_Callback_t type,
|
||||
void (*callback)(void));
|
||||
|
||||
int8_t BSP_UART_Transmit(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma);
|
||||
int8_t BSP_UART_Receive(BSP_UART_t uart, uint8_t *data, uint16_t size, bool dma);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user