Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Custom_LSM303 Custom_UBloxGPS LRAT-mbed-os USBDevice mbed-lora-radio-drv stm32EEPROM
Fork of LRAT-example-lorawan by
main.cpp@43:c900cd8ea072, 2018-09-06 (annotated)
- Committer:
- Huot87
- Date:
- Thu Sep 06 17:42:09 2018 +0000
- Revision:
- 43:c900cd8ea072
- Parent:
- 42:d59e50622292
- Child:
- 44:cf1070af05db
Spectrum Analyzer Test Code;
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 | */ |
lpeters | 36:dcc6f89fa39a | 17 | |
lpeters | 34:341fb423e74b | 18 | //#define TARGET_LRAT 1 |
lpeters | 35:73b3963c6dd3 | 19 | #define TARGET_DISCO1 1 |
lpeters | 35:73b3963c6dd3 | 20 | //#define TARGET_DISCO2 1 |
lpeters | 34:341fb423e74b | 21 | //#define SENSOR_TEMP 1 |
lpeters | 36:dcc6f89fa39a | 22 | |
Huot87 | 41:98266e454444 | 23 | #include "foo.h" |
lpeters | 42:d59e50622292 | 24 | int weGetSignal = 0; |
Huot87 | 41:98266e454444 | 25 | |
lpeters | 37:df69df1bcb1a | 26 | #define DCHILL 600 // Cycle delay in chill mode. |
lpeters | 37:df69df1bcb1a | 27 | #define DPANIC 60 // Cycle delay in panic mode. |
lpeters | 37:df69df1bcb1a | 28 | #define NPANIC 5 // Number of unprovoked panic cycles before cooldown. |
lpeters | 37:df69df1bcb1a | 29 | |
mbed_official | 0:7037ed05f54f | 30 | #include <stdio.h> |
tchari | 30:ff249a9156dc | 31 | #include "mbed.h" |
mbed_official | 3:8c7198d1a2a1 | 32 | |
mbed_official | 0:7037ed05f54f | 33 | #include "lorawan/LoRaWANInterface.h" |
mbed_official | 0:7037ed05f54f | 34 | #include "lorawan/system/lorawan_data_structures.h" |
mbed_official | 0:7037ed05f54f | 35 | #include "events/EventQueue.h" |
mbed_official | 0:7037ed05f54f | 36 | |
mbed_official | 0:7037ed05f54f | 37 | // Application helpers |
mbed_official | 0:7037ed05f54f | 38 | #include "DummySensor.h" |
mbed_official | 0:7037ed05f54f | 39 | #include "trace_helper.h" |
mbed_official | 0:7037ed05f54f | 40 | #include "lora_radio_helper.h" |
mbed_official | 0:7037ed05f54f | 41 | |
Huot87 | 40:eaf476b94684 | 42 | // HUOT ADDITION -- 08/21/2018 -- Import of EEPROM Library |
Huot87 | 40:eaf476b94684 | 43 | #include "stm32EEPROM.h" |
Huot87 | 40:eaf476b94684 | 44 | |
tchari | 30:ff249a9156dc | 45 | #include "mbed-trace/mbed_trace.h" |
tchari | 30:ff249a9156dc | 46 | #define TRACE_GROUP "MAIN" |
tchari | 30:ff249a9156dc | 47 | |
mbed_official | 0:7037ed05f54f | 48 | using namespace events; |
mbed_official | 0:7037ed05f54f | 49 | |
mbed_official | 12:5015dfead3f2 | 50 | // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. |
mbed_official | 12:5015dfead3f2 | 51 | // This example only communicates with much shorter messages (<30 bytes). |
mbed_official | 12:5015dfead3f2 | 52 | // If longer messages are used, these buffers must be changed accordingly. |
mbed_official | 12:5015dfead3f2 | 53 | uint8_t tx_buffer[30]; |
mbed_official | 12:5015dfead3f2 | 54 | uint8_t rx_buffer[30]; |
mbed_official | 0:7037ed05f54f | 55 | |
mbed_official | 0:7037ed05f54f | 56 | /* |
mbed_official | 0:7037ed05f54f | 57 | * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing |
mbed_official | 0:7037ed05f54f | 58 | */ |
mbed_official | 0:7037ed05f54f | 59 | #define TX_TIMER 10000 |
mbed_official | 0:7037ed05f54f | 60 | |
mbed_official | 0:7037ed05f54f | 61 | /** |
mbed_official | 0:7037ed05f54f | 62 | * Maximum number of events for the event queue. |
mbed_official | 12:5015dfead3f2 | 63 | * 10 is the safe number for the stack events, however, if application |
mbed_official | 0:7037ed05f54f | 64 | * also uses the queue for whatever purposes, this number should be increased. |
mbed_official | 0:7037ed05f54f | 65 | */ |
mbed_official | 12:5015dfead3f2 | 66 | #define MAX_NUMBER_OF_EVENTS 10 |
mbed_official | 0:7037ed05f54f | 67 | |
mbed_official | 0:7037ed05f54f | 68 | /** |
mbed_official | 0:7037ed05f54f | 69 | * Maximum number of retries for CONFIRMED messages before giving up |
mbed_official | 0:7037ed05f54f | 70 | */ |
Huot87 | 40:eaf476b94684 | 71 | #define CONFIRMED_MSG_RETRY_COUNTER 3 |
mbed_official | 0:7037ed05f54f | 72 | |
Huot87 | 40:eaf476b94684 | 73 | |
Huot87 | 40:eaf476b94684 | 74 | // HUOT NOTE -- THINK THIS CAN BE REMOVED |
mbed_official | 0:7037ed05f54f | 75 | /** |
mbed_official | 0:7037ed05f54f | 76 | * Dummy pin for dummy sensor |
mbed_official | 0:7037ed05f54f | 77 | */ |
Huot87 | 40:eaf476b94684 | 78 | //#define PC_9 0 |
mbed_official | 0:7037ed05f54f | 79 | |
mbed_official | 0:7037ed05f54f | 80 | /** |
mbed_official | 0:7037ed05f54f | 81 | * Dummy sensor class object |
mbed_official | 0:7037ed05f54f | 82 | */ |
Huot87 | 40:eaf476b94684 | 83 | //DS1820 ds1820(PC_9); |
Huot87 | 40:eaf476b94684 | 84 | |
Huot87 | 40:eaf476b94684 | 85 | #define PACKET_LEN 11 |
mbed_official | 0:7037ed05f54f | 86 | |
mbed_official | 0:7037ed05f54f | 87 | /** |
mbed_official | 0:7037ed05f54f | 88 | * This event queue is the global event queue for both the |
mbed_official | 0:7037ed05f54f | 89 | * application and stack. To conserve memory, the stack is designed to run |
mbed_official | 0:7037ed05f54f | 90 | * in the same thread as the application and the application is responsible for |
mbed_official | 0:7037ed05f54f | 91 | * providing an event queue to the stack that will be used for ISR deferment as |
mbed_official | 0:7037ed05f54f | 92 | * well as application information event queuing. |
mbed_official | 0:7037ed05f54f | 93 | */ |
mbed_official | 0:7037ed05f54f | 94 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS * EVENTS_EVENT_SIZE); |
mbed_official | 0:7037ed05f54f | 95 | |
mbed_official | 0:7037ed05f54f | 96 | /** |
mbed_official | 0:7037ed05f54f | 97 | * Event handler. |
mbed_official | 0:7037ed05f54f | 98 | * |
mbed_official | 0:7037ed05f54f | 99 | * This will be passed to the LoRaWAN stack to queue events for the |
mbed_official | 0:7037ed05f54f | 100 | * application which in turn drive the application. |
mbed_official | 0:7037ed05f54f | 101 | */ |
mbed_official | 0:7037ed05f54f | 102 | static void lora_event_handler(lorawan_event_t event); |
mbed_official | 0:7037ed05f54f | 103 | |
mbed_official | 0:7037ed05f54f | 104 | /** |
mbed_official | 2:dc95ac6d6d4e | 105 | * Constructing Mbed LoRaWANInterface and passing it down the radio object. |
mbed_official | 0:7037ed05f54f | 106 | */ |
mbed_official | 2:dc95ac6d6d4e | 107 | static LoRaWANInterface lorawan(radio); |
mbed_official | 0:7037ed05f54f | 108 | |
mbed_official | 0:7037ed05f54f | 109 | /** |
mbed_official | 0:7037ed05f54f | 110 | * Application specific callbacks |
mbed_official | 0:7037ed05f54f | 111 | */ |
mbed_official | 0:7037ed05f54f | 112 | static lorawan_app_callbacks_t callbacks; |
mbed_official | 0:7037ed05f54f | 113 | |
lpeters | 35:73b3963c6dd3 | 114 | /* |
lpeters | 35:73b3963c6dd3 | 115 | #if defined(TARGET_LRAT) or defined(TARGET_DISCO2) |
lpeters | 34:341fb423e74b | 116 | #include "USBSerial.h" |
lpeters | 34:341fb423e74b | 117 | USBSerial serial; |
lpeters | 34:341fb423e74b | 118 | FileHandle* mbed::mbed_override_console(int) { |
lpeters | 34:341fb423e74b | 119 | return &serial; |
lpeters | 34:341fb423e74b | 120 | } |
lpeters | 34:341fb423e74b | 121 | #endif |
lpeters | 35:73b3963c6dd3 | 122 | */ |
lpeters | 34:341fb423e74b | 123 | |
lpeters | 35:73b3963c6dd3 | 124 | uint32_t mytime = 0; |
lpeters | 35:73b3963c6dd3 | 125 | uint8_t mybatt = 0; |
lpeters | 35:73b3963c6dd3 | 126 | double mylat = 0; |
lpeters | 35:73b3963c6dd3 | 127 | double mylon = 0; |
lpeters | 33:e47306c32791 | 128 | |
lpeters | 33:e47306c32791 | 129 | int16_t myAccX = 0; |
lpeters | 33:e47306c32791 | 130 | int16_t myAccY = 0; |
lpeters | 33:e47306c32791 | 131 | int16_t myAccZ = 0; |
lpeters | 33:e47306c32791 | 132 | int16_t myMagX = 0; |
lpeters | 33:e47306c32791 | 133 | int16_t myMagY = 0; |
lpeters | 33:e47306c32791 | 134 | int16_t myMagZ = 0; |
lpeters | 33:e47306c32791 | 135 | int16_t myOffX = 0; |
lpeters | 33:e47306c32791 | 136 | int16_t myOffY = 0; |
lpeters | 33:e47306c32791 | 137 | int16_t myOffZ = 0; |
lpeters | 33:e47306c32791 | 138 | int16_t myTemp = 0; |
lpeters | 33:e47306c32791 | 139 | |
lpeters | 33:e47306c32791 | 140 | int16_t accMinX = 0; |
lpeters | 33:e47306c32791 | 141 | int16_t accMinY = 0; |
lpeters | 33:e47306c32791 | 142 | int16_t accMinZ = 0; |
lpeters | 33:e47306c32791 | 143 | int16_t accMaxX = 0; |
lpeters | 33:e47306c32791 | 144 | int16_t accMaxY = 0; |
lpeters | 33:e47306c32791 | 145 | int16_t accMaxZ = 0; |
lpeters | 33:e47306c32791 | 146 | |
lpeters | 33:e47306c32791 | 147 | int16_t magMinX = 0; |
lpeters | 33:e47306c32791 | 148 | int16_t magMinY = 0; |
lpeters | 33:e47306c32791 | 149 | int16_t magMinZ = 0; |
lpeters | 33:e47306c32791 | 150 | int16_t magMaxX = 0; |
lpeters | 33:e47306c32791 | 151 | int16_t magMaxY = 0; |
lpeters | 33:e47306c32791 | 152 | int16_t magMaxZ = 0; |
lpeters | 33:e47306c32791 | 153 | |
lpeters | 33:e47306c32791 | 154 | #define NEOM8M_ADR_GPS 0x84 |
lpeters | 34:341fb423e74b | 155 | //#define LSM303_ADR_ACC 0x32 |
lpeters | 33:e47306c32791 | 156 | #define LSM303_ADR_MAG 0x3C |
tchari | 30:ff249a9156dc | 157 | |
lpeters | 33:e47306c32791 | 158 | #define NEOM8M_REG_GPS_LENH 0xFD |
lpeters | 33:e47306c32791 | 159 | #define NEOM8M_REG_GPS_LENL 0xFE |
lpeters | 33:e47306c32791 | 160 | #define NEOM8M_REG_GPS_DATA 0xFE |
lpeters | 33:e47306c32791 | 161 | #define LSM303_REG_ACC_STATUS_REG_AUX_A 0x07 |
lpeters | 34:341fb423e74b | 162 | //#define LSM303_REG_ACC_OUT_TEMP_L_A 0x0C |
lpeters | 34:341fb423e74b | 163 | //#define LSM303_REG_ACC_OUT_TEMP_H_A 0x0D |
lpeters | 33:e47306c32791 | 164 | #define LSM303_REG_ACC_WHO_AM_I_A 0x0F |
lpeters | 34:341fb423e74b | 165 | //#define LSM303_REG_ACC_TEMP_CFG_REG_A 0x1F |
lpeters | 33:e47306c32791 | 166 | #define LSM303_REG_ACC_CTRL_REG1_A 0x20 |
lpeters | 33:e47306c32791 | 167 | #define LSM303_REG_ACC_CTRL_REG2_A 0x21 |
lpeters | 33:e47306c32791 | 168 | #define LSM303_REG_ACC_CTRL_REG3_A 0x22 |
lpeters | 33:e47306c32791 | 169 | #define LSM303_REG_ACC_CTRL_REG4_A 0x23 |
lpeters | 33:e47306c32791 | 170 | #define LSM303_REG_ACC_CTRL_REG5_A 0x24 |
lpeters | 34:341fb423e74b | 171 | #define LSM303_REG_ACC_CTRL_REG6_A 0x25 |
lpeters | 33:e47306c32791 | 172 | #define LSM303_REG_ACC_STATUS_REG_A 0x27 |
lpeters | 33:e47306c32791 | 173 | #define LSM303_REG_ACC_OUT_X_L_A 0x28 |
lpeters | 33:e47306c32791 | 174 | #define LSM303_REG_ACC_OUT_X_H_A 0x29 |
lpeters | 33:e47306c32791 | 175 | #define LSM303_REG_ACC_OUT_Y_L_A 0x2A |
lpeters | 33:e47306c32791 | 176 | #define LSM303_REG_ACC_OUT_Y_H_A 0x2B |
lpeters | 33:e47306c32791 | 177 | #define LSM303_REG_ACC_OUT_Z_L_A 0x2C |
lpeters | 33:e47306c32791 | 178 | #define LSM303_REG_ACC_OUT_Z_H_A 0x2D |
lpeters | 33:e47306c32791 | 179 | #define LSM303_REG_ACC_INT1_CFG_A 0x30 |
lpeters | 33:e47306c32791 | 180 | #define LSM303_REG_ACC_INT1_SRC_A 0x31 |
lpeters | 33:e47306c32791 | 181 | #define LSM303_REG_ACC_INT1_THS_A 0x32 |
lpeters | 33:e47306c32791 | 182 | #define LSM303_REG_ACC_INT1_DURATION_A 0x33 |
lpeters | 35:73b3963c6dd3 | 183 | |
lpeters | 33:e47306c32791 | 184 | #define LSM303_REG_MAG_OFFSET_X_REG_L_M 0x45 |
lpeters | 33:e47306c32791 | 185 | #define LSM303_REG_MAG_OFFSET_X_REG_H_M 0x46 |
lpeters | 33:e47306c32791 | 186 | #define LSM303_REG_MAG_OFFSET_Y_REG_L_M 0x47 |
lpeters | 33:e47306c32791 | 187 | #define LSM303_REG_MAG_OFFSET_Y_REG_H_M 0x48 |
lpeters | 33:e47306c32791 | 188 | #define LSM303_REG_MAG_OFFSET_Z_REG_L_M 0x49 |
lpeters | 33:e47306c32791 | 189 | #define LSM303_REG_MAG_OFFSET_Z_REG_H_M 0x4A |
lpeters | 34:341fb423e74b | 190 | //#define LSM303_REG_MAG_WHO_AM_I_M 0x4F |
lpeters | 34:341fb423e74b | 191 | //#define LSM303_REG_MAG_CFG_REG_A_M 0x60 |
lpeters | 34:341fb423e74b | 192 | //#define LSM303_REG_MAG_CFG_REG_B_M 0x61 |
lpeters | 34:341fb423e74b | 193 | //#define LSM303_REG_MAG_CFG_REG_C_M 0x62 |
lpeters | 33:e47306c32791 | 194 | #define LSM303_REG_MAG_INT_CTRL_REG_M 0x63 |
lpeters | 33:e47306c32791 | 195 | #define LSM303_REG_MAG_INT_SOURCE_REG_M 0x64 |
lpeters | 33:e47306c32791 | 196 | #define LSM303_REG_MAG_INT_THS_L_REG_M 0x65 |
lpeters | 33:e47306c32791 | 197 | #define LSM303_REG_MAG_INT_THS_H_REG_M 0x66 |
lpeters | 34:341fb423e74b | 198 | //#define LSM303_REG_MAG_STATUS_REG_M 0x67 |
lpeters | 34:341fb423e74b | 199 | //#define LSM303_REG_MAG_OUTX_L_REG_M 0x68 |
lpeters | 34:341fb423e74b | 200 | //#define LSM303_REG_MAG_OUTX_H_REG_M 0x69 |
lpeters | 34:341fb423e74b | 201 | //#define LSM303_REG_MAG_OUTY_L_REG_M 0x6A |
lpeters | 34:341fb423e74b | 202 | //#define LSM303_REG_MAG_OUTY_H_REG_M 0x6B |
lpeters | 34:341fb423e74b | 203 | //#define LSM303_REG_MAG_OUTZ_L_REG_M 0x6C |
lpeters | 34:341fb423e74b | 204 | //#define LSM303_REG_MAG_OUTZ_H_REG_M 0x6D |
lpeters | 33:e47306c32791 | 205 | |
lpeters | 35:73b3963c6dd3 | 206 | #define EEPROM_MAX 0x17FF |
lpeters | 35:73b3963c6dd3 | 207 | |
lpeters | 34:341fb423e74b | 208 | #if defined(TARGET_LRAT) |
lpeters | 34:341fb423e74b | 209 | #define LEDR PB_6 |
lpeters | 34:341fb423e74b | 210 | #define LEDG PB_7 |
lpeters | 34:341fb423e74b | 211 | #define LEDB PB_5 |
lpeters | 34:341fb423e74b | 212 | #define LEDW PB_2 |
lpeters | 35:73b3963c6dd3 | 213 | #define PIN_ACC PB_14 |
lpeters | 35:73b3963c6dd3 | 214 | #define PIN_MAG PB_12 |
lpeters | 35:73b3963c6dd3 | 215 | #define PIN_BTN PA_5 |
lpeters | 35:73b3963c6dd3 | 216 | #define PIN_ALT PA_4 |
lpeters | 34:341fb423e74b | 217 | #define LSM303_ADR_ACC 0x3A |
lpeters | 34:341fb423e74b | 218 | #define LSM303_REG_MAG_WHO_AM_I_M 0x0F |
lpeters | 34:341fb423e74b | 219 | #define LSM303_WHO_ACC 0x41 |
lpeters | 34:341fb423e74b | 220 | #define LSM303_WHO_MAG 0x3D |
lpeters | 34:341fb423e74b | 221 | #define LSM303_CTRL_REG7_A 0x26 |
lpeters | 34:341fb423e74b | 222 | #define LSM303_REG_MAG_CTRL_REG1_M 0x20 |
lpeters | 34:341fb423e74b | 223 | #define LSM303_REG_MAG_CTRL_REG2_M 0x21 |
lpeters | 34:341fb423e74b | 224 | #define LSM303_REG_MAG_CTRL_REG3_M 0x22 |
lpeters | 34:341fb423e74b | 225 | #define LSM303_REG_MAG_CTRL_REG4_M 0x23 |
lpeters | 34:341fb423e74b | 226 | #define LSM303_REG_MAG_CTRL_REG5_M 0x24 |
lpeters | 34:341fb423e74b | 227 | #define LSM303_REG_MAG_STATUS_REG_M 0x27 |
lpeters | 34:341fb423e74b | 228 | #define LSM303_REG_MAG_OUTX_L_REG_M 0x28 |
lpeters | 34:341fb423e74b | 229 | #define LSM303_REG_MAG_OUTX_H_REG_M 0x29 |
lpeters | 34:341fb423e74b | 230 | #define LSM303_REG_MAG_OUTY_L_REG_M 0x2A |
lpeters | 34:341fb423e74b | 231 | #define LSM303_REG_MAG_OUTY_H_REG_M 0x2B |
lpeters | 34:341fb423e74b | 232 | #define LSM303_REG_MAG_OUTZ_L_REG_M 0x2C |
lpeters | 34:341fb423e74b | 233 | #define LSM303_REG_MAG_OUTZ_H_REG_M 0x2D |
lpeters | 34:341fb423e74b | 234 | #define LSM303_REG_MAG_TEMP_L_M 0x2E |
lpeters | 34:341fb423e74b | 235 | #define LSM303_REG_MAG_TEMP_H_M 0x2F |
lpeters | 34:341fb423e74b | 236 | #define LSM303_REG_MAG_TEMP_CFG_REG_A 0x1F |
lpeters | 34:341fb423e74b | 237 | #define CFG_ACC_ADR LSM303_REG_ACC_CTRL_REG1_A |
lpeters | 34:341fb423e74b | 238 | #define CFG_ACC_LEN 7 |
lpeters | 34:341fb423e74b | 239 | #define CFG_MAG_ADR LSM303_REG_MAG_CTRL_REG1_M |
lpeters | 34:341fb423e74b | 240 | #define CFG_MAG_LEN 5 |
Huot87 | 39:6da6cb44f22e | 241 | |
Huot87 | 39:6da6cb44f22e | 242 | // HUOT ADDITIONS -- 08/30/2018 -- Port of LRAT LSM303CTR ACC Register definitions |
Huot87 | 39:6da6cb44f22e | 243 | // LSM303CTR Specific ACC Register ID's |
Huot87 | 39:6da6cb44f22e | 244 | #define LSM303_REG_ACC_INT1_THS_X1_A 0x32 |
Huot87 | 39:6da6cb44f22e | 245 | #define LSM303_REG_ACC_INT1_THS_Y1_A 0x33 |
Huot87 | 39:6da6cb44f22e | 246 | #define LSM303_REG_ACC_INT1_THS_Z1_A 0x34 |
Huot87 | 39:6da6cb44f22e | 247 | #define LSM303_REG_ACC_INT1_DURATION_A 0x35 |
Huot87 | 39:6da6cb44f22e | 248 | #define LSM303_REG_ACC_INT2_CFG_A 0x36 |
Huot87 | 39:6da6cb44f22e | 249 | #define LSM303_REG_ACC_INT2_SRC_A 0x37 |
Huot87 | 39:6da6cb44f22e | 250 | #define LSM303_REG_ACC_INT2_THS_A 0x38 |
Huot87 | 39:6da6cb44f22e | 251 | #define LSM303_REG_ACC_INT2_DURATION_A 0x39 |
Huot87 | 39:6da6cb44f22e | 252 | #define LSM303_REG_ACC_XL_REFERENCE 0x3A |
Huot87 | 39:6da6cb44f22e | 253 | #define LSM303_REG_ACC_XH_REFERENCE 0x3B |
Huot87 | 39:6da6cb44f22e | 254 | #define LSM303_REG_ACC_YL_REFERENCE 0x3C |
Huot87 | 39:6da6cb44f22e | 255 | #define LSM303_REG_ACC_YH_REFERENCE 0x3D |
Huot87 | 39:6da6cb44f22e | 256 | #define LSM303_REG_ACC_ZL_REFERENCE 0x3E |
Huot87 | 39:6da6cb44f22e | 257 | #define LSM303_REG_ACC_ZH_REFERENCE 0x3F |
Huot87 | 39:6da6cb44f22e | 258 | |
lpeters | 34:341fb423e74b | 259 | #else |
lpeters | 34:341fb423e74b | 260 | #define LEDR PB_7 |
lpeters | 34:341fb423e74b | 261 | #define LEDG PB_5 |
lpeters | 34:341fb423e74b | 262 | #define LEDB PB_6 |
lpeters | 34:341fb423e74b | 263 | #define LEDW PA_5 |
lpeters | 35:73b3963c6dd3 | 264 | #define PIN_ACC PB_14 // Not really. |
lpeters | 35:73b3963c6dd3 | 265 | #if defined(TARGET_DISCO2) |
lpeters | 35:73b3963c6dd3 | 266 | #define PIN_MAG PB_13 |
lpeters | 37:df69df1bcb1a | 267 | #define PIN_GPS PA_10 |
lpeters | 35:73b3963c6dd3 | 268 | #else |
lpeters | 35:73b3963c6dd3 | 269 | #define PIN_MAG PA_10 |
lpeters | 37:df69df1bcb1a | 270 | #define PIN_GPS PB_13 |
lpeters | 35:73b3963c6dd3 | 271 | #endif |
lpeters | 35:73b3963c6dd3 | 272 | #define PIN_BTN PB_2 |
lpeters | 34:341fb423e74b | 273 | #define LSM303_ADR_ACC 0x32 |
lpeters | 34:341fb423e74b | 274 | #define LSM303_REG_MAG_WHO_AM_I_M 0x4F |
lpeters | 34:341fb423e74b | 275 | #define LSM303_WHO_ACC 0x33 |
lpeters | 34:341fb423e74b | 276 | #define LSM303_WHO_MAG 0x40 |
lpeters | 34:341fb423e74b | 277 | #define LSM303_REG_ACC_OUT_TEMP_L_A 0x0C |
lpeters | 34:341fb423e74b | 278 | #define LSM303_REG_ACC_OUT_TEMP_H_A 0x0D |
lpeters | 34:341fb423e74b | 279 | #define LSM303_REG_ACC_TEMP_CFG_REG_A 0x1F |
lpeters | 34:341fb423e74b | 280 | #define LSM303_REG_MAG_CFG_REG_A_M 0x60 |
lpeters | 34:341fb423e74b | 281 | #define LSM303_REG_MAG_CFG_REG_B_M 0x61 |
lpeters | 34:341fb423e74b | 282 | #define LSM303_REG_MAG_CFG_REG_C_M 0x62 |
lpeters | 34:341fb423e74b | 283 | #define LSM303_REG_MAG_STATUS_REG_M 0x67 |
lpeters | 34:341fb423e74b | 284 | #define LSM303_REG_MAG_OUTX_L_REG_M 0x68 |
lpeters | 34:341fb423e74b | 285 | #define LSM303_REG_MAG_OUTX_H_REG_M 0x69 |
lpeters | 34:341fb423e74b | 286 | #define LSM303_REG_MAG_OUTY_L_REG_M 0x6A |
lpeters | 34:341fb423e74b | 287 | #define LSM303_REG_MAG_OUTY_H_REG_M 0x6B |
lpeters | 34:341fb423e74b | 288 | #define LSM303_REG_MAG_OUTZ_L_REG_M 0x6C |
lpeters | 34:341fb423e74b | 289 | #define LSM303_REG_MAG_OUTZ_H_REG_M 0x6D |
lpeters | 34:341fb423e74b | 290 | #define CFG_ACC_ADR LSM303_REG_ACC_TEMP_CFG_REG_A // Start Disco at TEMP CFG. |
lpeters | 34:341fb423e74b | 291 | #define CFG_ACC_LEN 7 |
lpeters | 34:341fb423e74b | 292 | #define CFG_MAG_ADR LSM303_REG_MAG_CFG_REG_A_M |
lpeters | 34:341fb423e74b | 293 | #define CFG_MAG_LEN 3 |
lpeters | 34:341fb423e74b | 294 | #endif |
tchari | 30:ff249a9156dc | 295 | |
tchari | 30:ff249a9156dc | 296 | I2C i2c(PB_9, PB_8); |
lpeters | 35:73b3963c6dd3 | 297 | InterruptIn accPin(PIN_ACC); |
lpeters | 35:73b3963c6dd3 | 298 | InterruptIn magPin(PIN_MAG); |
lpeters | 35:73b3963c6dd3 | 299 | InterruptIn btnPin(PIN_BTN); |
lpeters | 33:e47306c32791 | 300 | |
lpeters | 37:df69df1bcb1a | 301 | #if defined(TARGET_DISCO1) or defined(TARGET_DISCO2) |
lpeters | 37:df69df1bcb1a | 302 | DigitalOut gpsPin(PIN_GPS); |
lpeters | 37:df69df1bcb1a | 303 | #endif |
lpeters | 37:df69df1bcb1a | 304 | |
lpeters | 35:73b3963c6dd3 | 305 | uint8_t cfg; |
lpeters | 33:e47306c32791 | 306 | char ret; |
lpeters | 33:e47306c32791 | 307 | char rda = '\0'; |
lpeters | 33:e47306c32791 | 308 | char cmd[2]; |
lpeters | 37:df69df1bcb1a | 309 | //char buf[83]; |
lpeters | 37:df69df1bcb1a | 310 | char buf[192]; |
lpeters | 35:73b3963c6dd3 | 311 | uint8_t pos = 0; |
lpeters | 35:73b3963c6dd3 | 312 | |
lpeters | 33:e47306c32791 | 313 | int accShift = 0; |
lpeters | 33:e47306c32791 | 314 | int accScale = 0; |
lpeters | 33:e47306c32791 | 315 | int accEvent = 0; |
lpeters | 35:73b3963c6dd3 | 316 | uint8_t accSFire = 0; |
lpeters | 35:73b3963c6dd3 | 317 | uint8_t accHFire = 0; |
lpeters | 35:73b3963c6dd3 | 318 | uint8_t accSLIRQ = 0; |
lpeters | 35:73b3963c6dd3 | 319 | uint8_t accSHIRQ = 0; |
lpeters | 33:e47306c32791 | 320 | int magEvent = 0; |
lpeters | 35:73b3963c6dd3 | 321 | uint8_t magSFire = 0; |
lpeters | 35:73b3963c6dd3 | 322 | uint8_t magHFire = 0; |
lpeters | 35:73b3963c6dd3 | 323 | uint8_t magSLIRQ = 0; |
lpeters | 35:73b3963c6dd3 | 324 | uint8_t magSHIRQ = 0; |
lpeters | 35:73b3963c6dd3 | 325 | uint8_t btnHFire = 0; |
lpeters | 33:e47306c32791 | 326 | |
lpeters | 35:73b3963c6dd3 | 327 | char *res; |
lpeters | 35:73b3963c6dd3 | 328 | char sPass[26] = "[\u001b[32mPASS\u001b[0m]"; |
lpeters | 35:73b3963c6dd3 | 329 | char sFail[26] = "[\u001b[31mFAIL\u001b[0m]"; |
lpeters | 33:e47306c32791 | 330 | char cmdSendLoop[9] = "SendLoop"; |
lpeters | 37:df69df1bcb1a | 331 | //char cmdStopGNSS[14] = {0xB5, 0x62, 0x06, 0x57, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x4F, 0x50}; |
lpeters | 37:df69df1bcb1a | 332 | |
lpeters | 37:df69df1bcb1a | 333 | //#define CMDSTOPGNSS {0xFF, 0xB5, 0x62, 0x06, 0x57, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x4F, 0x50} |
lpeters | 37:df69df1bcb1a | 334 | //char cmdStopGNSS[16] {0xB5, 0x62, 0x06, 0x57, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // Leave 2 bytes for CRC. |
lpeters | 37:df69df1bcb1a | 335 | //char cmdStopGNSS[16] {0xB5, 0x62, 0x06, 0x57, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, 0x53, 0x54, 0x4F, 0x50}; // Leave 2 bytes for CRC. |
Huot87 | 38:fd7a407bfe3e | 336 | char cmdStopGNSS[16] {0xB5, 0x62, 0x06, 0x57, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, 0x50, 0x4F, 0x54, 0x53}; // Leave 2 bytes for CRC. // Note Byte character is Little Endian from what doc says |
lpeters | 37:df69df1bcb1a | 337 | char cmdReadGNSS[8] {0xB5, 0x62, 0x06, 0x57, 0x00, 0x00}; |
lpeters | 37:df69df1bcb1a | 338 | char cmdReadCfgU[8] {0xB5, 0x62, 0x06, 0x1B, 0x00, 0x00}; // Leave 2 bytes for CRC. |
lpeters | 37:df69df1bcb1a | 339 | char cmdPollPort[9] {0xB5, 0x62, 0x06, 0x00, 0x01, 0x00, 0x00}; |
lpeters | 33:e47306c32791 | 340 | |
Huot87 | 39:6da6cb44f22e | 341 | // HUOT ADDITIONS - 08/29/18 -- ADDED UBX-CFG CHAR BUFFERS -- BOTTOM TWO WORK! |
Huot87 | 38:fd7a407bfe3e | 342 | // UBX-CFG-GNSS -- Disable Glonass (also leave 2 bytes) |
Huot87 | 38:fd7a407bfe3e | 343 | char cmdDisableGLONASS[68] {0xB5, 0x62, 0x06, 0x3E, 0x3C, 0x00, 0x00, 0x20, 0x20, 0x07, 0x00, 0x08, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0x01, 0x02, 0x04, 0x08, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x08, 0x10, 0x00, 0x00, 0x00, 0x01, 0x01, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x03, 0x05, 0x00, 0x03, 0x00, 0x01, 0x00, 0x01, 0x05, 0x06, 0x08, 0x0E, 0x00, 0x01, 0x00, 0x01, 0x01}; |
Huot87 | 38:fd7a407bfe3e | 344 | // UBX-CFG-TP5 -- Disable Timepulse (also leave 2 bytes) |
Huot87 | 38:fd7a407bfe3e | 345 | char cmdDisableTimePulse[40] {0xB5, 0x62, 0x06, 0x31, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x40, 0x42, 0x0F, 0x00, 0x40, 0x42, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x86, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x00, 0x00, 0x00}; |
Huot87 | 38:fd7a407bfe3e | 346 | // UBX-CFG-PM2 -- Configure Power Management Setup (also leave 2 bytes) |
Huot87 | 38:fd7a407bfe3e | 347 | char cmdConfigPMS[52] {0xB5, 0x62, 0x06, 0x3B, 0x2C, 0x00, 0x01, 0x06, 0x00, 0x00, 0x2E, 0x08, 0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2C, 0x01, 0x00, 0x00, 0x4F, 0xC1, 0x03, 0x00, 0x86, 0x02, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x64, 0x40, 0x01, 0x00, 0x56, 0xA4}; |
Huot87 | 38:fd7a407bfe3e | 348 | char cmdSaveConfig[21] {0xB5, 0x62, 0x06, 0x09, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}; // UBX-CFG-CFG -- Save Configuration (also leave 2 bytes) |
Huot87 | 38:fd7a407bfe3e | 349 | char cmdResetDefaults[21] {0xB5, 0x62, 0x06, 0x09, 0x0D, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x03}; // UBX-CFG-CFG -- Reset to Defaults (also leave 2 bytes) |
Huot87 | 38:fd7a407bfe3e | 350 | |
Huot87 | 38:fd7a407bfe3e | 351 | // THIS WORKS |
Huot87 | 40:eaf476b94684 | 352 | char lowPowerMode[10] {0xB5, 0x62, 0x06, 0x11, 0x02, 0x00, 0x48, 0x01}; |
Huot87 | 38:fd7a407bfe3e | 353 | char secondTimeSleep[12] {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00,0x08, 0x00}; |
Huot87 | 38:fd7a407bfe3e | 354 | char secondTimeAwake[12] {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00,0x09, 0x00}; |
Huot87 | 38:fd7a407bfe3e | 355 | |
Huot87 | 40:eaf476b94684 | 356 | |
Huot87 | 40:eaf476b94684 | 357 | // HUOT ADDITIONS -- 08/31/2018 -- EEPROM Variables |
Huot87 | 40:eaf476b94684 | 358 | bool wasEEPROM = false; |
Huot87 | 40:eaf476b94684 | 359 | bool continueSending = true; |
Huot87 | 40:eaf476b94684 | 360 | int eepromCounter = 0; |
Huot87 | 40:eaf476b94684 | 361 | int registerLocation = 0; |
Huot87 | 40:eaf476b94684 | 362 | |
lpeters | 35:73b3963c6dd3 | 363 | time_t tInit = 0; |
lpeters | 35:73b3963c6dd3 | 364 | time_t tBump = 0; |
lpeters | 35:73b3963c6dd3 | 365 | time_t tLast = 0; |
lpeters | 35:73b3963c6dd3 | 366 | time_t tNext = 0; |
lpeters | 36:dcc6f89fa39a | 367 | time_t tSend = 0; |
lpeters | 37:df69df1bcb1a | 368 | time_t tCalm = 0; |
lpeters | 35:73b3963c6dd3 | 369 | |
lpeters | 34:341fb423e74b | 370 | DigitalOut myLedR(LEDR); |
lpeters | 34:341fb423e74b | 371 | DigitalOut myLedG(LEDG); |
lpeters | 34:341fb423e74b | 372 | DigitalOut myLedB(LEDB); |
lpeters | 34:341fb423e74b | 373 | DigitalOut myLedW(LEDW); |
lpeters | 34:341fb423e74b | 374 | |
lpeters | 34:341fb423e74b | 375 | void magInitSequence(); |
lpeters | 34:341fb423e74b | 376 | void accInitSequence(); |
lpeters | 34:341fb423e74b | 377 | void gpsInitSequence(); |
Huot87 | 40:eaf476b94684 | 378 | // HUOT ADDITION -- 08/30/2018 |
Huot87 | 38:fd7a407bfe3e | 379 | void gpsExecuteCommand(char *command, int length); |
lpeters | 34:341fb423e74b | 380 | void tmpRead(); |
lpeters | 34:341fb423e74b | 381 | void magRead(); |
lpeters | 34:341fb423e74b | 382 | void accRead(); |
Huot87 | 38:fd7a407bfe3e | 383 | int gpsRead(); |
lpeters | 37:df69df1bcb1a | 384 | void ubxRead(); |
lpeters | 36:dcc6f89fa39a | 385 | void send_message(); |
lpeters | 42:d59e50622292 | 386 | void send_backlog(); |
Huot87 | 40:eaf476b94684 | 387 | // HUOT ADDITION -- 08/31/2018 |
Huot87 | 40:eaf476b94684 | 388 | void backupPacket(); |
Huot87 | 40:eaf476b94684 | 389 | void prepBacklogSend(); |
lpeters | 34:341fb423e74b | 390 | |
lpeters | 35:73b3963c6dd3 | 391 | void onBtnIrq() |
lpeters | 35:73b3963c6dd3 | 392 | { |
lpeters | 35:73b3963c6dd3 | 393 | btnHFire++; |
lpeters | 36:dcc6f89fa39a | 394 | tSend = 0; |
lpeters | 35:73b3963c6dd3 | 395 | } |
lpeters | 35:73b3963c6dd3 | 396 | |
lpeters | 33:e47306c32791 | 397 | void onAccIrq() |
lpeters | 33:e47306c32791 | 398 | { |
lpeters | 33:e47306c32791 | 399 | accHFire++; |
lpeters | 36:dcc6f89fa39a | 400 | tSend = 0; |
lpeters | 33:e47306c32791 | 401 | } |
lpeters | 33:e47306c32791 | 402 | |
lpeters | 33:e47306c32791 | 403 | void onMagIrq() |
lpeters | 33:e47306c32791 | 404 | { |
lpeters | 33:e47306c32791 | 405 | magHFire++; |
lpeters | 36:dcc6f89fa39a | 406 | tSend = 0; |
lpeters | 33:e47306c32791 | 407 | } |
lpeters | 33:e47306c32791 | 408 | |
lpeters | 33:e47306c32791 | 409 | void accDumpCfg() |
lpeters | 33:e47306c32791 | 410 | { |
lpeters | 34:341fb423e74b | 411 | char start = CFG_ACC_ADR; |
lpeters | 34:341fb423e74b | 412 | for (int i = 0; i < CFG_ACC_LEN; i++) |
lpeters | 33:e47306c32791 | 413 | { |
lpeters | 33:e47306c32791 | 414 | cmd[0] = start + i; |
lpeters | 33:e47306c32791 | 415 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 33:e47306c32791 | 416 | i2c.read(LSM303_ADR_ACC, &buf[i], 1); |
lpeters | 33:e47306c32791 | 417 | } |
lpeters | 34:341fb423e74b | 418 | printf("CFGACC: | "); |
lpeters | 34:341fb423e74b | 419 | for (int i = 0; i < CFG_ACC_LEN; i++) |
lpeters | 34:341fb423e74b | 420 | { |
lpeters | 34:341fb423e74b | 421 | printf("%02X ", buf[i]); |
lpeters | 34:341fb423e74b | 422 | } |
lpeters | 34:341fb423e74b | 423 | printf("|\r\n"); |
lpeters | 33:e47306c32791 | 424 | } |
lpeters | 33:e47306c32791 | 425 | |
lpeters | 33:e47306c32791 | 426 | void magDumpCfg() |
lpeters | 33:e47306c32791 | 427 | { |
lpeters | 34:341fb423e74b | 428 | char start = CFG_MAG_ADR; |
lpeters | 34:341fb423e74b | 429 | for (int i = 0; i < CFG_MAG_LEN; i++) |
lpeters | 33:e47306c32791 | 430 | { |
lpeters | 33:e47306c32791 | 431 | cmd[0] = start + i; |
lpeters | 33:e47306c32791 | 432 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 33:e47306c32791 | 433 | i2c.read(LSM303_ADR_MAG, &buf[i], 1); |
lpeters | 33:e47306c32791 | 434 | } |
lpeters | 34:341fb423e74b | 435 | printf("CFGMAG: | "); |
lpeters | 34:341fb423e74b | 436 | for (int i = 0; i < CFG_MAG_LEN; i++) |
lpeters | 34:341fb423e74b | 437 | { |
lpeters | 34:341fb423e74b | 438 | printf("%02X ", buf[i]); |
lpeters | 34:341fb423e74b | 439 | } |
lpeters | 34:341fb423e74b | 440 | printf("|\r\n"); |
lpeters | 33:e47306c32791 | 441 | } |
lpeters | 33:e47306c32791 | 442 | |
lpeters | 37:df69df1bcb1a | 443 | // Clear any pending MAG IRQs |
lpeters | 42:d59e50622292 | 444 | inline void magWipeIrq() |
lpeters | 42:d59e50622292 | 445 | { |
lpeters | 37:df69df1bcb1a | 446 | cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M; |
lpeters | 37:df69df1bcb1a | 447 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 37:df69df1bcb1a | 448 | i2c.read(LSM303_ADR_MAG, &ret, 1); |
lpeters | 37:df69df1bcb1a | 449 | } |
lpeters | 37:df69df1bcb1a | 450 | |
mbed_official | 0:7037ed05f54f | 451 | /** |
mbed_official | 0:7037ed05f54f | 452 | * Entry point for application |
mbed_official | 0:7037ed05f54f | 453 | */ |
mbed_official | 0:7037ed05f54f | 454 | int main (void) |
mbed_official | 0:7037ed05f54f | 455 | { |
lpeters | 35:73b3963c6dd3 | 456 | //i2c.frequency(400000); |
lpeters | 33:e47306c32791 | 457 | wait(4); |
lpeters | 33:e47306c32791 | 458 | printf("\r\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\r\n"); |
lpeters | 34:341fb423e74b | 459 | |
lpeters | 34:341fb423e74b | 460 | // Boot Flash |
lpeters | 34:341fb423e74b | 461 | for (int i = 0; i <= 64; i++) |
lpeters | 33:e47306c32791 | 462 | { |
lpeters | 34:341fb423e74b | 463 | myLedR = i & 0x01; |
lpeters | 34:341fb423e74b | 464 | myLedG = i & 0x02; |
lpeters | 34:341fb423e74b | 465 | myLedB = i & 0x04; |
lpeters | 34:341fb423e74b | 466 | myLedW = i & 0x08; |
lpeters | 34:341fb423e74b | 467 | wait(0.01); |
lpeters | 33:e47306c32791 | 468 | } |
lpeters | 34:341fb423e74b | 469 | wait(4); |
lpeters | 35:73b3963c6dd3 | 470 | // btnPin.rise(&onBtnIrq); |
lpeters | 37:df69df1bcb1a | 471 | printf("Boot flash complete.\r\n"); |
mbed_official | 0:7037ed05f54f | 472 | // setup tracing |
mbed_official | 0:7037ed05f54f | 473 | setup_trace(); |
mbed_official | 0:7037ed05f54f | 474 | |
lpeters | 37:df69df1bcb1a | 475 | printf("Trace setup complete.\r\n"); |
lpeters | 37:df69df1bcb1a | 476 | |
mbed_official | 0:7037ed05f54f | 477 | // stores the status of a call to LoRaWAN protocol |
mbed_official | 0:7037ed05f54f | 478 | lorawan_status_t retcode; |
lpeters | 33:e47306c32791 | 479 | |
lpeters | 37:df69df1bcb1a | 480 | printf("Pre-sensor init.\r\n"); |
lpeters | 37:df69df1bcb1a | 481 | |
lpeters | 34:341fb423e74b | 482 | /* I2C init */ |
lpeters | 34:341fb423e74b | 483 | ret = 0x00; |
lpeters | 34:341fb423e74b | 484 | magDumpCfg(); |
lpeters | 34:341fb423e74b | 485 | accDumpCfg(); |
lpeters | 34:341fb423e74b | 486 | magInitSequence(); |
lpeters | 34:341fb423e74b | 487 | accInitSequence(); |
Huot87 | 38:fd7a407bfe3e | 488 | |
Huot87 | 39:6da6cb44f22e | 489 | // HUOT ADDITION -- 08/30/2018 -- RESET GPS TO DEFAULT BEFORE MOVING FORWARD |
Huot87 | 38:fd7a407bfe3e | 490 | gpsExecuteCommand(cmdResetDefaults, sizeof(cmdResetDefaults)); |
Huot87 | 38:fd7a407bfe3e | 491 | |
lpeters | 34:341fb423e74b | 492 | gpsInitSequence(); |
lpeters | 33:e47306c32791 | 493 | magDumpCfg(); |
lpeters | 33:e47306c32791 | 494 | accDumpCfg(); |
lpeters | 37:df69df1bcb1a | 495 | printf("Post-sensor init.\r\n"); |
lpeters | 33:e47306c32791 | 496 | |
lpeters | 34:341fb423e74b | 497 | cfg = 0x00; |
lpeters | 34:341fb423e74b | 498 | #if defined(TARGET_LRAT) |
lpeters | 33:e47306c32791 | 499 | cmd[0] = LSM303_REG_ACC_CTRL_REG1_A; |
lpeters | 33:e47306c32791 | 500 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 33:e47306c32791 | 501 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 34:341fb423e74b | 502 | cfg |= (ret & 0x80) >> 7; |
lpeters | 33:e47306c32791 | 503 | cmd[0] = LSM303_REG_ACC_CTRL_REG4_A; |
lpeters | 33:e47306c32791 | 504 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 33:e47306c32791 | 505 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 34:341fb423e74b | 506 | accScale = 1 << (!(ret & 0x30) ? 0 : ((ret & 0x30) >> 4) - 1); |
lpeters | 34:341fb423e74b | 507 | cmd[0] = LSM303_REG_MAG_CTRL_REG2_M; |
lpeters | 34:341fb423e74b | 508 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 509 | i2c.read(LSM303_ADR_MAG, &ret, 1); |
lpeters | 34:341fb423e74b | 510 | cfg |= (ret & 0x60) >> 1; |
lpeters | 34:341fb423e74b | 511 | //accShift = 0; // Full 16-bit resolution |
lpeters | 34:341fb423e74b | 512 | accShift = 4; |
lpeters | 34:341fb423e74b | 513 | #else |
lpeters | 33:e47306c32791 | 514 | cmd[0] = LSM303_REG_ACC_CTRL_REG1_A; |
lpeters | 33:e47306c32791 | 515 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 33:e47306c32791 | 516 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 33:e47306c32791 | 517 | cfg |= (ret & 0x08) >> 3; |
lpeters | 33:e47306c32791 | 518 | cmd[0] = LSM303_REG_ACC_CTRL_REG4_A; |
lpeters | 33:e47306c32791 | 519 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 33:e47306c32791 | 520 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 33:e47306c32791 | 521 | cfg |= (ret & 0x08) >> 2; |
lpeters | 33:e47306c32791 | 522 | accScale = 1 << ((ret & 0x30) >> 4); |
lpeters | 33:e47306c32791 | 523 | cmd[0] = LSM303_REG_MAG_CFG_REG_A_M; |
lpeters | 33:e47306c32791 | 524 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 33:e47306c32791 | 525 | i2c.read(LSM303_ADR_MAG, &ret, 1); |
lpeters | 33:e47306c32791 | 526 | cfg |= (ret & 0x10); |
lpeters | 33:e47306c32791 | 527 | if (cfg & 0x01) |
lpeters | 33:e47306c32791 | 528 | accShift = 8; |
lpeters | 33:e47306c32791 | 529 | else if (cfg & 0x02) |
lpeters | 33:e47306c32791 | 530 | accShift = 4; |
lpeters | 33:e47306c32791 | 531 | else |
lpeters | 33:e47306c32791 | 532 | accShift = 6; |
lpeters | 34:341fb423e74b | 533 | #endif |
lpeters | 33:e47306c32791 | 534 | printf("Quality: %02x AccShift: %d AccScale: %d\r\n", cfg, accShift, accScale); |
lpeters | 33:e47306c32791 | 535 | |
lpeters | 35:73b3963c6dd3 | 536 | time_t tNow; |
lpeters | 35:73b3963c6dd3 | 537 | /* |
lpeters | 35:73b3963c6dd3 | 538 | // while(1) |
lpeters | 35:73b3963c6dd3 | 539 | // { |
lpeters | 35:73b3963c6dd3 | 540 | tNow = time(NULL); |
lpeters | 35:73b3963c6dd3 | 541 | printf("RTC: %08X\r\n", tNow); |
lpeters | 34:341fb423e74b | 542 | #if defined(SENSOR_TEMP) |
lpeters | 34:341fb423e74b | 543 | tmpRead(); |
lpeters | 34:341fb423e74b | 544 | #endif |
lpeters | 34:341fb423e74b | 545 | magRead(); |
lpeters | 34:341fb423e74b | 546 | accRead(); |
lpeters | 34:341fb423e74b | 547 | gpsRead(); |
lpeters | 35:73b3963c6dd3 | 548 | printf("TIM: %d, SAT: %d, LAT: %f, LON: %f\r\n", mytime, mybatt, mylat, mylon); |
lpeters | 35:73b3963c6dd3 | 549 | printf("IRQ: A=%02X M=%02X B=%02X\r\n", accHFire, magHFire, btnHFire); |
lpeters | 35:73b3963c6dd3 | 550 | wait(1); |
lpeters | 35:73b3963c6dd3 | 551 | // } |
lpeters | 35:73b3963c6dd3 | 552 | */ |
lpeters | 35:73b3963c6dd3 | 553 | |
lpeters | 35:73b3963c6dd3 | 554 | for (int i = 0; i <= 64; i++) |
lpeters | 35:73b3963c6dd3 | 555 | { |
lpeters | 35:73b3963c6dd3 | 556 | myLedR = i & 0x01; |
lpeters | 35:73b3963c6dd3 | 557 | myLedG = i & 0x02; |
lpeters | 35:73b3963c6dd3 | 558 | myLedB = i & 0x04; |
lpeters | 35:73b3963c6dd3 | 559 | myLedW = i & 0x08; |
lpeters | 35:73b3963c6dd3 | 560 | wait(0.01); |
lpeters | 33:e47306c32791 | 561 | } |
lpeters | 33:e47306c32791 | 562 | |
Huot87 | 38:fd7a407bfe3e | 563 | // uint8_t crcA = 0; |
Huot87 | 38:fd7a407bfe3e | 564 | // uint8_t crcB = 0; |
Huot87 | 38:fd7a407bfe3e | 565 | // for(int i = 2; i <= 13; i++) |
Huot87 | 38:fd7a407bfe3e | 566 | // { |
Huot87 | 38:fd7a407bfe3e | 567 | // crcA = crcA + cmdStopGNSS[i]; |
Huot87 | 38:fd7a407bfe3e | 568 | // crcB = crcB + crcA; |
Huot87 | 38:fd7a407bfe3e | 569 | // } |
Huot87 | 38:fd7a407bfe3e | 570 | // cmdStopGNSS[14] = crcA; |
Huot87 | 38:fd7a407bfe3e | 571 | // cmdStopGNSS[15] = crcB; |
Huot87 | 38:fd7a407bfe3e | 572 | // printf("UBX CRC: %02X %02X\r\n", crcA, crcB); |
Huot87 | 38:fd7a407bfe3e | 573 | // i2c.write(NEOM8M_ADR_GPS, cmdStopGNSS, 16); |
Huot87 | 38:fd7a407bfe3e | 574 | // ubxRead(); |
Huot87 | 38:fd7a407bfe3e | 575 | // ubxRead(); |
Huot87 | 38:fd7a407bfe3e | 576 | // ubxRead(); |
Huot87 | 38:fd7a407bfe3e | 577 | // wait(1); |
Huot87 | 38:fd7a407bfe3e | 578 | // ubxRead(); |
Huot87 | 38:fd7a407bfe3e | 579 | // return 0; |
Huot87 | 38:fd7a407bfe3e | 580 | |
Huot87 | 39:6da6cb44f22e | 581 | // HUOT ADDITION -- 08/30/2018 -- MAIN LOOP TO TEST GPS SLEEP/WAKE FUNCTIONALITY -- GOT IT WORKING |
Huot87 | 39:6da6cb44f22e | 582 | int x = 0; |
Huot87 | 40:eaf476b94684 | 583 | |
Huot87 | 40:eaf476b94684 | 584 | // printf("LPM INIT \r\n"); |
Huot87 | 40:eaf476b94684 | 585 | // gpsExecuteCommand(lowPowerMode, sizeof(lowPowerMode)); |
Huot87 | 40:eaf476b94684 | 586 | // |
Huot87 | 38:fd7a407bfe3e | 587 | printf("Putting to sleep... Good Luck ================ \r\n"); |
Huot87 | 38:fd7a407bfe3e | 588 | gpsExecuteCommand(secondTimeSleep, sizeof(secondTimeSleep)); |
Huot87 | 38:fd7a407bfe3e | 589 | |
Huot87 | 40:eaf476b94684 | 590 | // while(1) |
Huot87 | 40:eaf476b94684 | 591 | // { |
Huot87 | 40:eaf476b94684 | 592 | // if ((x != 0) && (x % 10 == 0)) |
Huot87 | 40:eaf476b94684 | 593 | // { |
Huot87 | 40:eaf476b94684 | 594 | // printf("Waking up GPS... Good luck, Huot \r\n"); |
Huot87 | 40:eaf476b94684 | 595 | // gpsExecuteCommand(secondTimeAwake, sizeof(secondTimeAwake)); |
Huot87 | 40:eaf476b94684 | 596 | // |
Huot87 | 40:eaf476b94684 | 597 | // while (gpsRead() == 1); |
Huot87 | 40:eaf476b94684 | 598 | // |
Huot87 | 40:eaf476b94684 | 599 | // printf("GPS Fix is good! Going to sleep... \r\n"); |
Huot87 | 40:eaf476b94684 | 600 | // gpsExecuteCommand(secondTimeSleep, sizeof(secondTimeSleep)); |
Huot87 | 40:eaf476b94684 | 601 | // } |
Huot87 | 40:eaf476b94684 | 602 | // printf("Waiting 5 seconds... \r\n"); |
Huot87 | 40:eaf476b94684 | 603 | // wait(5); |
Huot87 | 40:eaf476b94684 | 604 | // x++; |
Huot87 | 40:eaf476b94684 | 605 | // printf("=== New loop incoming === \r\n"); |
Huot87 | 40:eaf476b94684 | 606 | // } |
Huot87 | 38:fd7a407bfe3e | 607 | |
Huot87 | 43:c900cd8ea072 | 608 | // TEST SEND -- SPECTRUM ANALYZER |
Huot87 | 43:c900cd8ea072 | 609 | printf("Will send packet in 10 seconds... \r\n"); |
Huot87 | 43:c900cd8ea072 | 610 | |
Huot87 | 43:c900cd8ea072 | 611 | for (int i = 9; i > 0; i--) |
Huot87 | 43:c900cd8ea072 | 612 | { |
Huot87 | 43:c900cd8ea072 | 613 | wait(1); |
Huot87 | 43:c900cd8ea072 | 614 | printf("%d ... \r\n", i); |
Huot87 | 43:c900cd8ea072 | 615 | } |
Huot87 | 43:c900cd8ea072 | 616 | |
Huot87 | 43:c900cd8ea072 | 617 | wait(1); |
Huot87 | 43:c900cd8ea072 | 618 | myLedG = 1; |
Huot87 | 43:c900cd8ea072 | 619 | |
lpeters | 37:df69df1bcb1a | 620 | |
mbed_official | 0:7037ed05f54f | 621 | // Initialize LoRaWAN stack |
mbed_official | 2:dc95ac6d6d4e | 622 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 623 | printf("\r\n LoRa initialization failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 624 | return -1; |
mbed_official | 0:7037ed05f54f | 625 | } |
mbed_official | 0:7037ed05f54f | 626 | |
Huot87 | 43:c900cd8ea072 | 627 | myLedG = 0; |
Huot87 | 43:c900cd8ea072 | 628 | |
mbed_official | 0:7037ed05f54f | 629 | printf("\r\n Mbed LoRaWANStack initialized \r\n"); |
lpeters | 36:dcc6f89fa39a | 630 | printf("MBED_CONF_LORA_APP_PORT: %d\r\n", MBED_CONF_LORA_APP_PORT); |
lpeters | 36:dcc6f89fa39a | 631 | printf("MBED_CONF_LORA_DUTY_CYCLE_ON: %d\r\n", MBED_CONF_LORA_DUTY_CYCLE_ON); |
mbed_official | 0:7037ed05f54f | 632 | |
mbed_official | 0:7037ed05f54f | 633 | // prepare application callbacks |
mbed_official | 0:7037ed05f54f | 634 | callbacks.events = mbed::callback(lora_event_handler); |
mbed_official | 2:dc95ac6d6d4e | 635 | lorawan.add_app_callbacks(&callbacks); |
mbed_official | 0:7037ed05f54f | 636 | |
mbed_official | 0:7037ed05f54f | 637 | // Set number of retries in case of CONFIRMED messages |
mbed_official | 2:dc95ac6d6d4e | 638 | if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) |
mbed_official | 0:7037ed05f54f | 639 | != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 640 | printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); |
mbed_official | 0:7037ed05f54f | 641 | return -1; |
mbed_official | 0:7037ed05f54f | 642 | } |
mbed_official | 0:7037ed05f54f | 643 | |
mbed_official | 0:7037ed05f54f | 644 | printf("\r\n CONFIRMED message retries : %d \r\n", |
mbed_official | 0:7037ed05f54f | 645 | CONFIRMED_MSG_RETRY_COUNTER); |
mbed_official | 0:7037ed05f54f | 646 | |
mbed_official | 0:7037ed05f54f | 647 | // Enable adaptive data rate |
mbed_official | 2:dc95ac6d6d4e | 648 | if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 649 | printf("\r\n enable_adaptive_datarate failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 650 | return -1; |
mbed_official | 0:7037ed05f54f | 651 | } |
mbed_official | 0:7037ed05f54f | 652 | |
mbed_official | 0:7037ed05f54f | 653 | printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); |
mbed_official | 0:7037ed05f54f | 654 | |
mbed_official | 2:dc95ac6d6d4e | 655 | retcode = lorawan.connect(); |
lpeters | 33:e47306c32791 | 656 | |
mbed_official | 0:7037ed05f54f | 657 | if (retcode == LORAWAN_STATUS_OK || |
mbed_official | 0:7037ed05f54f | 658 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
mbed_official | 0:7037ed05f54f | 659 | } else { |
mbed_official | 0:7037ed05f54f | 660 | printf("\r\n Connection error, code = %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 661 | return -1; |
mbed_official | 0:7037ed05f54f | 662 | } |
mbed_official | 0:7037ed05f54f | 663 | |
mbed_official | 0:7037ed05f54f | 664 | printf("\r\n Connection - In Progress ...\r\n"); |
mbed_official | 0:7037ed05f54f | 665 | |
mbed_official | 0:7037ed05f54f | 666 | // make your event queue dispatching events forever |
lpeters | 36:dcc6f89fa39a | 667 | //ev_queue.dispatch_forever(); |
lpeters | 36:dcc6f89fa39a | 668 | ev_queue.dispatch(); |
lpeters | 36:dcc6f89fa39a | 669 | printf("\r\n- = - = - = - = - = - DISPATCH 1 COMPLETE - = - = - = - = - = -\r\n"); |
lpeters | 36:dcc6f89fa39a | 670 | /* |
lpeters | 36:dcc6f89fa39a | 671 | printf("CONTROL LOOP GOES HERE!\r\n"); |
lpeters | 36:dcc6f89fa39a | 672 | #if defined(TARGET_LRAT) |
lpeters | 36:dcc6f89fa39a | 673 | #else |
lpeters | 36:dcc6f89fa39a | 674 | for (int i = 0; i < 6; i++) |
lpeters | 36:dcc6f89fa39a | 675 | { |
lpeters | 36:dcc6f89fa39a | 676 | tNow = time(NULL); |
lpeters | 36:dcc6f89fa39a | 677 | printf("RTC: %08X\r\n", tNow); |
lpeters | 36:dcc6f89fa39a | 678 | tNext = tNow + 10; |
lpeters | 36:dcc6f89fa39a | 679 | printf("NXT: %08X\r\n", tNext); |
lpeters | 36:dcc6f89fa39a | 680 | // Clear any pending IRQs |
lpeters | 36:dcc6f89fa39a | 681 | cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M; |
lpeters | 36:dcc6f89fa39a | 682 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 36:dcc6f89fa39a | 683 | i2c.read(LSM303_ADR_MAG, &ret, 1); |
lpeters | 36:dcc6f89fa39a | 684 | while (time(NULL) < tNext) |
lpeters | 36:dcc6f89fa39a | 685 | { |
lpeters | 36:dcc6f89fa39a | 686 | wait(1); |
lpeters | 36:dcc6f89fa39a | 687 | } |
lpeters | 36:dcc6f89fa39a | 688 | printf("Timer #%d complete.\r\n", i); |
lpeters | 36:dcc6f89fa39a | 689 | } |
lpeters | 36:dcc6f89fa39a | 690 | #endif |
lpeters | 36:dcc6f89fa39a | 691 | */ |
lpeters | 36:dcc6f89fa39a | 692 | tSend = time(NULL) + 1; |
lpeters | 36:dcc6f89fa39a | 693 | while (1) { |
lpeters | 42:d59e50622292 | 694 | continueSending = true; |
lpeters | 36:dcc6f89fa39a | 695 | while (time(NULL) < tSend) |
lpeters | 36:dcc6f89fa39a | 696 | wait(0.1); |
lpeters | 37:df69df1bcb1a | 697 | if (tSend == 0) |
lpeters | 37:df69df1bcb1a | 698 | tCalm = time(NULL) + (DPANIC * NPANIC); |
lpeters | 37:df69df1bcb1a | 699 | tSend = time(NULL) + (time(NULL) < tCalm ? DPANIC : DCHILL); |
lpeters | 37:df69df1bcb1a | 700 | magWipeIrq(); |
lpeters | 42:d59e50622292 | 701 | printf("RTC: %08X\r\n", time(NULL)); |
lpeters | 36:dcc6f89fa39a | 702 | printf("IT'S PACKET TIME!\r\n"); |
lpeters | 42:d59e50622292 | 703 | // HUOT ADDITION -- 08/31/2018 -- Implementation of the packet |
lpeters | 42:d59e50622292 | 704 | if (eepromCounter != 0) |
lpeters | 42:d59e50622292 | 705 | { |
lpeters | 42:d59e50622292 | 706 | int backsize = eepromCounter; |
lpeters | 42:d59e50622292 | 707 | printf("BACKLOG LOOP, FLUSHING %d ENTRIES\r\n", backsize); |
lpeters | 42:d59e50622292 | 708 | for (int i = 0; i < backsize && continueSending; i++) |
lpeters | 42:d59e50622292 | 709 | { |
lpeters | 42:d59e50622292 | 710 | printf("BACKLOG LOOP %d of %d\r\n", i, backsize); |
lpeters | 42:d59e50622292 | 711 | wasEEPROM = true; |
lpeters | 42:d59e50622292 | 712 | ev_queue.call(send_backlog); |
lpeters | 42:d59e50622292 | 713 | ev_queue.dispatch(); |
lpeters | 42:d59e50622292 | 714 | printf("Backlog sent, waiting 5 secs.\r\n"); |
lpeters | 42:d59e50622292 | 715 | wait(5); |
lpeters | 42:d59e50622292 | 716 | printf("\r\n EEPROM Backlog Sent out!"); |
lpeters | 42:d59e50622292 | 717 | printf("\r\n %d bytes scheduled for transmission \r\n", retcode); |
lpeters | 42:d59e50622292 | 718 | memset(tx_buffer, 0, sizeof(tx_buffer)); |
lpeters | 42:d59e50622292 | 719 | } |
lpeters | 42:d59e50622292 | 720 | if (continueSending) |
lpeters | 42:d59e50622292 | 721 | printf("BACKLOG FLUSH COMPLETE!\r\n"); |
lpeters | 42:d59e50622292 | 722 | else |
lpeters | 42:d59e50622292 | 723 | printf("BACKLOG FLUSH ABORTED!\r\n"); |
lpeters | 42:d59e50622292 | 724 | } |
lpeters | 36:dcc6f89fa39a | 725 | ev_queue.call(send_message); |
lpeters | 36:dcc6f89fa39a | 726 | ev_queue.dispatch(); |
lpeters | 36:dcc6f89fa39a | 727 | printf("\r\n- = - = - = - = - = - DISPATCH 2 COMPLETE - = - = - = - = - = -\r\n"); |
lpeters | 36:dcc6f89fa39a | 728 | } |
mbed_official | 3:8c7198d1a2a1 | 729 | |
mbed_official | 3:8c7198d1a2a1 | 730 | return 0; |
mbed_official | 0:7037ed05f54f | 731 | } |
mbed_official | 0:7037ed05f54f | 732 | |
lpeters | 42:d59e50622292 | 733 | static void send_backlog() |
lpeters | 42:d59e50622292 | 734 | { |
lpeters | 42:d59e50622292 | 735 | printf("In send_backlog()...\r\n"); |
lpeters | 42:d59e50622292 | 736 | int16_t retcode; |
lpeters | 42:d59e50622292 | 737 | //time_t tNow = time(NULL); |
lpeters | 42:d59e50622292 | 738 | //printf("Clock: %d\r\n", tNow); |
lpeters | 42:d59e50622292 | 739 | weGetSignal = 0; |
lpeters | 42:d59e50622292 | 740 | printf("Head Cleared ACK flag: %d\r\n", weGetSignal); |
lpeters | 42:d59e50622292 | 741 | prepBacklogSend(); |
lpeters | 42:d59e50622292 | 742 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, PACKET_LEN, |
lpeters | 42:d59e50622292 | 743 | MSG_CONFIRMED_FLAG); |
lpeters | 42:d59e50622292 | 744 | if (retcode < 0) { |
lpeters | 42:d59e50622292 | 745 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") |
lpeters | 42:d59e50622292 | 746 | : printf("\r\n send() - Error code %d \r\n", retcode); |
lpeters | 42:d59e50622292 | 747 | if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { |
lpeters | 42:d59e50622292 | 748 | //retry in 3 seconds |
lpeters | 42:d59e50622292 | 749 | // HUOT NOTE -- WE SHOULD BE CAREFUL ABOUT THIS AS WITH THE EEPROM BACKLOG WE MAY BLOCK OURSELVES AND OVERFLOW THE STACK WITH SUBSEQUENT CALLS |
lpeters | 42:d59e50622292 | 750 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
lpeters | 42:d59e50622292 | 751 | ev_queue.call_in(3000, send_message); |
lpeters | 42:d59e50622292 | 752 | } |
lpeters | 42:d59e50622292 | 753 | } |
lpeters | 42:d59e50622292 | 754 | return; |
lpeters | 42:d59e50622292 | 755 | } |
lpeters | 42:d59e50622292 | 756 | printf("\r\n %d bytes scheduled for transmission \r\n", retcode); |
lpeters | 42:d59e50622292 | 757 | //memset(tx_buffer, 0, sizeof(tx_buffer)); |
Huot87 | 43:c900cd8ea072 | 758 | |
Huot87 | 43:c900cd8ea072 | 759 | |
lpeters | 42:d59e50622292 | 760 | //LED Confirmation Output - MESSAGE SENT |
Huot87 | 43:c900cd8ea072 | 761 | // for (int i = 0; i < 10; i++) { |
Huot87 | 43:c900cd8ea072 | 762 | // myLedG = 1; |
Huot87 | 43:c900cd8ea072 | 763 | // wait(0.1); |
Huot87 | 43:c900cd8ea072 | 764 | // myLedG = 0; |
Huot87 | 43:c900cd8ea072 | 765 | // myLedR = 1; |
Huot87 | 43:c900cd8ea072 | 766 | // wait(0.1); |
Huot87 | 43:c900cd8ea072 | 767 | // myLedR = 0; |
Huot87 | 43:c900cd8ea072 | 768 | // //myLedB = 1; |
Huot87 | 43:c900cd8ea072 | 769 | // //wait(0.1); |
Huot87 | 43:c900cd8ea072 | 770 | // //myLedB = 0; |
Huot87 | 43:c900cd8ea072 | 771 | // } |
lpeters | 42:d59e50622292 | 772 | } |
lpeters | 42:d59e50622292 | 773 | |
mbed_official | 0:7037ed05f54f | 774 | /** |
mbed_official | 0:7037ed05f54f | 775 | * Sends a message to the Network Server |
mbed_official | 0:7037ed05f54f | 776 | */ |
mbed_official | 0:7037ed05f54f | 777 | static void send_message() |
mbed_official | 0:7037ed05f54f | 778 | { |
lpeters | 36:dcc6f89fa39a | 779 | printf("In send_message()...\r\n"); |
mbed_official | 0:7037ed05f54f | 780 | int16_t retcode; |
mbed_official | 0:7037ed05f54f | 781 | |
Huot87 | 40:eaf476b94684 | 782 | // HUOT NOTE -- 08/31/2018 -- Think this and all references to the "Dummy Sensor" can be removed at this point |
Huot87 | 40:eaf476b94684 | 783 | // |
Huot87 | 40:eaf476b94684 | 784 | // float sensor_value; |
Huot87 | 40:eaf476b94684 | 785 | // if (ds1820.begin()) { |
Huot87 | 40:eaf476b94684 | 786 | // ds1820.startConversion(); |
Huot87 | 40:eaf476b94684 | 787 | // sensor_value = ds1820.read(); |
Huot87 | 40:eaf476b94684 | 788 | // printf("\r\n Dummy Sensor Value = %3.1f \r\n", sensor_value); |
Huot87 | 40:eaf476b94684 | 789 | // ds1820.startConversion(); |
Huot87 | 40:eaf476b94684 | 790 | // } else { |
Huot87 | 40:eaf476b94684 | 791 | // printf("\r\n No sensor found \r\n"); |
Huot87 | 40:eaf476b94684 | 792 | // return; |
Huot87 | 40:eaf476b94684 | 793 | // } |
mbed_official | 0:7037ed05f54f | 794 | |
lpeters | 42:d59e50622292 | 795 | //time_t tNow = time(NULL); |
lpeters | 42:d59e50622292 | 796 | //printf("Clock: %d\r\n", tNow); |
lpeters | 34:341fb423e74b | 797 | |
lpeters | 42:d59e50622292 | 798 | weGetSignal = 0; |
lpeters | 42:d59e50622292 | 799 | printf("Head Cleared ACK flag: %d\r\n", weGetSignal); |
lpeters | 34:341fb423e74b | 800 | |
Huot87 | 40:eaf476b94684 | 801 | #if defined(SENSOR_TEMP) |
Huot87 | 40:eaf476b94684 | 802 | tmpRead(); |
Huot87 | 40:eaf476b94684 | 803 | #endif |
Huot87 | 40:eaf476b94684 | 804 | magRead(); |
Huot87 | 40:eaf476b94684 | 805 | accRead(); |
Huot87 | 40:eaf476b94684 | 806 | |
Huot87 | 40:eaf476b94684 | 807 | // HUOT ALTERATION -- 08/31/2018 -- Implement GPS Read Loop into Send Message |
Huot87 | 40:eaf476b94684 | 808 | // TODO - Implement 3 try counter (or something similar) |
Huot87 | 40:eaf476b94684 | 809 | printf("Waking up GPS... Good luck, Huot \r\n"); |
Huot87 | 40:eaf476b94684 | 810 | gpsExecuteCommand(secondTimeAwake, sizeof(secondTimeAwake)); |
Huot87 | 40:eaf476b94684 | 811 | |
Huot87 | 43:c900cd8ea072 | 812 | //while (gpsRead() == 1); |
Huot87 | 43:c900cd8ea072 | 813 | gpsRead(); |
Huot87 | 40:eaf476b94684 | 814 | |
Huot87 | 40:eaf476b94684 | 815 | printf("GPS Fix is good! Going to sleep... \r\n"); |
Huot87 | 40:eaf476b94684 | 816 | gpsExecuteCommand(secondTimeSleep, sizeof(secondTimeSleep)); |
Huot87 | 40:eaf476b94684 | 817 | printf("~~~~~~~~~~~~~ Constructing Packet ~~~~~~~~~~~~~\r\n"); |
Huot87 | 40:eaf476b94684 | 818 | |
Huot87 | 40:eaf476b94684 | 819 | int ilat = (int)(mylat * 100000); |
Huot87 | 40:eaf476b94684 | 820 | int ilon = (int)(mylon * 100000); |
Huot87 | 40:eaf476b94684 | 821 | printf("TIM: %d, SAT: %d, LAT: %d, LON: %d\r\n", mytime, mybatt, ilat, ilon); |
Huot87 | 40:eaf476b94684 | 822 | // packet_len = 11; |
Huot87 | 40:eaf476b94684 | 823 | tx_buffer[0] = (mytime >> 24) & 0xFF; |
Huot87 | 40:eaf476b94684 | 824 | tx_buffer[1] = (mytime >> 16) & 0xFF; |
Huot87 | 40:eaf476b94684 | 825 | tx_buffer[2] = (mytime >> 8) & 0xFF; |
Huot87 | 40:eaf476b94684 | 826 | tx_buffer[3] = (mytime >> 0) & 0xFF; |
Huot87 | 40:eaf476b94684 | 827 | tx_buffer[4] = ((mybatt << 4) & 0xF0) | ((ilat >> 22) & 0x0F); |
Huot87 | 40:eaf476b94684 | 828 | tx_buffer[5] = (ilat >> 14) & 0xFF; |
Huot87 | 40:eaf476b94684 | 829 | tx_buffer[6] = (ilat >> 6) & 0xFF; |
Huot87 | 40:eaf476b94684 | 830 | tx_buffer[7] = ((ilat << 2) & 0xFC) | ((ilon >> 24) & 0x03); |
Huot87 | 40:eaf476b94684 | 831 | tx_buffer[8] = (ilon >> 16) & 0xFF; |
Huot87 | 40:eaf476b94684 | 832 | tx_buffer[9] = (ilon >> 8) & 0xFF; |
Huot87 | 40:eaf476b94684 | 833 | tx_buffer[10] = (ilon >> 0) & 0xFF; |
Huot87 | 40:eaf476b94684 | 834 | printf("\r\nBUF: |"); |
Huot87 | 40:eaf476b94684 | 835 | for (int i = 0; i < PACKET_LEN; i++) { printf("%02X", tx_buffer[i]); } |
Huot87 | 40:eaf476b94684 | 836 | printf("|\r\n"); |
Huot87 | 43:c900cd8ea072 | 837 | |
Huot87 | 43:c900cd8ea072 | 838 | // TEST SEND -- SPECTRUM ANALYZER |
Huot87 | 43:c900cd8ea072 | 839 | printf("Will send packet in 10 seconds... \r\n"); |
Huot87 | 43:c900cd8ea072 | 840 | |
Huot87 | 43:c900cd8ea072 | 841 | for (int i = 9; i > 0; i--) |
Huot87 | 43:c900cd8ea072 | 842 | { |
Huot87 | 43:c900cd8ea072 | 843 | wait(1); |
Huot87 | 43:c900cd8ea072 | 844 | printf("%d ... \r\n", i); |
Huot87 | 43:c900cd8ea072 | 845 | } |
Huot87 | 43:c900cd8ea072 | 846 | wait(1); |
Huot87 | 43:c900cd8ea072 | 847 | myLedG = 1; |
Huot87 | 43:c900cd8ea072 | 848 | |
Huot87 | 40:eaf476b94684 | 849 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, PACKET_LEN, |
Huot87 | 40:eaf476b94684 | 850 | MSG_CONFIRMED_FLAG); |
Huot87 | 40:eaf476b94684 | 851 | |
Huot87 | 40:eaf476b94684 | 852 | if (retcode < 0) { |
Huot87 | 40:eaf476b94684 | 853 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") |
Huot87 | 40:eaf476b94684 | 854 | : printf("\r\n send() - Error code %d \r\n", retcode); |
Huot87 | 40:eaf476b94684 | 855 | |
Huot87 | 40:eaf476b94684 | 856 | if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { |
Huot87 | 40:eaf476b94684 | 857 | //retry in 3 seconds |
Huot87 | 40:eaf476b94684 | 858 | // HUOT NOTE -- WE SHOULD BE CAREFUL ABOUT THIS AS WITH THE EEPROM BACKLOG WE MAY BLOCK OURSELVES AND OVERFLOW THE STACK WITH SUBSEQUENT CALLS |
Huot87 | 40:eaf476b94684 | 859 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
Huot87 | 40:eaf476b94684 | 860 | ev_queue.call_in(3000, send_message); |
Huot87 | 40:eaf476b94684 | 861 | } |
lpeters | 34:341fb423e74b | 862 | } |
Huot87 | 40:eaf476b94684 | 863 | return; |
lpeters | 34:341fb423e74b | 864 | } |
lpeters | 34:341fb423e74b | 865 | |
Huot87 | 40:eaf476b94684 | 866 | printf("\r\n %d bytes scheduled for transmission \r\n", retcode); |
lpeters | 42:d59e50622292 | 867 | //memset(tx_buffer, 0, sizeof(tx_buffer)); |
Huot87 | 40:eaf476b94684 | 868 | |
Huot87 | 43:c900cd8ea072 | 869 | // //LED Confirmation Output - MESSAGE SENT |
Huot87 | 43:c900cd8ea072 | 870 | // for (int i = 0; i < 10; i++) { |
Huot87 | 43:c900cd8ea072 | 871 | // myLedG = 1; |
Huot87 | 43:c900cd8ea072 | 872 | // wait(0.1); |
Huot87 | 43:c900cd8ea072 | 873 | // myLedG = 0; |
Huot87 | 43:c900cd8ea072 | 874 | // myLedR = 1; |
Huot87 | 43:c900cd8ea072 | 875 | // wait(0.1); |
Huot87 | 43:c900cd8ea072 | 876 | // myLedR = 0; |
Huot87 | 43:c900cd8ea072 | 877 | // myLedB = 1; |
Huot87 | 43:c900cd8ea072 | 878 | // wait(0.1); |
Huot87 | 43:c900cd8ea072 | 879 | // myLedB = 0; |
Huot87 | 43:c900cd8ea072 | 880 | // } |
lpeters | 34:341fb423e74b | 881 | } |
lpeters | 34:341fb423e74b | 882 | |
lpeters | 34:341fb423e74b | 883 | /** |
lpeters | 34:341fb423e74b | 884 | * Receive a message from the Network Server |
lpeters | 34:341fb423e74b | 885 | */ |
lpeters | 34:341fb423e74b | 886 | static void receive_message() |
lpeters | 34:341fb423e74b | 887 | { |
lpeters | 36:dcc6f89fa39a | 888 | printf("In receive_message()...\r\n"); |
lpeters | 34:341fb423e74b | 889 | int16_t retcode; |
lpeters | 34:341fb423e74b | 890 | retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer, |
lpeters | 34:341fb423e74b | 891 | sizeof(rx_buffer), |
lpeters | 34:341fb423e74b | 892 | MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG); |
lpeters | 34:341fb423e74b | 893 | |
lpeters | 34:341fb423e74b | 894 | if (retcode < 0) { |
lpeters | 34:341fb423e74b | 895 | printf("\r\n receive() - Error code %d \r\n", retcode); |
lpeters | 34:341fb423e74b | 896 | return; |
lpeters | 34:341fb423e74b | 897 | } |
lpeters | 34:341fb423e74b | 898 | |
lpeters | 34:341fb423e74b | 899 | printf(" Data:"); |
lpeters | 34:341fb423e74b | 900 | |
lpeters | 34:341fb423e74b | 901 | for (uint8_t i = 0; i < retcode; i++) { |
lpeters | 34:341fb423e74b | 902 | printf("%x", rx_buffer[i]); |
lpeters | 34:341fb423e74b | 903 | } |
lpeters | 34:341fb423e74b | 904 | |
lpeters | 34:341fb423e74b | 905 | printf("\r\n Data Length: %d\r\n", retcode); |
lpeters | 34:341fb423e74b | 906 | |
lpeters | 34:341fb423e74b | 907 | /* |
lpeters | 34:341fb423e74b | 908 | int startLoop = 0; |
lpeters | 34:341fb423e74b | 909 | if (strncmp((char *)rx_buffer, cmdSendLoop, 8) == 0) |
lpeters | 34:341fb423e74b | 910 | { |
lpeters | 34:341fb423e74b | 911 | printf("SendLoop Command Received!\r\n"); |
lpeters | 34:341fb423e74b | 912 | startLoop = 1; |
lpeters | 34:341fb423e74b | 913 | } |
lpeters | 34:341fb423e74b | 914 | */ |
lpeters | 34:341fb423e74b | 915 | |
lpeters | 34:341fb423e74b | 916 | memset(rx_buffer, 0, sizeof(rx_buffer)); |
lpeters | 34:341fb423e74b | 917 | /* |
lpeters | 34:341fb423e74b | 918 | if (startLoop) |
lpeters | 34:341fb423e74b | 919 | send_message(); |
lpeters | 34:341fb423e74b | 920 | */ |
lpeters | 34:341fb423e74b | 921 | } |
lpeters | 34:341fb423e74b | 922 | |
lpeters | 34:341fb423e74b | 923 | /** |
lpeters | 34:341fb423e74b | 924 | * Event handler |
lpeters | 34:341fb423e74b | 925 | */ |
lpeters | 34:341fb423e74b | 926 | static void lora_event_handler(lorawan_event_t event) |
lpeters | 34:341fb423e74b | 927 | { |
lpeters | 34:341fb423e74b | 928 | tr_debug("In lora_event_handler(%d)...", event); |
lpeters | 34:341fb423e74b | 929 | switch (event) { |
lpeters | 34:341fb423e74b | 930 | case CONNECTED: |
lpeters | 34:341fb423e74b | 931 | printf("\r\n Connection - Successful \r\n"); |
lpeters | 36:dcc6f89fa39a | 932 | ev_queue.break_dispatch(); |
lpeters | 36:dcc6f89fa39a | 933 | /* |
lpeters | 34:341fb423e74b | 934 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
lpeters | 34:341fb423e74b | 935 | send_message(); |
lpeters | 34:341fb423e74b | 936 | } else { |
lpeters | 34:341fb423e74b | 937 | ev_queue.call_every(TX_TIMER, send_message); |
lpeters | 34:341fb423e74b | 938 | } |
lpeters | 36:dcc6f89fa39a | 939 | */ |
lpeters | 34:341fb423e74b | 940 | break; |
lpeters | 34:341fb423e74b | 941 | case DISCONNECTED: |
lpeters | 34:341fb423e74b | 942 | ev_queue.break_dispatch(); |
lpeters | 34:341fb423e74b | 943 | printf("\r\n Disconnected Successfully \r\n"); |
lpeters | 34:341fb423e74b | 944 | break; |
lpeters | 34:341fb423e74b | 945 | case TX_DONE: |
lpeters | 34:341fb423e74b | 946 | printf("\r\n Message Sent to Network Server \r\n"); |
lpeters | 42:d59e50622292 | 947 | printf("Dumping ACK Flag: %d\r\n", weGetSignal); |
lpeters | 42:d59e50622292 | 948 | if (weGetSignal == 0) |
lpeters | 42:d59e50622292 | 949 | { |
lpeters | 42:d59e50622292 | 950 | printf("No ACK received.\r\n"); |
lpeters | 42:d59e50622292 | 951 | if (wasEEPROM) |
lpeters | 42:d59e50622292 | 952 | continueSending = false; |
lpeters | 42:d59e50622292 | 953 | else |
lpeters | 42:d59e50622292 | 954 | backupPacket(); |
lpeters | 42:d59e50622292 | 955 | } |
lpeters | 42:d59e50622292 | 956 | if (wasEEPROM && weGetSignal) |
lpeters | 42:d59e50622292 | 957 | { |
lpeters | 42:d59e50622292 | 958 | eepromCounter--; |
lpeters | 42:d59e50622292 | 959 | } |
lpeters | 42:d59e50622292 | 960 | wasEEPROM = false; |
lpeters | 42:d59e50622292 | 961 | weGetSignal = 0; |
lpeters | 42:d59e50622292 | 962 | printf("Tail Cleared ACK Flag: %d\r\n", weGetSignal); |
lpeters | 36:dcc6f89fa39a | 963 | ev_queue.break_dispatch(); |
lpeters | 36:dcc6f89fa39a | 964 | /* |
lpeters | 34:341fb423e74b | 965 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
lpeters | 34:341fb423e74b | 966 | send_message(); |
lpeters | 34:341fb423e74b | 967 | } |
lpeters | 36:dcc6f89fa39a | 968 | */ |
Huot87 | 43:c900cd8ea072 | 969 | |
Huot87 | 43:c900cd8ea072 | 970 | myLedG = 0; |
Huot87 | 43:c900cd8ea072 | 971 | |
lpeters | 34:341fb423e74b | 972 | break; |
lpeters | 34:341fb423e74b | 973 | case TX_TIMEOUT: |
lpeters | 34:341fb423e74b | 974 | case TX_ERROR: |
lpeters | 34:341fb423e74b | 975 | case TX_CRYPTO_ERROR: |
lpeters | 34:341fb423e74b | 976 | case TX_SCHEDULING_ERROR: |
Huot87 | 43:c900cd8ea072 | 977 | myLedR = 1; |
Huot87 | 43:c900cd8ea072 | 978 | wait(1); |
Huot87 | 43:c900cd8ea072 | 979 | |
lpeters | 34:341fb423e74b | 980 | printf("\r\n Transmission Error - EventCode = %d \r\n", event); |
Huot87 | 40:eaf476b94684 | 981 | |
lpeters | 34:341fb423e74b | 982 | // try again |
lpeters | 36:dcc6f89fa39a | 983 | /* |
lpeters | 34:341fb423e74b | 984 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
lpeters | 34:341fb423e74b | 985 | send_message(); |
lpeters | 34:341fb423e74b | 986 | } |
lpeters | 36:dcc6f89fa39a | 987 | */ |
Huot87 | 40:eaf476b94684 | 988 | printf("====== Writing to EEPROM Backlog ====== \r\n"); |
Huot87 | 40:eaf476b94684 | 989 | backupPacket(); |
Huot87 | 40:eaf476b94684 | 990 | |
Huot87 | 40:eaf476b94684 | 991 | if (wasEEPROM) |
Huot87 | 40:eaf476b94684 | 992 | continueSending = false; |
Huot87 | 40:eaf476b94684 | 993 | |
Huot87 | 43:c900cd8ea072 | 994 | myLedG = 0; |
Huot87 | 43:c900cd8ea072 | 995 | myLedR = 0; |
Huot87 | 43:c900cd8ea072 | 996 | |
lpeters | 34:341fb423e74b | 997 | break; |
lpeters | 34:341fb423e74b | 998 | case RX_DONE: |
lpeters | 34:341fb423e74b | 999 | printf("\r\n Received message from Network Server \r\n"); |
lpeters | 34:341fb423e74b | 1000 | receive_message(); |
lpeters | 34:341fb423e74b | 1001 | break; |
lpeters | 34:341fb423e74b | 1002 | case RX_TIMEOUT: |
lpeters | 34:341fb423e74b | 1003 | case RX_ERROR: |
lpeters | 34:341fb423e74b | 1004 | printf("\r\n Error in reception - Code = %d \r\n", event); |
lpeters | 34:341fb423e74b | 1005 | break; |
lpeters | 34:341fb423e74b | 1006 | case JOIN_FAILURE: |
lpeters | 34:341fb423e74b | 1007 | printf("\r\n OTAA Failed - Check Keys \r\n"); |
lpeters | 34:341fb423e74b | 1008 | break; |
lpeters | 34:341fb423e74b | 1009 | case UPLINK_REQUIRED: |
lpeters | 34:341fb423e74b | 1010 | printf("\r\n Uplink required by NS \r\n"); |
lpeters | 36:dcc6f89fa39a | 1011 | /* |
lpeters | 34:341fb423e74b | 1012 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
lpeters | 34:341fb423e74b | 1013 | send_message(); |
lpeters | 34:341fb423e74b | 1014 | } |
lpeters | 36:dcc6f89fa39a | 1015 | */ |
lpeters | 34:341fb423e74b | 1016 | break; |
lpeters | 34:341fb423e74b | 1017 | default: |
lpeters | 34:341fb423e74b | 1018 | MBED_ASSERT("Unknown Event"); |
lpeters | 34:341fb423e74b | 1019 | } |
lpeters | 34:341fb423e74b | 1020 | } |
lpeters | 34:341fb423e74b | 1021 | |
lpeters | 34:341fb423e74b | 1022 | void magInitSequence() |
lpeters | 34:341fb423e74b | 1023 | { |
lpeters | 35:73b3963c6dd3 | 1024 | printf("In magInitSequence()...\r\n"); |
lpeters | 34:341fb423e74b | 1025 | myLedR = 0; |
lpeters | 34:341fb423e74b | 1026 | myLedG = 0; |
lpeters | 34:341fb423e74b | 1027 | cmd[0] = LSM303_REG_MAG_WHO_AM_I_M; |
lpeters | 34:341fb423e74b | 1028 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1029 | i2c.read(LSM303_ADR_MAG, &ret, 1); |
lpeters | 34:341fb423e74b | 1030 | res = (ret == LSM303_WHO_MAG ? sPass : sFail); |
lpeters | 34:341fb423e74b | 1031 | printf("MAG WhoAmI: %02X %s\r\n", ret, res); |
lpeters | 34:341fb423e74b | 1032 | |
lpeters | 34:341fb423e74b | 1033 | if (ret == LSM303_WHO_MAG) |
lpeters | 34:341fb423e74b | 1034 | myLedG = 1; |
lpeters | 34:341fb423e74b | 1035 | else |
lpeters | 34:341fb423e74b | 1036 | myLedR = 1; |
lpeters | 34:341fb423e74b | 1037 | |
lpeters | 34:341fb423e74b | 1038 | for (int i = 0; i < 2; i++) { |
lpeters | 34:341fb423e74b | 1039 | myLedB = 1; |
lpeters | 34:341fb423e74b | 1040 | wait(0.3); |
lpeters | 34:341fb423e74b | 1041 | myLedB = 0; |
lpeters | 34:341fb423e74b | 1042 | wait(0.3); |
lpeters | 34:341fb423e74b | 1043 | } |
lpeters | 34:341fb423e74b | 1044 | |
lpeters | 34:341fb423e74b | 1045 | #if defined(TARGET_LRAT) |
lpeters | 34:341fb423e74b | 1046 | cmd[0] = LSM303_REG_MAG_CTRL_REG1_M; |
lpeters | 34:341fb423e74b | 1047 | cmd[1] = 0x70; // Ultra-High Performance Mode on XY axes, ODR=10Hz |
lpeters | 34:341fb423e74b | 1048 | i2c.write(LSM303_ADR_MAG, cmd, 2); |
lpeters | 34:341fb423e74b | 1049 | cmd[0] = LSM303_REG_MAG_CTRL_REG3_M; |
lpeters | 34:341fb423e74b | 1050 | cmd[1] = 0x00; // High Resolution? (Full-power), Continuous |
lpeters | 34:341fb423e74b | 1051 | i2c.write(LSM303_ADR_MAG, cmd, 2); |
lpeters | 34:341fb423e74b | 1052 | cmd[0] = LSM303_REG_MAG_CTRL_REG4_M; |
lpeters | 34:341fb423e74b | 1053 | cmd[1] = 0x0C; // Ultra-High Performance Mode on Z axis |
lpeters | 34:341fb423e74b | 1054 | i2c.write(LSM303_ADR_MAG, cmd, 2); |
lpeters | 34:341fb423e74b | 1055 | #if defined(SENSOR_TEMP) |
lpeters | 34:341fb423e74b | 1056 | // Enable Temp Sensor |
lpeters | 34:341fb423e74b | 1057 | cmd[0] = LSM303_REG_MAG_CTRL_REG1_M; |
lpeters | 34:341fb423e74b | 1058 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1059 | i2c.read(LSM303_ADR_MAG, &ret, 1); |
lpeters | 34:341fb423e74b | 1060 | cmd[0] = LSM303_REG_MAG_CTRL_REG1_M; |
lpeters | 34:341fb423e74b | 1061 | cmd[1] = ret | 0x80; |
lpeters | 34:341fb423e74b | 1062 | i2c.write(LSM303_ADR_MAG, cmd, 2); |
lpeters | 34:341fb423e74b | 1063 | /* |
lpeters | 34:341fb423e74b | 1064 | cmd[0] = LSM303_REG_MAG_CTRL_REG5_M; |
lpeters | 34:341fb423e74b | 1065 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1066 | i2c.read(LSM303_ADR_MAG, &ret, 1); |
lpeters | 34:341fb423e74b | 1067 | cmd[0] = LSM303_REG_MAG_CTRL_REG5_M; |
lpeters | 34:341fb423e74b | 1068 | cmd[1] = ret | 0x40; |
lpeters | 34:341fb423e74b | 1069 | i2c.write(LSM303_ADR_MAG, cmd, 2); |
lpeters | 34:341fb423e74b | 1070 | */ |
lpeters | 34:341fb423e74b | 1071 | #endif |
lpeters | 34:341fb423e74b | 1072 | #else |
lpeters | 34:341fb423e74b | 1073 | cmd[0] = LSM303_REG_MAG_CFG_REG_A_M; |
lpeters | 34:341fb423e74b | 1074 | cmd[1] = 0x00; // Mag = 10 Hz (high-resolution and continuous mode) |
lpeters | 34:341fb423e74b | 1075 | i2c.write(LSM303_ADR_MAG, cmd, 2); |
lpeters | 34:341fb423e74b | 1076 | cmd[0] = LSM303_REG_MAG_CFG_REG_C_M; |
lpeters | 34:341fb423e74b | 1077 | //cmd[1] = 0x01; // Mag data-ready interrupt enable |
lpeters | 34:341fb423e74b | 1078 | cmd[1] = 0x40; // Mag enable interrupt on pin |
lpeters | 34:341fb423e74b | 1079 | i2c.write(LSM303_ADR_MAG, cmd, 2); |
lpeters | 34:341fb423e74b | 1080 | #endif |
lpeters | 34:341fb423e74b | 1081 | |
lpeters | 34:341fb423e74b | 1082 | for (int i = 0; i < 2; i++) { |
lpeters | 34:341fb423e74b | 1083 | myLedR = 1; |
lpeters | 34:341fb423e74b | 1084 | myLedG = 1; |
lpeters | 34:341fb423e74b | 1085 | myLedB = 1; |
lpeters | 34:341fb423e74b | 1086 | wait(0.5); |
lpeters | 34:341fb423e74b | 1087 | myLedR = 0; |
lpeters | 34:341fb423e74b | 1088 | myLedG = 0; |
lpeters | 34:341fb423e74b | 1089 | myLedB = 0; |
lpeters | 34:341fb423e74b | 1090 | wait(0.5); |
lpeters | 34:341fb423e74b | 1091 | } |
lpeters | 35:73b3963c6dd3 | 1092 | #if defined(TARGET_LRAT) |
lpeters | 35:73b3963c6dd3 | 1093 | // LRAT MAG IRQ SETUP GOES HERE |
lpeters | 35:73b3963c6dd3 | 1094 | #else |
lpeters | 34:341fb423e74b | 1095 | // MAG INTERRUPT SETUP |
lpeters | 34:341fb423e74b | 1096 | cmd[0] = LSM303_REG_MAG_INT_THS_L_REG_M; |
lpeters | 35:73b3963c6dd3 | 1097 | cmd[1] = 0xE8; |
lpeters | 34:341fb423e74b | 1098 | i2c.write(LSM303_ADR_MAG, cmd, 2); |
lpeters | 34:341fb423e74b | 1099 | cmd[0] = LSM303_REG_MAG_INT_THS_H_REG_M; |
lpeters | 35:73b3963c6dd3 | 1100 | cmd[1] = 0x03; |
lpeters | 34:341fb423e74b | 1101 | i2c.write(LSM303_ADR_MAG, cmd, 2); |
lpeters | 34:341fb423e74b | 1102 | cmd[0] = LSM303_REG_MAG_INT_CTRL_REG_M; |
lpeters | 34:341fb423e74b | 1103 | cmd[1] = 0xE7; |
lpeters | 34:341fb423e74b | 1104 | i2c.write(LSM303_ADR_MAG, cmd, 2); |
lpeters | 34:341fb423e74b | 1105 | magPin.rise(&onMagIrq); |
lpeters | 35:73b3963c6dd3 | 1106 | #endif |
lpeters | 34:341fb423e74b | 1107 | } |
lpeters | 34:341fb423e74b | 1108 | |
lpeters | 34:341fb423e74b | 1109 | void accInitSequence() |
lpeters | 34:341fb423e74b | 1110 | { |
lpeters | 35:73b3963c6dd3 | 1111 | printf("In accInitSequence()...\r\n"); |
lpeters | 34:341fb423e74b | 1112 | myLedR = 0; |
lpeters | 34:341fb423e74b | 1113 | myLedG = 0; |
lpeters | 34:341fb423e74b | 1114 | cmd[0] = LSM303_REG_ACC_WHO_AM_I_A; |
lpeters | 34:341fb423e74b | 1115 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1116 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 34:341fb423e74b | 1117 | res = (ret == LSM303_WHO_ACC ? sPass : sFail); |
lpeters | 34:341fb423e74b | 1118 | printf("ACC WhoAmI: %02X %s\r\n", ret, res); |
lpeters | 34:341fb423e74b | 1119 | |
lpeters | 34:341fb423e74b | 1120 | if (ret == LSM303_WHO_ACC) |
lpeters | 34:341fb423e74b | 1121 | myLedG = 1; |
lpeters | 34:341fb423e74b | 1122 | else |
lpeters | 34:341fb423e74b | 1123 | myLedR = 1; |
lpeters | 34:341fb423e74b | 1124 | |
lpeters | 34:341fb423e74b | 1125 | for (int i = 0; i < 2; i++) { |
lpeters | 34:341fb423e74b | 1126 | myLedB = 1; |
lpeters | 34:341fb423e74b | 1127 | wait(0.3); |
lpeters | 34:341fb423e74b | 1128 | myLedB = 0; |
lpeters | 34:341fb423e74b | 1129 | wait(0.3); |
lpeters | 34:341fb423e74b | 1130 | } |
lpeters | 34:341fb423e74b | 1131 | |
lpeters | 34:341fb423e74b | 1132 | #if defined(TARGET_LRAT) |
lpeters | 34:341fb423e74b | 1133 | cmd[0] = LSM303_REG_ACC_CTRL_REG1_A; |
lpeters | 34:341fb423e74b | 1134 | cmd[1] = 0xB7; // High Resolution, ODR=100Hz, Enable XYZ Axes |
lpeters | 34:341fb423e74b | 1135 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
Huot87 | 39:6da6cb44f22e | 1136 | |
Huot87 | 39:6da6cb44f22e | 1137 | // HUOT ADDITION -- 08/30/2018 -- Accelerometer Intterupt Init Sequence |
Huot87 | 39:6da6cb44f22e | 1138 | #if defined(ACC_INTERRUPT_ON) |
Huot87 | 39:6da6cb44f22e | 1139 | // ACC INTERRUPT SETUP |
Huot87 | 39:6da6cb44f22e | 1140 | // Enable Interrupt Pin |
Huot87 | 39:6da6cb44f22e | 1141 | cmd[0] = LSM303_REG_ACC_CTRL_REG3_A; |
Huot87 | 39:6da6cb44f22e | 1142 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
Huot87 | 39:6da6cb44f22e | 1143 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
Huot87 | 39:6da6cb44f22e | 1144 | |
Huot87 | 39:6da6cb44f22e | 1145 | // Configure Control Register 3 |
Huot87 | 39:6da6cb44f22e | 1146 | cmd[0] = LSM303_REG_ACC_CTRL_REG3_A; |
Huot87 | 39:6da6cb44f22e | 1147 | cmd[1] = 0x08; |
Huot87 | 39:6da6cb44f22e | 1148 | // cmd[1] = ret | 0x40; |
Huot87 | 39:6da6cb44f22e | 1149 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
Huot87 | 39:6da6cb44f22e | 1150 | |
Huot87 | 39:6da6cb44f22e | 1151 | cmd[0] = LSM303_CTRL_REG7_A; |
Huot87 | 39:6da6cb44f22e | 1152 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
Huot87 | 39:6da6cb44f22e | 1153 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
Huot87 | 39:6da6cb44f22e | 1154 | |
Huot87 | 39:6da6cb44f22e | 1155 | // Latch Interrupt Enabling |
Huot87 | 39:6da6cb44f22e | 1156 | cmd[0] = LSM303_CTRL_REG7_A; |
Huot87 | 39:6da6cb44f22e | 1157 | cmd[1] = 0x04; |
Huot87 | 39:6da6cb44f22e | 1158 | //cmd[1] = ret | 0x08; |
Huot87 | 39:6da6cb44f22e | 1159 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
Huot87 | 39:6da6cb44f22e | 1160 | |
Huot87 | 39:6da6cb44f22e | 1161 | // Set Threshold/Duration/Config |
Huot87 | 39:6da6cb44f22e | 1162 | // Set Threshold on X axis |
Huot87 | 39:6da6cb44f22e | 1163 | cmd[0] = LSM303_REG_ACC_INT1_THS_X1_A; |
Huot87 | 39:6da6cb44f22e | 1164 | cmd[1] = 0x7D; |
Huot87 | 39:6da6cb44f22e | 1165 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
Huot87 | 39:6da6cb44f22e | 1166 | |
Huot87 | 39:6da6cb44f22e | 1167 | // Set Threshold on Y axis |
Huot87 | 39:6da6cb44f22e | 1168 | cmd[0] = LSM303_REG_ACC_INT1_THS_Y1_A; |
Huot87 | 39:6da6cb44f22e | 1169 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
Huot87 | 39:6da6cb44f22e | 1170 | |
Huot87 | 39:6da6cb44f22e | 1171 | // Set Y Threshold on Z axis |
Huot87 | 39:6da6cb44f22e | 1172 | cmd[0] = LSM303_REG_ACC_INT1_THS_Z1_A; |
Huot87 | 39:6da6cb44f22e | 1173 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
Huot87 | 39:6da6cb44f22e | 1174 | |
Huot87 | 39:6da6cb44f22e | 1175 | // Set Interrupt Duration |
Huot87 | 39:6da6cb44f22e | 1176 | cmd[0] = LSM303_REG_ACC_INT1_DURATION_A; |
Huot87 | 39:6da6cb44f22e | 1177 | cmd[1] = 0x00; |
Huot87 | 39:6da6cb44f22e | 1178 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
Huot87 | 39:6da6cb44f22e | 1179 | |
Huot87 | 39:6da6cb44f22e | 1180 | // Set Interrupt Configuration |
Huot87 | 39:6da6cb44f22e | 1181 | cmd[0] = LSM303_REG_ACC_INT1_CFG_A; |
Huot87 | 39:6da6cb44f22e | 1182 | cmd[1] = 0x02; // Set Interrupt Generation on X-Axis High event |
Huot87 | 39:6da6cb44f22e | 1183 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
Huot87 | 39:6da6cb44f22e | 1184 | accPin.rise(&onAccIrq); |
Huot87 | 39:6da6cb44f22e | 1185 | #endif |
Huot87 | 39:6da6cb44f22e | 1186 | |
lpeters | 34:341fb423e74b | 1187 | #else |
lpeters | 34:341fb423e74b | 1188 | cmd[0] = LSM303_REG_ACC_CTRL_REG1_A; |
lpeters | 34:341fb423e74b | 1189 | cmd[1] = 0x57; // Enable XYZ Axes, ODR=100Hz |
lpeters | 34:341fb423e74b | 1190 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1191 | |
lpeters | 34:341fb423e74b | 1192 | // Enable High Resolution Mode |
lpeters | 34:341fb423e74b | 1193 | cmd[0] = LSM303_REG_ACC_CTRL_REG4_A; |
lpeters | 34:341fb423e74b | 1194 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1195 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 34:341fb423e74b | 1196 | cmd[0] = LSM303_REG_ACC_CTRL_REG4_A; |
lpeters | 34:341fb423e74b | 1197 | cmd[1] = ret | 0x08; // High Resolution |
lpeters | 34:341fb423e74b | 1198 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1199 | |
lpeters | 34:341fb423e74b | 1200 | #if defined(SENSOR_TEMP) |
lpeters | 34:341fb423e74b | 1201 | // Enable Temp Sensor |
lpeters | 34:341fb423e74b | 1202 | cmd[0] = LSM303_REG_ACC_TEMP_CFG_REG_A; |
lpeters | 34:341fb423e74b | 1203 | cmd[1] = 0xC0; |
lpeters | 34:341fb423e74b | 1204 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1205 | cmd[0] = LSM303_REG_ACC_CTRL_REG4_A; |
lpeters | 34:341fb423e74b | 1206 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1207 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 34:341fb423e74b | 1208 | cmd[0] = LSM303_REG_ACC_CTRL_REG4_A; |
lpeters | 34:341fb423e74b | 1209 | cmd[1] = ret | 0x80; |
lpeters | 34:341fb423e74b | 1210 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1211 | #endif |
lpeters | 34:341fb423e74b | 1212 | #endif |
lpeters | 34:341fb423e74b | 1213 | |
lpeters | 34:341fb423e74b | 1214 | //LED Confirmation Output - ACC INIT COMPLETE |
lpeters | 34:341fb423e74b | 1215 | for (int i = 0; i < 2; i++) { |
lpeters | 34:341fb423e74b | 1216 | myLedR = 1; |
lpeters | 34:341fb423e74b | 1217 | myLedG = 1; |
lpeters | 34:341fb423e74b | 1218 | myLedB = 1; |
lpeters | 34:341fb423e74b | 1219 | wait(0.5); |
lpeters | 34:341fb423e74b | 1220 | myLedR = 0; |
lpeters | 34:341fb423e74b | 1221 | myLedG = 0; |
lpeters | 34:341fb423e74b | 1222 | myLedB = 0; |
lpeters | 34:341fb423e74b | 1223 | wait(0.5); |
lpeters | 34:341fb423e74b | 1224 | } |
lpeters | 34:341fb423e74b | 1225 | |
lpeters | 34:341fb423e74b | 1226 | // Set Full Scale to 4g |
lpeters | 34:341fb423e74b | 1227 | /* |
lpeters | 34:341fb423e74b | 1228 | cmd[0] = LSM303_REG_ACC_CTRL_REG4_A; |
lpeters | 34:341fb423e74b | 1229 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1230 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 34:341fb423e74b | 1231 | cmd[0] = LSM303_REG_ACC_CTRL_REG4_A; |
lpeters | 34:341fb423e74b | 1232 | //cmd[1] = ret | 0x30; // 16g |
lpeters | 34:341fb423e74b | 1233 | //cmd[1] = (ret & ~0x10) | 0x20; // 8g |
lpeters | 34:341fb423e74b | 1234 | cmd[1] = (ret & ~0x20) | 0x10; // 4g |
lpeters | 34:341fb423e74b | 1235 | //cmd[1] = ret & ~0x30; // 2g |
lpeters | 34:341fb423e74b | 1236 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1237 | */ |
lpeters | 34:341fb423e74b | 1238 | /* |
lpeters | 34:341fb423e74b | 1239 | // IRQ Init from Datasheet. |
lpeters | 34:341fb423e74b | 1240 | cmd[0] = LSM303_REG_ACC_CTRL_REG1_A; |
lpeters | 34:341fb423e74b | 1241 | cmd[1] = 0xA7; |
lpeters | 34:341fb423e74b | 1242 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1243 | cmd[0] = LSM303_REG_ACC_CTRL_REG2_A; |
lpeters | 34:341fb423e74b | 1244 | cmd[1] = 0x00; |
lpeters | 34:341fb423e74b | 1245 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1246 | cmd[0] = LSM303_REG_ACC_CTRL_REG3_A; |
lpeters | 34:341fb423e74b | 1247 | cmd[1] = 0x40; |
lpeters | 34:341fb423e74b | 1248 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1249 | cmd[0] = LSM303_REG_ACC_CTRL_REG4_A; |
lpeters | 34:341fb423e74b | 1250 | cmd[1] = 0x00; |
lpeters | 34:341fb423e74b | 1251 | //cmd[1] = 0x10; |
lpeters | 34:341fb423e74b | 1252 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1253 | cmd[0] = LSM303_REG_ACC_CTRL_REG5_A; |
lpeters | 34:341fb423e74b | 1254 | cmd[1] = 0x08; |
lpeters | 34:341fb423e74b | 1255 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1256 | */ |
lpeters | 35:73b3963c6dd3 | 1257 | #if defined(TARGET_LRAT) |
lpeters | 35:73b3963c6dd3 | 1258 | // LRAT ACC IRQ SETUP GOES HERE |
lpeters | 35:73b3963c6dd3 | 1259 | #else |
lpeters | 34:341fb423e74b | 1260 | // ACC INTERRUPT SETUP |
lpeters | 34:341fb423e74b | 1261 | // Enable Interrupt Pin |
lpeters | 34:341fb423e74b | 1262 | cmd[0] = LSM303_REG_ACC_CTRL_REG3_A; |
lpeters | 34:341fb423e74b | 1263 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1264 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 34:341fb423e74b | 1265 | cmd[0] = LSM303_REG_ACC_CTRL_REG3_A; |
lpeters | 34:341fb423e74b | 1266 | cmd[1] = ret | 0x40; |
lpeters | 34:341fb423e74b | 1267 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1268 | // Enable Interrupt Latch |
lpeters | 34:341fb423e74b | 1269 | cmd[0] = LSM303_REG_ACC_CTRL_REG5_A; |
lpeters | 34:341fb423e74b | 1270 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1271 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 34:341fb423e74b | 1272 | cmd[0] = LSM303_REG_ACC_CTRL_REG5_A; |
lpeters | 34:341fb423e74b | 1273 | cmd[1] = ret | 0x08; |
lpeters | 34:341fb423e74b | 1274 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1275 | |
lpeters | 34:341fb423e74b | 1276 | // Set Threshold/Duration/Config |
lpeters | 34:341fb423e74b | 1277 | cmd[0] = LSM303_REG_ACC_INT1_THS_A; |
lpeters | 34:341fb423e74b | 1278 | //cmd[1] = 0x10; |
lpeters | 34:341fb423e74b | 1279 | //cmd[1] = 0x40; |
lpeters | 34:341fb423e74b | 1280 | //cmd[1] = 0x60; |
lpeters | 34:341fb423e74b | 1281 | cmd[1] = 0x7D; |
lpeters | 34:341fb423e74b | 1282 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1283 | cmd[0] = LSM303_REG_ACC_INT1_DURATION_A; |
lpeters | 34:341fb423e74b | 1284 | cmd[1] = 0x00; |
lpeters | 34:341fb423e74b | 1285 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1286 | cmd[0] = LSM303_REG_ACC_INT1_CFG_A; |
lpeters | 34:341fb423e74b | 1287 | cmd[1] = 0x2A; |
lpeters | 34:341fb423e74b | 1288 | //cmd[1] = 0x0A; |
lpeters | 34:341fb423e74b | 1289 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 35:73b3963c6dd3 | 1290 | accPin.rise(&onAccIrq); |
lpeters | 35:73b3963c6dd3 | 1291 | #endif |
lpeters | 34:341fb423e74b | 1292 | } |
lpeters | 34:341fb423e74b | 1293 | |
lpeters | 34:341fb423e74b | 1294 | void gpsInitSequence() |
lpeters | 34:341fb423e74b | 1295 | { |
lpeters | 35:73b3963c6dd3 | 1296 | printf("In gpsInitSequence()...\r\n"); |
lpeters | 35:73b3963c6dd3 | 1297 | gpsRead(); |
lpeters | 35:73b3963c6dd3 | 1298 | if (mytime > 0) |
lpeters | 35:73b3963c6dd3 | 1299 | { |
lpeters | 35:73b3963c6dd3 | 1300 | set_time(mytime); |
lpeters | 35:73b3963c6dd3 | 1301 | tInit = mytime; |
lpeters | 35:73b3963c6dd3 | 1302 | myLedG = 1; |
lpeters | 35:73b3963c6dd3 | 1303 | } |
lpeters | 35:73b3963c6dd3 | 1304 | else |
lpeters | 35:73b3963c6dd3 | 1305 | myLedR = 1; |
lpeters | 34:341fb423e74b | 1306 | |
lpeters | 34:341fb423e74b | 1307 | for (int i = 0; i < 2; i++) { |
lpeters | 34:341fb423e74b | 1308 | myLedB = 1; |
lpeters | 34:341fb423e74b | 1309 | wait(0.3); |
lpeters | 34:341fb423e74b | 1310 | myLedB = 0; |
lpeters | 34:341fb423e74b | 1311 | wait(0.3); |
lpeters | 34:341fb423e74b | 1312 | } |
lpeters | 34:341fb423e74b | 1313 | myLedG = 0; |
lpeters | 34:341fb423e74b | 1314 | myLedR = 0; |
Huot87 | 38:fd7a407bfe3e | 1315 | |
Huot87 | 38:fd7a407bfe3e | 1316 | // HUOT ADDITION -- GPS LOW POWER SETUP |
Huot87 | 38:fd7a407bfe3e | 1317 | //printf("Disabling GLONASS... \r\n"); |
Huot87 | 38:fd7a407bfe3e | 1318 | //gpsExecuteCommand(cmdDisableGLONASS, sizeof(cmdDisableGLONASS)); // Disable GLONASS |
Huot87 | 38:fd7a407bfe3e | 1319 | //printf("Disabling Time Pulse... \r\n"); |
Huot87 | 38:fd7a407bfe3e | 1320 | //gpsExecuteCommand(cmdDisableTimePulse, sizeof(cmdDisableTimePulse)); // Disable Time Pulse |
Huot87 | 38:fd7a407bfe3e | 1321 | //printf("Configuring PMS... \r\n"); |
Huot87 | 38:fd7a407bfe3e | 1322 | //gpsExecuteCommand(cmdConfigPMS, sizeof(cmdConfigPMS)); // Configure Power Management Setup |
Huot87 | 38:fd7a407bfe3e | 1323 | //printf("LPM Init Sequence Complete. \r\n"); |
Huot87 | 38:fd7a407bfe3e | 1324 | } |
Huot87 | 38:fd7a407bfe3e | 1325 | |
Huot87 | 39:6da6cb44f22e | 1326 | // HUOT ADDITION 08/30/2018 -- GENERIC GPS EXECUTE COMMAND FUNCTION |
Huot87 | 38:fd7a407bfe3e | 1327 | void gpsExecuteCommand(char *command, int length) |
Huot87 | 38:fd7a407bfe3e | 1328 | { |
Huot87 | 38:fd7a407bfe3e | 1329 | |
Huot87 | 38:fd7a407bfe3e | 1330 | uint8_t crcA = 0; |
Huot87 | 38:fd7a407bfe3e | 1331 | uint8_t crcB = 0; |
Huot87 | 38:fd7a407bfe3e | 1332 | for(int i = 2; i <= (length - 3); i++) |
Huot87 | 38:fd7a407bfe3e | 1333 | { |
Huot87 | 38:fd7a407bfe3e | 1334 | crcA = crcA + command[i]; |
Huot87 | 38:fd7a407bfe3e | 1335 | crcB = crcB + crcA; |
Huot87 | 38:fd7a407bfe3e | 1336 | } |
Huot87 | 38:fd7a407bfe3e | 1337 | |
Huot87 | 38:fd7a407bfe3e | 1338 | command[(length - 2)] = crcA; |
Huot87 | 38:fd7a407bfe3e | 1339 | command[(length - 1)] = crcB; |
Huot87 | 38:fd7a407bfe3e | 1340 | printf("UBX CRC: %02X %02X\r\n", crcA, crcB); |
Huot87 | 38:fd7a407bfe3e | 1341 | i2c.write(NEOM8M_ADR_GPS, command, length); |
Huot87 | 38:fd7a407bfe3e | 1342 | printf("Command Executed \r\n"); |
Huot87 | 38:fd7a407bfe3e | 1343 | wait(1); |
lpeters | 34:341fb423e74b | 1344 | } |
lpeters | 34:341fb423e74b | 1345 | |
lpeters | 34:341fb423e74b | 1346 | void magRead() |
lpeters | 34:341fb423e74b | 1347 | { |
lpeters | 34:341fb423e74b | 1348 | cmd[0] = LSM303_REG_MAG_STATUS_REG_M; |
lpeters | 34:341fb423e74b | 1349 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1350 | i2c.read(LSM303_ADR_MAG, &rda, 1); |
lpeters | 34:341fb423e74b | 1351 | if (rda) |
lpeters | 34:341fb423e74b | 1352 | { |
lpeters | 34:341fb423e74b | 1353 | cmd[0] = LSM303_REG_MAG_OUTX_L_REG_M; |
lpeters | 34:341fb423e74b | 1354 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1355 | i2c.read(LSM303_ADR_MAG, &buf[0], 1); |
lpeters | 34:341fb423e74b | 1356 | cmd[0] = LSM303_REG_MAG_OUTX_H_REG_M; |
lpeters | 34:341fb423e74b | 1357 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1358 | i2c.read(LSM303_ADR_MAG, &buf[1], 1); |
lpeters | 34:341fb423e74b | 1359 | cmd[0] = LSM303_REG_MAG_OUTY_L_REG_M; |
lpeters | 34:341fb423e74b | 1360 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1361 | i2c.read(LSM303_ADR_MAG, &buf[2], 1); |
lpeters | 34:341fb423e74b | 1362 | cmd[0] = LSM303_REG_MAG_OUTY_H_REG_M; |
lpeters | 34:341fb423e74b | 1363 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1364 | i2c.read(LSM303_ADR_MAG, &buf[3], 1); |
lpeters | 34:341fb423e74b | 1365 | cmd[0] = LSM303_REG_MAG_OUTZ_L_REG_M; |
lpeters | 34:341fb423e74b | 1366 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1367 | i2c.read(LSM303_ADR_MAG, &buf[4], 1); |
lpeters | 34:341fb423e74b | 1368 | cmd[0] = LSM303_REG_MAG_OUTZ_H_REG_M; |
lpeters | 34:341fb423e74b | 1369 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1370 | i2c.read(LSM303_ADR_MAG, &buf[5], 1); |
lpeters | 34:341fb423e74b | 1371 | myMagX = (buf[0] | (buf[1] << 8)); |
lpeters | 34:341fb423e74b | 1372 | myMagY = (buf[2] | (buf[3] << 8)); |
lpeters | 34:341fb423e74b | 1373 | myMagZ = (buf[4] | (buf[5] << 8)); |
lpeters | 34:341fb423e74b | 1374 | if (myMagX < magMinX) |
lpeters | 34:341fb423e74b | 1375 | magMinX = myMagX; |
lpeters | 34:341fb423e74b | 1376 | if (myMagY < magMinY) |
lpeters | 34:341fb423e74b | 1377 | magMinY = myMagY; |
lpeters | 34:341fb423e74b | 1378 | if (myMagZ < magMinZ) |
lpeters | 34:341fb423e74b | 1379 | magMinZ = myMagZ; |
lpeters | 34:341fb423e74b | 1380 | if (myMagX > magMaxX) |
lpeters | 34:341fb423e74b | 1381 | magMaxX = myMagX; |
lpeters | 34:341fb423e74b | 1382 | if (myMagY > magMaxY) |
lpeters | 34:341fb423e74b | 1383 | magMaxY = myMagY; |
lpeters | 34:341fb423e74b | 1384 | if (myMagZ > magMaxZ) |
lpeters | 34:341fb423e74b | 1385 | magMaxZ = myMagZ; |
lpeters | 34:341fb423e74b | 1386 | cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M; |
lpeters | 34:341fb423e74b | 1387 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1388 | i2c.read(LSM303_ADR_MAG, &ret, 1); |
lpeters | 34:341fb423e74b | 1389 | if (ret & 0x01 && magEvent == 0 && ret & 0xFC) |
lpeters | 34:341fb423e74b | 1390 | { |
lpeters | 34:341fb423e74b | 1391 | magSFire++; |
lpeters | 34:341fb423e74b | 1392 | magEvent = 1; |
lpeters | 34:341fb423e74b | 1393 | magSHIRQ++; |
lpeters | 34:341fb423e74b | 1394 | } |
lpeters | 34:341fb423e74b | 1395 | else if (!(ret & 0x01) && magEvent == 1 && !(ret & 0xFC)) |
lpeters | 34:341fb423e74b | 1396 | { |
lpeters | 34:341fb423e74b | 1397 | magSFire++; |
lpeters | 34:341fb423e74b | 1398 | magEvent = 0; |
lpeters | 34:341fb423e74b | 1399 | magSLIRQ++; |
lpeters | 34:341fb423e74b | 1400 | } |
lpeters | 34:341fb423e74b | 1401 | printf("M|%02X|%02X %02X %02X %02X %02X %02X|%*d,%*d,%*d|%*d,%*d,%*d|%*d,%*d,%*d|%02X|%02X/%02X %02X/%02X\r\n", rda, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], 6, myMagX, 6, myMagY, 6, myMagZ, 6, magMinX, 6, magMinY, 6, magMinZ, 6, magMaxX, 6, magMaxY, 6, magMaxZ, ret, magSHIRQ, magSLIRQ, magSFire, magHFire); |
lpeters | 34:341fb423e74b | 1402 | } |
lpeters | 34:341fb423e74b | 1403 | } |
lpeters | 34:341fb423e74b | 1404 | |
lpeters | 34:341fb423e74b | 1405 | void accRead() |
lpeters | 34:341fb423e74b | 1406 | { |
lpeters | 34:341fb423e74b | 1407 | cmd[0] = LSM303_REG_ACC_STATUS_REG_A; |
lpeters | 34:341fb423e74b | 1408 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1409 | i2c.read(LSM303_ADR_ACC, &rda, 1); |
lpeters | 34:341fb423e74b | 1410 | if (rda) |
lpeters | 34:341fb423e74b | 1411 | { |
lpeters | 34:341fb423e74b | 1412 | cmd[0] = LSM303_REG_ACC_OUT_X_L_A; |
lpeters | 34:341fb423e74b | 1413 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1414 | i2c.read(LSM303_ADR_ACC, &buf[0], 1); |
lpeters | 34:341fb423e74b | 1415 | cmd[0] = LSM303_REG_ACC_OUT_X_H_A; |
lpeters | 34:341fb423e74b | 1416 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1417 | i2c.read(LSM303_ADR_ACC, &buf[1], 1); |
lpeters | 34:341fb423e74b | 1418 | cmd[0] = LSM303_REG_ACC_OUT_Y_L_A; |
lpeters | 34:341fb423e74b | 1419 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1420 | i2c.read(LSM303_ADR_ACC, &buf[2], 1); |
lpeters | 34:341fb423e74b | 1421 | cmd[0] = LSM303_REG_ACC_OUT_Y_H_A; |
lpeters | 34:341fb423e74b | 1422 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1423 | i2c.read(LSM303_ADR_ACC, &buf[3], 1); |
lpeters | 34:341fb423e74b | 1424 | cmd[0] = LSM303_REG_ACC_OUT_Z_L_A; |
lpeters | 34:341fb423e74b | 1425 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1426 | i2c.read(LSM303_ADR_ACC, &buf[4], 1); |
lpeters | 34:341fb423e74b | 1427 | cmd[0] = LSM303_REG_ACC_OUT_Z_H_A; |
lpeters | 34:341fb423e74b | 1428 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1429 | i2c.read(LSM303_ADR_ACC, &buf[5], 1); |
lpeters | 34:341fb423e74b | 1430 | myAccX = ((int16_t)(buf[0] | (buf[1] << 8)) >> accShift); |
lpeters | 34:341fb423e74b | 1431 | myAccY = ((int16_t)(buf[2] | (buf[3] << 8)) >> accShift); |
lpeters | 34:341fb423e74b | 1432 | myAccZ = ((int16_t)(buf[4] | (buf[5] << 8)) >> accShift); |
lpeters | 34:341fb423e74b | 1433 | if (myAccX < accMinX) |
lpeters | 34:341fb423e74b | 1434 | accMinX = myAccX; |
lpeters | 34:341fb423e74b | 1435 | if (myAccY < accMinY) |
lpeters | 34:341fb423e74b | 1436 | accMinY = myAccY; |
lpeters | 34:341fb423e74b | 1437 | if (myAccZ < accMinZ) |
lpeters | 34:341fb423e74b | 1438 | accMinZ = myAccZ; |
lpeters | 34:341fb423e74b | 1439 | if (myAccX > accMaxX) |
lpeters | 34:341fb423e74b | 1440 | accMaxX = myAccX; |
lpeters | 34:341fb423e74b | 1441 | if (myAccY > accMaxY) |
lpeters | 34:341fb423e74b | 1442 | accMaxY = myAccY; |
lpeters | 34:341fb423e74b | 1443 | if (myAccZ > accMaxZ) |
lpeters | 34:341fb423e74b | 1444 | accMaxZ = myAccZ; |
lpeters | 34:341fb423e74b | 1445 | cmd[0] = LSM303_REG_ACC_INT1_SRC_A; |
lpeters | 34:341fb423e74b | 1446 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1447 | i2c.read(LSM303_ADR_ACC, &ret, 1); |
lpeters | 34:341fb423e74b | 1448 | if (ret & 0x40) |
lpeters | 34:341fb423e74b | 1449 | { |
lpeters | 34:341fb423e74b | 1450 | accSFire++; |
lpeters | 34:341fb423e74b | 1451 | if (accEvent == 1) |
lpeters | 34:341fb423e74b | 1452 | { |
lpeters | 34:341fb423e74b | 1453 | accEvent = 0; |
lpeters | 34:341fb423e74b | 1454 | accSLIRQ++; |
lpeters | 34:341fb423e74b | 1455 | cmd[0] = LSM303_REG_ACC_INT1_THS_A; |
lpeters | 34:341fb423e74b | 1456 | cmd[1] = 0x7D; |
lpeters | 34:341fb423e74b | 1457 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1458 | cmd[0] = LSM303_REG_ACC_INT1_DURATION_A; |
lpeters | 34:341fb423e74b | 1459 | cmd[1] = 0x00; |
lpeters | 34:341fb423e74b | 1460 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1461 | cmd[0] = LSM303_REG_ACC_INT1_CFG_A; |
lpeters | 34:341fb423e74b | 1462 | cmd[1] = 0x2A; |
lpeters | 34:341fb423e74b | 1463 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1464 | } |
lpeters | 34:341fb423e74b | 1465 | else |
lpeters | 34:341fb423e74b | 1466 | { |
lpeters | 34:341fb423e74b | 1467 | accEvent = 1; |
lpeters | 34:341fb423e74b | 1468 | accSHIRQ++; |
lpeters | 34:341fb423e74b | 1469 | cmd[0] = LSM303_REG_ACC_INT1_THS_A; |
lpeters | 34:341fb423e74b | 1470 | cmd[1] = 0x50; |
lpeters | 34:341fb423e74b | 1471 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1472 | cmd[0] = LSM303_REG_ACC_INT1_DURATION_A; |
lpeters | 34:341fb423e74b | 1473 | //cmd[1] = 0x7D; |
lpeters | 34:341fb423e74b | 1474 | cmd[1] = 0x03; |
lpeters | 34:341fb423e74b | 1475 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1476 | cmd[0] = LSM303_REG_ACC_INT1_CFG_A; |
lpeters | 34:341fb423e74b | 1477 | cmd[1] = 0x95; |
lpeters | 34:341fb423e74b | 1478 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 34:341fb423e74b | 1479 | } |
lpeters | 34:341fb423e74b | 1480 | } |
lpeters | 34:341fb423e74b | 1481 | printf("A|%02X|%02X %02X %02X %02X %02X %02X|%*d,%*d,%*d|%*d,%*d,%*d|%*d,%*d,%*d|%02X|%02X/%02X %02X/%02X\r\n", rda, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], 6, myAccX, 6, myAccY, 6, myAccZ, 6, accMinX, 6, accMinY, 6, accMinZ, 6, accMaxX, 6, accMaxY, 6, accMaxZ, ret, accSHIRQ, accSLIRQ, accSFire, accHFire); |
lpeters | 34:341fb423e74b | 1482 | } |
lpeters | 34:341fb423e74b | 1483 | } |
lpeters | 34:341fb423e74b | 1484 | |
lpeters | 34:341fb423e74b | 1485 | void tmpRead() |
lpeters | 34:341fb423e74b | 1486 | { |
lpeters | 34:341fb423e74b | 1487 | #if defined(TARGET_LRAT) |
lpeters | 34:341fb423e74b | 1488 | cmd[0] = LSM303_REG_MAG_TEMP_L_M; |
lpeters | 34:341fb423e74b | 1489 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1490 | i2c.read(LSM303_ADR_MAG, &buf[0], 1); |
lpeters | 34:341fb423e74b | 1491 | cmd[0] = LSM303_REG_MAG_TEMP_H_M; |
lpeters | 34:341fb423e74b | 1492 | i2c.write(LSM303_ADR_MAG, cmd, 1); |
lpeters | 34:341fb423e74b | 1493 | i2c.read(LSM303_ADR_MAG, &buf[1], 1); |
lpeters | 34:341fb423e74b | 1494 | myTemp = (int16_t)(buf[0] | (buf[1] << 8)); |
lpeters | 34:341fb423e74b | 1495 | printf("T|%02X %02X| (%d)\r\n", buf[0], buf[1], myTemp); |
lpeters | 34:341fb423e74b | 1496 | #else |
lpeters | 34:341fb423e74b | 1497 | cmd[0] = LSM303_REG_ACC_STATUS_REG_AUX_A; |
lpeters | 34:341fb423e74b | 1498 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1499 | i2c.read(LSM303_ADR_ACC, &rda, 1); |
lpeters | 34:341fb423e74b | 1500 | if (rda & 0x04) |
lpeters | 34:341fb423e74b | 1501 | { |
lpeters | 34:341fb423e74b | 1502 | cmd[0] = LSM303_REG_ACC_OUT_TEMP_L_A | 0x80; |
lpeters | 34:341fb423e74b | 1503 | i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 34:341fb423e74b | 1504 | i2c.read(LSM303_ADR_ACC, &buf[0], 2); |
lpeters | 34:341fb423e74b | 1505 | myTemp = (int16_t)(buf[0] | (buf[1] << 8)) >> 6; |
lpeters | 34:341fb423e74b | 1506 | printf("T|%02X %02X %02X| (%d)\r\n", rda, buf[0], buf[1], myTemp); |
lpeters | 34:341fb423e74b | 1507 | } |
lpeters | 34:341fb423e74b | 1508 | #endif |
lpeters | 34:341fb423e74b | 1509 | } |
lpeters | 34:341fb423e74b | 1510 | |
Huot87 | 38:fd7a407bfe3e | 1511 | |
Huot87 | 39:6da6cb44f22e | 1512 | // HUOT ALTERATION -- 08/30/2018 -- Change GPS read from "void" to return "int" to solidfy if fix was good or bad |
Huot87 | 38:fd7a407bfe3e | 1513 | int gpsRead() |
lpeters | 34:341fb423e74b | 1514 | { |
lpeters | 34:341fb423e74b | 1515 | bool gpsDone = false; |
lpeters | 34:341fb423e74b | 1516 | bool fixGood = false; |
lpeters | 35:73b3963c6dd3 | 1517 | uint8_t crcPass = 0; |
lpeters | 35:73b3963c6dd3 | 1518 | uint8_t crcFail = 0; |
lpeters | 35:73b3963c6dd3 | 1519 | uint32_t tDate = 0; |
lpeters | 35:73b3963c6dd3 | 1520 | uint32_t tTime = 0; |
lpeters | 34:341fb423e74b | 1521 | myLedW = 1; |
lpeters | 34:341fb423e74b | 1522 | pos = 0; |
lpeters | 33:e47306c32791 | 1523 | ret = 0xFF; |
lpeters | 33:e47306c32791 | 1524 | cmd[0] = 0xFF; |
lpeters | 33:e47306c32791 | 1525 | i2c.write(NEOM8M_ADR_GPS, cmd, 1); |
lpeters | 33:e47306c32791 | 1526 | while(!gpsDone) |
lpeters | 33:e47306c32791 | 1527 | { |
lpeters | 33:e47306c32791 | 1528 | while (ret == 0xFF) |
lpeters | 33:e47306c32791 | 1529 | { |
lpeters | 33:e47306c32791 | 1530 | i2c.read(NEOM8M_ADR_GPS, &ret, 1); |
lpeters | 33:e47306c32791 | 1531 | } |
lpeters | 33:e47306c32791 | 1532 | while (ret != 0xFF) |
lpeters | 33:e47306c32791 | 1533 | { |
lpeters | 33:e47306c32791 | 1534 | buf[pos++] = ret; |
lpeters | 33:e47306c32791 | 1535 | i2c.read(NEOM8M_ADR_GPS, &ret, 1); |
lpeters | 33:e47306c32791 | 1536 | if (ret == '\r') |
lpeters | 33:e47306c32791 | 1537 | { |
lpeters | 33:e47306c32791 | 1538 | i2c.read(NEOM8M_ADR_GPS, &ret, 1); |
lpeters | 33:e47306c32791 | 1539 | if (ret == '\n') |
lpeters | 33:e47306c32791 | 1540 | { |
lpeters | 33:e47306c32791 | 1541 | buf[pos] = 0x00; |
lpeters | 34:341fb423e74b | 1542 | // NMEA Validation |
lpeters | 33:e47306c32791 | 1543 | uint16_t crc = 0; |
lpeters | 35:73b3963c6dd3 | 1544 | char clr = '\0'; |
lpeters | 33:e47306c32791 | 1545 | if (buf[0] == '$' && buf[pos-3] == '*') |
lpeters | 33:e47306c32791 | 1546 | { |
lpeters | 35:73b3963c6dd3 | 1547 | for (int i = 1; i < pos-3; i++) |
lpeters | 33:e47306c32791 | 1548 | { |
lpeters | 33:e47306c32791 | 1549 | crc ^= buf[i]; |
lpeters | 33:e47306c32791 | 1550 | } |
lpeters | 33:e47306c32791 | 1551 | } |
lpeters | 33:e47306c32791 | 1552 | if (crc == ((buf[pos-2] < 'A' ? buf[pos-2] - '0' : buf[pos-2] - '7') << 4 | (buf[pos-1] < 'A' ? buf[pos-1] - '0' : buf[pos-1] - '7'))) |
lpeters | 35:73b3963c6dd3 | 1553 | { |
lpeters | 33:e47306c32791 | 1554 | clr = '2'; // 2 = Green |
lpeters | 35:73b3963c6dd3 | 1555 | crcPass++; |
lpeters | 35:73b3963c6dd3 | 1556 | } |
lpeters | 33:e47306c32791 | 1557 | else |
lpeters | 35:73b3963c6dd3 | 1558 | { |
lpeters | 33:e47306c32791 | 1559 | clr = '1'; // 1 = Red |
lpeters | 35:73b3963c6dd3 | 1560 | crcFail++; |
lpeters | 35:73b3963c6dd3 | 1561 | } |
lpeters | 33:e47306c32791 | 1562 | printf("GPS: [\u001b[3%cm%02X\u001b[0m] |%s|\r\n", clr, crc, buf); |
lpeters | 35:73b3963c6dd3 | 1563 | if (clr == '2') |
lpeters | 35:73b3963c6dd3 | 1564 | { |
lpeters | 33:e47306c32791 | 1565 | // Global Positioning System Fix Data |
lpeters | 33:e47306c32791 | 1566 | if(strncmp(buf, "$GNGGA", 6) == 0) |
lpeters | 33:e47306c32791 | 1567 | { |
lpeters | 33:e47306c32791 | 1568 | printf("GNGGA> "); |
lpeters | 33:e47306c32791 | 1569 | //sscanf(cmd, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude); |
lpeters | 33:e47306c32791 | 1570 | //pc.printf("GPGGA Fix taken at: %f, Latitude: %f %c, Longitude: %f %c, Fix quality: %d, Number of sat: %d, Altitude: %f M\n", timefix, latitude, ns, longitude, ew, fq, nst, altitude); |
lpeters | 33:e47306c32791 | 1571 | float fldTim, fldAlt; |
lpeters | 33:e47306c32791 | 1572 | double fldLat, fldLon; |
lpeters | 33:e47306c32791 | 1573 | char fldN_S, fldE_W; |
lpeters | 33:e47306c32791 | 1574 | int fldFix, fldSat; |
lpeters | 42:d59e50622292 | 1575 | if (sscanf(buf, "$GNGGA,%f,%lf,%c,%lf,%c,%d,%d,%*f,%f", &fldTim, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldFix, &fldSat, &fldAlt) == 8) |
lpeters | 42:d59e50622292 | 1576 | printf("Sec: %.2f, Lat: %.5f %c, Lon: %.5f %c, Fix: %d, Sat: %d, Alt: %.1f M\r\n", fldTim, fldLat, fldN_S, fldLon, fldE_W, fldFix, fldSat, fldAlt); |
lpeters | 33:e47306c32791 | 1577 | if (clr == '2') |
lpeters | 33:e47306c32791 | 1578 | { |
lpeters | 33:e47306c32791 | 1579 | mylat = fldLat / (fldN_S == 'S' ? -100 : 100); |
lpeters | 33:e47306c32791 | 1580 | mylon = fldLon / (fldE_W == 'W' ? -100 : 100); |
lpeters | 35:73b3963c6dd3 | 1581 | //mytime = (uint32_t)fldTim; |
lpeters | 33:e47306c32791 | 1582 | mybatt = (fldSat & 0xF0 ? 0x0F : fldSat & 0x0F); |
lpeters | 33:e47306c32791 | 1583 | } |
lpeters | 33:e47306c32791 | 1584 | } |
lpeters | 33:e47306c32791 | 1585 | // Satellite status |
lpeters | 33:e47306c32791 | 1586 | if(strncmp(buf, "$GNGSA", 6) == 0) |
lpeters | 33:e47306c32791 | 1587 | { |
lpeters | 35:73b3963c6dd3 | 1588 | //printf("GNGSA> "); |
lpeters | 33:e47306c32791 | 1589 | //sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst); |
lpeters | 33:e47306c32791 | 1590 | //pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst); |
lpeters | 33:e47306c32791 | 1591 | char fldTyp; |
lpeters | 33:e47306c32791 | 1592 | int fldDim, fldSat; |
lpeters | 33:e47306c32791 | 1593 | sscanf(buf, "$GNGSA,%c,%d,%d", &fldTyp, &fldDim, &fldSat); |
lpeters | 35:73b3963c6dd3 | 1594 | //printf("Typ: %c, Pos: %d, Sat: %d\r\n", fldTyp, fldDim, fldSat); |
lpeters | 33:e47306c32791 | 1595 | } |
lpeters | 33:e47306c32791 | 1596 | // Geographic position, Latitude and Longitude |
lpeters | 33:e47306c32791 | 1597 | if(strncmp(buf, "$GNGLL", 6) == 0) |
lpeters | 33:e47306c32791 | 1598 | { |
lpeters | 35:73b3963c6dd3 | 1599 | //printf("GNGLL> "); |
lpeters | 33:e47306c32791 | 1600 | //sscanf(cmd, "$GPGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix); |
lpeters | 33:e47306c32791 | 1601 | //pc.printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n", latitude, ns, longitude, ew, timefix); |
lpeters | 33:e47306c32791 | 1602 | float fldTim; |
lpeters | 33:e47306c32791 | 1603 | double fldLat, fldLon; |
lpeters | 33:e47306c32791 | 1604 | char fldN_S, fldE_W; |
lpeters | 33:e47306c32791 | 1605 | sscanf(buf, "$GNGLL,%lf,%c,%lf,%c,%f", &fldLat, &fldN_S, &fldLon, &fldE_W, &fldTim); |
lpeters | 35:73b3963c6dd3 | 1606 | //printf("Lat: %.5f %c, Lon: %.5f %c, Sec: %.2f\r\n", fldLat, fldN_S, fldLon, fldE_W, fldTim); |
lpeters | 33:e47306c32791 | 1607 | } |
lpeters | 33:e47306c32791 | 1608 | // Geographic position, Latitude and Longitude |
lpeters | 33:e47306c32791 | 1609 | if(strncmp(buf, "$GNRMC", 6) == 0) |
lpeters | 33:e47306c32791 | 1610 | { |
lpeters | 35:73b3963c6dd3 | 1611 | //printf("GPS: [\u001b[3%cm%02X\u001b[0m] |%s|\r\n", clr, crc, buf); |
lpeters | 33:e47306c32791 | 1612 | printf("GNRMC> "); |
lpeters | 33:e47306c32791 | 1613 | //sscanf(cmd, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date); |
lpeters | 33:e47306c32791 | 1614 | //pc.printf("GPRMC Fix taken at: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\n", timefix, status, latitude, ns, longitude, ew, speed, date); |
lpeters | 35:73b3963c6dd3 | 1615 | float fldTim, fldSpd, fldTrk; |
lpeters | 35:73b3963c6dd3 | 1616 | fldTrk = 0; |
lpeters | 33:e47306c32791 | 1617 | double fldLat, fldLon; |
lpeters | 33:e47306c32791 | 1618 | char fldSts, fldN_S, fldE_W; |
lpeters | 35:73b3963c6dd3 | 1619 | uint32_t fldDat; |
lpeters | 35:73b3963c6dd3 | 1620 | if (sscanf(buf, "$GNRMC,,%c", &fldSts) != 1 && |
lpeters | 35:73b3963c6dd3 | 1621 | sscanf(buf, "$GNRMC,%f,%c,,,,,,,%d", &fldTim, &fldSts, &fldDat) != 3 && |
lpeters | 35:73b3963c6dd3 | 1622 | sscanf(buf, "$GNRMC,%f,%c,%lf,%c,%lf,%c,%f,,%d", &fldTim, &fldSts, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldSpd, &fldDat) != 8 && |
lpeters | 35:73b3963c6dd3 | 1623 | sscanf(buf, "$GNRMC,%f,%c,%lf,%c,%lf,%c,%f,%f,%d", &fldTim, &fldSts, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldSpd, &fldTrk, &fldDat) != 9) |
lpeters | 35:73b3963c6dd3 | 1624 | printf("[\u001b[33mWARN\u001b[0m] Invalid GNRMC packet detected.\r\n"); |
lpeters | 35:73b3963c6dd3 | 1625 | //sscanf(buf, "$GNRMC,%f,%c,%lf,%c,%lf,%c,%f,%7[^,],%d", &fldTim, &fldSts, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldSpd, fldFoo, &fldDat); |
lpeters | 33:e47306c32791 | 1626 | printf("Sec: %.2f, Sts: %c, Lat: %.5f %c, Lon: %.5f %c, Spd: %.3f, Dat: %06d\r\n", fldTim, fldSts, fldLat, fldN_S, fldLon, fldE_W, fldSpd, fldDat); |
lpeters | 35:73b3963c6dd3 | 1627 | if (clr == '2') |
lpeters | 35:73b3963c6dd3 | 1628 | { |
lpeters | 35:73b3963c6dd3 | 1629 | tTime = (uint32_t)fldTim; |
lpeters | 35:73b3963c6dd3 | 1630 | tDate = fldDat; |
lpeters | 35:73b3963c6dd3 | 1631 | if (fldSts == 'A') |
lpeters | 35:73b3963c6dd3 | 1632 | fixGood = true; |
lpeters | 35:73b3963c6dd3 | 1633 | } |
lpeters | 35:73b3963c6dd3 | 1634 | } |
lpeters | 33:e47306c32791 | 1635 | } |
lpeters | 33:e47306c32791 | 1636 | pos = 0; |
lpeters | 33:e47306c32791 | 1637 | i2c.read(NEOM8M_ADR_GPS, &ret, 1); |
lpeters | 33:e47306c32791 | 1638 | } |
lpeters | 33:e47306c32791 | 1639 | else |
lpeters | 33:e47306c32791 | 1640 | { |
lpeters | 35:73b3963c6dd3 | 1641 | printf("[\u001b[33mWARN\u001b[0m] Expected '0A', received '%02X'.\r\n", ret); |
lpeters | 33:e47306c32791 | 1642 | } |
lpeters | 33:e47306c32791 | 1643 | } |
lpeters | 37:df69df1bcb1a | 1644 | //else if (pos == 82) |
lpeters | 37:df69df1bcb1a | 1645 | else if (pos == 191) |
lpeters | 33:e47306c32791 | 1646 | { |
lpeters | 33:e47306c32791 | 1647 | buf[pos] = 0x00; |
lpeters | 33:e47306c32791 | 1648 | printf("GPS: |%s| ...\r\n", buf); |
lpeters | 33:e47306c32791 | 1649 | pos = 0; |
lpeters | 33:e47306c32791 | 1650 | i2c.read(NEOM8M_ADR_GPS, &ret, 1); |
lpeters | 33:e47306c32791 | 1651 | } |
lpeters | 33:e47306c32791 | 1652 | } |
lpeters | 33:e47306c32791 | 1653 | buf[pos] = 0x00; |
lpeters | 33:e47306c32791 | 1654 | gpsDone = true; |
lpeters | 33:e47306c32791 | 1655 | } |
lpeters | 33:e47306c32791 | 1656 | if (pos > 0) |
lpeters | 33:e47306c32791 | 1657 | printf("GPS: |%s|\r\n", buf); |
lpeters | 34:341fb423e74b | 1658 | myLedW = 0; |
lpeters | 35:73b3963c6dd3 | 1659 | if (crcFail) |
lpeters | 35:73b3963c6dd3 | 1660 | printf("[\u001b[33mWARN\u001b[0m] CRC PASS: %d FAIL: %d\r\n", crcPass, crcFail); |
lpeters | 35:73b3963c6dd3 | 1661 | |
lpeters | 35:73b3963c6dd3 | 1662 | struct tm ts; |
lpeters | 35:73b3963c6dd3 | 1663 | time_t t; |
lpeters | 35:73b3963c6dd3 | 1664 | |
lpeters | 35:73b3963c6dd3 | 1665 | uint8_t tDay = tDate / 10000; |
lpeters | 35:73b3963c6dd3 | 1666 | uint8_t tMon = (tDate - (tDay * 10000)) / 100; |
lpeters | 35:73b3963c6dd3 | 1667 | uint8_t tYear = (tDate - ((tDay * 10000) + (tMon * 100))) + 100; |
lpeters | 35:73b3963c6dd3 | 1668 | uint8_t tHour = tTime / 10000; |
lpeters | 35:73b3963c6dd3 | 1669 | uint8_t tMin = (tTime - (tHour * 10000)) / 100; |
lpeters | 35:73b3963c6dd3 | 1670 | uint8_t tSec = (tTime - ((tHour * 10000) + (tMin * 100))); |
lpeters | 35:73b3963c6dd3 | 1671 | |
lpeters | 35:73b3963c6dd3 | 1672 | ts.tm_year = tYear; |
lpeters | 35:73b3963c6dd3 | 1673 | ts.tm_mon = tMon - 1; |
lpeters | 35:73b3963c6dd3 | 1674 | ts.tm_mday = tDay; |
lpeters | 35:73b3963c6dd3 | 1675 | ts.tm_hour = tHour; |
lpeters | 35:73b3963c6dd3 | 1676 | ts.tm_min = tMin; |
lpeters | 35:73b3963c6dd3 | 1677 | ts.tm_sec = tSec; |
lpeters | 35:73b3963c6dd3 | 1678 | t = mktime(&ts); |
lpeters | 35:73b3963c6dd3 | 1679 | //printf("DAT: %06d TIM: %d\r\n", tDate, tTime); |
lpeters | 35:73b3963c6dd3 | 1680 | //printf("CNV: %04d-%02d-%02d@%02d:%02d:%02d\r\n", tYear, tMon, tDay, tHour, tMin, tSec); |
lpeters | 35:73b3963c6dd3 | 1681 | //printf("T: %d\t%s", t, ctime(&t)); |
lpeters | 35:73b3963c6dd3 | 1682 | printf("GPS: %08X\t%s", t, ctime(&t)); |
lpeters | 35:73b3963c6dd3 | 1683 | mytime = t; |
lpeters | 33:e47306c32791 | 1684 | |
Huot87 | 43:c900cd8ea072 | 1685 | |
Huot87 | 43:c900cd8ea072 | 1686 | // if (fixGood) |
Huot87 | 43:c900cd8ea072 | 1687 | // myLedG = 1; |
Huot87 | 43:c900cd8ea072 | 1688 | // else |
Huot87 | 43:c900cd8ea072 | 1689 | // myLedR = 1; |
Huot87 | 43:c900cd8ea072 | 1690 | // |
Huot87 | 43:c900cd8ea072 | 1691 | // for (int i = 0; i < 10; i++) { |
Huot87 | 43:c900cd8ea072 | 1692 | // myLedB = 1; |
Huot87 | 43:c900cd8ea072 | 1693 | // wait(0.1); |
Huot87 | 43:c900cd8ea072 | 1694 | // myLedB = 0; |
Huot87 | 43:c900cd8ea072 | 1695 | // wait(0.1); |
Huot87 | 43:c900cd8ea072 | 1696 | // } |
Huot87 | 43:c900cd8ea072 | 1697 | |
Huot87 | 38:fd7a407bfe3e | 1698 | |
Huot87 | 38:fd7a407bfe3e | 1699 | if (fixGood) |
Huot87 | 38:fd7a407bfe3e | 1700 | return 0; |
Huot87 | 38:fd7a407bfe3e | 1701 | else |
Huot87 | 38:fd7a407bfe3e | 1702 | return 1; |
mbed_official | 0:7037ed05f54f | 1703 | } |
mbed_official | 0:7037ed05f54f | 1704 | |
lpeters | 37:df69df1bcb1a | 1705 | void ubxRead() |
lpeters | 37:df69df1bcb1a | 1706 | { |
lpeters | 37:df69df1bcb1a | 1707 | uint8_t crcPass = 0; |
lpeters | 37:df69df1bcb1a | 1708 | uint8_t crcFail = 0; |
lpeters | 37:df69df1bcb1a | 1709 | |
lpeters | 37:df69df1bcb1a | 1710 | myLedW = 1; |
lpeters | 37:df69df1bcb1a | 1711 | pos = 0; |
lpeters | 37:df69df1bcb1a | 1712 | ret = 0xFF; |
lpeters | 37:df69df1bcb1a | 1713 | cmd[0] = 0xFF; |
lpeters | 37:df69df1bcb1a | 1714 | i2c.write(NEOM8M_ADR_GPS, cmd, 1); |
lpeters | 37:df69df1bcb1a | 1715 | while (ret == 0xFF) |
lpeters | 37:df69df1bcb1a | 1716 | { |
lpeters | 37:df69df1bcb1a | 1717 | i2c.read(NEOM8M_ADR_GPS, &ret, 1); |
lpeters | 37:df69df1bcb1a | 1718 | } |
lpeters | 37:df69df1bcb1a | 1719 | while (ret != 0xFF) |
lpeters | 37:df69df1bcb1a | 1720 | { |
lpeters | 37:df69df1bcb1a | 1721 | buf[pos++] = ret; |
lpeters | 37:df69df1bcb1a | 1722 | i2c.read(NEOM8M_ADR_GPS, &ret, 1); |
lpeters | 37:df69df1bcb1a | 1723 | } |
lpeters | 37:df69df1bcb1a | 1724 | printf("UBX: | "); |
lpeters | 37:df69df1bcb1a | 1725 | for (int i = 0; i <= pos; i++) { printf("%02X ", buf[i]); } |
lpeters | 37:df69df1bcb1a | 1726 | printf("|\r\n", buf); |
lpeters | 37:df69df1bcb1a | 1727 | myLedW = 0; |
lpeters | 37:df69df1bcb1a | 1728 | //if (crcFail) |
lpeters | 37:df69df1bcb1a | 1729 | // printf("[\u001b[33mWARN\u001b[0m] CRC PASS: %d FAIL: %d\r\n", crcPass, crcFail); |
lpeters | 37:df69df1bcb1a | 1730 | |
lpeters | 37:df69df1bcb1a | 1731 | if (pos > 0) |
lpeters | 37:df69df1bcb1a | 1732 | myLedG = 1; |
lpeters | 37:df69df1bcb1a | 1733 | else |
lpeters | 37:df69df1bcb1a | 1734 | myLedR = 1; |
lpeters | 37:df69df1bcb1a | 1735 | |
lpeters | 37:df69df1bcb1a | 1736 | for (int i = 0; i < 10; i++) { |
lpeters | 37:df69df1bcb1a | 1737 | myLedB = 1; |
lpeters | 37:df69df1bcb1a | 1738 | wait(0.1); |
lpeters | 37:df69df1bcb1a | 1739 | myLedB = 0; |
lpeters | 37:df69df1bcb1a | 1740 | wait(0.1); |
lpeters | 37:df69df1bcb1a | 1741 | } |
lpeters | 37:df69df1bcb1a | 1742 | } |
lpeters | 37:df69df1bcb1a | 1743 | |
Huot87 | 40:eaf476b94684 | 1744 | // HUOT ADDITION -- 08/31/2018 -- Write Packet to EEPROM (on failed send) |
Huot87 | 40:eaf476b94684 | 1745 | void backupPacket() |
Huot87 | 40:eaf476b94684 | 1746 | { |
Huot87 | 40:eaf476b94684 | 1747 | printf("====== IN BACKUP PACKET METHOD ======= \r\n"); |
Huot87 | 40:eaf476b94684 | 1748 | int location = eepromCounter * 11; |
lpeters | 42:d59e50622292 | 1749 | printf("Writing entry #%d to 0x%08d\r\n", eepromCounter, location); |
lpeters | 42:d59e50622292 | 1750 | printf("BUF: |"); |
lpeters | 42:d59e50622292 | 1751 | for (int i = 0; i < PACKET_LEN; i++) { printf("%02X", tx_buffer[i]); } |
lpeters | 42:d59e50622292 | 1752 | printf("|\r\n"); |
Huot87 | 40:eaf476b94684 | 1753 | for (int i = 0; i < 11; i++) |
Huot87 | 40:eaf476b94684 | 1754 | { |
Huot87 | 40:eaf476b94684 | 1755 | writeEEPROMByte(location++, tx_buffer[i]); |
Huot87 | 40:eaf476b94684 | 1756 | } |
Huot87 | 40:eaf476b94684 | 1757 | eepromCounter++; |
Huot87 | 40:eaf476b94684 | 1758 | } |
Huot87 | 40:eaf476b94684 | 1759 | |
Huot87 | 40:eaf476b94684 | 1760 | // HUOT ADDITION -- 08/31/2018 -- Read EEPROM Memory into Buffer |
Huot87 | 40:eaf476b94684 | 1761 | void prepBacklogSend() |
Huot87 | 40:eaf476b94684 | 1762 | { |
Huot87 | 40:eaf476b94684 | 1763 | printf("====== IN PREP BACKLOG SEND METHOD ======= \r\n"); |
Huot87 | 40:eaf476b94684 | 1764 | int location = (eepromCounter - 1) * 11; |
lpeters | 42:d59e50622292 | 1765 | printf("Reading entry #%d from 0x%08d\r\n", eepromCounter - 1, location); |
Huot87 | 40:eaf476b94684 | 1766 | for (int i = 0; i < 11; i++) |
Huot87 | 40:eaf476b94684 | 1767 | { |
Huot87 | 40:eaf476b94684 | 1768 | tx_buffer[i] = readEEPROMByte(location++); |
Huot87 | 40:eaf476b94684 | 1769 | } |
lpeters | 42:d59e50622292 | 1770 | printf("BUF: |"); |
lpeters | 42:d59e50622292 | 1771 | for (int i = 0; i < PACKET_LEN; i++) { printf("%02X", tx_buffer[i]); } |
lpeters | 42:d59e50622292 | 1772 | printf("|\r\n"); |
Huot87 | 40:eaf476b94684 | 1773 | } |
Huot87 | 40:eaf476b94684 | 1774 | |
mbed_official | 0:7037ed05f54f | 1775 | // EOF |