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.
Diff: main.cpp
- Revision:
- 1:466049963f1f
- Parent:
- 0:63fac75e5afc
- Child:
- 2:bdfce41aae53
--- a/main.cpp Wed Oct 20 03:43:59 2021 +0000
+++ b/main.cpp Thu Nov 04 21:09:53 2021 +0000
@@ -5,7 +5,9 @@
#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
@@ -15,20 +17,55 @@
#define BSY PB_5
#define CNV PB_6
-#define DATA_LEN 144 //bits of data we will transmit 6-ch 24 bits/ch
-#define BUFF_SIZE 9 // 9, 16-bit words will come through
+#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
-uint16_t rx_buff[DATA_LEN]; //want to read 144 bits (literally bits)
+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
@@ -37,31 +74,41 @@
- decode the data
- repeat
*/
-
void read_data() {
- //request conversion
- CNV_PIN=1;
- wait_us(60);
- CNV_PIN=0;
- //WAIT FOR BSY --> bsy_LTC
- //while(bsy_LTC==1){}
- //then ask for data
- //cs_LTC=0;
- //spi id register
- //LTC_CHIP.write(0x00); // --> do we need this?
- //read 144 bytes
- /*
+ 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;
+ }
- int bytecount = 0;
- while(bytecount < BUFF_SIZE){
- rx_buff[bytecount] = LTC_CHIP.write(0x00);
- bytecount++;
+ //PACK THE STRUCT
+
+ for(int i = 0; i < BUFF_SIZE; i++)
+ {
+ ((uint8_t*)(&adc_data))[i] = rx_buff[i];
}
- */
- //lift CS
- //cs_LTC=1;
//WRITE THIS TO A STRUCT OF SOME SORT but we need to delete the bits we don't care about
}
@@ -71,7 +118,6 @@
{
// Initialise the digital pin LED1 as an output
DigitalOut led(LED1);
- cs_LTC=0;
CNV_PIN=0;
//setup pc
@@ -79,24 +125,63 @@
pc.printf("------STARTUP------\n\n\n");
//setup SPI
- LTC_CHIP.format(16, 0);
- LTC_CHIP.frequency(1000000); //60Mhz clock frequency
+ 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(ð);
+ 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
- /*
- pc.printf("first int: %d\n", rx_buff[0]); //just check the first "word" for now b/c it should be correct
- pc.printf("second int: %d\n", rx_buff[1]);
- pc.printf("third int: %d\n", rx_buff[2]);
- pc.printf("fourth int: %d\n", rx_buff[3]);
- pc.printf("fifth int: %d\n", rx_buff[4]);
- pc.printf("sixth int: %d\n", rx_buff[5]);
- pc.printf("seventh int: %d\n", rx_buff[6]);
- pc.printf("eight int: %d\n", rx_buff[7]);
- */
- //wait for 1 second
- wait(1);
+
+
+ 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();
}
