LM005A Lora Module test
Dependents: LoRaTest OnenetTest
LM005A/LM005A.cpp@0:5b5a1b5f1ae4, 2018-04-19 (annotated)
- Committer:
- yao6116601
- Date:
- Thu Apr 19 13:15:25 2018 +0000
- Revision:
- 0:5b5a1b5f1ae4
LM005A LoRa module interface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yao6116601 | 0:5b5a1b5f1ae4 | 1 | /* LM005A Example |
yao6116601 | 0:5b5a1b5f1ae4 | 2 | * Copyright (c) 2015 ARM Limited |
yao6116601 | 0:5b5a1b5f1ae4 | 3 | * |
yao6116601 | 0:5b5a1b5f1ae4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yao6116601 | 0:5b5a1b5f1ae4 | 5 | * you may not use this file except in compliance with the License. |
yao6116601 | 0:5b5a1b5f1ae4 | 6 | * You may obtain a copy of the License at |
yao6116601 | 0:5b5a1b5f1ae4 | 7 | * |
yao6116601 | 0:5b5a1b5f1ae4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yao6116601 | 0:5b5a1b5f1ae4 | 9 | * |
yao6116601 | 0:5b5a1b5f1ae4 | 10 | * Unless required by applicable law or agreed to in writing, software |
yao6116601 | 0:5b5a1b5f1ae4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yao6116601 | 0:5b5a1b5f1ae4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yao6116601 | 0:5b5a1b5f1ae4 | 13 | * See the License for the specific language governing permissions and |
yao6116601 | 0:5b5a1b5f1ae4 | 14 | * limitations under the License. |
yao6116601 | 0:5b5a1b5f1ae4 | 15 | */ |
yao6116601 | 0:5b5a1b5f1ae4 | 16 | |
yao6116601 | 0:5b5a1b5f1ae4 | 17 | #include "LM005A.h" |
yao6116601 | 0:5b5a1b5f1ae4 | 18 | #include <cstring> |
yao6116601 | 0:5b5a1b5f1ae4 | 19 | |
yao6116601 | 0:5b5a1b5f1ae4 | 20 | #define LM005A_DEFAULT_BAUD_RATE 115200 |
yao6116601 | 0:5b5a1b5f1ae4 | 21 | |
yao6116601 | 0:5b5a1b5f1ae4 | 22 | LM005A::LM005A(PinName tx, PinName rx, bool debug) |
yao6116601 | 0:5b5a1b5f1ae4 | 23 | : _serial(tx, rx, LM005A_DEFAULT_BAUD_RATE), |
yao6116601 | 0:5b5a1b5f1ae4 | 24 | _parser(&_serial), |
yao6116601 | 0:5b5a1b5f1ae4 | 25 | _fail(false) |
yao6116601 | 0:5b5a1b5f1ae4 | 26 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 27 | _serial.set_baud( LM005A_DEFAULT_BAUD_RATE ); |
yao6116601 | 0:5b5a1b5f1ae4 | 28 | _parser.debug_on(debug); |
yao6116601 | 0:5b5a1b5f1ae4 | 29 | _parser.set_delimiter("\r\n"); |
yao6116601 | 0:5b5a1b5f1ae4 | 30 | _parser.oob("+RXDONE:", callback(this, &LM005A::_packet_handler)); |
yao6116601 | 0:5b5a1b5f1ae4 | 31 | // _parser.oob("+JOIN:", callback(this, &LM005A::_tx_handler)); |
yao6116601 | 0:5b5a1b5f1ae4 | 32 | |
yao6116601 | 0:5b5a1b5f1ae4 | 33 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 34 | |
yao6116601 | 0:5b5a1b5f1ae4 | 35 | const char * LM005A::get_firmware_version() |
yao6116601 | 0:5b5a1b5f1ae4 | 36 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 37 | if (!(_parser.send("AT+VERSION?") |
yao6116601 | 0:5b5a1b5f1ae4 | 38 | && _parser.recv("+VERSION:%s\n",buffer) |
yao6116601 | 0:5b5a1b5f1ae4 | 39 | && _parser.recv("OK"))) { |
yao6116601 | 0:5b5a1b5f1ae4 | 40 | return NULL; |
yao6116601 | 0:5b5a1b5f1ae4 | 41 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 42 | return buffer; |
yao6116601 | 0:5b5a1b5f1ae4 | 43 | |
yao6116601 | 0:5b5a1b5f1ae4 | 44 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 45 | bool LM005A::init(void) |
yao6116601 | 0:5b5a1b5f1ae4 | 46 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 47 | if (!_parser.recv("Bootup Completed!!!")) |
yao6116601 | 0:5b5a1b5f1ae4 | 48 | return false; |
yao6116601 | 0:5b5a1b5f1ae4 | 49 | return true; |
yao6116601 | 0:5b5a1b5f1ae4 | 50 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 51 | bool LM005A::reset(void) |
yao6116601 | 0:5b5a1b5f1ae4 | 52 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 53 | if (!(_parser.send("AT+RESET") |
yao6116601 | 0:5b5a1b5f1ae4 | 54 | && _parser.recv("+RESET") |
yao6116601 | 0:5b5a1b5f1ae4 | 55 | && _parser.recv("OK") |
yao6116601 | 0:5b5a1b5f1ae4 | 56 | &&_parser.recv("Bootup Completed!!!"))) { |
yao6116601 | 0:5b5a1b5f1ae4 | 57 | return false; |
yao6116601 | 0:5b5a1b5f1ae4 | 58 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 59 | return true; |
yao6116601 | 0:5b5a1b5f1ae4 | 60 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 61 | |
yao6116601 | 0:5b5a1b5f1ae4 | 62 | int LM005A::set_mode(int mode) |
yao6116601 | 0:5b5a1b5f1ae4 | 63 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 64 | int res; |
yao6116601 | 0:5b5a1b5f1ae4 | 65 | if (!(_parser.send("AT+MODE=%d",mode) |
yao6116601 | 0:5b5a1b5f1ae4 | 66 | && _parser.recv("+MODE:%d", &res) |
yao6116601 | 0:5b5a1b5f1ae4 | 67 | && _parser.recv("OK"))) { |
yao6116601 | 0:5b5a1b5f1ae4 | 68 | return -1; |
yao6116601 | 0:5b5a1b5f1ae4 | 69 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 70 | |
yao6116601 | 0:5b5a1b5f1ae4 | 71 | return res; |
yao6116601 | 0:5b5a1b5f1ae4 | 72 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 73 | int LM005A::set_class(int class_mode) |
yao6116601 | 0:5b5a1b5f1ae4 | 74 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 75 | int res; |
yao6116601 | 0:5b5a1b5f1ae4 | 76 | if (!(_parser.send("AT+CLASS=%d",class_mode) |
yao6116601 | 0:5b5a1b5f1ae4 | 77 | && _parser.recv("+CLASS:%d", &res) |
yao6116601 | 0:5b5a1b5f1ae4 | 78 | && _parser.recv("OK"))) { |
yao6116601 | 0:5b5a1b5f1ae4 | 79 | return -1; |
yao6116601 | 0:5b5a1b5f1ae4 | 80 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 81 | |
yao6116601 | 0:5b5a1b5f1ae4 | 82 | return res; |
yao6116601 | 0:5b5a1b5f1ae4 | 83 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 84 | int LM005A::set_band(int gap,int band) |
yao6116601 | 0:5b5a1b5f1ae4 | 85 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 86 | int g,f; |
yao6116601 | 0:5b5a1b5f1ae4 | 87 | if (!(_parser.send("AT+FIXEDBAND=%d,%d",gap,band) |
yao6116601 | 0:5b5a1b5f1ae4 | 88 | && _parser.recv("+FIXEDBAND:%d,%d\n",&g ,&f) |
yao6116601 | 0:5b5a1b5f1ae4 | 89 | && _parser.recv("OK"))) { |
yao6116601 | 0:5b5a1b5f1ae4 | 90 | return 0; |
yao6116601 | 0:5b5a1b5f1ae4 | 91 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 92 | |
yao6116601 | 0:5b5a1b5f1ae4 | 93 | return f; } |
yao6116601 | 0:5b5a1b5f1ae4 | 94 | int LM005A::set_power(int power) |
yao6116601 | 0:5b5a1b5f1ae4 | 95 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 96 | int res; |
yao6116601 | 0:5b5a1b5f1ae4 | 97 | if (!(_parser.send("AT+POWER=%d",power) |
yao6116601 | 0:5b5a1b5f1ae4 | 98 | && _parser.recv("+POWER:%d\n", &res) |
yao6116601 | 0:5b5a1b5f1ae4 | 99 | && _parser.recv("OK"))) { |
yao6116601 | 0:5b5a1b5f1ae4 | 100 | return 0; |
yao6116601 | 0:5b5a1b5f1ae4 | 101 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 102 | |
yao6116601 | 0:5b5a1b5f1ae4 | 103 | return res; |
yao6116601 | 0:5b5a1b5f1ae4 | 104 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 105 | |
yao6116601 | 0:5b5a1b5f1ae4 | 106 | bool LM005A::join() |
yao6116601 | 0:5b5a1b5f1ae4 | 107 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 108 | |
yao6116601 | 0:5b5a1b5f1ae4 | 109 | if (!(_parser.send("AT+JOIN") |
yao6116601 | 0:5b5a1b5f1ae4 | 110 | && _parser.recv("OK"))) { |
yao6116601 | 0:5b5a1b5f1ae4 | 111 | return false; |
yao6116601 | 0:5b5a1b5f1ae4 | 112 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 113 | return true; |
yao6116601 | 0:5b5a1b5f1ae4 | 114 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 115 | |
yao6116601 | 0:5b5a1b5f1ae4 | 116 | const char *LM005A::getEUI(int type) |
yao6116601 | 0:5b5a1b5f1ae4 | 117 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 118 | if (type==0) |
yao6116601 | 0:5b5a1b5f1ae4 | 119 | { if (!(_parser.send("AT+DEVEUI?") |
yao6116601 | 0:5b5a1b5f1ae4 | 120 | && _parser.recv("+DEVEUI%s\n", buffer) |
yao6116601 | 0:5b5a1b5f1ae4 | 121 | && _parser.recv("OK"))) { |
yao6116601 | 0:5b5a1b5f1ae4 | 122 | return NULL; |
yao6116601 | 0:5b5a1b5f1ae4 | 123 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 124 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 125 | else |
yao6116601 | 0:5b5a1b5f1ae4 | 126 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 127 | if (!(_parser.send("AT+APPEUI?") |
yao6116601 | 0:5b5a1b5f1ae4 | 128 | && _parser.recv("+APPEUI%s\n", buffer) |
yao6116601 | 0:5b5a1b5f1ae4 | 129 | && _parser.recv("OK"))) { |
yao6116601 | 0:5b5a1b5f1ae4 | 130 | return NULL; |
yao6116601 | 0:5b5a1b5f1ae4 | 131 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 132 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 133 | return buffer; |
yao6116601 | 0:5b5a1b5f1ae4 | 134 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 135 | const char *LM005A::getDeviceAddress(void) |
yao6116601 | 0:5b5a1b5f1ae4 | 136 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 137 | |
yao6116601 | 0:5b5a1b5f1ae4 | 138 | |
yao6116601 | 0:5b5a1b5f1ae4 | 139 | if (!(_parser.send("AT+DEVADDR?") |
yao6116601 | 0:5b5a1b5f1ae4 | 140 | && _parser.recv("+DEVADDR:%s\n", buffer) |
yao6116601 | 0:5b5a1b5f1ae4 | 141 | && _parser.recv("OK"))) { |
yao6116601 | 0:5b5a1b5f1ae4 | 142 | return NULL; |
yao6116601 | 0:5b5a1b5f1ae4 | 143 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 144 | |
yao6116601 | 0:5b5a1b5f1ae4 | 145 | return buffer; |
yao6116601 | 0:5b5a1b5f1ae4 | 146 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 147 | |
yao6116601 | 0:5b5a1b5f1ae4 | 148 | unsigned char hex_char(char c) |
yao6116601 | 0:5b5a1b5f1ae4 | 149 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 150 | if ('0' <= c && c <= '9') return (unsigned char)(c - '0'); |
yao6116601 | 0:5b5a1b5f1ae4 | 151 | if ('A' <= c && c <= 'F') return (unsigned char)(c - 'A' + 10); |
yao6116601 | 0:5b5a1b5f1ae4 | 152 | if ('a' <= c && c <= 'f') return (unsigned char)(c - 'a' + 10); |
yao6116601 | 0:5b5a1b5f1ae4 | 153 | // printf("Error Val=%d\n",c); |
yao6116601 | 0:5b5a1b5f1ae4 | 154 | return 0xFF; |
yao6116601 | 0:5b5a1b5f1ae4 | 155 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 156 | bool LM005A::send(int port,int ack, const void *data, uint32_t amount) |
yao6116601 | 0:5b5a1b5f1ae4 | 157 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 158 | |
yao6116601 | 0:5b5a1b5f1ae4 | 159 | if (!(_parser.send("AT+MSG=%d,%d,%d,%.*s",port,ack,amount,amount,data) |
yao6116601 | 0:5b5a1b5f1ae4 | 160 | && _parser.recv("+MSG:TXDONE") |
yao6116601 | 0:5b5a1b5f1ae4 | 161 | && _parser.recv("+MSG:RXDONE") |
yao6116601 | 0:5b5a1b5f1ae4 | 162 | && _parser.recv("OK"))) |
yao6116601 | 0:5b5a1b5f1ae4 | 163 | return false; |
yao6116601 | 0:5b5a1b5f1ae4 | 164 | return true; |
yao6116601 | 0:5b5a1b5f1ae4 | 165 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 166 | bool LM005A::sendhex(int port,int ack, const void *data, uint32_t amount) |
yao6116601 | 0:5b5a1b5f1ae4 | 167 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 168 | |
yao6116601 | 0:5b5a1b5f1ae4 | 169 | if (!(_parser.send("AT+MSGHEX=%d,%d,%d,%.*s",port,ack,amount,amount,data) |
yao6116601 | 0:5b5a1b5f1ae4 | 170 | && _parser.recv("+MSGHEX:TXDONE") |
yao6116601 | 0:5b5a1b5f1ae4 | 171 | && _parser.recv("OK"))) |
yao6116601 | 0:5b5a1b5f1ae4 | 172 | return false; |
yao6116601 | 0:5b5a1b5f1ae4 | 173 | return true; |
yao6116601 | 0:5b5a1b5f1ae4 | 174 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 175 | |
yao6116601 | 0:5b5a1b5f1ae4 | 176 | void LM005A::_packet_handler() |
yao6116601 | 0:5b5a1b5f1ae4 | 177 | { uint32_t amount; |
yao6116601 | 0:5b5a1b5f1ae4 | 178 | char rBuf[128]; |
yao6116601 | 0:5b5a1b5f1ae4 | 179 | int i,p; |
yao6116601 | 0:5b5a1b5f1ae4 | 180 | char val; |
yao6116601 | 0:5b5a1b5f1ae4 | 181 | if (!_parser.recv("%d,",&amount)) { |
yao6116601 | 0:5b5a1b5f1ae4 | 182 | return; |
yao6116601 | 0:5b5a1b5f1ae4 | 183 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 184 | printf("amount=%d\n",amount); |
yao6116601 | 0:5b5a1b5f1ae4 | 185 | rCounter=rCounter+amount; |
yao6116601 | 0:5b5a1b5f1ae4 | 186 | _parser.read(rBuf,amount*3-1); |
yao6116601 | 0:5b5a1b5f1ae4 | 187 | p=0; |
yao6116601 | 0:5b5a1b5f1ae4 | 188 | for (i=0;i<amount;i++) |
yao6116601 | 0:5b5a1b5f1ae4 | 189 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 190 | val=hex_char( rBuf[p++]); |
yao6116601 | 0:5b5a1b5f1ae4 | 191 | val=(val<<4)+hex_char( rBuf[p++]); |
yao6116601 | 0:5b5a1b5f1ae4 | 192 | p++; |
yao6116601 | 0:5b5a1b5f1ae4 | 193 | rBuffer[rIndex++]=val; |
yao6116601 | 0:5b5a1b5f1ae4 | 194 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 195 | rBuffer[rIndex++]=0x00; |
yao6116601 | 0:5b5a1b5f1ae4 | 196 | // printf("Rec=%0.*s",amount,rBuffer); |
yao6116601 | 0:5b5a1b5f1ae4 | 197 | |
yao6116601 | 0:5b5a1b5f1ae4 | 198 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 199 | |
yao6116601 | 0:5b5a1b5f1ae4 | 200 | |
yao6116601 | 0:5b5a1b5f1ae4 | 201 | int32_t LM005A::recv(int id, void *data, uint32_t amount) |
yao6116601 | 0:5b5a1b5f1ae4 | 202 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 203 | rIndex=0; |
yao6116601 | 0:5b5a1b5f1ae4 | 204 | rCounter=0; |
yao6116601 | 0:5b5a1b5f1ae4 | 205 | |
yao6116601 | 0:5b5a1b5f1ae4 | 206 | while (true) { |
yao6116601 | 0:5b5a1b5f1ae4 | 207 | if (rCounter>0) |
yao6116601 | 0:5b5a1b5f1ae4 | 208 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 209 | memcpy(data,rBuffer,rCounter); |
yao6116601 | 0:5b5a1b5f1ae4 | 210 | break; |
yao6116601 | 0:5b5a1b5f1ae4 | 211 | |
yao6116601 | 0:5b5a1b5f1ae4 | 212 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 213 | if (!_parser.process_oob()) { |
yao6116601 | 0:5b5a1b5f1ae4 | 214 | return -1; |
yao6116601 | 0:5b5a1b5f1ae4 | 215 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 216 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 217 | return rCounter; |
yao6116601 | 0:5b5a1b5f1ae4 | 218 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 219 | |
yao6116601 | 0:5b5a1b5f1ae4 | 220 | |
yao6116601 | 0:5b5a1b5f1ae4 | 221 | void LM005A::setTimeout(uint32_t timeout_ms) |
yao6116601 | 0:5b5a1b5f1ae4 | 222 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 223 | _parser.set_timeout(timeout_ms); |
yao6116601 | 0:5b5a1b5f1ae4 | 224 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 225 | |
yao6116601 | 0:5b5a1b5f1ae4 | 226 | |
yao6116601 | 0:5b5a1b5f1ae4 | 227 | |
yao6116601 | 0:5b5a1b5f1ae4 | 228 | void LM005A::attach(Callback<void()> func) |
yao6116601 | 0:5b5a1b5f1ae4 | 229 | { |
yao6116601 | 0:5b5a1b5f1ae4 | 230 | _serial.sigio(func); |
yao6116601 | 0:5b5a1b5f1ae4 | 231 | } |
yao6116601 | 0:5b5a1b5f1ae4 | 232 | |
yao6116601 | 0:5b5a1b5f1ae4 | 233 | |
yao6116601 | 0:5b5a1b5f1ae4 | 234 | |
yao6116601 | 0:5b5a1b5f1ae4 | 235 | |
yao6116601 | 0:5b5a1b5f1ae4 | 236 | |
yao6116601 | 0:5b5a1b5f1ae4 | 237 |