Skip to content

Commit

Permalink
USE_GPIO EXIT for encoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anol Paisal committed May 29, 2019
1 parent 8562bbe commit 66dac51
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/apps/LoRaMac/encoder/SKiM980A/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/boards/SKiM980A/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/boards/SKiM980A/cmsis/stm32l1xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
80 changes: 64 additions & 16 deletions src/boards/SKiM980A/encoder-board.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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( );
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -203,6 +249,8 @@ void TIM2_IRQHandler( void )
HAL_TIM_IRQHandler( &TimHandle );
}

#endif /* USE_GPIO */

/*!
* \brief Function executed on Led 4 Timeout event
*/
Expand Down
1 change: 1 addition & 0 deletions src/boards/encoder-board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
2 changes: 1 addition & 1 deletion src/system/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 66dac51

Please sign in to comment.