CuVoodoo STM32F1 firmware template
busvoodoo_spi.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/gpio.h> // general purpose input output library
28 #include <libopencm3/stm32/rcc.h> // real-time control clock library
29 #include <libopencm3/stm32/spi.h> // SPI library
30 
31 /* own libraries */
32 #include "global.h" // board definitions
33 #include "print.h" // printing utilities
34 #include "menu.h" // menu definitions
35 #include "busvoodoo_global.h" // BusVoodoo definitions
36 #include "busvoodoo_oled.h" // OLED utilities
37 #include "busvoodoo_spi.h" // own definitions
38 
42 #define BUSVOODOO_SPI_ID 2
46 static enum busvoodoo_spi_setting_t {
47  BUSVOODOO_SPI_SETTING_NONE,
48  BUSVOODOO_SPI_SETTING_DUPLEX,
49  BUSVOODOO_SPI_SETTING_FREQUENCY,
50  BUSVOODOO_SPI_SETTING_DATABITS,
51  BUSVOODOO_SPI_SETTING_BITORDER,
52  BUSVOODOO_SPI_SETTING_MODE,
53  BUSVOODOO_SPI_SETTING_DRIVE,
54  BUSVOODOO_SPI_SETTING_DONE,
55 } busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_NONE;
57 static bool busvoodoo_spi_duplex = true;
59 static uint8_t busvoodoo_spi_baudrate = 1;
61 static uint8_t busvoodoo_spi_databits = 8;
63 static bool busvoodoo_spi_bitorder = true;
65 static uint8_t busvoodoo_spi_standard_mode = 0;
67 static bool busvoodoo_spi_drive = true;
69 static bool busvoodoo_spi_pullup = true;
70 
76 static bool busvoodoo_spi_setup(char** prefix, const char* line)
77 {
78  bool complete = false; // is the setup complete
79  if (NULL==line) { // first call
80  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_NONE; // re-start configuration
81  }
82  switch (busvoodoo_spi_setting) {
83  case BUSVOODOO_SPI_SETTING_NONE:
84  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DUPLEX;
85  printf("1) full-duplex\n");
86  printf("2) bidirectional\n");
88  *prefix = busvoodoo_global_string; // ask for setting
89  break;
90  case BUSVOODOO_SPI_SETTING_DUPLEX:
91  if (NULL==line || 0==strlen(line)) { // use default setting
92  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_FREQUENCY; // go to next setting
93  } else if (1==strlen(line)) { // setting provided
94  uint8_t duplex = atoi(line); // parse setting
95  if (1==duplex || 2==duplex) { // check setting
96  busvoodoo_spi_duplex = (1==duplex); // remember setting
97  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_FREQUENCY; // go to next setting
98  }
99  }
100  if (BUSVOODOO_SPI_SETTING_FREQUENCY==busvoodoo_spi_setting) { // if next setting
101  // SPI2 used APB1 as PCLK, which has a maximum frequency of 36 MHz
102  for (uint8_t div=0; div<8; div++) {
103  printf("%u) %u kHz\n", div+1, 36000/(2<<div)); // print possible frequencies
104  }
106  *prefix = busvoodoo_global_string; // display next setting
107  }
108  break;
109  case BUSVOODOO_SPI_SETTING_FREQUENCY:
110  if (NULL==line || 0==strlen(line)) { // use default setting
111  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DATABITS; // go to next setting
112  } else if (1==strlen(line)) { // setting provided
113  uint8_t baudrate = atoi(line); // parse setting
114  if (baudrate>0 && baudrate<=9) { // check setting
115  busvoodoo_spi_baudrate = baudrate-1; // remember setting
116  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DATABITS; // go to next setting
117  }
118  }
119  if (BUSVOODOO_SPI_SETTING_DATABITS==busvoodoo_spi_setting) { // if next setting
120  snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "data frame width in bits (8,16) [%u]", busvoodoo_spi_databits); // prepare next setting
121  *prefix = busvoodoo_global_string; // display next setting
122  }
123  break;
124  case BUSVOODOO_SPI_SETTING_DATABITS:
125  if (NULL==line || 0==strlen(line)) { // use default setting
126  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_BITORDER; // go to next setting
127  } else if (1==strlen(line) || 2==strlen(line)) { // setting provided
128  uint8_t databits = atoi(line); // parse setting
129  if (8==databits || 16==databits) { // check setting
130  busvoodoo_spi_databits = databits; // remember setting
131  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_BITORDER; // go to next setting
132  }
133  }
134  if (BUSVOODOO_SPI_SETTING_BITORDER==busvoodoo_spi_setting) { // if next setting
135  printf("1) most significant bit first\n");
136  printf("2) least significant bit first\n");
137  snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "data frame bit order (1,2) [%c]", busvoodoo_spi_bitorder ? '1' : '2'); // prepare next setting
138  *prefix = busvoodoo_global_string; // display next setting
139  }
140  break;
141  case BUSVOODOO_SPI_SETTING_BITORDER:
142  if (NULL==line || 0==strlen(line)) { // use default setting
143  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_MODE; // go to next setting
144  } else if (1==strlen(line)) { // setting provided
145  uint8_t bitorder = atoi(line); // parse setting
146  if (1==bitorder || 2==bitorder) { // check setting
147  busvoodoo_spi_bitorder = (1==bitorder); // remember setting
148  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_MODE; // go to next setting
149  }
150  }
151  if (BUSVOODOO_SPI_SETTING_MODE==busvoodoo_spi_setting) { // if next setting
152  printf("1) mode 0 (clock polarity: idle low, clock phase: sample data on rising edge)\n");
153  printf("2) mode 1 (clock polarity: idle low, clock phase: sample data on falling edge)\n");
154  printf("3) mode 2 (clock polarity: idle high, clock phase: sample data on falling edge)\n");
155  printf("4) mode 3 (clock polarity: idle high, clock phase: sample data on rising edge)\n");
156  snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "mode (1,2,3,4) [%u]", busvoodoo_spi_standard_mode+1); // prepare next setting
157  *prefix = busvoodoo_global_string; // display next setting
158  }
159  break;
160  case BUSVOODOO_SPI_SETTING_MODE:
161  if (NULL==line || 0==strlen(line)) { // use default setting
162  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DRIVE; // go to next setting
163  } else if (1==strlen(line)) { // setting provided
164  uint8_t mode = atoi(line); // parse setting
165  if (mode>=1 && mode<=4) {
166  busvoodoo_spi_standard_mode = mode-1; // remember setting
167  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DRIVE; // go to next setting
168  }
169  }
170  if (BUSVOODOO_SPI_SETTING_DRIVE==busvoodoo_spi_setting) { // if next setting
171  printf("1) push-pull (3.3V)\n");
172  printf("2) open-drain, with embedded pull-up resistors (2kO)\n");
173  printf("3) open-drain, with external pull-up resistors\n");
174  snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "drive mode (1,2,3) [%c]", busvoodoo_spi_drive ? '1' : (busvoodoo_spi_pullup ? '2' : '3')); // show drive mode
175  *prefix = busvoodoo_global_string; // display next setting
176  }
177  break;
178  case BUSVOODOO_SPI_SETTING_DRIVE:
179  if (NULL==line || 0==strlen(line)) { // use default setting
180  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DONE; // go to next setting
181  } else if (1==strlen(line)) { // setting provided
182  uint8_t drive = atoi(line); // parse setting
183  if (1==drive || 2==drive || 3==drive) { // check setting
184  busvoodoo_spi_drive = (1==drive); // remember setting
185  busvoodoo_spi_pullup = (2==drive); // remember setting
186  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DONE; // go to next setting
187  }
188  }
189  if (BUSVOODOO_SPI_SETTING_DONE==busvoodoo_spi_setting) { // we have all settings, configure SPI
190  rcc_periph_clock_enable(RCC_AFIO); // enable clock for SPI alternate function
191  rcc_periph_clock_enable(RCC_SPI(BUSVOODOO_SPI_ID)); // enable clock for SPI peripheral
192  spi_reset(SPI(BUSVOODOO_SPI_ID)); // clear SPI values to default
193  spi_set_baudrate_prescaler(SPI(BUSVOODOO_SPI_ID), busvoodoo_spi_baudrate); // set baud rate (i.e. frequency)
194  spi_set_standard_mode(SPI(BUSVOODOO_SPI_ID), busvoodoo_spi_standard_mode); // set SPI mode
195  if (8==busvoodoo_spi_databits) { // set data frame bit width
196  spi_set_dff_8bit(SPI(BUSVOODOO_SPI_ID)); // set to 8 bits
197  } else {
198  spi_set_dff_16bit(SPI(BUSVOODOO_SPI_ID)); // set to 16 bits
199  }
200  if (busvoodoo_spi_bitorder) { // set bit order
201  spi_send_msb_first(SPI(BUSVOODOO_SPI_ID)); // MSb-first
202  } else {
203  spi_send_lsb_first(SPI(BUSVOODOO_SPI_ID)); // LSb-first
204  }
205  rcc_periph_clock_enable(RCC_SPI_MOSI_PORT(BUSVOODOO_SPI_ID)); // enable clock for GPIO peripheral for MOSI signal
206  if (busvoodoo_spi_drive) {
207  gpio_set_mode(SPI_MOSI_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, SPI_MOSI_PIN(BUSVOODOO_SPI_ID)); // set MOSI as output
208  } else {
209  gpio_set_mode(SPI_MOSI_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, SPI_MOSI_PIN(BUSVOODOO_SPI_ID)); // set MOSI as output
210  }
211  if (busvoodoo_spi_duplex) {
212  spi_set_full_duplex_mode(SPI(BUSVOODOO_SPI_ID)); // set full duplex mode
213  rcc_periph_clock_enable(RCC_SPI_MISO_PORT(BUSVOODOO_SPI_ID)); // enable clock for GPIO peripheral for MISO signal
214  if (busvoodoo_spi_drive) {
215  gpio_set_mode(SPI_MISO_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, SPI_MISO_PIN(BUSVOODOO_SPI_ID)); // set MISO as input
216  } else {
217  gpio_set_mode(SPI_MISO_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, SPI_MISO_PIN(BUSVOODOO_SPI_ID)); // set MISO as input
218  }
219  } else {
220  spi_set_bidirectional_mode(SPI(BUSVOODOO_SPI_ID)); // set bidirectional mode
221  }
222  rcc_periph_clock_enable(RCC_SPI_SCK_PORT(BUSVOODOO_SPI_ID)); // enable clock for GPIO peripheral for SCK signal
223  if (busvoodoo_spi_drive) {
224  gpio_set_mode(SPI_SCK_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, SPI_SCK_PIN(BUSVOODOO_SPI_ID)); // set SCK as output
225  } else {
226  gpio_set_mode(SPI_SCK_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, SPI_SCK_PIN(BUSVOODOO_SPI_ID)); // set SCK as output
227  }
228  spi_enable_software_slave_management(SPI(BUSVOODOO_SPI_ID)); // control SS by software
229  spi_set_nss_high(SPI(BUSVOODOO_SPI_ID)); // set NSS high (internally) so we can output
230  rcc_periph_clock_enable(RCC_SPI_NSS_PORT(BUSVOODOO_SPI_ID)); // enable clock for GPIO peripheral for SS signal
231  gpio_set(SPI_NSS_PORT(BUSVOODOO_SPI_ID), SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // de-select slave (on high)
232  if (busvoodoo_spi_drive) {
233  gpio_set_mode(SPI_NSS_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // set NSS as output
234  } else {
235  gpio_set_mode(SPI_NSS_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // set NSS as output
236  }
237  spi_set_master_mode(SPI(BUSVOODOO_SPI_ID)); // set master mode
238  spi_enable(SPI(BUSVOODOO_SPI_ID)); // enable SPI
240  busvoodoo_embedded_pullup(true); // set embedded pull-ups
241  printf("use LV to set pull-up voltage\n");
242  }
243  led_off(); // disable LED because there is no activity
244  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_NONE; // restart settings next time
245  *prefix = "SPI"; // display mode
246  busvoodoo_oled_text_left(*prefix); // set mode title on OLED display
247  const char* pinout_io[10] = {"GND", "5V", "3V3", "LV", NULL, NULL, "MISO", "SCK", "MOSI", "SS"}; // SPI mode pinout
248  if (!busvoodoo_spi_duplex) {
249  pinout_io[6] = NULL; // MISO is not used
250  pinout_io[8] = "MOSIMISO"; // MOSI is also used as MISO
251  }
252  for (uint8_t i=0; i<LENGTH(pinout_io) && i<LENGTH(busvoodoo_global_pinout_io); i++) {
253  busvoodoo_global_pinout_io[i] = pinout_io[i]; // set pin names
254  }
255  busvoodoo_oled_text_pinout(pinout_io, true); // set pinout on display
256  busvoodoo_oled_update(); // update display to show text and pinout
257  complete = true; // configuration is complete
258  }
259  break;
260  default: // unknown case
261  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_NONE; // restart settings next time
262  break;
263  }
264  return complete;
265 }
266 
270 static void busvoodoo_spi_write(uint16_t value)
271 {
272  if (8==busvoodoo_spi_databits) {
273  printf("write: 0x%02x\n", (uint8_t)value);
274  } else {
275  printf("write: 0x%04x\n", value);
276  }
277  busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse red LED to show we are writing
278  while (!(SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_TXE)); // wait until Tx is empty
279  (void)SPI_DR(SPI(BUSVOODOO_SPI_ID)); // clear RXNE flag by reading previously received data
280  spi_send(SPI(BUSVOODOO_SPI_ID), value); // send data
281  if (busvoodoo_spi_duplex) {
282  busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we read data
283  value = spi_read(SPI(BUSVOODOO_SPI_ID)); // read data
284  if (8==busvoodoo_spi_databits) {
285  printf("read: 0x%02x\n", (uint8_t)value);
286  } else {
287  printf("read: 0x%04x\n", value);
288  }
289  }
290 }
291 
294 static void busvoodoo_spi_read(void)
295 {
296  if (busvoodoo_spi_duplex) { // in full duplex mode
297  busvoodoo_spi_write(0xffff); // send any value in order to read
298  } else { // unidirectional mode
299  busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we are reading
300  gpio_set_mode(SPI_MOSI_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, SPI_MOSI_PIN(BUSVOODOO_SPI_ID)); // set MOSI as input
301  while (!(SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_TXE)); // wait until Tx is empty
302  (void)SPI_DR(SPI(BUSVOODOO_SPI_ID)); // clear RXNE flag by reading previously received data
303  spi_send(SPI(BUSVOODOO_SPI_ID), 0xffff); // send any value
304  uint16_t value = spi_read(SPI(BUSVOODOO_SPI_ID)); // read data back
305  if (8==busvoodoo_spi_databits) {
306  printf("read: 0x%02x\n", (uint8_t)value);
307  } else {
308  printf("read: 0x%04x\n", value);
309  }
310  if (busvoodoo_spi_drive) {
311  gpio_set_mode(SPI_MOSI_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, SPI_MOSI_PIN(BUSVOODOO_SPI_ID)); // set MOSI as output
312  } else {
313  gpio_set_mode(SPI_MOSI_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, SPI_MOSI_PIN(BUSVOODOO_SPI_ID)); // set MOSI as output
314  }
315  }
316 }
317 
320 static void busvoodoo_spi_exit(void)
321 {
322  spi_reset(SPI(BUSVOODOO_SPI_ID)); // clear SPI values to default
323  spi_disable(SPI(BUSVOODOO_SPI_ID)); // disable SPI
324  rcc_periph_clock_disable(RCC_SPI(BUSVOODOO_SPI_ID)); // disable domain clock
325  gpio_set_mode(SPI_SCK_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, SPI_SCK_PIN(BUSVOODOO_SPI_ID)); // set pin back to floating input
326  gpio_set_mode(SPI_NSS_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // set pin back to floating input
327  gpio_set_mode(SPI_MISO_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, SPI_MISO_PIN(BUSVOODOO_SPI_ID)); // set pin back to floating input
328  gpio_set_mode(SPI_MOSI_PORT(BUSVOODOO_SPI_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, SPI_MOSI_PIN(BUSVOODOO_SPI_ID)); // set pin back to floating input
329  busvoodoo_embedded_pullup(false); // disable embedded pull-ups
330 }
331 
338 static bool busvoodoo_spi_action(const char* action, uint32_t repetition, bool perform)
339 {
340  uint32_t length = strlen(action); // remember length since it will be used a number of times
341  if (NULL==action || 0==length) { // there is nothing to do
342  return true;
343  }
344 
345  if (1==length && 'r'==action[0]) { // read data
346  if (!perform) {
347  return true;
348  }
349  for (uint32_t i=0; i<repetition; i++) {
350  busvoodoo_spi_read(); // read from SPI
351  }
352  } else if (1==length && '['==action[0]) { // select slave
353  if (!perform) {
354  return true;
355  }
356  printf("select slave\n");
357  while (SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_BSY); // wait until not busy
358  gpio_clear(SPI_NSS_PORT(BUSVOODOO_SPI_ID), SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // select slave (on low)
359  } else if (1==length && ']'==action[0]) { // deselect slave
360  if (!perform) {
361  return true;
362  }
363  printf("de-select slave\n");
364  while (SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_BSY); // wait until not busy
365  gpio_set(SPI_NSS_PORT(BUSVOODOO_SPI_ID), SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // de-select slave (on high)
366  } else if (1==length && 'u'==action[0]) { // sleep us
367  if (!perform) {
368  return true;
369  }
370  printf("wait for %u us\n", repetition);
371  sleep_us(repetition); // sleep
372  } else if (1==length && 'm'==action[0]) { // sleep ms
373  if (!perform) {
374  return true;
375  }
376  printf("wait for %u ms\n", repetition);
377  sleep_ms(repetition); // sleep
378  } else if ('0'==action[0]) { // send digit
379  if (1==length) { // just send 0
380  if (!perform) {
381  return true;
382  }
383  for (uint32_t i=0; i<repetition; i++) {
384  busvoodoo_spi_write(0); // write to SPI
385  }
386  } else if ('x'==action[1] || 'b'==action[1]) { // send hex/binary
387  return busvoodoo_spi_action(action+1, repetition, perform); // just retry without leading 0
388  } else if (action[1]>='0' && action[1]<='9') { // send decimal
389  return busvoodoo_spi_action(action+1, repetition, perform); // just retry without leading 0
390  } else { // malformed action
391  return false;
392  }
393  } else if ('x'==action[0] && length>1) { // send hexadecimal value
394  for (uint32_t i=1; i<length; i++) { // check string
395  if (!((action[i]>='0' && action[i]<='9') || (action[i]>='a' && action[i]<='f') || (action[i]>='A' && action[i]<='F'))) { // check for hexadecimal character
396  return false; // not an hexadecimal string
397  }
398  }
399  if (!perform) {
400  return true;
401  }
402  uint32_t value = strtol(&action[1], NULL, 16); // get hex value
403  for (uint32_t i=0; i<repetition; i++) {
404  busvoodoo_spi_write(value); // write to SPI
405  }
406  } else if ('b'==action[0] && length>1) { // send binary value
407  for (uint32_t i=1; i<length; i++) { // check string
408  if (action[i]<'0' || action[i]>'1') { // check for binary character
409  return false; // not a binary string
410  }
411  }
412  if (!perform) {
413  return true;
414  }
415  uint32_t value = strtol(&action[1], NULL, 2); // get binary value
416  for (uint32_t i=0; i<repetition; i++) {
417  busvoodoo_spi_write(value); // write to SPI
418  }
419  } else if (action[0]>='1' && action[0]<='9') { // send decimal value
420  for (uint32_t i=1; i<length; i++) { // check string
421  if (action[i]<'0' || action[i]>'9') { // check for decimal character
422  return false; // not a decimal string
423  }
424  }
425  if (!perform) {
426  return true;
427  }
428  uint32_t value = strtol(&action[0], NULL, 10); // get decimal value
429  for (uint32_t i=0; i<repetition; i++) {
430  busvoodoo_spi_write(value); // write to SPI
431  }
432  } else if (length>=2 && ('"'==action[0] || '\''==action[0]) && (action[length-1]==action[0])) { // send ASCII character
433  if (!perform) {
434  return true;
435  }
436  for (uint32_t r=0; r<repetition; r++) {
437  for (uint32_t i=1; i<length-1; i++) { // go through string
438  busvoodoo_spi_write(action[i]); // write to SPI
439  }
440  }
441  } else { // malformed action
442  return false;
443  }
444  return true; // all went well
445 }
446 
447 // command handlers
448 
452 static void busvoodoo_spi_command_actions(void* argument)
453 {
454  if (NULL==argument || 0==strlen(argument)) {
455  printf("available actions (separated by space or ,):\n");
456  printf("[/]\tselect/deselect slave\n");
457  printf("0\twrite decimal value\n");
458  printf("0x0\twrite hexadecimal value\n");
459  printf("0b0\twrite binary value\n");
460  printf("\"a\"/'a'\twrite ASCII characters\n");
461  printf("r\tread value\n");
462  printf("u/m\twait 1 us/ms\n");
463  printf(":n\trepeat action n times\n");
464  return;
465  }
466  // copy argument since it will be modified
467  char* copy = calloc(strlen(argument)+1, sizeof(char));
468  if (!copy) {
469  while (true);
470  }
471  strncpy(copy, argument, strlen(argument)+1);
472  // verify and perform actions
473  if (!busvoodoo_global_actions(copy, false, &busvoodoo_spi_action)) { // verify actions
474  printf("malformed action(s)\n");
475  } else { // action are ok
476  busvoodoo_global_actions(argument, true, &busvoodoo_spi_action); // perform action
477  }
478  free(copy); // release memory
479 }
480 
482 static const struct menu_command_t busvoodoo_spi_commands[] = {
483  {
484  'a',
485  "action",
486  "perform protocol actions",
488  "[actions]",
490  },
491 };
492 
494  "spi",
495  "Serial Peripheral Interface",
500 };
#define BUSVOODOO_SPI_ID
SPI peripheral.
Definition: busvoodoo_spi.c:42
command menu entry
Definition: menu.h:31
#define SPI_MOSI_PORT(x)
get SPI port for MOSI signal based on SPI identifier
Definition: global.h:297
#define BUSVOODOO_LED_PULSE
recommended duration in ms for pulsing LEDs to show activity
BusVoodoo global definitions and methods (API)
static bool busvoodoo_spi_setup(char **prefix, const char *line)
setup SPI mode
Definition: busvoodoo_spi.c:76
static void busvoodoo_spi_command_actions(void *argument)
command to perform actions
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
static bool busvoodoo_spi_drive
pin drive mode (true = push-pull, false = open-drain)
Definition: busvoodoo_spi.c:67
static enum busvoodoo_spi_setting_t busvoodoo_spi_setting
current mode setup stage
#define SPI_NSS_PORT(x)
get SPI port for NSS signal based on SPI identifier
Definition: global.h:291
void led_off(void)
switch off board LED
Definition: global.c:82
void busvoodoo_oled_update(void)
update OLED display RAM with current display buffer
#define SPI_MOSI_PIN(x)
get SPI pin for MOSI signal based on SPI identifier
Definition: global.h:305
static bool busvoodoo_spi_action(const char *action, uint32_t repetition, bool perform)
perform SPI action
global definitions and methods (API)
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
#define RCC_SPI_MISO_PORT(x)
get RCC for GPIO port for SPI MISO signals
Definition: global.h:281
static bool busvoodoo_spi_pullup
if embedded pull-up resistors are used
Definition: busvoodoo_spi.c:69
#define RCC_SPI_MOSI_PORT(x)
get RCC for GPIO port for SPI MOSI signals
Definition: global.h:286
static uint8_t busvoodoo_spi_standard_mode
SPI mode (defining clock polarity and phase)
Definition: busvoodoo_spi.c:65
#define RCC_SPI(x)
get RCC for SPI based on SPI identifier
Definition: global.h:269
#define SPI_NSS_PIN(x)
get SPI pin for NSS signal based on SPI identifier
Definition: global.h:299
static uint8_t busvoodoo_spi_baudrate
SPI baud rate (corresponding to baud rate control, e.g.
Definition: busvoodoo_spi.c:59
static void busvoodoo_spi_exit(void)
exit SPI mode
#define SPI_SCK_PORT(x)
get SPI port for SCK signal based on SPI identifier
Definition: global.h:293
BusVoodoo SPI mode (API)
struct busvoodoo_mode_t busvoodoo_spi_mode
SPI mode interface definition.
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
char busvoodoo_global_string[64]
shared string buffer, i.e.
bool busvoodoo_global_actions(char *actions, bool perform, bool(*action_handler)(const char *action, uint32_t repetition, bool perform))
parse and perform actions
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:159
#define SPI_MISO_PORT(x)
get SPI port for MISO signal based on SPI identifier
Definition: global.h:295
static void busvoodoo_spi_write(uint16_t value)
write to SPI
#define SPI(x)
get SPI based on SPI identifier
Definition: global.h:267
#define RCC_SPI_SCK_PORT(x)
get RCC for GPIO port for SPI SCK signals
Definition: global.h:276
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
#define LENGTH(x)
get the length of an array
Definition: global.h:26
static const struct menu_command_t busvoodoo_spi_commands[]
SPI menu commands.
static uint8_t busvoodoo_spi_databits
SPI data frame bit width (8 or 16)
Definition: busvoodoo_spi.c:61
static bool busvoodoo_spi_duplex
SPI duplex mode (true = full-duplex, false = bidirectional)
Definition: busvoodoo_spi.c:57
#define SPI_MISO_PIN(x)
get SPI pin for MISO signal based on SPI identifier
Definition: global.h:303
library to show BusVoodoo mode information on SSD1306 OLED display: name, activity, pinout (API)
static void busvoodoo_spi_read(void)
read from SPI
static bool busvoodoo_spi_bitorder
SPI data frame bit order (true = MSb first, false = LSb first)
Definition: busvoodoo_spi.c:63
#define RCC_SPI_NSS_PORT(x)
get RCC for GPIO port for SPI NSS signals
Definition: global.h:271
BusVoodoo mode interface.
#define SPI_SCK_PIN(x)
get SPI pin for SCK signal based on SPI identifier
Definition: global.h:301