2012-10-16 14:02:38 +00:00
|
|
|
/* MSPDebug - debugging tool for MSP430 MCUs
|
|
|
|
* Copyright (C) 2012 Unai Uribarri Rodriguez
|
|
|
|
*
|
|
|
|
* This program is free software; you can redisgibute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is disgibuted 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2018-06-26 08:09:08 +00:00
|
|
|
#include <stdio.h>
|
2012-10-16 14:02:38 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "simio_device.h"
|
|
|
|
#include "simio_console.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "output.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct console {
|
|
|
|
struct simio_device base;
|
|
|
|
|
|
|
|
/* Base address */
|
|
|
|
address_t base_addr;
|
|
|
|
char buffer[256];
|
|
|
|
unsigned buffer_offset;
|
2018-06-26 08:09:08 +00:00
|
|
|
|
|
|
|
/* File output */
|
|
|
|
FILE *file;
|
2012-10-16 14:02:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct simio_device *console_create(char **arg_text)
|
|
|
|
{
|
|
|
|
struct console *c;
|
|
|
|
|
|
|
|
(void)arg_text;
|
|
|
|
|
|
|
|
c = malloc(sizeof(*c));
|
|
|
|
if (!c) {
|
|
|
|
pr_error("console: can't allocate memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(c, 0, sizeof(*c));
|
|
|
|
c->base.type = &simio_console;
|
|
|
|
c->base_addr = 0xFF;
|
|
|
|
c->buffer_offset = 0;
|
|
|
|
return (struct simio_device *)c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void console_destroy(struct simio_device *dev)
|
|
|
|
{
|
|
|
|
struct console *c = (struct console *)dev;
|
2018-06-26 08:09:08 +00:00
|
|
|
|
|
|
|
if (c->file != NULL) {
|
|
|
|
fclose(c->file);
|
|
|
|
}
|
|
|
|
|
2012-10-16 14:02:38 +00:00
|
|
|
free(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void console_reset(struct simio_device *dev)
|
|
|
|
{
|
|
|
|
struct console *c = (struct console *)dev;
|
|
|
|
c->buffer_offset = 0;
|
2018-06-26 08:09:08 +00:00
|
|
|
|
|
|
|
if (c->file != NULL) {
|
|
|
|
rewind(c->file);
|
|
|
|
}
|
2012-10-16 14:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_addr(address_t *addr, char **arg_text)
|
|
|
|
{
|
|
|
|
char *text = get_arg(arg_text);
|
|
|
|
|
|
|
|
if (!text) {
|
|
|
|
printc_err("console: config: expected address\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr_eval(text, addr) < 0) {
|
|
|
|
printc_err("console: can't parse address: %s\n", text);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-26 08:09:08 +00:00
|
|
|
static int config_output(FILE **file, char **arg_text)
|
|
|
|
{
|
|
|
|
char *path = get_arg(arg_text);
|
|
|
|
|
|
|
|
if (path == NULL) {
|
|
|
|
printc_err("console: config: expected path\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't leak the descriptor if configured multiple times
|
|
|
|
if ((*file != NULL) && (*file != stdout)) {
|
|
|
|
fclose(*file);
|
|
|
|
}
|
|
|
|
|
|
|
|
*file = fopen(path, "w");
|
|
|
|
if (*file == NULL) {
|
|
|
|
printc_err("console: can't open %s for writing\n", path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-16 14:02:38 +00:00
|
|
|
static int console_config(struct simio_device *dev,
|
|
|
|
const char *param, char **arg_text)
|
|
|
|
{
|
|
|
|
struct console *c = (struct console *)dev;
|
|
|
|
|
2018-06-26 08:09:08 +00:00
|
|
|
if (!strcasecmp(param, "base")) {
|
2012-10-16 14:02:38 +00:00
|
|
|
return config_addr(&c->base_addr, arg_text);
|
|
|
|
}
|
2018-06-26 08:09:08 +00:00
|
|
|
else if (!strcasecmp(param, "output")) {
|
|
|
|
return config_output(&c->file, arg_text);
|
|
|
|
}
|
2012-10-16 14:02:38 +00:00
|
|
|
|
|
|
|
printc_err("console: config: unknown parameter: %s\n", param);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int console_info(struct simio_device *dev)
|
|
|
|
{
|
|
|
|
struct console *c = (struct console *)dev;
|
|
|
|
printc("Base address: 0x%04x\n", c->base_addr);
|
|
|
|
printc("Buffer: %.*s\n", c->buffer_offset, c->buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int console_write_b(struct simio_device *dev,
|
|
|
|
address_t addr, uint8_t data)
|
|
|
|
{
|
|
|
|
struct console *c = (struct console *)dev;
|
2018-06-26 08:09:08 +00:00
|
|
|
|
|
|
|
if (addr != c->base_addr) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write either to file or buffer
|
|
|
|
if (c->file != NULL)
|
|
|
|
{
|
|
|
|
size_t nbytes = sizeof(data);
|
|
|
|
if (fwrite(&data, nbytes, 1, c->file) != nbytes) {
|
|
|
|
printc_err("console: write error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for tail -f
|
|
|
|
if (data == '\n') {
|
|
|
|
fflush(c->file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // buffer
|
2012-10-16 14:02:38 +00:00
|
|
|
{
|
|
|
|
c->buffer[c->buffer_offset++] = data;
|
|
|
|
if (data == '\n' || c->buffer_offset == sizeof c->buffer)
|
|
|
|
{
|
|
|
|
printc("%.*s", c->buffer_offset, c->buffer);
|
|
|
|
c->buffer_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 08:09:08 +00:00
|
|
|
|
2012-10-16 14:02:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct simio_class simio_console = {
|
|
|
|
.name = "console",
|
|
|
|
.help =
|
2018-06-26 08:09:08 +00:00
|
|
|
"This peripheral prints to buffer or file every byte written to base address\n"
|
2012-10-16 14:02:38 +00:00
|
|
|
"\n"
|
|
|
|
"Config arguments are:\n"
|
|
|
|
" base <address>\n"
|
|
|
|
" Set the peripheral base address. Defaults to 0x00FF\n"
|
2018-06-26 08:09:08 +00:00
|
|
|
" output <path>\n"
|
|
|
|
" Print to file instead of a buffer.\n"
|
2012-10-16 14:02:38 +00:00
|
|
|
"\n",
|
|
|
|
|
|
|
|
.create = console_create,
|
|
|
|
.destroy = console_destroy,
|
|
|
|
.reset = console_reset,
|
|
|
|
.config = console_config,
|
|
|
|
.info = console_info,
|
|
|
|
.write_b = console_write_b,
|
|
|
|
};
|