嵌入式开发者社区
标题:
OMAPL138的内存映射是否在Linux下已经完成???
[打印本页]
作者:
zc_fly
时间:
2014-8-19 16:38
标题:
OMAPL138的内存映射是否在Linux下已经完成???
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
5 w2 e# e: {) m2 X- C: C! W- m
/ F1 x% \6 w: }2 {/ l8 i( r
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
2 A3 s% b2 t/ b2 w$ J( L$ L
#include <unistd.h>
. a' h- z% D9 m/ l5 y
#include <sys/mman.h>
: U. M' ]! Q7 E l8 ?* ]
#include <sys/types.h>
- G5 d9 e2 B/ r
#include <fcntl.h>
, S' {" \& t, D. m5 {0 V, n; M/ z
# p1 ?: p" {! i4 d9 U
#define SHAER_RAM_BASE_ADDR (0x80000000)
. J5 J3 s, I' {: H
' G( b% X+ Y! F. q3 M, y
typedef struct
; `, G" i! }5 M% @2 V
{
# T( H. z" k& \/ x
unsigned int a;
+ A' s8 I5 j8 J2 M' m8 d" }- W
unsigned int b;
& @3 S$ c& h, B( }
unsigned int packet_cout;
$ | L& {7 e* f% P+ ^8 ^. E
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
" s4 q. }5 [5 p: F6 Y
5 c9 ^, Y' O% c
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
$ k: d- F& z4 a8 }/ {' Q8 q, {1 j- r
unsigned int count_copy = 0;
?; Z4 E$ J- O% y! X' d, j) k1 i
, t8 F2 Q2 k6 H7 _* V6 v6 A
0 u2 o, @1 L' `$ U
int main()
9 y# q7 z; B8 h o3 K& S+ a
{
; Y* r4 @4 ~6 Y
pRX_MSG_PROTOCOL pshreRAM = NULL;
. _% T( w$ _* E3 I4 I% ]. _& `
pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;
- w) T. K# I. l1 l
, d: ~& z/ E+ w& w9 X
while(1)
* i% V6 {% m+ _9 V+ _
{
& q+ H [/ q1 A) l3 ]+ A9 b: I
read_MSG_buffer(pshreRAM);
]' Z) j7 s6 X! Q/ f
}
0 Z. H$ b/ D" |6 ^) [; @0 W
}
4 {4 m/ C* {2 C, X& A, V: u
/ M. R9 M8 K G R
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
- n0 g3 P* v1 O6 W
{
% s6 a* Q: r; x" |" |- ]" w
RX_MSG_PROTOCOL buf;
* o' W. I$ i1 @8 ^
$ _7 O* G4 [& O" o' K$ S- u6 H' }
buf.a = pshreRAM->a;
# `$ c+ V& S: S- L( U5 R2 B
buf.b = pshreRAM->b;
( G6 Z- w8 i4 [# O3 X5 T+ N
buf.packet_cout = pshreRAM->packet_cout;
2 M& j7 R2 k0 A6 Z5 Y+ W
* c+ ^/ V3 R* U. b. G+ L
if(buf.packet_cout != count_copy)
4 j" L" Q8 R& b7 H
{
1 G' K# a M) x; ?# T9 Q0 h
printf("a is %d\n", buf.a);
5 |4 D& F& U2 V. f8 A: k! B3 X5 R
printf("b is %d\n", buf.b);
1 H4 k7 Z6 }4 y# X! @
printf("count is %d\n", buf.packet_cout);
* U# s6 z' n [) @
count_copy = buf.packet_cout;
9 _9 W. V \: c: }" z) Y8 A% C- }% B
}
1 w) K; u2 M4 e* }/ c
else
4 E) f. Y9 `3 o: x
{
: m2 n! t( b2 j$ {( }
printf("No effective message!");
9 t& n& C8 Q# z% x3 P
}
3 Y) x2 x1 ~. j, j0 c+ U! f2 Y) e
}
, P9 b7 n$ i! L2 e
# e! R# B% k+ @" D3 b, U
8 L5 E& a# x: s/ ^
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。
& }& ~+ W1 `/ Q
使用下面代码,对内存使用了mmap函数后:
) K: S% ]% O; z1 {9 P- c
#include <stdio.h>
6 e1 R T9 ~0 a9 X; }) e! z" i- ^
#include <unistd.h>
' i. P$ P* n! I! B+ L; N3 `/ \
#include <sys/mman.h>
% l0 \/ W5 P# ]& b/ t+ U
#include <sys/types.h>
: S& p4 x" M7 @. k( r/ c' s- a+ W- B
#include <fcntl.h>
' J5 E* |( A6 R6 D5 P v8 `
8 E8 v, @0 A- b! n" D, y
#define SHAER_RAM_BASE_ADDR (0x80000000)
1 G3 m/ s7 w5 m S0 {/ P
#define SHAER_RAM_SIZE (0x20000)
+ R1 Y; d5 p6 t+ f" C
) @: B. P8 r: \& t, W( S* `
typedef struct
8 E' M- w- u$ J( P a# ~( ]
{
( M" I* C) x+ M6 }3 u* x2 ]8 ~
unsigned int a;
% q, ?* G7 x$ n
unsigned int b;
4 q( _+ \( }( g1 T! g' l
unsigned int packet_cout;
; h8 W+ L* L" |# s# }8 g& r
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
4 R& Q9 f4 F; f6 L: ]- v- X
# Y; ^) x$ q6 s. |" m. {8 r
void read_MSG_buffer(int *baseaddr);
/ E3 `+ [ P# @% G. P7 ]2 F
unsigned int count_copy = 0;
6 _' ] b% I/ J1 x. e! V2 N) Q% c
* l0 B! |. I1 a
int main()
; [3 H( f3 W1 S7 b1 j- g
{
+ _2 a% F5 ~+ Z: g2 T3 g
int fd;
: ^0 X* I6 k- W5 p
int *mem = NULL;
" G' m3 Y2 x/ z; m: w- `
; y% C% U1 D n0 Q$ Z# c
if((fd = open("/dev/mem", O_RDWR)) <0)
/ J# r* X) _/ s3 o( m
{
6 W* {1 b$ |/ }$ z4 d$ L- h0 v% @
perror("open error");
4 K; |7 B/ i" l' M1 r9 C
return -1;
4 G. P0 p9 J! h( E
}
6 @! P$ @' W; T3 @5 V0 q5 Z
* R0 p9 b7 A% U
mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
: r& P' r, K! m
' V2 y8 m# Y( @, p8 T$ b# v
while(1)
0 {0 X2 S% a( p% }8 v( {. m! M$ }4 L E
{
A3 ?3 L+ o: B9 s1 O# h5 r
read_MSG_buffer(mem);
0 `+ E0 X& P ?# R! c
}
+ E6 |4 J4 n* p; I i
}
) Y2 M7 q Y' D6 e. n. A+ I- g
. S) \$ G2 E( d, {
void read_MSG_buffer(int *baseaddr)
0 d w+ a0 L, ]" q( D" p1 m+ q; H
{
0 M( v" f8 L# ^( G" s; i0 j
pRX_MSG_PROTOCOL pshreRAM = NULL;
. R, b2 y; K2 b# ~& `. D
9 C& H" y% _! h
pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;
. L+ J9 l* e$ T% Z
- c9 `/ X6 m/ O+ a% S
if(pshreRAM->packet_cout != count_copy)
4 F+ t" c% r9 P6 N) c
{
4 e/ e4 l+ t" g: @/ ?, S& f/ l
printf("a is %d\n", pshreRAM->a);
! d" F/ N3 H0 d! S
printf("b is %d\n", pshreRAM->b);
( O: g) R. H& j5 v; a
printf("count is %d\n", pshreRAM->packet_cout);
7 z9 H% A# P- S; d1 i, R/ N5 ~
count_copy = pshreRAM->packet_cout;
+ Q3 Z$ ^! E3 |( l0 E1 T2 j* l
}
5 ?; q# l L$ B/ K+ e5 R
else
' \" h2 p8 y8 \8 R$ w
{
5 ]7 M+ N# _1 Q1 R3 f
printf("No effective message!\n");
" ?5 ?6 u# \' o0 V }5 s9 S7 J
}
, V+ N! G- Q3 q/ D
}
1 Q5 ]) x m4 S8 [ r
$ k9 O j7 q! i2 \" k7 F
没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???
1 q2 _& e- y1 a5 p- o6 f
/ P) F( |) Q5 F5 o0 C* s
* D' K, }% a- ] d4 U$ t1 }1 g
# z+ C& x m. H. @
2 N& a; \- q3 o+ _5 [
欢迎光临 嵌入式开发者社区 (https://51ele.net/)
Powered by Discuz! X3.4