# how to read and write with 3-wire SPI interface?

**URL:** <https://forum.sierrawireless.com/t/how-to-read-and-write-with-3-wire-spi-interface/2597>\
**Category:** General wireless topics\
**Created:** [October 16, 2008, 7:32am UTC](https://forum.sierrawireless.com/t/how-to-read-and-write-with-3-wire-spi-interface/2597 "2008-10-16T07:32:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gokhan](https://avatars.discourse-cdn.com/v4/letter/g/7993a0/32.png) [@gokhan](https://forum.sierrawireless.com/u/gokhan)\
**Post date:** [October 16, 2008, 7:32am UTC](https://forum.sierrawireless.com/t/how-to-read-and-write-with-3-wire-spi-interface/2597/1 "2008-10-16T07:32:24Z")

</div>

Hi all,  
could somebody tell me how to read and write with 3-wire SPI interface to external M25P16 serial flash on the Q2687 module application?  
any idea please;  
thanks,  
Gokhan

---

<div class="post-metadata">

**Author:** ![yturhan](https://avatars.discourse-cdn.com/v4/letter/y/67e7ee/32.png) [@yturhan](https://forum.sierrawireless.com/u/yturhan)\
**Post date:** [October 16, 2008, 9:00am UTC](https://forum.sierrawireless.com/t/how-to-read-and-write-with-3-wire-spi-interface/2597/2 "2008-10-16T09:00:57Z")

</div>

gokhan merhaba,

You can find my test code for SPI Serial memory access below. I test this code with Atmel AT45DB series serial flash memories on Q2687 6.63c firmware. Code simply initialize SPI bus and sends 0x9F command for reading memory identification data.

I hope it helps,

```auto
#include "adl_global.h"
const u16 wm_apmCustomStackSize = 1024;

#define _SELF_CS_

void tryRead(int opCode);

int intBusHandle = 0;
void* lpReceiveBuffer;
adl_busSPISettings_t sBusSettings;
adl_busAccess_t sBusAccess;

#ifdef _SELF_CS
adl_ioLabel_Q2687_e eIOLabel;
s32 intIOHandler = -1;
#endif

void adl_main ( adl_InitType_e InitType )
{
#ifdef _SELF_CS
	adl_ioConfig_t	sConfig;
	eIOLabel = ADL_IO_Q2687_GPIO_31;
	

	
	sConfig.eDirection = ADL_IO_OUTPUT;
	sConfig.eState = ADL_IO_HIGH;
	sConfig.eLabel.Q2687_Label = eIOLabel;
	sConfig.Pad = 0;
	
	intIOHandler = adl_ioSubscribe(1,&sConfig,ADL_TMR_TYPE_100MS,0,0);
#endif
	
	
	sBusSettings.Clk_Speed = 127;
	sBusSettings.Clk_Mode = ADL_BUS_SPI_CLK_MODE_0;
#ifdef _SELF_CS
	sBusSettings.ChipSelect = ADL_BUS_SPI_ADDR_CS_NONE;
	sBusSettings.GpioChipSelect = 0;
#else
	sBusSettings.ChipSelect = ADL_BUS_SPI_ADDR_CS_GPIO;
	sBusSettings.GpioChipSelect = ADL_IO_Q2687_GPIO_31 ;
#endif
	
	sBusSettings.ChipSelectPolarity = ADL_BUS_SPI_CS_POL_LOW;
	sBusSettings.LsbFirst = ADL_BUS_SPI_MSB_FIRST;
	sBusSettings.WriteHandling = ADL_BUS_SPI_FRAME_HANDLING;
	sBusSettings.DataLinesConf = ADL_BUS_SPI_DATA_UNIDIR;
	
		
	lpReceiveBuffer = adl_memGet(0x0F);
		
	intBusHandle = adl_busSubscribe(ADL_BUS_SPI1,(adl_busSettings_u*)&sBusSettings);
	sBusAccess.Address = 0;
	sBusAccess.AddressLength = 0;
	sBusAccess.OpcodeLength = 8;
	sBusAccess.Size = ADL_BUS_SIZE_BYTE ;
	tryRead(0x9F000000);
}
	

void tryRead(int opCode)
{
	int intReadResult = 0;

#ifdef _SELF_CS
	adl_ioWriteSingle(intIOHandler,eIOLabel,ADL_IO_LOW);
#endif 
	
	sBusAccess.Opcode = opCode;
	wm_memset(lpReceiveBuffer,0x00,0x0F);
	intReadResult = adl_busRead(intBusHandle,&sBusAccess,0x0F,lpReceiveBuffer);
#ifdef _SELF_CS
	adl_ioWriteSingle(intIOHandler,eIOLabel,ADL_IO_HIGH);
#endif
}
```

---

<div class="post-metadata">

**Author:** ![gokhan](https://avatars.discourse-cdn.com/v4/letter/g/7993a0/32.png) [@gokhan](https://forum.sierrawireless.com/u/gokhan)\
**Post date:** [October 21, 2008, 11:20am UTC](https://forum.sierrawireless.com/t/how-to-read-and-write-with-3-wire-spi-interface/2597/3 "2008-10-21T11:20:32Z")

</div>

Thanks a lot… this information is usefull for me.  
Regards  
Gokhan
