cancel
Showing results for 
Search instead for 
Did you mean: 

Reading and writing to SPI ?

antonius
Senior
Posted on September 30, 2015 at 08:14

Guys,

Can I replace this function

SPIPutChar(uint8_t data[])

{

/* Write and Read a byte on SPI interface. */

unsigned char inb;

/* Wait if TXE cleared, Tx FIFO is full. */

while ((SPI1->SR & TXE) == 0);

SPI1->DR = outb;

/* Wait if RNE cleared, Rx FIFO is empty. */

while ((SPI1->SR & RXNE) == 0);

inb = SPI1->DR;

return (inb); HAL_SPI_Transmit_DMA(&hspi1,data,1);

and

char SPIGetChar(void)

{

char Data = 0;

/* Wait until the transmit buffer is empty */

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

/* Send the byte */

SPI_I2S_SendData(SPI1, 0xFF);

/* Wait until a data is received */

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

/* Get the received data */

/Data = SPI_I2S_ReceiveData(SPI1);

/* Return the shifted data */

return Data;

} to :

HAL_SPI_Receive_DMA(&hspi1, data, 1)

Please correct me if I'm wrong with this function, I tried to replace with HAL function

unsigned char Mp3ReadRegister( unsigned char addressbyte)

{

int resultvalue = 0;

Mp3DeselectData();

Mp3SelectControl(); //XCS = 0

uint8_t data7[2] = { VS_READ_COMMAND, addressbyte };

HAL_SPI_Transmit_DMA(&hspi1,data7,2);

//SPIPutChar(VS_READ_COMMAND); //????????

//SPIPutChar(addressbyte); //????????

uint8_t data8[1] = {0x00};

resultvalue = HAL_SPI_Receive_DMA(&hspi1, data8, 1) << 8;

resultvalue |= HAL_SPI_Receive_DMA(&hspi1, data8, 1);

//resultvalue = SPIGetChar() << 8;//???8???

//resultvalue |= SPIGetChar(); //???8???

Mp3DeselectControl();

return resultvalue; //??16??????

}

Thank you

5 REPLIES 5
Posted on September 30, 2015 at 17:19

Hi h.rick,

In your original code, do you use DMA for transmission? if so, you can use the two mentioned function. If not, you have to configure DMA.

After that, if you encounter any problem, you can post it.

-Shahrzad-

antonius
Senior
Posted on September 30, 2015 at 23:55

It was not using DMA on the original code,

but I put DMA on STM32CubeMx,

 MX_DMA_Init();

How can I configure DMA to work with SPI ? thanks

antonius
Senior
Posted on October 01, 2015 at 11:24

Something I must change with my DMA configuration ?

Posted on October 01, 2015 at 12:09

Hi h.rick,

To configure DMA, you can use CubeMx as you have indicated. Just make sure that in the configuration view, within SPI parameter settings, you set desired DMA Settings (click Add button-Select the DMA Request- and set all other seetings). So, the generated project will contain DMA configuration.

Besides, you can refer to examples using DMA within STM32Cube firmware package.

-Shahrzad-

antonius
Senior
Posted on October 01, 2015 at 16:18

Is my configuration correct ? thanks

0690X00000602a5QAA.jpg