可用的文件,全部

This commit is contained in:
2026-03-21 01:49:41 +08:00
commit da4f00f652
56 changed files with 9299 additions and 0 deletions

148
Core/Bsp/README.md Normal file
View File

@@ -0,0 +1,148 @@
# BSP Drivers
本目录包含外设驱动模块包括继电器驱动、传感器驱动和UART通信。
---
## 1. 继电器驱动 (Relay)
控制 LIGHT 和 FAN 两个继电器。
### 硬件连接
| 继电器 | GPIO端口 | 引脚 |
|--------|----------|------|
| LIGHT | GPIOB | PB15 |
| FAN | GPIOB | PB14 |
> 注意:两个继电器均为**低电平有效**(低电平触发开启)
### API 接口
| 函数 | 描述 |
|------|------|
| `BSP_Relay_Init()` | 初始化所有继电器,默认为关闭状态 |
| `BSP_LIGHT_On()` | 打开 LIGHT 继电器 |
| `BSP_LIGHT_Off()` | 关闭 LIGHT 继电器 |
| `BSP_LIGHT_Toggle()` | 切换 LIGHT 继电器状态 |
| `BSP_FAN_On()` | 打开 FAN 继电器 |
| `BSP_FAN_Off()` | 关闭 FAN 继电器 |
| `BSP_FAN_Toggle()` | 切换 FAN 继电器状态 |
| `BSP_FAN_SetMode()` | 设置风扇模式(自动/手动) |
| `BSP_FAN_SetThreshold()` | 设置自动模式温度阈值 |
| `BSP_FAN_AutoControl()` | 根据温度自动控制风扇 |
---
## 2. BMP280 气压/温度传感器
使用 I2C1 接口 (PB6-SCL, PB7-SDA)I2C地址: 0x76 (SDO接地)
### 配置说明
- I2C接口: I2C1
- I2C地址: 0x76
- 引脚: PB6 (SCL), PB7 (SDA)
---
## 3. UART1 通信驱动 (MQTT透传)
使用 DMA + IDLE空闲中断 实现高效接收用于MQTT模块透明传输。
### 硬件连接
| 功能 | GPIO端口 | 引脚 |
|------|----------|------|
| TX | GPIOA | PA9 |
| RX | GPIOA | PA10 |
### DMA配置
| DMA通道 | 方向 | 说明 |
|---------|------|------|
| DMA1_Channel4 | TX | USART1发送 |
| DMA1_Channel5 | RX | USART1接收 |
### 工作原理
1. DMA持续接收数据到缓冲区
2. 检测到IDLE空闲线一帧结束时触发中断
3. 中断只保存数据长度并重启DMA
4. 主循环中调用 `BSP_UART1_Process()` 解析JSON命令并执行
5. 执行结果以JSON格式通过UART1返回
### 文件结构
```
Core/Bsp/
├── bsp_uart.h / .c # UART1 DMA驱动
├── bsp_cmd.h / .c # JSON命令解析与执行
├── cJSON.h / .c # cJSON库精简版
├── bsp_relay.h / .c # 继电器驱动
├── bsp_flash.h / .c # Flash存储驱动
├── bsp_bmp280.h / .c # BMP280 BSP
├── driver_bmp280.h / .c # LibDriver BMP280
└── driver_bmp280_interface.c / .h # HAL接口
```
---
## 4. JSON 命令协议
通信采用 JSON 格式,所有命令通过 UART1 接收,响应也通过 UART1 返回。
### 请求格式
```json
{"cmd":"xxx","value":"xxx"}
```
### 支持的命令
| 命令 | 请求示例 | 说明 |
|------|---------|------|
| 查询状态 | `{"cmd":"status"}` | 返回所有状态信息 |
| 风扇模式 | `{"cmd":"fan_mode","value":"auto"}` | 设置风扇为自动/手动 |
| 风扇阈值 | `{"cmd":"fan_threshold","value":30.0}` | 设置温度阈值 |
| 风扇开关 | `{"cmd":"fan","value":"on"}` | 手动开/关风扇 |
| 灯光开关 | `{"cmd":"light","value":"on"}` | 开/关灯 |
| 恢复默认 | `{"cmd":"reset"}` | 恢复出厂设置 |
### 响应格式
**简单响应:**
```json
{"status":"ok","msg":"fan mode set to auto"}
{"status":"error","msg":"unknown command"}
```
**状态查询响应:**
```json
{"status":"ok","data":{"fan_mode":"auto","fan_threshold":30.0,"fan":"off","light":"off"}}
```
---
## 5. Flash存储 (bsp_flash)
将设置参数存储到内部Flash实现断电保存。
- 存储地址: `0x0801F800` (最后一页)
---
## 初始化
驱动在 `MX_FREERTOS_Init()` 和任务启动时初始化:
```c
// MX_FREERTOS_Init() 中
BSP_Relay_Init(); // 继电器初始化
BSP_BMP280_Init(); // BMP280初始化
// StartDefaultTask() 中
BSP_UART1_Init(); // UART1 DMA初始化
```
任务循环中调用 `BSP_UART1_Process()` 处理命令。

96
Core/Bsp/bsp_bmp280.c Normal file
View File

@@ -0,0 +1,96 @@
/**
* @file bsp_bmp280.c
* @brief BMP280 sensor driver implementation (BSP layer)
*/
#include "bsp_bmp280.h"
#include "driver_bmp280_interface.h"
/* Private variables ----------------------------------------------------------*/
static bmp280_handle_t gs_bmp280_handle;
static uint8_t gs_bmp280_inited = 0;
/**
* @brief Initialize BMP280 sensor
* @retval 0: success
* other: error code
*/
uint8_t BSP_BMP280_Init(void)
{
uint8_t res;
/* Initialize handle */
DRIVER_BMP280_LINK_INIT(&gs_bmp280_handle, bmp280_handle_t);
/* Link interface functions */
DRIVER_BMP280_LINK_IIC_INIT(&gs_bmp280_handle, bmp280_interface_iic_init);
DRIVER_BMP280_LINK_IIC_DEINIT(&gs_bmp280_handle, bmp280_interface_iic_deinit);
DRIVER_BMP280_LINK_IIC_READ(&gs_bmp280_handle, bmp280_interface_iic_read);
DRIVER_BMP280_LINK_IIC_WRITE(&gs_bmp280_handle, bmp280_interface_iic_write);
DRIVER_BMP280_LINK_SPI_INIT(&gs_bmp280_handle, bmp280_interface_spi_init);
DRIVER_BMP280_LINK_SPI_DEINIT(&gs_bmp280_handle, bmp280_interface_spi_deinit);
DRIVER_BMP280_LINK_SPI_READ(&gs_bmp280_handle, bmp280_interface_spi_read);
DRIVER_BMP280_LINK_SPI_WRITE(&gs_bmp280_handle, bmp280_interface_spi_write);
DRIVER_BMP280_LINK_DELAY_MS(&gs_bmp280_handle, bmp280_interface_delay_ms);
DRIVER_BMP280_LINK_DEBUG_PRINT(&gs_bmp280_handle, bmp280_interface_debug_print);
/* Set I2C interface */
bmp280_set_interface(&gs_bmp280_handle, BMP280_INTERFACE_IIC);
/* Set I2C address (SDO pin grounded: 0x76) */
bmp280_set_addr_pin(&gs_bmp280_handle, BMP280_ADDRESS_ADO_LOW);
/* Initialize BMP280 */
res = bmp280_init(&gs_bmp280_handle);
if (res != 0)
{
gs_bmp280_inited = 0;
return res;
}
/* Configure sensor */
bmp280_set_temperatue_oversampling(&gs_bmp280_handle, BMP280_OVERSAMPLING_x2);
bmp280_set_pressure_oversampling(&gs_bmp280_handle, BMP280_OVERSAMPLING_x2);
bmp280_set_filter(&gs_bmp280_handle, BMP280_FILTER_COEFF_16);
bmp280_set_standby_time(&gs_bmp280_handle, BMP280_STANDBY_TIME_500_MS);
bmp280_set_mode(&gs_bmp280_handle, BMP280_MODE_NORMAL);
gs_bmp280_inited = 1;
return 0;
}
/**
* @brief Read temperature and pressure data
* @param data: pointer to store sensor data
* @retval 0: success
* other: error code
*/
uint8_t BSP_BMP280_Read(bsp_bmp280_data_t *data)
{
uint8_t res;
uint32_t temp_raw;
uint32_t press_raw;
if (gs_bmp280_inited == 0)
{
return 1;
}
if (data == NULL)
{
return 2;
}
res = bmp280_read_temperature_pressure(&gs_bmp280_handle,
&temp_raw, &data->temperature,
&press_raw, &data->pressure);
if (res != 0)
{
data->is_ready = 0;
return res;
}
data->is_ready = 1;
return 0;
}

32
Core/Bsp/bsp_bmp280.h Normal file
View File

@@ -0,0 +1,32 @@
/**
* @file bsp_bmp280.h
* @brief BMP280 sensor driver (BSP layer)
* @details I2C1 interface, wrapper for LibDriver BMP280
*/
#ifndef __BSP_BMP280_H
#define __BSP_BMP280_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "driver_bmp280.h"
/* Exported types ------------------------------------------------------------*/
typedef struct {
float temperature; /* Temperature in Celsius */
float pressure; /* Pressure in Pa */
uint8_t is_ready; /* Sensor ready flag */
} bsp_bmp280_data_t;
/* Exported functions prototypes --------------------------------------------*/
uint8_t BSP_BMP280_Init(void);
uint8_t BSP_BMP280_Read(bsp_bmp280_data_t *data);
#ifdef __cplusplus
}
#endif
#endif /* __BSP_BMP280_H */

