Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

190 lines
5.1 KiB

  1. /*!
  2. * \file eeprom-board.c
  3. *
  4. * \brief Target board EEPROM driver implementation
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013-2017 Semtech
  16. *
  17. * \endcode
  18. *
  19. * \author Miguel Luis ( Semtech )
  20. *
  21. * \author Gregory Cristian ( Semtech )
  22. */
  23. #include <stdint.h>
  24. #include <stdbool.h>
  25. #include "stm32l4xx.h"
  26. #include "eeprom_emul.h"
  27. #include "eeprom-board.h"
  28. #include "utilities.h"
  29. uint16_t EepromVirtualAddress[NB_OF_VARIABLES];
  30. __IO uint32_t ErasingOnGoing = 0;
  31. /*!
  32. * \brief Initializes the EEPROM emulation module.
  33. */
  34. void EepromMcuInit( void )
  35. {
  36. EE_Status eeStatus = EE_OK;
  37. // Unlock the Flash Program Erase controller
  38. HAL_FLASH_Unlock( );
  39. // Set user List of Virtual Address variables: 0x0000 and 0xFFFF values are prohibited
  40. for( uint16_t varValue = 0; varValue < NB_OF_VARIABLES; varValue++ )
  41. {
  42. EepromVirtualAddress[varValue] = varValue + 1;
  43. }
  44. // Set EEPROM emulation firmware to erase all potentially incompletely erased
  45. // pages if the system came from an asynchronous reset. Conditional erase is
  46. // safe to use if all Flash operations where completed before the system reset
  47. if( __HAL_PWR_GET_FLAG( PWR_FLAG_SB ) == RESET )
  48. {
  49. // System reset comes from a power-on reset: Forced Erase
  50. // Initialize EEPROM emulation driver (mandatory)
  51. eeStatus = EE_Init( EepromVirtualAddress, EE_FORCED_ERASE );
  52. if( eeStatus != EE_OK )
  53. {
  54. assert_param( LMN_STATUS_ERROR );
  55. }
  56. }
  57. else
  58. {
  59. // Clear the Standby flag
  60. __HAL_PWR_CLEAR_FLAG( PWR_FLAG_SB );
  61. // Check and Clear the Wakeup flag
  62. if( __HAL_PWR_GET_FLAG( PWR_FLAG_WUF1 ) != RESET )
  63. {
  64. __HAL_PWR_CLEAR_FLAG( PWR_FLAG_WUF1 );
  65. }
  66. // System reset comes from a STANDBY wakeup: Conditional Erase
  67. // Initialize EEPROM emulation driver (mandatory)
  68. eeStatus = EE_Init( EepromVirtualAddress, EE_CONDITIONAL_ERASE );
  69. if( eeStatus != EE_OK )
  70. {
  71. assert_param( LMN_STATUS_ERROR );
  72. }
  73. }
  74. // Lock the Flash Program Erase controller
  75. HAL_FLASH_Lock( );
  76. }
  77. /*!
  78. * \brief Indicates if an erasing operation is on going.
  79. *
  80. * \retval isEradingOnGoing Returns true is an erasing operation is on going.
  81. */
  82. bool EepromMcuIsErasingOnGoing( void )
  83. {
  84. return ErasingOnGoing;
  85. }
  86. LmnStatus_t EepromMcuWriteBuffer( uint16_t addr, uint8_t *buffer, uint16_t size )
  87. {
  88. LmnStatus_t status = LMN_STATUS_OK;
  89. EE_Status eeStatus = EE_OK;
  90. // Unlock the Flash Program Erase controller
  91. HAL_FLASH_Unlock( );
  92. CRITICAL_SECTION_BEGIN( );
  93. for( uint32_t i = 0; i < size; i++ )
  94. {
  95. eeStatus |= EE_WriteVariable8bits( EepromVirtualAddress[addr + i], buffer[i] );
  96. }
  97. CRITICAL_SECTION_END( );
  98. if( eeStatus != EE_OK )
  99. {
  100. status = LMN_STATUS_ERROR;
  101. }
  102. if( ( eeStatus & EE_STATUSMASK_CLEANUP ) == EE_STATUSMASK_CLEANUP )
  103. {
  104. ErasingOnGoing = 0;
  105. eeStatus |= EE_CleanUp( );
  106. }
  107. if( ( eeStatus & EE_STATUSMASK_ERROR ) == EE_STATUSMASK_ERROR )
  108. {
  109. status = LMN_STATUS_ERROR;
  110. }
  111. // Lock the Flash Program Erase controller
  112. HAL_FLASH_Lock( );
  113. return status;
  114. }
  115. LmnStatus_t EepromMcuReadBuffer( uint16_t addr, uint8_t *buffer, uint16_t size )
  116. {
  117. LmnStatus_t status = LMN_STATUS_OK;
  118. // Unlock the Flash Program Erase controller
  119. HAL_FLASH_Unlock( );
  120. for( uint32_t i = 0; i < size; i++ )
  121. {
  122. if( EE_ReadVariable8bits( EepromVirtualAddress[addr + i], buffer + i ) != EE_OK )
  123. {
  124. status = LMN_STATUS_ERROR;
  125. break;
  126. }
  127. }
  128. // Lock the Flash Program Erase controller
  129. HAL_FLASH_Lock( );
  130. return status;
  131. }
  132. void EepromMcuSetDeviceAddr( uint8_t addr )
  133. {
  134. assert_param( LMN_STATUS_ERROR );
  135. }
  136. LmnStatus_t EepromMcuGetDeviceAddr( void )
  137. {
  138. assert_param( LMN_STATUS_ERROR );
  139. return 0;
  140. }
  141. /*!
  142. * \brief FLASH end of operation interrupt callback.
  143. * \param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  144. * Mass Erase: Bank number which has been requested to erase
  145. * Page Erase: Page which has been erased
  146. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  147. * Program: Address which was selected for data program
  148. * \retval None
  149. */
  150. void HAL_FLASH_EndOfOperationCallback( uint32_t ReturnValue )
  151. {
  152. // Call CleanUp callback when all requested pages have been erased
  153. if( ReturnValue == 0xFFFFFFFF )
  154. {
  155. EE_EndOfCleanup_UserCallback( );
  156. }
  157. }
  158. /*!
  159. * \brief Clean Up end of operation interrupt callback.
  160. * \param None
  161. * \retval None
  162. */
  163. void EE_EndOfCleanup_UserCallback( void )
  164. {
  165. ErasingOnGoing = 0;
  166. }