hal_flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**************************************************************************************************
  2. Filename: _hal_flash.c
  3. Revised: $Date:$
  4. Revision: $Revision:$
  5. Description: This file contains the interface to the H/W Flash driver.
  6. Copyright 2006-2009 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /* ------------------------------------------------------------------------------------------------
  34. * Includes
  35. * ------------------------------------------------------------------------------------------------
  36. */
  37. #include "hal_board_cfg.h"
  38. #include "hal_dma.h"
  39. #include "hal_flash.h"
  40. #include "hal_types.h"
  41. /* ------------------------------------------------------------------------------------------------
  42. * Macros
  43. * ------------------------------------------------------------------------------------------------
  44. */
  45. /* ------------------------------------------------------------------------------------------------
  46. * Constants
  47. * ------------------------------------------------------------------------------------------------
  48. */
  49. // These values depend on RAM_CODE_FLASH in the .xcl file used.
  50. #if defined CC2530F64 // RemoTI is using 0x01DDD --> pg 3, offset 0x5DD.
  51. #define OSET_OF_RAM_CODE 0x5DD
  52. #define PAGE_OF_RAM_CODE 3
  53. #define SIZE_OF_RAM_CODE 0x23
  54. #elif defined HAL_OAD_BOOT_CODE // OAD boot code does not use RAM copy
  55. #define OSET_OF_RAM_CODE 0x7E5
  56. #define PAGE_OF_RAM_CODE 0
  57. #define SIZE_OF_RAM_CODE 0x1B
  58. #else // Z-Stack is using 0x39DDD --> pg 51, offset 0x5DD.
  59. #define OSET_OF_RAM_CODE 0x5DD
  60. #define PAGE_OF_RAM_CODE 51
  61. #define SIZE_OF_RAM_CODE 0x23
  62. #endif
  63. /* ------------------------------------------------------------------------------------------------
  64. * Typedefs
  65. * ------------------------------------------------------------------------------------------------
  66. */
  67. /* ------------------------------------------------------------------------------------------------
  68. * Global Variables
  69. * ------------------------------------------------------------------------------------------------
  70. */
  71. /* ------------------------------------------------------------------------------------------------
  72. * Global Functions
  73. * ------------------------------------------------------------------------------------------------
  74. */
  75. /* ------------------------------------------------------------------------------------------------
  76. * Local Variables
  77. * ------------------------------------------------------------------------------------------------
  78. */
  79. #pragma location="RAM_CODE_XDATA"
  80. static __no_init uint8 ramCode[SIZE_OF_RAM_CODE];
  81. /* ------------------------------------------------------------------------------------------------
  82. * Local Functions
  83. * ------------------------------------------------------------------------------------------------
  84. */
  85. #pragma location="RAM_CODE_FLASH"
  86. #if defined HAL_OAD_BOOT_CODE
  87. static void HalFlashWriteTrigger(void);
  88. #else
  89. static __monitor void HalFlashWriteTrigger(void);
  90. #endif
  91. /**************************************************************************************************
  92. * @fn HalFlashInit
  93. *
  94. * @brief This function initializes the environment for this module.
  95. *
  96. * input parameters
  97. *
  98. * None.
  99. *
  100. * output parameters
  101. *
  102. * None.
  103. *
  104. * @return None.
  105. **************************************************************************************************
  106. */
  107. void HalFlashInit(void)
  108. {
  109. // Load the code to run from RAM into its reserved area of RAM once at startup.
  110. HalFlashRead(PAGE_OF_RAM_CODE, OSET_OF_RAM_CODE, ramCode, SIZE_OF_RAM_CODE);
  111. }
  112. /**************************************************************************************************
  113. * @fn HalFlashRead
  114. *
  115. * @brief This function reads 'cnt' bytes from the internal flash.
  116. *
  117. * input parameters
  118. *
  119. * @param pg - A valid flash page number.
  120. * @param offset - A valid offset into the page.
  121. * @param buf - A valid buffer space at least as big as the 'cnt' parameter.
  122. * @param cnt - A valid number of bytes to read.
  123. *
  124. * output parameters
  125. *
  126. * None.
  127. *
  128. * @return None.
  129. **************************************************************************************************
  130. */
  131. void HalFlashRead(uint8 pg, uint16 offset, uint8 *buf, uint16 cnt)
  132. {
  133. // Calculate the offset into the containing flash bank as it gets mapped into XDATA.
  134. uint8 *ptr = (uint8 *)(offset + HAL_FLASH_PAGE_MAP) +
  135. ((pg % HAL_FLASH_PAGE_PER_BANK) * HAL_FLASH_PAGE_SIZE);
  136. uint8 memctr = MEMCTR; // Save to restore.
  137. #if !defined HAL_OAD_BOOT_CODE
  138. halIntState_t is;
  139. #endif
  140. pg /= HAL_FLASH_PAGE_PER_BANK; // Calculate the flash bank from the flash page.
  141. #if !defined HAL_OAD_BOOT_CODE
  142. HAL_ENTER_CRITICAL_SECTION(is);
  143. #endif
  144. // Calculate and map the containing flash bank into XDATA.
  145. MEMCTR = (MEMCTR & 0xF8) | pg;
  146. while (cnt--)
  147. {
  148. *buf++ = *ptr++;
  149. }
  150. MEMCTR = memctr;
  151. #if !defined HAL_OAD_BOOT_CODE
  152. HAL_EXIT_CRITICAL_SECTION(is);
  153. #endif
  154. }
  155. /**************************************************************************************************
  156. * @fn HalFlashWrite
  157. *
  158. * @brief This function writes 'cnt' bytes to the internal flash.
  159. *
  160. * input parameters
  161. *
  162. * @param addr - Valid HAL flash write address: actual addr / 4 and quad-aligned.
  163. * @param buf - Valid buffer space at least as big as 'cnt' X 4.
  164. * @param cnt - Number of 4-byte blocks to write.
  165. *
  166. * output parameters
  167. *
  168. * None.
  169. *
  170. * @return None.
  171. **************************************************************************************************
  172. */
  173. void HalFlashWrite(uint16 addr, uint8 *buf, uint16 cnt)
  174. {
  175. halDMADesc_t *ch = HAL_NV_DMA_GET_DESC();
  176. HAL_DMA_SET_SOURCE(ch, buf);
  177. HAL_DMA_SET_DEST(ch, &FWDATA);
  178. HAL_DMA_SET_VLEN(ch, HAL_DMA_VLEN_USE_LEN);
  179. HAL_DMA_SET_LEN(ch, (cnt * HAL_FLASH_WORD_SIZE));
  180. HAL_DMA_SET_WORD_SIZE(ch, HAL_DMA_WORDSIZE_BYTE);
  181. HAL_DMA_SET_TRIG_MODE(ch, HAL_DMA_TMODE_SINGLE);
  182. HAL_DMA_SET_TRIG_SRC(ch, HAL_DMA_TRIG_FLASH);
  183. HAL_DMA_SET_SRC_INC(ch, HAL_DMA_SRCINC_1);
  184. HAL_DMA_SET_DST_INC(ch, HAL_DMA_DSTINC_0);
  185. // The DMA is to be polled and shall not issue an IRQ upon completion.
  186. HAL_DMA_SET_IRQ(ch, HAL_DMA_IRQMASK_DISABLE);
  187. HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS);
  188. HAL_DMA_SET_PRIORITY(ch, HAL_DMA_PRI_HIGH);
  189. HAL_DMA_CLEAR_IRQ(HAL_NV_DMA_CH);
  190. HAL_DMA_ARM_CH(HAL_NV_DMA_CH);
  191. FADDRL = (uint8)addr;
  192. FADDRH = (uint8)(addr >> 8);
  193. HalFlashWriteTrigger();
  194. }
  195. /**************************************************************************************************
  196. * @fn HalFlashErase
  197. *
  198. * @brief This function erases the specified page of the internal flash.
  199. *
  200. * input parameters
  201. *
  202. * @param pg - A valid flash page number to erase.
  203. *
  204. * output parameters
  205. *
  206. * None.
  207. *
  208. * @return None.
  209. **************************************************************************************************
  210. */
  211. void HalFlashErase(uint8 pg)
  212. {
  213. FADDRH = pg * (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE / 256);
  214. FCTL |= 0x01;
  215. }
  216. /**************************************************************************************************
  217. * @fn HalFlashWriteTrigger
  218. *
  219. * @brief This function must be copied to RAM before running because it triggers and then
  220. * awaits completion of Flash write, which can only be done from RAM.
  221. *
  222. * input parameters
  223. *
  224. * None.
  225. *
  226. * output parameters
  227. *
  228. * None.
  229. *
  230. * @return None.
  231. **************************************************************************************************
  232. */
  233. #if defined HAL_OAD_BOOT_CODE
  234. #pragma optimize=medium
  235. static void HalFlashWriteTrigger(void)
  236. #else
  237. static __monitor void HalFlashWriteTrigger(void)
  238. #endif
  239. {
  240. MEMCTR |= 0x08; // Start the Memory Arbiter running CODE from RAM.
  241. FCTL |= 0x02; // Trigger the DMA writes.
  242. while (FCTL & 0x80); // Wait until writing is done.
  243. MEMCTR &= ~0x08; // Stop the Memory Arbiter.
  244. }
  245. /**************************************************************************************************
  246. */