cancel
Showing results for 
Search instead for 
Did you mean: 

SPI3 conflict with JATG on stm32f103RET

a_patiar
Associate II
Posted on June 16, 2015 at 17:58

Hi

I saw lots of discussions and solutions about working with SPI3 by disabling JTAG on STM32F103 devices. But I already have problem with it.

I am using SWD for program and debug the mcu with Keil and the same code is working flawlessly for SPI1/SPI2.

here is my initialization code:

&sharpdefine SD_SCK_PIN GPIO_Pin_3

&sharpdefine SD_SCK_PRT GPIOB

&sharpdefine SD_MOSI_PIN GPIO_Pin_5

&sharpdefine SD_MOSI_PRT GPIOB

&sharpdefine SD_MISO_PIN GPIO_Pin_4

&sharpdefine SD_MISO_PRT GPIOB

&sharpdefine SD_CS_PIN GPIO_Pin_7

&sharpdefine SD_CS_PRT GPIOC

&sharpdefine SD_SPI SPI3

void SPI_SDMode(void)

{

SPI_InitTypeDef  SPI_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO ,ENABLE );

RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI3,ENABLE );

/* PB3=CLK, PB4=MISO, PB5=MOSI alternate function */

GPIO_InitStructure.GPIO_Pin =  SD_SCK_PIN | SD_MOSI_PIN | GPIO_Mode_IPU;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(SD_SCK_PRT, &GPIO_InitStructure); 

/* Configure PC7 as CS  open drain push-pull output */

GPIO_SetBits(SD_CS_PRT, SD_CS_PIN); //set high.

GPIO_InitStructure.GPIO_Pin = SD_CS_PIN;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(SD_CS_PRT, &GPIO_InitStructure); 

/* SPI configuration */

SPI_Cmd(SD_SPI, DISABLE); //disable SPI for configuration.

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_Init(SD_SPI, &SPI_InitStructure);

/* Enable SPI  */

SPI_Cmd(SD_SPI, ENABLE);

//--------------------------------------

GPIO_SetBits(SD_CS_PRT, SD_CS_PIN);

/* Loop while DR register in not emplty */

    while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET);

/* Send byte through the SPI peripheral */

    SPI_I2S_SendData(SD_SPI, 0xff);

/* Wait to receive a byte */

    while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET);

    /* Return the byte read from the SPI bus */

SPI_I2S_ReceiveData(SD_SPI);

//----------------------------------------------------end

GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE);    

GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);   

}

can anyone help me with this matter?

#spi3 #stm32f103 #jtag
3 REPLIES 3
Posted on June 16, 2015 at 18:13

Probably be advisable to remap the JTAG/SWD off the pins *BEFORE* you try to use the peripheral interface on them?

STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\GPIO\JTAG_Remap\main.c

This looks wrong:

GPIO_InitStructure.GPIO_Pin =  SD_SCK_PIN | SD_MOSI_PIN | GPIO_Mode_IPU;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
a_patiar
Associate II
Posted on June 17, 2015 at 15:59

Thanks clive for your quick reply

It was a silly typo.

I've corrected my code as you mentioned but there is a random behavior on initializing  SPI3 again.

Sometimes it works and I can read and write micro-SD card over SPI3 and sometimes it does not. It's interesting because the operation is always successful when I connect the SD card to MCU's SPI1 or SPI2.

It seems that its not a programmatic conflict between JTAG and SPI3 on STM32F103 mcu.

Maybe I should investigate the problem on signal layer.

a_patiar
Associate II
Posted on June 18, 2015 at 04:04

Dear Clive

I've added 47R ''series termination resistor'' to SCK, MISO and MOSI lines between MCU and slave SPI device(micro-SD card) and the before-mentioned random behavior is now completely vanished.

It seems that existence of 3 ''series termination resistor'' on the communication lines is the

must

when one wants to connect a device to SPI3. (I put 47ohm resistor in series on the SCK and MOSI lines as close as to the MCU and on MISO's as close as SD card)

By the way, now everything works fine.

Thank you

MCU (master)

                                                                       

SD card (slave)

SCK-----/\/\/\/\------------------------------------------------------ SCK

MOSI---/\/\/\/\-------------------------------------------------------MISO

MISO----------------------------------------------------/\/\/\/\/\----MOSI