fholin fholin / Mbed 2 deprecated MiniMousetest

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
fholin
Date:
Mon Dec 18 16:31:11 2017 +0000
Parent:
0:2325d1d28df3
Commit message:
inline with github repository : ; https://github.com/LoRaWanMiniMouse/Mini-Mouse.git

Changed in this revision

.mbed Show annotated file Show diff for this revision Revisions of this file
MinimouseSrc/ApiFlash.cpp Show annotated file Show diff for this revision Revisions of this file
MinimouseSrc/ApiFlash.h Show annotated file Show diff for this revision Revisions of this file
MinimouseSrc/ApiRtc.cpp Show annotated file Show diff for this revision Revisions of this file
MinimouseSrc/ApiRtc.h Show annotated file Show diff for this revision Revisions of this file
MinimouseSrc/Define.h Show annotated file Show diff for this revision Revisions of this file
MinimouseSrc/LoraWanProcess.cpp Show annotated file Show diff for this revision Revisions of this file
MinimouseSrc/MacLayer.cpp Show annotated file Show diff for this revision Revisions of this file
MinimouseSrc/MacLayer.h Show annotated file Show diff for this revision Revisions of this file
MinimouseSrc/PhyLayer.cpp Show annotated file Show diff for this revision Revisions of this file
MinimouseSrc/main.cpp Show annotated file Show diff for this revision Revisions of this file
radio/SX1276Lib/sx1276/sx1276-hal.cpp Show annotated file Show diff for this revision Revisions of this file
radio/SX1276Lib/sx1276/sx1276.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.mbed	Mon Dec 18 16:31:11 2017 +0000
@@ -0,0 +1,1 @@
+ROOT=.
--- a/MinimouseSrc/ApiFlash.cpp	Fri Dec 15 13:15:04 2017 +0000
+++ b/MinimouseSrc/ApiFlash.cpp	Mon Dec 18 16:31:11 2017 +0000
@@ -27,7 +27,7 @@
     // to be completed by mcu providers
     return (0);
 }
