A mbed library for the RN2483. Heavily based on the Sodaq_RN2483 library for Arduino (https://github.com/SodaqMoja/Sodaq_RN2483). This is currently under-going initial testing, but seems to work! Tested on a NRF51 and FRDM K64F.
Dependents: rn2483-TestProgram
Diff: RN2483.cpp
- Revision:
- 2:336a025b82b5
- Parent:
- 1:cf9b0c21907a
- Child:
- 3:ee222a99783c
--- a/RN2483.cpp Sat Nov 19 23:30:34 2016 +0000 +++ b/RN2483.cpp Sun Nov 20 14:37:54 2016 +0000 @@ -44,21 +44,22 @@ } /** -* @brief Connect to network using Over The Air activation. +* @brief Initialise settings and connect to network using Over The Air activation. * @param devEUI provided by LoRaWAN Network server registration. * @param appEUI provided by LoRaWAN Network server registration. * @param appKey provided by LoRaWAN Network server registration. -* @return Network confirmation if sucessfully connected. +* @return Returns true if network confirmation and able to save settings. */ bool RN2483::initOTA(const uint8_t devEUI[8], const uint8_t appEUI[8], const uint8_t appKey[16], bool adr) { init(); - return resetDevice() && - setMacParam(STR_DEV_EUI, devEUI, 8) && - setMacParam(STR_APP_EUI, appEUI, 8) && - setMacParam(STR_APP_KEY, appKey, 16) && - setMacParam(STR_ADR, BOOL_TO_ONOFF(adr)) && - joinNetwork(STR_OTAA); + if(resetDevice() && setMacParam(STR_DEV_EUI, devEUI, 8) && setMacParam(STR_APP_EUI, appEUI, 8) && + setMacParam(STR_APP_KEY, appKey, 16) && setMacParam(STR_ADR, BOOL_TO_ONOFF(adr)) && joinOTTA()) { + if(saveConfiguration()) { + return true; + } + } + return false; } /** @@ -66,17 +67,37 @@ * @param devADDR provided by LoRaWAN Network server registration. * @param appSKey provided by LoRaWAN Network server registration. * @param nwkSKey provided by LoRaWAN Network server registration. -* @return Returns true if the parameters was valid. +* @return Returns true if the parameters were valid and able to save settings. */ bool RN2483::initABP(const uint8_t devAddr[4], const uint8_t appSKey[16], const uint8_t nwkSKey[16], bool adr) { init(); - return resetDevice() && - setMacParam(STR_DEV_ADDR, devAddr, 4) && - setMacParam(STR_APP_SESSION_KEY, appSKey, 16) && - setMacParam(STR_NETWORK_SESSION_KEY, nwkSKey, 16) && - setMacParam(STR_ADR, BOOL_TO_ONOFF(adr)) && - joinNetwork(STR_ABP); + if(resetDevice() && setMacParam(STR_DEV_ADDR, devAddr, 4) && setMacParam(STR_APP_SESSION_KEY, appSKey, 16) && + setMacParam(STR_NETWORK_SESSION_KEY, nwkSKey, 16) && setMacParam(STR_ADR, BOOL_TO_ONOFF(adr)) && + joinABP()) { + if(saveConfiguration()) { + return true; + } + } + return false; +} + +/** +* @brief Attempts to connect to the network using Over The Air Activation. +* @return Returns true if able to join network. +*/ +bool RN2483::joinOTTA() +{ + return joinNetwork(STR_OTAA); +} + +/** +* @brief Attempts to connect to the network using Activation By Personalization. +* @return Returns true if able to join network. +*/ +bool RN2483::joinABP() +{ + return joinNetwork(STR_ABP); } /** @@ -184,6 +205,21 @@ return 0; } +/** +* @brief Informs the RN2483 to do an ADC conversion on the VDD. +* @return Returns mV as a decimal from 0 to 3600. +*/ +uint8_t RN2483::getVDD() +{ + _RN2483.printf(STR_CMD_GET_VDD); + _RN2483.printf(CRLF); + char buffer[10]; + if(readBytesUntil('\n', buffer, 10) > 0) { + return (uint8_t) strtol(buffer,NULL,10); + } + return 0; +} + #ifdef ENABLE_SLEEP /** * @brief Sends a serial line break to wake up the RN2483 @@ -372,7 +408,7 @@ if(linkCheckInterval <= 65535) { return setMacParam(STR_LNK_CHK, linkCheckInterval); } else { - return 0; + return false; } } @@ -387,7 +423,7 @@ if(batLvl <= 255) { return setMacParam(STR_BAT, batLvl); } else { - return 0; + return false; } } @@ -398,16 +434,17 @@ * 863000000 to 870000000 or 433050000 to 434790000 in Hz * @return Returns true if parameters are valid or false if not. */ -bool RN2483::setChannelFreq(uint8_t channelID, uint32_t frequency){ - if((channelID <= 15 && channelID >= 3)){ - if((frequency <=870000000 && frequency >= 863000000)||(frequency <=434790000 && frequency >= 433050000)){ +bool RN2483::setChannelFreq(uint8_t channelID, uint32_t frequency) +{ + if((channelID <= 15 && channelID >= 3)) { + if((frequency <=870000000 && frequency >= 863000000)||(frequency <=434790000 && frequency >= 433050000)) { char buffer [15]; int bytesWritten = sprintf(buffer, "%d %d", channelID, frequency); // Check to make sure sprintf did not return an error before sending. if(bytesWritten > 0) { - setMacParam(STR_CH_FREQ, buffer); - } - } + return setMacParam(STR_CH_FREQ, buffer); + } + } } return false; } @@ -428,7 +465,7 @@ int bytesWritten = sprintf(buffer, "%d %d", channelID, dutyCycleSetting); // Check to make sure sprintf did not return an error before sending. if(bytesWritten > 0) { - setMacParam(STR_CH_DCYCLE, buffer); + return setMacParam(STR_CH_DCYCLE, buffer); } } return false; @@ -442,8 +479,9 @@ * @param Number representing the maximum data rate range from 0 to 7 * @return Returns true if parameters are valid or false if not. */ -bool RN2483::setDrRange(uint8_t channelID, uint8_t minRange, uint8_t maxRange){ - if((channelID <= 15)&&(minRange<=7)&&(maxRange<=7)){ +bool RN2483::setDrRange(uint8_t channelID, uint8_t minRange, uint8_t maxRange) +{ + if((channelID <= 15)&&(minRange<=7)&&(maxRange<=7)) { char buffer [15]; int bytesWritten = sprintf(buffer, "%d %d %d", channelID, minRange, maxRange); // Check to make sure sprintf did not return an error before sending. @@ -462,36 +500,39 @@ * before enabling! * @return Returns true if parameters are valid or false if not. */ -bool RN2483::setStatus(uint8_t channelID, bool status){ - if((channelID <= 15)){ - int bytesWritten = 0; - char buffer [15]; - if(status) - bytesWritten = sprintf(buffer, "%d %s", channelID, "on"); - else { - bytesWritten = sprintf(buffer, "%d %s", channelID, "off"); - } - // Check to make sure sprintf did not return an error before sending. - if(bytesWritten > 0) { - return setMacParam(STR_CH_STATUS, buffer); - } +bool RN2483::setStatus(uint8_t channelID, bool status) +{ + if((channelID <= 15)) { + int bytesWritten = 0; + char buffer [15]; + if(status) + bytesWritten = sprintf(buffer, "%d %s", channelID, "on"); + else { + bytesWritten = sprintf(buffer, "%d %s", channelID, "off"); + } + // Check to make sure sprintf did not return an error before sending. + if(bytesWritten > 0) { + return sendCommand(STR_CMD_SET_CHANNEL_STATUS, buffer); + } } return false; } /** -* @brief The network can issue a command to silence the RN2483. This restores the module. +* @brief The network can issue a command to silence the RN2483. This restores the module. * @return Returns true if parameters are valid or false if not. */ -bool RN2483::forceEnable(){ +bool RN2483::forceEnable() +{ return sendCommand(STR_MAC_FORCEENABLE); } /** -* @brief Saves configurable parameters to eeprom. +* @brief Saves configurable parameters to eeprom. * @return Returns true if parameters are valid or false if not. */ -bool RN2483::saveConfiguration(){ +bool RN2483::saveConfiguration() +{ return sendCommand(STR_CMD_SAVE); } @@ -630,7 +671,7 @@ { STR_RESULT_INVALID_PARAM, InternalError }, { STR_RESULT_NOT_JOINED, NotConnected }, { STR_RESULT_NO_FREE_CHANNEL, Busy }, - { STR_RESULT_SILENT, Busy }, + { STR_RESULT_SILENT, Silent }, { STR_RESULT_FRAME_COUNTER_ERROR, NetworkFatalError }, { STR_RESULT_BUSY, Busy }, { STR_RESULT_MAC_PAUSED, InternalError },