Skip to content

Commit

Permalink
Added ADC Watchdog.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anol Paisal committed Jun 4, 2019
1 parent b940a4a commit d94747e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/apps/LoRaMac/encoder/SKiM980A/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ static void McpsIndication( McpsIndication_t *mcpsIndication )
case 3:
// Downlink encoder configuration
if (mcpsIndication->BufferSize == 3) {
config.analog_alarm = (((uint16_t)mcpsIndication->Buffer[2] << 8) | (uint16_t) mcpsIndication->Buffer[1]);
config.analog_alarm = (((uint16_t)mcpsIndication->Buffer[2] << 8) | (uint16_t) mcpsIndication->Buffer[1]);
config.digital_alarm = (mcpsIndication->Buffer[0] >> 7) & 0x01;
config.sampling = (mcpsIndication->Buffer[0] & 0x0f);

Expand All @@ -838,6 +838,11 @@ static void McpsIndication( McpsIndication_t *mcpsIndication )
printf( "Sampling : %02X\r\n", config.sampling );
printf( "Digital alarm enable : %02X\r\n", config.digital_alarm );
printf( "Analog alarm lvl. : %04X\r\n\r\n", config.analog_alarm );

if (config.analog_alarm > 0)
{
BoardSetADCAlarmLVL(config.analog_alarm);
}
}
// OnTxNextPacketTimerEvent(NULL);
}
Expand Down
38 changes: 38 additions & 0 deletions src/boards/SKiM980A/adc-board.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
#include "stm32l1xx.h"
#include "board-config.h"
#include "adc-board.h"
#include "board.h"
#include "encoder.h"

ADC_HandleTypeDef AdcHandle;
ADC_AnalogWDGConfTypeDef awd;

void AdcMcuInit( Adc_t *obj, PinNames adcInput )
{
Expand Down Expand Up @@ -116,3 +119,38 @@ uint16_t AdcMcuReadChannel( Adc_t *obj, uint32_t channel )

return adcData;
}

void AdcMcuWatchdog( Adc_t *obj, uint32_t channel, uint32_t high_lvl, uint32_t low_lvl)
{
awd.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG; /*!< Configures the ADC analog watchdog mode: single/all channels, regular/injected group.
This parameter can be a value of @ref ADC_analog_watchdog_mode. */
awd.Channel = channel; /*!< Selects which ADC channel to monitor by analog watchdog.
This parameter has an effect only if watchdog mode is configured on single channel (parameter WatchdogMode)
This parameter can be a value of @ref ADC_channels. */
awd.ITMode = ENABLE; /*!< Specifies whether the analog watchdog is configured in interrupt or polling mode.
This parameter can be set to ENABLE or DISABLE */
awd.HighThreshold = high_lvl; /*!< Configures the ADC analog watchdog High threshold value.
This parameter must be a number between Min_Data = 0x000 and Max_Data = 0xFFF. */
awd.LowThreshold = low_lvl; /*!< Configures the ADC analog watchdog High threshold value.
This parameter must be a number between Min_Data = 0x000 and Max_Data = 0xFFF. */
awd.WatchdogNumber = 0; /*!< Reserved for future use, can be set to 0 */

HAL_ADC_AnalogWDGConfig(&AdcHandle, &awd);
}

/**
* @brief Analog watchdog callback in non blocking mode.
* @param hadc: ADC handle
* @retval None
*/
void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
{
CRITICAL_SECTION_BEGIN();
if (Encoder.OnSendOneshot != NULL && config.analog_alarm > 0)
{
awd.ITMode = DISABLE;
HAL_ADC_AnalogWDGConfig(&AdcHandle, &awd);
Encoder.OnSendOneshot();
}
CRITICAL_SECTION_END();
}
4 changes: 4 additions & 0 deletions src/boards/SKiM980A/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,10 @@ void BoardLowPowerHandler( void )
__enable_irq( );
}

void BoardSetADCAlarmLVL(uint32_t lower_threshold)
{
AdcSetWatchdogLVL(&Adc, ADC_CHANNEL_3, lower_threshold );
}
/**
* @brief This function configures the source of the time base.
* @brief don't enable systick
Expand Down
10 changes: 10 additions & 0 deletions src/boards/adc-board.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ void AdcMcuConfig( void );
*/
uint16_t AdcMcuReadChannel( Adc_t *obj, uint32_t channel );

/*!
* \brief Set AWD the value of the given channel
*
* \param [IN] obj ADC object
* \param [IN] channel ADC input channel
* \param [IN] high_lvl ADC high level threshold
* \param [IN] low_lvl ADC low level threshold
*/
void AdcMcuWatchdog( Adc_t *obj, uint32_t channel, uint32_t high_lvl, uint32_t low_lvl);

#endif // __ADC_BOARD_H__
2 changes: 2 additions & 0 deletions src/boards/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,6 @@ uint8_t GetBoardPowerSource( void );
*/
Version_t BoardGetVersion( void );

void BoardSetADCAlarmLVL(uint32_t lower_threshold);

#endif // __BOARD_H__
1 change: 1 addition & 0 deletions src/boards/encoder-board.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef enum {
RATE_30_MIN,
RATE_60_MIN,
RATE_90_MIN
/* 5 to 15 reserved for default RATE_01_MIN */
} DUTYCYCLE_RATE ;

// An encoder.c file has to be implmented under system directory.
Expand Down
5 changes: 5 additions & 0 deletions src/system/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ uint16_t AdcReadChannel( Adc_t *obj, uint32_t channel )
return 0;
}
}

void AdcSetWatchdogLVL( Adc_t *obj, uint32_t channel, uint32_t lower_threshold )
{
AdcMcuWatchdog( obj, channel, UINT32_MAX, lower_threshold);
}
2 changes: 2 additions & 0 deletions src/system/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ void AdcDeInit( Adc_t *obj );
*/
uint16_t AdcReadChannel( Adc_t *obj, uint32_t channel );

void AdcSetWatchdogLVL( Adc_t *obj, uint32_t channel, uint32_t lower_threshold );

#endif // __ADC_H__

0 comments on commit d94747e

Please sign in to comment.