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  busvoodoo_led_blue_off(); // disable blue 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  if (busvoodoo_full) {
256  const char* pinout_rscan[5] = {"HV", NULL, NULL, NULL, NULL}; // HiZ mode RS/CAN pinout
257  for (uint8_t i=0; i<LENGTH(pinout_rscan) && i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
258  busvoodoo_global_pinout_rscan[i] = pinout_rscan[i]; // set pin names
259  }
260  }
261  busvoodoo_oled_text_pinout(pinout_io, true); // set pinout on display
262  busvoodoo_oled_update(); // update display to show text and pinout
263  complete = true; // configuration is complete
264  }
265  break;
266  default: // unknown case
267  busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_NONE; // restart settings next time
268  break;
269  }
270  return complete;
271 }
272 
276 static void busvoodoo_spi_write(uint16_t value)
277 {
278  if (8==busvoodoo_spi_databits) {
279  printf("write: 0x%02x\n", (uint8_t)value);
280  } else {
281  printf("write: 0x%04x\n", value);
282  }
283  busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we are writing
284  while (!(SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_TXE)); // wait until Tx is empty
285  (void)SPI_DR(SPI(BUSVOODOO_SPI_ID)); // clear RXNE flag by reading previously received data
286  spi_send(SPI(BUSVOODOO_SPI_ID), value); // send data
287  if (busvoodoo_spi_duplex) {
288  busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we read data
289  value = spi_read(SPI(BUSVOODOO_SPI_ID)); // read data
290  if (8==busvoodoo_spi_databits) {
291  printf("read: 0x%02x\n", (uint8_t)value);
292  } else {
293  printf("read: 0x%04x\n", value);
294  }
295  }
296 }
297 
300 static void busvoodoo_spi_read(void)
301 {
302  if (busvoodoo_spi_duplex) { // in full duplex mode
303  busvoodoo_spi_write(0xffff); // send any value in order to read
304  } else { // unidirectional mode
305  busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // pulse blue LED to show we are reading
306  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
307  while (!(SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_TXE)); // wait until Tx is empty
308  (void)SPI_DR(SPI(BUSVOODOO_SPI_ID)); // clear RXNE flag by reading previously received data
309  spi_send(SPI(BUSVOODOO_SPI_ID), 0xffff); // send any value
310  uint16_t value = spi_read(SPI(BUSVOODOO_SPI_ID)); // read data back
311  if (8==busvoodoo_spi_databits) {
312  printf("read: 0x%02x\n", (uint8_t)value);
313  } else {
314  printf("read: 0x%04x\n", value);
315  }
316  if (busvoodoo_spi_drive) {
317  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
318  } else {
319  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
320  }
321  }
322 }
323 
326 static void busvoodoo_spi_exit(void)
327 {
328  spi_reset(SPI(BUSVOODOO_SPI_ID)); // clear SPI values to default
329  spi_disable(SPI(BUSVOODOO_SPI_ID)); // disable SPI
330  rcc_periph_clock_disable(RCC_SPI(BUSVOODOO_SPI_ID)); // disable domain clock
331  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
332  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
333  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
334  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
335  busvoodoo_embedded_pullup(false); // disable embedded pull-ups
336 }
337 
344 static bool busvoodoo_spi_action(const char* action, uint32_t repetition, bool perform)
345 {
346  uint32_t length = strlen(action); // remember length since it will be used a number of times
347  if (NULL==action || 0==length) { // there is nothing to do
348  return true;
349  }
350 
351  if (1==length && 'r'==action[0]) { // read data
352  if (!perform) {
353  return true;
354  }
355  for (uint32_t i=0; i<repetition; i++) {
356  busvoodoo_spi_read(); // read from SPI
357  }
358  } else if (1==length && '['==action[0]) { // select slave
359  if (!perform) {
360  return true;
361  }
362  printf("select slave\n");
363  while (SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_BSY); // wait until not busy
364  gpio_clear(SPI_NSS_PORT(BUSVOODOO_SPI_ID), SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // select slave (on low)
365  } else if (1==length && ']'==action[0]) { // deselect slave
366  if (!perform) {
367  return true;
368  }
369  printf("de-select slave\n");
370  while (SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_BSY); // wait until not busy
371  gpio_set(SPI_NSS_PORT(BUSVOODOO_SPI_ID), SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // de-select slave (on high)
372  } else if (1==length && 'u'==action[0]) { // sleep us
373  if (!perform) {
374  return true;
375  }
376  printf("wait for %u us\n", repetition);
377  sleep_us(repetition); // sleep
378  } else if (1==length && 'm'==action[0]) { // sleep ms
379  if (!perform) {
380  return true;
381  }
382  printf("wait for %u ms\n", repetition);
383  sleep_ms(repetition); // sleep
384  } else if ('0'==action[0]) { // send digit
385  if (1==length) { // just send 0
386  if (!perform) {
387  return true;
388  }
389  for (uint32_t i=0; i<repetition; i++) {
390  busvoodoo_spi_write(0); // write to SPI
391  }
392  } else if ('x'==action[1] || 'b'==action[1]) { // send hex/binary
393  return busvoodoo_spi_action(action+1, repetition, perform); // just retry without leading 0
394  } else if (action[1]>='0' && action[1]<='9') { // send decimal
395  return busvoodoo_spi_action(action+1, repetition, perform); // just retry without leading 0
396  } else { // malformed action
397  return false;
398  }
399  } else if ('x'==action[0] && length>1) { // send hexadecimal value
400  for (uint32_t i=1; i<length; i++) { // check string
401  if (!((action[i]>='0' && action[i]<='9') || (action[i]>='a' && action[i]<='f') || (action[i]>='A' && action[i]<='F'))) { // check for hexadecimal character
402  return false; // not an hexadecimal string
403  }
404  }
405  if (!perform) {
406  return true;
407  }
408  uint32_t value = strtol(&action[1], NULL, 16); // get hex value
409  for (uint32_t i=0; i<repetition; i++) {
410  busvoodoo_spi_write(value); // write to SPI
411  }
412  } else if ('b'==action[0] && length>1) { // send binary value
413  for (uint32_t i=1; i<length; i++) { // check string
414  if (action[i]<'0' || action[i]>'1') { // check for binary character
415  return false; // not a binary string
416  }
417  }
418  if (!perform) {
419  return true;
420  }
421  uint32_t value = strtol(&action[1], NULL, 2); // get binary value
422  for (uint32_t i=0; i<repetition; i++) {
423  busvoodoo_spi_write(value); // write to SPI
424  }
425  } else if (action[0]>='1' && action[0]<='9') { // send decimal value
426  for (uint32_t i=1; i<length; i++) { // check string
427  if (action[i]<'0' || action[i]>'9') { // check for decimal character
428  return false; // not a decimal string
429  }
430  }
431  if (!perform) {
432  return true;
433  }
434  uint32_t value = strtol(&action[0], NULL, 10); // get decimal value
435  for (uint32_t i=0; i<repetition; i++) {
436  busvoodoo_spi_write(value); // write to SPI
437  }
438  } else if (length>=2 && ('"'==action[0] || '\''==action[0]) && (action[length-1]==action[0])) { // send ASCII character
439  if (!perform) {
440  return true;
441  }
442  for (uint32_t r=0; r<repetition; r++) {
443  for (uint32_t i=1; i<length-1; i++) { // go through string
444  busvoodoo_spi_write(action[i]); // write to SPI
445  }
446  }
447  } else { // malformed action
448  return false;
449  }
450  return true; // all went well
451 }
452 
453 // command handlers
454 
458 static void busvoodoo_spi_command_actions(void* argument)
459 {
460  if (NULL==argument || 0==strlen(argument)) {
461  printf("available actions (separated by space or ,):\n");
462  printf("[/]\tselect/deselect slave\n");
463  printf("0\twrite decimal value\n");
464  printf("0x0\twrite hexadecimal value\n");
465  printf("0b0\twrite binary value\n");
466  printf("\"a\"/'a'\twrite ASCII characters\n");
467  printf("r\tread value\n");
468  printf("u/m\twait 1 us/ms\n");
469  printf(":n\trepeat action n times\n");
470  return;
471  }
472  // copy argument since it will be modified
473  char* copy = calloc(strlen(argument)+1, sizeof(char));
474  if (!copy) {
475  while (true);
476  }
477  strncpy(copy, argument, strlen(argument)+1);
478  // verify and perform actions
479  if (!busvoodoo_global_actions(copy, false, &busvoodoo_spi_action)) { // verify actions
480  printf("malformed action(s)\n");
481  } else { // action are ok
482  busvoodoo_global_actions(argument, true, &busvoodoo_spi_action); // perform action
483  }
484  free(copy); // release memory
485 }
486 
488 static const struct menu_command_t busvoodoo_spi_commands[] = {
489  {
490  .shortcut = 'a',
491  .name = "action",
492  .command_description = "perform protocol actions",
493  .argument = MENU_ARGUMENT_STRING,
494  .argument_description = "[actions]",
495  .command_handler = &busvoodoo_spi_command_actions,
496  },
497 };
498 
500  .name = "spi",
501  .description = "Serial Peripheral Interface",
502  .full_only = false,
503  .setup = &busvoodoo_spi_setup,
504  .commands = busvoodoo_spi_commands,
505  .commands_nb = LENGTH(busvoodoo_spi_commands),
506  .exit = &busvoodoo_spi_exit,
507 };
#define BUSVOODOO_SPI_ID
SPI peripheral.
Definition: busvoodoo_spi.c:42
command menu entry
Definition: menu.h:32
#define SPI_MOSI_PORT(x)
get SPI port for MOSI signal based on SPI identifier
Definition: global.h:301
#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
const char * name
name of the mode (i.e.
void busvoodoo_led_blue_pulse(uint32_t ms)
pulse blue 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:295
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:309
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
static bool busvoodoo_spi_action(const char *action, uint32_t repetition, bool perform)
perform SPI action
const struct busvoodoo_mode_t busvoodoo_spi_mode
SPI mode interface definition.
global definitions and methods (API)
void sleep_us(uint32_t duration)
go to sleep for some microseconds
Definition: global.c:102
#define RCC_SPI_MISO_PORT(x)
get RCC for GPIO port for SPI MISO signals
Definition: global.h:285
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:290
static uint8_t busvoodoo_spi_standard_mode
SPI mode (defining clock polarity and phase)
Definition: busvoodoo_spi.c:65
bool busvoodoo_full
is the BusVoodoo board fully populated (with HV voltage regulator, RS-232, RS-485, CAN transceiver on the back side)
#define RCC_SPI(x)
get RCC for SPI based on SPI identifier
Definition: global.h:273
void busvoodoo_led_blue_off(void)
switch off blue LED
#define SPI_NSS_PIN(x)
get SPI pin for NSS signal based on SPI identifier
Definition: global.h:303
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:297
BusVoodoo SPI mode (API)
char shortcut
short command code (0 if not available)
Definition: menu.h:33
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:117
#define SPI_MISO_PORT(x)
get SPI port for MISO signal based on SPI identifier
Definition: global.h:299
static void busvoodoo_spi_write(uint16_t value)
write to SPI
#define SPI(x)
get SPI based on SPI identifier
Definition: global.h:271
#define RCC_SPI_SCK_PORT(x)
get RCC for GPIO port for SPI SCK signals
Definition: global.h:280
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:307
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:275
BusVoodoo mode interface.
#define SPI_SCK_PIN(x)
get SPI pin for SCK signal based on SPI identifier
Definition: global.h:305