Sl6087 - spi


#1

I have a problem with the reading from an eeprom on SPI.

The model of eeprom that i’m using is MicroChip 25LC1024. Here is the datasheet of the chip:http://ww1.microchip.com/downloads/en/DeviceDoc/22064B.pdf

I have a development kit for the SL6087 to which i’ve connected the eeprom.
The connections from the SPI 1to the eeprom, are the secvent:

SL 6087 DEV KIT - EEPROM
SPI 1-IO/GPIO13 - SO (pin 2)
SPI 1-I/GPIO19 - SI (pin 5)
SPI 1-CLK/GPIO12 - SCK (pin 6)
SPI 1-CS/GPIO20 - CS (pin 1)
VCC_2v8 - VCC (pin 8 )
I’m not a guru in electronics, so i’m guessing that the connections are in the correct order.

My test code for the SPI is the next one:

#include "adl_global.h"
#include "adl_bus.h"

/* code utils */
ascii* toString(u32 value){
	ascii *text;
	text = (ascii *)adl_memGet(20);
	wm_itoa(value,text);
	return text;
};

int lengthOfU(u8 * str)
{
    int i = 0;
    while(*(str++)){
        i++;
        if(i == INT_FAST32_MAX)
            return -1;
    }
    return i;
};

int lengthOfUC(ascii * str)
{
    int i = 0;
    while(*(str++)){
        i++;
        if(i == INT_FAST32_MAX)
            return -1;
    }
    return i;
};

ascii* GetAsciiChars(u8* value)
{
	ascii* item=(ascii*)adl_memGet(lengthOfU(value));
	wm_sprintf(item,"%s",value);
	wm_strcat(item,"\0");
	return item;
};

u8* GetUChars(ascii* value){
	u8 * returnValue=(u8 *)malloc(wm_strlen(value));
	memcpy(returnValue,value,wm_strlen(value));
	return returnValue;
};


adl_busAccess_t SpiBus_Access;

// Bus access structure compute function
void ComputeBusAccess ( u8 Opcode, u32 Address )
{
	// Compute Bus Access structure from provided opcode & address
    u8 OpcodeMax = 32, AddressMax = 32;
    u8 OpcodeLength = 8, AddressLength = 16;

    // Set opcode
    SpiBus_Access.Opcode  = Opcode  << ( OpcodeMax - OpcodeLength );
    // Check if address is required
    if ( Address != 0xFFFF )
    {
      // Set address
      SpiBus_Access.Address = Address << ( AddressMax - AddressLength );
    }
    else
    {
       AddressLength = 0;
       SpiBus_Access.Address = 0;
    }
       TRACE((1,"ComputeBusAccess  - OPCODE: %d, Address: %d",SpiBus_Access.Opcode,SpiBus_Access.Address));
}


const u16 wm_apmCustomStackSize = 1024;

// SPI Subscription data
adl_busSPISettings_t MySPIConfig =
{
		5,                          // Clk_Speed    (2 MHz)
        ADL_BUS_SPI_CLK_MODE_0,     // Clk_Mode;
        ADL_BUS_SPI_ADDR_CS_GPIO,   // ChipSelect;
        ADL_BUS_SPI_CS_POL_LOW,     // ChipSelectPolarity;
        ADL_BUS_SPI_MSB_FIRST,      // LsbFirst;
        ADL_IO_GPIO | 20,           // GpioChipSelect;
        ADL_BUS_SPI_FRAME_HANDLING, // WriteHandling;
        ADL_BUS_SPI_DATA_UNIDIR,    // DataLinesConf;
        ADL_BUS_SPI_MASTER_MODE,
        ADL_BUS_SPI_BUSY_UNUSED
};

// Write/Read buffer sizes
#define WRITE_SIZE 5
#define READ_SIZE 3


// BUS Handles
s32 MySPIHandle;
// Somewhere in the application code, used as an event handler

