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.
Dependencies: lib_i2c_slave_block
main.cpp
00001 00002 #include <mbed.h> 00003 #include "smbus.h" 00004 00005 #define IRQ_OUT_PIN D6 00006 00007 const int SLAVE_ADDRESS = 0xA0; 00008 00009 RawSerial pc(USBTX, USBRX); 00010 00011 DigitalOut irqOutPin(IRQ_OUT_PIN); 00012 irq_t irq; 00013 00014 DigitalIn button(USER_BUTTON); 00015 00016 uint8_t test_buf[32]; 00017 00018 void fill_tx_buf(uint8_t cmd) 00019 { 00020 static uint8_t cnt = 0; 00021 unsigned i; 00022 00023 /* answering (master read) request here */ 00024 00025 switch (cmd) { 00026 case CMD_TEST3: 00027 case CMD_TEST12: 00028 case CMD_TEST32: 00029 for (i = 0; i < cmd_to_length[cmd]; i++) 00030 i2c.tx_buf[i] = i + cnt; 00031 cnt++; 00032 break; 00033 case CMD_BUFFER: 00034 memcpy(i2c.tx_buf, test_buf, sizeof(test_buf)); 00035 break; 00036 case CMD_IRQ: 00037 for (i = 0; i < cmd_to_length[CMD_IRQ]; i++) 00038 i2c.tx_buf[i] = irq.buf[i]; 00039 00040 /* interrupt was (is being) read, clear it */ 00041 irq.fields.flags.button = 0; 00042 if (irq.buf[0] == 0) 00043 irqOutPin = 0; 00044 00045 break; 00046 } 00047 } 00048 00049 bool cmd_allowed(uint8_t) 00050 { 00051 /* return false if slave cannot take this cmd now */ 00052 return true; 00053 } 00054 00055 void service_i2c_write(uint8_t cmd, uint8_t len, const uint8_t* req) 00056 { 00057 uint8_t s8; 00058 00059 /* taking master write here */ 00060 00061 switch (cmd) { 00062 case CMD_TEST3: 00063 case CMD_TEST12: 00064 case CMD_TEST32: 00065 for (s8 = 0; s8 < cmd_to_length[cmd]; s8++) 00066 pc.printf("%02x ", req[s8]); 00067 pc.printf("\r\n"); 00068 break; 00069 case CMD_BUFFER: 00070 memcpy(test_buf, req, sizeof(test_buf)); 00071 break; 00072 /* failures: */ 00073 case CMD_BUSERR: 00074 pc.printf("BUSERR%u\r\n", req[0]); 00075 break; 00076 case CMD_ARLO: 00077 pc.printf("ARLO%u\r\n", req[0]); // req[0] tx_cndtr 00078 break; 00079 case CMD_TIMEOUT: 00080 pc.printf("TIMEOUT%u\r\n", req[0]); // req[0] tx_cndtr 00081 break; 00082 default: 00083 pc.printf("??%02x??\r\n", cmd); 00084 break; 00085 } // ..switch (cmd) 00086 } 00087 00088 int main() 00089 { 00090 int res; 00091 bool but; 00092 uint8_t cnt = 0; 00093 00094 pc.baud(115200); 00095 pc.printf("\r\nreset\r\n"); 00096 00097 res = smbus_init(I2C_SDA, I2C_SCL, SLAVE_ADDRESS); 00098 pc.printf("%d = smbus_init()\r\n", res); 00099 00100 but = button.read(); 00101 while (1) { 00102 service_i2c(); 00103 00104 if (but != button.read()) { 00105 irq.fields.cnt = cnt++; 00106 irq.fields.flags.button = 1; 00107 irqOutPin = 1; 00108 00109 but = button.read(); 00110 } 00111 00112 } // ..while (1) 00113 } 00114
Generated on Fri Jul 15 2022 01:13:08 by
1.7.2