嵌入式开发者社区
标题:
OMAPL138的内存映射是否在Linux下已经完成???
[打印本页]
作者:
zc_fly
时间:
2014-8-19 16:38
标题:
OMAPL138的内存映射是否在Linux下已经完成???
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
, w) L6 x# ^ J1 B3 b) K( ?
1 {( O2 C; C$ w3 P- i6 j6 A
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
3 |5 ?: ^) k/ P- f9 U% D
#include <unistd.h>
& ^+ H4 R$ V* Z0 b
#include <sys/mman.h>
! h; d' _+ ?" [3 h2 ~- Y
#include <sys/types.h>
8 M6 x# {. [3 X1 ^" b: D
#include <fcntl.h>
! `; e; Q4 L8 }( ~6 V0 i' c
% F* R3 a3 ^7 c+ L: D- J8 N
#define SHAER_RAM_BASE_ADDR (0x80000000)
# W6 }& ^. M! j/ ?' g0 u
8 C2 T2 T- W3 K5 _( A. O! j: d
typedef struct
, |0 o6 Z0 H$ A9 Q% K3 @! t
{
! c2 h, I- f; h
unsigned int a;
+ M3 q* g4 B' `
unsigned int b;
7 t* q4 b+ x- @2 q+ c* ?9 i, I! p
unsigned int packet_cout;
3 `) S0 Q/ ?% S7 J. C
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
) @! y- Z! @% E) [, B5 F
% N5 M2 m% f- Y' C( \6 r2 n. g. Q
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
- n: h! n2 a3 \, h& @" W
unsigned int count_copy = 0;
+ l4 x* R# z$ b
- w$ N! {7 `* Y, k% j
$ _/ l; ~$ i4 e2 c3 a) n) U; l7 P
int main()
5 i+ ^4 p8 W5 E: @; m# @
{
& Z' N1 U% q, l( [ g
pRX_MSG_PROTOCOL pshreRAM = NULL;
0 X9 S s, d7 m1 a# ?& ~, j, p2 N
pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;
1 t) r- ?8 r3 Z# D5 M
! T; e; m: [' L. y% `
while(1)
1 g) h2 U3 a4 G% c: R7 l
{
! T- A& m0 i4 p) L! P7 V
read_MSG_buffer(pshreRAM);
5 @# k3 g0 L& a0 ^ H. s, J
}
) L+ g# y( m% N8 F2 |& V+ V
}
/ A8 }& V2 C' U; p; |) g/ o
0 J$ e! X/ d. j Q! h
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
6 G A" K1 |, A0 Q) R$ k7 Z% N
{
0 g& Q( n3 T' d) f- ]
RX_MSG_PROTOCOL buf;
, V8 m, w; i0 i6 a- s
4 W$ Q6 ^" Q) ]( E" t5 D: }3 S/ n
buf.a = pshreRAM->a;
( o* `* `5 h8 P
buf.b = pshreRAM->b;
8 o; ?8 n6 _$ ]: `7 @3 B/ G6 t
buf.packet_cout = pshreRAM->packet_cout;
. G/ I6 r+ K0 i! N% H2 h' `
' {+ p! N$ B( |0 k# a# v& ?* a( Y
if(buf.packet_cout != count_copy)
* i6 N; ?+ @) b, w
{
+ k2 W* e6 z4 C3 ?7 }8 g& p
printf("a is %d\n", buf.a);
2 j& {" a& Z; f
printf("b is %d\n", buf.b);
/ r& w4 K" D: l' W' Y; I
printf("count is %d\n", buf.packet_cout);
7 F4 K/ o3 x2 e/ s
count_copy = buf.packet_cout;
6 @ b# j3 ]' y+ [
}
$ b& e, f% z0 s
else
, c. s- k X3 p J
{
) R3 i- ?- f. {: t
printf("No effective message!");
! q* B. n" [& j0 S2 E% t, q
}
) E' X3 z( d' S8 Y
}
$ ^# F+ Q( l# q K
/ R8 {( X7 O& y% i
( }: m0 c3 k: Z% G; }2 U
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。
2 {4 q/ |" f5 ?# R) _- J
使用下面代码,对内存使用了mmap函数后:
2 u3 n# p# C6 q" {! `6 @9 R0 @- |
#include <stdio.h>
1 a8 [4 V4 k+ n9 G
#include <unistd.h>
6 l/ _! z0 `' j# X* m
#include <sys/mman.h>
+ @1 l# [' l; }
#include <sys/types.h>
2 d' D& L: J' g) r
#include <fcntl.h>
% D& R' e8 p- \
" a, z' f- [+ `' P$ H
#define SHAER_RAM_BASE_ADDR (0x80000000)
2 t l/ D( P! C- J
#define SHAER_RAM_SIZE (0x20000)
$ C2 r3 Q7 M: m d- p2 o% U
" `/ ?0 W! [* I8 c+ f' Z
typedef struct
3 E- {* q$ F6 g$ k( _( U8 j
{
( T8 |/ M0 a0 C1 p- T% [" c
unsigned int a;
5 x9 g D* r* c) n3 M
unsigned int b;
; z3 U# k! }5 f& B
unsigned int packet_cout;
6 V7 x, X- G0 q. c" s) n$ I- w
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
/ h4 ?7 R1 x" n5 P, z, ^$ O" b
2 R& _3 Y2 F2 D" T+ ]
void read_MSG_buffer(int *baseaddr);
9 R3 q& e) |' v& E5 t/ q& \
unsigned int count_copy = 0;
9 P3 Q9 L$ u4 R" t: E
3 N: J& N+ S) H( r3 G
int main()
& c& c7 B6 z5 Y; K8 z
{
* b4 |8 i/ `$ n: V$ g
int fd;
% o' w u7 N) S) ?# b& {4 e
int *mem = NULL;
' ]$ l% g: |7 h) X
9 M) `" A8 b! u: S: y9 s
if((fd = open("/dev/mem", O_RDWR)) <0)
6 P: B* G* A h* W
{
- y T( U, l$ j; k# l
perror("open error");
# o( L% } d; T
return -1;
& z0 U! ^- C4 L; _5 F' h
}
# j2 j0 E2 k- C- o! R2 U" K
/ {! k* H$ a5 o* |8 t8 }$ g' U
mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
: J1 F- L# Y8 @0 }6 d
2 {2 Q. X9 E5 R3 c& R4 L; M$ c( k8 k1 ^
while(1)
" p; T! u; Z4 N8 J) W1 H+ m$ v
{
: `' p/ r6 R! F" u
read_MSG_buffer(mem);
# B$ P8 K8 ?- w
}
" K! t! |, n& X1 O
}
- ~& V! `6 i. _
; f; x! k0 A* G t
void read_MSG_buffer(int *baseaddr)
`2 T6 j3 }- ]' C: `9 o1 D \$ O
{
: u9 P+ u! U/ g4 |
pRX_MSG_PROTOCOL pshreRAM = NULL;
& L" k1 x* A$ G! b( Z
, W5 A* W% i) G1 j0 u5 j
pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;
1 G8 g( N' {4 b, `
5 p& q' c% O8 F$ C
if(pshreRAM->packet_cout != count_copy)
/ I) l; D) E$ m9 H) j7 V1 }
{
9 @9 Z9 {" s$ y8 ?
printf("a is %d\n", pshreRAM->a);
6 w$ c& a) c7 h( A* Z& W
printf("b is %d\n", pshreRAM->b);
7 \# x8 F5 Z/ j9 p* s: h$ L- S
printf("count is %d\n", pshreRAM->packet_cout);
2 y& K( l0 E4 k+ Z' U; R; s
count_copy = pshreRAM->packet_cout;
% F& M' d+ C' s; z: @/ \7 x
}
: T( L( k8 `. J& O r
else
8 ^2 k7 h8 {2 S( m4 G; N
{
; B( m' x+ Z+ a3 ]( g# ?
printf("No effective message!\n");
+ D$ [/ U1 u8 i& f3 V1 q
}
S2 b: M# S0 M: \- B) d7 E
}
2 B4 g$ v9 w+ x, Z
( F( [7 B3 M- m8 q
没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???
: T Q' {. G1 x# x D& _
% x4 F$ i c8 p1 ` Z0 {
1 S. e( X6 G u, C+ a: f" A
' i1 a; b4 G1 F4 A* {. N6 j
* G3 g! m: j, \ q+ J& y2 l1 \
欢迎光临 嵌入式开发者社区 (https://51ele.net/)
Powered by Discuz! X3.4