嵌入式开发者社区

标题: OMAPL138的内存映射是否在Linux下已经完成??? [打印本页]

作者: zc_fly    时间: 2014-8-19 16:38
标题: OMAPL138的内存映射是否在Linux下已经完成???
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑 " w3 \7 ~( A9 d. _
! L7 @( R# P* I0 n, M- H
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>! E' @4 r2 c, f7 e' U
#include <unistd.h>
  b* G3 r/ p" D/ h! a5 K& f#include <sys/mman.h>5 d1 s+ B2 v) t2 @& x7 |+ N
#include <sys/types.h>4 m" ^1 [% T: V" b' S
#include <fcntl.h>* T" Z8 A: }) Q; C6 m) h* Z2 F

7 j' T+ E5 C/ h3 Y! b6 w* ~+ W#define SHAER_RAM_BASE_ADDR    (0x80000000)   
3 P; D. p) H* v- q9 m
# M" u% p7 f/ ^9 [. G) r: Y; a9 t) Mtypedef struct/ G/ r* A2 t2 E4 i5 c! T
{8 g6 H2 c/ ^5 N4 ?' z
        unsigned int a;
4 g1 A( }% W& i0 G8 `. p8 q        unsigned int b;. O2 f/ r+ }  s0 ?6 `/ t5 t
        unsigned int packet_cout;: }; q" D' o9 E" }& `# `! [0 W
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
4 ^# L, k9 \, ], h7 H4 \& {! |/ `! y' Y
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
9 n1 o' `2 E# Z0 Q9 punsigned int count_copy = 0;8 v% }3 A! I6 N$ ~* G

: n7 V! D1 E7 w+ c8 `) i# W9 [: J; o/ J0 X
int main()
- z. n3 s; z( A# u+ {7 R{
3 G! r( D  Z# o: d' h1 ?! x  P' S        pRX_MSG_PROTOCOL pshreRAM = NULL;
* b7 T+ Q2 O6 ]: _) }- f; F  Y        pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;
! F4 f, ~; {6 G" |$ \
+ @# H+ h8 B. z4 n        while(1); \* ]; K$ y. N. s7 t2 {: S, b* }
        {
# `) e2 z8 j/ t1 ]                read_MSG_buffer(pshreRAM);9 |. g. s. D: M# C3 b+ A6 {( i! z0 N
        }               
