实验步骤:
1. 上电后,在 u-boot 中将修改后重新编译的内核通过网口以 tftp 方式下载到 SDRAM 的地
址 0x30008000 处并启动 Linux 内核。可以用命令
run loadlinux
2. 进入 Linux 的 shell 界面后,输入如下命令:
mount 192.168.1.99:/tftpboot/examples /mnt/nfs
cd /mnt/nfs
insmod stepper.o
mknod /dev/steppertest c 96 0
./steppertest start 步进电机开始转
./steppertest reverse 步进电机反转
./steppertest down 步进电机减速
./steppertest up 步进电机加速
./steppertest stop 步进电机停止
代码说明:
1. 因为步进电机用到了 I/O 端口,而在 ARM9 中操作端口要用虚拟地址而非实际的物理地
址,所以要修改内核代码。
打开文件 linux/arch/arm/mach-s3c2410/smdk.c,在结构体
static struct map_desc smdk_io_desc[] __initdata = {
/* virtual physical length domain r w c b */
{ vCS8900_BASE, pCS8900_BASE, 0x00100000, DOMAIN_IO, 0, 1, 0, 0 },
{ vCF_MEM_BASE, pCF_MEM_BASE, 0x01000000, DOMAIN_IO, 0, 1, 0, 0 },
{ vCF_IO_BASE, pCF_IO_BASE, 0x01000000, DOMAIN_IO, 0, 1, 0, 0 },
LAST_DESC
};
中添加一行数组元素{ 0xd3000000, 0x28000000, 0x01000000, DOMAIN_IO, 0, 1, 0, 0 },
则步进电机的物理地址 0x28000006 对应的虚拟地址为 0xd3000006,在驱动程序中应对
这个地址进行操作。
2. 然后重新编译内核,make zImage,并将映像文件拷贝到/tftpboot 目录中。
3. 写步进电机的设备驱动程序 stepper.c:
定义全局变量 num 和 status 用来控制步进电机的速度和方向:
static int num=1; /*used to control the speed of the stepper*/
static enum{off,clockwise,anticlockwise} status=off; /*used to indicate and set the
status of the stepper*/
定义步进电机的整步模式正转脉冲表:
unsigned char pulse_table[] =
{
0x05, 0x09, 0x0a, 0x06,
};