本帖最后由 ddllxxrr 于 2015-3-20 15:57 编辑
可以在Atmle Studio6.2环境下,直接插上开发板,则新建例程可以直接打开例程。
其第一步就是串口初始化:
static void configure_console(void)
{
const usart_serial_options_t uart_serial_options = {
.baudrate = CONF_UART_BAUDRATE,
#ifdef CONF_UART_CHAR_LENGTH
.charlength = CONF_UART_CHAR_LENGTH,
#endif
.paritytype = CONF_UART_PARITY,
#ifdef CONF_UART_STOP_BITS
.stopbits = CONF_UART_STOP_BITS,
#endif
};
/* Configure console UART. */
sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
stdio_serial_init(CONF_UART, &uart_serial_options);
}
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的一系列操作:
/* Output example information */
puts(STRING_HEADER);
/* Initialize flash: 6 wait states for flash writing. */
ul_rc = flash_init(FLASH_ACCESS_MODE_128, 6);
if (ul_rc != FLASH_RC_OK) {
printf("-F- Initialization error %lu\n\r", (UL)ul_rc);
return 0;
}
/* Unlock page */
printf("-I- Unlocking test page: 0x%08x\r\n", ul_test_page_addr);
ul_rc = flash_unlock(ul_test_page_addr,
ul_test_page_addr + IFLASH_PAGE_SIZE - 1, 0, 0);
if (ul_rc != FLASH_RC_OK) {
printf("-F- Unlock error %lu\n\r", (UL)ul_rc);
return 0;
}
/* Write page */
printf("-I- Writing test page with walking bit pattern\n\r");
for (ul_idx = 0; ul_idx < (IFLASH_PAGE_SIZE / 4); ul_idx++) {
ul_page_buffer[ul_idx] = 1 << (ul_idx % 32);
}
#if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CP || SAMG || SAM4CM)
/* The EWP command is not supported for non-8KByte sectors in all devices
* SAM4 series, so an erase command is requried before the write operation.
*/
ul_rc = flash_erase_sector(ul_test_page_addr);
if (ul_rc != FLASH_RC_OK) {
printf("-F- Flash programming error %lu\n\r", (UL)ul_rc);
return 0;
}
ul_rc = flash_write(ul_test_page_addr, ul_page_buffer,
IFLASH_PAGE_SIZE, 0);
#else
ul_rc = flash_write(ul_test_page_addr, ul_page_buffer,
IFLASH_PAGE_SIZE, 1);
#endif
if (ul_rc != FLASH_RC_OK) {
printf("-F- Flash programming error %lu\n\r", (UL)ul_rc);
return 0;
}
/* Validate page */
printf("-I- Checking page contents ");
for (ul_idx = 0; ul_idx < (IFLASH_PAGE_SIZE / 4); ul_idx++) {
printf(".");
if (pul_test_page[ul_idx] != ul_page_buffer[ul_idx]) {
printf("\n\r-F- data error\n\r");
return 0;
}
}
printf("OK\n\r");
#if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CP || SAMG || SAM4CM)
/* The EWP command is not supported for non-8KByte sectors in some SAM4
* series, so an erase command is requried before the write operation.
*/
ul_rc = flash_erase_sector(ul_test_page_addr);
if (ul_rc != FLASH_RC_OK) {
printf("-F- Flash programming error %lu\n\r", (UL)ul_rc);
return 0;
}
#endif
/* Lock page */
printf("-I- Locking test page\n\r");
ul_rc = flash_lock(ul_test_page_addr,
ul_test_page_addr + IFLASH_PAGE_SIZE - 1, 0, 0);
if (ul_rc != FLASH_RC_OK) {
printf("-F- Flash locking error %lu\n\r", (UL)ul_rc);
return 0;
}
/* Check if the associated region is locked. */
printf("-I- Try to program the locked page ...\n\r");
ul_rc = flash_write(ul_test_page_addr, ul_page_buffer,
IFLASH_PAGE_SIZE,
#if (SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM)
0);
#else
1);
#endif
if (ul_rc != FLASH_RC_OK) {
printf("-I- The page to be programmed belongs to locked region. Error %lu\n\r",
(UL)ul_rc);
}
printf("-I- Please open Segger's JMem program \n\r");
printf("-I- Read memory at address 0x%08lx to check contents\n\r",
(UL)ul_test_page_addr);
printf("-I- Press any key to continue...\n\r");
scanf("%c", (char *)&uc_key);
printf("-I- Good job!\n\r"
"-I- Now set the security bit \n\r"
"-I- Press any key to continue to see what happened...\n\r");
scanf("%c", (char *)&uc_key);
/* Set security bit */
printf("-I- Setting security bit \n\r");
ul_rc = flash_enable_security_bit();
if (ul_rc != FLASH_RC_OK) {
printf("-F- Set security bit error %lu\n\r", (UL)ul_rc);
}
printf("-I- All tests done\n\r");
while (1) {
/* Do nothing */
}
这个程序执行了以下几项:
锁定测试页。
编程测试页,写为(0x1,0x2,0x4,...)
检测写得对否
锁定测试页,然后看锁没锁。
设定密码比特。
以下是运行截图:
注意本程执行后会锁死,解决的方法如下:
SAM4N XPLAINED pro出现 No device detected. Error 4109的解决办法
|