BV SPI: add initial support
This commit is contained in:
parent
bb014ce1e0
commit
efe89f61d4
|
@ -0,0 +1,310 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** BusVoodoo SPI mode (code)
|
||||
* @file busvoodoo_spi.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2018
|
||||
* @note peripherals used: SPI @ref busvoodoo_spi
|
||||
*/
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdlib.h> // standard utilities
|
||||
#include <string.h> // string utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
||||
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
||||
#include <libopencm3/stm32/spi.h> // SPI library
|
||||
|
||||
/* own libraries */
|
||||
#include "global.h" // board definitions
|
||||
#include "print.h" // printing utilities
|
||||
#include "menu.h" // menu definitions
|
||||
#include "busvoodoo_global.h" // BusVoodoo definitions
|
||||
#include "busvoodoo_oled.h" // OLED utilities
|
||||
#include "busvoodoo_spi.h" // own definitions
|
||||
|
||||
/** @defgroup busvoodoo_spi SPI peripheral used for SPI communication
|
||||
* @{
|
||||
*/
|
||||
#define BUSVOODOO_SPI_ID 2 /**< SPI peripheral */
|
||||
/** @} */
|
||||
|
||||
/** mode setup stage */
|
||||
static enum busvoodoo_spi_setting_t {
|
||||
BUSVOODOO_SPI_SETTING_NONE,
|
||||
BUSVOODOO_SPI_SETTING_DUPLEX,
|
||||
BUSVOODOO_SPI_SETTING_FREQUENCY,
|
||||
BUSVOODOO_SPI_SETTING_DATABITS,
|
||||
BUSVOODOO_SPI_SETTING_BITORDER,
|
||||
BUSVOODOO_SPI_SETTING_MODE,
|
||||
BUSVOODOO_SPI_SETTING_DRIVE,
|
||||
BUSVOODOO_SPI_SETTING_DONE,
|
||||
} busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_NONE;
|
||||
/** SPI duplex mode (true = full-duplex, false = bidirectional) */
|
||||
static bool busvoodoo_spi_duplex = true;
|
||||
/** SPI baud rate (corresponding to baud rate control, e.g. 36MHz/(2<<br))) */
|
||||
static uint8_t busvoodoo_spi_baudrate = 1;
|
||||
/** SPI data frame bit width (8 or 16) */
|
||||
static uint8_t busvoodoo_spi_databits = 8;
|
||||
/** SPI data frame bit order (true = MSB, false = LSB) */
|
||||
static bool busvoodoo_spi_bitorder = true;
|
||||
/** SPI mode (defining clock polarity and phase) */
|
||||
static uint8_t busvoodoo_spi_standard_mode = 0;
|
||||
/** pin drive mode (true = push-pull, false = open-drain) */
|
||||
static bool busvoodoo_spi_drive = true;
|
||||
/** if embedded pull-up resistors are used */
|
||||
static bool busvoodoo_spi_pullup = true;
|
||||
|
||||
/** setup SPI mode
|
||||
* @param[out] prefix terminal prompt prefix
|
||||
* @param[in] line terminal prompt line to configure mode
|
||||
* @return if setup is complete
|
||||
*/
|
||||
static bool busvoodoo_spi_setup(char** prefix, const char* line)
|
||||
{
|
||||
bool complete = false; // is the setup complete
|
||||
if (NULL==line) { // first call
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_NONE; // re-start configuration
|
||||
}
|
||||
switch (busvoodoo_spi_setting) {
|
||||
case BUSVOODOO_SPI_SETTING_NONE:
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DUPLEX;
|
||||
printf("1) full-duplex\n");
|
||||
printf("2) bidirectional\n");
|
||||
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "duplex mode (1,2) [%c]", busvoodoo_spi_duplex ? '1' : '2');
|
||||
*prefix = busvoodoo_global_string; // ask for setting
|
||||
break;
|
||||
case BUSVOODOO_SPI_SETTING_DUPLEX:
|
||||
if (NULL==line || 0==strlen(line)) { // use default setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_FREQUENCY; // go to next setting
|
||||
} else if (1==strlen(line)) { // setting provided
|
||||
uint8_t duplex = atoi(line); // parse setting
|
||||
if (1==duplex || 2==duplex) { // check setting
|
||||
busvoodoo_spi_duplex = (1==duplex); // remember setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_FREQUENCY; // go to next setting
|
||||
}
|
||||
}
|
||||
if (BUSVOODOO_SPI_SETTING_FREQUENCY==busvoodoo_spi_setting) { // if next setting
|
||||
// SPI2 used APB1 as PCLK, which has a maximum frequency of 36 MHz
|
||||
for (uint8_t div=0; div<8; div++) {
|
||||
printf("%u) %u kHz\n", div+1, 36000/(2<<div)); // print possible frequencies
|
||||
}
|
||||
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "frequency (1-9) [%u]", busvoodoo_spi_baudrate+1);
|
||||
*prefix = busvoodoo_global_string; // display next setting
|
||||
}
|
||||
break;
|
||||
case BUSVOODOO_SPI_SETTING_FREQUENCY:
|
||||
if (NULL==line || 0==strlen(line)) { // use default setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DATABITS; // go to next setting
|
||||
} else if (1==strlen(line)) { // setting provided
|
||||
uint8_t baudrate = atoi(line); // parse setting
|
||||
if (baudrate>0 && baudrate<=9) { // check setting
|
||||
busvoodoo_spi_baudrate = baudrate-1; // remember setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DATABITS; // go to next setting
|
||||
}
|
||||
}
|
||||
if (BUSVOODOO_SPI_SETTING_DATABITS==busvoodoo_spi_setting) { // if next setting
|
||||
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "data frame width in bits (8,16) [%u]", busvoodoo_spi_databits); // prepare next setting
|
||||
*prefix = busvoodoo_global_string; // display next setting
|
||||
}
|
||||
break;
|
||||
case BUSVOODOO_SPI_SETTING_DATABITS:
|
||||
if (NULL==line || 0==strlen(line)) { // use default setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_BITORDER; // go to next setting
|
||||
} else if (1==strlen(line) || 2==strlen(line)) { // setting provided
|
||||
uint8_t databits = atoi(line); // parse setting
|
||||
if (8==databits || 16==databits) { // check setting
|
||||
busvoodoo_spi_databits = databits; // remember setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_BITORDER; // go to next setting
|
||||
}
|
||||
}
|
||||
if (BUSVOODOO_SPI_SETTING_BITORDER==busvoodoo_spi_setting) { // if next setting
|
||||
printf("1) most significant bit first\n");
|
||||
printf("2) least significant bit first\n");
|
||||
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "data frame bit order (1,2) [%c]", busvoodoo_spi_bitorder ? '1' : '2'); // prepare next setting
|
||||
*prefix = busvoodoo_global_string; // display next setting
|
||||
}
|
||||
break;
|
||||
case BUSVOODOO_SPI_SETTING_BITORDER:
|
||||
if (NULL==line || 0==strlen(line)) { // use default setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_MODE; // go to next setting
|
||||
} else if (1==strlen(line)) { // setting provided
|
||||
uint8_t bitorder = atoi(line); // parse setting
|
||||
if (1==bitorder || 2==bitorder) { // check setting
|
||||
busvoodoo_spi_bitorder = (1==bitorder); // remember setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_MODE; // go to next setting
|
||||
}
|
||||
}
|
||||
if (BUSVOODOO_SPI_SETTING_MODE==busvoodoo_spi_setting) { // if next setting
|
||||
printf("1) mode 0 (clock polarity: idle low, clock phase: sample data on rising edge)\n");
|
||||
printf("2) mode 1 (clock polarity: idle low, clock phase: sample data on falling edge)\n");
|
||||
printf("3) mode 2 (clock polarity: idle high, clock phase: sample data on falling edge)\n");
|
||||
printf("4) mode 3 (clock polarity: idle high, clock phase: sample data on rising edge)\n");
|
||||
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "mode (1,2,3,4) [%u]", busvoodoo_spi_standard_mode+1); // prepare next setting
|
||||
*prefix = busvoodoo_global_string; // display next setting
|
||||
}
|
||||
break;
|
||||
case BUSVOODOO_SPI_SETTING_MODE:
|
||||
if (NULL==line || 0==strlen(line)) { // use default setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DRIVE; // go to next setting
|
||||
} else if (1==strlen(line)) { // setting provided
|
||||
uint8_t mode = atoi(line); // parse setting
|
||||
if (mode>=1 && mode<=4) {
|
||||
busvoodoo_spi_standard_mode = mode-1; // remember setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DRIVE; // go to next setting
|
||||
}
|
||||
}
|
||||
if (BUSVOODOO_SPI_SETTING_DRIVE==busvoodoo_spi_setting) { // if next setting
|
||||
printf("1) push-pull (3.3V)\n");
|
||||
printf("2) open-drain, with embedded pull-up (2kO)\n");
|
||||
printf("2) open-drain, with external pull-up\n");
|
||||
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
|
||||
*prefix = busvoodoo_global_string; // display next setting
|
||||
}
|
||||
break;
|
||||
case BUSVOODOO_SPI_SETTING_DRIVE:
|
||||
if (NULL==line || 0==strlen(line)) { // use default setting
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DONE; // go to next setting
|
||||
} else if (1==strlen(line)) { // setting provided
|
||||
uint8_t drive = atoi(line); // parse setting
|
||||
if (1==drive || 2==drive || 3==drive) { // check setting
|
||||
busvoodoo_spi_drive = (1==drive); // remember setting
|
||||
busvoodoo_spi_pullup = (2==drive); // remember setting
|
||||
if (!busvoodoo_spi_drive && busvoodoo_spi_pullup) {
|
||||
printf("use LV to set voltage\n");
|
||||
}
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_DONE; // go to next setting
|
||||
}
|
||||
}
|
||||
if (BUSVOODOO_SPI_SETTING_DONE==busvoodoo_spi_setting) { // we have all settings, configure UART
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable clock for SPI alternate function
|
||||
rcc_periph_clock_enable(RCC_SPI(BUSVOODOO_SPI_ID)); // enable clock for SPI peripheral
|
||||
spi_reset(SPI(BUSVOODOO_SPI_ID)); // clear SPI values to default
|
||||
spi_set_baudrate_prescaler(SPI(BUSVOODOO_SPI_ID), busvoodoo_spi_baudrate); // set baud rate (i.e. frequency)
|
||||
spi_set_standard_mode(SPI(BUSVOODOO_SPI_ID), busvoodoo_spi_standard_mode); // set SPI mode
|
||||
if (8==busvoodoo_spi_databits) { // set data frame bit width
|
||||
spi_set_dff_8bit(SPI(BUSVOODOO_SPI_ID)); // set to 8 bits
|
||||
} else {
|
||||
spi_set_dff_16bit(SPI(BUSVOODOO_SPI_ID)); // set to 16 bits
|
||||
}
|
||||
if (busvoodoo_spi_bitorder) { // set bit order
|
||||
spi_send_msb_first(SPI(BUSVOODOO_SPI_ID)); // MSb-first
|
||||
} else {
|
||||
spi_send_lsb_first(SPI(BUSVOODOO_SPI_ID)); // LSb-first
|
||||
}
|
||||
rcc_periph_clock_enable(RCC_SPI_MOSI_PORT(BUSVOODOO_SPI_ID)); // enable clock for GPIO peripheral for MOSI signal
|
||||
if (busvoodoo_spi_drive) {
|
||||
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
|
||||
} else {
|
||||
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
|
||||
}
|
||||
if (busvoodoo_spi_duplex) {
|
||||
spi_set_full_duplex_mode(SPI(BUSVOODOO_SPI_ID)); // set full duplex mode
|
||||
rcc_periph_clock_enable(RCC_SPI_MISO_PORT(BUSVOODOO_SPI_ID)); // enable clock for GPIO peripheral for MISO signal
|
||||
if (busvoodoo_spi_drive) {
|
||||
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 output
|
||||
} else {
|
||||
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 output
|
||||
}
|
||||
} else {
|
||||
spi_set_bidirectional_mode(SPI(BUSVOODOO_SPI_ID)); // set bidirectional mode
|
||||
}
|
||||
rcc_periph_clock_enable(RCC_SPI_SCK_PORT(BUSVOODOO_SPI_ID)); // enable clock for GPIO peripheral for SCK signal
|
||||
if (busvoodoo_spi_drive) {
|
||||
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 NSS as output
|
||||
} else {
|
||||
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
|
||||
}
|
||||
spi_enable_software_slave_management(SPI(BUSVOODOO_SPI_ID)); // control SS by software
|
||||
rcc_periph_clock_enable(RCC_SPI_NSS_PORT(BUSVOODOO_SPI_ID)); // enable clock for GPIO peripheral for SS signal
|
||||
gpio_set(SPI_NSS_PORT(BUSVOODOO_SPI_ID), SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // de-select slave (on high)
|
||||
if (busvoodoo_spi_drive) {
|
||||
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
|
||||
} else {
|
||||
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
|
||||
}
|
||||
spi_set_master_mode(SPI(BUSVOODOO_SPI_ID)); // set master mode
|
||||
spi_enable(SPI(BUSVOODOO_SPI_ID)); // enable SPI
|
||||
if (!busvoodoo_spi_drive) { // in open drain mode
|
||||
busvoodoo_embedded_pullup(busvoodoo_spi_pullup); // set embedded pull-ups
|
||||
}
|
||||
led_off(); // disable LED because there is no activity
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_NONE; // restart settings next time
|
||||
*prefix = "SPI"; // display mode
|
||||
busvoodoo_oled_text_left(*prefix); // set mode title on OLED display
|
||||
const char* pinout_io[10] = {"GND", "3V3", "5V", "LV", NULL, NULL, "MISO", "SCK", "MOSI", "SS"}; // SPI mode pinout
|
||||
if (!busvoodoo_spi_duplex) {
|
||||
pinout_io[6] = NULL; // MISO is not used
|
||||
pinout_io[8] = "MOSIMISO"; // MOSI is also used as MISO
|
||||
}
|
||||
for (uint8_t i=0; i<LENGTH(pinout_io) && i<LENGTH(busvoodoo_global_pinout_io); i++) {
|
||||
busvoodoo_global_pinout_io[i] = pinout_io[i]; // set pin names
|
||||
}
|
||||
busvoodoo_oled_text_pinout(pinout_io, true); // set pinout on display
|
||||
busvoodoo_oled_update(); // update display to show text and pinout
|
||||
complete = true; // configuration is complete
|
||||
}
|
||||
break;
|
||||
default: // unknown case
|
||||
busvoodoo_spi_setting = BUSVOODOO_SPI_SETTING_NONE; // restart settings next time
|
||||
break;
|
||||
}
|
||||
return complete;
|
||||
}
|
||||
|
||||
/** exit UART mode
|
||||
*/
|
||||
static void busvoodoo_spi_exit(void)
|
||||
{
|
||||
spi_reset(SPI(BUSVOODOO_SPI_ID)); // clear SPI values to default
|
||||
spi_disable(SPI(BUSVOODOO_SPI_ID)); // disable SPI
|
||||
rcc_periph_clock_disable(RCC_SPI(BUSVOODOO_SPI_ID)); // disable domain clock
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
busvoodoo_embedded_pullup(false); // disable embedded pull-ups
|
||||
}
|
||||
|
||||
// command handlers
|
||||
|
||||
/** command to transmit and receive data
|
||||
* @param[in] argument no argument required
|
||||
*/
|
||||
static void busvoodoo_spi_command_transceive(void* argument)
|
||||
{
|
||||
(void)argument; // we won't use the argument
|
||||
}
|
||||
|
||||
static const struct menu_command_t busvoodoo_spi_commands[] = {
|
||||
{
|
||||
'x',
|
||||
"transceive",
|
||||
"transmit and receive data",
|
||||
MENU_ARGUMENT_NONE,
|
||||
NULL,
|
||||
&busvoodoo_spi_command_transceive,
|
||||
},
|
||||
};
|
||||
|
||||
struct busvoodoo_mode_t busvoodoo_spi_mode = {
|
||||
"spi",
|
||||
"Serial Peripheral Interface",
|
||||
&busvoodoo_spi_setup,
|
||||
busvoodoo_spi_commands,
|
||||
LENGTH(busvoodoo_spi_commands),
|
||||
&busvoodoo_spi_exit,
|
||||
};
|
|
@ -0,0 +1,23 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** BusVoodoo SPI mode (API)
|
||||
* @file busvoodoo_spi.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2018
|
||||
* @note peripherals used: SPI @ref busvoodoo_spi
|
||||
*/
|
||||
|
||||
/** SPI mode interface definition */
|
||||
extern struct busvoodoo_mode_t busvoodoo_spi_mode;
|
Loading…
Reference in New Issue