program for conv1

Dependencies:   mbed-src

Committer:
marcus255
Date:
Wed Dec 16 19:42:44 2015 +0000
Revision:
4:9e2adff677ac
Parent:
3:45f63cbde7b3
Reading/Writting EEPROM works fine, Reading/Writting works fine when temperature sensor connected to the i2c bus. Device addresses must be hardcoded for now.

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 4:9e2adff677ac 12 if (addr == RTC_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 4:9e2adff677ac 17 while(!SDA_state) { //writting to slave
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 4:9e2adff677ac 37 else if (addr & 0x01 == 0x01){ //reading from slave
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 3:45f63cbde7b3 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 2:42a084b14fb2 48 }
marcus255 0:ab0f6f4ef7ac 49 }
marcus255 0:ab0f6f4ef7ac 50 }
marcus255 1:455088a984b0 51 if(!slave.write(uart_rec2)) {
marcus255 1:455088a984b0 52 device.putc(END_OF_TRANSMISSION);
marcus255 1:455088a984b0 53 break;
marcus255 1:455088a984b0 54 }
marcus255 0:ab0f6f4ef7ac 55 }
marcus255 1:455088a984b0 56 slave.read();
marcus255 0:ab0f6f4ef7ac 57 }
marcus255 0:ab0f6f4ef7ac 58 else {
marcus255 0:ab0f6f4ef7ac 59 //pc.printf("Unknown address: %x\n\r",addr);
marcus255 0:ab0f6f4ef7ac 60 slave.stop();
marcus255 0:ab0f6f4ef7ac 61 }
marcus255 0:ab0f6f4ef7ac 62 SDA_interrupt.enable_irq();
marcus255 0:ab0f6f4ef7ac 63 }
marcus255 0:ab0f6f4ef7ac 64
marcus255 0:ab0f6f4ef7ac 65 void init(void){
marcus255 0:ab0f6f4ef7ac 66 SDA_interrupt.fall(&i2cStart);
marcus255 0:ab0f6f4ef7ac 67 slave.frequency(100000);
marcus255 0:ab0f6f4ef7ac 68 device.baud(921600);
marcus255 4:9e2adff677ac 69 slave.address(RTC_ADDR);
marcus255 0:ab0f6f4ef7ac 70 }