|
|
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
9 E. T& ]* t4 U$ `: `9 D
0 R0 x2 N7 `6 UOMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
Q8 k H6 [1 Z; x; W W#include <unistd.h>
4 O: v$ Z- n! U( j1 \/ e7 n#include <sys/mman.h>; [5 q# H& Y# f( Z$ U1 T# @
#include <sys/types.h>
2 X9 S3 j& g$ N- X! c#include <fcntl.h>
/ y w5 x0 I; J5 c6 S8 y; B% u/ q% Z6 ^# ^
#define SHAER_RAM_BASE_ADDR (0x80000000) - \2 ~2 d {: _6 t3 W5 x7 I! [
0 |( _9 z7 M0 F: t+ f0 l: ~
typedef struct
+ ~' M7 x2 Y+ t' `{7 z( c. o" F7 c
unsigned int a;# B9 c, h: n; k% @* K8 |; v5 i
unsigned int b;
, a- V7 b/ J% M$ `# |5 |) m unsigned int packet_cout;0 r1 q7 _6 I( {7 e
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
. }0 T4 R/ c. I3 e
7 x$ @+ b+ w( }) fvoid read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
8 E0 G z+ H! r& `1 Ounsigned int count_copy = 0;
8 R+ _5 C4 ^. C) I0 {% i3 n
9 W! T9 [6 y. |" f- ~' W
& ~9 q- o" \" s; p" ?4 ~- m& Lint main()
; z* S, L' k' F( ^( R{
; l3 n6 `6 U% u% l. D y0 j pRX_MSG_PROTOCOL pshreRAM = NULL;$ j) @5 e8 c% s. _: ?
pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;' P9 Y6 h6 F8 s# L+ v& I) G
. g8 O" I+ b1 B! M7 q5 h6 i
while(1), |% ]' Q8 h2 [; V8 r
{, w& Y$ s! z) c1 \
read_MSG_buffer(pshreRAM);
# N) Y$ S/ E. N. w. z4 q }
: g" P2 D! x9 ?! b; w+ U m+ L8 M}& ~7 g: L( B. G
& q7 s% a5 t, Q# Lvoid read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
1 L4 q# I: F2 P5 G+ o; ]5 J6 O{
G! Q+ d- c! N7 L* c# Z& U RX_MSG_PROTOCOL buf;0 w7 l' C: r C! O3 g
6 p8 ~1 z( {- V+ U) z6 t* G
buf.a = pshreRAM->a;
4 H# f" ?' H5 s( v4 \# W% X buf.b = pshreRAM->b;; q9 X6 e0 \5 i/ k
buf.packet_cout = pshreRAM->packet_cout;
! g- l' J+ |' e# y+ W o# O3 y 3 P! a3 s) n# B- V! X1 X @
if(buf.packet_cout != count_copy)8 R! X7 a' ?: O* g: l
{
5 [0 j4 R! k4 _7 Z _ printf("a is %d\n", buf.a);
; J8 }% s9 ~) b/ \ printf("b is %d\n", buf.b);
: g0 @( A: y! P. ` printf("count is %d\n", buf.packet_cout);# r9 E% \5 r+ V
count_copy = buf.packet_cout;
9 r& E: v* {' z/ L; x+ _$ u }
0 t9 j) [5 M. q7 ^' w) V0 j' `7 k else
& ?, ~9 t7 i4 Z5 p7 s {- z$ U1 t) J/ G" G: G
printf("No effective message!");
: Y* a0 v1 X3 P! W* O( e }$ R+ I* K% A5 t9 O: c& N
}
v9 O( m% ]! G+ N; `! {. U. R7 T% [
' G1 r3 J2 d: A4 B: h7 L& K( g' ?# z* R% M# z1 C" e
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。 e0 V5 w$ k" h; O* E1 X& S
使用下面代码,对内存使用了mmap函数后:6 w( ?& }* y8 X# v0 {! J
#include <stdio.h>6 [. k1 `" g" g8 n1 ]% w1 s
#include <unistd.h>
2 f/ d; V) y) y- A#include <sys/mman.h>
: \+ g, f6 d* m. x) i7 i6 c1 @#include <sys/types.h>5 x" r, g! O% }% A, ?5 [$ H+ R
#include <fcntl.h>
8 H6 s ]/ Q6 X- u
8 ?8 S- |$ j: p0 u. w% h; q#define SHAER_RAM_BASE_ADDR (0x80000000)
. ` o" A# e' J5 z2 F#define SHAER_RAM_SIZE (0x20000)
: y/ Q0 a/ C2 Y0 k1 I2 m1 x/ s" z* [+ a9 S
typedef struct4 N" b+ r9 y$ x0 Y @! ~$ ]
{3 [- t' s1 k8 n( f" c5 h5 l
unsigned int a;
& s4 J5 D+ H. L2 d1 l7 j# ]3 _0 B unsigned int b;
* J9 W" R5 k& C3 z/ ] unsigned int packet_cout;; r9 J- @, f. `0 V# w, x) B
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
4 X5 V* H7 v z" q# A
8 p/ j# x1 \8 ?4 q4 ?6 dvoid read_MSG_buffer(int *baseaddr);# }% K6 C2 i% b4 j& }
unsigned int count_copy = 0;
1 @% z% c9 @; K( w( b- ]& {) E4 P) c V
int main()4 A" d5 b; f# v: g# b
{
- J' ]5 I. d8 @! k. y" z int fd;
: w5 q, f) X% Z/ C4 V int *mem = NULL;2 \; y! W, A6 F% R/ U. v) h
* W( T& D, S! x6 e* F( Z; z' {
if((fd = open("/dev/mem", O_RDWR)) <0)
0 }0 n. `5 M7 f1 m {
( ^2 v% m$ r! g perror("open error");
) L* n* e( r$ u- m$ A: W return -1;
. q7 n; ~# x+ U* O! H }
' _8 J9 f2 e6 d9 v
: @' @0 X$ }3 E, f8 ?0 U mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);) I1 G. ]+ i: a% u7 w
4 d8 F7 O5 N$ q5 S# m: d5 z- b5 o while(1)( Z% L7 E: `9 L6 K- V" i E3 m, J
{1 _% b2 V! I2 k$ c4 H( M# y
read_MSG_buffer(mem);
: y4 h. S" V9 i* {/ s } ( i& D6 z$ a3 s* \3 b+ K
}+ u, e2 d. }" Y: Q3 V
% l+ |" U3 H9 X+ Y# O; gvoid read_MSG_buffer(int *baseaddr)
! f Q6 k6 L$ U3 d{
, L5 N) [, G) j pRX_MSG_PROTOCOL pshreRAM = NULL;
0 G- f+ w; O5 y" J- G& @5 s6 |+ J$ Z2 h, z/ p5 t# D6 x
pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;, S6 w: ~" J/ H3 M: p
# L" P, _( y6 I- X3 _: P( S# V if(pshreRAM->packet_cout != count_copy)" t. Z! K' F2 R' `1 H' G
{8 G1 b! ?/ }3 r1 E X
printf("a is %d\n", pshreRAM->a);
* u4 F; l4 i5 |9 q printf("b is %d\n", pshreRAM->b);- u' w* R. [" q( \. f5 u t
printf("count is %d\n", pshreRAM->packet_cout);/ \. t- _6 o8 F# M5 l! M
count_copy = pshreRAM->packet_cout; A( n, X! w$ k7 v. U
}+ l) _( a9 T& \% B' |
else2 f. K" l9 n: G! ~& {
{
1 ]1 |' g2 J0 y: w5 [' ~ printf("No effective message!\n");+ @* W" \6 c8 h# r' n7 S
}4 ?$ v) e, w1 G+ l% C @3 i7 ~
}* h3 \" X+ w. J# w! U
}! i2 P, h/ M+ e( q6 I P没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???; {' ^: J+ p2 ^4 J& Z1 f
. l6 }1 X( J" @/ [/ @2 Z5 T! h8 j0 ]$ Z5 q6 |
9 L4 r- f5 y3 ]& z5 i: p4 } j1 _% Y1 y g7 ?7 d
|
|