Lab7 : 字符设备驱动程序 - 东西都在攻略里有啊

前言

本实验使用MAX7219驱动8x8点阵。上位机使用Ubuntu 14.04,下位机使用Raspberry pi 2。

本次实验又有攻略带飞,简直爽。

使用MAX7219点阵模块

使用MAX7219点阵模块除了需要提供GND以及VCC外,只需要再提供三根引脚即可点亮矩阵。其中,DIN引脚输入数据,CS(LOAD)引脚控制数据输入,CLK引脚用于区分每个bit。

时序图 from [max7219芯片4片级联C51程序与电路](http://wenku.baidu.com/link?url=yvGwKCMgCbCUsy28GbHc4D3u0n7mLDzwdrqFi0i44VsqAFrN19tJ3xFiTwodmxo_B6g90OsbeQBLXwlIuGb_GgpxAn0svQuyCuMjYCLr9S_)

整个写入流程为,首先CS引脚置0,表示允许写入。而后从高位顺序写入16个bit。每个bit的写入方式为首先DIN置为要写入的bit值,而后CLK产生一个下降沿(图中为上升沿,不知道为何有差别)即被读入。最后CS引脚置1表示写入结束。

而除了数据传输流程之外,接下来是如何在点阵上显示图案了。

在运行之前,需要进行一次初始化,其行为是向某几个特定的地址写入特定的值。至少需要写入两个地址,第一个是0x0b,写入0x07表示扫描显示所有行。第二个是0x0c,写入1表示进入工作模式。

而后点阵上每一行都有其地址,如第一行是0x01到第八行是0x08,每次向固定行的地址写入一个8位二进制数即可在指定行上显示图案。

据此写成程序即可。

树莓派使用引脚1, 3, 5, 7, 9,其中1为VCC,9为GND。而3, 5, 7分别是GPIO_Pin2, GPIO_Pin3, GPIO_Pin4。3接DIN,5接CS,7接CLK。

由于上一个实验编译内核给我造成了巨大的阴影,我决定这次实验不编译Arduino-ish库,而是直接使用Linux自带的GPIO子系统,通过操作文件的方式控制GPIO。而文件这种东西写起来最方便的就是linux自带的shell了。

#!/bin/bash

# DIN, CS, CLK的GPIO口位置
DIN=4
CS=3
CLK=2

# 一些文件路径
GPIO_BASE=/sys/class/gpio
GPIO_EXPORT=${GPIO_BASE}/export
GPIO_UNEXPORT=${GPIO_BASE}/unexport

BIN=(00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000)

# 生成指定GPIO引脚的文件夹位置
function GPIO(){
    echo ${GPIO_BASE}/gpio$1
}

# 将某个引脚export到用户态
function GPIO_export(){
    if [ -d `GPIO $1` ]; then
        echo GPIO pin $1 found in folder.
    else
        echo $1 > ${GPIO_EXPORT}
    fi
}

# unexport某个引脚
function GPIO_unexport(){
    if [ -d `GPIO $1` ]; then
        echo $1 > ${GPIO_UNEXPORT}
    else
        echo GPIO pin $1 not found.
    fi
}

# 改变某个引脚的方向
function GPIO_direction(){
    echo $2 > `GPIO $1`/direction
}

# 改变某个引脚的值
function GPIO_set(){
    echo $2 > `GPIO $1`/value
}

# 改变DIN的值
function GPIO_DIN(){
    GPIO_set $DIN $1
}

# 改变CS的值
function GPIO_CS(){
    GPIO_set $CS $1
}

# 改变CLK的值
function GPIO_CLK(){
    GPIO_set $CLK $1
}

# 向MAX7219发送一个byte的值
function Matrix_send_char(){
    local i=1
    for ((i=1;i<=8;i++)); do
        chr=`expr substr $1 $i 1`
        GPIO_DIN $chr
        GPIO_CLK 1
        GPIO_CLK 0
    done
}

# 向MAX7219发送一次完整的信号
function Matrix_send_word(){
    GPIO_CS 1
    GPIO_CS 0
    GPIO_CLK 0
    Matrix_send_char $1
    Matrix_send_char $2
    GPIO_CS 1
}

# 初始化GPIO引脚
function GPIO_init(){
    GPIO_export $DIN
    GPIO_export $CS
    GPIO_export $CLK

    sleep 2

    GPIO_direction $DIN out
    GPIO_direction $CS out
    GPIO_direction $CLK out
}

# 清除GPIO引脚
function GPIO_clear(){
    GPIO_unexport $DIN
    GPIO_unexport $CS
    GPIO_unexport $CLK
}

# 在点阵上显示数据
function Matrix_render(){
    local i=1
    for ((i=0;i<8;i++)); do
        echo $i $1
        Matrix_send_word ${BIN[$i]} $1
        shift
    done
}

# 使用文件中的数据进行显示
function Matrix_render_file(){
    local tmp=(`cat $1`)
    Matrix_render "${tmp[@]}"
}

# 使用某个图案清屏
function Matrix_clear(){
    local LM=(
        10000000
        10000000
        10000000
        10010001
        10011011
        11110101
        00010001
        00010001
    )
    Matrix_render "${LM[@]}"
}

# 初始化点阵
function Matrix_init(){
    # 编码模式
    Matrix_send_word 00001001 00000000
    # 亮度
    Matrix_send_word 00001010 00000011
    # 扫描数码管个数
    Matrix_send_word 00001011 00000111
    # 工作模式
    Matrix_send_word 00001100 00000001

    # 初始化完毕后清屏显示默认图案
    Matrix_clear
}

使用方式为

source matrix.sh
GPIO_init
Matrix_init
清屏时显示的图案

而后,使用Matrix_render_file可以将某个文件内的数据显示到矩阵上。

pi@raspberrypi ~/src $ cat journey 
00000000
01111010
00101100
00101110
01111010
00001110
01011010
00000000
pi@raspberrypi ~/src $ Matrix_render_file journey 
0 00000000
1 01111010
2 00101100
3 00101110
4 01111010
5 00001110
6 01011010
7 00000000
使用journey文件中的数据显示的矩阵

当然,使用上面那个shell程序的弊端在于交不了作业啊,翁老大要求的是c/c++的程序啊。

所以说,照着写一个c版本的就好了喽。

/*
* 这里原本有一些支持函数,用于将引脚操作映射为文件写入操作
* 但是这些函数在地震中消失了
*/
#define DIN 4
#define CS 3
#define CLK 2
GPIO_Pin din, cs, clk;

int write_byte(unsigned char b){
    unsigned char i, tmp;
    for (i=0; i<8; i++){
        tmp = (b & 0x80) > 0;
        b <<= 1;
        GPIO_Pin_Write(&din, tmp);
        GPIO_Pin_Write(&clk, 1);
        GPIO_Pin_Write(&clk, 0);
    }
}

int write_word(unsigned char addr, unsigned char num){
    GPIO_Pin_Write(&cs, 1);
    GPIO_Pin_Write(&cs, 0);
    GPIO_Pin_Write(&clk, 0);
    write_byte(addr);
    write_byte(num);
    GPIO_Pin_Write(&cs, 1);
}

int Matrix_init(){
    write_word(0x09, 0x00);
    write_word(0x0a, 0x03);
    write_word(0x0b, 0x07);
    write_word(0x0c, 0x01);
}

int Matrix_render(unsigned char* tmp){
    int i;
    for (i=0; i<8; i++){
        printf("%d %d\n", i, tmp[i]);
        write_word(i+1, tmp[i]);
    }
}

int Matrix_clear(){
    unsigned char LM[]={
        0x80, 0x80, 0x80, 0x91, 0x9b, 0xf5, 0x11, 0x11
    };
    Matrix_render(LM);
}

int main(){
    GPIO_Pin_export(&din, DIN, GPIO_DIRECTION_OUT);
    GPIO_Pin_export(&cs, CS, GPIO_DIRECTION_OUT);
    GPIO_Pin_export(&clk, CLK, GPIO_DIRECTION_OUT);

    Matrix_init();
    Matrix_clear();

    return 0;
}

字符设备驱动程序

编译内核模块相关的一些步骤参照Lab6。而本节关心的是如何使用内核模块折腾MAX7219。

该模块在加载到系统内部时,会申请在/dev目录下生成一个matrix文件。使用的时候直接向其输出想要在点阵上显示的字符即可。

而在这个过程中,写入的时候会触发模块中的matrix_write函数。

如果是阻塞式的写入,那么遍历一遍buffer数组,并将其中的每个字符调用Matrix_render显示出来即可,字符与字符之间可以使用mdelay进行毫秒级别的延迟操作。

而如果是非阻塞式的写入,则要使用生产者消费者的思想。写入操作触发的matrix_write是生产者,所做的只需要将当前需要显示的所有内容放置入显示队列内即可返回。而在另一面,有一个定时触发的消费者函数,该函数根据队列内部是否有未显示字符进行操作。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

MODULE_LICENSE("GPL");
MODULE_AUTHOR("lmzqwer2 ");
MODULE_DESCRIPTION("In-kernel MAX7219 support.");

#define DIN 4
#define CS 3
#define CLK 2

void write_byte(unsigned char b){
    unsigned char i, tmp;
    for (i=0; i<8; i++){
        tmp = (b & 0x80) > 0;
        b <<= 1;
        gpio_set_value(DIN, tmp);
        gpio_set_value(CLK, 1);
        gpio_set_value(CLK, 0);
    }
}

void write_word(unsigned char addr, unsigned char num){
    gpio_set_value(CLK, 0);
    gpio_set_value(CS, 1);
    gpio_set_value(CS, 0);
    write_byte(addr);
    write_byte(num);
    gpio_set_value(CS, 1);
}

void Matrix_render(unsigned char* tmp){
    int i;
    for (i=0; i<8; i++){
        write_word(i+1, tmp[i]);
    }
}

unsigned char digits[][8]={
    {0x1c, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x1c}, // 0
    {0x08, 0x18, 0x08, 0x08, 0x08, 0x08, 0x08, 0x1c}, // 1
    {0x1c, 0x22, 0x22, 0x04, 0x08, 0x10, 0x20, 0x3e}, // 2
    {0x1c, 0x22, 0x02, 0x0c, 0x02, 0x02, 0x22, 0x1c}, // 3
    {0x04, 0x0c, 0x14, 0x14, 0x24, 0x1e, 0x04, 0x04}, // 4
    {0x3e, 0x20, 0x20, 0x3c, 0x02, 0x02, 0x22, 0x1c}, // 5
    {0x1c, 0x22, 0x20, 0x3c, 0x22, 0x22, 0x22, 0x1c}, // 6
    {0x3e, 0x24, 0x04, 0x08, 0x08, 0x08, 0x08, 0x08}, // 7
    {0x1c, 0x22, 0x22, 0x1c, 0x22, 0x22, 0x22, 0x1c}, // 8
    {0x1c, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x22, 0x1c}, // 9
};

unsigned char LM[]={
    0x80, 0x80, 0x80, 0x91, 0x9b, 0xf5, 0x11, 0x11
};

void Matrix_clear(void){
    Matrix_render(LM);
}

void Matrix_init(void){
    write_word(0x09, 0x00);
    write_word(0x0a, 0x03);
    write_word(0x0b, 0x07);
    write_word(0x0c, 0x01);

    Matrix_clear();
}

#define BUFFERSIZE 128
#define DELAYTIME 1
unsigned char disp[BUFFERSIZE];
int head = 0, tail = 0;
static struct timer_list timer;

void Matrix_next_display(unsigned long);

void ptr_inc(int *ptr){
    *ptr = (*ptr + 1) % BUFFERSIZE;
}

static void timer_register(struct timer_list* ptimer){
    init_timer(ptimer);
    ptimer->data = DELAYTIME;
    ptimer->expires = jiffies + (DELAYTIME * HZ);
    ptimer->function = Matrix_next_display;
    add_timer(ptimer);
}

void disp_start(void){
    timer_register(&timer);
}

void Matrix_next_display(unsigned long data){
    if (head != tail){
        unsigned char *ptr = LM;
        unsigned char c = disp[head];
        if ('0' <= c && c <= '9'){
            ptr = digits[c - '0'];
        }
        Matrix_render(ptr);
        ptr_inc(&head);
        disp_start();
    }else{
        Matrix_clear();
    }
}

static int matrix_write(struct file *file, const char __user *buffer,
        size_t count, loff_t *ppos){
    int i;
    if (head == tail && count > 0){
        disp_start();
    }
    for (i=0; i

然后就是熟悉的编译,载入,运行。命令中使用echo命令触发matrix_write函数。

lmuser@LMubt [05:53:21] [~/tmp/miscmatric] 
% make && scp matrix.ko pi@.....
.....
pi@raspberrypi ~ $ sudo insmod matrix.ko
pi@raspberrypi ~ $ cd /dev
pi@raspberrypi /dev $ sudo chown pi:pi matrix 
pi@raspberrypi /dev $ echo 0123456789 > matrix
按顺序显示0-9

参考资料

  • fm.zju.edu.cn / Embedded System / MAX7219.zip
  • max7219芯片4片级联C51程序与电路
  • max7219数码管驱动程序
  • MAX7219
  • MAX7219芯片驱动8*8点阵
  • Linux内核驱动之GPIO子系统(一)GPIO的使用
  • 基于树莓派Raspberry: 字符设备内核驱动程序框架编写
  • Creating a Basic LED Driver for Raspberry Pi

你可能感兴趣的:(Lab7 : 字符设备驱动程序 - 东西都在攻略里有啊)