MCPcopy Create free account
hub / github.com/DexterInd/GoPiGo3 / grove_i2c_start

Method grove_i2c_start

Software/C/GoPiGo3.cpp:437–486  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

435}
436
437int GoPiGo3::grove_i2c_start(uint8_t port, i2c_struct_t *i2c_struct){
438 uint8_t msg_type;
439 uint8_t port_index;
440 switch(port){
441 case GROVE_1:
442 msg_type = GPGSPI_MESSAGE_START_GROVE_I2C_1;
443 port_index = 0;
444 break;
445 case GROVE_2:
446 msg_type = GPGSPI_MESSAGE_START_GROVE_I2C_2;
447 port_index = 1;
448 break;
449 default:
450 fatal_error("grove_i2c_start error. Must be one grove port at a time. GROVE_1 or GROVE_2.");
451 }
452 spi_array_out[0] = Address;
453 spi_array_out[1] = msg_type;
454 spi_array_out[2] = ((i2c_struct->address & 0x7F) << 1);
455
456 if(i2c_struct->length_read > LONGEST_I2C_TRANSFER){
457 i2c_struct->length_read = LONGEST_I2C_TRANSFER;
458 }
459 spi_array_out[3] = i2c_struct->length_read;
460 GroveI2CInBytes[port_index] = i2c_struct->length_read;
461
462 if(i2c_struct->length_write > LONGEST_I2C_TRANSFER){
463 i2c_struct->length_write = LONGEST_I2C_TRANSFER;
464 }
465 spi_array_out[4] = i2c_struct->length_write;
466
467 for(uint8_t i = 0; i < i2c_struct->length_write; i++){
468 spi_array_out[5 + i] = i2c_struct->buffer_write[i];
469 }
470
471 // assign error to the value returned by spi_transfer_array, and if not 0:
472 if(int error = spi_transfer_array((5 + i2c_struct->length_write), spi_array_out, spi_array_in)){
473 return error;
474 }
475
476 // If the fourth byte received is not 0xA5
477 if(spi_array_in[3] != 0xA5){
478 return ERROR_SPI_RESPONSE;
479 }
480 // If the fifth byte received is not GROVE_STATUS_VALID_DATA
481 if(spi_array_in[4] != GROVE_STATUS_VALID_DATA){
482 return spi_array_in[4];
483 }
484
485 return ERROR_NONE;
486}
487
488int GoPiGo3::get_grove_value(uint8_t port, void *value_ptr){
489 uint8_t msg_type;

Callers

nothing calls this directly

Calls 2

fatal_errorFunction · 0.85
spi_transfer_arrayFunction · 0.85

Tested by

no test coverage detected