Skip to content

Commit

Permalink
Mapping PA1 to ADC and 500ms timing for check ARC state.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anol Paisal committed Mar 9, 2022
1 parent 864dd48 commit 438e0dc
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 53 deletions.
100 changes: 61 additions & 39 deletions src/apps/LoRaMac/periodic-uplink-lpp-counter/SKiM980A/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
static uint32_t in_cnt = 0U;
static uint32_t in_out_cnt = 0U;
const volatile uint32_t stop_mode_timer = 8000U; /* Test at 8 sec */
static uint8_t uplink_buffer[8U] = { 0 };
static uint8_t uplink_buffer[9U] = { 0 };
static uint8_t test_read_buffer[8U] = { 0 };

#ifndef ACTIVE_REGION
Expand Down Expand Up @@ -148,10 +148,10 @@ static TimerEvent_t TxTimer;
*/
static TimerEvent_t Led4Timer;

// /*!
// * Timer to handle the state of LED2
// */
// static TimerEvent_t Led2Timer;
/*!
* Timer to handle the state of LED2
*/
static TimerEvent_t Led2Timer;

/*!
* Timer to handle the state of LED beacon indicator
Expand Down Expand Up @@ -191,10 +191,10 @@ static void OnTxTimerEvent( void* context );
*/
static void OnLed4TimerEvent( void* context );

// /*!
// * Function executed on Led 2 Timeout event
// */
// static void OnLed2TimerEvent( void* context );
/*!
* Function executed on Led 2 Timeout event
*/
static void OnLed2TimerEvent( void* context );

/*!
* \brief Function executed on Beacon timer Timeout event
Expand All @@ -206,10 +206,10 @@ static void OnLedBeaconTimerEvent( void* context );
// */
// static TimerEvent_t WKUPTimer;

// /*!
// * Function executed on Wakeup event
// */
// static void OnWKUPTimerEvent(void *context);
/*!
* Function executed on Wakeup event
*/
void OnWKUPEvent(void *context);

/*!
* Function executed on IO1 event
Expand Down Expand Up @@ -278,7 +278,7 @@ static volatile uint8_t IsStopMode = 0;
*/
extern Gpio_t Led4; // Tx
// extern Gpio_t Led2; // Rx and blinks every 5 seconds when beacon is acquired
extern Gpio_t Led3; // App
// extern Gpio_t Led3; // App
extern Gpio_t Wkup;
extern Gpio_t Io1;
extern Gpio_t Io2;
Expand Down Expand Up @@ -311,8 +311,9 @@ int main( void )
TimerInit( &Led4Timer, OnLed4TimerEvent );
TimerSetValue( &Led4Timer, 25 );

// TimerInit( &Led2Timer, OnLed2TimerEvent );
// TimerSetValue( &Led2Timer, 25 );
TimerInit( &Led2Timer, OnLed2TimerEvent );
TimerSetValue( &Led2Timer, 500 );
TimerStart(&Led2Timer);

TimerInit( &LedBeaconTimer, OnLedBeaconTimerEvent );
TimerSetValue( &LedBeaconTimer, 5000 );
Expand Down Expand Up @@ -433,7 +434,7 @@ static void OnRxData( LmHandlerAppData_t* appData, LmHandlerRxParams_t* params )
case LORAWAN_APP_PORT:
{
AppLedStateOn = appData->Buffer[0] & 0x01;
GpioWrite( &Led3, ( ( AppLedStateOn & 0x01 ) != 0 ) ? 1 : 0 );
// GpioWrite( &Led3, ( ( AppLedStateOn & 0x01 ) != 0 ) ? 1 : 0 );
}
break;
default:
Expand Down Expand Up @@ -511,14 +512,14 @@ static void PrepareTxFrame( void )

// CayenneLppReset( );

// uint8_t potiPercentage = 0;
uint8_t potiPercentage = 0;
// uint16_t vdd = 0;

// // Read the current potentiometer setting in percent
// potiPercentage = BoardGetPotiLevel( );

