cancel
Showing results for 
Search instead for 
Did you mean: 

How to properly reset SPI

Kuikui
Associate III
Posted on February 20, 2011 at 19:06

How to properly reset SPI

#spi2-usart3
17 REPLIES 17
antonio239955
Associate II
Posted on May 17, 2011 at 14:25

I have an application that I want to be able to update in the field. So I want a user friendly code to burn the new code in the device. However: this requirement came late so  Ihad all the code already working, including SPI and everything. Well to be short: I did nothing to change the code, I followed the IAP example from ST, placed it in lower memory, then recompiled the original code at a different location. The IAP ( in application programming) code loads and burns the application code and JUMPS to it. That was all!  I did absolutely no new effort on the old code. And there was no problem with the SPI and any other peripheral. So my idea, at your problem, is : first try to get the same code working in different locations, may be there is something that is not-relocatable and cannot work from the normal reset if not placed in the exact same position.
Kuikui
Associate III
Posted on May 17, 2011 at 14:25

After hours of tests, I think I found the cause... But not sure yet.

It seems like SPI2 ( dunno about the others ) does not like to be powered-down at any ''random'' time.

Without debugger, after the first power-up, it works. And if I ''play'' with the reset button ( pin RST ), sometimes it's still working ... and sometimes it's not. And when it's not, it's not .... until you make another ''lucky'' reset where it will work back.

Same thing with entering low power mode with       PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);

When you enter stop mode, APB1 and APB2 are no more clocked, and if you're unlucky and did it at a time the SPI(2) doesn't want, then you won't be able to re-enable SPI after waking up from stop mode.

When I say ''it's not working'', I mean : or the SCK doesn't clock, either SCK clocks but MOSI freezed at level 1.

Before an SPI send :

RCC->CR = 0x03035883

RCC->CFGR = 0x0051BD0A

RCC->CIR = 0

RCC->APB2/1RSTR = 0

RCC->AHBENR = 0x00000015

RCC->APB2ENR = 0x0000421D

RCC->APB1ENR = 0x18C04000

RCC->BDCR = 0x00008103

RCC->CSR = 0x0C000000

GPIOB->CRH = 0xBBB3EE42

GPIOB->IDR = 0x0000B654

GPIOB->ODR = 0x00001050

GPIOB->LKCR = 0x00000000

SPI2->CR1 = 0x0000034F

SPI2->CR2 = 0

SPI2->SR = 2

SPI2->CRCPR = 7

AFIO->EVCR = 0

AFIO->MAPR = 0

AFIO->EXTICR1 = 0

AFIO->EXTICR2 = 0x00002000

AFIO->EXTICR3 = 0x00002020

AFIO->EXTICR4 = 0

Result is here : http://img225.imageshack.us/i/failsb.jpg/

Whereas executed code is :

    retry = 0;

    SPI_CS_LOW();

    cmd = sFLASH_CMD_RDSR;

    while (!(SPI2->SR & SPI_I2S_FLAG_TXE));

    SPI2->DR = cmd;

    while (!(SPI2->SR & SPI_I2S_FLAG_RXNE));

    cmd = SPI2->DR;

  

    do

    {

      data = sFLASH_DUMMY_BYTE;

      while (!(SPI2->SR & SPI_I2S_FLAG_TXE));

      SPI2->DR = data;

      while (!(SPI2->SR & SPI_I2S_FLAG_RXNE));

      data = SPI2->DR;

      retry++;

    } while ((data & sFLASH_WIP_FLAG) == SET && retry < 101);

    SPI_CS_HIGH();

