on my STM32F103C8T6 I am using SPI for communication and at one moment I need to disable it, make pins GPIO and drive them LOW. After some time need to release them from GPIO and give them back SPI function and communicate some more.
The code is generated with the latest STM32Cube and all works but bringing SPI back.
Here is the C-Pseudo just for an explanation first, below it is functions used to do work.
main(){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_SPI1_Init();
while(1){
SPI1_TURN_OFF();// It seems (maybe) to work because I can use pins as GPIO
// use gpio pins for some work
SPI1_TURN_ON(); // Registers look ok but it doesn't work
COMMUNICATE_OVER_SPI(); // Does't work
}
}
These are two functions that should turn off SPI and bring it back on.
static void SPI1_TURN_OFF(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, PIN_X |PIN_Y, GPIO_PIN_RESET);
HAL_SPI_DeInit(&hspi1);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, PIN_A|PIN_B|PIN_C, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = PIN_A|PIN_B|PIN_C;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
static void SPI1_TURN_ON(void){
HAL_GPIO_WritePin(GPIOB, PIN_X |PIN_Y, GPIO_PIN_SET);
HAL_GPIO_DeInit(GPIOB, PIN_A|PIN_B|PIN_C);
MX_SPI1_Init();
}
I am not using anything out of general order described by STM32 HAL documentation, what could go wrong with this?
*Remark: There isn't any other code which does something to this in effect, how it looks here follows the same logic in business logic.
Tnx for helping in advance!
MX_SPI1_InitandMX_GPIO_Initto your question. It seems to me that you are re-purposing GPIOs but not reinitializing it for SPI work. - Tarick Welling