pcDuino裸板程序-led

最近调驱动时,调试led时遇到了点问题,于是回过头来再写个led裸板程序。在我写的pcDuino第一个裸板程序uart的基础上,再写个led裸板程序还是很轻松的。很多人觉得没有必要写什么pcDuino裸板程序,觉得没啥意义。我觉得可以用来熟悉硬件,特别是想做底层驱动开发,以及系统移植,熟悉底层硬件还是有用的。其实做底层驱动开发,也是跟硬件打交道,硬件相关的操作和裸板程序是一样的。下面介绍怎样在pcDuino上跑一个最简单的led裸板程序。

开发环境:
系统:ubuntu 10.04.4
单板:pcDuino
编译器:arm-2009q3-67-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2

目标:实现pcDuino上的TX_LED闪烁

一、硬件介绍

仔细看pcDuino上的原理图和pcDuino的手册,发现二者不是完全对应的,还是以原理图为准。根据原理图知道TX_LED是接到PH15上,可以当做普通IO口用,不需要连跳线

二、编写源代码

主要是看手册30.Port Controller,根据手册写led初始化程序主要包括设为输出、是能上拉及Multi-Driving寄存器设置。包括start.S、main.c、clock.c、clock.h、Makefile,下面贴出全部代码

文件start.S:

  1. <p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">[plain] view plaincopyprint?.global _start</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">_start:
  2. ldr sp, =0x00007f00</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">b main</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">.global _start</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">_start:
  3. ldr sp, =0x00007f00</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">b main</p>
复制代码

