Skip to content

Commit

Permalink
Merge changes from upstream. Tested for AVR, ARM, ESP and RISC-V.
Browse files Browse the repository at this point in the history
Strange behaviour on building for my AVR128DB48 precluded testing there, which
means the largest new feature has _not_ been tested for full integration. The
issue in question appears not to influence upstream, for some reason.
  • Loading branch information
anarchodin committed Apr 8, 2021
1 parent 895ed59 commit 7612919
Show file tree
Hide file tree
Showing 20 changed files with 184 additions and 73 deletions.
13 changes: 7 additions & 6 deletions platforms.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

(defparameter *platforms*
'((:avr
(:types zzero symbol number stream string pair)
(:types zzero symbol code number stream string pair)
(:streams serial i2c spi sd)
(:keywords
("CPU_ATmega328P"
(DIGITALWRITE HIGH LOW)
(PINMODE INPUT INPUT_PULLUP OUTPUT)
(ANALOGREFERENCE DEFAULT INTERNAL EXTERNAL))
("CPU_ATmega1284P"
(DIGITALWRITE HIGH LOW)
(PINMODE INPUT INPUT_PULLUP OUTPUT)
(ANALOGREFERENCE DEFAULT INTERNAL1V1 INTERNAL2V56 EXTERNAL))
("CPU_ATmega2560"
(DIGITALWRITE HIGH LOW)
(PINMODE INPUT INPUT_PULLUP OUTPUT)
Expand All @@ -31,7 +35,7 @@
(ANALOGREFERENCE DEFAULT VDD INTERNAL1V024 INTERNAL2V048 INTERNAL4V096
INTERNAL2V5 EXTERNAL)
(ANALOGREAD ADC_DAC0 ADC_TEMPERATURE)))
(:features :dacreference))
(:features :code :dacreference))
(:arm
(:types zzero code number stream float array string pair)
(:streams serial i2c spi sd string gfx)
Expand Down Expand Up @@ -85,10 +89,7 @@
(:types zzero code number stream float array string pair)
(:streams serial i2c spi sd string gfx)
(:keywords
("ESP8266"
(DIGITALWRITE HIGH LOW)
(PINMODE INPUT INPUT_PULLUP OUTPUT))
("ESP32"
(nil
(DIGITALWRITE HIGH LOW)
(PINMODE INPUT INPUT_PULLUP INPUT_PULLDOWN OUTPUT)))
(:features :float :gfx :code :array :stringstream :write-resolution)))
Expand Down
8 changes: 4 additions & 4 deletions sections/arm/saveimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ int loadimage (object *arg) {
else error(LOADIMAGE, PSTR("illegal argument"), arg);
if (!file) error2(LOADIMAGE, PSTR("problem loading from SD card"));
SDReadInt(file);
int imagesize = SDReadInt(file);
unsigned int imagesize = SDReadInt(file);
GlobalEnv = (object *)SDReadInt(file);
GCStack = (object *)SDReadInt(file);
#if SYMBOLTABLESIZE > BUFFERSIZE
Expand All @@ -205,7 +205,7 @@ int loadimage (object *arg) {
for (int i=0; i<SymbolUsed; i++) SymbolTable[i] = file.read();
#endif
for (int i=0; i<CODESIZE; i++) MyCode[i] = file.read();
for (int i=0; i<imagesize; i++) {
for (unsigned int i=0; i<imagesize; i++) {
object *obj = &Workspace[i];
car(obj) = (object *)SDReadInt(file);
cdr(obj) = (object *)SDReadInt(file);
Expand All @@ -217,7 +217,7 @@ int loadimage (object *arg) {
if (!FlashSetup()) error2(LOADIMAGE, PSTR("no DataFlash found."));
FlashBeginRead();
FlashReadInt(); // Skip eval address
int imagesize = FlashReadInt();
unsigned int imagesize = FlashReadInt();
if (imagesize == 0 || imagesize == 0xFFFFFFFF) error2(LOADIMAGE, PSTR("no saved image"));
GlobalEnv = (object *)FlashReadInt();
GCStack = (object *)FlashReadInt();
Expand All @@ -227,7 +227,7 @@ int loadimage (object *arg) {
for (int i=0; i<SymbolUsed; i++) SymbolTable[i] = FlashReadByte();
#endif
for (int i=0; i<CODESIZE; i++) MyCode[i] = FlashReadByte();
for (int i=0; i<imagesize; i++) {
for (unsigned int i=0; i<imagesize; i++) {
object *obj = &Workspace[i];
car(obj) = (object *)FlashReadInt();
cdr(obj) = (object *)FlashReadInt();
Expand Down
2 changes: 1 addition & 1 deletion sections/arm/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ void setup () {
initenv();
initsleep();
initgfx();
pfstring(PSTR("uLisp 3.5 "), pserial); pln(pserial);
pfstring(PSTR("uLisp 3.6 "), pserial); pln(pserial);
}
1 change: 1 addition & 0 deletions sections/avr/header.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const char LispLibrary[] PROGMEM = "";
// #define printgcs
// #define sdcardsupport
// #define lisplibrary
#define assemblerlist
// #define lineeditor
// #define vt100

Expand Down
93 changes: 65 additions & 28 deletions sections/avr/saveimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,40 @@ void SDWriteInt (File file, int data) {
file.write(data & 0xFF); file.write(data>>8 & 0xFF);
}
#elif defined(FLASHWRITESIZE)
#elif defined(FLASHWRITESIZE)
#define BASE_ADDRESS 0x1C000
#if defined (CPU_ATmega1284P)
// save-image area is the 16K bytes (64 256-byte pages) from 0x1bc00 to 0x1fc00
const uint32_t BaseAddress = 0x1bc00;
uint8_t FlashCheck() {
return 0;
}

boolean FlashSetup () {
return (Flash.checkWritable() == FLASHWRITE_OK);
void FlashWriteInt (uint32_t *addr, int data) {
if (((*addr) & 0xFF) == 0) optiboot_page_erase(BaseAddress + ((*addr) & 0xFF00));
optiboot_page_fill(BaseAddress + *addr, data);
if (((*addr) & 0xFF) == 0xFE) optiboot_page_write(BaseAddress + ((*addr) & 0xFF00));
(*addr)++; (*addr)++;
}

boolean FlashBeginWrite (int pages) {
// Erase how many 512K pages we need; must be a power of 2 <= 32.
return Flash.erasePage(BASE_ADDRESS, pages) == 0;
void FlashEndWrite (uint32_t *addr) {
if (((*addr) & 0xFF) != 0) optiboot_page_write((BaseAddress + ((*addr) & 0xFF00)));
}

void FlashWriteByte (uint32_t *addr, uint8_t data) {
Flash.writeByte((*addr)++, data);
#elif defined (CPU_AVR128DX48)
// save-image area is the 16K bytes (32 512-byte pages) from 0x1c000 to 0x20000
const uint32_t BaseAddress = 0x1c000;
uint8_t FlashCheck() {
return Flash.checkWritable();
}

void FlashWriteInt (uint32_t *addr, int data) {
Flash.writeWord(*addr, data);
if (((*addr) & 0x1FF) == 0) Flash.erasePage(BaseAddress + ((*addr) & 0xFE00));
Flash.writeWord(BaseAddress + *addr, data);
(*addr)++; (*addr)++;
}

void FlashEndWrite (uint32_t *addr) {
}
#endif
#else
void EEPROMWriteInt (unsigned int *addr, int data) {
EEPROM.write((*addr)++, data & 0xFF); EEPROM.write((*addr)++, data>>8 & 0xFF);
Expand All @@ -51,6 +65,9 @@ unsigned int saveimage (object *arg) {
int SymbolUsed = SymbolTop - SymbolTable;
for (int i=0; i<SymbolUsed; i++) file.write(SymbolTable[i]);
#endif
#if defined(CODESIZE)
for (int i=0; i<CODESIZE; i++) file.write(MyCode[i]);
#endif
for (unsigned int i=0; i<imagesize; i++) {
object *obj = &Workspace[i];
SDWriteInt(file, (uintptr_t)car(obj));
Expand All @@ -60,26 +77,29 @@ unsigned int saveimage (object *arg) {
return imagesize;
#elif defined(FLASHWRITESIZE)
if (!(arg == NULL || listp(arg))) error(SAVEIMAGE, invalidarg, arg);
if (!FlashSetup()) error2(SAVEIMAGE, PSTR("no DataFlash found."));
if (FlashCheck()) error2(SAVEIMAGE, PSTR("flash write not supported"));
// Save to Flash
int SymbolUsed = SymbolTop - SymbolTable;
int bytesneeded = imagesize*4 + SymbolUsed + 10;
int bytesneeded = 10 + SYMBOLTABLESIZE + CODESIZE + imagesize*4;
if (bytesneeded > FLASHWRITESIZE) error(SAVEIMAGE, PSTR("image too large"), number(imagesize));
if (!FlashBeginWrite(32)) error2(SAVEIMAGE, PSTR("problem erasing flash"));
uint32_t addr = BASE_ADDRESS;
uint32_t addr = 0;
FlashWriteInt(&addr, (uintptr_t)arg);
FlashWriteInt(&addr, imagesize);
FlashWriteInt(&addr, (uintptr_t)GlobalEnv);
FlashWriteInt(&addr, (uintptr_t)GCStack);
#if SYMBOLTABLESIZE > BUFFERSIZE
FlashWriteInt(&addr, (uintptr_t)SymbolTop);
for (int i=0; i<SymbolUsed; i++) FlashWriteByte(&addr, SymbolTable[i]);
for (int i=0; i<SYMBOLTABLESIZE/2; i++) FlashWriteInt(&addr, SymbolTable[i*2] | SymbolTable[i*2+1]<<8);
#endif
#if defined(CODESIZE)
for (int i=0; i<CODESIZE/2; i++) FlashWriteInt(&addr, MyCode[i*2] | MyCode[i*2+1]<<8);
#endif
for (unsigned int i=0; i<imagesize; i++) {
object *obj = &Workspace[i];
FlashWriteInt(&addr, (uintptr_t)car(obj));
FlashWriteInt(&addr, (uintptr_t)cdr(obj));
}
FlashEndWrite(&addr);
return imagesize;
#else
if (!(arg == NULL || listp(arg))) error(SAVEIMAGE, invalidarg, arg);
Expand Down Expand Up @@ -110,15 +130,27 @@ int SDReadInt (File file) {
return b0 | b1<<8;
}
#elif defined(FLASHWRITESIZE)
#if defined (CPU_ATmega1284P)
uint8_t FlashReadByte (uint32_t *addr) {
return Flash.readByte((*addr)++);
return pgm_read_byte_far(BaseAddress + (*addr)++);
}

int FlashReadInt (uint32_t *addr) {
int data = Flash.readWord(*addr);
int data = pgm_read_word_far(BaseAddress + *addr);
(*addr)++; (*addr)++;
return data;
}
#elif defined (CPU_AVR128DX48)
uint8_t FlashReadByte (uint32_t *addr) {
return Flash.readByte(BaseAddress + (*addr)++);
}

int FlashReadInt (uint32_t *addr) {
int data = Flash.readWord(BaseAddress + *addr);
(*addr)++; (*addr)++;
return data;
}
#endif
#else
int EEPROMReadInt (unsigned int *addr) {
uint8_t b0 = EEPROM.read((*addr)++); uint8_t b1 = EEPROM.read((*addr)++);
Expand All @@ -135,15 +167,18 @@ unsigned int loadimage (object *arg) {
else error(LOADIMAGE, invalidarg, arg);
if (!file) error2(LOADIMAGE, PSTR("problem loading from SD card"));
SDReadInt(file);
int imagesize = SDReadInt(file);
unsigned int imagesize = SDReadInt(file);
GlobalEnv = (object *)SDReadInt(file);
GCStack = (object *)SDReadInt(file);
#if SYMBOLTABLESIZE > BUFFERSIZE
SymbolTop = (char *)SDReadInt(file);
int SymbolUsed = SymbolTop - SymbolTable;
for (int i=0; i<SymbolUsed; i++) SymbolTable[i] = file.read();
#endif
for (int i=0; i<imagesize; i++) {
#if defined(CODESIZE)
for (int i=0; i<CODESIZE; i++) MyCode[i] = file.read();
#endif
for (unsigned int i=0; i<imagesize; i++) {
object *obj = &Workspace[i];
car(obj) = (object *)SDReadInt(file);
cdr(obj) = (object *)SDReadInt(file);
Expand All @@ -152,19 +187,21 @@ unsigned int loadimage (object *arg) {
gc(NULL, NULL);
return imagesize;
#elif defined(FLASHWRITESIZE)
if (!FlashSetup()) error2(LOADIMAGE, PSTR("no Flash found."));
uint32_t addr = BASE_ADDRESS;
if (FlashCheck()) error2(SAVEIMAGE, PSTR("flash write not supported"));
uint32_t addr = 0;
FlashReadInt(&addr); // Skip eval address
int imagesize = FlashReadInt(&addr);
if (imagesize == 0 || imagesize == 0xFFFFFFFF) error2(LOADIMAGE, PSTR("no saved image"));
unsigned int imagesize = FlashReadInt(&addr);
if (imagesize == 0 || imagesize == 0xFFFF) error2(LOADIMAGE, PSTR("no saved image"));
GlobalEnv = (object *)FlashReadInt(&addr);
GCStack = (object *)FlashReadInt(&addr);
#if SYMBOLTABLESIZE > BUFFERSIZE
SymbolTop = (char *)FlashReadInt(&addr);
int SymbolUsed = SymbolTop - SymbolTable;
for (int i=0; i<SymbolUsed; i++) SymbolTable[i] = FlashReadByte(&addr);
for (int i=0; i<SYMBOLTABLESIZE; i++) SymbolTable[i] = FlashReadByte(&addr);
#endif
for (int i=0; i<imagesize; i++) {
#if defined(CODESIZE)
for (int i=0; i<CODESIZE; i++) MyCode[i] = FlashReadByte(&addr);
#endif
for (unsigned int i=0; i<imagesize; i++) {
object *obj = &Workspace[i];
car(obj) = (object *)FlashReadInt(&addr);
cdr(obj) = (object *)FlashReadInt(&addr);
Expand All @@ -182,7 +219,7 @@ unsigned int loadimage (object *arg) {
int SymbolUsed = SymbolTop - SymbolTable;
for (int i=0; i<SymbolUsed; i++) SymbolTable[i] = EEPROM.read(addr++);
#endif
for (int i=0; i<imagesize; i++) {
for (unsigned int i=0; i<imagesize; i++) {
object *obj = &Workspace[i];
car(obj) = (object *)EEPROMReadInt(&addr);
cdr(obj) = (object *)EEPROMReadInt(&addr);
Expand All @@ -204,7 +241,7 @@ void autorunimage () {
apply(0, autorun, NULL, NULL);
}
#elif defined(FLASHWRITESIZE)
uint32_t addr = BASE_ADDRESS;
uint32_t addr = 0;
object *autorun = (object *)FlashReadInt(&addr);
if (autorun != NULL && (unsigned int)autorun != 0xFFFF) {
loadimage(nil);
Expand Down
2 changes: 1 addition & 1 deletion sections/avr/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ void setup () {
initworkspace();
initenv();
initsleep();
pfstring(PSTR("uLisp 3.5 "), pserial); pln(pserial);
pfstring(PSTR("uLisp 3.6 "), pserial); pln(pserial);
}
24 changes: 12 additions & 12 deletions sections/avr/stream-interface.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Streams

inline int spiread () { return SPI.transfer(0); }
#if defined(CPU_ATmega1284P)
#if defined(CPU_ATmega1284P) || defined(CPU_AVR128DX48)
inline int serial1read () { while (!Serial1.available()) testescape(); return Serial1.read(); }
#elif defined(CPU_ATmega2560) || defined(CPU_AVR128DX48)
#elif defined(CPU_ATmega2560)
inline int serial1read () { while (!Serial1.available()) testescape(); return Serial1.read(); }
inline int serial2read () { while (!Serial2.available()) testescape(); return Serial2.read(); }
inline int serial3read () { while (!Serial3.available()) testescape(); return Serial3.read(); }
Expand All @@ -23,10 +23,10 @@ inline int SDread () {
void serialbegin (int address, int baud) {
#if defined(CPU_ATmega328P)
(void) address; (void) baud;
#elif defined(CPU_ATmega1284P)
#elif defined(CPU_ATmega1284P) || defined(CPU_AVR128DX48)
if (address == 1) Serial1.begin((long)baud*100);
else error(WITHSERIAL, PSTR("port not supported"), number(address));
#elif defined(CPU_ATmega2560) || defined(CPU_AVR128DX48)
#elif defined(CPU_ATmega2560)
if (address == 1) Serial1.begin((long)baud*100);
else if (address == 2) Serial2.begin((long)baud*100);
else if (address == 3) Serial3.begin((long)baud*100);
Expand All @@ -37,9 +37,9 @@ void serialbegin (int address, int baud) {
void serialend (int address) {
#if defined(CPU_ATmega328P)
(void) address;
#elif defined(CPU_ATmega1284P)
#elif defined(CPU_ATmega1284P) || defined(CPU_AVR128DX48)
if (address == 1) {Serial1.flush(); Serial1.end(); }
#elif defined(CPU_ATmega2560) || defined(CPU_AVR128DX48)
#elif defined(CPU_ATmega2560)
if (address == 1) {Serial1.flush(); Serial1.end(); }
else if (address == 2) {Serial2.flush(); Serial2.end(); }
else if (address == 3) {Serial3.flush(); Serial3.end(); }
Expand All @@ -58,9 +58,9 @@ gfun_t gstreamfun (object *args) {
else if (streamtype == SPISTREAM) gfun = spiread;
else if (streamtype == SERIALSTREAM) {
if (address == 0) gfun = gserial;
#if defined(CPU_ATmega1284P)
#if defined(CPU_ATmega1284P) || defined(CPU_AVR128DX48)
else if (address == 1) gfun = serial1read;
#elif defined(CPU_ATmega2560) || defined(CPU_AVR128DX48)
#elif defined(CPU_ATmega2560)
else if (address == 1) gfun = serial1read;
else if (address == 2) gfun = serial2read;
else if (address == 3) gfun = serial3read;
Expand All @@ -74,9 +74,9 @@ gfun_t gstreamfun (object *args) {
}

inline void spiwrite (char c) { SPI.transfer(c); }
#if defined(CPU_ATmega1284P)
#if defined(CPU_ATmega1284P) || defined(CPU_AVR128DX48)
inline void serial1write (char c) { Serial1.write(c); }
#elif defined(CPU_ATmega2560) || defined(CPU_AVR128DX48)
#elif defined(CPU_ATmega2560)
inline void serial1write (char c) { Serial1.write(c); }
inline void serial2write (char c) { Serial2.write(c); }
inline void serial3write (char c) { Serial3.write(c); }
Expand All @@ -97,9 +97,9 @@ pfun_t pstreamfun (object *args) {
else if (streamtype == SPISTREAM) pfun = spiwrite;
else if (streamtype == SERIALSTREAM) {
if (address == 0) pfun = pserial;
#if defined(CPU_ATmega1284P)
#if defined(CPU_ATmega1284P) || defined(CPU_AVR128DX48)
else if (address == 1) pfun = serial1write;
#elif defined(CPU_ATmega2560) || defined(CPU_AVR128DX48)
#elif defined(CPU_ATmega2560)
else if (address == 1) pfun = serial1write;
else if (address == 2) pfun = serial2write;
else if (address == 3) pfun = serial3write;
Expand Down
Loading

0 comments on commit 7612919

Please sign in to comment.