Add xprintf(xitoa) from elm-chan.org

This commit is contained in:
tmk
2013-05-14 16:16:57 +09:00
parent b9f558b3d8
commit d9c06db600
10 changed files with 789 additions and 222 deletions

View File

@@ -11,6 +11,7 @@ SRC += $(COMMON_DIR)/host.c \
$(COMMON_DIR)/print.c \
$(COMMON_DIR)/bootloader.c \
$(COMMON_DIR)/suspend.c \
$(COMMON_DIR)/xprintf.S \
$(COMMON_DIR)/util.c

View File

@@ -18,14 +18,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef DEBUG_H
#define DEBUG_H 1
#include <stdbool.h>
#include "print.h"
#include "debug_config.h"
#ifndef NO_DEBUG
#define dprint(s) do { if (debug_enable) print(s); } while (0)
#define dprintln() do { if (debug_enable) print_crlf(); } while (0)
#define dprintf(fmt, ...) do { if (debug_enable) __xprintf(PSTR(fmt), ##__VA_ARGS__); } while (0)
#define dmsg(s) dprintf("%s at %s: %S\n", __FILE__, __LINE__, PSTR(s))
/* DO NOT USE these anymore */
#define debug(s) do { if (debug_enable) print(s); } while (0)
#define debugln(s) do { if (debug_enable) println(s); } while (0)
#define debugln(s) do { if (debug_enable) print_crlf(); } while (0)
#define debug_S(s) do { if (debug_enable) print_S(s); } while (0)
#define debug_P(s) do { if (debug_enable) print_P(s); } while (0)
#define debug_msg(s) do { \
@@ -50,58 +56,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define debug_bin_reverse(data) debug_bin8(data)
#else
#define debug(s)
#define debugln(s)
#define debug_S(s)
#define debug_P(s)
#define debug_msg(s)
#define debug_dec(data)
#define debug_decs(data)
#define debug_hex4(data)
#define debug_hex8(data)
#define debug_hex16(data)
#define debug_hex32(data)
#define debug_bin8(data)
#define debug_bin16(data)
#define debug_bin32(data)
#define debug_bin_reverse8(data)
#define debug_bin_reverse16(data)
#define debug_bin_reverse32(data)
#define debug_hex(data)
#define debug_bin(data)
#define debug_bin_reverse(data)
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* NOTE: Not portable. Bit field order depends on implementation */
typedef union {
uint8_t raw;
struct {
bool enable:1;
bool matrix:1;
bool keyboard:1;
bool mouse:1;
uint8_t reserved:4;
};
} debug_config_t;
debug_config_t debug_config;
/* for backward compatibility */
#define debug_enable (debug_config.enable)
#define debug_matrix (debug_config.matrix)
#define debug_keyboard (debug_config.keyboard)
#define debug_mouse (debug_config.mouse)
#ifdef __cplusplus
}
#include "nodebug.h"
#endif
#endif

51
common/debug_config.h Normal file
View File

@@ -0,0 +1,51 @@
/*
Copyright 2013 Jun Wako <wakojun@gmail.com>
This program is free software: you can redistribute 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 distributed 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef DEBUG_CONFIG_H
#define DEBUG_CONFIG_H 1
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/* NOTE: Not portable. Bit field order depends on implementation */
typedef union {
uint8_t raw;
struct {
bool enable:1;
bool matrix:1;
bool keyboard:1;
bool mouse:1;
uint8_t reserved:4;
};
} debug_config_t;
debug_config_t debug_config;
/* for backward compatibility */
#define debug_enable (debug_config.enable)
#define debug_matrix (debug_config.matrix)
#define debug_keyboard (debug_config.keyboard)
#define debug_mouse (debug_config.mouse)
#ifdef __cplusplus
}
#endif
#endif

49
common/nodebug.h Normal file
View File