// // Read the current voltage level
// BoardGetBatteryLevel( ); // Updates the value returned by BoardGetBatteryVoltage( ) function.
potiPercentage = BoardGetBatteryLevel( ); // Updates the value returned by BoardGetBatteryVoltage( ) function.
// vdd = BoardGetBatteryVoltage( );

// CayenneLppAddDigitalInput( channel++, AppLedStateOn );
Expand All @@ -529,7 +530,8 @@ static void PrepareTxFrame( void )
// CayenneLppCopy( AppData.Buffer );
// AppData.BufferSize = CayenneLppGetSize( );

/* Test uplink WKUP and IO1 */
/* Test uplink WKUP and IO2 */

uplink_buffer[0U] = in_cnt << 24U;
uplink_buffer[1U] = in_cnt << 16U;
uplink_buffer[2U] = in_cnt << 8U;
Expand All @@ -538,8 +540,11 @@ static void PrepareTxFrame( void )
uplink_buffer[5U] = in_out_cnt << 16U;
uplink_buffer[6U] = in_out_cnt << 8U;
uplink_buffer[7U] = in_out_cnt & 0x00FF;
uplink_buffer[8U] = potiPercentage;

memcpy1(AppData.Buffer, uplink_buffer, 8U);
memcpy1(AppData.Buffer, uplink_buffer, 9U);

AppData.BufferSize = 9U;

if( LmHandlerSend( &AppData, LmHandlerParams.IsTxConfirmed ) == LORAMAC_HANDLER_SUCCESS )
{
Expand Down Expand Up @@ -633,15 +638,32 @@ static void OnLed4TimerEvent( void* context )
GpioWrite( &Led4, 0 );
}

// /*!
// * Function executed on Led 2 Timeout event
// */
// static void OnLed2TimerEvent( void* context )
// {
// TimerStop( &Led2Timer );
// // Switch LED 2 OFF
// GpioWrite( &Led2, 0 );
// }
/*!
* Function executed on Led 2 Timeout event
*/
bool IsArc = false;
static void OnLed2TimerEvent( void* context )
{
TimerStop( &Led2Timer );
// // Switch LED 2 OFF
// GpioWrite( &Led2, 0 );

uint8_t potiLevel = BoardGetPotiLevel();

if(potiLevel < 50 && !IsArc)
{
IsArc = true;
OnIO2Event(NULL);
printf("Arc lvl: %d\n", potiLevel);
} else if (potiLevel > 50 && IsArc)
{
IsArc = false;
printf("Arc lvl: %d\n", potiLevel);
}


TimerStart( &Led2Timer );
}

