program for conv1, universal converter before including conv2 code
Fork of _8_CONV_1_SLAVE by
main.cpp@11:3f1020fcd090, 2016-01-08 (annotated)
- Committer:
- marcus255
- Date:
- Fri Jan 08 13:47:03 2016 +0000
- Revision:
- 11:3f1020fcd090
- Parent:
- 10:7eaae60dc9e4
Latest version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcus255 | 9:58c110e686f0 | 1 | /* |
marcus255 | 9:58c110e686f0 | 2 | * Project: I2C to UART converter |
marcus255 | 11:3f1020fcd090 | 3 | * File: main.cpp |
marcus255 | 11:3f1020fcd090 | 4 | * Author: Marek Trojan |
marcus255 | 9:58c110e686f0 | 5 | */ |
marcus255 | 9:58c110e686f0 | 6 | |
marcus255 | 0:ab0f6f4ef7ac | 7 | #include "main.h" |
marcus255 | 0:ab0f6f4ef7ac | 8 | |
marcus255 | 7:e86480c767ea | 9 | int main() |
marcus255 | 7:e86480c767ea | 10 | { |
marcus255 | 7:e86480c767ea | 11 | init_conv(); |
marcus255 | 0:ab0f6f4ef7ac | 12 | while (1) {} |
marcus255 | 0:ab0f6f4ef7ac | 13 | } |
marcus255 | 0:ab0f6f4ef7ac | 14 | |
marcus255 | 8:47ae8b74cd43 | 15 | void onUartChar() |
marcus255 | 8:47ae8b74cd43 | 16 | { |
marcus255 | 8:47ae8b74cd43 | 17 | char first_char = device.getc(); |
marcus255 | 10:7eaae60dc9e4 | 18 | if (first_char == SET_CONV2_FUNCT) { |
marcus255 | 11:3f1020fcd090 | 19 | char slave_address = device.getc(); // not used at all, may be delete later |
marcus255 | 9:58c110e686f0 | 20 | init_conv2(I2C_FREQUENCY_STANDARD); |
marcus255 | 10:7eaae60dc9e4 | 21 | } else if (first_char == DATA_TO_WRITE_CHAR) { |
marcus255 | 8:47ae8b74cd43 | 22 | char received = device.getc(); |
marcus255 | 8:47ae8b74cd43 | 23 | if (received & 0x01 == 0x01) { // reading from slave |
marcus255 | 8:47ae8b74cd43 | 24 | char rec; |
marcus255 | 8:47ae8b74cd43 | 25 | if (!transmission) { |
marcus255 | 8:47ae8b74cd43 | 26 | i2c.start(); |
marcus255 | 8:47ae8b74cd43 | 27 | transmission = 1; |
marcus255 | 8:47ae8b74cd43 | 28 | } |
marcus255 | 10:7eaae60dc9e4 | 29 | if (i2c.write((int)rec)) { // TODO: check if here should be received instead of rec |
marcus255 | 8:47ae8b74cd43 | 30 | while (device.getc() == DATA_TO_READ_CHAR) { |
marcus255 | 8:47ae8b74cd43 | 31 | rec = (char)i2c.read(1); |
marcus255 | 8:47ae8b74cd43 | 32 | device.putc(UART_CHAR_INCOMING); |
marcus255 | 8:47ae8b74cd43 | 33 | device.putc(rec); |
marcus255 | 8:47ae8b74cd43 | 34 | } |
marcus255 | 8:47ae8b74cd43 | 35 | i2c.read(0); |
marcus255 | 8:47ae8b74cd43 | 36 | } else { |
marcus255 | 8:47ae8b74cd43 | 37 | device.putc(UART_NON_CONFIRMATION_CHAR); |
marcus255 | 8:47ae8b74cd43 | 38 | } |
marcus255 | 8:47ae8b74cd43 | 39 | device.putc(END_OF_TRANSMISSION); |
marcus255 | 8:47ae8b74cd43 | 40 | } else { // writing to slave |
marcus255 | 8:47ae8b74cd43 | 41 | if (!transmission) { |
marcus255 | 8:47ae8b74cd43 | 42 | i2c.start(); |
marcus255 | 8:47ae8b74cd43 | 43 | transmission = 1; |
marcus255 | 8:47ae8b74cd43 | 44 | } |
marcus255 | 8:47ae8b74cd43 | 45 | if (i2c.write((int)received)) |
marcus255 | 8:47ae8b74cd43 | 46 | device.putc(UART_CONFIRMATION_CHAR); |
marcus255 | 8:47ae8b74cd43 | 47 | else { |
marcus255 | 8:47ae8b74cd43 | 48 | device.putc(UART_NON_CONFIRMATION_CHAR); |
marcus255 | 8:47ae8b74cd43 | 49 | } |
marcus255 | 8:47ae8b74cd43 | 50 | while (device.getc() == DATA_TO_WRITE_CHAR) { |
marcus255 | 8:47ae8b74cd43 | 51 | received = device.getc(); |
marcus255 | 8:47ae8b74cd43 | 52 | if (i2c.write((int)received)) |
marcus255 | 8:47ae8b74cd43 | 53 | device.putc(UART_CONFIRMATION_CHAR); |
marcus255 | 8:47ae8b74cd43 | 54 | else { |
marcus255 | 8:47ae8b74cd43 | 55 | device.putc(UART_NON_CONFIRMATION_CHAR); |
marcus255 | 8:47ae8b74cd43 | 56 | break; |
marcus255 | 8:47ae8b74cd43 | 57 | } |
marcus255 | 8:47ae8b74cd43 | 58 | } |
marcus255 | 8:47ae8b74cd43 | 59 | } |
marcus255 | 8:47ae8b74cd43 | 60 | } |
marcus255 | 9:58c110e686f0 | 61 | i2c.stop(); |
marcus255 | 8:47ae8b74cd43 | 62 | transmission = 0; |
marcus255 | 8:47ae8b74cd43 | 63 | } |
marcus255 | 8:47ae8b74cd43 | 64 | |
marcus255 | 7:e86480c767ea | 65 | void on_SDA_falling_slope_interrupt(void) |
marcus255 | 7:e86480c767ea | 66 | { |
marcus255 | 0:ab0f6f4ef7ac | 67 | SDA_interrupt.disable_irq(); |
marcus255 | 0:ab0f6f4ef7ac | 68 | char addr = (char)slave.read(); |
marcus255 | 7:e86480c767ea | 69 | if (addr & 0x01 == 0x01) { //reading from slave |
marcus255 | 7:e86480c767ea | 70 | char uart_rec2; |
marcus255 | 7:e86480c767ea | 71 | device.putc(DATA_TO_WRITE_CHAR); |
marcus255 | 7:e86480c767ea | 72 | device.putc(addr); |
marcus255 | 7:e86480c767ea | 73 | while(1) { |
marcus255 | 7:e86480c767ea | 74 | device.putc(DATA_TO_READ_CHAR); |
marcus255 | 7:e86480c767ea | 75 | while(1) { // waiting for data byte from conv2 |
marcus255 | 7:e86480c767ea | 76 | if(device.readable()) { |
marcus255 | 7:e86480c767ea | 77 | if (device.getc() == UART_CHAR_INCOMING) { |
marcus255 | 7:e86480c767ea | 78 | uart_rec2 = device.getc(); |
marcus255 | 7:e86480c767ea | 79 | break; |
marcus255 | 7:e86480c767ea | 80 | } |
marcus255 | 7:e86480c767ea | 81 | } |
marcus255 | 7:e86480c767ea | 82 | } |
marcus255 | 7:e86480c767ea | 83 | if(!slave.write(uart_rec2)) { |
marcus255 | 7:e86480c767ea | 84 | device.putc(END_OF_TRANSMISSION); |
marcus255 | 7:e86480c767ea | 85 | break; |
marcus255 | 7:e86480c767ea | 86 | } |
marcus255 | 7:e86480c767ea | 87 | } |
marcus255 | 7:e86480c767ea | 88 | slave.read(); |
marcus255 | 7:e86480c767ea | 89 | } else { |
marcus255 | 0:ab0f6f4ef7ac | 90 | count = 0; |
marcus255 | 0:ab0f6f4ef7ac | 91 | char uart_rec; |
marcus255 | 0:ab0f6f4ef7ac | 92 | device.putc(DATA_TO_WRITE_CHAR); |
marcus255 | 0:ab0f6f4ef7ac | 93 | device.putc(addr); |
marcus255 | 4:9e2adff677ac | 94 | while(!SDA_state) { //writting to slave |
marcus255 | 0:ab0f6f4ef7ac | 95 | buffer = (char)slave.read(); |
marcus255 | 7:e86480c767ea | 96 | for(int y = 0; y < 1024; y++) {} //some delay required for signal establishment |
marcus255 | 0:ab0f6f4ef7ac | 97 | if(SDA_state) break; |
marcus255 | 0:ab0f6f4ef7ac | 98 | device.putc(DATA_TO_WRITE_CHAR); |
marcus255 | 7:e86480c767ea | 99 | device.putc(buffer); |
marcus255 | 7:e86480c767ea | 100 | |
marcus255 | 0:ab0f6f4ef7ac | 101 | while(1) { // waiting until confirmation char is received from converter 2 |
marcus255 | 0:ab0f6f4ef7ac | 102 | if(device.readable()) { |
marcus255 | 0:ab0f6f4ef7ac | 103 | uart_rec = device.getc(); |
marcus255 | 0:ab0f6f4ef7ac | 104 | if(uart_rec == UART_CONFIRMATION_CHAR || uart_rec == UART_NON_CONFIRMATION_CHAR) |
marcus255 | 0:ab0f6f4ef7ac | 105 | break; |
marcus255 | 0:ab0f6f4ef7ac | 106 | } |
marcus255 | 0:ab0f6f4ef7ac | 107 | } |
marcus255 | 0:ab0f6f4ef7ac | 108 | if (uart_rec == UART_NON_CONFIRMATION_CHAR) |
marcus255 | 0:ab0f6f4ef7ac | 109 | break; |
marcus255 | 7:e86480c767ea | 110 | } |
marcus255 | 0:ab0f6f4ef7ac | 111 | device.putc(END_OF_TRANSMISSION); |
marcus255 | 0:ab0f6f4ef7ac | 112 | } |
marcus255 | 0:ab0f6f4ef7ac | 113 | SDA_interrupt.enable_irq(); |
marcus255 | 0:ab0f6f4ef7ac | 114 | } |
marcus255 | 0:ab0f6f4ef7ac | 115 | |
marcus255 | 7:e86480c767ea | 116 | void init_conv(void) |
marcus255 | 7:e86480c767ea | 117 | { |
marcus255 | 9:58c110e686f0 | 118 | SDA_state.mode(PullUp); |
marcus255 | 9:58c110e686f0 | 119 | SDA_interrupt.mode(PullUp); |
marcus255 | 9:58c110e686f0 | 120 | SCL_interrupt.mode(PullNone); |
marcus255 | 7:e86480c767ea | 121 | SCL_interrupt.rise(&on_SCL_rising_slope_interrupt); |
marcus255 | 0:ab0f6f4ef7ac | 122 | device.baud(921600); |
marcus255 | 7:e86480c767ea | 123 | discovered_address = 0; |
marcus255 | 7:e86480c767ea | 124 | clk_count = 0; |
marcus255 | 8:47ae8b74cd43 | 125 | device.attach(&onUartChar); |
marcus255 | 7:e86480c767ea | 126 | } |
marcus255 | 7:e86480c767ea | 127 | |
marcus255 | 7:e86480c767ea | 128 | void init_conv1(int frequency, char address) |
marcus255 | 7:e86480c767ea | 129 | { |
marcus255 | 7:e86480c767ea | 130 | slave.frequency(frequency); |
marcus255 | 7:e86480c767ea | 131 | slave.address(address); |
marcus255 | 7:e86480c767ea | 132 | SDA_interrupt.fall(&on_SDA_falling_slope_interrupt); |
marcus255 | 7:e86480c767ea | 133 | } |
marcus255 | 7:e86480c767ea | 134 | |
marcus255 | 8:47ae8b74cd43 | 135 | void init_conv2(int frequency) |
marcus255 | 7:e86480c767ea | 136 | { |
marcus255 | 8:47ae8b74cd43 | 137 | i2c.frequency(frequency); |
marcus255 | 9:58c110e686f0 | 138 | SCL_interrupt.rise(NULL); |
marcus255 | 7:e86480c767ea | 139 | } |
marcus255 | 7:e86480c767ea | 140 | |
marcus255 | 7:e86480c767ea | 141 | void on_SCL_rising_slope_interrupt(void) |
marcus255 | 7:e86480c767ea | 142 | { |
marcus255 | 7:e86480c767ea | 143 | if (clk_count<9) { |
marcus255 | 7:e86480c767ea | 144 | if (SDA_state) |
marcus255 | 7:e86480c767ea | 145 | discovered_address |= (0x01<<(7-clk_count)); |
marcus255 | 7:e86480c767ea | 146 | clk_count++; |
marcus255 | 7:e86480c767ea | 147 | } else { |
marcus255 | 9:58c110e686f0 | 148 | SCL_interrupt.rise(NULL); |
marcus255 | 7:e86480c767ea | 149 | device.putc(SET_CONV2_FUNCT); |
marcus255 | 7:e86480c767ea | 150 | device.putc(discovered_address); |
marcus255 | 9:58c110e686f0 | 151 | init_conv1(I2C_FREQUENCY_STANDARD, (char)discovered_address); |
marcus255 | 7:e86480c767ea | 152 | } |
marcus255 | 0:ab0f6f4ef7ac | 153 | } |