DSP

DSP C54X ioport的用法

2019-07-13 12:50发布

The ioport keyword enables access to the I/O port space of the C54x devices. The keyword has the form: ioport type port hex_num ioport is the keyword that indicates this is a port variable.
type must be char, short, int, or the unsigned variable.
porthex_num refers to the port number. The hex_num argument is a hexadecimal number.
All declarations of port variables must be done at the file level. Port variables declared at the function level are not supported. Do not use the ioport keyword in a function prototype.
For example, the following code declares the I/O port as unsigned port 10h, writes a to port 10h, then reads port 10h into b:
ioport unsigned port10; /* variable to access I/O port 10h */
int func ()
{
     ...
     port10 = a;         /* write a to port 10h             */
     ...
     b = port10;         /* read port 10h into b            */
     ...
}
The use of port variables is not limited to assignments. Port variables can be used in expressions like any other variable. Following are examples:
a = port10 + b; /* read port 10h, add b, assign to a       */
port10 += a;     /* read port 10h, add a, write to port 10h */
In calls, port variables are passed by value, not by reference:
call(port10);    /* read port 10h and pass (by value) to call */
call(&port10);    /* invalid pass by reference! */

DSP中如何访问I/O,数据空间 访问I/O空间
I/O空间地址声明
要在程序中访问io空间地址,必须首先用关键字“ioport”对要访问的地址进行定义。
语法:ioport t ype porthex_num
ioport 声明io空间端口变量的关键字;
type 变量类型,可以为char, short, int或unsigned int;
porthex_num 端口号,port后面接16进制数字。

ioport unsigned int port10;

注:声明io空间地址必须在C文件起始声明,不允许在函数中使用ioport声明io空间地址。 I/O空间地址访问
访问用ioport关键字声明的I/O端口变量和访问一般变量没有区别。

ioport unsigned int port10;
int func ()
{
...
port10 = a;
...
b = port10;
...
}
I/O端口变量的使用不仅仅局限于赋值,和其他变量同样也可以应用于其它的表达式。

call (port10); 
a = port10 + b; 
port10 += a; 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
程序中访问的任何一个IO地址都必须在C语言程序起始处用ioport关键字声明!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
访问数据空间
访问数据空间不需要对要访问的单元预先声明,访问是通过指针的方法实现的。

unsigned int org,cnt,block,offset,tmp,i;
org = *(unsigned int *) 0x8000;
cnt = *(unsigned int *) 0x8001;
block = *(unsigned int *) 0x8002;
offset = *(unsigned int *) 0x8003;
for (i=0; i
tmp = *(unsigned int *) (org + i);
*(unsigned int *) (org + offset +i) = tmp;

#define IMR *(volatile unsigned int *)0x0004 为例:
1)0x0004,这个容易理解,是一个十六进制的常数
2)再看(volatile unsigned int *),这是一个强制转换,将0x0004转换成(volatile usigned int *)
3)现在解释volatile usigned int *是什么。* 表示指针(地址),这个指针指向一个 usigned int的数。最前面的volatile有特殊含义:这个指针必须保存在RAM中(不是FLASHDSP寄存器中)
4)综合上面1)2)3)可知: (volatile unsigned int *)0x0004 的含义是:
0x0004是一个地址,在这个地址内保存的是一个unsigned int 类型的整数。这个地址存在于RAM
5)*(volatile unsigned int *)0x0004 最左边多一个*,表示取地址指向单元的内容。综合起来的意思是:从0x0004地址单元中取出一个usigned int 类型的数据值
6)#define IMR *(volatile unsigned int *)0x0004 的意思是,将从0x0004地址单元中取出一个usigned int 类型的数据值定义为IMR
7)根据DSP硬件,在DSPRAM中的 0x0004地址单元恰好是中断管理寄存器!
8)绕了半天,#define IMR *(volatile unsigned int *)0x0004 的意思是:在C语言程序中,定义了一个宏标示符 IMR 来表示DSP中的中断管理寄存器。在程序中,可以通过这个宏标示符来使用这个寄存器:
unsigned int v = IMR; //取出中断管理寄存器的值并赋给v