|
|
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑 1 h7 ?$ y" d4 `, O5 j
- S: h7 N4 o6 X3 F9 S, {" n
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
; G2 F( |9 y' h* ~#include <unistd.h>
5 y3 [5 j8 j/ P1 j, L- w! T#include <sys/mman.h>% @1 L9 f6 f5 |" E4 e' P
#include <sys/types.h>$ B. j, G9 Y9 S' M1 `6 c
#include <fcntl.h>
. I2 T$ p" I0 B) o! D
: c" h4 k9 y- i* H#define SHAER_RAM_BASE_ADDR (0x80000000)
n5 y. [# }/ J, Q% D1 r& }5 ?5 {
9 L" s% S9 q. p, ttypedef struct+ u. m& i- g% O* S+ @, {( M* d
{: v& Y, F# j1 M5 J: Y( J
unsigned int a;
$ _' m/ ^7 r4 } n% N unsigned int b;
2 _' g' `9 u' M: ?5 Q; X unsigned int packet_cout;
6 H* M; ]4 i+ o: u/ K- @}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
' u" k( \# m5 }# ?6 Y
" H* P: W) K8 Y: D2 a( \6 V3 rvoid read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
1 m& Y% c/ k2 d7 [ S+ w! f9 dunsigned int count_copy = 0;
& l3 O' h" j% Y8 D: B' x" c3 R7 W/ ~4 J: t5 A8 k
/ T; y; w2 C2 p6 y
int main()
# ?/ w0 o( p' W/ f ~: R1 W{
" I: u- j! m# ]$ v6 J& V7 b7 | pRX_MSG_PROTOCOL pshreRAM = NULL;
6 U0 W) n. Z; g6 l- W' [ pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;# J! u/ G5 \( e |6 y. V
2 Y6 X [' I9 w, ?$ B3 x) `
while(1)4 g' Z+ e$ l$ M. O, Y @
{ e% [* q0 J% \/ c, u4 Z* y
read_MSG_buffer(pshreRAM);9 W% Z4 j: c! ^( {- f& m8 R+ o
} % z2 [3 j k4 c( q- z( _
}5 t4 d y, `6 j @; i! v# P# ?
* e A1 f$ @0 B8 ovoid read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
. {( ]( _( t1 }' \/ h- \9 H{, \9 V/ ]! Z/ {* T8 ]
RX_MSG_PROTOCOL buf;
* V8 J+ ~% p+ O; j* i! c
5 a" L& A+ y' Y2 \ ] buf.a = pshreRAM->a;
7 Y/ n2 F9 g+ q5 X0 ~ buf.b = pshreRAM->b;5 Q7 H2 \/ |6 ^! L9 e8 H3 R
buf.packet_cout = pshreRAM->packet_cout;1 e5 p: r2 ?6 k6 i6 _7 f( z
+ w9 C/ C8 X) Y* w
if(buf.packet_cout != count_copy)# R% H# J5 }& y. Y c1 ~4 c# F8 ~
{& n) L; Y! z0 E' k( J' R
printf("a is %d\n", buf.a);
: E& `! D. s5 d ^) A' h printf("b is %d\n", buf.b);
! l$ U# ~& ]1 {* H( k2 N printf("count is %d\n", buf.packet_cout);
7 M4 f8 n8 K1 N- Q# i8 b4 | count_copy = buf.packet_cout;
& g; ?3 x; {+ T }
8 t5 A) \: u, ^: f- ^) L else
$ _) ?! Y2 _/ p- j$ @ {# V" s) c4 x3 R/ x; u0 }+ R
printf("No effective message!");
% F- g4 ]: c6 Y# W0 V2 Z; p }" F8 z4 Q; @: X4 y1 q: v& U( e
}
0 O/ h6 G5 d) i6 a4 x0 q
# J+ A9 o/ [2 w! h8 q3 s4 m5 S, P b+ F/ Z! I3 K
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。
! n0 q% z7 q7 h9 N: A: M4 v" w使用下面代码,对内存使用了mmap函数后:% ?7 U5 _ |$ E7 p+ T+ Q
#include <stdio.h>+ ~& I0 Q" D1 F0 E! ~
#include <unistd.h>- O/ a3 ~0 q( }- h3 ?: e* C+ m
#include <sys/mman.h>
6 Z( \2 q! s* l! f$ W( K9 n4 ]" I#include <sys/types.h>
( f1 [; K# M2 z6 W0 s5 |- J4 d5 `#include <fcntl.h>
/ E" I$ w$ K- u+ U5 u2 B. s: G5 R& [2 w6 v
#define SHAER_RAM_BASE_ADDR (0x80000000)& ]3 x$ z0 Z2 w' [" q% Y; G$ B$ I( e
#define SHAER_RAM_SIZE (0x20000)
& s. c: h2 `5 `( K# g
2 X9 O1 Z- f: r. u$ i7 M& ytypedef struct# o& S6 }2 b& g; ~
{
. E4 ]. m" L% d$ q# W unsigned int a;9 d' W/ u0 {& ?% e0 m
unsigned int b;
4 g/ B( h3 M9 I4 J/ j0 m unsigned int packet_cout;. u: F! {- o$ q4 K( x
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;; Z, r3 c& k% o7 \+ ]% O, c3 d
8 r' v9 T6 e3 N. | Evoid read_MSG_buffer(int *baseaddr);; p+ Y5 F. m ?" b
unsigned int count_copy = 0;& y8 p/ B3 l: C w6 R! F
1 ~2 ?. T0 f$ v5 J
int main(). L0 D2 _6 r$ G: c) i* C) O9 S
{
' ~3 @: ?5 P( h- d6 M6 E int fd;
( q2 D7 w# g3 v; X. V3 P: y" E2 `9 O int *mem = NULL;
6 k3 V% Z9 K& h6 N& h+ `
8 ~, ]7 X' p5 C K6 p0 b0 f+ p if((fd = open("/dev/mem", O_RDWR)) <0)' _' m% J' D' v% S
{
) J1 _! q, b7 o perror("open error");) H M. V: H( V9 Z$ d: u
return -1;
9 r9 H+ R5 g5 l) o }( X: ~" G7 R# U5 i4 l1 a @
* U( U/ p; c- ~" B/ x4 v3 `9 T
mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);' x6 w, c% N; Z6 _" _0 o
( y9 C9 B) G7 c! I& a% a: g while(1)3 r: e8 R+ A" f+ ?9 x9 a3 O
{
! L, k, ?) K \# r8 P read_MSG_buffer(mem);
, G. Y" q/ b& V8 g( P$ I% B" Q }
0 W( r$ h4 B# j3 N}) V/ j. k6 h8 p0 h% `
) `7 A7 d9 O2 J5 x: p8 Z" i) F
void read_MSG_buffer(int *baseaddr)
+ }# D" t8 J! m5 x{
6 w. ~' b! X' X e; z: S pRX_MSG_PROTOCOL pshreRAM = NULL;& R, Z1 D3 F1 H; c7 p& u& R
& G! K7 y0 i0 Y1 h1 W pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;
! P- `! y) T. _: j/ _2 H# Q
) {4 L, W$ e% A" | if(pshreRAM->packet_cout != count_copy)
! D; P0 l, E" J" T' a3 l L8 i {
; u* [% S! T _1 f) W8 b printf("a is %d\n", pshreRAM->a);
7 b7 ?6 Z' a7 X- c* i* p printf("b is %d\n", pshreRAM->b);
; u8 N& ^- g( @6 Y2 Z2 L9 l printf("count is %d\n", pshreRAM->packet_cout);" |/ b% U. Q2 K
count_copy = pshreRAM->packet_cout;9 K7 w' o# F- g; K; ?4 n
}
' [0 A) i/ K: V; {+ R' ` else
2 f6 x S v' G; g) G5 o { n2 H/ B: T5 C) d9 Y+ C
printf("No effective message!\n");
/ H4 h5 k; u) R) d }9 B5 |$ s( ]- u: ]
}
7 E: B- T" `5 h5 h/ G+ F k% J, n. o+ d4 M
没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现??? R# V4 x. `8 g# o1 m
+ r$ |; S% v& [) r( X: V
9 a. ^/ C4 q" b1 z6 C$ _, A' i
% O' m' P, I- ~/ E3 ~7 f: G9 d
" F: W) h6 ^0 {; \' B. w# a |
|