cancel
Showing results for 
Search instead for 
Did you mean: 

SPI 9bit FOR STM32F0

sky_brake
Associate II
Posted on March 14, 2014 at 07:25

Hello everyone.

I'm working with STM32F0.I'm want to make 9 bits of data to be transferred via SPI. I've done the initial configuration for the SPI but it seems like the main program that I made wrong because when I check on the oscilloscope, MISO or MOSI do not have data pulse. please help me find where the mistake. thanks for your help

#include ''stm32f0xx.h''
#include ''stm32f0xx_gpio.h''
#include ''stm32f0xx_rcc.h''
#include ''stm32f0xx_tim.h''
#include ''stm32f0xx_spi.h''
void mx_pinout_config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIOB->BSRR |= (0x01<<
6
);
}
void SPI_config(void)
{
SPI_InitTypeDef SPI_InitStruct;
SPI_InitStruct.SPI_Direction
= 
SPI_Direction_2Lines_FullDuplex
;
SPI_InitStruct.SPI_Mode
= 
SPI_Mode_Master
;
SPI_InitStruct.SPI_DataSize
= 
SPI_DataSize_9b
;
SPI_InitStruct.SPI_CPOL
= 
SPI_CPOL_Low
;
SPI_InitStruct.SPI_CPHA
= 
SPI_CPHA_1Edge
;
SPI_InitStruct.SPI_NSS
= 
SPI_NSS_Soft
;
SPI_InitStruct.SPI_BaudRatePrescaler
= 
SPI_BaudRatePrescaler_128
; // 42000kHz/
128
=
328kHz
< 400kHz
SPI_InitStruct.SPI_FirstBit
= 
SPI_FirstBit_MSB
;
SPI_InitStruct.SPI_CRCPolynomial
= 
7
;
SPI_Init(SPI1, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE); // enable SPI1
}
uint8_t SPI1_send(uint8_t data){
SPI1->DR = data; // write data to be transmitted to the SPI data register
while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
return SPI1->DR; // return received data from SPI data register
}
int main(void)
{
uint8_t received_val = 0;
mx_pinout_config();
SPI_config();
while(1)
{
SPI1_send(0xAA); // transmit data
}
}

2 REPLIES 2
hkamba
Associate II
Posted on March 14, 2014 at 10:26

Hi Hakim,

I recently worked on STM32F030C8 and wrote an SPI driver for it. I observed that no matter how you try to set the SPI_CR2_DS_x bits of SPIx_CR2 register, it always revert back to 8-bit data. I do not know if it is an issue known to STM... Below is my initialisation:

void SPI2_init( void )
{ 
RCC->APB1ENR |= RCC_APB1ENR_SPI2EN; // Enable APB1 peripheral clock for SPI2
RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // Enable the peripheral clock for SPI2 pins
// Set PB12, PB13, PB14 and PB15 as Alternate function, Open-drain, 2MHz, pull-down
GPIOB->MODER &= ~( GPIO_MODER_MODER12 | GPIO_MODER_MODER13 | GPIO_MODER_MODER14 | GPIO_MODER_MODER15 );
GPIOB->MODER |= ( GPIO_MODER_MODER12_1 | GPIO_MODER_MODER13_1 | GPIO_MODER_MODER14_1 | GPIO_MODER_MODER15_1 ); // Alternate function
GPIOB->OTYPER &= ~( GPIO_OTYPER_OT_12 | GPIO_OTYPER_OT_13 | GPIO_OTYPER_OT_14 | GPIO_OTYPER_OT_15 ); // Output push-pull
GPIOB->OSPEEDR &= ~( GPIO_OSPEEDER_OSPEEDR12 | GPIO_OSPEEDER_OSPEEDR13 | GPIO_OSPEEDER_OSPEEDR14 | GPIO_OSPEEDER_OSPEEDR15 ); // Low speed
GPIOB->PUPDR &= ~( GPIO_PUPDR_PUPDR12 | GPIO_PUPDR_PUPDR13 | GPIO_PUPDR_PUPDR14 | GPIO_PUPDR_PUPDR15 );
GPIOB->PUPDR |= ( GPIO_PUPDR_PUPDR12_1 | GPIO_PUPDR_PUPDR13_1 | GPIO_PUPDR_PUPDR14_1 | GPIO_PUPDR_PUPDR15_1 ); // Pull-down
// Select SPI Alternate Function for PB12 (SPI2_NSS), PB13 (SPI2_SCK), PB14 (SPI2_MISO), and PB15 (SPI2_MOSI) pins
GPIOB->AFR[1] &= ~( GPIO_AFRH_AFRH4 | GPIO_AFRH_AFRH5 | GPIO_AFRH_AFRH6 | GPIO_AFRH_AFRH7 ); 
SPI2->CR1 &= 0x0000;
SPI2->CR2 &= 0x0000;
if ( SysInfo.CommsMode == SPI_MASTER )
{
// No CRC, Full duplex, MSB first, PCLK/128 Hz, Master mode, Mode 0 (Note: PCLK = 8 MHz)
SPI2->CR1 |= ( (SPI_CR1_BR_2 | SPI_CR1_BR_1) | SPI_CR1_MSTR ); 
}
if ( SysInfo.CommsMode == SPI_SLAVE )
{
// No CRC, Full duplex, MSB first, PCLK/128 Hz, Slave mode, Mode 0 (Note: PCLK = 8 MHz)
SPI2->CR1 |= ( (SPI_CR1_BR_2 | SPI_CR1_BR_1) );
} 
// 8-bit data transfer, Motorola mode, NSSP, SSOE
SPI2->CR2 |= SPI_CR2_FRXTH | (SPI_CR2_DS_0 | SPI_CR2_DS_1 | SPI_CR2_DS_2) | SPI_CR2_RXNEIE | SPI_CR2_NSSP | SPI_CR2_SSOE; 
SPI2->CR1 |= SPI_CR1_SPE; // SPI enabled
txBuf = (uint8_t*)&SPI2->DR; // Necessary to send only 8-bit and not two chucnks of 8 bits by accessing the SPI2_DR register directly!!
NVIC_SetPriority(SPI2_IRQn, NVIC_PRIO_SPI2); // Configure SPI2 IRQ
NVIC_EnableIRQ(SPI2_IRQn); // Enable SPI2 IRQ
if ( SysInfo.CommsMode == SPI_MASTER )
{
TIMER1_set( TIMER_SPIRXPOLL, SPI2_rx_poll, SPIRXPOLL_TIMER, TIMER_REPEATING ); // Register SPI2 poll timer callback
}
if ( SysInfo.CommsMode == SPI_SLAVE )
{ 
spi2_init_irq();
}
}

Posted on March 14, 2014 at 11:59

Definitely going to need to enable the SPI clock on the appropriate APB/AHB bus

I don't understand where this SPI send stuff is coming from, but why does every one write SPI->DR before checking TXE?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..