Marek Trojan
/
_8_CONV_2_MASTER
program for conv2
Fork of I2C_HelloWorld_Mbed by
main.cpp@4:35e714396141, 2015-12-01 (annotated)
- Committer:
- marcus255
- Date:
- Tue Dec 01 22:15:31 2015 +0000
- Revision:
- 4:35e714396141
- Parent:
- 3:df6232c70efd
- Child:
- 5:759439e9e976
writing ok, reading in progress
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcus255 | 4:35e714396141 | 1 | // Conv2 Master |
marcus255 | 4:35e714396141 | 2 | #include "main.h" |
marcus255 | 4:35e714396141 | 3 | |
marcus255 | 4:35e714396141 | 4 | int main() { |
marcus255 | 4:35e714396141 | 5 | init(); |
marcus255 | 4:35e714396141 | 6 | //conv2.printf("conv2 says: Hello World!\n\r"); |
marcus255 | 4:35e714396141 | 7 | while (1) {} |
marcus255 | 4:35e714396141 | 8 | } |
mbed_official | 0:f76c26307f9a | 9 | |
marcus255 | 4:35e714396141 | 10 | void onUartChar(){ |
marcus255 | 4:35e714396141 | 11 | NVIC_DisableIRQ(UART0_IRQn); |
marcus255 | 4:35e714396141 | 12 | if (conv2.getc() == DATA_TO_WRITE_CHAR){ |
marcus255 | 4:35e714396141 | 13 | char received = conv2.getc(); |
marcus255 | 4:35e714396141 | 14 | //conv2.putc(received); |
marcus255 | 4:35e714396141 | 15 | if (received == SLAVE_ADDR){ // writing to slave |
marcus255 | 4:35e714396141 | 16 | if (!transmission) { |
marcus255 | 4:35e714396141 | 17 | i2c.start(); |
marcus255 | 4:35e714396141 | 18 | transmission = 1; |
marcus255 | 4:35e714396141 | 19 | } |
marcus255 | 4:35e714396141 | 20 | if (i2c.write((int)received)) |
marcus255 | 4:35e714396141 | 21 | conv2.putc(UART_CONFIRMATION_CHAR); |
marcus255 | 4:35e714396141 | 22 | else { |
marcus255 | 4:35e714396141 | 23 | conv2.putc(UART_NON_CONFIRMATION_CHAR); |
marcus255 | 4:35e714396141 | 24 | } |
marcus255 | 4:35e714396141 | 25 | while (conv2.getc() == DATA_TO_WRITE_CHAR){ |
marcus255 | 4:35e714396141 | 26 | received = conv2.getc(); |
marcus255 | 4:35e714396141 | 27 | if (i2c.write((int)received)) |
marcus255 | 4:35e714396141 | 28 | conv2.putc(UART_CONFIRMATION_CHAR); |
marcus255 | 4:35e714396141 | 29 | else { |
marcus255 | 4:35e714396141 | 30 | conv2.putc(UART_NON_CONFIRMATION_CHAR); |
marcus255 | 4:35e714396141 | 31 | break; |
marcus255 | 4:35e714396141 | 32 | } |
marcus255 | 4:35e714396141 | 33 | } |
marcus255 | 4:35e714396141 | 34 | } |
marcus255 | 4:35e714396141 | 35 | else if (received == (SLAVE_ADDR | 0x01)){ // reading from slave |
marcus255 | 4:35e714396141 | 36 | char rec; |
marcus255 | 4:35e714396141 | 37 | if (!transmission) { |
marcus255 | 4:35e714396141 | 38 | i2c.start(); |
marcus255 | 4:35e714396141 | 39 | transmission = 1; |
marcus255 | 4:35e714396141 | 40 | } |
marcus255 | 4:35e714396141 | 41 | if (i2c.write((int)rec)) |
marcus255 | 4:35e714396141 | 42 | conv2.putc(UART_CONFIRMATION_CHAR); |
marcus255 | 4:35e714396141 | 43 | else { |
marcus255 | 4:35e714396141 | 44 | conv2.putc(UART_NON_CONFIRMATION_CHAR); |
marcus255 | 4:35e714396141 | 45 | } |
marcus255 | 4:35e714396141 | 46 | while (conv2.getc() == DATA_TO_READ_CHAR){ |
marcus255 | 4:35e714396141 | 47 | rec = (char)i2c.read(1); |
marcus255 | 4:35e714396141 | 48 | conv2.putc(UART_CHAR_INCOMING); |
marcus255 | 4:35e714396141 | 49 | conv2.putc(rec); |
marcus255 | 4:35e714396141 | 50 | } |
marcus255 | 4:35e714396141 | 51 | |
marcus255 | 4:35e714396141 | 52 | }/* |
marcus255 | 4:35e714396141 | 53 | else{ |
marcus255 | 4:35e714396141 | 54 | conv2.putc((char)0x12); |
marcus255 | 4:35e714396141 | 55 | } */ |
marcus255 | 4:35e714396141 | 56 | } |
marcus255 | 4:35e714396141 | 57 | |
marcus255 | 4:35e714396141 | 58 | /* |
marcus255 | 4:35e714396141 | 59 | while (conv2.getc() == DATA_TO_WRITE_CHAR){ |
marcus255 | 4:35e714396141 | 60 | char received = conv2.getc(); |
marcus255 | 4:35e714396141 | 61 | if (received & 0x01 == 0x00){ // writing to slave |
marcus255 | 4:35e714396141 | 62 | if (!transmission) { |
marcus255 | 4:35e714396141 | 63 | i2c.start(); |
marcus255 | 4:35e714396141 | 64 | transmission = 1; |
marcus255 | 4:35e714396141 | 65 | } |
marcus255 | 4:35e714396141 | 66 | if (i2c.write((int)received)) |
marcus255 | 4:35e714396141 | 67 | conv2.putc(UART_CONFIRMATION_CHAR); |
marcus255 | 4:35e714396141 | 68 | else { |
marcus255 | 4:35e714396141 | 69 | conv2.putc(UART_NON_CONFIRMATION_CHAR); |
marcus255 | 4:35e714396141 | 70 | break; |
marcus255 | 4:35e714396141 | 71 | } |
marcus255 | 4:35e714396141 | 72 | } |
marcus255 | 4:35e714396141 | 73 | else if (received & 0x01 == 0x01){ // reading from slave |
marcus255 | 4:35e714396141 | 74 | |
marcus255 | 4:35e714396141 | 75 | } |
marcus255 | 4:35e714396141 | 76 | else{} |
marcus255 | 4:35e714396141 | 77 | |
marcus255 | 4:35e714396141 | 78 | } */ |
marcus255 | 4:35e714396141 | 79 | /*if (first_rec == DATA_TO_READ_CHAR){ |
marcus255 | 4:35e714396141 | 80 | char sec_rec = conv2.getc(); |
marcus255 | 4:35e714396141 | 81 | if (i2c.write((int)sec_rec)) |
marcus255 | 4:35e714396141 | 82 | conv2.putc(UART_CONFIRMATION_CHAR); |
marcus255 | 4:35e714396141 | 83 | else { |
marcus255 | 4:35e714396141 | 84 | conv2.putc(UART_NON_CONFIRMATION_CHAR); |
marcus255 | 4:35e714396141 | 85 | break; |
marcus255 | 4:35e714396141 | 86 | } |
marcus255 | 4:35e714396141 | 87 | while (conv2.getc()) == DATA_TO_READ_CHAR){ |
marcus255 | 4:35e714396141 | 88 | |
marcus255 | 4:35e714396141 | 89 | } |
marcus255 | 4:35e714396141 | 90 | }*/ |
marcus255 | 4:35e714396141 | 91 | i2c.stop(); |
marcus255 | 4:35e714396141 | 92 | transmission = 0; |
marcus255 | 4:35e714396141 | 93 | NVIC_EnableIRQ(UART0_IRQn); |
marcus255 | 4:35e714396141 | 94 | } |
mbed_official | 0:f76c26307f9a | 95 | |
marcus255 | 4:35e714396141 | 96 | void init(void){ |
marcus255 | 4:35e714396141 | 97 | i2c.frequency(100000); |
marcus255 | 4:35e714396141 | 98 | conv2.baud(921600); |
marcus255 | 4:35e714396141 | 99 | conv2.attach(&onUartChar); |
marcus255 | 4:35e714396141 | 100 | } |