嵌入式开发者社区
标题:
OMAPL138的内存映射是否在Linux下已经完成???
[打印本页]
作者:
zc_fly
时间:
2014-8-19 16:38
标题:
OMAPL138的内存映射是否在Linux下已经完成???
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
) G& |* I. g* B5 ?$ w
% v6 q: n- A/ p) T
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
7 t3 B, Q9 r% Y! c$ Y
#include <unistd.h>
6 [ n; M9 s# u4 t- t: N2 n e$ X
#include <sys/mman.h>
+ H; y& ~! U6 ~9 P
#include <sys/types.h>
8 m+ ]6 G8 \- e
#include <fcntl.h>
! m7 z4 s( _; T% r. j! ?2 R
3 N# f: Q" [7 @1 p: Y0 ?3 \7 r
#define SHAER_RAM_BASE_ADDR (0x80000000)
, D R0 P& a9 D$ Y+ N- R, f
6 P( V2 y/ g P: e2 F+ q0 T- Y
typedef struct
: ], |0 W' C5 k2 y+ m2 q
{
2 D6 C/ ^- p% I7 f' J* ]$ q) I
unsigned int a;
0 E6 I5 Q% T: _4 x
unsigned int b;
) N* L0 ^4 v) H% |8 X
unsigned int packet_cout;
1 s5 F( H% s$ r% S
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
U8 d% q# T+ x' R( O$ f7 }; h+ j2 ^
% X; c6 u, I4 z4 Z, H o' n
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
( B: x2 L& x" f$ I d7 i
unsigned int count_copy = 0;
" Q4 j7 J3 ]8 n. u0 L9 F
4 u4 c/ Y! [/ l" o: N! J1 L9 |# U
5 }/ x- D* h8 U" }$ x) a* x) Y
int main()
# W$ z4 Y# b" O+ ]- e0 H
{
2 S% i( s8 _; B/ a
pRX_MSG_PROTOCOL pshreRAM = NULL;
$ ?; R/ N4 H, V
pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;
' N4 q0 G+ l% V8 z- D9 y, ?' q/ T
0 l0 v6 o; P1 _) u5 L* `
while(1)
N. \# P6 u S) s; S
{
`# W# n3 W* W& K2 k' s5 Q7 B$ L
read_MSG_buffer(pshreRAM);
0 y0 u* U% L. n
}
8 f7 q2 Z: V4 }: T7 x8 X
}
! X/ [/ l' [4 B) Z3 n
" G- ]2 _" C s$ ?+ _& w" T1 X
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)
( H/ t3 C, |, \. q- x6 Q: d
{
& h3 c. {: J, R: A
RX_MSG_PROTOCOL buf;
5 z4 N. E" s$ Z: t
9 \. O1 v! a. H. _
buf.a = pshreRAM->a;
6 {; ?' D4 N- P9 ?2 ?- O
buf.b = pshreRAM->b;
7 e, t5 O! F3 ]- e0 F; u8 E
buf.packet_cout = pshreRAM->packet_cout;
8 \$ c( Y, V% w* A$ k
( \4 c8 v% [( w% D, H
if(buf.packet_cout != count_copy)
. _) |. {- r' C6 q! S' {
{
' t- O" I* V+ a8 X6 s- h( R/ ^# q$ ~
printf("a is %d\n", buf.a);
6 c l2 g, o, B1 G# S6 \( d% P
printf("b is %d\n", buf.b);
/ a) s; {, [4 G' G& ]/ |2 y, @
printf("count is %d\n", buf.packet_cout);
2 w6 L/ L( i% t
count_copy = buf.packet_cout;
# ?( u# C5 {* F5 p
}
8 l4 [9 f& y% b& b& k' q
else
3 `/ `) i- E6 o( H/ g4 I/ e5 J
{
4 e8 L r6 C6 E3 N2 @# q
printf("No effective message!");
5 K) n3 j F$ m+ ]. U
}
0 U* a7 ~' N, D+ z( j- k) P0 t# i
}
9 z4 U/ {# ^ m
8 c; [2 G6 ]5 }7 h5 W1 W
. ^; G1 r0 n( ?0 o+ x- W* h
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。
/ ^) P9 M$ S1 ]4 ~% ~
使用下面代码,对内存使用了mmap函数后:
# L' r9 Z8 q8 L. L+ ^# k
#include <stdio.h>
: j3 Z y# J. }! Z! J3 C
#include <unistd.h>
* ]" A3 H# Y0 X% O* q" w: ^
#include <sys/mman.h>
9 s5 B% D9 A9 ?. x: [8 [
#include <sys/types.h>
. |9 W- O$ Y$ y0 c s% |
#include <fcntl.h>
. U5 N: d" }( g/ s6 L
0 h5 B7 A& c N8 j$ s
#define SHAER_RAM_BASE_ADDR (0x80000000)
" |' @% @; O) [8 L* Y. I; M0 L
#define SHAER_RAM_SIZE (0x20000)
! _7 I6 b( B3 Y7 z6 z9 S% t f
9 } f) i* r7 X) n
typedef struct
8 D( K$ L4 ~) \$ n% o
{
# u O# }6 A9 t5 W& N- Q# e
unsigned int a;
- @6 }: c; j% a0 b9 T4 j9 ]
unsigned int b;
2 E& c' X2 d% B# h
unsigned int packet_cout;
8 `' I1 q0 r7 v% y( G+ |+ g( ^
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
5 r. p/ U2 f G7 {' Z
% @1 L8 O4 g5 p/ d
void read_MSG_buffer(int *baseaddr);
: m4 {& I! X( A/ Z
unsigned int count_copy = 0;
' K% V8 b; ?/ k- x" s
0 }# R' c+ G+ ^$ d8 q- m- O
int main()
0 {7 @) m: q% @1 [) H& K8 H9 j' }9 j
{
" R( n+ y- v/ N0 t' R
int fd;
" d2 l- v/ @1 r: X" Q4 W2 u6 S
int *mem = NULL;
* \# o% u' Q6 r: x# y! r3 I
7 D* O. }! g- n( s
if((fd = open("/dev/mem", O_RDWR)) <0)
; B) _! y/ A7 h& }0 D8 Y, L2 Y
{
# t) x Z0 a* j. W
perror("open error");
+ g D6 [# n: e$ e/ K2 ^3 l1 |
return -1;
. w; t1 F9 c( \' Z3 {
}
7 O& }- U# `2 V
: A! S9 D7 p* P! Q
mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
% D+ w) n* |/ X! ^- B$ E
9 B- o! h) }4 {, B3 R
while(1)
- i( V/ B; V9 o3 E" o
{
3 ~4 t3 W9 ]& O7 d7 l" L! K: Z6 ?
read_MSG_buffer(mem);
+ ?: v1 `4 P( Z5 Z
}
' e( E9 D' q$ T0 ?$ {
}
5 y0 @* i+ n; V8 R$ X# M
, [# R& h" ^) i. G
void read_MSG_buffer(int *baseaddr)
) L! i# j0 v3 B& n& Q9 U! K
{
* a) T& m6 a" e5 }+ h+ O
pRX_MSG_PROTOCOL pshreRAM = NULL;
$ L# ?8 z [; n. H0 R
8 F/ J5 H! k0 h# _( \# G
pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;
1 V7 x# d& J6 V5 T6 Y
5 c- x0 M1 b, p, N) a
if(pshreRAM->packet_cout != count_copy)
# h, L; ^/ C7 y# Z7 s- c e* D
{
; D3 o! m8 U# _# j9 a
printf("a is %d\n", pshreRAM->a);
' L( S' \3 g; B: D
printf("b is %d\n", pshreRAM->b);
* v- D9 R3 c3 a6 I+ W
printf("count is %d\n", pshreRAM->packet_cout);
( }1 H6 T% E, ?4 [ v1 z
count_copy = pshreRAM->packet_cout;
) t$ w5 }- A: }' k- Y+ q& M
}
, T4 v8 Q. \9 _& _2 D* F+ M: \
else
, `- O* ]7 }1 ]& a& r( V
{
|; M A" Z# S! y4 F0 {
printf("No effective message!\n");
; }- @- D& F4 j4 k
}
/ P E5 d+ m& O7 P4 m ~ @# [
}
1 ~+ [" ]& k% l- Z& ~$ d; @
$ T6 P, o9 r, P+ s; X* O
没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???
# z# n. `) S$ |
6 i9 X2 g( u" ~- E+ l/ J
9 ? J1 F% K# m+ S8 H x m
& h( Q. z0 X/ P H5 k8 Z0 D$ r
" V3 v. v5 F- Q& E1 s
欢迎光临 嵌入式开发者社区 (https://51ele.net/)
Powered by Discuz! X3.4