嵌入式开发者社区

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

作者: zc_fly    时间: 2014-8-19 16:38
标题: OMAPL138的内存映射是否在Linux下已经完成???
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑 ; [( W7 V2 F- v5 A
6 ?9 \0 w! a2 l
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>) @4 K( n2 H2 g
#include <unistd.h>, ~/ B9 t5 _% m1 |3 M, N0 x# B7 g
#include <sys/mman.h>
; p3 O2 X" Y+ G#include <sys/types.h>
9 A# H: q- e* A+ g#include <fcntl.h>& S9 _& q4 E  l$ {  b: ]) O# i
' O# \$ H% H0 Q' E& u0 ~3 e, M. O8 q
#define SHAER_RAM_BASE_ADDR    (0x80000000)   5 u& \( B$ b! b* K8 o, {  l; ]

, O7 y, B8 f( u6 T+ Mtypedef struct2 _$ @9 i' g- L, T1 @4 u
{& R& I# E# `1 _! Q+ l+ P# S, S
        unsigned int a;$ q+ ^  V. [2 {& t4 ]/ n) D
        unsigned int b;
2 V, }* T3 m" X0 H" e        unsigned int packet_cout;: x7 ?5 @: `5 i( ?& F, s& |
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;. O9 p2 i% v# R( E: t

0 b6 p2 |/ M( \+ _void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
! G0 ~1 w/ e. [* n+ m3 zunsigned int count_copy = 0;- _" n6 D- R. w+ ~7 T
, k+ V& b1 a! @% i, P  l1 u1 R: u

: O8 [" \$ V1 e: S) z) Dint main()! ]: K2 y$ I3 ?3 B8 _
{
+ v. J. r% d2 C) y$ v2 ]! U( ?        pRX_MSG_PROTOCOL pshreRAM = NULL;
2 `' {6 X0 F- e6 k+ _        pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;
: x0 M' _. u7 y, q) J3 _- U, ~% J0 J! ^$ b. U
        while(1)$ W& w* ^9 B- j2 l/ c
        {
; X9 [3 K( M, c5 w5 R                read_MSG_buffer(pshreRAM);
: k2 A; O/ }6 ~( b        }                # {! _. |. ^$ J; M
}( H3 B* J2 e8 O  W

0 h: P: H3 S7 B* y5 N& @void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
' M, N+ ~* [! i+ _0 z& u6 k' V{" r% k* r* I- v) s( Q) O. \
        RX_MSG_PROTOCOL buf;( C- j1 T% m9 G5 G. q
        % m  N% W  z' }( r: w4 H
        buf.a = pshreRAM->a;
& {3 P  m" e: P1 q        buf.b = pshreRAM->b;
. C$ f6 x0 ]$ z4 g4 g% Q& O        buf.packet_cout = pshreRAM->packet_cout;
7 m4 q# s6 M6 S8 j$ ]        5 g( a# F- O3 Y3 k% k0 w. l2 U8 J
        if(buf.packet_cout != count_copy)! M, b! b5 r* S2 a- V; e# u
        {
  {+ k5 N& ]  @  P4 V" o                printf("a is %d\n", buf.a);+ j) c0 y+ _# U
                printf("b is %d\n", buf.b);; }; c, P/ E$ z  @. K! \4 }6 I: b* [# U
                printf("count is %d\n", buf.packet_cout);
+ }' T# ]% G+ j- h                count_copy = buf.packet_cout;( l6 X& B  t9 I/ X7 f
        }/ n$ e5 g9 y$ R1 W, x7 V) _
        else& l; m1 e3 y6 v/ q/ n+ W. Y
        {
- `  f. X9 @6 A+ P( ?+ `                printf("No effective message!");
* |& q  [0 E; Q/ A: U+ R        }6 o$ g% Q1 Y& j8 C% J1 v
}
, P! ?7 F% `  I0 c0 K! a
$ i. ^' T8 |' v1 A5 R- h% q3 G* O9 R6 v
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。
) {$ f2 N4 ~) K5 P$ f& w# z使用下面代码,对内存使用了mmap函数后:( B3 u9 t2 C+ S  j
#include <stdio.h>
% B1 ]2 \) J1 `2 x  x* l#include <unistd.h>
+ A0 u( y3 R3 q0 A  v* n#include <sys/mman.h>" @' ~( P  E- r% R0 J/ G0 s
#include <sys/types.h>
! `# B8 C0 P# a1 H#include <fcntl.h>
! ~+ s1 t7 s# Q! @7 l6 O# [% s( L# \( F' ~6 C( g# J; Y
#define SHAER_RAM_BASE_ADDR    (0x80000000)9 {) K3 L+ M" C5 H# E
#define SHAER_RAM_SIZE         (0x20000)   
- O. d# E/ }' F; B4 @" @: k4 u1 ]
typedef struct
* [9 f1 g  L+ A) o{
' X; e1 b, \  u/ z1 q        unsigned int a;/ b; K4 O+ D, l
        unsigned int b;$ x" ?6 T) l& ~2 s
        unsigned int packet_cout;& [  D& G% M- A' x4 c1 A' g8 u3 l
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
: f4 l! O4 X7 M' U( y" r% g. J0 p
5 A$ t+ m0 M7 k4 Q. s& Avoid read_MSG_buffer(int *baseaddr);- J! M/ J$ B  b
unsigned int count_copy = 0;
$ G; K% w7 i  w2 D: ~! B3 a) A# j$ g
int main()
+ O7 i9 F( w8 |  p6 k+ J' u{
5 `7 t& S3 z$ E+ Y! s/ U  h% ~        int fd;+ O( H( P$ ~. J! E$ `9 q3 S& U
        int *mem = NULL;# g3 Q; V0 I8 G6 O) ?8 }
2 U" h$ S' S- w) h. s/ I7 m# K3 s
        if((fd = open("/dev/mem", O_RDWR)) <0)
* b1 a9 {% W7 T5 i1 L, w. j        {
! j7 }( a. F% x5 K4 p& T% u' b                perror("open error");4 ]; A  u( r5 y7 i5 f
                return -1;/ d! s1 C5 X+ `
        }4 |- x6 S% n2 w4 _8 h! U' n
        + j1 c! P3 ?8 J# ]+ Z, |5 @4 f# b
        mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
3 y3 A% X6 f: m6 z* W5 v5 }) q; E" w& ]7 G& C# w+ R8 t; J8 C
        while(1)6 O! M% {( k% f4 {, O, K
        {
: L: y2 e9 _, ]2 R4 x                read_MSG_buffer(mem);9 h3 M! U  J* I/ |/ g" Y. Y
        }               
% {! {2 K* u0 s3 l+ ]7 r}: x3 ?. [; L3 R

/ f8 }. ^% o( e% `void read_MSG_buffer(int *baseaddr)9 ?! I4 v7 U1 I
{, ]) r7 ~9 K) T% S! O* c/ K
        pRX_MSG_PROTOCOL pshreRAM = NULL;
9 O8 B2 ]& m6 {( |2 G; q6 N" O5 B/ v
        pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;) S* f; B/ y# }8 w

6 A% U% t. C8 I$ U( r* ^! K        if(pshreRAM->packet_cout != count_copy)
/ K) Q* p, T' A1 t. D. @        {
+ f- M0 n, z1 |5 y$ U# _, J                printf("a is %d\n", pshreRAM->a);
/ J$ L1 k0 Y0 ]: u6 E: |                printf("b is %d\n", pshreRAM->b);
3 e/ B, u5 Y5 w9 ^  |                printf("count is %d\n", pshreRAM->packet_cout);
4 O1 f* S! A6 p) _                count_copy = pshreRAM->packet_cout;1 _2 I; ^7 l* c3 R5 o- Y
        }
/ |' ?' R0 g9 a3 Q- j$ q        else
; {3 ^4 m! ]1 k4 A        {
  ?! H4 Q- E1 n                printf("No effective message!\n");
6 ?. P4 u+ K7 A$ S' H        }" l# E* R5 |3 {1 }8 c
}
% \4 H# c" R* ?( D0 S" e8 P$ I/ S/ i, z0 v
没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???
! d# u8 [! S$ a9 I
2 n/ |* G7 G4 U& T
8 K8 N! N. `& P0 w7 V# F
, q- ^& _( c( V# e  T
2 ]: b% |* Z8 `2 D% P  N, Y




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