IoT_SCV_CH584M/APP/bsp_wf5803.c

347 lines
8.6 KiB
C
Raw Normal View History

2024-11-14 09:47:21 +08:00
#include "bsp_wf5803.h"
#include "CONFIG.h"
2024-12-01 14:00:17 +08:00
#include "bsp_uart.h"
#include "log.h"
#undef LOG_ENABLE
2024-12-02 13:13:49 +08:00
#define LOG_ENABLE 0
2024-12-01 14:00:17 +08:00
2024-11-19 19:01:02 +08:00
typedef enum
{
kPressIn = 0,
kPressOut = 1,
kPressAtom = 2,
kPressMaxIndex
} TePressSensorIndex;
static tmosTaskID press_task_id = INVALID_TASK_ID;
2024-12-01 14:00:17 +08:00
#define PRESS_IN_CS_HIGH() GPIOA_SetBits(GPIO_Pin_3)
#define PRESS_IN_CS_LOW() GPIOA_ResetBits(GPIO_Pin_3)
2024-12-01 14:00:17 +08:00
#define PRESS_OUT_CS_HIGH() GPIOA_SetBits(GPIO_Pin_2)
#define PRESS_OUT_CS_LOW() GPIOA_ResetBits(GPIO_Pin_2)
2024-12-01 14:00:17 +08:00
#define PRESS_ATOM_CS_HIGH() GPIOA_SetBits(GPIO_Pin_12)
#define PRESS_ATOM_CS_LOW() GPIOA_ResetBits(GPIO_Pin_12)
2024-11-19 19:01:02 +08:00
uint8_t volatile press_done_flag = 0;
2024-11-14 09:47:21 +08:00
2024-11-19 19:01:02 +08:00
uint8_t press_raw_data[kPressMaxIndex][5];
2024-11-26 18:21:41 +08:00
// 1 pa
uint32_t press_value[kPressMaxIndex];
// 0.1 C
int16_t temp_value[kPressMaxIndex];
2024-11-19 19:01:02 +08:00
void PRESS_SPI_CsStart(TePressSensorIndex index)
2024-11-14 09:47:21 +08:00
{
2024-11-19 19:01:02 +08:00
switch (index)
{
case kPressIn:
PRESS_IN_CS_LOW();
break;
case kPressOut:
PRESS_OUT_CS_LOW();
break;
case kPressAtom:
PRESS_ATOM_CS_LOW();
break;
default:
break;
}
2024-11-14 09:47:21 +08:00
}
2024-11-19 19:01:02 +08:00
void PRESS_SPI_CsStop(TePressSensorIndex index)
2024-11-14 09:47:21 +08:00
{
2024-11-19 19:01:02 +08:00
switch (index)
{
case kPressIn:
PRESS_IN_CS_HIGH();
break;
case kPressOut:
PRESS_OUT_CS_HIGH();
break;
case kPressAtom:
PRESS_ATOM_CS_HIGH();
break;
default:
break;
}
2024-11-14 09:47:21 +08:00
}
2024-11-19 19:01:02 +08:00
uint8_t PRESS_SPI_SendByte(uint8_t data)
2024-11-14 09:47:21 +08:00
{
R8_SPI0_BUFFER = data;
while (!(R8_SPI0_INT_FLAG & RB_SPI_FREE));
return (R8_SPI0_BUFFER);
2024-11-14 09:47:21 +08:00
}
2024-11-19 19:01:02 +08:00
void WF5803_WriteReg(uint8_t Address, uint8_t value, TePressSensorIndex index)
2024-11-14 09:47:21 +08:00
{
2024-11-19 19:01:02 +08:00
PRESS_SPI_CsStart(index);
PRESS_SPI_SendByte(0x00);
PRESS_SPI_SendByte(Address);
PRESS_SPI_SendByte(value);
PRESS_SPI_CsStop(index);
2024-11-14 09:47:21 +08:00
}
2024-11-19 19:01:02 +08:00
uint8_t WF5803_ReadReg(uint8_t addr, TePressSensorIndex index)
2024-11-14 09:47:21 +08:00
{
uint8_t value;
2024-11-19 19:01:02 +08:00
PRESS_SPI_CsStart(index);
PRESS_SPI_SendByte(0x80);
PRESS_SPI_SendByte(addr);
value = PRESS_SPI_SendByte(0xFF);
PRESS_SPI_CsStop(index);
2024-11-14 09:47:21 +08:00
return value;
}
2024-11-19 19:01:02 +08:00
void PRESS_IO_SPI_Init(void)
2024-11-14 09:47:21 +08:00
{
/**
* SCL: PA13
* SDA: PA14
* SDO: PA15
*/
// SDA: MOSI
// SDO: MISO
2024-12-01 14:00:17 +08:00
// 板载
// CSB1: PA12
GPIOA_SetBits(GPIO_Pin_12);
GPIOA_ModeCfg(GPIO_Pin_12, GPIO_ModeOut_PP_5mA);
2024-11-26 17:14:42 +08:00
2024-12-01 14:00:17 +08:00
// CSB2: PA3
GPIOA_SetBits(GPIO_Pin_3);
GPIOA_ModeCfg(GPIO_Pin_3, GPIO_ModeOut_PP_5mA);
2024-12-01 14:00:17 +08:00
// CSB3: PA2
GPIOA_SetBits(GPIO_Pin_2);
GPIOA_ModeCfg(GPIO_Pin_2, GPIO_ModeOut_PP_5mA);
2024-11-19 19:01:02 +08:00
PRESS_SPI_CsStop(kPressIn);
PRESS_SPI_CsStop(kPressOut);
PRESS_SPI_CsStop(kPressAtom);
GPIOA_ModeCfg(GPIO_Pin_13 | GPIO_Pin_14, GPIO_ModeOut_PP_5mA);
GPIOA_ModeCfg(GPIO_Pin_15, GPIO_ModeIN_PU);
SPI0_MasterDefInit();
2024-11-14 09:47:21 +08:00
}
2024-11-19 19:01:02 +08:00
void WF5803_Init(void)
{
PRESS_IO_SPI_Init();
2024-12-01 14:00:17 +08:00
WF5803_WriteReg(0x00, 0x81, kPressIn); // 锟斤拷锟斤拷spi为锟斤拷锟斤拷模式
WF5803_WriteReg(0x00, 0x81, kPressOut); // 锟斤拷锟斤拷spi为锟斤拷锟斤拷模式
WF5803_WriteReg(0x00, 0x81, kPressAtom); // 锟斤拷锟斤拷spi为锟斤拷锟斤拷模式
2024-11-19 19:01:02 +08:00
}
2024-11-19 19:01:02 +08:00
void PRESS_LowerIO_Init(void)
{
2024-12-01 14:00:17 +08:00
// WF5803默锟较癸拷锟斤拷时锟斤拷锟斤拷锟斤拷IO锟斤拷锟角高碉拷平
// SPI
2024-11-19 19:01:02 +08:00
GPIOA_SetBits(GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
GPIOA_ModeCfg(GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15, GPIO_ModeIN_PU);
2024-12-01 14:00:17 +08:00
// 板载
// CSB1: PA12
GPIOA_SetBits(GPIO_Pin_12);
GPIOA_ModeCfg(GPIO_Pin_12, GPIO_ModeIN_PU);
2024-12-01 14:00:17 +08:00
// CSB2: PA3
GPIOA_SetBits(GPIO_Pin_3);
GPIOA_ModeCfg(GPIO_Pin_3, GPIO_ModeIN_PU);
2024-12-01 14:00:17 +08:00
// CSB3: PA2
GPIOA_SetBits(GPIO_Pin_2);
GPIOA_ModeCfg(GPIO_Pin_2, GPIO_ModeIN_PU);
2024-11-19 19:01:02 +08:00
}
2024-12-01 14:00:17 +08:00
// 鍦ㄨ繖閲岃繘浼戠湢瀵笽O鎿嶄綔浜<E7B694>
2024-11-19 19:01:02 +08:00
void Lower_IO_Deinit(void)
{
// LED
2024-12-01 14:00:17 +08:00
GPIOB_ResetBits(GPIO_Pin_4 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_16 | GPIO_Pin_17);
GPIOB_ModeCfg(GPIO_Pin_4 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_16 | GPIO_Pin_17, GPIO_ModeIN_PD);
// KEY
2024-12-02 13:13:49 +08:00
GPIOB_SetBits(GPIO_Pin_0);
2024-12-01 14:00:17 +08:00
// 由外部上拉电阻了
GPIOB_ModeCfg(GPIO_Pin_0, GPIO_ModeIN_Floating);
// motor
2024-12-02 13:13:49 +08:00
GPIOA_ResetBits(GPIO_Pin_8 | GPIO_Pin_9);
GPIOA_ModeCfg(GPIO_Pin_8 | GPIO_Pin_9, GPIO_ModeIN_PD);
2024-12-01 14:00:17 +08:00
GPIOA_ResetBits(GPIO_Pin_7);
2024-12-02 13:13:49 +08:00
GPIOA_ModeCfg(GPIO_Pin_7, GPIO_ModeIN_PD);
// // nsleep
// GPIOA_ResetBits(GPIO_Pin_7);
// // 由外部上拉电阻了
// GPIOA_ModeCfg(GPIO_Pin_7, GPIO_ModeOut_PP_5mA);
2024-12-01 14:00:17 +08:00
// ADC_VBAT
2024-12-02 13:13:49 +08:00
GPIOADigitalCfg(DISABLE, GPIO_Pin_1);
// GPIOA_ResetBits(GPIO_Pin_1);
2024-12-01 14:00:17 +08:00
GPIOA_ModeCfg(GPIO_Pin_1, GPIO_ModeIN_Floating);
2024-11-26 17:14:42 +08:00
2024-12-02 13:13:49 +08:00
// RB_PIN_ADC11_IE
// RB_PIN_ADC11_IE
2024-12-01 14:00:17 +08:00
// 4g的串口也得deinit
// 4G
// 3V8_EN不能浮空
// 3V8_EN
GPIOB_ResetBits(GPIO_Pin_3);
// 由外部上拉电阻了
GPIOB_ModeCfg(GPIO_Pin_3, GPIO_ModeOut_PP_5mA);
2024-12-02 13:13:49 +08:00
// NET_TXD
GPIOB_SetBits(GPIO_Pin_12);
// 由外部上拉电阻了
GPIOB_ModeCfg(GPIO_Pin_12, GPIO_ModeIN_Floating);
GPIOB_ResetBits(GPIO_Pin_13 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
GPIOB_ModeCfg(GPIO_Pin_13 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7, GPIO_ModeIN_PD);
}
2024-11-19 19:01:02 +08:00
void PRESS_LowPower(void)
{
Lower_IO_Deinit();
if (press_done_flag == 1)
{
PRESS_LowerIO_Init();
}
}
2024-11-14 09:47:21 +08:00
2024-11-19 19:01:02 +08:00
uint8_t GetSensorData(TePressSensorIndex index)
2024-11-14 09:47:21 +08:00
{
2024-11-19 19:01:02 +08:00
memset(press_raw_data[index], 0, 5);
2024-11-26 18:21:41 +08:00
if (WF5803_ReadReg(0x02, index) != 0x01)
{
PRINT("Status = %02x\r\n", WF5803_ReadReg(0x02, index));
return 1;
}
2024-12-01 14:00:17 +08:00
// 锟斤拷压锟斤拷锟斤拷
// 锟铰讹拷锟斤拷锟斤拷
2024-11-19 19:01:02 +08:00
for (uint8_t i = 0; i < 5; i++)
{
press_raw_data[index][i] = WF5803_ReadReg(0x06 + i, index);
}
2024-11-19 19:01:02 +08:00
return 0;
}
2024-11-26 18:21:41 +08:00
__HIGH_CODE
__attribute__((noinline))
2024-11-19 19:01:02 +08:00
void SensorData_Process(TePressSensorIndex index)
2024-11-14 09:47:21 +08:00
{
2024-11-19 19:01:02 +08:00
long reading = 0;
float fDat = 0;
2024-11-21 15:04:27 +08:00
float press = 0;
float temp = 0;
2024-11-19 19:01:02 +08:00
GetSensorData(index);
reading = press_raw_data[index][0];
reading = reading << 8;
reading |= press_raw_data[index][1];
2024-11-14 09:47:21 +08:00
reading = reading << 8;
2024-11-19 19:01:02 +08:00
reading |= press_raw_data[index][2];
2024-11-14 09:47:21 +08:00
if (reading >= 8388608)
{
fDat = (int32_t)(reading - 16777216) / 8388608.0f;
}
else
{
fDat = reading / 8388608.0f;
}
2024-12-01 14:00:17 +08:00
press = fDat * 125 + 17.5; // WF5803_1BAR 锟斤拷锟斤拷锟绞癸拷锟<E68BB7>10m锟斤拷锟斤拷锟斤拷畲拷锟绞癸拷锟斤拷锟斤拷
2024-11-26 18:21:41 +08:00
press_value[index] = (uint32_t)(press * 1000); // pa
2024-11-14 09:47:21 +08:00
2024-11-19 19:01:02 +08:00
reading = press_raw_data[index][3];
2024-11-14 09:47:21 +08:00
reading = reading << 8;
2024-11-19 19:01:02 +08:00
reading |= press_raw_data[index][4];
2024-11-14 09:47:21 +08:00
if (reading > 32768)
{
temp = (reading - 65844) / 256.0f;
2024-11-26 18:21:41 +08:00
temp_value[index] = (int16_t)(temp * 10); // .1 C
2024-11-14 09:47:21 +08:00
}
else
{
temp = (reading - 308) / 256.0f;
2024-11-26 18:21:41 +08:00
temp_value[index] = (int16_t)(temp * 10); // .1 C
2024-11-14 09:47:21 +08:00
}
2024-11-19 19:01:02 +08:00
2024-11-26 18:21:41 +08:00
// PRINT("P[%d] = %d.%d pa\r\n", index, (int)(press * 1000), ((int)(press * 10000000.0f) % 10000));
// PRINT("T[%d] = %d.%d \r\n", index, (int)temp, ((int)(temp * 100.0f) % 100));
2024-11-14 09:47:21 +08:00
}
2024-11-26 18:21:41 +08:00
__HIGH_CODE
__attribute__((noinline))
uint16_t WF5803_ProcessEvent(uint8_t task_id, uint16_t events)
2024-11-14 09:47:21 +08:00
{
2024-11-19 19:01:02 +08:00
if (events & WF5803_EVT_START)
{
2024-11-19 19:01:02 +08:00
press_done_flag = 0;
WF5803_Init();
2024-11-19 19:01:02 +08:00
WF5803_WriteReg(0x30, 0x0A, kPressIn);
WF5803_WriteReg(0x30, 0x0A, kPressOut);
WF5803_WriteReg(0x30, 0x0A, kPressAtom);
2024-11-19 19:01:02 +08:00
tmos_start_task(press_task_id, WF5803_EVT_READ, MS1_TO_SYSTEM_TIME(5));
2024-12-01 14:00:17 +08:00
logDebug("WF5803_EVT_START");
return (events ^ WF5803_EVT_START);
}
else if (events & WF5803_EVT_READ)
2024-11-14 09:47:21 +08:00
{
2024-11-19 19:01:02 +08:00
SensorData_Process(kPressIn);
SensorData_Process(kPressOut);
SensorData_Process(kPressAtom);
2024-11-26 18:21:41 +08:00
press_value[kPressIn];
press_value[kPressOut];
press_value[kPressAtom];
PRINT("%d %d %d pa\r\n", press_value[kPressIn], press_value[kPressOut], press_value[kPressAtom]);
PRINT("%d %d %d C\r\n", temp_value[kPressIn], temp_value[kPressOut], temp_value[kPressAtom]);
if (press_value[kPressIn] > press_value[kPressOut])
{
PRINT("> %d\r\n", press_value[kPressIn] - press_value[kPressOut]);
}
else
{
PRINT("< %d\r\n", press_value[kPressOut] - press_value[kPressIn]);
}
2024-11-19 19:01:02 +08:00
tmos_start_task(press_task_id, WF5803_EVT_START, MS1_TO_SYSTEM_TIME(800));
2024-12-01 14:00:17 +08:00
logDebug("WF5803_EVT_READ");
2024-11-19 19:01:02 +08:00
press_done_flag = 1;
return (events ^ WF5803_EVT_READ);
2024-11-14 09:47:21 +08:00
}
return 0;
}
2024-11-19 19:01:02 +08:00
void BSP_PRESS_Init(void)
2024-11-14 09:47:21 +08:00
{
2024-11-19 19:01:02 +08:00
press_task_id = TMOS_ProcessEventRegister(WF5803_ProcessEvent);
tmos_set_event(press_task_id, WF5803_EVT_START);
2024-12-01 14:00:17 +08:00
logDebug("BSP_PRESS_Init");
2024-11-14 09:47:21 +08:00
}