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.
Revision 2:4dd2f26c5648, committed 2020-11-04
- Comitter:
- d2_h10s
- Date:
- Wed Nov 04 07:24:38 2020 +0000
- Parent:
- 1:9b7603b90063
- Commit message:
- in jae
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Aug 31 15:09:36 2020 +0000 +++ b/main.cpp Wed Nov 04 07:24:38 2020 +0000 @@ -16,9 +16,14 @@ #define CMD_ROBOT_MOVE 4 #define CMD_ROBOT_STOP 5 -uint8_t device_addr = 0x0A; -char data[52] = {device_addr, 1, CMD_NULL,}; +#define device_addr 0x0A<<1 +#define BUFF_SIZE 52 + +char data[BUFF_SIZE] = {0x0A, 1, CMD_NULL, (CMD_NULL&0xFF), 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 20, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +char sub_addr; I2CSlave slave(I2C_SDA, I2C_SCL); AnalogIn adc_temp(ADC_TEMP); @@ -26,18 +31,38 @@ //void writeByte(uint8_t address, uint8_t regAddress, uint8_t data); //char readByte(uint8_t address, uint8_t regAddress); + +/* protocol + * [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28] [29] [30] [31] [32] [33] [34] [35] [36] [37] [38] [39] [40] [41] [42] [43] [44] [45] [46] [47] [48] [49] + * device_addr LEN CMD write_data + */ int main() { + PC_DEBUG("hello") slave.frequency(400000); printf("slave started\n"); slave.address(device_addr); - uint8_t status = 0; + int status = 0; while(1){ status = slave.receive(); - if(status) printf("status: %d\n", status); if(status == I2CSlave::WriteAddressed){ - slave.read(data, 52); - printf("Read from master: %s\n", data); + sub_addr = slave.read(); + int len = slave.read(data+sub_addr, BUFF_SIZE); + printf("Read from master: %02x, %02x: ", sub_addr, len); + for(int i = 0; i < BUFF_SIZE; i++){ + printf("%02X ", data[i]); + } + printf("\n"); + } + else if(status == I2CSlave::ReadAddressed){ + + slave.write(data+sub_addr, data[1]); + data[1] = 1; + printf("Written to master: "); + for(int i = 0; i < 52; i++){ + printf("%02X ", data[i]); + } + printf("\n"); } } }