|
|
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
$ p& W; R5 C4 x/ a" G, N$ `
9 ~$ E/ [9 {4 s! o0 ]OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>8 {# w/ V4 ?' A; S$ X- \. K9 f2 d2 m5 p: E
#include <unistd.h>" e$ T4 t0 S- F6 p! y3 e0 J4 A! L
#include <sys/mman.h>) Y0 W' B3 v) R& a0 E( z
#include <sys/types.h>" }( E$ o& s# z% i4 B" `
#include <fcntl.h>' b- B3 T2 c x! A) A
' _) h1 I5 @- _( ^" C7 J#define SHAER_RAM_BASE_ADDR (0x80000000)
; K' E2 f; y0 Y: T2 _& D- H8 J% I2 s& J% Z
typedef struct1 @/ |8 R: w+ Z8 ]( _) k
{
/ R6 A5 h& i7 [7 l f unsigned int a;9 K6 d( a4 w, _+ k3 Q& F- r+ z
unsigned int b;/ X$ i2 k# U8 x3 B+ H, \
unsigned int packet_cout;) }6 W( @* t$ W1 [6 w' o j4 z
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
$ F( F, a9 C( K" Z% N6 W- [' K
: @! ]; K8 i! | @6 Fvoid read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
/ @& \+ a) h" v" Gunsigned int count_copy = 0;
, X" M5 L% Z' p3 ]; ?" Z' d
9 c8 }* }/ D! G( Y o1 z% n) q3 ?+ g! f4 _
int main()3 n7 X& M% e% c, s
{( v9 K* ~4 \6 B, ]5 v6 U+ }
pRX_MSG_PROTOCOL pshreRAM = NULL;
# ]1 a: f5 \" u7 V, V4 t pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;! ~. x7 {) N: i0 K3 Y
9 r' L+ {! S& d5 O8 J1 A) Z
while(1)$ j1 D, H/ B% N* o: C# n
{
6 C0 K/ a- h& \& S; ^ read_MSG_buffer(pshreRAM);
. R/ N, x. @0 r8 k/ B }
X& b) }" a/ J6 L9 i0 m}
9 a( Z2 `3 `( e5 i$ N& r7 n
3 U* S; H% c5 v4 x. }1 x, ^void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)8 Y0 H% L, q3 p/ H" j& y4 Y
{
3 }5 q* L2 M8 Q+ j* { RX_MSG_PROTOCOL buf;0 N @" P4 y A( f
' Q! X& O, n/ B7 C% I
buf.a = pshreRAM->a;8 Z2 ?. o4 _/ a/ p( E
buf.b = pshreRAM->b;
/ Z y( i4 A" q* w buf.packet_cout = pshreRAM->packet_cout;
% M( [5 @: m# ` * a0 G. f# O% [4 |9 U, ~
if(buf.packet_cout != count_copy)1 @0 @! d X* L4 {+ N
{* D4 c& i0 c$ g8 Z# l
printf("a is %d\n", buf.a);1 e5 o, o3 z+ f8 c/ |% o9 \
printf("b is %d\n", buf.b);
! u+ e9 }+ i- l8 c printf("count is %d\n", buf.packet_cout);
# [( b! O1 `* d$ o count_copy = buf.packet_cout;
5 E/ E6 G2 _4 w$ o0 y0 T q }
& `* d) w2 R/ t; J8 h& H2 r else% H' g$ L" L% W3 c3 t2 [
{9 A9 x# g" a4 R* s: f
printf("No effective message!");
u% z6 M0 y- t6 a9 g! o8 }1 o: Y9 { }
# @% Q( _/ S4 T4 d* [2 Q}. U7 B' I6 T0 w6 V$ m3 o) T& y9 Z7 G
6 \5 e. t( G3 H) a7 @
; d( z7 L& L) r, i
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。
: R8 J3 ]2 s% n+ w, `) V0 i2 ^# s; l使用下面代码,对内存使用了mmap函数后: Z7 w4 N, V- v( w' u
#include <stdio.h>/ \1 r' t ]# Q8 ?
#include <unistd.h>. X; R4 a2 n. {; D
#include <sys/mman.h>" ~# u1 y) h8 R. n0 ?
#include <sys/types.h>
, a5 {( V; G* K& l; }; l. j, e#include <fcntl.h>! L6 H+ W' D1 I# D& P
) e j# d4 x7 ^ `& E3 n9 n; j
#define SHAER_RAM_BASE_ADDR (0x80000000)
& x6 ~1 x- x8 d$ `2 P( Z#define SHAER_RAM_SIZE (0x20000)
# {+ ?, P: [- s, R. v. V
; j# Z7 W4 e7 B/ m/ h: Rtypedef struct
8 R. w" P l( L" s{
. l3 F/ a, t) d& [; Q9 S unsigned int a;- f. R) d9 B# D7 L& }6 H. x
unsigned int b;; L' A1 P9 c( u# f
unsigned int packet_cout;/ @# x7 j9 p" E
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;3 ~; c# m' [& ]- O" j3 z# l
6 S* L' c; S0 b
void read_MSG_buffer(int *baseaddr);
$ S; I N0 w- h$ h# n y" c5 |unsigned int count_copy = 0;# [4 g! Q+ r8 F- _* P7 z0 q l
2 r$ [7 w6 y, F! M0 Uint main()
: h. [7 E0 b) T J{5 P& V! [4 u& m6 \* i. j* j9 F
int fd;# L* h% P2 ]' L; o4 F
int *mem = NULL;
$ S3 c- F- H$ {7 A0 j; o) q% {8 Q2 y. A
if((fd = open("/dev/mem", O_RDWR)) <0)/ C E6 i6 k: N3 o1 g
{
0 W& w" g4 j9 `0 { perror("open error"); }- a( ~) A6 X: K4 o4 i
return -1;
1 [+ F# d- A1 M }
$ ^4 i+ G7 G) ]) S0 {0 |% Z | . [2 _6 w3 H& q5 t# _
mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
6 f+ W+ U$ _' @$ P: }
, V4 s3 ]$ W5 @9 p while(1)9 Q5 d, f9 K: B# W7 M- {7 a
{
3 T% Q8 `* i1 `# \0 @ read_MSG_buffer(mem);
" a, D" ?( p. b+ l% |5 g6 h1 i }
' c/ M1 i. h. ?- x" J: Z}* ]" T( c" f( W, _3 F+ I4 `( J T1 C
1 x& o# K" d8 i$ y
void read_MSG_buffer(int *baseaddr)
& x; c( z/ J V) Y A, `{
. H& D* i6 ~% i pRX_MSG_PROTOCOL pshreRAM = NULL;% S: H2 D, I, ^1 J/ R2 \/ b$ W
0 x7 l. M: M: j
pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;
+ b4 m8 g/ ^5 X
& Z# Q, \: |# {8 f if(pshreRAM->packet_cout != count_copy)* H& v$ N1 f6 j" ]' g0 R
{" y) _( R7 @, z
printf("a is %d\n", pshreRAM->a);' O4 n+ K6 h' @% G6 E
printf("b is %d\n", pshreRAM->b);4 Q3 j7 d8 C s* P
printf("count is %d\n", pshreRAM->packet_cout);2 x, {/ b) U/ a. q: R
count_copy = pshreRAM->packet_cout;1 Y, e9 t; H. [! a2 ~3 ^# h) S0 D
}
s9 A2 O0 ^/ @ r5 H, A else1 K! ~6 D( }2 C9 R
{( H- s1 S1 i- r
printf("No effective message!\n");4 i# q- Q% k( T/ b
}7 v1 j# Q5 z7 i e0 s" t
}
4 q0 T' S; Z$ O
! h& |+ B r" q3 P" W+ @1 b( i6 W没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???
- b+ F B, s- J O& K, p0 _" S( ]
" P! r4 _- j$ M' M* S9 E" \8 n f/ A$ u: b& v' Y, G
. y& r; m* h/ ]/ l% b$ E1 j/ `! b1 [/ z. k: E
|
|