嵌入式开发者社区

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

作者: zc_fly    时间: 2014-8-19 16:38
标题: OMAPL138的内存映射是否在Linux下已经完成???
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
4 b7 I9 y+ N8 l' k, |6 n6 m
& A' i4 h( x6 t- }9 S8 LOMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
! ~8 |. X4 e( J" g$ `: b& D#include <unistd.h>
' |) ~5 c  j9 i( f+ I( w$ _#include <sys/mman.h>4 v( ]' d$ e. z* u" k& W
#include <sys/types.h>
8 R( |$ ]) o! s+ o- V#include <fcntl.h>9 I% c5 [2 I# [

" ^& Y3 w5 J1 z' K+ e# ^#define SHAER_RAM_BASE_ADDR    (0x80000000)   ; j2 T0 Z4 G. }# z5 t9 z

: Y5 n6 O" W/ A( _typedef struct
+ Z' i1 b9 o6 O! P+ b. F{# z5 B9 l7 h- U3 I
        unsigned int a;
2 C7 m2 v+ E% D, D; g2 x; B        unsigned int b;
* e# s" ^8 n: W# R% i0 s        unsigned int packet_cout;1 p2 c  W. L5 Q2 |+ V
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;! j+ @  o3 B0 F
# p# y: K2 S, |5 t
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
4 D1 H$ ~* r& c: qunsigned int count_copy = 0;
$ M8 L& p- N1 I) m2 n6 W9 t! C  b0 }9 D5 b

5 B" M: f0 k2 n: t: I9 x$ Lint main()1 F! W% q3 K7 L  ?: s( H0 v
{4 r, L- e4 U6 N5 v* V% W, h2 V9 w# j
        pRX_MSG_PROTOCOL pshreRAM = NULL;
) ]1 O! c9 s+ d4 r3 a9 L2 p        pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;
" B* F1 C' R( p. z. ?2 C  Y& U( b) J8 h0 _3 L
        while(1)" O  ], d+ \& n& V8 u! V2 c' s
        {
# F% U% V+ d  v: n6 c                read_MSG_buffer(pshreRAM);
( O4 a- @% _9 M; M& B( f+ A        }                ! Z# m* @  k; j! u' `0 M
}
1 c3 x/ n! u* `1 e2 K/ u# F6 s* q% ?5 g  L, F8 u2 e
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
+ |$ [, N8 U, b1 f6 V{
9 i2 ?7 Q+ p* J& t6 j        RX_MSG_PROTOCOL buf;$ @% K/ |  ?/ g9 ]6 n" r
        
4 O0 e  ^: q; \# r9 }$ z        buf.a = pshreRAM->a;
5 B  \! o3 c" e' b- J        buf.b = pshreRAM->b;0 K* X) Q0 u! Z! G9 D' @
        buf.packet_cout = pshreRAM->packet_cout;
( V) k/ m1 I! Z7 }6 N4 P6 I        * r" M: E* @; l9 Z
        if(buf.packet_cout != count_copy)+ R& ]: D4 Z$ I+ V
        {
( K8 O  w: c- y& P/ v; r% G                printf("a is %d\n", buf.a);: g7 O  f) S$ g3 a
                printf("b is %d\n", buf.b);
8 V/ s0 c9 s' c0 P0 M                printf("count is %d\n", buf.packet_cout);
* U7 y( _  R, `: {6 ?. o                count_copy = buf.packet_cout;
' d: I) s( b8 S% X8 J' B        }
) h+ ], P' [! g% b* `        else7 x2 o+ v5 U  N& D0 ^; Q
        {' S& ]$ s5 T+ _6 s2 r3 r
                printf("No effective message!");
9 e( N% ~: h9 i& L        }
7 K# O( `  L  r% M; P1 `}
9 x+ F; v$ Z/ `  R! N. D& K
3 |# O5 S5 L3 e/ M* S& Y
7 ^$ ~( C' u1 `: f7 A但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。& y1 j, S1 |  p3 R+ t6 P. Y, C
使用下面代码,对内存使用了mmap函数后:
, K) t, N# w3 E0 T$ V2 |#include <stdio.h># G. r1 u# a- e' z
#include <unistd.h>, Q: E1 P+ z% d
#include <sys/mman.h>! R4 m& E- ^5 L$ x
#include <sys/types.h>  O; o* l. F; D
#include <fcntl.h>+ R: ?* L1 y; f7 N, T

