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@7:e86480c767ea, 2015-12-17 (annotated)
- Committer:
- marcus255
- Date:
- Thu Dec 17 23:02:07 2015 +0000
- Revision:
- 7:e86480c767ea
- Parent:
- 4:9e2adff677ac
- Child:
- 8:47ae8b74cd43
conv1 just before including conv2 code
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| marcus255 | 0:ab0f6f4ef7ac | 1 | // Conv1 Slave |
| marcus255 | 0:ab0f6f4ef7ac | 2 | #include "main.h" |
| marcus255 | 0:ab0f6f4ef7ac | 3 | |
| marcus255 | 7:e86480c767ea | 4 | int main() |
| marcus255 | 7:e86480c767ea | 5 | { |
| marcus255 | 7:e86480c767ea | 6 | init_conv(); |
| marcus255 | 0:ab0f6f4ef7ac | 7 | while (1) {} |
| marcus255 | 0:ab0f6f4ef7ac | 8 | } |
| marcus255 | 0:ab0f6f4ef7ac | 9 | |
| marcus255 | 7:e86480c767ea | 10 | void on_SDA_falling_slope_interrupt(void) |
| marcus255 | 7:e86480c767ea | 11 | { |
| marcus255 | 0:ab0f6f4ef7ac | 12 | SDA_interrupt.disable_irq(); |
| marcus255 | 0:ab0f6f4ef7ac | 13 | char addr = (char)slave.read(); |
| marcus255 | 7:e86480c767ea | 14 | if (addr & 0x01 == 0x01) { //reading from slave |
| marcus255 | 7:e86480c767ea | 15 | char uart_rec2; |
| marcus255 | 7:e86480c767ea | 16 | device.putc(DATA_TO_WRITE_CHAR); |
| marcus255 | 7:e86480c767ea | 17 | device.putc(addr); |
| marcus255 | 7:e86480c767ea | 18 | while(1) { |
| marcus255 | 7:e86480c767ea | 19 | device.putc(DATA_TO_READ_CHAR); |
| marcus255 | 7:e86480c767ea | 20 | while(1) { // waiting for data byte from conv2 |
| marcus255 | 7:e86480c767ea | 21 | if(device.readable()) { |
| marcus255 | 7:e86480c767ea | 22 | if (device.getc() == UART_CHAR_INCOMING) { |
| marcus255 | 7:e86480c767ea | 23 | uart_rec2 = device.getc(); |
| marcus255 | 7:e86480c767ea | 24 | break; |
| marcus255 | 7:e86480c767ea | 25 | } |
| marcus255 | 7:e86480c767ea | 26 | } |
| marcus255 | 7:e86480c767ea | 27 | } |
| marcus255 | 7:e86480c767ea | 28 | if(!slave.write(uart_rec2)) { |
| marcus255 | 7:e86480c767ea | 29 | device.putc(END_OF_TRANSMISSION); |
| marcus255 | 7:e86480c767ea | 30 | break; |
| marcus255 | 7:e86480c767ea | 31 | } |
| marcus255 | 7:e86480c767ea | 32 | } |
| marcus255 | 7:e86480c767ea | 33 | slave.read(); |
| marcus255 | 7:e86480c767ea | 34 | } else { |
| marcus255 | 0:ab0f6f4ef7ac | 35 | count = 0; |
| marcus255 | 0:ab0f6f4ef7ac | 36 | char uart_rec; |
| marcus255 | 0:ab0f6f4ef7ac | 37 | device.putc(DATA_TO_WRITE_CHAR); |
| marcus255 | 0:ab0f6f4ef7ac | 38 | device.putc(addr); |
| marcus255 | 4:9e2adff677ac | 39 | while(!SDA_state) { //writting to slave |
| marcus255 | 0:ab0f6f4ef7ac | 40 | buffer = (char)slave.read(); |
| marcus255 | 7:e86480c767ea | 41 | for(int y = 0; y < 1024; y++) {} //some delay required for signal establishment |
| marcus255 | 0:ab0f6f4ef7ac | 42 | if(SDA_state) break; |
| marcus255 | 0:ab0f6f4ef7ac | 43 | device.putc(DATA_TO_WRITE_CHAR); |
| marcus255 | 7:e86480c767ea | 44 | device.putc(buffer); |
| marcus255 | 7:e86480c767ea | 45 | |
| marcus255 | 0:ab0f6f4ef7ac | 46 | while(1) { // waiting until confirmation char is received from converter 2 |
| marcus255 | 0:ab0f6f4ef7ac | 47 | if(device.readable()) { |
| marcus255 | 0:ab0f6f4ef7ac | 48 | uart_rec = device.getc(); |
| marcus255 | 0:ab0f6f4ef7ac | 49 | if(uart_rec == UART_CONFIRMATION_CHAR || uart_rec == UART_NON_CONFIRMATION_CHAR) |
| marcus255 | 0:ab0f6f4ef7ac | 50 | break; |
| marcus255 | 0:ab0f6f4ef7ac | 51 | } |
| marcus255 | 0:ab0f6f4ef7ac | 52 | } |
| marcus255 | 0:ab0f6f4ef7ac | 53 | if (uart_rec == UART_NON_CONFIRMATION_CHAR) |
| marcus255 | 0:ab0f6f4ef7ac | 54 | break; |
| marcus255 | 7:e86480c767ea | 55 | } |
| marcus255 | 0:ab0f6f4ef7ac | 56 | device.putc(END_OF_TRANSMISSION); |
| marcus255 | 0:ab0f6f4ef7ac | 57 | } |
| marcus255 | 0:ab0f6f4ef7ac | 58 | SDA_interrupt.enable_irq(); |
| marcus255 | 0:ab0f6f4ef7ac | 59 | } |
| marcus255 | 0:ab0f6f4ef7ac | 60 | |
| marcus255 | 7:e86480c767ea | 61 | void init_conv(void) |
| marcus255 | 7:e86480c767ea | 62 | { |
| marcus255 | 7:e86480c767ea | 63 | SCL_interrupt.rise(&on_SCL_rising_slope_interrupt); |
| marcus255 | 0:ab0f6f4ef7ac | 64 | device.baud(921600); |
| marcus255 | 7:e86480c767ea | 65 | discovered_address = 0; |
| marcus255 | 7:e86480c767ea | 66 | clk_count = 0; |
| marcus255 | 7:e86480c767ea | 67 | frequency = I2C_FREQUENCY; |
| marcus255 | 7:e86480c767ea | 68 | } |
| marcus255 | 7:e86480c767ea | 69 | |
| marcus255 | 7:e86480c767ea | 70 | void init_conv1(int frequency, char address) |
| marcus255 | 7:e86480c767ea | 71 | { |
| marcus255 | 7:e86480c767ea | 72 | slave.frequency(frequency); |
| marcus255 | 7:e86480c767ea | 73 | slave.address(address); |
| marcus255 | 7:e86480c767ea | 74 | SDA_interrupt.fall(&on_SDA_falling_slope_interrupt); |
| marcus255 | 7:e86480c767ea | 75 | } |
| marcus255 | 7:e86480c767ea | 76 | |
| marcus255 | 7:e86480c767ea | 77 | void init_conv2(void) |
| marcus255 | 7:e86480c767ea | 78 | { |
| marcus255 | 7:e86480c767ea | 79 | |
| marcus255 | 7:e86480c767ea | 80 | } |
| marcus255 | 7:e86480c767ea | 81 | |
| marcus255 | 7:e86480c767ea | 82 | void on_SCL_rising_slope_interrupt(void) |
| marcus255 | 7:e86480c767ea | 83 | { |
| marcus255 | 7:e86480c767ea | 84 | if (clk_count<9) { |
| marcus255 | 7:e86480c767ea | 85 | if (SDA_state) |
| marcus255 | 7:e86480c767ea | 86 | discovered_address |= (0x01<<(7-clk_count)); |
| marcus255 | 7:e86480c767ea | 87 | clk_count++; |
| marcus255 | 7:e86480c767ea | 88 | } else { |
| marcus255 | 7:e86480c767ea | 89 | SCL_interrupt.disable_irq(); |
| marcus255 | 7:e86480c767ea | 90 | /*//commented for now, as conv2 software stays as it was for now |
| marcus255 | 7:e86480c767ea | 91 | device.putc(SET_CONV2_FUNCT); |
| marcus255 | 7:e86480c767ea | 92 | device.putc(discovered_address); |
| marcus255 | 7:e86480c767ea | 93 | device.putc((char)(frequency/1000)); //in kHz */ |
| marcus255 | 7:e86480c767ea | 94 | init_conv1(frequency, (char)discovered_address); |
| marcus255 | 7:e86480c767ea | 95 | } |
| marcus255 | 0:ab0f6f4ef7ac | 96 | } |
