program for conv1

Dependencies:   mbed-src

Committer:
marcus255
Date:
Tue Dec 01 22:15:52 2015 +0000
Revision:
0:ab0f6f4ef7ac
Child:
1:455088a984b0
writing ok, reading in progress

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 0:ab0f6f4ef7ac 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 0:ab0f6f4ef7ac 43 while(1) { // waiting for data byte from conv2
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 0:ab0f6f4ef7ac 49 }
marcus255 0:ab0f6f4ef7ac 50 }
marcus255 0:ab0f6f4ef7ac 51 if(!slave.write(uart_rec2))
marcus255 0:ab0f6f4ef7ac 52 break;
marcus255 0:ab0f6f4ef7ac 53 }
marcus255 0:ab0f6f4ef7ac 54 slave.stop();
marcus255 0:ab0f6f4ef7ac 55
marcus255 0:ab0f6f4ef7ac 56 }
marcus255 0:ab0f6f4ef7ac 57 else {
marcus255 0:ab0f6f4ef7ac 58 //pc.printf("Unknown address: %x\n\r",addr);
marcus255 0:ab0f6f4ef7ac 59 slave.stop();
marcus255 0:ab0f6f4ef7ac 60 }
marcus255 0:ab0f6f4ef7ac 61 SDA_interrupt.enable_irq();
marcus255 0:ab0f6f4ef7ac 62 }
marcus255 0:ab0f6f4ef7ac 63
marcus255 0:ab0f6f4ef7ac 64 void init(void){
marcus255 0:ab0f6f4ef7ac 65 SDA_interrupt.fall(&i2cStart);
marcus255 0:ab0f6f4ef7ac 66 slave.frequency(100000);
marcus255 0:ab0f6f4ef7ac 67 device.baud(921600);
marcus255 0:ab0f6f4ef7ac 68 slave.address(SLAVE_ADDR);
marcus255 0:ab0f6f4ef7ac 69 }