4 `9 p: s8 }4 ~$ X4 W* C8 h#define SHAER_RAM_BASE_ADDR    (0x80000000)
- Y- u* j6 }; X#define SHAER_RAM_SIZE         (0x20000)   8 h2 S, i: n7 B  s
' L, `. q! F$ R0 ?: V4 t' \/ B
typedef struct0 A% n3 Q9 O; }; o
{
/ _  }3 `2 k, G        unsigned int a;
% M/ l1 n5 _% @9 p( _4 z7 h        unsigned int b;% T* E4 g& _4 ~( M/ \( v% c3 h" Y
        unsigned int packet_cout;
8 D( Q! h1 k% p% Y- U}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
9 F/ L' C% W2 s% C5 ]
1 \: o3 q, m$ {void read_MSG_buffer(int *baseaddr);
! I2 P" t4 ^* N7 Aunsigned int count_copy = 0;  C  V) p0 U( g0 q" {
" m4 o& u0 ^7 ]) y9 w% Y9 Z( p8 n  x/ V
int main()" X) m* ^' U8 A! c! m% Y4 B% {
{
$ F6 H% ^2 d) r# V        int fd;* {* E; @: t: e, _5 I' L8 u3 I
        int *mem = NULL;0 z8 Z; R/ l6 V, A% D
2 t. L2 _# c3 f) ~( X
        if((fd = open("/dev/mem", O_RDWR)) <0)
" z7 b9 H5 d0 h$ z6 x, S, y, g# B        {
# n9 ?2 j) w2 v4 m! \2 j! F                perror("open error");+ J3 q" X5 ]) h9 Z+ t
                return -1;
( F' [8 G/ @: L& q        }
# I2 d+ U( o+ E2 U6 j" _: C* ~        
$ e' g: x2 y3 p2 u: c0 X! Q/ l        mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);% U" u7 q1 ?4 s, ?1 q: z

. U( V4 w) D9 N        while(1)
- m/ L. P, i8 f6 f) f" ]% B. ^        {( e7 y. u/ o# N
                read_MSG_buffer(mem);8 k2 O) M! P6 `; F0 d
        }               
9 W9 Y9 p6 S1 j3 `/ _}
4 I$ A) ^# s, }5 u/ ]) D* ]# l2 D
void read_MSG_buffer(int *baseaddr)
' W; U) Z9 [6 R0 M{
$ C9 R& P+ ~" M+ S        pRX_MSG_PROTOCOL pshreRAM = NULL;
& S1 H* ~0 c/ {/ x: M( f
0 a# M! ?) z! \  _6 m, A7 @# A        pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;9 @1 p. w# X- f  \
) o, B: R$ ^6 a1 F1 K( P2 T, W
        if(pshreRAM->packet_cout != count_copy)# x, {# A9 G2 Y3 i
        {
5 l7 G8 l2 j) w7 [0 `                printf("a is %d\n", pshreRAM->a);
; G" P" I+ a& M! C: E; ^' r9 |5 G; _                printf("b is %d\n", pshreRAM->b);, ?8 S) X& Z7 A9 a1 M; O; w
                printf("count is %d\n", pshreRAM->packet_cout);. h! {5 e) J& Q0 R$ z. a! s. F0 }$ ]
                count_copy = pshreRAM->packet_cout;
* N9 ^( B4 x9 x        }) K2 B0 @( Z/ P7 m, m3 t! ]
        else
. K" Y+ p1 x* P5 b0 x4 b5 Y        {+ D. ^1 R  w9 S( ~: i
                printf("No effective message!\n");6 E1 Q7 u* E0 |% V6 H& }
        }3 J) T1 g$ D0 F6 E9 L1 F: ~
}+ R% x& v. q& K. i) ^. v
$ `- v# N& L) u3 l! A9 @0 E
没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???( |1 L! s" T# b
7 `9 d- C5 Z8 M: O7 l

" ]: d- Y* T4 b- g6 r
$ S1 x( L5 B1 N2 u1 `! Z( L4 ]; Y* F- H9 d# C) W





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