|
|
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
/ |5 f5 T3 l5 b7 D+ |+ j! {8 W7 c: {: z
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>4 u! G s% \: K
#include <unistd.h>
. p9 W2 P8 U9 |. O) ?0 i% [0 g8 d#include <sys/mman.h>
0 [! U/ s9 s6 t% X' R#include <sys/types.h>1 h! y* @! l+ }8 O
#include <fcntl.h>
( z9 q- A6 C- V6 T1 Y3 D3 J& A# u* d# z' f4 b1 w
#define SHAER_RAM_BASE_ADDR (0x80000000) ; E' j) v3 d# Y. ^
6 `: s- ?7 [: H9 J
typedef struct
/ U; }+ g1 b) o% A( p" R* |# }{7 p6 y2 \! I* F- P* O1 C
unsigned int a;
3 S' ]" O0 ? Q) H6 V unsigned int b;5 g2 ^ n% R p/ X. [ ?; P- t+ G
unsigned int packet_cout;
1 u( y( W+ F0 E) X. ]# t) C: N! k}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;% {1 u+ u- L6 r" l
4 u# w: g* x2 J
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);3 [& q' q p: ?, \
unsigned int count_copy = 0;% v E' {% Y5 t; i( y
( P& O3 i! B% o1 E8 Y
6 S) t+ X. n2 k8 a8 aint main()
. f y& D' K, q6 c{ ^: J5 [9 t; ], H7 s
pRX_MSG_PROTOCOL pshreRAM = NULL;( [9 B& t% { R, _8 P7 S, @- B
pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;! L) y% X' T( @2 V) v
& Q& k" h5 d% v: f6 f ~
while(1)
6 @: c# @6 k3 Y$ P/ i7 h" o {
" d2 V) y/ P5 ~1 h4 Q) \) k& j# N) l read_MSG_buffer(pshreRAM);: b1 _: c+ L) Q4 B3 n
}
, F8 ^' _ s/ O8 z: x; T( k}
3 z" ^- w; t1 I l
2 S3 ?) V+ [% L& y) ~' Jvoid read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
2 {: c* M' U0 L" B5 d; j{) Q9 L2 D+ S4 D/ z# ?, }
RX_MSG_PROTOCOL buf;
) L5 ]) c& S5 o( ~' C# I 5 L. {" a* N5 C6 V' G0 ]. n
buf.a = pshreRAM->a;
6 A/ _7 Y6 V- S: h( r buf.b = pshreRAM->b;
9 t% g! U6 q) I C+ ` buf.packet_cout = pshreRAM->packet_cout;. q& P2 R, f0 H; s
# Z1 y/ b( U& K. p; v- b$ l# j% p
if(buf.packet_cout != count_copy)
/ U9 {9 R j/ w( n/ e. o+ ^ {3 b# `- `' O% U* l4 g
printf("a is %d\n", buf.a);4 ~0 o, X$ g2 p* \# b# n
printf("b is %d\n", buf.b);, }! d: X- n8 h C) C
printf("count is %d\n", buf.packet_cout);
& p$ U# q2 B7 l. d: p; u# |( V count_copy = buf.packet_cout;
: n) M: s8 [2 @+ s }
0 s3 I. q5 V, U- o0 ] else9 C9 m( O) |# J3 t- d
{! s& R9 }) N( o9 S! o$ i
printf("No effective message!");
! m& f+ i% H4 _ E' ^2 s }1 B3 [: M8 X3 [; s" U5 y
}
* I) V4 D% j/ [4 T3 C
$ h3 I, h0 D0 s# D9 x4 C7 O, i% t( Z4 `9 P9 O
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。
' _4 e, p5 f: }7 C; E使用下面代码,对内存使用了mmap函数后:2 B1 c& O' @# _* w) u
#include <stdio.h>% H: L8 q1 L& X6 L
#include <unistd.h>
& P3 x. [' ^ @#include <sys/mman.h>) |8 V/ e! d! w# e6 w; y
#include <sys/types.h>
3 G- Z+ ]$ n5 D#include <fcntl.h>9 Y0 ]3 w. M: B% n6 j1 q
! F) u& T3 N/ e# @2 m: ^3 B9 l( ^#define SHAER_RAM_BASE_ADDR (0x80000000), E: K; m, v$ v" X
#define SHAER_RAM_SIZE (0x20000) : z5 A; r, L7 r4 G
4 V* L" v Y; G; K' htypedef struct" n! M' y# h- ?3 u4 p& G
{. ^' w6 e2 | X0 Z7 O
unsigned int a;- r. ?) I' u! A* r
unsigned int b;. O y# p/ ^: t
unsigned int packet_cout;$ \4 l; I' V/ c( c! c. a ^; a
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL; L6 L. j) }$ D7 I
( f. y' g% @ A( [- X$ L5 ^
void read_MSG_buffer(int *baseaddr);" f! S; ~& l6 R1 ~8 v7 _* r: x
unsigned int count_copy = 0;7 J4 d: w1 G& k% c$ k4 A% A
4 p2 k3 i$ |0 c
int main(), h1 J& [3 y! \3 d; A5 R& K+ n
{
8 E8 G/ w5 X8 I' Y8 q+ h2 q { int fd;# z* O8 R+ q1 u' ~: x1 R* S& m( j
int *mem = NULL;) r* c* E( L& e0 {) t/ @
* L7 Z' j U/ J4 d- H% i if((fd = open("/dev/mem", O_RDWR)) <0)3 U/ S" f g1 v5 R2 C$ H* N
{
" y& j& ^) q3 H( a( S+ M$ d) ] perror("open error");
& B( {; N* E) n3 M" C& { return -1;$ K. S, `8 y6 v L! P) \
}
6 ~+ l6 P) \5 A' T3 O) B9 F% {7 {
: g3 v) h4 K% A$ h7 i$ y mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
$ x8 d* O8 `' |+ {% a* ?- d/ v$ S1 D
while(1)
8 L& `+ j. \; [9 w2 s8 s3 ]6 L {
( f% `) s5 M2 g; j: G+ b; Y read_MSG_buffer(mem);& p9 Q! u5 ]% T9 l) @
} " B: k8 H; p; ?/ ^
}
) I4 l: S. L; K# L9 ^: Z. ]( V M3 P% a7 \+ A" q6 ]
void read_MSG_buffer(int *baseaddr), d4 J3 V3 r G# h2 x+ [; L
{
' t0 M5 c6 a' P; O2 g; x pRX_MSG_PROTOCOL pshreRAM = NULL;
9 U; u( N+ I) f0 G% f8 |" ^ F1 s$ J
pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;
: }' k1 e9 x, l" q. I5 E0 D! Z, r6 l) z( c% d) {5 E
if(pshreRAM->packet_cout != count_copy)
7 {6 l# H0 ^) g! A7 d { r+ E* ? i' P2 b
printf("a is %d\n", pshreRAM->a);* g1 J0 b0 W$ f3 x
printf("b is %d\n", pshreRAM->b);
9 ?" z6 J. M* z printf("count is %d\n", pshreRAM->packet_cout);0 D/ s: w- k" j# W7 `' Y+ j9 V
count_copy = pshreRAM->packet_cout;
; H4 D) D) l, Y( R8 M }; m0 d* a, i: Q. W f
else
G, J" Z7 f+ u5 H+ ]7 s {
; @% @5 j R5 M" i5 W, E printf("No effective message!\n");! r* `) C7 }& J( g
}" |2 t/ q* @& W& ]: y4 L
}4 @: ~% t8 y- s' L/ c. `, [7 S
/ F+ H% ^! v& z$ Z6 \没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???
" \( ?' }0 V, X' j6 w$ H0 C
3 H7 x2 d) Q# Q: g6 a9 |& v1 I! D% ?% R/ [) a. M6 Z% O2 n& @2 s
' d1 s. n/ X3 N; v- G
+ `; j- Y- X: \; ^6 s" n, Y" ]; {7 A |
|