Altera_Forum
Honored Contributor
19 years agoNIOS II SPI HAL driver open code
#ifndef __ALT_AVALON_SPI_H__# define __ALT_AVALON_SPI_H__
/****************************************************************************** * File: quartus60\sopc_builder\components\altera_avalon_spi\ * * HAL\inc\altera_avalon_spi.h * * Auth: lvjian (lvjian75@126.com) * * date: 2006.8.1 * * version: 0.1 * ******************************************************************************/ # include <stddef.h> # include "sys/alt_dev.h"# include "sys/alt_alarm.h"# include "sys/alt_warning.h" # include "os/alt_sem.h"# include "os/alt_flag.h" /* If the user wants all drivers to be small rather than fast then make sure * this one is marked as needing to be small. */# define ALT_AVALON_SPI_BUF_LEN (16) /* * Device structure definition. This is needed by alt_sys_init in order to reserve memory * for the device instance. */ typedef struct { int pos; int datalen; alt_u8 buf[ALT_AVALON_SPI_BUF_LEN]; }spi_data_buf; typedef struct { alt_dev dev; /* The device callback structure */ void* base; /* The base address of the device */ alt_u32 irq_enable; alt_u32 ctrl; /* Shadow value of the control register */ spi_data_buf rx_data; /* Start of the pending receive data */ spi_data_buf tx_data; /* End of the pending receive data */ alt_u8 ismaster; /* master or slave work mode */ alt_u8 databits; /* the number of the interface bits */ ALT_FLAG_GRP (events) /* Event flags used for * foreground/background in mult-threaded * mode */ ALT_SEM (rx_wait) /* interrupt isr notify the reader */ ALT_SEM (tx_wait) /* interrupt isr notify the writer */ } alt_avalon_spi_dev; extern int alt_avalon_spi_read (alt_fd* fd, char* ptr, int len); extern int alt_avalon_spi_write (alt_fd* fd, const char* ptr, int len); extern void alt_avalon_spi_init (alt_avalon_spi_dev* dev, int base, int irq); extern int alt_avalon_spi_ioctl (alt_fd* fd, int req, void* arg); # define SPI_MASTER_IOCTL_SELECT_SLAVE 0x01 /* * Macros used by alt_sys_init */ # define ALTERA_AVALON_SPI_INSTANCE(name, device) static alt_avalon_spi_dev device = { { ALT_LLIST_ENTRY, name##_NAME, NULL, /* open */ NULL, /* close */ alt_avalon_spi_read, alt_avalon_spi_write, NULL, /* lseek */ NULL, /* fstat */ alt_avalon_spi_ioctl, }, } # define ALTERA_AVALON_SPI_INIT(name, dev) { if (name##_ISMASTER) dev.ismaster = 1; else dev.ismaster = 0; dev.databits = name##_DATABITS; if (name##_IRQ == ALT_IRQ_NOT_CONNECTED) { ALT_LINK_ERROR ("Error: Interrupt not connected for "# dev ". " "You have selected the interrupt driven version of " "the ALTERA Avalon JTAG UART driver, but the " "interrupt is not connected for this device. You can " "select a polled mode driver by checking the 'small " "driver' option in the HAL configuration window, or " "by using the -DALTERA_AVALON_JTAG_UART_SMALL " "preprocessor flag."); } else alt_avalon_spi_init (&dev, name##_BASE, name##_IRQ); } /* * */ # endif /* __ALT_AVALON_SPI_H__ */