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.
 
 
 
 
 
 

240 lines
6.9 KiB

  1. /*!
  2. * \file sysIrqHandlers.c
  3. *
  4. * \brief Default IRQ handlers
  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 "stm32l4xx.h"
  25. #include "eeprom_emul.h"
  26. /*!
  27. * \brief This function handles NMI exception.
  28. * \param None
  29. * \retval None
  30. */
  31. void NMI_Handler( void )
  32. {
  33. uint32_t corruptedflashaddress;
  34. // Check if NMI is due to flash ECCD (error detection)
  35. if( __HAL_FLASH_GET_FLAG( FLASH_FLAG_ECCD ) )
  36. {
  37. // Compute corrupted flash address
  38. corruptedflashaddress = FLASH_BASE + ( FLASH->ECCR & FLASH_ECCR_ADDR_ECC );
  39. #if defined(FLASH_OPTR_BFB2)
  40. // Add bank size to corrupteflashaddress if fail bank is bank 2
  41. if( READ_BIT( FLASH->ECCR, FLASH_ECCR_BK_ECC ) == FLASH_ECCR_BK_ECC )
  42. {
  43. corruptedflashaddress += FLASH_BANK_SIZE;
  44. }
  45. #endif
  46. // Check if corrupted flash address is in eeprom emulation pages
  47. if( ( corruptedflashaddress >= START_PAGE_ADDRESS ) || ( corruptedflashaddress <= END_EEPROM_ADDRESS ) )
  48. {
  49. /* Delete the corrupted flash address */
  50. if( EE_DeleteCorruptedFlashAddress( corruptedflashaddress ) == EE_OK )
  51. {
  52. // Resume execution if deletion succeeds
  53. return;
  54. }
  55. }
  56. }
  57. /* Go to infinite loop when NMI occurs in case:
  58. - ECCD is raised in eeprom emulation flash pages but corrupted flash address deletion fails
  59. - ECCD is raised out of eeprom emulation flash pages
  60. - no ECCD is raised */
  61. while( 1 )
  62. {
  63. }
  64. }
  65. /*!
  66. * \brief This function handles Hard Fault exception.
  67. * \param None
  68. * \retval None
  69. */
  70. void HardFault_Handler( void )
  71. {
  72. //*************************************************************************
  73. // When a power down or external reset occurs during a Flash Write operation,
  74. // the first line at 0x0 may be corrupted at 0x0 (low occurrence).
  75. // In this case the Flash content is restored.
  76. // address : flash bank1 base address
  77. // data : first flash line (64 bits). This variable must be updated after each build.
  78. // sram2address : sram2 start address to copy and restore first flash page
  79. //*************************************************************************
  80. uint32_t address = FLASH_BASE;
  81. uint64_t data = 0x08002df120001bc0;
  82. uint32_t sram2address = 0x20030000;
  83. uint32_t page = 0;
  84. uint32_t banks = FLASH_BANK_1;
  85. uint32_t element = 0U;
  86. if( ( *( __IO uint32_t* )address == 0x0 ) && ( *( __IO uint32_t* )( address + 4 ) == 0x0 ) )
  87. {
  88. // Authorize the FLASH Registers access
  89. FLASH->KEYR = FLASH_KEY1;
  90. FLASH->KEYR = FLASH_KEY2;
  91. // Save first flash page in SRAM2
  92. for( element = 2; element < FLASH_PAGE_SIZE; element++ )
  93. {
  94. *( __IO uint32_t* )( sram2address + ( element * 4 ) ) = *( __IO uint32_t* )( address + ( element * 4 ) );
  95. }
  96. // Restore the first flash line in SRAM2 to its correct value
  97. *( __IO uint32_t* )sram2address = ( uint32_t )data;
  98. *( __IO uint32_t* )( sram2address + 4 ) = ( uint32_t )( data >> 32 );
  99. // Clear FLASH all errors
  100. __HAL_FLASH_CLEAR_FLAG( FLASH_FLAG_ALL_ERRORS );
  101. // Erase first flash page
  102. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  103. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  104. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  105. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  106. if( READ_BIT( FLASH->OPTR, FLASH_OPTR_DBANK ) == RESET )
  107. {
  108. CLEAR_BIT( FLASH->CR, FLASH_CR_BKER );
  109. }
  110. else
  111. #endif
  112. {
  113. if( ( banks & FLASH_BANK_1 ) != RESET )
  114. {
  115. CLEAR_BIT( FLASH->CR, FLASH_CR_BKER );
  116. }
  117. else
  118. {
  119. SET_BIT( FLASH->CR, FLASH_CR_BKER );
  120. }
  121. }
  122. #endif
  123. // Proceed to erase the page
  124. MODIFY_REG( FLASH->CR, FLASH_CR_PNB, ( page << POSITION_VAL( FLASH_CR_PNB ) ) );
  125. SET_BIT( FLASH->CR, FLASH_CR_PER );
  126. SET_BIT( FLASH->CR, FLASH_CR_STRT );
  127. // Wait for last operation to be completed
  128. while( __HAL_FLASH_GET_FLAG( FLASH_FLAG_BSY ) ){ }
  129. // If the erase operation is completed, disable the PER Bit
  130. CLEAR_BIT( FLASH->CR, ( FLASH_CR_PER | FLASH_CR_PNB ) );
  131. // Restore first flash page in flash from SRAM2
  132. for( element = 0; element < FLASH_PAGE_SIZE; element++ )
  133. {
  134. // Wait for last operation to be completed
  135. while( __HAL_FLASH_GET_FLAG( FLASH_FLAG_BSY ) ){ }
  136. // Set PG bit
  137. SET_BIT( FLASH->CR, FLASH_CR_PG );
  138. // Write in flash
  139. *( __IO uint32_t* )( address + ( element * 4 ) ) = *( __IO uint32_t* )( sram2address + ( element * 4 ) );
  140. }
  141. // System reset
  142. NVIC_SystemReset( );
  143. }
  144. // Go to infinite loop when Hard Fault exception occurs
  145. while( 1 )
  146. {
  147. }
  148. }
  149. /*!
  150. * \brief This function handles Memory Manage exception.
  151. * \param None
  152. * \retval None
  153. */
  154. void MemManage_Handler( void )
  155. {
  156. /* Go to infinite loop when Memory Manage exception occurs */
  157. while ( 1 )
  158. {
  159. }
  160. }
  161. /*!
  162. * \brief This function handles Bus Fault exception.
  163. * \param None
  164. * \retval None
  165. */
  166. void BusFault_Handler( void )
  167. {
  168. /* Go to infinite loop when Bus Fault exception occurs */
  169. while ( 1 )
  170. {
  171. }
  172. }
  173. /*!
  174. * \brief This function handles Usage Fault exception.
  175. * \param None
  176. * \retval None
  177. */
  178. void UsageFault_Handler( void )
  179. {
  180. /* Go to infinite loop when Usage Fault exception occurs */
  181. while ( 1 )
  182. {
  183. }
  184. }
  185. /*!
  186. * \brief This function handles Debug Monitor exception.
  187. * \param None
  188. * \retval None
  189. */
  190. void DebugMon_Handler( void )
  191. {
  192. }
  193. /*!
  194. * \brief This function handles Flash interrupt request.
  195. * \param None
  196. * \retval None
  197. */
  198. void FLASH_IRQHandler( void )
  199. {
  200. HAL_FLASH_IRQHandler();
  201. }
  202. /*!
  203. * \brief This function handles PVD interrupt request.
  204. * \param None
  205. * \retval None
  206. */
  207. void PVD_PVM_IRQHandler( void )
  208. {
  209. // Loop inside the handler to prevent the Cortex from using the Flash,
  210. // allowing the flash interface to finish any ongoing transfer.
  211. while( __HAL_PWR_GET_FLAG( PWR_FLAG_PVDO ) != RESET )
  212. {
  213. }
  214. }