⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi.c

📁 Blackfin 533 SPI接口驱动程序
💻 C
字号:

//
// Code to read some parameters from the flash
//
//
//
#include <sys\exception.h>
#include <cdefBF532.h>

#define SPISE1ON() *pSPI_FLG = 0x0108
#define SPISE1OFF() *pSPI_FLG = 0x0908
#define ECG_CACHE_LEN 2000
#define ECG_CHANNELS 8
typedef unsigned char BYTE;
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned int WORD;
U8 channel[ECG_CHANNELS]={0x00,0x08,0x10,0x18,0x20,0x28,0x30,0x38};
U16 ECG_RAW[ECG_CHANNELS][ECG_CACHE_LEN];
U16 praw=0;
//00000000
//00001000
//00010000
//00011000
//00100000
//00101000
//00110000
//00111000
//Function Get ADC result from SPI
//U8 ch must be channel[0..ECG_CHANNELS] which means the next channel to be sampled.
//ch DO NOT mean the current channel!
//return: sample result of U16
U16 GetADC(U8 ch)
{
	*pSPI_TDBR = ch<<8;
	SPISE1ON();
	WORD stat = *pSPI_STAT;
	while( (stat&TXS) == TXS )
		stat = *pSPI_STAT;
		
	while( (stat&RXS) != RXS )
		stat = *pSPI_STAT;
		
	U16 b = *pSPI_RDBR;
	SPISE1OFF();
	return b>>2;
}

//SPI delay for test
void SPIStablize(void)
{
	int i, j;
	// wait until spi is spablized 
	for( i = 0; i < 40 ; ++i)
		for (j=0; j<0xaff; j++)
			asm ( "	nop; ");
}

void InitADC(void)
{

	WORD stat = *pSPI_STAT;
	// Make sure this is shut off
	*pDMA5_CONFIG = 0x0000;

	// Enable PF2
	// Set baud rate SCK = HCLK/(2*SPIBAUD) SCK = 2MHz	
	*pSPI_BAUD = 24;//24; //SCLK_SPEED/9216000;
	// configure spi port
	// SPI read, 16-bit data, MSB first, SPI Master, enable
	*pSPI_CTL = MSTR | SPE | GM | SIZE|CPOL|0x01|CPHA;
	*pSPI_FLG = 0x0108;

	while( (stat&RXS) == RXS )
	{
		BYTE b = *pSPI_RDBR;
		stat = *pSPI_STAT;
	}
	*pSPI_STAT = 0xFF;//cleanup
	// wait until spi is spablized 
	SPIStablize();
}

void Init_EBIU(void)
{
	*pEBIU_AMBCTL0	= 0x7bb07bb0;
	*pEBIU_AMBCTL1	= 0x7bb07bb0;
	*pEBIU_AMGCTL	= 0x000f;
}

void main()
{
	U8 c;
    Init_EBIU();
	*((volatile unsigned char *)0x20300000)=0x00;//init ext board port
	InitADC(); 
	GetADC(0);//start

	while(1)
	{
	    for(c=0;c<ECG_CHANNELS-1;c++)
	    {
	        ECG_RAW[c][praw]=GetADC(channel[c+1]);
	    }
	    ECG_RAW[c][praw]=GetADC(channel[0]);
	    if( praw++>=ECG_CACHE_LEN) praw=0;
		SPIStablize();
	}
			
	// disable spi
	*pSPI_CTL = 0x0000;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -