diff --git a/Core/Inc/adc_if.h b/Core/Inc/adc_if.h index fc660c3..e22c0ba 100644 --- a/Core/Inc/adc_if.h +++ b/Core/Inc/adc_if.h @@ -91,7 +91,11 @@ int16_t SYS_GetTemperatureLevel(void); uint16_t SYS_GetBatteryLevel(void); /* USER CODE BEGIN EFP */ - +/** + * @brief YUNHORN-STS Get the current VBAT--INPUT level + * @return value battery level in linear scale + */ +uint16_t STS_GetBatteryLevel(void); /* USER CODE END EFP */ #ifdef __cplusplus diff --git a/Core/Inc/main.h b/Core/Inc/main.h index e5979d3..c24e068 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -80,6 +80,13 @@ void MX_TOF_Init(void); #if defined(STM32WLE5xx) #define LED1_Pin GPIO_PIN_2 #define LED1_GPIO_Port GPIOB +#define STS_LED1_Pin GPIO_PIN_2 +#define STS_LED1_GPIO_Port GPIOB +#define STS_LED1_Pin GPIO_PIN_2 +#define STS_LED1_GPIO_Port GPIOB +#define STS_LED1_ON HAL_GPIO_WritePin(STS_LED1_GPIO_Port, STS_LED1_Pin, GPIO_PIN_SET) +#define STS_LED1_OFF HAL_GPIO_WritePin(STS_LED1_GPIO_Port, STS_LED1_Pin, GPIO_PIN_RESET) + #define LED_ON HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET ) #define LED_OFF HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET ) #define LED_TOGGLE HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin) @@ -110,6 +117,9 @@ void MX_TOF_Init(void); #define USARTx_TX_Pin GPIO_PIN_2 #define USARTx_TX_GPIO_Port GPIOA +#define STS_BAT_DETECT_PIN GPIO_PIN_15 +#define STS_BAT_DETECT_Port GPIOA + #define MEMS_POWER_Pin GPIO_PIN_4 // PMU_ENABLE #define MEMS_POWER_GPIO_Port GPIOB // PMU_ENABLE diff --git a/Core/Inc/yunhorn_sts_prd_conf.h b/Core/Inc/yunhorn_sts_prd_conf.h index 8bec17f..26c7db0 100644 --- a/Core/Inc/yunhorn_sts_prd_conf.h +++ b/Core/Inc/yunhorn_sts_prd_conf.h @@ -148,6 +148,10 @@ #ifdef STS_USE_TOF_VL53L0X // RETIRED TODO #define I2C_TIMING_TOF250 0x20303E5D #define I2C_TIMING 0x20303E5D //DEFAULT FOR I2C TOF + // FOR STM32WLE5CCU6 UFQFPN48 PA12 PA11 +#define I2C2_STANDARD_100K 0x00000E14 +#define I2C2_FAST_400K 0x00000004 +#define I2C2_FAST_PLUS_1M 0x00000000 #endif #define STS_Status_Door_Close (0) //Normal Close NC:Open diff --git a/Core/Src/adc.c b/Core/Src/adc.c index 6cf7e15..3a81ba6 100644 --- a/Core/Src/adc.c +++ b/Core/Src/adc.c @@ -73,7 +73,7 @@ void MX_ADC_Init(void) void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) { - + GPIO_InitTypeDef GPIO_InitStruct = {0}; if(adcHandle->Instance==ADC) { /* USER CODE BEGIN ADC_MspInit 0 */ @@ -82,7 +82,15 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) /* ADC clock enable */ __HAL_RCC_ADC_CLK_ENABLE(); /* USER CODE BEGIN ADC_MspInit 1 */ - + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**ADC GPIO Configuration + PB3 ------> ADC_IN2 + PA15 ------> ADC_IN11 + */ + GPIO_InitStruct.Pin = STS_BAT_DETECT_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(STS_BAT_DETECT_Port, &GPIO_InitStruct); /* USER CODE END ADC_MspInit 1 */ } } @@ -98,7 +106,11 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) /* Peripheral clock disable */ __HAL_RCC_ADC_CLK_DISABLE(); /* USER CODE BEGIN ADC_MspDeInit 1 */ - + /**ADC GPIO Configuration + PB3 ------> ADC_IN2 + PA15 ------> ADC_IN11 + */ + HAL_GPIO_DeInit(STS_BAT_DETECT_Port, STS_BAT_DETECT_PIN); /* USER CODE END ADC_MspDeInit 1 */ } } diff --git a/Core/Src/adc_if.c b/Core/Src/adc_if.c index 4a0e362..5442950 100644 --- a/Core/Src/adc_if.c +++ b/Core/Src/adc_if.c @@ -145,8 +145,7 @@ uint16_t SYS_GetBatteryLevel(void) uint16_t batteryLevelmV = 0; uint32_t measuredLevel = 0; - //measuredLevel = ADC_ReadChannels(ADC_CHANNEL_VREFINT); - measuredLevel = ADC_ReadChannels(ADC_CHANNEL_5); //ADC5, PA15 for VBAT measure 2024-07-04 + measuredLevel = ADC_ReadChannels(ADC_CHANNEL_VREFINT); if (measuredLevel == 0) { @@ -179,6 +178,42 @@ uint16_t SYS_GetBatteryLevel(void) /* Private Functions Definition -----------------------------------------------*/ /* USER CODE BEGIN PrFD */ +uint16_t STS_GetBatteryLevel(void) +{ + /* USER CODE BEGIN SYS_GetBatteryLevel_1 */ + + /* USER CODE END SYS_GetBatteryLevel_1 */ + uint16_t batteryLevelmV = 0; + uint32_t measuredLevel = 0; + + measuredLevel = ADC_ReadChannels(LL_ADC_CHANNEL_11); //LL_ADC_CHANNEL_14 + + if (measuredLevel == 0) + { + batteryLevelmV = 0; + } + else + { + if ((uint32_t)*VREFINT_CAL_ADDR != (uint32_t)0xFFFFU) + { + /* Device with Reference voltage calibrated in production: + use device optimized parameters */ + batteryLevelmV = __LL_ADC_CALC_VREFANALOG_VOLTAGE(measuredLevel, + ADC_RESOLUTION_12B); + } + else + { + /* Device with Reference voltage not calibrated in production: + use generic parameters */ + batteryLevelmV = (uint16_t)(VREFINT_CAL_VREF * 1510) / measuredLevel; + } + } + + return (uint16_t)2*batteryLevelmV; + /* USER CODE BEGIN SYS_GetBatteryLevel_2 */ + + /* USER CODE END SYS_GetBatteryLevel_2 */ +} /* USER CODE END PrFD */ static uint32_t ADC_ReadChannels(uint32_t channel) diff --git a/Core/Src/dma.c b/Core/Src/dma.c index b2e80c9..343b6b8 100644 --- a/Core/Src/dma.c +++ b/Core/Src/dma.c @@ -77,7 +77,7 @@ void MX_DMA_Init(void) HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn); #endif -#if 0 +#if 1 /* DMA1_Channel7_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 2, 0); HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn); diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index 2ea6d49..17b3c58 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -65,7 +65,9 @@ void MX_GPIO_Init(void) #if 1 __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); + //__HAL_RCC_GPIOC_CLK_ENABLE(); +#endif +#if 0 //0b0000 0000 0000 0000 //0b0001 1010 0000 0000 INCLUDE PA9 PA11 PA12 --> 0x1A00 //0b1110 0101 1111 1111 exclude PA9 PA11 PA12 --> 0xE5FF diff --git a/Core/Src/i2c.c b/Core/Src/i2c.c index 89074ee..06748de 100644 --- a/Core/Src/i2c.c +++ b/Core/Src/i2c.c @@ -44,7 +44,8 @@ void MX_I2C2_Init(void) /* USER CODE END I2C2_Init 1 */ hi2c2.Instance = I2C2; - hi2c2.Init.Timing = 0x20303E5D; //0x2010091A = 400K Fast Mode, 0x20303E5D, 100K Standard mode, 0x20000209 Fast Mode Plus, 1Mbps + //hi2c2.Init.Timing = 0x20303E5D; //0x2010091A = 400K Fast Mode, 0x20303E5D, 100K Standard mode, 0x20000209 Fast Mode Plus, 1Mbps + hi2c2.Init.Timing = I2C2_FAST_400K; // 2024-07-09 UPDATE hi2c2.Init.OwnAddress1 = 0; hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; diff --git a/Core/Src/main.c b/Core/Src/main.c index ebe5c74..7909695 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -113,9 +113,9 @@ int main(void) MX_I2C2_Init(); MX_LoRaWAN_Init(); - LED_ON; - HAL_Delay(200); - LED_OFF; + //LED_ON; + //HAL_Delay(200); + //LED_OFF; #if 0 if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) == RESET) { diff --git a/Core/Src/stm32wlxx_it.c b/Core/Src/stm32wlxx_it.c index 9abcb85..bc049b4 100644 --- a/Core/Src/stm32wlxx_it.c +++ b/Core/Src/stm32wlxx_it.c @@ -358,14 +358,14 @@ void DMA1_Channel7_IRQHandler(void) /* USER CODE END DMA1_Channel7_IRQn 1 */ } #endif -#if 0 +#if 1 void EXTI3_IRQHandler(void) { /* USER CODE BEGIN EXTI3_IRQn 0 */ /* USER CODE END EXTI3_IRQn 0 */ #ifdef STM32WLE5xx - //HAL_GPIO_EXTI_IRQHandler(TOF_INT_EXTI_PIN); + HAL_GPIO_EXTI_IRQHandler(TOF_INT_EXTI_PIN); #endif /* USER CODE BEGIN EXTI3_IRQn 1 */ diff --git a/LoRaWAN/App/lora_app.c b/LoRaWAN/App/lora_app.c index 9bd0a54..716e18d 100644 --- a/LoRaWAN/App/lora_app.c +++ b/LoRaWAN/App/lora_app.c @@ -785,7 +785,8 @@ static void SendTxData(void) /* USER CODE BEGIN SendTxData_1 */ LmHandlerErrorStatus_t status = LORAMAC_HANDLER_ERROR; uint8_t batteryLevel = GetBatteryLevel(); - uint16_t batteryLevelmV = SYS_GetBatteryLevel(); + //uint16_t batteryLevelmV = SYS_GetBatteryLevel(); + uint16_t batteryLevelmV = STS_GetBatteryLevel(); //sensor_t sensor_data; #if defined(YUNHORN_STS_R0_ENABLED)||defined(YUNHORN_STS_R5_ENABLED) STS_R0_SensorDataTypeDef r0_data; @@ -806,14 +807,17 @@ static void SendTxData(void) #endif #if defined(YUNHORN_STS_R0_ENABLED)||defined(YUNHORN_STS_R5_ENABLED) - STS_SENSOR_Power_ON(1); - HAL_Delay(1000); + PME_ON; + LED_ON; + //HAL_Delay(1000); MX_TOF_Init(); MX_TOF_Process(); STS_R0_SENSOR_Read(&r0_data); - HAL_Delay(1000); - STS_SENSOR_Power_OFF(1); + //HAL_Delay(1000); + PME_OFF; + LED_OFF; r0_data.battery_Pct = (uint8_t)(99*batteryLevel/254); + #endif // EnvSensors_Read(&sensor_data); // APP_LOG(TS_ON, VLEVEL_M, "\r\nVDDA: %d\r\n", batteryLevel); @@ -866,8 +870,9 @@ static void SendTxData(void) #ifdef SOAP_LEVEL AppData.Buffer[i++] = (uint8_t)(sts_soap_level_state)&0xff; //#11 #endif -// AppData.Buffer[i++] = (uint8_t)(r0_data.battery_mV >>8)&0xff; //#12 -// AppData.Buffer[i++] = (uint8_t)(r0_data.battery_mV)&0xff; //#13 + //AppData.Buffer[i++] = (uint8_t)(r0_data.battery_mV >>8)&0xff; //#12 + //AppData.Buffer[i++] = (uint8_t)(r0_data.battery_mV/100)&0xff; //#13 + AppData.Buffer[i++] = (uint8_t)(batteryLevelmV/100)&0xff; //#13 } diff --git a/LoRaWAN/App/lora_app.h b/LoRaWAN/App/lora_app.h index a83ec96..11123f6 100644 --- a/LoRaWAN/App/lora_app.h +++ b/LoRaWAN/App/lora_app.h @@ -51,7 +51,7 @@ extern "C" { /*! * Defines the application data transmission duty cycle. 10s, value in [ms]. */ -#define APP_TX_DUTYCYCLE 10000 +#define APP_TX_DUTYCYCLE 20000 /*! * LoRaWAN User application port diff --git a/TOF/App/X-WL55_WLE5_53L0X.c b/TOF/App/X-WL55_WLE5_53L0X.c index 9d9260c..95f447c 100644 --- a/TOF/App/X-WL55_WLE5_53L0X.c +++ b/TOF/App/X-WL55_WLE5_53L0X.c @@ -77,7 +77,7 @@ static void _I2cFailRecover(){ // We can't assume bus state based on SDA and SCL state (we may be in a data or NAK bit so SCL=SDA=1) // by setting SDA high and toggling SCL at least 10 time we ensure whatever agent and state // all agent should end up seeing a "stop" and bus get back to an known idle i2c bus state - + PME_ON; // Enable I/O __GPIOA_CLK_ENABLE(); HAL_GPIO_WritePin(I2C2_SCL_PORT, I2C2_SCL_PIN, GPIO_PIN_SET); @@ -116,7 +116,7 @@ static void _I2cFailRecover(){ HAL_Delay(33); HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET); HAL_Delay(33*20); - }while(1); + }while(0); } } @@ -148,6 +148,7 @@ int XWL55_WLE5_53L0X_I2C2Configure() { XNUCLEO53L1A1_hi2c.Instance = I2C2; //XNUCLEO53L1A1_hi2c.Init.Timing = 0x00300F38; /* set 400KHz fast mode i2c*/ + XNUCLEO53L1A1_hi2c.Init.Timing = I2C2_FAST_400K; //UPDATE 2024-07-09 XNUCLEO53L1A1_hi2c.Init.Timing = 0x2010091A; //0x2010091A = 400K Fast Mode, 0x20303E5D, 100K Standard mode, 0x20000209 Fast Mode Plus, 1Mbps XNUCLEO53L1A1_hi2c.Init.OwnAddress1 = 0;