风筝
发表于: 2018-12-17 12:50:54 | 显示全部楼层

本篇文章主要描述了如何使用Atmel ICE编程SAMD21微控制器,在Mac OS X或Linux Debian系统上使用命令行,无需Atmel Studio或任何其他IDE,甚至任何硬件抽象库。


为了说明对SAMD21编程的方式,我们将使用Sparkfun SAMD21 dev分线板作为我们的目标。它采用了流行的Atmel SAMD21G18,我们的示例应该在类似平台上进行很少或不进行修改,例如Arduino M0或TAU


在硬件方面,本文还假设您拥有Atmel ICE Programmer。

atmelice_leds_small.jpg

您需要安装一些软件包来编程SAMD21开发板。在Debian Linux上,使用您的包管理器(例如'aptitude')。在Mac上,我们建议使用homebrew来安装此处描述的不同工具。


第1步:安装编程插头

要使用OpenOCD对SAMD21板进行编程,您需要使用SWD插头将该编程器连接到您的开发板上。在Sparkfun SAMD21分线板上,此SWD插头存在但未焊接,如下图所示,位于左侧。您需要在右侧安装一个2x5针1.27mm公头,如下图所示。

std_swd_samd21.jpg


您不一定需要将插头焊接到电路板上:通常只需插入插头保证电气接触良好即可。然后,您将在Atmel ICE上插入相应的连接器。由于连接器未锁定,因此有两种方式可以将Atmel ICE插入电路板上。如果您尝试一个方向但不起作用,只需翻转连接器即可!


步骤2:为ARM安装C编译器

需要在系统上安装ARM开发人员工具(arm-none-eabi)。


在Mac上,使用homebrew,它归结为一个命令:

  1. $ brew cask install gcc-arm-embedded
复制代码

在具有Debian风格操作系统的Linux上,您需要引用您的包管理器(apt-get或aptitude)。


第3步:安装OpenOCD

在Mac上安装OpenOCD也是一行代码:

  1. $ brew install openocd
复制代码

在Debian Linux上,可以使用类似aptitude install openocd的命令就可以了。


安装后,下一步是正确设置OpenOCD。为此,创建一个名为openocf.cfg的文件,其中包含以下内容:

  1. # Atmel-ICE JTAG/SWD in-circuit debugger.
  2. interface cmsis-dap

  3. # Chip info
  4. set CHIPNAME at91samd21g18
  5. source [find target/at91samdXX.cfg]
复制代码

您应该更改值91samd21g18以匹配您正在使用的微控制器。


您只需键入openocd即可测试openocd.cfg文件。您应该得到类似于此的输出:

  1. $ openocd
  2. Open On-Chip Debugger 0.10.0
  3. Licensed under GNU GPL v2
  4. For bug reports, read
  5.     http://openocd.org/doc/doxygen/bugs.html
  6. Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.
  7. none separate
  8. adapter speed: 400 kHz
  9. cortex_m reset_config sysresetreq
  10. Info : CMSIS-DAP: SWD  Supported
  11. Info : CMSIS-DAP: JTAG Supported
  12. Info : CMSIS-DAP: Interface Initialised (SWD)
  13. Info : CMSIS-DAP: FW Version = 01.26.0081
  14. Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
  15. Info : CMSIS-DAP: Interface ready
  16. Info : clock speed 400 kHz
  17. in procedure 'init'
  18. in procedure 'ocd_bouncer'
复制代码

现在,您可以在要编程的电路板中插入SWD标头。 不要忘记单独为电路板供电,例如USB。 如果再次启动OpenOCD,则应获得以下输出:

  1. $ openocd
  2. Open On-Chip Debugger 0.10.0
  3. Licensed under GNU GPL v2
  4. For bug reports, read
  5.     http://openocd.org/doc/doxygen/bugs.html
  6. Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.
  7. none separate
  8. adapter speed: 400 kHz
  9. cortex_m reset_config sysresetreq
  10. Info : CMSIS-DAP: SWD  Supported
  11. Info : CMSIS-DAP: JTAG Supported
  12. Info : CMSIS-DAP: Interface Initialised (SWD)
  13. Info : CMSIS-DAP: FW Version = 01.26.0081
  14. Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
  15. Info : CMSIS-DAP: Interface ready
  16. Info : clock speed 400 kHz
  17. Info : SWD DPIDR 0x0bc11477
  18. Info : at91samd21g18.cpu: hardware has 4 breakpoints, 2 watchpoints
复制代码

如果失败,您可以反向插入SWD连接器:只需将其翻转即可。


