BV I2C (minor): add space around operators
This commit is contained in:
parent
5faaeb02cc
commit
93d3d3048e
|
@ -74,31 +74,31 @@ static bool busvoodoo_i2c_setup(char** prefix, const char* line)
|
|||
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_SPEED;
|
||||
break;
|
||||
case BUSVOODOO_I2C_SETTING_SPEED:
|
||||
if (NULL==line || 0==strlen(line)) { // use default setting
|
||||
if (NULL == line || 0 == strlen(line)) { // use default setting
|
||||
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_ADDRESSBITS; // go to next setting
|
||||
} else { // setting provided
|
||||
uint32_t speed = atoi(line); // parse setting
|
||||
if (speed>0 && speed<=400) { // check setting
|
||||
if (speed > 0 && speed <= 400) { // check setting
|
||||
busvoodoo_i2c_speed = speed; // remember setting
|
||||
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_ADDRESSBITS; // go to next setting
|
||||
}
|
||||
}
|
||||
if (BUSVOODOO_I2C_SETTING_ADDRESSBITS==busvoodoo_i2c_setting) { // if next setting
|
||||
if (BUSVOODOO_I2C_SETTING_ADDRESSBITS == busvoodoo_i2c_setting) { // if next setting
|
||||
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "address size in bits (7,10) [%u]", busvoodoo_i2c_addressbits); // prepare next setting
|
||||
*prefix = busvoodoo_global_string; // display next setting
|
||||
}
|
||||
break;
|
||||
case BUSVOODOO_I2C_SETTING_ADDRESSBITS:
|
||||
if (NULL==line || 0==strlen(line)) { // use default setting
|
||||
if (NULL == line || 0 == strlen(line)) { // use default setting
|
||||
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_PULLUP; // go to next setting
|
||||
} else { // setting provided
|
||||
uint8_t addressbits = atoi(line); // parse setting
|
||||
if (7==addressbits || 10==addressbits) { // check setting
|
||||
if (7 == addressbits || 10 == addressbits) { // check setting
|
||||
busvoodoo_i2c_addressbits = addressbits; // remember setting
|
||||
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_PULLUP; // go to next setting
|
||||
}
|
||||
}
|
||||
if (BUSVOODOO_I2C_SETTING_PULLUP==busvoodoo_i2c_setting) { // if next setting
|
||||
if (BUSVOODOO_I2C_SETTING_PULLUP == busvoodoo_i2c_setting) { // if next setting
|
||||
printf("1) open-drain, with embedded pull-up resistors (2kO)\n");
|
||||
printf("2) open-drain, with external pull-up resistors\n");
|
||||
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "drive mode (1,2) [%c]", busvoodoo_i2c_embedded_pullup ? '1' : '2'); // show drive mode
|
||||
|
@ -106,16 +106,16 @@ static bool busvoodoo_i2c_setup(char** prefix, const char* line)
|
|||
}
|
||||
break;
|
||||
case BUSVOODOO_I2C_SETTING_PULLUP:
|
||||
if (NULL==line || 0==strlen(line)) { // use default setting
|
||||
if (NULL == line || 0 == strlen(line)) { // use default setting
|
||||
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_DONE; // go to next setting
|
||||
} else if (1==strlen(line)) { // setting provided
|
||||
} else if (1 == strlen(line)) { // setting provided
|
||||
uint8_t drive = atoi(line); // parse setting
|
||||
if (1==drive || 2==drive) { // check setting
|
||||
busvoodoo_i2c_embedded_pullup = (1==drive); // remember setting
|
||||
if (1 == drive || 2 == drive) { // check setting
|
||||
busvoodoo_i2c_embedded_pullup = (1 == drive); // remember setting
|
||||
busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_DONE; // go to next setting
|
||||
}
|
||||
}
|
||||
if (BUSVOODOO_I2C_SETTING_DONE==busvoodoo_i2c_setting) { // we have all settings, configure I2C
|
||||
if (BUSVOODOO_I2C_SETTING_DONE == busvoodoo_i2c_setting) { // we have all settings, configure I2C
|
||||
i2c_master_setup(BUSVOODOO_I2C, busvoodoo_i2c_speed); // setup I2C
|
||||
if (busvoodoo_i2c_embedded_pullup) {
|
||||
busvoodoo_embedded_pullup(true); // set pull-up
|
||||
|
@ -131,7 +131,7 @@ static bool busvoodoo_i2c_setup(char** prefix, const char* line)
|
|||
}
|
||||
if (busvoodoo_full) {
|
||||
const char* pinout_rscan[5] = {"HV", NULL, NULL, NULL, NULL}; // HiZ mode RS/CAN pinout
|
||||
for (uint8_t i=0; i<LENGTH(pinout_rscan) && i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
|
||||
for (uint8_t i = 0; i < LENGTH(pinout_rscan) && i < LENGTH(busvoodoo_global_pinout_rscan); i++) {
|
||||
busvoodoo_global_pinout_rscan[i] = pinout_rscan[i]; // set pin names
|
||||
}
|
||||
}
|
||||
|
@ -224,14 +224,14 @@ static void busvoodoo_i2c_write(uint8_t data)
|
|||
static void busvoodoo_i2c_select(uint16_t slave, bool write)
|
||||
{
|
||||
printf("select slave ");
|
||||
if (10==busvoodoo_i2c_addressbits) {
|
||||
if (10 == busvoodoo_i2c_addressbits) {
|
||||
printf("0x%03x", slave);
|
||||
} else {
|
||||
printf("0x%02x", slave);
|
||||
}
|
||||
printf(" to %s: ", write ? "write" : "read");
|
||||
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we send the slave address
|
||||
switch (i2c_master_select_slave(BUSVOODOO_I2C, slave, 10==busvoodoo_i2c_addressbits, write)) {
|
||||
switch (i2c_master_select_slave(BUSVOODOO_I2C, slave, 10 == busvoodoo_i2c_addressbits, write)) {
|
||||
case I2C_MASTER_RC_NONE: // all went fine
|
||||
printf("selected");
|
||||
break;
|
||||
|
@ -265,71 +265,71 @@ static void busvoodoo_i2c_select(uint16_t slave, bool write)
|
|||
static bool busvoodoo_i2c_action(const char* action, uint32_t repetition, bool perform)
|
||||
{
|
||||
uint32_t length = strlen(action); // remember length since it will be used a number of times
|
||||
if (NULL==action || 0==length) { // there is nothing to do
|
||||
if (NULL == action || 0 == length) { // there is nothing to do
|
||||
return true;
|
||||
}
|
||||
|
||||
if (1==length && 'r'==action[0]) { // read data
|
||||
if (1 == length && 'r' == action[0]) { // read data
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
for (uint32_t i = 0; i < repetition; i++) {
|
||||
busvoodoo_i2c_read(); // read from I2C
|
||||
}
|
||||
} else if (1==length && '['==action[0]) { // select slave
|
||||
} else if (1 == length && '[' == action[0]) { // select slave
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
printf("send start condition: ");
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
for (uint32_t i = 0; i < repetition; i++) {
|
||||
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we send start condition
|
||||
printf("%s ", I2C_MASTER_RC_NONE==i2c_master_start(BUSVOODOO_I2C) ? "sent" : "error");
|
||||
printf("%s ", I2C_MASTER_RC_NONE == i2c_master_start(BUSVOODOO_I2C) ? "sent" : "error");
|
||||
}
|
||||
printf("\n");
|
||||
} else if (1==length && ']'==action[0]) { // deselect slave
|
||||
} else if (1 == length && ']' == action[0]) { // deselect slave
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
printf("send stop condition: ");
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
for (uint32_t i = 0; i < repetition; i++) {
|
||||
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we send stop condition
|
||||
printf("%s ", I2C_MASTER_RC_NONE==i2c_master_stop(BUSVOODOO_I2C) ? "sent" : "error");
|
||||
printf("%s ", I2C_MASTER_RC_NONE == i2c_master_stop(BUSVOODOO_I2C) ? "sent" : "error");
|
||||
}
|
||||
printf("\n");
|
||||
} else if (1==length && 'u'==action[0]) { // sleep us
|
||||
} else if (1 == length && 'u' == action[0]) { // sleep us
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
printf("wait for %u us\n", repetition);
|
||||
sleep_us(repetition); // sleep
|
||||
} else if (1==length && 'm'==action[0]) { // sleep ms
|
||||
} else if (1 == length && 'm' == action[0]) { // sleep ms
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
printf("wait for %u ms\n", repetition);
|
||||
sleep_ms(repetition); // sleep
|
||||
} else if ('0'==action[0]) { // send digit
|
||||
if (1==length) { // just send 0
|
||||
} else if ('0' == action[0]) { // send digit
|
||||
if (1 == length) { // just send 0
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
for (uint32_t i = 0; i < repetition; i++) {
|
||||
busvoodoo_i2c_write(0); // write to I2C
|
||||
}
|
||||
} else if ('x'==action[1] || 'b'==action[1]) { // send hex/binary
|
||||
return busvoodoo_i2c_action(action+1, repetition, perform); // just retry without leading 0
|
||||
} else if (action[1]>='0' && action[1]<='9') { // send decimal
|
||||
return busvoodoo_i2c_action(action+1, repetition, perform); // just retry without leading 0
|
||||
} else if ('r'==action[1] && 2==length) { // select slave to read
|
||||
} else if ('x' == action[1] || 'b' == action[1]) { // send hex/binary
|
||||
return busvoodoo_i2c_action(action + 1, repetition, perform); // just retry without leading 0
|
||||
} else if (action[1] >= '0' && action[1] <= '9') { // send decimal
|
||||
return busvoodoo_i2c_action(action + 1, repetition, perform); // just retry without leading 0
|
||||
} else if ('r' == action[1] && 2 == length) { // select slave to read
|
||||
busvoodoo_i2c_select(0, false); // select slave 0 to read
|
||||
} else if ('w'==action[1] && 2==length) { // select slave to write
|
||||
} else if ('w' == action[1] && 2 == length) { // select slave to write
|
||||
busvoodoo_i2c_select(0, true); // select slave 0 to write
|
||||
} else { // malformed action
|
||||
return false;
|
||||
}
|
||||
} else if ('x'==action[0] && length>1) { // send hexadecimal value
|
||||
for (uint32_t i=1; i<length; i++) { // check string
|
||||
if (!((action[i]>='0' && action[i]<='9') || (action[i]>='a' && action[i]<='f') || (action[i]>='A' && action[i]<='F') || ('r'==action[i] && i==length-1) || ('w'==action[i] && i==length-1))) { // check for hexadecimal character or r/w flag
|
||||
} else if ('x' == action[0] && length > 1) { // send hexadecimal value
|
||||
for (uint32_t i = 1; i < length; i++) { // check string
|
||||
if (!((action[i] >= '0' && action[i] <= '9') || (action[i] >= 'a' && action[i] <= 'f') || (action[i] >= 'A' && action[i] <= 'F') || ('r' == action[i] && i == length - 1) || ('w' == action[i] && i == length - 1))) { // check for hexadecimal character or r/w flag
|
||||
return false; // not an hexadecimal string
|
||||
}
|
||||
}
|
||||
|
@ -337,18 +337,18 @@ static bool busvoodoo_i2c_action(const char* action, uint32_t repetition, bool p
|
|||
return true;
|
||||
}
|
||||
uint32_t value = strtol(&action[1], NULL, 16); // get hex value
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
if ('r'==action[length-1]) {
|
||||
for (uint32_t i = 0; i < repetition; i++) {
|
||||
if ('r' == action[length - 1]) {
|
||||
busvoodoo_i2c_select(value, false); // select slave to read
|
||||
} else if ('w'==action[length-1]) {
|
||||
} else if ('w' == action[length - 1]) {
|
||||
busvoodoo_i2c_select(value, true); // select slave to write
|
||||
} else {
|
||||
busvoodoo_i2c_write(value); // write to I2C
|
||||
}
|
||||
}
|
||||
} else if ('b'==action[0] && length>1) { // send binary value
|
||||
for (uint32_t i=1; i<length; i++) { // check string
|
||||
if (!('0'==action[i] || '1'==action[i] || ('r'==action[i] && i==length-1) || ('w'==action[i] && i==length-1))) { // check for binary character or r/w flag
|
||||
} else if ('b' == action[0] && length > 1) { // send binary value
|
||||
for (uint32_t i = 1; i < length; i++) { // check string
|
||||
if (!('0' == action[i] || '1' == action[i] || ('r' == action[i] && i == length - 1) || ('w' == action[i] && i == length - 1))) { // check for binary character or r/w flag
|
||||
return false; // not a binary string
|
||||
}
|
||||
}
|
||||
|
@ -356,18 +356,18 @@ static bool busvoodoo_i2c_action(const char* action, uint32_t repetition, bool p
|
|||
return true;
|
||||
}
|
||||
uint32_t value = strtol(&action[1], NULL, 2); // get binary value
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
if ('r'==action[length-1]) {
|
||||
for (uint32_t i = 0; i < repetition; i++) {
|
||||
if ('r' == action[length - 1]) {
|
||||
busvoodoo_i2c_select(value, false); // select slave to read
|
||||
} else if ('w'==action[length-1]) {
|
||||
} else if ('w' == action[length - 1]) {
|
||||
busvoodoo_i2c_select(value, true); // select slave to write
|
||||
} else {
|
||||
busvoodoo_i2c_write(value); // write to I2C
|
||||
}
|
||||
}
|
||||
} else if (action[0]>='1' && action[0]<='9') { // send decimal value
|
||||
for (uint32_t i=1; i<length; i++) { // check string
|
||||
if (!((action[i]>='0' && action[i]<='9') || ('w'==action[i] && i==length-1))) { // check for decimal character or r/w flag
|
||||
} else if (action[0] >= '1' && action[0] <= '9') { // send decimal value
|
||||
for (uint32_t i = 1; i < length; i++) { // check string
|
||||
if (!((action[i] >= '0' && action[i] <= '9') || ('w' == action[i] && i == length - 1))) { // check for decimal character or r/w flag
|
||||
return false; // not a decimal string
|
||||
}
|
||||
}
|
||||
|
@ -375,21 +375,21 @@ static bool busvoodoo_i2c_action(const char* action, uint32_t repetition, bool p
|
|||
return true;
|
||||
}
|
||||
uint32_t value = strtol(&action[0], NULL, 10); // get decimal value
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
if ('r'==action[length-1]) {
|
||||
for (uint32_t i = 0; i < repetition; i++) {
|
||||
if ('r' == action[length - 1]) {
|
||||
busvoodoo_i2c_select(value, false); // select slave to read
|
||||
} else if ('w'==action[length-1]) {
|
||||
} else if ('w' == action[length - 1]) {
|
||||
busvoodoo_i2c_select(value, true); // select slave to write
|
||||
} else {
|
||||
busvoodoo_i2c_write(value); // write to I2C
|
||||
}
|
||||
}
|
||||
} else if (length>=2 && ('"'==action[0] || '\''==action[0]) && (action[length-1]==action[0])) { // send ASCII character
|
||||
} else if (length >= 2 && ('"' == action[0] || '\'' == action[0]) && (action[length - 1] == action[0])) { // send ASCII character
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
for (uint32_t r=0; r<repetition; r++) {
|
||||
for (uint32_t i=1; i<length-1; i++) { // go through string
|
||||
for (uint32_t r = 0; r < repetition; r++) {
|
||||
for (uint32_t i = 1; i < length - 1; i++) { // go through string
|
||||
busvoodoo_i2c_write(action[i]); // write to I2C
|
||||
}
|
||||
}
|
||||
|
@ -418,7 +418,7 @@ static void busvoodoo_i2c_command_reset(void* argument)
|
|||
*/
|
||||
static void busvoodoo_i2c_command_actions(void* argument)
|
||||
{
|
||||
if (NULL==argument || 0==strlen(argument)) {
|
||||
if (NULL == argument || 0 == strlen(argument)) {
|
||||
printf("available actions (separated by space or ,):\n");
|
||||
printf("[/]\tsend start/stop conditions\n");
|
||||
printf("0x0r\tselect slave with I2C address to read\n");
|
||||
|
@ -433,11 +433,11 @@ static void busvoodoo_i2c_command_actions(void* argument)
|
|||
return;
|
||||
}
|
||||
// copy argument since it will be modified
|
||||
char* copy = calloc(strlen(argument)+1, sizeof(char));
|
||||
char* copy = calloc(strlen(argument) + 1, sizeof(char));
|
||||
if (!copy) {
|
||||
while (true);
|
||||
}
|
||||
strncpy(copy, argument, strlen(argument)+1);
|
||||
strncpy(copy, argument, strlen(argument) + 1);
|
||||
// verify and perform actions
|
||||
if (!busvoodoo_global_actions(copy, false, &busvoodoo_i2c_action)) { // verify actions
|
||||
printf("malformed action(s)\n");
|
||||
|
@ -467,12 +467,12 @@ static void busvoodoo_i2c_command_scan(void* argument)
|
|||
int16_t i2c_slaves = 0; // number of slaves found
|
||||
// check for I2C slaves by going through the address space
|
||||
printf("I2C slaves found: ");
|
||||
for (uint16_t address=0; address < (1<<busvoodoo_i2c_addressbits); address++) {
|
||||
for (uint16_t address = 0; address < (1 << busvoodoo_i2c_addressbits); address++) {
|
||||
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show transmission
|
||||
switch (i2c_master_select_slave(BUSVOODOO_I2C, address, 10==busvoodoo_i2c_addressbits, false)) { // try to select slave (includes start condition)
|
||||
switch (i2c_master_select_slave(BUSVOODOO_I2C, address, 10 == busvoodoo_i2c_addressbits, false)) { // try to select slave (includes start condition)
|
||||
case I2C_MASTER_RC_NONE: // slave found
|
||||
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we found a slave
|
||||
printf((busvoodoo_i2c_addressbits>7) ? "0x%03x " : "0x%02x ", address); // display address
|
||||
printf((busvoodoo_i2c_addressbits > 7) ? "0x%03x " : "0x%02x ", address); // display address
|
||||
i2c_slaves++; // increase slave count
|
||||
break;
|
||||
case I2C_MASTER_RC_START_STOP_IN_PROGESS: // I2C peripheral error
|
||||
|
@ -495,16 +495,17 @@ static void busvoodoo_i2c_command_scan(void* argument)
|
|||
i2c_slaves = -1; // remember error
|
||||
break;
|
||||
}
|
||||
if (i2c_slaves<0) { // error happened
|
||||
// TODO use proper flag
|
||||
if (i2c_slaves < 0) { // error happened
|
||||
busvoodoo_leds_blink(0.5, 0.5); // show error on LEDs
|
||||
if (-1==i2c_slaves) { // just end communication
|
||||
if (-1 == i2c_slaves) { // just end communication
|
||||
i2c_master_stop(BUSVOODOO_I2C); // send stop condition
|
||||
} else if (-2==i2c_slaves) { // reset peripheral
|
||||
} else if (-2 == i2c_slaves) { // reset peripheral
|
||||
busvoodoo_i2c_command_reset(NULL);
|
||||
}
|
||||
break; // stop scan
|
||||
} else {
|
||||
if (I2C_MASTER_RC_NONE!=i2c_master_stop(BUSVOODOO_I2C)) { // stop stop condition
|
||||
if (I2C_MASTER_RC_NONE != i2c_master_stop(BUSVOODOO_I2C)) { // stop stop condition
|
||||
i2c_master_reset(BUSVOODOO_I2C); // reset the I2C peripheral since it might be stuck
|
||||
printf("stop condition failed\n"); // show error to user
|
||||
busvoodoo_leds_blink(0.5, 0.5); // show error on LEDs
|
||||
|
@ -514,7 +515,7 @@ static void busvoodoo_i2c_command_scan(void* argument)
|
|||
}
|
||||
}
|
||||
printf("\n");
|
||||
if (i2c_slaves>=0) {
|
||||
if (i2c_slaves >= 0) {
|
||||
printf("%u slave(s) found\n", i2c_slaves); // show summary
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue