工物系《微控制器开发技术》
原站著录信息
摘要
实验报告一 helloworld程序的运行 一、 uCLinux开发环境 实验中的硬件开发环境包括PC机、目标板(ColdFire板)、HUB,软件开发环境是Linux下的GNU开发环境。目标板用于运行操作系统和系统应用软件。uCLinux操作系统内核的编译、应用程序的开发和调试是在主机上完成的,目标板和主机采用串口及以太网口建立连接关系。 二、 GNU make 在大型项目开发中,通常有几十到上百个源文件,如果每次均手工键入GCC命令进行编译会很不方便。因此,人们常利用make工具自动完成编译工作。Make是用于…
实验报告一 helloworld程序的运行
一、 uCLinux开发环境
实验中的硬件开发环境包括PC机、目标板(ColdFire板)、HUB,软件开发环境是Linux下的GNU开发环境。目标板用于运行操作系统和系统应用软件。uCLinux操作系统内核的编译、应用程序的开发和调试是在主机上完成的,目标板和主机采用串口及以太网口建立连接关系。
二、 GNU make
在大型项目开发中,通常有几十到上百个源文件,如果每次均手工键入GCC命令进行编译会很不方便。因此,人们常利用make工具自动完成编译工作。Make是用于自动编译、链接程序的实用工具。
在uCLinux目录下执行make,完成如下工作:
(一)编译内核:即编译linux目录,生成linux.bin。
(二)编译应用程序:先编译应用程序所用的libc库,即uCLinux/lib目录下内容;然后再根据user/Makefile中DIRSy所制定的要编译应用程序目录连标逐一进行编译,并将各个可执行文件复制到uCLinux/romfs/bin/目录表下,供打包烧写。
(三)生成二进制格式文件image.bin:它由两部分组成:内核(linux.bin)和文件系统(romfs.img),romfs.img是由uCLinux/romfs/目录打包生成的,它里面的文件目录结构就是最终用户在板子minicom下ls所看到的文件和目录。
三、 实验步骤
在PC机上运行“超级终端”程序,将目标板的局域网相关设置(如 server、client、ethaddr、netmask)改好;同时打开TFTPServ.exe程序,使得目标板和PC机通过串口连接起来。
PC机上用secureCRT5.0进入自己的uCLinux空间操作系统,先完成make操作,然后编写自己的helloworld程序,代码如下(修改代码直接用nano helloworld命令即可):
#include<stdlib.h>
#include<stdarg.h>
#include<fcntl.h>
#include <stdio.h>
static int testio_fd;
int main()
{
printf("HelloWorld!\n");
testio_fd=open("/dev/testio",O_SYNC | O_RDWR,0);
ioctl(testio_fd,30,0);
close(testio_fd);
return 0;
}
用命令m68k-elf-gcc -Wall -elf2flt -m5200 helloworld.c -lc -o helloworld 编译成可执行的helloworld程序,如果有错,会出现提示,则返回重新修改程序,直至编译通过为止。
在超级终端中键入如下命令
dBUG> dn /从PC主机上下载image.bin/
dBUG> go 20000 /设定到固定内存中运行程序/
/> cd /tmp /进入用户目录/
/var/tmp> ls /查看目录下已有的文件/
/var/tmp> ftp 192.168.1.200 /连接到ftp操作系统主机/
Connected to 192.168.1.200.
220 uClinux-mot.ip.tsinghua.edu.cn FTP server (Version wu-2.6.2(1) Sun Apr 17 09
:42:13 UTC 2005) ready.
Name (192.168.1.200): s28 /连到自己的那块空间/
331 Password required for s28.
Password:******
230 User s28 logged in.
ftp> get helloworld /下载文件/
ftp> quit /退出ftp空间/
/var/tmp> ls –l /查看文件是否下载下来/
-rw-r--r-- 1 0 0 20676 Nov 30 00:00 helloworld
/var/tmp> chmod 777 helloworld /修改访问权限/
/var/tmp> ls –l /修改属性后查看操作权限/
-rwxrwxrwx 1 0 0 20676 Nov 30 00:00 helloworld
/var/tmp> ./helloworld /运行程序/
注:/var目录中是专门存放那些不断在扩充和更改的目录。这个目录对每个系统是特定的,即不通过网络与其它计算机共享。
最后执行程序,得到
HelloWorld!
This is the test_io driver.Welcome all of you ! 哈哈!
这充分说明驱动被调动了,函数open起到了作用,同时helloworld程序运行成功,为进一步验证,我们查了驱动源程序的代码
uCLinux-dist/Linux-2.4.x/drivers/char/test_io.c:
static int test_io_open(struct inode *inode, struct file *filp)
{
unsigned long i;
if (test_io_dev.opened) {
printk(KERN_INFO "TEST_IO: the device is busy\n");
return -EBUSY;
}
//You can add some printk information here.
printk("This is the test_io driver.Welcome all of you ! 哈哈!\n");
MOD_INC_USE_COUNT;
test_io_dev.opened = 1;
return 0;
}
四、 实验小结
1、每次实验都要开TFTPServ.exe,这样才能保证目标板和PC机是连着的;
2、每次实验如改动驱动程序test_io.c,都需要重新make一下,从而生成了新的image.bin,每次需要下载到PC机上,再通过dn命令把它下载到目标板上;
3、对于目标板上一些地址,可通过驱动程序来写状态,应用程序直接调用即可。
实验报告二 目标板上信号灯的闪烁
实验一中我们已经跑通了目标板,并大致了解了在uCLinux操作系统中开发简单程序的步骤和原理,实验二中我们需要实现目标板上信号灯的闪烁。
我们首先从uCLinux-dist/user/testio/目录下下载应用程序testio.c:
#include<stdlib.h>
#include<stdarg.h>
#include<fcntl.h>
static int testio_fd;
main()
{
testio_fd=open("/dev/testio",O_SYNC | O_RDWR,0);
ioctl(testio_fd,30,0);
close(testio_fd);
}
由于要实现目标板上信号灯的闪烁,所以要给目标板上固定的端口写上程序,这里,我们需要研究并调用uCLinux-dist/Linux-2.4.x/drivers/char/目录下的驱动程序test_io.c,并作出适当修改,写上程序,以便应用程序调用。
在对test_io.c程序进行仔细分析后,我决定在应用程序testio.c中利用ioctl参数调用完成目标。于是在驱动程序中ioctl函数写下了这样的代码:
static int test_io_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case 0:
*(volatile unsigned short *)(0x40100012)=0xFFFF;
*(volatile unsigned short *)(0x40100002)=0xFFFF;
return 0x00;
case 1:
*(volatile unsigned short *)(0x40100012)=0xFFFF;
*(volatile unsigned short *)(0x40100002)=0x00FF;
return 0x01;
}
return -EINVAL;
}
以上程序中0x40100002地址中的高两位有效,且0表示灯亮,所以case 0情况表示灯全灭,case 1情况表示灯全亮。在应用程序中,交替执行0和1的情况就能实现灯的闪烁。但考虑到程序执行速度非常快,为使人眼能够分辨清楚,必须在执行0和1的情况之间插入延时程序,所以最后应用程序testio.c代码如
下:
#include<stdlib.h>
#include<stdarg.h>
#include<fcntl.h>
#include <stdio.h>
static int testio_fd;
void delay();
int main()
{
testio_fd=open("/dev/testio",O_SYNC | O_RDWR,0);
while(1)
{ ioctl(testio_fd,0,0);
delay();
ioctl(testio_fd,1,0);
delay();
}
close(testio_fd);
}
void delay()
{
unsigned long j=1;
while(j<=0xfffff)
j++;
}
编写好应用程序testio.c,如实验一,用m68k-elf-gcc -Wall -elf2flt -m5200 testio.c -lc -o testio编译好testio.c,形成可执行文件testio。
由于驱动程序test_io.c已经做了修改,需要把修改后的程序重新传回uCLinux-dist/user/testio/目录下,然后要在uCLinux操作系统下用make命令重新编译。得到的image.bin也要重新下载到PC机上,然后传到目标板上。
最后在超级终端中大致重复实验一中的步骤,只不过此时的应用程序是testio,而不是helloworld而已,就可以看到灯光的闪烁。
若是程序执行不通或现象不对,需要返回uCLinux操作系统目录下重新检测、调试程序。
实验报告三 中断的调用
这个实验学习中断的调用,将目标板上按键的动作写成一个“中断源”,然后在驱动程序中通过printk打印信息调试、证明中断调用成功。
于是我们又重新修改了驱动程序test_io.c,首先定义了中断源:
static int __init test_io_init(void)
{
int result;
unsigned long i;
printk(KERN_INFO "TEST_IO driver launched. By Tom Xue 2006.03.13 <[email protected]>.\n");
/*
result = register_chrdev(test_io_dev_minor, "testio",
&test_io_fops);
*/
result = misc_register(&test_io_miscdev);
result = request_irq(71,test_io_interrupt,SA_INTERRUPT,"test_io_interrupt",NULL);
*(volatile unsigned char *)(0x40130003)=0x80;
i=*(volatile unsigned long *)(0x40000C0C);
i=i & ~(0x80+0x01);
*(volatile unsigned long *)(0x40000C0C)=i;
return result;
}
然后我们再在驱动程序test_io.c中修改了interrupt的函数的内容,代码如下:
static void test_io_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
unsigned long j=1;
while(j<=0xffffff) {
j++; }
printk(KERN_INFO"INTERRUPT CALLS!\n");
}
这里我们仍旧采用实验二中应用程序testio.c进行调试。
在超级终端中重复实验二中步骤以后,可以看到
一、 uCLinux开发环境
实验中的硬件开发环境包括PC机、目标板(ColdFire板)、HUB,软件开发环境是Linux下的GNU开发环境。目标板用于运行操作系统和系统应用软件。uCLinux操作系统内核的编译、应用程序的开发和调试是在主机上完成的,目标板和主机采用串口及以太网口建立连接关系。
二、 GNU make
在大型项目开发中,通常有几十到上百个源文件,如果每次均手工键入GCC命令进行编译会很不方便。因此,人们常利用make工具自动完成编译工作。Make是用于自动编译、链接程序的实用工具。
在uCLinux目录下执行make,完成如下工作:
(一)编译内核:即编译linux目录,生成linux.bin。
(二)编译应用程序:先编译应用程序所用的libc库,即uCLinux/lib目录下内容;然后再根据user/Makefile中DIRSy所制定的要编译应用程序目录连标逐一进行编译,并将各个可执行文件复制到uCLinux/romfs/bin/目录表下,供打包烧写。
(三)生成二进制格式文件image.bin:它由两部分组成:内核(linux.bin)和文件系统(romfs.img),romfs.img是由uCLinux/romfs/目录打包生成的,它里面的文件目录结构就是最终用户在板子minicom下ls所看到的文件和目录。
三、 实验步骤
在PC机上运行“超级终端”程序,将目标板的局域网相关设置(如 server、client、ethaddr、netmask)改好;同时打开TFTPServ.exe程序,使得目标板和PC机通过串口连接起来。
PC机上用secureCRT5.0进入自己的uCLinux空间操作系统,先完成make操作,然后编写自己的helloworld程序,代码如下(修改代码直接用nano helloworld命令即可):
#include<stdlib.h>
#include<stdarg.h>
#include<fcntl.h>
#include <stdio.h>
static int testio_fd;
int main()
{
printf("HelloWorld!\n");
testio_fd=open("/dev/testio",O_SYNC | O_RDWR,0);
ioctl(testio_fd,30,0);
close(testio_fd);
return 0;
}
用命令m68k-elf-gcc -Wall -elf2flt -m5200 helloworld.c -lc -o helloworld 编译成可执行的helloworld程序,如果有错,会出现提示,则返回重新修改程序,直至编译通过为止。
在超级终端中键入如下命令
dBUG> dn /从PC主机上下载image.bin/
dBUG> go 20000 /设定到固定内存中运行程序/
/> cd /tmp /进入用户目录/
/var/tmp> ls /查看目录下已有的文件/
/var/tmp> ftp 192.168.1.200 /连接到ftp操作系统主机/
Connected to 192.168.1.200.
220 uClinux-mot.ip.tsinghua.edu.cn FTP server (Version wu-2.6.2(1) Sun Apr 17 09
:42:13 UTC 2005) ready.
Name (192.168.1.200): s28 /连到自己的那块空间/
331 Password required for s28.
Password:******
230 User s28 logged in.
ftp> get helloworld /下载文件/
ftp> quit /退出ftp空间/
/var/tmp> ls –l /查看文件是否下载下来/
-rw-r--r-- 1 0 0 20676 Nov 30 00:00 helloworld
/var/tmp> chmod 777 helloworld /修改访问权限/
/var/tmp> ls –l /修改属性后查看操作权限/
-rwxrwxrwx 1 0 0 20676 Nov 30 00:00 helloworld
/var/tmp> ./helloworld /运行程序/
注:/var目录中是专门存放那些不断在扩充和更改的目录。这个目录对每个系统是特定的,即不通过网络与其它计算机共享。
最后执行程序,得到
HelloWorld!
This is the test_io driver.Welcome all of you ! 哈哈!
这充分说明驱动被调动了,函数open起到了作用,同时helloworld程序运行成功,为进一步验证,我们查了驱动源程序的代码
uCLinux-dist/Linux-2.4.x/drivers/char/test_io.c:
static int test_io_open(struct inode *inode, struct file *filp)
{
unsigned long i;
if (test_io_dev.opened) {
printk(KERN_INFO "TEST_IO: the device is busy\n");
return -EBUSY;
}
//You can add some printk information here.
printk("This is the test_io driver.Welcome all of you ! 哈哈!\n");
MOD_INC_USE_COUNT;
test_io_dev.opened = 1;
return 0;
}
四、 实验小结
1、每次实验都要开TFTPServ.exe,这样才能保证目标板和PC机是连着的;
2、每次实验如改动驱动程序test_io.c,都需要重新make一下,从而生成了新的image.bin,每次需要下载到PC机上,再通过dn命令把它下载到目标板上;
3、对于目标板上一些地址,可通过驱动程序来写状态,应用程序直接调用即可。
实验报告二 目标板上信号灯的闪烁
实验一中我们已经跑通了目标板,并大致了解了在uCLinux操作系统中开发简单程序的步骤和原理,实验二中我们需要实现目标板上信号灯的闪烁。
我们首先从uCLinux-dist/user/testio/目录下下载应用程序testio.c:
#include<stdlib.h>
#include<stdarg.h>
#include<fcntl.h>
static int testio_fd;
main()
{
testio_fd=open("/dev/testio",O_SYNC | O_RDWR,0);
ioctl(testio_fd,30,0);
close(testio_fd);
}
由于要实现目标板上信号灯的闪烁,所以要给目标板上固定的端口写上程序,这里,我们需要研究并调用uCLinux-dist/Linux-2.4.x/drivers/char/目录下的驱动程序test_io.c,并作出适当修改,写上程序,以便应用程序调用。
在对test_io.c程序进行仔细分析后,我决定在应用程序testio.c中利用ioctl参数调用完成目标。于是在驱动程序中ioctl函数写下了这样的代码:
static int test_io_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case 0:
*(volatile unsigned short *)(0x40100012)=0xFFFF;
*(volatile unsigned short *)(0x40100002)=0xFFFF;
return 0x00;
case 1:
*(volatile unsigned short *)(0x40100012)=0xFFFF;
*(volatile unsigned short *)(0x40100002)=0x00FF;
return 0x01;
}
return -EINVAL;
}
以上程序中0x40100002地址中的高两位有效,且0表示灯亮,所以case 0情况表示灯全灭,case 1情况表示灯全亮。在应用程序中,交替执行0和1的情况就能实现灯的闪烁。但考虑到程序执行速度非常快,为使人眼能够分辨清楚,必须在执行0和1的情况之间插入延时程序,所以最后应用程序testio.c代码如
下:
#include<stdlib.h>
#include<stdarg.h>
#include<fcntl.h>
#include <stdio.h>
static int testio_fd;
void delay();
int main()
{
testio_fd=open("/dev/testio",O_SYNC | O_RDWR,0);
while(1)
{ ioctl(testio_fd,0,0);
delay();
ioctl(testio_fd,1,0);
delay();
}
close(testio_fd);
}
void delay()
{
unsigned long j=1;
while(j<=0xfffff)
j++;
}
编写好应用程序testio.c,如实验一,用m68k-elf-gcc -Wall -elf2flt -m5200 testio.c -lc -o testio编译好testio.c,形成可执行文件testio。
由于驱动程序test_io.c已经做了修改,需要把修改后的程序重新传回uCLinux-dist/user/testio/目录下,然后要在uCLinux操作系统下用make命令重新编译。得到的image.bin也要重新下载到PC机上,然后传到目标板上。
最后在超级终端中大致重复实验一中的步骤,只不过此时的应用程序是testio,而不是helloworld而已,就可以看到灯光的闪烁。
若是程序执行不通或现象不对,需要返回uCLinux操作系统目录下重新检测、调试程序。
实验报告三 中断的调用
这个实验学习中断的调用,将目标板上按键的动作写成一个“中断源”,然后在驱动程序中通过printk打印信息调试、证明中断调用成功。
于是我们又重新修改了驱动程序test_io.c,首先定义了中断源:
static int __init test_io_init(void)
{
int result;
unsigned long i;
printk(KERN_INFO "TEST_IO driver launched. By Tom Xue 2006.03.13 <[email protected]>.\n");
/*
result = register_chrdev(test_io_dev_minor, "testio",
&test_io_fops);
*/
result = misc_register(&test_io_miscdev);
result = request_irq(71,test_io_interrupt,SA_INTERRUPT,"test_io_interrupt",NULL);
*(volatile unsigned char *)(0x40130003)=0x80;
i=*(volatile unsigned long *)(0x40000C0C);
i=i & ~(0x80+0x01);
*(volatile unsigned long *)(0x40000C0C)=i;
return result;
}
然后我们再在驱动程序test_io.c中修改了interrupt的函数的内容,代码如下:
static void test_io_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
unsigned long j=1;
while(j<=0xffffff) {
j++; }
printk(KERN_INFO"INTERRUPT CALLS!\n");
}
这里我们仍旧采用实验二中应用程序testio.c进行调试。
在超级终端中重复实验二中步骤以后,可以看到
全文与附件
提交申请后,可在会员中心查看原件和附件的查找进度。
