cherry-pick from busvoodoo branch, part 3

This commit is contained in:
King Kévin 2018-02-18 15:20:47 +01:00
parent e14f6bccd6
commit 8ba50b2d65
11 changed files with 230 additions and 90 deletions

View File

@ -37,7 +37,7 @@ void main(void)
// check of DFU mode is forced
bool dfu_force = false; // to remember if DFU mode is forced
// check if a soft boot has been used
if (0==(RCC_CSR&0xfc000000)) { // no reset flag present -> this was a soft reset using csr_reset_core(), very probably to start the DFU mode
if (0==(RCC_CSR&0xfc000000)) { // no reset flag present -> this was a soft reset using scb_reset_core() after clearing the flags using RCC_CSR_RMVF, very probably to start the DFU mode
dfu_force = true;
} else { // check if the force DFU mode input is set
// disable SWJ pin to use as GPIO

View File

@ -35,6 +35,11 @@
volatile bool button_flag = false;
volatile uint32_t sleep_duration = 0; /**< sleep duration count down (in SysTick interrupts) */
volatile bool user_input_available = false;
static volatile uint8_t user_input_buffer[64] = {0}; /**< ring buffer for received data */
static volatile uint8_t user_input_i = 0; /**< current position of read received data */
static volatile uint8_t user_input_used = 0; /**< how much data has been received and not red */
char* b2s(uint64_t binary, uint8_t rjust)
{
@ -122,6 +127,30 @@ void sys_tick_handler(void)
}
}
char user_input_get(void)
{
while (!user_input_available) { // wait for user input
__WFI(); // go to sleep
}
volatile char to_return = user_input_buffer[user_input_i]; // get the next available character
user_input_i = (user_input_i+1)%LENGTH(user_input_buffer); // update used buffer
user_input_used--; // update used buffer
user_input_available = (user_input_used!=0); // update available data
return to_return;
}
void user_input_store(char c)
{
// only save data if there is space in the buffer
if (user_input_used>=LENGTH(user_input_buffer)) { // if buffer is full
user_input_i = (user_input_i+1)%LENGTH(user_input_buffer); // drop oldest data
user_input_used--; // update used buffer information
}
user_input_buffer[(user_input_i+user_input_used)%LENGTH(user_input_buffer)] = c; // put character in buffer
user_input_used++; // update used buffer
user_input_available = true; // update available data
}
void board_setup(void)
{
// setup LED
@ -145,6 +174,11 @@ void board_setup(void)
exti_enable_request(EXTI(BUTTON_PIN)); // enable external interrupt
nvic_enable_irq(NVIC_EXTI_IRQ(BUTTON_PIN)); // enable interrupt
#endif
// reset user input buffer
user_input_available = false;
user_input_i = 0;
user_input_used = 0;
}
#if defined(BUTTON_PIN)

View File

@ -368,8 +368,10 @@ extern uint32_t __application_beginning;
* @note this symbol will be provided by the linker script
*/
extern uint32_t __application_end;
extern volatile bool button_flag; /**< flag set when board user button has been pressed/released */
/** flag set when board user button has been pressed/released */
extern volatile bool button_flag;
/** flag set when user input is available */
extern volatile bool user_input_available;
/** get binary representation of a number
* @param[in] binary number to represent in binary
@ -377,13 +379,10 @@ extern volatile bool button_flag; /**< flag set when board user button has been
* @return string with binary representation of the number
*/
char* b2s(uint64_t binary, uint8_t rjust);
/** switch on board LED */
void led_on(void);
/** switch off board LED */
void led_off(void);
/** toggle board LED */
void led_toggle(void);
@ -391,12 +390,21 @@ void led_toggle(void);
* @param[in] duration sleep duration in us
*/
void sleep_us(uint32_t duration);
/** go to sleep for some milliseconds
* @param[in] duration sleep duration in ms
*/
void sleep_ms(uint32_t duration);
/** get user input
* @note verify availability using user_input_available
* @warning blocks and sleeps until user input is available
* @return user input character
*/
char user_input_get(void);
/** store user input
* @param[in] c user input character to store
* @note old data will be overwritten when buffer is full
*/
void user_input_store(char c);
/** setup board peripherals */
void board_setup(void);

118
lib/menu.c Normal file
View File

@ -0,0 +1,118 @@
/* 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/>.
*
*/
/** definitions to build menus (code)
* @file menu.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdbool.h> // boolean types
#include <stdlib.h> // standard utilities
#include <string.h> // string utilities
/* own libraries */
#include "menu.h" // definitions for the menu
#include "print.h" // print utilities
bool menu_handle_command(const char* line, const struct menu_command_t* command_list, size_t command_list_length)
{
// ensure line is not empty
if (!line || 0==strlen(line)) {
return false;
}
// ensure command are available
if (NULL==command_list || 0==command_list_length) {
return false;
}
// get command
char* dup = malloc(strlen(line)+1); // buffer to copy the line
strncpy(dup, line, strlen(line)+1); // make a copy of the line since strtok can modify it
const char* delimiter = " "; // words are separated by spaces
char* word = strtok(dup, delimiter); // get first word
if (!word) {
return false;
}
// find corresponding command
bool command_found = false; // remember if we found the corresponding command
for (size_t i=0; i<command_list_length; i++) { // go through available command list
struct menu_command_t command = command_list[i]; // get current command
if ((1==strlen(word) && command.shortcut==word[0]) || 0==strcmp(word, command.name)) { // check if shortcut or name match
command_found = true; // remember we found the command
if (command.command_handler) { // ensure there is an command handler
if (MENU_ARGUMENT_NONE==command.argument) { // check if argument can be passed
(*command.command_handler)(NULL); // call without argument
} else { // argument can be passed
word = strtok(NULL, delimiter); // get next word
if (!word) { // no argument provided
(*command.command_handler)(NULL); // call without argument
} else if (MENU_ARGUMENT_SIGNED==command.argument) { // next argument should be a signed integer
int32_t argument = atoi(word); // get signed integer
(*command.command_handler)(&argument); // call with argument
} else if (MENU_ARGUMENT_UNSIGNED==command.argument) { // next argument should be an unsigned integer
uint32_t argument = atoi(word); // get unsigned integer
(*command.command_handler)(&argument); // call with argument
} else if (MENU_ARGUMENT_FLOAT==command.argument) { // next argument should be a floating point number
double argument = atof(word); // get floating point number
(*command.command_handler)(&argument); // call with argument
} else if (MENU_ARGUMENT_STRING==command.argument) { // next argument should be a string
(*command.command_handler)(word); // call with argument
}
}
}
break; // stop searching for the command
}
}
// find default command
if (!command_found) { // we didn't find the corresponding command
for (size_t i=0; i<command_list_length; i++) { // go through available command list
struct menu_command_t command = command_list[i]; // get current command
if (0==command.shortcut && NULL==command.name) { // check if there is a default command
command_found = true; // remember we found the command
if (command.command_handler) { // ensure there is an command handler
(*command.command_handler)(word); // call with current word
}
break; // stop searching for the command
}
}
}
free(dup); // free line copy
return command_found;
}
void menu_print_commands(const struct menu_command_t* command_list, size_t command_list_length)
{
for (size_t i=0; i<command_list_length; i++) {
struct menu_command_t command = command_list[i];
if (command.name) {
if (command.shortcut) {
printf("%c|", command.shortcut);
}
printf("%s", command.name);
if (MENU_ARGUMENT_NONE!=command.argument && command.argument_description) {
printf(" %s", command.argument_description);
}
if (command.command_description) {
printf("\t%s", command.command_description);
}
printf("\n");
}
}
}

52
lib/menu.h Normal file
View File

@ -0,0 +1,52 @@
/* 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/>.
*
*/
/** definitions to build menus (API)
* @file menu.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
*/
/** types of argument accepted by the command */
enum menu_argument_t {
MENU_ARGUMENT_NONE, /**< no argument accepted */
MENU_ARGUMENT_SIGNED, /**< int32 argument accepted */
MENU_ARGUMENT_UNSIGNED, /**< uint32 argument accepted */
MENU_ARGUMENT_FLOAT, /**< double argument accepted */
MENU_ARGUMENT_STRING, /**< string argument accepted */
};
/** command menu entry */
struct menu_command_t {
char shortcut; /**< short command code (0 if not available) */
char* name; /**< complete name of the command (space-free) */
char* command_description; /**< human readable description of the command purpose */
enum menu_argument_t argument; /**< what kind of argument it accepts */
char* argument_description; /**< human readable description of the argument it can accept */
void (*command_handler)(void* argument); /**< function to be called to handle this command */
};
/** parse command from line and call corresponding command
* @param[in] line use input to parse
* @param[in] command_list list of available commands
* @prarm[in] command_list_length number of available commands
* @return if an command corresponding to the line has been found and called
*/
bool menu_handle_command(const char* line, const struct menu_command_t* command_list, size_t command_list_length);
/** print the commands from the command list
* @param[in] command_list list of available commands
* @prarm[in] command_list_length number of available commands
*/
void menu_print_commands(const struct menu_command_t* command_list, size_t command_list_length);

View File

@ -167,7 +167,7 @@ static size_t print_float(char** str, size_t* size, double f, uint32_t padding,
exponent -= padding;
}
} else {
while (f_abs>pow(10.0, exponent)) { // find the positive exponent, base 10
while (f_abs>=pow(10.0, exponent)) { // find the positive exponent, base 10
exponent += 3; // increment in kilo
}
if (padding) { // respect padding wish

View File

@ -295,7 +295,7 @@ static void terminal_process_escape(void)
/** print current line and set position */
static void terminal_print_line(void)
{
printf("\r\x1b[K%s%s", terminal_prefix ? terminal_prefix : "", &terminal_buffer[terminal_line]); // erase line and display last one
printf("\r\x1b[K%s%s%s", terminal_prefix ? terminal_prefix : "", ": ", &terminal_buffer[terminal_line]); // erase line and display last one
if (terminal_pos<terminal_line+strlen(&terminal_buffer[terminal_line])) { // cursor is not at the end of the line
printf("\x1b[%uD", terminal_line+strlen(&terminal_buffer[terminal_line])-terminal_pos); // set position by going back
}

View File

@ -42,16 +42,11 @@
#define UART_BAUDRATE 921600 /**< serial baudrate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) */
/* input and output ring buffer, indexes, and available memory */
static volatile uint8_t rx_buffer[UART_BUFFER] = {0}; /**< ring buffer for received data */
static volatile uint8_t rx_i = 0; /**< current position of read received data */
static volatile uint8_t rx_used = 0; /**< how much data has been received and not red */
static volatile uint8_t tx_buffer[UART_BUFFER] = {0}; /**< ring buffer for data to transmit */
/* output ring buffer, indexes, and available memory */
static volatile uint8_t tx_buffer[64] = {0}; /**< ring buffer for data to transmit */
static volatile uint8_t tx_i = 0; /**< current position of transmitted data */
static volatile uint8_t tx_used = 0; /**< how much data needs to be transmitted */
volatile bool uart_received = false;
void uart_setup(void)
{
/* enable UART I/O peripheral */
@ -77,9 +72,6 @@ void uart_setup(void)
/* reset buffer states */
tx_i = 0;
tx_used = 0;
rx_i = 0;
rx_used = 0;
uart_received = false;
}
void uart_putchar_blocking(char c)
@ -96,20 +88,6 @@ void uart_flush(void)
usart_wait_send_ready(USART(UART_ID)); // wait until transmit register is empty (transmission might not be complete)
}
char uart_getchar(void)
{
while (!rx_used) { // idle until data is available
__WFI(); // sleep until interrupt
}
usart_disable_rx_interrupt(USART(UART_ID)); // disable receive interrupt to prevent index corruption
volatile char to_return = rx_buffer[rx_i]; // get the next available character
rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
rx_used--; // update used buffer
uart_received = (rx_used!=0); // update available data
usart_enable_rx_interrupt(USART(UART_ID)); // enable receive interrupt
return to_return;
}
void uart_putchar_nonblocking(char c)
{
while (tx_used>=LENGTH(tx_buffer)) { // idle until buffer has some space
@ -130,19 +108,12 @@ void USART_ISR(UART_ID)(void)
usart_disable_tx_interrupt(USART(UART_ID)); // disable transmit interrupt
} else {
usart_send(USART(UART_ID),tx_buffer[tx_i]); // put data in transmit register
tx_i = (tx_i+1)%LENGTH(rx_buffer); // update location on buffer
tx_i = (tx_i+1)%LENGTH(tx_buffer); // update location on buffer
tx_used--; // update used size
}
}
while (usart_get_flag(USART(UART_ID), USART_SR_RXNE)) { // data has been received (repeat while receiving)
char c = usart_recv(USART(UART_ID)); // save character and free UART buffer
// only save data if there is space in the buffer
if (rx_used>=LENGTH(rx_buffer)) { // if buffer is full
rx_i = (rx_i+1)%LENGTH(rx_buffer); // drop oldest data
rx_used--; // update used buffer information
}
rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = c; // put character in buffer
rx_used++; // update used buffer
uart_received = true; // update available data
user_input_store(c); // store data
}
}

View File

@ -20,11 +20,6 @@
*/
#pragma once
/** transmit and receive buffer sizes */
#define UART_BUFFER 128
/** how many bytes available in the received buffer since last read */
extern volatile bool uart_received;
/** setup UART peripheral */
void uart_setup(void);
/** send character over UART (blocking)
@ -35,11 +30,6 @@ void uart_putchar_blocking(char c);
* @note block until all data has been transmitted
*/
void uart_flush(void);
/** get character received over UART (blocking)
* @return character received over UART
* @note blocks until character is received over UART when received buffer is empty
*/
char uart_getchar(void);
/** send character over UART (non-blocking)
* @param[in] c character to send
* @note blocks if transmit buffer is full, else puts in buffer and returns

View File

@ -231,16 +231,11 @@ static const char *usb_strings[] = {
"CuVoodoo DFU bootloader (runtime mode)",
};
/* input and output ring buffer, indexes, and available memory */
static uint8_t rx_buffer[CDCACM_BUFFER] = {0}; /**< ring buffer for received data */
static volatile uint8_t rx_i = 0; /**< current position of read received data */
static volatile uint8_t rx_used = 0; /**< how much data has been received and not red */
mutex_t rx_lock = MUTEX_UNLOCKED; /**< lock to update rx_i or rx_used */
static uint8_t tx_buffer[CDCACM_BUFFER] = {0}; /**< ring buffer for data to transmit */
/* output ring buffer, index, and available memory */
static uint8_t tx_buffer[64] = {0}; /**< ring buffer for data to transmit */
static volatile uint8_t tx_i = 0; /**< current position if transmitted data */
static volatile uint8_t tx_used = 0; /**< how much data needs to be transmitted */
mutex_t tx_lock = MUTEX_UNLOCKED; /**< lock to update tx_i or tx_used */
volatile uint8_t usb_cdcacm_received = 0; // same as rx_used, but since the user can write this variable we don't rely on it
static bool connected = false; /**< is the USB device is connected to a host */
/** disconnect USB by pulling down D+ to for re-enumerate */
@ -382,11 +377,9 @@ static void usb_cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
/* receive data */
usb_length = usbd_ep_read_packet(usbd_dev, 0x02, usb_data, sizeof(usb_data));
if (usb_length) { // copy received data
for (uint16_t i=0; i<usb_length && rx_used<LENGTH(rx_buffer); i++) { // only until buffer is full
rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usb_data[i]; // put character in buffer
rx_used++; // update used buffer
for (uint16_t i=0; i<usb_length; i++) { // only until buffer is full
user_input_store(usb_data[i]); // store user data
}
usb_cdcacm_received = rx_used; // update available data
}
}
@ -444,27 +437,11 @@ void usb_cdcacm_setup(void)
nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); // enable interrupts (to not have to poll all the time)
// reset buffer states
rx_i = 0;
rx_used = 0;
mutex_unlock(&rx_lock);
usb_cdcacm_received = 0;
tx_i = 0;
tx_used = 0;
mutex_unlock(&tx_lock);
}
char usb_cdcacm_getchar(void)
{
while (!rx_used) { // idle until data is available
__WFI(); // sleep until interrupt (not sure if it's a good idea here)
}
char to_return = rx_buffer[rx_i]; // get the next available character
rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
rx_used--; // update used buffer
usb_cdcacm_received = rx_used; // update available data
return to_return;
}
void usb_cdcacm_putchar(char c)
{
if (!usb_device || !connected) {

View File

@ -19,18 +19,8 @@
*/
#pragma once
/** transmit and receive buffer sizes */
#define CDCACM_BUFFER 64
/** how many bytes available in the received buffer since last read */
extern volatile uint8_t usb_cdcacm_received;
/** setup USB CDC ACM peripheral */
void usb_cdcacm_setup(void);
/** get character received over USB (blocking)
* @return character received over USB
* @note blocks until character is received over USB when received buffer is empty
*/
char usb_cdcacm_getchar(void);
/** send character over USB (non-blocking)
* @param[in] c character to send
* @note blocks if transmit buffer is full, else puts in buffer and returns