Skip to content

Commit

Permalink
add the lorawan hal file and porting file
Browse files Browse the repository at this point in the history
  • Loading branch information
shengpingdai committed Feb 8, 2018
1 parent 6fd0344 commit 1698cf2
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 29 deletions.
51 changes: 51 additions & 0 deletions include/hal/lorawan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/

#ifndef HAL_LRWAN_H
#define HAL_LRWAN_H

#ifdef __cplusplus
extern "C" {
#endif

/*INCLUDES*******************************************************************
* SYSTEM HEADER FILES
*END***********************************************************************/
#include <stdint.h>
#include "base.h"
#include "timeServer.h"

/*MACROS*********************************************************************
* DATATYPE DEFINITIONS
*END***********************************************************************/
/* the struct is for changing the device working mode */
typedef struct
{
void (*enter_stop_mode)(void);
void (*exit_stop_mode)(void);
void (*enter_sleep_mode)(void);
} hal_lrwan_dev_chg_mode_t;

/* LoRaWan time and timer interface */
typedef struct
{
void (*delay_ms)(uint32_t delay);
uint32_t (*set_timer_context)(void);
uint32_t (*get_timer_context)(void);
uint32_t (*get_timer_elapsed_time)(void);
uint32_t (*ms2tick)(TimerTime_t timeMicroSec);
TimerTime_t (*tick2ms)(uint32_t tick);
uint32_t (*get_min_timeout)(void);
uint32_t (*get_timer_val)(void);
void (*stop_alarm)(void);
void (*set_alarm)(uint32_t timeout);
void (*set_uc_wakeup_time)(void);
} hal_lrwan_time_itf_t;

#ifdef __cplusplus
}
#endif

#endif /* HAL_LRWAN_H */

4 changes: 2 additions & 2 deletions kernel/protocols/lorawan/Lora/Utilities/delay.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ Maintainer: Miguel Luis and Gregory Cristian
/* Includes ------------------------------------------------------------------*/
#include "hw.h"
#include "timeServer.h"
#include "lorawan_port.h"

void DelayMs( uint32_t ms )
{
HW_RTC_DelayMs( ms );

aos_lrwan_time_itf.delay_ms(ms);
}

void Delay( float s )
Expand Down
7 changes: 4 additions & 3 deletions kernel/protocols/lorawan/Lora/Utilities/low_power.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
/* Includes ------------------------------------------------------------------*/
#include "hw.h"
#include "low_power.h"
#include "lorawan_port.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
Expand Down Expand Up @@ -122,18 +123,18 @@ void LowPower_Handler( void )

DBG_PRINTF_CRITICAL("dz\n\r");

HW_EnterStopMode( );
aos_lrwan_chg_mode.enter_stop_mode();

/* mcu dependent. to be implemented by user*/
HW_ExitStopMode();
aos_lrwan_chg_mode.exit_stop_mode();

HW_RTC_setMcuWakeUpTime( );
}
else
{
DBG_PRINTF_CRITICAL("z\n\r");

HW_EnterSleepMode( );
aos_lrwan_chg_mode.enter_sleep_mode();
}

}
Expand Down
36 changes: 18 additions & 18 deletions kernel/protocols/lorawan/Lora/Utilities/timeServer.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Maintainer: Miguel Luis, Gregory Cristian and Wael Guibene
#include "hw.h"
#include "timeServer.h"
//#include "low_power.h"

#include "lorawan_port.h"