ASM is below , sent values are 0x05 and 0xA5 ( not 0xFF )

    retry = 0;

 

    0x8003ebe: 0x2000         MOVS      r0, #0

 

    0x8003ec0: 0x0004         MOVS      r4, r0

 

    SPI_CS_LOW();

 

    0x8003ec2: 0x483b         LDR.N     r0, ??DataTable3_3 [0x8003fb0] ; GPIOB_BRR

 

    0x8003ec4: 0xf44f 0x5180  MOV.W     r1, #4096               ; 0x1000

 

    0x8003ec8: 0x6001         STR       r1, [r0]

 

    cmd = sFLASH_CMD_RDSR;

 

    0x8003eca: 0x2005         MOVS      r0, #5

 

    0x8003ecc: 0xf8ad 0x0006  STRH.W    r0, [sp, #0x6]

 

    while (!(SPI2->SR & SPI_I2S_FLAG_TXE));

 

??Flash_WaitForWriteEnd_2:

 

    0x8003ed0: 0x4838         LDR.N     r0, ??DataTable3_4 [0x8003fb4] ; SPI2_SR

 

    0x8003ed2: 0x8800         LDRH      r0, [r0]

 

    0x8003ed4: 0x0780         LSLS      r0, r0, #30

 

    0x8003ed6: 0xd5fb         BPL.N     ??Flash_WaitForWriteEnd_2 ; 0x8003ed0

 

    SPI2->DR = cmd;

 

    0x8003ed8: 0x4837         LDR.N     r0, ??DataTable3_5 [0x8003fb8] ; SPI2_DR

 

    0x8003eda: 0xf8bd 0x1006  LDRH.W    r1, [sp, #0x6]

 

    0x8003ede: 0x8001         STRH      r1, [r0]

 

    while (!(SPI2->SR & SPI_I2S_FLAG_RXNE));

 

??Flash_WaitForWriteEnd_3:

 

    0x8003ee0: 0x4834         LDR.N     r0, ??DataTable3_4 [0x8003fb4] ; SPI2_SR

 

    0x8003ee2: 0x8800         LDRH      r0, [r0]

 

    0x8003ee4: 0x07c0         LSLS      r0, r0, #31

 

    0x8003ee6: 0xd5fb         BPL.N     ??Flash_WaitForWriteEnd_3 ; 0x8003ee0

 

    cmd = SPI2->DR;

 

    0x8003ee8: 0x4833         LDR.N     r0, ??DataTable3_5 [0x8003fb8] ; SPI2_DR

 

    0x8003eea: 0x8800         LDRH      r0, [r0]

 

    0x8003eec: 0xf8ad 0x0006  STRH.W    r0, [sp, #0x6]

 

      data = sFLASH_DUMMY_BYTE;

 

??Flash_WaitForWriteEnd_4:

 

    0x8003ef0: 0x20a5         MOVS      r0, #165                ; 0xa5

 

    0x8003ef2: 0xf8ad 0x0004  STRH.W    r0, [sp, #0x4]

 

      while (!(SPI2->SR & SPI_I2S_FLAG_TXE));

 

??Flash_WaitForWriteEnd_5:

And, after some fails ... I got one working ... : http://img842.imageshack.us/i/works.jpg/

Kuikui
Associate III
Posted on May 17, 2011 at 14:25

Hi,

I come back to this topic, cause I'm facing this issue once again.

Sometimes, the SCK pin of SPI2 of my STM32F102R8 does not clock, while MOSI sends correct data.

What can cause the SCK pin of SPI2 not to clock ?

SPI2 is not remapped, is enabled in RCC, and the pin is in ''alternate push-pull 50MHz'' mode.

Only a hardware reset ( I mean powering the board down then up ) can really ''reset'' the bug, and make SPI working correctly.

upanie
Associate II
Posted on May 14, 2013 at 00:04

Hi.

It was long time ago but have you found solution to this problem?

I have the same issue. My SPI2 SCK (GPIOB Pin13) simply stops clocking when i configure GPIOB Pin11 as RX for USART3. MOSI works fine but SCK is low all the time.

Maybe someone knows something about this strange ''feature''. Errata doesn't say a word about it.
Posted on May 14, 2013 at 01:16

How are you initializing USART3?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
upanie
Associate II
Posted on May 14, 2013 at 09:45

It is not directly caused by USRT3 initialization

as it turned out

. I've found that SCK pin stops generating clock right after configuring pin 11 for USART3. When SPI2 works fine I do the following:

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_Init(GPIOB,&GPIO_InitStructure);

and SCK stops. When I configure Pin 11 before configuring SPI2 everything works. So for now problem is resolved but this seams to be a hardware bug or maybe I'm missing something important.
Posted on May 14, 2013 at 09:58

Can you please start a new thread, post a minimal but complete compilable example of code which produces the error, together with stating what device are you using.

JW

upanie
Associate II
Posted on May 20, 2013 at 12:32

I will do it as soon as i have time.