开启Beaglebone的PWM和SPI

转自:http://tuzhaoliang.blog.163.com/blog/static/2100811262012823112954898/


一、开启PWM


1、需要更新内核版本,在 https://github.com/Hexxeh/beaglebone-kernel/tree/linux-ti33x-psp-3.2.21-r13d+gitr720e07b4c1f687b61b147b31c698cb6816d72f01这里下载最新kennel源码包

2、必须用beaglebone的光盘资料里的readme里面的步骤把下载的beaglebone的源码包先预编译一遍,步骤如下
--------------------------------------------------------------------------------
 
 [Kernel compile guide]:
 
 * Before compiling the kernel, please install the original ti-sdk-am335x (download URL http://software-dl.ti.com/dsps/dsps_public_sw/am_bu/sdk/AM335xSDK/latest/index_FDS.html).
   We assume that you install it in the default location(/usr/local/ti-sdk-am335x-evm/).
 
 * Compile steps:
 
   cd source
   tar zxvf kernel-chipsee-05.04.01.00.tar.gz
   cd kernel-chipsee-05.04.01.00
 
   sudo apt-get install uboot-mkimage
 
   export PATH=$PATH:/usr/local/ti-sdk-am335x-evm/linux-devkit/bin
   export CROSS_COMPILE=arm-arago-linux-gnueabi-
   export ARCH=arm
 
   make distclean
   make am335x_evm_defconfig
   make uImage
 
   Compiled uImage is located in arch/arm/boot/uImage.
 
 --------------------------------------------------------------------------------

3、再次使用自己的编译方法编译就行,如下:
~$su 先获得root权限才能开始编译这个下载的新内核
~#export PATH=$PATH:/usr/local/ti-sdk-am335x-evm/linux-devkit/bin    这个是先设置环境变量,就是指向TI-SDK的工具
~#make ARCH=arm CROSS_COMPILE=arm-arago-linux-gnueabi- menuconfig    这个是配置内核,要选上PWM下的东西
~#make ARCH=arm CROSS_COMPILE=arm-arago-linux-gnueabi- uImage    这步就把内核编译出来了,在arch/arm/boot下,把这个拷贝到SD卡里面就行

4、启动beaglebone开发板,PWM已经可以在/sys/class/pwm里面了,可以正常使用了,方法如下:
Controlling the PWM outputs can be done in user space, and is done the same way for both EHRPWM outputs and eCAP PWM outputs:
  1. cd /sys/class/pwm/ehrpwm.1\:0
  2. cat request
  3. echo 1 > request
  4. echo 1 > run
  5. echo 100 > period_freq
  6. echo 10 > duty_percent
Wien you are done, release the port:
  1. echo 0 > request

Note that the paired EHRPWMs share the frequency setting. I.e. ehrpwm.1:0 and ehrpwm.1:1 always have the same frequency.

For one servo I have, I've measured the following min/max duty cycles for the given period frequency: 
  • Period_freq 100, duty_percent: 5-22
  • Period_freq 90, duty_percent: 4-20
  • Period_freq 80, duty_percent: 4-18
  • Period_freq 70, duty_percent: 3-16
  • Period_freq 60, duty_percent: 3-13
  • Period_freq 50, duty_percent: 2-11



二、 开启SPI

The newest Angstrom release now has SPIDEV enabled by default! The information below is still useful if you want to enable the second SPIDEV bus, but it will need some slight modification.

After lots of searching on how to get SPI working on the BeagleBone I decided to post up a very brief tutorial on how to enable SPI on the BeagleBone and access it through the userspace driver SPIDEV. My searches usually proved fruitless hence the reason for putting this quick guide here.


This guide assumes that you have access to the kernel sources and are able to re-compile the beaglebone kernel and boot it on the board. If there is interest I will post a guide on how to do this also in the future, let me know in the comments!

Step 1) Add the following struct to the kernel source file arch/arm/mach-omap2/board-am335xevm.c

static struct spi_board_info bone_spi0_info[] = {
{
.modalias = "spidev",
.max_speed_hz = 48000000, //48 Mbps
.bus_num = 1,
.chip_select = 0,
.mode = SPI_MODE_1,
},
};



Step 2)
 Change the spi0_init function in the kernel source file arch/arm/mach-omap2/board-am335xevm.c so it reads the same as the following:

static void spi0_init(int evm_id, int profile)
{
setup_pin_mux(spi0_pin_mux);
spi_register_board_info(bone_spi0_info,
ARRAY_SIZE(bone_spi0_info));
return;
}



Step 3)
 Add the following to the beaglebone_dev_cfg[] struct:

{spi0_init, DEV_ON_BASEBOARD, PROFILE_NONE},//经过测试,此处要加在这个结构体最后一个,加在前面系统启动不了



Step 4)
 Ensure the kenerl config has SPIDEV enabled either as a module or compiled in.

 
 



Step 5)
 Recompile the kernel and boot the BeagleBone with the new kernel.

Step 6) Check that the SPIDEV module has loaded and initialised the hardware.

root@beaglebone:~# ls /dev/spidev*

/dev/spidev1.0



Step 7)
 Cross compile the following program:

http://lxr.linux.no/#linux+v3.2.6/Documentation/spi/spidev_test.c

 

documention目录下有spidev_test.c,下一步就是gcc一下(arm-arago-linux-gnueabi-gcc spidev_test.c –o spidevtest),会生成一个spidev_test的可执行文件,cp到sd卡的随便一个文件夹下。



Step 8)
 Connect pins 18 and 21 together on the P9 header.

 

Step 9) Copy your spidev_test program onto the board and run as follows:

./spidev_test -D /dev/spidev1.0



If all is working as it should then you will be presented with the following output:


spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

FF FF FF FF FF FF
40 00 00 00 00 95
FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF FF FF FF FF
DE AD BE EF BA AD
F0 0D

 

So, thats it. SPI working through the userspace SPIDEV driver on the BeagleBone.

Go forth and play.

你可能感兴趣的:(AM335x,Linux)