cancel
Showing results for 
Search instead for 
Did you mean: 

SPI configured to send 8bit sends 16bits

malehakim
Associate III

I configured my spi to have a datasize of 8bits. However, its sending an extra '\0' after every letter and i can't figure out where this is coming from. Am running a Nucleo-F303k8.

Here below, am trying to send a Hello World! message but it gets corrupted all the way.

0693W00000BbzC1QAJ.pngHere is how i configured my settings.

void SystemClock_Config(void);
 
void SPIConfig(void)
{
	RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;							// Enable SPI1 Clock
	SPI1->CR1 |= SPI_CR1_CPHA | SPI_CR1_CPOL;					// Enable CPHA=1 and CPOL=1
	SPI1->CR1 |= SPI_CR1_MSTR;									// Master Mode
	SPI1->CR1 |= (7 << 3);										// Baudrate divide with 256
	SPI1->CR1 &= ~(SPI_CR1_LSBFIRST);							// LSBFIRST = 0, MSB_FIRST = 1
	SPI1->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI;						// SSM = 1, SSI = 1 -> Slave Management
	SPI1->CR1 &= ~(SPI_CR1_RXONLY);								// Full-duplex (Transmit and receive)
 
	SPI1->CR2 |= (7 << 8);										// SPI Datasize -> 0111: 8-bit
 
	__IO uint32_t tempRD = SPI1->SR;							// Clear all flags in the status register
}
 
void GPIOConfig(void)
{
	RCC->AHBENR |= RCC_AHBENR_GPIOAEN;							// Enable GPIOA
 
	// b10: Alternate function mode for PINS MOSI, SCLK & MISO
	GPIOA->MODER |= (2 << 14 | 2 << 10 | 2 << 12);
 
	// General purpose output mode for CSN PIN
	GPIOA->MODER |= (1 << 8);
 
	// b11: High speed for all 4 pins
	GPIOA->OSPEEDR |= (3 << 14) | (3 << 12) | (3 << 10) | (3 << 8);
 
	// Set to alternate function AF5 which corresponds to SPI1
	GPIOA->AFR[0] |= (5 << 28) | (5 << 24) | (5 << 20);
 
	// GPIO port pull-up/pull-down register
	GPIOA->PUPDR &= ~(1 << 14 | 1 << 12 | 1 << 10 | 1 << 8);
 
	// Put the output type of CSN pin to output push-pull ( reset state)
	GPIOA->OTYPER &= ~(1 << 4);
 
	// Set the CSN ODR to High initially
	GPIOA->ODR |= (1 << 4);
}
 
// SPE = 1, peripheral enabled
void SPI_Enable(void)
{
	SPI1->CR1 |= SPI_CR1_SPE;
}
 
// SPE = 0, peripheral disabled
void SPI_Disable(void)
{
	SPI1->CR1 &= ~SPI_CR1_SPE;
}
 
void CSN_Enable(void)
{
	GPIOA->ODR &= ~(1 << 4);
}
 
void CSN_Disable(void)
{
	GPIOA->ODR |= (1 << 4);
}
 
void SPI_Transmit(char *data, int size)
{
	uint8_t i = 0;
	while(i < size)
	{
		// Wait for TXE bit to set -> This indicactes that the buffer is empty and ready to recieve stuff
		while(!(SPI1->SR & SPI_SR_TXE)) {};
 
		// Load data into the data register
		SPI1->DR = data[i];
		i++;
	}
 
	while (!(SPI1->SR & SPI_SR_TXE)) {};  // wait for TXE bit to set -> This will indicate that the buffer is empty
	while (SPI1->SR & SPI_SR_BSY) {};  // wait for BSY bit to Reset -> This will indicate that SPI is not busy in communication
 
	//  Clear the Overrun flag by reading DR and SR
	__IO uint32_t temp = SPI1->DR;
	temp = SPI1->SR;
}
 
char* myTxData = "Hello World!";
 
int main(void)
{
	HAL_Init();
 
	SystemClock_Config();
	GPIOConfig();
	SPIConfig();
	SPI_Enable();
 
	while (1)
	{
		HAL_Delay(1000);
		CSN_Enable();
		SPI_Transmit(myTxData, 11);
		CSN_Disable();
	}
}

1 ACCEPTED SOLUTION

Accepted Solutions
Nikita91
Lead II

SPI-> DR is sensible to write size. Try:

* ((__IO uint8_t *) & pSpi->DR) = (uint8_t) data [i] ;

View solution in original post

2 REPLIES 2
malehakim
Associate III

It seems to be adding 0x00 after every character sent.

0693W00000BbzFeQAJ.png

Nikita91
Lead II

SPI-> DR is sensible to write size. Try:

* ((__IO uint8_t *) & pSpi->DR) = (uint8_t) data [i] ;