255
Core/Bsp/bsp_cmd.c Normal file
View File

@@ -0,0 +1,255 @@
/**
* @file bsp_cmd.c
* @brief JSON command parser - receives JSON, executes command, returns JSON via UART1
*/
#include "bsp_cmd.h"
#include "bsp_relay.h"
#include "bsp_uart.h"
#include "bsp_flash.h"
#include "cJSON.h"
#include <string.h>
#include <stdio.h>
/**
* @brief Send JSON response via UART1
*/
static void send_response(cJSON *root)
{
char *json_str = cJSON_PrintUnformatted(root);
if (json_str != NULL)
{
BSP_UART1_SendString(json_str);
BSP_UART1_SendString("\r\n");
cJSON_free(json_str);
}
cJSON_Delete(root);
}
/**
* @brief Make a simple {"status":"ok","msg":"xxx"} response
*/
static void respond_ok(const char *msg)
{
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "status", "ok");
cJSON_AddStringToObject(root, "msg", msg);
send_response(root);
}
/**
* @brief Make a {"status":"error","msg":"xxx"} response
*/
static void respond_error(const char *msg)
{
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "status", "error");
cJSON_AddStringToObject(root, "msg", msg);
send_response(root);
}
/**
* @brief Process JSON command
*
* Supported commands:
* {"cmd":"status"}
* {"cmd":"fan_mode","value":"auto"} or "manual"
* {"cmd":"fan_threshold","value":30.0}
* {"cmd":"fan","value":"on"} or "off"
* {"cmd":"light","value":"on"} or "off"
* {"cmd":"reset"}
*/
void BSP_Cmd_ProcessJSON(const uint8_t *buf, uint16_t len)
{
cJSON *root = NULL;
cJSON *cmd_item = NULL;
cJSON *val_item = NULL;
if (buf == NULL || len == 0)
{
respond_error("empty input");
return;
}
/* Ensure null termination and strip non-JSON prefix/suffix */
char tmp[256];
if (len >= sizeof(tmp))
len = sizeof(tmp) - 1;
memcpy(tmp, buf, len);
tmp[len] = '\0';
/* Find the first '{' to skip any MQTT topic prefix */
char *json_start = strchr(tmp, '{');
if (json_start == NULL)
{
respond_error("no json found");
return;
}
/* Find matching '}' to trim trailing garbage */
char *json_end = json_start;
int depth = 0;
while (*json_end != '\0')
{
if (*json_end == '{') depth++;
else if (*json_end == '}') depth--;
if (depth == 0) { json_end++; break; }
json_end++;
}
*json_end = '\0';
/* Parse JSON */
root = cJSON_Parse(json_start);
if (root == NULL)
{
respond_error("invalid json");
return;
}
/* Get "cmd" field */
cmd_item = cJSON_GetObjectItemCaseSensitive(root, "cmd");
if (cmd_item == NULL || !cJSON_IsString(cmd_item))
{
respond_error("missing cmd field");
cJSON_Delete(root);
return;
}
const char *cmd = cmd_item->valuestring;
/* Get "value" field (optional) */
val_item = cJSON_GetObjectItemCaseSensitive(root, "value");
/* --- STATUS --- */
if (strcasecmp(cmd, "status") == 0)
{
system_state_t state;
BSP_System_GetState(&state);
cJSON *resp = cJSON_CreateObject();
cJSON_AddStringToObject(resp, "status", "ok");
cJSON *data = cJSON_CreateObject();
cJSON_AddStringToObject(data, "fan_mode", state.fan_mode == FAN_MODE_AUTO ? "auto" : "manual");
cJSON_AddNumberToObject(data, "fan_threshold", state.fan_threshold);
cJSON_AddStringToObject(data, "fan", state.fan_state ? "on" : "off");
cJSON_AddStringToObject(data, "light", state.light_state ? "on" : "off");
cJSON_AddItemToObject(resp, "data", data);
send_response(resp);
}
/* --- FAN MODE --- */
else if (strcasecmp(cmd, "fan_mode") == 0)
{
if (val_item == NULL || !cJSON_IsString(val_item))
{
respond_error("missing value");
}
else
{
const char *val = val_item->valuestring;
if (strcasecmp(val, "auto") == 0)
{
BSP_FAN_SetMode(FAN_MODE_AUTO);
respond_ok("fan mode set to auto");
}
else if (strcasecmp(val, "manual") == 0)
{
BSP_FAN_SetMode(FAN_MODE_MANUAL);
respond_ok("fan mode set to manual");
}
else
{
respond_error("invalid fan_mode value");
}
}
}
/* --- FAN THRESHOLD --- */
else if (strcasecmp(cmd, "fan_threshold") == 0)
{
if (val_item == NULL || !cJSON_IsNumber(val_item))
{
respond_error("missing or invalid value");
}
else
{
float thresh = (float)val_item->valuedouble;
if (thresh > 0 && thresh < 100)
{
BSP_FAN_SetThreshold(thresh);
respond_ok("fan threshold updated");
}
else
{
respond_error("threshold out of range");
}
}
}
/* --- FAN ON/OFF --- */
else if (strcasecmp(cmd, "fan") == 0)
{
if (val_item == NULL || !cJSON_IsString(val_item))
{
respond_error("missing value");
}
else
{
const char *val = val_item->valuestring;
if (strcasecmp(val, "on") == 0)
{
BSP_FAN_SetMode(FAN_MODE_MANUAL);
BSP_FAN_On();
respond_ok("fan on");
}
else if (strcasecmp(val, "off") == 0)
{
BSP_FAN_SetMode(FAN_MODE_MANUAL);
BSP_FAN_Off();
respond_ok("fan off");
}
else
{
respond_error("invalid fan value");
}
}
}
/* --- LIGHT ON/OFF --- */
else if (strcasecmp(cmd, "light") == 0)
{
if (val_item == NULL || !cJSON_IsString(val_item))
{
respond_error("missing value");
}
else
{
const char *val = val_item->valuestring;
if (strcasecmp(val, "on") == 0)
{
BSP_LIGHT_On();
respond_ok("light on");
}
else if (strcasecmp(val, "off") == 0)
{
BSP_LIGHT_Off();
respond_ok("light off");
}
else
{
respond_error("invalid light value");
}
}
}
/* --- RESET --- */
else if (strcasecmp(cmd, "reset") == 0)
{
BSP_Flash_ResetSettings();
respond_ok("settings reset to default");
}
/* --- UNKNOWN --- */
else
{
respond_error("unknown command");
}
cJSON_Delete(root);
}

21
Core/Bsp/bsp_cmd.h Normal file
View File

@@ -0,0 +1,21 @@
/**
* @file bsp_cmd.h
* @brief JSON command parser for UART1 (MQTT)
*/
#ifndef __BSP_CMD_H
#define __BSP_CMD_H
#include <stdint.h>
/**
* @brief Parse and execute a JSON command from UART1
* Request format: {"cmd":"xxx","value":"xxx"}
* Response format: {"status":"ok/error","msg":"xxx","data":{...}}
*
* @param buf: received JSON string (null-terminated)
* @param len: string length
*/
void BSP_Cmd_ProcessJSON(const uint8_t *buf, uint16_t len);
#endif

114
Core/Bsp/bsp_flash.c Normal file
View File

@@ -0,0 +1,114 @@
/**
* @file bsp_flash.c
* @brief Flash storage driver implementation
*/
#include "bsp_flash.h"
#include "main.h"
/* Default settings */
static const system_settings_t default_settings = {
.fan_threshold = 30.0f,
.fan_mode = 1, // AUTO
.light_state = 0, // OFF
.valid = SETTINGS_VALID_FLAG
};
/**
* @brief Get Flash page address
*/
static uint32_t get_page_address(uint32_t addr)
{
return addr & 0xFFFFF800; // 2KB page alignment
}
/**
* @brief Erase Flash page
*/
static HAL_StatusTypeDef erase_page(uint32_t page_address)
{
FLASH_EraseInitTypeDef erase_init;
uint32_t page_error = 0;
HAL_FLASH_Unlock();
erase_init.TypeErase = FLASH_TYPEERASE_PAGES;
erase_init.PageAddress = page_address;
erase_init.NbPages = 1;
HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&erase_init, &page_error);
HAL_FLASH_Lock();
return status;
}
/**
* @brief Load settings from Flash
*/
bool BSP_Flash_LoadSettings(system_settings_t *settings)
{
if (settings == NULL) return false;
/* Read from Flash */
system_settings_t *flash_data = (system_settings_t *)SETTINGS_ADDR;
/* Check valid flag */
if (flash_data->valid == SETTINGS_VALID_FLAG)
{
settings->fan_threshold = flash_data->fan_threshold;
settings->fan_mode = flash_data->fan_mode;
settings->light_state = flash_data->light_state;
settings->valid = flash_data->valid;
return true;
}
/* No valid settings, use defaults */
*settings = default_settings;
return false;
}
/**
* @brief Save settings to Flash
*/
bool BSP_Flash_SaveSettings(const system_settings_t *settings)
{
if (settings == NULL) return false;
uint32_t page_addr = get_page_address(SETTINGS_ADDR);
/* Erase page first */
if (erase_page(page_addr) != HAL_OK)
{
return false;
}
HAL_FLASH_Unlock();
/* Write data halfword by halfword */
uint16_t *src = (uint16_t *)settings;
uint32_t dst = SETTINGS_ADDR;
for (uint32_t i = 0; i < sizeof(system_settings_t) / 2; i++)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, dst, *src) != HAL_OK)
{
HAL_FLASH_Lock();
return false;
}
dst += 2;
src++;
}
HAL_FLASH_Lock();
return true;
}
/**
* @brief Reset settings to default
*/
void BSP_Flash_ResetSettings(void)
{
BSP_Flash_SaveSettings(&default_settings);
}

