cancel
Showing results for 
Search instead for 
Did you mean: 

Change Mode of Pin

Se Jasmine
Associate
Posted on November 14, 2017 at 16:40

Hey,

i have a STMF070CB microcontroller and use one pin for exti and spi-miso.

The pin is configured as EXTI as follows:

GPIO_InitStruct.Pin = GPIO_PIN_6;

GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET);

/* EXTI interrupt init*/

HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 3);

HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

SPI is configured as follows:

static void MX_SPI1_Init(void)

{

hspi1.Instance = SPI1;

hspi1.Init.Mode = SPI_MODE_MASTER;

hspi1.Init.Direction = SPI_DIRECTION_2LINES;

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;

hspi1.Init.NSS = SPI_NSS_SOFT;

hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; //APB:48MHz -> Prescaler 16 -> 3MHz (nearest possible above 2MHz) should be 16, but 32 took since didn't work properly on nucleo

hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi1.Init.CRCPolynomial = 10;

hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;

if (HAL_SPI_Init(&hspi1) != HAL_OK)

{

Error_Handler();

}

}

In stm32f0xx_it.c there are the lines

void EXTI4_15_IRQHandler(void){

HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_6);

}

which just yield towards a counter in the main file.

At first it works just as MISO. Then i use it for a while just as EXTI. When i then want to use it for SPI again i always get with HAL_SPI_TransmitReceive 0x00 for receive even when on the line 0xFF would be. The rest is working fine.

Why does that happen? And have you any ideas how i can avoid this?

Cheers

1 ACCEPTED SOLUTION

Accepted Solutions
Se Jasmine
Associate
Posted on November 15, 2017 at 11:23

Thanks a lot for the replies.

The problem was the following:

I had a HAL_SPI_Transmit command before using the Miso pin for exti.

I changed the the command to HAL_SPI_TransmitReceive and now it just works fine. I actually can't explain it to myself why its that way.

Would be nice if someone could explain that.

View solution in original post

5 REPLIES 5
Posted on November 14, 2017 at 18:04

EXTI works in parallel to all other functionalities of the pin (except perhaps when set to Analog).

This is not a very well documented detail, and Cube blurs it further.

JW

S.Ma
Principal
Posted on November 14, 2017 at 18:56

Make sure you don't get EXTI interrupts when transmitting in SPI mode.

Are the pulses generated for EXTI disturbing SPI listening? (this depends on the SCK pattern during this time)

Se Jasmine
Associate
Posted on November 15, 2017 at 11:23

Thanks a lot for the replies.

The problem was the following:

I had a HAL_SPI_Transmit command before using the Miso pin for exti.

I changed the the command to HAL_SPI_TransmitReceive and now it just works fine. I actually can't explain it to myself why its that way.

Would be nice if someone could explain that.

Posted on November 15, 2017 at 11:44

When you only transmit, the function returns instantly while the SCK starts sending clock pulses and data.

When you use transmit receive, the function will return only when the byte has been fully transmitted.