文件main.c:

  1. <p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">[plain] view plaincopyprint?#include “clock.h”</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">#define PH_CFG1 (*(volatile unsigned int *)0x01c20900)
  2. #define PH_DAT (*(volatile unsigned int *)0x01c2090C)
  3. #define PH_DRI (*(volatile unsigned int *)0x01c20910)
  4. #define PH_PULL (*(volatile unsigned int *)0x01c20918)
  5. void gpio_init()
  6. {
  7. /*PCDUINO GPIO4–PH9:
  8. *bit[6:4]:PH9_SELECT 001:OUTPUT
  9. *PCDUINO GPIO5–PH10:
  10. *bit[10:8]:PH10_SELECT 001:OUTPUT
  11. */
  12. PH_CFG1 |= ((0×1<<4)|(0×1<<8)|(0X1<<28));
  13. PH_DRI = 0XFFFFFFFF;
  14. PH_PULL = 0X55555555;
  15. }</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">void delay()
  16. {
  17. volatile int i = 0×300000;
  18. while (i–);
  19. }
  20. int main(void)
  21. {
  22. char c;
  23. clock_init(); /* 初始化时钟 */
  24. gpio_init();</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">while (1)
  25. {
  26. PH_DAT = 0×00;
  27. delay();
  28. PH_DAT = 0xffff;
  29. delay();
  30. }</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">return 0;
  31. }</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">#include "clock.h"</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">#define PH_CFG1 (*(volatile unsigned int *)0x01c20900)
  32. #define PH_DAT (*(volatile unsigned int *)0x01c2090C)
  33. #define PH_DRI (*(volatile unsigned int *)0x01c20910)
  34. #define PH_PULL (*(volatile unsigned int *)0x01c20918)
  35. void gpio_init()
  36. {
  37. /*PCDUINO GPIO4–PH9:
  38. *bit[6:4]:PH9_SELECT 001:OUTPUT
  39. *PCDUINO GPIO5–PH10:
  40. *bit[10:8]:PH10_SELECT 001:OUTPUT
  41. */
  42. PH_CFG1 |= ((0×1<<4)|(0×1<<8)|(0X1<<28));
  43. PH_DRI = 0XFFFFFFFF;
  44. PH_PULL = 0X55555555;
  45. }</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">void delay()
  46. {
  47. volatile int i = 0×300000;
  48. while (i–);
  49. }
  50. int main(void)
  51. {
  52. char c;
  53. clock_init(); /* 初始化时钟 */
  54. gpio_init();</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">while (1)
  55. {
  56. PH_DAT = 0×00;
  57. delay();
  58. PH_DAT = 0xffff;
  59. delay();
  60. }</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">return 0;
  61. }
  62. 文件clock.c:</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">[plain] view plaincopyprint?#define CPU_AHB_APB0_CFG (*(volatile unsigned int *)0x01c20054)
  63. #define PLL1_CFG (*(volatile unsigned int *)0x01c20000)
  64. #define APB1_CLK_DIV_CFG (*(volatile unsigned int *)0x01c20058)
  65. #define APB1_GATE (*(volatile unsigned int *)0x01c2006C)</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">void sdelay(unsigned long loops)
  66. {
  67. __asm__ volatile("1:\n" "subs %0, %1, #1\n"
  68. "bne 1b":"=r" (loops):"0"(loops));
  69. }
  70. void clock_init(void)
  71. {
  72. /*AXI_DIV_1[1:0] AXI_CLK_DIV_RATIO 00:/1 AXI Clock source is CPU clock
  73. *AHB_DIV_2[5:4] AHP_CLK_DIV_RATIO 01:/2 AHB Clock source is AXI CLOCK
  74. *APB0_DIV_1[9:8] APB0_CLK_RATIO 00:/2 APB0 clock source is AHB2 clock
  75. *CPU_CLK_SRC_OSC24M[17:16] CPU_CLK_SRC_SEL 01:OSC24M
  76. */
  77. CPU_AHB_APB0_CFG = ((0<<0)|(0×1<<4)|(0<<8)|(1<<16));</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">/*bit31:PLL1_Enable 1:Enable
  78. *bit25:EXG_MODE 0×0:Exchange mode
  79. *bit[17:16]:PLL1_OUT_EXT_DIVP 0×0:P=1
  80. *bit[12:8]:PLL1_FACTOR_N 0×10:Factor=16,N=16
  81. *bit[5:4]:PLL1_FACTOR_K 0×0:K=1
  82. *bit3:SIG_DELT_PAT_IN 0×0
  83. *bit2:SIG_DELT_PAT_EN 0×0
  84. *bit[1:0]PLL1_FACTOR_M 0×0:M=1
  85. *The PLL1 output=(24M*N*K)/(M*P)=(24M*16*1)/(1*1)=384M is for the coreclk
  86. */
  87. PLL1_CFG = 0xa1005000;</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">sdelay(200);</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">CPU_AHB_APB0_CFG = ((0<<0)|(0×1<<4)|(0<<8)|(2<<16));//CPU_CLK_SRC_SEL 10:PLL1</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">/*uart clock source is apb1,config apb1 clock*/
  88. /*bit[25:24]:APB1_CLK_SRC_SEL 00:OSC24M
  89. *bit[17:16]:CLK_RAT_N 0X0:1 The select clock source is pre-divided by 2^1
  90. *bit[4:0]:CLK_RAT_M 0×0:1 The pre-devided clock is divided by(m+1)
  91. */
  92. APB1_CLK_DIV_CFG = ((0<<5)|(0<<16)|(0<<24));
  93. /*open the clock for uart0*/
  94. /*bit16:UART0_APB_GATING 1:pass 0:mask*/
  95. APB1_GATE = (0×1<<16);
  96. }</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">#define CPU_AHB_APB0_CFG (*(volatile unsigned int *)0x01c20054)
  97. #define PLL1_CFG (*(volatile unsigned int *)0x01c20000)
  98. #define APB1_CLK_DIV_CFG (*(volatile unsigned int *)0x01c20058)
  99. #define APB1_GATE (*(volatile unsigned int *)0x01c2006C)</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">void sdelay(unsigned long loops)
  100. {
  101. __asm__ volatile("1:\n" "subs %0, %1, #1\n"
  102. "bne 1b":"=r" (loops):"0"(loops));
  103. }
  104. void clock_init(void)
  105. {
  106. /*AXI_DIV_1[1:0] AXI_CLK_DIV_RATIO 00:/1 AXI Clock source is CPU clock
  107. *AHB_DIV_2[5:4] AHP_CLK_DIV_RATIO 01:/2 AHB Clock source is AXI CLOCK
  108. *APB0_DIV_1[9:8] APB0_CLK_RATIO 00:/2 APB0 clock source is AHB2 clock
  109. *CPU_CLK_SRC_OSC24M[17:16] CPU_CLK_SRC_SEL 01:OSC24M
  110. */
  111. CPU_AHB_APB0_CFG = ((0<<0)|(0×1<<4)|(0<<8)|(1<<16));</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">/*bit31:PLL1_Enable 1:Enable
  112. *bit25:EXG_MODE 0×0:Exchange mode
  113. *bit[17:16]:PLL1_OUT_EXT_DIVP 0×0:P=1
  114. *bit[12:8]:PLL1_FACTOR_N 0×10:Factor=16,N=16
  115. *bit[5:4]:PLL1_FACTOR_K 0×0:K=1
  116. *bit3:SIG_DELT_PAT_IN 0×0
  117. *bit2:SIG_DELT_PAT_EN 0×0
  118. *bit[1:0]PLL1_FACTOR_M 0×0:M=1
  119. *The PLL1 output=(24M*N*K)/(M*P)=(24M*16*1)/(1*1)=384M is for the coreclk
  120. */
  121. PLL1_CFG = 0xa1005000;</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">sdelay(200);</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">CPU_AHB_APB0_CFG = ((0<<0)|(0×1<<4)|(0<<8)|(2<<16));//CPU_CLK_SRC_SEL 10:PLL1</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">/*uart clock source is apb1,config apb1 clock*/
  122. /*bit[25:24]:APB1_CLK_SRC_SEL 00:OSC24M
  123. *bit[17:16]:CLK_RAT_N 0X0:1 The select clock source is pre-divided by 2^1
  124. *bit[4:0]:CLK_RAT_M 0×0:1 The pre-devided clock is divided by(m+1)
  125. */
  126. APB1_CLK_DIV_CFG = ((0<<5)|(0<<16)|(0<<24));
  127. /*open the clock for uart0*/
  128. /*bit16:UART0_APB_GATING 1:pass 0:mask*/
  129. APB1_GATE = (0×1<<16);
  130. }</p>