42
Core/Bsp/bsp_flash.h Normal file
View File

@@ -0,0 +1,42 @@
/**
* @file bsp_flash.h
* @brief Flash storage driver for saving settings
*/
#ifndef __BSP_FLASH_H
#define __BSP_FLASH_H
#include <stdint.h>
#include <stdbool.h>
/* Settings structure - stored in Flash */
typedef struct {
float fan_threshold; // Temperature threshold
uint8_t fan_mode; // Fan mode: 0=manual, 1=auto
uint8_t light_state; // Light state: 0=off, 1=on
uint8_t valid; // Valid flag (0xAA = valid)
} system_settings_t;
#define SETTINGS_VALID_FLAG 0xAA
#define SETTINGS_ADDR 0x0801F800 // Last page of 64KB Flash
/* Exported functions ---------------------------------------------------------*/
/**
* @brief Load settings from Flash
* @return true if valid settings found
*/
bool BSP_Flash_LoadSettings(system_settings_t *settings);
/**
* @brief Save settings to Flash
* @return true if success
*/
bool BSP_Flash_SaveSettings(const system_settings_t *settings);
/**
* @brief Reset settings to default
*/
void BSP_Flash_ResetSettings(void);
#endif

177
Core/Bsp/bsp_relay.c Normal file
View File

@@ -0,0 +1,177 @@
/**
* @file bsp_relay.c
* @brief Relay control driver implementation
*/
#include "bsp_relay.h"
#include "bsp_flash.h"
#include <stddef.h>
/* Private variables ----------------------------------------------------------*/
static fan_mode_t s_fan_mode = FAN_MODE_MANUAL;
static float s_fan_threshold = 30.0f; // Default threshold: 30°C
/**
* @brief Initialize all relay pins
* @retval None
*/
void BSP_Relay_Init(void)
{
/* Set all relays to OFF state (high level) by default */
HAL_GPIO_WritePin(LIGHT_GPIO_Port, LIGHT_Pin, RELAY_OFF);
HAL_GPIO_WritePin(FAN_GPIO_Port, FAN_Pin, RELAY_OFF);
/* Load settings from Flash */
system_settings_t settings;
if (BSP_Flash_LoadSettings(&settings))
{
s_fan_mode = (fan_mode_t)settings.fan_mode;
s_fan_threshold = settings.fan_threshold;
}
else
{
/* Use default values if no valid settings */
s_fan_mode = FAN_MODE_AUTO;
s_fan_threshold = 30.0f;
}
}
/**
* @brief Turn LIGHT relay ON (low level output)
* @retval None
*/
void BSP_LIGHT_On(void)
{
HAL_GPIO_WritePin(LIGHT_GPIO_Port, LIGHT_Pin, RELAY_ON);
}
/**
* @brief Turn LIGHT relay OFF (high level output)
* @retval None
*/
void BSP_LIGHT_Off(void)
{
HAL_GPIO_WritePin(LIGHT_GPIO_Port, LIGHT_Pin, RELAY_OFF);
}
/**
* @brief Toggle LIGHT relay state
* @retval None
*/
void BSP_LIGHT_Toggle(void)
{
HAL_GPIO_TogglePin(LIGHT_GPIO_Port, LIGHT_Pin);
}
uint8_t BSP_LIGHT_GetState(void)
{
return (HAL_GPIO_ReadPin(LIGHT_GPIO_Port, LIGHT_Pin) == RELAY_ON) ? 1 : 0;
}
/**
* @brief Turn FAN relay ON (low level output)
* @retval None
*/
void BSP_FAN_On(void)
{
HAL_GPIO_WritePin(FAN_GPIO_Port, FAN_Pin, RELAY_ON);
}
/**
* @brief Turn FAN relay OFF (high level output)
* @retval None
*/
void BSP_FAN_Off(void)
{
HAL_GPIO_WritePin(FAN_GPIO_Port, FAN_Pin, RELAY_OFF);
}
/**
* @brief Toggle FAN relay state
* @retval None
*/
void BSP_FAN_Toggle(void)
{
HAL_GPIO_TogglePin(FAN_GPIO_Port, FAN_Pin);
}
uint8_t BSP_FAN_GetState(void)
{
return (HAL_GPIO_ReadPin(FAN_GPIO_Port, FAN_Pin) == RELAY_ON) ? 1 : 0;
}
/**
* @brief Set FAN mode and save to Flash
*/
void BSP_FAN_SetMode(fan_mode_t mode)
{
s_fan_mode = mode;
/* Save to Flash */
system_settings_t settings = {
.fan_threshold = s_fan_threshold,
.fan_mode = mode,
.light_state = BSP_LIGHT_GetState(),
.valid = SETTINGS_VALID_FLAG
};
BSP_Flash_SaveSettings(&settings);
}
fan_mode_t BSP_FAN_GetMode(void)
{
return s_fan_mode;
}
/**
* @brief Set temperature threshold for auto mode and save to Flash
*/
void BSP_FAN_SetThreshold(float threshold)
{
s_fan_threshold = threshold;
/* Save to Flash */
system_settings_t settings = {
.fan_threshold = threshold,
.fan_mode = s_fan_mode,
.light_state = BSP_LIGHT_GetState(),
.valid = SETTINGS_VALID_FLAG
};
BSP_Flash_SaveSettings(&settings);
}
float BSP_FAN_GetThreshold(void)
{
return s_fan_threshold;
}
/**
* @brief Auto control FAN based on temperature
* @param current_temp: current temperature in Celsius
*/
void BSP_FAN_AutoControl(float current_temp)
{
if (s_fan_mode != FAN_MODE_AUTO)
return;
if (current_temp >= s_fan_threshold)
{
BSP_FAN_On();
}
else
{
BSP_FAN_Off();
}
}
/**
* @brief Get system state
*/
void BSP_System_GetState(system_state_t *state)
{
if (state == NULL) return;
state->fan_mode = s_fan_mode;
state->fan_threshold = s_fan_threshold;
state->light_state = BSP_LIGHT_GetState();
state->fan_state = BSP_FAN_GetState();
}

60
Core/Bsp/bsp_relay.h Normal file
View File

@@ -0,0 +1,60 @@
/**
* @file bsp_relay.h
* @brief Relay control driver (LIGHT & FAN)
* @details Low level effective (low level = ON)
*/
#ifndef __BSP_RELAY_H
#define __BSP_RELAY_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Exported defines ----------------------------------------------------------*/
#define RELAY_ON GPIO_PIN_RESET // Low level activates relay
#define RELAY_OFF GPIO_PIN_SET // High level deactivates relay
/* Exported types ------------------------------------------------------------*/
typedef enum {
FAN_MODE_MANUAL = 0, // Manual control
FAN_MODE_AUTO = 1 // Auto control by temperature
} fan_mode_t;
typedef struct {
fan_mode_t fan_mode; // FAN mode: manual or auto
float fan_threshold; // Temperature threshold for auto mode (Celsius)
uint8_t light_state; // LIGHT state: 0=off, 1=on
uint8_t fan_state; // FAN state: 0=off, 1=on
} system_state_t;
/* Exported functions prototypes --------------------------------------------*/
void BSP_Relay_Init(void);
void BSP_LIGHT_On(void);
void BSP_LIGHT_Off(void);
void BSP_LIGHT_Toggle(void);
uint8_t BSP_LIGHT_GetState(void);
void BSP_FAN_On(void);
void BSP_FAN_Off(void);
void BSP_FAN_Toggle(void);
uint8_t BSP_FAN_GetState(void);
/* Auto control functions (for temperature-based fan control) -------------*/
void BSP_FAN_SetMode(fan_mode_t mode);
fan_mode_t BSP_FAN_GetMode(void);
void BSP_FAN_SetThreshold(float threshold);
float BSP_FAN_GetThreshold(void);
void BSP_FAN_AutoControl(float current_temp);
/* System state --------------------------------------------------------------*/
void BSP_System_GetState(system_state_t *state);
#ifdef __cplusplus
}
#endif
#endif /* __BSP_RELAY_H */

131
Core/Bsp/bsp_uart.c Normal file
View File

