1) 获取系统的镜像,下载地址为
http://down.myir-tech.com/MYS-6ULX/ ,需要下载的文件是mys6ull-debian8.rootfs.sdcard。下载文件大小为1.5G左右。
2) 准备一张SD卡,我用的是8G卡,class6级别。下载完成之后得到一个.sdcard后缀的文件,使用Win32Disk Imager可以打开,然后烧写到SD卡里面。
3) 上电启动
开发板。将SD卡放入卡槽,拨码开关设置为0101。连接串口线,USB供电线,即可启动开发板。本次在windows10下用putty连接开发板
4) 启动之后登陆,用户名root,密码为123456,进入系统
5)输入:mkdir gpio_led 建立一个新文件夹
6)输入:vi gpio_led.c ,编写C语言程序,从驱动层控制led灯不断闪烁
#include
#include
#include
#include
#include
#include
//#define DEBUG 1
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define LED_DELAY_US (200*1000) /* 200 ms */
#ifdef DEBUG
#define dbg_info(fmt, args...) printf(fmt, #args)
#else
#define dbg_info(fmt, args...)
#endif
#define dbg_err(fmt, args...) printf(fmt, #args)
typedef struct gpio_s {
int gpio;
char value_path[64];
char dir_path[64];
int value;
}gpio_t;
void leds_ctrl(const char **leds, int count, unsigned int status)
{
int i = 0;
char cmd[128] = {0};
static unsigned int pre_status = 0;/* It was on by default */
for (i = 0; i < count; i++, leds++) {
if ((pre_status ^ status) & (1 << i)) {
sprintf(cmd, "echo %d > %s", !!(status&(1 << i)), *leds);
if (system(cmd)) {
dbg_info("run cmd [%s] failed!n", cmd);
}
}
}
pre_status = status;
}
void leds_play(const char **leds, int count)
{
unsigned int status = 1;
unsigned int mask;
if (count > 32) {
dbg_err("too many leds!(%d, expected <= 32)", count);
return;
}
mask = (0x1 << count) - 1;
for (;;) {
leds_ctrl(leds, count, status);
usleep(LED_DELAY_US);
status = (status << 1) | ((status >> (count -1))&0x1);
}
}
int main (int argc, char *argv[])
{
int keys_fd;
char ret[2];
struct input_event t[2];
fd_set fds;
struct timeval tv;
const char* leds[]={argv[1],argv[2]};
keys_fd = open (argv[1], O_RDONLY);
if (keys_fd <= 0) {
dbg_err ("open %s device error!n", argv[1]);
return 0;
}
/* Set all LEDs to ON to info user for key pressing */
leds_ctrl(leds, ARRAY_SIZE(leds), 0xF);
usleep(LED_DELAY_US);
leds_ctrl(leds, ARRAY_SIZE(leds), 0);
leds_play(leds, ARRAY_SIZE(leds));
return 0;
}
保存退出vi
7)输入:arm-linux-gnueablihf-gcc -o gpio_led gpio_led.c 直接在开发板上编译代码生成可执行文件
8)输入: ./gpio_led /sys/class/leds/user/brightness 直接控制LED灯
虽然有错误,但是led灯还是按照程序设计那样不断的闪烁
按Ctr+C停止
.......enjoy..........