@@ -0,0 +1,49 @@
/*
Copyright 2013 Jun Wako <wakojun@gmail.com>
This program is free software: you can redistribute 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 distributed 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef NODEBUG_H
#define NODEBUG_H 1
#include "debug_config.h"
#define dprint(s)
#define dprintln(s)
#define dprintf(fmt, ...)
#define dmsg(s)
#define debug(s)
#define debugln(s)
#define debug_S(s)
#define debug_P(s)
#define debug_msg(s)
#define debug_dec(data)
#define debug_decs(data)
#define debug_hex4(data)
#define debug_hex8(data)
#define debug_hex16(data)
#define debug_hex32(data)
#define debug_bin8(data)
#define debug_bin16(data)
#define debug_bin32(data)
#define debug_bin_reverse8(data)
#define debug_bin_reverse16(data)
#define debug_bin_reverse32(data)
#define debug_hex(data)
#define debug_bin(data)
#define debug_bin_reverse(data)
#endif

View File

@@ -1,4 +1,4 @@
/* Copyright 2012 Jun Wako <wakojun@gmail.com> */
/* Copyright 2012,2013 Jun Wako <wakojun@gmail.com> */
/* Very basic print functions, intended to be used with usb_debug_only.c
* http://www.pjrc.com/teensy/
* Copyright (c) 2008 PJRC.COM, LLC
@@ -29,20 +29,14 @@
#ifndef NO_PRINT
#define sendchar(c) do { if (print_sendchar_func) (print_sendchar_func)(c); } while (0)
#define sendchar(c) xputc(c)
static int8_t (*print_sendchar_func)(uint8_t) = 0;
void print_set_sendchar(int8_t (*sendchar_func)(uint8_t))
{
print_sendchar_func = sendchar_func;
xdev_out(sendchar_func);
}
/* print string stored in data memory(SRAM)
* print_P("hello world");
* This consumes precious SRAM memory space for string.
*/
void print_S(const char *s)
{
uint8_t c;
@@ -54,140 +48,15 @@ void print_S(const char *s)
}
}
/* print string stored in program memory(FLASH)
* print_P(PSTR("hello world");
* This consumes relatively abundant FLASH memory area not SRAM.
*/
void print_P(const char *s)
void print_lf(void)
{
uint8_t c;
while (1) {
c = pgm_read_byte(s++);
if (!c) break;
if (c == '\n') sendchar('\r');
sendchar(c);
}
sendchar('\n');
}
void print_CRLF(void)
void print_crlf(void)
{
sendchar('\r'); sendchar('\n');
}
#define SIGNED 0x80
#define BIN 2
#define OCT 8
#define DEC 10
#define HEX 16
static inline
char itoc(uint8_t i)
{
return (i < 10 ? '0' + i : 'A' + i - 10);
}
static inline
void print_int(uint16_t data, uint8_t base)
{
char buf[7] = {'\0'};
char *p = &buf[6];
if ((base & SIGNED) && (data & 0x8000)) {
data = -data;
buf[0] = '-';
}
base &= ~SIGNED;
uint16_t n;
do {
n = data;
data /= base;
*(--p) = itoc(n - data*base);
} while (data);
if (buf[0]) *(--p) = buf[0];
print_S(p);
}
void print_dec(uint16_t data)
{
print_int(data, DEC);
}
void print_decs(int16_t data)
{
print_int(data, DEC|SIGNED);
}
void print_hex4(uint8_t data)
{
sendchar(data + ((data < 10) ? '0' : 'A' - 10));
}
void print_hex8(uint8_t data)
{
print_hex4(data>>4);
print_hex4(data&0x0F);
}
void print_hex16(uint16_t data)
{
print_hex8(data>>8);
print_hex8(data);
}
void print_hex32(uint32_t data)
{
print_hex16(data>>16);
print_hex16(data);
}
void print_bin4(uint8_t data)
{
for (int i = 4; i >= 0; i--) {
sendchar((data & (1<<i)) ? '1' : '0');
}
}
void print_bin8(uint8_t data)
{
for (int i = 7; i >= 0; i--) {
sendchar((data & (1<<i)) ? '1' : '0');
}
}
void print_bin16(uint16_t data)
{
print_bin8(data>>8);
print_bin8(data);
}
void print_bin32(uint32_t data)
{
print_bin8(data>>24);
print_bin8(data>>16);
print_bin8(data>>8);
print_bin8(data);
}
void print_bin_reverse8(uint8_t data)
{
for (int i = 0; i < 8; i++) {
sendchar((data & (1<<i)) ? '1' : '0');
}
}
void print_bin_reverse16(uint16_t data)
{
print_bin_reverse8(data);
print_bin_reverse8(data>>8);
}
void print_bin_reverse32(uint32_t data)
{
print_bin_reverse8(data);
print_bin_reverse8(data>>8);
print_bin_reverse8(data>>16);
print_bin_reverse8(data>>24);
sendchar('\r');
sendchar('\n');
}
#endif

View File

