1
- /* uLisp ESP Release 4.4b - www.ulisp.com
2
- David Johnson-Davies - www.technoblogy.com - 31st March 2023
1
+ /* uLisp ESP Release 4.4c - www.ulisp.com
2
+ David Johnson-Davies - www.technoblogy.com - 21st April 2023
3
3
4
4
Licensed under the MIT license: https://opensource.org/licenses/MIT
5
5
*/
@@ -80,6 +80,14 @@ Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST);
80
80
#define analogWrite (x,y ) dacWrite((x),(y))
81
81
#define SDCARD_SS_PIN 13
82
82
83
+ #elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO)
84
+ #define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
85
+ #define LITTLEFS
86
+ #include " FS.h"
87
+ #include < LittleFS.h>
88
+ #define SDCARD_SS_PIN 13
89
+ #define LED_BUILTIN 13
90
+
83
91
#elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32S2)
84
92
#define WORKSPACESIZE (9216 -SDSIZE) /* Cells (8*bytes) */
85
93
#define LITTLEFS
@@ -1745,46 +1753,55 @@ object *mapcarcan (object *args, object *env, mapfun_t fun) {
1745
1753
}
1746
1754
}
1747
1755
1748
- // I2C interface for one port , using Arduino Wire
1756
+ // I2C interface for up to two ports , using Arduino Wire
1749
1757
1750
- void I2Cinit (bool enablePullup) {
1758
+ void I2Cinit (TwoWire *port, bool enablePullup) {
1751
1759
(void ) enablePullup;
1752
- Wire. begin ();
1760
+ port-> begin ();
1753
1761
}
1754
1762
1755
- int I2Cread () {
1756
- return Wire. read ();
1763
+ int I2Cread (TwoWire *port ) {
1764
+ return port-> read ();
1757
1765
}
1758
1766
1759
- void I2Cwrite (uint8_t data) {
1760
- Wire. write (data);
1767
+ void I2Cwrite (TwoWire *port, uint8_t data) {
1768
+ port-> write (data);
1761
1769
}
1762
1770
1763
- bool I2Cstart (uint8_t address, uint8_t read) {
1771
+ bool I2Cstart (TwoWire *port, uint8_t address, uint8_t read) {
1764
1772
int ok = true ;
1765
1773
if (read == 0 ) {
1766
- Wire. beginTransmission (address);
1767
- ok = (Wire. endTransmission (true ) == 0 );
1768
- Wire. beginTransmission (address);
1774
+ port-> beginTransmission (address);
1775
+ ok = (port-> endTransmission (true ) == 0 );
1776
+ port-> beginTransmission (address);
1769
1777
}
1770
- else Wire. requestFrom (address, I2Ccount);
1778
+ else port-> requestFrom (address, I2Ccount);
1771
1779
return ok;
1772
1780
}
1773
1781
1774
- bool I2Crestart (uint8_t address, uint8_t read) {
1775
- int error = (Wire. endTransmission (false ) != 0 );
1776
- if (read == 0 ) Wire. beginTransmission (address);
1777
- else Wire. requestFrom (address, I2Ccount);
1782
+ bool I2Crestart (TwoWire *port, uint8_t address, uint8_t read) {
1783
+ int error = (port-> endTransmission (false ) != 0 );
1784
+ if (read == 0 ) port-> beginTransmission (address);
1785
+ else port-> requestFrom (address, I2Ccount);
1778
1786
return error ? false : true ;
1779
1787
}
1780
1788
1781
- void I2Cstop (uint8_t read) {
1782
- if (read == 0 ) Wire. endTransmission (); // Check for error?
1789
+ void I2Cstop (TwoWire *port, uint8_t read) {
1790
+ if (read == 0 ) port-> endTransmission (); // Check for error?
1783
1791
}
1784
1792
1785
1793
// Streams
1786
1794
1795
+ // Simplify board differences
1796
+ #if defined(ARDUINO_ADAFRUIT_QTPY_ESP32S2)
1797
+ #define ULISP_I2C1
1798
+ #endif
1799
+
1787
1800
inline int spiread () { return SPI.transfer (0 ); }
1801
+ inline int i2cread () { return I2Cread (&Wire); }
1802
+ #if defined(ULISP_I2C1)
1803
+ inline int i2c1read () { return I2Cread (&Wire1); }
1804
+ #endif
1788
1805
inline int serial1read () { while (!Serial1.available ()) testescape (); return Serial1.read (); }
1789
1806
#if defined(sdcardsupport)
1790
1807
File SDpfile, SDgfile;
@@ -1817,6 +1834,7 @@ void serialbegin (int address, int baud) {
1817
1834
1818
1835
void serialend (int address) {
1819
1836
if (address == 1 ) {Serial1.flush (); Serial1.end (); }
1837
+ else error (PSTR (" port not supported" ), number (address));
1820
1838
}
1821
1839
1822
1840
gfun_t gstreamfun (object *args) {
@@ -1827,8 +1845,12 @@ gfun_t gstreamfun (object *args) {
1827
1845
int stream = isstream (first (args));
1828
1846
streamtype = stream>>8 ; address = stream & 0xFF ;
1829
1847
}
1830
- if (streamtype == I2CSTREAM) gfun = (gfun_t )I2Cread;
1831
- else if (streamtype == SPISTREAM) gfun = spiread;
1848
+ if (streamtype == I2CSTREAM) {
1849
+ if (address < 128 ) gfun = i2cread;
1850
+ #if defined(ULISP_I2C1)
1851
+ else gfun = i2c1read;
1852
+ #endif
1853
+ } else if (streamtype == SPISTREAM) gfun = spiread;
1832
1854
else if (streamtype == SERIALSTREAM) {
1833
1855
if (address == 0 ) gfun = gserial;
1834
1856
else if (address == 1 ) gfun = serial1read;
@@ -1842,6 +1864,10 @@ gfun_t gstreamfun (object *args) {
1842
1864
}
1843
1865
1844
1866
inline void spiwrite (char c) { SPI.transfer (c); }
1867
+ inline void i2cwrite (char c) { I2Cwrite (&Wire, c); }
1868
+ #if defined(ULISP_I2C1)
1869
+ inline void i2c1write (char c) { I2Cwrite (&Wire1, c); }
1870
+ #endif
1845
1871
inline void serial1write (char c) { Serial1.write (c); }
1846
1872
inline void WiFiwrite (char c) { client.write (c); }
1847
1873
#if defined(sdcardsupport)
@@ -1859,8 +1885,12 @@ pfun_t pstreamfun (object *args) {
1859
1885
int stream = isstream (first (args));
1860
1886
streamtype = stream>>8 ; address = stream & 0xFF ;
1861
1887
}
1862
- if (streamtype == I2CSTREAM) pfun = (pfun_t )I2Cwrite;
1863
- else if (streamtype == SPISTREAM) pfun = spiwrite;
1888
+ if (streamtype == I2CSTREAM) {
1889
+ if (address < 128 ) pfun = i2cwrite;
1890
+ #if defined(ULISP_I2C1)
1891
+ else pfun = i2c1write;
1892
+ #endif
1893
+ } else if (streamtype == SPISTREAM) pfun = spiwrite;
1864
1894
else if (streamtype == SERIALSTREAM) {
1865
1895
if (address == 0 ) pfun = pserial;
1866
1896
else if (address == 1 ) pfun = serial1write;
@@ -1892,6 +1922,8 @@ void checkanalogread (int pin) {
1892
1922
error (PSTR (" invalid pin" ), number (pin));
1893
1923
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) || defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT)
1894
1924
if (!(pin==8 || (pin>=14 && pin<=18 ))) error (PSTR (" invalid pin" ), number (pin));
1925
+ #elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO)
1926
+ if (!(pin==4 || pin==7 || (pin>=12 && pin<=15 ) || (pin>=25 && pin<=27 ) || (pin>=32 && pin<=33 ))) error (PSTR (" invalid pin" ), number (pin));
1895
1927
#elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32S2)
1896
1928
if (!((pin>=5 && pin<=9 ) || (pin>=16 && pin<=18 ))) error (PSTR (" invalid pin" ), number (pin));
1897
1929
#elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3)
@@ -1908,7 +1940,7 @@ void checkanalogread (int pin) {
1908
1940
void checkanalogwrite (int pin) {
1909
1941
#if defined(ESP8266)
1910
1942
if (!(pin>=0 && pin<=16 )) error (PSTR (" invalid pin" ), number (pin));
1911
- #elif defined(ESP32) || defined(ARDUINO_FEATHER_ESP32) || defined(ARDUINO_ESP32_DEV)
1943
+ #elif defined(ESP32) || defined(ARDUINO_FEATHER_ESP32) || defined(ARDUINO_ESP32_DEV) || defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO)
1912
1944
if (!(pin>=25 && pin<=26 )) error (PSTR (" invalid pin" ), number (pin));
1913
1945
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) || defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT) || defined(ARDUINO_ADAFRUIT_QTPY_ESP32S2) || defined(ARDUINO_FEATHERS2) || defined(ARDUINO_ESP32S2_DEV)
1914
1946
if (!(pin>=17 && pin<=18 )) error (PSTR (" invalid pin" ), number (pin));
@@ -2394,20 +2426,28 @@ object *sp_withi2c (object *args, object *env) {
2394
2426
object *var = first (params);
2395
2427
int address = checkinteger (eval (second (params), env));
2396
2428
params = cddr (params);
2397
- if (address == 0 && params != NULL ) params = cdr (params); // Ignore port
2429
+ if ((address == 0 || address == 1 ) && params != NULL ) {
2430
+ address = address * 128 + checkinteger (eval (first (params), env));
2431
+ params = cdr (params);
2432
+ }
2398
2433
int read = 0 ; // Write
2399
2434
I2Ccount = 0 ;
2400
2435
if (params != NULL ) {
2401
2436
object *rw = eval (first (params), env);
2402
2437
if (integerp (rw)) I2Ccount = rw->integer ;
2403
2438
read = (rw != NULL );
2404
2439
}
2405
- I2Cinit (1 ); // Pullups
2406
- object *pair = cons (var, (I2Cstart (address, read)) ? stream (I2CSTREAM, address) : nil);
2440
+ // Top bit of address is I2C port
2441
+ TwoWire *port = &Wire;
2442
+ #if defined(ULISP_I2C1)
2443
+ if (address > 127 ) port = &Wire1;
2444
+ #endif
2445
+ I2Cinit (port, 1 ); // Pullups
2446
+ object *pair = cons (var, (I2Cstart (port, address & 0x7F , read)) ? stream (I2CSTREAM, address) : nil);
2407
2447
push (pair,env);
2408
2448
object *forms = cdr (args);
2409
2449
object *result = eval (tf_progn (forms,env), env);
2410
- I2Cstop (read);
2450
+ I2Cstop (port, read);
2411
2451
return result;
2412
2452
}
2413
2453
@@ -3672,7 +3712,12 @@ object *fn_restarti2c (object *args, object *env) {
3672
3712
}
3673
3713
int address = stream & 0xFF ;
3674
3714
if (stream>>8 != I2CSTREAM) error2 (PSTR (" not an i2c stream" ));
3675
- return I2Crestart (address, read) ? tee : nil;
3715
+ TwoWire *port;
3716
+ if (address < 128 ) port = &Wire;
3717
+ #if defined(ULISP_I2C1)
3718
+ else port = &Wire1;
3719
+ #endif
3720
+ return I2Crestart (port, address & 0x7F , read) ? tee : nil;
3676
3721
}
3677
3722
3678
3723
object *fn_gc (object *obj, object *env) {
@@ -5505,7 +5550,7 @@ bool findsubstring (char *part, builtin_t name) {
5505
5550
}
5506
5551
5507
5552
void testescape () {
5508
- if (Serial.read () == ' ~' ) error2 (PSTR (" escape!" ));
5553
+ if (Serial.available () && Serial. read () == ' ~' ) error2 (PSTR (" escape!" ));
5509
5554
}
5510
5555
5511
5556
bool keywordp (object *obj) {
@@ -6216,7 +6261,7 @@ void setup () {
6216
6261
initenv ();
6217
6262
initsleep ();
6218
6263
initgfx ();
6219
- pfstring (PSTR (" uLisp 4.4b " ), pserial); pln (pserial);
6264
+ pfstring (PSTR (" uLisp 4.4c " ), pserial); pln (pserial);
6220
6265
}
6221
6266
6222
6267
// Read/Evaluate/Print loop
0 commit comments