CVS: libraries/commandline checksum.c,NONE,1.1 commandline.c,NONE,1.1 commandline_number.c,NONE,1.1 help.c,NONE,1.1 hexdump.c,NONE,1.1 makefile,NONE,1.1 memory_erase.c,NONE,1.1 memory_write.c,NONE,1.1 reset.c,NONE,1.1
Chris Liechti <[email protected]>
| Newsgroups | gmane.comp.hardware.texas-instruments.msp430.gcc.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/mspgcc/libraries/commandline
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20541/libraries/commandline
Added Files:
checksum.c commandline.c commandline_number.c help.c hexdump.c
makefile memory_erase.c memory_write.c reset.c
Log Message:
initial import of commandline library
simple command line parser and some commands
--- NEW FILE: checksum.c ---
#include <stdio.h>
#include "mspgcc/util.h"
#include "commandline.h"
int command_checksum(int argc, char *argv[]) {
unsigned int address;
unsigned int length = 160;
if (argc < 2) {
printf("USAGE: %s address [size]\n", argv[0]);
return 1;
}
address = commandline_number(argv[1]);
if (argc == 3) {
length = commandline_number(argv[2]);
}
printf("Checksum over 0x%04x-0x%04x is 0x%04x\n",
address,
address+length,
checksum_xor((void *)address, length)
);
return 0;
}
--- NEW FILE: commandline.c ---
#include <stdio.h>
#include <string.h>
#include "commandline.h"
#define MAXARGS 4
int commandline_eval(char *commandline, const COMMANDLINE_TABLE *command_table) {
int argc = 0;
char *argv[MAXARGS];
char *ptr;
// Split string into separate arguments
ptr = commandline;
// skip whitespace
while (*ptr && (*ptr == ' ')) {
ptr++;
}
//handle empty line
if (!*ptr) {
return 0;
}
argv[argc++] = ptr;
while (*ptr) { // Search to the end of the string
if (*ptr == ' ') { // Space is the delimiter
*ptr = 0;
if (argc >= MAXARGS) {
//addidional args will get lost...
break;
}
// skip whitespace
do {
ptr++;
} while (*ptr == ' ');
if (*ptr) {
argv[argc++] = ptr;
} else {
break;
}
}
ptr++;
}
//search table for command
for (int i = 0; command_table[i].name; i++) {
if (strcmp(argv[0], command_table[i].name) == 0) {
return command_table[i].function(argc, argv);
}
}
return COMMANDLINE_NO_SUCH_COMMNAD;
}
--- NEW FILE: commandline_number.c ---
#include <stdlib.h>
unsigned int commandline_number(const char *s) {
return strtol(s, NULL, 0);
}
--- NEW FILE: help.c ---
#include <stdio.h>
#include "commandline.h"
//the help commands expect that there is a global command table
extern const COMMANDLINE_TABLE commandline_table[];
int command_help(int argc, char *argv[]) {
const COMMANDLINE_TABLE *table_entry = commandline_table;
puts("Commands:");
while (table_entry->name) {
printf("%-10s %s\n", table_entry->name, table_entry->help);
table_entry++;
}
return 0;
}
--- NEW FILE: hexdump.c ---
#include <stdio.h>
#include <stdlib.h>
#include "commandline.h"
#include "mspgcc/util.h"
int command_hexdump(int argc, char *argv[]) {
unsigned int address;
unsigned int length = 256;
if (argc < 2) {
printf("USAGE: %s address [size]\n", argv[0]);
return 1;
}
address = commandline_number(argv[1]);
if (argc >= 3) {
length = commandline_number(argv[2]);
}
hexdump((void *)address, length, address);
return 0;
}
--- NEW FILE: makefile ---
# makfile configuration
LIBRARYNAME = commandline
CSOURCES = commandline.c commandline_number.c \
help.c reset.c checksum.c \
memory_write.c memory_erase.c hexdump.c
#~ ASOURCES =
CPU = msp1
ASFLAGS = -mmcu=${CPU} -D_GNU_ASSEMBLER_ -I ../include
CFLAGS = -mmcu=${CPU} -O2 -Wall -std=gnu99 -g -I ../include
#switch the compiler (for the internal make rules)
CC = msp430-gcc
AS = msp430-gcc
AR = msp430-ar
OBJECTS = ${CSOURCES:.c=.o} ${ASOURCES:.S=.o}
.PHONY: all FORCE clean
#all should be the first target. it's built when make is run without args
all: lib$(LIBRARYNAME).a lib$(LIBRARYNAME).lst dependencies.d
lib$(LIBRARYNAME).a: $(OBJECTS)
$(AR) rcf $@ $^
lib$(LIBRARYNAME).lst: lib$(LIBRARYNAME).a
msp430-objdump -dSt $^ >$@
@echo "----- RAM/Flash Usage -----"
msp430-size $^
clean:
rm -f lib$(LIBRARYNAME).a lib$(LIBRARYNAME).lst ${OBJECTS} dependencies.d
#dummy target as dependecy if something has to be build everytime
FORCE:
#project dependencies
dependencies.d:
$(CC) -MM ${CFLAGS} ${CSOURCES} > dependencies.d
ifdef ASOURCES
$(CC) -MM ${ASFLAGS} ${ASOURCES} >> dependencies.d
endif
-include dependencies.d
--- NEW FILE: memory_erase.c ---
#include <stdio.h>
#include <stdlib.h>
#include "commandline.h"
#include "mspgcc/flash.h"
int command_erase(int argc, char *argv[]) {
if (argc < 2) {
printf("USAGE: %s address\n", argv[0]);
return 1;
}
unsigned int address = commandline_number(argv[1]);
printf("Erasing segment at 0x%04x\n", address);
flash_erase_segment((void *)address);
return 0;
}
--- NEW FILE: memory_write.c ---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "commandline.h"
#include "mspgcc/util.h"
#include "mspgcc/flash.h"
int command_write(int argc, char *argv[]) {
char source[32];
unsigned int size;
if (argc < 3) {
printf("USAGE: %s address data\n", argv[0]);
return 1;
}
char *address = (char *)commandline_number(argv[1]);
size = hex_decode(source, sizeof(source), argv[2], strlen(argv[2]));
if (address <= (char *)0xff) { //bytemode peripherals
for (int offset = 0; offset < size; offset++) {
address[offset] = source[offset];
}
} else if (address < (char *)0x1000) { //wordmode peripherals, RAM
for (int offset = 0; offset < size/2; offset++) {
((short *)address)[offset] = ((short *)source)[offset];
}
} else { //flash
flash_write(address, source, size);
//verify
return !(memcmp(address, source, size) == 0);
}
return 0;
}
--- NEW FILE: reset.c ---
#include <stdio.h>
// must include one of the msp430 headerfiles to get the WDTCTL definitions
// doesn't matter which one as all MSP430 are compatible in this respect.
// this one is a small CPU and does not generate too much entries in the
// listing files ;-)
#include <msp430x11x.h>
#include "commandline.h"
int __attribute__((noreturn)) command_reset(int argc, char *argv[]) {
while (1) {
printf("Executing watchdog reset...\n ");
WDTCTL = 0xDEAD;
}
}
-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf