Added some basic commands

This commit is contained in:
Stephen Moody 2019-08-16 15:29:22 +01:00
parent c6caaafde7
commit fdaf848014
3 changed files with 156 additions and 156 deletions

View File

@ -1,27 +1,27 @@
#ifndef DECODECOMMANDS_H #ifndef DECODECOMMANDS_H
#define DECODECOMMANDS_H #define DECODECOMMANDS_H
#include "stm32f0xx_system.h" #include "stm32f0xx_system.h"
enum CommsErrors enum CommsErrors
{ {
CMD_NO_ERROR = 0, CMD_NO_ERROR = 0,
CMD_NOT_KNOWN = -1, CMD_NOT_KNOWN = -1,
CMD_BUSY = -2, CMD_BUSY = -2,
CMD_PARAM_ERROR = -3, CMD_PARAM_ERROR = -3,
CMD_RANGE_ERROR = -4 CMD_RANGE_ERROR = -4
}; };
#define MAX_BUFFER_LENGTH 255 #define MAX_BUFFER_LENGTH 255
typedef struct typedef struct
{ {
char *command; // Two character command code char *command; // Two character command code
int16_t (*function_ptr)(); // Pointer to command function int16_t (*function_ptr)(); // Pointer to command function
char *desc; // Pointer to description string for help char *desc; // Pointer to description string for help
} command_entry_t; } command_entry_t;
void decode_command(char* commandStr); void decode_command(char* commandStr);
#endif #endif

View File

@ -1,116 +1,116 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include "commands.h" #include "commands.h"
#include "gpio.h" #include "gpio.h"
extern const char *firmware_version; extern const char *firmware_version;
int16_t cmd_print_help(void); int16_t cmd_print_help(void);
int16_t cmd_print_version(void); int16_t cmd_print_version(void);
int16_t cmd_reset_state(char *params); int16_t cmd_reset_state(char *params);
const command_entry_t g_command_tab[] = const command_entry_t g_command_tab[] =
{ {
{"??", cmd_print_help, "Displays this help with a list of available commands"}, {"??", cmd_print_help, "Displays this help with a list of available commands"},
{":S", cmd_reset_state, "Sets the reset state of the target board, params 0 or 1"}, {":S", cmd_reset_state, "Sets the reset state of the target board, params 0 or 1"},
{":V", cmd_print_version, "Displays the version number"} {":V", cmd_print_version, "Displays the version number"}
}; };
// Number of commands in the Command Table // Number of commands in the Command Table
const uint16_t g_number_commands = sizeof(g_command_tab) / sizeof(command_entry_t); const uint16_t g_number_commands = sizeof(g_command_tab) / sizeof(command_entry_t);
// This allows commands from RS232 interface to control the board, format given above. // This allows commands from RS232 interface to control the board, format given above.
void decode_command(char* commandStr) void decode_command(char* commandStr)
{ {
char tempCommand[3] = {0}; char tempCommand[3] = {0};
char *paramPtr; char *paramPtr;
// Convert command string to upper case // Convert command string to upper case
strupr(commandStr); strupr(commandStr);
// skip whitespace characters // skip whitespace characters
while (isspace(*commandStr) && (*commandStr != '\0')) while (isspace(*commandStr) && (*commandStr != '\0'))
commandStr++; commandStr++;
if (*commandStr == '\0') if (*commandStr == '\0')
{ {
// no command found on // no command found on
printf("%d\n", CMD_NOT_KNOWN); printf("%d\n", CMD_NOT_KNOWN);
return; return;
} }
// command found search for parameter string // command found search for parameter string
paramPtr = commandStr + 2; paramPtr = commandStr + 2;
// skip whitespace characters // skip whitespace characters
while (isspace(*paramPtr) && (*paramPtr != '\0')) while (isspace(*paramPtr) && (*paramPtr != '\0'))
paramPtr++; paramPtr++;
// set parameter pointer to zero if no parameter found on line // set parameter pointer to zero if no parameter found on line
if (*paramPtr == '\0') if (*paramPtr == '\0')
paramPtr = 0; paramPtr = 0;
// copy 1st 2 characters of command string into tempCommand // copy 1st 2 characters of command string into tempCommand
tempCommand[0] = commandStr[0]; tempCommand[0] = commandStr[0];
tempCommand[1] = commandStr[1]; tempCommand[1] = commandStr[1];
// search for valid command // search for valid command
for (int cmd = 0; cmd < g_number_commands; cmd++) for (int cmd = 0; cmd < g_number_commands; cmd++)
{ {
if (strcmp(tempCommand, g_command_tab[cmd].command) == 0) if (strcmp(tempCommand, g_command_tab[cmd].command) == 0)
{ {
// jump to matching command and save error code from command handler // jump to matching command and save error code from command handler
int tmpErr = (*g_command_tab[cmd].function_ptr)(paramPtr); int tmpErr = (*g_command_tab[cmd].function_ptr)(paramPtr);
if (tmpErr != CMD_NO_ERROR) if (tmpErr != CMD_NO_ERROR)
printf("%d\n", tmpErr); printf("%d\n", tmpErr);
return; return;
} }
} }
// here if no commands matched // here if no commands matched
printf("%d\n", CMD_NOT_KNOWN); printf("%d\n", CMD_NOT_KNOWN);
} }
int16_t cmd_print_help() int16_t cmd_print_help()
{ {
printf("Commands available:\n"); printf("Commands available:\n");
// Print the command code and the description for each command in the table // Print the command code and the description for each command in the table
for (uint16_t i = 0; i < g_number_commands; i++) for (uint16_t i = 0; i < g_number_commands; i++)
{ {
if (g_command_tab[i].command != NULL) if (g_command_tab[i].command != NULL)
printf("\t%s - %s\n", g_command_tab[i].command, g_command_tab[i].desc); printf("\t%s - %s\n", g_command_tab[i].command, g_command_tab[i].desc);
else else
printf("%s\n", g_command_tab[i].desc); printf("%s\n", g_command_tab[i].desc);
} }
return CMD_NO_ERROR; return CMD_NO_ERROR;
} }
int16_t cmd_print_version() int16_t cmd_print_version()
{ {
printf("%d\nEPROM Emulator firmware version: %s\n", CMD_NO_ERROR, firmware_version); printf("%d\nEPROM Emulator firmware version: %s\n", CMD_NO_ERROR, firmware_version);
return CMD_NO_ERROR; return CMD_NO_ERROR;
} }
int16_t cmd_reset_state(char *params) int16_t cmd_reset_state(char *params)
{ {
int state; int state;
int numParams = sscanf(params, "%d", &state); int numParams = sscanf(params, "%d", &state);
if (numParams != 1) if (numParams != 1)
{ {
printf("%d\n", CMD_PARAM_ERROR); printf("%d\n", CMD_PARAM_ERROR);
return CMD_PARAM_ERROR; return CMD_PARAM_ERROR;
} }
set_target_reset_state((uint8_t)state); set_target_reset_state((uint8_t)state);
printf("%d\n", CMD_NO_ERROR); printf("%d\n", CMD_NO_ERROR);
return CMD_NO_ERROR; return CMD_NO_ERROR;
} }

