BV SPI: add action handler
This commit is contained in:
parent
2ec1d22a99
commit
fed8637079
|
@ -59,7 +59,7 @@ static bool busvoodoo_spi_duplex = true;
|
|||
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) */
|
||||
/** SPI data frame bit order (true = MSb first, false = LSb first) */
|
||||
static bool busvoodoo_spi_bitorder = true;
|
||||
/** SPI mode (defining clock polarity and phase) */
|
||||
static uint8_t busvoodoo_spi_standard_mode = 0;
|
||||
|
@ -170,7 +170,7 @@ static bool busvoodoo_spi_setup(char** prefix, const char* line)
|
|||
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");
|
||||
printf("3) 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
|
||||
}
|
||||
|
@ -215,20 +215,21 @@ static bool busvoodoo_spi_setup(char** prefix, const char* line)
|
|||
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
|
||||
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
|
||||
} 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
|
||||
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
|
||||
}
|
||||
} 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
|
||||
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
|
||||
} 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
|
||||
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
|
||||
}
|
||||
spi_enable_software_slave_management(SPI(BUSVOODOO_SPI_ID)); // control SS by software
|
||||
spi_set_nss_high(SPI(BUSVOODOO_SPI_ID)); // set NSS high (internally) so we can output
|
||||
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) {
|
||||
|
@ -265,7 +266,55 @@ static bool busvoodoo_spi_setup(char** prefix, const char* line)
|
|||
return complete;
|
||||
}
|
||||
|
||||
/** exit UART mode
|
||||
/** write to SPI
|
||||
* @param[in] value value to write
|
||||
*/
|
||||
static void busvoodoo_spi_write(uint16_t value)
|
||||
{
|
||||
if (8==busvoodoo_spi_databits) {
|
||||
printf("write: 0x%02x\n", (uint8_t)value);
|
||||
} else {
|
||||
printf("write: 0x%04x\n", value);
|
||||
}
|
||||
while (!(SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_TXE)); // wait until Tx is empty
|
||||
(void)SPI_DR(SPI(BUSVOODOO_SPI_ID)); // clear RXNE flag by reading previously received data
|
||||
spi_send(SPI(BUSVOODOO_SPI_ID), value); // send data
|
||||
if (busvoodoo_spi_duplex) {
|
||||
value = spi_read(SPI(BUSVOODOO_SPI_ID)); // read data
|
||||
if (8==busvoodoo_spi_databits) {
|
||||
printf("read: 0x%02x\n", (uint8_t)value);
|
||||
} else {
|
||||
printf("read: 0x%04x\n", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** read from SPI
|
||||
*/
|
||||
static void busvoodoo_spi_read(void)
|
||||
{
|
||||
if (busvoodoo_spi_duplex) { // in full duplex mode
|
||||
busvoodoo_spi_write(0xffff); // send any value in order to read
|
||||
} else { // unidirectional mode
|
||||
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
|
||||
while (!(SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_TXE)); // wait until Tx is empty
|
||||
(void)SPI_DR(SPI(BUSVOODOO_SPI_ID)); // clear RXNE flag by reading previously received data
|
||||
spi_send(SPI(BUSVOODOO_SPI_ID), 0xffff); // send any value
|
||||
uint16_t value = spi_read(SPI(BUSVOODOO_SPI_ID)); // read data back
|
||||
if (8==busvoodoo_spi_databits) {
|
||||
printf("read: 0x%02x\n", (uint8_t)value);
|
||||
} else {
|
||||
printf("read: 0x%04x\n", value);
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** exit SPI mode
|
||||
*/
|
||||
static void busvoodoo_spi_exit(void)
|
||||
{
|
||||
|
@ -279,6 +328,121 @@ static void busvoodoo_spi_exit(void)
|
|||
busvoodoo_embedded_pullup(false); // disable embedded pull-ups
|
||||
}
|
||||
|
||||
/** perform SPI action
|
||||
* @param[in] action action to perform
|
||||
* @param[in] repetition how many times to perform the action
|
||||
* @param[in] perform the action (true) or just check it (false)
|
||||
* @return true if the action has been performed, false if it is malformed
|
||||
*/
|
||||
static bool busvoodoo_spi_action(const char* action, uint32_t repetition, bool perform)
|
||||
{
|
||||
uint32_t length = strlen(action); // remember length since it will be used a number of times
|
||||
if (NULL==action || 0==length) { // there is nothing to do
|
||||
return true;
|
||||
}
|
||||
|
||||
if (1==length && 'r'==action[0]) { // read data
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
busvoodoo_spi_read(); // write from SPI
|
||||
}
|
||||
} else if (1==length && '['==action[0]) { // select slave
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
printf("select slave\n");
|
||||
while (SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_BSY); // wait until not busy
|
||||
gpio_clear(SPI_NSS_PORT(BUSVOODOO_SPI_ID), SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // select slave (on low)
|
||||
} else if (1==length && ']'==action[0]) { // deselect slave
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
printf("de-select slave\n");
|
||||
while (SPI_SR(SPI(BUSVOODOO_SPI_ID))&SPI_SR_BSY); // wait until not busy
|
||||
gpio_set(SPI_NSS_PORT(BUSVOODOO_SPI_ID), SPI_NSS_PIN(BUSVOODOO_SPI_ID)); // de-select slave (on high)
|
||||
} else if (1==length && 'u'==action[0]) { // sleep us
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
printf("wait for %u us\n", repetition);
|
||||
sleep_us(repetition); // sleep
|
||||
} else if (1==length && 'm'==action[0]) { // sleep ms
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
printf("wait for %u ms\n", repetition);
|
||||
sleep_ms(repetition); // sleep
|
||||
} else if ('0'==action[0]) { // send digit
|
||||
if (1==length) { // just send 0
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
busvoodoo_spi_write(0); // write to SPI
|
||||
}
|
||||
} else if ('x'==action[1] || 'b'==action[1]) { // send hex/binary
|
||||
return busvoodoo_spi_action(action+1, repetition, perform); // just retry without leading 0
|
||||
} else if (action[1]>='0' && action[1]<='9') { // send decimal
|
||||
return busvoodoo_spi_action(action+1, repetition, perform); // just retry without leading 0
|
||||
} else { // malformed action
|
||||
return false;
|
||||
}
|
||||
} else if ('x'==action[0] && length>1) { // send hexadecimal value
|
||||
for (uint32_t i=1; i<length; i++) { // check string
|
||||
if (!((action[i]>='0' && action[i]<='9') || (action[i]>='a' && action[i]<='f') || (action[i]>='A' && action[i]<='F'))) { // check for hexadecimal character
|
||||
return false; // not an hexadecimal string
|
||||
}
|
||||
}
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
uint32_t value = strtol(&action[1], NULL, 16); // get hex value
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
busvoodoo_spi_write(value); // write to SPI
|
||||
}
|
||||
} else if ('b'==action[0] && length>1) { // send binary value
|
||||
for (uint32_t i=1; i<length; i++) { // check string
|
||||
if (action[i]<'0' || action[i]>'1') { // check for binary character
|
||||
return false; // not a binary string
|
||||
}
|
||||
}
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
uint32_t value = strtol(&action[1], NULL, 2); // get binary value
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
busvoodoo_spi_write(value); // write to SPI
|
||||
}
|
||||
} else if (action[0]>='1' && action[0]<='9') { // send decimal value
|
||||
for (uint32_t i=1; i<length; i++) { // check string
|
||||
if (action[i]<'0' || action[i]>'9') { // check for decimal character
|
||||
return false; // not a decimal string
|
||||
}
|
||||
}
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
uint32_t value = strtol(&action[0], NULL, 10); // get decimal value
|
||||
for (uint32_t i=0; i<repetition; i++) {
|
||||
busvoodoo_spi_write(value); // write to SPI
|
||||
}
|
||||
} else if (length>=2 && ('"'==action[0] || '\''==action[0]) && (action[length-1]==action[0])) { // send ASCII character
|
||||
if (!perform) {
|
||||
return true;
|
||||
}
|
||||
for (uint32_t r=0; r<repetition; r++) {
|
||||
for (uint32_t i=1; i<length-1; i++) { // go through string
|
||||
busvoodoo_spi_write(action[i]); // write to SPI
|
||||
}
|
||||
}
|
||||
} else { // malformed action
|
||||
return false;
|
||||
}
|
||||
return true; // all went well
|
||||
}
|
||||
|
||||
// command handlers
|
||||
|
||||
/** command to transmit and receive data
|
||||
|
@ -286,16 +450,40 @@ static void busvoodoo_spi_exit(void)
|
|||
*/
|
||||
static void busvoodoo_spi_command_transceive(void* argument)
|
||||
{
|
||||
(void)argument; // we won't use the argument
|
||||
if (NULL==argument || 0==strlen(argument)) {
|
||||
printf("available actions (separated by space or ,):\n");
|
||||
printf("[/]\tselect/deselect slave\n");
|
||||
printf("0\twrite decimal value\n");
|
||||
printf("0x0\twrite hexadecimal value\n");
|
||||
printf("0b0\twrite binary value\n");
|
||||
printf("\"a\"/'a'\twrite ASCII characters\n");
|
||||
printf("r\tread value\n");
|
||||
printf("u/m\twait 1 us/ms\n");
|
||||
printf(":n\trepeat action n times\n");
|
||||
return;
|
||||
}
|
||||
// copy argument since it will be modified
|
||||
char* copy = calloc(strlen(argument)+1, sizeof(char));
|
||||
if (!copy) {
|
||||
while (true);
|
||||
}
|
||||
strncpy(copy, argument, strlen(argument)+1);
|
||||
// verify and perform actions
|
||||
if (!busvoodoo_global_actions(copy, false, &busvoodoo_spi_action)) { // verify actions
|
||||
printf("malformed action(s)\n");
|
||||
} else { // action are ok
|
||||
busvoodoo_global_actions(argument, true, &busvoodoo_spi_action); // perform action
|
||||
}
|
||||
free(copy); // release memory
|
||||
}
|
||||
|
||||
static const struct menu_command_t busvoodoo_spi_commands[] = {
|
||||
{
|
||||
'x',
|
||||
"transceive",
|
||||
"transmit and receive data",
|
||||
MENU_ARGUMENT_NONE,
|
||||
NULL,
|
||||
'a',
|
||||
"action",
|
||||
"run protocol action",
|
||||
MENU_ARGUMENT_STRING,
|
||||
"[actions]",
|
||||
&busvoodoo_spi_command_transceive,
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue