serial: Use const void * everywhere

Buffers should be stored in read-only areas of memory.
This commit is contained in:
Sean Cross 2014-08-19 15:54:15 +08:00
parent c869671563
commit afefdde334
3 changed files with 11 additions and 17 deletions

View file

@ -4,12 +4,12 @@
#include <stdint.h> #include <stdint.h>
int serial_putc(uint8_t c); int serial_putc(uint8_t c);
int serial_puts(void *s); int serial_puts(const void *s);
void serial_puth(uint32_t hex, int digits); /* Put hex */ void serial_puth(uint32_t hex, int digits); /* Put hex */
uint8_t serial_getc(void); uint8_t serial_getc(void);
void serial_init(void); void serial_init(void);
int serial_print_hex(void *bfr, int count); int serial_print_hex(const void *bfr, int count);
#endif /* __SERIAL_H__ */ #endif /* __SERIAL_H__ */

View file

@ -104,9 +104,9 @@ uint8_t serial_getc(void)
return uart_getreg(UART_RBR); return uart_getreg(UART_RBR);
} }
int serial_puts(void *s) int serial_puts(const void *s)
{ {
char *str = s; const char *str = s;
while(*str) { while(*str) {
if (*str == '\n') if (*str == '\n')
serial_putc('\r'); serial_putc('\r');

20
utils.c
View file

@ -28,11 +28,12 @@ void serial_puth(uint32_t pair, int digits)
serial_putc(HEX_CHARS[(pair >> 0) & 0xf]); serial_putc(HEX_CHARS[(pair >> 0) & 0xf]);
} }
int serial_print_hex_offset(uint8_t *block, int count, int offset) int serial_print_hex_offset(const void *block, int count, int offset)
{ {
int byte; int byte;
const uint8_t *b = block;
count += offset; count += offset;
block -= offset; b -= offset;
for ( ; offset < count; offset += 16) { for ( ; offset < count; offset += 16) {
serial_puth(offset, 8); serial_puth(offset, 8);
@ -41,22 +42,22 @@ int serial_print_hex_offset(uint8_t *block, int count, int offset)
serial_putc(' '); serial_putc(' ');
serial_putc(' '); serial_putc(' ');
if (offset + byte < count) if (offset + byte < count)
serial_puth(block[offset + byte] & 0xff, 2); serial_puth(b[offset + byte] & 0xff, 2);
else else
serial_puts(" "); serial_puts(" ");
} }
serial_puts(" |"); serial_puts(" |");
for (byte = 0; byte < 16 && byte + offset < count; byte++) for (byte = 0; byte < 16 && byte + offset < count; byte++)
serial_putc(_isprint(block[offset + byte]) ? serial_putc(_isprint(b[offset + byte]) ?
block[offset + byte] : b[offset + byte] :
'.'); '.');
serial_puts("|\n"); serial_puts("|\n");
} }
return 0; return 0;
} }
int serial_print_hex(void *block, int count) int serial_print_hex(const void *block, int count)
{ {
return serial_print_hex_offset(block, count, 0); return serial_print_hex_offset(block, count, 0);
} }
@ -67,10 +68,3 @@ uint32_t _udiv64(uint64_t n, uint32_t d)
return __udiv64(n >> 32, n, d); return __udiv64(n >> 32, n, d);
} }
void _memcpy(void *dst0, void *_src, int length)
{
uint8_t *ptr = dst0;
uint8_t *src = _src;
while(length--)
*ptr++ = *src++;
}