pyl_v8

Dependencies:   mbed-rtos mbed stingr

Committer:
jmoreno10
Date:
Mon Oct 29 12:44:05 2018 +0000
Revision:
0:5c85e75df952
pyl_v8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmoreno10 0:5c85e75df952 1 #include "mbed.h"
jmoreno10 0:5c85e75df952 2 #include "rtos.h"
jmoreno10 0:5c85e75df952 3 #include "Stingr.h"
jmoreno10 0:5c85e75df952 4
jmoreno10 0:5c85e75df952 5 Serial stingrUART(p13, p14, 9600); // tx, rx Comunicación Serial con el STINGR
jmoreno10 0:5c85e75df952 6 Serial pc(USBTX, USBRX, 9600); // tx, rx Comunicación Serial con la PC
jmoreno10 0:5c85e75df952 7 I2CSlave slave(p9, p10); // sda, scl
jmoreno10 0:5c85e75df952 8
jmoreno10 0:5c85e75df952 9 const int BUF_LEN = 200;
jmoreno10 0:5c85e75df952 10 const int I2C_ADDR = 0x1E;
jmoreno10 0:5c85e75df952 11 const int I2C_FREQ = 400000;
jmoreno10 0:5c85e75df952 12
jmoreno10 0:5c85e75df952 13 Stingr stingr;
jmoreno10 0:5c85e75df952 14
jmoreno10 0:5c85e75df952 15 int main()
jmoreno10 0:5c85e75df952 16 {
jmoreno10 0:5c85e75df952 17 // I2C variables
jmoreno10 0:5c85e75df952 18 slave.frequency(I2C_FREQ); // 400 MHz interface
jmoreno10 0:5c85e75df952 19 slave.address(I2C_ADDR); // slave address
jmoreno10 0:5c85e75df952 20
jmoreno10 0:5c85e75df952 21 // local vars
jmoreno10 0:5c85e75df952 22 char buf[BUF_LEN]; // read array from master
jmoreno10 0:5c85e75df952 23
jmoreno10 0:5c85e75df952 24 // I2C communication
jmoreno10 0:5c85e75df952 25 while(1)
jmoreno10 0:5c85e75df952 26 {
jmoreno10 0:5c85e75df952 27 int state = slave.receive();
jmoreno10 0:5c85e75df952 28 switch(state)
jmoreno10 0:5c85e75df952 29 {
jmoreno10 0:5c85e75df952 30 // Master reads from slave
jmoreno10 0:5c85e75df952 31 case I2CSlave::ReadAddressed:
jmoreno10 0:5c85e75df952 32 slave.write(stingr.get_resp(), strlen(stingr.get_resp()) + 1);
jmoreno10 0:5c85e75df952 33 printf("Writing to Master:\t<<- %s\n", stingr.get_resp());
jmoreno10 0:5c85e75df952 34 break;
jmoreno10 0:5c85e75df952 35 // Master writes to slave
jmoreno10 0:5c85e75df952 36 case I2CSlave::WriteAddressed:
jmoreno10 0:5c85e75df952 37 slave.read(buf, 200); // Read up to 200 characters from Master
jmoreno10 0:5c85e75df952 38 printf("\n\nReading from Master:\t->> %s\n", buf);
jmoreno10 0:5c85e75df952 39 stingr.command(buf);
jmoreno10 0:5c85e75df952 40 break;
jmoreno10 0:5c85e75df952 41 }
jmoreno10 0:5c85e75df952 42 }
jmoreno10 0:5c85e75df952 43 }