@@ -0,0 +1,131 @@
/**
* @file bsp_uart.c
* @brief UART1 DMA + IDLE interrupt driver
*/
#include "bsp_uart.h"
#include "bsp_cmd.h"
#include "usart.h"
#include <string.h>
/* External DMA handle */
extern DMA_HandleTypeDef hdma_usart1_rx;
/* DMA receive buffer */
uint8_t uart1_rx_buf[UART1_RX_BUF_SIZE];
/* IDLE interrupt sets this flag, main loop checks and processes */
static volatile uint16_t idle_rx_len = 0;
/**
* @brief Init UART1 for MQTT module with DMA
*/
void BSP_UART1_Init(void)
{
/* Start DMA reception */
HAL_UART_Receive_DMA(&huart1, uart1_rx_buf, UART1_RX_BUF_SIZE);
/* Enable IDLE line detection interrupt */
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
}
/**
* @brief Send data via UART1 (blocking)
*/
uint16_t BSP_UART1_Send(uint8_t *data, uint16_t len)
{
return HAL_UART_Transmit(&huart1, data, len, 1000);
}
/**
* @brief Send string via UART1
*/
uint16_t BSP_UART1_SendString(const char *str)
{
return BSP_UART1_Send((uint8_t*)str, strlen(str));
}
/**
* @brief Get received data length in DMA buffer
*/
uint16_t BSP_UART1_GetRxLen(void)
{
return UART1_RX_BUF_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
}
/**
* @brief Read data from DMA rx buffer (non-destructive copy)
*/
uint16_t BSP_UART1_Read(uint8_t *buf, uint16_t len)
{
uint16_t head = UART1_RX_BUF_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
uint16_t cnt = 0;
while (cnt < len && cnt < head)
{
buf[cnt] = uart1_rx_buf[cnt];
cnt++;
}
return cnt;
}
/**
* @brief Clear rx buffer (restart DMA)
*/
void BSP_UART1_ClearRxBuf(void)
{
HAL_UART_AbortReceive(&huart1);
HAL_UART_Receive_DMA(&huart1, uart1_rx_buf, UART1_RX_BUF_SIZE);
}
/**
* @brief Check if data received
*/
bool BSP_UART1_IsDataReceived(void)
{
return (idle_rx_len > 0);
}
/**
* @brief Process received data (call in main loop)
* Copies data, parses JSON command, sends JSON response via UART1
*/
void BSP_UART1_Process(void)
{
if (idle_rx_len == 0)
return;
/* Copy length and clear flag */
uint16_t rx_len = idle_rx_len;
idle_rx_len = 0;
/* Copy data to local buffer so DMA restart won't overwrite */
uint8_t local_buf[UART1_RX_BUF_SIZE];
if (rx_len > sizeof(local_buf))
rx_len = sizeof(local_buf);
memcpy(local_buf, uart1_rx_buf, rx_len);
/* Process JSON command */
BSP_Cmd_ProcessJSON(local_buf, rx_len);
/* Restart DMA for next frame */
BSP_UART1_ClearRxBuf();
}
/**
* @brief Called from USART1 IDLE interrupt - saves length and restarts DMA
*/
void BSP_UART1_IDLE_IRQHandler(void)
{
uint16_t rx_len = UART1_RX_BUF_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
if (rx_len > 0)
{
idle_rx_len = rx_len;
}
/* Restart DMA immediately */
HAL_UART_AbortReceive(&huart1);
HAL_UART_Receive_DMA(&huart1, uart1_rx_buf, UART1_RX_BUF_SIZE);
}

59
Core/Bsp/bsp_uart.h Normal file
View File

@@ -0,0 +1,59 @@
/**
* @file bsp_uart.h
* @brief UART1 communication driver (DMA + IDLE interrupt for MQTT)
*/
#ifndef __BSP_UART_H
#define __BSP_UART_H
#include "main.h"
#include <stdint.h>
#include <stdbool.h>
#define UART1_RX_BUF_SIZE 256
/* Exported functions prototypes */
void BSP_UART1_Init(void);
/**
* @brief Send data via UART1 (blocking)
*/
uint16_t BSP_UART1_Send(uint8_t *data, uint16_t len);
/**
* @brief Send string via UART1 (blocking)
*/
uint16_t BSP_UART1_SendString(const char *str);
/**
* @brief Get received data length
*/
uint16_t BSP_UART1_GetRxLen(void);
/**
* @brief Read data from rx buffer
*/
uint16_t BSP_UART1_Read(uint8_t *buf, uint16_t len);
/**
* @brief Clear rx buffer (restart DMA)
*/
void BSP_UART1_ClearRxBuf(void);
/**
* @brief Check if data received
*/
bool BSP_UART1_IsDataReceived(void);
/**
* @brief Process received data (call in main loop)
*/
void BSP_UART1_Process(void);
/**
* @brief IDLE interrupt handler (call from ISR)
*/
void BSP_UART1_IDLE_IRQHandler(void);
#endif

540
Core/Bsp/cJSON.c Normal file
View File

