Communication of Lora module SX1278 based on STM32

Recently, I found two Lora modules for serial communication: SX1278.
This paper introduces the following two Lora module communication problems.
1, Lora module introduction
1. Pin definition and module connection diagram



Note: when connecting the AUX and TXD pins of some 5v single-chip computers, it is better to connect a 4-10K pull-up resistor.
2. Sending method
(1) , point to point transmission
(2) , broadcast transmission

The address is set to ff ff or 0000 for broadcast transmission.
3. Mode selection

4. Instruction format

5. Default factory settings

Namely: 0x00 0x00 0x1a 0x17 0x44
6. Parameter setting
M0,M1 pull up, set the module to mode three. Then send the HEAD byte to the module (0XC0: the set parameter can be saved in case of power failure; 0XC2: the set parameter cannot be saved in case of power failure)
Then five bytes are sent.
First byte set module high address (default 0x00)
Second address setting module low address (default 0x00)
The third byte sets the module communication parameters, such as setting baud rate, propagation speed, etc
(default 0x1a: 9600 n 8 1)
The fourth byte sets the module communication channel (default 433M is 0x17)
The fifth byte sets the sending mode, wake-up time, etc. (default 0x44)
See the following figure for specific configuration parameter options:




See the user manual for more details.
2, Parameter configuration
We use the software provided by Yibai to set the above parameter information and prepare a USB to TTL cable.
1. Pull M0 and M1 high, 3.3v power supply, AUX suspended, TXD connected to RXD of serial port line, RXD connected to TXD of serial port line.
2. Open the software, set the baud rate to 9600, draw configuration parameters in the software interface, and then click write parameters.

3, Program implementation, pin diagram connection as shown in Figure

SX1278Lora module provided by EBT: e32433t20dc (serial communication mode)
1. Initialize the serial port
usart.c

#include "sys.h"
#include "usart.h"	  
#include "lora.h"

//////////////////////////////////////////////////////////////////
//Add the following code to support the printf function without selecting use MicroLIB	  
#if 1
#pragma import(__use_no_semihosting)             
//Support functions required by standard library                 
struct __FILE 
{ 
	int handle; 

}; 

FILE __stdout;       
//Define sys exit() to avoid semi host mode    
void _sys_exit(int x) 
{ 
	x = x; 
} 
//Redefining fputc function 
int fputc(int ch, FILE *f)
{      
	while((USART1->SR&0X40)==0);//Send circularly until sending is completed   
    USART1->DR = (u8) ch;      
	return ch;
}
#endif 

/*How to use microLib*/
 /* 
int fputc(int ch, FILE *f)
{
	USART_SendData(USART1, (uint8_t) ch);

	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}	
   
    return ch;
}
int GetKey (void)  { 

    while (!(USART1->SR & USART_FLAG_RXNE));

    return ((int)(USART1->DR & 0x1FF));
}
*/
  
void uart_init(u32 bound)
{
	//GPIO port settings
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	//Enable USART1, GPIOA clock
	USART_DeInit(USART1); //Initialize serial port 1
	//USART1_TX   GPIOA.9
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//Multiplexing push pull output
	GPIO_Init(GPIOA, &GPIO_InitStructure);//Initialize GPIOA.9

	//Usart1? Rx GPIOA.10 initialization
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//Floating input
	GPIO_Init(GPIOA, &GPIO_InitStructure);//Initialize GPIOA.10  

	//Usart1 NVIC configuration
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//Preempt priority 3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//Sub priority 3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ channel enable
	NVIC_Init(&NVIC_InitStructure);	//Initialize the VIC register according to the specified parameters

	//USART initialization settings

	USART_InitStructure.USART_BaudRate = bound;//Serial port baud rate
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//The word length is 8-bit data format
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//One stop bit
	USART_InitStructure.USART_Parity = USART_Parity_No;//No parity bit
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//No hardware data flow control
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//Transceiver mode

	USART_Init(USART1, &USART_InitStructure); //Initialize serial port 1
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//Open serial port to accept interrupt
	USART_Cmd(USART1, DISABLE);                    //Disable serial port 1  

}