void SpiWR()
{
  	int a;
    u32 AddSize=0;
    // Subscribe to the SPI1 BUS
/*============================== SUBSCRIBE TO BUS======================================================= */
    MySPIHandle = adl_busSubscribe ( ADL_BUS_ID_SPI, 1, &MySPIConfig );
    TRACE((1,"BUS HANDLE: %d",MySPIHandle));
	switch(MySPIHandle){
	case OK:
		 TRACE((1,"adl_busSubscribe  - OK"));
		break;
	case ADL_RET_ERR_PARAM:
		 TRACE((1,"adl_busSubscribe  - ADL_RET_ERR_PARAM"));
		break;
	case ADL_RET_ERR_ALREADY_SUBSCRIBED :
		 TRACE((1,"adl_busSubscribe - ADL_RET_ERR_ALREADY_SUBSCRIBED"));
		break;
	case  ADL_RET_ERR_BAD_HDL:
		 TRACE((1,"adl_busSubscribe  - ADL_RET_ERR_BAD_HDL"));
		break;
	case ADL_RET_ERR_NOT_SUPPORTED:
		 TRACE((1,"adl_busSubscribe  - ADL_RET_ERR_NOT_SUPPORTED"));
		break;
	case ADL_RET_ERR_SERVICE_LOCKED:
		 TRACE((1,"adl_busSubscribe  - ADL_RET_ERR_SERVICE_LOCKED"));
		break;
	}
    adl_busIOCtl ( MySPIHandle, ADL_BUS_CMD_SET_ADD_SIZE, &AddSize );

    ComputeBusAccess(0x09,0x0001);
    ascii* text=(ascii*)adl_memGet(16);
    text="information info";

    int i=wm_strlen(text);
    u16 sizeofE=sizeof(u8)*i;
    TRACE((1,wm_strcat(toString(i),"|- Length to Write\r\n")));

    u8* Writeitem=GetUChars(text);
/*============================== WRITE TO BUS======================================================= */
    a=adl_busWrite ( MySPIHandle, &SpiBus_Access,i,Writeitem);
    TRACE((1,"write error: %d",a));
    switch(a){
    case OK:
    	TRACE((1,"adl_busWrite  - OK"));
    	adl_atSendResponse(ADL_AT_UNS,"adl_busWrite  - OK\r\n");
    	TRACE((1,"adl_busWrite - First Letter %c",Writeitem[0]));

    	/* setting an ascii string for setting to Debug UART*/
    	ascii* textS=(ascii*)adl_memGet(100);
    	wm_memset(textS,'\0',100);
    	wm_strcat(textS,text);
    	wm_strcat(textS,"|- content wrote to memory\r\n");
    	adl_atSendResponse(ADL_AT_UNS,textS);
    	/**/
    	break;
    case ERROR :
    	TRACE((1,"adl_busWrite  - ERROR"));
    	break;
    case  ADL_RET_ERR_UNKNOWN_HDL:
    	TRACE((1,"adl_busWrite  - ADL_RET_ERR_UNKNOWN_HDL"));
    	break;
    case ADL_RET_ERR_PARAM:
    	TRACE((1,"adl_busWrite  - ADL_RET_ERR_PARAM"));
    	break;
    case ADL_RET_ERR_SERVICE_LOCKED:
    	TRACE((1,"adl_busWrite  - ADL_RET_ERR_SERVICE_LOCKED"));
    	break;
    }

	//ComputeBusAccess(0x09,0x0001);
    u8 *Readitem =adl_memGet(i);
    wm_memset(Readitem,'\0',sizeofE);
	TRACE((1, wm_strcat(toString(i),"|- Length to READ\r\n")));
/*============================== READ FROM BUS======================================================= */
    a=adl_busRead ( MySPIHandle , &SpiBus_Access, i , Readitem);
    switch(a){
    case OK:
    	TRACE((1,"adl_busRead  - OK"));
    	adl_atSendResponse(ADL_AT_UNS,"adl_busRead  - OK\r\n");
    	/* testing if is null or empty*/
    	if(Readitem=='\0'){
    		TRACE((1,"adl_busRead - OK - Length \0"));
    	}else if(Readitem==NULL){
    		TRACE((1,"adl_busRead - OK - Length NULL"));
    	}
    	/* compare two infos */
    	if(wm_strcmp(Readitem,Writeitem)==0){
    		TRACE((1,"adl_busRead - Ok - There are the same\r\n"));
    	}
    	TRACE((1,"adl_busRead - Content Length  of ReadItem: %d",wm_strlen(Readitem)));
    	TRACE((1,"adl_busRead - Content Length  of ReadItem: %d",lengthOfU(Readitem)));
    	TRACE((1,"adl_busRead - First Letter of u8 ReadItem : %c",Readitem[0]));
    	TRACE((1,"adl_busRead - Read Item : ",Readitem));
    	ascii* text1=GetAsciiChars(Readitem);
    	TRACE((1,"adl_busRead - First Letter of char ReadItem : %c",text1[0]));
    	adl_atSendResponse(ADL_AT_UNS, wm_strcat(text1,"|-  content read from memory\r\n"));
    	break;
    case ERROR :
    	TRACE((1,"adl_busRead  - ERROR"));
    	break;
    case ADL_RET_ERR_UNKNOWN_HDL:
    	TRACE((1,"adl_busRead  - ADL_RET_ERR_UNKNOWN_HDL"));
    	break;
    case ADL_RET_ERR_PARAM:
    	TRACE((1,"adl_busRead  - ADL_RET_ERR_PARAM"));
    	break;
    case ADL_RET_ERR_SERVICE_LOCKED:
    	TRACE((1,"adl_busRead  - ADL_RET_ERR_SERVICE_LOCKED"));
    	break;
    }
/*============================== UNSUBSCRIBE FROM BUS======================================================= */
    switch(adl_busUnsubscribe ( MySPIHandle )){
		case OK :
			TRACE((1,"adl_busUnsubscribe  - OK"));
			break;
		case ADL_RET_ERR_UNKNOWN_HDL:
			TRACE((1,"adl_busUnsubscribe  - ADL_RET_ERR_UNKNOWN_HDL"));
			break;
		case ADL_RET_ERR_BAD_STATE:
			TRACE((1,"adl_busUnsubscribe  - ADL_RET_ERR_BAD_STATE"));
			break;
		case ADL_RET_ERR_SERVICE_LOCKED:
			TRACE((1,"adl_busUnsubscribe  - ADL_RET_ERR_SERVICE_LOCKED"));
			break;
    }
    }
    void adl_main(adl_InitType_e adlInitType)
    {
       adl_atSendResponse(ADL_AT_UNS, "\r\nMain Program\r\n");
       SpiWR();

    }

