MSPML1306小练-从官方例程看FLASH的写入顺序

[复制链接]
 楼主| xyz549040622 发表于 2023-11-30 22:30 | 显示全部楼层 |阅读模式
本帖最后由 xyz549040622 于 2023-11-30 22:31 编辑
  1. /*
  2. * Copyright (c) 2021, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * *  Redistributions of source code must retain the above copyright
  10. *    notice, this list of conditions and the following disclaimer.
  11. *
  12. * *  Redistributions in binary form must reproduce the above copyright
  13. *    notice, this list of conditions and the following disclaimer in the
  14. *    documentation and/or other materials provided with the distribution.
  15. *
  16. * *  Neither the name of Texas Instruments Incorporated nor the names of
  17. *    its contributors may be used to endorse or promote products derived
  18. *    from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */

  32. #include "ti_msp_dl_config.h"

  33. /* Address in main memory to write to */
  34. #define MAIN_BASE_ADDRESS (0x00001000)

  35. /* 8-bit data to write to flash */
  36. uint8_t gData8 = 0x11;

  37. /* 16-bit data to write to flash */
  38. uint16_t gData16 = 0x2222;

  39. /* 32-bit data to write to flash */
  40. uint32_t gData32 = 0x33333333;

  41. /* Array to write 64-bits to flash */
  42. uint32_t gDataArray64[] = {0xABCDEF00, 0x12345678};

  43. /* 32-bit data array to write to flash */
  44. uint32_t gDataArray32[] = {0x00000000, 0x11111111, 0x22222222, 0x33333333,
  45.     0x4444, 0x55555, 0x66, 0x77, 0x8, 0x9};

  46. volatile DL_FLASHCTL_COMMAND_STATUS gCmdStatus;

  47. int main(void)
  48. {
  49.     SYSCFG_DL_init();

  50.     /* Unprotect sector in main memory*/
  51.     DL_FlashCTL_unprotectSector(
  52.         FLASHCTL, MAIN_BASE_ADDRESS, DL_FLASHCTL_REGION_SELECT_MAIN);
  53.     /* Erase sector in main memory */
  54.     gCmdStatus = DL_FlashCTL_eraseMemoryFromRAM(
  55.         FLASHCTL, MAIN_BASE_ADDRESS, DL_FLASHCTL_COMMAND_SIZE_SECTOR);
  56.     if (gCmdStatus == DL_FLASHCTL_COMMAND_STATUS_FAILED) {
  57.         /* If command was not successful, set a breakpoint */
  58.         __BKPT(0);
  59.     }

  60.     /* 8-bit write to flash in main memory without ECC */
  61.     DL_FlashCTL_unprotectSector(
  62.         FLASHCTL, MAIN_BASE_ADDRESS, DL_FLASHCTL_REGION_SELECT_MAIN);
  63.     gCmdStatus = DL_FlashCTL_programMemoryFromRAM8(
  64.         FLASHCTL, MAIN_BASE_ADDRESS, &gData8);
  65.     if (gCmdStatus == DL_FLASHCTL_COMMAND_STATUS_FAILED) {
  66.         /* If command was not successful, set a breakpoint */
  67.         __BKPT(0);
  68.     }

  69.     /*
  70.      * 16-bit write to flash in main memory without ECC.
  71.      * The target program address must be a flash word address
  72.      * (8-byte aligned), so we increase the address in increments of 8.
  73.      */
  74.     DL_FlashCTL_unprotectSector(
  75.         FLASHCTL, MAIN_BASE_ADDRESS, DL_FLASHCTL_REGION_SELECT_MAIN);
  76.     gCmdStatus = DL_FlashCTL_programMemoryFromRAM16(
  77.         FLASHCTL, (MAIN_BASE_ADDRESS + 8), &gData16);
  78.     if (gCmdStatus == DL_FLASHCTL_COMMAND_STATUS_FAILED) {
  79.         /* If command was not successful, set a breakpoint */
  80.         __BKPT(0);
  81.     }

  82.     /* 32-bit write to flash in main memory without ECC */
  83.     DL_FlashCTL_unprotectSector(
  84.         FLASHCTL, MAIN_BASE_ADDRESS, DL_FLASHCTL_REGION_SELECT_MAIN);
  85.     gCmdStatus = DL_FlashCTL_programMemoryFromRAM32(
  86.         FLASHCTL, (MAIN_BASE_ADDRESS + 16), &gData32);
  87.     if (gCmdStatus == DL_FLASHCTL_COMMAND_STATUS_FAILED) {
  88.         /* If command was not successful, set a breakpoint */
  89.         __BKPT(0);
  90.     }

  91.     /*
  92.      * 64-bit write to flash in main memory without ECC.
  93.      * Data must be loaded 32-bits at a time, but a single word program
  94.      * is executed
  95.      */
  96.     DL_FlashCTL_unprotectSector(
  97.         FLASHCTL, MAIN_BASE_ADDRESS, DL_FLASHCTL_REGION_SELECT_MAIN);
  98.     gCmdStatus = DL_FlashCTL_programMemoryFromRAM64(
  99.         FLASHCTL, (MAIN_BASE_ADDRESS + 24), &gDataArray64[0]);
  100.     if (gCmdStatus == DL_FLASHCTL_COMMAND_STATUS_FAILED) {
  101.         __BKPT(0);
  102.     }

  103.     /* If writes successful, set LED */
  104.     DL_GPIO_clearPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);

  105.     /*
  106.      * Set a breakpoint to check the contents of MAIN_BASE_ADDRESS:
  107.      * FF FF FF 11
  108.      * FF FF FF FF
  109.      * FF FF 22 22
  110.      * FF FF FF FF
  111.      * 33 33 33 33
  112.      * FF FF FF FF
  113.      * AB CD EF 00
  114.      * 12 34 56 78
  115.      */
  116.     __BKPT(0);

  117.     while (1) {
  118.         __WFI();
  119.     }
如上所述是一段操作Flash的基于MSPM0L的例程,从此可以看出flash的操作顺序:
1、解锁受保护的扇区
2、擦除扇区
3、写入数据
FLASH擦写操作无需SYSCONFIG的配置,可以直接调用函数来完成,每次执行完成擦写操作的时候,FLASH的写保护寄存器都会置位,也就是自动保护起来,所以下一次操作之前都需要重新解锁。



您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群: 嵌入式系统arm初学者 224636155←← +→→点击-->小 i 精品课全集,21ic公开课~~←←→→点击-->小 i 精品课全集,给你全方位的技能策划~~←←

2841

主题

19330

帖子

110

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:qq群: 嵌入式系统arm初学者 224636155←← +→→点击-->小 i 精品课全集,21ic公开课~~←←→→点击-->小 i 精品课全集,给你全方位的技能策划~~←←

2841

主题

19330

帖子

110

粉丝
快速回复 在线客服 返回列表 返回顶部