@@ -0,0 +1,540 @@
/*
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
*/
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
#include "cJSON.h"
/* define our own boolean type */
#ifdef true
#undef true
#endif
#define true ((cJSON_bool)1)
#ifdef false
#undef false
#endif
#define false ((cJSON_bool)0)
typedef struct {
const unsigned char *content;
size_t length;
size_t offset;
size_t depth;
} parse_buffer;
/* case insensitive strcmp */
CJSON_PUBLIC(int) cJSON_Strcasecmp(const char *a, const char *b)
{
if (!a && !b) return 0;
if (!a || !b) return 1;
for (; tolower(*a) == tolower(*b); a++, b++)
{
if (*a == '\0') return 0;
}
return tolower(*a) - tolower(*b);
}
static cJSON_bool parse_number(cJSON *item, parse_buffer *input)
{
double number = 0;
unsigned char *after_end = NULL;
unsigned char number_c_string[64];
size_t i = 0;
if (input->length == 0) return false;
if (input->length > (sizeof(number_c_string) - 1)) return false;
for (i = 0; (i < input->length) && (input->content[input->offset + i] != '\0'); i++)
{
number_c_string[i] = input->content[input->offset + i];
}
number_c_string[i] = '\0';
number = strtod((char *)number_c_string, (char **)&after_end);
if (number_c_string == after_end) return false;
item->valuedouble = number;
item->valueint = (int)number;
item->type = cJSON_Number;
input->offset += (size_t)(after_end - number_c_string);
return true;
}
static cJSON_bool parse_string(cJSON *item, parse_buffer *input)
{
const unsigned char *input_pointer = input->content + input->offset;
size_t i = 0;
if (input->length < 2) return false;
if (input_pointer[0] != '\"') return false;
input_pointer++;
while ((i < input->length) && (input_pointer[0] != '\0'))
{
if (input_pointer[0] == '\"')
{
char *output = (char *)malloc(i + 1);
if (!output) return false;
memcpy(output, input_pointer - i, i);
output[i] = '\0';
item->valuestring = output;
item->type = cJSON_String;
input->offset += i + 2;
return true;
}
i++;
input_pointer++;
}
return false;
}
static parse_buffer *skip_whitespace(parse_buffer *buffer)
{
if (buffer == NULL || buffer->content == NULL) return NULL;
while ((buffer->offset < buffer->length) && isspace(buffer->content[buffer->offset]))
{
buffer->offset++;
}
return buffer;
}
static cJSON_bool parse_value(cJSON *item, parse_buffer *input);
static cJSON_bool parse_array(cJSON *item, parse_buffer *input)
{
cJSON *child = NULL;
if (input->content[input->offset] != '[') return false;
input->offset++;
skip_whitespace(input);
if (input->content[input->offset] == ']')
{
item->type = cJSON_Array;
input->offset++;
return true;
}
input->offset--;
child = cJSON_CreateNull();
if (child == NULL) return false;
input->offset++;
if (!parse_value(child, skip_whitespace(input))) { cJSON_Delete(child); return false; }
item->child = child;
skip_whitespace(input);
while (input->content[input->offset] == ',')
{
cJSON *new_item = cJSON_CreateNull();
if (new_item == NULL) { cJSON_Delete(child); return false; }
input->offset++;
if (!parse_value(new_item, skip_whitespace(input))) { cJSON_Delete(new_item); cJSON_Delete(child); return false; }
child->next = new_item;
new_item->prev = child;
child = new_item;
skip_whitespace(input);
}
if (input->content[input->offset] != ']') { cJSON_Delete(child); return false; }
input->offset++;
item->type = cJSON_Array;
return true;
}
static cJSON_bool parse_object(cJSON *item, parse_buffer *input)
{
cJSON *child = NULL;
if (input->content[input->offset] != '{') return false;
input->offset++;
skip_whitespace(input);
if (input->content[input->offset] == '}')
{
item->type = cJSON_Object;
input->offset++;
return true;
}
child = cJSON_CreateNull();
if (child == NULL) return false;
/* Parse key */
if (!parse_string(child, input)) { cJSON_Delete(child); return false; }
char *key = child->valuestring;
child->valuestring = NULL; /* clear before parse_value overwrites it */
/* Parse value */
skip_whitespace(input);
if (input->content[input->offset] != ':') { cJSON_Delete(child); return false; }
input->offset++;
if (!parse_value(child, skip_whitespace(input))) { cJSON_Delete(child); return false; }
item->child = child;
child->string = key;
skip_whitespace(input);
while (input->content[input->offset] == ',')
{
cJSON *new_item = cJSON_CreateNull();
if (new_item == NULL) { cJSON_Delete(child); return false; }
input->offset++;
/* Parse key */
if (!parse_string(new_item, input)) { cJSON_Delete(new_item); cJSON_Delete(child); return false; }
char *new_key = new_item->valuestring;
new_item->valuestring = NULL;
/* Parse value */
skip_whitespace(input);
if (input->content[input->offset] != ':') { cJSON_Delete(new_item); cJSON_Delete(child); return false; }
input->offset++;
if (!parse_value(new_item, skip_whitespace(input))) { cJSON_Delete(new_item); cJSON_Delete(child); return false; }
new_item->string = new_key;
child->next = new_item;
new_item->prev = child;
child = new_item;
skip_whitespace(input);
}
if (input->content[input->offset] != '}') { cJSON_Delete(child); return false; }
input->offset++;
item->type = cJSON_Object;
return true;
}
static cJSON_bool parse_value(cJSON *item, parse_buffer *input)
{
if (input == NULL || item == NULL) return false;
skip_whitespace(input);
if (input->length == 0) return false;
switch (input->content[input->offset])
{
case '-': case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
return parse_number(item, input);
case 't': case 'f':
if (strncmp((const char *)input->content + input->offset, "true", 4) == 0)
{
item->type = cJSON_True;
input->offset += 4;
return true;
}
if (strncmp((const char *)input->content + input->offset, "false", 5) == 0)
{
item->type = cJSON_False;
input->offset += 5;
return true;
}
return false;
case 'n':
if (strncmp((const char *)input->content + input->offset, "null", 4) == 0)
{
item->type = cJSON_NULL;
input->offset += 4;
return true;
}
return false;
case '\"':
return parse_string(item, input);
case '[':
return parse_array(item, input);
case '{':
return parse_object(item, input);
default:
return false;
}
}
/* Custom hooks */
static cJSON_Hooks hooks = { malloc, free };
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks *h)
{
if (h != NULL)
{
hooks.malloc_fn = h->malloc_fn;
hooks.free_fn = h->free_fn;
}
}
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
{
parse_buffer buffer = {0};
cJSON *item = NULL;
if (value == NULL) return NULL;
buffer.length = strlen(value);
buffer.content = (const unsigned char *)value;
buffer.offset = 0;
item = (cJSON *)hooks.malloc_fn(sizeof(cJSON));
if (item == NULL) return NULL;
memset(item, 0, sizeof(cJSON));
if (!parse_value(item, skip_whitespace(&buffer)))
{
cJSON_Delete(item);
return NULL;
}
return item;
}
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
{
cJSON *next = NULL;
while (item != NULL)
{
next = item->next;
if (item->child != NULL) cJSON_Delete(item->child);
if (item->valuestring != NULL) hooks.free_fn(item->valuestring);
if (item->string != NULL) hooks.free_fn(item->string);
hooks.free_fn(item);
item = next;
}
}
/* --- Print functions (unformatted) --- */
static size_t print_value_ptr(const cJSON *item, char *buffer);
static size_t print_string_to(const char *str, char *buf)
{
size_t len = strlen(str) + 2;
if (buf)
{
buf[0] = '\"';
memcpy(buf + 1, str, len - 2);
buf[len - 1] = '\"';
}
return len;
}
static size_t print_number_to(const cJSON *item, char *buf)
{
char tmp[32];
size_t len;
if (fabs(floor(item->valuedouble) - item->valuedouble) <= DBL_EPSILON && fabs(item->valuedouble) < 1.0e15)
len = (size_t)sprintf(tmp, "%d", item->valueint);
else
len = (size_t)sprintf(tmp, "%g", item->valuedouble);
if (buf) memcpy(buf, tmp, len);
return len;
}
static size_t print_array_to(const cJSON *item, char *buf)
{
size_t pos = 0;
if (buf) buf[pos++] = '['; else pos++;
cJSON *child = item->child;
while (child != NULL)
{
pos += print_value_ptr(child, buf ? buf + pos : NULL);
if (child->next != NULL) { if (buf) buf[pos++] = ','; else pos++; }
child = child->next;
}
if (buf) buf[pos++] = ']'; else pos++;
return pos;
}
static size_t print_object_to(const cJSON *item, char *buf)
{
size_t pos = 0;
if (buf) buf[pos++] = '{'; else pos++;
cJSON *child = item->child;
while (child != NULL)
{
pos += print_string_to(child->string, buf ? buf + pos : NULL);
if (buf) buf[pos++] = ':'; else pos++;
pos += print_value_ptr(child, buf ? buf + pos : NULL);
if (child->next != NULL) { if (buf) buf[pos++] = ','; else pos++; }
child = child->next;
}
if (buf) buf[pos++] = '}'; else pos++;
return pos;
}
static size_t print_value_ptr(const cJSON *item, char *buffer)
{
switch (item->type & 0xFF)
{
case cJSON_NULL: if (buffer) { memcpy(buffer, "null", 4); } return 4;
case cJSON_False: if (buffer) { memcpy(buffer, "false", 5); } return 5;
case cJSON_True: if (buffer) { memcpy(buffer, "true", 4); } return 4;
case cJSON_Number: return print_number_to(item, buffer);
case cJSON_String: return print_string_to(item->valuestring, buffer);
case cJSON_Array: return print_array_to(item, buffer);
case cJSON_Object: return print_object_to(item, buffer);
default: return 0;
}
}
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
{
return cJSON_PrintUnformatted(item);
}
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
{
size_t length = 0;
char *output = NULL;
length = print_value_ptr(item, NULL);
output = (char *)hooks.malloc_fn(length + 1);
if (output == NULL) return NULL;
print_value_ptr(item, output);
output[length] = '\0';
return output;
}
/* --- Item access --- */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
{
cJSON *child = NULL;
size_t size = 0;
if (array == NULL || !cJSON_IsArray(array)) return 0;
child = array->child;
while (child != NULL) { size++; child = child->next; }
return (int)size;
}
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)
{
cJSON *child = NULL;
int i = 0;
if (array == NULL || !cJSON_IsArray(array)) return NULL;
child = array->child;
while (child != NULL && i < index) { i++; child = child->next; }
return child;
}
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON *object, const char *string)
{
cJSON *current = NULL;
if (object == NULL || string == NULL) return NULL;
current = object->child;
while (current != NULL)
{
if (current->string != NULL && strcmp(current->string, string) == 0) return current;
current = current->next;
}
return NULL;
}
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *object, const char *string)
{
cJSON *current = NULL;
if (object == NULL || string == NULL) return NULL;
current = object->child;
while (current != NULL)
{
if (current->string != NULL && cJSON_Strcasecmp(current->string, string) == 0) return current;
current = current->next;
}
return NULL;
}
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string)
{
return cJSON_GetObjectItem(object, string) != NULL;
}
static const char *global_error_ptr = NULL;
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) { return global_error_ptr; }
/* --- Create items --- */
static cJSON *cJSON_New_Item(void)
{
cJSON *item = (cJSON *)hooks.malloc_fn(sizeof(cJSON));
if (item != NULL) memset(item, 0, sizeof(cJSON));
return item;
}
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) { cJSON *item = cJSON_New_Item(); if(item) item->type = cJSON_NULL; return item; }
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void) { cJSON *item = cJSON_New_Item(); if(item) item->type = cJSON_True; return item; }
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) { cJSON *item = cJSON_New_Item(); if(item) item->type = cJSON_False; return item; }
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool b) { cJSON *item = cJSON_New_Item(); if(item) item->type = b ? cJSON_True : cJSON_False; return item; }
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) { cJSON *item = cJSON_New_Item(); if(item) { item->type = cJSON_Number; item->valuedouble = num; item->valueint = (int)num; } return item; }
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *str)
{
cJSON *item = cJSON_New_Item();
if (item && str)
{
item->type = cJSON_String;
item->valuestring = (char *)hooks.malloc_fn(strlen(str) + 1);
if (item->valuestring) strcpy(item->valuestring, str);
}
return item;
}
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) { return cJSON_CreateString(raw); }
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) { cJSON *item = cJSON_New_Item(); if(item) item->type = cJSON_Array; return item; }
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) { cJSON *item = cJSON_New_Item(); if(item) item->type = cJSON_Object; return item; }
/* --- Add items --- */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item)
{
cJSON *child = NULL;
if (array == NULL || item == NULL) return false;
child = array->child;
if (child == NULL) { array->child = item; }
else
{
while (child->next) child = child->next;
child->next = item;
item->prev = child;
}
return true;
}
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *name, cJSON *item)
{
if (object == NULL || name == NULL || item == NULL) return false;
if (item->string != NULL) hooks.free_fn(item->string);
item->string = (char *)hooks.malloc_fn(strlen(name) + 1);
if (item->string) strcpy(item->string, name);
return cJSON_AddItemToArray(object, item);
}
/* --- Type checks --- */
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON *item) { return (item != NULL) && (item->type == cJSON_Invalid); }
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON *item) { return (item != NULL) && (item->type == cJSON_False); }
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON *item) { return (item != NULL) && (item->type == cJSON_True); }
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON *item) { return (item != NULL) && (item->type & cJSON_Bool); }
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON *item) { return (item != NULL) && (item->type == cJSON_Number); }
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON *item) { return (item != NULL) && (item->type == cJSON_String); }
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON *item) { return (item != NULL) && (item->type == cJSON_Array); }
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON *item) { return (item != NULL) && (item->type == cJSON_Object); }
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON *item) { return (item != NULL) && (item->type == cJSON_Raw); }
/* --- Add helpers --- */
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON *object, const char *name) { cJSON *item = cJSON_CreateNull(); if(item) cJSON_AddItemToObject(object, name, item); return item; }
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON *object, const char *name) { cJSON *item = cJSON_CreateTrue(); if(item) cJSON_AddItemToObject(object, name, item); return item; }
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON *object, const char *name) { cJSON *item = cJSON_CreateFalse(); if(item) cJSON_AddItemToObject(object, name, item); return item; }
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON *object, const char *name, cJSON_bool b) { cJSON *item = cJSON_CreateBool(b); if(item) cJSON_AddItemToObject(object, name, item); return item; }
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON *object, const char *name, double n) { cJSON *item = cJSON_CreateNumber(n); if(item) cJSON_AddItemToObject(object, name, item); return item; }
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON *object, const char *name, const char *s) { cJSON *item = cJSON_CreateString(s); if(item) cJSON_AddItemToObject(object, name, item); return item; }
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON *object, const char *name, const char *raw) { cJSON *item = cJSON_CreateRaw(raw); if(item) cJSON_AddItemToObject(object, name, item); return item; }
CJSON_PUBLIC(void) cJSON_SetValuestring(cJSON *object, const char *valuestring)
{
if (object == NULL || !cJSON_IsString(object) || valuestring == NULL) return;
if (object->valuestring != NULL) hooks.free_fn(object->valuestring);
object->valuestring = (char *)hooks.malloc_fn(strlen(valuestring) + 1);
if (object->valuestring) strcpy(object->valuestring, valuestring);
}
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) { (void)numbers; (void)count; return NULL; }
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) { (void)numbers; (void)count; return NULL; }
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) { (void)numbers; (void)count; return NULL; }
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count) { (void)strings; (void)count; return NULL; }
CJSON_PUBLIC(void) cJSON_free(void *ptr) { if(ptr) hooks.free_fn(ptr); }