-int Flash::read(uint8_t *buffer, uint32_t addr, uint32_t size){
+int Flash::RestoreContext(uint8_t *buffer, uint32_t addr, uint32_t size){
      /* have to be implemented by mcu providers
     the following code propose a lite implementation without any error cases*/
     int i;
@@ -39,7 +39,7 @@
 
 
 
-int Flash::program(const void *buffer, uint32_t addr, uint32_t size){
+int Flash::StoreContext(const void *buffer, uint32_t addr, uint32_t size){
     /* have to be implemented by mcu providers
     the following code propose a lite implementation without any error cases
     this section have to be very robust, have to support failure mode such as  power off during flash programmation    
--- a/MinimouseSrc/ApiFlash.h	Fri Dec 15 13:15:04 2017 +0000
+++ b/MinimouseSrc/ApiFlash.h	Mon Dec 18 16:31:11 2017 +0000
@@ -32,7 +32,7 @@
     int init(); 
  
 
-    /** Read data from a flash device.  
+    /** RestoreContext data from a flash device.  
      * 
      *  This method invokes memcpy - reads number of bytes from the address 
      * 
@@ -41,10 +41,10 @@
      *  @param size   Size to read in bytes 
      *  @return       0 on success, negative error code on failure 
      */ 
-    int read(uint8_t *buffer, uint32_t addr, uint32_t size); 
+    int RestoreContext(uint8_t *buffer, uint32_t addr, uint32_t size); 
 
  
-    /** Program data to flash
+    /** StoreContext data to flash
      *  To be safer this function have to implement a read/check data sequence after programation 
      *  
      * 
@@ -53,7 +53,7 @@
      *  @param size   Size to write in bytes,
      *  @return       0 on success, negative error code on failure 
      */ 
-    int program(const void *buffer, uint32_t addr, uint32_t size); 
+    int StoreContext(const void *buffer, uint32_t addr, uint32_t size); 
 
  
     /** Erase sectors 
--- a/MinimouseSrc/ApiRtc.cpp	Fri Dec 15 13:15:04 2017 +0000
+++ b/MinimouseSrc/ApiRtc.cpp	Mon Dec 18 16:31:11 2017 +0000
@@ -181,28 +181,30 @@
     NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler);
     NVIC_EnableIRQ(RTC_WKUP_IRQn);
     myrtc_write ( 0 );
-// __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_EVENT();
+
+}
+
+
+
+
+
+
+
+//void mysleep (int time)
+//{
 //    pcf.printf("status %d\n",HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 10000, 0));
-//    HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle);
-//    pcf.printf("rtccounter %d\n",HAL_RTCEx_GetWakeUpTimer(&RtcHandle));
-//    wait_ms(100);
-//    pcf.printf("rtccounter %d\n",HAL_RTCEx_GetWakeUpTimer(&RtcHandle));
-}
-void mysleep (int time)
-{
-    pcf.printf("status %d\n",HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 10000, 0));
-    deepsleep();
-}
+//    deepsleep();
+//}
 
 void wait_s ( int t )
 {
     wait(t);
 }
-Timer timerglobal ; 
-void TimerLoraInit(void){
-    timerglobal.start();
-}
-int GetTime(void)
-{
-    return(timerglobal.read_ms());
-}
\ No newline at end of file
+//Timer timerglobal ; 
+//void TimerLoraInit(void){
+//    timerglobal.start();
+//}
+//int GetTime(void)
+//{
+//    return(timerglobal.read_ms());
+//}
\ No newline at end of file
--- a/MinimouseSrc/ApiRtc.h	Fri Dec 15 13:15:04 2017 +0000
+++ b/MinimouseSrc/ApiRtc.h	Mon Dec 18 16:31:11 2017 +0000
@@ -18,8 +18,8 @@
 #ifndef APIRTC_H
 #define APIRTC_H
 #include "mbed.h"
-void my_rtc_init( void );
-uint32_t RtcGetTimeMs( void ) ;//uint32_t  *Seconds, uint16_t * SubSeconds);
+void     my_rtc_init     ( void ) ;
+uint32_t RtcGetTimeMs    ( void ) ;//uint32_t  *Seconds, uint16_t * SubSeconds);
 uint32_t RtcGetTimeSecond( void ) ;//uint32_t  *Seconds, uint16_t * SubSeconds);
 void myrtc_write(  time_t t );
 void mysleep (int time);
@@ -30,4 +30,310 @@
 void RtcSetAlarm( void );
 void RtcGetAlarm( void );
 void wait_s ( int t );
+
+  
+
+/* Specific implementation for STM32 . The following implementation is just to put in place
+a setalarm function as described on the architecture specification of minimouse */
+
+#define RTC_INIT_DATE 1
+#define RTC_INIT_MONTH  RTC_MONTH_JANUARY
+#define RTC_INIT_YEAR  0
+#define RTC_INIT_WEEKDAY RTC_WEEKDAY_SATURDAY
+#define RTC_INIT_SUBSEC_SECONDS_MINUTES_HOURS 0 /*time at 0:0:0*/
+
+#define END_OF_FEBRUARY_LEAP 60 //31+29
+#define END_OF_JULY_LEAP    213 //31+29+...
+
+#define END_OF_FEBRUARY_NORM 59 //31+28
+#define END_OF_JULY_NORM    212 //31+28+...
+
+#define DIV_1461(X) (( (X)*91867+22750)>>25)
+   
+#define DIV_APPROX_86400(X)    ( ((X)>>18) +( (X)>>17))
+   
+#define DIV_1461(X) (( (X)*91867+22750)>>25)
+   
+#define DIV_APPROX_1000(X)    ( ((X)>>10) +( (X)>>16) + ( (X)>>17))
+   
+#define DIV_APPROX_60(X)   ( ( (X) *17476 )>>20 )
+   
+#define DIV_APPROX_61(X) (((X) *68759)>>22)
+   
+#define MODULO_7(X)   ( (X) -(( ( ((X)+1)  *299593)>>21)*7))
+
+#define  DAYS_IN_MONTH_CORRECTION_NORM     ((uint32_t) 0x99AAA0 )
+#define  DAYS_IN_MONTH_CORRECTION_LEAP     ((uint32_t) 0x445550 )
+
+#define  DAYS_IN_LEAP_YEAR (uint32_t) 366
+
+#define  DAYS_IN_YEAR      (uint32_t) 365
+
+#define  SECONDS_IN_1DAY   (uint32_t) 86400
+
+#define  SECONDS_IN_1HOUR   (uint32_t) 3600
+
+#define  SECONDS_IN_1MINUTE   (uint32_t) 60
+
+#define  MINUTES_IN_1HOUR    (uint32_t) 60
+
+#define  HOURS_IN_1DAY      (uint32_t) 24
+
+/* Calculates ceiling(X/N) */
+#define DIVC(X,N)   ( ( (X) + (N) -1 ) / (N) )
+
+#define DIVC_BY_4(X)   ( ( (X) + 3 ) >>2 )
+
+#define DIVC_BY_2(X)   ( ( (X) + 1 ) >>1 )
+
+
+
+/* subsecond number of bits */
+#define N_PREDIV_S                 10
+
+/* Synchonuous prediv  */
+#define PREDIV_S                  ((1<<N_PREDIV_S)-1)
+
+/* Asynchonuous prediv   */
+#define PREDIV_A                  (1<<(15-N_PREDIV_S))-1
+
+
+
+/* Sub-second mask definition  */
+#define HW_RTC_ALARMSUBSECONDMASK N_PREDIV_S<<RTC_ALRMASSR_MASKSS_Pos
+
+/* RTC Time base in us */
+#define USEC_NUMBER               1000000
+#define MSEC_NUMBER               (USEC_NUMBER/1000)
+#define RTC_ALARM_TIME_BASE       (USEC_NUMBER>>N_PREDIV_S)
+
+#define COMMON_FACTOR        3
+#define CONV_NUMER                (MSEC_NUMBER>>COMMON_FACTOR)
+#define CONV_DENOM                (1<<(N_PREDIV_S-COMMON_FACTOR))
+
+
+__STATIC_INLINE uint32_t cal_div_61( uint32_t in )
+{
+#if 0
+  return  (in / 61 );
+#else
+
+  uint32_t out_temp=0;
+  uint32_t div_result=DIV_APPROX_61(in);
+  while (div_result >=1 )
+  {
+    out_temp+=div_result;
+    in -= div_result*61;
+    div_result=DIV_APPROX_61(in);
+  }
+  if( in>= 61 )
+  {
+    out_temp+=1;
+    in -= 61;
+  }
+  return out_temp;
+#endif
+}
+
+__STATIC_INLINE uint32_t cal_get_month(uint32_t Days, uint32_t year)
+{
+  uint32_t month;
+  if ( (year%4) ==0  )
+  {  /*leap year*/
+    if ( Days<END_OF_FEBRUARY_LEAP )
+    { /*January or february*/
+     // month =  Days*2/(30+31);
+      month =  cal_div_61(Days*2);
+    }
+    else if ( Days<END_OF_JULY_LEAP )
+    {
+      month =  cal_div_61((Days-END_OF_FEBRUARY_LEAP)*2)+2;
+    }
+    else
+    {
+      month =  cal_div_61((Days-END_OF_JULY_LEAP)*2)+7;
+    }
+  }
+  else
+  {
+    if ( Days<END_OF_FEBRUARY_NORM )
+    { /*January of february*/
+      month =  cal_div_61(Days*2);
+    }
+    else if ( Days<END_OF_JULY_NORM )
+    {
+      month =  cal_div_61((Days-END_OF_FEBRUARY_NORM)*2)+2;
+    }
+    else
+    {
+      month =  cal_div_61((Days-END_OF_JULY_NORM)*2)+7;
+    }
+  }
+  return month;
+}
+
+__STATIC_INLINE void cal_div_86400( uint32_t in, uint32_t* out, uint32_t* remainder )
+{
+#if 0
+  *remainder= total_nb_seconds % SECONDS_IN_1DAY;
+  * nbDays =     total_nb_seconds / SECONDS_IN_1DAY;
+#else
+
+  uint32_t out_temp=0;
+  uint32_t div_result=DIV_APPROX_86400(in);
+  while (div_result >=1 )
+  {
+    out_temp+=div_result;
+    in -= div_result*86400;
+    div_result=DIV_APPROX_86400(in);
+  }
+  if( in>=86400 )
+  {
+    out_temp+=1;
+    in -= 86400;
+  }
+
+  *remainder = in;
+  *out = out_temp;
+#endif
+}
+
+
+
+__STATIC_INLINE void cal_div_1000( uint32_t in, uint32_t* out, uint32_t* remainder )
+{
+#if 0
+  *remainder= total_nb_seconds % 1000;
+  *out      = total_nb_seconds / 1000;
+#else
+
+  uint32_t out_temp=0;
+  uint32_t div_result=DIV_APPROX_1000(in);
+  while (div_result >=1 )
+  {
+    out_temp+=div_result;
+    in -= div_result*1000;
+    div_result=DIV_APPROX_1000(in);
+  }
+  if( in>= 1000 )
+  {
+    out_temp+=1;
+    in -= 1000;
+  }
+  *remainder = in;
+  *out = out_temp;
+#endif
+}
+
+__STATIC_INLINE void cal_div_60( uint32_t in, uint32_t* out, uint32_t* remainder )
+{
+#if 0
+  *remainder= total_nb_seconds % 60;
+  *out      = total_nb_seconds / 60;
+#else
+
+  uint32_t out_temp=0;
+  uint32_t div_result=DIV_APPROX_60(in);
+  while (div_result >=1 )
+  {
+    out_temp+=div_result;
+    in -= div_result*60;
+    div_result=DIV_APPROX_60(in);
+  }
+  if( in>= 60 )
+  {
+    out_temp+=1;
+    in -= 60;
+  }
+  *remainder = in;
+  *out = out_temp;
+#endif
+}
+
+__STATIC_INLINE uint64_t cal_convertBCD_2_Cnt64( RTC_DateTypeDef* RTC_DateStruct, RTC_TimeTypeDef* RTC_TimeStruct )
+{
+  uint64_t cnt64 = 0;
+  
+  uint32_t nbdays ;
+  
+  uint32_t nbsecs;
+   
+  uint32_t correction_month[4]={DAYS_IN_MONTH_CORRECTION_LEAP,
+                                DAYS_IN_MONTH_CORRECTION_NORM,
+                                DAYS_IN_MONTH_CORRECTION_NORM,
+                                DAYS_IN_MONTH_CORRECTION_NORM};
+  
+  nbdays= DIVC( (DAYS_IN_YEAR*3 + DAYS_IN_LEAP_YEAR)* RTC_DateStruct->Year , 4);
+ 
+  nbdays +=( DIVC_BY_2( (RTC_DateStruct->Month-1)*(30+31) ) - (((correction_month[RTC_DateStruct->Year % 4]>> ((RTC_DateStruct->Month-1)*2) )&0x3)));
+
+  nbdays += (RTC_DateStruct->Date -1);
+  
+  /* convert from days to seconds */
+  cnt64 = nbdays* SECONDS_IN_1DAY; 
+
+  nbsecs = ( ( uint32_t )RTC_TimeStruct->Seconds + 
+                     ( ( uint32_t )RTC_TimeStruct->Minutes * SECONDS_IN_1MINUTE ) +
+                     ( ( uint32_t )RTC_TimeStruct->Hours * SECONDS_IN_1HOUR ) ) ;
+
+  cnt64 = nbdays * SECONDS_IN_1DAY + nbsecs ; 
+  
+  cnt64 = (cnt64<<N_PREDIV_S) + ( RTC_TimeStruct->SubSeconds);
+  
+  return cnt64;
+}
+
+
+
+__STATIC_INLINE void cal_convert_Cnt64_2_Bcd( RTC_DateTypeDef* Date, RTC_TimeTypeDef* Time, uint64_t cnt64 )
+{
+  uint32_t correction_month[4]={DAYS_IN_MONTH_CORRECTION_LEAP,
+                                     DAYS_IN_MONTH_CORRECTION_NORM,
+                                     DAYS_IN_MONTH_CORRECTION_NORM,
+                                     DAYS_IN_MONTH_CORRECTION_NORM};
+  uint32_t weekDays = (RTC_INIT_WEEKDAY-1);
+  uint32_t cnt32 = cnt64>> N_PREDIV_S; /*total in seconds (136 year)*/
+  uint32_t seconds;
+  uint32_t minutes;
+  uint32_t Days;
+  uint32_t div_out;
+  uint32_t div_rem;
+
+  cal_div_86400(cnt32, &Days, &seconds);
+
+  Time->SubSeconds = ( cnt64 & PREDIV_S);
+
+  /* calculates seconds */
+  cal_div_60(seconds, &minutes, &div_rem);
+  Time->Seconds=(uint8_t) div_rem;
+
+  /* calculates minutes and hours*/
+  cal_div_60(minutes, &div_out, &div_rem);
+  Time->Minutes = (uint8_t) div_rem;
+  Time->Hours = (uint8_t)   div_out;
+ /* calculates Year */
+  Date->Year = DIV_1461(Days);
+  Days-= DIVC_BY_4( (DAYS_IN_YEAR*3 + DAYS_IN_LEAP_YEAR)* Date->Year );
+
+  /*calculates month*/
+  Date->Month = cal_get_month( Days, Date->Year) ;
+
+  /*calculates weekdays*/
+  weekDays +=  DIVC_BY_4((Date->Year*5));
+  weekDays +=  Days;
+  Date->WeekDay  = MODULO_7( weekDays );
+
+  Days -=( DIVC_BY_2( (Date->Month)*(30+31) ) - (((correction_month[Date->Year % 4]>> ((Date->Month)*2) )&0x3)));
+
+  /* convert 0 to 1 indexed. */
+  Date->WeekDay ++ ;/* 0 to 1 indexed.  month={0..6} to {1..7}*/
+  Date->Month++; /* 0 to 1 indexed.  month={0..11} to {1..12}*/
+  Date->Date = (Days+1);/* 0 to 1 indexed */
+}
+
+
+
+
+
+
 #endif
\ No newline at end of file
--- a/MinimouseSrc/Define.h	Fri Dec 15 13:15:04 2017 +0000
+++ b/MinimouseSrc/Define.h	Mon Dec 18 16:31:11 2017 +0000
@@ -20,6 +20,47 @@
 #include "mbed.h"
 
 /********************************************************************************/
+/*                         PINOUT Platform dependant                            */
+/********************************************************************************/
+                    // MURATA   //    sx1276 MBAS
+#define LORA_SPI_MOSI   PA_7    //      D11
+#define LORA_SPI_MISO   PA_6    //      D12
+#define LORA_SPI_SCLK   PB_3    //      D13
+#define LORA_CS         PA_15   //      D10
+#define LORA_RESET      PC_0    //      A0
+#define TX_RX_IT        PB_4    //      D2
+#define RX_TIMEOUT_IT   PB_1    //      D3
+#define SERIAL_TX       PA_9    //      D0
+#define SERIAL_RX       PA_10   //      D1
+
+
+/********************************************************************************/
+/*                         LORA KEYS USER Specific                              */
+/********************************************************************************/
+static uint8_t LoRaMacNwkSKey[] =
+{ 0x94, 0x02, 0x9E, 0xAB, 0xE9, 0x0C, 0xE3, 0xCF, 0x51, 0xA2, 0x31, 0x7F, 0x03, 0x27, 0xEF, 0xA6 };
+
+/*!
+ * AES encryption/decryption cipher application session key
+ */
+static uint8_t LoRaMacAppSKey[] =
+{ 0xBB, 0xEB, 0xEB, 0x4D, 0x5C, 0xAF, 0x95, 0x5D, 0x33, 0xD1, 0xC0, 0xC4, 0x41, 0x55, 0xB5, 0xA1 };
+
+
+static uint8_t LoRaMacAppKey[] =
+{ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
+static uint8_t AppEui[] = 
+{ 0x70, 0xB3, 0xD5, 0x7E, 0xF0, 0x00, 0x36, 0x12 };
+
+
+static uint8_t DevEui[] = 
+{ 0x33, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x33 };    
+
+
+
+
+
+/********************************************************************************/
 /*                         LoraWan Process States                               */
 /********************************************************************************/
 
@@ -89,6 +130,23 @@
     RXTIMING_SETUP_REQ,
     RXTIMING_SETUP_ANS,
 };
+enum {
+    LINK_CHECK_REQ_SIZE ,
+    LINK_CHECK_ANS_SIZE ,
+    LINK_ADR_REQ_SIZE,
+    LINK_ADR_ANS_SIZE,
+    DUTY_CYCLE_REQ_SIZE,
+    DUTY_CYCLE_ANS_SIZE,
+    RXPARRAM_SETUP_REQ_SIZE,
+    RXPARRAM_SETUP_ANS_SIZE,
+    DEV_STATUS_REQ_SIZE,
+    DEV_STATUS_ANS_SIZE,
+    NEW_CHANNEL_REQ_SIZE,
+    NEW_CHANNEL_ANS_SIZE,
+    RXTIMING_SETUP_REQ_SIZE,
+    RXTIMING_SETUP_ANS_SIZE ,
+};
+
 #define RX1DELAY              1000 // define in ms
 #define RX1DELAYJOIN          5000 // define in ms
 #define MAX_FCNT_GAP          16384
@@ -131,17 +189,18 @@
     MOBILELOWPOWERADRMODE,
 } eDataRateStrategy;
 
-enum { 
+typedef enum { 
     ERRORLORAWAN = -1,
     OKLORAWAN    = 0,
-};
+}eStatusLoRaWan;
 
-enum {
+typedef enum {
     NOVALIDRXPACKET,
     USERRXPACKET,
+    USERRX_FOPTSPACKET,
     NWKRXPACKET,
     JOINACCEPTPACKET,
-};
+} eRxPacketType;
 enum {
     RX1,
     RX2
--- a/MinimouseSrc/LoraWanProcess.cpp	Fri Dec 15 13:15:04 2017 +0000
+++ b/MinimouseSrc/LoraWanProcess.cpp	Mon Dec 18 16:31:11 2017 +0000
@@ -18,7 +18,7 @@
 #include "LoraWanProcess.h"
 #include "ApiRtc.h"
 #include "utilities.h"
-Serial pcf( PA_9, PA_10 );
+Serial pcf( SERIAL_TX, SERIAL_RX );
 
 LoraWanObjet::LoraWanObjet( PinName interrupt ):packet( interrupt ){
     StateLoraWanProcess=LWPSTATE_IDLE;
@@ -136,7 +136,7 @@
             if ( ValidRxPacket == JOINACCEPTPACKET){
                 packet.UpdateJoinProcedure( );
             }
-            if ( ValidRxPacket == NWKRXPACKET){
+            if ( ( ValidRxPacket == NWKRXPACKET) || ( ValidRxPacket == USERRX_FOPTSPACKET) ) {
                 packet.ParseManagementPacket( );
             }
             packet.UpdateMacLayer();
--- a/MinimouseSrc/MacLayer.cpp	Fri Dec 15 13:15:04 2017 +0000
+++ b/MinimouseSrc/MacLayer.cpp	Mon Dec 18 16:31:11 2017 +0000
@@ -108,8 +108,9 @@
     /****************************/
     /*       DecodeRxFrame      */
     /****************************/
-int LoraWanContainer::DecodeRxFrame( void ) {
+eRxPacketType LoraWanContainer::DecodeRxFrame( void ) {
     int status = OKLORAWAN ;
+    eRxPacketType RxPacketType = NOVALIDRXPACKET ; 
     uint32_t micIn ;
     status += CheckRxPayloadLength ( );
     status += ExtractRxMhdr ( ) ;
@@ -143,20 +144,26 @@
         }
         if ( status == OKLORAWAN) {
             MacRxPayloadSize = MacRxPayloadSize - FHDROFFSET - FoptsLength ;
-            LoRaMacPayloadDecrypt( &Phy.RxPhyPayload[FHDROFFSET + FoptsLength], MacRxPayloadSize, (FportRx == 0 )?nwkSKey:appSKey, DevAddr, 1, FcntDwn, &MacRxPayload[0] );
+            if ( FportRx == 0 ) {
+                LoRaMacPayloadDecrypt( &Phy.RxPhyPayload[FHDROFFSET + FoptsLength], MacRxPayloadSize, nwkSKey, DevAddr, 1, FcntDwn, &MacNwkPayload[0] );
+                MacNwkPayloadSize = MacRxPayloadSize;
+                RxPacketType = NWKRXPACKET ;
+            } else {
+                LoRaMacPayloadDecrypt( &Phy.RxPhyPayload[FHDROFFSET + FoptsLength], MacRxPayloadSize, appSKey, DevAddr, 1, FcntDwn, &MacRxPayload[0] );
+                if ( FoptsLength != 0 ) {
+                    memcpy ( MacNwkPayload, Fopts, FoptsLength);
+                    MacNwkPayloadSize = FoptsLength;
+                    RxPacketType = USERRX_FOPTSPACKET ;
+                } else {
+                    AvailableRxPacketForUser = LORARXPACKETAVAILABLE; 
+                    RxPacketType = USERRXPACKET ;
+                }
+            }
             pcf.printf( " MtypeRx = %d \n ",MtypeRx );
             pcf.printf( " FcntDwn = %d \n ",FcntDwn );
         }
     }
-    if (status != OKLORAWAN ) {
-        return ( NOVALIDRXPACKET );
-    } 
-    else if ( FportRx == 0 ) {
-        return ( NWKRXPACKET ) ;
-    } else {
-        AvailableRxPacketForUser = LORARXPACKETAVAILABLE; //@note in this case fopts should be not equal to zero and so there are also management frame available + user data
-        return ( USERRXPACKET ) ;
-    }
+    return ( RxPacketType );
 }
     /*********************************/
     /*      End of Decode Frame      */
@@ -164,33 +171,41 @@
     /************************************************************************************************/
     /*                      NWK MANAGEMENTS Methods                                                 */
     /************************************************************************************************/
-void LoraWanContainer::ParseManagementPacket( void ) {
+eStatusLoRaWan LoraWanContainer::ParseManagementPacket( void ) {
     uint8_t CmdIdentifier;
-    CmdIdentifier = MacRxPayload[0];
-    switch ( CmdIdentifier ) {
-        case LINK_CHECK_ANS :
-            LinkCheckParser( );
-            break;
-        case LINK_ADR_REQ :
-            LinkADRParser( );
-            break;
-        case DUTY_CYCLE_REQ :
-            DutyCycleParser( );
-            break;
-        case RXPARRAM_SETUP_REQ :
-            RXParamSetupParser( );
-            break;
-        case DEV_STATUS_REQ :
-            DevStatusParser( );
-            break;
-        case NEW_CHANNEL_REQ :
-            NewChannelParser( );
-            break;
-        case RXTIMING_SETUP_REQ :
-            RXTimingSetupParser( );
-            break;
+    eStatusLoRaWan status = OKLORAWAN ;
+    uint8_t MaxCmdNum = 16 ; //@note security to avoid an infinite While erro 
+    while ( ( MacNwkPayloadSize > 0 ) || (  MaxCmdNum > 0 ) ) { //@note MacNwkPayloadSize and MacNwkPayload[0] are updated in Parser's method
+        MaxCmdNum --; 
+        if ( MaxCmdNum == 0 ) {
+            return ( ERRORLORAWAN );
+        }
+        CmdIdentifier = MacNwkPayload[0];
+        switch ( CmdIdentifier ) {
+            case LINK_CHECK_ANS :
+                LinkCheckParser( );
+                break;
+            case LINK_ADR_REQ :
+                LinkADRParser( );
+                break;
+            case DUTY_CYCLE_REQ :
+                DutyCycleParser( );
+                break;
+            case RXPARRAM_SETUP_REQ :
+                RXParamSetupParser( );
+                break;
+            case DEV_STATUS_REQ :
+                DevStatusParser( );
+                break;
+            case NEW_CHANNEL_REQ :
+                NewChannelParser( );
+                break;
+            case RXTIMING_SETUP_REQ :
+                RXTimingSetupParser( );
+                break;
+        }
     }
-    
+    return ( status ); 
 }
 
 void LoraWanContainer::UpdateMacLayer ( void ) {
@@ -527,12 +542,12 @@
     memcpy( &BackUpFlash.MacMinMaxSFChannel[0], &MacMinMaxSFChannel[0], 16);
     memcpy( &BackUpFlash.nwkSKey[0], &nwkSKey[0], 16);
     memcpy( &BackUpFlash.appSKey[0], &appSKey[0], 16);
-    gFlash.program( &BackUpFlash, USERFLASHADRESS, sizeof(sBackUpFlash) );    
+    gFlash.StoreContext( &BackUpFlash, USERFLASHADRESS, sizeof(sBackUpFlash) );    
 }
 
 
 void LoraWanContainer::LoadFromFlash ( ) {
-    gFlash.read((uint8_t *)(&BackUpFlash), USERFLASHADRESS, sizeof(sBackUpFlash));
+    gFlash.RestoreContext((uint8_t *)(&BackUpFlash), USERFLASHADRESS, sizeof(sBackUpFlash));
     BackUpFlash.FcntUp            +=  FLASH_UPDATE_PERIOD; //@note automatic increment
     MacTxSf                       = BackUpFlash.MacTxSf;
     MacTxPower                    = BackUpFlash.MacTxPower;
@@ -551,7 +566,7 @@
     memcpy( &MacMinMaxSFChannel[0], &BackUpFlash.MacMinMaxSFChannel[0], 16);
     memcpy( &nwkSKey[0], &BackUpFlash.nwkSKey[0], 16);
     memcpy( &appSKey[0], &BackUpFlash.appSKey[0], 16); 
-    gFlash.program( &BackUpFlash, USERFLASHADRESS, sizeof(sBackUpFlash) );    
+    gFlash.StoreContext( &BackUpFlash, USERFLASHADRESS, sizeof(sBackUpFlash) );    
 }
 /**************************************TIMER PART**********************************************************/
 /**********************************************************************************************************/
--- a/MinimouseSrc/MacLayer.h	Fri Dec 15 13:15:04 2017 +0000
+++ b/MinimouseSrc/MacLayer.h	Mon Dec 18 16:31:11 2017 +0000
@@ -24,26 +24,7 @@
 #define MAC_LAYER_H
 
 #define LORA_MAC_SYNCWORD                           0x34
-static uint8_t LoRaMacNwkSKey[] =
-{ 0x94, 0x02, 0x9E, 0xAB, 0xE9, 0x0C, 0xE3, 0xCF, 0x51, 0xA2, 0x31, 0x7F, 0x03, 0x27, 0xEF, 0xA6 };
 
-/*!
- * AES encryption/decryption cipher application session key
- */
-static uint8_t LoRaMacAppSKey[] =
-{ 0xBB, 0xEB, 0xEB, 0x4D, 0x5C, 0xAF, 0x95, 0x5D, 0x33, 0xD1, 0xC0, 0xC4, 0x41, 0x55, 0xB5, 0xA1 };
-
-
-static uint8_t LoRaMacAppKey[] =
-{ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
-static uint8_t AppEui[] = 
-{ 0x70, 0xB3, 0xD5, 0x7E, 0xF0, 0x00, 0x36, 0x12 };
-
-
-static uint8_t DevEui[] = 
-{ 0x33, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x33 };    
-
-//@note find the right place of the keys
 
 
 class LoraWanContainer { 
@@ -57,11 +38,11 @@
     void ConfigureRadioForRx1 ( void );
     void ConfigureRadioForRx2 ( void );
     void ConfigureTimerForRx  ( int type );
-    int  DecodeRxFrame        ( void );
     void UpdateMacLayer       ( void );
-    void ParseManagementPacket( void );
     void UpdateJoinProcedure  ( void );
     uint8_t IsFrameToSend;
+    eRxPacketType   DecodeRxFrame            ( void );
+    eStatusLoRaWan  ParseManagementPacket    ( void );
 /* LoraWan Context */ 
 /* Only 16 ch mask => ChMaskCntl not used */
 /* Duty cycle is not managed */
@@ -130,6 +111,9 @@
     uint8_t  JoinedStatus; 
     
 /* LoRaWan Mac Data for nwk Ans */
+    uint8_t    MacNwkPayload[255];  //@note resize this buffer 
+    uint8_t    MacNwkPayloadSize;
+
     uint8_t    MacNwkAns[255];  //@note reuse user payload data or at least reduce size or use opt byte
     uint8_t    MacNwkAnsSize;
 
--- a/MinimouseSrc/PhyLayer.cpp	Fri Dec 15 13:15:04 2017 +0000
+++ b/MinimouseSrc/PhyLayer.cpp	Mon Dec 18 16:31:11 2017 +0000
@@ -21,13 +21,14 @@
 #include "MacLayer.h"
 #include "LoraWanProcess.h"
 #include "ApiRtc.h"
+#include "Define.h"
 
 static RadioEvents_t RadioEvents;
 
 
 
 RadioContainer::RadioContainer( PinName interrupt )
-                :Radio( NULL ), TxInterrupt( interrupt ), RxTimeoutInterrupt ( PB_1 ) {
+                :Radio( NULL ), TxInterrupt( interrupt ), RxTimeoutInterrupt ( RX_TIMEOUT_IT ) {
     StateRadioProcess = RADIOSTATE_IDLE;
     TxInterrupt.rise( this,&RadioContainer::IsrRadio );
     RxTimeoutInterrupt.rise( this,&RadioContainer::IsrRadio );
--- a/MinimouseSrc/main.cpp	Fri Dec 15 13:15:04 2017 +0000
+++ b/MinimouseSrc/main.cpp	Mon Dec 18 16:31:11 2017 +0000
@@ -11,7 +11,7 @@
 
 
 struct sBackUpFlash BackUpFlash;
-static LoraWanObjet Lp( PB_4 );
+static LoraWanObjet Lp( TX_RX_IT );
 
 //@note set to board definition
 //#define CHECKFCNTDOWN 0
--- a/radio/SX1276Lib/sx1276/sx1276-hal.cpp	Fri Dec 15 13:15:04 2017 +0000
+++ b/radio/SX1276Lib/sx1276/sx1276-hal.cpp	Mon Dec 18 16:31:11 2017 +0000
@@ -13,7 +13,7 @@
 Maintainers: Miguel Luis, Gregory Cristian and Nicolas Huguenin
 */
 #include "sx1276-hal.h"
-
+#include "Define.h"
 const RadioRegisters_t SX1276MB1xAS::RadioRegsInit[] = RADIO_INIT_REGISTERS_VALUE;
 
 
@@ -49,16 +49,7 @@
     this->settings.State = RF_IDLE ;
 }
 
-#define LORA_SPI_MOSI   PA_7
-#define LORA_SPI_MISO   PA_6
-#define LORA_SPI_SCLK   PB_3
-#define LORA_CS         PA_15
-#define LORA_RESET      PC_0
-#define LORA_DIO0       PB_4
-#define LORA_DIO1       PB_1
-#define LORA_DIO2       PB_0
-#define LORA_DIO3       PC_13
-#define LORA_DIO4       PA_5
+
 #define LORA_DIO5       PA_4
 #define LORA_ANT_RX     PA_1
 #define LORA_ANT_TX     PC_2
--- a/radio/SX1276Lib/sx1276/sx1276.cpp	Fri Dec 15 13:15:04 2017 +0000
+++ b/radio/SX1276Lib/sx1276/sx1276.cpp	Mon Dec 18 16:31:11 2017 +0000
@@ -17,7 +17,6 @@
 
 
 
-Serial pcsx (PA_9, PA_10);
 # define DEBUG_PRINT(x)   do {} while (0)
 const FskBandwidth_t SX1276::FskBandwidths[] =
 {       
@@ -1285,7 +1284,7 @@
             case MODEM_LORA:
                 {
                     int8_t snr = 0;
-                    pcsx.printf("RX interrupt func in  enter irq flag %d\n", Read(REG_LR_IRQFLAGS));
+                    //pcsx.printf("RX interrupt func in  enter irq flag %d\n", Read(REG_LR_IRQFLAGS));
                     // Clear Irq
                     Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_RXDONE );