mirror of
https://github.com/elua/elua.git
synced 2025-01-25 01:02:54 +08:00
Merge branch 'master' into newrpc
This commit is contained in:
commit
47137f93a6
@ -57,7 +57,7 @@ enum
|
||||
},
|
||||
},
|
||||
|
||||
{ sig = "void* #platform_get_last_free_ram#( unsigned id );",
|
||||
{ sig = "void* #platform_get_first_free_ram#( unsigned id );",
|
||||
desc = [[Returns the start address of a free RAM area in the system (this is the RAM that will be used by any part of the code that uses malloc(),
|
||||
a good example being the Lua interpreter itself). There can be multiple free RAM areas in the system (for example the internal MCU RAM and external
|
||||
RAM chips). Implemented in $src/common.c$, it uses the the $MEM_START_ADDRESS$ macro that must be defined in the platform's $platform_conf.h$
|
||||
|
56
inc/linenoise_posix.h
Normal file
56
inc/linenoise_posix.h
Normal file
@ -0,0 +1,56 @@
|
||||
/* linenoise.h -- guerrilla line editing library against the idea that a
|
||||
* line editing lib needs to be 20,000 lines of C code.
|
||||
*
|
||||
* See linenoise.c for more information.
|
||||
*
|
||||
* ------------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
* Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __LINENOISE_H
|
||||
#define __LINENOISE_H
|
||||
|
||||
typedef struct linenoiseCompletions {
|
||||
size_t len;
|
||||
char **cvec;
|
||||
} linenoiseCompletions;
|
||||
|
||||
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
|
||||
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
|
||||
void linenoiseAddCompletion(linenoiseCompletions *, char *);
|
||||
|
||||
char *linenoise(const char *prompt);
|
||||
int linenoiseHistoryAdd(const char *line);
|
||||
int linenoiseHistorySetMaxLen(int len);
|
||||
int linenoiseHistorySave(char *filename);
|
||||
int linenoiseHistoryLoad(char *filename);
|
||||
void linenoiseClearScreen(void);
|
||||
|
||||
#endif /* __LINENOISE_H */
|
@ -16,9 +16,8 @@ if platform.system() == "Windows":
|
||||
lua_full_files += " src/serial/serial_win32.c"
|
||||
cdefs += " -DWIN32_BUILD"
|
||||
else:
|
||||
lua_full_files += " src/serial/serial_posix.c"
|
||||
external_libs += " -lreadline"
|
||||
cdefs += " -DLUA_USE_READLINE"
|
||||
lua_full_files += " src/serial/serial_posix.c src/linenoise_posix.c"
|
||||
cdefs += " -DLUA_USE_LINENOISE "
|
||||
|
||||
local_include = "-Isrc/lua -Iinc -Isrc/modules -Iinc/desktop"
|
||||
|
||||
|
612
src/linenoise_posix.c
Normal file
612
src/linenoise_posix.c
Normal file
@ -0,0 +1,612 @@
|
||||
/* linenoise.c -- guerrilla line editing library against the idea that a
|
||||
* line editing lib needs to be 20,000 lines of C code.
|
||||
*
|
||||
* You can find the latest source code at:
|
||||
*
|
||||
* http://github.com/antirez/linenoise
|
||||
*
|
||||
* Does a number of crazy assumptions that happen to be true in 99.9999% of
|
||||
* the 2010 UNIX computers around.
|
||||
*
|
||||
* ------------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
* Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* ------------------------------------------------------------------------
|
||||
*
|
||||
* References:
|
||||
* - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
|
||||
* - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
|
||||
*
|
||||
* Todo list:
|
||||
* - Switch to gets() if $TERM is something we can't support.
|
||||
* - Filter bogus Ctrl+<char> combinations.
|
||||
* - Win32 support
|
||||
*
|
||||
* Bloat:
|
||||
* - Completion?
|
||||
* - History search like Ctrl+r in readline?
|
||||
*
|
||||
* List of escape sequences used by this program, we do everything just
|
||||
* with three sequences. In order to be so cheap we may have some
|
||||
* flickering effect with some slow terminal, but the lesser sequences
|
||||
* the more compatible.
|
||||
*
|
||||
* CHA (Cursor Horizontal Absolute)
|
||||
* Sequence: ESC [ n G
|
||||
* Effect: moves cursor to column n
|
||||
*
|
||||
* EL (Erase Line)
|
||||
* Sequence: ESC [ n K
|
||||
* Effect: if n is 0 or missing, clear from cursor to end of line
|
||||
* Effect: if n is 1, clear from beginning of line to cursor
|
||||
* Effect: if n is 2, clear entire line
|
||||
*
|
||||
* CUF (CUrsor Forward)
|
||||
* Sequence: ESC [ n C
|
||||
* Effect: moves cursor forward of n chars
|
||||
*
|
||||
* The following are used to clear the screen: ESC [ H ESC [ 2 J
|
||||
* This is actually composed of two sequences:
|
||||
*
|
||||
* cursorhome
|
||||
* Sequence: ESC [ H
|
||||
* Effect: moves the cursor to upper left corner
|
||||
*
|
||||
* ED2 (Clear entire screen)
|
||||
* Sequence: ESC [ 2 J
|
||||
* Effect: clear the whole screen
|
||||
*
|
||||
*/
|
||||
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include "linenoise_posix.h"
|
||||
|
||||
#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
|
||||
#define LINENOISE_MAX_LINE 4096
|
||||
static char *unsupported_term[] = {"dumb","cons25",NULL};
|
||||
static linenoiseCompletionCallback *completionCallback = NULL;
|
||||
|
||||
static struct termios orig_termios; /* in order to restore at exit */
|
||||
static int rawmode = 0; /* for atexit() function to check if restore is needed*/
|
||||
static int atexit_registered = 0; /* register atexit just 1 time */
|
||||
static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
|
||||
static int history_len = 0;
|
||||
char **history = NULL;
|
||||
|
||||
static void linenoiseAtExit(void);
|
||||
int linenoiseHistoryAdd(const char *line);
|
||||
|
||||
static int isUnsupportedTerm(void) {
|
||||
char *term = getenv("TERM");
|
||||
int j;
|
||||
|
||||
if (term == NULL) return 0;
|
||||
for (j = 0; unsupported_term[j]; j++)
|
||||
if (!strcasecmp(term,unsupported_term[j])) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void freeHistory(void) {
|
||||
if (history) {
|
||||
int j;
|
||||
|
||||
for (j = 0; j < history_len; j++)
|
||||
free(history[j]);
|
||||
free(history);
|
||||
}
|
||||
}
|
||||
|
||||
static int enableRawMode(int fd) {
|
||||
struct termios raw;
|
||||
|
||||
if (!isatty(STDIN_FILENO)) goto fatal;
|
||||
if (!atexit_registered) {
|
||||
atexit(linenoiseAtExit);
|
||||
atexit_registered = 1;
|
||||
}
|
||||
if (tcgetattr(fd,&orig_termios) == -1) goto fatal;
|
||||
|
||||
raw = orig_termios; /* modify the original mode */
|
||||
/* input modes: no break, no CR to NL, no parity check, no strip char,
|
||||
* no start/stop output control. */
|
||||
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
||||
/* output modes - disable post processing */
|
||||
raw.c_oflag &= ~(OPOST);
|
||||
/* control modes - set 8 bit chars */
|
||||
raw.c_cflag |= (CS8);
|
||||
/* local modes - choing off, canonical off, no extended functions,
|
||||
* no signal chars (^Z,^C) */
|
||||
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
||||
/* control chars - set return condition: min number of bytes and timer.
|
||||
* We want read to return every single byte, without timeout. */
|
||||
raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
|
||||
|
||||
/* put terminal in raw mode after flushing */
|
||||
if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal;
|
||||
rawmode = 1;
|
||||
return 0;
|
||||
|
||||
fatal:
|
||||
errno = ENOTTY;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void disableRawMode(int fd) {
|
||||
/* Don't even check the return value as it's too late. */
|
||||
if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1)
|
||||
rawmode = 0;
|
||||
}
|
||||
|
||||
/* At exit we'll try to fix the terminal to the initial conditions. */
|
||||
static void linenoiseAtExit(void) {
|
||||
disableRawMode(STDIN_FILENO);
|
||||
freeHistory();
|
||||
}
|
||||
|
||||
static int getColumns(void) {
|
||||
struct winsize ws;
|
||||
|
||||
if (ioctl(1, TIOCGWINSZ, &ws) == -1) return 80;
|
||||
return ws.ws_col;
|
||||
}
|
||||
|
||||
static void refreshLine(int fd, const char *prompt, char *buf, size_t len, size_t pos, size_t cols) {
|
||||
char seq[64];
|
||||
size_t plen = strlen(prompt);
|
||||
|
||||
while((plen+pos) >= cols) {
|
||||
buf++;
|
||||
len--;
|
||||
pos--;
|
||||
}
|
||||
while (plen+len > cols) {
|
||||
len--;
|
||||
}
|
||||
|
||||
/* Cursor to left edge */
|
||||
snprintf(seq,64,"\x1b[0G");
|
||||
if (write(fd,seq,strlen(seq)) == -1) return;
|
||||
/* Write the prompt and the current buffer content */
|
||||
if (write(fd,prompt,strlen(prompt)) == -1) return;
|
||||
if (write(fd,buf,len) == -1) return;
|
||||
/* Erase to right */
|
||||
snprintf(seq,64,"\x1b[0K");
|
||||
if (write(fd,seq,strlen(seq)) == -1) return;
|
||||
/* Move cursor to original position. */
|
||||
snprintf(seq,64,"\x1b[0G\x1b[%dC", (int)(pos+plen));
|
||||
if (write(fd,seq,strlen(seq)) == -1) return;
|
||||
}
|
||||
|
||||
static void beep() {
|
||||
fprintf(stderr, "\x7");
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
static void freeCompletions(linenoiseCompletions *lc) {
|
||||
size_t i;
|
||||
for (i = 0; i < lc->len; i++)
|
||||
free(lc->cvec[i]);
|
||||
if (lc->cvec != NULL)
|
||||
free(lc->cvec);
|
||||
}
|
||||
|
||||
static int completeLine(int fd, const char *prompt, char *buf, size_t buflen, size_t *len, size_t *pos, size_t cols) {
|
||||
linenoiseCompletions lc = { 0, NULL };
|
||||
int nread, nwritten;
|
||||
char c = 0;
|
||||
|
||||
completionCallback(buf,&lc);
|
||||
if (lc.len == 0) {
|
||||
beep();
|
||||
} else {
|
||||
size_t stop = 0, i = 0;
|
||||
size_t clen;
|
||||
|
||||
while(!stop) {
|
||||
/* Show completion or original buffer */
|
||||
if (i < lc.len) {
|
||||
clen = strlen(lc.cvec[i]);
|
||||
refreshLine(fd,prompt,lc.cvec[i],clen,clen,cols);
|
||||
} else {
|
||||
refreshLine(fd,prompt,buf,*len,*pos,cols);
|
||||
}
|
||||
|
||||
nread = read(fd,&c,1);
|
||||
if (nread <= 0) {
|
||||
freeCompletions(&lc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch(c) {
|
||||
case 9: /* tab */
|
||||
i = (i+1) % (lc.len+1);
|
||||
if (i == lc.len) beep();
|
||||
break;
|
||||
case 27: /* escape */
|
||||
/* Re-show original buffer */
|
||||
if (i < lc.len) {
|
||||
refreshLine(fd,prompt,buf,*len,*pos,cols);
|
||||
}
|
||||
stop = 1;
|
||||
break;
|
||||
default:
|
||||
/* Update buffer and return */
|
||||
if (i < lc.len) {
|
||||
nwritten = snprintf(buf,buflen,"%s",lc.cvec[i]);
|
||||
*len = *pos = nwritten;
|
||||
}
|
||||
stop = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
freeCompletions(&lc);
|
||||
return c; /* Return last read character */
|
||||
}
|
||||
|
||||
void linenoiseClearScreen(void) {
|
||||
if (write(STDIN_FILENO,"\x1b[H\x1b[2J",7) <= 0) {
|
||||
/* nothing to do, just to avoid warning. */
|
||||
}
|
||||
}
|
||||
|
||||
static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt) {
|
||||
size_t plen = strlen(prompt);
|
||||
size_t pos = 0;
|
||||
size_t len = 0;
|
||||
size_t cols = getColumns();
|
||||
int history_index = 0;
|
||||
|
||||
buf[0] = '\0';
|
||||
buflen--; /* Make sure there is always space for the nulterm */
|
||||
|
||||
/* The latest history entry is always our current buffer, that
|
||||
* initially is just an empty string. */
|
||||
linenoiseHistoryAdd("");
|
||||
|
||||
if (write(fd,prompt,plen) == -1) return -1;
|
||||
while(1) {
|
||||
char c;
|
||||
int nread;
|
||||
char seq[2], seq2[2];
|
||||
|
||||
nread = read(fd,&c,1);
|
||||
if (nread <= 0) return len;
|
||||
|
||||
/* Only autocomplete when the callback is set. It returns < 0 when
|
||||
* there was an error reading from fd. Otherwise it will return the
|
||||
* character that should be handled next. */
|
||||
if (c == 9 && completionCallback != NULL) {
|
||||
c = completeLine(fd,prompt,buf,buflen,&len,&pos,cols);
|
||||
/* Return on errors */
|
||||
if (c < 0) return len;
|
||||
/* Read next character when 0 */
|
||||
if (c == 0) continue;
|
||||
}
|
||||
|
||||
switch(c) {
|
||||
case 13: /* enter */
|
||||
history_len--;
|
||||
free(history[history_len]);
|
||||
return (int)len;
|
||||
case 3: /* ctrl-c */
|
||||
errno = EAGAIN;
|
||||
return -1;
|
||||
case 127: /* backspace */
|
||||
case 8: /* ctrl-h */
|
||||
if (pos > 0 && len > 0) {
|
||||
memmove(buf+pos-1,buf+pos,len-pos);
|
||||
pos--;
|
||||
len--;
|
||||
buf[len] = '\0';
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
}
|
||||
break;
|
||||
case 4: /* ctrl-d, remove char at right of cursor */
|
||||
if (len > 1 && pos < (len-1)) {
|
||||
memmove(buf+pos,buf+pos+1,len-pos);
|
||||
len--;
|
||||
buf[len] = '\0';
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
} else if (len == 0) {
|
||||
history_len--;
|
||||
free(history[history_len]);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case 20: /* ctrl-t */
|
||||
if (pos > 0 && pos < len) {
|
||||
int aux = buf[pos-1];
|
||||
buf[pos-1] = buf[pos];
|
||||
buf[pos] = aux;
|
||||
if (pos != len-1) pos++;
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
}
|
||||
break;
|
||||
case 2: /* ctrl-b */
|
||||
goto left_arrow;
|
||||
case 6: /* ctrl-f */
|
||||
goto right_arrow;
|
||||
case 16: /* ctrl-p */
|
||||
seq[1] = 65;
|
||||
goto up_down_arrow;
|
||||
case 14: /* ctrl-n */
|
||||
seq[1] = 66;
|
||||
goto up_down_arrow;
|
||||
break;
|
||||
case 27: /* escape sequence */
|
||||
if (read(fd,seq,2) == -1) break;
|
||||
if (seq[0] == 91 && seq[1] == 68) {
|
||||
left_arrow:
|
||||
/* left arrow */
|
||||
if (pos > 0) {
|
||||
pos--;
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
}
|
||||
} else if (seq[0] == 91 && seq[1] == 67) {
|
||||
right_arrow:
|
||||
/* right arrow */
|
||||
if (pos != len) {
|
||||
pos++;
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
}
|
||||
} else if (seq[0] == 91 && (seq[1] == 65 || seq[1] == 66)) {
|
||||
up_down_arrow:
|
||||
/* up and down arrow: history */
|
||||
if (history_len > 1) {
|
||||
/* Update the current history entry before to
|
||||
* overwrite it with tne next one. */
|
||||
free(history[history_len-1-history_index]);
|
||||
history[history_len-1-history_index] = strdup(buf);
|
||||
/* Show the new entry */
|
||||
history_index += (seq[1] == 65) ? 1 : -1;
|
||||
if (history_index < 0) {
|
||||
history_index = 0;
|
||||
break;
|
||||
} else if (history_index >= history_len) {
|
||||
history_index = history_len-1;
|
||||
break;
|
||||
}
|
||||
strncpy(buf,history[history_len-1-history_index],buflen);
|
||||
buf[buflen] = '\0';
|
||||
len = pos = strlen(buf);
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
}
|
||||
} else if (seq[0] == 91 && seq[1] > 48 && seq[1] < 55) {
|
||||
/* extended escape */
|
||||
if (read(fd,seq2,2) == -1) break;
|
||||
if (seq[1] == 51 && seq2[0] == 126) {
|
||||
/* delete */
|
||||
if (len > 0 && pos < len) {
|
||||
memmove(buf+pos,buf+pos+1,len-pos-1);
|
||||
len--;
|
||||
buf[len] = '\0';
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (len < buflen) {
|
||||
if (len == pos) {
|
||||
buf[pos] = c;
|
||||
pos++;
|
||||
len++;
|
||||
buf[len] = '\0';
|
||||
if (plen+len < cols) {
|
||||
/* Avoid a full update of the line in the
|
||||
* trivial case. */
|
||||
if (write(fd,&c,1) == -1) return -1;
|
||||
} else {
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
}
|
||||
} else {
|
||||
memmove(buf+pos+1,buf+pos,len-pos);
|
||||
buf[pos] = c;
|
||||
len++;
|
||||
pos++;
|
||||
buf[len] = '\0';
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 21: /* Ctrl+u, delete the whole line. */
|
||||
buf[0] = '\0';
|
||||
pos = len = 0;
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
break;
|
||||
case 11: /* Ctrl+k, delete from current to end of line. */
|
||||
buf[pos] = '\0';
|
||||
len = pos;
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
break;
|
||||
case 1: /* Ctrl+a, go to the start of the line */
|
||||
pos = 0;
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
break;
|
||||
case 5: /* ctrl+e, go to the end of the line */
|
||||
pos = len;
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
break;
|
||||
case 12: /* ctrl+l, clear screen */
|
||||
linenoiseClearScreen();
|
||||
refreshLine(fd,prompt,buf,len,pos,cols);
|
||||
}
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) {
|
||||
int fd = STDIN_FILENO;
|
||||
int count;
|
||||
|
||||
if (buflen == 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (!isatty(STDIN_FILENO)) {
|
||||
if (fgets(buf, buflen, stdin) == NULL) return -1;
|
||||
count = strlen(buf);
|
||||
if (count && buf[count-1] == '\n') {
|
||||
count--;
|
||||
buf[count] = '\0';
|
||||
}
|
||||
} else {
|
||||
if (enableRawMode(fd) == -1) return -1;
|
||||
count = linenoisePrompt(fd, buf, buflen, prompt);
|
||||
disableRawMode(fd);
|
||||
printf("\n");
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
char *linenoise(const char *prompt) {
|
||||
char buf[LINENOISE_MAX_LINE];
|
||||
int count;
|
||||
|
||||
if (isUnsupportedTerm()) {
|
||||
size_t len;
|
||||
|
||||
printf("%s",prompt);
|
||||
fflush(stdout);
|
||||
if (fgets(buf,LINENOISE_MAX_LINE,stdin) == NULL) return NULL;
|
||||
len = strlen(buf);
|
||||
while(len && (buf[len-1] == '\n' || buf[len-1] == '\r')) {
|
||||
len--;
|
||||
buf[len] = '\0';
|
||||
}
|
||||
return strdup(buf);
|
||||
} else {
|
||||
count = linenoiseRaw(buf,LINENOISE_MAX_LINE,prompt);
|
||||
if (count == -1) return NULL;
|
||||
return strdup(buf);
|
||||
}
|
||||
}
|
||||
|
||||
/* Register a callback function to be called for tab-completion. */
|
||||
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) {
|
||||
completionCallback = fn;
|
||||
}
|
||||
|
||||
void linenoiseAddCompletion(linenoiseCompletions *lc, char *str) {
|
||||
size_t len = strlen(str);
|
||||
char *copy = malloc(len+1);
|
||||
memcpy(copy,str,len+1);
|
||||
lc->cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1));
|
||||
lc->cvec[lc->len++] = copy;
|
||||
}
|
||||
|
||||
/* Using a circular buffer is smarter, but a bit more complex to handle. */
|
||||
int linenoiseHistoryAdd(const char *line) {
|
||||
char *linecopy;
|
||||
|
||||
if (history_max_len == 0) return 0;
|
||||
if (history == NULL) {
|
||||
history = malloc(sizeof(char*)*history_max_len);
|
||||
if (history == NULL) return 0;
|
||||
memset(history,0,(sizeof(char*)*history_max_len));
|
||||
}
|
||||
linecopy = strdup(line);
|
||||
if (!linecopy) return 0;
|
||||
if (history_len == history_max_len) {
|
||||
free(history[0]);
|
||||
memmove(history,history+1,sizeof(char*)*(history_max_len-1));
|
||||
history_len--;
|
||||
}
|
||||
history[history_len] = linecopy;
|
||||
history_len++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int linenoiseHistorySetMaxLen(int len) {
|
||||
char **new;
|
||||
|
||||
if (len < 1) return 0;
|
||||
if (history) {
|
||||
int tocopy = history_len;
|
||||
|
||||
new = malloc(sizeof(char*)*len);
|
||||
if (new == NULL) return 0;
|
||||
if (len < tocopy) tocopy = len;
|
||||
memcpy(new,history+(history_max_len-tocopy), sizeof(char*)*tocopy);
|
||||
free(history);
|
||||
history = new;
|
||||
}
|
||||
history_max_len = len;
|
||||
if (history_len > history_max_len)
|
||||
history_len = history_max_len;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Save the history in the specified file. On success 0 is returned
|
||||
* otherwise -1 is returned. */
|
||||
int linenoiseHistorySave(char *filename) {
|
||||
FILE *fp = fopen(filename,"w");
|
||||
int j;
|
||||
|
||||
if (fp == NULL) return -1;
|
||||
for (j = 0; j < history_len; j++)
|
||||
fprintf(fp,"%s\n",history[j]);
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Load the history from the specified file. If the file does not exist
|
||||
* zero is returned and no operation is performed.
|
||||
*
|
||||
* If the file exists and the operation succeeded 0 is returned, otherwise
|
||||
* on error -1 is returned. */
|
||||
int linenoiseHistoryLoad(char *filename) {
|
||||
FILE *fp = fopen(filename,"r");
|
||||
char buf[LINENOISE_MAX_LINE];
|
||||
|
||||
if (fp == NULL) return -1;
|
||||
|
||||
while (fgets(buf,LINENOISE_MAX_LINE,fp) != NULL) {
|
||||
char *p;
|
||||
|
||||
p = strchr(buf,'\r');
|
||||
if (!p) p = strchr(buf,'\n');
|
||||
if (p) *p = '\0';
|
||||
linenoiseHistoryAdd(buf);
|
||||
}
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
@ -51,7 +51,7 @@ static void MaybeByteSwap(char *number, size_t numbersize, DumpState *D)
|
||||
int platform_little_endian = *(char*)&x;
|
||||
if (platform_little_endian != D->target.little_endian)
|
||||
{
|
||||
int i;
|
||||
unsigned long i;
|
||||
for (i=0; i<numbersize/2; i++)
|
||||
{
|
||||
char temp = number[i];
|
||||
@ -91,7 +91,7 @@ static void DumpInt(int x, DumpState* D)
|
||||
DumpIntWithSize(x,D->target.sizeof_int,D);
|
||||
}
|
||||
|
||||
static void DumpSize(int32_t x, DumpState* D)
|
||||
static void DumpSize(uint32_t x, DumpState* D)
|
||||
{
|
||||
/* dump unsigned integer */
|
||||
switch(D->target.sizeof_strsize_t) {
|
||||
@ -179,7 +179,7 @@ static void DumpString(const TString* s, DumpState* D)
|
||||
}
|
||||
else
|
||||
{
|
||||
strsize_t size=s->tsv.len+1; /* include trailing '\0' */
|
||||
strsize_t size=( strsize_t )s->tsv.len+1; /* include trailing '\0' */
|
||||
DumpSize(size,D);
|
||||
DumpBlock(getstr(s),size,D);
|
||||
}
|
||||
|
@ -300,6 +300,13 @@
|
||||
if (lua_strlen(L,idx) > 0) /* non-empty line? */ \
|
||||
add_history(lua_tostring(L, idx)); /* add it to history */
|
||||
#define lua_freeline(L,b) ((void)L, free(b))
|
||||
#elif defined(LUA_USE_LINENOISE) // #if defined(LUA_USE_READLINE)
|
||||
#include "linenoise_posix.h"
|
||||
#define lua_readline(L,b,p) ((void)L, ((b)=linenoise(p)) != NULL)
|
||||
#define lua_saveline(L,idx) \
|
||||
if (lua_strlen(L,idx) > 0) /* non-empty line? */ \
|
||||
linenoiseHistoryAdd(lua_tostring(L, idx)); /* add it to history */
|
||||
#define lua_freeline(L,b) ((void)L, free(b))
|
||||
#else // #if defined(LUA_USE_READLINE)
|
||||
#define lua_readline(L,b,p) \
|
||||
((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
|
||||
|
@ -100,7 +100,7 @@ int transport_get_char(Transport *t)
|
||||
// Read & Write to Transport
|
||||
void transport_read_buffer (Transport *tpt, u8 *buffer, int length)
|
||||
{
|
||||
u32 n;
|
||||
int n;
|
||||
struct exception e;
|
||||
TRANSPORT_VERIFY_OPEN;
|
||||
|
||||
|
@ -4,9 +4,11 @@
|
||||
|
||||
// Modified by BogdanM for eLua
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "auxmods.h"
|
||||
#include "type.h"
|
||||
#include "lrotable.h"
|
||||
|
@ -45,7 +45,7 @@ static int bitarray_new( lua_State *L )
|
||||
if( lua_isnumber( L, 1 ) )
|
||||
{
|
||||
// capacity, [element_size_bits], [fill]
|
||||
capacity = luaL_checkinteger( L, 1 );
|
||||
capacity = ( u32 )luaL_checkinteger( L, 1 );
|
||||
if( lua_isnumber( L, 2 ) )
|
||||
elsize = luaL_checkinteger( L, 2 );
|
||||
else
|
||||
@ -69,7 +69,7 @@ static int bitarray_new( lua_State *L )
|
||||
elsize = 8;
|
||||
if( ( temp << 3 ) % elsize )
|
||||
return luaL_error( L, "length is not a multiple of element size." );
|
||||
capacity = ( temp << 3 ) / elsize;
|
||||
capacity = ( u32 )( temp << 3 ) / elsize;
|
||||
}
|
||||
else
|
||||
return luaL_error( L, "invalid arguments." );
|
||||
@ -141,7 +141,7 @@ static int bitarray_get( lua_State *L )
|
||||
u32 idx;
|
||||
|
||||
pa = bitarray_check( L );
|
||||
idx = luaL_checkinteger( L, 2 );
|
||||
idx = ( u32 )luaL_checkinteger( L, 2 );
|
||||
if( ( idx <= 0 ) || ( idx > pa->capacity ) )
|
||||
return luaL_error( L, "invalid index." );
|
||||
lua_pushinteger( L, bitarray_getval( pa, idx ) );
|
||||
@ -156,8 +156,8 @@ static int bitarray_set( lua_State *L )
|
||||
u8 rest, mask;
|
||||
|
||||
pa = bitarray_check( L );
|
||||
idx = luaL_checkinteger( L, 2 );
|
||||
newval = luaL_checkinteger( L, 3 );
|
||||
idx = ( u32 )luaL_checkinteger( L, 2 );
|
||||
newval = ( u32 )luaL_checkinteger( L, 3 );
|
||||
if( ( idx <= 0 ) || ( idx > pa->capacity ) )
|
||||
return luaL_error( L, "invalid index." );
|
||||
idx --;
|
||||
@ -206,7 +206,7 @@ static int bitarray_iter( lua_State *L )
|
||||
u32 idx;
|
||||
|
||||
pa = bitarray_check( L );
|
||||
idx = luaL_checkinteger( L, 2 ) + 1;
|
||||
idx = ( u32 )luaL_checkinteger( L, 2 ) + 1;
|
||||
if( idx <= pa->capacity )
|
||||
{
|
||||
lua_pushinteger( L, idx );
|
||||
|
@ -62,7 +62,7 @@ static void doswap(int swap, void *p, size_t n)
|
||||
{
|
||||
char *a=p;
|
||||
int i,j;
|
||||
for (i=0, j=n-1, n=n/2; n--; i++, j--)
|
||||
for (i=0, j=( int )n-1, n=n/2; n--; i++, j--)
|
||||
{
|
||||
char t=a[i]; a[i]=a[j]; a[j]=t;
|
||||
}
|
||||
@ -74,7 +74,7 @@ static void doswap(int swap, void *p, size_t n)
|
||||
{ \
|
||||
T a; \
|
||||
int m=sizeof(a); \
|
||||
if (i+m>len) goto done; \
|
||||
if (((unsigned long)i+m)>len) goto done; \
|
||||
memcpy(&a,s+i,m); \
|
||||
i+=m; \
|
||||
doswap(swap,&a,m); \
|
||||
@ -88,10 +88,10 @@ static void doswap(int swap, void *p, size_t n)
|
||||
{ \
|
||||
T l; \
|
||||
int m=sizeof(l); \
|
||||
if (i+m>len) goto done; \
|
||||
if (((unsigned long)i+m)>len) goto done; \
|
||||
memcpy(&l,s+i,m); \
|
||||
doswap(swap,&l,m); \
|
||||
if (i+m+l>len) goto done; \
|
||||
if (((unsigned long)i+m+l)>len) goto done; \
|
||||
i+=m; \
|
||||
lua_pushlstring(L,s+i,l); \
|
||||
i+=l; \
|
||||
@ -131,7 +131,7 @@ static int l_unpack(lua_State *L) /** unpack(s,f,[init]) */
|
||||
case OP_STRING:
|
||||
{
|
||||
++N;
|
||||
if (i+N>len) goto done;
|
||||
if (((unsigned long)i+N)>len) goto done;
|
||||
lua_pushlstring(L,s+i,N);
|
||||
i+=N;
|
||||
++n;
|
||||
@ -141,7 +141,7 @@ static int l_unpack(lua_State *L) /** unpack(s,f,[init]) */
|
||||
case OP_ZSTRING:
|
||||
{
|
||||
size_t l;
|
||||
if (i>=len) goto done;
|
||||
if (((unsigned long)i)>=len) goto done;
|
||||
l=strlen(s+i);
|
||||
lua_pushlstring(L,s+i,l);
|
||||
i+=l+1;
|
||||
|
@ -176,7 +176,7 @@ static void transport_write_u8( Transport *tpt, u8 x )
|
||||
|
||||
static void swap_bytes( uint8_t *number, size_t numbersize )
|
||||
{
|
||||
int i;
|
||||
u32 i;
|
||||
for ( i = 0 ; i < numbersize / 2 ; i ++ )
|
||||
{
|
||||
uint8_t temp = number[ i ];
|
||||
@ -218,7 +218,7 @@ static void transport_write_u32( Transport *tpt, u32 x )
|
||||
// read a lua number from the transport
|
||||
static lua_Number transport_read_number( Transport *tpt )
|
||||
{
|
||||
lua_Number x;
|
||||
lua_Number x = 0;
|
||||
u8 b[ tpt->lnum_bytes ];
|
||||
struct exception e;
|
||||
TRANSPORT_VERIFY_OPEN;
|
||||
@ -449,7 +449,7 @@ static void write_variable( Transport *tpt, lua_State *L, int var_index )
|
||||
u32 len;
|
||||
transport_write_u8( tpt, RPC_STRING );
|
||||
s = lua_tostring( L, var_index );
|
||||
len = lua_strlen( L, var_index );
|
||||
len = ( u32 )lua_strlen( L, var_index );
|
||||
transport_write_u32( tpt, len );
|
||||
transport_write_string( tpt, s, len );
|
||||
break;
|
||||
@ -493,7 +493,7 @@ static void write_variable( Transport *tpt, lua_State *L, int var_index )
|
||||
luaL_error( L, "light userdata transmission unsupported" );
|
||||
break;
|
||||
}
|
||||
MYASSERT( lua_gettop( L ) == stack_at_start );
|
||||
lua_assert( lua_gettop( L ) == stack_at_start );
|
||||
}
|
||||
|
||||
|
||||
@ -633,7 +633,7 @@ static void client_negotiate( Transport *tpt )
|
||||
header[1] = 'R';
|
||||
header[2] = 'P';
|
||||
header[3] = 'C';
|
||||
header[4] = RPC_PROTOCOL_VERSION;
|
||||
header[4] = ( char )RPC_PROTOCOL_VERSION;
|
||||
header[5] = tpt->loc_little;
|
||||
header[6] = tpt->lnum_bytes;
|
||||
header[7] = tpt->loc_intnum;
|
||||
@ -780,7 +780,7 @@ static int handle_index (lua_State *L)
|
||||
const char *s;
|
||||
|
||||
check_num_args( L, 2 );
|
||||
MYASSERT( lua_isuserdata( L, 1 ) && ismetatable_type( L, 1, "rpc.handle" ) );
|
||||
lua_assert( lua_isuserdata( L, 1 ) && ismetatable_type( L, 1, "rpc.handle" ) );
|
||||
|
||||
if( lua_type( L, 2 ) != LUA_TSTRING )
|
||||
return luaL_error( L, "can't index a handle with a non-string" );
|
||||
@ -802,7 +802,7 @@ static int handle_newindex( lua_State *L )
|
||||
const char *s;
|
||||
|
||||
check_num_args( L, 3 );
|
||||
MYASSERT( lua_isuserdata( L, 1 ) && ismetatable_type( L, 1, "rpc.handle" ) );
|
||||
lua_assert( lua_isuserdata( L, 1 ) && ismetatable_type( L, 1, "rpc.handle" ) );
|
||||
|
||||
if( lua_type( L, 2 ) != LUA_TSTRING )
|
||||
return luaL_error( L, "can't index handle with a non-string" );
|
||||
@ -821,12 +821,13 @@ static int handle_newindex( lua_State *L )
|
||||
// replays series of indexes to remote side as a string
|
||||
static void helper_remote_index( Helper *helper )
|
||||
{
|
||||
int i, len;
|
||||
int i;
|
||||
u32 len;
|
||||
Helper **hstack;
|
||||
Transport *tpt = &helper->handle->tpt;
|
||||
|
||||
// get length of name & make stack of helpers
|
||||
len = strlen( helper->funcname );
|
||||
len = ( u32 )strlen( helper->funcname );
|
||||
if( helper->nparents > 0 ) // If helper has parents, build string to remote index
|
||||
{
|
||||
hstack = ( Helper ** )alloca( sizeof( Helper * ) * helper->nparents );
|
||||
@ -844,14 +845,14 @@ static void helper_remote_index( Helper *helper )
|
||||
// replay helper key names
|
||||
for( i = 0 ; i < helper->nparents ; i ++ )
|
||||
{
|
||||
transport_write_string( tpt, hstack[ i ]->funcname, strlen( hstack[ i ]->funcname ) );
|
||||
transport_write_string( tpt, hstack[ i ]->funcname, ( int )strlen( hstack[ i ]->funcname ) );
|
||||
transport_write_string( tpt, ".", 1 );
|
||||
}
|
||||
}
|
||||
else // If helper has no parents, just use length of global
|
||||
transport_write_u32( tpt, len );
|
||||
|
||||
transport_write_string( tpt, helper->funcname, strlen( helper->funcname ) );
|
||||
transport_write_string( tpt, helper->funcname, ( int )strlen( helper->funcname ) );
|
||||
}
|
||||
|
||||
static void helper_wait_ready( Transport *tpt, u8 cmd )
|
||||
@ -1073,7 +1074,7 @@ static int helper_index( lua_State *L )
|
||||
const char *s;
|
||||
|
||||
check_num_args( L, 2 );
|
||||
MYASSERT( lua_isuserdata( L, 1 ) && ismetatable_type( L, 1, "rpc.helper" ) );
|
||||
lua_assert( lua_isuserdata( L, 1 ) && ismetatable_type( L, 1, "rpc.helper" ) );
|
||||
|
||||
if( lua_type( L, 2 ) != LUA_TSTRING )
|
||||
return luaL_error( L, "can't index handle with non-string" );
|
||||
@ -1260,13 +1261,13 @@ static void read_cmd_call( Transport *tpt, lua_State *L )
|
||||
// handle errors
|
||||
if ( error_code )
|
||||
{
|
||||
size_t len;
|
||||
size_t elen;
|
||||
const char *errmsg;
|
||||
errmsg = lua_tolstring (L, -1, &len);
|
||||
errmsg = lua_tolstring( L, -1, &elen );
|
||||
transport_write_u8( tpt, 1 );
|
||||
transport_write_u32( tpt, error_code );
|
||||
transport_write_u32( tpt, len );
|
||||
transport_write_string( tpt, errmsg, len );
|
||||
transport_write_u32( tpt, ( u32 )elen );
|
||||
transport_write_string( tpt, errmsg, ( int )elen );
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -1282,11 +1283,11 @@ static void read_cmd_call( Transport *tpt, lua_State *L )
|
||||
{
|
||||
// bad function
|
||||
const char *msg = "undefined function: ";
|
||||
int errlen = strlen( msg ) + len;
|
||||
int errlen = ( int )strlen( msg ) + len;
|
||||
transport_write_u8( tpt, 1 );
|
||||
transport_write_u32( tpt, LUA_ERRRUN );
|
||||
transport_write_u32( tpt, errlen );
|
||||
transport_write_string( tpt, msg, strlen( msg ) );
|
||||
transport_write_string( tpt, msg, ( int )strlen( msg ) );
|
||||
transport_write_string( tpt, funcname, len );
|
||||
}
|
||||
// empty the stack
|
||||
|
@ -81,7 +81,7 @@
|
||||
#define NETLINE
|
||||
#endif
|
||||
|
||||
#if defined( BUILD_RPC )
|
||||
#if defined( BUILD_RPC )
|
||||
#define RPCLINE _ROM( AUXLIB_RPC, luaopen_rpc, rpc_map )
|
||||
#else
|
||||
#define RPCLINE
|
||||
@ -128,8 +128,8 @@
|
||||
#else
|
||||
#define NUM_TIMER 3
|
||||
#endif
|
||||
#define NUM_PWM 7 // PWM7 is on GPIO50
|
||||
#define NUM_ADC 8 // Though ADC3 pin is the Ethernet IRQ
|
||||
#define NUM_PWM 7 // PWM7 is on GPIO50
|
||||
#define NUM_ADC 8 // Though ADC3 pin is the Ethernet IRQ
|
||||
#define NUM_CAN 0
|
||||
|
||||
// As flow control seems not to work, we use a large buffer so that people
|
||||
|
@ -789,7 +789,7 @@ __attribute__((__interrupt__)) void vMACB_ISR(void)
|
||||
// Variable definitions can be made now.
|
||||
volatile unsigned long ulIntStatus, ulEventStatus;
|
||||
|
||||
// Find the cause of the interrupt.
|
||||
// Find the cause of the interrupt.
|
||||
ulIntStatus = AVR32_MACB.isr;
|
||||
ulEventStatus = AVR32_MACB.rsr;
|
||||
|
||||
|
@ -44,8 +44,8 @@
|
||||
// UIP sys tick data
|
||||
// NOTE: when using virtual timers, SYSTICKHZ and VTMR_FREQ_HZ should have the
|
||||
// same value, as they're served by the same timer (the systick)
|
||||
#define SYSTICKHZ 4
|
||||
#define SYSTICKMS (1000 / SYSTICKHZ)
|
||||
#define SYSTICKHZ 4
|
||||
#define SYSTICKMS (1000 / SYSTICKHZ)
|
||||
|
||||
#ifdef BUILD_UIP
|
||||
static int eth_timer_fired;
|
||||
@ -54,7 +54,7 @@ static int eth_timer_fired;
|
||||
// ****************************************************************************
|
||||
// Platform initialization
|
||||
#ifdef BUILD_UIP
|
||||
u32 platform_ethernet_setup(void);
|
||||
u32 platform_ethernet_setup( void );
|
||||
#endif
|
||||
|
||||
extern int pm_configure_clocks( pm_freq_param_t *param );
|
||||
@ -88,7 +88,7 @@ __attribute__((__interrupt__)) static void tmr_int_handler()
|
||||
// of incrementing the timers and taking the appropriate actions.
|
||||
platform_eth_force_interrupt();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
const u32 uart_base_addr[ ] = {
|
||||
@ -870,8 +870,8 @@ static const gpio_map_t pwm_pins =
|
||||
{ AVR32_PWM_1_PIN, AVR32_PWM_1_FUNCTION },
|
||||
{ AVR32_PWM_2_PIN, AVR32_PWM_2_FUNCTION },
|
||||
{ AVR32_PWM_3_PIN, AVR32_PWM_3_FUNCTION },
|
||||
{ AVR32_PWM_4_1_PIN, AVR32_PWM_4_1_FUNCTION }, // PB27
|
||||
{ AVR32_PWM_5_1_PIN, AVR32_PWM_5_1_FUNCTION }, // PB28
|
||||
{ AVR32_PWM_4_1_PIN, AVR32_PWM_4_1_FUNCTION }, // PB27
|
||||
{ AVR32_PWM_5_1_PIN, AVR32_PWM_5_1_FUNCTION }, // PB28
|
||||
{ AVR32_PWM_6_PIN, AVR32_PWM_6_FUNCTION },
|
||||
};
|
||||
|
||||
@ -1012,80 +1012,81 @@ u32 platform_pwm_op( unsigned id, int op, u32 data)
|
||||
#ifdef BUILD_UIP
|
||||
static const gpio_map_t MACB_GPIO_MAP =
|
||||
{
|
||||
{AVR32_MACB_MDC_0_PIN, AVR32_MACB_MDC_0_FUNCTION },
|
||||
{AVR32_MACB_MDIO_0_PIN, AVR32_MACB_MDIO_0_FUNCTION },
|
||||
{AVR32_MACB_RXD_0_PIN, AVR32_MACB_RXD_0_FUNCTION },
|
||||
{AVR32_MACB_TXD_0_PIN, AVR32_MACB_TXD_0_FUNCTION },
|
||||
{AVR32_MACB_RXD_1_PIN, AVR32_MACB_RXD_1_FUNCTION },
|
||||
{AVR32_MACB_TXD_1_PIN, AVR32_MACB_TXD_1_FUNCTION },
|
||||
{AVR32_MACB_TX_EN_0_PIN, AVR32_MACB_TX_EN_0_FUNCTION },
|
||||
{AVR32_MACB_RX_ER_0_PIN, AVR32_MACB_RX_ER_0_FUNCTION },
|
||||
{AVR32_MACB_RX_DV_0_PIN, AVR32_MACB_RX_DV_0_FUNCTION },
|
||||
{AVR32_MACB_TX_CLK_0_PIN, AVR32_MACB_TX_CLK_0_FUNCTION}
|
||||
{ AVR32_MACB_MDC_0_PIN, AVR32_MACB_MDC_0_FUNCTION },
|
||||
{ AVR32_MACB_MDIO_0_PIN, AVR32_MACB_MDIO_0_FUNCTION },
|
||||
{ AVR32_MACB_RXD_0_PIN, AVR32_MACB_RXD_0_FUNCTION },
|
||||
{ AVR32_MACB_TXD_0_PIN, AVR32_MACB_TXD_0_FUNCTION },
|
||||
{ AVR32_MACB_RXD_1_PIN, AVR32_MACB_RXD_1_FUNCTION },
|
||||
{ AVR32_MACB_TXD_1_PIN, AVR32_MACB_TXD_1_FUNCTION },
|
||||
{ AVR32_MACB_TX_EN_0_PIN, AVR32_MACB_TX_EN_0_FUNCTION },
|
||||
{ AVR32_MACB_RX_ER_0_PIN, AVR32_MACB_RX_ER_0_FUNCTION },
|
||||
{ AVR32_MACB_RX_DV_0_PIN, AVR32_MACB_RX_DV_0_FUNCTION },
|
||||
{ AVR32_MACB_TX_CLK_0_PIN, AVR32_MACB_TX_CLK_0_FUNCTION },
|
||||
};
|
||||
|
||||
u32 platform_ethernet_setup()
|
||||
u32 platform_ethernet_setup()
|
||||
{
|
||||
static struct uip_eth_addr sTempAddr;
|
||||
// Assign GPIO to MACB
|
||||
gpio_enable_module(MACB_GPIO_MAP, sizeof(MACB_GPIO_MAP) / sizeof(MACB_GPIO_MAP[0]));
|
||||
static struct uip_eth_addr sTempAddr = {
|
||||
.addr[0] = ETHERNET_CONF_ETHADDR0,
|
||||
.addr[1] = ETHERNET_CONF_ETHADDR1,
|
||||
.addr[2] = ETHERNET_CONF_ETHADDR2,
|
||||
.addr[3] = ETHERNET_CONF_ETHADDR3,
|
||||
.addr[4] = ETHERNET_CONF_ETHADDR4,
|
||||
.addr[5] = ETHERNET_CONF_ETHADDR5,
|
||||
};
|
||||
|
||||
// initialize MACB & Phy Layers
|
||||
if (xMACBInit(&AVR32_MACB) == FALSE ) {
|
||||
return PLATFORM_ERR;
|
||||
}
|
||||
// Assign GPIO to MACB
|
||||
gpio_enable_module( MACB_GPIO_MAP, sizeof(MACB_GPIO_MAP ) / sizeof( MACB_GPIO_MAP[0] ) );
|
||||
|
||||
sTempAddr.addr[0] = ETHERNET_CONF_ETHADDR0;
|
||||
sTempAddr.addr[1] = ETHERNET_CONF_ETHADDR1;
|
||||
sTempAddr.addr[2] = ETHERNET_CONF_ETHADDR2;
|
||||
sTempAddr.addr[3] = ETHERNET_CONF_ETHADDR3;
|
||||
sTempAddr.addr[4] = ETHERNET_CONF_ETHADDR4;
|
||||
sTempAddr.addr[5] = ETHERNET_CONF_ETHADDR5;
|
||||
// initialize MACB & Phy Layers
|
||||
if ( xMACBInit( &AVR32_MACB ) == FALSE ) {
|
||||
return PLATFORM_ERR;
|
||||
}
|
||||
|
||||
// Initialize the eLua uIP layer
|
||||
elua_uip_init( &sTempAddr );
|
||||
return PLATFORM_OK;
|
||||
}
|
||||
// Initialize the eLua uIP layer
|
||||
elua_uip_init( &sTempAddr );
|
||||
return PLATFORM_OK;
|
||||
}
|
||||
|
||||
void platform_eth_send_packet( const void* src, u32 size )
|
||||
{
|
||||
lMACBSend(&AVR32_MACB,src, size, TRUE);
|
||||
lMACBSend( &AVR32_MACB,src, size, TRUE );
|
||||
}
|
||||
|
||||
u32 platform_eth_get_packet_nb( void* buf, u32 maxlen )
|
||||
{
|
||||
u32 len;
|
||||
u32 len;
|
||||
|
||||
/* Obtain the size of the packet. */
|
||||
len = ulMACBInputLength();
|
||||
/* Obtain the size of the packet. */
|
||||
len = ulMACBInputLength();
|
||||
|
||||
if (len > maxlen) {
|
||||
return 0;
|
||||
}
|
||||
if( len > maxlen ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( len ) {
|
||||
/* Let the driver know we are going to read a new packet. */
|
||||
vMACBRead( NULL, 0, len );
|
||||
vMACBRead( buf, len, len );
|
||||
}
|
||||
if( len ) {
|
||||
/* Let the driver know we are going to read a new packet. */
|
||||
vMACBRead( NULL, 0, len );
|
||||
vMACBRead( buf, len, len );
|
||||
}
|
||||
|
||||
return len;
|
||||
return len;
|
||||
}
|
||||
|
||||
void platform_eth_force_interrupt()
|
||||
{
|
||||
elua_uip_mainloop();
|
||||
elua_uip_mainloop();
|
||||
}
|
||||
|
||||
u32 platform_eth_get_elapsed_time()
|
||||
{
|
||||
if( eth_timer_fired )
|
||||
{
|
||||
eth_timer_fired = 0;
|
||||
return SYSTICKMS;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
if( eth_timer_fired )
|
||||
{
|
||||
eth_timer_fired = 0;
|
||||
return SYSTICKMS;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -259,16 +259,18 @@ void pm_configure_usb_clock(void)
|
||||
pm_gc_enable(&AVR32_PM, AVR32_PM_GCLK_USBB);
|
||||
#else
|
||||
// Use 12MHz from OSC0 and generate 96 MHz
|
||||
pm_pll_setup(&AVR32_PM, 1, // pll.
|
||||
7, // mul.
|
||||
1, // div.
|
||||
0, // osc.
|
||||
16); // lockcount.
|
||||
pm_pll_setup(&AVR32_PM,
|
||||
1, // pll.
|
||||
7, // mul.
|
||||
1, // div.
|
||||
0, // osc.
|
||||
16); // lockcount.
|
||||
|
||||
pm_pll_set_option(&AVR32_PM, 1, // pll.
|
||||
1, // pll_freq: choose the range 80-180MHz.
|
||||
1, // pll_div2.
|
||||
0); // pll_wbwdisable.
|
||||
pm_pll_set_option(&AVR32_PM,
|
||||
1, // pll.
|
||||
1, // pll_freq: choose the range 80-180MHz.
|
||||
1, // pll_div2.
|
||||
0); // pll_wbwdisable.
|
||||
|
||||
// start PLL1 and wait forl lock
|
||||
pm_pll_enable(&AVR32_PM, 1);
|
||||
|
@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
#include "pwm.h"
|
||||
#include "platform_conf.h" // for REQ_PBA_FREQ
|
||||
#include "platform_conf.h" // for REQ_PBA_FREQ
|
||||
|
||||
/*
|
||||
* The AVR32 has 7 PWM channels, each of which chooses its clock from
|
||||
@ -91,7 +91,7 @@ void pwm_set_linear_divider( unsigned prea, unsigned diva )
|
||||
|
||||
mr.prea = prea;
|
||||
mr.diva = diva;
|
||||
mr.preb = 0; // Turn clock B off
|
||||
mr.preb = 0; // Turn clock B off
|
||||
mr.divb = 0;
|
||||
AVR32_PWM.MR = mr;
|
||||
}
|
||||
@ -107,9 +107,9 @@ u32 pwm_get_clock_freq( void )
|
||||
if (divisor == 0)
|
||||
{
|
||||
// This clock is turned off. A frequency of 0 should surprise them.
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
return REQ_PBA_FREQ / ( ( 1<<prescaler ) * divisor );
|
||||
}
|
||||
|
||||
@ -130,7 +130,7 @@ u32 pwm_get_clock_freq( void )
|
||||
* the old frequency at the new duty period or vice versa.
|
||||
* Worse, you have to be careful in which order you change them, since
|
||||
* duty_period > cycle_period is not allowed by the hardware.
|
||||
*
|
||||
*
|
||||
* - To know when one value has been flushed to its register, you have to
|
||||
* enable the per-channel PWM interrupt, clear its status flag and wait until
|
||||
* that interrupt status flag goes high, which happens at the end of each
|
||||
@ -236,8 +236,8 @@ static void pwm_channel_set_period( unsigned id, u32 period )
|
||||
|
||||
// Select updating of the period and write into the update register
|
||||
AVR32_PWM.channel[id].CMR.cpd = AVR32_PWM_CMR_CPD_UPDATE_CPRD;
|
||||
update_has_flushed &= ~(1 << id); // The update hasn't happened yet...
|
||||
AVR32_PWM.channel[id].cupd = period; // Schedule the update to be performed
|
||||
update_has_flushed &= ~(1 << id); // The update hasn't happened yet...
|
||||
AVR32_PWM.channel[id].cupd = period; // Schedule the update to be performed
|
||||
}
|
||||
|
||||
static void pwm_channel_set_duty_cycle( unsigned id, u32 duty )
|
||||
|
@ -4,7 +4,7 @@
|
||||
* Martin Guy <martinwguy@gmail.com>, March 2011
|
||||
*/
|
||||
|
||||
#include "platform.h" // for u32
|
||||
#include "platform.h" // for u32
|
||||
|
||||
// Initialize the PWM system, called at startup
|
||||
void pwm_init( void );
|
||||
|
@ -117,7 +117,7 @@ int spi_initMaster(volatile avr32_spi_t *spi, const spi_master_options_t *opt, U
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
int spi_setupChipReg(volatile avr32_spi_t *spi,
|
||||
unsigned char reg, const spi_options_t *options, U32 pba_hz)
|
||||
unsigned char reg, const spi_options_t *options, U32 pba_hz)
|
||||
{
|
||||
u_avr32_spi_csr_t u_avr32_spi_csr;
|
||||
|
||||
@ -167,11 +167,11 @@ int spi_selectChip(volatile avr32_spi_t *spi, unsigned char chip)
|
||||
spi->mr |= AVR32_SPI_MR_PCS_MASK;
|
||||
|
||||
if (spi->mr & AVR32_SPI_MR_PCSDEC_MASK) {
|
||||
// The signal is decoded; allow up to 15 chips.
|
||||
// The signal is decoded; allow up to 15 chips.
|
||||
if (chip > 14) goto err;
|
||||
spi->mr &= ~AVR32_SPI_MR_PCS_MASK | (chip << AVR32_SPI_MR_PCS_OFFSET);
|
||||
}else {
|
||||
if (chip > 3) goto err;
|
||||
if (chip > 3) goto err;
|
||||
spi->mr &= ~(1 << (AVR32_SPI_MR_PCS_OFFSET + chip));
|
||||
}
|
||||
|
||||
@ -183,7 +183,7 @@ err:
|
||||
int spi_unselectChip(volatile avr32_spi_t *spi, unsigned char chip)
|
||||
{
|
||||
while (!(spi->sr & AVR32_SPI_SR_TXEMPTY_MASK))
|
||||
continue;
|
||||
continue;
|
||||
|
||||
// Assert all lines; no peripheral is selected.
|
||||
spi->mr |= AVR32_SPI_MR_PCS_MASK;
|
||||
|
@ -4,15 +4,15 @@
|
||||
#include <avr32/io.h>
|
||||
|
||||
#ifndef AVR32_SPI0
|
||||
#define AVR32_SPI0 AVR32_SPI
|
||||
#define AVR32_SPI0_ADDRESS AVR32_SPI_ADDRESS
|
||||
#define AVR32_SPI0 AVR32_SPI
|
||||
#define AVR32_SPI0_ADDRESS AVR32_SPI_ADDRESS
|
||||
#endif
|
||||
|
||||
|
||||
typedef enum {
|
||||
SPI_MODE_0 = 0,
|
||||
SPI_MODE_1,
|
||||
SPI_MODE_2,
|
||||
SPI_MODE_3
|
||||
SPI_MODE_0 = 0,
|
||||
SPI_MODE_1,
|
||||
SPI_MODE_2,
|
||||
SPI_MODE_3
|
||||
} spi_mode_t;
|
||||
|
||||
//! Option structure for SPI channels.
|
||||
@ -37,12 +37,12 @@ typedef struct
|
||||
|
||||
typedef struct
|
||||
{
|
||||
//! Mode fault detection disable
|
||||
Bool modfdis;
|
||||
//! Chip select decoding
|
||||
Bool pcs_decode;
|
||||
//! delay before chip select (in microseconds)
|
||||
unsigned int delay;
|
||||
//! Mode fault detection disable
|
||||
Bool modfdis;
|
||||
//! Chip select decoding
|
||||
Bool pcs_decode;
|
||||
//! delay before chip select (in microseconds)
|
||||
unsigned int delay;
|
||||
} spi_master_options_t;
|
||||
|
||||
|
||||
|
@ -34,7 +34,7 @@ typedef unsigned short uip_stats_t;
|
||||
//
|
||||
#define UIP_CONF_PINGADDRCONF 0
|
||||
|
||||
//
|
||||
//
|
||||
// TCP support on or off
|
||||
//
|
||||
#define UIP_CONF_TCP 1
|
||||
|
@ -124,14 +124,14 @@ static int usart_set_async_baudrate(volatile avr32_usart_t *usart, unsigned int
|
||||
*/
|
||||
unsigned int usart_get_async_baudrate(volatile avr32_usart_t *usart, unsigned long pba_hz)
|
||||
{
|
||||
unsigned int clock; // Master clock frequency
|
||||
unsigned int clock; // Master clock frequency
|
||||
unsigned int over; // divisor of 8 or 16
|
||||
unsigned int cd; // clock divider (0-65535)
|
||||
unsigned int fp; // fractional part of clock divider (0-7)
|
||||
unsigned int divisor; // What the master clock is divided by to get
|
||||
// the final baud rate
|
||||
|
||||
// Find
|
||||
// Find
|
||||
switch ((usart->mr & AVR32_USART_MR_USCLKS_MASK) >> AVR32_USART_MR_USCLKS_OFFSET)
|
||||
{
|
||||
case AVR32_USART_MR_USCLKS_MCK:
|
||||
@ -145,7 +145,7 @@ unsigned int usart_get_async_baudrate(volatile avr32_usart_t *usart, unsigned lo
|
||||
case AVR32_USART_MR_USCLKS_SCK:
|
||||
// If we have an external clock, we don't know its frequency here.
|
||||
default:
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
over = usart->mr & AVR32_USART_MR_OVER_MASK;
|
||||
@ -154,7 +154,7 @@ unsigned int usart_get_async_baudrate(volatile avr32_usart_t *usart, unsigned lo
|
||||
|
||||
// if CD==0, no baud rate is generated.
|
||||
// if CD==1, the clock divider and fractional part are bypassed.
|
||||
if (cd == 0) return 0;
|
||||
if (cd == 0) return 0;
|
||||
if (cd == 1) fp = 0;
|
||||
|
||||
// Rewriting "divisor = 8 * (2 - over) * (cd + fp/8)" for integer math:
|
||||
|
@ -13,7 +13,7 @@ static int ser_win32_set_timeouts( HANDLE hComm, DWORD ri, DWORD rtm, DWORD rtc,
|
||||
{
|
||||
COMMTIMEOUTS timeouts;
|
||||
|
||||
if( GetCommTimeouts( hComm, &timeouts ) == FALSE )
|
||||
if( !GetCommTimeouts( hComm, &timeouts ) )
|
||||
{
|
||||
CloseHandle( hComm );
|
||||
return SER_ERR;
|
||||
@ -23,10 +23,10 @@ static int ser_win32_set_timeouts( HANDLE hComm, DWORD ri, DWORD rtm, DWORD rtc,
|
||||
timeouts.ReadTotalTimeoutMultiplier = rtc;
|
||||
timeouts.WriteTotalTimeoutConstant = wtm;
|
||||
timeouts.WriteTotalTimeoutMultiplier = wtc;
|
||||
if( SetCommTimeouts( hComm, &timeouts ) == FALSE )
|
||||
{
|
||||
CloseHandle( hComm );
|
||||
return SER_ERR;
|
||||
if( !SetCommTimeouts( hComm, &timeouts ) )
|
||||
{
|
||||
CloseHandle( hComm );
|
||||
return SER_ERR;
|
||||
}
|
||||
|
||||
return SER_OK;
|
||||
@ -45,6 +45,11 @@ ser_handler ser_open( const char* sername )
|
||||
return WIN_ERROR;
|
||||
if( !SetupComm( hComm, 2048, 2048 ) )
|
||||
return WIN_ERROR;
|
||||
if( !FlushFileBuffers( hComm ) ||
|
||||
!PurgeComm( hComm, PURGE_TXABORT | PURGE_RXABORT |
|
||||
PURGE_TXCLEAR | PURGE_RXCLEAR ) )
|
||||
return WIN_ERROR;
|
||||
|
||||
return hComm;
|
||||
}
|
||||
|
||||
@ -77,11 +82,12 @@ int ser_setup( ser_handler id, u32 baud, int databits, int parity, int stopbits
|
||||
/**/ dcb.fAbortOnError = FALSE;
|
||||
dcb.fOutxCtsFlow = FALSE;
|
||||
dcb.fOutxDsrFlow = FALSE;
|
||||
dcb.fDtrControl = DTR_CONTROL_DISABLE;
|
||||
// dcb.fDtrControl = DTR_CONTROL_DISABLE; -- prevents data being read with LM3S eval board
|
||||
dcb.fDsrSensitivity = FALSE;
|
||||
dcb.fRtsControl = RTS_CONTROL_DISABLE;
|
||||
dcb.fOutxCtsFlow = FALSE;
|
||||
if( SetCommState( hComm, &dcb ) == 0 )
|
||||
|
||||
if( !SetCommState( hComm, &dcb ) )
|
||||
{
|
||||
CloseHandle( hComm );
|
||||
return SER_ERR;
|
||||
@ -103,7 +109,7 @@ u32 ser_read( ser_handler id, u8* dest, u32 maxsize )
|
||||
HANDLE hComm = ( HANDLE )id;
|
||||
DWORD readbytes;
|
||||
|
||||
if( ReadFile( hComm, dest, maxsize, &readbytes, NULL ) == FALSE )
|
||||
if( !ReadFile( hComm, dest, maxsize, &readbytes, NULL ) )
|
||||
return 0;
|
||||
return readbytes;
|
||||
}
|
||||
@ -124,7 +130,7 @@ u32 ser_write( ser_handler id, const u8 *src, u32 size )
|
||||
HANDLE hComm = ( HANDLE )id;
|
||||
DWORD written;
|
||||
|
||||
if( WriteFile( hComm, src, size, &written, NULL ) == FALSE )
|
||||
if( !WriteFile( hComm, src, size, &written, NULL ) )
|
||||
return 0;
|
||||
return written;
|
||||
}
|
||||
@ -153,7 +159,8 @@ int ser_readable( ser_handler id )
|
||||
DWORD dwErrors;
|
||||
HANDLE hComm = ( HANDLE )id;
|
||||
|
||||
ClearCommError(hComm, &dwErrors, &comStat);
|
||||
if( !ClearCommError(hComm, &dwErrors, &comStat) )
|
||||
return 0;
|
||||
|
||||
return ( comStat.cbInQue > 0 );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user