From 66dac51d89bc69e0c22f1dec0342f73e71075496 Mon Sep 17 00:00:00 2001 From: Anol Paisal Date: Wed, 29 May 2019 13:13:53 +0700 Subject: [PATCH] USE_GPIO EXIT for encoder. --- src/apps/LoRaMac/encoder/SKiM980A/main.c | 5 +- src/boards/SKiM980A/board.c | 7 +- .../SKiM980A/cmsis/stm32l1xx_hal_conf.h | 2 +- src/boards/SKiM980A/encoder-board.c | 80 +++++++++++++++---- src/boards/encoder-board.h | 1 + src/system/encoder.h | 2 +- 6 files changed, 75 insertions(+), 22 deletions(-) diff --git a/src/apps/LoRaMac/encoder/SKiM980A/main.c b/src/apps/LoRaMac/encoder/SKiM980A/main.c index 05615d30f..e32ee50cf 100644 --- a/src/apps/LoRaMac/encoder/SKiM980A/main.c +++ b/src/apps/LoRaMac/encoder/SKiM980A/main.c @@ -46,7 +46,7 @@ /*! * Defines the application data transmission duty cycle. 5s, value in [ms]. */ -#define APP_TX_DUTYCYCLE 1000 * 60//60U * 1000U * 15U /* 15 min */ +#define APP_TX_DUTYCYCLE 1000 * 60 // 1 min /*! * Defines a random delay for application data transmission duty cycle. 1s, @@ -391,6 +391,7 @@ static void PrepareTxFrame( uint8_t port ) // Read the current potentiometer setting in percent #if defined( USE_ENCODER ) + memset1(AppDataBuffer, 0x0, LORAWAN_APP_DATA_MAX_SIZE); potiPercentage = BoardGetPotiLevel( ); vdd = BoardGetBatteryLevel( ); AppDataSizeBackup = AppDataSize = 11; @@ -1149,7 +1150,7 @@ int main( void ) LoRaMacInitialization( &macPrimitives, &macCallbacks, ACTIVE_REGION ); // Encoder.OnSendOneshot = OnTxOneShotPacketEvent; - Encoder.OnPulseDetect = OnLed3Toggle; + Encoder.OnShowPulseDetect = OnLed3Toggle; Encoder.ConfigData->isActiveMode = 1; DeviceState = DEVICE_STATE_RESTORE; diff --git a/src/boards/SKiM980A/board.c b/src/boards/SKiM980A/board.c index 1a03356d4..752972ec2 100644 --- a/src/boards/SKiM980A/board.c +++ b/src/boards/SKiM980A/board.c @@ -425,7 +425,10 @@ void SystemClockConfig( void ) __HAL_RCC_PWR_CLK_ENABLE( ); - __HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE1 ); + __HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE2 ); + + /* Wait Until the Voltage Regulator is ready */ + while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET); RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSE ; RCC_OscInitStruct.HSEState = RCC_HSE_ON; @@ -485,7 +488,7 @@ void CalibrateSystemWakeupTime( void ) void SystemClockReConfig( void ) { __HAL_RCC_PWR_CLK_ENABLE( ); - __HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE1 ); + __HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE2 ); /* Wait Until the Voltage Regulator is ready */ while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET); diff --git a/src/boards/SKiM980A/cmsis/stm32l1xx_hal_conf.h b/src/boards/SKiM980A/cmsis/stm32l1xx_hal_conf.h index 77ccfa72d..eb75f600f 100644 --- a/src/boards/SKiM980A/cmsis/stm32l1xx_hal_conf.h +++ b/src/boards/SKiM980A/cmsis/stm32l1xx_hal_conf.h @@ -133,7 +133,7 @@ #define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ #define TICK_INT_PRIORITY ((uint32_t)0) /*!< tick interrupt priority */ #define USE_RTOS 0 -#define PREFETCH_ENABLE 0 +#define PREFETCH_ENABLE 1 #define INSTRUCTION_CACHE_ENABLE 1 #define DATA_CACHE_ENABLE 1 diff --git a/src/boards/SKiM980A/encoder-board.c b/src/boards/SKiM980A/encoder-board.c index 73b97933e..4c281f6d7 100644 --- a/src/boards/SKiM980A/encoder-board.c +++ b/src/boards/SKiM980A/encoder-board.c @@ -26,11 +26,13 @@ #include "math.h" // #include "timer.h" +#define USE_GPIO + #define TAMPER_FLAG 0x1 #define ALARM_FLAG 0x2 #define DIR_FLAG 0x4 -EncoderIrqHandler *EncoderIrq[] = { OnTamperingIrq , OnAlarmIrq, NULL}; +EncoderIrqHandler *EncoderIrq[] = { OnTamperingIrq , OnAlarmIrq, OnPulseDetected, NULL}; Encoder_t Encoder; flow_t flow; @@ -52,6 +54,18 @@ void EncoderInit( Encoder_t *obj, EncoderId_t timId, PinNames pulse, PinNames di if( timId == TIM_2 ) { + GpioInit( &obj->Tampering, tampering, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 1 ); + GpioSetInterrupt( &obj->Tampering, IRQ_RISING_FALLING_EDGE, IRQ_HIGH_PRIORITY, EncoderIrq[0]); + // GpioSetContext(&obj->Tampering, &obj); + GpioInit( &obj->Alarm, alarm, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 1 ); + GpioSetInterrupt( &obj->Alarm, IRQ_FALLING_EDGE, IRQ_HIGH_PRIORITY, EncoderIrq[1]); + // GpioSetContext(&obj->Alarm, &obj); + +#ifdef USE_GPIO + GpioInit( &obj->Pulse, pulse, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 1 ); + GpioSetInterrupt( &obj->Pulse, IRQ_FALLING_EDGE, IRQ_HIGH_PRIORITY, EncoderIrq[2]); + GpioInit( &obj->Direction, dir, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 1 ); +#else __HAL_RCC_TIM2_CLK_ENABLE(); TimHandle.Instance = ( TIM_TypeDef* )TIM2_BASE; @@ -68,14 +82,14 @@ void EncoderInit( Encoder_t *obj, EncoderId_t timId, PinNames pulse, PinNames di TimHandle.Init.Period = 0xffff; TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; sConfig.EncoderMode = TIM_ENCODERMODE_TI1; - sConfig.IC1Polarity = TIM_INPUTCHANNELPOLARITY_FALLING; + sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; sConfig.IC1Prescaler = TIM_ICPSC_DIV1; - sConfig.IC1Filter = 0; - // sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING; - // sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; - // sConfig.IC2Prescaler = TIM_ICPSC_DIV1; - // sConfig.IC2Filter = 0; + sConfig.IC1Filter = 15; + sConfig.IC2Polarity = TIM_ICPOLARITY_FALLING; + sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; + sConfig.IC2Prescaler = TIM_ICPSC_DIV1; + sConfig.IC2Filter = 15; HAL_TIM_Encoder_Init(&TimHandle, &sConfig); sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; @@ -85,19 +99,16 @@ void EncoderInit( Encoder_t *obj, EncoderId_t timId, PinNames pulse, PinNames di HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM2_IRQn); - GpioInit( &obj->Tampering, tampering, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 1 ); - GpioSetInterrupt( &obj->Tampering, IRQ_RISING_FALLING_EDGE, IRQ_VERY_HIGH_PRIORITY, EncoderIrq[0]); - // GpioSetContext(&obj->Tampering, &obj); - GpioInit( &obj->Alarm, alarm, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 1 ); - GpioSetInterrupt( &obj->Alarm, IRQ_FALLING_EDGE, IRQ_VERY_HIGH_PRIORITY, EncoderIrq[1]); - // GpioSetContext(&obj->Alarm, &obj); + HAL_TIM_Encoder_Start_IT(&TimHandle, TIM_CHANNEL_1); +#endif + Encoder.FlowData = &flow; Encoder.LastFlowData = &last_flow; Encoder.ConfigData = &config; // TimerInit( &StorePacketTimer, OnStorePacketTimerEvent ); // TimerSetValue( &StorePacketTimer, 60000 ); - HAL_TIM_Encoder_Start_IT(&TimHandle, TIM_CHANNEL_1); + } CRITICAL_SECTION_END( ); @@ -157,6 +168,41 @@ void OnAlarmIrq( void* context ) } } +#ifdef USE_GPIO + +void OnPulseDetected( void* context ) +{ + CRITICAL_SECTION_BEGIN(); + + if (GpioRead(&Encoder.Direction) != GPIO_PIN_RESET) + { + flow.status |= DIR_FLAG; + flow.fwd_cnt++; + printf( "\r\nForward cnt : %ld\r\n\r\n", flow.fwd_cnt); + } else { + flow.status &= ~DIR_FLAG; + flow.rev_cnt++; + printf( "\r\nBackward cnt : %ld\r\n\r\n", flow.rev_cnt); + } + flow.rate++; + + if (Encoder.OnShowPulseDetect != NULL) + { + Encoder.OnShowPulseDetect(); + } + + if(Encoder.ConfigData->isActiveMode == 0) { + Encoder.ConfigData->isActiveMode = 1; + if (Encoder.OnSendOneshot != NULL) + { + Encoder.OnSendOneshot( ); + } + } + + CRITICAL_SECTION_END(); +} + +#else void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { CRITICAL_SECTION_BEGIN(); @@ -183,9 +229,9 @@ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) CRITICAL_SECTION_END(); - if (Encoder.OnPulseDetect != NULL) + if (Encoder.OnShowPulseDetect != NULL) { - Encoder.OnPulseDetect(); + Encoder.OnShowPulseDetect(); } if(Encoder.ConfigData->isActiveMode == 0) { @@ -203,6 +249,8 @@ void TIM2_IRQHandler( void ) HAL_TIM_IRQHandler( &TimHandle ); } +#endif /* USE_GPIO */ + /*! * \brief Function executed on Led 4 Timeout event */ diff --git a/src/boards/encoder-board.h b/src/boards/encoder-board.h index 99aa987e3..157d04aef 100644 --- a/src/boards/encoder-board.h +++ b/src/boards/encoder-board.h @@ -27,5 +27,6 @@ // An encoder.c file has to be implmented under system directory. void OnTamperingIrq( void* context ); void OnAlarmIrq( void* context ); +void OnPulseDetected( void* context ); #endif // __ENCODER_BOARD_H__ diff --git a/src/system/encoder.h b/src/system/encoder.h index 355049827..47f6d035b 100644 --- a/src/system/encoder.h +++ b/src/system/encoder.h @@ -67,7 +67,7 @@ typedef struct Encoder_s Gpio_t Tampering; Gpio_t Alarm; void ( *OnSendOneshot) ( void ); - void ( *OnPulseDetect ) ( void ); + void ( *OnShowPulseDetect ) ( void ); flow_t * FlowData; flow_t * LastFlowData; flow_config_t * ConfigData;