203
Core/Bsp/cJSON.h Normal file
View File

@@ -0,0 +1,203 @@
/*
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* cJSON */
/* JSON parser in C. */
#ifndef CJSON_H
#define CJSON_H
#ifdef __cplusplus
extern "C"
{
#endif
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
#define __WINDOWS__
#endif
#ifdef __WINDOWS__
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbols
*/
#ifndef CJSON_HIDE_SYMBOLS
#define CJSON_EXPORT_SYMBOLS
#endif
#endif /* __WINDOWS__ */
#ifdef CJSON_EXPORT_SYMBOLS
#define CJSON_PUBLIC(type) __declspec(dllexport) type
#elif defined(CJSON_IMPORT_SYMBOLS)
#define CJSON_PUBLIC(type) __declspec(dllimport) type
#else
#define CJSON_PUBLIC(type) type
#endif
#include <stddef.h>
/* project version */
#define CJSON_VERSION_MAJOR 1
#define CJSON_VERSION_MINOR 7
#define CJSON_VERSION_PATCH 18
/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False (1 << 0)
#define cJSON_True (1 << 1)
#define cJSON_Bool (cJSON_True | cJSON_False)
#define cJSON_Number (1 << 2)
#define cJSON_String (1 << 3)
#define cJSON_Array (1 << 4)
#define cJSON_Object (1 << 5)
#define cJSON_Raw (1 << 6)
#define cJSON_NULL (1 << 7)
#define cJSON_IsReference 256
#define cJSON_StringIsConst 512
/* The cJSON structure: */
typedef struct cJSON
{
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *next;
struct cJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
struct cJSON *child;
/* The type of the item, as above. */
int type;
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring;
/* The item's name string, if this item is the child of an object */
char *string;
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
} cJSON;
typedef struct cJSON_Hooks
{
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;
typedef int cJSON_bool;
/* Limits how deeply nested a structure can be */
#define CJSON_NESTING_LIMIT 1000
/* String comparison, case insensitive */
CJSON_PUBLIC(int) cJSON_Strcasecmp(const char *a, const char *b);
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
/* Render a cJSON entity to text for transfer/storage. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. */
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
/* Delete a cJSON entity and all subentities. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
/* Get item "string" from object. Case insensitive. */
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
/* These calls create a cJSON item of the appropriate type. */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
/* raw json */
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
/* These utilities create an Array of count items. */
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);
/* Append item to the specified array/object. */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
/* Append item to the specified object. */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
/* These functions check the type of an item */
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
/* These calls create a cJSON item of the appropriate type and add it to the specified object. */
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
/* Assign a raw string value to an object. */
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
/* Memory management */
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks *hooks);
CJSON_PUBLIC(void) cJSON_free(void *ptr);
/* Macros for creating things quickly. */
#define cJSON_AddNullToObject(object,name) cJSON_AddNullToObject(object, name)
#define cJSON_AddTrueToObject(object,name) cJSON_AddTrueToObject(object, name)
#define cJSON_AddFalseToObject(object,name) cJSON_AddFalseToObject(object, name)
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddBoolToObject(object, name, b)
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddNumberToObject(object, name, n)
#define cJSON_AddStringToObject(object,name,s) cJSON_AddStringToObject(object, name, s)
/* When assigning a string value, cJSON will copy the string, so you don't need to worry about it being freed.
* When assigning a raw value, cJSON will NOT copy the string, so it MUST be persistent. */
CJSON_PUBLIC(void) cJSON_SetValuestring(cJSON *object, const char *valuestring);
#ifdef __cplusplus
}
#endif
#endif

1635
Core/Bsp/driver_bmp280.c Normal file

File diff suppressed because it is too large Load Diff

669
Core/Bsp/driver_bmp280.h Normal file
View File

