嵌入式开发者社区
标题:
OMAPL138的内存映射是否在Linux下已经完成???
[打印本页]
作者:
zc_fly
时间:
2014-8-19 16:38
标题:
OMAPL138的内存映射是否在Linux下已经完成???
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
; }) U1 I8 o3 V
# E, W. J9 h3 H6 p
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
6 l& v% _' [, _9 h
#include <unistd.h>
1 n7 b' M9 j' t
#include <sys/mman.h>
7 E3 U5 N7 q$ I) U- X; Y7 _6 Q
#include <sys/types.h>
" R5 ?. h4 |6 {9 O$ w0 k1 i7 [
#include <fcntl.h>
7 `! w( v' I* J+ t6 ^
6 ` C! K1 A q/ g. \$ s
#define SHAER_RAM_BASE_ADDR (0x80000000)
( I+ ~+ H0 f6 s2 Q6 g4 {6 q% C
6 j/ L" z& i L
typedef struct
# s1 l+ A# \( I- y+ K/ i7 ~5 ~# {
{
5 w, n+ j, d6 h& a
unsigned int a;
9 S' A- U1 Q3 d& ?4 q( n5 N
unsigned int b;
+ f# K7 Z* c3 M2 t+ l& }# \
unsigned int packet_cout;
0 W* ~" P/ R! ]' E# B- P0 C
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
" u' ~9 Z1 @0 n" N, F% t9 o- B
$ ?. a4 f/ w- `* S7 b
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
1 `3 U: \9 }% _* |( u. P
unsigned int count_copy = 0;
' n5 L! Q# `! v
) q" s [+ h4 Z- c
0 w5 i$ j2 y/ \( u2 A: \
int main()
$ I1 U) r) ]2 S6 ?8 x
{
& T2 n$ B! D4 y
pRX_MSG_PROTOCOL pshreRAM = NULL;
' m% s, n! R- H9 `
pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;
7 R/ ?4 c* w! J9 c, N2 m+ ]
6 w7 g' c/ O. `# K: h
while(1)
% I# K( X0 W8 Q0 q& n) J+ X- S
{
, b+ J1 ~: J1 l% M6 S) m% m
read_MSG_buffer(pshreRAM);
3 t' _2 D' B# C' W
}
7 u; K0 V( A7 P5 h6 z6 u- ^
}
$ z! b& W7 `! t" J0 Y4 ]
/ L3 s8 C4 ?: K2 U$ R$ g: o6 v
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
3 }2 g$ P. m$ s" h
{
. l# T8 c1 W' l. d. A" ]) f. t
RX_MSG_PROTOCOL buf;
7 L+ g5 Q. W$ E* ^
1 x% _; ^: e! h1 x x7 _8 n
buf.a = pshreRAM->a;
/ b ~9 b+ p# d) s- F
buf.b = pshreRAM->b;
( J* \" q5 k/ w- [* B9 d
buf.packet_cout = pshreRAM->packet_cout;
1 H1 D& K7 Q4 H3 z5 `1 E
0 U+ G) v0 ^/ D* e0 y; E k
if(buf.packet_cout != count_copy)
* \8 k( k$ P6 D2 p% d4 j. J
{
q1 C* O5 Z0 \ T% l$ o
printf("a is %d\n", buf.a);
& I1 D' j3 P! [$ C
printf("b is %d\n", buf.b);
( O/ |4 G7 J2 V5 W! y6 @
printf("count is %d\n", buf.packet_cout);
/ ^' m0 d% b7 K
count_copy = buf.packet_cout;
: j; a# J, g8 h8 e* b" p
}
1 Q& b6 u3 r$ t5 H, P! I5 N
else
8 T0 t* s' l! m" Q
{
7 o- o1 `0 w% n% D+ {* j6 [
printf("No effective message!");
Z1 e! t" k' F1 B `
}
1 H! F- l, D" o% }) U: g0 U' j2 i! X
}
w8 c+ e( x8 d9 p8 y. v
) j( J! _; b4 i1 G2 x
' r, s4 C' T+ @! z8 M
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。
* i" B i5 t D- D3 V" [
使用下面代码,对内存使用了mmap函数后:
5 ]8 D$ P/ K0 d5 E, z
#include <stdio.h>
1 k- |' u8 }) X! F/ ]8 v
#include <unistd.h>
3 `# D) ]$ G1 J5 X
#include <sys/mman.h>
" u# K+ x2 B% o/ C- L/ u6 k
#include <sys/types.h>
3 m( r/ x, o8 d5 ^
#include <fcntl.h>
- P5 G9 N1 ]) g4 W. K d
" Y9 q0 Y+ x( T9 N! `
#define SHAER_RAM_BASE_ADDR (0x80000000)
m" O* M" R, w! F9 l8 e( k
#define SHAER_RAM_SIZE (0x20000)
2 P8 ^) h8 q$ K: y. i$ y* Z2 F
' o8 d: s8 V& R! Q
typedef struct
5 j, J u, L- z& V3 x, g6 I
{
3 ?: M& I4 P5 e; |- o' j
unsigned int a;
. k8 Y% b: S% x/ p* ~
unsigned int b;
& n! J# K4 D6 @0 d" w
unsigned int packet_cout;
) X3 A. q p* M$ Z. d3 A
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
4 U) }8 {+ c) R0 t% \6 S3 q& i
2 K. u9 q f, V+ F. `2 H3 P$ K+ _
void read_MSG_buffer(int *baseaddr);
; V" v7 |; U! `. g3 W7 D0 B
unsigned int count_copy = 0;
3 v( k0 W9 j$ T
) W1 B. j+ p3 d5 j
int main()
7 X6 a V4 q( X, Z: U4 v; n7 q
{
/ O) y; F/ V+ Y0 G9 C
int fd;
+ F, ^% c& V3 o' F, c5 |0 [/ |
int *mem = NULL;
/ k* `: B/ U. u6 V B6 d
- I# N4 m( o+ S t- b% h
if((fd = open("/dev/mem", O_RDWR)) <0)
% Z+ S! M% e1 Z
{
$ h) ~( f4 ]7 W9 f
perror("open error");
1 D& k& ~( M( R) Z. ~& P e1 _' J
return -1;
0 n; y) b9 m* _1 K) o! M0 y9 ^
}
8 Q C* Z6 v( L
' D9 Y0 A3 j1 \& L) Y) q
mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
% k5 ]4 [( s9 C0 d6 I; m* C
1 v0 I$ n5 h# d+ u" x. G
while(1)
- q7 P* c& B0 x8 a6 C% s
{
. p- g3 V' m7 `) ^
read_MSG_buffer(mem);
+ v E" N6 U: A
}
1 I6 w% F' J1 M
}
3 b( c3 ?2 x n9 P
# W9 Z' @+ I* b/ g4 U5 |& I
void read_MSG_buffer(int *baseaddr)
: E0 X4 l+ Z# s5 r% T2 a, j
{
. {8 O/ I" x& j* F- T* I
pRX_MSG_PROTOCOL pshreRAM = NULL;
& i, Z4 R( c- V9 ~9 A
' A f* x' u/ M: @3 S$ b
pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;
@/ N; b; F9 @. o( u/ j
3 q+ B2 \9 Q0 C& |8 ^ ]
if(pshreRAM->packet_cout != count_copy)
, [6 n6 _$ n: U5 `' I) I0 h
{
* M- H. f; `1 P0 W: s3 u
printf("a is %d\n", pshreRAM->a);
$ k& }7 q) Q2 |& E! I2 {& {
printf("b is %d\n", pshreRAM->b);
0 K- C# U; |5 @) e t" S# o
printf("count is %d\n", pshreRAM->packet_cout);
, r7 t5 T5 u) k! A2 J3 H
count_copy = pshreRAM->packet_cout;
8 p: n1 U1 e0 s$ L
}
% e6 y U# I/ m0 d3 [9 j
else
: V* z K- p$ ]
{
8 X" E: z( p- t. g5 I9 B
printf("No effective message!\n");
- x& v& r% t* ~& l" _; n( ]
}
. n* d% D/ T) K& u* \
}
( G3 z2 U1 ^$ @+ A
) g5 M2 |& L D% E
没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???
* |" G8 N7 n1 a: t4 J- J1 G
' e* P1 d9 I/ c ^$ i
! R7 a- l0 f+ D; i6 c! i
5 f+ R) d7 h7 t( t3 m
6 `* M7 e2 J5 P: T: _
欢迎光临 嵌入式开发者社区 (https://51ele.net/)
Powered by Discuz! X3.4