Hello,
Im trying for implement a SPI comunication and my code is the following:
#define WRITE_SIZE 5
#define READ_SIZE 5
u8 WriteBuffer[WRITE_SIZE], ReadBuffer[READ_SIZE];
static s32 HandleSPI; //SPI handler
adl_busAccess_t AccessConfig=
{
0,0 //No Opcode, no Address
};
static adl_busSPISettings_t SPIconfig =
{
1, //Master max freqclock = 13000/(1+127)~= 102 kHz
ADL_BUS_SPI_CLK_MODE_0, //rest state 0, data valid on rising edge.
ADL_BUS_SPI_ADDR_CS_GPIO, //cs_gio
ADL_BUS_SPI_CS_POL_HIGH, //Chip select signal is high level.
ADL_BUS_SPI_MSB_FIRST, //Send MSB bit first.
ADL_IO_GPIO | 31, //We use a gpio for chip select
ADL_BUS_SPI_LOAD_UNUSED, //The load signal is unused.
ADL_BUS_SPI_DATA_BIDIR, //3 wire mode miso, mosi and clk.
ADL_BUS_SPI_MASTER_MODE, //bus is used in master mode.
ADL_BUS_SPI_BUSY_UNUSED //The busy signal is not used.
};
/*********************** Headers Functions **********************/
void InitSPI(); //Initialize SPI
void SPI_Write(u8 *Data,int DataLenght); //Write Data in SPI Bus
void SPI_Read (u8 *Data,int DataLengh); //Read DAta from SPI Bus
void Function_Read(u8 ID, void * Context);//Function Read
void Function_Write(u8 ID, void * Context); //Function Write
void EchoSPI(); // All data received from SPI BUS is returned another time to SPI BUS
/***********************End Headers Functions*********************/
/**********************Functions**********************************/
void SPI_Write(u8 *Data,int DataLenght) //Write Data in SPI BUS
{
adl_busWrite(HandleSPI,&AccessConfig,DataLenght,Data);
}
void SPI_Read (u8 *Data,int DataLengh) //Read data from the SPI BUS
{
adl_busRead(HandleSPI,&AccessConfig,DataLengh,Data);
}
void Function_Read_Handler(u8 ID, void * Context) // Put read data in ReadBuffer
{
SPI_Read(ReadBuffer,READ_SIZE);
TRACE (( 7, "Reading" )); //Reading
TRACE ((9,"%d%d%d%d%d",ReadBuffer[0],ReadBuffer[1],ReadBuffer[2],ReadBuffer[3],ReadBuffer[4])); //Trace read Buffer
}
void Function_Write_Handler(u8 ID, void * Context) // Put WriteBuffer in SPI bus
{
TRACE (( 7, "Writing" )); //Reading
wm_memset(WriteBuffer,1,WRITE_SIZE); //I set WriteBuffer to '1'
//TRACE ((8,"%d%d%d%d%d",WriteBuffer[0],WriteBuffer[1],WriteBuffer[2],WriteBuffer[3],WriteBuffer[4]));
SPI_Write(WriteBuffer,WRITE_SIZE); //I send WriteBuffer by the SPI bus
}
void InitSPI()
{
HandleSPI = adl_busSubscribe(ADL_BUS_ID_SPI,1,&SPIconfig); //Subscribes to SPI bus
if(HandleSPI>=0) //If subscription OK
{
TRACE (( 6, "suscribed BUS SPI" )); //Suscription to SPI BUS succesful
t_write=adl_tmrSubscribe ( TRUE, 10, ADL_TMR_TYPE_100MS, Function_Write_Handler ); //I write from SPI BUS
t_read=adl_tmrSubscribe ( TRUE, 10, ADL_TMR_TYPE_100MS, Function_Read_Handler ); // I read from SPI BUS
}
else
{
TRACE (( 6, " No suscribed BUS SPI" )); //Suscription failes to SPI BUS
}
}
/************************end Functions ******************************/
/******************************Main******************************/
void main_task ( void )
{
adl_InitType_e adl_InitType = adl_InitGetType ();
InitSPI(); //Initialize SPI
}
The bus suscription is ok, the write process i think that is ok, but when i do the read function, i write in my ReadBuffer the same data that i have send in the write function.
For example:
if my WriteBuffer is ‘11111’ I read ’ 255 255 255 255 255’.
if my WriteBuffer is ‘00000’ I read ‘00000’.
Why that? Is my code correct for do a SPI communication?
If i want do a protocol, this functions are well implemented??
Thank you