@@ -0,0 +1,669 @@
/**
* Copyright (c) 2015 - present LibDriver All rights reserved
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file driver_bmp280.h
* @brief driver bmp280 header file
* @version 1.0.0
* @author Shifeng Li
* @date 2024-01-15
*
* <h3>history</h3>
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2024/01/15 <td>1.0 <td>Shifeng Li <td>first upload
* </table>
*/
#ifndef DRIVER_BMP280_H
#define DRIVER_BMP280_H
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#ifdef __cplusplus
extern "C"{
#endif
/**
* @defgroup bmp280_driver bmp280 driver function
* @brief bmp280 driver modules
* @{
*/
/**
* @addtogroup bmp280_base_driver
* @{
*/
/**
* @brief bmp280 interface enumeration definition
*/
typedef enum
{
BMP280_INTERFACE_IIC = 0x00, /**< iic interface */
BMP280_INTERFACE_SPI = 0x01, /**< spi interface */
} bmp280_interface_t;
/**
* @brief bmp280 address enumeration definition
*/
typedef enum
{
BMP280_ADDRESS_ADO_LOW = (0x76 << 1), /**< ADO is low */
BMP280_ADDRESS_ADO_HIGH = (0x77 << 1), /**< ADO is HIGH */
} bmp280_address_t;
/**
* @brief bmp280 status enumeration definition
*/
typedef enum
{
BMP280_STATUS_MEASURING = (1 << 3), /**< measuring */
BMP280_STATUS_IM_UPDATE = (1 << 0), /**< im update */
} bmp280_status_t;
/**
* @brief bmp280 oversampling enumeration definition
*/
typedef enum
{
BMP280_OVERSAMPLING_SKIP = 0x00, /**< oversampling x1 */
BMP280_OVERSAMPLING_x1 = 0x01, /**< oversampling x1 */
BMP280_OVERSAMPLING_x2 = 0x02, /**< oversampling x2 */
BMP280_OVERSAMPLING_x4 = 0x03, /**< oversampling x4 */
BMP280_OVERSAMPLING_x8 = 0x04, /**< oversampling x8 */
BMP280_OVERSAMPLING_x16 = 0x05, /**< oversampling x16 */
} bmp280_oversampling_t;
/**
* @brief bmp280 mode enumeration definition
*/
typedef enum
{
BMP280_MODE_SLEEP = 0x00, /**< sleep mode */
BMP280_MODE_FORCED = 0x01, /**< forced mode */
BMP280_MODE_NORMAL = 0x03, /**< normal mode */
} bmp280_mode_t;
/**
* @brief bmp280 standby time enumeration definition
*/
typedef enum
{
BMP280_STANDBY_TIME_0P5_MS = 0x00, /**< 0.5ms */
BMP280_STANDBY_TIME_62P5_MS = 0x01, /**< 62.5ms */
BMP280_STANDBY_TIME_125_MS = 0x02, /**< 125ms */
BMP280_STANDBY_TIME_250_MS = 0x03, /**< 250ms */
BMP280_STANDBY_TIME_500_MS = 0x04, /**< 500ms */
BMP280_STANDBY_TIME_1000_MS = 0x05, /**< 1000ms */
BMP280_STANDBY_TIME_2000_MS = 0x06, /**< 2000ms */
BMP280_STANDBY_TIME_4000_MS = 0x07, /**< 4000ms */
} bmp280_standby_time_t;
/**
* @brief bmp280 filter enumeration definition
*/
typedef enum
{
BMP280_FILTER_OFF = 0x00, /**< off */
BMP280_FILTER_COEFF_2 = 0x01, /**< coeff 2 */
BMP280_FILTER_COEFF_4 = 0x02, /**< coeff 4 */
BMP280_FILTER_COEFF_8 = 0x03, /**< coeff 8 */
BMP280_FILTER_COEFF_16 = 0x04, /**< coeff 16 */
} bmp280_filter_t;
/**
* @brief bmp280 spi wire enumeration definition
*/
typedef enum
{
BMP280_SPI_WIRE_4 = 0x00, /**< 4 wire */
BMP280_SPI_WIRE_3 = 0x01, /**< 3 wire */
} bmp280_spi_wire_t;
/**
* @brief bmp280 handle structure definition
*/
typedef struct bmp280_handle_s
{
uint8_t iic_addr; /**< iic device address */
uint8_t (*iic_init)(void); /**< point to an iic_init function address */
uint8_t (*iic_deinit)(void); /**< point to an iic_deinit function address */
uint8_t (*iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len); /**< point to an iic_read function address */
uint8_t (*iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len); /**< point to an iic_write function address */
uint8_t (*spi_init)(void); /**< point to a spi_init function address */
uint8_t (*spi_deinit)(void); /**< point to a spi_deinit function address */
uint8_t (*spi_read)(uint8_t reg, uint8_t *buf, uint16_t len); /**< point to a spi_read function address */
uint8_t (*spi_write)(uint8_t reg, uint8_t *buf, uint16_t len); /**< point to a spi_write function address */
void (*delay_ms)(uint32_t ms); /**< point to a delay_ms function address */
void (*debug_print)(const char *const fmt, ...); /**< point to a debug_print function address */
uint8_t inited; /**< inited flag */
uint8_t iic_spi; /**< iic spi interface */
uint16_t t1; /**< t1 register */
int16_t t2; /**< t2 register */
int16_t t3; /**< t3 register */
uint16_t p1; /**< p1 register */
int16_t p2; /**< p2 register */
int16_t p3; /**< p3 register */
int16_t p4; /**< p4 register */
int16_t p5; /**< p5 register */
int16_t p6; /**< p6 register */
int16_t p7; /**< p7 register */
int16_t p8; /**< p8 register */
int16_t p9; /**< p9 register */
int32_t t_fine; /**< inner register */
} bmp280_handle_t;
/**
* @brief bmp280 information structure definition
*/
typedef struct bmp280_info_s
{
char chip_name[32]; /**< chip name */
char manufacturer_name[32]; /**< manufacturer name */
char interface[8]; /**< chip interface name */
float supply_voltage_min_v; /**< chip min supply voltage */
float supply_voltage_max_v; /**< chip max supply voltage */
float max_current_ma; /**< chip max current */
float temperature_min; /**< chip min operating temperature */
float temperature_max; /**< chip max operating temperature */
uint32_t driver_version; /**< driver version */
} bmp280_info_t;
/**
* @}
*/
/**
* @defgroup bmp280_link_driver bmp280 link driver function
* @brief bmp280 link driver modules
* @ingroup bmp280_driver
* @{
*/
/**
* @brief initialize bmp280_handle_t structure
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] STRUCTURE bmp280_handle_t
* @note none
*/
#define DRIVER_BMP280_LINK_INIT(HANDLE, STRUCTURE) memset(HANDLE, 0, sizeof(STRUCTURE))
/**
* @brief link iic_init function
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] FUC pointer to an iic_init function address
* @note none
*/
#define DRIVER_BMP280_LINK_IIC_INIT(HANDLE, FUC) (HANDLE)->iic_init = FUC
/**
* @brief link iic_deinit function
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] FUC pointer to an iic_deinit function address
* @note none
*/
#define DRIVER_BMP280_LINK_IIC_DEINIT(HANDLE, FUC) (HANDLE)->iic_deinit = FUC
/**
* @brief link iic_read function
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] FUC pointer to an iic_read function address
* @note none
*/
#define DRIVER_BMP280_LINK_IIC_READ(HANDLE, FUC) (HANDLE)->iic_read = FUC
/**
* @brief link iic_write function
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] FUC pointer to an iic_write function address
* @note none
*/
#define DRIVER_BMP280_LINK_IIC_WRITE(HANDLE, FUC) (HANDLE)->iic_write = FUC
/**
* @brief link spi_init function
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] FUC pointer to a spi_init function address
* @note none
*/
#define DRIVER_BMP280_LINK_SPI_INIT(HANDLE, FUC) (HANDLE)->spi_init = FUC
/**
* @brief link spi_deinit function
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] FUC pointer to a spi_deinit function address
* @note none
*/
#define DRIVER_BMP280_LINK_SPI_DEINIT(HANDLE, FUC) (HANDLE)->spi_deinit = FUC
/**
* @brief link spi_read function
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] FUC pointer to a spi_read function address
* @note none
*/
#define DRIVER_BMP280_LINK_SPI_READ(HANDLE, FUC) (HANDLE)->spi_read = FUC
/**
* @brief link spi_write function
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] FUC pointer to a spi_write function address
* @note none
*/
#define DRIVER_BMP280_LINK_SPI_WRITE(HANDLE, FUC) (HANDLE)->spi_write = FUC
/**
* @brief link delay_ms function
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] FUC pointer to a delay_ms function address
* @note none
*/
#define DRIVER_BMP280_LINK_DELAY_MS(HANDLE, FUC) (HANDLE)->delay_ms = FUC
/**
* @brief link debug_print function
* @param[in] HANDLE pointer to a bmp280 handle structure
* @param[in] FUC pointer to a debug_print function address
* @note none
*/
#define DRIVER_BMP280_LINK_DEBUG_PRINT(HANDLE, FUC) (HANDLE)->debug_print = FUC
/**
* @}
*/
/**
* @defgroup bmp280_base_driver bmp280 base driver function
* @brief bmp280 base driver modules
* @ingroup bmp280_driver
* @{
*/
/**
* @brief get chip's information
* @param[out] *info pointer to a bmp280 info structure
* @return status code
* - 0 success
* - 2 handle is NULL
* @note none
*/
uint8_t bmp280_info(bmp280_info_t *info);
/**
* @brief set the interface
* @param[in] *handle pointer to a bmp280 handle structure
* @param[in] interface chip interface
* @return status code
* - 0 success
* - 2 handle is NULL
* @note none
*/
uint8_t bmp280_set_interface(bmp280_handle_t *handle, bmp280_interface_t interface);
/**
* @brief get the interface
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *interface pointer to a chip interface buffer
* @return status code
* - 0 success
* - 2 handle is NULL
* @note none
*/
uint8_t bmp280_get_interface(bmp280_handle_t *handle, bmp280_interface_t *interface);
/**
* @brief set the iic address pin
* @param[in] *handle pointer to a bmp280 handle structure
* @param[in] addr_pin iic address pin
* @return status code
* - 0 success
* - 2 handle is NULL
* @note none
*/
uint8_t bmp280_set_addr_pin(bmp280_handle_t *handle, bmp280_address_t addr_pin);
/**
* @brief get the iic address pin
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *addr_pin pointer to an iic address pin buffer
* @return status code
* - 0 success
* - 2 handle is NULL
* @note none
*/
uint8_t bmp280_get_addr_pin(bmp280_handle_t *handle, bmp280_address_t *addr_pin);
/**
* @brief initialize the chip
* @param[in] *handle pointer to a bmp280 handle structure
* @return status code
* - 0 success
* - 1 iic or spi initialization failed
* - 2 handle is NULL
* - 3 linked functions is NULL
* - 4 id is error
* - 5 get nvm calibration failed
* - 6 read calibration failed
* @note none
*/
uint8_t bmp280_init(bmp280_handle_t *handle);
/**
* @brief close the chip
* @param[in] *handle pointer to a bmp280 handle structure
* @return status code
* - 0 success
* - 1 iic deinit failed
* - 2 handle is NULL
* - 3 handle is not initialized
* - 4 power down failed
* @note none
*/
uint8_t bmp280_deinit(bmp280_handle_t *handle);
/**
* @brief read the temperature and pressure data
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *temperature_raw pointer to a raw temperature buffer
* @param[out] *temperature_c pointer to a converted temperature buffer
* @param[out] *pressure_raw pointer to a raw pressure buffer
* @param[out] *pressure_pa pointer to a converted pressure buffer
* @return status code
* - 0 success
* - 1 read failed
* - 2 handle is NULL
* - 3 handle is not initialized
* - 4 compensate pressure failed
* - 5 read timeout
* @note none
*/
uint8_t bmp280_read_temperature_pressure(bmp280_handle_t *handle, uint32_t *temperature_raw, float *temperature_c,
uint32_t *pressure_raw, float *pressure_pa);
/**
* @brief read the pressure data
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *pressure_raw pointer to a raw pressure buffer
* @param[out] *pressure_pa pointer to a converted pressure buffer
* @return status code
* - 0 success
* - 1 pressure read failed
* - 2 handle is NULL
* - 3 handle is not initialized
* - 4 compensate pressure failed
* - 5 read timeout
* @note none
*/
uint8_t bmp280_read_pressure(bmp280_handle_t *handle, uint32_t *pressure_raw, float *pressure_pa);
/**
* @brief read the temperature data
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *temperature_raw pointer to a raw temperature buffer
* @param[out] *temperature_c pointer to a converted temperature buffer
* @return status code
* - 0 success
* - 1 temperature read failed
* - 2 handle is NULL
* - 3 handle is not initialized
* - 4 compensate pressure failed
* - 5 read timeout
* @note none
*/
uint8_t bmp280_read_temperature(bmp280_handle_t *handle, uint32_t *temperature_raw, float *temperature_c);
/**
* @brief soft reset
* @param[in] *handle pointer to a bmp280 handle structure
* @return status code
* - 0 success
* - 1 soft reset failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_soft_reset(bmp280_handle_t *handle);
/**
* @brief get status
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *status pointer to a status buffer
* @return status code
* - 0 success
* - 1 get status failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_get_status(bmp280_handle_t *handle, uint8_t *status);
/**
* @brief set temperatue oversampling
* @param[in] *handle pointer to a bmp280 handle structure
* @param[in] oversampling temperatue oversampling
* @return status code
* - 0 success
* - 1 set temperatue oversampling failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_set_temperatue_oversampling(bmp280_handle_t *handle, bmp280_oversampling_t oversampling);
/**
* @brief get temperatue oversampling
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *oversampling pointer to a oversampling buffer
* @return status code
* - 0 success
* - 1 get temperatue oversampling failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_get_temperatue_oversampling(bmp280_handle_t *handle, bmp280_oversampling_t *oversampling);
/**
* @brief set pressure oversampling
* @param[in] *handle pointer to a bmp280 handle structure
* @param[in] oversampling pressure oversampling
* @return status code
* - 0 success
* - 1 set pressure oversampling failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_set_pressure_oversampling(bmp280_handle_t *handle, bmp280_oversampling_t oversampling);
/**
* @brief get pressure oversampling
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *oversampling pointer to a oversampling buffer
* @return status code
* - 0 success
* - 1 get pressure oversampling failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_get_pressure_oversampling(bmp280_handle_t *handle, bmp280_oversampling_t *oversampling);
/**
* @brief set mode
* @param[in] *handle pointer to a bmp280 handle structure
* @param[in] mode chip mode
* @return status code
* - 0 success
* - 1 set mode failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_set_mode(bmp280_handle_t *handle, bmp280_mode_t mode);
/**
* @brief get mode
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *mode pointer to a mode buffer
* @return status code
* - 0 success
* - 1 get mode failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_get_mode(bmp280_handle_t *handle, bmp280_mode_t *mode);
/**
* @brief set standby time
* @param[in] *handle pointer to a bmp280 handle structure
* @param[in] standby_time standby time
* @return status code
* - 0 success
* - 1 set standby time failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_set_standby_time(bmp280_handle_t *handle, bmp280_standby_time_t standby_time);
/**
* @brief get standby time
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *standby_time pointer to a standby time buffer
* @return status code
* - 0 success
* - 1 get standby time failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_get_standby_time(bmp280_handle_t *handle, bmp280_standby_time_t *standby_time);
/**
* @brief set filter
* @param[in] *handle pointer to a bmp280 handle structure
* @param[in] filter input filter
* @return status code
* - 0 success
* - 1 set filter failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_set_filter(bmp280_handle_t *handle, bmp280_filter_t filter);
/**
* @brief get filter
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *filter pointer to a filter buffer
* @return status code
* - 0 success
* - 1 get filter failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_get_filter(bmp280_handle_t *handle, bmp280_filter_t *filter);
/**
* @brief set spi wire
* @param[in] *handle pointer to a bmp280 handle structure
* @param[in] spi spi wire
* @return status code
* - 0 success
* - 1 set spi wire failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_set_spi_wire(bmp280_handle_t *handle, bmp280_spi_wire_t spi);
/**
* @brief get spi wire
* @param[in] *handle pointer to a bmp280 handle structure
* @param[out] *spi pointer to a spi wire buffer
* @return status code
* - 0 success
* - 1 get spi wire failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_get_spi_wire(bmp280_handle_t *handle, bmp280_spi_wire_t *spi);
/**
* @}
*/
/**
* @defgroup bmp280_extern_driver bmp280 extern driver function
* @brief bmp280 extern driver modules
* @ingroup bmp280_driver
* @{
*/
/**
* @brief set the chip register
* @param[in] *handle pointer to a bmp280 handle structure
* @param[in] reg iic register address
* @param[in] value data written to the register
* @return status code
* - 0 success
* - 1 write failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_set_reg(bmp280_handle_t *handle, uint8_t reg, uint8_t value);
/**
* @brief get the chip register
* @param[in] *handle pointer to a bmp280 handle structure
* @param[in] reg iic register address
* @param[out] *value pointer to a read data buffer
* @return status code
* - 0 success
* - 1 read failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t bmp280_get_reg(bmp280_handle_t *handle, uint8_t reg, uint8_t *value);
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,180 @@
/**
* @file driver_bmp280_interface.c
* @brief BMP280 driver interface for STM32 HAL (I2C1)
*/
#include "driver_bmp280_interface.h"
#include "i2c.h"
#include "usart.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
/**
* @brief interface iic bus init
* @return status code
* - 0 success
* - 1 iic init failed
* @note none
*/
uint8_t bmp280_interface_iic_init(void)
{
/* I2C is initialized in MX_I2C1_Init() */
return 0;
}
/**
* @brief interface iic bus deinit
* @return status code
* - 0 success
* - 1 iic deinit failed
* @note none
*/
uint8_t bmp280_interface_iic_deinit(void)
{
/* I2C deinit if needed */
return 0;
}
/**
* @brief interface iic bus read
* @param[in] addr iic device write address
* @param[in] reg iic register address
* @param[out] *buf pointer to a data buffer
* @param[in] len length of the data buffer
* @return status code
* - 0 success
* - 1 read failed
* @note none
*/
uint8_t bmp280_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
{
HAL_StatusTypeDef status;
/* Send register address */
status = HAL_I2C_Master_Transmit(&hi2c1, addr, &reg, 1, 1000);
if (status != HAL_OK)
{
return 1;
}
/* Read data */
status = HAL_I2C_Master_Receive(&hi2c1, addr | 0x01, buf, len, 1000);
if (status != HAL_OK)
{
return 1;
}
return 0;
}
/**
* @brief interface iic bus write
* @param[in] addr iic device write address
* @param[in] reg iic register address
* @param[in] *buf pointer to a data buffer
* @param[in] len length of the data buffer
* @return status code
* - 0 success
* - 1 write failed
* @note none
*/
uint8_t bmp280_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
{
HAL_StatusTypeDef status;
uint8_t tx_buf[256];
tx_buf[0] = reg;
for (uint16_t i = 0; i < len; i++)
{
tx_buf[i + 1] = buf[i];
}
status = HAL_I2C_Master_Transmit(&hi2c1, addr, tx_buf, len + 1, 1000);
if (status != HAL_OK)
{
return 1;
}
return 0;
}
/**
* @brief interface spi bus init
* @return status code
* - 0 success
* - 1 spi init failed
* @note none
*/
uint8_t bmp280_interface_spi_init(void)
{
return 0;
}
/**
* @brief interface spi bus deinit
* @return status code
* - 0 success
* - 1 spi deinit failed
* @note none
*/
uint8_t bmp280_interface_spi_deinit(void)
{
return 0;
}
/**
* @brief interface spi bus read
* @param[in] reg register address
* @param[out] *buf pointer to a data buffer
* @param[in] len length of data buffer
* @return status code
* - 0 success
* - 1 read failed
* @note none
*/
uint8_t bmp280_interface_spi_read(uint8_t reg, uint8_t *buf, uint16_t len)
{
return 0;
}
/**
* @brief interface spi bus write
* @param[in] reg register address
* @param[in] *buf pointer to a data buffer
* @param[in] len length of data buffer
* @return status code
* - 0 success
* - 1 write failed
* @note none
*/
uint8_t bmp280_interface_spi_write(uint8_t reg, uint8_t *buf, uint16_t len)
{
return 0;
}
/**
* @brief interface delay ms
* @param[in] ms time
* @note none
*/
void bmp280_interface_delay_ms(uint32_t ms)
{
HAL_Delay(ms);
}
/**
* @brief interface print format data
* @param[in] fmt format data
* @note none
*/
void bmp280_interface_debug_print(const char *const fmt, ...)
{
char buf[256];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
HAL_UART_Transmit(&huart2, (uint8_t*)buf, strlen(buf), 1000);
}

View File

@@ -0,0 +1,161 @@
/**
* Copyright (c) 2015 - present LibDriver All rights reserved
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file driver_bmp280_interface.h
* @brief driver bmp280 interface header file
* @version 1.0.0
* @author Shifeng Li
* @date 2024-01-15
*
* <h3>history</h3>
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2024/01/15 <td>1.0 <td>Shifeng Li <td>first upload
* </table>
*/
#ifndef DRIVER_BMP280_INTERFACE_H
#define DRIVER_BMP280_INTERFACE_H
#include "driver_bmp280.h"
#ifdef __cplusplus
extern "C"{
#endif
/**
* @defgroup bmp280_interface_driver bmp280 interface driver function
* @brief bmp280 interface driver modules
* @ingroup bmp280_driver
* @{
*/
/**
* @brief interface iic bus init
* @return status code
* - 0 success
* - 1 iic init failed
* @note none
*/
uint8_t bmp280_interface_iic_init(void);
/**
* @brief interface iic bus deinit
* @return status code
* - 0 success
* - 1 iic deinit failed
* @note none
*/
uint8_t bmp280_interface_iic_deinit(void);
/**
* @brief interface iic bus read
* @param[in] addr iic device write address
* @param[in] reg iic register address
* @param[out] *buf pointer to a data buffer
* @param[in] len length of the data buffer
* @return status code
* - 0 success
* - 1 read failed
* @note none
*/
uint8_t bmp280_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len);
/**
* @brief interface iic bus write
* @param[in] addr iic device write address
* @param[in] reg iic register address
* @param[in] *buf pointer to a data buffer
* @param[in] len length of the data buffer
* @return status code
* - 0 success
* - 1 write failed
* @note none
*/
uint8_t bmp280_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len);
/**
* @brief interface spi bus init
* @return status code
* - 0 success
* - 1 spi init failed
* @note none
*/
uint8_t bmp280_interface_spi_init(void);
/**
* @brief interface spi bus deinit
* @return status code
* - 0 success
* - 1 spi deinit failed
* @note none
*/
uint8_t bmp280_interface_spi_deinit(void);
/**
* @brief interface spi bus read
* @param[in] reg register address
* @param[out] *buf pointer to a data buffer
* @param[in] len length of data buffer
* @return status code
* - 0 success
* - 1 read failed
* @note none
*/
uint8_t bmp280_interface_spi_read(uint8_t reg, uint8_t *buf, uint16_t len);
/**
* @brief interface spi bus write
* @param[in] reg register address
* @param[in] *buf pointer to a data buffer
* @param[in] len length of data buffer
* @return status code
* - 0 success
* - 1 write failed
* @note none
*/
uint8_t bmp280_interface_spi_write(uint8_t reg, uint8_t *buf, uint16_t len);
/**
* @brief interface delay ms
* @param[in] ms time
* @note none
*/
void bmp280_interface_delay_ms(uint32_t ms);
/**
* @brief interface print format data
* @param[in] fmt format data
* @note none
*/
void bmp280_interface_debug_print(const char *const fmt, ...);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif