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
- Committer:
- marcus255
- Date:
- 2015-12-17
- Revision:
- 5:d622f8a4a016
- Parent:
- 4:9e2adff677ac
- Child:
- 6:b9578f756c9c
File content as of revision 5:d622f8a4a016:
// Conv1 Slave
#include "main.h"
int main()
{
init();
while (1) {}
}
void i2cStart(void)
{
SDA_interrupt.disable_irq();
char addr = (char)slave.read();
if (addr & 0x01 == 0x01) { //reading from slave
char uart_rec2;
device.putc(DATA_TO_WRITE_CHAR);
device.putc(addr);
while(1) {
device.putc(DATA_TO_READ_CHAR);
while(1) { // waiting for data byte from conv2
if(device.readable()) {
if (device.getc() == UART_CHAR_INCOMING) {
uart_rec2 = device.getc();
break;
}
}
}
if(!slave.write(uart_rec2)) {
device.putc(END_OF_TRANSMISSION);
break;
}
}
slave.read();
} else {
count = 0;
char uart_rec;
device.putc(DATA_TO_WRITE_CHAR);
device.putc(addr);
while(!SDA_state) { //writting to slave
buffer = (char)slave.read();
for(int y = 0; y < 1024; y++) {} //some delay required for signal establishment
if(SDA_state) break;
device.putc(DATA_TO_WRITE_CHAR);
device.putc(buffer);
while(1) { // waiting until confirmation char is received from converter 2
if(device.readable()) {
uart_rec = device.getc();
if(uart_rec == UART_CONFIRMATION_CHAR || uart_rec == UART_NON_CONFIRMATION_CHAR)
break;
}
}
if (uart_rec == UART_NON_CONFIRMATION_CHAR)
break;
}
device.putc(END_OF_TRANSMISSION);
}
SDA_interrupt.enable_irq();
}
void init(void)
{
device.baud(921600);
SDA_interrupt.fall(&on_SDA_falling_slope_interrupt);
}
void on_SDA_falling_slope_interrupt(void)
{
SDA_interrupt.disable_irq();
int prev_state = 1;
int temp_state;
uint8_t address = 0;
for (int i = 7; i >= 0; i--) //for every clock cycle in frame
{
while(1) //sample CLK line state
{
if ((temp_state = SCL_state) == prev_state)
continue;
else
{
if (SDA_state == 1)
address |= (0x01 << i);
prev_state = temp_state;
//device.putc((char)SDA_state);
break;
}
}
}
// now we (probably) know the Slave address, so we can initialize i2c bus
// and set this Conv as Conv1 and send info to second Conv to act as Conv2
//device.putc(SET_CONV2_FUNCT);
device.putc(address);
//init_I2C(address, 100000);
SDA_interrupt.enable_irq();
}
void init_I2C(char addr, int freq)
{
SDA_interrupt.fall(&i2cStart);
slave.address(addr);
slave.frequency(freq);
}
