Teste Flash

Dependencies:   pulga-lorawan-drv Si1133 BME280

Committer:
pancotinho
Date:
Tue Jun 01 20:19:52 2021 +0000
Revision:
69:2d56b571c78e
Parent:
68:fc357095c8ef
Child:
70:99b7a15c09da
updated gps and lorawan for pulga stack

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:7037ed05f54f 1 /**
mbed_official 0:7037ed05f54f 2 * Copyright (c) 2017, Arm Limited and affiliates.
mbed_official 0:7037ed05f54f 3 * SPDX-License-Identifier: Apache-2.0
mbed_official 0:7037ed05f54f 4 *
mbed_official 0:7037ed05f54f 5 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 0:7037ed05f54f 6 * you may not use this file except in compliance with the License.
mbed_official 0:7037ed05f54f 7 * You may obtain a copy of the License at
mbed_official 0:7037ed05f54f 8 *
mbed_official 0:7037ed05f54f 9 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:7037ed05f54f 10 *
mbed_official 0:7037ed05f54f 11 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:7037ed05f54f 12 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 0:7037ed05f54f 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:7037ed05f54f 14 * See the License for the specific language governing permissions and
mbed_official 0:7037ed05f54f 15 * limitations under the License.
mbed_official 0:7037ed05f54f 16 */
mbed_official 0:7037ed05f54f 17 #include <stdio.h>
ruschigo 62:89df9529dbb0 18 #include "mbed.h"
mbed_official 3:8c7198d1a2a1 19
pancotinho 66:f40f985a6b97 20 // Application helpers
mbed_official 0:7037ed05f54f 21
mbed_official 0:7037ed05f54f 22 #include "trace_helper.h"
pancotinho 66:f40f985a6b97 23
pancotinho 66:f40f985a6b97 24 // Application peripherals
mbed_official 0:7037ed05f54f 25
ruschigo 62:89df9529dbb0 26 #include "serial.h"
ruschigo 63:4ec1808fb547 27 #include "gps.h"
pancotinho 69:2d56b571c78e 28 #include "lora_radio.h"
ruschigo 62:89df9529dbb0 29
ruschigo 64:ed68ddac6360 30
mbed_official 0:7037ed05f54f 31 using namespace events;
mbed_official 0:7037ed05f54f 32
ruschigo 62:89df9529dbb0 33
mbed_official 0:7037ed05f54f 34 /*
mbed_official 0:7037ed05f54f 35 * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
mbed_official 0:7037ed05f54f 36 */
pancotinho 69:2d56b571c78e 37 #define TX_TIMER 60000
mbed_official 0:7037ed05f54f 38
mbed_official 0:7037ed05f54f 39 /**
mbed_official 0:7037ed05f54f 40 * Maximum number of events for the event queue.
mbed_official 12:5015dfead3f2 41 * 10 is the safe number for the stack events, however, if application
mbed_official 0:7037ed05f54f 42 * also uses the queue for whatever purposes, this number should be increased.
mbed_official 0:7037ed05f54f 43 */
mbed_official 12:5015dfead3f2 44 #define MAX_NUMBER_OF_EVENTS 10
mbed_official 0:7037ed05f54f 45
mbed_official 0:7037ed05f54f 46 /**
mbed_official 0:7037ed05f54f 47 * Maximum number of retries for CONFIRMED messages before giving up
mbed_official 0:7037ed05f54f 48 */
mbed_official 0:7037ed05f54f 49 #define CONFIRMED_MSG_RETRY_COUNTER 3
mbed_official 0:7037ed05f54f 50
mbed_official 0:7037ed05f54f 51
ruschigo 62:89df9529dbb0 52 void serial_post_to_queue(void);
ruschigo 62:89df9529dbb0 53
mbed_official 0:7037ed05f54f 54 /**
mbed_official 0:7037ed05f54f 55 * This event queue is the global event queue for both the
mbed_official 0:7037ed05f54f 56 * application and stack. To conserve memory, the stack is designed to run
mbed_official 0:7037ed05f54f 57 * in the same thread as the application and the application is responsible for
mbed_official 0:7037ed05f54f 58 * providing an event queue to the stack that will be used for ISR deferment as
mbed_official 0:7037ed05f54f 59 * well as application information event queuing.
mbed_official 0:7037ed05f54f 60 */
mbed_official 46:a124538e2891 61 static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);
mbed_official 0:7037ed05f54f 62
mbed_official 0:7037ed05f54f 63 /**
mbed_official 0:7037ed05f54f 64 * Application specific callbacks
mbed_official 0:7037ed05f54f 65 */
mbed_official 0:7037ed05f54f 66 static lorawan_app_callbacks_t callbacks;
mbed_official 0:7037ed05f54f 67
mbed_official 0:7037ed05f54f 68 /**
mbed_official 0:7037ed05f54f 69 * Entry point for application
mbed_official 0:7037ed05f54f 70 */
pancotinho 59:a4fc1efb1569 71
ruschigo 65:4090220e19d2 72 mbed::DigitalOut _alive_led(P1_13, 0);
ruschigo 65:4090220e19d2 73 mbed::DigitalOut _actuated_led(P1_14,1);
brunnobbco 61:65744bc8ab55 74 int latitude=0;
brunnobbco 61:65744bc8ab55 75 int longitude=0;
brunnobbco 61:65744bc8ab55 76
brunnobbco 61:65744bc8ab55 77
brunnobbco 61:65744bc8ab55 78 void GPS_Read(void)
brunnobbco 61:65744bc8ab55 79 {
pancotinho 69:2d56b571c78e 80 uint8_t tx_buffer[256];
pancotinho 69:2d56b571c78e 81 uint16_t packet_len;
pancotinho 69:2d56b571c78e 82 int16_t retcode;
pancotinho 69:2d56b571c78e 83
brunnobbco 61:65744bc8ab55 84 gps_print_local();
pancotinho 69:2d56b571c78e 85
pancotinho 69:2d56b571c78e 86 packet_len = sprintf((char *) tx_buffer, "%d, %d\n", get_latitude(), get_longitude());
pancotinho 69:2d56b571c78e 87 retcode = lora_send_message(tx_buffer, packet_len);
pancotinho 69:2d56b571c78e 88
ruschigo 63:4ec1808fb547 89 }
ruschigo 64:ed68ddac6360 90
ruschigo 62:89df9529dbb0 91 void serial_rx(){
ruschigo 62:89df9529dbb0 92 if(pc.readable()){
ruschigo 62:89df9529dbb0 93 pc.printf("rx: %c\n", pc.getc());
ruschigo 62:89df9529dbb0 94 }
ruschigo 62:89df9529dbb0 95 pc.attach(&serial_post_to_queue, RawSerial::RxIrq);
ruschigo 62:89df9529dbb0 96 return;
ruschigo 62:89df9529dbb0 97 }
ruschigo 62:89df9529dbb0 98
ruschigo 62:89df9529dbb0 99 void serial_post_to_queue(void){
ruschigo 62:89df9529dbb0 100 //disable serial rx interrupt
ruschigo 62:89df9529dbb0 101 pc.attach(NULL, RawSerial::RxIrq);
ruschigo 62:89df9529dbb0 102 //enqueue the serial rx reception as a normal task
ruschigo 62:89df9529dbb0 103 ev_queue.call(SerialRx);
ruschigo 62:89df9529dbb0 104 return;
ruschigo 62:89df9529dbb0 105 }
pancotinho 59:a4fc1efb1569 106
mbed_official 46:a124538e2891 107 int main(void)
mbed_official 0:7037ed05f54f 108 {
ruschigo 62:89df9529dbb0 109 pc.printf("init\n");
ruschigo 62:89df9529dbb0 110 pc.baud(9600);
ruschigo 62:89df9529dbb0 111 pc.printf("config9600\n");
ruschigo 62:89df9529dbb0 112 //enable serial rx interrupt
ruschigo 62:89df9529dbb0 113 pc.attach(&serial_post_to_queue, RawSerial::RxIrq);
brunnobbco 61:65744bc8ab55 114 gps_config();
brunnobbco 61:65744bc8ab55 115 gps_leBootMsg();
brunnobbco 61:65744bc8ab55 116 gps_config_gnss ();
brunnobbco 61:65744bc8ab55 117
brunnobbco 61:65744bc8ab55 118 wait_ms(250);
pancotinho 66:f40f985a6b97 119
brunnobbco 61:65744bc8ab55 120 //########################################################
mbed_official 0:7037ed05f54f 121 // setup tracing
mbed_official 0:7037ed05f54f 122 setup_trace();
mbed_official 0:7037ed05f54f 123
mbed_official 0:7037ed05f54f 124 // stores the status of a call to LoRaWAN protocol
mbed_official 0:7037ed05f54f 125 lorawan_status_t retcode;
mbed_official 0:7037ed05f54f 126
mbed_official 0:7037ed05f54f 127 // Initialize LoRaWAN stack
ruschigo 64:ed68ddac6360 128 /*if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
ruschigo 62:89df9529dbb0 129 pc.printf("\r\n LoRa initialization failed! \r\n");
mbed_official 0:7037ed05f54f 130 return -1;
ruschigo 64:ed68ddac6360 131 }*/
ruschigo 64:ed68ddac6360 132
ruschigo 64:ed68ddac6360 133 if(lorawan_initialize_stack(&ev_queue) != 0){
ruschigo 64:ed68ddac6360 134 return -1;
mbed_official 0:7037ed05f54f 135 }
mbed_official 0:7037ed05f54f 136
ruschigo 62:89df9529dbb0 137 pc.printf("\r\n Mbed LoRaWANStack initialized \r\n");
mbed_official 0:7037ed05f54f 138
mbed_official 0:7037ed05f54f 139 // prepare application callbacks
ruschigo 64:ed68ddac6360 140 /*callbacks.events = mbed::callback(lora_event_handler);
mbed_official 2:dc95ac6d6d4e 141 lorawan.add_app_callbacks(&callbacks);
ruschigo 64:ed68ddac6360 142 */
ruschigo 64:ed68ddac6360 143
ruschigo 64:ed68ddac6360 144 callbacks.events = mbed::callback(lora_event_handler);
ruschigo 64:ed68ddac6360 145 lorawan_add_callbacks(callbacks);
ruschigo 64:ed68ddac6360 146
pancotinho 66:f40f985a6b97 147 // Set number of retries in case of CONFIRMED message
ruschigo 64:ed68ddac6360 148 if(lorawan_set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) != 0){
ruschigo 64:ed68ddac6360 149 pc.printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
ruschigo 64:ed68ddac6360 150 return -1;
mbed_official 0:7037ed05f54f 151 }
ruschigo 64:ed68ddac6360 152
mbed_official 0:7037ed05f54f 153
ruschigo 62:89df9529dbb0 154 pc.printf("\r\n CONFIRMED message retries : %d \r\n",
mbed_official 0:7037ed05f54f 155 CONFIRMED_MSG_RETRY_COUNTER);
pancotinho 66:f40f985a6b97 156
mbed_official 0:7037ed05f54f 157 // Enable adaptive data rate
ruschigo 64:ed68ddac6360 158 if(lorawan_enable_adaptive_datarate() != 0){
ruschigo 64:ed68ddac6360 159 pc.printf("\r\n enable_adaptive_datarate failed! \r\n");
ruschigo 64:ed68ddac6360 160 return -1;
ruschigo 64:ed68ddac6360 161 }
ruschigo 64:ed68ddac6360 162
mbed_official 0:7037ed05f54f 163
ruschigo 62:89df9529dbb0 164 pc.printf("\r\n Adaptive data rate (ADR) - Enabled \r\n");
mbed_official 0:7037ed05f54f 165
ruschigo 64:ed68ddac6360 166
ruschigo 64:ed68ddac6360 167 if(lorawan_connect() != 0){
ruschigo 64:ed68ddac6360 168 pc.printf("\r\n Connection error, code = %d \r\n", retcode);
ruschigo 64:ed68ddac6360 169 return -1;
ruschigo 64:ed68ddac6360 170 }
ruschigo 64:ed68ddac6360 171
ruschigo 62:89df9529dbb0 172 pc.printf("\r\n Connection - In Progress ...\r\n");
pancotinho 59:a4fc1efb1569 173
ruschigo 65:4090220e19d2 174 _actuated_led =0;
ruschigo 65:4090220e19d2 175
ruschigo 64:ed68ddac6360 176 //
mbed_official 0:7037ed05f54f 177 // make your event queue dispatching events forever
pancotinho 69:2d56b571c78e 178 ev_queue.call_every(TX_TIMER, GPS_Read);
mbed_official 0:7037ed05f54f 179 ev_queue.dispatch_forever();
mbed_official 3:8c7198d1a2a1 180 return 0;
mbed_official 0:7037ed05f54f 181 }
mbed_official 0:7037ed05f54f 182
mbed_official 0:7037ed05f54f 183
mbed_official 0:7037ed05f54f 184 /**
mbed_official 0:7037ed05f54f 185 * Event handler
mbed_official 0:7037ed05f54f 186 */
ruschigo 64:ed68ddac6360 187 void lora_event_handler(lorawan_event_t event)
mbed_official 0:7037ed05f54f 188 {
mbed_official 0:7037ed05f54f 189 switch (event) {
mbed_official 0:7037ed05f54f 190 case CONNECTED:
ruschigo 62:89df9529dbb0 191 pc.printf("\r\n Connection - Successful \r\n");
mbed_official 0:7037ed05f54f 192 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
ruschigo 64:ed68ddac6360 193 //send_message();
ruschigo 65:4090220e19d2 194 //lora_send_message((uint8_t*)"testeLora", (uint16_t)10);
ruschigo 64:ed68ddac6360 195 } //else {
ruschigo 64:ed68ddac6360 196 //ev_queue.call_every(TX_TIMER, (void)lora_send_message((uint8_t*)"testeLoraEvery", (uint16_t)15));
ruschigo 64:ed68ddac6360 197 //}
mbed_official 0:7037ed05f54f 198
mbed_official 0:7037ed05f54f 199 break;
mbed_official 0:7037ed05f54f 200 case DISCONNECTED:
mbed_official 0:7037ed05f54f 201 ev_queue.break_dispatch();
ruschigo 62:89df9529dbb0 202 pc.printf("\r\n Disconnected Successfully \r\n");
mbed_official 0:7037ed05f54f 203 break;
mbed_official 0:7037ed05f54f 204 case TX_DONE:
brunnobbco 61:65744bc8ab55 205 // printf("\r\n Message Sent to Network Server \r\n");
mbed_official 0:7037ed05f54f 206 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
ruschigo 65:4090220e19d2 207 //lora_send_message((uint8_t*)"teste", (uint16_t)6);
mbed_official 0:7037ed05f54f 208 }
mbed_official 0:7037ed05f54f 209 break;
mbed_official 0:7037ed05f54f 210 case TX_TIMEOUT:
brunnobbco 61:65744bc8ab55 211 // printf("\r\n Transmission Error TX_Timeout");
ruschigo 62:89df9529dbb0 212 break;
mbed_official 0:7037ed05f54f 213 case TX_ERROR:
brunnobbco 61:65744bc8ab55 214 // printf("\r\n Transmission Error TX_Error");
ruschigo 62:89df9529dbb0 215 break;
mbed_official 0:7037ed05f54f 216 case TX_CRYPTO_ERROR:
brunnobbco 61:65744bc8ab55 217 // printf("\r\n Transmission Error TX_Crypto_Error");
ruschigo 62:89df9529dbb0 218 break;
mbed_official 0:7037ed05f54f 219 case TX_SCHEDULING_ERROR:
brunnobbco 61:65744bc8ab55 220 // printf("\r\n Transmission Error - EventCode = %d \r\n", event);
mbed_official 0:7037ed05f54f 221 // try again
mbed_official 0:7037ed05f54f 222 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
ruschigo 65:4090220e19d2 223 //lora_send_message((uint8_t*)"teste2", (uint16_t)7);
mbed_official 0:7037ed05f54f 224 }
mbed_official 0:7037ed05f54f 225 break;
mbed_official 0:7037ed05f54f 226 case RX_DONE:
brunnobbco 61:65744bc8ab55 227 // printf("\r\n Received message from Network Server \r\n");
ruschigo 64:ed68ddac6360 228 lora_receive_message();
mbed_official 0:7037ed05f54f 229 break;
mbed_official 0:7037ed05f54f 230 case RX_TIMEOUT:
brunnobbco 61:65744bc8ab55 231 // printf("\r\n Transmission Error RX_Timeout");
ruschigo 62:89df9529dbb0 232 break;
mbed_official 0:7037ed05f54f 233 case RX_ERROR:
brunnobbco 61:65744bc8ab55 234 // printf("\r\n Error in reception - Code = %d \r\n", event);
mbed_official 0:7037ed05f54f 235 break;
mbed_official 0:7037ed05f54f 236 case JOIN_FAILURE:
brunnobbco 61:65744bc8ab55 237 // printf("\r\n OTAA Failed - Check Keys \r\n");
mbed_official 0:7037ed05f54f 238 break;
mbed_official 26:f07f5febf97f 239 case UPLINK_REQUIRED:
brunnobbco 61:65744bc8ab55 240 // printf("\r\n Uplink required by NS \r\n");
mbed_official 26:f07f5febf97f 241 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
ruschigo 65:4090220e19d2 242 //lora_send_message((uint8_t*)"uplink", (uint16_t)7);
mbed_official 26:f07f5febf97f 243 }
mbed_official 26:f07f5febf97f 244 break;
mbed_official 0:7037ed05f54f 245 default:
mbed_official 0:7037ed05f54f 246 MBED_ASSERT("Unknown Event");
ruschigo 62:89df9529dbb0 247 break;
mbed_official 0:7037ed05f54f 248 }
mbed_official 0:7037ed05f54f 249 }
mbed_official 0:7037ed05f54f 250
mbed_official 0:7037ed05f54f 251 // EOF