CuVoodoo STM32F1 firmware template
busvoodoo_i2c.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
21 /* standard libraries */
22 #include <stdint.h> // standard integer types
23 #include <stdlib.h> // standard utilities
24 #include <string.h> // string utilities
25 
26 /* STM32 (including CM3) libraries */
27 #include <libopencm3/stm32/i2c.h> // I2C library
28 
29 /* own libraries */
30 #include "global.h" // board definitions
31 #include "print.h" // printing utilities
32 #include "menu.h" // menu definitions
33 #include "i2c_master.h" // I2C utilities
34 #include "busvoodoo_global.h" // BusVoodoo definitions
35 #include "busvoodoo_oled.h" // OLED utilities
36 #include "busvoodoo_i2c.h" // own definitions
37 
41 #define BUSVOODOO_I2C I2C2
45 static enum busvoodoo_i2c_setting_t {
46  BUSVOODOO_I2C_SETTING_NONE,
47  BUSVOODOO_I2C_SETTING_SPEED,
48  BUSVOODOO_I2C_SETTING_ADDRESSBITS,
49  BUSVOODOO_I2C_SETTING_PULLUP,
50  BUSVOODOO_I2C_SETTING_DONE,
51 } busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_NONE;
53 uint16_t busvoodoo_i2c_speed = 100;
58 
64 static bool busvoodoo_i2c_setup(char** prefix, const char* line)
65 {
66  bool complete = false; // is the setup complete
67  if (NULL==line) { // first call
68  busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_NONE; // re-start configuration
69  }
70  switch (busvoodoo_i2c_setting) {
71  case BUSVOODOO_I2C_SETTING_NONE:
73  *prefix = busvoodoo_global_string; // ask for speed
74  busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_SPEED;
75  break;
76  case BUSVOODOO_I2C_SETTING_SPEED:
77  if (NULL==line || 0==strlen(line)) { // use default setting
78  busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_ADDRESSBITS; // go to next setting
79  } else { // setting provided
80  uint32_t speed = atoi(line); // parse setting
81  if (speed>0 && speed<=400) { // check setting
82  busvoodoo_i2c_speed = speed; // remember setting
83  busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_ADDRESSBITS; // go to next setting
84  }
85  }
86  if (BUSVOODOO_I2C_SETTING_ADDRESSBITS==busvoodoo_i2c_setting) { // if next setting
87  snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "address size in bits (7,10) [%u]", busvoodoo_i2c_addressbits); // prepare next setting
88  *prefix = busvoodoo_global_string; // display next setting
89  }
90  break;
91  case BUSVOODOO_I2C_SETTING_ADDRESSBITS:
92  if (NULL==line || 0==strlen(line)) { // use default setting
93  busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_PULLUP; // go to next setting
94  } else { // setting provided
95  uint8_t addressbits = atoi(line); // parse setting
96  if (7==addressbits || 10==addressbits) { // check setting
97  busvoodoo_i2c_addressbits = addressbits; // remember setting
98  busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_PULLUP; // go to next setting
99  }
100  }
101  if (BUSVOODOO_I2C_SETTING_PULLUP==busvoodoo_i2c_setting) { // if next setting
102  printf("1) open-drain, with embedded pull-up resistors (2kO)\n");
103  printf("2) open-drain, with external pull-up resistors\n");
104  snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "drive mode (1,2) [%c]", busvoodoo_i2c_embedded_pullup ? '1' : '2'); // show drive mode
105  *prefix = busvoodoo_global_string; // display next setting
106  }
107  break;
108  case BUSVOODOO_I2C_SETTING_PULLUP:
109  if (NULL==line || 0==strlen(line)) { // use default setting
110  busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_DONE; // go to next setting
111  } else if (1==strlen(line)) { // setting provided
112  uint8_t drive = atoi(line); // parse setting
113  if (1==drive || 2==drive) { // check setting
114  busvoodoo_i2c_embedded_pullup = (1==drive); // remember setting
115  busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_DONE; // go to next setting
116  }
117  }
118  if (BUSVOODOO_I2C_SETTING_DONE==busvoodoo_i2c_setting) { // we have all settings, configure I2C
121  busvoodoo_embedded_pullup(true); // set pull-up
122  printf("use LV to set pull-up voltage\n");
123  }
124  led_off(); // disable LED because there is no activity
125  busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_NONE; // restart settings next time
126  *prefix = "I2C"; // display mode
127  busvoodoo_oled_text_left(*prefix); // set mode title on OLED display
128  const char* pinout_io[10] = {"GND", "5V", "3V3", "LV", "SDA", "SCL", NULL, NULL}; // HiZ mode pinout
129  for (uint8_t i=0; i<LENGTH(pinout_io) && i<LENGTH(busvoodoo_global_pinout_io); i++) {
130  busvoodoo_global_pinout_io[i] = pinout_io[i]; // set pin names
131  }
132  busvoodoo_oled_text_pinout(pinout_io, true); // set pinout on display
133  busvoodoo_oled_update(); // update display to show text and pinout
134  complete = true; // configuration is complete
135  }
136  break;
137  case BUSVOODOO_I2C_SETTING_DONE: // this case is already handled before
138  default: // unknown case
139  busvoodoo_i2c_setting = BUSVOODOO_I2C_SETTING_NONE; // restart settings next time
140  complete = false; // restart setting
141  break;
142  }
143  return complete;
144 }
145 
148 static void busvoodoo_i2c_exit(void)
149 {
150  i2c_master_release(BUSVOODOO_I2C); // release I2C peripheral
151  busvoodoo_embedded_pullup(false); // disable embedded pull-ups
152 }
153 
155 static void busvoodoo_i2c_read(void)
156 {
157  printf("read: ");
158  busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we read data
159  uint8_t data; // to store the data read
160  switch (i2c_master_read(BUSVOODOO_I2C, &data, 1)) {
161  case I2C_MASTER_RC_NONE: // all went fine
162  printf("0x%02x", data); // show data
163  break;
165  printf("slave not selected");
166  break;
168  printf("slave not ready");
169  break;
171  printf("not in receive mode");
172  break;
173  default:
174  printf("error");
175  }
176  printf("\n");
177 }
178 
182 static void busvoodoo_i2c_write(uint8_t data)
183 {
184  printf("write 0x%02x: ", data);
185  busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse red LED to show we send data
186  switch (i2c_master_write(BUSVOODOO_I2C, &data, 1)) {
187  case I2C_MASTER_RC_NONE: // all went fine
188  printf("ack");
189  break;
190  case I2C_MASTER_RC_NAK:
191  printf("nack");
192  break;
194  printf("slave not selected");
195  break;
197  printf("slave not ready");
198  break;
200  printf("not in transmit mode");
201  break;
202  default:
203  printf("error");
204  }
205  printf("\n");
206 }
207 
212 static void busvoodoo_i2c_select(uint16_t slave, bool write)
213 {
214  printf("select slave ");
215  if (10==busvoodoo_i2c_addressbits) {
216  printf("0x%03x", slave);
217  } else {
218  printf("0x%02x", slave);
219  }
220  printf(" to %s: ", write ? "write" : "read");
221  busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse red LED to show we send the slave address
223  case I2C_MASTER_RC_NONE: // all went fine
224  printf("selected");
225  break;
226  case I2C_MASTER_RC_NAK:
227  printf("no such slave");
228  break;
230  printf("can't become master");
231  break;
233  printf("not in receive mode");
234  break;
236  printf("not in transmit mode");
237  break;
238  default:
239  printf("error");
240  }
241  printf("\n");
242 }
243 
250 static bool busvoodoo_i2c_action(const char* action, uint32_t repetition, bool perform)
251 {
252  uint32_t length = strlen(action); // remember length since it will be used a number of times
253  if (NULL==action || 0==length) { // there is nothing to do
254  return true;
255  }
256 
257  if (1==length && 'r'==action[0]) { // read data
258  if (!perform) {
259  return true;
260  }
261  for (uint32_t i=0; i<repetition; i++) {
262  busvoodoo_i2c_read(); // read from I2C
263  }
264  } else if (1==length && '['==action[0]) { // select slave
265  if (!perform) {
266  return true;
267  }
268  printf("send start condition: ");
269  for (uint32_t i=0; i<repetition; i++) {
270  busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse red LED to show we send start condition
271  printf("%s ", I2C_MASTER_RC_NONE==i2c_master_start(BUSVOODOO_I2C) ? "sent" : "error");
272  }
273  printf("\n");
274  } else if (1==length && ']'==action[0]) { // deselect slave
275  if (!perform) {
276  return true;
277  }
278  printf("send stop condition: ");
279  for (uint32_t i=0; i<repetition; i++) {
280  busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse red LED to show we send stop condition
281  printf("%s ", I2C_MASTER_RC_NONE==i2c_master_stop(BUSVOODOO_I2C) ? "sent" : "error");
282  }
283  printf("\n");
284  } else if (1==length && 'u'==action[0]) { // sleep us
285  if (!perform) {
286  return true;
287  }
288  printf("wait for %u us\n", repetition);
289  sleep_us(repetition); // sleep
290  } else if (1==length && 'm'==action[0]) { // sleep ms
291  if (!perform) {
292  return true;
293  }
294  printf("wait for %u ms\n", repetition);
295  sleep_ms(repetition); // sleep
296  } else if ('0'==action[0]) { // send digit
297  if (1==length) { // just send 0
298  if (!perform) {
299  return true;
300  }
301  for (uint32_t i=0; i<repetition; i++) {
302  busvoodoo_i2c_write(0); // write to I2C
303  }
304  } else if ('x'==action[1] || 'b'==action[1]) { // send hex/binary
305  return busvoodoo_i2c_action(action+1, repetition, perform); // just retry without leading 0
306  } else if (action[1]>='0' && action[1]<='9') { // send decimal
307  return busvoodoo_i2c_action(action+1, repetition, perform); // just retry without leading 0
308  } else if ('r'==action[1] && 2==length) { // select slave to read
309  busvoodoo_i2c_select(0, false); // select slave 0 to read
310  } else if ('w'==action[1] && 2==length) { // select slave to write
311  busvoodoo_i2c_select(0, true); // select slave 0 to write
312  } else { // malformed action
313  return false;
314  }
315  } else if ('x'==action[0] && length>1) { // send hexadecimal value
316  for (uint32_t i=1; i<length; i++) { // check string
317  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
318  return false; // not an hexadecimal string
319  }
320  }
321  if (!perform) {
322  return true;
323  }
324  uint32_t value = strtol(&action[1], NULL, 16); // get hex value
325  for (uint32_t i=0; i<repetition; i++) {
326  if ('r'==action[length-1]) {
327  busvoodoo_i2c_select(value, false); // select slave to read
328  } else if ('w'==action[length-1]) {
329  busvoodoo_i2c_select(value, true); // select slave to write
330  } else {
331  busvoodoo_i2c_write(value); // write to I2C
332  }
333  }
334  } else if ('b'==action[0] && length>1) { // send binary value
335  for (uint32_t i=1; i<length; i++) { // check string
336  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
337  return false; // not a binary string
338  }
339  }
340  if (!perform) {
341  return true;
342  }
343  uint32_t value = strtol(&action[1], NULL, 2); // get binary value
344  for (uint32_t i=0; i<repetition; i++) {
345  if ('r'==action[length-1]) {
346  busvoodoo_i2c_select(value, false); // select slave to read
347  } else if ('w'==action[length-1]) {
348  busvoodoo_i2c_select(value, true); // select slave to write
349  } else {
350  busvoodoo_i2c_write(value); // write to I2C
351  }
352  }
353  } else if (action[0]>='1' && action[0]<='9') { // send decimal value
354  for (uint32_t i=1; i<length; i++) { // check string
355  if (!((action[i]>='0' && action[i]<='9') || ('w'==action[i] && i==length-1))) { // check for decimal character or r/w flag
356  return false; // not a decimal string
357  }
358  }
359  if (!perform) {
360  return true;
361  }
362  uint32_t value = strtol(&action[0], NULL, 10); // get decimal value
363  for (uint32_t i=0; i<repetition; i++) {
364  if ('r'==action[length-1]) {
365  busvoodoo_i2c_select(value, false); // select slave to read
366  } else if ('w'==action[length-1]) {
367  busvoodoo_i2c_select(value, true); // select slave to write
368  } else {
369  busvoodoo_i2c_write(value); // write to I2C
370  }
371  }
372  } else if (length>=2 && ('"'==action[0] || '\''==action[0]) && (action[length-1]==action[0])) { // send ASCII character
373  if (!perform) {
374  return true;
375  }
376  for (uint32_t r=0; r<repetition; r++) {
377  for (uint32_t i=1; i<length-1; i++) { // go through string
378  busvoodoo_i2c_write(action[i]); // write to I2C
379  }
380  }
381  } else { // malformed action
382  return false;
383  }
384  return true; // all went well
385 }
386 
387 // command handlers
388 
392 static void busvoodoo_i2c_command_actions(void* argument)
393 {
394  if (NULL==argument || 0==strlen(argument)) {
395  printf("available actions (separated by space or ,):\n");
396  printf("[/]\tsend start/stop conditions\n");
397  printf("0x0r\tselect slave with I2C address to read\n");
398  printf("0x0w\tselect slave with I2C address to write\n");
399  printf("0\twrite decimal value\n");
400  printf("0x0\twrite hexadecimal value\n");
401  printf("0b0\twrite binary value\n");
402  printf("\"a\"/'a'\twrite ASCII characters\n");
403  printf("r\tread value\n");
404  printf("u/m\twait 1 us/ms\n");
405  printf(":n\trepeat action n times\n");
406  return;
407  }
408  // copy argument since it will be modified
409  char* copy = calloc(strlen(argument)+1, sizeof(char));
410  if (!copy) {
411  while (true);
412  }
413  strncpy(copy, argument, strlen(argument)+1);
414  // verify and perform actions
415  if (!busvoodoo_global_actions(copy, false, &busvoodoo_i2c_action)) { // verify actions
416  printf("malformed action(s)\n");
417  } else { // action are ok
418  busvoodoo_global_actions(argument, true, &busvoodoo_i2c_action); // perform action
419  }
420  free(copy); // release memory
421 }
422 
426 static void busvoodoo_i2c_command_scan(void* argument)
427 {
428  (void)argument; // we won't use the argument
429  if (!i2c_master_check_signals(BUSVOODOO_I2C)) { // ensure SCL and SDA are high
430  printf("SCL or/and SDA are low. The signals need to be pulled up\n");
432  printf("set pull-up voltage with LV\n");
433  }
434  led_blink(0.5, 0.5); // show error on LEDs
435  return; // stop here
436  } else {
437  led_off(); // switch LEDs off
438  }
439  printf("scanning for I2C slaves\n");
440  int16_t i2c_slaves = 0; // number of slaves found
441  // check for I2C slaves by going through the address space
442  for (uint16_t address=0; address < (1<<busvoodoo_i2c_addressbits); address++) {
443  busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse red LED to show transmission
444  switch (i2c_master_select_slave(BUSVOODOO_I2C, address, 10==busvoodoo_i2c_addressbits, false)) { // try to select slave (includes start condition)
445  case I2C_MASTER_RC_NONE: // slave found
446  busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we found a slave
447  printf((busvoodoo_i2c_addressbits>7) ? "0x%03x " : "0x%02x ", address); // display address
448  i2c_slaves++; // increase slave count
449  break;
450  case I2C_MASTER_RC_START_STOP_IN_PROGESS: // I2C peripheral error
451  i2c_master_reset(BUSVOODOO_I2C); // reset the I2C peripheral since it might be stuck
452  printf("start condition failed\n"); // show error to user
453  led_blink(0.5, 0.5); // show error on LEDs
454  i2c_slaves = -2; // remember error
455  break; // stop scanning
456  case I2C_MASTER_RC_NOT_MASTER: // start condition failed
457  i2c_master_reset(BUSVOODOO_I2C); // reset the I2C peripheral since it might be stuck
458  printf("start condition failed\n"); // show error to user
459  led_blink(0.5, 0.5); // show error on LEDs
460  i2c_slaves = -1; // remember error
461  break; // stop scanning
462  case I2C_MASTER_RC_NAK: // slave did not respond
463  break; // nothing to do
464  default: // unexpected error
465  i2c_slaves = -1; // remember error
466  break;
467  }
468  if (i2c_slaves<0) { // error happend
469  led_blink(0.5, 0.5); // show error on LEDs
470  if (-1==i2c_slaves) { // just end communication
471  i2c_master_stop(BUSVOODOO_I2C); // send stop condition
472  } else if (-2==i2c_slaves) { // reset peripheral
473  i2c_master_reset(BUSVOODOO_I2C); // reset the I2C peripheral since it might be stuck
474  }
475  break; // stop scan
476  } else {
477  if (I2C_MASTER_RC_NONE!=i2c_master_stop(BUSVOODOO_I2C)) { // stop stop condition
478  i2c_master_reset(BUSVOODOO_I2C); // reset the I2C peripheral since it might be stuck
479  printf("stop condition failed\n"); // show error to user
480  led_blink(0.5, 0.5); // show error on LEDs
481  i2c_slaves = -1; // remember error
482  break;
483  }
484  }
485  }
486  if (i2c_slaves>0) {
487  printf("\n");
488  }
489  if (i2c_slaves>=0) {
490  printf("%u slave(s) found\n", i2c_slaves); // show summary
491  }
492 }
493 
495 static const struct menu_command_t busvoodoo_i2c_commands[] = {
496  {
497  'a',
498  "action",
499  "perform protocol actions",
501  "[actions]",
503  },
504  {
505  's',
506  "scan",
507  "scan for slave devices",
509  NULL,
511  },
512 };
513 
515  "i2c",
516  "Inter-Integrated Circuit",
521 };
library to communicate using I2C as master (API)
command menu entry
Definition: menu.h:31
enum i2c_master_rc i2c_master_stop(uint32_t i2c)
sent stop condition
Definition: i2c_master.c:425
#define BUSVOODOO_LED_PULSE
recommended duration in ms for pulsing LEDs to show activity
BusVoodoo global definitions and methods (API)
struct busvoodoo_mode_t busvoodoo_i2c_mode
I2C mode interface definition.
enum i2c_master_rc i2c_master_select_slave(uint32_t i2c, uint16_t slave, bool address_10bit, bool write)
select I2C slave device
Definition: i2c_master.c:287
static void busvoodoo_i2c_write(uint8_t data)
write to I2C
not in receive mode
Definition: i2c_master.h:29
void busvoodoo_oled_text_pinout(const char *pins[10], bool io_connector)
draw pin names on bottom (blue) part in display buffer
void busvoodoo_led_red_pulse(uint16_t ms)
pulse red LED for short duration
enum i2c_master_rc i2c_master_read(uint32_t i2c, uint8_t *data, size_t data_size)
read data over I2C
Definition: i2c_master.c:356
void i2c_master_reset(uint32_t i2c)
reset I2C peripheral, fixing any locked state
Definition: i2c_master.c:235
void led_off(void)
switch off board LED
Definition: global.c:82
enum i2c_master_rc i2c_master_start(uint32_t i2c)
send start condition
Definition: i2c_master.c:265
void busvoodoo_oled_update(void)
update OLED display RAM with current display buffer
uint16_t busvoodoo_i2c_speed
I2C speed (in kHz)
Definition: busvoodoo_i2c.c:53
global definitions and methods (API)
static void busvoodoo_i2c_exit(void)
exit I2C mode
void busvoodoo_led_blue_pulse(uint16_t ms)
pulse blue LED for short duration
void sleep_us(uint32_t duration)
go to sleep for some microseconds
Definition: global.c:144
not in master mode
Definition: i2c_master.h:27
static void busvoodoo_i2c_select(uint16_t slave, bool write)
select I2C slave
enum i2c_master_rc i2c_master_write(uint32_t i2c, const uint8_t *data, size_t data_size)
write data over I2C
Definition: i2c_master.c:391
a start or stop condition is already in progress
Definition: i2c_master.h:26
#define BUSVOODOO_I2C
I2C peripheral.
Definition: busvoodoo_i2c.c:41
bool busvoodoo_i2c_embedded_pullup
if embedded or external pull-up resistors are use
Definition: busvoodoo_i2c.c:57
void i2c_master_setup(uint32_t i2c, uint16_t frequency)
setup I2C peripheral
Definition: i2c_master.c:173
void i2c_master_release(uint32_t i2c)
release I2C peripheral
Definition: i2c_master.c:211
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
static bool busvoodoo_i2c_setup(char **prefix, const char *line)
setup I2C mode
Definition: busvoodoo_i2c.c:64
char busvoodoo_global_string[64]
shared string buffer, i.e.
static const struct menu_command_t busvoodoo_i2c_commands[]
I2C menu commands.
bool busvoodoo_global_actions(char *actions, bool perform, bool(*action_handler)(const char *action, uint32_t repetition, bool perform))
parse and perform actions
static bool busvoodoo_i2c_action(const char *action, uint32_t repetition, bool perform)
perform I2C action
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:159
slave is not read (previous operations has been nacked)
Definition: i2c_master.h:30
void busvoodoo_oled_text_left(char *text)
draw mode text on top (yellow) left side in display buffer
float busvoodoo_embedded_pullup(bool on)
enable embedded pull-up resistors
static void busvoodoo_i2c_read(void)
read from I2C
#define LENGTH(x)
get the length of an array
Definition: global.h:26
bool i2c_master_check_signals(uint32_t i2c)
check if SDA and SCL signals are high
Definition: i2c_master.c:225
not in transmit mode
Definition: i2c_master.h:28
library to show BusVoodoo mode information on SSD1306 OLED display: name, activity, pinout (API)
static enum busvoodoo_i2c_setting_t busvoodoo_i2c_setting
current mode setup stage
BusVoodoo mode interface.
static void busvoodoo_i2c_command_actions(void *argument)
command to perform actions
uint8_t busvoodoo_i2c_addressbits
I2C address bits (7 or 10)
Definition: busvoodoo_i2c.c:55
BusVoodoo I²C mode (API)
static void busvoodoo_i2c_command_scan(void *argument)
command to scan for slave devices
not acknowledge received
Definition: i2c_master.h:31