diff --git a/Core/Src/stm32f4xx_hal_msp.c b/Core/Src/stm32f4xx_hal_msp.c index 41e16dc..e69e397 100644 --- a/Core/Src/stm32f4xx_hal_msp.c +++ b/Core/Src/stm32f4xx_hal_msp.c @@ -70,6 +70,10 @@ void HAL_MspInit(void) __HAL_RCC_SYSCFG_CLK_ENABLE(); __HAL_RCC_PWR_CLK_ENABLE(); + + __HAL_RCC_SYSCFG_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); + /* System interrupt init*/ /* PendSV_IRQn interrupt configuration */ HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0); diff --git a/usrc/app_main.cpp b/usrc/app_main.cpp index 4959e99..b28775a 100644 --- a/usrc/app_main.cpp +++ b/usrc/app_main.cpp @@ -99,6 +99,8 @@ void umain() { break; case kLargeSpaceDMPowerCtrlBoard: case kSmallSpaceDMPowerCtrlBoard: + LargeSpaceDmPowerCtrlBoard::ins()->initialize(); + ProtocolProcesserMgr::ins()->regProcesser(LargeSpaceDmPowerCtrlBoardPP::ins()); break; case kPipeDMLiquidCtrlBoard: break; diff --git a/usrc/board/board.hpp b/usrc/board/board.hpp index 5c00e26..c6a30ca 100644 --- a/usrc/board/board.hpp +++ b/usrc/board/board.hpp @@ -1,3 +1,4 @@ #pragma once #include "liquid_ctrl_board.hpp" -#include "public_board.hpp" \ No newline at end of file +#include "public_board.hpp" +#include "large_space_dm_power_ctrl_board.hpp" \ No newline at end of file diff --git a/usrc/board/large_space_dm_power_ctrl_board.cpp b/usrc/board/large_space_dm_power_ctrl_board.cpp new file mode 100644 index 0000000..3b68443 --- /dev/null +++ b/usrc/board/large_space_dm_power_ctrl_board.cpp @@ -0,0 +1,224 @@ +#include "large_space_dm_power_ctrl_board.hpp" + +#include "utils/halutils.hpp" +using namespace iflytop; + +#define TAG "LSDMPowerCtrlBoard" + +#define HEATER_CTRL_GPIO PC6 +#define HEATER_SAFE_CTRL_GPIO PC7 +#define HEATER_ELECTRIC_CURRENT_ADC_CH ADC_CHANNEL_2 +#define HEATER_TEMPERATURE_ADC_CH ADC_CHANNEL_8 + +#define BLOWER_CTRL_GPIO PC4 +#define BLOWER_SAFE_CTRL_GPIO PC5 +#define BLOWER_ELECTRIC_CURRENT_ADC_CH ADC_CHANNEL_1 + +#define AIRCOMPRESSOR_CTRL_GPIO PC2 +#define AIRCOMPRESSOR_SAFE_CTRL_GPIO PC3 +#define AIRCOMPRESSOR_ELECTRIC_CURRENT_ADC_CH ADC_CHANNEL_0 + + +/*********************************************************************************************************************** + * EXT * + ***********************************************************************************************************************/ + +static osThreadId H2O2CaptureThreadId; +static osThreadId AdcCaptureThreadId; + +/*********************************************************************************************************************** + * FUNC * + ***********************************************************************************************************************/ +static void c_onAdcCaptureThread(void const* argument) { LargeSpaceDmPowerCtrlBoard::ins()->onAdcCaptureThread(); } +// ADC init +// + +static void adcInit() { + __HAL_RCC_ADC1_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + ADC_ChannelConfTypeDef sConfig = {0}; + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.ScanConvMode = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + if (HAL_ADC_Init(&hadc1) != HAL_OK) { + Error_Handler(); + } + + /**ADC1 GPIO Configuration +PA0-WKUP ------> ADC1_IN0 +PA1 ------> ADC1_IN1 +PA2 ------> ADC1_IN2 +PA3 ------> ADC1_IN3 +PB0 ------> ADC1_IN8 +PB1 ------> ADC1_IN9 +*/ + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* ADC1 interrupt Init */ + HAL_NVIC_SetPriority(ADC_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(ADC_IRQn); + + sConfig.Channel = ADC_CHANNEL_0; + sConfig.Rank = 1; + sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } +} + +static void MX_USART2_UART_Init(void) { + __HAL_RCC_USART2_CLK_ENABLE(); + __HAL_RCC_GPIOD_CLK_ENABLE(); + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + huart2.Instance = USART2; + huart2.Init.BaudRate = 19200; + huart2.Init.WordLength = UART_WORDLENGTH_8B; + huart2.Init.StopBits = UART_STOPBITS_1; + huart2.Init.Parity = UART_PARITY_NONE; + huart2.Init.Mode = UART_MODE_TX_RX; + huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart2.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart2) != HAL_OK) { + Error_Handler(); + } + UART_HandleTypeDef* uartHandle = &huart2; + + /**USART2 GPIO Configuration + PD5 ------> USART2_TX + PD6 ------> USART2_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF7_USART2; + HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); + + /* USART2 interrupt Init */ + HAL_NVIC_SetPriority(USART2_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(USART2_IRQn); + + // DMAINI + __HAL_RCC_DMA1_CLK_ENABLE(); + HalUtils::uartdmainit(&huart2, &hdma1_stream5, DMA_CHANNEL_4, &hdma1_stream6, DMA_CHANNEL_4); + HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn); + HAL_NVIC_SetPriority(DMA1_Stream6_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn); +} + +static void MX_USART3_UART_Init(void) { + __HAL_RCC_USART3_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + huart3.Instance = USART3; + huart3.Init.BaudRate = 19200; + huart3.Init.WordLength = UART_WORDLENGTH_8B; + huart3.Init.StopBits = UART_STOPBITS_2; + huart3.Init.Parity = UART_PARITY_NONE; + huart3.Init.Mode = UART_MODE_TX_RX; + huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart3.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart3) != HAL_OK) { + Error_Handler(); + } + /**USART3 GPIO Configuration + PB10 ------> USART3_TX + PB11 ------> USART3_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF7_USART3; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + HAL_NVIC_SetPriority(USART3_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(USART3_IRQn); + + /* DMA controller clock enable */ + + __HAL_RCC_DMA1_CLK_ENABLE(); + HalUtils::uartdmainit(&huart2, &hdma1_stream1, DMA_CHANNEL_4, &hdma1_stream3, DMA_CHANNEL_4); + HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn); + HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn); +} + +void LargeSpaceDmPowerCtrlBoard::initialize() { + MX_USART2_UART_Init(); + MX_USART3_UART_Init(); + + /*********************************************************************************************************************** + * 加热片 * + ***********************************************************************************************************************/ + m_Heater_ctrlGpio.initAsOutput(HEATER_CTRL_GPIO, kxs_gpio_nopull, true, false); + m_Heater_electricCurrentAdc.initialize("HeaterCurrentAdc", &hadc1, HEATER_ELECTRIC_CURRENT_ADC_CH); + m_Heater_temperatureAdc.initialize("temperatureAdc", &hadc1, HEATER_TEMPERATURE_ADC_CH); + /*********************************************************************************************************************** + * 鼓风机 * + ***********************************************************************************************************************/ + + m_Blowser_ctrlGpio.initAsOutput(BLOWER_CTRL_GPIO, kxs_gpio_nopull, true, false); + m_Blowser_electricCurrentAdc.initialize("BlowserCurrentAdc", &hadc1, BLOWER_ELECTRIC_CURRENT_ADC_CH); + + /*********************************************************************************************************************** + * 空压机 * + ***********************************************************************************************************************/ + m_AirCompressor_ctrlGpio.initAsOutput(AIRCOMPRESSOR_CTRL_GPIO, kxs_gpio_nopull, true, false); + m_AirCompressor_electricCurrentAdc.initialize("AirCompressorCurrentAdc", &hadc1, AIRCOMPRESSOR_ELECTRIC_CURRENT_ADC_CH); + + /*********************************************************************************************************************** + * H2O2传感器 * + ***********************************************************************************************************************/ + /** + * @brief 探测HMP110 + */ + osDelay(2000); // 等待传感器上电 + m_h2o2Sensor.initialize(&huart2, &hadc1, ADC_CHANNEL_0); + + osThreadDef(AdcCaptureThread, c_onAdcCaptureThread, osPriorityNormal, 0, 1024); + AdcCaptureThreadId = osThreadCreate(osThread(AdcCaptureThread), NULL); +} + +void LargeSpaceDmPowerCtrlBoard::onAdcCaptureThread() { + while (1) { + // osDelay(30); + m_Heater_electricCurrentAdc.updateAdcValToCache(); + m_Heater_temperatureAdc.updateAdcValToCache(); + m_AirCompressor_electricCurrentAdc.updateAdcValToCache(); + m_Blowser_electricCurrentAdc.updateAdcValToCache(); + + osDelay(1000); + int32_t adcv1 = m_Blowser_electricCurrentAdc.getCacheVal(); + int32_t adcv2 = m_AirCompressor_electricCurrentAdc.getCacheVal(); + int32_t adcv3 = m_Heater_electricCurrentAdc.getCacheVal(); + ZLOGI(TAG, "b:%d, a:%d, h:%d", adcv1, adcv2, adcv3); + } +} diff --git a/usrc/board/large_space_dm_power_ctrl_board.hpp b/usrc/board/large_space_dm_power_ctrl_board.hpp new file mode 100644 index 0000000..9f859ea --- /dev/null +++ b/usrc/board/large_space_dm_power_ctrl_board.hpp @@ -0,0 +1,65 @@ +#pragma once +#include +#include + +#include "zsdk/zsdk.hpp" +#include "zstm32/zstm32.hpp" + +// +#include "zsdk/hmp110/hmp110.hpp" +#include "zsdk/hpp272/hpp272.hpp" +#include "zsdk/modbus/modbus_block_host.hpp" +#include "zsdk/pxx_pressure_sensor_driver/pxx_pressure_sensor_bus.hpp" +#include "zsdk/zadc.hpp" +#include "zsdk/zsdk.hpp" +// +#include "formula/formula.hpp" +#include "share/h2o2_sensor_driver.hpp" +#include "transmit_disfection_protocol/transmit_disfection_protocol.hpp" +namespace iflytop { +#define H2O2_SENSOR_TYPE_HMP110 +using namespace transmit_disfection_protocol; + +class LargeSpaceDmPowerCtrlBoard { + public: + // 加热片控制 + ZGPIO m_Heater_ctrlGpio; + ZADC m_Heater_electricCurrentAdc; + ZADC m_Heater_temperatureAdc; + + // 鼓风机 + ZGPIO m_Blowser_ctrlGpio; + ZADC m_Blowser_electricCurrentAdc; + + // 空压机 + ZGPIO m_AirCompressor_ctrlGpio; + ZADC m_AirCompressor_electricCurrentAdc; + + H2O2SensorDriver m_h2o2Sensor; + + public: + static LargeSpaceDmPowerCtrlBoard* ins() { + static LargeSpaceDmPowerCtrlBoard instance; + return &instance; + } + void initialize(); + + void heater_ctrl(int32_t val) { m_Heater_ctrlGpio.write(val); } + void heater_ctrl_safe_valve(int32_t val) {} + int32_t heater_read_temperature_data() { return heaterAdc2Temp(m_Heater_temperatureAdc.getCacheVal()); } + int32_t heater_read_electric_current() { return hearterAdcToCurrent(m_Heater_electricCurrentAdc.getCacheVal()); } + + void blower_ctrl(int32_t val) { m_Blowser_ctrlGpio.write(val); } + void blower_ctrl_safe_valve(int32_t val) {} + int32_t blower_read_electric_current() { return blowserAdcToCurrent(m_Blowser_electricCurrentAdc.getCacheVal()); } + + void air_compressor_ctrl(int32_t val) { m_AirCompressor_ctrlGpio.write(val); } + void air_compressor_ctrl_safe_valve(int32_t val) {} + int32_t air_compressor_read_electric_current() { return airCompressorAdcToCurrent(m_AirCompressor_electricCurrentAdc.getCacheVal()); } + + public: + void onAdcCaptureThread(); + + private: +}; +} // namespace iflytop \ No newline at end of file diff --git a/usrc/board/share/h2o2_sensor_driver.cpp b/usrc/board/share/h2o2_sensor_driver.cpp new file mode 100644 index 0000000..05ac188 --- /dev/null +++ b/usrc/board/share/h2o2_sensor_driver.cpp @@ -0,0 +1,88 @@ +#include "h2o2_sensor_driver.hpp" +using namespace iflytop; + +#define TAG "LSDMPowerCtrlBoard" + +static osThreadId H2O2CaptureThreadId; +/*********************************************************************************************************************** + * FUNC * + ***********************************************************************************************************************/ +static void c_onH2O2CaptureThread(void const* argument) { + H2O2SensorDriver* driver = (H2O2SensorDriver*)argument; + driver->onH2O2CaptureThread(); +} + +void H2O2SensorDriver::initialize(UART_HandleTypeDef* huart, ADC_HandleTypeDef* hadc, int32_t adcChannel) { + ZASSERT(huart->Init.BaudRate == 19200); + m_ModbusBlockHost.initialize(huart); + m_H2O2Adc.initialize("H2O2Adc", hadc, adcChannel); + m_HMP110.init(&m_ModbusBlockHost); + + if (m_HMP110.ping(1)) { + m_detectId = 1; + } + if (m_HMP110.ping(240)) { + m_detectId = 240; + } + m_HMP110.setid(m_detectId); + ZLOGI(TAG, "H2O2 HMP110 Sensor detect id: %d", m_detectId); + + osThreadDef(H2O2CaptureThread, c_onH2O2CaptureThread, osPriorityNormal, 0, 1024); + H2O2CaptureThreadId = osThreadCreate(osThread(H2O2CaptureThread), this); +} + +/*********************************************************************************************************************** + * H2O2 * + ***********************************************************************************************************************/ + +bool H2O2SensorDriver::h2o2_sensor_is_online() { + if (m_detectId <= 0) return false; + int32_t ecode = m_HMP110.read_cache_errorcode(); + if (ecode == -1) return false; + return true; +} +int32_t H2O2SensorDriver::h2o2_sensor_read_calibration_date(int32_t* year, int32_t* month, int32_t* day) { // + *year = 1; + *month = 2; + *day = 3; + return 0; +} +int32_t H2O2SensorDriver::h2o2_sensor_read_sub_ic_errorcode() { // + return m_HMP110.read_cache_errorcode(); +} +int32_t H2O2SensorDriver::h2o2_sensor_read_sub_ic_reg(int32_t add, uint16_t* val, size_t len) { // + return m_HMP110.read_reg(add, val, len); +} +int32_t H2O2SensorDriver::h2o2_sensor_data(report_h2o2_data_t* readdata) { + HMP110::hmp110_sensordata_t sensordata; + int h2o2adcData = 0; + + h2o2adcData = m_H2O2Adc.getCacheVal(); + m_HMP110.read_cache_sensor_data(&sensordata); + + readdata->sensor_error = m_HMP110.read_cache_errorcode() != 0; + readdata->h2o2 = h2o2adcToPPM(h2o2adcData); + readdata->humid = sensordata.rh; + readdata->temp = sensordata.temp; + readdata->saturation = 0; + ZLOGI(TAG, "ppm:%d, rh:%d, temp:%d, df_ptemp:%d, ah:%d, mr:%d, wbt:%d, eh:%d", // + readdata->h2o2, // + sensordata.rh, // + sensordata.temp, // + sensordata.df_ptemp, // + sensordata.ah, // + sensordata.mr, // + sensordata.wet_bulb_temp, // + sensordata.enthalpy); + return 0; +} + +void H2O2SensorDriver::onH2O2CaptureThread() { + while (1) { + osDelay(1000); + if (m_detectId > 0) { + m_H2O2Adc.updateAdcValToCache(); + m_HMP110.updateSensorDataAndErrorcode(); + } + } +} \ No newline at end of file diff --git a/usrc/board/share/h2o2_sensor_driver.hpp b/usrc/board/share/h2o2_sensor_driver.hpp new file mode 100644 index 0000000..11b035c --- /dev/null +++ b/usrc/board/share/h2o2_sensor_driver.hpp @@ -0,0 +1,43 @@ +#pragma once +#include +#include + +#include "zsdk/zsdk.hpp" +#include "zstm32/zstm32.hpp" + +// +#include "zsdk/hmp110/hmp110.hpp" +#include "zsdk/hpp272/hpp272.hpp" +#include "zsdk/modbus/modbus_block_host.hpp" +#include "zsdk/pxx_pressure_sensor_driver/pxx_pressure_sensor_bus.hpp" +#include "zsdk/zadc.hpp" +#include "zsdk/zsdk.hpp" +// +#include "formula/formula.hpp" +#include "transmit_disfection_protocol/transmit_disfection_protocol.hpp" +namespace iflytop { +#define H2O2_SENSOR_TYPE_HMP110 +using namespace transmit_disfection_protocol; + +class H2O2SensorDriver { + public: + ModbusBlockHost m_ModbusBlockHost; // + ZADC m_H2O2Adc; // H2O2传感器控制 + HMP110 m_HMP110; // H2O2传感器 + int32_t m_detectId = -1; + + public: + + void initialize(UART_HandleTypeDef* huart, ADC_HandleTypeDef* hadc, int32_t adcChannel); + bool h2o2_sensor_is_online(); + int32_t h2o2_sensor_read_calibration_date(int32_t* year, int32_t* month, int32_t* day); + int32_t h2o2_sensor_read_sub_ic_errorcode(); + int32_t h2o2_sensor_read_sub_ic_reg(int32_t add, uint16_t* val, size_t len); + int32_t h2o2_sensor_data(report_h2o2_data_t* sensorData); + + public: + void onH2O2CaptureThread(); + + private: +}; +} // namespace iflytop \ No newline at end of file diff --git a/usrc/formula/formula.hpp b/usrc/formula/formula.hpp new file mode 100644 index 0000000..1489e1c --- /dev/null +++ b/usrc/formula/formula.hpp @@ -0,0 +1,33 @@ +#pragma once +#include + +static inline int32_t h2o2adcToPPM(int32_t h2o2adcVal) { + double h2o2ma = (h2o2adcVal / 4095.0 * 3.3 * 1000) / 150.0; + int32_t h2o2ppm = (h2o2ma - 4) * 1.0 / (20 - 4) * 2000; + if (h2o2ppm < 0) h2o2ppm = 0; + return h2o2ppm; +} + +static inline int32_t hearterAdcToCurrent(int32_t adcv) { + int32_t ma = (adcv * 0.00336 - 2.5) * 1000; + if (ma < 50) ma = 0; + return ma; +} + +static inline int32_t blowserAdcToCurrent(int32_t adcv) { + int32_t ma = (adcv * 0.00167 - 1.25) * 1000; + if (ma < 50) ma = 0; + return ma; +} + +static inline int32_t airCompressorAdcToCurrent(int32_t adcv) { + int32_t ma = (adcv * 0.00167 - 1.25) * 1000; + if (ma < 50) ma = 0; + return ma; +} + +static inline int32_t heaterAdc2Temp(int32_t adcv) { + int32_t ma = (adcv / 4095.0 * 3.3 * 1000) / 150.0; + int32_t temp = (ma - 4) / (20 - 4) * (4000 - 0) + 0; // 4000:是量程 + return temp; // C*10 +} \ No newline at end of file diff --git a/usrc/protocol_processer_impl/large_space_dm_power_ctrl_board_pp.cpp b/usrc/protocol_processer_impl/large_space_dm_power_ctrl_board_pp.cpp new file mode 100644 index 0000000..933c39c --- /dev/null +++ b/usrc/protocol_processer_impl/large_space_dm_power_ctrl_board_pp.cpp @@ -0,0 +1,77 @@ +#include "large_space_dm_power_ctrl_board_pp.hpp" + +#include "board/large_space_dm_power_ctrl_board.hpp" +#include "share/h2o2_sensor_pp.hpp" +using namespace iflytop; + +#define TAG "LargeSpaceDmPowerCtrlBoardPP" +#define ThisClass LargeSpaceDmPowerCtrlBoardPP +#define DEVICE LargeSpaceDmPowerCtrlBoard::ins() + +/*********************************************************************************************************************** + * LargeSpaceDmPowerCtrlBoardPP * + ***********************************************************************************************************************/ + +void LargeSpaceDmPowerCtrlBoardPP::initialize() { + REG_FN(heater_ctrl); + REG_FN(heater_ctrl_safe_valve); + REG_FN(heater_read_electric_current); + REG_FN(heater_read_temperature_data); + REG_FN(blower_ctrl); + REG_FN(blower_ctrl_safe_valve); + REG_FN(blower_read_electric_current); + REG_FN(air_compressor_ctrl); + REG_FN(air_compressor_ctrl_safe_valve); + REG_FN(air_compressor_read_electric_current); + + BIND_FN(H2O2SensorPP, h2o2_sensor_read_calibration_date); + BIND_FN(H2O2SensorPP, h2o2_sensor_read_sub_ic_errorcode); + BIND_FN(H2O2SensorPP, h2o2_sensor_read_sub_ic_reg); + + H2O2SensorPP::ins()->initialize(&DEVICE->m_h2o2Sensor); +} + +/*********************************************************************************************************************** + * PUMP * + ***********************************************************************************************************************/ + +void LargeSpaceDmPowerCtrlBoardPP::heater_ctrl(ProcessContext* cxt) { // + DEVICE->heater_ctrl(GET_PARAM(0)); + zcanbus_send_ack(cxt->packet, NULL, 0); +} +void LargeSpaceDmPowerCtrlBoardPP::heater_ctrl_safe_valve(ProcessContext* cxt) { // + DEVICE->heater_ctrl_safe_valve(GET_PARAM(0)); + zcanbus_send_ack(cxt->packet, NULL, 0); +} +void LargeSpaceDmPowerCtrlBoardPP::heater_read_electric_current(ProcessContext* cxt) { // + auto val = DEVICE->heater_read_electric_current(); + zcanbus_send_ack(cxt->packet, (uint8_t*)val, sizeof(val)); +} +void LargeSpaceDmPowerCtrlBoardPP::heater_read_temperature_data(ProcessContext* cxt) { // + auto val = DEVICE->heater_read_temperature_data(); + zcanbus_send_ack(cxt->packet, (uint8_t*)val, sizeof(val)); +} +void LargeSpaceDmPowerCtrlBoardPP::blower_ctrl(ProcessContext* cxt) { // + DEVICE->blower_ctrl(GET_PARAM(0)); + zcanbus_send_ack(cxt->packet, NULL, 0); +} +void LargeSpaceDmPowerCtrlBoardPP::blower_ctrl_safe_valve(ProcessContext* cxt) { // + DEVICE->blower_ctrl_safe_valve(GET_PARAM(0)); + zcanbus_send_ack(cxt->packet, NULL, 0); +} +void LargeSpaceDmPowerCtrlBoardPP::blower_read_electric_current(ProcessContext* cxt) { // + auto val = DEVICE->blower_read_electric_current(); + zcanbus_send_ack(cxt->packet, (uint8_t*)val, sizeof(val)); +} +void LargeSpaceDmPowerCtrlBoardPP::air_compressor_ctrl(ProcessContext* cxt) { // + DEVICE->air_compressor_ctrl(GET_PARAM(0)); + zcanbus_send_ack(cxt->packet, NULL, 0); +} +void LargeSpaceDmPowerCtrlBoardPP::air_compressor_ctrl_safe_valve(ProcessContext* cxt) { // + DEVICE->air_compressor_ctrl_safe_valve(GET_PARAM(0)); + zcanbus_send_ack(cxt->packet, NULL, 0); +} +void LargeSpaceDmPowerCtrlBoardPP::air_compressor_read_electric_current(ProcessContext* cxt) { // + auto val = DEVICE->air_compressor_read_electric_current(); + zcanbus_send_ack(cxt->packet, (uint8_t*)val, sizeof(val)); +} \ No newline at end of file diff --git a/usrc/protocol_processer_impl/large_space_dm_power_ctrl_board_pp.hpp b/usrc/protocol_processer_impl/large_space_dm_power_ctrl_board_pp.hpp new file mode 100644 index 0000000..61ab41a --- /dev/null +++ b/usrc/protocol_processer_impl/large_space_dm_power_ctrl_board_pp.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "protocol_processer_mgr/i_protocol_processer.hpp" + +namespace iflytop { +using namespace std; +using namespace zscanprotocol; +using namespace transmit_disfection_protocol; + +class LargeSpaceDmPowerCtrlBoardPP : public IProtocolProcesser { + uint8_t boardResetFlag; // 0: 重启标志 + + public: + static LargeSpaceDmPowerCtrlBoardPP* ins() { + static LargeSpaceDmPowerCtrlBoardPP ins; + return &ins; + } + + virtual void initialize() override; + virtual const char* getName() override { return "LargeSpaceDmPowerCtrlBoardPP"; }; + + private: + void heater_ctrl(ProcessContext* cxt); + void heater_ctrl_safe_valve(ProcessContext* cxt); + void heater_read_electric_current(ProcessContext* cxt); + void heater_read_temperature_data(ProcessContext* cxt); + + void blower_ctrl(ProcessContext* cxt); + void blower_ctrl_safe_valve(ProcessContext* cxt); + void blower_read_electric_current(ProcessContext* cxt); + + void air_compressor_ctrl(ProcessContext* cxt); + void air_compressor_ctrl_safe_valve(ProcessContext* cxt); + void air_compressor_read_electric_current(ProcessContext* cxt); +}; + +} // namespace iflytop diff --git a/usrc/protocol_processer_impl/processer.hpp b/usrc/protocol_processer_impl/processer.hpp index aaca13b..2da8665 100644 --- a/usrc/protocol_processer_impl/processer.hpp +++ b/usrc/protocol_processer_impl/processer.hpp @@ -1,3 +1,4 @@ #pragma once #include "public_cmd_processer.hpp" -#include "liquid_ctrl_board_cmd_processer.hpp" \ No newline at end of file +#include "liquid_ctrl_board_cmd_processer.hpp" +#include "large_space_dm_power_ctrl_board_pp.hpp" \ No newline at end of file diff --git a/usrc/protocol_processer_impl/share/h2o2_sensor_pp.cpp b/usrc/protocol_processer_impl/share/h2o2_sensor_pp.cpp new file mode 100644 index 0000000..bcf5e37 --- /dev/null +++ b/usrc/protocol_processer_impl/share/h2o2_sensor_pp.cpp @@ -0,0 +1,47 @@ +#include "h2o2_sensor_pp.hpp" +using namespace iflytop; + +#define TAG "H2O2SensorPP" +#define ThisClass H2O2SensorPP +#define DEVICE LargeSpaceDmPowerCtrlBoard::ins() + +/*********************************************************************************************************************** + * H2O2SensorPP * + ***********************************************************************************************************************/ + +static osTimerId PacketReportTimerId; +H2O2SensorDriver* m_h2o2sd; + +static void onPacketReportTimer(void const* argument) { + static report_h2o2_data_t data; + memset(&data, 0, sizeof(data)); + m_h2o2sd->h2o2_sensor_data(&data); + zcanbus_send_report(kreport_h2o2_sensor_data, (uint8_t*)&data, sizeof(data), 30); +} + +void H2O2SensorPP::initialize(H2O2SensorDriver* h2o2sd) { + m_h2o2sd = h2o2sd; + osTimerDef(PacketReportTimer, onPacketReportTimer); + PacketReportTimerId = osTimerCreate(osTimer(PacketReportTimer), osTimerPeriodic, NULL); + osTimerStart(PacketReportTimerId, 5000); +} + +void H2O2SensorPP::h2o2_sensor_read_calibration_date(ProcessContext* cxt) { + int32_t data[3]; + m_h2o2sd->h2o2_sensor_read_calibration_date(&data[0], &data[1], &data[2]); + zcanbus_send_ack(cxt->packet, (uint8_t*)&data, sizeof(data)); +} +void H2O2SensorPP::h2o2_sensor_read_sub_ic_errorcode(ProcessContext* cxt) { + int32_t ecode = m_h2o2sd->h2o2_sensor_read_sub_ic_errorcode(); + zcanbus_send_ack(cxt->packet, (uint8_t*)&ecode, sizeof(ecode)); +} +void H2O2SensorPP::h2o2_sensor_read_sub_ic_reg(ProcessContext* cxt) { + int32_t addr = GET_PARAM(0); + int32_t regNum = GET_PARAM(1); + if (regNum > 10) regNum = 10; // 最多读10个寄存器 + static uint16_t data[10]; + memset(data, 0, sizeof(data)); + + m_h2o2sd->h2o2_sensor_read_sub_ic_reg(addr, data, regNum); + zcanbus_send_ack(cxt->packet, (uint8_t*)&data, regNum * 2); +} \ No newline at end of file diff --git a/usrc/protocol_processer_impl/share/h2o2_sensor_pp.hpp b/usrc/protocol_processer_impl/share/h2o2_sensor_pp.hpp new file mode 100644 index 0000000..c39bd48 --- /dev/null +++ b/usrc/protocol_processer_impl/share/h2o2_sensor_pp.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "protocol_processer_mgr/i_protocol_processer.hpp" + +namespace iflytop { +using namespace std; +using namespace zscanprotocol; +using namespace transmit_disfection_protocol; + +class H2O2SensorPP { + + public: + static H2O2SensorPP* ins() { + static H2O2SensorPP instance; + return &instance; + } + + void initialize(H2O2SensorDriver* h2o2sd); + + public: + void h2o2_sensor_read_calibration_date(ProcessContext* cxt); + void h2o2_sensor_read_sub_ic_errorcode(ProcessContext* cxt); + void h2o2_sensor_read_sub_ic_reg(ProcessContext* cxt); +}; + +} // namespace iflytop diff --git a/usrc/protocol_processer_mgr/i_protocol_processer.hpp b/usrc/protocol_processer_mgr/i_protocol_processer.hpp index ff60260..a80e075 100644 --- a/usrc/protocol_processer_mgr/i_protocol_processer.hpp +++ b/usrc/protocol_processer_mgr/i_protocol_processer.hpp @@ -16,7 +16,9 @@ using namespace std; using namespace zscanprotocol; using namespace transmit_disfection_protocol; -#define REG_FN(fn) cmdprocesser.push_back(CmdProcesser(kfn_##fn, bind(&ThisClass::fn, this, placeholders::_1))) +#define REG_FN(fn) cmdprocesser.push_back(CmdProcesser(kfn_##fn, bind(&ThisClass::fn, this, placeholders::_1))) +#define BIND_FN(ClassName, fn) cmdprocesser.push_back(CmdProcesser(kfn_##fn, bind(&ClassName::fn, ClassName::ins(), placeholders::_1))); + #define GET_PARAM(off) ((((int32_t*)(cxt->packet->params))[off])) #define PRAAM_LEN() ((cxt->packetlen - sizeof(zcanbus_packet_t)) / 4) @@ -81,6 +83,4 @@ class IProtocolProcesser { virtual ~IProtocolProcesser() {} }; - - } // namespace iflytop \ No newline at end of file diff --git a/usrc/utils/halutils.cpp b/usrc/utils/halutils.cpp new file mode 100644 index 0000000..e69de29 diff --git a/usrc/utils/halutils.hpp b/usrc/utils/halutils.hpp new file mode 100644 index 0000000..a384301 --- /dev/null +++ b/usrc/utils/halutils.hpp @@ -0,0 +1,52 @@ +#pragma once +#include +#include + +#include "zsdk/zsdk.hpp" +#include "zstm32/zstm32.hpp" + +namespace iflytop { + +class HalUtils { + public: + static void uartdmainit(UART_HandleTypeDef* uart, DMA_HandleTypeDef* txdma, uint32_t txdmach, DMA_HandleTypeDef* rxdma, uint32_t rxdmach) { + /* USART2 DMA Init */ + /* USART2_RX Init */ + rxdma->Instance = getDmaStreamTypeDef(rxdma); + rxdma->Init.Channel = rxdmach; + rxdma->Init.Direction = DMA_PERIPH_TO_MEMORY; + rxdma->Init.PeriphInc = DMA_PINC_DISABLE; + rxdma->Init.MemInc = DMA_MINC_ENABLE; + rxdma->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + rxdma->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + rxdma->Init.Mode = DMA_NORMAL; + rxdma->Init.Priority = DMA_PRIORITY_LOW; + rxdma->Init.FIFOMode = DMA_FIFOMODE_DISABLE; + if (HAL_DMA_Init(rxdma) != HAL_OK) { + Error_Handler(); + } + + __HAL_LINKDMA(uart, hdmarx, *rxdma); + + /* USART2_TX Init */ + txdma->Instance = getDmaStreamTypeDef(txdma); + txdma->Init.Channel = txdmach; + txdma->Init.Direction = DMA_MEMORY_TO_PERIPH; + txdma->Init.PeriphInc = DMA_PINC_DISABLE; + txdma->Init.MemInc = DMA_MINC_ENABLE; + txdma->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + txdma->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + txdma->Init.Mode = DMA_NORMAL; + txdma->Init.Priority = DMA_PRIORITY_LOW; + txdma->Init.FIFOMode = DMA_FIFOMODE_DISABLE; + if (HAL_DMA_Init(txdma) != HAL_OK) { + Error_Handler(); + } + + __HAL_LINKDMA(uart, hdmatx, *txdma); + + + } +}; + +} // namespace iflytop \ No newline at end of file diff --git a/zsdk b/zsdk index 21ce93c..b05095f 160000 --- a/zsdk +++ b/zsdk @@ -1 +1 @@ -Subproject commit 21ce93cb5aaa9686b147274b08e9d8007cef6334 +Subproject commit b05095f6f18dbf58877e487caa0013066d077612 diff --git a/zstm32/halport/gins.c b/zstm32/halport/gins.c index 916ec52..8d0263d 100644 --- a/zstm32/halport/gins.c +++ b/zstm32/halport/gins.c @@ -34,6 +34,26 @@ DEFINE_GLOBAL(DMA_HandleTypeDef, hdma2_stream5); DEFINE_GLOBAL(DMA_HandleTypeDef, hdma2_stream6); DEFINE_GLOBAL(DMA_HandleTypeDef, hdma2_stream7); +DMA_Stream_TypeDef* getDmaStreamTypeDef(DMA_HandleTypeDef* dmah) { + if (dmah->Instance) return dmah->Instance; + + if (dmah == &hdma1_stream1) return DMA1_Stream1; + if (dmah == &hdma1_stream2) return DMA1_Stream2; + if (dmah == &hdma1_stream3) return DMA1_Stream3; + if (dmah == &hdma1_stream4) return DMA1_Stream4; + if (dmah == &hdma1_stream5) return DMA1_Stream5; + if (dmah == &hdma1_stream6) return DMA1_Stream6; + if (dmah == &hdma1_stream7) return DMA1_Stream7; + if (dmah == &hdma2_stream0) return DMA2_Stream0; + if (dmah == &hdma2_stream1) return DMA2_Stream1; + if (dmah == &hdma2_stream2) return DMA2_Stream2; + if (dmah == &hdma2_stream3) return DMA2_Stream3; + if (dmah == &hdma2_stream4) return DMA2_Stream4; + if (dmah == &hdma2_stream5) return DMA2_Stream5; + if (dmah == &hdma2_stream6) return DMA2_Stream6; + if (dmah == &hdma2_stream7) return DMA2_Stream7; + return NULL; +} DEFINE_GLOBAL(SPI_HandleTypeDef, hspi1); DEFINE_GLOBAL(SPI_HandleTypeDef, hspi2); @@ -43,14 +63,5 @@ DEFINE_GLOBAL(ADC_HandleTypeDef, hadc1); /*********************************************************************************************************************** * PTR * ***********************************************************************************************************************/ -DEFINE_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart1_rx); -DEFINE_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart1_tx); -DEFINE_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart2_rx); -DEFINE_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart2_tx); -DEFINE_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart3_rx); -DEFINE_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart3_tx); -DEFINE_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart4_rx); -DEFINE_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart4_tx); - DEFINE_GLOBAL(IWDG_HandleTypeDef, hiwdg); diff --git a/zstm32/halport/gins.h b/zstm32/halport/gins.h index 4a0fd7a..e43c83a 100644 --- a/zstm32/halport/gins.h +++ b/zstm32/halport/gins.h @@ -8,13 +8,11 @@ extern "C" { #endif -#define EXTERN_GLOBAL(type, name) \ - extern type name; \ +#define EXTERN_GLOBAL(type, name) extern type name; #define EXTERN_GLOBAL_PTR(type, name) extern type* name; -#define DEFINE_GLOBAL(type, name) \ - type name; \ +#define DEFINE_GLOBAL(type, name) type name; #define DEFINE_GLOBAL_PTR(type, name) type* name; @@ -61,23 +59,9 @@ EXTERN_GLOBAL(SPI_HandleTypeDef, hspi3); EXTERN_GLOBAL(ADC_HandleTypeDef, hadc1); - EXTERN_GLOBAL(IWDG_HandleTypeDef, hiwdg); - - -/*********************************************************************************************************************** - * PTR * - ***********************************************************************************************************************/ - -EXTERN_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart1_rx); -EXTERN_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart1_tx); -EXTERN_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart2_rx); -EXTERN_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart2_tx); -EXTERN_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart3_rx); -EXTERN_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart3_tx); -EXTERN_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart4_rx); -EXTERN_GLOBAL_PTR(DMA_HandleTypeDef, hdma_usart4_tx); +DMA_Stream_TypeDef* getDmaStreamTypeDef(DMA_HandleTypeDef* dmah); #ifdef __cplusplus }