contain lorawan with serial_rx enabled
Dependencies: pulga-lorawan-drv SPI_MX25R Si1133 BME280
gps.cpp@63:4ec1808fb547, 2021-02-26 (annotated)
- Committer:
- ruschigo
- Date:
- Fri Feb 26 18:11:06 2021 +0000
- Revision:
- 63:4ec1808fb547
- Child:
- 65:4090220e19d2
insert serial rx by interrupt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ruschigo | 63:4ec1808fb547 | 1 | #include "gps.h" |
ruschigo | 63:4ec1808fb547 | 2 | |
ruschigo | 63:4ec1808fb547 | 3 | //GPS DEclaration |
ruschigo | 63:4ec1808fb547 | 4 | SPI spi_2(P0_5, P0_7, P0_11); // mosi, miso, sclk |
ruschigo | 63:4ec1808fb547 | 5 | DigitalOut cs(P0_30); |
ruschigo | 63:4ec1808fb547 | 6 | DigitalOut gps_reset(P1_2); |
ruschigo | 63:4ec1808fb547 | 7 | DigitalOut gps_int (P0_23); |
ruschigo | 63:4ec1808fb547 | 8 | DigitalOut gps_DSEL (P0_27); |
ruschigo | 63:4ec1808fb547 | 9 | |
ruschigo | 63:4ec1808fb547 | 10 | void gps_leBootMsg(){ |
ruschigo | 63:4ec1808fb547 | 11 | |
ruschigo | 63:4ec1808fb547 | 12 | #define MAXIMUM_PACKET_SIZE 60 |
ruschigo | 63:4ec1808fb547 | 13 | int packet_size; |
ruschigo | 63:4ec1808fb547 | 14 | uint8_t packet[MAXIMUM_PACKET_SIZE]; |
ruschigo | 63:4ec1808fb547 | 15 | uint8_t value; |
ruschigo | 63:4ec1808fb547 | 16 | int cont; |
ruschigo | 63:4ec1808fb547 | 17 | value = spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 18 | |
ruschigo | 63:4ec1808fb547 | 19 | while (value != '$' ){ //wait start boot msg |
ruschigo | 63:4ec1808fb547 | 20 | value = spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 21 | wait_ms(5); |
ruschigo | 63:4ec1808fb547 | 22 | cont++; |
ruschigo | 63:4ec1808fb547 | 23 | if (cont > 100){ |
ruschigo | 63:4ec1808fb547 | 24 | printf("\n no response \n"); |
ruschigo | 63:4ec1808fb547 | 25 | return; |
ruschigo | 63:4ec1808fb547 | 26 | } |
ruschigo | 63:4ec1808fb547 | 27 | } |
ruschigo | 63:4ec1808fb547 | 28 | packet[0] = value; |
ruschigo | 63:4ec1808fb547 | 29 | value = spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 30 | for (packet_size = 1 ; packet_size <= MAXIMUM_PACKET_SIZE ;packet_size++) { |
ruschigo | 63:4ec1808fb547 | 31 | if (value != '\n' ){ |
ruschigo | 63:4ec1808fb547 | 32 | packet [packet_size]= value; |
ruschigo | 63:4ec1808fb547 | 33 | value = spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 34 | } |
ruschigo | 63:4ec1808fb547 | 35 | else { |
ruschigo | 63:4ec1808fb547 | 36 | //lora_send_packet (packet , (uint8_t) packet_size+1); // manda atraves do lora a mensagem de boot do gps |
ruschigo | 63:4ec1808fb547 | 37 | printf("Boot msg: %s \n", packet); |
ruschigo | 63:4ec1808fb547 | 38 | return; |
ruschigo | 63:4ec1808fb547 | 39 | } |
ruschigo | 63:4ec1808fb547 | 40 | |
ruschigo | 63:4ec1808fb547 | 41 | } |
ruschigo | 63:4ec1808fb547 | 42 | |
ruschigo | 63:4ec1808fb547 | 43 | } |
ruschigo | 63:4ec1808fb547 | 44 | |
ruschigo | 63:4ec1808fb547 | 45 | void gps_le_envia_linha(){ |
ruschigo | 63:4ec1808fb547 | 46 | |
ruschigo | 63:4ec1808fb547 | 47 | uint8_t packet[150]; |
ruschigo | 63:4ec1808fb547 | 48 | uint8_t value; |
ruschigo | 63:4ec1808fb547 | 49 | |
ruschigo | 63:4ec1808fb547 | 50 | for (int i=0;i < 149; i++){ |
ruschigo | 63:4ec1808fb547 | 51 | if (value == '\n'){ |
ruschigo | 63:4ec1808fb547 | 52 | //lora_send_packet (packet , (uint8_t) i); |
ruschigo | 63:4ec1808fb547 | 53 | return; |
ruschigo | 63:4ec1808fb547 | 54 | } |
ruschigo | 63:4ec1808fb547 | 55 | else packet [i] =value; |
ruschigo | 63:4ec1808fb547 | 56 | } |
ruschigo | 63:4ec1808fb547 | 57 | //lora_send_packet (packet , (uint8_t) 99); |
ruschigo | 63:4ec1808fb547 | 58 | |
ruschigo | 63:4ec1808fb547 | 59 | } |
ruschigo | 63:4ec1808fb547 | 60 | |
ruschigo | 63:4ec1808fb547 | 61 | |
ruschigo | 63:4ec1808fb547 | 62 | gps_ubxPacket gps_calcula_check(gps_ubxPacket Packet) { |
ruschigo | 63:4ec1808fb547 | 63 | |
ruschigo | 63:4ec1808fb547 | 64 | uint8_t Buffer[Packet.len + 4]; |
ruschigo | 63:4ec1808fb547 | 65 | uint8_t CK_A=0; |
ruschigo | 63:4ec1808fb547 | 66 | uint8_t CK_B=0; |
ruschigo | 63:4ec1808fb547 | 67 | |
ruschigo | 63:4ec1808fb547 | 68 | Buffer[0]= Packet.cls; |
ruschigo | 63:4ec1808fb547 | 69 | Buffer[1]= Packet.id; |
ruschigo | 63:4ec1808fb547 | 70 | Buffer[2]= Packet.len & 0xFF; |
ruschigo | 63:4ec1808fb547 | 71 | Buffer[3]= (Packet.len >> 8)& 0xFF;; |
ruschigo | 63:4ec1808fb547 | 72 | |
ruschigo | 63:4ec1808fb547 | 73 | //send_packet (Buffer, (uint8_t) 4 ); |
ruschigo | 63:4ec1808fb547 | 74 | |
ruschigo | 63:4ec1808fb547 | 75 | for (uint16_t i = 0; i < Packet.len; i++) { |
ruschigo | 63:4ec1808fb547 | 76 | Buffer [i+4] = Packet.payload[i] ; |
ruschigo | 63:4ec1808fb547 | 77 | } |
ruschigo | 63:4ec1808fb547 | 78 | |
ruschigo | 63:4ec1808fb547 | 79 | //send_packet (Buffer, (uint8_t) Packet.len + 4 ); |
ruschigo | 63:4ec1808fb547 | 80 | |
ruschigo | 63:4ec1808fb547 | 81 | for(int i=0 ; i < Packet.len + 4 ; i++) { |
ruschigo | 63:4ec1808fb547 | 82 | CK_A = CK_A + Buffer[i]; |
ruschigo | 63:4ec1808fb547 | 83 | CK_B = CK_B + CK_A; |
ruschigo | 63:4ec1808fb547 | 84 | CK_A = CK_A & 0xFF; |
ruschigo | 63:4ec1808fb547 | 85 | CK_B = CK_B & 0xFF; |
ruschigo | 63:4ec1808fb547 | 86 | //uint8_t packet_check[5] ={Buffer[i],(uint8_t) (i+1),CK_A,CK_B,'#'}; |
ruschigo | 63:4ec1808fb547 | 87 | // send_packet (packet_check , (uint8_t) 5); |
ruschigo | 63:4ec1808fb547 | 88 | } |
ruschigo | 63:4ec1808fb547 | 89 | |
ruschigo | 63:4ec1808fb547 | 90 | |
ruschigo | 63:4ec1808fb547 | 91 | // uint8_t packet_check[2] ={CK_A, CK_B}; |
ruschigo | 63:4ec1808fb547 | 92 | // send_packet (packet_check , (uint8_t) 2); |
ruschigo | 63:4ec1808fb547 | 93 | |
ruschigo | 63:4ec1808fb547 | 94 | Packet.checksumA = CK_A; |
ruschigo | 63:4ec1808fb547 | 95 | Packet.checksumB = CK_B; |
ruschigo | 63:4ec1808fb547 | 96 | |
ruschigo | 63:4ec1808fb547 | 97 | return Packet; |
ruschigo | 63:4ec1808fb547 | 98 | |
ruschigo | 63:4ec1808fb547 | 99 | } |
ruschigo | 63:4ec1808fb547 | 100 | |
ruschigo | 63:4ec1808fb547 | 101 | void send_gps_packet(gps_ubxPacket packet){ |
ruschigo | 63:4ec1808fb547 | 102 | |
ruschigo | 63:4ec1808fb547 | 103 | spi_2.write(SYNC1); |
ruschigo | 63:4ec1808fb547 | 104 | spi_2.write(SYNC2); |
ruschigo | 63:4ec1808fb547 | 105 | spi_2.write(packet.cls); |
ruschigo | 63:4ec1808fb547 | 106 | spi_2.write(packet.id); |
ruschigo | 63:4ec1808fb547 | 107 | spi_2.write(packet.len & 0xFF); |
ruschigo | 63:4ec1808fb547 | 108 | spi_2.write((packet.len >> 8)& 0xFF); |
ruschigo | 63:4ec1808fb547 | 109 | |
ruschigo | 63:4ec1808fb547 | 110 | for (uint16_t i = 0; i < packet.len; i++) { |
ruschigo | 63:4ec1808fb547 | 111 | spi_2.write(packet.payload[i]); |
ruschigo | 63:4ec1808fb547 | 112 | } |
ruschigo | 63:4ec1808fb547 | 113 | spi_2.write(packet.checksumA); |
ruschigo | 63:4ec1808fb547 | 114 | spi_2.write(packet.checksumB); |
ruschigo | 63:4ec1808fb547 | 115 | |
ruschigo | 63:4ec1808fb547 | 116 | //=============imprime resposta |
ruschigo | 63:4ec1808fb547 | 117 | wait_ms(50); |
ruschigo | 63:4ec1808fb547 | 118 | gps_le_envia_linha(); |
ruschigo | 63:4ec1808fb547 | 119 | gps_le_envia_linha(); |
ruschigo | 63:4ec1808fb547 | 120 | |
ruschigo | 63:4ec1808fb547 | 121 | } |
ruschigo | 63:4ec1808fb547 | 122 | |
ruschigo | 63:4ec1808fb547 | 123 | gps_navPVT le_nav_pvt () { |
ruschigo | 63:4ec1808fb547 | 124 | |
ruschigo | 63:4ec1808fb547 | 125 | gps_navPVT Pac; |
ruschigo | 63:4ec1808fb547 | 126 | |
ruschigo | 63:4ec1808fb547 | 127 | char state = 0; |
ruschigo | 63:4ec1808fb547 | 128 | int cont =0; |
ruschigo | 63:4ec1808fb547 | 129 | int numb=0; |
ruschigo | 63:4ec1808fb547 | 130 | |
ruschigo | 63:4ec1808fb547 | 131 | while (1){ //começou mensagem |
ruschigo | 63:4ec1808fb547 | 132 | if (cont > 100) { |
ruschigo | 63:4ec1808fb547 | 133 | // BMX160_read_acc(); |
ruschigo | 63:4ec1808fb547 | 134 | return Pac; |
ruschigo | 63:4ec1808fb547 | 135 | } |
ruschigo | 63:4ec1808fb547 | 136 | if (state == 0 ){ |
ruschigo | 63:4ec1808fb547 | 137 | |
ruschigo | 63:4ec1808fb547 | 138 | if ( spi_2.write(0x00) == 0xB5){ |
ruschigo | 63:4ec1808fb547 | 139 | state =1; |
ruschigo | 63:4ec1808fb547 | 140 | } |
ruschigo | 63:4ec1808fb547 | 141 | else { |
ruschigo | 63:4ec1808fb547 | 142 | cont++; |
ruschigo | 63:4ec1808fb547 | 143 | wait_ms(40); |
ruschigo | 63:4ec1808fb547 | 144 | } |
ruschigo | 63:4ec1808fb547 | 145 | |
ruschigo | 63:4ec1808fb547 | 146 | } |
ruschigo | 63:4ec1808fb547 | 147 | else if (state == 1){ // read 0xb5 |
ruschigo | 63:4ec1808fb547 | 148 | if (spi_2.write(0x00) == 0x62){ |
ruschigo | 63:4ec1808fb547 | 149 | state =2; |
ruschigo | 63:4ec1808fb547 | 150 | //wait_ms(30); |
ruschigo | 63:4ec1808fb547 | 151 | } |
ruschigo | 63:4ec1808fb547 | 152 | else state =0; |
ruschigo | 63:4ec1808fb547 | 153 | } |
ruschigo | 63:4ec1808fb547 | 154 | else if (state == 2) {// read 0xb5 0x62 |
ruschigo | 63:4ec1808fb547 | 155 | if (spi_2.write(0x00) == 0x01){ |
ruschigo | 63:4ec1808fb547 | 156 | //printf("le_nav_pvt going to state 3"); |
ruschigo | 63:4ec1808fb547 | 157 | state =3; |
ruschigo | 63:4ec1808fb547 | 158 | //wait_ms(30); |
ruschigo | 63:4ec1808fb547 | 159 | } |
ruschigo | 63:4ec1808fb547 | 160 | else state =0; |
ruschigo | 63:4ec1808fb547 | 161 | } |
ruschigo | 63:4ec1808fb547 | 162 | else if (state == 3) {// read 0xb5 0x62 0x01 |
ruschigo | 63:4ec1808fb547 | 163 | if (spi_2.write(0x00) == 0x07){ |
ruschigo | 63:4ec1808fb547 | 164 | state =4; |
ruschigo | 63:4ec1808fb547 | 165 | //printf("le_nav_pvt going to state 4"); |
ruschigo | 63:4ec1808fb547 | 166 | //wait_ms(30); |
ruschigo | 63:4ec1808fb547 | 167 | } |
ruschigo | 63:4ec1808fb547 | 168 | else state =0; |
ruschigo | 63:4ec1808fb547 | 169 | } |
ruschigo | 63:4ec1808fb547 | 170 | else if (state == 4) {// read 0xb5 0x62 0x01 0x07 |
ruschigo | 63:4ec1808fb547 | 171 | if (spi_2.write(0x00) == 0x5c){ |
ruschigo | 63:4ec1808fb547 | 172 | state =5; |
ruschigo | 63:4ec1808fb547 | 173 | //printf("le_nav_pvt going to state 5"); |
ruschigo | 63:4ec1808fb547 | 174 | //wait_ms(30); |
ruschigo | 63:4ec1808fb547 | 175 | } |
ruschigo | 63:4ec1808fb547 | 176 | else state =0; |
ruschigo | 63:4ec1808fb547 | 177 | } |
ruschigo | 63:4ec1808fb547 | 178 | else if (state == 5) {// read 0xb5 0x62 0x01 0x07 0x5c |
ruschigo | 63:4ec1808fb547 | 179 | if (spi_2.write(0x00) == 0x00){ |
ruschigo | 63:4ec1808fb547 | 180 | state =6; |
ruschigo | 63:4ec1808fb547 | 181 | //printf("le_nav_pvt going to state 6"); |
ruschigo | 63:4ec1808fb547 | 182 | //wait_ms(30); |
ruschigo | 63:4ec1808fb547 | 183 | } |
ruschigo | 63:4ec1808fb547 | 184 | else state =0; |
ruschigo | 63:4ec1808fb547 | 185 | } |
ruschigo | 63:4ec1808fb547 | 186 | else if (state == 6){ // read 0xb5 0x62 0x01 0x07 0x5c 0x00 |
ruschigo | 63:4ec1808fb547 | 187 | uint8_t value1,value2,value3,value4; |
ruschigo | 63:4ec1808fb547 | 188 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 189 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 190 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 191 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 192 | Pac.iTOW = (value4 << 24) + (value3 <<16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 193 | |
ruschigo | 63:4ec1808fb547 | 194 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 195 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 196 | Pac.year = (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 197 | |
ruschigo | 63:4ec1808fb547 | 198 | Pac.month = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 199 | Pac.day = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 200 | Pac.hour = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 201 | Pac.min = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 202 | Pac.sec = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 203 | Pac.valid = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 204 | |
ruschigo | 63:4ec1808fb547 | 205 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 206 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 207 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 208 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 209 | Pac.tAcc = (value4 << 24) + (value3 <<16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 210 | |
ruschigo | 63:4ec1808fb547 | 211 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 212 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 213 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 214 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 215 | Pac.nano = (value4 << 24) + (value3 <<16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 216 | |
ruschigo | 63:4ec1808fb547 | 217 | Pac.fixtype = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 218 | Pac.flags = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 219 | Pac.flags2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 220 | Pac.numSV = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 221 | |
ruschigo | 63:4ec1808fb547 | 222 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 223 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 224 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 225 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 226 | Pac.lon = (value4 << 24) + (value3 <<16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 227 | lon= (value4 << 24) + (value3 <<16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 228 | //printf("Long Data %d \n ", lon); |
ruschigo | 63:4ec1808fb547 | 229 | |
ruschigo | 63:4ec1808fb547 | 230 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 231 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 232 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 233 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 234 | Pac.lat = (value4 << 24) + (value3 <<16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 235 | lat = (value4 << 24) + (value3 <<16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 236 | //printf("Lat Data %d", lat); |
ruschigo | 63:4ec1808fb547 | 237 | |
ruschigo | 63:4ec1808fb547 | 238 | |
ruschigo | 63:4ec1808fb547 | 239 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 240 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 241 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 242 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 243 | Pac.height = (value4 << 24) + (value3 <<16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 244 | |
ruschigo | 63:4ec1808fb547 | 245 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 246 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 247 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 248 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 249 | Pac.hMSL = (value4 << 24) + (value3 <<16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 250 | |
ruschigo | 63:4ec1808fb547 | 251 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 252 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 253 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 254 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 255 | Pac.hAcc = (value4 << 24) + (value3 <<16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 256 | |
ruschigo | 63:4ec1808fb547 | 257 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 258 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 259 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 260 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 261 | Pac.vAcc = (value4 << 24) + (value3 << 16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 262 | |
ruschigo | 63:4ec1808fb547 | 263 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 264 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 265 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 266 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 267 | Pac.velN = (value4 << 24) + (value3 << 16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 268 | |
ruschigo | 63:4ec1808fb547 | 269 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 270 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 271 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 272 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 273 | Pac.velE = (value4 << 24) + (value3 << 16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 274 | |
ruschigo | 63:4ec1808fb547 | 275 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 276 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 277 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 278 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 279 | Pac.velD = (value4 << 24) + (value3 << 16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 280 | |
ruschigo | 63:4ec1808fb547 | 281 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 282 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 283 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 284 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 285 | Pac.gSpeed = (value4 << 24) + (value3 << 16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 286 | |
ruschigo | 63:4ec1808fb547 | 287 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 288 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 289 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 290 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 291 | Pac.headMot = (value4 << 24) + (value3 << 16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 292 | |
ruschigo | 63:4ec1808fb547 | 293 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 294 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 295 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 296 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 297 | Pac.sAcc = (value4 << 24) + (value3 << 16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 298 | |
ruschigo | 63:4ec1808fb547 | 299 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 300 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 301 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 302 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 303 | Pac.headAcc = (value4 << 24) + (value3 << 16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 304 | |
ruschigo | 63:4ec1808fb547 | 305 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 306 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 307 | Pac.pDOP = (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 308 | |
ruschigo | 63:4ec1808fb547 | 309 | for (int i=0; i < 6; i++)spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 310 | |
ruschigo | 63:4ec1808fb547 | 311 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 312 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 313 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 314 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 315 | Pac.headAcc = (value4 << 24) + (value3 << 16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 316 | |
ruschigo | 63:4ec1808fb547 | 317 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 318 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 319 | value3 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 320 | value4 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 321 | Pac.headAcc = (value4 << 24) + (value3 << 16) + (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 322 | |
ruschigo | 63:4ec1808fb547 | 323 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 324 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 325 | Pac.magDec = (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 326 | |
ruschigo | 63:4ec1808fb547 | 327 | value1 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 328 | value2 = 0xff & spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 329 | Pac.magAcc = (value2 << 8) + value1; |
ruschigo | 63:4ec1808fb547 | 330 | |
ruschigo | 63:4ec1808fb547 | 331 | |
ruschigo | 63:4ec1808fb547 | 332 | return Pac; |
ruschigo | 63:4ec1808fb547 | 333 | } |
ruschigo | 63:4ec1808fb547 | 334 | } |
ruschigo | 63:4ec1808fb547 | 335 | wait_ms(100); |
ruschigo | 63:4ec1808fb547 | 336 | } |
ruschigo | 63:4ec1808fb547 | 337 | |
ruschigo | 63:4ec1808fb547 | 338 | void send_nav_pvt (){ |
ruschigo | 63:4ec1808fb547 | 339 | uint8_t packet_nav_pvt[] = { 0xB5, 0x62, 0x01, 0x07, 0x00, 0x00, 0x08, 0x19}; |
ruschigo | 63:4ec1808fb547 | 340 | |
ruschigo | 63:4ec1808fb547 | 341 | //=============envia pacote nav pvt |
ruschigo | 63:4ec1808fb547 | 342 | for ( int i=0; i< sizeof(packet_nav_pvt) ; i++){ |
ruschigo | 63:4ec1808fb547 | 343 | spi_2.write(packet_nav_pvt[i]); |
ruschigo | 63:4ec1808fb547 | 344 | wait_ms(5); |
ruschigo | 63:4ec1808fb547 | 345 | } |
ruschigo | 63:4ec1808fb547 | 346 | |
ruschigo | 63:4ec1808fb547 | 347 | gps_navPVT Data = le_nav_pvt(); |
ruschigo | 63:4ec1808fb547 | 348 | |
ruschigo | 63:4ec1808fb547 | 349 | uint8_t packet [100]; |
ruschigo | 63:4ec1808fb547 | 350 | |
ruschigo | 63:4ec1808fb547 | 351 | packet [0]= (Data.lon >> 24)& 0xff; |
ruschigo | 63:4ec1808fb547 | 352 | packet [1]= (Data.lon >> 16) & 0xff ; |
ruschigo | 63:4ec1808fb547 | 353 | packet [2]= (Data.lon >> 8) & 0xff; |
ruschigo | 63:4ec1808fb547 | 354 | packet [3]= Data.lon & 0xff; |
ruschigo | 63:4ec1808fb547 | 355 | packet [4]= (Data.lat >> 24)& 0xff; |
ruschigo | 63:4ec1808fb547 | 356 | packet [5]= (Data.lat >> 16) & 0xff ; |
ruschigo | 63:4ec1808fb547 | 357 | packet [6]= (Data.lat >> 8) & 0xff; |
ruschigo | 63:4ec1808fb547 | 358 | packet [7]= Data.lat & 0xff; |
ruschigo | 63:4ec1808fb547 | 359 | packet [8]= (Data.hMSL >> 24)& 0xff; |
ruschigo | 63:4ec1808fb547 | 360 | packet [9]= (Data.hMSL >> 16) & 0xff ; |
ruschigo | 63:4ec1808fb547 | 361 | packet [10]= (Data.hMSL >> 8) & 0xff; |
ruschigo | 63:4ec1808fb547 | 362 | packet [11]= Data.hMSL & 0xff; |
ruschigo | 63:4ec1808fb547 | 363 | |
ruschigo | 63:4ec1808fb547 | 364 | if (Data.lon !=0 || Data.lat !=0 ){ |
ruschigo | 63:4ec1808fb547 | 365 | //lora_send_packet (packet , (uint8_t) 12); |
ruschigo | 63:4ec1808fb547 | 366 | } |
ruschigo | 63:4ec1808fb547 | 367 | } |
ruschigo | 63:4ec1808fb547 | 368 | |
ruschigo | 63:4ec1808fb547 | 369 | void send_gps_data(uint8_t *packet, uint8_t size){ |
ruschigo | 63:4ec1808fb547 | 370 | uint8_t packet_rec[size]; |
ruschigo | 63:4ec1808fb547 | 371 | for ( int i=0; i< size ; i++){ |
ruschigo | 63:4ec1808fb547 | 372 | spi_2.write(packet[i]); |
ruschigo | 63:4ec1808fb547 | 373 | |
ruschigo | 63:4ec1808fb547 | 374 | //wait_ms(5); |
ruschigo | 63:4ec1808fb547 | 375 | } |
ruschigo | 63:4ec1808fb547 | 376 | //send_packet (packet_rec ,size); |
ruschigo | 63:4ec1808fb547 | 377 | } |
ruschigo | 63:4ec1808fb547 | 378 | |
ruschigo | 63:4ec1808fb547 | 379 | /* |
ruschigo | 63:4ec1808fb547 | 380 | void wait_packet (uint8_t *header) { |
ruschigo | 63:4ec1808fb547 | 381 | |
ruschigo | 63:4ec1808fb547 | 382 | char state = 0; |
ruschigo | 63:4ec1808fb547 | 383 | int cont =0; |
ruschigo | 63:4ec1808fb547 | 384 | |
ruschigo | 63:4ec1808fb547 | 385 | while (1){ //começou mensagem |
ruschigo | 63:4ec1808fb547 | 386 | if (cont > 250) { |
ruschigo | 63:4ec1808fb547 | 387 | //led2=!led2; |
ruschigo | 63:4ec1808fb547 | 388 | return; |
ruschigo | 63:4ec1808fb547 | 389 | } |
ruschigo | 63:4ec1808fb547 | 390 | if (state == 0 ){ |
ruschigo | 63:4ec1808fb547 | 391 | if (spi_2.write(0x00) == header[0]) |
ruschigo | 63:4ec1808fb547 | 392 | state =1; |
ruschigo | 63:4ec1808fb547 | 393 | else { |
ruschigo | 63:4ec1808fb547 | 394 | cont++; |
ruschigo | 63:4ec1808fb547 | 395 | wait_ms(10); |
ruschigo | 63:4ec1808fb547 | 396 | } |
ruschigo | 63:4ec1808fb547 | 397 | |
ruschigo | 63:4ec1808fb547 | 398 | |
ruschigo | 63:4ec1808fb547 | 399 | } |
ruschigo | 63:4ec1808fb547 | 400 | else if (state == 1){ // read 0xb5 |
ruschigo | 63:4ec1808fb547 | 401 | if (spi_2.write(0x00) == header[1]){ |
ruschigo | 63:4ec1808fb547 | 402 | state =2; |
ruschigo | 63:4ec1808fb547 | 403 | } |
ruschigo | 63:4ec1808fb547 | 404 | else state =0; |
ruschigo | 63:4ec1808fb547 | 405 | } |
ruschigo | 63:4ec1808fb547 | 406 | else if (state == 2) {// read 0xb5 0x62 |
ruschigo | 63:4ec1808fb547 | 407 | if (spi_2.write(0x00) == header[2]){ |
ruschigo | 63:4ec1808fb547 | 408 | state =3; |
ruschigo | 63:4ec1808fb547 | 409 | } |
ruschigo | 63:4ec1808fb547 | 410 | else state =0; |
ruschigo | 63:4ec1808fb547 | 411 | } |
ruschigo | 63:4ec1808fb547 | 412 | else if (state == 3) {// read 0xb5 0x62 0x06 |
ruschigo | 63:4ec1808fb547 | 413 | if (spi_2.write(0x00) == header[3]){ |
ruschigo | 63:4ec1808fb547 | 414 | state =4; |
ruschigo | 63:4ec1808fb547 | 415 | } |
ruschigo | 63:4ec1808fb547 | 416 | else state =0; |
ruschigo | 63:4ec1808fb547 | 417 | } |
ruschigo | 63:4ec1808fb547 | 418 | else if (state == 4) {// read 0xb5 0x62 0x06 0x013 |
ruschigo | 63:4ec1808fb547 | 419 | |
ruschigo | 63:4ec1808fb547 | 420 | uint8_t packet[100]; |
ruschigo | 63:4ec1808fb547 | 421 | uint8_t value = spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 422 | uint8_t packet_size; |
ruschigo | 63:4ec1808fb547 | 423 | |
ruschigo | 63:4ec1808fb547 | 424 | for (packet_size = 0 ; packet_size < 100 ;packet_size++) { |
ruschigo | 63:4ec1808fb547 | 425 | if (value != '\n' ){ |
ruschigo | 63:4ec1808fb547 | 426 | packet [packet_size]= value; |
ruschigo | 63:4ec1808fb547 | 427 | value = spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 428 | } |
ruschigo | 63:4ec1808fb547 | 429 | else { |
ruschigo | 63:4ec1808fb547 | 430 | lora_send_packet (packet , (uint8_t) packet_size+1); |
ruschigo | 63:4ec1808fb547 | 431 | //packet_size = MAXIMUM_PACKET_SIZE +1; |
ruschigo | 63:4ec1808fb547 | 432 | return; |
ruschigo | 63:4ec1808fb547 | 433 | } |
ruschigo | 63:4ec1808fb547 | 434 | } |
ruschigo | 63:4ec1808fb547 | 435 | |
ruschigo | 63:4ec1808fb547 | 436 | } |
ruschigo | 63:4ec1808fb547 | 437 | } |
ruschigo | 63:4ec1808fb547 | 438 | }*/ |
ruschigo | 63:4ec1808fb547 | 439 | |
ruschigo | 63:4ec1808fb547 | 440 | void wait_packet_byte (uint8_t *header, uint8_t byte) { |
ruschigo | 63:4ec1808fb547 | 441 | |
ruschigo | 63:4ec1808fb547 | 442 | char state = 0; |
ruschigo | 63:4ec1808fb547 | 443 | int cont =0; |
ruschigo | 63:4ec1808fb547 | 444 | |
ruschigo | 63:4ec1808fb547 | 445 | while (1){ //começou mensagem |
ruschigo | 63:4ec1808fb547 | 446 | if (cont > 250) { |
ruschigo | 63:4ec1808fb547 | 447 | //led2=!led2; |
ruschigo | 63:4ec1808fb547 | 448 | return; |
ruschigo | 63:4ec1808fb547 | 449 | } |
ruschigo | 63:4ec1808fb547 | 450 | if (state == 0 ){ |
ruschigo | 63:4ec1808fb547 | 451 | if (spi_2.write(0x00) == header[0]) |
ruschigo | 63:4ec1808fb547 | 452 | state =1; |
ruschigo | 63:4ec1808fb547 | 453 | else { |
ruschigo | 63:4ec1808fb547 | 454 | cont++; |
ruschigo | 63:4ec1808fb547 | 455 | wait_ms(10); |
ruschigo | 63:4ec1808fb547 | 456 | } |
ruschigo | 63:4ec1808fb547 | 457 | |
ruschigo | 63:4ec1808fb547 | 458 | |
ruschigo | 63:4ec1808fb547 | 459 | } |
ruschigo | 63:4ec1808fb547 | 460 | else if (state == 1){ // read 0xb5 |
ruschigo | 63:4ec1808fb547 | 461 | if (spi_2.write(0x00) == header[1]){ |
ruschigo | 63:4ec1808fb547 | 462 | state =2; |
ruschigo | 63:4ec1808fb547 | 463 | } |
ruschigo | 63:4ec1808fb547 | 464 | else state =0; |
ruschigo | 63:4ec1808fb547 | 465 | } |
ruschigo | 63:4ec1808fb547 | 466 | else if (state == 2) {// read 0xb5 0x62 |
ruschigo | 63:4ec1808fb547 | 467 | if (spi_2.write(0x00) == header[2]){ |
ruschigo | 63:4ec1808fb547 | 468 | state =3; |
ruschigo | 63:4ec1808fb547 | 469 | } |
ruschigo | 63:4ec1808fb547 | 470 | else state =0; |
ruschigo | 63:4ec1808fb547 | 471 | } |
ruschigo | 63:4ec1808fb547 | 472 | else if (state == 3) {// read 0xb5 0x62 0x06 |
ruschigo | 63:4ec1808fb547 | 473 | if (spi_2.write(0x00) == header[3]){ |
ruschigo | 63:4ec1808fb547 | 474 | state =4; |
ruschigo | 63:4ec1808fb547 | 475 | } |
ruschigo | 63:4ec1808fb547 | 476 | else state =0; |
ruschigo | 63:4ec1808fb547 | 477 | } |
ruschigo | 63:4ec1808fb547 | 478 | else if (state == 4) {// read 0xb5 0x62 0x06 0x013 |
ruschigo | 63:4ec1808fb547 | 479 | |
ruschigo | 63:4ec1808fb547 | 480 | if (byte == 0){//mostrar todo o pacote |
ruschigo | 63:4ec1808fb547 | 481 | uint8_t packet[100]; |
ruschigo | 63:4ec1808fb547 | 482 | uint8_t value = spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 483 | uint8_t packet_size; |
ruschigo | 63:4ec1808fb547 | 484 | |
ruschigo | 63:4ec1808fb547 | 485 | for (packet_size = 0 ; packet_size < 100 ;packet_size++) { |
ruschigo | 63:4ec1808fb547 | 486 | if (value != '\n' ){ |
ruschigo | 63:4ec1808fb547 | 487 | packet [packet_size]= value; |
ruschigo | 63:4ec1808fb547 | 488 | value = spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 489 | } |
ruschigo | 63:4ec1808fb547 | 490 | else { |
ruschigo | 63:4ec1808fb547 | 491 | //lora_send_packet (packet , (uint8_t) packet_size+1); |
ruschigo | 63:4ec1808fb547 | 492 | //packet_size = MAXIMUM_PACKET_SIZE +1; |
ruschigo | 63:4ec1808fb547 | 493 | return; |
ruschigo | 63:4ec1808fb547 | 494 | } |
ruschigo | 63:4ec1808fb547 | 495 | }//fim for |
ruschigo | 63:4ec1808fb547 | 496 | }//fim if byte |
ruschigo | 63:4ec1808fb547 | 497 | |
ruschigo | 63:4ec1808fb547 | 498 | else {//mostrar apenas um byte |
ruschigo | 63:4ec1808fb547 | 499 | |
ruschigo | 63:4ec1808fb547 | 500 | |
ruschigo | 63:4ec1808fb547 | 501 | for (int i = 0 ; i < byte-1 ;i++) |
ruschigo | 63:4ec1808fb547 | 502 | spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 503 | |
ruschigo | 63:4ec1808fb547 | 504 | uint8_t packet[]={(uint8_t)99,(uint8_t)spi_2.write(0x00)}; |
ruschigo | 63:4ec1808fb547 | 505 | //lora_send_packet (packet , (uint8_t) 2); |
ruschigo | 63:4ec1808fb547 | 506 | |
ruschigo | 63:4ec1808fb547 | 507 | |
ruschigo | 63:4ec1808fb547 | 508 | for (int i = 0 ; i < 100 ;i++) |
ruschigo | 63:4ec1808fb547 | 509 | if (spi_2.write(0x00) == '\n') |
ruschigo | 63:4ec1808fb547 | 510 | return; |
ruschigo | 63:4ec1808fb547 | 511 | |
ruschigo | 63:4ec1808fb547 | 512 | return; |
ruschigo | 63:4ec1808fb547 | 513 | |
ruschigo | 63:4ec1808fb547 | 514 | }//fim else byte |
ruschigo | 63:4ec1808fb547 | 515 | |
ruschigo | 63:4ec1808fb547 | 516 | } |
ruschigo | 63:4ec1808fb547 | 517 | |
ruschigo | 63:4ec1808fb547 | 518 | } |
ruschigo | 63:4ec1808fb547 | 519 | |
ruschigo | 63:4ec1808fb547 | 520 | } |
ruschigo | 63:4ec1808fb547 | 521 | |
ruschigo | 63:4ec1808fb547 | 522 | |
ruschigo | 63:4ec1808fb547 | 523 | void gps_wait_same_packet () { |
ruschigo | 63:4ec1808fb547 | 524 | |
ruschigo | 63:4ec1808fb547 | 525 | char state = 0; |
ruschigo | 63:4ec1808fb547 | 526 | int cont =0; |
ruschigo | 63:4ec1808fb547 | 527 | |
ruschigo | 63:4ec1808fb547 | 528 | while (1){ //começou mensagem |
ruschigo | 63:4ec1808fb547 | 529 | if (cont > 250) { |
ruschigo | 63:4ec1808fb547 | 530 | //led2=!led2; |
ruschigo | 63:4ec1808fb547 | 531 | return; |
ruschigo | 63:4ec1808fb547 | 532 | } |
ruschigo | 63:4ec1808fb547 | 533 | if (state == 0 ){ |
ruschigo | 63:4ec1808fb547 | 534 | if (spi_2.write(0x00) == 0xb5) |
ruschigo | 63:4ec1808fb547 | 535 | state =1; |
ruschigo | 63:4ec1808fb547 | 536 | else { |
ruschigo | 63:4ec1808fb547 | 537 | cont++; |
ruschigo | 63:4ec1808fb547 | 538 | wait_ms(10); |
ruschigo | 63:4ec1808fb547 | 539 | } |
ruschigo | 63:4ec1808fb547 | 540 | |
ruschigo | 63:4ec1808fb547 | 541 | |
ruschigo | 63:4ec1808fb547 | 542 | } |
ruschigo | 63:4ec1808fb547 | 543 | else if (state == 1){ // read 0xb5 |
ruschigo | 63:4ec1808fb547 | 544 | if (spi_2.write(0x00) == 0x62){ |
ruschigo | 63:4ec1808fb547 | 545 | state =2; |
ruschigo | 63:4ec1808fb547 | 546 | } |
ruschigo | 63:4ec1808fb547 | 547 | else state =0; |
ruschigo | 63:4ec1808fb547 | 548 | } |
ruschigo | 63:4ec1808fb547 | 549 | else if (state == 2){ |
ruschigo | 63:4ec1808fb547 | 550 | uint8_t packet[200]; |
ruschigo | 63:4ec1808fb547 | 551 | uint8_t value = spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 552 | uint8_t packet_size; |
ruschigo | 63:4ec1808fb547 | 553 | |
ruschigo | 63:4ec1808fb547 | 554 | for (packet_size = 0 ; packet_size < 200 ;packet_size++) { |
ruschigo | 63:4ec1808fb547 | 555 | if (value != '\n' ){ |
ruschigo | 63:4ec1808fb547 | 556 | packet [packet_size]= value; |
ruschigo | 63:4ec1808fb547 | 557 | value = spi_2.write(0x00); |
ruschigo | 63:4ec1808fb547 | 558 | } |
ruschigo | 63:4ec1808fb547 | 559 | else { |
ruschigo | 63:4ec1808fb547 | 560 | //lora_send_packet (packet , (uint8_t) packet_size+1); |
ruschigo | 63:4ec1808fb547 | 561 | //packet_size = MAXIMUM_PACKET_SIZE +1; |
ruschigo | 63:4ec1808fb547 | 562 | return; |
ruschigo | 63:4ec1808fb547 | 563 | } |
ruschigo | 63:4ec1808fb547 | 564 | |
ruschigo | 63:4ec1808fb547 | 565 | }//fim for |
ruschigo | 63:4ec1808fb547 | 566 | // lora_send_packet (packet , 200); |
ruschigo | 63:4ec1808fb547 | 567 | } |
ruschigo | 63:4ec1808fb547 | 568 | } |
ruschigo | 63:4ec1808fb547 | 569 | } |
ruschigo | 63:4ec1808fb547 | 570 | |
ruschigo | 63:4ec1808fb547 | 571 | void gps_config_gnss (){ |
ruschigo | 63:4ec1808fb547 | 572 | |
ruschigo | 63:4ec1808fb547 | 573 | uint8_t packet_cfg_gnss[] = { |
ruschigo | 63:4ec1808fb547 | 574 | 0xB5, 0x62, // Header |
ruschigo | 63:4ec1808fb547 | 575 | 0x03, 0x3E, // Class ID = CFG, Msg ID = UBX-CFG-GNSS |
ruschigo | 63:4ec1808fb547 | 576 | 0x3C, 0x00, // Payload Length = 60 bytes |
ruschigo | 63:4ec1808fb547 | 577 | 0x00, 0x00, 0x20, 0x07, // msgVer=0, numTrkChHw=32, numTrkChUse=32, numConfigBlocks=7 |
ruschigo | 63:4ec1808fb547 | 578 | 0x00, 0x08, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, // gnssId=0 (GPS), resTrkCh=8, maxTrkCh=12, ENABLE |
ruschigo | 63:4ec1808fb547 | 579 | 0x01, 0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0x00, // gnssId=1 (SBAS), resTrkCh=1, maxTrkCh=2, ENABLE |
ruschigo | 63:4ec1808fb547 | 580 | 0x02, 0x04, 0x0A, 0x00, 0x01, 0x00, 0x01, 0x00, // gnssId=2 (Galileo), resTrkCh=4, maxTrkCh=10, ENABLE |
ruschigo | 63:4ec1808fb547 | 581 | 0x03, 0x04, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, // gnssId=3 (BeiDou), resTrkCh=4, maxTrkCh=8, DISABLE |
ruschigo | 63:4ec1808fb547 | 582 | 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, // gnssId=4 (IMES), resTrkCh=0, maxTrkCh=8, DISABLE |
ruschigo | 63:4ec1808fb547 | 583 | 0x05, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, // gnssId=5 (QZSS), resTrkCh=0, maxTrkCh=3, DISABLE |
ruschigo | 63:4ec1808fb547 | 584 | 0x06, 0x04, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00, // gnssId=6 (GLONASS), resTrkCh=4, maxTrkCh=8, ENABLE |
ruschigo | 63:4ec1808fb547 | 585 | 0x00, 0x00 //checksums A & B |
ruschigo | 63:4ec1808fb547 | 586 | }; |
ruschigo | 63:4ec1808fb547 | 587 | uint8_t pos_ck = sizeof(packet_cfg_gnss)-2; |
ruschigo | 63:4ec1808fb547 | 588 | printf("gnss config finish"); |
ruschigo | 63:4ec1808fb547 | 589 | uint8_t ubxi; |
ruschigo | 63:4ec1808fb547 | 590 | //calcula checksum |
ruschigo | 63:4ec1808fb547 | 591 | for (ubxi=2; ubxi<pos_ck ; ubxi++) { |
ruschigo | 63:4ec1808fb547 | 592 | packet_cfg_gnss[pos_ck] = packet_cfg_gnss[pos_ck] + packet_cfg_gnss[ubxi]; |
ruschigo | 63:4ec1808fb547 | 593 | packet_cfg_gnss[pos_ck+1] = packet_cfg_gnss[pos_ck+1] + packet_cfg_gnss[pos_ck]; |
ruschigo | 63:4ec1808fb547 | 594 | } |
ruschigo | 63:4ec1808fb547 | 595 | |
ruschigo | 63:4ec1808fb547 | 596 | } |
ruschigo | 63:4ec1808fb547 | 597 | |
ruschigo | 63:4ec1808fb547 | 598 | |
ruschigo | 63:4ec1808fb547 | 599 | void gps_print_local (){ |
ruschigo | 63:4ec1808fb547 | 600 | uint8_t packet_nav_pvt[] = { 0xB5, 0x62, 0x01, 0x07, 0x00, 0x00, 0x08, 0x19}; |
ruschigo | 63:4ec1808fb547 | 601 | |
ruschigo | 63:4ec1808fb547 | 602 | //=============envia pacote nav pvt |
ruschigo | 63:4ec1808fb547 | 603 | for ( int i=0; i< sizeof(packet_nav_pvt) ; i++){ |
ruschigo | 63:4ec1808fb547 | 604 | spi_2.write(packet_nav_pvt[i]); |
ruschigo | 63:4ec1808fb547 | 605 | wait_ms(20); |
ruschigo | 63:4ec1808fb547 | 606 | } |
ruschigo | 63:4ec1808fb547 | 607 | |
ruschigo | 63:4ec1808fb547 | 608 | gps_navPVT Data = le_nav_pvt(); |
ruschigo | 63:4ec1808fb547 | 609 | |
ruschigo | 63:4ec1808fb547 | 610 | //printf ("gps lat=%d lon=%d \n",Data.lat ,Data.lon ); |
ruschigo | 63:4ec1808fb547 | 611 | //lat=Data.lat; |
ruschigo | 63:4ec1808fb547 | 612 | //lon=Data.lon; |
ruschigo | 63:4ec1808fb547 | 613 | } |
ruschigo | 63:4ec1808fb547 | 614 | |
ruschigo | 63:4ec1808fb547 | 615 | void gps_get_lat_lon(int *latitude, int *logitude){ |
ruschigo | 63:4ec1808fb547 | 616 | uint8_t packet_nav_pvt[] = { 0xB5, 0x62, 0x01, 0x07, 0x00, 0x00, 0x08, 0x19}; |
ruschigo | 63:4ec1808fb547 | 617 | |
ruschigo | 63:4ec1808fb547 | 618 | //=============envia pacote nav pvt |
ruschigo | 63:4ec1808fb547 | 619 | for ( int i=0; i< sizeof(packet_nav_pvt) ; i++){ |
ruschigo | 63:4ec1808fb547 | 620 | spi_2.write(packet_nav_pvt[i]); |
ruschigo | 63:4ec1808fb547 | 621 | wait_ms(20); |
ruschigo | 63:4ec1808fb547 | 622 | } |
ruschigo | 63:4ec1808fb547 | 623 | |
ruschigo | 63:4ec1808fb547 | 624 | gps_navPVT Data = le_nav_pvt(); |
ruschigo | 63:4ec1808fb547 | 625 | |
ruschigo | 63:4ec1808fb547 | 626 | //printf ("gps lat=%d lon=%d \n",Data.lat ,Data.lon ); |
ruschigo | 63:4ec1808fb547 | 627 | //latitude = &(int)Data.lat; |
ruschigo | 63:4ec1808fb547 | 628 | //longitude = &(int)Data.lon; |
ruschigo | 63:4ec1808fb547 | 629 | return; |
ruschigo | 63:4ec1808fb547 | 630 | } |
ruschigo | 63:4ec1808fb547 | 631 | |
ruschigo | 63:4ec1808fb547 | 632 | void gps_config (){ |
ruschigo | 63:4ec1808fb547 | 633 | gps_DSEL = 0; |
ruschigo | 63:4ec1808fb547 | 634 | //spi gps configuration |
ruschigo | 63:4ec1808fb547 | 635 | spi_2.format(8,0); |
ruschigo | 63:4ec1808fb547 | 636 | spi_2.frequency(1000000); //1MHz |
ruschigo | 63:4ec1808fb547 | 637 | cs=1; |
ruschigo | 63:4ec1808fb547 | 638 | wait(0.1); |
ruschigo | 63:4ec1808fb547 | 639 | cs=0; |
ruschigo | 63:4ec1808fb547 | 640 | wait(0.1); |
ruschigo | 63:4ec1808fb547 | 641 | |
ruschigo | 63:4ec1808fb547 | 642 | gps_int = 0; |
ruschigo | 63:4ec1808fb547 | 643 | |
ruschigo | 63:4ec1808fb547 | 644 | //gps reset |
ruschigo | 63:4ec1808fb547 | 645 | gps_reset = 1; |
ruschigo | 63:4ec1808fb547 | 646 | wait_ms(50); |
ruschigo | 63:4ec1808fb547 | 647 | //gps_reset = 0; |
ruschigo | 63:4ec1808fb547 | 648 | //wait(1.5); |
ruschigo | 63:4ec1808fb547 | 649 | } |
ruschigo | 63:4ec1808fb547 | 650 | |
ruschigo | 63:4ec1808fb547 | 651 | int get_latitude(){ |
ruschigo | 63:4ec1808fb547 | 652 | return lat; |
ruschigo | 63:4ec1808fb547 | 653 | } |
ruschigo | 63:4ec1808fb547 | 654 | |
ruschigo | 63:4ec1808fb547 | 655 | int get_longitude(){ |
ruschigo | 63:4ec1808fb547 | 656 | return lon; |
ruschigo | 63:4ec1808fb547 | 657 | } |