View File

@ -1,4 +1,4 @@
#include "shiftregister.h" #include "shiftregister.h"
//Global Variables //Global Variables
uint32_t g_output_shift_data = 0; //!< Data to be written to the shift register outputs uint32_t g_output_shift_data = 0; //!< Data to be written to the shift register outputs
@ -19,16 +19,16 @@ void set_sr_data_out(uint8_t state)
{ {
HAL_GPIO_WritePin(SR_DATA_GPIO_Port, SR_DATA_Pin, state); HAL_GPIO_WritePin(SR_DATA_GPIO_Port, SR_DATA_Pin, state);
} }
void set_sr_reset_state(uint8_t state) void set_sr_reset_state(uint8_t state)
{ {
HAL_GPIO_WritePin(SR_RST_GPIO_Port, SR_RST_Pin, state); HAL_GPIO_WritePin(SR_RST_GPIO_Port, SR_RST_Pin, state);
}
void set_sr_output_state(uint8_t state)
{
HAL_GPIO_WritePin(SR_OE_GPIO_Port, SR_OE_Pin, state);
} }
void set_sr_output_state(uint8_t state)
{
HAL_GPIO_WritePin(SR_OE_GPIO_Port, SR_OE_Pin, state);
}
uint32_t read_sr_data_in(void) uint32_t read_sr_data_in(void)
{ {
@ -51,8 +51,8 @@ uint32_t update_shift_registers(void)
uint32_t data_out = 0; uint32_t data_out = 0;
uint32_t data_in = 0; uint32_t data_in = 0;
uint32_t bit_mask; uint32_t bit_mask;
uint8_t i; uint8_t i;
// Shift Data out // Shift Data out
data_out = g_output_shift_data; data_out = g_output_shift_data;
bit_mask = 0x0800; bit_mask = 0x0800;
@ -60,7 +60,7 @@ uint32_t update_shift_registers(void)
for (i = 0; i < 24; i++) for (i = 0; i < 24; i++)
{ {
set_sr_clock(0); set_sr_clock(0);
if (data_out & bit_mask) if (data_out & bit_mask)
set_sr_data_out(1); set_sr_data_out(1);
else else