Teste Flash
Dependencies: pulga-lorawan-drv Si1133 BME280
main.cpp@70:99b7a15c09da, 2021-09-13 (annotated)
- Committer:
- MatteusCarr
- Date:
- Mon Sep 13 18:55:32 2021 +0000
- Revision:
- 70:99b7a15c09da
- Parent:
- 69:2d56b571c78e
Teste Flash
Who changed what in which revision?
User | Revision | Line number | New 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" |
MatteusCarr | 70:99b7a15c09da | 29 | #include "SPI_MX25R.h" |
ruschigo | 62:89df9529dbb0 | 30 | |
ruschigo | 64:ed68ddac6360 | 31 | |
mbed_official | 0:7037ed05f54f | 32 | using namespace events; |
mbed_official | 0:7037ed05f54f | 33 | |
ruschigo | 62:89df9529dbb0 | 34 | |
mbed_official | 0:7037ed05f54f | 35 | /* |
mbed_official | 0:7037ed05f54f | 36 | * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing |
mbed_official | 0:7037ed05f54f | 37 | */ |
pancotinho | 69:2d56b571c78e | 38 | #define TX_TIMER 60000 |
mbed_official | 0:7037ed05f54f | 39 | |
mbed_official | 0:7037ed05f54f | 40 | /** |
mbed_official | 0:7037ed05f54f | 41 | * Maximum number of events for the event queue. |
mbed_official | 12:5015dfead3f2 | 42 | * 10 is the safe number for the stack events, however, if application |
mbed_official | 0:7037ed05f54f | 43 | * also uses the queue for whatever purposes, this number should be increased. |
mbed_official | 0:7037ed05f54f | 44 | */ |
mbed_official | 12:5015dfead3f2 | 45 | #define MAX_NUMBER_OF_EVENTS 10 |
mbed_official | 0:7037ed05f54f | 46 | |
mbed_official | 0:7037ed05f54f | 47 | /** |
mbed_official | 0:7037ed05f54f | 48 | * Maximum number of retries for CONFIRMED messages before giving up |
mbed_official | 0:7037ed05f54f | 49 | */ |
mbed_official | 0:7037ed05f54f | 50 | #define CONFIRMED_MSG_RETRY_COUNTER 3 |
mbed_official | 0:7037ed05f54f | 51 | |
mbed_official | 0:7037ed05f54f | 52 | |
ruschigo | 62:89df9529dbb0 | 53 | void serial_post_to_queue(void); |
ruschigo | 62:89df9529dbb0 | 54 | |
mbed_official | 0:7037ed05f54f | 55 | /** |
mbed_official | 0:7037ed05f54f | 56 | * This event queue is the global event queue for both the |
mbed_official | 0:7037ed05f54f | 57 | * application and stack. To conserve memory, the stack is designed to run |
mbed_official | 0:7037ed05f54f | 58 | * in the same thread as the application and the application is responsible for |
mbed_official | 0:7037ed05f54f | 59 | * providing an event queue to the stack that will be used for ISR deferment as |
mbed_official | 0:7037ed05f54f | 60 | * well as application information event queuing. |
mbed_official | 0:7037ed05f54f | 61 | */ |
mbed_official | 46:a124538e2891 | 62 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); |
mbed_official | 0:7037ed05f54f | 63 | |
mbed_official | 0:7037ed05f54f | 64 | /** |
mbed_official | 0:7037ed05f54f | 65 | * Application specific callbacks |
mbed_official | 0:7037ed05f54f | 66 | */ |
mbed_official | 0:7037ed05f54f | 67 | static lorawan_app_callbacks_t callbacks; |
mbed_official | 0:7037ed05f54f | 68 | |
mbed_official | 0:7037ed05f54f | 69 | /** |
mbed_official | 0:7037ed05f54f | 70 | * Entry point for application |
mbed_official | 0:7037ed05f54f | 71 | */ |
pancotinho | 59:a4fc1efb1569 | 72 | |
ruschigo | 65:4090220e19d2 | 73 | mbed::DigitalOut _alive_led(P1_13, 0); |
ruschigo | 65:4090220e19d2 | 74 | mbed::DigitalOut _actuated_led(P1_14,1); |
brunnobbco | 61:65744bc8ab55 | 75 | int latitude=0; |
brunnobbco | 61:65744bc8ab55 | 76 | int longitude=0; |
brunnobbco | 61:65744bc8ab55 | 77 | |
brunnobbco | 61:65744bc8ab55 | 78 | |
brunnobbco | 61:65744bc8ab55 | 79 | void GPS_Read(void) |
brunnobbco | 61:65744bc8ab55 | 80 | { |
pancotinho | 69:2d56b571c78e | 81 | uint8_t tx_buffer[256]; |
pancotinho | 69:2d56b571c78e | 82 | uint16_t packet_len; |
pancotinho | 69:2d56b571c78e | 83 | int16_t retcode; |
pancotinho | 69:2d56b571c78e | 84 | |
brunnobbco | 61:65744bc8ab55 | 85 | gps_print_local(); |
pancotinho | 69:2d56b571c78e | 86 | |
pancotinho | 69:2d56b571c78e | 87 | packet_len = sprintf((char *) tx_buffer, "%d, %d\n", get_latitude(), get_longitude()); |
pancotinho | 69:2d56b571c78e | 88 | retcode = lora_send_message(tx_buffer, packet_len); |
pancotinho | 69:2d56b571c78e | 89 | |
ruschigo | 63:4ec1808fb547 | 90 | } |
ruschigo | 64:ed68ddac6360 | 91 | |
ruschigo | 62:89df9529dbb0 | 92 | void serial_rx(){ |
ruschigo | 62:89df9529dbb0 | 93 | if(pc.readable()){ |
ruschigo | 62:89df9529dbb0 | 94 | pc.printf("rx: %c\n", pc.getc()); |
ruschigo | 62:89df9529dbb0 | 95 | } |
ruschigo | 62:89df9529dbb0 | 96 | pc.attach(&serial_post_to_queue, RawSerial::RxIrq); |
ruschigo | 62:89df9529dbb0 | 97 | return; |
ruschigo | 62:89df9529dbb0 | 98 | } |
ruschigo | 62:89df9529dbb0 | 99 | |
ruschigo | 62:89df9529dbb0 | 100 | void serial_post_to_queue(void){ |
ruschigo | 62:89df9529dbb0 | 101 | //disable serial rx interrupt |
ruschigo | 62:89df9529dbb0 | 102 | pc.attach(NULL, RawSerial::RxIrq); |
ruschigo | 62:89df9529dbb0 | 103 | //enqueue the serial rx reception as a normal task |
ruschigo | 62:89df9529dbb0 | 104 | ev_queue.call(SerialRx); |
ruschigo | 62:89df9529dbb0 | 105 | return; |
ruschigo | 62:89df9529dbb0 | 106 | } |
pancotinho | 59:a4fc1efb1569 | 107 | |
mbed_official | 46:a124538e2891 | 108 | int main(void) |
mbed_official | 0:7037ed05f54f | 109 | { |
ruschigo | 62:89df9529dbb0 | 110 | pc.printf("init\n"); |
ruschigo | 62:89df9529dbb0 | 111 | pc.baud(9600); |
ruschigo | 62:89df9529dbb0 | 112 | pc.printf("config9600\n"); |
ruschigo | 62:89df9529dbb0 | 113 | //enable serial rx interrupt |
brunnobbco | 61:65744bc8ab55 | 114 | |
brunnobbco | 61:65744bc8ab55 | 115 | wait_ms(250); |
pancotinho | 59:a4fc1efb1569 | 116 | |
ruschigo | 65:4090220e19d2 | 117 | _actuated_led =0; |
ruschigo | 65:4090220e19d2 | 118 | |
MatteusCarr | 70:99b7a15c09da | 119 | uint8_t data[2]; |
MatteusCarr | 70:99b7a15c09da | 120 | data[0]= 0xFF; //byte qualquer |
MatteusCarr | 70:99b7a15c09da | 121 | data[1] = 0xFF; //byte qualquer |
MatteusCarr | 70:99b7a15c09da | 122 | |
MatteusCarr | 70:99b7a15c09da | 123 | SPI_MX25R *flash = new SPI_MX25R(P0_20, P0_17, P0_22, P0_24); |
MatteusCarr | 70:99b7a15c09da | 124 | |
MatteusCarr | 70:99b7a15c09da | 125 | flash->writeEnable(); |
MatteusCarr | 70:99b7a15c09da | 126 | flash->programPage(0x191F50, data, 2); |
MatteusCarr | 70:99b7a15c09da | 127 | |
MatteusCarr | 70:99b7a15c09da | 128 | pc.printf("o numero foi: %d\n\r", flash->read8(0x191F50)); |
MatteusCarr | 70:99b7a15c09da | 129 | pc.printf("o numero foi: %d\n\r", flash->read8(0x191F51)); |
MatteusCarr | 70:99b7a15c09da | 130 | |
mbed_official | 3:8c7198d1a2a1 | 131 | return 0; |
mbed_official | 0:7037ed05f54f | 132 | } |
mbed_official | 0:7037ed05f54f | 133 | |
mbed_official | 0:7037ed05f54f | 134 | |
mbed_official | 0:7037ed05f54f | 135 | /** |
mbed_official | 0:7037ed05f54f | 136 | * Event handler |
mbed_official | 0:7037ed05f54f | 137 | */ |
ruschigo | 64:ed68ddac6360 | 138 | void lora_event_handler(lorawan_event_t event) |
mbed_official | 0:7037ed05f54f | 139 | { |
mbed_official | 0:7037ed05f54f | 140 | switch (event) { |
mbed_official | 0:7037ed05f54f | 141 | case CONNECTED: |
ruschigo | 62:89df9529dbb0 | 142 | pc.printf("\r\n Connection - Successful \r\n"); |
mbed_official | 0:7037ed05f54f | 143 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
ruschigo | 64:ed68ddac6360 | 144 | //send_message(); |
ruschigo | 65:4090220e19d2 | 145 | //lora_send_message((uint8_t*)"testeLora", (uint16_t)10); |
ruschigo | 64:ed68ddac6360 | 146 | } //else { |
ruschigo | 64:ed68ddac6360 | 147 | //ev_queue.call_every(TX_TIMER, (void)lora_send_message((uint8_t*)"testeLoraEvery", (uint16_t)15)); |
ruschigo | 64:ed68ddac6360 | 148 | //} |
mbed_official | 0:7037ed05f54f | 149 | |
mbed_official | 0:7037ed05f54f | 150 | break; |
mbed_official | 0:7037ed05f54f | 151 | case DISCONNECTED: |
mbed_official | 0:7037ed05f54f | 152 | ev_queue.break_dispatch(); |
ruschigo | 62:89df9529dbb0 | 153 | pc.printf("\r\n Disconnected Successfully \r\n"); |
mbed_official | 0:7037ed05f54f | 154 | break; |
mbed_official | 0:7037ed05f54f | 155 | case TX_DONE: |
brunnobbco | 61:65744bc8ab55 | 156 | // printf("\r\n Message Sent to Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 157 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
ruschigo | 65:4090220e19d2 | 158 | //lora_send_message((uint8_t*)"teste", (uint16_t)6); |
mbed_official | 0:7037ed05f54f | 159 | } |
mbed_official | 0:7037ed05f54f | 160 | break; |
mbed_official | 0:7037ed05f54f | 161 | case TX_TIMEOUT: |
brunnobbco | 61:65744bc8ab55 | 162 | // printf("\r\n Transmission Error TX_Timeout"); |
ruschigo | 62:89df9529dbb0 | 163 | break; |
mbed_official | 0:7037ed05f54f | 164 | case TX_ERROR: |
brunnobbco | 61:65744bc8ab55 | 165 | // printf("\r\n Transmission Error TX_Error"); |
ruschigo | 62:89df9529dbb0 | 166 | break; |
mbed_official | 0:7037ed05f54f | 167 | case TX_CRYPTO_ERROR: |
brunnobbco | 61:65744bc8ab55 | 168 | // printf("\r\n Transmission Error TX_Crypto_Error"); |
ruschigo | 62:89df9529dbb0 | 169 | break; |
mbed_official | 0:7037ed05f54f | 170 | case TX_SCHEDULING_ERROR: |
brunnobbco | 61:65744bc8ab55 | 171 | // printf("\r\n Transmission Error - EventCode = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 172 | // try again |
mbed_official | 0:7037ed05f54f | 173 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
ruschigo | 65:4090220e19d2 | 174 | //lora_send_message((uint8_t*)"teste2", (uint16_t)7); |
mbed_official | 0:7037ed05f54f | 175 | } |
mbed_official | 0:7037ed05f54f | 176 | break; |
mbed_official | 0:7037ed05f54f | 177 | case RX_DONE: |
brunnobbco | 61:65744bc8ab55 | 178 | // printf("\r\n Received message from Network Server \r\n"); |
ruschigo | 64:ed68ddac6360 | 179 | lora_receive_message(); |
mbed_official | 0:7037ed05f54f | 180 | break; |
mbed_official | 0:7037ed05f54f | 181 | case RX_TIMEOUT: |
brunnobbco | 61:65744bc8ab55 | 182 | // printf("\r\n Transmission Error RX_Timeout"); |
ruschigo | 62:89df9529dbb0 | 183 | break; |
mbed_official | 0:7037ed05f54f | 184 | case RX_ERROR: |
brunnobbco | 61:65744bc8ab55 | 185 | // printf("\r\n Error in reception - Code = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 186 | break; |
mbed_official | 0:7037ed05f54f | 187 | case JOIN_FAILURE: |
brunnobbco | 61:65744bc8ab55 | 188 | // printf("\r\n OTAA Failed - Check Keys \r\n"); |
mbed_official | 0:7037ed05f54f | 189 | break; |
mbed_official | 26:f07f5febf97f | 190 | case UPLINK_REQUIRED: |
brunnobbco | 61:65744bc8ab55 | 191 | // printf("\r\n Uplink required by NS \r\n"); |
mbed_official | 26:f07f5febf97f | 192 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
ruschigo | 65:4090220e19d2 | 193 | //lora_send_message((uint8_t*)"uplink", (uint16_t)7); |
mbed_official | 26:f07f5febf97f | 194 | } |
mbed_official | 26:f07f5febf97f | 195 | break; |
mbed_official | 0:7037ed05f54f | 196 | default: |
mbed_official | 0:7037ed05f54f | 197 | MBED_ASSERT("Unknown Event"); |
ruschigo | 62:89df9529dbb0 | 198 | break; |
mbed_official | 0:7037ed05f54f | 199 | } |
mbed_official | 0:7037ed05f54f | 200 | } |
mbed_official | 0:7037ed05f54f | 201 | |
mbed_official | 0:7037ed05f54f | 202 | // EOF |