|
|
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑 ) F6 I& L$ F0 |3 j; t# L7 K
+ a$ ?5 |. d' a3 f. G( E, d( ]) q
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>3 O5 T& K4 Y9 E, {' ^7 e, Q
#include <unistd.h>
, P* R% O4 T# I#include <sys/mman.h>) a9 q+ I! ?& S8 p. @' G
#include <sys/types.h>
, @4 [6 r9 Q; Q#include <fcntl.h>! F! d# y8 c! v$ c4 F" r7 d% t
0 Y- F" [& ]$ u% P" U4 W
#define SHAER_RAM_BASE_ADDR (0x80000000)
8 ?/ g5 o0 F" T- e c# y; e/ ? ^, u! w9 {
typedef struct+ A9 r! P+ i6 Z- P' e
{
+ Z: @9 v- G( @2 W- e) Y unsigned int a;
6 e& U0 [6 M+ p$ c8 d5 ~ unsigned int b;
5 r' I1 R- ~/ w! J4 F unsigned int packet_cout;1 C3 b6 F9 Z5 S) a5 Q" N
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
" d3 \0 x7 z# a0 i0 ~
5 e8 y K7 _$ h1 Z1 q `void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
3 b% F9 T/ Z, p7 r/ o9 x r0 Yunsigned int count_copy = 0;
" [" K6 |# G. V. @6 F8 x. @4 U3 B% |$ o/ ?: c+ j3 m1 _
t+ n: I% L. k8 N! A: t9 j6 Q
int main(); Z: @4 A5 y1 d/ d
{& U4 ~- {: ]* F S/ c
pRX_MSG_PROTOCOL pshreRAM = NULL;
! ?$ g8 Z& o( t ~! B3 D- h pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;
: `4 Z% @% p, V$ A+ k3 q' |. q/ D3 L
while(1)- S6 F* F3 l/ d$ P( Y1 _% f; W
{7 X5 f- \3 |' \6 C; ~9 v' S: L
read_MSG_buffer(pshreRAM);. n: T% c* q- ?: M
}
4 w( m- \2 p( }2 c7 Z9 L}/ |6 [- X$ n( w* I- l
( u6 r. t( ^( f& w1 @* N0 J
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
5 @7 q% m" ?# }# e, q- k. y/ J. G{2 [, D8 U- h: }5 ~( J; f
RX_MSG_PROTOCOL buf;1 V1 j; m+ J! O& F2 E2 W% ?
9 j% h4 ?5 ]% p
buf.a = pshreRAM->a;" ]- ^& s7 V# j/ H) S1 p8 {
buf.b = pshreRAM->b;+ Y e8 [* K1 G5 b
buf.packet_cout = pshreRAM->packet_cout;
/ O1 V% {% j2 T" T& c, i( R' A # S/ @* F( {3 `1 R: p
if(buf.packet_cout != count_copy)
( x Y- M2 p1 o- C8 s7 J, e {
% U% A0 c3 M& \: S1 w) P9 A3 K printf("a is %d\n", buf.a);
: m% E: Z+ _6 q9 [1 p6 H" @2 p printf("b is %d\n", buf.b);
( ?6 F0 n8 |" r. ]; b. I2 j: S printf("count is %d\n", buf.packet_cout);# b0 n z+ O: Z1 ]/ c+ T
count_copy = buf.packet_cout;
- _) S% h. z$ K) O& _9 U8 m }% K* O. U& y4 X6 j. V1 X4 d
else1 N5 `+ P, M+ L
{
; R/ n# S& T8 S- e* f, @1 o printf("No effective message!");5 ~$ H/ x3 X% E% r
}, K4 \, ^/ h1 \5 D
}
' A/ P% _: R2 v; T! S: i9 C
) A2 s$ x" O. m0 _0 `
; |* X; R2 { s L但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。7 \) [! b( h* }' ?6 d/ ~' G
使用下面代码,对内存使用了mmap函数后:
, y E. {) i' N( f k#include <stdio.h>0 H* ~9 e) H2 B" e/ r+ j! \4 t
#include <unistd.h>
2 b, Q/ U1 E/ y3 _! l! Y {; |) l#include <sys/mman.h>
. Q# P& s0 [3 D3 W#include <sys/types.h>3 z) |% [( W9 e' o0 Q
#include <fcntl.h>
2 C2 A5 \% ?3 X, A; V/ b
. ?6 C W5 o' Y: r! h2 z#define SHAER_RAM_BASE_ADDR (0x80000000)6 d- ]2 }9 J/ d7 f
#define SHAER_RAM_SIZE (0x20000)
/ z9 S0 _. e2 G6 S% Y
9 g+ F/ S# L) \7 utypedef struct
6 s8 ^( ]4 U1 ~% I! V{
3 W) g4 `6 r) f9 G. ? unsigned int a;
8 o! n& M4 s7 v' G unsigned int b;
( T. ^$ ]& E% a unsigned int packet_cout;
2 r3 S$ R5 o0 J; `6 N}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;% C1 V5 a2 m& [7 i0 y
# u5 b, F! p3 J* G$ b; i5 ?1 ]+ ?void read_MSG_buffer(int *baseaddr);
5 l- w& k4 Z5 `unsigned int count_copy = 0;$ H- E0 S( P2 I; `0 k
. @+ [. `( r* \& Z; f# {int main()
) m& l/ U" r! i2 w* d{
/ R! J& O# Y% c$ m5 Z int fd;
6 q% h; c2 k, N; r3 @ int *mem = NULL;# x; O! w- |5 b% I3 |
* Z2 X- ~* h' q1 f2 l: \" {- b, [ if((fd = open("/dev/mem", O_RDWR)) <0)' d8 y0 X- y. H/ D" K0 p
{
; O8 D4 J9 R. \$ i3 U0 { perror("open error");
0 R0 |. o+ e" i# E# d* k v4 R2 r, L return -1;1 h8 }1 j0 D- f5 z
}" P* _! I; A8 v/ x( \. m
: |$ L* F! j+ x( z+ D9 W- ]2 a4 U
mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);) E; g- C# L {: O. B* S
6 M/ A1 X. {* D( e5 ^$ V, ]8 T while(1)7 ?5 P7 F# G9 M# Q" R
{: r9 {. z; J" n+ ]/ x6 {
read_MSG_buffer(mem);: Z5 }+ ^# f" M. h+ W: i
} & G8 M7 W4 Q7 H, t( r3 \+ G
}% J2 V- f+ u' X! {$ j/ j
/ @" B, l9 k e% Ovoid read_MSG_buffer(int *baseaddr)0 C1 p q7 I1 w1 v6 O
{
; q+ G3 l& n) u4 I pRX_MSG_PROTOCOL pshreRAM = NULL;
" [ W9 z: P5 g) M# F2 V9 r: T
2 L* I: w0 S2 n* D6 X( g pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;+ L4 V0 q- _" d* y; R
! N* L- W9 F' e/ ~' @( @ if(pshreRAM->packet_cout != count_copy)+ \ B. I) i7 u4 ^% h0 k& M
{
: t4 N0 E$ ^5 K printf("a is %d\n", pshreRAM->a);
; x# m4 S! Z1 }1 U* _1 j! @3 p printf("b is %d\n", pshreRAM->b);
7 I, O/ |3 S1 _ printf("count is %d\n", pshreRAM->packet_cout);
. K* n. j: O& D# H& W. w( D count_copy = pshreRAM->packet_cout;
( E% i* e1 M/ P* v }
# j+ `% ]# t; @* p( r" C6 _& { else
" }# m0 a7 |: L6 u# h3 `& b) R4 k+ i {& t! h$ ]* k: U6 N+ L) T
printf("No effective message!\n");
1 G$ t1 `' P; f( p3 X/ e }
+ Y- ~2 ?% a6 z" q* U}# n8 D4 U" K2 S# e
# U: g4 P7 j2 h% \& b: A6 h没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???
1 S9 }8 e- k3 Z2 v% ^; t( `1 j, v9 ~
2 t: s4 S; `6 b% ?. w. M
' b: \; |3 u/ c3 Q
" m# p3 _3 w! R2 G/ b |
|