Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of _8_CONV_1_SLAVE by
main.cpp@9:58c110e686f0, 2015-12-25 (annotated)
- Committer:
- marcus255
- Date:
- Fri Dec 25 21:33:52 2015 +0000
- Revision:
- 9:58c110e686f0
- Parent:
- 8:47ae8b74cd43
- Child:
- 10:7eaae60dc9e4
Final version of universal converter
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 | 9:58c110e686f0 | 3 | * File: main.cpp |
| marcus255 | 9:58c110e686f0 | 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 | 8:47ae8b74cd43 | 18 | if (first_char == SET_CONV2_FUNCT){ |
| marcus255 | 8:47ae8b74cd43 | 19 | char slave_address = device.getc(); // not used at all, may be delete latar |
| marcus255 | 9:58c110e686f0 | 20 | init_conv2(I2C_FREQUENCY_STANDARD); |
| marcus255 | 8:47ae8b74cd43 | 21 | } |
| marcus255 | 8:47ae8b74cd43 | 22 | else if (first_char == DATA_TO_WRITE_CHAR) { |
| marcus255 | 8:47ae8b74cd43 | 23 | char received = device.getc(); |
| marcus255 | 8:47ae8b74cd43 | 24 | if (received & 0x01 == 0x01) { // reading from slave |
| marcus255 | 8:47ae8b74cd43 | 25 | char rec; |
| marcus255 | 8:47ae8b74cd43 | 26 | if (!transmission) { |
| marcus255 | 8:47ae8b74cd43 | 27 | i2c.start(); |
| marcus255 | 8:47ae8b74cd43 | 28 | transmission = 1; |
| marcus255 | 8:47ae8b74cd43 | 29 | } |
| marcus255 | 8:47ae8b74cd43 | 30 | if (i2c.write((int)rec)) { |
| marcus255 | 8:47ae8b74cd43 | 31 | while (device.getc() == DATA_TO_READ_CHAR) { |
| marcus255 | 8:47ae8b74cd43 | 32 | rec = (char)i2c.read(1); |
| marcus255 | 8:47ae8b74cd43 | 33 | device.putc(UART_CHAR_INCOMING); |
| marcus255 | 8:47ae8b74cd43 | 34 | device.putc(rec); |
| marcus255 | 8:47ae8b74cd43 | 35 | } |
| marcus255 | 8:47ae8b74cd43 | 36 | i2c.read(0); |
| marcus255 | 8:47ae8b74cd43 | 37 | } else { |
| marcus255 | 8:47ae8b74cd43 | 38 | device.putc(UART_NON_CONFIRMATION_CHAR); |
| marcus255 | 8:47ae8b74cd43 | 39 | } |
| marcus255 | 8:47ae8b74cd43 | 40 | device.putc(END_OF_TRANSMISSION); |
| marcus255 | 8:47ae8b74cd43 | 41 | } else { // writing to slave |
| marcus255 | 8:47ae8b74cd43 | 42 | if (!transmission) { |
| marcus255 | 8:47ae8b74cd43 | 43 | i2c.start(); |
| marcus255 | 8:47ae8b74cd43 | 44 | transmission = 1; |
| marcus255 | 8:47ae8b74cd43 | 45 | } |
| marcus255 | 8:47ae8b74cd43 | 46 | if (i2c.write((int)received)) |
| marcus255 | 8:47ae8b74cd43 | 47 | device.putc(UART_CONFIRMATION_CHAR); |
| marcus255 | 8:47ae8b74cd43 | 48 | else { |
| marcus255 | 8:47ae8b74cd43 | 49 | device.putc(UART_NON_CONFIRMATION_CHAR); |
| marcus255 | 8:47ae8b74cd43 | 50 | } |
| marcus255 | 8:47ae8b74cd43 | 51 | while (device.getc() == DATA_TO_WRITE_CHAR) { |
| marcus255 | 8:47ae8b74cd43 | 52 | received = device.getc(); |
| marcus255 | 8:47ae8b74cd43 | 53 | if (i2c.write((int)received)) |
| marcus255 | 8:47ae8b74cd43 | 54 | device.putc(UART_CONFIRMATION_CHAR); |
| marcus255 | 8:47ae8b74cd43 | 55 | else { |
| marcus255 | 8:47ae8b74cd43 | 56 | device.putc(UART_NON_CONFIRMATION_CHAR); |
| marcus255 | 8:47ae8b74cd43 | 57 | break; |
| marcus255 | 8:47ae8b74cd43 | 58 | } |
| marcus255 | 8:47ae8b74cd43 | 59 | } |
| marcus255 | 8:47ae8b74cd43 | 60 | } |
| marcus255 | 8:47ae8b74cd43 | 61 | } |
| marcus255 | 9:58c110e686f0 | 62 | i2c.stop(); |
| marcus255 | 8:47ae8b74cd43 | 63 | transmission = 0; |
| marcus255 | 8:47ae8b74cd43 | 64 | } |
| marcus255 | 8:47ae8b74cd43 | 65 | |
| marcus255 | 7:e86480c767ea | 66 | void on_SDA_falling_slope_interrupt(void) |
| marcus255 | 7:e86480c767ea | 67 | { |
| marcus255 | 0:ab0f6f4ef7ac | 68 | SDA_interrupt.disable_irq(); |
| marcus255 | 0:ab0f6f4ef7ac | 69 | char addr = (char)slave.read(); |
| marcus255 | 7:e86480c767ea | 70 | if (addr & 0x01 == 0x01) { //reading from slave |
| marcus255 | 7:e86480c767ea | 71 | char uart_rec2; |
| marcus255 | 7:e86480c767ea | 72 | device.putc(DATA_TO_WRITE_CHAR); |
| marcus255 | 7:e86480c767ea | 73 | device.putc(addr); |
| marcus255 | 7:e86480c767ea | 74 | while(1) { |
| marcus255 | 7:e86480c767ea | 75 | device.putc(DATA_TO_READ_CHAR); |
| marcus255 | 7:e86480c767ea | 76 | while(1) { // waiting for data byte from conv2 |
| marcus255 | 7:e86480c767ea | 77 | if(device.readable()) { |
| marcus255 | 7:e86480c767ea | 78 | if (device.getc() == UART_CHAR_INCOMING) { |
| marcus255 | 7:e86480c767ea | 79 | uart_rec2 = device.getc(); |
| marcus255 | 7:e86480c767ea | 80 | break; |
| marcus255 | 7:e86480c767ea | 81 | } |
| marcus255 | 7:e86480c767ea | 82 | } |
| marcus255 | 7:e86480c767ea | 83 | } |
| marcus255 | 7:e86480c767ea | 84 | if(!slave.write(uart_rec2)) { |
| marcus255 | 7:e86480c767ea | 85 | device.putc(END_OF_TRANSMISSION); |
| marcus255 | 7:e86480c767ea | 86 | break; |
| marcus255 | 7:e86480c767ea | 87 | } |
| marcus255 | 7:e86480c767ea | 88 | } |
| marcus255 | 7:e86480c767ea | 89 | slave.read(); |
| marcus255 | 7:e86480c767ea | 90 | } else { |
| marcus255 | 0:ab0f6f4ef7ac | 91 | count = 0; |
| marcus255 | 0:ab0f6f4ef7ac | 92 | char uart_rec; |
| marcus255 | 0:ab0f6f4ef7ac | 93 | device.putc(DATA_TO_WRITE_CHAR); |
| marcus255 | 0:ab0f6f4ef7ac | 94 | device.putc(addr); |
| marcus255 | 4:9e2adff677ac | 95 | while(!SDA_state) { //writting to slave |
| marcus255 | 0:ab0f6f4ef7ac | 96 | buffer = (char)slave.read(); |
| marcus255 | 7:e86480c767ea | 97 | for(int y = 0; y < 1024; y++) {} //some delay required for signal establishment |
| marcus255 | 0:ab0f6f4ef7ac | 98 | if(SDA_state) break; |
| marcus255 | 0:ab0f6f4ef7ac | 99 | device.putc(DATA_TO_WRITE_CHAR); |
| marcus255 | 7:e86480c767ea | 100 | device.putc(buffer); |
| marcus255 | 7:e86480c767ea | 101 | |
| marcus255 | 0:ab0f6f4ef7ac | 102 | while(1) { // waiting until confirmation char is received from converter 2 |
| marcus255 | 0:ab0f6f4ef7ac | 103 | if(device.readable()) { |
| marcus255 | 0:ab0f6f4ef7ac | 104 | uart_rec = device.getc(); |
| marcus255 | 0:ab0f6f4ef7ac | 105 | if(uart_rec == UART_CONFIRMATION_CHAR || uart_rec == UART_NON_CONFIRMATION_CHAR) |
| marcus255 | 0:ab0f6f4ef7ac | 106 | break; |
| marcus255 | 0:ab0f6f4ef7ac | 107 | } |
| marcus255 | 0:ab0f6f4ef7ac | 108 | } |
| marcus255 | 0:ab0f6f4ef7ac | 109 | if (uart_rec == UART_NON_CONFIRMATION_CHAR) |
| marcus255 | 0:ab0f6f4ef7ac | 110 | break; |
| marcus255 | 7:e86480c767ea | 111 | } |
| marcus255 | 0:ab0f6f4ef7ac | 112 | device.putc(END_OF_TRANSMISSION); |
| marcus255 | 0:ab0f6f4ef7ac | 113 | } |
| marcus255 | 0:ab0f6f4ef7ac | 114 | SDA_interrupt.enable_irq(); |
| marcus255 | 0:ab0f6f4ef7ac | 115 | } |
| marcus255 | 0:ab0f6f4ef7ac | 116 | |
| marcus255 | 7:e86480c767ea | 117 | void init_conv(void) |
| marcus255 | 7:e86480c767ea | 118 | { |
| marcus255 | 9:58c110e686f0 | 119 | SDA_state.mode(PullUp); |
| marcus255 | 9:58c110e686f0 | 120 | SDA_interrupt.mode(PullUp); |
| marcus255 | 9:58c110e686f0 | 121 | SCL_interrupt.mode(PullNone); |
| marcus255 | 7:e86480c767ea | 122 | SCL_interrupt.rise(&on_SCL_rising_slope_interrupt); |
| marcus255 | 0:ab0f6f4ef7ac | 123 | device.baud(921600); |
| marcus255 | 7:e86480c767ea | 124 | discovered_address = 0; |
| marcus255 | 7:e86480c767ea | 125 | clk_count = 0; |
| marcus255 | 8:47ae8b74cd43 | 126 | device.attach(&onUartChar); |
| marcus255 | 7:e86480c767ea | 127 | } |
| marcus255 | 7:e86480c767ea | 128 | |
| marcus255 | 7:e86480c767ea | 129 | void init_conv1(int frequency, char address) |
| marcus255 | 7:e86480c767ea | 130 | { |
| marcus255 | 7:e86480c767ea | 131 | slave.frequency(frequency); |
| marcus255 | 7:e86480c767ea | 132 | slave.address(address); |
| marcus255 | 7:e86480c767ea | 133 | SDA_interrupt.fall(&on_SDA_falling_slope_interrupt); |
| marcus255 | 7:e86480c767ea | 134 | } |
| marcus255 | 7:e86480c767ea | 135 | |
| marcus255 | 8:47ae8b74cd43 | 136 | void init_conv2(int frequency) |
| marcus255 | 7:e86480c767ea | 137 | { |
| marcus255 | 8:47ae8b74cd43 | 138 | i2c.frequency(frequency); |
| marcus255 | 9:58c110e686f0 | 139 | SCL_interrupt.rise(NULL); |
| marcus255 | 7:e86480c767ea | 140 | } |
| marcus255 | 7:e86480c767ea | 141 | |
| marcus255 | 7:e86480c767ea | 142 | void on_SCL_rising_slope_interrupt(void) |
| marcus255 | 7:e86480c767ea | 143 | { |
| marcus255 | 7:e86480c767ea | 144 | if (clk_count<9) { |
| marcus255 | 7:e86480c767ea | 145 | if (SDA_state) |
| marcus255 | 7:e86480c767ea | 146 | discovered_address |= (0x01<<(7-clk_count)); |
| marcus255 | 7:e86480c767ea | 147 | clk_count++; |
| marcus255 | 7:e86480c767ea | 148 | } else { |
| marcus255 | 9:58c110e686f0 | 149 | SCL_interrupt.rise(NULL); |
| marcus255 | 7:e86480c767ea | 150 | device.putc(SET_CONV2_FUNCT); |
| marcus255 | 7:e86480c767ea | 151 | device.putc(discovered_address); |
| marcus255 | 9:58c110e686f0 | 152 | init_conv1(I2C_FREQUENCY_STANDARD, (char)discovered_address); |
| marcus255 | 7:e86480c767ea | 153 | } |
| marcus255 | 0:ab0f6f4ef7ac | 154 | } |
