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
Diff: main.cpp
- Revision:
- 7:e86480c767ea
- Parent:
- 4:9e2adff677ac
- Child:
- 8:47ae8b74cd43
diff -r 9e2adff677ac -r e86480c767ea main.cpp
--- a/main.cpp Wed Dec 16 19:42:44 2015 +0000
+++ b/main.cpp Thu Dec 17 23:02:07 2015 +0000
@@ -1,26 +1,48 @@
// Conv1 Slave
#include "main.h"
-int main() {
- init();
+int main()
+{
+ init_conv();
while (1) {}
}
-void i2cStart(void){
+void on_SDA_falling_slope_interrupt(void)
+{
SDA_interrupt.disable_irq();
char addr = (char)slave.read();
- if (addr == RTC_ADDR) {
+ 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
+ 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);
-
+ device.putc(buffer);
+
while(1) { // waiting until confirmation char is received from converter 2
if(device.readable()) {
uart_rec = device.getc();
@@ -30,41 +52,45 @@
}
if (uart_rec == UART_NON_CONFIRMATION_CHAR)
break;
- }
- //slave.stop();
+ }
device.putc(END_OF_TRANSMISSION);
- }
- else 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 {
- //pc.printf("Unknown address: %x\n\r",addr);
- slave.stop();
}
SDA_interrupt.enable_irq();
}
-void init(void){
- SDA_interrupt.fall(&i2cStart);
- slave.frequency(100000);
+void init_conv(void)
+{
+ SCL_interrupt.rise(&on_SCL_rising_slope_interrupt);
device.baud(921600);
- slave.address(RTC_ADDR);
+ discovered_address = 0;
+ clk_count = 0;
+ frequency = I2C_FREQUENCY;
+}
+
+void init_conv1(int frequency, char address)
+{
+ slave.frequency(frequency);
+ slave.address(address);
+ SDA_interrupt.fall(&on_SDA_falling_slope_interrupt);
+}
+
+void init_conv2(void)
+{
+
+}
+
+void on_SCL_rising_slope_interrupt(void)
+{
+ if (clk_count<9) {
+ if (SDA_state)
+ discovered_address |= (0x01<<(7-clk_count));
+ clk_count++;
+ } else {
+ SCL_interrupt.disable_irq();
+ /*//commented for now, as conv2 software stays as it was for now
+ device.putc(SET_CONV2_FUNCT);
+ device.putc(discovered_address);
+ device.putc((char)(frequency/1000)); //in kHz */
+ init_conv1(frequency, (char)discovered_address);
+ }
}
\ No newline at end of file
