cancel
Showing results for 
Search instead for 
Did you mean: 

STM32072RB does not receive/transmit data over SPI in slave mode

Javier Carrasco
Associate II
Posted on June 21, 2017 at 10:49

Hello, I am using the uC to receive and transmit data over SPI2 in slave mode with the following configuration:

CR1 =0x0078, CR2 = 0x0700, AFRH = 0x55353500, MODER = 0xa2a0556a

The APB1ENR is also properly configured.

The current program just checks the RXNE flag, reads the received data from DR and sends a random value writing to DR.

The status register when I receive data has the following value: SR = 0x1403

The master sends data properly and I checked the signals at the slave pins (clock phase and polarity are identical on both sides and the NSS signal is cleared before sending SCK and data over MOSI). I even configured the pins as inputs and I know I could read any digital signal the master could send. With the current configuration it seems the slave receives something because the RXNE is set when the master sends data but the read value is always 0x00. I have tried different configurations (software/hardware NSS, different data sizes...) but I always get 0x00. Moreover, the random value I send after reading DR is not sent to the outputs. That is my current function, which is called continuously:

unsigned char spi_rx_slave(unsigned char spiPort, unsigned char *receiveBuffer)

{

uint8_t temp;

static unsigned long sr;

if (!spi_isOpen(spiPort))

{

sendDebug('%s() Error: spiPort not in use!\r\n',__func__);

return false;

}

if (spiDescriptor[spiPort]->powerdown == true)

{

sendDebug('%s() Error: spiPort in powerdown!\r\n',__func__);

return false;

}

/* wait till spi is not busy anymore */

while((spiDescriptor[spiPort]->spiBase->SR) & SPI_SR_BSY)

{

sendDebug('SPI is busy(1)\r\n');

vTaskDelay(2);

}

sendDebug('CR1 = 0x%04x, ', spiDescriptor[spiPort]->spiBase->CR1);

sendDebug('CR2 = 0x%04x, ', spiDescriptor[spiPort]->spiBase->CR2);

sendDebug('AFRH address = 0x%08x, AFRH value = %08x, ', (unsigned long*)(GPIOB_BASE+0x24), *(unsigned long*)(GPIOB_BASE+0x24));

sendDebug('MODER address = 0x%08x, MODER value = %08x\r\n', (unsigned long*)(GPIOB_BASE), *(unsigned long*)(GPIOB_BASE));

sr = spiDescriptor[spiPort]->spiBase->SR;

while(sr & SPI_SR_RXNE)

{

/* get rx byte */

temp = *(uint8_t *)&(spiDescriptor[spiPort]->spiBase->DR);

spiDescriptor[spiPort]->spiBase->DR = 0x53;

sendDebug('-------->DR address = 0x%08x, data received: 0x%02x\r\n', &spiDescriptor[spiPort]->spiBase->DR, temp);

sendDebug('SR = 0x%04x\r\n', sr);

vTaskDelay(1);

sr = spiDescriptor[spiPort]->spiBase->SR;

}

while((spiDescriptor[spiPort]->spiBase->SR) & SPI_SR_BSY)

{

sendDebug('SPI is busy(2)\r\n');

vTaskDelay(2);

}

return true;

}

What am I doing wrong? Is there anything I did not configure properly? Thanks in advance.

Regards,

Javier
1 REPLY 1
Javier Carrasco
Associate II
Posted on June 23, 2017 at 11:36

I found a CUBE example that works fine on my evalkit but I copied the register values on my project setting NSS to software instead of hardware and now I have CR1 = 0x0278 and CR2 = 0x1700, but I keep on getting 0x00 so I wonder if there is something else I did not configure... May I be missing some clock/IO configuration?