yuphone 发表于 2010-11-26 11:00:35

关于Nios II的数据类型的一点想法

鄙人觉得Nios II HAL自带的数据类型宏真的不好用,而且还有个alt_前缀,移植很麻烦。

代码1 alt_types.h
#ifndef __ALT_TYPES_H__
#define __ALT_TYPES_H__

/******************************************************************************
*                                                                           *
* License Agreement                                                         *
*                                                                           *
* Copyright (c) 2003 Altera Corporation, San Jose, California, USA.         *
* All rights reserved.                                                      *
*                                                                           *
******************************************************************************/

/*
* Don't declare these typedefs if this file is included by assembly source.
*/
#ifndef ALT_ASM_SRC
typedef signed charalt_8;
typedef unsigned charalt_u8;
typedef signed short alt_16;
typedef unsigned short alt_u16;
typedef signed long alt_32;
typedef unsigned long alt_u32;
typedef long long alt_64;
typedef unsigned long long alt_u64;
#endif

#define ALT_INLINE      __inline__
#define ALT_ALWAYS_INLINE __attribute__ ((always_inline))
#define ALT_WEAK          __attribute__((weak))

#endif /* __ALT_TYPES_H__ */



我习惯于使用STM32的数据类型宏,因为都是32位的RISC处理器,所以都是通用的,推荐大家也使用。哈哈,看起来都比较清爽。
代码2 my_types.h
// copy from stm32f10x_type.h

#ifndef MY_TYPES_H_
#define MY_TYPES_H_

typedef signed longs32;
typedef signed short s16;
typedef signed chars8;

typedef signed longconst sc32;/* Read Only */
typedef signed short const sc16;/* Read Only */
typedef signed charconst sc8;   /* Read Only */

typedef volatile signed longvs32;
typedef volatile signed short vs16;
typedef volatile signed charvs8;

typedef volatile signed longconst vsc32;/* Read Only */
typedef volatile signed short const vsc16;/* Read Only */
typedef volatile signed charconst vsc8;   /* Read Only */

typedef unsigned longu32;
typedef unsigned short u16;
typedef unsigned charu8;

typedef unsigned longconst uc32;/* Read Only */
typedef unsigned short const uc16;/* Read Only */
typedef unsigned charconst uc8;   /* Read Only */

typedef volatile unsigned longvu32;
typedef volatile unsigned short vu16;
typedef volatile unsigned charvu8;

typedef volatile unsigned longconst vuc32;/* Read Only */
typedef volatile unsigned short const vuc16;/* Read Only */
typedef volatile unsigned charconst vuc8;   /* Read Only */

typedef enum {FALSE = 0, TRUE = !FALSE} bool;

#endif /* MY_TYPES_H_ */


下面给个my_types.h的使用范例。
代码3 PIO寄存器结构体
#include "my_types.h"

// PIO Data structure
typedef struct
{
    vu32 DATA         : 32;
    vu32 DIRECTION      : 32;
    vu32 INTERRUPT_MASK : 32;
    vu32 EDGE_CAPTURE   : 32;

}PIO_T;

lieshi 发表于 2010-11-26 13:07:27

不错,值得借鉴
页: [1]
查看完整版本: 关于Nios II的数据类型的一点想法