* n! O7 s/ n) k, N7 B: m}! M0 y4 J0 \3 A7 n
4 U1 o% @9 y/ T/ y
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
/ @% [" k9 g; J  T" n{
2 a* w7 y% d  u( B  q1 n# l; o        RX_MSG_PROTOCOL buf;7 X; H- q/ e" x, O" H
        
4 f0 i0 _+ f5 C" O3 n        buf.a = pshreRAM->a;, m. Q1 O$ _1 ]1 [8 }' c9 I
        buf.b = pshreRAM->b;2 }7 O% n: l( Y3 \' m! ?4 }
        buf.packet_cout = pshreRAM->packet_cout;
7 Z" f% @1 w3 Y9 e% A% k- l! I& d+ n        $ C4 J3 Q& H! ^, e
        if(buf.packet_cout != count_copy)6 c# W( o( f, N
        {
( a: h  m- T$ C3 D; G! A9 p                printf("a is %d\n", buf.a);
1 b9 _" X2 i( s" x                printf("b is %d\n", buf.b);
% W: F+ b4 r, w  G+ r                printf("count is %d\n", buf.packet_cout);$ P6 V- L' _. P  i- m
                count_copy = buf.packet_cout;5 B! d5 B+ t3 Q: X4 A
        }
1 v( \8 q, s" N: h4 W0 H        else1 Q8 d% W' X+ O# D2 J1 ~" Q
        {3 y8 {, H$ ]" t" l  w9 ]4 \, K
                printf("No effective message!");
1 c( y! c2 Y4 V. W        }& J/ M+ T9 ]# Z& S9 O  E/ c2 B
}/ d# O# ]3 E3 a$ y

4 x) i. [6 {/ h6 _; [( T
- v" U4 F4 m& b但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。$ h6 t8 Z: x; i$ M5 Z2 G
使用下面代码,对内存使用了mmap函数后:5 Q9 H  U$ V! J! i" z4 [) Z% S
#include <stdio.h>2 w1 x8 a; g  A/ ]' v& s
#include <unistd.h>
' l, F' u" a0 n! ]5 L4 h  W; f#include <sys/mman.h>
7 f% R9 `( T: t( f( ~3 Z#include <sys/types.h>' J9 @8 P9 i# L! i5 Z( A! Y! x
#include <fcntl.h>7 f. O- ?8 S3 q7 _; L! e

, {# p; @! \0 V7 ~8 t" w3 M% E#define SHAER_RAM_BASE_ADDR    (0x80000000)
6 r9 l; D' E% H- c# }#define SHAER_RAM_SIZE         (0x20000)   & S; ?& y. s% h# l7 t# _0 d
* L; Z1 x* U8 Y( x' o5 Q
typedef struct: V- c9 _' k3 c. X% w" U, Y! d
{$ A9 b2 e9 [) F2 G
        unsigned int a;& w9 f' y2 i: R; {' i# j
        unsigned int b;
; I4 n6 \9 d6 N  G        unsigned int packet_cout;
$ w# Y$ Z/ ~, t9 C/ s- p  {}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
) H) U4 S8 b9 i& B5 {5 a4 e0 L% }0 d8 F7 Q  L
void read_MSG_buffer(int *baseaddr);
" m7 G) H6 j) t+ M5 x' q: {unsigned int count_copy = 0;
/ d, L3 f. b  {4 Z
- B2 o: F+ e1 d: Hint main()6 K1 M: g8 {4 |* i" v6 {
{- r  m4 n8 d) \" Z9 ?
        int fd;
# _- u4 Q5 t9 E: ]% G, V        int *mem = NULL;
6 c/ v# h4 T( [8 C# v4 e& Y- Z; K. r7 M, \) h5 F' Z
        if((fd = open("/dev/mem", O_RDWR)) <0)0 J1 Z6 ^/ @* k- V' @
        {% f$ v8 x- P+ W: ?% Z
                perror("open error");
. _) I) M# g% P                return -1;
3 i1 \  H5 o  c        }/ l  h3 a2 G* U) N
        5 G0 ~7 z9 c$ A% ?
        mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);- Q4 a( e0 ?$ l" r0 S( ?7 p$ Z+ ?
- E5 z; l0 t3 I4 ^9 u
        while(1)7 c+ {1 \0 ?  L% D' `
        {  R' Q5 E' @% G# A2 ]6 i7 ~- G
                read_MSG_buffer(mem);2 F# C* A6 A. n; a5 C( K( V4 R4 q
        }               
1 n9 O8 ?& i8 A: f" n}
: @  l& o" @  o% J
$ @. y% I! U9 ~4 P6 Z( Hvoid read_MSG_buffer(int *baseaddr)8 _5 K3 _1 F. ~4 A2 F) V2 |
{
; Z6 A, w' y* ]' R( A6 m        pRX_MSG_PROTOCOL pshreRAM = NULL;
! x1 ~2 L' x; |7 m& w9 s3 i  F3 m# H3 Z( N8 T- T0 C
        pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;- a" Y% k, g6 ?7 g7 E2 Z

0 |7 R7 [, F9 [0 B. ]& N  q        if(pshreRAM->packet_cout != count_copy)2 s$ m- r, O+ y' m) v5 P3 M$ C: g
        {
# `2 O5 }6 l6 ]! Y                printf("a is %d\n", pshreRAM->a);8 b+ t% D+ Y! J  A4 V
                printf("b is %d\n", pshreRAM->b);3 R# \" {2 f. d! c  H
                printf("count is %d\n", pshreRAM->packet_cout);
. d# G: t1 w8 Z9 H4 w: P                count_copy = pshreRAM->packet_cout;
; B, ^! D: ?8 b& x* A        }0 t& s. X$ N( v: ]7 W7 V6 P+ `1 A
        else
/ ]. r. s% U& j& @% R8 W, f        {, L4 d3 \0 U: G
                printf("No effective message!\n");, T: f5 j4 N+ F* _  {& f7 ?6 M1 p
        }
, b! y0 X3 w2 o9 }6 ^: w}
; |2 E2 j$ t. N: z- ^
8 f& h  ^+ }" e7 p, ~没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???- H; {7 b* R/ F3 S

+ W. ]" S1 U! L2 R# L
- t/ ]2 y, y1 n) c* B- T% h' {, w! Q4 p
+ Y' `% n! j4 l  j: X6 C





欢迎光临 嵌入式开发者社区 (https://51ele.net/) Powered by Discuz! X3.4