forked from gary/BCU
2
0
Fork 0

添加mqtt,wifi,ble,cjson,mqtt传输代码框架等

This commit is contained in:
ahu_gq 2025-04-14 18:35:28 +08:00
parent 49265f18dd
commit 6f3d0a9cd8
32 changed files with 4668 additions and 96 deletions

View File

@ -312,8 +312,8 @@ void bms_init_comm(void)
drv_uart_set_interrupt(EXTER_UART1_PORT, kUartInterrupt_Rx, APP_CFG_INT_PRIO_UART2_RX, exter_uart1_rx_it_call); drv_uart_set_interrupt(EXTER_UART1_PORT, kUartInterrupt_Rx, APP_CFG_INT_PRIO_UART2_RX, exter_uart1_rx_it_call);
drv_uart_set_interrupt(EXTER_UART2_PORT, kUartInterrupt_Rx, APP_CFG_INT_PRIO_UART2_RX, exter_uart2_rx_it_call); drv_uart_set_interrupt(EXTER_UART2_PORT, kUartInterrupt_Rx, APP_CFG_INT_PRIO_UART2_RX, exter_uart2_rx_it_call);
drv_uart_set_interrupt(SIM_UART_PORT, kUartInterrupt_Rx, APP_CFG_INT_PRIO_UART2_RX, sim_uart_rx_it_call);//#define SIM_UART_PORT kUartDev_2 //设置接收回调函数
//drv_uart_set_interrupt(SIM_UART_PORT, kUartInterrupt_Rx, APP_CFG_INT_PRIO_UART2_RX, sim_uart_rx_it_call);//#define SIM_UART_PORT kUartDev_2
drv_can_init(INTER_CAN_PORT, 250, kGpioType_Can_Ch1_Rx, kGpioType_Can_Ch1_Tx); drv_can_init(INTER_CAN_PORT, 250, kGpioType_Can_Ch1_Rx, kGpioType_Can_Ch1_Tx);
drv_can_set_interrupt(INTER_CAN_PORT, kcan_interrupt_e_Rx, APP_CFG_INT_PRIO_CAN1_RX, inter_can_rx_it_call); drv_can_set_interrupt(INTER_CAN_PORT, kcan_interrupt_e_Rx, APP_CFG_INT_PRIO_CAN1_RX, inter_can_rx_it_call);

View File

@ -50,8 +50,8 @@ const GpioArray io_array[kGpioType_End] =
kGpioMode_Comm_Rx, kGpioStatus_Low, GPIO_PORT_PIN(kGpioPort_A, 1), kGpioPort_A, kGpioPin_1, //rs485A Rx(mcu uart4) kGpioMode_Comm_Rx, kGpioStatus_Low, GPIO_PORT_PIN(kGpioPort_A, 1), kGpioPort_A, kGpioPin_1, //rs485A Rx(mcu uart4)
kGpioMode_Comm_Tx, kGpioStatus_Low, GPIO_PORT_PIN(kGpioPort_A, 0), kGpioPort_A, kGpioPin_0, //rs485A Tx(mcu uart4) kGpioMode_Comm_Tx, kGpioStatus_Low, GPIO_PORT_PIN(kGpioPort_A, 0), kGpioPort_A, kGpioPin_0, //rs485A Tx(mcu uart4)
kGpioMode_Comm_Rx, kGpioStatus_Low, GPIO_PORT_PIN(kGpioPort_A, 2), kGpioPort_A, kGpioPin_2, //SIM UART Rx(mcu uart2) kGpioMode_Comm_Rx, kGpioStatus_Low, GPIO_PORT_PIN(kGpioPort_A, 2), kGpioPort_A, kGpioPin_2, //Sim卡/wifi/ble Rx(mcu uart2)
kGpioMode_Comm_Tx, kGpioStatus_Low, GPIO_PORT_PIN(kGpioPort_A, 3), kGpioPort_A, kGpioPin_3, //SIM UART Tx(mcu uart2) kGpioMode_Comm_Tx, kGpioStatus_Low, GPIO_PORT_PIN(kGpioPort_A, 3), kGpioPort_A, kGpioPin_3, //Sim卡/wifi/ble Tx(mcu uart2)
/*CAN枚举*/ /*CAN枚举*/
kGpioMode_Comm_Rx, kGpioStatus_Low, GPIO_PORT_PIN(kGpioPort_A, 11), kGpioPort_A, kGpioPin_11, //can1 rx kGpioMode_Comm_Rx, kGpioStatus_Low, GPIO_PORT_PIN(kGpioPort_A, 11), kGpioPort_A, kGpioPin_11, //can1 rx

View File

@ -57,8 +57,8 @@ typedef enum
kGpioType_Rs485_Ch3_Rx, kGpioType_Rs485_Ch3_Rx,
kGpioType_Rs485_Ch3_Tx, kGpioType_Rs485_Ch3_Tx,
kGpioType_SIMUart_Rx, //Sim卡 kGpioType_WIRELESSUart_Rx, //Sim卡/wifi/ble
kGpioType_SIMUart_Tx, //Sim卡 kGpioType_WIRELESSUart_Tx, //Sim卡/wifi/ble
/*CAN枚举*/ /*CAN枚举*/
kGpioType_Can_Ch1_Rx, kGpioType_Can_Ch1_Rx,

View File