/*!
* safely execute call back
Expand Down Expand Up @@ -154,12 +154,12 @@ void TimerStart( TimerEvent_t *obj )

if( TimerListHead == NULL )
{
HW_RTC_SetTimerContext( );
aos_lrwan_time_itf.set_timer_context();
TimerInsertNewHeadTimer( obj ); // insert a timeout at now+obj->Timestamp
}
else
{
elapsedTime = HW_RTC_GetTimerElapsedTime( );
elapsedTime = aos_lrwan_time_itf.get_timer_elapsed_time();
obj->Timestamp += elapsedTime;

if( obj->Timestamp < TimerListHead->Timestamp )
Expand Down Expand Up @@ -219,8 +219,8 @@ void TimerIrqHandler( void )



uint32_t old = HW_RTC_GetTimerContext( );
uint32_t now = HW_RTC_SetTimerContext( );
uint32_t old = aos_lrwan_time_itf.get_timer_context();
uint32_t now = aos_lrwan_time_itf.set_timer_context();
uint32_t DeltaContext = now - old; //intentionnal wrap around

/* update timeStamp based upon new Time Reference*/
Expand Down Expand Up @@ -251,7 +251,7 @@ void TimerIrqHandler( void )


// remove all the expired object from the list
while( ( TimerListHead != NULL ) && ( TimerListHead->Timestamp < HW_RTC_GetTimerElapsedTime( ) ))
while( ( TimerListHead != NULL ) && ( TimerListHead->Timestamp < aos_lrwan_time_itf.get_timer_elapsed_time( ) ))
{
cur = TimerListHead;
TimerListHead = TimerListHead->Next;
Expand Down Expand Up @@ -293,7 +293,7 @@ void TimerStop( TimerEvent_t *obj )
}
else
{
HW_RTC_StopAlarm( );
aos_lrwan_time_itf.stop_alarm( );
TimerListHead = NULL;
}
}
Expand Down Expand Up @@ -362,11 +362,11 @@ void TimerReset( TimerEvent_t *obj )
void TimerSetValue( TimerEvent_t *obj, uint32_t value )
{
uint32_t minValue = 0;
uint32_t ticks = HW_RTC_ms2Tick( value );
uint32_t ticks = aos_lrwan_time_itf.ms2tick( value );

TimerStop( obj );

minValue = HW_RTC_GetMinimumTimeout( );
minValue = aos_lrwan_time_itf.get_min_timeout( );

if( ticks < minValue )
{
Expand All @@ -379,28 +379,28 @@ void TimerSetValue( TimerEvent_t *obj, uint32_t value )

TimerTime_t TimerGetCurrentTime( void )
{
uint32_t now = HW_RTC_GetTimerValue( );
return HW_RTC_Tick2ms(now);
uint32_t now = aos_lrwan_time_itf.get_timer_val( );
return aos_lrwan_time_itf.tick2ms(now);
}

TimerTime_t TimerGetElapsedTime( TimerTime_t past )
{
uint32_t nowInTicks = HW_RTC_GetTimerValue( );
uint32_t pastInTicks = HW_RTC_ms2Tick( past );
uint32_t nowInTicks = aos_lrwan_time_itf.get_timer_val( );
uint32_t pastInTicks = aos_lrwan_time_itf.ms2tick( past );
/* intentional wrap around. Works Ok if tick duation below 1ms */
return HW_RTC_Tick2ms( nowInTicks- pastInTicks );
return aos_lrwan_time_itf.tick2ms( nowInTicks- pastInTicks );
}

static void TimerSetTimeout( TimerEvent_t *obj )
{
int32_t minTicks= HW_RTC_GetMinimumTimeout( );
int32_t minTicks= aos_lrwan_time_itf.get_min_timeout( );
obj->IsRunning = true;

//in case deadline too soon
if(obj->Timestamp < (HW_RTC_GetTimerElapsedTime( ) + minTicks) )
if(obj->Timestamp < (aos_lrwan_time_itf.get_timer_elapsed_time( ) + minTicks) )
{
obj->Timestamp = HW_RTC_GetTimerElapsedTime( ) + minTicks;
obj->Timestamp = aos_lrwan_time_itf.get_timer_elapsed_time( ) + minTicks;
}
HW_RTC_SetAlarm( obj->Timestamp );
aos_lrwan_time_itf.set_alarm( obj->Timestamp );
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
173 changes: 173 additions & 0 deletions kernel/protocols/lorawan/lora/hal/lorawan_port.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/

/* Includes ------------------------------------------------------------------*/
#include "lorawan_port.h"
#include "hw_msp.h"
#include "hw_rtc.h"


static void enter_stop_mode()
{
HW_EnterStopMode();
}

static void exit_stop_mode()
{
HW_ExitStopMode();
}

static void enter_sleep_mode()
{
HW_EnterSleepMode();
}

/**
* @fn delay_ms
* @brief Delay of delay ms
* @param Delay in ms
* @rtn None
*/
static void delay_ms(time_ms_t delay)
{
HW_RTC_DelayMs(delay);
}

/**
* @fn set_timer_context
* @brief set timer reference
* @param None
* @rtn Timer Reference Value in Ticks
*/
static uint32_t set_timer_context()
{
return (time_tick_t)HW_RTC_SetTimerContext();
}

/**
* @fn get_timer_context
* @brief get timer reference
* @param None
* @rtn Timer Reference Value in Ticks
*/
static uint32_t get_timer_context()
{
return (time_tick_t)HW_RTC_GetTimerContext();
}

/* */
/**
* @fn get_timer_elapsed_time
* @brief Gget the low level time since the last alarm was set
* @param None
* @rtn The Elapsed time in ticks
*/
static uint32_t get_timer_elapsed_time()
{
return (time_tick_t)HW_RTC_GetTimerElapsedTime();
}

/**
* @fn ms2tick
* @brief Converts time in ms to time in ticks
* @param Time in milliseconds
* @rtn Returns time in timer ticks
*/
static uint32_t ms2tick(uint32_t u_sec)
{
return (time_tick_t)HW_RTC_ms2Tick(u_sec);

}

/**
* @fn tick2ms
* @brief Converts time in ticks to time in ms
* @param Time in ticks
* @rtn Time in timer milliseconds
*/
static uint32_t tick2ms(uint32_t tick)
{
return (time_ms_t)HW_RTC_Tick2ms(tick);
}


/**
* @fn get_min_timeout
* @brief Return the minimum timeout(uC wake up time)
* the low lower timer is able to handle
* @param None
* @rtn Minimum value for a timeout
*/
static uint32_t get_min_timeout()
{
return (time_tick_t)HW_RTC_GetMinimumTimeout();
}

/**
* @fn get_timer_val
* @brief Get the low level timer value
* @param None
* @rtn Timer value in ticks
*/
static uint32_t get_timer_val()
{
return (time_tick_t)HW_RTC_GetTimerValue();
}

/**
* @fn set_uc_wakeup_time
* @brief Calculates the wake up time between wake up and mcu start
* @param None
* @rtn None
*/
static void set_uc_wakeup_time()
{
HW_RTC_setMcuWakeUpTime();
}

/**
* @fn set_alarm
* @brief Set the alarm
* @param Timeout Duration of the Timer ticks
* @rtn None
*/
static void set_alarm(uint32_t timeout)
{
HW_RTC_SetAlarm(timeout);
}

/**
* @fn stop_alarm
* @brief Stop the Alarm
* @param None
* @rtn None
*/
static void stop_alarm()
{
HW_RTC_StopAlarm();
}

/* the struct is for changing the device working mode */
hal_lrwan_dev_chg_mode_t aos_lrwan_chg_mode = {
.enter_stop_mode = enter_stop_mode,
.exit_stop_mode = exit_stop_mode,
.enter_sleep_mode = enter_sleep_mode,
};

/* LoRaWan time and timer interface */
hal_lrwan_time_itf_t aos_lrwan_time_itf = {
.delay_ms = delay_ms,
.set_timer_context = set_timer_context,
.get_timer_context = get_timer_context,
.get_timer_elapsed_time = get_timer_elapsed_time,
.ms2tick = ms2tick,
.tick2ms = tick2ms,
.get_min_timeout = get_min_timeout,
.get_timer_val = get_timer_val,
.stop_alarm = stop_alarm,
.set_alarm = set_alarm,
.set_uc_wakeup_time = set_uc_wakeup_time,
};


Loading

0 comments on commit 1698cf2

Please sign in to comment.