add string and token helpers
This commit is contained in:
committed by
Jannis (jix) Harder
parent
6c702b9a68
commit
e7e5cfd126
@@ -33,13 +33,13 @@ ifeq ($(DEBUG),1)
|
||||
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o \
|
||||
main.o usb_bulk.o uart.o fifo.o sram.o crc.o debug.o \
|
||||
dump.o timer.o watchdog.o rle.c loader.o info.o shared_memory.o \
|
||||
pwm.o shell.o irq.o command.o testing.o
|
||||
pwm.o util.o shell.o irq.o command.o testing.o
|
||||
else
|
||||
LDFLAGS =-Wl,-u
|
||||
CFLAGS =-Iusbdrv -I. -DDEBUG_LEVEL=0 -DNO_DEBUG -DNO_INFO
|
||||
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o usb_bulk.o \
|
||||
uart.o fifo.o sram.o crc.o debug.o dump.o timer.o watchdog.o rle.c loader.o \
|
||||
pwm.o shell.o info.o shared_memory.o command.o irq.o
|
||||
pwm.o uril.o shell.o info.o shared_memory.o command.o irq.o
|
||||
endif
|
||||
|
||||
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)
|
||||
|
||||
@@ -33,6 +33,109 @@
|
||||
volatile uint8_t recv_counter = 0;
|
||||
volatile uint8_t cr = 0;
|
||||
|
||||
|
||||
static char *token_ptr;
|
||||
|
||||
static char *get_token(void)
|
||||
{
|
||||
char *p = token_ptr;
|
||||
while (*p == ' ')
|
||||
p++;
|
||||
if (*p == '\0')
|
||||
return NULL;
|
||||
token_ptr = p;
|
||||
do {
|
||||
token_ptr++;
|
||||
if (*token_ptr == ' ') {
|
||||
*token_ptr++ = '\0';
|
||||
break;
|
||||
}
|
||||
} while (*token_ptr != '\0');
|
||||
return p;
|
||||
}
|
||||
|
||||
static int get_dec(int *decval)
|
||||
{
|
||||
const char *t;
|
||||
t = get_token();
|
||||
if (t != NULL) {
|
||||
int x = Util_sscandec(t);
|
||||
if (x < 0)
|
||||
return FALSE;
|
||||
*decval = x;
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static int parse_hex(const char *s, UWORD *hexval)
|
||||
{
|
||||
int x = Util_sscanhex(s);
|
||||
#ifdef MONITOR_HINTS
|
||||
int y = find_label_value(s);
|
||||
if (y >= 0) {
|
||||
if (x < 0 || x > 0xffff || x == y) {
|
||||
*hexval = (UWORD) y;
|
||||
return TRUE;
|
||||
}
|
||||
/* s can be a hex number or a label name */
|
||||
printf("%s is ambiguous. Use 0%X or %X instead.\n", s, x, y);
|
||||
return FALSE;
|
||||
}
|
||||
#endif
|
||||
if (x < 0 || x > 0xffff)
|
||||
return FALSE;
|
||||
*hexval = (UWORD) x;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static int get_hex(UWORD *hexval)
|
||||
{
|
||||
const char *t;
|
||||
t = get_token();
|
||||
if (t != NULL)
|
||||
return parse_hex(t, hexval);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static int get_hex2(UWORD *hexval1, UWORD *hexval2)
|
||||
{
|
||||
return get_hex(hexval1) && get_hex(hexval2);
|
||||
}
|
||||
|
||||
static int get_hex3(UWORD *hexval1, UWORD *hexval2, UWORD *hexval3)
|
||||
{
|
||||
return get_hex(hexval1) && get_hex(hexval2) && get_hex(hexval3);
|
||||
}
|
||||
|
||||
static void get_uword(UWORD *val)
|
||||
{
|
||||
if (!get_hex(val))
|
||||
printf("Invalid argument!\n");
|
||||
}
|
||||
|
||||
static void get_ubyte(UBYTE *val)
|
||||
{
|
||||
UWORD uword;
|
||||
if (!get_hex(&uword) || uword > 0xff)
|
||||
printf("Invalid argument!\n");
|
||||
else
|
||||
*val = (UBYTE) uword;
|
||||
}
|
||||
|
||||
static int get_bool(void)
|
||||
{
|
||||
const char *t;
|
||||
t = get_token();
|
||||
if (t != NULL) {
|
||||
int result = Util_sscanbool(t);
|
||||
if (result >= 0)
|
||||
return result;
|
||||
}
|
||||
printf("Invalid argument (should be 0 or 1)!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ISR(USART0_RX_vect) // Interrupt for UART Byte received
|
||||
{
|
||||
UCSR0B &= (255 - (1<<RXCIE0));// Interrupts disable for RxD
|
||||
@@ -41,6 +144,7 @@ ISR(USART0_RX_vect) // Interrupt for UART Byte received
|
||||
cr=1;
|
||||
recv_buf[recv_counter]='\0';
|
||||
recv_counter=0;
|
||||
uart_putc('\r');
|
||||
uart_putc('\n');
|
||||
uart_putc(':');
|
||||
uart_putc('>');
|
||||
@@ -52,7 +156,10 @@ ISR(USART0_RX_vect) // Interrupt for UART Byte received
|
||||
cr = 1; // found a CR, so the application should do something
|
||||
recv_buf[++recv_counter]='\0'; // terminate string
|
||||
recv_counter = 0;
|
||||
uart_putc('\r');
|
||||
uart_putc('\n');
|
||||
uart_putc(':');
|
||||
uart_putc('>');
|
||||
} else {
|
||||
// we accept backspace or delete
|
||||
if ((recv_buf[recv_counter] == 0x08 || recv_buf[recv_counter] == 0x7f) && recv_counter > 0) {
|
||||
|
||||
132
avr/usbload/util.c
Normal file
132
avr/usbload/util.c
Normal file
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* ________ .__ __ ________ ____ ________
|
||||
* \_____ \ __ __|__| ____ | | __\______ \ _______ _/_ |/ _____/
|
||||
* / / \ \| | \ |/ ___\| |/ / | | \_/ __ \ \/ /| / __ \
|
||||
* / \_/. \ | / \ \___| < | ` \ ___/\ / | \ |__\ \
|
||||
* \_____\ \_/____/|__|\___ >__|_ \/_______ /\___ >\_/ |___|\_____ /
|
||||
* \__> \/ \/ \/ \/ \/
|
||||
*
|
||||
* www.optixx.org
|
||||
*
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 07/21/2009 03:32:16 PM
|
||||
* Author: david@optixx.org
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
uint8_t *util_strupper(uint8_t *s)
|
||||
{
|
||||
uint8_t *p;
|
||||
for (p = s; *p != '\0'; p++)
|
||||
if (*p >= 'a' && *p <= 'z')
|
||||
*p += 'A' - 'a';
|
||||
return s;
|
||||
}
|
||||
|
||||
uint8_t *util_strlower(uint8_t *s)
|
||||
{
|
||||
uint8_t *p;
|
||||
for (p = s; *p != '\0'; p++)
|
||||
if (*p >= 'A' && *p <= 'Z')
|
||||
*p += 'a' - 'A';
|
||||
return s;
|
||||
}
|
||||
|
||||
void util_chomp(uint8_t *s)
|
||||
{
|
||||
uint16_t len;
|
||||
|
||||
len = strlen((char*)s);
|
||||
if (len >= 2 && s[len - 1] == '\n' && s[len - 2] == '\r')
|
||||
s[len - 2] = '\0';
|
||||
else if (len >= 1 && (s[len - 1] == '\n' || s[len - 1] == '\r'))
|
||||
s[len - 1] = '\0';
|
||||
}
|
||||
|
||||
void util_trim(uint8_t *s)
|
||||
{
|
||||
uint8_t *p = s;
|
||||
uint8_t *q;
|
||||
/* skip leading whitespace */
|
||||
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
|
||||
p++;
|
||||
/* now p points at the first non-whitespace uint8_tacter */
|
||||
|
||||
if (*p == '\0') {
|
||||
/* only whitespace */
|
||||
*s = '\0';
|
||||
return;
|
||||
}
|
||||
|
||||
q = s + strlen((char*)s);
|
||||
/* skip trailing whitespace */
|
||||
/* we have found p < q such that *p is non-whitespace,
|
||||
so this loop terminates with q >= p */
|
||||
do
|
||||
q--;
|
||||
while (*q == ' ' || *q == '\t' || *q == '\r' || *q == '\n');
|
||||
|
||||
/* now q points at the last non-whitespace uint8_tacter */
|
||||
/* cut off trailing whitespace */
|
||||
*++q = '\0';
|
||||
|
||||
/* move to string */
|
||||
memmove(s, p, q + 1 - p);
|
||||
}
|
||||
|
||||
uint32_t util_sscandec(const uint8_t *s)
|
||||
{
|
||||
uint32_t result;
|
||||
if (*s == '\0')
|
||||
return -1;
|
||||
result = 0;
|
||||
for (;;) {
|
||||
if (*s >= '0' && *s <= '9')
|
||||
result = 10 * result + *s - '0';
|
||||
else if (*s == '\0')
|
||||
return result;
|
||||
else
|
||||
return -1;
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t util_sscanhex(const uint8_t *s)
|
||||
{
|
||||
uint32_t result;
|
||||
if (*s == '\0')
|
||||
return -1;
|
||||
result = 0;
|
||||
for (;;) {
|
||||
if (*s >= '0' && *s <= '9')
|
||||
result = 16 * result + *s - '0';
|
||||
else if (*s >= 'A' && *s <= 'F')
|
||||
result = 16 * result + *s - 'A' + 10;
|
||||
else if (*s >= 'a' && *s <= 'f')
|
||||
result = 16 * result + *s - 'a' + 10;
|
||||
else if (*s == '\0')
|
||||
return result;
|
||||
else
|
||||
return -1;
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t util_sscanbool(const uint8_t *s)
|
||||
{
|
||||
if (*s == '0' && s[1] == '\0')
|
||||
return 0;
|
||||
if (*s == '1' && s[1] == '\0')
|
||||
return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
32
avr/usbload/util.h
Normal file
32
avr/usbload/util.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* ________ .__ __ ________ ____ ________
|
||||
* \_____ \ __ __|__| ____ | | __\______ \ _______ _/_ |/ _____/
|
||||
* / / \ \| | \ |/ ___\| |/ / | | \_/ __ \ \/ /| / __ \
|
||||
* / \_/. \ | / \ \___| < | ` \ ___/\ / | \ |__\ \
|
||||
* \_____\ \_/____/|__|\___ >__|_ \/_______ /\___ >\_/ |___|\_____ /
|
||||
* \__> \/ \/ \/ \/ \/
|
||||
*
|
||||
* www.optixx.org
|
||||
*
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 07/21/2009 03:32:16 PM
|
||||
* Author: david@optixx.org
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_H__
|
||||
#define __UTIL_H__
|
||||
|
||||
uint8_t *util_strupper(uint8_t *s);
|
||||
uint8_t *util_strlower(uint8_t *s);
|
||||
void util_chomp(uint8_t *s);
|
||||
void util_trim(uint8_t *s);
|
||||
uint32_t util_sscandec(const uint8_t *s);
|
||||
uint32_t util_sscanhex(const uint8_t *s);
|
||||
uint8_t util_sscanbool(const uint8_t *s);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user