[Atmel] 跑一下SAM4N例程(十二):FLASH测试

[复制链接]
1574|0
 楼主| ddllxxrr 发表于 2015-3-19 21:57 | 显示全部楼层 |阅读模式
本帖最后由 ddllxxrr 于 2015-3-20 15:57 编辑

可以在Atmle Studio6.2环境下,直接插上开发板,则新建例程可以直接打开例程。



其第一步就是串口初始化:

  1. static void configure_console(void)
  2. {
  3.         const usart_serial_options_t uart_serial_options = {
  4.                 .baudrate = CONF_UART_BAUDRATE,
  5. #ifdef CONF_UART_CHAR_LENGTH
  6.                 .charlength = CONF_UART_CHAR_LENGTH,
  7. #endif
  8.                 .paritytype = CONF_UART_PARITY,
  9. #ifdef CONF_UART_STOP_BITS
  10.                 .stopbits = CONF_UART_STOP_BITS,
  11. #endif
  12.         };

  13.         /* Configure console UART. */
  14.         sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
  15.         stdio_serial_init(CONF_UART, &uart_serial_options);
  16. }

CONSOLE_UART_ID 在sam4n_xplained_pro.h中定义

而CONF_UART_BAUDRATE 及 CONF_UART_PARITY在conf_uart_serial.h中定义。

而 TEST_PAGE_ADDRESS是个宏定义:(IFLASH_ADDR + IFLASH_SIZE - IFLASH_PAGE_SIZE * 4)

IFLASH_ADDR及IFLASH_SIZE还有IFLASH_PAGE_SIZE都在sam4n16b.h中定义

主程序是对FLASH的一系列操作:


  1. /* Output example information */
  2.         puts(STRING_HEADER);

  3.         /* Initialize flash: 6 wait states for flash writing. */
  4.         ul_rc = flash_init(FLASH_ACCESS_MODE_128, 6);
  5.         if (ul_rc != FLASH_RC_OK) {
  6.                 printf("-F- Initialization error %lu\n\r", (UL)ul_rc);
  7.                 return 0;
  8.         }

  9.         /* Unlock page */
  10.         printf("-I- Unlocking test page: 0x%08x\r\n", ul_test_page_addr);
  11.         ul_rc = flash_unlock(ul_test_page_addr,
  12.                         ul_test_page_addr + IFLASH_PAGE_SIZE - 1, 0, 0);
  13.         if (ul_rc != FLASH_RC_OK) {
  14.                 printf("-F- Unlock error %lu\n\r", (UL)ul_rc);
  15.                 return 0;
  16.         }

  17.         /* Write page */
  18.         printf("-I- Writing test page with walking bit pattern\n\r");
  19.         for (ul_idx = 0; ul_idx < (IFLASH_PAGE_SIZE / 4); ul_idx++) {
  20.                 ul_page_buffer[ul_idx] = 1 << (ul_idx % 32);
  21.         }

  22. #if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CP || SAMG || SAM4CM)
  23.         /* The EWP command is not supported for non-8KByte sectors in all devices
  24.          *  SAM4 series, so an erase command is requried before the write operation.
  25.          */
  26.         ul_rc = flash_erase_sector(ul_test_page_addr);
  27.         if (ul_rc != FLASH_RC_OK) {
  28.                 printf("-F- Flash programming error %lu\n\r", (UL)ul_rc);
  29.                 return 0;
  30.         }

  31.         ul_rc = flash_write(ul_test_page_addr, ul_page_buffer,
  32.                         IFLASH_PAGE_SIZE, 0);
  33. #else
  34.         ul_rc = flash_write(ul_test_page_addr, ul_page_buffer,
  35.                         IFLASH_PAGE_SIZE, 1);
  36. #endif
  37.         if (ul_rc != FLASH_RC_OK) {
  38.                 printf("-F- Flash programming error %lu\n\r", (UL)ul_rc);
  39.                 return 0;
  40.         }

  41.         /* Validate page */
  42.         printf("-I- Checking page contents ");
  43.         for (ul_idx = 0; ul_idx < (IFLASH_PAGE_SIZE / 4); ul_idx++) {
  44.                 printf(".");
  45.                 if (pul_test_page[ul_idx] != ul_page_buffer[ul_idx]) {
  46.                         printf("\n\r-F- data error\n\r");
  47.                         return 0;
  48.                 }
  49.         }
  50.         printf("OK\n\r");

  51. #if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CP || SAMG || SAM4CM)
  52.         /* The EWP command is not supported for non-8KByte sectors in some SAM4
  53.          * series, so an erase command is requried before the write operation.
  54.          */
  55.         ul_rc = flash_erase_sector(ul_test_page_addr);
  56.         if (ul_rc != FLASH_RC_OK) {
  57.                 printf("-F- Flash programming error %lu\n\r", (UL)ul_rc);
  58.                 return 0;
  59.         }
  60. #endif

  61.         /* Lock page */
  62.         printf("-I- Locking test page\n\r");
  63.         ul_rc = flash_lock(ul_test_page_addr,
  64.                         ul_test_page_addr + IFLASH_PAGE_SIZE - 1, 0, 0);
  65.         if (ul_rc != FLASH_RC_OK) {
  66.                 printf("-F- Flash locking error %lu\n\r", (UL)ul_rc);
  67.                 return 0;
  68.         }

  69.         /* Check if the associated region is locked. */
  70.         printf("-I- Try to program the locked page ...\n\r");
  71.         ul_rc = flash_write(ul_test_page_addr, ul_page_buffer,
  72.                         IFLASH_PAGE_SIZE,
  73. #if (SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM)
  74.                         0);
  75. #else
  76.                         1);
  77. #endif
  78.         if (ul_rc != FLASH_RC_OK) {
  79.                 printf("-I- The page to be programmed belongs to locked region. Error %lu\n\r",
  80.                                 (UL)ul_rc);
  81.         }

  82.         printf("-I- Please open Segger's JMem program \n\r");
  83.         printf("-I- Read memory at address 0x%08lx to check contents\n\r",
  84.                         (UL)ul_test_page_addr);
  85.         printf("-I- Press any key to continue...\n\r");
  86.         scanf("%c", (char *)&uc_key);

  87.         printf("-I- Good job!\n\r"
  88.                         "-I- Now set the security bit \n\r"
  89.                         "-I- Press any key to continue to see what happened...\n\r");
  90.         scanf("%c", (char *)&uc_key);

  91.         /* Set security bit */
  92.         printf("-I- Setting security bit \n\r");
  93.         ul_rc = flash_enable_security_bit();
  94.         if (ul_rc != FLASH_RC_OK) {
  95.                 printf("-F- Set security bit error %lu\n\r", (UL)ul_rc);
  96.         }

  97.         printf("-I- All tests done\n\r");

  98.         while (1) {
  99.                 /* Do nothing */
  100.         }


这个程序执行了以下几项:
锁定测试页。
编程测试页,写为(0x1,0x2,0x4,...)
检测写得对否
锁定测试页,然后看锁没锁。
设定密码比特。


以下是运行截图:



注意本程执行后会锁死,解决的方法如下:

SAM4N XPLAINED pro出现 No device detected. Error 4109的解决办法

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

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

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7001

帖子

68

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