Skip to main content
MSkor.1
Associate II
February 2, 2023
Question

STM32F103C8 SPI1 MOSI ISSIE

  • February 2, 2023
  • 6 replies
  • 1753 views

Hi

I try to run SPI 1 on STM32F103C8 but when i send data i have only SCK signal. MOSI(PB5) is in high state (dont change).

 SPI2 on PORTA works grate.

void MX_SPI1_Init(void)
{
 
 /* USER CODE BEGIN SPI1_Init 0 */
 
 /* USER CODE END SPI1_Init 0 */
 
 /* USER CODE BEGIN SPI1_Init 1 */
 
 /* USER CODE END SPI1_Init 1 */
 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_1EDGE;
 hspi1.Init.NSS = SPI_NSS_SOFT;
 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi1.Init.CRCPolynomial = 10;
 if (HAL_SPI_Init(&hspi1) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN SPI1_Init 2 */
 LL_GPIO_AF_Remap_SWJ_NOJTAG();
 while((AFIO->MAPR & AFIO_MAPR_SWJ_CFG_JTAGDISABLE) != AFIO_MAPR_SWJ_CFG_JTAGDISABLE);
 /* USER CODE END SPI1_Init 2 */
 
}
 
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{
 
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 if(spiHandle->Instance==SPI1)
 {
 /* USER CODE BEGIN SPI1_MspInit 0 */
 
 /* USER CODE END SPI1_MspInit 0 */
 /* SPI1 clock enable */
 __HAL_RCC_SPI1_CLK_ENABLE();
 
 __HAL_RCC_GPIOB_CLK_ENABLE();
 /**SPI1 GPIO Configuration
 PB3 ------> SPI1_SCK
 PB4 ------> SPI1_MISO
 PB5 ------> SPI1_MOSI
 */
 GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_5;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 GPIO_InitStruct.Pin = GPIO_PIN_4;
 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 __HAL_AFIO_REMAP_SPI1_ENABLE();
 
 /* USER CODE BEGIN SPI1_MspInit 1 */
 
 /* USER CODE END SPI1_MspInit 1 */
 }
}
 for (uint16_t x=0; x<254; x++){
	 uint8_t chr = 0x55;
	 HAL_SPI_Transmit(&hspi1, &chr, 1, 1);
	 HAL_Delay(10);
	 HAL_GPIO_WritePin(PORT_LD, PIN_LD, SET);
	 HAL_Delay(1);
	 HAL_GPIO_WritePin(PORT_LD, PIN_LD, RESET);
 HAL_Delay(1);
 }

0693W00000Y9p0fQAB.png

This topic has been closed for replies.

6 replies

waclawek.jan
Super User
February 2, 2023

> __HAL_AFIO_REMAP_SPI1_ENABLE();

You need to enable AFIO clock in RCC for this to work.

JW

MSkor.1
MSkor.1Author
Associate II
February 2, 2023

__HAL_AFIO_REMAP_SPI1_ENABLE(); is enable by HAL

 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 __HAL_AFIO_REMAP_SPI1_ENABLE();
 
 /* USER CODE BEGIN SPI1_MspInit 1 */
 
 /* USER CODE END SPI1_MspInit 1 */
 }
}

waclawek.jan
Super User
February 2, 2023

Try a simple program, which sets PB5 as a GPIO output and toggle it in program.

(In other words, check if PB5 is properly connected).

JW

MSkor.1
MSkor.1Author
Associate II
February 2, 2023

Yes I check this at first time. 

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, RESET);

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, SET);

works ok (whene SPI1 is disabled)

I also replase MCU to new one.

MSkor.1
MSkor.1Author
Associate II
February 2, 2023

Ok

I found out what is causing the problem.

you can't have I2C1 enabled with SPI1 even if I2C1 pins are remapped

any way to bypass this problem?

waclawek.jan
Super User
February 3, 2023

> any way to bypass this problem?

Probably not in an easy way.

0693W00000Y9vX1QAJ.png 

If you use I2C as master, you can try to bit-bang it.

You can consider using a different pin-compatible STM32.

JW