/*!
* \brief Function executed on Beacon timer Timeout event
Expand All @@ -668,23 +690,23 @@ static void OnLedBeaconTimerEvent( void* context )
*/
void OnWKUPEvent(void *context)
{
uint32_t timeout = 1000000 * 10;
uint32_t timeout = 1000000;

CRITICAL_SECTION_BEGIN( );

TimerStop( &TxTimer );
IsStopMode = 1U;

while (--timeout <= 0U) __NOP();
while (--timeout > 0x0);

in_cnt++;

printf("In : %ld\n", in_cnt);

IsTxFramePending = 1;

CRITICAL_SECTION_END( ) ;


printf("In : %ld\n", in_cnt);

// Schedule next transmission
TimerSetValue( &TxTimer, TxPeriodicity );
TimerStart( &TxTimer );
Expand All @@ -703,24 +725,24 @@ void OnIO1Event(void *context)
*/
void OnIO2Event(void *context)
{
uint32_t timeout = 1000000 * 10;
// uint32_t timeout = 1000000;

CRITICAL_SECTION_BEGIN( );

TimerStop( &TxTimer );
IsStopMode = 2U;

while (--timeout <= 0U) __NOP();
// while (--timeout > 0);

in_out_cnt++;

printf("In Out: %ld\n", in_out_cnt);

// read/write input/output
IsTxFramePending = 1;

CRITICAL_SECTION_END( ) ;

printf("In Out: %ld\n", in_out_cnt);

// Schedule next transmission
TimerSetValue( &TxTimer, TxPeriodicity );
TimerStart( &TxTimer );
Expand Down
68 changes: 66 additions & 2 deletions src/boards/SKiM980A/adc-board.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

ADC_HandleTypeDef AdcHandle;

/*!
* Function executed on IO2 event
*/
extern void OnIO2Event(void *context);

void AdcMcuInit( Adc_t *obj, PinNames adcInput )
{
AdcHandle.Instance = ( ADC_TypeDef* )ADC1_BASE;
Expand All @@ -37,24 +42,59 @@ void AdcMcuInit( Adc_t *obj, PinNames adcInput )
if( adcInput != NC )
{
GpioInit( &obj->AdcInput, adcInput, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
HAL_NVIC_SetPriority( ADC1_IRQn , 0, 0 );
HAL_NVIC_EnableIRQ( ADC1_IRQn );
}
}

void AdcMcuConfig( void )
{
ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};
ADC_ChannelConfTypeDef adcConf = { 0 };

// Configure ADC
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.ContinuousConvMode = DISABLE;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T6_TRGO;
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.LowPowerAutoWait = DISABLE;
AdcHandle.Init.LowPowerAutoPowerOff = DISABLE;
HAL_ADC_Init( &AdcHandle );
AdcHandle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
// AdcHandle.Init.ScanConvMode = ADC_SCAN_DISABLE;

if (HAL_ADC_Init( &AdcHandle )!= HAL_OK)
{

}

// /** Configure the analog watchdog
// */
// AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;
// AnalogWDGConfig.Channel = ADC_CHANNEL_1;
// AnalogWDGConfig.ITMode = ENABLE;
// AnalogWDGConfig.HighThreshold = 0x800;
// AnalogWDGConfig.LowThreshold = 0x200;
// if (HAL_ADC_AnalogWDGConfig(&AdcHandle, &AnalogWDGConfig) != HAL_OK)
// {

// }

// adcConf.Channel = ADC_CHANNEL_1;
// adcConf.Rank = ADC_REGULAR_RANK_1;
// adcConf.SamplingTime = ADC_SAMPLETIME_192CYCLES;
// if (HAL_ADC_ConfigChannel( &AdcHandle, &adcConf )!= HAL_OK)
// {

// }



}

uint16_t AdcMcuReadChannel( Adc_t *obj, uint32_t channel )
Expand Down Expand Up @@ -102,3 +142,27 @@ uint16_t AdcMcuReadChannel( Adc_t *obj, uint32_t channel )

return adcData;
}

/**
* @brief This function handles ADC global interrupt.
*/
void ADC1_IRQHandler(void)
{
/* USER CODE BEGIN ADC1_IRQn 0 */

/* USER CODE END ADC1_IRQn 0 */
HAL_ADC_IRQHandler(&AdcHandle);
/* USER CODE BEGIN ADC1_IRQn 1 */

/* USER CODE END ADC1_IRQn 1 */
}

void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
{
/* Prevent unused argument(s) compilation warning */
OnIO2Event(NULL);

/* NOTE : This function should not be modified. When the callback is needed,
function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
*/
}
4 changes: 2 additions & 2 deletions src/boards/SKiM980A/board-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ extern "C"
#define LED_1 PA_3
#endif
// #define LED_2 PA_0
#define LED_3 PA_1
// #define LED_3 PA_1
#define LED_4 PA_8

// Debug pins definition.
#define RADIO_DBG_PIN_TX NC
#define RADIO_DBG_PIN_RX NC

#define IO1 PA_8
#define IO2 POTI //PB_7
#define IO2 PA_1 //NFC:PA_1 //NON-NFC:PA_3 //iMS62F:PB_7

#define WKUP PA_0

Expand Down
Loading

0 comments on commit 438e0dc

Please sign in to comment.