#if EN_USART1_RX   //If reception is enabled
//Serial port 1 interrupt service program
//Note that reading usartx - > SR avoids unintelligible errors   	
u8 USART_RX_BUF[USART_REC_LEN];     //Receive buffer, max. USART? Rec? Len bytes
//Reception state
//bit15, receive complete flag
//bit14, received 0x0d
//bit13~0, number of valid bytes received
u16 USART_RX_STA=0;       //Receive status flag	

void USART1_IRQHandler(void)                	//Serial port 1 interrupt service program
{
	u8 Res;
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //Receive interrupt (received data must be end of 0x0d 0x0a)
	{
		Res =USART_ReceiveData(USART1);	//Read received data
		
		if((USART_RX_STA&0x8000)==0)//Receive incomplete
		{
			if(USART_RX_STA&0x4000)//Received 0x0d
			{
				if(Res!=0x0a)USART_RX_STA=0;//Receive error, restart
				else USART_RX_STA|=0x8000;	//Reception completed 
			}
			else //I haven't received 0X0D yet
			{	
				if(Res==0x0d)USART_RX_STA|=0x4000;
				else
				{
					//lora.EmitData[USART_RX_STA&0X3FFF]=Res;
					lora.ReadData[USART_RX_STA&0X3FFF]=Res;
					USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
					USART_RX_STA++;
					if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//Receive data error, restart receiving	  
				}		 
			}
		}   		 
     } 
} 
#endif	

usart.h

#ifndef __USART_H
#define __USART_H
#include "stdio.h"	
#include "sys.h" 

#define USART_REC_LEN  			200  	//Define the maximum number of bytes received 200
#define EN_USART1_RX 			1		//Enable (1) / disable (0) serial port 1 receive
	  	
extern u8  USART_RX_BUF[USART_REC_LEN]; //Receive buffer, max. USART? Rec? Len bytes. Last byte is line break 
extern u16 USART_RX_STA;         		//Receive status flag	
//If you want the serial port to interrupt reception, please do not comment the following macro definition
void uart_init(u32 bound);
#endif


lora.h

#ifndef __lora_H
#define __lora_H
#include "sys.h"
#include "delay.h"
#include "usart.h"	

typedef struct _LORA
{
	u8 SetWParameterSave;            //Set working parameters and save in case of power failure
	u8 SetWParameterNSave;           //Set working parameters without saving after power failure
	u8 SetParameterReturn[3];        //Query the currently saved working parameters
	u8 VersionInformationReturn[3];  //Query version information
	u8 SystemReset[3];               //System reset command
	u8 SaveActualParameter[5];       //Working parameters
	u8 ReadData[USART_REC_LEN];     //Save the data. The maximum received data is based on the maximum length of the data received by the serial port. For the definition of USART? Rec? Len, see the serial port
	u8 EmitData[USART_REC_LEN];     //Save the data. The maximum received data is based on the maximum length of the data received by the serial port. For the definition of USART? Rec? Len, see the serial port
}LORA;

extern LORA lora;

#define LORA_PORT GPIOB

//#define LORA_TXD_Pin   GPIO_Pin_11
//#define LORA_RXD_Pin   GPIO_Pin_10
#define LORA_M0_Pin    GPIO_Pin_3
#define LORA_M1_Pin    GPIO_Pin_4
#define LORA_AUX_Pin   GPIO_Pin_15

#define LORA_M0   PBout(3)
#define LORA_M1   PBout(4)
#define LORA_AUX  PBin(15)

void lora_Init(void);
void Read_Lora_Para(u8 mode);
void Reset_Lora(void);
void LORA_Set_Working_Parameters(u8 save,u8 parameter[5]);
void LORA_Mode_Change(u8 mode);
u8 Read_Lora_Data(u8 time);
void Emit_Lora_Data(u8 *str,u8 len);
#endif


Through the LORA structure, save the commands required by the relevant configuration: for example: 0XC0, relevant configuration information and return information. Save the received data in lora.ReadData and the program to be sent in lora.EmitData
lora.c writes related sending data, receiving data, restoring module, setting function of module parameter, calling these functions in main function to transmit data between two Lora.

Test, the antenna installation position will affect the transmission data, theoretically, the open distance can transmit 3km

Published 14 original articles, won praise 9, visited 2718
Private letter follow

Posted on Mon, 16 Mar 2020 23:27:15 -0700 by maxpup979