|
|
本帖最后由 zc_fly 于 2014-8-19 16:53 编辑
9 T7 l# S* { {$ N' C# F0 M1 A
OMAPL138的ARM端对共享区内存进行直接访问,代码如下所示:#include <stdio.h>
9 l$ `) V% H- U5 y1 e#include <unistd.h>
: O: F, ^! D) n x R#include <sys/mman.h>
8 s3 l, J* f' `6 n6 V#include <sys/types.h>
c. u R( {1 K( R. y1 j#include <fcntl.h>
6 ]% k2 O. C- L' s) n- A1 _: j4 ` i
#define SHAER_RAM_BASE_ADDR (0x80000000)
, U0 B+ U/ q- A0 \0 n; c9 X
9 H/ i" x, O1 xtypedef struct( A2 p0 V2 l& a+ w" E
{
_! ~+ v, s6 B. u; S8 ^ unsigned int a;
8 v% T* I' z* k unsigned int b;
1 f* y8 T! h$ C6 l6 @ unsigned int packet_cout;2 d1 u# z( G8 N. v+ V9 E3 q: [7 l
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
; H0 e) g9 Q5 ~& h& F$ {
, f( V# v" q( ~7 vvoid read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM);
) q, `" b4 e5 ?2 l6 k n: Junsigned int count_copy = 0;
8 M. M+ U6 ?1 ]7 Q+ ~7 a0 y+ x8 T- p/ b4 L! L) n9 E2 `- S
6 A8 _, J: G; w2 g& i, l; u- P
int main()5 c0 z+ I! ~) t2 S8 \
{) p5 m/ _' p0 f
pRX_MSG_PROTOCOL pshreRAM = NULL;) W- ~1 G w/ [
pshreRAM = (pRX_MSG_PROTOCOL)SHAER_RAM_BASE_ADDR;$ v- q' Q# h, _7 E3 c" l/ G
' w* h. \2 \, Y0 a
while(1)
8 X" I( T7 `' O. V! k K {* l$ ?7 x6 }1 B) K" J. r
read_MSG_buffer(pshreRAM);
1 H$ q6 }. _8 I* Z. `' e4 o0 v }
( w! Y B, D8 n7 U}, P" l" {5 o/ F/ T, h
) {5 F8 p( {/ h# x7 S, b5 y. l% d
void read_MSG_buffer(pRX_MSG_PROTOCOL pshreRAM)# Q' D# `$ T( M$ V+ k
{. ~' z$ F$ o: H) H. \! a _
RX_MSG_PROTOCOL buf;. d* a% Y0 U/ n9 J9 q
" I, i' ~( Q' j
buf.a = pshreRAM->a;
/ h, W" f' G* q) H% H9 v' W buf.b = pshreRAM->b;1 f3 L5 x. X) W
buf.packet_cout = pshreRAM->packet_cout;
& J* _6 `. d; ]9 g. `5 e# C ! R& V9 S* _- i0 B, b! o4 Z
if(buf.packet_cout != count_copy)
f0 z2 c( |( O; U, Z {
% _0 j! \' S( i$ x% } printf("a is %d\n", buf.a);: k% L* Y4 X0 {* l# |
printf("b is %d\n", buf.b);+ h. J2 g1 S# V6 I2 n* q% y, M
printf("count is %d\n", buf.packet_cout);5 c# T7 c# |5 t# ^) g' L+ c/ I
count_copy = buf.packet_cout;5 F" h/ [* }1 u: @( L9 g
}
4 B) c+ p" E* S; r/ D# ] else
' ~) k f9 \: r# z8 n {
; |, I6 p4 X$ w y printf("No effective message!");% ^9 i4 V/ k( X7 B# \! P
}
! S2 S c, c5 r, q; C}, Y% b8 {4 b) v: d/ l6 j$ a2 Z
& R e* Q& b& \, M- h# _* v
- C& o4 \" g0 C9 i) c8 r7 n
但是出现segmentation fault的问题,这是什么原因啊???我在DSP端烧写了一段小程序,向共享区0x80000000位置写入数据,用CCS 测试发现写入成功。
+ L% r4 V7 ~( x6 v+ W8 z8 W使用下面代码,对内存使用了mmap函数后:! Y( |' G1 i0 x" s5 q
#include <stdio.h>
}$ J& \4 z8 }; N" {: W! g#include <unistd.h>7 [; x' T$ v: b1 @
#include <sys/mman.h>
5 v# G% b6 | K! H, y#include <sys/types.h>: x. i' W+ H* D: g# d. G
#include <fcntl.h>
4 s) O6 x6 r8 U6 g4 y" J
, T5 W$ c( t C8 N+ c! j% n O: Z#define SHAER_RAM_BASE_ADDR (0x80000000)6 W# g& e7 ^% G9 c+ G
#define SHAER_RAM_SIZE (0x20000) $ K" E& [9 N! U' E
9 w9 V" Y, P+ ?
typedef struct, P$ J! I3 S( j+ Q. Y5 Y. N% J
{
# J# M, w6 H g- a3 b# N unsigned int a;$ B0 n* k5 J# d0 b+ O
unsigned int b;; {+ Y; A7 T; c- h' l
unsigned int packet_cout;, q3 _: M0 F$ P
}RX_MSG_PROTOCOL, *pRX_MSG_PROTOCOL;
, [* p, ^7 ^1 `0 ~- z" F/ p( }& V1 Q7 w1 U
void read_MSG_buffer(int *baseaddr);
% M& B3 S6 q# _. eunsigned int count_copy = 0;% q+ E% F7 t' U, H9 D% \3 Z
% k4 Y# r3 \8 e. }: uint main(); F1 E f, B" o# B! Z; `4 C
{
7 R" x" f. W! J& @$ `0 L int fd;
* ?& B3 l; Z! @% O' J* r% n% @4 q int *mem = NULL;% ?8 |. S1 @) A2 `6 y8 c3 T8 p
, h) j! @+ R$ Y; G, S, J if((fd = open("/dev/mem", O_RDWR)) <0)
Z' E. f2 [. w9 R {
) H$ T1 l& E7 Y* j* t' f perror("open error");
' }. m, |: G ~% l return -1;
+ P5 o0 P5 r/ c9 _. S }
% Y2 B0 e8 ]8 O y: H$ E
0 ~* R( l( ^' G% U* i" {" K8 g mem = mmap((void *)SHAER_RAM_BASE_ADDR, SHAER_RAM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);. p' ^0 a8 T! P7 c% @! Y. ^
) L) u: i" i; |- s8 p: ]& n while(1)" H7 t4 }4 B* _# B
{. H, ?: J; L% y7 ^
read_MSG_buffer(mem);* x0 J4 s. |# @' q; W1 I
} 1 u- k0 w: U& k+ b
}
) v2 A+ I# v0 L$ }# u* H7 T& S8 v2 C5 ?$ B
void read_MSG_buffer(int *baseaddr)/ i# Q: |; _; d( n, Z' P5 f, u& y
{, V$ m0 H5 n E4 f/ N
pRX_MSG_PROTOCOL pshreRAM = NULL;
* w8 j9 H& k: i! t. N/ k- n
* d; A @4 |! p pshreRAM = (pRX_MSG_PROTOCOL)baseaddr;
+ }# s5 i* n8 G- m4 t3 c& r( ] p; i6 `% [- B' O
if(pshreRAM->packet_cout != count_copy), }0 c0 g0 u0 G
{
) n0 K# N/ @7 P' n9 M, G/ p0 a printf("a is %d\n", pshreRAM->a);* j) i, _% Y6 a" H- L
printf("b is %d\n", pshreRAM->b);
1 M8 X% ` _5 \% n+ l$ C printf("count is %d\n", pshreRAM->packet_cout);
9 W) X* `, L8 e$ M( R) I0 H' T count_copy = pshreRAM->packet_cout;$ ~3 u$ e" G1 F$ C, L U; x
}, V) d; J5 V2 B) s
else
+ U9 } B9 V# `3 \, m& s& T {
4 R$ F8 P' L3 q; G t& |. f8 C printf("No effective message!\n");) z r0 a2 V2 Z4 L5 V
}
. h0 l7 ?( Q! _' z2 T0 [6 u3 V" ^}. {' r1 y3 v. _7 D2 @. z6 y I# D
9 K5 W. O$ Y$ o7 Q4 g' ^没有出现segmentation fault这个错误,但是读出来的数据均为0。这该怎样解决啊???主要问题还是在Linux系统下,OMAPL138是否可以直接访问映射地址???该怎样实现???
9 s2 ~; s. i7 j* V6 v/ v5 O( H
$ u- S* E" v0 |- |; k5 U! ?: l: A+ d$ G5 e# R4 U
9 D- X" ?& A& Q& H9 L% o# v
4 Z5 ?4 p& G; t( ?* V6 S2 @
|
|