The result of the code is presented in the atachement picture.

As you can see in the debug console the two strings aren’t the same.
I don’t understand way i’m receiving more bytes then i’m sending to the eeprom.

In the SPI Communication sample from the Developer Studio, i saw that the adl_BusSettings -> Gpio Chip Select is set to 35. If think this number is the number of the pin on witch SPI make the Chip Selection. In my case the pin that i’m using is SPI1-CS/GPIO20 (i think is pin 20).

So if someone who is more experienced whould help me, i’ll be forever grateful to him.
If there is a problem in my code please let me know.
More examples for this SPI interface of SL6087 whould be very usefull.

Thank you.



#2

Hi, i have this code to manage a MMA7455L accelerometer with my SL6087, maybe it can help you:

// Global variables

u32 spidlen = 1;
u32 spi_Handler;
u8 wspidata;
u8 SpiRdBuf;
u32 opsize = 0;
u32 datasize = 8;



adl_ioDefs_t salidasConfig [ 1 ] = {
(ADL_IO_GPIO | 20) | ADL_IO_DIR_OUT | ADL_IO_LEV_HIGH // SPI_CS
};

adl_busAccess_t SPIaccess =
{
		0, 		//address
		0		//opcode
};

#define outSpiCs &salidasConfig[0]


//Write function (call with u8 type address and data parameters):

void spi_write ( u8 addr, u8 data )
{
	addrrd = (((addr & 0x3f)<<1)|0x80);
	wspidata = data;

	adl_ioWriteSingle(salidasHandle, outSpiCs, FALSE);
	adl_busWrite (spi_Handler, (adl_busAccess_t *) &SPIaccess, spidlen, (void*) &addrrd);
	if ( adl_busWrite(spi_Handler, (adl_busAccess_t *) &SPIaccess, spidlen, (void*) &wspidata) == OK)
	{
		adl_atSendResponse(ADL_AT_UNS,"\r\n SPI Write OK\r\n");
	}
    adl_ioWriteSingle(salidasHandle, outSpiCs, TRUE);

    return;
}

//Read function (call with u8 type address parameter):

u8 spi_read ( u8 addr )
{
	addrrd = ((addr & 0x3f)<<1);

	adl_ioWriteSingle(salidasHandle, outSpiCs, FALSE);
	if (adl_busWrite (spi_Handler, &SPIaccess, spidlen, &addrrd) == OK)
	{
		adl_busRead (spi_Handler, &SPIaccess, spidlen, &SpiRdBuf);
	}
	adl_ioWriteSingle(salidasHandle, outSpiCs, TRUE);

	wm_ibuftohexa ((ascii*)&spirsp, &SpiRdBuf, 1);

	return FALSE;
}


//In MAIN_TASK function:

spi_Handler = adl_busSubscribe ( ADL_BUS_ID_SPI, 1, &SpiSettings );
adl_busIOCtl (spi_Handler, ADL_BUS_CMD_SET_OP_SIZE , &opsize);
adl_busIOCtl (spi_Handler, ADL_BUS_CMD_SET_ADD_SIZE , &opsize);
adl_busIOCtl (spi_Handler, ADL_BUS_CMD_SET_DATA_SIZE , &datasize);

Cheers!