@@ -28,6 +28,8 @@
#include <stdint.h>
#include <stdbool.h>
#include <avr/pgmspace.h>
#include "xprintf.h"
#include "util.h"
// this macro allows you to write print("some text") and
@@ -49,17 +51,17 @@
#define pbin_reverse16(data) print_bin_reverse16(data)
/* print value utility */
#define print_val_dec(v) do { print_P(PSTR(#v ": ")); print_dec(v); print_P(PSTR("\n")); } while (0)
#define print_val_decs(v) do { print_P(PSTR(#v ": ")); print_decs(v); print_P(PSTR("\n")); } while (0)
#define print_val_hex8(v) do { print_P(PSTR(#v ": ")); print_hex8(v); print_P(PSTR("\n")); } while (0)
#define print_val_hex16(v) do { print_P(PSTR(#v ": ")); print_hex16(v); print_P(PSTR("\n")); } while (0)
#define print_val_hex32(v) do { print_P(PSTR(#v ": ")); print_hex32(v); print_P(PSTR("\n")); } while (0)
#define print_val_bin8(v) do { print_P(PSTR(#v ": ")); print_bin8(v); print_P(PSTR("\n")); } while (0)
#define print_val_bin16(v) do { print_P(PSTR(#v ": ")); print_bin16(v); print_P(PSTR("\n")); } while (0)
#define print_val_bin32(v) do { print_P(PSTR(#v ": ")); print_bin32(v); print_P(PSTR("\n")); } while (0)
#define print_val_bin_reverse8(v) do { print_P(PSTR(#v ": ")); print_bin_reverse8(v); print_P(PSTR("\n")); } while (0)
#define print_val_bin_reverse16(v) do { print_P(PSTR(#v ": ")); print_bin_reverse16(v); print_P(PSTR("\n")); } while (0)
#define print_val_bin_reverse32(v) do { print_P(PSTR(#v ": ")); print_bin_reverse32(v); print_P(PSTR("\n")); } while (0)
#define print_val_dec(v) xprintf(#v ": %u\n", v)
#define print_val_decs(v) xprintf(#v ": %d\n", v)
#define print_val_hex8(v) xprintf(#v ": %X\n", v)
#define print_val_hex16(v) xprintf(#v ": %02X\n", v)
#define print_val_hex32(v) xprintf(#v ": %04lX\n", v)
#define print_val_bin8(v) xprintf(#v ": %08b\n", v)
#define print_val_bin16(v) xprintf(#v ": %016b\n", v)
#define print_val_bin32(v) xprintf(#v ": %032lb\n", v)
#define print_val_bin_reverse8(v) xprintf(#v ": %08b\n", bitrev(v))
#define print_val_bin_reverse16(v) xprintf(#v ": %016b\n", bitrev16(v))
#define print_val_bin_reverse32(v) xprintf(#v ": %032lb\n", bitrev32(v))
@@ -68,34 +70,46 @@
#ifdef __cplusplus
extern "C" {
#endif
/* function pointer of sendchar to be used by print utility */
void print_set_sendchar(int8_t (*print_sendchar_func)(uint8_t));
/* print string stored in data memory(SRAM) */
/* print string stored in data memory(SRAM)
* print_S("hello world");
* This consumes precious SRAM memory space for string.
*/
void print_S(const char *s);
/* print string stored in program memory(FLASH) */
void print_P(const char *s);
void print_CRLF(void);
void print_lf(void);
void print_crlf(void);
/* print string stored in program memory(FLASH)
* print_P(PSTR("hello world");
* This consumes relatively abundant FLASH memory area not SRAM.
*/
#define print_P(s) xputs(s)
/* decimal */
void print_dec(uint16_t data);
void print_decs(int16_t data);
#define print_dec(i) xprintf("%u", i)
#define print_decs(i) xprintf("%d", i)
/* hex */
void print_hex4(uint8_t data);
void print_hex8(uint8_t data);
void print_hex16(uint16_t data);
void print_hex32(uint32_t data);
#define print_hex4(i) xprintf("%X", i)
#define print_hex8(i) xprintf("%02X", i)
#define print_hex16(i) xprintf("%04X", i)
#define print_hex32(i) xprintf("%08lX", i)
/* binary */
void print_bin4(uint8_t data);
void print_bin8(uint8_t data);
void print_bin16(uint16_t data);
void print_bin32(uint32_t data);
void print_bin_reverse8(uint8_t data);
void print_bin_reverse16(uint16_t data);
void print_bin_reverse32(uint32_t data);
#define print_bin4(i) xprintf("%04b", i)
#define print_bin8(i) xprintf("%08b", i)
#define print_bin16(i) xprintf("%016b", i)
#define print_bin32(i) xprintf("%032lb", i)
#define print_bin_reverse8(i) xprintf("%08b", bitrev(i))
#define print_bin_reverse16(i) xprintf("%016b", bitrev16(i))
#define print_bin_reverse32(i) xprintf("%032lb", bitrev32(i))
#ifdef __cplusplus
}
#endif
@@ -105,7 +119,6 @@ void print_bin_reverse32(uint32_t data);
#define print_set_sendchar(func)
#define print_S(s)
#define print_P(s)
#define print_CRLF()
#define print_dec(data)
#define print_decs(data)
#define print_hex4(data)