现在,当OpenOCD仍在运行时,我们可以通过在另一个终端窗口中键入arm-none-eabi-gdb -iex“target extended-remote localhost:3333”来测试gdb的工作原理:

  1. $ arm-none-eabi-gdb -iex "target extended-remote localhost:3333"
  2. GNU gdb (GNU Tools for ARM Embedded Processors) 7.10.1.20151217-cvs
  3. Copyright (C) 2015 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  7. and "show warranty" for details.
  8. This GDB was configured as "--host=x86_64-apple-darwin10 --target=arm-none-eabi".
  9. Type "show configuration" for configuration details.
  10. For bug reporting instructions, please see:
  11. <http://www.gnu.org/software/gdb/bugs/>.
  12. Find the GDB manual and other documentation resources online at:
  13. <http://www.gnu.org/software/gdb/documentation/>.
  14. For help, type "help".
  15. Type "apropos word" to search for commands related to "word".
  16. Remote debugging using localhost:3333
  17. 0x00000168 in ?? ()
  18. (gdb)
复制代码

如果你运行到此,OpenOCD已经完成了。


第4步:编写代码使LED闪烁

Sparkfun SAMD21分线板的引脚上标有“D13”的LED,它映射到GPIO PA17:端口A上的第17个io端口。我们将使其闪烁以测试我们的设置。 如果您有另一块电路板,它可能在不同的PIN上有LED或根本没有,您需要调整下面的代码(例如,SAMD21 Xplained pro在PB30上有一个LED)。

  1. /*
  2. * main.c
  3. */
  4. #include <samd21.h>

  5. static void delay(int n)
  6. {
  7.     int i;

  8.     for (;n >0; n--)
  9.     {
  10.         for (i=0;i<100;i++)
  11.             __asm("nop");
  12.     }
  13. }

  14. int main(void)
  15. {
  16.     REG_PORT_DIR0 |= (1<<17);
  17.     while (1)
  18.     {
  19.         REG_PORT_OUT0 &= ~(1<<17);
  20.         delay(500);
  21.         REG_PORT_OUT0 |= (1<<17);
  22.         delay(500);
  23.     }
  24. }
复制代码

在上面的代码中,通过设置和清除名为REG_PORT_DIR0的特定寄存器的第17位来实现LED PA17的闪烁,该寄存器对应于SAMD21上的端口A.实际上有几种方法可以在SAMD21上实现相同的结果,我们只是为了简单起见而展示了一个。


要编译此文件,您需要Microchip / Atmel提供的一组标头。首先,从http://www.microchip.com/avr-support/advanced-software-framework-(asf)下载Atmel软件框架(ASF)。实际上,您只会使用这个大框架的一小部分,如果以后需要,您将能够删除该框架的大部分内容。


解压缩文件时,根据您下载的版本,您将获得名为xdk-asf-3.37 /的类似目录。让我们将ASF_ROOT命名为与该目录对应的绝对路径(例如,设置ASF_ROOT =“/ Users / pannetra / Src / xdk-asf-3.37”)。


转到放置openocd.cfg文件的目录并执行以下操作:

  1. $ cp $ASF_ROOT/sam0/utils/cmsis/samd21/source/gcc/startup_samd21.c .
复制代码

现在,根据您拥有的微控制器,以下步骤需要进行小型定制。对于我们的Sparkfun板,它是SAMD21G18A:

  1. $ cp $ASF_ROOT/sam0/utils/linker_scripts/samd21/gcc/samd21g18a_flash.ld .
复制代码

