|
|
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
- u8 N) t; M* ?0 B3 I8 Y; u: w
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
1 r; L- l. S# n( C#include <unistd.h>
" f. p, E, f$ n9 V0 u#include <sys/mman.h>
1 Q) x- c" n3 ?( ]: \/ j- g% l( ~0 O#include <sys/types.h>
/ W# }' z7 F3 ?2 z/ a#include <fcntl.h>
: }) [. c/ [4 \; v% ^ A# m, k* u( J5 m+ A& S
#define SHAER_RAM_BASE_ADDR (0x80000000) # V# Z4 H) l" K F3 H( ]- ?7 I' _7 R
' \, D6 o/ I6 h% D5 v7 Gtypedef struct% N8 k" L( Q: s9 J
{
% n; q9 K5 J5 c- } unsigned int a;' ?3 d) y7 b$ }1 @. B
unsigned int b;3 t5 |9 `. a6 B
unsigned int packet_cout;" _. A$ P8 E7 O1 G/ P- I( G/ j& A
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
$ h# w& U: K4 A X, G$ q; f
$ n* u1 ]: S/ \void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
+ f9 x1 X$ I: ~" Q- h/ W7 cunsigned int count_copy = 0;
, r- J" U8 L& d) Z, d7 z9 p/ H5 g1 j$ {( k
# e* I! [0 I: }1 r, |( m
int main()% x; q: J G0 O
{! t: G- p0 B( H; K0 m8 y: Y
pRX_MSG_PROTOCOL pshreRAM = NULL;! h% `+ I0 o% B1 K
pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;
( G' F% c5 [( ]4 g' E$ |) N2 j& A! {
6 l- y" z$ Q' ]0 a j! l+ [ while(1)
2 l* {6 t) ~, } \6 q {1 ?% }/ y1 H' J q
read_MSG_buffer(pshreRAM);
: v: t# j& }6 k2 Y" T" L/ d1 n% R }
8 i( q1 j0 B# t! ]6 t! v# F}
0 T+ R! V& u" a: x; U0 p: ?6 F u
; Z1 I' i+ S5 G5 Z- S4 ~6 lvoid read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM), `# K4 Z3 V& H. E" [
{
: t, O! ]6 K& E RX_MSG_PROTOCOL buf;
5 X& W4 C) L# e* \
; |+ r1 A0 O/ X# a3 ~, ~8 m$ ]0 w8 a buf.a = pshreRAM->a;
9 C+ d# Z% L8 m6 I/ l! b- s5 o buf.b = pshreRAM->b;* [7 k) |- M9 S& w* F6 \4 B" n
buf.packet_cout = pshreRAM->packet_cout;
5 e7 c- e/ S7 O) ?' x3 ^* l# E
* D7 y: b# o9 X$ D& @5 l" K if(buf.packet_cout != count_copy)
1 j, b# c0 ?3 W6 x {, T) _. G ^$ u# x5 v, P. U4 \
printf("a is %d\n", buf.a);* p6 j& u. _$ u4 X
printf("b is %d\n", buf.b);, J2 n) o* `/ F- ]
printf("count is %d\n", buf.packet_cout);8 e0 l: l o' c
count_copy = buf.packet_cout;6 d. } V! r% d: o8 u
}
& _( p [6 o3 z5 _& ?& i else9 O6 a, A. b& K3 }
{+ q/ }# x" ~7 u
printf("No effective message!");) N" p: u* f; B0 Z* l% m
}3 U) R! k- B3 F. X5 z; K
}
( N% ~' t) ]8 K9 G. q
% {2 y; F8 O# g/ t" n/ j7 y) ~6 d5 r7 p+ |, A0 M9 F% h0 C( C7 H5 d4 O
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。
9 ~6 g# X/ g! m: I9 s使用下面代码,对内存使用了mmap函数后:+ H* X& M; Y# i* S
#include <stdio.h>) w6 E7 l- d8 S; U# y' s3 O3 y
#include <unistd.h>2 h S' [: o- s' h2 H3 P
#include <sys/mman.h>
7 q @3 B$ m' f#include <sys/types.h>" F) n& A J1 _4 V' @
#include <fcntl.h>' g& L! X9 P' K: Y7 g* |
9 W7 w$ Q* ^- T5 t5 Y
#define SHAER_RAM_BASE_ADDR (0x80000000)
) b8 M- q2 ~1 g) V#define SHAER_RAM_SIZE (0x20000)
) F; a; Y0 q' i4 t8 u w/ r" v- K0 Z6 q1 z* P
typedef struct
- O6 M# j2 ^( t' _{
8 r. a8 M4 r3 t4 P s p unsigned int a;
% j9 x* j4 q; z9 d1 Q: f unsigned int b;# }, z: Q- E' x9 {- U$ X
unsigned int packet_cout;
1 ]$ C/ v ^( Y0 z}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
4 Q6 L2 ~" W9 x# s _9 v; `% w8 B5 b, v7 G
void read_MSG_buffer(int *baseaddr);
+ R" e7 `6 e! g" L' ?1 vunsigned int count_copy = 0;9 \# P" |$ u7 L! E/ L
8 Q% _8 ~& Z3 N
int main()
1 x5 Q" W2 L8 d1 T1 @{
7 D t# x8 W6 q8 ~; w- k int fd;' m: y- P# B. Z. k" {; W+ _3 j
int *mem = NULL;+ ]; N6 {$ ~8 g# ^! a7 z
7 T, V8 F8 {5 t! N
if((fd = open("/dev/mem", O_RDWR)) <0)6 e+ r% q8 g `; G! W
{( `. A" i* y8 \, I5 g' z
perror("open error");7 \* W5 T5 z% |
return -1;
9 ]4 O" \) @+ k6 z% U' ? x8 ^ }0 g. B. v+ V" n
( ] Q6 _) H$ H
mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
' z: q2 i* Q2 S4 L( G: B+ A( w
while(1)
2 C6 X! u' C9 m$ Z, A& ] {; w/ D1 O% _( H* N# V6 d
read_MSG_buffer(mem);2 ^9 z9 V5 \( D X) _' u
}
0 U$ A$ j! `9 s2 }# r0 x/ B}& y, E# x, F+ X# F
" d ~: }% K8 C5 l9 d2 k7 v
void read_MSG_buffer(int *baseaddr)* I5 e; b) |+ ]9 N; Y, Y
{/ T6 w; o f9 G* p
pRX_MSG_PROTOCOL pshreRAM = NULL;. B j% E2 ]# b8 L4 G4 F
( q3 i7 E7 X, b ~ @- O! Y
pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;
, {* q. O! R+ w! d6 w
1 F9 L. K6 `7 k% A if(pshreRAM->packet_cout != count_copy)
* @# x* [3 F1 c8 Q+ Y7 ~ {
" O' i, w9 l# w: X; M% H1 w# t8 ` printf("a is %d\n", pshreRAM->a);
* l, S6 i- h1 s) F! z0 ? printf("b is %d\n", pshreRAM->b);% R" D% `) _+ N/ O% p4 `$ A0 u# a
printf("count is %d\n", pshreRAM->packet_cout);
. q& A; ^; s; ~( F) g$ x count_copy = pshreRAM->packet_cout;- Y$ `, S p3 P4 N5 }2 V
}
. h' s" f( Y% }' o2 ]8 E2 D) [# r9 r else m& M4 g1 g! c6 J9 q
{
" a" `& @$ }5 r* D$ L4 g printf("No effective message!\n");
( s, x/ ]8 O+ _3 r5 a3 U } v+ H7 Z ]5 d4 [
}
: f( l; L# I0 F+ U- \+ h+ g: r% W8 N
没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???$ N4 z$ N2 L) M% i8 C& O1 U1 P9 }5 s
' p" C1 y0 d6 l7 a. ?$ w- \- D: ~
% y2 g0 W. v+ J
! q7 n& _5 M% H/ h3 A# u" s3 ?3 f* W" \7 P$ T W. i: j& @2 p4 |/ ~
|
|