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@5:d622f8a4a016, 2015-12-17 (annotated)
- Committer:
- marcus255
- Date:
- Thu Dec 17 00:03:01 2015 +0000
- Revision:
- 5:d622f8a4a016
- Parent:
- 4:9e2adff677ac
- Child:
- 6:b9578f756c9c
hardware address getting - not work for now;
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 | 5:d622f8a4a016 | 4 | int main() |
| marcus255 | 5:d622f8a4a016 | 5 | { |
| marcus255 | 0:ab0f6f4ef7ac | 6 | init(); |
| marcus255 | 0:ab0f6f4ef7ac | 7 | while (1) {} |
| marcus255 | 0:ab0f6f4ef7ac | 8 | } |
| marcus255 | 0:ab0f6f4ef7ac | 9 | |
| marcus255 | 5:d622f8a4a016 | 10 | void i2cStart(void) |
| marcus255 | 5:d622f8a4a016 | 11 | { |
| marcus255 | 0:ab0f6f4ef7ac | 12 | SDA_interrupt.disable_irq(); |
| marcus255 | 0:ab0f6f4ef7ac | 13 | char addr = (char)slave.read(); |
| marcus255 | 5:d622f8a4a016 | 14 | if (addr & 0x01 == 0x01) { //reading from slave |
| marcus255 | 5:d622f8a4a016 | 15 | char uart_rec2; |
| marcus255 | 5:d622f8a4a016 | 16 | device.putc(DATA_TO_WRITE_CHAR); |
| marcus255 | 5:d622f8a4a016 | 17 | device.putc(addr); |
| marcus255 | 5:d622f8a4a016 | 18 | while(1) { |
| marcus255 | 5:d622f8a4a016 | 19 | device.putc(DATA_TO_READ_CHAR); |
| marcus255 | 5:d622f8a4a016 | 20 | while(1) { // waiting for data byte from conv2 |
| marcus255 | 5:d622f8a4a016 | 21 | if(device.readable()) { |
| marcus255 | 5:d622f8a4a016 | 22 | if (device.getc() == UART_CHAR_INCOMING) { |
| marcus255 | 5:d622f8a4a016 | 23 | uart_rec2 = device.getc(); |
| marcus255 | 5:d622f8a4a016 | 24 | break; |
| marcus255 | 5:d622f8a4a016 | 25 | } |
| marcus255 | 5:d622f8a4a016 | 26 | } |
| marcus255 | 5:d622f8a4a016 | 27 | } |
| marcus255 | 5:d622f8a4a016 | 28 | if(!slave.write(uart_rec2)) { |
| marcus255 | 5:d622f8a4a016 | 29 | device.putc(END_OF_TRANSMISSION); |
| marcus255 | 5:d622f8a4a016 | 30 | break; |
| marcus255 | 5:d622f8a4a016 | 31 | } |
| marcus255 | 5:d622f8a4a016 | 32 | } |
| marcus255 | 5:d622f8a4a016 | 33 | slave.read(); |
| marcus255 | 5:d622f8a4a016 | 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 | 5:d622f8a4a016 | 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 | 5:d622f8a4a016 | 44 | device.putc(buffer); |
| marcus255 | 5:d622f8a4a016 | 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 | 5:d622f8a4a016 | 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 | 5:d622f8a4a016 | 61 | void init(void) |
| marcus255 | 5:d622f8a4a016 | 62 | { |
| marcus255 | 5:d622f8a4a016 | 63 | device.baud(921600); |
| marcus255 | 5:d622f8a4a016 | 64 | SDA_interrupt.fall(&on_SDA_falling_slope_interrupt); |
| marcus255 | 5:d622f8a4a016 | 65 | } |
| marcus255 | 5:d622f8a4a016 | 66 | |
| marcus255 | 5:d622f8a4a016 | 67 | void on_SDA_falling_slope_interrupt(void) |
| marcus255 | 5:d622f8a4a016 | 68 | { |
| marcus255 | 5:d622f8a4a016 | 69 | SDA_interrupt.disable_irq(); |
| marcus255 | 5:d622f8a4a016 | 70 | int prev_state = 1; |
| marcus255 | 5:d622f8a4a016 | 71 | int temp_state; |
| marcus255 | 5:d622f8a4a016 | 72 | uint8_t address = 0; |
| marcus255 | 5:d622f8a4a016 | 73 | for (int i = 7; i >= 0; i--) //for every clock cycle in frame |
| marcus255 | 5:d622f8a4a016 | 74 | { |
| marcus255 | 5:d622f8a4a016 | 75 | while(1) //sample CLK line state |
| marcus255 | 5:d622f8a4a016 | 76 | { |
| marcus255 | 5:d622f8a4a016 | 77 | if ((temp_state = SCL_state) == prev_state) |
| marcus255 | 5:d622f8a4a016 | 78 | continue; |
| marcus255 | 5:d622f8a4a016 | 79 | else |
| marcus255 | 5:d622f8a4a016 | 80 | { |
| marcus255 | 5:d622f8a4a016 | 81 | if (SDA_state == 1) |
| marcus255 | 5:d622f8a4a016 | 82 | address |= (0x01 << i); |
| marcus255 | 5:d622f8a4a016 | 83 | prev_state = temp_state; |
| marcus255 | 5:d622f8a4a016 | 84 | //device.putc((char)SDA_state); |
| marcus255 | 5:d622f8a4a016 | 85 | break; |
| marcus255 | 5:d622f8a4a016 | 86 | } |
| marcus255 | 5:d622f8a4a016 | 87 | } |
| marcus255 | 5:d622f8a4a016 | 88 | } |
| marcus255 | 5:d622f8a4a016 | 89 | |
| marcus255 | 5:d622f8a4a016 | 90 | // now we (probably) know the Slave address, so we can initialize i2c bus |
| marcus255 | 5:d622f8a4a016 | 91 | // and set this Conv as Conv1 and send info to second Conv to act as Conv2 |
| marcus255 | 5:d622f8a4a016 | 92 | |
| marcus255 | 5:d622f8a4a016 | 93 | //device.putc(SET_CONV2_FUNCT); |
| marcus255 | 5:d622f8a4a016 | 94 | device.putc(address); |
| marcus255 | 5:d622f8a4a016 | 95 | //init_I2C(address, 100000); |
| marcus255 | 5:d622f8a4a016 | 96 | SDA_interrupt.enable_irq(); |
| marcus255 | 5:d622f8a4a016 | 97 | } |
| marcus255 | 5:d622f8a4a016 | 98 | |
| marcus255 | 5:d622f8a4a016 | 99 | void init_I2C(char addr, int freq) |
| marcus255 | 5:d622f8a4a016 | 100 | { |
| marcus255 | 0:ab0f6f4ef7ac | 101 | SDA_interrupt.fall(&i2cStart); |
| marcus255 | 5:d622f8a4a016 | 102 | slave.address(addr); |
| marcus255 | 5:d622f8a4a016 | 103 | slave.frequency(freq); |
| marcus255 | 0:ab0f6f4ef7ac | 104 | } |
