mbed_slave_full
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "memory.h" 00003 00004 DigitalOut aLED(LED1); 00005 Serial pc(USBTX, USBRX); // tx, rx 00006 I2CSlave slave(p28, p27); 00007 00008 const int SLAVE_ADDRESS = 0x84; 00009 const int I2C_FREQUENCY = 100000; 00010 const int I2C_BUFFER_SIZE = 6; 00011 00012 enum COMMAND { PUSH, PULL, CLEAR, PRINT }; 00013 00014 #define intToByte(pbytebuff,intval) (*((int*)(pbytebuff)) = intval) 00015 #define byteToInt(pbytebuff,pintval) (*(pintval) = *((int*)(pbytebuff))) 00016 00017 int main() { 00018 pc.baud(115200); 00019 pc.printf("Size of integer is %d bytes\r\n", sizeof(int)); 00020 00021 // Alive LED 00022 int cAlive = 0; 00023 00024 // Configure I2C 00025 slave.frequency(I2C_FREQUENCY); 00026 pc.printf("Slave is working @ %dHz\r\n", I2C_FREQUENCY); 00027 slave.address(SLAVE_ADDRESS); 00028 pc.printf("Slave is working @ SLAVE_ADDRESS = 0x%x\r\n", SLAVE_ADDRESS); 00029 00030 // Setup memory 00031 Memory memory; 00032 pc.printf("Size of memory buffer is %d elements\r\n", Memory::MEMORY_SIZE); 00033 memory.print(); 00034 pc.printf("Awaiting commands from master ...\r\n"); 00035 00036 // I2C buffer 00037 char buffer[I2C_BUFFER_SIZE]; 00038 00039 // Internal address pointer 00040 int pointer = 0; 00041 00042 while (1) { 00043 int rec = slave.receive(); 00044 switch (rec) { 00045 case I2CSlave::ReadAddressed: 00046 { 00047 pc.printf("ReadAddressed:\r\n"); 00048 int value = memory.get(pointer); 00049 buffer[0] = pointer; 00050 intToByte(buffer+1, value); 00051 if (!slave.write(buffer, 5)) { 00052 pc.printf("Retrieving and sending to master %d@%d\r\n", value, pointer); 00053 } else { 00054 pc.printf("Failed to send to master %d@%d\r\n", value, pointer); 00055 } 00056 break; 00057 } 00058 case I2CSlave::WriteAddressed: 00059 { 00060 pc.printf("WriteAddressed:\r\n"); 00061 // First we read the command byte 00062 int command = slave.read(); 00063 pc.printf("Command: %d \r\n", command); 00064 // Check the command 00065 switch (command) 00066 { 00067 case PUSH: 00068 // Expect 5 more bytes [address] [int value] 00069 if(!slave.read(buffer, 5)) { 00070 int address = buffer[0]; 00071 int value; 00072 byteToInt(buffer+1, &value); 00073 pc.printf("Storing %d@%d\r\n", value, address); 00074 memory.set(address, value); 00075 } else { 00076 pc.printf("PUSH received with missing address/data\r\n"); 00077 } 00078 break; 00079 00080 case PULL: 00081 // Expect 1 more byte [address] 00082 if(!slave.read(buffer, 1)) { 00083 int address = buffer[0]; 00084 pc.printf("Setting pointer to %d\r\n", address); 00085 if (address < Memory::MEMORY_SIZE) { 00086 pointer = address; 00087 } else { 00088 pc.printf("Address out of boundary\r\n"); 00089 } 00090 } else { 00091 pc.printf("PULL received with missing address\r\n"); 00092 } 00093 break; 00094 00095 case CLEAR: 00096 pc.printf("Clearing the memory\r\n"); 00097 memory.reset(); 00098 slave.stop(); 00099 break; 00100 00101 case PRINT: 00102 memory.print(); 00103 slave.stop(); 00104 break; 00105 00106 default: 00107 pc.printf("Unknown command byte\r\n"); 00108 } 00109 } 00110 } 00111 00112 // Clear buffer 00113 for (int i = 0; i < I2C_BUFFER_SIZE; i++) { 00114 buffer[i] = 0; 00115 } 00116 00117 // Alive LED 00118 cAlive = (cAlive + 1) % 100000; 00119 if (!cAlive) { 00120 aLED = !aLED; 00121 } 00122 } 00123 }
Generated on Sat Aug 6 2022 19:26:06 by
1.7.2