View File

@@ -77,3 +77,25 @@ uint8_t biton32(uint32_t bits)
if (bits >> 1) { bits >>= 1; n += 1;}
return n;
}
uint8_t bitrev(uint8_t bits)
{
bits = (bits & 0x0f)<<4 | (bits & 0xf0)>>4;
bits = (bits & 0b00110011)<<2 | (bits & 0b11001100)>>2;
bits = (bits & 0b01010101)<<1 | (bits & 0b10101010)>>1;
return bits;
}
uint16_t bitrev16(uint16_t bits)
{
bits = bitrev(bits & 0x00ff)<<8 | bitrev((bits & 0xff00)>>8);
return bits;
}
uint32_t bitrev32(uint32_t bits)
{
bits = (uint32_t)bitrev16(bits & 0x0000ffff)<<16 | bitrev16((bits & 0xffff0000)>>16);
return bits;
}

View File

@@ -36,4 +36,8 @@ uint8_t biton(uint8_t bits);
uint8_t biton16(uint16_t bits);
uint8_t biton32(uint32_t bits);
uint8_t bitrev(uint8_t bits);
uint16_t bitrev16(uint16_t bits);
uint32_t bitrev32(uint32_t bits);
#endif

500
common/xprintf.S Normal file

File diff suppressed because it is too large Load Diff

103
common/xprintf.h Normal file
View File

@@ -0,0 +1,103 @@
/*---------------------------------------------------------------------------
Extended itoa, puts and printf (C)ChaN, 2011
-----------------------------------------------------------------------------*/
#ifndef XPRINTF_H
#define XPRINTF_H
#include <inttypes.h>
#include <avr/pgmspace.h>
extern void (*xfunc_out)(uint8_t);
#define xdev_out(func) xfunc_out = (void(*)(uint8_t))(func)
/* This is a pointer to user defined output function. It must be initialized
before using this modle.
*/
void xputc(char chr);
/* This is a stub function to forward outputs to user defined output function.
All outputs from this module are output via this function.
*/
/*-----------------------------------------------------------------------------*/
void xputs(const prog_char *string);
/* The string placed in the ROM is forwarded to xputc() directly.
*/
/*-----------------------------------------------------------------------------*/
void xitoa(long value, char radix, char width);
/* Extended itoa().
value radix width output
100 10 6 " 100"
100 10 -6 "000100"
100 10 0 "100"
4294967295 10 0 "4294967295"
4294967295 -10 0 "-1"
655360 16 -8 "000A0000"
1024 16 0 "400"
0x55 2 -8 "01010101"
*/
/*-----------------------------------------------------------------------------*/
#define xprintf(format, ...) __xprintf(PSTR(format), ##__VA_ARGS__)
#define xsprintf(str, format, ...) __xsprintf(str, PSTR(format), ##__VA_ARGS__)
#define xfprintf(func, format, ...) __xfprintf(func, PSTR(format), ##__VA_ARGS__)
void __xprintf(const prog_char *format, ...); /* Send formatted string to the registered device */
void __xsprintf(char*, const prog_char *format, ...); /* Put formatted string to the memory */
void __xfprintf(void(*func)(uint8_t), const prog_char *format, ...); /* Send formatted string to the specified device */
/* Format string is placed in the ROM. The format flags is similar to printf().
%[flag][width][size]type
flag
A '0' means filled with '0' when output is shorter than width.
' ' is used in default. This is effective only numeral type.
width
Minimum width in decimal number. This is effective only numeral type.
Default width is zero.
size
A 'l' means the argument is long(32bit). Default is short(16bit).
This is effective only numeral type.
type
'c' : Character, argument is the value
's' : String placed on the RAM, argument is the pointer
'S' : String placed on the ROM, argument is the pointer
'd' : Signed decimal, argument is the value
'u' : Unsigned decimal, argument is the value
'X' : Hexdecimal, argument is the value
'b' : Binary, argument is the value
'%' : '%'
*/
/*-----------------------------------------------------------------------------*/
char xatoi(char **str, long *ret);
/* Get value of the numeral string.
str
Pointer to pointer to source string
"0b11001010" binary
"0377" octal
"0xff800" hexdecimal
"1250000" decimal
"-25000" decimal
ret
Pointer to return value
*/
#endif