I 'm trying to understand the code of the Lora library and understand how from the datasheet of the SX1278 the creator of the library ended up to the code.
Most of the parts of the code can, relatively easily, be 'found' in the datasheet.
But some parts of it, not.
Like the Lora.print.
Is it the 'LoRaClass::write'? And how using 'Lora.print()' we actually call the function 'Lora.write'?
And how did he end up to this code? From transmit sequence in page 38 in the datasheet?
This transmit sequence is actually 3 functions?
-LoRaClass::beginPacket
-LoRaClass::write
-LoRaClass::endPacket
This is the transmit sequence in pseudo code (from page 38 in the datasheet, I wrote it my self):
(I have put line numbers for better reference)
TRANSMIT SEQUENCE
1. Mode request Stdby
2. Tx init
3. Write Data FIFO :LINE3
a. LoRaTMTransmit Data FIFO Filling
In order to write packet data into FIFO user should:
a1. Set FifoPtrAddr to FifoTxPtrBase.
a2. Write PayloadLength bytes to the FIFO (RegFifo) (payload=ωφέλιμο φορτίο)
4. Mode Request TX
5. Wait for IRQ TxDone
6. Automatic Mode change STAND BY
7. if new TX
goto LINE3
else
New Mode request
From line 1 it's logical to end to this code:
// put in standby mode
idle();
(the idle function does what the datasheet says to put it in Stdby)
Ok, understandable.
But, line #2? I mean the 'Tx init'. Which part of the code realizes this?
That means that the LoRa class inherits all of the behaviors of the Stream class. You can find Stream.h in the Arduino core. There you will find:
class Stream : public Print
That means that the Stream class inherits all of the behaviors of the Print class. You can find Print.h in the Arduino core. The Print class has all of the .print() and .println() methods which do their output by calling the 'virtual' function .write(). Because the .write() function is not defined in Print or Stream it has to be defined in LoRa. When you call LoRa::print(x) you are calling Print::print(x) which turns the value 'x' into characters and calls LoRa::write() for each character.
Just curious but are you trying to understand the working of that particular library or the sequence of register writes needed to make the LoRa device transmit what you want, and why ?
As for the TXinit, I would just assume that is setting up with the device with appropriate LoRa modem, power and frequency parameters.
panoss:
I want to understand how from a datasheet of a device one can make a library.
There is no one universal way. Some manufacturers just provide a list of specifications and low level configurations. Then normally they provide sample code. The code then in effect provides a sequence of instructions on how to use the device.
However which STM32 core are you using with your 'Arduino' and specifically what is it about the LoRa library you are using as an example that does not work on your STM32 ?
I 'm using an STM32F030K6T6. With Atollic TrueSTUDIO, not with Arduino IDE.
What I want is to understand how the creator of the library, from the datasheet, concluded that the code for transmitting data should be the code found in functions:
LoRaClass::beginPacket
LoRaClass::write
LoRaClass::endPacket
Let me quote the code so you don 't have to search:
int LoRaClass::beginPacket(int implicitHeader)
{
// put in standby mode
idle();
if (implicitHeader) {
_implicitHeaderMode = 1;
writeRegister(REG_MODEM_CONFIG_1, readRegister(REG_MODEM_CONFIG_1) | 0x01);
} else {
_implicitHeaderMode = 0;
writeRegister(REG_MODEM_CONFIG_1, readRegister(REG_MODEM_CONFIG_1) & 0xfe);
}
// reset FIFO address and paload length
writeRegister(REG_FIFO_ADDR_PTR, 0);
writeRegister(REG_PAYLOAD_LENGTH, 0);
return 1;
}
size_t LoRaClass::write(uint8_t byte)
{
return write(&byte, sizeof(byte));
}
size_t LoRaClass::write(const uint8_t *buffer, size_t size)
{
int currentLength = readRegister(REG_PAYLOAD_LENGTH);
// check size
if ((currentLength + size) > MAX_PKT_LENGTH) {
size = MAX_PKT_LENGTH - currentLength;
}
// write data // Write Data FIFO :LINE3
for (size_t i = 0; i < size; i++) {
writeRegister(REG_FIFO, buffer[i]);
}
// update length // 2. Write PayloadLength bytes to the FIFO (RegFifo) ????????
writeRegister(REG_PAYLOAD_LENGTH, currentLength + size);
return size;
}
int LoRaClass::endPacket()
{
// put in TX mode // Mode Request TX
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
// wait for TX done
while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) {
yield();
}
// clear IRQ's
writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);
return 1;
}
panoss:
I 'm using an STM32F030K6T6. With Atollic TrueSTUDIO, not with Arduino IDE.
What I want is to understand how the creator of the library, from the datasheet, concluded that the code for transmitting data should be the code found in functions:
I already answered that in post #4.
When asking questions of the volunteers here in this Arduino forum, it would help if you make clear when your questions are not related to the Arduino platform.