cancel
Showing results for 
Search instead for 
Did you mean: 

SPI1 Read / MISO problem

alexandre239955
Associate II
Posted on April 02, 2009 at 11:09

SPI1 Read / MISO problem

1 REPLY 1
alexandre239955
Associate II
Posted on May 17, 2011 at 13:08

Hello,

I'm new with STM32 and I have a problem with SPI1. I have written an SPI driver for FRAM (FM25H20) and the Flash. I need to run it on two different boards with the same STM32F103ZET6 uc. On one board, the FRAM is on SPI2, on the other one on SPI1. I am using the same code (exept for GPIO pins).

1. The one with SPI2 works fine, the function to read the register returns 0x42 (correct value after a WREN command).

2. The one with SPI1 does not work. With an oscilloscope, I see the signals MOSI and SCK going to the FRAM (read status register command), and the FRAM responds correctly; I can see the response signal MISO on the pin PA.06 of the chip. But, my read function always return a 0x00 value instead of the 0x42 corresponding to the signal.

My code is the following :

// GPIO pins for SPI bus

#define CPU_MEM_MASTER_PIN_MISO GPIO_Pin_6

#define CPU_MEM_MASTER_PIN_MOSI GPIO_Pin_7

#define CPU_MEM_MASTER_PIN_SCK GPIO_Pin_5

#define CPU_MEM_MASTER_GPIO_SCK_MISO_MOSI GPIOA

// GPIO pins for CS and Hold

#define CPU_MEM_MASTER_PIN_CS_FRAMA GPIO_Pin_2

#define CPU_MEM_MASTER_PIN_HOLD_FRAMA GPIO_Pin_6

#define CPU_MEM_MASTER_GPIO_CS_HOLD GPIOG

unsigned char cpu_mem_ReadFlashFramStatusRegister(void)

{

unsigned char status = 0;

// cs = 0

GPIO_WriteBit(cpu_mem_csGpio, cpu_mem_csPin, Bit_RESET);

// write

cpu_mem_WriteByte(CPU_FRAM_FLASH_CMD_RDSR);

status = cpu_mem_ReadByte();

// cs = 1

GPIO_WriteBit(cpu_mem_csGpio, cpu_mem_csPin, Bit_SET);

return status;

}

/*!

* Write a byte to the SPI bus

*/

unsigned char cpu_mem_WriteByte(unsigned char c)

{

unsigned char byte = 0;

while (SPI_I2S_GetFlagStatus(cpu_mem_spi, SPI_I2S_FLAG_TXE) == RESET)

;

SPI_I2S_SendData(cpu_mem_spi, c);

while (SPI_I2S_GetFlagStatus(cpu_mem_spi, SPI_I2S_FLAG_RXNE) == RESET)

;

byte = SPI_I2S_ReceiveData(cpu_mem_spi);

// if (byte != 0) while(1) // NEVER STUCKS !

return byte;

}

/*!

* Read a byte from the SPI bus

*/

unsigned char cpu_mem_ReadByte(void)

{

unsigned char result;

result = cpu_mem_WriteByte(CPU_MEM_DUMMY_BYTE);

return result;

}

/*!

* Init the SPI device

*/

void cpu_mem_InitSpi(SPI_TypeDef * SPIx)

{

SPI_InitTypeDef SPI_InitStructure;

// SPI2 Periph clock enable

if (SPIx == SPI1)

{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

}

else if (SPIx == SPI2)

{

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

}

// init SPI

// configure SPI for FRAM FM25H20, 8bits, MSB first, Mode 0 or 3

SPI_I2S_DeInit(SPIx);

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

// 8 bits, not 16

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

// Mode 0 : CPOL = 0, CPHA = 0 - only Mode 0 and mode 3 (CPOL=1, CPHA=1) are supported by FRAM FM25H20

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;

// MSB first

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_Init(SPIx, &SPI_InitStructure);

SPI_Cmd(SPIx, ENABLE);

}

/*!

* Init the Gpio pins for Master processor

*/

void cpu_mem_InitMasterGpio(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

// init clock for GPIOA and G

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOG | RCC_APB2Periph_AFIO, ENABLE);

// configure SPI1 pins: SCK (PA.05), MISO (PA.06) and MOSI (PA.07)

GPIO_InitStructure.GPIO_Pin = CPU_MEM_MASTER_PIN_SCK | CPU_MEM_MASTER_PIN_MISO | CPU_MEM_MASTER_PIN_MOSI;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(CPU_MEM_MASTER_GPIO_SCK_MISO_MOSI, &GPIO_InitStructure);

// CS and Hold pins for flashs and frams

GPIO_InitStructure.GPIO_Pin = CPU_MEM_MASTER_PIN_CS_FRAMA | CPU_MEM_MASTER_PIN_HOLD_FRAMA;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(CPU_MEM_MASTER_GPIO_CS_HOLD, &GPIO_InitStructure);

// initialize CS and Hold pins

GPIO_WriteBit(CPU_MEM_MASTER_GPIO_CS_HOLD, CPU_MEM_MASTER_PIN_CS_FRAMA, Bit_SET);

GPIO_WriteBit(CPU_MEM_MASTER_GPIO_CS_HOLD, CPU_MEM_MASTER_PIN_HOLD_FRAMA, Bit_SET);

}

void init(void)

{

cpu_mem_InitMasterGpio();

cpu_mem_spi = CPU_MASTER_PROCESSOR_SPI; // SPI1

cpu_mem_InitSpi(cpu_mem_spi);

}

Note: for the SPI2 processor, I have another function to init the GPIO, but it is the same except that it is GPIOB and pins 13, 14 and 15.

1. The software does not get stuck, it simply reads 0's everywhere.

2. The lines to the FRAM seem to be ok as I can read the answer on the pin of the chip.

3. I have tried to configure the PA.06 pin as Out_PP to see if it was working, it works well (not broken internally).

Can someone please help me ?

Thanks !

[ This message was edited by: alexandre.habersaat on 02-04-2009 14:40 ]

[ This message was edited by: alexandre.habersaat on 02-04-2009 14:41 ]