|
|
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
I! u0 \' J5 l N$ l, u. D* P# p) M3 z# h E5 I. T- k
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
% D& Y7 T8 Q% X) z/ K1 F#include <unistd.h>" p/ j9 s0 K8 E4 c( ^: z
#include <sys/mman.h>
' a3 c( ]4 m* I5 b#include <sys/types.h>
" w- e& b7 ~; I#include <fcntl.h>8 u& s1 b V6 W5 s% }0 ?' t
6 [: P$ o f: T5 Q' e#define SHAER_RAM_BASE_ADDR (0x80000000) , Z2 M0 G- b8 u5 ^6 D
, Q' K; R- N2 Q' |/ j: S8 p9 z
typedef struct& p; Y Q$ I& ?& k! M8 _+ ^
{# b* o7 t! f' f1 J0 F, i
unsigned int a;
/ \8 E! W$ b' F2 w! b unsigned int b;
" D, \3 q3 W7 D% N } unsigned int packet_cout;" r* E! l( a! T; S9 k3 n
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;* i B- ~ C# s1 }" |3 G5 M6 \# [
2 m: K5 k; c$ v* I. p
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);7 j" ^' G* \5 [% L5 t/ G7 p4 O
unsigned int count_copy = 0;
) ]5 N( F3 {& ?, y! ~/ h& D. k" z
& {% J, }8 `" J- r3 f& u8 P; o0 m( C* o" \; ]8 U
int main()
# T* S, ~* b( n- u/ B{; h6 C$ S3 p" g7 y# @5 X
pRX_MSG_PROTOCOL pshreRAM = NULL;
8 v! r9 i9 A0 C& {8 b pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;; e9 U& {+ j! l' \
9 |3 h9 L6 N( ^1 P' c7 y while(1)/ H8 ~+ { n; T% Y* y* G$ O
{
: F0 J# l5 w# o; I% p# s3 t( m. C read_MSG_buffer(pshreRAM);! _3 i- U' P! O5 E2 Z" `" |# \
}
) X) ~7 s" i1 _# J7 l}% s9 w. d/ I; Y9 B# R9 k" G, j
5 s# E: I2 `0 g( x+ j7 L8 ^4 B( Bvoid read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)! E* {* a0 E, e+ `0 g
{) d" I2 E X% r$ Q
RX_MSG_PROTOCOL buf;4 \$ j7 f; e0 ~& z- @. [
3 A' ]$ {% f- w
buf.a = pshreRAM->a;8 V: z+ |: X6 M: B/ a
buf.b = pshreRAM->b;
) x9 v# t7 `# E( s buf.packet_cout = pshreRAM->packet_cout;
9 T# Y: P; R6 X" [; H7 S% u$ B ' }% V; v. Q, G7 P
if(buf.packet_cout != count_copy)
1 r& m x% B( `6 ?( B {! u! [- r- ?% x' a- [% u- I
printf("a is %d\n", buf.a);3 {1 E2 {0 ^# Q0 F8 k; d
printf("b is %d\n", buf.b);5 Y) z. s, v% h4 \# Z l
printf("count is %d\n", buf.packet_cout);
1 e. m% C/ q) \, g count_copy = buf.packet_cout;
: P# N8 C5 R+ R }
8 L) \8 p& ^. H5 C else
, `! r/ ?* |7 c a0 e" ?) n. v" F& m" X {1 O+ F3 C) H, r: U8 Q" ^
printf("No effective message!");: r$ k+ q+ ]$ s+ v* ^
}
% l [& n0 m o( e6 K}
4 g( T9 O9 l" Q9 _+ R. E G# I( a8 }5 I. M* A
; \# ~0 r% E. Z0 K但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。' `& X' @/ U S9 k, T' D
使用下面代码,对内存使用了mmap函数后:
5 K; G5 ]& W, x; ?6 I5 w#include <stdio.h>5 _) i8 \, k E( H6 Y8 U6 R9 l% r
#include <unistd.h>
7 C* w; L% D4 Q7 |#include <sys/mman.h>
2 N' \+ r0 s5 D) i. z; u7 p* Y& B; y#include <sys/types.h>
. _4 s1 Q9 ]# S; [#include <fcntl.h>7 T- R+ K; ~. {6 ]8 M+ l
. B& N& ]% L$ |4 f6 e; d8 l, w#define SHAER_RAM_BASE_ADDR (0x80000000)$ f! ]8 C& [' c- k0 {0 o
#define SHAER_RAM_SIZE (0x20000)
, d8 u/ i; ? I# J- H3 `, |9 y# ]& A0 x, _
typedef struct" b9 b- K1 U- [6 @! c
{
6 z( \, i, v4 ? unsigned int a;
; K3 i' Z, m# M9 E5 q unsigned int b;
+ D8 ~4 L! b+ \3 y+ w0 Z unsigned int packet_cout;
" f$ k" L1 T+ H3 { S}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
9 Z o3 V" i4 g* s& w [
" Z' f# C! b1 o. mvoid read_MSG_buffer(int *baseaddr);0 ?" {7 ^1 X, F$ o2 K* H- d2 D3 i5 S
unsigned int count_copy = 0;
/ R K. @+ @% \) y* V B9 G" d3 f o6 E
int main()
4 X' ]9 q/ G! ^7 x' g* `{
1 ^1 U7 D' l8 q3 j/ c* X: c% J int fd;
- k* A8 A) p- S, U7 _ int *mem = NULL;9 J$ ?( R5 t, M! @& \
7 Y1 S- }, w/ X# T if((fd = open("/dev/mem", O_RDWR)) <0)1 ?; ]; o" @) x: J
{4 I. A% L ?% K- V' ]
perror("open error");
8 C5 { X5 @: H3 F$ |9 _; n- z: N$ f return -1;
: x# ?4 j+ j7 c( S" Z% d3 A }
8 n( [' w5 l, y: e0 M) k8 v! V
8 n7 R4 b: e) h mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
N! |% m; l$ ~7 q! C
. ~0 a T( \2 Q while(1): k" E' A6 D8 y+ c' h2 ~) L
{
9 m! ]4 Q9 q- a+ k+ k read_MSG_buffer(mem);
1 C/ p% c2 c" U9 q8 G } & V6 s6 c/ U/ O' m
}' s; @; F8 R# b, z; z
. |- R( c4 K: a
void read_MSG_buffer(int *baseaddr)
5 h- c7 ]# G u{
: b5 b5 g3 T! d3 y" ~* Q pRX_MSG_PROTOCOL pshreRAM = NULL;
+ [# t* t) L& u' D. r
7 m( s4 `0 J; d. Q1 B x pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;
6 l- a3 i5 e- _7 I, I/ M. S6 C) n* o/ D4 s; T
if(pshreRAM->packet_cout != count_copy)
, h$ a9 z8 `3 _) F/ z# q3 D% S1 | {
; F: I% O: A* \, _0 U& q printf("a is %d\n", pshreRAM->a);
) j, r) `' l' l4 e7 [5 \0 |3 q printf("b is %d\n", pshreRAM->b);5 X2 @# u$ [' x$ Z. W; u! F7 ~1 p
printf("count is %d\n", pshreRAM->packet_cout);
" \; O! ?1 [ x$ ^% h' w, Z, _! Z count_copy = pshreRAM->packet_cout;
& k o- q5 ^4 H1 H, m6 `- B" ~ }
. V$ ~) @- I" } else
/ r* D$ c* J2 D5 K {
. A3 ~& z# k. o printf("No effective message!\n");
$ W6 v+ z, k: x* Q3 f1 G0 o }
' _- I( X4 t: v/ w: u$ [ r, w- ]}
' m6 N) M5 ?! F- K$ l0 N% s, H" R5 b" [1 z" u& e
没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???5 J! M& {- k3 G0 K
, {" J ]% _. n8 S: b
- f6 y, f" o4 [8 x+ A
$ H& G8 S3 c& z# m1 E8 f4 k0 p
! O6 h& }9 R% @# r/ t! S |
|