Maxim Integrated / Mbed OS MAX1473_RF_Receiver_Demo

Dependencies:   max32630fthr USBDevice

Revision:
11:72363a906638
Parent:
10:aec2b8ba06a0
--- a/main.cpp	Fri Aug 14 19:18:42 2020 +0000
+++ b/main.cpp	Fri Sep 04 18:02:33 2020 +0000
@@ -1,5 +1,5 @@
 /*******************************************************************************
-* Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
+* Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
@@ -29,7 +29,23 @@
 * property whatsoever. Maxim Integrated Products, Inc. retains all
 * ownership rights.
 *******************************************************************************
+  
+This is code for the MAX1473 RF Receiver circuit. This program will capture,
+translate, and unencrypt messages received using the ForwardErrCorr.h format.
+The example uses MAX30208 temperature data as an example.
+
+Hardware Setup and connections:
+
+    MAX32630FTHR->  MAX1473 Ev-Kit
+    
+    3.3V        ->  VDD
+    GND         ->  GND
+    P3_0        ->  DATA_OUT
+    1.8V -> 200KOhm -> TP2
+    
+*******************************************************************************
 */
+
 #include "mbed.h"
 #include "max32630fthr.h"
 #include "USBSerial.h"
@@ -38,16 +54,18 @@
 
 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
 
-// Hardware serial port over DAPLink
-Serial daplink(USBTX, USBRX);          // Use USB debug probe for serial link
-Serial uart(P2_1, P2_0);
-RawSerial uart2(P3_1,P3_0);//tx,rx
+//Read UART Data
+RawSerial uart(P3_1,P3_0);//tx,rx
 
 I2C i2c(P3_4, P3_5);  //sda,scl
 
+//Temperature Sensor Object to convert raw temp data to degress C
 MAX30208 TempSensor(i2c, 0x50); //Constructor takes 7-bit slave adrs
-char  TransTable[] = {0x1F,0x18,0x06,0x01};
-Translator transRx(uart2, TransTable);
+
+
+char  TransTable[] = {0x1F,0x18,0x06,0x01}; //Used to translate data for FEC -- Make sure it is identical to transmitter
+#define SymmetricKey "RfIsCoOl" //Set Symmetric Key here -- Make sure it is identical to transmitter
+Translator transRx(SymmetricKey, TransTable);
 
 // Virtual serial port over USB
 USBSerial microUSB; 
@@ -58,76 +76,106 @@
 DigitalIn TakeData2(P5_5);
 DigitalIn sw1(SW1);
 
-void buildArray(int j, char *input,char *output){
+//*****************************************************************************
+int buildArray(int j, char *input,char *output){
     int i = j+1;
     int k = 0;
-    while(input[i] != 'c'){
+    while(input[i] != 'c' && i < 50){
         output[k] = input[i];
         i++;
         k++;
     }
+    if(input[i] == 'c'){
+        return(0);
+    }
+    return(1);
 }
 char dataIn[50];
 
+//*****************************************************************************
 void SerialCallback(){
-    //Timer timer;
-    //timer.start();
-    wait_ms(1);
-    int i = strlen(dataIn);
+    int i = strlen(dataIn);     //Make sure not to overflow the storage
     if (i >= 50){
         i = 0;
     }
-    while(uart2.readable()){
-        dataIn[i] = uart2.getc();
+    
+    //Read Data while it is available on RX pin
+    while(uart.readable()){
+        dataIn[i] = uart.getc();
         i++;
     }
-    //wait_ms(10);
 }
 
-// main() runs in its own thread in the OS
-// (note the calls to Thread::wait below for delays)
+//*****************************************************************************
+
 int main()
 {
+    //Turn on LEDS
     rLED = LED_ON;
     gLED = LED_ON;
     bLED = LED_OFF;
-    uart2.baud(9600);
-    uart2.attach(&SerialCallback);
     rLED = LED_OFF;
-    char dataOut[50];
-    uint16_t output[10];
+    
+    //Uart Settings
+    uart.baud(9600);                //Set baud to 9600
+    uart.attach(&SerialCallback);   //Set intterupt function to read uart data from reciever
+
+    char dataOut[50];       //Hold incoming messages            
+    uint16_t output[5];    //Hold packet information after decoding
+    printf("Program Begin\r\n");
+    
+    //Runs continuously after initial setup
     while(1) {
-        if (strlen(dataIn) >= 12){
-            int j = 0;
-            while(dataIn[j] != 'b' && j < 30){
-                j++;
+        if (strlen(dataIn) >= 14){              //Wait until at least 1 packet has been recorded
+            wait_ms(50);                        //Short pause to ensure interrupt function has completed data read
+            
+            //Find the start of the packet
+            int start = 0;
+            while(dataIn[j] != 'b' && j < 34){
+                start++;
             }
-            if (dataIn[j] == 'b'){
-                microUSB.printf("\r\n\r\n\r\n\r\n");
-                buildArray(j,dataIn,dataOut);
-                microUSB.printf("\r\n");
-                if (transRx.Reconstruct(dataOut,output) == 1){
-                    microUSB.printf("Error reconstructing\r\n");
+            
+            //Check that starting character was found
+            if (dataIn[start] == 'b'){
+                printf("Building Array\r\n");
+                
+                //Build array that holds only one data packet
+                if (buildArray(start,dataIn,dataOut)==0){
+                    microUSB.printf("\r\n");
+                    
+                    //Decrypt the packet
+                    if (transRx.Decrypt(dataOut,output) == 1){
+                        printf("Error reconstructing\r\n");
+                    }//if
+                    
+                    //Print out the data value, Device ID, Device Type, and Temperature
+                    printf("Decoded Data Value: %i\r\n",output[0]);
+                    printf("Device Type: %c\r\n", dataIn[j+1]);
+                    printf("Device ID: %i\r\n", dataIn[j+2]);
+                    float clesius = TempSensor.toCelsius(output[0]);
+                    float fare = TempSensor.toFahrenheit(clesius);
+                    printf("Temperature: %f F\r\n",fare);
+                    printf("Temperature: %f C",clesius);
+                    printf("\r\n\r\n");
+                }//if
+                else{
+                    
+                    //If the end character cannot be found, clear the data and listen for new messages
+                    printf("error building array\r\n");
                 }
                 
-                //Print out the Device Type, ID, and Temperature
-                microUSB.printf("Device Type: %c\r\n", dataIn[j+1]);
-                microUSB.printf("Device ID: %c\r\n", dataIn[j+2]);
-                
-                //uint16_t temp1 = (output[0]<<8);
-                //uint16_t delivery = (temp1 + output[1]);
-                //float clesius = TempSensor.toCelsius(output[0]);
-                //float fare = TempSensor.toFahrenheit(clesius);
-                microUSB.printf("Data read %u\r\n",output[0]);
+                //Clear the data array after the data has been decrypted
                 for (int i = 0;i<sizeof(dataIn);i++){
                     dataIn[i] = 0;
-                }
-            }
+                }//for
+            }//if
+            
+            //If start character cannot be found, clear array before listening for more data
             else{
                 for (int i = 0;i<sizeof(dataIn);i++){
                     dataIn[i] = 0;
-                }
-            }
-        }
+                }//for
+            }//else
+        }//if
     }
 }