2018-10-09 09:55 AM
I am new in SPI protocol and trying to perform an application with LORA-Ra02 module via SPI protocol. I can send data through MOSI by considering DataFrame, CPOL, CPHA, rw bit of the frame but I cannot receive any data through MISO. I performed this application with both STM32 and Arduino to compare the differences. I want to share some picture and code to be understood better.
This is the picture of trying with Stm32f103, no incoming data :
This is the picture of trying with arduio, Datas must be like in this picture actually :
This is the information about SPI protocol of LORA Ra-02 module :
This is the codes I wrote :
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_spi.h"
#define SPIx_RCC RCC_APB2Periph_SPI1
#define SPIx SPI1
#define SPI_GPIO_RCC RCC_APB2Periph_GPIOA
#define SPI_GPIO GPIOA
#define SPI_PIN_MOSI GPIO_Pin_7
#define SPI_PIN_MISO GPIO_Pin_6
#define SPI_PIN_SCK GPIO_Pin_5
#define SPI_PIN_SS GPIO_Pin_4
void SPIx_Init(void);
uint8_t SPIx_Transfer(uint8_t data);
void SPIx_EnableSlave(void);
void SPIx_DisableSlave(void);
uint8_t receivedByte;
void delay(unsigned long int time){
unsigned long c;
for(c=1; c<=time; ++c); // some delay
}
int main()
{
// Initialization struct
SPI_InitTypeDef SPI_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
// Step 1: Initialize SPI
RCC_APB2PeriphClockCmd(SPIx_RCC, ENABLE);
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
SPI_Init(SPIx, &SPI_InitStruct);
SPI_Cmd(SPIx, ENABLE);
// Step 2: Initialize GPIO
RCC_APB2PeriphClockCmd(SPI_GPIO_RCC, ENABLE);
// GPIO pins for MOSI, MISO, and SCK
GPIO_InitStruct.GPIO_Pin = SPI_PIN_MOSI | SPI_PIN_MISO | SPI_PIN_SCK;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_GPIO, &GPIO_InitStruct);
// GPIO pin for SS
GPIO_InitStruct.GPIO_Pin = SPI_PIN_SS;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_GPIO, &GPIO_InitStruct);
// Disable SPI slave device
SPIx_DisableSlave();
receivedByte = SPIx_Transfer(0x22);
while(1)
{
}
}
uint8_t SPIx_Transfer(uint8_t data)
{
uint8_t receivedOne;
SPIx_EnableSlave();
delay(1000);
// Write data to be transmitted to the SPI data register
SPIx->DR = data;
// Wait until transmit complete
while (!(SPIx->SR & (SPI_I2S_FLAG_TXE)));
delay(1000);
// Wait until receive complete
while (!(SPIx->SR & (SPI_I2S_FLAG_RXNE)));
// Wait until SPI is not busy anymore
while (SPIx->SR & (SPI_I2S_FLAG_BSY));
// Return received data from SPI data register
receivedOne = SPIx->DR;
SPIx_DisableSlave();
return receivedOne;
}
void SPIx_EnableSlave()
{
// Set slave SS pin low
SPI_GPIO->BRR = SPI_PIN_SS;
}
void SPIx_DisableSlave()
{
// Set slave SS pin high
SPI_GPIO->BSRR = SPI_PIN_SS;
}
What seems to be my problem ?
Thanks in advance !
2018-10-09 10:41 AM
>>What seems to be my problem ?
On the F1's program the MISO pin as INPUT FLOATING
2018-10-09 11:20 AM
I did what you said but the result is the same. I found an example on the web that someone else perform, he also program sck, miso and mosi as AF.
2018-10-09 04:34 PM
did you make a custom board ?
are you sure you dont have Miso and Mosi swapped around ?
Can you check that the pin Miso actually connects from one chip to the other,
Check that with a meter, it should be 0Ohms approx.
2018-10-09 09:58 PM
No, I am using stm32 dev board called Blue Pill. I am sure I connected MISO to MISO and MOSI to MOSI. I suppose the problem is in software side. If there seems to be no problem with the codes, I will try another slave device ?
2018-10-10 12:28 AM
If you are using any kind of compiler optimization then your delay function will be optimized out as useless code. Try making the "c" variable volatile or use a better delay function based on systick or any other timer.
2018-10-10 12:36 AM
I put delay there at random I was just wondering if it has any effect or not. Without or with delay, Result doesnt change.
2018-10-11 10:08 AM
I have tried everything but I cannot make it work
2018-10-11 03:07 PM
check you pin numbers...
if you linked Miso to Mosi, then the scope would show that...
but if you connected the wrong pair of pins.., nothing will work.
did you check on the scope that the data is getting to the pin since you changed your configuration ?
2018-10-12 01:22 AM
There is no problem with pin connections, I controlled pairs of pins, all is true, I thought that maybe device is broken-down I tried another lora, result was the same, Configurations seem true ; 8 bit, msb first, mode = 0, full duplex etc.
I never changed spi configuration, considering SPI protocol of LORA as it is seen above, configuration seems true
In arduino example, They use RESET and DI0 pin on LORA. Do those pins affect SPI communication ?