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.
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 int value = memory.get(pointer); 00048 buffer[0] = pointer; 00049 intToByte(buffer+1, value); 00050 if (!slave.write(buffer, 5)) { 00051 pc.printf("Retrieving and sending to master %d@%d\r\n", value, pointer); 00052 } else { 00053 pc.printf("Failed to send to master %d@%d\r\n", value, pointer); 00054 } 00055 break; 00056 } 00057 case I2CSlave::WriteAddressed: 00058 { 00059 // First we read the command byte 00060 int command = slave.read(); 00061 00062 // Check the command 00063 switch (command) 00064 { 00065 case PUSH: 00066 // Expect 5 more bytes [address] [int value] 00067 if(!slave.read(buffer, 5)) { 00068 int address = buffer[0]; 00069 int value; 00070 byteToInt(buffer+1, &value); 00071 pc.printf("Storing %d@%d\r\n", value, address); 00072 memory.set(address, value); 00073 } else { 00074 pc.printf("PUSH received with missing address/data\r\n"); 00075 } 00076 break; 00077 00078 case PULL: 00079 // Expect 1 more byte [address] 00080 if(!slave.read(buffer, 1)) { 00081 int address = buffer[0]; 00082 pc.printf("Setting pointer to %d\r\n", address); 00083 if (address < Memory::MEMORY_SIZE) { 00084 pointer = address; 00085 } else { 00086 pc.printf("Address out of boundary\r\n"); 00087 } 00088 } else { 00089 pc.printf("PULL received with missing address\r\n"); 00090 } 00091 break; 00092 00093 case CLEAR: 00094 pc.printf("Clearing the memory\r\n"); 00095 memory.reset(); 00096 slave.stop(); 00097 break; 00098 00099 case PRINT: 00100 memory.print(); 00101 slave.stop(); 00102 break; 00103 00104 default: 00105 pc.printf("Unknown command byte\r\n"); 00106 } 00107 } 00108 } 00109 00110 // Clear buffer 00111 for (int i = 0; i < I2C_BUFFER_SIZE; i++) { 00112 buffer[i] = 0; 00113 } 00114 00115 // Alive LED 00116 cAlive = (cAlive + 1) % 100000; 00117 if (!cAlive) { 00118 aLED = !aLED; 00119 } 00120 } 00121 }
Generated on Wed Jul 13 2022 23:20:25 by
1.7.2