SmartMesh QSL for STM32F4 version

Fork of COG-AD4050_QSL by APS Lab

Committer:
APS_Lab
Date:
Thu Jul 12 09:19:12 2018 +0000
Revision:
1:b909b8399252
Parent:
0:8ca1e814a851
SmartMesh for STM32F4 version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
APS_Lab 0:8ca1e814a851 1 #include "mbed.h"
APS_Lab 0:8ca1e814a851 2 /*
APS_Lab 0:8ca1e814a851 3 Copyright (c) 2016, Dust Networks. All rights reserved.
APS_Lab 0:8ca1e814a851 4
APS_Lab 0:8ca1e814a851 5 SimplePublish example application for the mbed OS.
APS_Lab 0:8ca1e814a851 6
APS_Lab 0:8ca1e814a851 7 \license See attached DN_LICENSE.txt.
APS_Lab 0:8ca1e814a851 8 */
APS_Lab 0:8ca1e814a851 9
APS_Lab 0:8ca1e814a851 10 #include "mbed.h"
APS_Lab 0:8ca1e814a851 11 #include "millis.h"
APS_Lab 0:8ca1e814a851 12 #include "dn_qsl_api.h" // Only really need this include from QSL
APS_Lab 0:8ca1e814a851 13 #include "dn_debug.h" // Included to borrow debug macros
APS_Lab 0:8ca1e814a851 14 #include "dn_endianness.h" // Included to borrow array copying
APS_Lab 0:8ca1e814a851 15 #include "dn_time.h" // Included to borrow sleep function
APS_Lab 1:b909b8399252 16 //#include "ADXL362.h"
APS_Lab 0:8ca1e814a851 17
APS_Lab 0:8ca1e814a851 18 #define NETID 0 // Factory default value used if zero (1229)
APS_Lab 0:8ca1e814a851 19 #define JOINKEY NULL // Factory default value used if NULL (44 55 53 54 4E 45 54 57 4F 52 4B 53 52 4F 43 4B)
APS_Lab 0:8ca1e814a851 20 #define BANDWIDTH_MS 500 // Not changed if zero (default base bandwidth given by manager is 9 s)
APS_Lab 0:8ca1e814a851 21 #define SRC_PORT 60000 // Default port used if zero (0xf0b8)
APS_Lab 0:8ca1e814a851 22 #define DEST_PORT 0 // Default port used if zero (0xf0b8)
APS_Lab 0:8ca1e814a851 23 #define DATA_PERIOD_MS 500 // Should be longer than (or equal to) bandwidth
APS_Lab 0:8ca1e814a851 24
APS_Lab 0:8ca1e814a851 25 // We can use debug macros from dn_debug, as stdio defaults to this serial
APS_Lab 1:b909b8399252 26 Serial serialDebug(USBTX, USBRX);
APS_Lab 0:8ca1e814a851 27 #define log_info serialDebug.printf
APS_Lab 0:8ca1e814a851 28
APS_Lab 0:8ca1e814a851 29 // LED2 is the green LED on the NUCLEO-L053R8; might change for other boards
APS_Lab 0:8ca1e814a851 30 DigitalOut myled(LED1);
APS_Lab 0:8ca1e814a851 31 DigitalOut status(LED2);
APS_Lab 1:b909b8399252 32 //ADXL362 Acc(P1_07, P1_08, P1_06, P1_10);
APS_Lab 0:8ca1e814a851 33
APS_Lab 0:8ca1e814a851 34 static uint16_t randomWalk(void);
APS_Lab 0:8ca1e814a851 35 static void parsePayload(const uint8_t *payload, uint8_t size);
APS_Lab 0:8ca1e814a851 36
APS_Lab 1:b909b8399252 37 //void init_acc(void)
APS_Lab 1:b909b8399252 38 //{
APS_Lab 1:b909b8399252 39 // Acc.init_spi();
APS_Lab 1:b909b8399252 40 // Acc.init_adxl362();
APS_Lab 1:b909b8399252 41 //}
APS_Lab 0:8ca1e814a851 42
APS_Lab 0:8ca1e814a851 43 int main()
APS_Lab 0:8ca1e814a851 44 {
APS_Lab 0:8ca1e814a851 45 uint8_t payload[4];
APS_Lab 0:8ca1e814a851 46 uint8_t inboxBuf[DN_DEFAULT_PAYLOAD_SIZE_LIMIT];
APS_Lab 0:8ca1e814a851 47 uint8_t bytesRead;
APS_Lab 0:8ca1e814a851 48 uint8_t i,e;
APS_Lab 1:b909b8399252 49 //int8_t x0, y0, z0;
APS_Lab 0:8ca1e814a851 50
APS_Lab 0:8ca1e814a851 51 myled=1;
APS_Lab 0:8ca1e814a851 52 status=1;
APS_Lab 0:8ca1e814a851 53
APS_Lab 0:8ca1e814a851 54 for(e=0;e<10;e++)
APS_Lab 0:8ca1e814a851 55 {
APS_Lab 0:8ca1e814a851 56 myled=!myled;
APS_Lab 0:8ca1e814a851 57 status=!status;
APS_Lab 0:8ca1e814a851 58 wait(0.5);
APS_Lab 0:8ca1e814a851 59 }
APS_Lab 0:8ca1e814a851 60
APS_Lab 0:8ca1e814a851 61 myled=1;
APS_Lab 0:8ca1e814a851 62 status=1;
APS_Lab 0:8ca1e814a851 63
APS_Lab 1:b909b8399252 64 //init_acc();
APS_Lab 0:8ca1e814a851 65 // Configure SysTick for timing
APS_Lab 0:8ca1e814a851 66 millisStart();
APS_Lab 0:8ca1e814a851 67
APS_Lab 0:8ca1e814a851 68 // Set PC debug serial baudrate
APS_Lab 1:b909b8399252 69 serialDebug.baud(115200);
APS_Lab 1:b909b8399252 70 serialDebug.printf("STM32F4 SmartMesh Demo\n");
APS_Lab 0:8ca1e814a851 71
APS_Lab 1:b909b8399252 72 log_info("Initializing...");
APS_Lab 0:8ca1e814a851 73 dn_qsl_init();
APS_Lab 0:8ca1e814a851 74
APS_Lab 0:8ca1e814a851 75 // Flash LED to indicate start-up complete
APS_Lab 0:8ca1e814a851 76 for (i = 0; i < 10; i++)
APS_Lab 0:8ca1e814a851 77 {
APS_Lab 0:8ca1e814a851 78 myled = !myled;
APS_Lab 0:8ca1e814a851 79 dn_sleep_ms(50);
APS_Lab 0:8ca1e814a851 80 }
APS_Lab 0:8ca1e814a851 81 myled=0;
APS_Lab 0:8ca1e814a851 82
APS_Lab 0:8ca1e814a851 83 while(TRUE) {
APS_Lab 0:8ca1e814a851 84 if (dn_qsl_isConnected())
APS_Lab 0:8ca1e814a851 85 {
APS_Lab 1:b909b8399252 86 //Acc.ACC_GetXYZ8(&x0, &y0, &z0);
APS_Lab 1:b909b8399252 87 //payload[0]=x0;
APS_Lab 1:b909b8399252 88 //payload[1]=y0;
APS_Lab 1:b909b8399252 89 //payload[2]=z0;
APS_Lab 1:b909b8399252 90 uint16_t val = randomWalk();
APS_Lab 0:8ca1e814a851 91 static uint8_t count = 0;
APS_Lab 0:8ca1e814a851 92 myled = 1; // Turn off LED during send/read
APS_Lab 0:8ca1e814a851 93 status = 0;
APS_Lab 1:b909b8399252 94 dn_write_uint16_t(payload, val);
APS_Lab 1:b909b8399252 95 payload[0] = count;
APS_Lab 0:8ca1e814a851 96
APS_Lab 0:8ca1e814a851 97 if (dn_qsl_send(payload, sizeof (payload), DEST_PORT))
APS_Lab 0:8ca1e814a851 98 {
APS_Lab 1:b909b8399252 99 log_info("Sent message nr %u: %u", count, val);
APS_Lab 0:8ca1e814a851 100 count++;
APS_Lab 0:8ca1e814a851 101 status=1;
APS_Lab 0:8ca1e814a851 102 } else
APS_Lab 0:8ca1e814a851 103 {
APS_Lab 1:b909b8399252 104 log_info("Send failed");
APS_Lab 0:8ca1e814a851 105 status=0;
APS_Lab 0:8ca1e814a851 106 }
APS_Lab 0:8ca1e814a851 107
APS_Lab 0:8ca1e814a851 108 do
APS_Lab 0:8ca1e814a851 109 {
APS_Lab 0:8ca1e814a851 110 bytesRead = dn_qsl_read(inboxBuf);
APS_Lab 0:8ca1e814a851 111 parsePayload(inboxBuf, bytesRead);
APS_Lab 0:8ca1e814a851 112 } while (bytesRead > 0);
APS_Lab 0:8ca1e814a851 113
APS_Lab 0:8ca1e814a851 114 myled = 1; // Turn on LED
APS_Lab 0:8ca1e814a851 115 dn_sleep_ms(DATA_PERIOD_MS);
APS_Lab 0:8ca1e814a851 116 } else
APS_Lab 0:8ca1e814a851 117 {
APS_Lab 1:b909b8399252 118 log_info("Connecting...");
APS_Lab 0:8ca1e814a851 119 myled = 1; // Not connected; turn off LED
APS_Lab 0:8ca1e814a851 120 if (dn_qsl_connect(NETID, JOINKEY, SRC_PORT, BANDWIDTH_MS))
APS_Lab 0:8ca1e814a851 121 {
APS_Lab 0:8ca1e814a851 122 myled = 0; // Connected; turn on LED
APS_Lab 0:8ca1e814a851 123 status=1;//log_info("Connected to network");
APS_Lab 0:8ca1e814a851 124 } else
APS_Lab 0:8ca1e814a851 125 {
APS_Lab 1:b909b8399252 126 log_info("Failed to connect");
APS_Lab 0:8ca1e814a851 127 }
APS_Lab 0:8ca1e814a851 128 }
APS_Lab 0:8ca1e814a851 129 status=0;
APS_Lab 0:8ca1e814a851 130 wait(0.5);
APS_Lab 0:8ca1e814a851 131 status=1;
APS_Lab 0:8ca1e814a851 132 }
APS_Lab 0:8ca1e814a851 133 }
APS_Lab 0:8ca1e814a851 134
APS_Lab 0:8ca1e814a851 135 static uint16_t randomWalk(void)
APS_Lab 0:8ca1e814a851 136 {
APS_Lab 0:8ca1e814a851 137 static bool first = TRUE;
APS_Lab 0:8ca1e814a851 138 static uint16_t lastValue = 0x7fff; // Start in middle of uint16 range
APS_Lab 0:8ca1e814a851 139 const int powerLevel = 9001;
APS_Lab 0:8ca1e814a851 140
APS_Lab 0:8ca1e814a851 141 // Seed random number generator on first call
APS_Lab 0:8ca1e814a851 142 if (first)
APS_Lab 0:8ca1e814a851 143 {
APS_Lab 0:8ca1e814a851 144 first = FALSE;
APS_Lab 0:8ca1e814a851 145 srand(dn_time_ms());
APS_Lab 0:8ca1e814a851 146 }
APS_Lab 0:8ca1e814a851 147
APS_Lab 0:8ca1e814a851 148 // Random walk within +/- powerLevel
APS_Lab 0:8ca1e814a851 149 lastValue += rand() / (RAND_MAX / (2*powerLevel) + 1) - powerLevel;
APS_Lab 0:8ca1e814a851 150 return lastValue;
APS_Lab 0:8ca1e814a851 151 }
APS_Lab 0:8ca1e814a851 152
APS_Lab 0:8ca1e814a851 153 static void parsePayload(const uint8_t *payload, uint8_t size)
APS_Lab 0:8ca1e814a851 154 {
APS_Lab 0:8ca1e814a851 155 uint8_t i;
APS_Lab 0:8ca1e814a851 156 char msg[size + 1];
APS_Lab 0:8ca1e814a851 157
APS_Lab 0:8ca1e814a851 158 if (size == 0)
APS_Lab 0:8ca1e814a851 159 {
APS_Lab 0:8ca1e814a851 160 // Nothing to parse
APS_Lab 0:8ca1e814a851 161 return;
APS_Lab 0:8ca1e814a851 162 }
APS_Lab 0:8ca1e814a851 163
APS_Lab 0:8ca1e814a851 164 // Parse bytes individually as well as together as a string
APS_Lab 0:8ca1e814a851 165 //log_info("Received downstream payload of %u bytes:", size);
APS_Lab 0:8ca1e814a851 166 for (i = 0; i < size; i++)
APS_Lab 0:8ca1e814a851 167 {
APS_Lab 0:8ca1e814a851 168 msg[i] = payload[i];
APS_Lab 1:b909b8399252 169 log_info("\tByte# %03u: %#.2x (%u)", i, payload[i], payload[i]);
APS_Lab 0:8ca1e814a851 170 }
APS_Lab 0:8ca1e814a851 171 msg[size] = '\0';
APS_Lab 1:b909b8399252 172 log_info("\tMessage: %s", msg);
APS_Lab 0:8ca1e814a851 173 }