The NSL01 library contains all functions for controlling the NSL01 LoRaWAN shield from mCloud System GmbH. The NSL01 is a professional plug & play LoRaWAN shield for a wide range of STM32 Nucleo-64 boards with Arduino Uno Rev 3 connectivity. For more information about the NSL01 LoRaWAN shield: http://www.mcloud-systems.com/nsl01-lorawan-nucleo-arduino-shield

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialDevice.cpp Source File

SerialDevice.cpp

00001 /*******************************************************************************
00002  *  Functions for NSL01 class to communicate with onboard RF radio module via
00003  *  UART interface.
00004  *
00005  *  For more information about the NSL01 LoRaWAN shield:
00006  *      http://www.mcloud-systems.com/nsl01-lorawan-nucleo-arduino-shield
00007  *
00008  *  @note The SerialDevice files are included in the dependencies directory of
00009  *        the project and these files are necessary for the NSL01 class!
00010  *
00011  *  @author  -
00012  *  @version 1.0
00013  *  @date    20-June-2018
00014 *******************************************************************************/
00015 
00016 //------------------------------------------------------------------------------
00017 //
00018 //  Include Files
00019 //
00020 //------------------------------------------------------------------------------
00021 
00022 #include "./dependencies/SerialDevice.h"
00023 
00024 //------------------------------------------------------------------------------
00025 //
00026 //  Function: SerialDevice_Open
00027 //
00028 //  @brief Function to open serial device with given parameters.
00029 //
00030 //  @param baudrate : Baudrate for serial communication
00031 //  @param dataBits : Number of data bits for serial communication
00032 //
00033 //------------------------------------------------------------------------------
00034 
00035 void
00036 SerialDevice_Open(UINT32        baudRate,
00037                   int           dataBits)
00038 {
00039     //--Initialize UART interface
00040     comm_LoRa.baud(baudRate);
00041     comm_LoRa.format(dataBits, Serial::None, 1);
00042     comm_LoRa.attach(&rxCallback, Serial::RxIrq);
00043 }
00044 
00045 //------------------------------------------------------------------------------
00046 //
00047 //  Function: SerialDevice_SendData
00048 //
00049 //  @brief Function to transmit data to the RF device.
00050 //
00051 //  @param txBuffer : Pointer to Tx buffer
00052 //  @param txLength : Length of Tx buffer
00053 //
00054 //  @returns number of Tx bytes on success, -1 on error
00055 //
00056 //------------------------------------------------------------------------------
00057 
00058 int
00059 SerialDevice_SendData(UINT8* txBuffer, int txLength)
00060 {              
00061     //--Internal variables and constants
00062     UINT32  numTxBytes= 0;
00063     
00064     //--Display input Tx buffer for debugging
00065     #ifdef DEBUG
00066         comm_pc.printf("\r\n Content Tx buffer: %u\r\n",&txBuffer[0]);
00067     #endif
00068         
00069     //--Transmit txBuffer
00070     while((numTxBytes <= (UINT32)txLength))
00071     {
00072         //--Transmit data array byte by byte
00073         comm_LoRa.putc(txBuffer[numTxBytes]); 
00074        
00075         //--Update counter
00076         numTxBytes++;        
00077     }
00078         
00079     //--Display SLIP encoded message for debugging
00080     #ifdef DEBUG
00081         comm_pc.printf("\r\n Encoded transmit command: ");
00082         
00083         for (int i=0; i< txLength; i++)
00084         {
00085             comm_pc.printf("%02X ",txBuffer[i]);
00086         }
00087         
00088         comm_pc.printf("\r\n");    
00089     #endif
00090         
00091     //--Check if all bytes were written
00092     if (numTxBytes == (UINT32)txLength + 1)
00093     {
00094         //--OK
00095         #ifdef DEBUG
00096             comm_pc.printf("\r\n numTX Bytes: %i\n\r", numTxBytes);
00097         #endif
00098         
00099         return numTxBytes;
00100     }
00101 
00102     return -1;
00103 }
00104 
00105 //------------------------------------------------------------------------------
00106 //
00107 //  Function: SerialDevice_SendByte
00108 //
00109 //  @brief Function to transmit a single (wakeup) byte to the RF device.
00110 //
00111 //  @param txByte : Tx byte
00112 //
00113 //  @returns 1 on success, -1 on error
00114 //
00115 //------------------------------------------------------------------------------
00116 
00117 int
00118 SerialDevice_SendByte(UINT8 txByte)
00119 {    
00120     //--Transmit a single byte
00121     comm_LoRa.putc(txByte);
00122     
00123     return 1;
00124 }
00125 
00126 //------------------------------------------------------------------------------
00127 //
00128 //  Function: SerialDevice_ReadData
00129 //
00130 //  @brief Function to manually read data into Rx buffer.
00131 //
00132 //  @note This function is not necessary at the moment, because of Rx callback
00133 //        function!
00134 //
00135 //  @param rxBuffer     : Pointer to Rx buffer
00136 //  @param rxBufferSize : Length of Rx buffer
00137 //
00138 //  @returns number of Rx bytes on success, -1 on error
00139 //
00140 //------------------------------------------------------------------------------
00141 
00142 int
00143 SerialDevice_ReadData(UINT8* rxBuffer, int rxBufferSize)
00144 {  
00145     //--Internal variables and constants
00146     UINT8 numRxBytes = 0;
00147     UINT8 tmp_tx;
00148     
00149     //--Check if interface is accessible
00150     if ((!comm_LoRa.readable()))
00151         return -1;
00152 
00153     //--Read rxBuffer
00154     while(numRxBytes < rxBufferSize)
00155     {
00156         //--Read data byte by byte
00157         tmp_tx = (UINT8)comm_LoRa.getc();    
00158         rxBuffer[numRxBytes] = tmp_tx;   
00159     }
00160   
00161     return (int)numRxBytes;      
00162 }
00163 
00164 //------------------------------------------------------------------------------
00165 //
00166 //  Function: rxCallback
00167 //
00168 //  @brief UART Rx callback function for data reception (UART IRQ)
00169 //
00170 //------------------------------------------------------------------------------
00171 
00172 void 
00173 rxCallback() 
00174 {   
00175     //--Init Rx Buffer
00176     UINT8 tmp_tx[1];
00177     
00178     //--Get actual character from device
00179     tmp_tx[0]= (UINT8)comm_LoRa.getc();
00180     
00181     //--Decode chunk of data
00182     SLIP_DecodeData(tmp_tx, 1);
00183 }
00184 
00185 //------------------------------------------------------------------------------
00186 // end of file
00187 //------------------------------------------------------------------------------