?? test_mcp2510.c
字號:
/****************************************************************************
【文 件 名 稱】Test_MCP2510.C
【功 能 描 述】三星S3C2410A板demo程序代碼
【程 序 版 本】4.0
【創建人及創建日期】icesoar//2002年11月19日19:26
【修改人及修改日期】icesoar//2004-12-8 17:25
****************************************************************************/
#include "def.h"
#include "2410addr.h"
#include "config.h"
#include "board.h"
#include "utils.h"
#include "MCP2510.h"
/****************************************************************************
MCP2510_CS GPE11 output
MCP2510_SI GPB5 output
MCP2510_SO GPB6 input
MCP2510_SCK GPB7 output
MCP2510_INT GPG0 input (EINT8)
****************************************************************************/
#define MCP2510_DEBUG 0
#define DELAY_TIME 100
#define MCP2510_CS_OUT ( rGPECON = rGPECON & (~(3<<22)) | (1<<22) ) //GPE11
#define MCP2510_CS_H rGPEDAT = rGPEDAT | (1<<11)
#define MCP2510_CS_L rGPEDAT = rGPEDAT & (~(1<<11))
#define MCP2510_SI_OUT ( rGPBCON = rGPBCON & (~(3<<10)) | (1<<10) ) //GPB5
#define MCP2510_SI_H rGPBDAT = rGPBDAT | (1<<5)
#define MCP2510_SI_L rGPBDAT = rGPBDAT & (~(1<<5))
#define MCP2510_SCK_OUT ( rGPBCON = rGPBCON & (~(3<<14)) | (1<<14) ) //GPB7
#define MCP2510_SCK_H rGPBDAT = rGPBDAT | (1<<7)
#define MCP2510_SCK_L rGPBDAT = rGPBDAT & (~(1<<7))
#define MCP2510_SO_IN ( rGPBCON = rGPBCON & (~(3<<12)) | (0<<12) ) //GPB6
#define MCP2510_SO_GET ( rGPBDAT & 0x40 )
#define MCP2510_INT_IN ( rGPGCON = rGPGCON & (~(3<<0)) )
#define MCP2510_INT_GET ( rGPGDAT & 0x01 )
/********************** MCP2510 Instruction *********************************/
#define MCP2510INSTR_RESET 0xc0
#define MCP2510INSTR_READ 0x03
#define MCP2510INSTR_WRITE 0x02
#define MCP2510INSTR_RTS 0x80 //request to send
#define MCP2510INSTR_RDSTAT 0xa0 //read status
#define MCP2510INSTR_BITMDFY 0x05 //bit modify
//***************************************************************************
/****************************************************************************
【功能說明】
****************************************************************************/
void MCP2510_IO_CS_Init( void )
{
MCP2510_CS_OUT ;
MCP2510_SI_OUT ;
MCP2510_SCK_OUT ;
MCP2510_SO_IN ;
MCP2510_SCK_H ; //SCK put 1
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
MCP2510_CS_L ; // Select the MCP2510
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
}
/****************************************************************************
【功能說明】
****************************************************************************/
void Spi_Write( U8 Data )
{
U8 m ;
for( m = 0; m < 8; m++ )
{
MCP2510_SCK_L ; //SCK put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
if( (Data&0x80)==0x80 )
MCP2510_SI_H; //SI put 1
else
MCP2510_SI_L; //SI put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
MCP2510_SCK_H ; //SCK put 1
Data = Data<<1 ;
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
}
}
/****************************************************************************
【功能說明】
****************************************************************************/
U8 Spi_Read( )
{
U8 m ;
U8 data = 0 ;
for( m = 0; m < 8; m++ )
{
MCP2510_SCK_L ; //SCK put 0
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
data=data<<1;
if( MCP2510_SO_GET != 0 )
data |= 0x01 ;
else
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
MCP2510_SCK_H ; //SCK put 1
{ U16 k=0; for( ; k <= DELAY_TIME; k++ ) ; } //延時至少300ns
}
return (data);
}
/****************************************************************************
【功能說明】 Send Command to MCP2510 via SPI
****************************************************************************/
void SendCMDMCP2510( U8 CMD )
{
MCP2510_IO_CS_Init() ; //Initial IO port and CS is select
Spi_Write(CMD );
MCP2510_CS_H ; // Deselect the MCP2510
}
/****************************************************************************
【功能說明】
****************************************************************************/
void MCP2510_Reset()
{
MCP2510_IO_CS_Init() ;
Spi_Write(MCP2510INSTR_RESET);
MCP2510_CS_H ;
}
/****************************************************************************
【功能說明】
****************************************************************************/
void MCP2510_Write(int address, int value)
{
MCP2510_IO_CS_Init() ;
Spi_Write(MCP2510INSTR_WRITE);
Spi_Write((unsigned char)address);
Spi_Write((unsigned char)value);
MCP2510_CS_H ;
}
/****************************************************************************
【功能說明】
****************************************************************************/
unsigned char MCP2510_Read(int address)
{
unsigned char result;
MCP2510_IO_CS_Init() ;
Spi_Write(MCP2510INSTR_READ);
Spi_Write((unsigned char)address);
Spi_Write(0);
result=Spi_Read();
MCP2510_CS_H ;
return result;
}
/****************************************************************************
【功能說明】 Read often used status
//Status 7 6 5 4 3 2 1 0
// | | | | | | | |
// | | | | | | | |___CANINTF.RX0IF
// | | | | | | |_______CANINTF.RX1IF
// | | | | | |___________TXB0CTRL.TXREQ
// | | | | |_______________CANINTF.TX0IF
// | | | |___________________TXB1CTRL.TXREQ
// | | |_______________________CANINTF.TX1IF
// | |___________________________TXB2CTRL.TXREQ
// |_______________________________CANINTF.TX2IF
****************************************************************************/
unsigned char MCP2510_ReadStatus()
{
unsigned char result;
MCP2510_IO_CS_Init() ;
Spi_Write(MCP2510INSTR_RDSTAT);
Spi_Write(0);
result=Spi_Read();
MCP2510_CS_H ;
if( MCP2510_DEBUG ) printf( "StatusREG = 0x%x\n", result ) ;
return result;
}
/****************************************************************************
【功能說明】
****************************************************************************/
void MCP2510_WriteBits( int address, int data, int mask )
{
MCP2510_IO_CS_Init() ;
Spi_Write(MCP2510INSTR_BITMDFY);
Spi_Write((unsigned char)address);
Spi_Write((unsigned char)mask);
Spi_Write((unsigned char)data);
MCP2510_CS_H ;
}
/****************************************************************************
【功能說明】
****************************************************************************/
void MCP2510_SetBandRate(CanBandRate bandrate, int IsBackNormal)
{
//
// Bit rate calculations.
//
//Input clock fre=16MHz
// In this case, we'll use a speed of 125 kbit/s, 250 kbit/s, 500 kbit/s.
// If we set the length of the propagation segment to 7 bit time quanta,
// and we set both the phase segments to 4 quanta each,
// one bit will be 1+7+4+4 = 16 quanta in length.
//
// setting the prescaler (BRP) to 0 => 500 kbit/s.
// setting the prescaler (BRP) to 1 => 250 kbit/s.
// setting the prescaler (BRP) to 3 => 125 kbit/s.
//
// If we set the length of the propagation segment to 3 bit time quanta,
// and we set both the phase segments to 1 quanta each,
// one bit will be 1+3+2+2 = 8 quanta in length.
// setting the prescaler (BRP) to 0 => 1 Mbit/s.
// Go into configuration mode
MCP2510_Write(MCP2510REG_CANCTRL, MODE_CONFIG);
switch(bandrate){
case BandRate_125kbps:
MCP2510_Write(CNF1, SJW1|BRP4); //Synchronization Jump Width Length =1 TQ
MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG4<<3)|SEG7); // Phase Seg 1 = 4, Prop Seg = 7
MCP2510_Write(CNF3, SEG4);// Phase Seg 2 = 4
break;
case BandRate_250kbps:
MCP2510_Write(CNF1, SJW1|BRP2); //Synchronization Jump Width Length =1 TQ
MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG4<<3)|SEG7); // Phase Seg 1 = 4, Prop Seg = 7
MCP2510_Write(CNF3, SEG4);// Phase Seg 2 = 4
break;
case BandRate_500kbps:
MCP2510_Write(CNF1, SJW1|BRP1); //Synchronization Jump Width Length =1 TQ
MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG4<<3)|SEG7); // Phase Seg 1 = 4, Prop Seg = 7
MCP2510_Write(CNF3, SEG4);// Phase Seg 2 = 4
break;
case BandRate_1Mbps:
MCP2510_Write(CNF1, SJW1|BRP1); //Synchronization Jump Width Length =1 TQ
MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG3<<3)|SEG2); // Phase Seg 1 = 2, Prop Seg = 3
MCP2510_Write(CNF3, SEG2);// Phase Seg 2 = 1
break;
}
if(IsBackNormal){
//Enable clock output
MCP2510_Write(CLKCTRL, MODE_NORMAL | CLKEN | CLK1);
}
}
/*******************************************\
* 序列讀取MCP2510數據 *
\*******************************************/
void MCP2510_SRead( int address, unsigned char* pdata, int nlength )
{
int i;
MCP2510_IO_CS_Init() ;
Spi_Write(MCP2510INSTR_READ);
Spi_Write((unsigned char)address);
for (i=0; i<nlength; i++) {
Spi_Write(0);
*pdata=Spi_Read();
pdata++;
}
MCP2510_CS_H ;
}
/*******************************************\
* 序列寫入MCP2510數據 *
\*******************************************/
void MCP2510_Swrite(int address, unsigned char* pdata, int nlength)
{
int i;
MCP2510_IO_CS_Init() ;
Spi_Write(MCP2510INSTR_WRITE);
Spi_Write((unsigned char)address);
for (i=0; i < nlength; i++) {
Spi_Write((unsigned char)*pdata);
pdata++;
}
MCP2510_CS_H ;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -