Aditya Mehrotra / Mbed OS Sensor_Training_BoardV2

main.cpp

Committer:
adimmit
Date:
2021-11-04
Revision:
1:466049963f1f
Parent:
0:63fac75e5afc
Child:
2:bdfce41aae53

File content as of revision 1:466049963f1f:

/* mbed Microcontroller Library
 * Copyright (c) 2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "platform/mbed_thread.h"
#include "ltc_message.h"
#include <SPI.h>
#include "EthernetInterface.h"

//SETUP THE LTC CHIP
#define LTC_MOSI    PB_2
#define LTC_MISO    PC_11
#define LTC_CLK     PC_10
#define LTC_CS      PA_15
#define BSY         PB_5
#define CNV         PB_6

#define BUFF_SIZE   24   // 24, 8-bit words will come through
#define NUM_CHANNELS   8   // 8 total channels
#define CH_SIZE   3   // 3 words / channel

SPI LTC_CHIP(LTC_MOSI, LTC_MISO, LTC_CLK);
DigitalOut cs_LTC(LTC_CS);
DigitalIn bsy_LTC(BSY);
DigitalOut CNV_PIN(CNV);

float CONV_FACTOR = 0.00031294782; //--> for -10.24 --> 10.24V

//SETUP REGISTERS
uint8_t rx_buff[BUFF_SIZE]; //each is an 8-bit word 
ltc_spi adc_data;

//setup SERIAL
Serial       pc(USBTX, USBRX);

//Setup ETHERNET
// Set up ethernet connection
const int SERVER_PORT = 2;
const char* SERVER_ADDRESS = "192.168.1.2";    // Adress of the other Mbed (Mbed B)
const int LOCAL_PORT = 1;
const char* ip = "192.168.1.1";     // Mbed A = 1; Mbed B = 2
const char* mask = "255.255.255.0";
const char* gateway = "192.168.1.10";
EthernetInterface eth; // network stack
SocketAddress local; // local address
SocketAddress client; // client address (connection to other board)
UDPSocket server; // UDP socket (peripheral on this board)

// overall loop time
#define DT 0.001f // 0.005f is 200Hz
//Setup Timer
Timer t;

//MESSAGE TO SEND 
//float msg[6];
int16_t msg[6];
char send_buf[36];
//server.sendto(client, send_buf, sizeof(send_buf)-1); --> command to send
/*msg[0] = thumb_prox.read_u16()/64;
        msg[1] = thumb_dist.read_u16()/64; 
        msg[2] = index_prox.read_u16()/64; 
        msg[3] = index_dist.read_u16()/64; 
        msg[4] = mid_prox.read_u16()/64; 
        msg[5] = mid_dist.read_u16()/64; */ //--> populating the message


/*
We need to 
- pull CS low
- read the data into a register
- release CS
- decode the data
- repeat
*/
void read_data() {
    
    for (int i = 0; i<NUM_CHANNELS; i++) {
        //request conversion
        CNV_PIN=1;
        wait_ns(60); // wait for 60ns
        CNV_PIN=0;
        //WAIT FOR BSY --> bsy_LTC
        while(bsy_LTC==1){}
        //debugging ONLY
        wait_us(1);
        //then ask for data
        cs_LTC=0;
        //spi id register
        //LTC_CHIP.write(0x00); // --> do we need this? 
        //read 144 bytes
        
        //read data
        int bytecount = CH_SIZE*i;
        while(bytecount < CH_SIZE*(1+i)){
            rx_buff[bytecount] = LTC_CHIP.write(0x00);
            bytecount++;
        }
        
        //lift CS
        cs_LTC=1;
    }
    
    //PACK THE STRUCT
    
    for(int i = 0; i < BUFF_SIZE; i++)
    {
        ((uint8_t*)(&adc_data))[i] = rx_buff[i];
    }
    

    //WRITE THIS TO A STRUCT OF SOME SORT but we need to delete the bits we don't care about
}


int main()
{
    // Initialise the digital pin LED1 as an output
    DigitalOut led(LED1);
    CNV_PIN=0;
    
    //setup pc
    pc.baud(115200);
    pc.printf("------STARTUP------\n\n\n");
    
    //setup SPI
    LTC_CHIP.format(8, 0);
    LTC_CHIP.frequency(10000); //60Mhz clock frequency 
    
    //setup ETHERNET
    eth.set_network(ip, mask, gateway);
    eth.connect();
    
    //more ETHERNET
    
    client.set_port(SERVER_PORT);
    client.set_ip_address(SERVER_ADDRESS);
    local.set_port(LOCAL_PORT);
    local.set_ip_address(ip);
    int code = server.open(&eth);
    if(code!=0) { pc.printf("Error from opening server = %d\n\r",code); }    
    code = server.bind(local);
    if(code!=0) { pc.printf("Error from binding socket = %d\n\r",code); }
    
    //start timer
    t.start();
    

    while (true) {
        //timer reset
        t.reset();
        //read and print data to serial terminal
        read_data();
        //print the spi bytes
        
        
        msg[0] = adc_data.channel[0].cnv_upper<<8 | adc_data.channel[0].cnv_lower;
        msg[1] = adc_data.channel[1].cnv_upper<<8 | adc_data.channel[1].cnv_lower;
        msg[2] = adc_data.channel[2].cnv_upper<<8 | adc_data.channel[2].cnv_lower;
        msg[3] = adc_data.channel[3].cnv_upper<<8 | adc_data.channel[3].cnv_lower;
        msg[4] = adc_data.channel[4].cnv_upper<<8 | adc_data.channel[4].cnv_lower;
        msg[5] = adc_data.channel[5].cnv_upper<<8 | adc_data.channel[5].cnv_lower;
        
        //uint16_t conv_1_z = adc_data.channel[3].info;
        //int16_t output = adc_data.channel[3].cnv_upper<<8 | adc_data.channel[3].cnv_lower;
        
        //convert to float --> 2's Complement Number from -10.24V --> 10.24V
        //float cnv1 = CONV_FACTOR*(float)output; //--> CONVERT ON THE PYTHON SIDE 
        
        //POPULATE THE ETHERNET MESSAGE
        sprintf(send_buf, "%d,%d,%d,%d,%d,%d", msg[0],msg[1],msg[2],msg[3],msg[4],msg[5]);
        server.sendto(client, send_buf, sizeof(send_buf)-1); // send message
        
        
        //pc.printf("conversion: %x vs %f\n", output, cnv1); //just check the first "word" for now b/c it should be correct
        //pc.printf("zero_check: %d\n", conv_1_z);
        
        //wait for DT second
        //wait(1);
        while(t.read()<DT){;}
    }
    
    // Terminate connection (if you want)
    server.close();
    eth.disconnect();
}