hal_flash.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**************************************************************************************************
  2. Filename: hal_flash.c
  3. Revised: $Date: 2010-10-07 02:19:52 -0700 (Thu, 07 Oct 2010) $
  4. Revision: $Revision: 24049 $
  5. Description: This file contains the interface to the H/W Flash driver.
  6. Copyright 2006-2010 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_mcu.h"
  41. #include "hal_types.h"
  42. /**************************************************************************************************
  43. * @fn HalFlashRead
  44. *
  45. * @brief This function reads 'cnt' bytes from the internal flash.
  46. *
  47. * input parameters
  48. *
  49. * @param pg - A valid flash page number.
  50. * @param offset - A valid offset into the page.
  51. * @param buf - A valid buffer space at least as big as the 'cnt' parameter.
  52. * @param cnt - A valid number of bytes to read.
  53. *
  54. * output parameters
  55. *
  56. * None.
  57. *
  58. * @return None.
  59. **************************************************************************************************
  60. */
  61. void HalFlashRead(uint8 pg, uint16 offset, uint8 *buf, uint16 cnt)
  62. {
  63. // Calculate the offset into the containing flash bank as it gets mapped into XDATA.
  64. uint8 *pData = (uint8 *)(offset + HAL_FLASH_PAGE_MAP) +
  65. ((pg % HAL_FLASH_PAGE_PER_BANK) * HAL_FLASH_PAGE_SIZE);
  66. uint8 memctr = MEMCTR; // Save to restore.
  67. #if (!defined HAL_OAD_BOOT_CODE) && (!defined HAL_OTA_BOOT_CODE)
  68. halIntState_t is;
  69. #endif
  70. pg /= HAL_FLASH_PAGE_PER_BANK; // Calculate the flash bank from the flash page.
  71. #if (!defined HAL_OAD_BOOT_CODE) && (!defined HAL_OTA_BOOT_CODE)
  72. HAL_ENTER_CRITICAL_SECTION(is);
  73. #endif
  74. // Calculate and map the containing flash bank into XDATA.
  75. MEMCTR = (MEMCTR & 0xF8) | pg;
  76. while (cnt--)
  77. {
  78. *buf++ = *pData++;
  79. }
  80. MEMCTR = memctr;
  81. #if (!defined HAL_OAD_BOOT_CODE) && (!defined HAL_OTA_BOOT_CODE)
  82. HAL_EXIT_CRITICAL_SECTION(is);
  83. #endif
  84. }
  85. /**************************************************************************************************
  86. * @fn HalFlashWrite
  87. *
  88. * @brief This function writes 'cnt' bytes to the internal flash.
  89. *
  90. * input parameters
  91. *
  92. * @param addr - Valid HAL flash write address: actual addr / 4 and quad-aligned.
  93. * @param buf - Valid buffer space at least as big as 'cnt' X 4.
  94. * @param cnt - Number of 4-byte blocks to write.
  95. *
  96. * output parameters
  97. *
  98. * None.
  99. *
  100. * @return None.
  101. **************************************************************************************************
  102. */
  103. void HalFlashWrite(uint16 addr, uint8 *buf, uint16 cnt)
  104. {
  105. #if (defined HAL_DMA) && (HAL_DMA == TRUE)
  106. halDMADesc_t *ch = HAL_NV_DMA_GET_DESC();
  107. HAL_DMA_SET_SOURCE(ch, buf);
  108. HAL_DMA_SET_DEST(ch, &FWDATA);
  109. HAL_DMA_SET_VLEN(ch, HAL_DMA_VLEN_USE_LEN);
  110. HAL_DMA_SET_LEN(ch, (cnt * HAL_FLASH_WORD_SIZE));
  111. HAL_DMA_SET_WORD_SIZE(ch, HAL_DMA_WORDSIZE_BYTE);
  112. HAL_DMA_SET_TRIG_MODE(ch, HAL_DMA_TMODE_SINGLE);
  113. HAL_DMA_SET_TRIG_SRC(ch, HAL_DMA_TRIG_FLASH);
  114. HAL_DMA_SET_SRC_INC(ch, HAL_DMA_SRCINC_1);
  115. HAL_DMA_SET_DST_INC(ch, HAL_DMA_DSTINC_0);
  116. // The DMA is to be polled and shall not issue an IRQ upon completion.
  117. HAL_DMA_SET_IRQ(ch, HAL_DMA_IRQMASK_DISABLE);
  118. HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS);
  119. HAL_DMA_SET_PRIORITY(ch, HAL_DMA_PRI_HIGH);
  120. HAL_DMA_CLEAR_IRQ(HAL_NV_DMA_CH);
  121. HAL_DMA_ARM_CH(HAL_NV_DMA_CH);
  122. FADDRL = (uint8)addr;
  123. FADDRH = (uint8)(addr >> 8);
  124. FCTL |= 0x02; // Trigger the DMA writes.
  125. while (FCTL & 0x80); // Wait until writing is done.
  126. #endif
  127. }
  128. /**************************************************************************************************
  129. * @fn HalFlashErase
  130. *
  131. * @brief This function erases the specified page of the internal flash.
  132. *
  133. * input parameters
  134. *
  135. * @param pg - A valid flash page number to erase.
  136. *
  137. * output parameters
  138. *
  139. * None.
  140. *
  141. * @return None.
  142. **************************************************************************************************
  143. */
  144. void HalFlashErase(uint8 pg)
  145. {
  146. FADDRH = pg * (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE / 256);
  147. FCTL |= 0x01;
  148. }
  149. /**************************************************************************************************
  150. */