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@8:47ae8b74cd43, 2015-12-18 (annotated)
- Committer:
- marcus255
- Date:
- Fri Dec 18 00:01:28 2015 +0000
- Revision:
- 8:47ae8b74cd43
- Parent:
- 7:e86480c767ea
- Child:
- 9:58c110e686f0
the same conv software uploaded to both KL05, memory communication ok, sensor communication fails
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 | 8:47ae8b74cd43 | 10 | void onUartChar() |
| marcus255 | 8:47ae8b74cd43 | 11 | { |
| marcus255 | 8:47ae8b74cd43 | 12 | NVIC_DisableIRQ(UART0_IRQn); // TODO: check if this statement is necessary here |
| marcus255 | 8:47ae8b74cd43 | 13 | char first_char = device.getc(); |
| marcus255 | 8:47ae8b74cd43 | 14 | if (first_char == SET_CONV2_FUNCT){ |
| marcus255 | 8:47ae8b74cd43 | 15 | char slave_address = device.getc(); // not used at all, may be delete latar |
| marcus255 | 8:47ae8b74cd43 | 16 | int i2c_frequency = 1000 * (int)device.getc(); |
| marcus255 | 8:47ae8b74cd43 | 17 | init_conv2(i2c_frequency); |
| marcus255 | 8:47ae8b74cd43 | 18 | } |
| marcus255 | 8:47ae8b74cd43 | 19 | else if (first_char == DATA_TO_WRITE_CHAR) { |
| marcus255 | 8:47ae8b74cd43 | 20 | char received = device.getc(); |
| marcus255 | 8:47ae8b74cd43 | 21 | if (received & 0x01 == 0x01) { // reading from slave |
| marcus255 | 8:47ae8b74cd43 | 22 | char rec; |
| marcus255 | 8:47ae8b74cd43 | 23 | if (!transmission) { |
| marcus255 | 8:47ae8b74cd43 | 24 | i2c.start(); |
| marcus255 | 8:47ae8b74cd43 | 25 | transmission = 1; |
| marcus255 | 8:47ae8b74cd43 | 26 | } |
| marcus255 | 8:47ae8b74cd43 | 27 | if (i2c.write((int)rec)) { |
| marcus255 | 8:47ae8b74cd43 | 28 | while (device.getc() == DATA_TO_READ_CHAR) { |
| marcus255 | 8:47ae8b74cd43 | 29 | rec = (char)i2c.read(1); |
| marcus255 | 8:47ae8b74cd43 | 30 | device.putc(UART_CHAR_INCOMING); |
| marcus255 | 8:47ae8b74cd43 | 31 | device.putc(rec); |
| marcus255 | 8:47ae8b74cd43 | 32 | } |
| marcus255 | 8:47ae8b74cd43 | 33 | i2c.read(0); |
| marcus255 | 8:47ae8b74cd43 | 34 | } else { |
| marcus255 | 8:47ae8b74cd43 | 35 | device.putc(UART_NON_CONFIRMATION_CHAR); |
| marcus255 | 8:47ae8b74cd43 | 36 | } |
| marcus255 | 8:47ae8b74cd43 | 37 | device.putc(END_OF_TRANSMISSION); |
| marcus255 | 8:47ae8b74cd43 | 38 | } else { // writing to slave |
| marcus255 | 8:47ae8b74cd43 | 39 | if (!transmission) { |
| marcus255 | 8:47ae8b74cd43 | 40 | i2c.start(); |
| marcus255 | 8:47ae8b74cd43 | 41 | transmission = 1; |
| marcus255 | 8:47ae8b74cd43 | 42 | } |
| marcus255 | 8:47ae8b74cd43 | 43 | if (i2c.write((int)received)) |
| marcus255 | 8:47ae8b74cd43 | 44 | device.putc(UART_CONFIRMATION_CHAR); |
| marcus255 | 8:47ae8b74cd43 | 45 | else { |
| marcus255 | 8:47ae8b74cd43 | 46 | device.putc(UART_NON_CONFIRMATION_CHAR); |
| marcus255 | 8:47ae8b74cd43 | 47 | } |
| marcus255 | 8:47ae8b74cd43 | 48 | while (device.getc() == DATA_TO_WRITE_CHAR) { |
| marcus255 | 8:47ae8b74cd43 | 49 | received = device.getc(); |
| marcus255 | 8:47ae8b74cd43 | 50 | if (i2c.write((int)received)) |
| marcus255 | 8:47ae8b74cd43 | 51 | device.putc(UART_CONFIRMATION_CHAR); |
| marcus255 | 8:47ae8b74cd43 | 52 | else { |
| marcus255 | 8:47ae8b74cd43 | 53 | device.putc(UART_NON_CONFIRMATION_CHAR); |
| marcus255 | 8:47ae8b74cd43 | 54 | break; |
| marcus255 | 8:47ae8b74cd43 | 55 | } |
| marcus255 | 8:47ae8b74cd43 | 56 | } |
| marcus255 | 8:47ae8b74cd43 | 57 | } |
| marcus255 | 8:47ae8b74cd43 | 58 | } |
| marcus255 | 8:47ae8b74cd43 | 59 | transmission = 0; |
| marcus255 | 8:47ae8b74cd43 | 60 | NVIC_EnableIRQ(UART0_IRQn); // TODO: check if this statement is necessary here |
| marcus255 | 8:47ae8b74cd43 | 61 | } |
| marcus255 | 8:47ae8b74cd43 | 62 | |
| marcus255 | 7:e86480c767ea | 63 | void on_SDA_falling_slope_interrupt(void) |
| marcus255 | 7:e86480c767ea | 64 | { |
| marcus255 | 0:ab0f6f4ef7ac | 65 | SDA_interrupt.disable_irq(); |
| marcus255 | 0:ab0f6f4ef7ac | 66 | char addr = (char)slave.read(); |
| marcus255 | 7:e86480c767ea | 67 | if (addr & 0x01 == 0x01) { //reading from slave |
| marcus255 | 7:e86480c767ea | 68 | char uart_rec2; |
| marcus255 | 7:e86480c767ea | 69 | device.putc(DATA_TO_WRITE_CHAR); |
| marcus255 | 7:e86480c767ea | 70 | device.putc(addr); |
| marcus255 | 7:e86480c767ea | 71 | while(1) { |
| marcus255 | 7:e86480c767ea | 72 | device.putc(DATA_TO_READ_CHAR); |
| marcus255 | 7:e86480c767ea | 73 | while(1) { // waiting for data byte from conv2 |
| marcus255 | 7:e86480c767ea | 74 | if(device.readable()) { |
| marcus255 | 7:e86480c767ea | 75 | if (device.getc() == UART_CHAR_INCOMING) { |
| marcus255 | 7:e86480c767ea | 76 | uart_rec2 = device.getc(); |
| marcus255 | 7:e86480c767ea | 77 | break; |
| marcus255 | 7:e86480c767ea | 78 | } |
| marcus255 | 7:e86480c767ea | 79 | } |
| marcus255 | 7:e86480c767ea | 80 | } |
| marcus255 | 7:e86480c767ea | 81 | if(!slave.write(uart_rec2)) { |
| marcus255 | 7:e86480c767ea | 82 | device.putc(END_OF_TRANSMISSION); |
| marcus255 | 7:e86480c767ea | 83 | break; |
| marcus255 | 7:e86480c767ea | 84 | } |
| marcus255 | 7:e86480c767ea | 85 | } |
| marcus255 | 7:e86480c767ea | 86 | slave.read(); |
| marcus255 | 7:e86480c767ea | 87 | } else { |
| marcus255 | 0:ab0f6f4ef7ac | 88 | count = 0; |
| marcus255 | 0:ab0f6f4ef7ac | 89 | char uart_rec; |
| marcus255 | 0:ab0f6f4ef7ac | 90 | device.putc(DATA_TO_WRITE_CHAR); |
| marcus255 | 0:ab0f6f4ef7ac | 91 | device.putc(addr); |
| marcus255 | 4:9e2adff677ac | 92 | while(!SDA_state) { //writting to slave |
| marcus255 | 0:ab0f6f4ef7ac | 93 | buffer = (char)slave.read(); |
| marcus255 | 7:e86480c767ea | 94 | for(int y = 0; y < 1024; y++) {} //some delay required for signal establishment |
| marcus255 | 0:ab0f6f4ef7ac | 95 | if(SDA_state) break; |
| marcus255 | 0:ab0f6f4ef7ac | 96 | device.putc(DATA_TO_WRITE_CHAR); |
| marcus255 | 7:e86480c767ea | 97 | device.putc(buffer); |
| marcus255 | 7:e86480c767ea | 98 | |
| marcus255 | 0:ab0f6f4ef7ac | 99 | while(1) { // waiting until confirmation char is received from converter 2 |
| marcus255 | 0:ab0f6f4ef7ac | 100 | if(device.readable()) { |
| marcus255 | 0:ab0f6f4ef7ac | 101 | uart_rec = device.getc(); |
| marcus255 | 0:ab0f6f4ef7ac | 102 | if(uart_rec == UART_CONFIRMATION_CHAR || uart_rec == UART_NON_CONFIRMATION_CHAR) |
| marcus255 | 0:ab0f6f4ef7ac | 103 | break; |
| marcus255 | 0:ab0f6f4ef7ac | 104 | } |
| marcus255 | 0:ab0f6f4ef7ac | 105 | } |
| marcus255 | 0:ab0f6f4ef7ac | 106 | if (uart_rec == UART_NON_CONFIRMATION_CHAR) |
| marcus255 | 0:ab0f6f4ef7ac | 107 | break; |
| marcus255 | 7:e86480c767ea | 108 | } |
| marcus255 | 0:ab0f6f4ef7ac | 109 | device.putc(END_OF_TRANSMISSION); |
| marcus255 | 0:ab0f6f4ef7ac | 110 | } |
| marcus255 | 0:ab0f6f4ef7ac | 111 | SDA_interrupt.enable_irq(); |
| marcus255 | 0:ab0f6f4ef7ac | 112 | } |
| marcus255 | 0:ab0f6f4ef7ac | 113 | |
| marcus255 | 7:e86480c767ea | 114 | void init_conv(void) |
| marcus255 | 7:e86480c767ea | 115 | { |
| marcus255 | 7:e86480c767ea | 116 | SCL_interrupt.rise(&on_SCL_rising_slope_interrupt); |
| marcus255 | 0:ab0f6f4ef7ac | 117 | device.baud(921600); |
| marcus255 | 7:e86480c767ea | 118 | discovered_address = 0; |
| marcus255 | 7:e86480c767ea | 119 | clk_count = 0; |
| marcus255 | 8:47ae8b74cd43 | 120 | device.attach(&onUartChar); |
| marcus255 | 7:e86480c767ea | 121 | } |
| marcus255 | 7:e86480c767ea | 122 | |
| marcus255 | 7:e86480c767ea | 123 | void init_conv1(int frequency, char address) |
| marcus255 | 7:e86480c767ea | 124 | { |
| marcus255 | 7:e86480c767ea | 125 | slave.frequency(frequency); |
| marcus255 | 7:e86480c767ea | 126 | slave.address(address); |
| marcus255 | 7:e86480c767ea | 127 | SDA_interrupt.fall(&on_SDA_falling_slope_interrupt); |
| marcus255 | 7:e86480c767ea | 128 | } |
| marcus255 | 7:e86480c767ea | 129 | |
| marcus255 | 8:47ae8b74cd43 | 130 | void init_conv2(int frequency) |
| marcus255 | 7:e86480c767ea | 131 | { |
| marcus255 | 8:47ae8b74cd43 | 132 | i2c.frequency(frequency); |
| marcus255 | 7:e86480c767ea | 133 | } |
| marcus255 | 7:e86480c767ea | 134 | |
| marcus255 | 7:e86480c767ea | 135 | void on_SCL_rising_slope_interrupt(void) |
| marcus255 | 7:e86480c767ea | 136 | { |
| marcus255 | 7:e86480c767ea | 137 | if (clk_count<9) { |
| marcus255 | 7:e86480c767ea | 138 | if (SDA_state) |
| marcus255 | 7:e86480c767ea | 139 | discovered_address |= (0x01<<(7-clk_count)); |
| marcus255 | 7:e86480c767ea | 140 | clk_count++; |
| marcus255 | 7:e86480c767ea | 141 | } else { |
| marcus255 | 7:e86480c767ea | 142 | SCL_interrupt.disable_irq(); |
| marcus255 | 8:47ae8b74cd43 | 143 | frequency = I2C_FREQUENCY; |
| marcus255 | 7:e86480c767ea | 144 | device.putc(SET_CONV2_FUNCT); |
| marcus255 | 7:e86480c767ea | 145 | device.putc(discovered_address); |
| marcus255 | 8:47ae8b74cd43 | 146 | device.putc((char)(frequency/1000)); //in kHz |
| marcus255 | 7:e86480c767ea | 147 | init_conv1(frequency, (char)discovered_address); |
| marcus255 | 7:e86480c767ea | 148 | } |
| marcus255 | 0:ab0f6f4ef7ac | 149 | } |
