Marek Trojan / _8_CONV

Dependencies:   mbed-src

Fork of _8_CONV_1_SLAVE by Marek Trojan

Revision:
8:47ae8b74cd43
Parent:
7:e86480c767ea
Child:
9:58c110e686f0
--- a/main.cpp	Thu Dec 17 23:02:07 2015 +0000
+++ b/main.cpp	Fri Dec 18 00:01:28 2015 +0000
@@ -7,6 +7,59 @@
     while (1) {}
 }
 
+void onUartChar()
+{
+    NVIC_DisableIRQ(UART0_IRQn); // TODO: check if this statement is necessary here
+    char first_char = device.getc();
+    if (first_char == SET_CONV2_FUNCT){
+        char slave_address = device.getc(); // not used at all, may be delete latar
+        int i2c_frequency = 1000 * (int)device.getc(); 
+        init_conv2(i2c_frequency);
+    }
+    else if (first_char == DATA_TO_WRITE_CHAR) {
+        char received = device.getc();
+        if (received & 0x01 == 0x01) { // reading from slave
+            char rec;
+            if (!transmission) {
+                i2c.start();
+                transmission = 1;
+            }
+            if (i2c.write((int)rec)) {
+                while (device.getc() == DATA_TO_READ_CHAR) {
+                    rec = (char)i2c.read(1);
+                    device.putc(UART_CHAR_INCOMING);
+                    device.putc(rec);
+                }
+                i2c.read(0);
+            } else {
+                device.putc(UART_NON_CONFIRMATION_CHAR);
+            }
+            device.putc(END_OF_TRANSMISSION);
+        } else { // writing to slave
+            if (!transmission) {
+                i2c.start();
+                transmission = 1;
+            }
+            if (i2c.write((int)received))
+                device.putc(UART_CONFIRMATION_CHAR);
+            else {
+                device.putc(UART_NON_CONFIRMATION_CHAR);
+            }
+            while (device.getc() == DATA_TO_WRITE_CHAR) {
+                received = device.getc();
+                if (i2c.write((int)received))
+                    device.putc(UART_CONFIRMATION_CHAR);
+                else {
+                    device.putc(UART_NON_CONFIRMATION_CHAR);
+                    break;
+                }
+            }
+        }
+    }
+    transmission = 0;
+    NVIC_EnableIRQ(UART0_IRQn); // TODO: check if this statement is necessary here
+}
+
 void on_SDA_falling_slope_interrupt(void)
 {
     SDA_interrupt.disable_irq();
@@ -64,7 +117,7 @@
     device.baud(921600);
     discovered_address = 0;
     clk_count = 0;
-    frequency = I2C_FREQUENCY;
+    device.attach(&onUartChar);
 }
 
 void init_conv1(int frequency, char address)
@@ -74,9 +127,9 @@
     SDA_interrupt.fall(&on_SDA_falling_slope_interrupt);
 }
 
-void init_conv2(void)
+void init_conv2(int frequency)
 {
-
+    i2c.frequency(frequency);
 }
 
 void on_SCL_rising_slope_interrupt(void)
@@ -87,10 +140,10 @@
         clk_count++;
     } else {
         SCL_interrupt.disable_irq();
-        /*//commented for now, as conv2 software stays as it was for now
+        frequency = I2C_FREQUENCY;
         device.putc(SET_CONV2_FUNCT);
         device.putc(discovered_address);
-        device.putc((char)(frequency/1000)); //in kHz */
+        device.putc((char)(frequency/1000)); //in kHz 
         init_conv1(frequency,  (char)discovered_address);
     }
 }
\ No newline at end of file