program for conv1

Dependencies:   mbed-src

Committer:
marcus255
Date:
Sun Dec 06 21:24:27 2015 +0000
Revision:
1:455088a984b0
Parent:
0:ab0f6f4ef7ac
Child:
2:42a084b14fb2
still not complete write-read sequence

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcus255 0:ab0f6f4ef7ac 1 // Conv1 Slave
marcus255 0:ab0f6f4ef7ac 2 #include "main.h"
marcus255 0:ab0f6f4ef7ac 3
marcus255 0:ab0f6f4ef7ac 4 int main() {
marcus255 0:ab0f6f4ef7ac 5 init();
marcus255 0:ab0f6f4ef7ac 6 while (1) {}
marcus255 0:ab0f6f4ef7ac 7 }
marcus255 0:ab0f6f4ef7ac 8
marcus255 0:ab0f6f4ef7ac 9 void i2cStart(void){
marcus255 0:ab0f6f4ef7ac 10 SDA_interrupt.disable_irq();
marcus255 0:ab0f6f4ef7ac 11 char addr = (char)slave.read();
marcus255 0:ab0f6f4ef7ac 12 if (addr == (char)SLAVE_ADDR) {
marcus255 0:ab0f6f4ef7ac 13 count = 0;
marcus255 0:ab0f6f4ef7ac 14 char uart_rec;
marcus255 0:ab0f6f4ef7ac 15 device.putc(DATA_TO_WRITE_CHAR);
marcus255 0:ab0f6f4ef7ac 16 device.putc(addr);
marcus255 0:ab0f6f4ef7ac 17 while(!SDA_state) {
marcus255 0:ab0f6f4ef7ac 18 buffer = (char)slave.read();
marcus255 0:ab0f6f4ef7ac 19 for(int y = 0; y < 1024; y++){} //some delay required for signal establishment
marcus255 0:ab0f6f4ef7ac 20 if(SDA_state) break;
marcus255 0:ab0f6f4ef7ac 21 device.putc(DATA_TO_WRITE_CHAR);
marcus255 0:ab0f6f4ef7ac 22 device.putc(buffer);
marcus255 0:ab0f6f4ef7ac 23
marcus255 0:ab0f6f4ef7ac 24 while(1) { // waiting until confirmation char is received from converter 2
marcus255 0:ab0f6f4ef7ac 25 if(device.readable()) {
marcus255 0:ab0f6f4ef7ac 26 uart_rec = device.getc();
marcus255 0:ab0f6f4ef7ac 27 if(uart_rec == UART_CONFIRMATION_CHAR || uart_rec == UART_NON_CONFIRMATION_CHAR)
marcus255 0:ab0f6f4ef7ac 28 break;
marcus255 0:ab0f6f4ef7ac 29 }
marcus255 0:ab0f6f4ef7ac 30 }
marcus255 0:ab0f6f4ef7ac 31 if (uart_rec == UART_NON_CONFIRMATION_CHAR)
marcus255 0:ab0f6f4ef7ac 32 break;
marcus255 0:ab0f6f4ef7ac 33 }
marcus255 1:455088a984b0 34 //slave.stop();
marcus255 0:ab0f6f4ef7ac 35 device.putc(END_OF_TRANSMISSION);
marcus255 0:ab0f6f4ef7ac 36 }
marcus255 0:ab0f6f4ef7ac 37 else if (addr == (char)(SLAVE_ADDR | 0x01)){
marcus255 0:ab0f6f4ef7ac 38 char uart_rec2;
marcus255 0:ab0f6f4ef7ac 39 device.putc(DATA_TO_WRITE_CHAR);
marcus255 0:ab0f6f4ef7ac 40 device.putc(addr);
marcus255 0:ab0f6f4ef7ac 41 while(1){
marcus255 0:ab0f6f4ef7ac 42 device.putc(DATA_TO_READ_CHAR);
marcus255 1:455088a984b0 43 while(1) { // waiting for data byte from conv2 //seems to w8 4ever...
marcus255 0:ab0f6f4ef7ac 44 if(device.readable()) {
marcus255 0:ab0f6f4ef7ac 45 if (device.getc() == UART_CHAR_INCOMING){
marcus255 0:ab0f6f4ef7ac 46 uart_rec2 = device.getc();
marcus255 0:ab0f6f4ef7ac 47 break;
marcus255 0:ab0f6f4ef7ac 48 }
marcus255 1:455088a984b0 49
marcus255 0:ab0f6f4ef7ac 50 }
marcus255 0:ab0f6f4ef7ac 51 }
marcus255 1:455088a984b0 52 if(!slave.write(uart_rec2)) {
marcus255 1:455088a984b0 53 device.putc(END_OF_TRANSMISSION);
marcus255 1:455088a984b0 54 break;
marcus255 1:455088a984b0 55 }
marcus255 1:455088a984b0 56
marcus255 1:455088a984b0 57
marcus255 0:ab0f6f4ef7ac 58 }
marcus255 1:455088a984b0 59 slave.read();
marcus255 0:ab0f6f4ef7ac 60
marcus255 0:ab0f6f4ef7ac 61 }
marcus255 0:ab0f6f4ef7ac 62 else {
marcus255 0:ab0f6f4ef7ac 63 //pc.printf("Unknown address: %x\n\r",addr);
marcus255 0:ab0f6f4ef7ac 64 slave.stop();
marcus255 0:ab0f6f4ef7ac 65 }
marcus255 0:ab0f6f4ef7ac 66 SDA_interrupt.enable_irq();
marcus255 0:ab0f6f4ef7ac 67 }
marcus255 0:ab0f6f4ef7ac 68
marcus255 0:ab0f6f4ef7ac 69 void init(void){
marcus255 0:ab0f6f4ef7ac 70 SDA_interrupt.fall(&i2cStart);
marcus255 0:ab0f6f4ef7ac 71 slave.frequency(100000);
marcus255 0:ab0f6f4ef7ac 72 device.baud(921600);
marcus255 0:ab0f6f4ef7ac 73 slave.address(SLAVE_ADDR);
marcus255 0:ab0f6f4ef7ac 74 }