如果您有一个与SAMD21G18A不同的微控制器,则应更改文件名samd21g18a_flash.ld以匹配您的微控制器。

  1. LDSCRIPT = samd21g18a_flash.ld
  2. PTYPE=__SAMD21G18A__

  3. CC=arm-none-eabi-gcc
  4. LD=arm-none-eabi-gcc
  5. AR=arm-none-eabi-ar
  6. AS=arm-none-eabi-as

  7. ELF=$(notdir $(CURDIR)).elf

  8. ASF_ROOT=../../Src/xdk-asf-3.37

  9. INCLUDES= \
  10.           sam0/utils/cmsis/samd21/include \
  11.           sam0/utils/cmsis/samd21/source \
  12.           thirdparty/CMSIS/Include \
  13.           thirdparty/CMSIS/Lib/GCC

  14. OBJS = startup_samd21.o main.o

  15. LDFLAGS+= -T$(LDSCRIPT) -mthumb -mcpu=cortex-m0 -Wl,--gc-sections
  16. CFLAGS+= -mcpu=cortex-m0 -mthumb -g
  17. CFLAGS+= $(INCLUDES:%=-I $(ASF_ROOT)/%) -I .
  18. CFLAGS+= -D$(PTYPE)
  19. CFLAGS+=-pipe -Wall -Wstrict-prototypes -Wmissing-prototypes -Werror-implicit-function-declaration \
  20. -Wpointer-arith -std=gnu99 -fno-strict-aliasing -ffunction-sections -fdata-sections \
  21. -Wchar-subscripts -Wcomment -Wformat=2 -Wimplicit-int -Wmain -Wparentheses -Wsequence-point \
  22. -Wreturn-type -Wswitch -Wtrigraphs -Wunused -Wuninitialized -Wunknown-pragmas -Wfloat-equal \
  23. -Wundef -Wshadow -Wbad-function-cast -Wwrite-strings -Wsign-compare -Waggregate-return \
  24. -Wmissing-declarations -Wformat -Wmissing-format-attribute -Wno-deprecated-declarations \
  25. -Wpacked -Wredundant-decls -Wnested-externs -Wlong-long -Wunreachable-code -Wcast-align \
  26. --param max-inline-insns-single=500

  27. $(ELF):     $(OBJS)
  28.         $(LD) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

  29. # compile and generate dependency info

  30. %.o:    %.c
  31.         $(CC) -c $(CFLAGS) [        DISCUZ_CODE_1035        ]lt; -o $@
  32.         $(CC) -MM $(CFLAGS) [        DISCUZ_CODE_1035        ]lt; > $*.d

  33. %.o:    %.s
  34.         $(AS) [        DISCUZ_CODE_1035        ]lt; -o $@

  35. info:      
  36.         @echo CFLAGS=$(CFLAGS)
  37.         @echo OBJS=$(OBJS)

  38. clean:
  39.         rm -f $(OBJS) $(OBJS:.o=.d) $(ELF) $(CLEANOTHER)

  40. debug:  $(ELF)
  41.         arm-none-eabi-gdb -iex "target extended-remote localhost:3333" $(ELF)

  42. -include    $(OBJS:.o=.d)
复制代码

上面的Makefile源于Geoffrey Brown在STM32上的伟大工作


如果您有一个与SAMD21G18A不同的微控制器,则需要在Makefile中更改以下两行:LDSCRIPT = samd21g18a_flash.ld和PTYPE = __ SAMD21G18A__,用您自己的samd21g18a替换对samd21g18a的引用。


第5步:编译并运行代码

如果您按照上述所有步骤操作,则应在当前目录中包含以下文件:

●    Makefile文件

●    openocd.cfg

●    startup_samd21.c

●    main.c

●    samd21g18a_flash.ld


我们将检查代码是否按预期编译。

  1. $ make
复制代码

现在,您已准备好运行该程序。 连接Atmel ICE并为您的电路板供电。 如前所示启动OpenOCD。 在单独的终端窗口中,我们将使用gdb加载程序并运行它:

  1. $ make debug
  2. arm-none-eabi-gdb -iex "target extended-remote localhost:3333" OPENOCD_TEST.elf
  3. GNU gdb (GNU Tools for ARM Embedded Processors) 7.10.1.20151217-cvs
  4. Copyright (C) 2015 Free Software Foundation, Inc.
  5. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  6. This is free software: you are free to change and redistribute it.
  7. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  8. and "show warranty" for details.
  9. This GDB was configured as "--host=x86_64-apple-darwin10 --target=arm-none-eabi".
  10. Type "show configuration" for configuration details.
  11. For bug reporting instructions, please see:
  12. <http://www.gnu.org/software/gdb/bugs/>.
  13. Find the GDB manual and other documentation resources online at:
  14. <http://www.gnu.org/software/gdb/documentation/>.
  15. For help, type "help".
  16. Type "apropos word" to search for commands related to "word"...
  17. Remote debugging using localhost:3333
  18. 0x00000168 in ?? ()
  19. Reading symbols from OPENOCD_TEST.elf...done.
  20. (gdb) load
  21. Loading section .text, size 0x488 lma 0x0
  22. Loading section .relocate, size 0x428 lma 0x488
  23. Start address 0x0, load size 2224
  24. Transfer rate: 2 KB/sec, 1112 bytes/write.
  25. (gdb) monitor reset halt
  26. target halted due to debug-request, current mode: Thread
  27. xPSR: 0x81000000 pc: 0x00000270 msp: 0x20001418
  28. (gdb) c
  29. Continuing.
复制代码

注意以下的gdb命令:

●    load将代码上传到SAMD21

●    monitor reset halt复位SAMD21

●    c开始运行代码(c是contiue的简写)


此时你的LED应该闪烁!

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 16 | 回复: 21



手机版|

GMT+8, 2024-3-29 03:41 , Processed in 0.096580 second(s), 7 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表