复制代码

文件·clock.h:

  1. [plain] view plaincopyprint?void clock_init(void);
复制代码

void clock_init(void);文件·Makefile:

  1. <p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">[plain] view plaincopyprint?led.bin:start.S main.c clock.c
  2. arm-none-linux-gnueabi-gcc -nostdlib -c start.S -o start.o
  3. arm-none-linux-gnueabi-gcc -nostdlib -c main.c -o main.o
  4. arm-none-linux-gnueabi-gcc -nostdlib -c clock.c -o clock.o
  5. arm-none-linux-gnueabi-ld -Ttext 0xD0020010 start.o main.o clock.o -o led_elf
  6. arm-none-linux-gnueabi-objcopy -O binary -S led_elf led.bin</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">clean:
  7. rm -rf *.o *.bin led_elf *.dis</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">led.bin:start.S main.c clock.c
  8. arm-none-linux-gnueabi-gcc -nostdlib -c start.S -o start.o
  9. arm-none-linux-gnueabi-gcc -nostdlib -c main.c -o main.o
  10. arm-none-linux-gnueabi-gcc -nostdlib -c clock.c -o clock.o
  11. arm-none-linux-gnueabi-ld -Ttext 0xD0020010 start.o main.o clock.o -o led_elf
  12. arm-none-linux-gnueabi-objcopy -O binary -S led_elf led.bin</p><p style="margin-bottom: 1.714285714rem; border: 0px; vertical-align: baseline; ">clean:
  13. rm -rf *.o *.bin led_elf *.dis</p>
复制代码

代码确实很简单,上面也有看手册时留下的注释,就不分析了,有问题留言吧。

三、编译、测试

1.安装交叉编译链,给个链接 http://code.google.com/p/smp-on-qemu/downloads/list

选择arm-2009q3-67-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2并下载。然后在ubuntu下直接解压即可,过程就不说了,还不清楚的看Ubuntu 10.04.4开发环境配置。

2.编译

  1. change@change :~$ cd Si/A10/2_led/
  2. change@change :~/Si/A10/2_led$ ls
  3. clock.c clock.h main.c Makefile mksunxiboot start.S
  4. change@change :~/Si/A10/2_led$ make
  5. arm-none-linux-gnueabi-gcc -nostdlib -c start.S -o start.o
  6. arm-none-linux-gnueabi-gcc -nostdlib -c main.c -o main.o
  7. arm-none-linux-gnueabi-gcc -nostdlib -c clock.c -o clock.o
  8. arm-none-linux-gnueabi-ld -Ttext 0xD0020010 start.o main.o clock.o -o led_elf
  9. arm-none-linux-gnueabi-objcopy -O binary -S led_elf led.bin
  10. change@change :~/Si/A10/2_led$ ./mksunxiboot led.bin leds.bin
  11. File size: 0×154
  12. Load size: 0×154
  13. Read 0×154 bytes
  14. Write 0×200 bytes
  15. change@change :~/Si/A10/2_led$
复制代码
其中有个./mksunxiboot led.bin leds.bin要注意,不经过mksunxiboot工具 的.bin文件,pcDuino是运行不了的。这个工具在官网上都有下。现在的处理启动都很复杂,内有固化有bl0代码,在跳转到bl1时需要校验程序的合法性,这个工具mksunxiboot简单点少就是给我们程序加了点头部,让处理器能够识别我们写的代码。你可以分析led.bin和leds.bin的反汇编代码,就一目了然了。这部分感兴趣的可以一起讨论。

3.测试

上面生成的leds.bin就可以放到板子上运行了。为了不破会NAND中的系统,直接放到tf卡运行。不用担心那个先启动,看全志手册就知道pcDuino默认先从tf卡启动,只有tf卡没有启动的引导程序才会跳到NAND启动。插上tf卡到PC机

  1. change@change:~/Si/A10/2_led$ sudo dd if=/dev/zero of=/dev/sdb bs=1M count=1
  2. 1+0 records in
  3. 1+0 records out
  4. 1048576 bytes (1.0 MB) copied, 0.425886 s, 2.5 MB/s
  5. change@change:~/Si/A10/2_led$ sudo dd if=leds.bin of=/dev/sdb bs=1024 seek=8
  6. 0+1 records in
  7. 0+1 records out
  8. 512 bytes (512 B) copied, 0.00600667 s, 85.2 kB/s
  9. change@change:~/Si/A10/2_led$
复制代码

然后取下tf卡,插到pcDuino上,RX LED就开始闪烁了。如果你手上有led,接到GPIO4、GPIO5也会闪烁。


程序

你可能感兴趣的:(pcDuino裸板程序-led)