@ -104,7 +104,7 @@ static bool is_volt_ad_stable(int32_t *buf, uint16_t len, uint16_t aim_rate)
min = buf[i]; min = buf[i];
} }
} }
//KIT_DEBUG_PRINTF("iso ad %d %d %d\r\n", max, min, (max - min) * 10000 / min); //KIT_PRINTF("iso ad %d %d %d\r\n", max, min, (max - min) * 10000 / min);
if((min > 0) && (((int64_t)(max - min) * 10000 / min) < aim_rate)) if((min > 0) && (((int64_t)(max - min) * 10000 / min) < aim_rate))
{ {
res = true; res = true;
@ -126,13 +126,13 @@ void insu_sample_calc_insu_res(void)
{ {
if (iso_item.v2 <= iso_item.v1) if (iso_item.v2 <= iso_item.v1)
{ {
KIT_DEBUG_PRINTF("iso err v1 %d v2 %d\r\n", iso_item.v1, iso_item.v2); KIT_PRINTF("iso err v1 %d v2 %d\r\n", iso_item.v1, iso_item.v2);
iso_item.v2 = iso_item.v1 + 1; iso_item.v2 = iso_item.v1 + 1;
} }
if (iso_item.v3 >= iso_item.v1) if (iso_item.v3 >= iso_item.v1)
{ {
KIT_DEBUG_PRINTF("iso err v1 %d v3 %d\r\n", iso_item.v1, iso_item.v3); KIT_PRINTF("iso err v1 %d v3 %d\r\n", iso_item.v1, iso_item.v3);
iso_item.v3 = iso_item.v1 - 1; iso_item.v3 = iso_item.v1 - 1;
} }
@ -153,7 +153,7 @@ void insu_sample_calc_insu_res(void)
} }
else else
{ {
KIT_DEBUG_PRINTF("iso pos neg\r\n"); KIT_PRINTF("iso pos neg\r\n");
} }
tmp = 4 * v0v3 - 2 * v0v1 - v1v3; tmp = 4 * v0v3 - 2 * v0v1 - v1v3;
@ -166,7 +166,7 @@ void insu_sample_calc_insu_res(void)
} }
else else
{ {
KIT_DEBUG_PRINTF("iso neg neg\r\n"); KIT_PRINTF("iso neg neg\r\n");
} }
// iso_item.data[kIsoData_Res] = (iso_item.data[kIsoData_PosRes] > iso_item.data[kIsoData_NegRes]) ? iso_item.data[kIsoData_NegRes] : iso_item.data[kIsoData_PosRes]; // iso_item.data[kIsoData_Res] = (iso_item.data[kIsoData_PosRes] > iso_item.data[kIsoData_NegRes]) ? iso_item.data[kIsoData_NegRes] : iso_item.data[kIsoData_PosRes];
@ -187,8 +187,8 @@ void insu_sample_calc_insu_res(void)
iso_item.data[kIsoData_Res] = (iso_item.data[kIsoData_PosRes] > iso_item.data[kIsoData_NegRes]) ? iso_item.data[kIsoData_NegRes] : iso_item.data[kIsoData_PosRes]; iso_item.data[kIsoData_Res] = (iso_item.data[kIsoData_PosRes] > iso_item.data[kIsoData_NegRes]) ? iso_item.data[kIsoData_NegRes] : iso_item.data[kIsoData_PosRes];
iso_item.data[kIsoData_Value] = (iso_item.data[kIsoData_PosValue] > iso_item.data[kIsoData_NegValue]) ? iso_item.data[kIsoData_NegValue] : iso_item.data[kIsoData_PosValue]; iso_item.data[kIsoData_Value] = (iso_item.data[kIsoData_PosValue] > iso_item.data[kIsoData_NegValue]) ? iso_item.data[kIsoData_NegValue] : iso_item.data[kIsoData_PosValue];
KIT_DEBUG_PRINTF("iso %d %d %d %d\r\n", iso_item.v0, iso_item.v1, iso_item.v2, iso_item.v3); KIT_PRINTF("iso %d %d %d %d\r\n", iso_item.v0, iso_item.v1, iso_item.v2, iso_item.v3);
KIT_DEBUG_PRINTF("iso pos %d neg %d\r\n", iso_item.data[kIsoData_PosRes], iso_item.data[kIsoData_NegRes]); KIT_PRINTF("iso pos %d neg %d\r\n", iso_item.data[kIsoData_PosRes], iso_item.data[kIsoData_NegRes]);
} }
} }
} }
@ -216,13 +216,13 @@ void bms_poll_iso(uint32_t base_time)
{ {
iso_item.dly = 0; iso_item.dly = 0;
iso_item.step = kIsoStep_TotalVolt; iso_item.step = kIsoStep_TotalVolt;
KIT_DEBUG_PRINTF("iso tv \r\n"); KIT_PRINTF("iso tv \r\n");
} }
break; break;
case kIsoStep_TotalVolt: case kIsoStep_TotalVolt:
if ((is_volt_ad_stable(iso_item.ad_buf[kIsoVolt_TotalVolt], ISO_SAMPLE_CNT, ISO_STABLE_AD_RATE) == true) || (iso_item.dly >= ISO_SAMPLE_TIME_MAX)) if ((is_volt_ad_stable(iso_item.ad_buf[kIsoVolt_TotalVolt], ISO_SAMPLE_CNT, ISO_STABLE_AD_RATE) == true) || (iso_item.dly >= ISO_SAMPLE_TIME_MAX))
{ {
KIT_DEBUG_PRINTF("iso tv %d\r\n", iso_item.dly); KIT_PRINTF("iso tv %d\r\n", iso_item.dly);
ISO_S1_ON(); ISO_S1_ON();
iso_item.dly = 0; iso_item.dly = 0;
iso_item.step = kIsoStep_IsoVolt; iso_item.step = kIsoStep_IsoVolt;
@ -233,7 +233,7 @@ void bms_poll_iso(uint32_t base_time)
if (((is_volt_ad_stable(iso_item.ad_buf[kIsoVolt_Other], ISO_SAMPLE_CNT, ISO_STABLE_AD_RATE) == true) || (iso_item.dly >= ISO_SAMPLE_TIME_MAX)) if (((is_volt_ad_stable(iso_item.ad_buf[kIsoVolt_Other], ISO_SAMPLE_CNT, ISO_STABLE_AD_RATE) == true) || (iso_item.dly >= ISO_SAMPLE_TIME_MAX))
&& (iso_item.dly >= ISO_SAMPLE_TIME_MIN)) && (iso_item.dly >= ISO_SAMPLE_TIME_MIN))
{ {
KIT_DEBUG_PRINTF("iso iv %d\r\n", iso_item.dly); KIT_PRINTF("iso iv %d\r\n", iso_item.dly);
ISO_S2_ON(); ISO_S2_ON();
iso_item.dly = 0; iso_item.dly = 0;
iso_item.step = kIsoStep_PosVolt; iso_item.step = kIsoStep_PosVolt;
@ -244,7 +244,7 @@ void bms_poll_iso(uint32_t base_time)
if (((is_volt_ad_stable(iso_item.ad_buf[kIsoVolt_Other], ISO_SAMPLE_CNT, ISO_STABLE_AD_RATE) == true) || (iso_item.dly >= ISO_SAMPLE_TIME_MAX)) if (((is_volt_ad_stable(iso_item.ad_buf[kIsoVolt_Other], ISO_SAMPLE_CNT, ISO_STABLE_AD_RATE) == true) || (iso_item.dly >= ISO_SAMPLE_TIME_MAX))
&& (iso_item.dly >= ISO_SAMPLE_TIME_MIN)) && (iso_item.dly >= ISO_SAMPLE_TIME_MIN))
{ {
KIT_DEBUG_PRINTF("iso pv %d\r\n", iso_item.dly); KIT_PRINTF("iso pv %d\r\n", iso_item.dly);
ISO_S2_OFF(); ISO_S2_OFF();
ISO_S3_ON(); ISO_S3_ON();
iso_item.dly = 0; iso_item.dly = 0;
@ -257,7 +257,7 @@ void bms_poll_iso(uint32_t base_time)
if (((is_volt_ad_stable(iso_item.ad_buf[kIsoVolt_Other], ISO_SAMPLE_CNT, ISO_STABLE_AD_RATE) == true) || (iso_item.dly >= ISO_SAMPLE_TIME_MAX)) if (((is_volt_ad_stable(iso_item.ad_buf[kIsoVolt_Other], ISO_SAMPLE_CNT, ISO_STABLE_AD_RATE) == true) || (iso_item.dly >= ISO_SAMPLE_TIME_MAX))
&& (iso_item.dly >= ISO_SAMPLE_TIME_MIN)) && (iso_item.dly >= ISO_SAMPLE_TIME_MIN))
{ {
KIT_DEBUG_PRINTF("iso nv %d\r\n", iso_item.dly); KIT_PRINTF("iso nv %d\r\n", iso_item.dly);
ISO_S3_OFF(); ISO_S3_OFF();
iso_item.dly = 0; iso_item.dly = 0;
iso_item.v3 = kit_get_int32_avg_filter_max_min(iso_item.ad_buf[kIsoVolt_Other], ISO_SAMPLE_CNT); iso_item.v3 = kit_get_int32_avg_filter_max_min(iso_item.ad_buf[kIsoVolt_Other], ISO_SAMPLE_CNT);

View File

@ -14,7 +14,7 @@ int main()
{ {
__disable_irq(); __disable_irq();
KIT_DEBUG_PRINTF("bcu start \r\n"); KIT_PRINTF("bcu start \r\n");
drv_clk_set_system_tick(APP_CFG_INT_PRIO_SYSTICK, 1, ucos_irq_handler); drv_clk_set_system_tick(APP_CFG_INT_PRIO_SYSTICK, 1, ucos_irq_handler);
OS_TRACE_INIT(); OS_TRACE_INIT();

View File

@ -63,13 +63,13 @@ uint16_t bms_check_pwr_on_ocv(uint16_t soc)
uint32_t now_time = drv_rtc_get_tick(); uint32_t now_time = drv_rtc_get_tick();
uint32_t last_time = get_eeprom_data(kEep_PowerOffTimeL, kEepromDataType_Double); uint32_t last_time = get_eeprom_data(kEep_PowerOffTimeL, kEepromDataType_Double);
uint32_t standby_time = (uint32_t)get_eeprom_data(kEep_OcvStandbyCur_Time, kEepromDataType_Low) * 360; uint32_t standby_time = (uint32_t)get_eeprom_data(kEep_OcvStandbyCur_Time, kEepromDataType_Low) * 360;
KIT_DEBUG_PRINTF("OCV Last Time:%d \r\n", last_time); KIT_PRINTF("OCV Last Time:%d \r\n", last_time);
KIT_DEBUG_PRINTF("OCV Now Time:%d \r\n", now_time); KIT_PRINTF("OCV Now Time:%d \r\n", now_time);
if((standby_time != 0) && (now_time > last_time) if((standby_time != 0) && (now_time > last_time)
&& ((now_time - last_time) >= standby_time)) && ((now_time - last_time) >= standby_time))
{ {
ocv_dly = standby_time * 1000 - KIT_SECOND_CONVERT(30); ocv_dly = standby_time * 1000 - KIT_SECOND_CONVERT(30);
KIT_DEBUG_PRINTF("Power On OCV\r\n"); KIT_PRINTF("Power On OCV\r\n");
soc = bms_check_ocv(soc); soc = bms_check_ocv(soc);
} }
return soc; return soc;

View File

@ -17,7 +17,7 @@ const uint8_t tcp_server_task_name[W5500_MODBUS_NUM][14] =
void poll_1ms_task_init(void); void poll_1ms_task_init(void);
void poll_10ms_task_init(void); void poll_10ms_task_init(void);
void poll_mqtt_init(void);
bool is_first_run = false; bool is_first_run = false;
static int a = 0; static int a = 0;
@ -48,6 +48,12 @@ void bms_poll_bmu_handler(uint32_t base_time)
bms_poll_bmu(base_time); bms_poll_bmu(base_time);
} }
void task_mqtt_handler(uint32_t base_time)
{
mqtt_publish_bms_data(base_time);
}
void task_100ms_handler(uint32_t base_time) void task_100ms_handler(uint32_t base_time)
{ {
static uint32_t cout100ms = 0; static uint32_t cout100ms = 0;
@ -76,6 +82,8 @@ GLOBAL_TASK_VARS_CCM(modbus_rtu_task3, 8, 50, 5000, 400, NULL,
GLOBAL_TASK_VARS_CCM(ord_comm_can_task, 5, 100, 5000, 400, NULL, task_can1_handler, NULL); GLOBAL_TASK_VARS_CCM(ord_comm_can_task, 5, 100, 5000, 400, NULL, task_can1_handler, NULL);
GLOBAL_TASK_VARS_CCM(bms_poll_bmu_task, 9, 100, 5000, 400, bms_bmu_init, bms_poll_bmu_handler, NULL); GLOBAL_TASK_VARS_CCM(bms_poll_bmu_task, 9, 100, 5000, 400, bms_bmu_init, bms_poll_bmu_handler, NULL);
GLOBAL_TASK_VARS_CCM(bms_poll_mqtt_task, 9, 1000, 5000, 400, poll_mqtt_init, task_mqtt_handler, NULL);
#define BMU_FINISH_BIT (0u) #define BMU_FINISH_BIT (0u)
#define SIGNAL_FINISH_BIT (1u) #define SIGNAL_FINISH_BIT (1u)
@ -106,26 +114,26 @@ void poll_1ms_task_init(void)
bms_init_di_do(bmsCircuitCtrl, kDoCtrlStrategy_End, doLogicArray); bms_init_di_do(bmsCircuitCtrl, kDoCtrlStrategy_End, doLogicArray);
bsp_create_task(&poll_10ms_task, (uint8_t *)"10ms"); bsp_create_task(&poll_10ms_task, (uint8_t *)"10ms");
//KIT_DEBUG_PRINTF("cur_hv init start \r\n"); //KIT_PRINTF("cur_hv init start \r\n");
//debug here is no more run //debug here is no more run
bms_init_cur_hv(); bms_init_cur_hv();
//KIT_DEBUG_PRINTF("cur_hv init ok \r\n"); //KIT_PRINTF("cur_hv init ok \r\n");
while(KIT_GET_BIT_32(is_data_ok, BMU_FINISH_BIT) == 0) while(KIT_GET_BIT_32(is_data_ok, BMU_FINISH_BIT) == 0)
{ {
bsp_task_delay_ms(10); bsp_task_delay_ms(10);
} }
//耗时初始化 ≈300ms 确保信号正常 //耗时初始化 ≈300ms 确保信号正常
//KIT_DEBUG_PRINTF("signal init start \r\n"); //KIT_PRINTF("signal init start \r\n");
bms_poll_statistic(0); bms_poll_statistic(0);
bms_init_signal(); bms_init_signal();
//刷新信号策略 确保例如常闭等需要立即闭合do输出 //刷新信号策略 确保例如常闭等需要立即闭合do输出
task_dido_ctrl_handle(1000); task_dido_ctrl_handle(1000);
//KIT_DEBUG_PRINTF("signal init ok \r\n"); //KIT_PRINTF("signal init ok \r\n");
KIT_SET_BIT_MASK_32(is_data_ok, SIGNAL_FINISH_BIT); KIT_SET_BIT_MASK_32(is_data_ok, SIGNAL_FINISH_BIT);
bsp_create_task(&ord_ctrl_task, (uint8_t *)"ctrl"); bsp_create_task(&ord_ctrl_task, (uint8_t *)"ctrl");
@ -138,9 +146,9 @@ void poll_10ms_task_init(void)
uint8_t i = 0; uint8_t i = 0;
bms_init_fdb(); bms_init_fdb();
//耗时初始化 等待从机上电并初始化完成 ≈500ms且请求两轮数据 ≈1300ms(最大) //耗时初始化 等待从机上电并初始化完成 ≈500ms且请求两轮数据 ≈1300ms(最大)
KIT_DEBUG_PRINTF("bmu init start \r\n"); KIT_PRINTF("bmu init start \r\n");
//bms_init_chain_bmu(); //bms_init_chain_bmu();
KIT_DEBUG_PRINTF("bmu init ok \r\n"); KIT_PRINTF("bmu init ok \r\n");
bms_poll_statistic(0); bms_poll_statistic(0);
//SOC初始化需要统计数据 //SOC初始化需要统计数据
bms_init_soc(); bms_init_soc();
@ -153,7 +161,7 @@ void poll_10ms_task_init(void)
{ {
bsp_task_delay_ms(10); bsp_task_delay_ms(10);
} }
KIT_DEBUG_PRINTF("data init ok \r\n"); KIT_PRINTF("data init ok \r\n");
protocol_comm_init(); protocol_comm_init();
@ -182,7 +190,25 @@ void poll_10ms_task_init(void)
} }
void poll_mqtt_init(void)
{
static uint16_t qfc41d_init_count = 0;
static uint16_t mqtt_init_count = 0;
while(drv_qfc41d_init() && qfc41d_init_count < 10)
{
drv_wdog_feed();
kit_time_dly_ms(100);
qfc41d_init_count++;
}
// MQTT ³õʼ»¯
while(drv_mqtt_init()&& mqtt_init_count < 10)
{
drv_wdog_feed();
kit_time_dly_ms(100);
mqtt_init_count++;
}
}
/***************************************************************************** /*****************************************************************************
* @brief ucos系统中断回调 * @brief ucos系统中断回调
* @param[in] void: * @param[in] void:

View File

@ -40,7 +40,7 @@ extern "C" {
#include "hv_adc_manager.h" #include "hv_adc_manager.h"
#include "ucos_ii.h" #include "ucos_ii.h"
#include "cpu_core.h" #include "cpu_core.h"
#include "drv_wdog.h"
#include "os_cpu.h" #include "os_cpu.h"
#include "kit_debug.h" #include "kit_debug.h"
@ -48,7 +48,7 @@ extern "C" {
#include "bmu_manager.h" #include "bmu_manager.h"
#include "bmu_adbms1818.h" #include "bmu_adbms1818.h"
#include "protocol_comm.h" #include "protocol_comm.h"
#include "protocol_mqtt_bcu.h"
typedef void (*SN_BMS_INIT_CALL)(uint32_t base_time); typedef void (*SN_BMS_INIT_CALL)(uint32_t base_time);
int sn_bms_init(SN_BMS_INIT_CALL call); int sn_bms_init(SN_BMS_INIT_CALL call);

View File

@ -50,7 +50,7 @@
<InvalidFlash>1</InvalidFlash> <InvalidFlash>1</InvalidFlash>
</TargetStatus> </TargetStatus>
<OutputDirectory>.\Objects\</OutputDirectory> <OutputDirectory>.\Objects\</OutputDirectory>
<OutputName>BCU_APP2</OutputName> <OutputName>BCU_APP</OutputName>
<CreateExecutable>1</CreateExecutable> <CreateExecutable>1</CreateExecutable>
<CreateLib>0</CreateLib> <CreateLib>0</CreateLib>
<CreateHexFile>1</CreateHexFile> <CreateHexFile>1</CreateHexFile>
@ -340,7 +340,7 @@
<MiscControls></MiscControls> <MiscControls></MiscControls>
<Define>STM32F40_41xxx,APP_START_ADDR = 0x08020000,UCOS2,BSE1B,ADS_8688_EN</Define> <Define>STM32F40_41xxx,APP_START_ADDR = 0x08020000,UCOS2,BSE1B,ADS_8688_EN</Define>
<Undefine></Undefine> <Undefine></Undefine>
<IncludePath>..\app;..\..\..\library\bsp;..\..\..\library\core;..\..\..\library\kit;..\..\..\library\drv_peripheral;..\ord;..\..\..\library\drv_stm32f4xx;..\..\..\library\J1939;..\protocol;..\..\..\library\ucos\uC-OS2\Source;..\..\..\library\ucos\uC-OS2\Ports\ARM-Cortex-M\ARMv7-M\ARM;..\..\..\library\ucos\uC-OS2\Cfg\Template;..\..\..\library\ucos\uC-CPU\ARM-Cortex-M\ARMv7-M\ARM;..\..\..\library\ucos\uC-CPU\Cfg\Template;..\..\..\library\ucos\uC-CPU\Template;..\..\..\library\ucos\uC-CPU;..\..\..\library\ucos\uC-LIB;..\..\..\library\ucos\uC-LIB\Cfg\Template;..\..\..\library\ucos\uC-LIB\Ports\ARM-Cortex-M4\RealView;..\..\..\library\ucos;..\..\..\library\segger\Config;..\..\..\library\segger\Sample\uCOS-II;..\..\..\library\segger\Sample\uCOS-II\Config;..\..\..\library\segger\SEGGER;..\table</IncludePath> <IncludePath>..\app;..\..\..\library\bsp;..\..\..\library\core;..\..\..\library\kit;..\..\..\library\drv_peripheral;..\ord;..\..\..\library\drv_stm32f4xx;..\..\..\library\J1939;..\protocol;..\..\..\library\ucos\uC-OS2\Source;..\..\..\library\ucos\uC-OS2\Ports\ARM-Cortex-M\ARMv7-M\ARM;..\..\..\library\ucos\uC-OS2\Cfg\Template;..\..\..\library\ucos\uC-CPU\ARM-Cortex-M\ARMv7-M\ARM;..\..\..\library\ucos\uC-CPU\Cfg\Template;..\..\..\library\ucos\uC-CPU\Template;..\..\..\library\ucos\uC-CPU;..\..\..\library\ucos\uC-LIB;..\..\..\library\ucos\uC-LIB\Cfg\Template;..\..\..\library\ucos\uC-LIB\Ports\ARM-Cortex-M4\RealView;..\..\..\library\ucos;..\..\..\library\segger\Config;..\..\..\library\segger\Sample\uCOS-II;..\..\..\library\segger\Sample\uCOS-II\Config;..\..\..\library\segger\SEGGER;..\table;..\..\..\library\cjson</IncludePath>
</VariousControls> </VariousControls>
</Cads> </Cads>
<Aads> <Aads>
@ -638,6 +638,11 @@
<FileType>1</FileType> <FileType>1</FileType>
<FilePath>..\..\..\library\drv_peripheral\drv_eg25gminipice.c</FilePath> <FilePath>..\..\..\library\drv_peripheral\drv_eg25gminipice.c</FilePath>
</File> </File>
<File>
<FileName>drv_qfc41d.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\..\library\drv_peripheral\drv_qfc41d.c</FilePath>
</File>
</Files> </Files>
</Group> </Group>
<Group> <Group>
@ -1032,6 +1037,21 @@
<FileType>1</FileType> <FileType>1</FileType>
<FilePath>..\protocol\protocol_modbus_bcu.c</FilePath> <FilePath>..\protocol\protocol_modbus_bcu.c</FilePath>
</File> </File>
<File>
<FileName>protocol_mqtt_bcu.c</FileName>
<FileType>1</FileType>
<FilePath>..\protocol\protocol_mqtt_bcu.c</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>Cjson</GroupName>
<Files>
<File>
<FileName>cJSON.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\..\library\cjson\cJSON.c</FilePath>
</File>
</Files> </Files>
</Group> </Group>
</Groups> </Groups>
@ -1599,6 +1619,11 @@
<FileType>1</FileType> <FileType>1</FileType>
<FilePath>..\..\..\library\drv_peripheral\drv_eg25gminipice.c</FilePath> <FilePath>..\..\..\library\drv_peripheral\drv_eg25gminipice.c</FilePath>
</File> </File>
<File>
<FileName>drv_qfc41d.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\..\library\drv_peripheral\drv_qfc41d.c</FilePath>
</File>
</Files> </Files>
</Group> </Group>
<Group> <Group>
@ -1924,6 +1949,21 @@
<FileType>1</FileType> <FileType>1</FileType>
<FilePath>..\protocol\protocol_modbus_bcu.c</FilePath> <FilePath>..\protocol\protocol_modbus_bcu.c</FilePath>
</File> </File>
<File>
<FileName>protocol_mqtt_bcu.c</FileName>
<FileType>1</FileType>
<FilePath>..\protocol\protocol_mqtt_bcu.c</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>Cjson</GroupName>
<Files>
<File>
<FileName>cJSON.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\..\library\cjson\cJSON.c</FilePath>
</File>
</Files> </Files>
</Group> </Group>
</Groups> </Groups>
@ -1943,4 +1983,13 @@
</files> </files>
</RTE> </RTE>
<LayerInfo>
<Layers>
<Layer>
<LayName>BCU_APP</LayName>
<LayPrjMark>1</LayPrjMark>
</Layer>
</Layers>
</LayerInfo>
</Project> </Project>

View File

@ -0,0 +1,107 @@
/******************************************************************************
* @file protocol_mqtt_bcu.c
* @brief protocol_mqtt_bcu
* @version V1.0
* @author Gary
* @copyright
******************************************************************************/
#include "protocol_mqtt_bcu.h"
//把这个请按照数据模型填充完毕
const devPointMap bms_points[] = {
{"BCU_2", 0, 0, NULL}, // 总告警
{"BCU_3", 0, 0, NULL}, // 总故障
{"BCU_4", 0, 0, NULL}, // 单体过压告警一级
{"BCU_5", 0, 0, NULL}, // 单体欠压告警一级
{"BCU_6", 0, 0, NULL}, // 单体过温告警一级
{"BCU_7", 0, 0, NULL}, // 单体低温告警一级
{"BCU_8", 0, 0, NULL}, // 单体压差告警一级
{"BCU_9", 0, 0, NULL}, // 充电过流告警一级
};
const int bms_point_count = sizeof(bms_points) / sizeof(bms_points[0]);
void protocol_build_json(uint16_t groupId)
{
int val = 0;
cJSON* root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "timeStamp", drv_rtc_get_tick());
// devData 数组
cJSON* devDataArr = cJSON_CreateArray();
cJSON_AddItemToObject(root, "devData", devDataArr);
// 构建每一个设备项
cJSON* deviceObj = cJSON_CreateObject();
cJSON_AddItemToArray(devDataArr, deviceObj);
cJSON_AddStringToObject(deviceObj, "devType", "4");
cJSON_AddStringToObject(deviceObj, "devName", "BCU");
cJSON_AddStringToObject(deviceObj, "devId", "001");
cJSON_AddStringToObject(deviceObj, "sn", "SN123456");
// 构建 data 对象
cJSON* dataObj = cJSON_CreateObject();
cJSON_AddItemToObject(deviceObj, "data", dataObj);
for (int i = 0; i < bms_point_count; ++i)
{
const devPointMap* point = &bms_points[i];
if(point->groupId == groupId)
{
if (bms_points[i].get_val != NULL)
{
val = point->get_val(point->input);
}
else
{
val = 0; // 默认值
}
}
cJSON_AddNumberToObject(dataObj, point->key, val);
}
// 转为 JSON 字符串
char* json_str = cJSON_PrintUnformatted(root);
if (json_str)
{
drv_mqtt_publish(json_str, strlen(json_str));
cJSON_free(json_str);
}
cJSON_Delete(root);
}
void publish_all_bms_groups(void) {
uint8_t max_group = 0;
for (size_t i = 0; i < bms_point_count; i++) {
if (bms_points[i].groupId > max_group) {
max_group = bms_points[i].groupId;
}
}
for (uint8_t g = 0; g <= max_group; g++) {
protocol_build_json(g);
}
}
void mqtt_publish_bms_data(uint32_t basetime)
{
static uint32_t mqtt_cycle_tick = 0;
static uint8_t groupMax = 0;
uint16_t i = 0;
mqtt_cycle_tick += basetime;
if(mqtt_cycle_tick > 60000)
{
mqtt_cycle_tick = 0;
publish_all_bms_groups();
}
}

View File

@ -0,0 +1,93 @@
/******************************************************************************
* @file protocol_mqtt_bcu.c
* @brief protocol_mqtt_bcu
* @version V1.0
* @author Gary
* @copyright
******************************************************************************/
#include "protocol_mqtt_bcu.h"
const devPointMap bms_points[] = {
{"BCU_2", NULL}, // 总告警
{"BCU_3", NULL}, // 总故障
{"BCU_4", NULL}, // 单体过压告警一级
{"BCU_5", NULL}, // 单体欠压告警一级
{"BCU_6", NULL}, // 单体过温告警一级
{"BCU_7", NULL}, // 单体低温告警一级
{"BCU_8", NULL}, // 单体压差告警一级
{"BCU_9", NULL}, // 充电过流告警一级
};
const int bms_point_count = sizeof(bms_points) / sizeof(bms_points[0]);
char* protocol_build_json(void)
{
cJSON* root = cJSON_CreateObject();
if (!root) return NULL;
// 时间戳字段(此处你需替换成实际时间函数)
cJSON_AddNumberToObject(root, "timeStamp", drv_rtc_get_tick());
// devData 数组
cJSON* devDataArr = cJSON_CreateArray();
cJSON_AddItemToObject(root, "devData", devDataArr);
// 构建每一个设备项
cJSON* deviceObj = cJSON_CreateObject();
cJSON_AddItemToArray(devDataArr, deviceObj);
cJSON_AddStringToObject(deviceObj, "devType", "BMS");
cJSON_AddStringToObject(deviceObj, "devName", "BCU");
cJSON_AddStringToObject(deviceObj, "devId", "001");
cJSON_AddStringToObject(deviceObj, "sn", "SN123456");
// 构建 data 对象
cJSON* dataObj = cJSON_CreateObject();
cJSON_AddItemToObject(deviceObj, "data", dataObj);
for (int i = 0; i < bms_point_count; ++i)
{
const char* key = bms_points[i].key;
int val = 0;
if (bms_points[i].get_val != NULL)
{
val = bms_points[i].get_val();
}
else
{
val = 0; // 默认值
}
cJSON_AddNumberToObject(dataObj, key, val);
}
// 转为 JSON 字符串
char* json_str = cJSON_PrintUnformatted(root); // 使用 Unformatted 避免换行
cJSON_Delete(root);
return json_str;
}
void mqtt_publish_bms_data(uint16_t basetime)
{
static uint32_t mqtt_cycle_tick = 0;
mqtt_cycle_tick += basetime;
if(mqtt_cycle_tick > 60000)
{
mqtt_cycle_tick = 0;
char* json_str = protocol_build_json();
if (json_str == NULL)
{
return;
}
drv_mqtt_publish(json_str, strlen(json_str));
cJSON_free(json_str);
}
}

View File

@ -0,0 +1,38 @@
/******************************************************************************
* @file protocol_mqtt_bcu.h
* @brief protocol_mqtt_bcu.h
* @version V1.0
* @author Gary
* @copyright
******************************************************************************/
#ifndef PROTOCOL_MQTT_BCU_H_
#define PROTOCOL_MQTT_BCU_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "cJSON.h"
#include "drv_qfc41d.h"
#include "drv_rtc.h"
typedef struct {
const char* key; // JSON 中的键,比如 "BCU_2"
uint8_t groupId; // mqtt因为json长度限制这里面用于分组传输,从0开始
uint16_t input; // get_val的入参用于获取key对应的数据
uint32_t (*get_val)(uint16_t); // 对应值的获取函数
} devPointMap;
extern const devPointMap bms_points[];
void mqtt_publish_bms_data(uint32_t basetime);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,36 @@
/******************************************************************************
* @file protocol_mqtt_bcu.h
* @brief protocol_mqtt_bcu.h
* @version V1.0
* @author Gary
* @copyright
******************************************************************************/
#ifndef PROTOCOL_MQTT_BCU_H_
#define PROTOCOL_MQTT_BCU_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "cJSON.h"
#include "drv_qfc41d.h"
#include "drv_rtc.h"
typedef struct {
const char* key; // JSON 中的键,比如 "BCU_2"
uint32_t (*get_val)(uint16_t); // 对应值的获取函数
} devPointMap;
extern const devPointMap bms_points[];
void mqtt_publish_bms_data(uint32_t basetime);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -110,7 +110,7 @@ void check_error(int error)
{ {
if (error == -1) if (error == -1)
{ {
KIT_DEBUG_PRINTF("A PEC error was detected in the received data"); KIT_PRINTF("A PEC error was detected in the received data");
} }
} }
@ -124,30 +124,30 @@ void print_cells(uint8_t datalog_en)
{ {
if (datalog_en == 0) if (datalog_en == 0)
{ {
KIT_DEBUG_PRINTF(" IC "); KIT_PRINTF(" IC ");
// KIT_DEBUG_PRINTF(current_ic + 1, DEC); // KIT_PRINTF(current_ic + 1, DEC);
KIT_DEBUG_PRINTF(", "); KIT_PRINTF(", ");
for (int i = 0; i < BMS_IC[0].ic_reg.cell_channels; i++) for (int i = 0; i < BMS_IC[0].ic_reg.cell_channels; i++)
{ {
KIT_DEBUG_PRINTF(" C"); KIT_PRINTF(" C");
// KIT_DEBUG_PRINTF(i + 1, DEC); // KIT_PRINTF(i + 1, DEC);
// KIT_DEBUG_PRINTF(":"); // KIT_PRINTF(":");
// KIT_DEBUG_PRINTF(BMS_IC[current_ic].cells.c_codes[i] * 0.0001, 4); // KIT_PRINTF(BMS_IC[current_ic].cells.c_codes[i] * 0.0001, 4);
KIT_DEBUG_PRINTF(","); KIT_PRINTF(",");
} }
KIT_DEBUG_PRINTF("\n"); KIT_PRINTF("\n");
} }
else else
{ {
KIT_DEBUG_PRINTF(" Cells, "); KIT_PRINTF(" Cells, ");
for (int i = 0; i < BMS_IC[0].ic_reg.cell_channels; i++) for (int i = 0; i < BMS_IC[0].ic_reg.cell_channels; i++)
{ {
// KIT_DEBUG_PRINTF(BMS_IC[current_ic].cells.c_codes[i] * 0.0001, 4); // KIT_PRINTF(BMS_IC[current_ic].cells.c_codes[i] * 0.0001, 4);
KIT_DEBUG_PRINTF(","); KIT_PRINTF(",");
} }
} }
} }
KIT_DEBUG_PRINTF("\n"); KIT_PRINTF("\n");
} }
void ADBMS1818_reset_crc_count(uint8_t total_ic, cell_asic *ic) void ADBMS1818_reset_crc_count(uint8_t total_ic, cell_asic *ic)

View File

@ -222,7 +222,7 @@ bool bsp_boot_move_firmware(BootItem *item, uint32_t start_addr, uint32_t max_le
{ {
drv_flash_read_u32(item->flash_item, item->back_up_addr + APP_CFG_BOOT_FW_LENGTH_OFFSET, &data); drv_flash_read_u32(item->flash_item, item->back_up_addr + APP_CFG_BOOT_FW_LENGTH_OFFSET, &data);
KIT_ASSERT_PARAM(data < (max_len - APP_CFG_BOOT_FW_DATA_OFFSET)); KIT_ASSERT_PARAM(data < (max_len - APP_CFG_BOOT_FW_DATA_OFFSET));
KIT_DEBUG_PRINTF("GUIDE:need update\r\n"); KIT_PRINTF("GUIDE:need update\r\n");
if((data < (max_len - APP_CFG_BOOT_FW_DATA_OFFSET)) && (kit_check_crc16((uint8_t *)(item->back_up_addr + APP_CFG_BOOT_FW_DATA_OFFSET), data) == 0)) if((data < (max_len - APP_CFG_BOOT_FW_DATA_OFFSET)) && (kit_check_crc16((uint8_t *)(item->back_up_addr + APP_CFG_BOOT_FW_DATA_OFFSET), data) == 0))
{ {
bsp_flash_erase_page(item->flash_item, start_addr, data); bsp_flash_erase_page(item->flash_item, start_addr, data);
@ -243,7 +243,7 @@ bool bsp_boot_move_firmware(BootItem *item, uint32_t start_addr, uint32_t max_le
} }
else else
{ {
KIT_DEBUG_PRINTF("GUIDE:crc err\r\n"); KIT_PRINTF("GUIDE:crc err\r\n");
} }
} }
} }

View File

@ -83,7 +83,7 @@ void bsp_task_beat_wdog(void)
{ {
//task_item.reset_call(i); //task_item.reset_call(i);
cur_task->is_time_out = true; cur_task->is_time_out = true;
KIT_DEBUG_PRINTF("wdog timeout task id %d \r\n", cur_task->task_id); KIT_PRINTF("wdog timeout task id %d \r\n", cur_task->task_id);
} }
// is_timeout = true; // is_timeout = true;
} }
@ -131,7 +131,7 @@ kit_ret_e bsp_create_task(task_array_t* task, uint8_t * name)
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
OSTaskNameSet(task->task_id, name, &res); OSTaskNameSet(task->task_id, name, &res);
KIT_DEBUG_PRINTF("task creat %s \r\n", name); KIT_PRINTF("task creat %s \r\n", name);
#else #else
res = OSTaskCreate(_task_template, (void *)task, &task->stk_array[task->stk_size - 1], task->task_id); res = OSTaskCreate(_task_template, (void *)task, &task->stk_array[task->stk_size - 1], task->task_id);
#endif #endif

3164
library/cjson/cJSON.c Normal file

File diff suppressed because it is too large Load Diff

306
library/cjson/cJSON.h Normal file
View File

@ -0,0 +1,306 @@
/*
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.
*/
#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 symbol
For *nix builds that support visibility attribute, you can define similar behavior by
setting default visibility to hidden by adding
-fvisibility=hidden (for gcc)
or
-xldscope=hidden (for sun cc)
to CFLAGS
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
*/
#define CJSON_CDECL __cdecl
#define CJSON_STDCALL __stdcall
/* export symbols by default, this is necessary for copy pasting the C and header file */
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
#define CJSON_EXPORT_SYMBOLS
#endif
#if defined(CJSON_HIDE_SYMBOLS)
#define CJSON_PUBLIC(type) type CJSON_STDCALL
#elif defined(CJSON_EXPORT_SYMBOLS)
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
#elif defined(CJSON_IMPORT_SYMBOLS)
#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
#endif
#else /* !__WINDOWS__ */
#define CJSON_CDECL
#define CJSON_STDCALL
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
#else
#define CJSON_PUBLIC(type) type
#endif
#endif
/* project version */
#define CJSON_VERSION_MAJOR 1
#define CJSON_VERSION_MINOR 7
#define CJSON_VERSION_PATCH 18
#include <stddef.h>
/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False (1 << 0)
#define cJSON_True (1 << 1)
#define cJSON_NULL (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw (1 << 7) /* raw json */
#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;
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
} cJSON;
typedef struct cJSON_Hooks
{
/* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
void *(CJSON_CDECL *malloc_fn)(size_t sz);
void (CJSON_CDECL *free_fn)(void *ptr);
} cJSON_Hooks;
typedef int cJSON_bool;
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
* This is to prevent stack overflows. */
#ifndef CJSON_NESTING_LIMIT
#define CJSON_NESTING_LIMIT 1000
#endif
/* Limits the length of circular references can be before cJSON rejects to parse them.
* This is to prevent stack overflows. */
#ifndef CJSON_CIRCULAR_LIMIT
#define CJSON_CIRCULAR_LIMIT 10000
#endif
/* returns the version of cJSON as a string */
CJSON_PUBLIC(const char*) cJSON_Version(void);
/* Supply malloc, realloc and free functions to cJSON */
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated);
/* 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);
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
/* 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);
/* Check item type and return its value */
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const 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_IsNull(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. */
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);
/* Create a string where valuestring references a string so
* it will not be freed by cJSON_Delete */
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
/* Create an object/array that only references it's elements so
* they will not be freed by cJSON_Delete */
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
/* These utilities create an Array of count items.
* The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/
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 *const *strings, int count);
/* Append item to the specified array/object. */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
* writing to `item->string` */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
/* Remove/Detach items from Arrays/Objects. */
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
/* Update array items. */
CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
/* Duplicate a cJSON item */
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
* need to be released. With recurse!=0, it will duplicate any children connected to the item.
* The item->next and ->prev pointers are always zero on return from Duplicate. */
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
* The input pointer json cannot point to a read-only address area, such as a string constant,
* but should point to a readable and writable address area. */
CJSON_PUBLIC(void) cJSON_Minify(char *json);
/* Helper functions for creating and adding items to an object at the same time.
* They return the added item or NULL on failure. */
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);
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
/* helper for the cJSON_SetNumberValue macro */
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/
#define cJSON_SetBoolValue(object, boolValue) ( \
(object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \
(object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \
cJSON_Invalid\
)
/* Macro for iterating over an array or object */
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
CJSON_PUBLIC(void) cJSON_free(void *object);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -108,14 +108,14 @@ void drv_mqtt_connect(void)
drv_send_at_cmd("AT+QMTOPEN=0,\"mqtt.example.com\",1883", 5000); drv_send_at_cmd("AT+QMTOPEN=0,\"mqtt.example.com\",1883", 5000);
drv_send_at_cmd("AT+QMTCONN=0,\"client_id\",\"username\",\"password\"", 5000); drv_send_at_cmd("AT+QMTCONN=0,\"client_id\",\"username\",\"password\"", 5000);
} }
/*
void drv_mqtt_publish(const char *topic, const char *message) void drv_mqtt_publish(const char *topic, const char *message)
{ {
char cmd[128]; char cmd[128];
sprintf(cmd, "AT+QMTPUB=0,0,0,0,\"%s\",\"%s\"", topic, message); sprintf(cmd, "AT+QMTPUB=0,0,0,0,\"%s\",\"%s\"", topic, message);
drv_send_at_cmd(cmd, 500); drv_send_at_cmd(cmd, 500);
} }
*/
/* /*

View File

@ -50,7 +50,7 @@ void drv_uart2_sendString(const char *str);
void drv_send_at_cmd(const char *cmd, int delay_ms); void drv_send_at_cmd(const char *cmd, int delay_ms);
void drv_eg25g_init(void); void drv_eg25g_init(void);
void drv_mqtt_connect(void); void drv_mqtt_connect(void);
void drv_mqtt_publish(const char *topic, const char *message); //void drv_mqtt_publish(const char *topic, const char *message);
void drv_sim_push_data(Lte4GItem* item, uint8_t *buf, uint16_t len); void drv_sim_push_data(Lte4GItem* item, uint8_t *buf, uint16_t len);
@ -58,3 +58,4 @@ void drv_sim_push_data(Lte4GItem* item, uint8_t *buf, uint16_t len);
#endif //DRV_EG25GMINIPICE_H_ #endif //DRV_EG25GMINIPICE_H_

View File

@ -0,0 +1,313 @@
/******************************************************************************
* @file drv_qfc41d.c
* @brief drv_qfc41d driver
* @version V1.0
* @author Gary
* @copyright
******************************************************************************/
#include "drv_qfc41d.h"
uint8_t rev_buff[QFC41D_MAX_RECV_SIZE] = {0};
uint8_t send_buff[QFC41D_MAX_SEND_SIZE] = {0};
uint8_t wifiName[40] = {0}, wifiPassWord[40] = {0}, bleName[40] = {0};
char cmd[100] = {0}; //降低栈空间使用
//定义
WifiBleItem qfc41dItem = {
.dev = QFC41D_UART_PORT,
.buf_pos = 0,
.buf_size = QFC41D_MAX_RECV_SIZE,
.buf = rev_buff,
.sendCall = NULL,
};
static void drv_qfc41d_push_data(WifiBleItem* item, uint8_t *buf, uint16_t len)
{
uint16_t i = 0;
if((item != NULL) && (item->buf_pos + len < item->buf_size))
{
item->buf[item->buf_pos++] = buf[i];
// 处理换行符,表示一条完整的 AT 指令返回
if (buf[i] == '\n')
{
item->buf[item->buf_pos] = '\0'; // 添加结束符
item->buf_pos = 0; // 重置索引,准备下一条数据
}
}
}
static void drv_qf41d_uart_rx_it_call(kit_ret_e res, void *data)
{
OS_TRACE_ISR_ENTER();
drv_qfc41d_push_data(&qfc41dItem, data, 1);
OS_TRACE_ISR_EXIT();
}
// 发送单个字符
void drv_wireless_send_char(char c)
{
while (!(USART2->SR & USART_SR_TXE)); // 等待发送缓冲区空
USART2->DR = c;
}
// 发送字符串
void drv_wireless_send_string(const char *str)
{
while (*str)
{
drv_wireless_send_char(*str++);
}
drv_wireless_send_char('\r'); // 发送 CR
drv_wireless_send_char('\n'); // 发送 LF
}
// 发送 AT 指令并等待返回值
int drv_at_send_cmd(const char *cmd, const char *response, uint32_t timeout)
{
memset(qfc41dItem.buf, 0, QFC41D_MAX_RECV_SIZE);
qfc41dItem.buf_pos = 0; // 清空接收缓冲区索引
drv_wireless_send_string(cmd);
uint32_t start_time = kit_time_get_tick(); // 获取当前时间
while (kit_time_get_tick() - start_time < timeout)
{
if (strstr((char *)qfc41dItem.buf, response))
{
return 1; // 成功匹配返回值
}
}
return 0; // 超时未收到期望数据
}
static uint8_t drv_wifi_init()
{
// 1. 设置模组波特率
if (!drv_at_send_cmd(QFC41D_SET_BATE, "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("WiFi Module not responding!\r\n");
return 1 ;
}
// 2. 测试 AT 指令
if (!drv_at_send_cmd("AT", "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("WiFi Module not responding!\r\n");
return 1 ;
}
// 3. 设置 Wi-Fi 为 DHCP使能
if (!drv_at_send_cmd(QFC41D_SET_DHCP_ENABLE, "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to set WiFi mode!\r\n");
return 1;
}
// 4. 连接 Wi-Fi
snprintf(cmd, sizeof(cmd), "AT+QSTAAPINFODEF=%s,%s", wifiName, wifiPassWord);
if (!drv_at_send_cmd(cmd, "WIFI CONNECTED", WIFI_TIMEOUT * 2))
{
KIT_PRINTF("Failed to connect to WiFi!\r\n");
//return 1; //错误不返回-防止影响主任务执行
}
// 5. 获取 IP 地址
if (!drv_at_send_cmd("AT+QGETIP=station", "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to get IP address!\r\n");
//return 1; //错误不返回-防止影响主任务执行
}
KIT_PRINTF("WiFi Initialization Successful!\r\n");
return 0;
}
static uint8_t drv_ble_init()
{
// 1. 初始化,蓝牙作为服务器
if (!drv_at_send_cmd("AT+QBLEINIT=2", "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to power off BLE!\r\n");
return 1;
}
// 2. 设置蓝牙名称
snprintf(cmd, sizeof(cmd), "AT+QBLENAME=%s", bleName);
if (!drv_at_send_cmd(cmd, "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to set BLE name!\r\n");
return 1;
}
// 3. 设置广播参数
if (!drv_at_send_cmd("AT+QBLEADVPARAM=2048,2048", "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to enable BLE!\r\n");
return 1;
}
// 4. 设置广播数据
if (!drv_at_send_cmd("AT+QBLEADVDATA=06094643343144", "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to enable BLE!\r\n");
return 1;
}
// 5. 创建服务
if (!drv_at_send_cmd("AT+QBLEGATTSSRV=FFF0", "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to set BLE role!\r\n");
return 1;
}
// 6. 创建特征值
if (!drv_at_send_cmd("AT+QBLEGATTSSRV=FFF1", "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to set BLE role!\r\n");
return 1;
}
// 7. 开始广播
if (!drv_at_send_cmd("AT+QBLEADVSTART", "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to start BLE advertising!\r\n");
return 1;
}
KIT_PRINTF("BLE Initialization Successful!\r\n");
return 0;
}
uint8_t drv_qfc41d_init(void)
{
//初始化串口
drv_uart_init((UartDev)qfc41dItem.dev, 115200, 0 | UART_CFG_STOP_BIT_1, kGpioType_WIRELESSUart_Tx, kGpioType_WIRELESSUart_Rx);
//设置接收回调函数
drv_uart_set_interrupt(QFC41D_UART_PORT, kUartInterrupt_Rx, APP_CFG_INT_PRIO_UART2_RX, drv_qf41d_uart_rx_it_call);//#define SIM_UART_PORT kUartDev_2
//初始化wifi
if(drv_wifi_init() != 0)
{
return 1;
}
/* 蓝牙预留
//初始化蓝牙BLE
if (drv_ble_init() != 0)
{
return 1;
}
*/
return 0;
}
#define MQTT_BROKER "mqtt.example.com" // 你的 MQTT 服务器地址
#define MQTT_PORT 1883 // MQTT 端口
#define MQTT_CLIENT_ID "fc41d_client" // 客户端 ID
#define MQTT_USER "user" // MQTT 用户名
#define MQTT_PASSWORD "password" // MQTT 密码
#define MQTT_TOPIC "fc41d/topic" // 订阅/发布的主题
#define MQTT_MESSAGE "Hello from FC41D!" // 发送的消息
// 连接 MQTT 服务器
static uint8_t drv_mqtt_connect()
{
//1、设置 MQTT 协议的版本号为 V4
snprintf(cmd, sizeof(cmd), "AT+QMTCFG=\"version\",1,4");
if (!drv_at_send_cmd(cmd, "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to set MQTT server!\r\n");
return 1;
}
//2、建立 MQTT 客户端与服务器之间的会话连接
snprintf(cmd, sizeof(cmd), "AT+QMTOPEN=1,\"%s\",%d",MQTT_BROKER,MQTT_PORT);
if (!drv_at_send_cmd(cmd, "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to set MQTT server!\r\n");
return 1;
}
//2、MQTT 客户端与服务器之间建立 MQTT 连接 AT+QMTCONN=<clientID>,<client_identity>,<username>,<password>
snprintf(cmd, sizeof(cmd), "AT+QMTCONN=1,%s,%s,%s",MQTT_CLIENT_ID,MQTT_USER, MQTT_PASSWORD);
if (!drv_at_send_cmd(cmd, "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to set MQTT client credentials!\r\n");
return 1;
}
KIT_PRINTF("MQTT Connection Successful!\r\n");
return 0;
}
// 订阅 MQTT 主题
static uint8_t drv_mqtt_subscribe()
{
snprintf(cmd, sizeof(cmd), "AT+MQTTSUB=1,1,\"%s\",0", MQTT_TOPIC);
if (!drv_at_send_cmd(cmd, "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to subscribe to topic!\r\n");
return 1;
}
KIT_PRINTF("Subscribed to MQTT topic successfully!\r\n");
return 0;
}
// 发布 MQTT 消息
/*
* AT+QMTPUB=<clientID>,<msgID>,<QoS>,<retain>,<topic>,<payload_len gth>,<payload>
* length 0~1500
* str mqtt传输的json串
*/
uint8_t drv_mqtt_publish(char* str,uint16_t length)
{
snprintf(cmd, sizeof(cmd), "AT+QMTPUB=1,1,0,0,%s,%d,%s",MQTT_TOPIC, length,str);
if (!drv_at_send_cmd(cmd, "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to publish message!\r\n");
return 1;
}
KIT_PRINTF("MQTT Message Published Successfully!\r\n");
return 0;
}
// MQTT 初始化
uint8_t drv_mqtt_init()
{
if (drv_mqtt_connect() != 0)
{
return 1;
}
if (drv_mqtt_subscribe() != 0)
{
return 1;
}
return 0;
}

View File

@ -0,0 +1,150 @@
/******************************************************************************
* @file drv_qfc41d.c
* @brief drv_qfc41d driver
* @version V1.0
* @author Gary
* @copyright
******************************************************************************/
#include "drv_qfc41d.h"
uint8_t rev_buff[QFC41D_MAX_RECV_SIZE] = {0};
uint8_t send_buff[QFC41D_MAX_SEND_SIZE] = {0};
uint8_t wifiName[40] = {0}, wifiPassWord[40] = {0};
//定义
WifiBleItem qfc41dItem = {
.dev = QFC41D_UART_PORT,
.buf_pos = 0,
.buf_size = QFC41D_MAX_RECV_SIZE,
.buf = rev_buff,
.sendCall = NULL,
};
static void drv_qfc41d_push_data(WifiBleItem* item, uint8_t *buf, uint16_t len)
{
uint16_t i = 0;
if((item != NULL) && (item->buf_pos + len < item->buf_size))
{
item->buf[item->buf_pos++] = buf[i];
// 处理换行符,表示一条完整的 AT 指令返回
if (buf[i] == '\n')
{
item->buf[item->buf_pos] = '\0'; // 添加结束符
item->buf_pos = 0; // 重置索引,准备下一条数据
}
}
}
static void qf41d_uart_rx_it_call(kit_ret_e res, void *data)
{
OS_TRACE_ISR_ENTER();
drv_qfc41d_push_data(&qfc41dItem, data, 1);
OS_TRACE_ISR_EXIT();
}
// 发送单个字符
void drv_wireless_send_char(char c)
{
while (!(USART2->SR & USART_SR_TXE)); // 等待发送缓冲区空
USART2->DR = c;
}
// 发送字符串
void drv_wireless_send_string(const char *str)
{
while (*str)
{
drv_wireless_send_char(*str++);
}
drv_wireless_send_char('\r'); // 发送 CR
drv_wireless_send_char('\n'); // 发送 LF
}
// 发送 AT 指令并等待返回值
int drv_wifi_send_cmd(const char *cmd, const char *response, uint32_t timeout)
{
memset(qfc41dItem.buf, 0, QFC41D_MAX_RECV_SIZE);
qfc41dItem.buf_pos = 0; // 清空接收缓冲区索引
drv_wireless_send_string(cmd);
uint32_t start_time = kit_time_get_tick(); // 获取当前时间
while (kit_time_get_tick() - start_time < timeout)
{
if (strstr((char *)qfc41dItem.buf, response))
{
return 1; // 成功匹配返回值
}
}
return 0; // 超时未收到期望数据
}
static uint8_t drv_wifi_init()
{
// 1. 测试 AT 指令
if (!drv_wifi_send_cmd("AT", "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("WiFi Module not responding!\r\n");
return 1 ;
}
// 2. 设置 Wi-Fi 为 STA 模式
if (!drv_wifi_send_cmd("AT+CWMODE=1", "OK", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to set WiFi mode!\r\n");
return 1;
}
// 3. 连接 Wi-Fi
char cmd[128];
snprintf(cmd, sizeof(cmd), "AT+CWJAP=\"%s\",\"%s\"", wifiName, wifiPassWord);
if (!drv_wifi_send_cmd(cmd, "WIFI CONNECTED", WIFI_TIMEOUT * 2))
{
KIT_PRINTF("Failed to connect to WiFi!\r\n");
//return 1; //错误不返回-防止影响主任务执行
}
// 4. 获取 IP 地址
if (!drv_wifi_send_cmd("AT+CIFSR", ".", WIFI_TIMEOUT))
{
KIT_PRINTF("Failed to get IP address!\r\n");
//return 1; //错误不返回-防止影响主任务执行
}
KIT_PRINTF("WiFi Initialization Successful!\r\n");
return 0;
}
uint8_t drv_qfc41d_init(void)
{
//初始化串口
drv_uart_init((UartDev)qfc41dItem.dev, 115200, 0 | UART_CFG_STOP_BIT_1, kGpioType_WIRELESSUart_Tx, kGpioType_WIRELESSUart_Rx);
//设置接收回调函数
drv_uart_set_interrupt(QFC41D_UART_PORT, kUartInterrupt_Rx, APP_CFG_INT_PRIO_UART2_RX, qf41d_uart_rx_it_call);//#define SIM_UART_PORT kUartDev_2
//初始化wifi
if(drv_wifi_init() != 0)
{
return 1;
` }
//初始化蓝牙
return 0;
}

View File

@ -0,0 +1,69 @@
/******************************************************************************
* @file drv_qfc41d.h
* @brief drv_qfc41d.h
* @version V1.0
* @author Gary
* @copyright
******************************************************************************/
#ifndef DRV_QFC41D_H_
#define DRV_QFC41D_H_
#include <stdio.h>
#include <stdint.h>
#include <stm32f4xx.h>
#include <string.h>
#include "drv_uart.h"
#include "kit_macro.h"
#include "ucos_ii.h"
#include "gpio_manager.h"
#include "kit_time.h"
#include "kit_debug.h"
#define QFC41D_UART_PORT kUartDev_2
#define QFC41D_MAX_SEND_SIZE 2048
#define QFC41D_MAX_RECV_SIZE 200
#define WIFI_TIMEOUT 100
#define QFC41D_RESTART_CMD "AT+QRST"
#define QFC41D_PING_CMD "AT+QPING=<IP>"
#define QFC41D_LOW_POWER_MODE "AT+QLOWPOWER"
#define QFC41D_GET_MACK_ADDR "AT+QWLMAC"
#define QFC41D_GET_WIFISTA_STATUS "AT+QSTAST"
#define QFC41D_SET_DHCP_ENABLE "AT+QSTADHCPDEF=1"
//该命令用于设置串口波特率:AT+QSETBAND=<baud_rate>[,<save>] <baud_rate> 整型,串口波特率。<save>省略该参数则保存波特率。0:不保存波特率 1:保存波特率
#define QFC41D_SET_BATE "AT+QSETBAND=115200,1"
/* 下面命令用于模块作为外围设备时发送 GATT 数据
* AT+QBLEGATTSNTFY=<UUID>[,<hex_lngth>],<data>
* <UUID> UUID 2 16
* <hex_length> 使<data> <data> 123456 0x123456
* <data> GATT
*/
#define QFC41D_BLE_INIT "AT+QBLEINIT=2"
typedef void (*FC41DSendCall)(UartDev dev, uint8_t *buf, uint16_t len);
typedef struct
{
uint8_t dev;
uint16_t buf_pos;
uint16_t buf_size;
uint8_t *buf;
FC41DSendCall sendCall;
}WifiBleItem;
uint8_t drv_qfc41d_init(void);
// MQTT 初始化
uint8_t drv_mqtt_init();
// 发布 MQTT 消息
uint8_t drv_mqtt_publish(char* str,uint16_t length);
#endif //DRV_QFC41D_H_

View File

@ -61,7 +61,7 @@ void drv_w5500_init(W5500Item *item, uint32_t ip, uint16_t *port_array, uint32_t
#ifdef W5500_DEBUG #ifdef W5500_DEBUG
drv_w5500_read_comm_reg(item, W5500_GAR0_REG_0x01, buf, 4 + 4 + 6 + 4); drv_w5500_read_comm_reg(item, W5500_GAR0_REG_0x01, buf, 4 + 4 + 6 + 4);
KIT_MODULE_PRINTF(W5500_PRT_EN, "w5500 param:"); KIT_MODULE_PRINTF(W5500_PRT_EN, "w5500 param:");
KIT_DEBUG_PRINTF_U8_BUF(buf, 4 + 4 + 6 + 4); KIT_PRINTF_U8_BUF(buf, 4 + 4 + 6 + 4);
#endif #endif
w5500_semaphore = OSSemCreate(1); w5500_semaphore = OSSemCreate(1);
} }

View File

@ -76,7 +76,7 @@ void PVD_IRQHandler(void)
RCC->APB1ENR = 0; RCC->APB1ENR = 0;
pwr_off_call(); pwr_off_call();
// KIT_DEBUG_PRINTF("%s\r\n", "Poweroff"); // KIT_PRINTF("%s\r\n", "Poweroff");
} }
} }

View File

@ -60,7 +60,7 @@ bool drv_rtc_init(void)
uint32_t dly = 0; uint32_t dly = 0;
//if((RCC->BDCR & RCC_BDCR_RTCEN_POS) != RCC_BDCR_RTCEN_POS) //if((RCC->BDCR & RCC_BDCR_RTCEN_POS) != RCC_BDCR_RTCEN_POS)
{ {
KIT_DEBUG_PRINTF("rtc init\r\n"); KIT_PRINTF("rtc init\r\n");
RCC->APB1ENR |= RCC_APB1Periph_PWR; RCC->APB1ENR |= RCC_APB1Periph_PWR;
//使能允许写入RTC和后备寄存器 //使能允许写入RTC和后备寄存器
PWR->CR |= PWR_CR_DBP; PWR->CR |= PWR_CR_DBP;
@ -71,11 +71,11 @@ bool drv_rtc_init(void)
bsp_task_delay_ms(10); bsp_task_delay_ms(10);
if(dly++ > 500) if(dly++ > 500)
{ {
KIT_DEBUG_PRINTF("rtc fail \r\n"); KIT_PRINTF("rtc fail \r\n");
return res; return res;
} }
} }
KIT_DEBUG_PRINTF("rtc dly %d\r\n", dly); KIT_PRINTF("rtc dly %d\r\n", dly);
RCC->BDCR |= (RCC_RTCCLKSource_LSE | RCC_BDCR_RTCEN_POS); RCC->BDCR |= (RCC_RTCCLKSource_LSE | RCC_BDCR_RTCEN_POS);
//RTC配置使用默认配置 //RTC配置使用默认配置
//设置同步分频系数和异步分频系数默认为1Hz = 32768/(同步分频 + 1)/(异步分频 + 1) //设置同步分频系数和异步分频系数默认为1Hz = 32768/(同步分频 + 1)/(异步分频 + 1)
@ -189,7 +189,9 @@ uint32_t drv_rtc_get_tick(void)
{ {
tmp.tm_mon--; tmp.tm_mon--;
tmp.tm_year = (tmp.tm_year > 2018) ? (tmp.tm_year - 1900) : 119; tmp.tm_year = (tmp.tm_year > 2018) ? (tmp.tm_year - 1900) : 119;
res = kit_time_get_stamp(2019, &tmp); res = mktime(&tmp);
//res = kit_time_get_stamp(2019, &tmp);
} }
return res; return res;

View File

@ -7,6 +7,7 @@ extern "C" {
#include "kit_macro.h" #include "kit_macro.h"
uint8_t kit_check_crc8(uint16_t msg_id, const uint8_t *buf, uint32_t len); uint8_t kit_check_crc8(uint16_t msg_id, const uint8_t *buf, uint32_t len);
void kit_append_crc8(uint16_t msg_id,uint8_t* buf, uint32_t len); void kit_append_crc8(uint16_t msg_id,uint8_t* buf, uint32_t len);
uint8_t kit_check_sum8(uint8_t *buf, uint16_t len); uint8_t kit_check_sum8(uint8_t *buf, uint16_t len);

View File

@ -7,12 +7,12 @@
void kit_assert_param(uint8_t* file, uint32_t line) void kit_assert_param(uint8_t* file, uint32_t line)
{ {
KIT_DEBUG_PRINTF("%d, %s, %d\r\n", kit_time_get_tick(), file, line); KIT_PRINTF("%d, %s, %d\r\n", kit_time_get_tick(), file, line);
} }
void kit_assert_res(uint8_t* file, uint32_t line, uint32_t dev, kit_ret_e res) void kit_assert_res(uint8_t* file, uint32_t line, uint32_t dev, kit_ret_e res)
{ {
KIT_DEBUG_PRINTF("%d, %s, line:%d, dev:%d, res:%d\r\n", kit_time_get_tick(), file, line, dev, res); KIT_PRINTF("%d, %s, line:%d, dev:%d, res:%d\r\n", kit_time_get_tick(), file, line, dev, res);
} }
void kit_debug_queue(uint16_t *buf, uint8_t len, uint16_t value) void kit_debug_queue(uint16_t *buf, uint8_t len, uint16_t value)

View File

@ -32,7 +32,7 @@ extern "C" {
#if defined (DEBUG_PRINTF_RTT) #if defined (DEBUG_PRINTF_RTT)
#include "SEGGER_RTT.h" #include "SEGGER_RTT.h"
#define KIT_DEBUG_PRINTF(fmt, args...) \ #define KIT_PRINTF(fmt, args...) \
do\ do\
{\ {\
SEGGER_RTT_printf(0, "%d %s>> ", kit_time_get_tick(), __FUNCTION__);\ SEGGER_RTT_printf(0, "%d %s>> ", kit_time_get_tick(), __FUNCTION__);\
@ -46,7 +46,7 @@ extern "C" {
SEGGER_RTT_printf(0, fmt, ##args);\ SEGGER_RTT_printf(0, fmt, ##args);\
} }
#define KIT_DEBUG_PRINTF_TIME() \ #define KIT_PRINTF_TIME() \
do\ do\
{\ {\
SEGGER_RTT_printf(0, "%d %s>> ", kit_time_get_tick(), __FUNCTION__);\ SEGGER_RTT_printf(0, "%d %s>> ", kit_time_get_tick(), __FUNCTION__);\
@ -54,7 +54,7 @@ extern "C" {
}while(0) }while(0)
#elif defined (DEBUG_ASSERT_UART) #elif defined (DEBUG_ASSERT_UART)
#include "stdio.h" #include "stdio.h"
#define KIT_DEBUG_PRINTF(fmt, args...) \ #define KIT_PRINTF(fmt, args...) \
do\ do\
{\ {\
printf("%d %s>> ", kit_time_get_tick(), __FUNCTION__);\ printf("%d %s>> ", kit_time_get_tick(), __FUNCTION__);\
@ -62,12 +62,12 @@ extern "C" {
}while(0) }while(0)
#else #else
#define KIT_DEBUG_PRINTF(fmt, args...) ((void)0) #define KIT_PRINTF(fmt, args...) ((void)0)
#endif #endif
#if defined (DEBUG_PRINTF_RTT) #if defined (DEBUG_PRINTF_RTT)
#include "SEGGER_RTT.h" #include "SEGGER_RTT.h"
#define KIT_DEBUG_PRINTF_U8_BUF(buf, len) \ #define KIT_PRINTF_U8_BUF(buf, len) \
do\ do\
{\ {\
uint32_t i = 0; \ uint32_t i = 0; \
@ -79,7 +79,7 @@ extern "C" {
SEGGER_RTT_printf(0, "\r\n");\ SEGGER_RTT_printf(0, "\r\n");\
}while(0) }while(0)
#define KIT_DEBUG_PRINTF_U16_BUF(buf, len) \ #define KIT_PRINTF_U16_BUF(buf, len) \
do\ do\
{\ {\
uint32_t i = 0; \ uint32_t i = 0; \
@ -91,7 +91,7 @@ extern "C" {
SEGGER_RTT_printf(0, "\r\n");\ SEGGER_RTT_printf(0, "\r\n");\
}while(0) }while(0)
#define KIT_DEBUG_PRINTF_I32_BUF(buf, len) \ #define KIT_PRINTF_I32_BUF(buf, len) \
do\ do\
{\ {\
uint32_t i = 0; \ uint32_t i = 0; \
@ -104,7 +104,7 @@ extern "C" {
}while(0) }while(0)
#elif defined (DEBUG_ASSERT_UART) #elif defined (DEBUG_ASSERT_UART)
#include "stdio.h" #include "stdio.h"
#define KIT_DEBUG_PRINTF(fmt, args...) \ #define KIT_PRINTF(fmt, args...) \
do\ do\
{\ {\
printf("%d %s>> ", kit_time_get_tick(), __FUNCTION__);\ printf("%d %s>> ", kit_time_get_tick(), __FUNCTION__);\
@ -112,10 +112,10 @@ extern "C" {
}while(0) }while(0)
#else #else
#define KIT_DEBUG_PRINTF_U8_BUF(buf, len) ((void)0) #define KIT_PRINTF_U8_BUF(buf, len) ((void)0)
#define KIT_DEBUG_PRINTF_U16_BUF(buf, len) ((void)0) #define KIT_PRINTF_U16_BUF(buf, len) ((void)0)
#define KIT_DEBUG_PRINTF_I32_BUF(buf, len) ((void)0) #define KIT_PRINTF_I32_BUF(buf, len) ((void)0)
#define KIT_DEBUG_PRINTF(fmt, args...) ((void)0) #define KIT_PRINTF(fmt, args...) ((void)0)
#define KIT_MODULE_PRINTF(en, fmt, args...) ((void)0) #define KIT_MODULE_PRINTF(en, fmt, args...) ((void)0)
#endif #endif

View File

@ -9,19 +9,16 @@ extern "C"
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
typedef enum /***************************ÖжÏÓÅÏȼ¶*********************/
{ #define APP_CFG_INT_PRIO_PWR_OFF 0u
kKit_Ret_Ok = 0x00, #define APP_CFG_INT_PRIO_CAN1_RX 1u
kKit_Ret_Error, #define APP_CFG_INT_PRIO_CAN1_TX 2u
kKit_Ret_Null, #define APP_CFG_INT_PRIO_CAN2_RX 3u
kKit_Ret_TimeOut, #define APP_CFG_INT_PRIO_CAN2_TX 4u
kKit_Ret_OutRange,
#define APP_CFG_INT_PRIO_SYSTICK 5u
#define APP_CFG_INT_PRIO_UART2_RX 6u
kKit_Ret_NullPtr,
kKit_Ret_ParamErr,
kKit_Ret_CheckErr,
kKit_Ret_OverFlow,
} kit_ret_e;
#define USE_BIT_ARRAY false #define USE_BIT_ARRAY false
#define KIT_ABS(a) (((a) < 0) ? (-(a)) : (a)) #define KIT_ABS(a) (((a) < 0) ? (-(a)) : (a))
@ -30,7 +27,7 @@ extern "C"
#define KIT_MIN(a, b) (((a) < (b)) ? (a) : (b)) #define KIT_MIN(a, b) (((a) < (b)) ? (a) : (b))
#if (USE_BIT_ARRAY == true) #if (USE_BIT_ARRAY == true)
extern const uint32_t bit_array[]; extern const uint32_t bit_array[];
#define KIT_BIT_MASK_32(pos) bit_array[bit] #define KIT_BIT_MASK_32(pos) bit_array[bit]
#else #else
#define KIT_BIT_MASK_32(pos) ((uint32_t)0x01u << (pos)) #define KIT_BIT_MASK_32(pos) ((uint32_t)0x01u << (pos))
@ -49,8 +46,8 @@ extern "C"
#define KIT_GET_BIT_64(val, pos) ((val) & KIT_BIT_MASK_64(pos)) #define KIT_GET_BIT_64(val, pos) ((val) & KIT_BIT_MASK_64(pos))
#define KIT_TOGGLE_BIT(val, mask) ((val) ^= (mask)) #define KIT_TOGGLE_BIT(val, mask) ((val) ^= (mask))
#define KIT_BIT_MASK_64S(start, end) ((0xFFFFFFFFFFFFFFFFu << (start)) & (0xFFFFFFFFFFFFFFFFu >> (63u - (uint32_t)(end)))) #define KIT_BIT_MASK_64S(start, end) ((0xFFFFFFFFFFFFFFFFu << (start)) & (0xFFFFFFFFFFFFFFFFu >> (63u - (uint32_t)(end))))
// #define KIT_IS_BIT_SET(val, mask) (((val) & (mask)) == (mask)) // #define KIT_IS_BIT_SET(val, mask) (((val) & (mask)) == (mask))
// #define KIT_IS_BIT_CLR(val, mask) (((val) & (mask)) == 0u) // #define KIT_IS_BIT_CLR(val, mask) (((val) & (mask)) == 0u)
#define KIT_CREATE_IP(ip1, ip2, ip3, ip4) ((ip1) + ((uint32_t)(ip2) << 8) + ((uint32_t)(ip3) << 16) + ((uint32_t)(ip4) << 24)) #define KIT_CREATE_IP(ip1, ip2, ip3, ip4) ((ip1) + ((uint32_t)(ip2) << 8) + ((uint32_t)(ip3) << 16) + ((uint32_t)(ip4) << 24))
#define KIT_CUR_CONVERT(val) ((val * 10)) #define KIT_CUR_CONVERT(val) ((val * 10))
@ -182,11 +179,25 @@ extern "C"
#define ARM_WRITE_INT32U(addr) *((uint32_t *)(addr)) #define ARM_WRITE_INT32U(addr) *((uint32_t *)(addr))
typedef void (*NoArgFuncCall)(void); typedef enum
typedef void (*VoidArgFunCall)(void *arg); {
typedef void (*UintArgFunCall)(uint32_t arg); kKit_Ret_Ok = 0x00,
typedef void (*KitIrqCall)(kit_ret_e res, void *data); kKit_Ret_Error,
typedef void (*UintArgFunCall2)(kit_ret_e res, uint32_t data); kKit_Ret_Null,
kKit_Ret_TimeOut,
kKit_Ret_OutRange,
kKit_Ret_NullPtr,
kKit_Ret_ParamErr,
kKit_Ret_CheckErr,
kKit_Ret_OverFlow,
} kit_ret_e;
typedef void (*NoArgFuncCall)(void);
typedef void (*VoidArgFunCall)(void *arg);
typedef void (*UintArgFunCall)(uint32_t arg);
typedef void (*KitIrqCall)(kit_ret_e res, void *data);
typedef void (*UintArgFunCall2)(kit_ret_e res, uint32_t data);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -0,0 +1,206 @@
#ifndef _KIT_MACRO_H_
#define _KIT_MACRO_H_
#ifdef __cplusplus
extern "C"
{
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
/***************************ÖжÏÓÅÏȼ¶*********************/
#define APP_CFG_INT_PRIO_PWR_OFF 0u
#define APP_CFG_INT_PRIO_CAN1_RX 1u
#define APP_CFG_INT_PRIO_CAN1_TX 2u
#define APP_CFG_INT_PRIO_CAN2_RX 3u
#define APP_CFG_INT_PRIO_CAN2_TX 4u
#define APP_CFG_INT_PRIO_SYSTICK 5u
#define APP_CFG_INT_PRIO_UART2_RX 6u
#define USE_BIT_ARRAY false
#define KIT_ABS(a) (((a) < 0) ? (-(a)) : (a))
#define KIT_ABS_DIFF(a, b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
#define KIT_MAX(a, b) (((a) > (b)) ? (a) : (b))
#define KIT_MIN(a, b) (((a) < (b)) ? (a) : (b))
#if (USE_BIT_ARRAY == true)
extern const uint32_t bit_array[];
#define KIT_BIT_MASK_32(pos) bit_array[bit]
#else
#define KIT_BIT_MASK_32(pos) ((uint32_t)0x01u << (pos))
#define KIT_BIT_MASK_64(pos) ((uint64_t)0x01u << (pos))
#endif
#define KIT_GET_BIT_VAL(val, pos) (((val) >> (pos)) & 0x01u)
#define KIT_SET_BIT_MASK_32_AT_POS(val, pos, bit) ((val) |= ((bit) << (pos)))
#define KIT_BIT_MASK_32S(start, end) ((0xFFFFFFFFu << (start)) & (0xFFFFFFFFu >> (31u - (uint32_t)(end))))
#define KIT_SET_BIT_MASK_32(val, pos) ((val) |= KIT_BIT_MASK_32(pos))
#define KIT_SET_BIT_MASK_64(val, pos) ((val) |= KIT_BIT_MASK_64(pos))
#define KIT_CLR_BIT_32(val, pos) ((val) &= ~KIT_BIT_MASK_32(pos))
#define KIT_CLR_BIT_64(val, pos) ((val) &= ~KIT_BIT_MASK_64(pos))
#define KIT_GET_BIT_32(val, pos) ((val) & KIT_BIT_MASK_32(pos))
#define KIT_GET_BIT_64(val, pos) ((val) & KIT_BIT_MASK_64(pos))
#define KIT_TOGGLE_BIT(val, mask) ((val) ^= (mask))
#define KIT_BIT_MASK_64S(start, end) ((0xFFFFFFFFFFFFFFFFu << (start)) & (0xFFFFFFFFFFFFFFFFu >> (63u - (uint32_t)(end))))
// #define KIT_IS_BIT_SET(val, mask) (((val) & (mask)) == (mask))
// #define KIT_IS_BIT_CLR(val, mask) (((val) & (mask)) == 0u)
#define KIT_CREATE_IP(ip1, ip2, ip3, ip4) ((ip1) + ((uint32_t)(ip2) << 8) + ((uint32_t)(ip3) << 16) + ((uint32_t)(ip4) << 24))
#define KIT_CUR_CONVERT(val) ((val * 10))
#define KIT_TEMP_CONVERT(val) ((val + 50) * 10)
#define KIT_MILLI_CONVERT(val) (val)
#define KIT_SECOND_CONVERT(val) ((val) * 1000)
#define KIT_MINUTE_CONVERT(val) (KIT_SECOND_CONVERT(val) * 60)
#define KIT_HOUR_CONVERT(val) (KIT_MINUTE_CONVERT(val) * 60)
#define READ_BT_INT8U(ADDRESS, POS) (POS++, ADDRESS[POS - 1])
#define READ_BT_INT16U(ADDRESS, POS) (POS += 2, (ADDRESS[POS - 1]) + ((uint16_t)(ADDRESS[POS - 2]) << 8))
#define READ_BT_INT16U_BY_CONST_POS(ADDRESS, POS) ((ADDRESS[POS + 1]) + ((uint16_t)(ADDRESS[POS]) << 8))
#define READ_BT_INT24U(ADDRESS, POS) (POS += 3, (ADDRESS[POS - 1]) + ((uint32_t)(ADDRESS[POS - 2]) << 8) + ((uint32_t)(ADDRESS[POS - 3]) << 16))
#define READ_BT_INT24U_BY_CONST_POS(ADDRESS, POS) ((ADDRESS[POS + 2]) + ((uint32_t)(ADDRESS[POS + 1]) << 8) + ((uint32_t)(ADDRESS[POS]) << 16))
#define READ_BT_INT32U(ADDRESS, POS) (POS += 4, (ADDRESS[POS - 1]) + ((uint32_t)(ADDRESS[POS - 2]) << 8) + ((uint32_t)(ADDRESS[POS - 3]) << 16) + ((uint32_t)(ADDRESS[POS - 4]) << 24))
#define READ_BT_INT32U_BY_CONST_POS(ADDRESS, POS) ((ADDRESS[POS + 3]) + ((uint32_t)(ADDRESS[POS + 2]) << 8) + ((uint32_t)(ADDRESS[POS + 1]) << 16) + ((uint32_t)(ADDRESS[POS]) << 24))
#define READ_LT_INT8U(ADDRESS, POS) (POS++, ADDRESS[POS - 1])
#define READ_LT_INT16U(ADDRESS, POS) (POS += 2, (ADDRESS[POS - 2]) + ((uint16_t)(ADDRESS[POS - 1]) << 8))
#define READ_LT_INT24U(ADDRESS, POS) (POS += 3, (ADDRESS[POS - 3]) + ((uint32_t)(ADDRESS[POS - 2]) << 8) + ((uint32_t)(ADDRESS[POS - 1]) << 16))
#define READ_LT_INT32U(ADDRESS, POS) (POS += 4, (ADDRESS[POS - 4]) + ((uint32_t)(ADDRESS[POS - 3]) << 8) + ((uint32_t)(ADDRESS[POS - 2]) << 16) + ((uint32_t)(ADDRESS[POS - 1]) << 24))
#define READ_LT_INT8U_BY_CONST_POS(ADDRESS, POS) (ADDRESS[POS])
#define READ_LT_INT16U_BY_CONST_POS(ADDRESS, POS) ((ADDRESS[POS]) + ((uint16_t)(ADDRESS[POS + 1]) << 8))
#define READ_LT_INT24U_BY_CONST_POS(ADDRESS, POS) ((ADDRESS[POS]) + ((uint32_t)(ADDRESS[POS + 1]) << 8) + ((uint32_t)(ADDRESS[POS + 2]) << 16))
#define READ_LT_INT32U_BY_CONST_POS(ADDRESS, POS) ((ADDRESS[POS]) + ((uint32_t)(ADDRESS[POS + 1]) << 8) + ((uint32_t)(ADDRESS[POS + 2]) << 16) + ((uint32_t)(ADDRESS[POS + 3]) << 24))
#define WRITE_BT_INT8U(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS++] = (uint8_t)(VALUE); \
}
#define WRITE_BT_INT16U(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 8); \
ADDRESS[POS++] = (uint8_t)(VALUE & 0xFF); \
}
#define WRITE_BT_INT24U(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 16); \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 8); \
ADDRESS[POS++] = (uint8_t)(VALUE & 0xFF); \
}
#define WRITE_BT_INT32U(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 24); \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 16); \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 8); \
ADDRESS[POS++] = (uint8_t)(VALUE & 0xFF); \
}
#define WRITE_BT_INT16U_BY_CONST_POS(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS] = (uint8_t)((VALUE) >> 8); \
ADDRESS[POS + 1] = (uint8_t)(VALUE & 0xFF); \
}
#define WRITE_BT_INT32U_BY_CONST_POS(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS] = (uint8_t)((VALUE) >> 24); \
ADDRESS[POS + 1] = (uint8_t)((VALUE) >> 16); \
ADDRESS[POS + 2] = (uint8_t)((VALUE) >> 8); \
ADDRESS[POS + 3] = (uint8_t)(VALUE & 0xFF); \
}
#define WRITE_LT_INT32U(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS++] = (uint8_t)(VALUE); \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 8); \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 16); \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 24); \
}
#define WRITE_LT_INT24U(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS++] = (uint8_t)(VALUE); \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 8); \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 16); \
}
#define WRITE_LT_INT16U(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS++] = (uint8_t)(VALUE); \
ADDRESS[POS++] = (uint8_t)((VALUE) >> 8); \
}
#define WRITE_LT_INT8U(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS++] = (uint8_t)(VALUE); \
}
#define WRITE_LT_INT32U_WITH_BUFF_SIZE(ADDRESS, POS, VALUE, SIZE) \
{ \
if (POS + 3 < SIZE) \
WRITE_LT_INT32U(ADDRESS, POS, VALUE); \
}
#define WRITE_LT_INT24U_WITH_BUFF_SIZE(ADDRESS, POS, VALUE, SIZE) \
{ \
if (POS + 2 < SIZE) \
WRITE_LT_INT24U(ADDRESS, POS, VALUE); \
}
#define WRITE_LT_INT16U_WITH_BUFF_SIZE(ADDRESS, POS, VALUE, SIZE) \
{ \
if (POS + 1 < SIZE) \
WRITE_LT_INT16U(ADDRESS, POS, VALUE); \
}
#define WRITE_LT_INT8U_WITH_BUFF_SIZE(ADDRESS, POS, VALUE, SIZE) \
{ \
if (POS < SIZE) \
WRITE_LT_INT8U(ADDRESS, POS, VALUE); \
}
#define WRITE_LT_INT16U_BY_CONST_POS(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS] = (uint8_t)(VALUE); \
ADDRESS[POS + 1] = (uint8_t)((VALUE) >> 8); \
}
#define WRITE_LT_INT24U_BY_CONST_POS(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS] = (uint8_t)(VALUE); \
ADDRESS[POS + 1] = (uint8_t)((VALUE) >> 8); \
ADDRESS[POS + 2] = (uint8_t)((VALUE) >> 16); \
}
#define WRITE_LT_INT32U_BY_CONST_POS(ADDRESS, POS, VALUE) \
{ \
ADDRESS[POS] = (uint8_t)(VALUE); \
ADDRESS[POS + 1] = (uint8_t)((VALUE) >> 8); \
ADDRESS[POS + 2] = (uint8_t)((VALUE) >> 16); \
ADDRESS[POS + 3] = (uint8_t)((VALUE) >> 24); \
}
#define ARM_READ_INT16U(addr) *((uint16_t *)(addr))
#define ARM_READ_INT32U(addr) *((uint32_t *)(addr))
#define ARM_WRITE_INT32U(addr) *((uint32_t *)(addr))
typedef enum
{
kKit_Ret_Ok = 0x00,
kKit_Ret_Error,
kKit_Ret_Null,
kKit_Ret_TimeOut,
kKit_Ret_OutRange,
kKit_Ret_NullPtr,
kKit_Ret_ParamErr,
kKit_Ret_CheckErr,
kKit_Ret_OverFlow,
} kit_ret_e;
typedef void (*NoArgFuncCall)(void);
typedef void (*VoidArgFunCall)(void *arg);
typedef void (*UintArgFunCall)(uint32_t arg);
typedef void (*KitIrqCall)(kit_ret_e res, void *data);
typedef void (*UintArgFunCall2)(kit_ret_e res, uint32_t data);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -255,7 +255,7 @@ void HardFaultHandler(unsigned int* pStack)
// Halt execution // Halt execution
// To step out of the HardFaultHandler, change the variable value to != 0. // To step out of the HardFaultHandler, change the variable value to != 0.
// //
KIT_DEBUG_PRINTF("%s\r\n", "HardFault"); KIT_PRINTF("%s\r\n", "HardFault");
bms_write_hardfault_data((uint8_t *)&HardFaultRegs, 64); bms_write_hardfault_data((uint8_t *)&HardFaultRegs, 64);
drv_misc_reset_mcu(); drv_misc_reset_mcu();
#else #else