|
|
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
" P5 a( N! b) c9 W
9 x/ [/ X+ e& r% `4 b( MOMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
- B( U9 \1 x: B, s+ ?#include <unistd.h> x/ v4 J: _/ G# d
#include <sys/mman.h>
! \- x j- U+ N' a L* e. I0 f#include <sys/types.h>% r; Y5 f+ D4 O8 k) X3 J, Q
#include <fcntl.h>
3 w: g; X5 M3 ^* A- K& ~7 D- v) j: o* r
0 `1 H* i% c& F0 R4 e4 e* h) x#define SHAER_RAM_BASE_ADDR (0x80000000)
* h3 \0 W$ }* c6 O6 R4 B1 h8 ^' N
typedef struct
- C/ P8 B2 w) k{0 o, V) t0 }( b H0 [1 V$ Z+ q
unsigned int a;/ T+ L# q! Y) [; U6 i# w
unsigned int b;
A" }5 m$ L! |( @ unsigned int packet_cout;
& z3 {# ?: o' M0 ?' t}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;/ G9 E! d p4 Z" p" e0 W
7 H4 x7 _, g: j s3 s) [8 Q
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);' X, P& w6 _' W: o. P/ F7 G
unsigned int count_copy = 0;
! m0 S. q) Y" s+ S2 l3 S7 x# L
7 Z0 ]# g8 C$ Y7 y
. w# s0 {3 ^: \" r+ R) @int main()
# H9 _& |& `( d @; e" y5 R J u2 b7 G{
3 ^: J% m; }8 o: R: e pRX_MSG_PROTOCOL pshreRAM = NULL;4 c: f, m2 t b+ H8 \! V; s
pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;, h P% R% {0 b2 Y4 t
# f: Z$ K8 e: l! d while(1)
$ P. l. ^" g/ c$ R9 R {$ |! [' c9 f" {
read_MSG_buffer(pshreRAM);
- K- k: `6 V0 l }
0 k! I$ ~+ k) w" ?; F}
5 h( c+ \- N, ^) l* a3 X. @5 B: s; q7 W* ~: }
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
- Z, A6 a) I" ~{# c2 M. o2 d7 a) T
RX_MSG_PROTOCOL buf;
! x% S: T- Z( Q# L8 X [
6 e/ u9 G. [/ J3 i( E6 T' A buf.a = pshreRAM->a;" T' V6 K, Z, o B6 I# @
buf.b = pshreRAM->b;: L7 V* @% a2 ~9 E, ^. ]# Z
buf.packet_cout = pshreRAM->packet_cout;& h3 U1 q+ t% T$ M0 Z9 B
" W" k P; u" @& ?. |8 C- @ if(buf.packet_cout != count_copy)0 l. B0 j) |& \# W
{
5 G' [7 J* y+ _6 j+ M5 t printf("a is %d\n", buf.a);
; \) T6 E3 M& D; m# Z6 q printf("b is %d\n", buf.b);
4 z; n# o: a' Q6 _ printf("count is %d\n", buf.packet_cout);* N% I- p+ Q a/ a
count_copy = buf.packet_cout;6 Y6 V8 W2 u9 p8 O3 [
}3 ^: z" }- M/ t% f+ |
else% F( j& ?7 }# Y6 ] d
{
, w' H# r) W3 R1 Z6 {* `6 }2 ` printf("No effective message!");
$ S' f% A; r. S y1 { }# Z" z5 h2 L! }' y( W5 a
}
7 f9 o$ C7 K& ~. r1 f
, m$ k! j4 b# K/ g/ ^6 c4 `3 X7 e5 y
" W+ G/ Y/ B. s! z# a. ?2 Q$ `但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。, {/ r3 R" P- @. V
使用下面代码,对内存使用了mmap函数后:
6 {- L& g( U* j' \9 g( {#include <stdio.h>0 k+ o* u2 i# U: g
#include <unistd.h>
; @* q$ ?0 d/ x4 D#include <sys/mman.h>. U+ C) \+ q8 \" ]* X2 r
#include <sys/types.h>$ V: P p6 \3 n7 X
#include <fcntl.h>( N4 `: O3 q9 m
4 r" k$ O0 n/ e( p+ o2 {. v
#define SHAER_RAM_BASE_ADDR (0x80000000)
) r$ b5 H8 i" |& o6 @#define SHAER_RAM_SIZE (0x20000)
; ]- E' w! G2 c" `
& B# b/ u8 F/ A( ftypedef struct# d. b: k+ ~* x6 N. W- r; g/ I
{) r+ F2 T9 G p& Y v' b. }
unsigned int a;: H6 z% j2 e5 t, U* `- e X
unsigned int b;, E9 P; ~$ L6 ]% X$ E
unsigned int packet_cout;' X6 q/ ^) c& p0 o5 P" S1 ^
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
) f# l0 [& E' V1 i* _! M' B2 Z# e7 \( n' n' w6 k* M( i
void read_MSG_buffer(int *baseaddr);% i8 ]: l2 P7 `6 e1 q
unsigned int count_copy = 0;
( d# N+ I# D- n
6 [+ }1 @' s; N% {int main()
. b" g, j! ?4 |/ }; W{
$ ?; E- q! F8 m' D- i* F0 l2 y* i int fd;( S0 S f% q# E/ e& ^
int *mem = NULL;
! Y- \# L8 K4 _6 A7 \8 c( u @7 A6 k
if((fd = open("/dev/mem", O_RDWR)) <0)2 N( L. X# w+ a# [- x% p
{: P. b3 C4 V/ y8 p- V
perror("open error");' h: a4 ?3 ]0 w5 Y G
return -1;$ H4 D! {& r' ]8 e/ `, `
}
) w: U5 ?0 o% a3 e6 ?- I
5 } g: y [+ W* K) B( _% X mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);8 d5 E Z& j1 s& ]4 V/ a6 R0 e
& V, g$ n4 ?6 Y/ M h9 ~
while(1)
1 G s6 L: s, o' B# ` {
; G' {4 f5 J0 e- Q" s read_MSG_buffer(mem);
" P: U# |. I5 ~* w+ V }
" t, U: G! s" y' D C}
3 X/ \2 J# w) |: R* j& W* G" X8 G w$ R
void read_MSG_buffer(int *baseaddr)
* `' H+ r& B6 `# c{
0 p; f* o, @" J# x5 ` pRX_MSG_PROTOCOL pshreRAM = NULL;
5 w( N: j' B# f0 N
% Z @1 w3 b# s: L. h8 p pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;+ `3 b8 n/ h( @. Y6 l' O/ o
2 ~! u, @; G) R. I: L) T6 X
if(pshreRAM->packet_cout != count_copy); m+ w% b2 t# t* W' S* X8 b/ p8 e
{
; s# V. U8 _) e9 Q% q } printf("a is %d\n", pshreRAM->a);
' C' y" f% D: j _) u" N( |8 t printf("b is %d\n", pshreRAM->b);
* c6 z, ^2 d+ r5 P printf("count is %d\n", pshreRAM->packet_cout);0 h7 x$ p9 j9 n- ~9 G
count_copy = pshreRAM->packet_cout;
0 E" y- k8 v) m }" t; |6 W1 \( o) C: i7 }
else
1 ^4 U: ?/ _$ g( \. [" c {
5 `- [) M, U8 u1 G printf("No effective message!\n");
- m# a; U4 l7 Y }
1 B- R ~2 L/ c% u}8 ^& w9 v7 p5 \+ t9 d' ~
& T( _# |( L7 E v k7 w/ x4 [没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现??? r( H2 Q0 R# _" w9 T1 T8 J2 g8 I2 u
" b% B4 N: X2 x0 w2 `+ c
) X6 P- d- N/ J6 T7 _' z; G4 O$ e, \9 j' u4 t
1 y# ~. J* s$ P! L# S) c |
|