uMIDI
The swiss army knife for quick and easy developement of MIDI applications.
Functions | Variables
serial_communication.c File Reference

Implementation of the serial communication module. More...

#include <stdlib.h>
#include <string.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include "xboot/xbootapi.h"
#include "background_tasks.h"
#include "usb.h"
#include "serial_communication.h"
#include "system.h"
Include dependency graph for serial_communication.c:

Functions

static bool exec_help (void)
 Handler for the help command. More...
 
static bool exec_update (const char *const command)
 Handler for the update command. More...
 
static void execute_command (const char *const command)
 Executes an interactive command. More...
 
static void print_command_from_history (const int8_t offset)
 Prints out a command from the history. More...
 
static bool handle_escape_sequence (const char data)
 Handles ANSI escape sequences. More...
 
static void process_command_char (void)
 Processes a command character. More...
 
static void process_update_data (void)
 Processes binary data arriving over USB during the update process. More...
 
void init_serial_communication (const struct serial_command *const commands, uint8_t commands_size)
 
void serial_communication_task (void)
 Main task for USB communication. More...
 

Variables

static char cmd_buffer [100] = ""
 Buffer for incoming commands. More...
 
static uint8_t cmd_buffer_index = 0
 Command buffer write index. More...
 
static char cmd_history [4][100] = {"",}
 Buffer for incoming commands. More...
 
static uint8_t cmd_history_index = 0
 Command buffer write index. More...
 
static const struct serial_commanduser_commands
 Array of user-defined commands. More...
 
static uint8_t user_commands_size
 Number of registered user commands. More...
 
static bool reset = false
 Device reset flag. More...
 
static bool update_in_progress = false
 This flag indicates if an update is in progress. More...
 
static uint16_t update_bytes_pending = 0
 Number of pending update bytes. More...
 
static uint16_t num_pages = 0
 Number of pages required for update. More...
 
static uint16_t expected_crc = 0
 CRC of the firmware. More...
 
static uint8_t page_buffer [SPM_PAGESIZE] = ""
 Buffer for application pages. More...
 
static uint16_t page_buffer_index = 0
 Page buffer write index. More...
 
static uint32_t temp_app_addr = 0
 Temporary application write address. More...
 
static bool echo_on = true
 Terminal echo flag. More...
 

Detailed Description

Implementation of the serial communication module.

See also
module header

Function Documentation

◆ exec_help()

static bool exec_help ( void  )
inlinestatic

Handler for the help command.

◆ exec_update()

static bool exec_update ( const char *const  command)
inlinestatic

Handler for the update command.

◆ execute_command()

static void execute_command ( const char *const  command)
inlinestatic

Executes an interactive command.

Tries to match the beginning of the supplied string to the registered commands. If a matching command string is found, its handler is invoked. In case no suitable command could be matched, or the executed handler returns a non-zero value, an error message is sent.

Parameters
commandthe full command line as a C-string

◆ handle_escape_sequence()

static bool handle_escape_sequence ( const char  data)
static

Handles ANSI escape sequences.

Returns
true as long as we are in the middle of an escape sequence

◆ init_serial_communication()

void init_serial_communication ( const struct serial_command *const  commands,
uint8_t  commands_size 
)

◆ print_command_from_history()

static void print_command_from_history ( const int8_t  offset)
inlinestatic

Prints out a command from the history.

Clears out the last printed command first.

Parameters
offsetoffset of the command to be printed

◆ process_command_char()

static void process_command_char ( void  )
inlinestatic

Processes a command character.

If the supplied character is a carriage return, the command line read so far is executed, otherwise the character is simply appended to a (circular!) buffer. If echo_on is set, the character is also immediately sent back to the sender, whereby carriage return characters are replaced by USB_NEWLINE.

◆ process_update_data()

static void process_update_data ( void  )
inlinestatic

Processes binary data arriving over USB during the update process.

If the supplied character is a carriage return, the command line read so far is executed, otherwise the character is simply appended to a (circular!) buffer.

◆ serial_communication_task()

void serial_communication_task ( void  )

Main task for USB communication.

This task must be run as a slow or medium speed task in order to enable USB communication and firmware updates. It processes commands arriving on the bus and takes required actions by calling registered handler functions.

See also
init_serial_communication

Variable Documentation

◆ cmd_buffer

char cmd_buffer[100] = ""
static

Buffer for incoming commands.

◆ cmd_buffer_index

uint8_t cmd_buffer_index = 0
static

Command buffer write index.

◆ cmd_history

char cmd_history[4][100] = {"",}
static

Buffer for incoming commands.

◆ cmd_history_index

uint8_t cmd_history_index = 0
static

Command buffer write index.

◆ echo_on

bool echo_on = true
static

Terminal echo flag.

When this flag is set, received bytes are immediately echoed back to provide a shell-like experience.

◆ expected_crc

uint16_t expected_crc = 0
static

CRC of the firmware.

◆ num_pages

uint16_t num_pages = 0
static

Number of pages required for update.

◆ page_buffer

uint8_t page_buffer[SPM_PAGESIZE] = ""
static

Buffer for application pages.

◆ page_buffer_index

uint16_t page_buffer_index = 0
static

Page buffer write index.

◆ reset

bool reset = false
static

Device reset flag.

This flag is set when the device receives the reset command. The device will then stop receiving data, send a shutdown message, count down a timeout and reset itself.

◆ temp_app_addr

uint32_t temp_app_addr = 0
static

Temporary application write address.

◆ update_bytes_pending

uint16_t update_bytes_pending = 0
static

Number of pending update bytes.

◆ update_in_progress

bool update_in_progress = false
static

This flag indicates if an update is in progress.

◆ user_commands

const struct serial_command* user_commands
static

Array of user-defined commands.

See also
init_serial_communication

◆ user_commands_size

uint8_t user_commands_size
static

Number of registered user commands.