I transmit 3-byte(24-bit) using SPI from this Nucleo board with HAL as follows (SPIoutputBuffer is a char array):
SPIoutputBuffer[0] = 0b00010000;
SPIoutputBuffer[1] = (char)(value >> 8);
SPIoutputBuffer[2] = (char)(value & 0x00FF);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi3, (uint8_t *)SPIoutputBuffer, 3, 1);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_SET);
The logic analyzer shows the expected clock and MOSI timing diagrams.
When I try to send another three bytes without using HAL by following method:
SPIoutputBuffer[0] = 0b00010000;
SPIoutputBuffer[1] = (char)(value >> 8);
SPIoutputBuffer[2] = (char)(value & 0x00FF);
GPIOC->ODR &= ~(1<<4); // Reset the Pin PC4
SPI3->DR = SPIoutputBuffer[0];//Send first byte
while((SPI3->SR & SPI_SR_TXE)!= SPI_SR_TXE);
SPI3->DR = SPIoutputBuffer[1];//Send second byte
while((SPI3->SR & SPI_SR_TXE)!= SPI_SR_TXE);
SPI3->DR = SPIoutputBuffer[2];//Send third byte
while((SPI3->SR & SPI_SR_TXE)!= SPI_SR_TXE);
while((SPI3->SR & SPI_SR_BSY) == SPI_SR_BSY );
GPIOC->ODR |= 1<<4; // Set the Pin PC4
But now the logic analyzer shows the following diagrams:
The second case SPI clock number is doubled and the MOSI line outputs the bytes with one more byte padded zeros: In hex 1000 instead of 10, 0200 instead of 02 and 1100 instead of 11 as you see in the above screen-shot.
What am I doing wrong in the second(non-HAL) case?
edit for an answer:
static void MX_SPI3_Init(void)
{
/* USER CODE BEGIN SPI3_Init 0 */
/* USER CODE END SPI3_Init 0 */
/* USER CODE BEGIN SPI3_Init 1 */
/* USER CODE END SPI3_Init 1 */
/* SPI3 parameter configuration*/
hspi3.Instance = SPI3;
hspi3.Init.Mode = SPI_MODE_MASTER;
hspi3.Init.Direction = SPI_DIRECTION_2LINES;
hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi3.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi3.Init.NSS = SPI_NSS_SOFT;
hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi3.Init.CRCPolynomial = 7;
hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi3.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI3_Init 2 */
SPI3->CR2 = (SPI3->CR2 & ~(0x0F << SPI_CR2_DS_Pos)) | (0x07 << SPI_CR2_DC_Pos);
SPI3->CR1 |= SPI_CR1_SPE;//Enable SPI
/* USER CODE END SPI3_Init 2 */
}