|
|
我讲创龙在DSP中使用的例子移植到ARM中,如下是我的底层驱动程序:
6 V9 ?1 m" B# ]4 [) b6 u/*! c1 U. q5 y& O6 R2 J4 H
* Copyright (C) 2009 Texas Instruments Inc/ u$ Y% Y7 _4 W
*
$ j7 k+ \% E6 | D; w * This program is free software; you can redistribute it and/or modify
% F! }7 k9 S- v * it under the terms of the GNU General Public License as published by
7 [8 A N- A! K$ w1 [8 D8 k * the Free Software Foundation; either version 2 of the License, or9 K8 A/ J$ l, l9 }8 c
* (at your option)any later version.
0 D0 |& z# n6 ^, P9 l2 T! v* J *
' i7 `+ g1 M v( g; d# e * This program is distributed in the hope that it will be useful,
# m6 M7 L; j, `" r8 R4 o * but WITHOUT ANY WARRANTY; without even the implied warranty of8 U3 z/ I# {" v% u! k6 `8 W6 s
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the( W6 |! S: g) p/ I! V0 u2 a
* GNU General Public License for more details.7 ]8 _. r! E8 P9 z
*/ Q" M. C5 Q- X! H: a- x( H
* You should have received a copy of the GNU General Public License
3 [9 n; M% G# m3 y2 H* N5 [ * along with this program; if not, write to the Free Software
. q5 y2 T( w+ Z8 s * Foundati' q8 _8 t; x4 b+ \# X" F
*/1 Z: A7 n; J, t9 [. m
#include <linux/module.h>+ ?' S- ?, D4 g; F. ]
#include <linux/init.h>
; {& @, j) x' `6 ]9 W#include <linux/errno.h>) P" n4 `; W. {- x- K% N
#include <linux/types.h>
* m. O; W j5 j#include <linux/interrupt.h>
) c2 Q) a/ y% T" t# k: L#include <linux/io.h>& j2 ~# t8 v: x6 m: I' f
#include <linux/sysctl.h>
) [: `7 @* }0 e, ]. a7 {#include <linux/mm.h>9 W4 d1 p, K& b& G! `5 g/ y9 I
#include <linux/delay.h>
m4 v8 ~1 [" }- W#include<linux/kernel.h>
2 b9 ^8 c2 W4 y) P& e) l5 W7 m#include<linux/fs.h>
2 U/ V) d8 R# F: N* d: f#include<linux/ioctl.h>- d! M# L: \) _4 D& b. {6 B1 c
#include<linux/cdev.h>! N; ~7 Q. |0 T5 _
#include<linux/kdev_t.h>0 m; {" |1 J# w! y+ t& R2 V. n# R
#include<linux/gpio.h>' d5 I4 F) I/ j" F: V. W
#include <mach/hardware.h>
. R b( k* @6 X* x#include <mach/irqs.h>
1 g) U+ j2 e: B% D
) H6 ~. p5 Y0 @# c#include <asm/mach-types.h>
r# J. }/ F; A+ o0 A0 | I#include <asm/mach/arch.h>
+ G M' s' D+ e$ O7 W! n#include <mach/da8xx.h>
9 ]5 G7 t Y' O; m#define SYSCFG_BASE 0x01c14000
/ w( s, Z: {3 w+ T3 n: f, G$ [#define PINMUX1_OFFSET 0x124
% k @, v3 M* W#define PINMUX18_OFFSET 0x168
. f1 x. e5 h8 q4 g5 T6 q#define PINMUX19_OFFSET 0x16c
- I4 G. f# U# z. C: S4 D#define SPCR_ADDR 0x01D11008 //MCBSP1_SPCR: ~: d" P" {1 _9 O; |. K0 d% v
#define RCR_ADDR 0x01D1100C //MCBSP1_RCR
/ L3 c2 W# J( n0 \ `' j#define XCR_ADDR 0x01D11010 //MCBSP1_XCR
$ n) J. l- S: @, |( N2 I#define SRGR_ADDR 0x01D11014 //MCBSP1_SRGR: t$ J5 `& j: ?5 d; U6 i G
#define PCR_ADDR 0x01D11024 //MCBSP1_PCR
: {; p4 v* v7 `6 ~% [
! R& A( {( _6 W9 N3 N7 u#define DXR_ADDR 0x01D11004 //MCBSP1_DXR
( L8 t* d3 \! K4 A$ g$ H4 U#define DRR_ADDR 0x01D11000 //MCBSP1_DRR) T0 K1 N3 u) c3 f
//PSC
+ Q, h) t' y& { m. P7 h#define PTCMD_ADDR 0x01E27120 + Q3 ~7 m I; n: m" g
#define MDCTL15_ADDR 0x01E27A3C5 [1 y |' P; c1 P
#define PDCTL1_ADDR 0x01E27304. x2 z. ]$ Q7 f) V! e. h5 m
//GPIO8 direction2 e N+ \# |1 _6 Y$ C
#define GPIO8_DIRECT 0x01E260B0
3 ~- Z# O9 m; d& `#define GPIO8_OUT 0x01E260B4
L2 t G. O' ^! \; a3 p#define GPIO8_IN 0x01E260C0+ E( H& J2 o: u- h
6 v; N( X8 ~/ ?' F1 _
//#define MCBSP1_RINT 99
+ O3 b1 c3 ^4 O/ T. H//#define MCBSP1_XINT 100
9 k/ t7 J' D1 H6 Y; L: ^0 Ystatic int MCBSP_MAJOR=239;% o7 P5 E2 `6 S( Z2 V) _ e
static int MCBSP_MINOR=0;% \- f1 Y4 L/ o3 q5 \4 U
static int count =1;
- Y# w$ H. K1 Z! E- Y- h3 a% g1 ]: J" f
#define MCBSP_NAME "MCBSP-device"
" G: {1 Z. ]0 m; `) R! A9 l" O! L+ s) v4 P4 \; ?9 M) K; a
static struct cdev *mcbsp_cdev;! _8 |+ O. u: [& l* J8 q
static struct class *mcbsp_class;% z+ C) H3 e5 ^) `7 _. n/ G7 b7 y0 x; ^
static dev_t mcbsp_dev;7 S7 e! X, b9 e( n( _ F1 H
unsigned int DRR_data;
8 G: }# t/ L9 x0 D( t- P; | u1 Qunsigned int DXR_data;+ M2 u4 w: x- [! w# n) d: q
static int mcbsp_open(struct inode *inode,struct file *file)
2 G+ ]- \1 u/ z/ [# U7 \7 B' V0 {{+ J5 g! s9 E% P, V, c6 A9 h7 u2 F
7 g5 C9 [& l" A8 W: z //interrupt enable,initialized; ~/ K& M/ |$ C0 y5 e
unsigned int temp;' y- t8 G, g* l
//SLEEP_EN(GPIO8[10])---0
! E0 y' S1 b8 c) |1 b9 M; q. q. `" r temp = __raw_readl(IO_ADDRESS(GPIO8_OUT));$ z3 y! U( v1 I; k6 {
temp=temp&(~0x00000400);
# @% |2 [$ V! [8 e; ^& Q __raw_writel(temp,IO_ADDRESS(GPIO8_OUT));//GPIO8[10]
2 b" ~3 G/ ?1 h //RESETn(GPIO8[8])----0----13 i$ z7 M% D$ T
temp = __raw_readl(IO_ADDRESS(GPIO8_OUT));
9 b( \. s' U' j temp=temp&(~0x00000100);
! A) T0 e# ~& N& Q6 P __raw_writel(temp,IO_ADDRESS(GPIO8_OUT));//GPIO8[8]---02 j" Z! \) j7 e! [7 u8 s
udelay(100);7 J5 E" }+ c7 J/ U9 M
temp=temp| 0x00000100;8 y5 |: g0 z8 Z& R
__raw_writel(temp,IO_ADDRESS(GPIO8_OUT));//GPIO8[8]---10 m! P+ _4 y* x s* j* ?
udelay(100);" Y8 O$ {9 i# w/ H* F
printk("open success!\n");; {& _9 U) o; ?1 u+ O. n4 j
return 0;) a& o# G; q; W$ P: r; \# u2 k
}
5 x- ~2 N+ p4 _0 p7 N. l a6 b3 a$ w/ z3 v# Y4 s" R& X
static int mcbsp_release(struct inode *inode,struct file *file)7 H& `3 @( r9 o, s$ f+ N- s3 ?# K
{
2 ], R* h5 [# l" E p printk("release success!\n");
+ L! |4 l% N8 e8 W" n return 0;
) T5 ?7 ]3 o. J}6 o. y2 c& p% B4 @
! M1 [8 D! L q, w
static ssize_t mcbsp_write(struct file *filp,const char *buf,size_t len,loff_t *off)
( ?9 j; C4 }# }{
8 A+ u2 I+ I* {5 J% X copy_from_user(&DXR_data,buf,len);5 P( U% W2 O2 b( h
iowrite32(DXR_data,IO_ADDRESS(DXR_ADDR));
8 v5 z) ^# k; K) u/ Q* v return 0;9 E' E, P6 W' B+ i/ N i2 ^
% p* u0 q: B3 K7 [}
6 c3 Y- |0 }( j8 z0 h6 K1 V8 ~! ?. o+ D- r) G
static ssize_t mcbsp_read(struct file *filp,char *buf,size_t len,loff_t *off)
( I* R' g% I$ r# {8 N- l6 P{ ; N' \1 C# k# @, N! @1 C) H' ?5 F
DRR_data = ioread16(IO_ADDRESS(DRR_ADDR));+ ]6 j' D" B4 f) K
copy_to_user(buf,&DRR_data,len); 9 w! o5 o. y! M/ T
return 0;
$ q, z' ` @( z; {3 f( F4 w}3 _; c' C' h- y( z2 ]1 r+ N" t
/ ^0 _& H6 E* k8 [' D2 c' O" }3 [4 l$ M/ [2 U8 j; C7 i
static struct file_operations mcbsp_fops=
0 B0 x8 q6 f- W Z' `5 s{1 z" U2 d$ v4 [% H- ?" q
.owner=THIS_MODULE,% B# J3 s8 e3 H$ m9 G
.open=mcbsp_open,
6 V& y* K& ~1 q5 t& ~ .release=mcbsp_release,
c; n& x: m5 W7 t& u .write=mcbsp_write,
1 \9 C# @* y* U6 K0 x) S .read=mcbsp_read,
5 s, F9 [- P# h9 v- j) i; Q; Q};
5 ^8 j1 H3 p% ?) Kstatic int __init MCBSP_init(void)+ H6 Z" ~8 K- O6 I" J" B2 v. Z1 ~% `
{
2 M/ o7 \8 i3 M7 l int ret;& i6 Y I! _. N/ s- @/ e- [, D
unsigned int PINMUX1_REG_old;
( d0 g% X& ?5 y" Z* \, m# U# o% U unsigned int PINMUX18_REG_old;
0 b8 K I! s3 Y/ g unsigned int PINMUX19_REG_old;
1 @. w; Z/ W- s8 w unsigned int temp;
W8 a; k5 o; H3 k# s3 u$ u if(MCBSP_MAJOR)* O* j/ l+ O* n
{ b, ?; |) `0 ]) |& g4 ]
mcbsp_dev=MKDEV(MCBSP_MAJOR,MCBSP_MINOR); U# B0 ]/ o" p- E
ret=register_chrdev_region(mcbsp_dev,count,MCBSP_NAME);& f. x9 U4 s5 D1 H4 A9 {
}
8 z* P* i# q( E: m* L# ] else( Q* q1 P2 e% U+ ?8 ~: a. j
{% B% `# Z$ A( j( c
ret=alloc_chrdev_region(&mcbsp_dev,MCBSP_MINOR,count,MCBSP_NAME);$ G; l \- B8 G
MCBSP_MAJOR=MAJOR(mcbsp_dev);" y l/ T6 {; K* E) y
}
" t' \: ?- k* {* v, h* d) T - z1 b a+ ^/ i8 m0 w. Y8 c$ y- o
if(ret<0)
+ _! Z/ R6 L9 F8 \0 w3 q: | {& }, Q( U3 o( V" g j
printk(KERN_ERR "register chrdev fail!");5 ~4 G' B# G/ d& R+ D* ~
return -1;
1 `* y. ~6 b2 c, w& Z }
: E2 {3 |: ~! U) Y' X9 \ + E( G* h4 Y$ h& w
mcbsp_cdev=cdev_alloc();
! s3 u" r& M' c8 ~9 X1 s
0 K t _! F" z6 I3 U$ t7 e, t if(mcbsp_cdev!=NULL)1 U0 E1 N! C! `- A0 B! E" J% p9 e
{
1 X' @3 W8 _8 N9 g+ p5 Z" X cdev_init(mcbsp_cdev,&mcbsp_fops);* H# _$ R5 w( o; M- N6 K3 w; Y
mcbsp_cdev->ops=&mcbsp_fops;: S7 u y+ g9 o% R8 N! ]
mcbsp_cdev->owner=THIS_MODULE;
' s" D/ ]3 f2 ^! W
. n$ a4 k2 n9 A+ B" Q$ f& w Y if(cdev_add(mcbsp_cdev,mcbsp_dev,count))* @: A/ a' b9 h2 T v7 Z" x
printk(KERN_ERR "register cdev fail!");0 ~. _* F% E2 S
else" K0 z; B' V# A; P( G' o
printk(KERN_ERR "register success!\n");% [' n9 }3 Q0 X- ^" Y8 T
}
3 i) f0 u& \3 T1 ~* y) ? else
: |! `# z! t; b1 w% D7 Y& I {+ E; c2 _4 A% j: G a
printk(KERN_ERR "register cdev err!");
+ g& G" Z! H1 N return -1;! ^/ O! n2 _! u
}$ A5 X! B0 i. y
3 B8 n2 @6 O |# J3 J mcbsp_class=class_create(THIS_MODULE,MCBSP_NAME);0 c8 {/ N) p- O6 |% }
if(IS_ERR(mcbsp_class))5 T, d- u5 q& I4 i% g1 j$ W* V
{
- y- z1 M6 d- I1 b3 s4 j printk(KERN_ERR "register class err!");
$ W% N8 J* |5 |% k! R return -1;
- I* G% x3 a. b$ O: L$ m }
F* O+ z/ @0 h) A' [ device_create(mcbsp_class,NULL,mcbsp_dev,NULL,MCBSP_NAME);! }3 t9 C$ {7 \+ U
$ l" G& y* y& T9 z! j //PSC8 J+ H, @; Q( p- y+ _' F6 ^! m
//add Enable MCBSP5 z$ T0 ~1 Z! q9 D& ^* ~
//test. t) L: l: e; w* f Q
temp = 0x80000003;
]6 Z4 M) B. T( h writel(temp, IO_ADDRESS(MDCTL15_ADDR));: b) T" A* `% {- o
temp = 0x00000003;( V0 f6 N. p3 S1 R5 _
writel(temp, IO_ADDRESS(PTCMD_ADDR));2 B" _2 E& K6 {8 o
5 U8 e7 s& g# Q) A
temp = 0x001FF201;4 [7 H$ {0 ~ T( s
writel(temp, IO_ADDRESS(PDCTL1_ADDR));
- X7 w9 \# Y% @# C$ S4 P4 U $ W& T" p y& d2 @" [& A: [/ S+ l
//PINMUX
* G* ?6 {" e9 ~ //MCBSP_CLK(receive/send),MCBSP_STRB(receive/send),MCBSP_DX,MCBSP_DR,( f/ Z% I' ]% H
PINMUX1_REG_old=readl(IO_ADDRESS(SYSCFG_BASE)+PINMUX1_OFFSET); ) p! U n! G; X0 u# N& C
PINMUX1_REG_old=(PINMUX1_REG_old&0xf000000f)|0x02222220; 8 F" Z2 t$ w# W
writel(PINMUX1_REG_old, IO_ADDRESS(SYSCFG_BASE)+PINMUX1_OFFSET);9 U" x/ U; c5 X# p5 i
; f, J& N/ t, }1 a5 r& B/ [
//SLEEP_EN,EPR,L138_SHK1,L138_RC
+ Q7 F ]6 B8 U7 t6 b PINMUX18_REG_old=readl(IO_ADDRESS(SYSCFG_BASE)+PINMUX18_OFFSET); ; ^: N B$ v5 H+ x4 o
PINMUX18_REG_old=(PINMUX18_REG_old&0x0000ffff)|0x88880000; $ V2 v) |+ U, ^1 r+ n1 u$ i
writel(PINMUX18_REG_old, IO_ADDRESS(SYSCFG_BASE)+PINMUX18_OFFSET);
* U R; E" k! G1 k- k4 B
( {) w% h, ]% c6 e5 c4 W //RESETn,L138_SHK2
N: X$ t ]4 m/ t( l PINMUX19_REG_old=readl(IO_ADDRESS(SYSCFG_BASE)+PINMUX19_OFFSET);
5 C, l1 t1 r' A( ~ PINMUX19_REG_old=(PINMUX19_REG_old&0xf0ffff0f)|0x08000080; 5 N# x; _5 x% l3 X% Z+ k# A' h
writel(PINMUX19_REG_old, IO_ADDRESS(SYSCFG_BASE)+PINMUX19_OFFSET);! m8 ^ Q* M' I7 I' ^+ t
3 l. {0 g5 t2 v ' ?' P! t( u; h
//SPCR Register
! F, v9 \2 f, H* c0 y# i2 {# v //FREE-1,SOFT-1,FRST-0,GRST-0,XRST-0,RRST-0,RJUST-00(Zero-fill MSBs)reset
8 C" _, A; m# j. m5 x2 m% g temp = 0x03000000;//(DLB=0)
: a5 Y$ E# |. q* b5 m3 k' u- i( C // temp = 0x03008000;//(DLB=1)# Q' H& |% t0 m& S, g- m9 Y7 J
writel(temp,IO_ADDRESS(SPCR_ADDR)); //reset9 [+ |3 B( u$ B: U, U- Y8 }
temp = readl(IO_ADDRESS(SPCR_ADDR));
( W2 j$ w: L$ ~5 [( p printk("temp=%x\n",temp);9 t, D' @2 U/ ^' ?# V8 s- h, \
1 J6 C! T+ @6 N- {$ ?4 T //PCR Register! K0 P" @5 L; c1 [7 X# Q
//FSXM-1,FSRM-1,CLKXM-1,CLKRM-1,SCLKME-0,FSXP-0,FSRP-0,CLKXP-0,CLKRP-0
% @/ C2 O2 ` h. ~% C# B // temp = 0x00000F0F;
- R7 e) l' ?# e, a temp = 0x00000B0F;) ^# g4 [- e, ^4 g1 Z* t/ o, W% m$ f
writel(temp,IO_ADDRESS(PCR_ADDR)); //initialized 1 w4 R+ ~6 V9 j$ v$ {# u; |2 |$ P
temp = readl(IO_ADDRESS(PCR_ADDR));
5 M" ]. A: g- D printk("temp=%x\n",temp); , X1 m) Q R- a! P R4 V9 n
//SRGR Register
! t. r: I5 Z8 D2 P% `& z //GSYNC-0,CLKSP-0,CLKSM-1,FPER-31,FWID-0,CLKGDV==113 G, b. R8 S" `# R% A& a% |
//temp = 0x301F000B;9 T9 ^4 C$ U. G# W4 P% i( V
writel(0x0301F000B,IO_ADDRESS(SRGR_ADDR)); //initialized 0 l/ @0 e- t" a. ~
temp = readl(IO_ADDRESS(SRGR_ADDR)); g5 f* {2 G( i/ e/ P0 d% U. w
printk("temp=%x\n",temp);8 {2 Q. T4 S$ U9 _5 E" _
//RCR
# ?" `) _. s" [% `* N# W2 ~# V, a //RPHASE-0,RFRLEN2-0,RWDLEN2-2,RCOMPAND-0,RFIG-1,6 m x; G% c- Q! Q3 N2 y7 u
//RDATDLY-0,RFRLEN1-0,RWDLEN1-2,RWDREVRS-0
2 ?; c7 w9 c6 k& s+ Q temp = 0x00440040;
& h' y. u. x# L6 _$ a writel(temp,IO_ADDRESS(RCR_ADDR)); //initialized
5 a9 ^ }" G- ?* I) l: W8 I" U temp = readl(IO_ADDRESS(RCR_ADDR));
; @- E$ a- e4 G( P' J/ u printk("temp=%x\n",temp);
4 P: V' W. E# g& f //XCR
* k. w) Z. g! ?/ S0 z //XPHASE-0,XFRLEN2-0,XWDLEN2-2,XCOMPAND-0,XFIG-1( E6 c2 @) t' l4 d6 g( \0 s6 Z/ [
//XDATDLY-0,XFRLEN1-0,XWDLEN1-2,XWDREVRS-07 X& X+ Y& K b# G
temp = 0x00440040; s( q8 X' o- u3 v. e! l
writel(temp,IO_ADDRESS(XCR_ADDR)); //initialized 2 m& Y" ?( B& h7 p: U# q+ V
temp = readl(IO_ADDRESS(XCR_ADDR));
9 y0 w+ I3 G2 S! _6 { printk("temp=%x\n",temp);: x6 `: F4 w. T& W/ C. l
udelay(100);
3 ~! _+ r" ?, g2 J; A/ j //SPCR Register
& H* \7 a4 l6 v( }, M3 E //FREE-1,SOFT-1,FRST-1,GRST-1,XRST-1,RRST-1
5 i: H/ b% o: Z2 l9 q7 E+ Y$ m( n temp = 0x03C10001; //DLB = 0 VS DLB = 1
9 G- R$ G9 H; B: x0 c. F" O writel(temp,IO_ADDRESS(SPCR_ADDR)); //enabled5 q2 C* ~/ c$ j' J
temp = readl(IO_ADDRESS(SPCR_ADDR));5 E/ l% ]! N6 B+ W/ d
printk("temp=%x\n",temp);
C* N2 q2 ?9 u. |; t; D udelay(100);2 ]( b' @& j& N; M& B
3 h7 Y" f k B- U8 S' c //set GPIO direction1 p' U2 }- A! ?' i2 }
temp = __raw_readl(IO_ADDRESS(GPIO8_DIRECT));4 A: P' E. f e1 O7 ^
temp = temp | 0x00000100;//EPR----input. @+ F5 L6 V" Q O/ x' j" e+ o
temp = temp&(~0x00000500);//RESETn,SLEEP_EN---output
. @, O7 D8 z2 A& o4 v __raw_writel(temp,IO_ADDRESS(GPIO8_DIRECT));
3 Y- c$ v( A8 V* |) m+ }3 D" i
$ l# m6 K6 Y8 e return 0;
3 }# S& g9 A0 a6 L. f. s0 |- b}4 t1 H+ S ^" L5 h! \! L. U
static void __exit MCBSP_exit(void)& G4 [$ M- M: z( n; T
{. L0 u% e4 `8 Z% {, g
printk("mcbsp chrdev exit!\n");
+ L6 j: n+ |7 X# N: O/ J cdev_del(mcbsp_cdev);" Z f: x- F4 r% g: k4 p0 {$ l
unregister_chrdev_region(mcbsp_dev,count);$ W" U& V8 i$ j; I1 ^+ h+ B
device_destroy(mcbsp_class,mcbsp_dev);
+ Z8 y5 J- X3 t/ O3 t class_destroy(mcbsp_class);6 n/ J0 s1 o( \& e
}( H7 w$ B7 ~0 N* ~/ A* X6 n
module_init(MCBSP_init);& b7 |9 Y( F# G: G
module_exit(MCBSP_exit);4 }" p2 V( W% s
8 m# o+ o! M) c; o
MODULE_LICENSE("GPL");
9 ~0 L' I0 ]1 L' D& c% }$ V
$ t4 E: n8 H. N4 l4 k8 x我讲上面的程序insmod之后我用示波器量 McBSP的发送时钟脚,发送数据脚(我还没有发送数据)以及发送同步脚,3个脚的信号是一样的,不管是用我自己的底板测试还是用创龙OMAPL38 的开发板测试,都是一样的,类似于噪声的信号时钟频率也不对。
B0 U% L7 A W" j+ Q我的应用层的测试程序如下
. j: W/ L! s! s# p#include <stdio.h>
8 D; T, P- d- H' A7 _9 y#include <string.h>2 E6 s6 ~! p- A q
#include <fcntl.h>
- G0 c& D7 L2 y. E5 K& D+ P' ? W, w#include <unistd.h># j" A7 C4 u, N0 N$ j% Y4 _: J
#include <signal.h>* d/ a3 D* O5 n" l9 M1 w
#include <pthread.h> //线程
9 S6 s! ~: E0 R) g& a; E#include <stdlib.h># ~, }9 @% [. r: |1 @( v5 g
#include <pcap.h> //捕获网口数据
" k! B7 y* Q, v A#include <semaphore.h> //信号3 }) U( W$ v. N
#include <sys/types.h> //消息对列
& F2 u# i6 K9 w1 o+ M% n f#include <sys/ipc.h> //消息队列" i: f: g& Y6 F- o# ~* Q
#include <sys/msg.h> //消息队列) j) }$ y9 \: L
#include <sys/select.h>
+ C7 N: ~6 `( U7 O#include <sys/syscall.h>1 s3 W9 |9 z# ~6 N; @
#include <sys/stat.h>* Z3 h& c& {, L3 t0 \& A' N
#include <sys/mman.h>$ }6 }9 u9 ]3 E7 @/ G: ?& v
#define msleep(x) usleep(1000*x)" ]: x; P3 P; o1 s- T2 E2 F
# o/ a. G3 G" Q# p9 U$ Sint main()
7 b3 p! e. k( s+ F3 @: F6 a" b{ % L" w2 d3 O% k2 Z" s9 Y( o: a$ ?
//MCBSP,ARM与AMBE2000交互设备/ I% Y( Y: e& L: R( k, n) j5 S
int fd;5 }" V4 o5 N( L- T
unsigned short data_write = 0x5555;
4 P9 P6 J9 t; o8 F8 W. y o# j unsigned short data_read = 0x00;
; i8 |8 u4 i7 S8 P8 E4 y6 _ fd = open("/dev/MCBSP-device", O_RDWR | O_NONBLOCK | O_SYNC);: y+ S; g. ]/ N5 P u
// fd = open("/dev/led-device", O_RDWR | O_NONBLOCK | O_SYNC);
7 U( g3 X% Q: e% c
8 Z3 o4 Z7 D R6 f/ ~: l; V% o if(fd < 0): p7 |5 G( D H7 P" D3 ^
{: P$ S6 q$ V, N1 }
perror("open failed\n");5 Z8 W7 D1 @. M$ d. d. \* i
return -1;
7 X. k! H& u: D. M2 N }
{ Y( S8 ]: t- S 1 L/ J+ ~+ O# S9 r$ G8 X
while(1)
' }) b( x t) J6 i0 A5 t {
' _! X0 G0 C+ s2 |& ]' m& V
( _8 L5 Z' b, l+ S5 r8 ` //AMBE2000每次读写是24个字为一帧
9 d- b) G( c& Y, l- u //写数据时将数据在底层存储起来,等到中断的时候再发送
8 m* b0 S7 ]/ G //AMBE2000输入数据是以0x13EC开头的
; O5 ~; B2 c& |3 i4 \* D write(fd,&data_write,sizeof(unsigned short));
6 y( O1 Q2 I# n y5 n7 F7 p) X & K+ r9 }# u0 j i5 F1 h& j4 ?
//读数据的时候,在底层中断中将接收到的数据存储起来,然后再传送到应用层
" I# `/ T' n! E+ R read(fd,&data_read,sizeof(unsigned short));$ C* f5 i1 b! I# M
; Y9 ^- ^4 Y+ F' M if(data_read == 0x13Ec)* K; p7 c3 D: l+ J! T5 C: u' M: V
{: J5 k9 V: e( h: J( a3 Q) x
5 p3 H& I% T# k4 M, D9 }$ i3 X: f' A4 p
printf("data_read = %x\n",data_read);6 v8 h/ l$ g" h9 Q# U% \
}! g5 L1 Q* s6 Z9 Q) E& D/ m
, g1 ~8 B0 Z. w msleep(10);
7 d9 m/ g: i; z: Q* z W6 y: `: c8 f0 \+ R5 A
/*7 ]) r1 r# M+ Z, t1 p
ioctl(fd,1); r' E* N& I+ C2 T
sleep(1);
9 F- M" ?0 g8 K" J; h$ D: f# n& t ioctl(fd,0);' ~4 e; Y! E. Y4 E& P+ Q6 w
sleep(1);) r+ \6 W/ S& ~; b
*/
{# E; M* z1 [7 J; @/ K( L }
; f0 ^( X% S/ @# k7 j& B) l return 0;
: U/ @% w9 m% h! b# v, L7 u4 |9 B 6 M( J+ a- C. c# S
}" h9 Q& H& Z( P
9 i d5 [4 m9 H( @6 k' k0 |$ ~多谢各位指教,谢谢! 急
F/ }. L( _9 t5 ]/ O6 T
4 J' @; Q7 P/ U% W) C! U2 p0 [, h k Z3 U9 ^7 @
/ c$ L6 E9 v: N. c# w( R
; b7 Y; k( Y E/ h+ ?3 _( Y. c
_" Y$ F% b/ c& n% f/ _ |
|