-
-
Notifications
You must be signed in to change notification settings - Fork 759
#606. Initial framework for hardware SPI for ESP8266. #692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0a0af1c
438acce
9b9e0e1
ca26817
eee637d
c9640da
1020e47
85acce4
71d1975
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/__pycache__/ | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ | |
'flash' : 1024, | ||
'speed' : 80, | ||
'usart' : 1, | ||
'spi' : 0, | ||
'spi' : 1, | ||
'i2c' : 1, | ||
'adc' : 1, | ||
'dac' : 0, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ | |
'flash' : 512, | ||
'speed' : 80, | ||
'usart' : 1, | ||
'spi' : 0, | ||
'spi' : 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to also add it to the ESP8266_12.py board... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree ... there are a couple of issues floating around just now ... see Issue #675 for the one that is most relevant. |
||
'i2c' : 1, | ||
'adc' : 1, | ||
'dac' : 0, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
#include <espmissingincludes.h> | ||
#include <uart.h> | ||
#include <i2c_master.h> | ||
#include <spi.h> // Include the MetalPhreak/ESP8266_SPI_Library headers. | ||
|
||
//#define FAKE_STDLIB | ||
#define _GCC_WRAP_STDINT_H | ||
|
@@ -53,6 +54,10 @@ typedef long long int64_t; | |
// Address in RTC RAM where we save the time | ||
#define RTC_TIME_ADDR (256/4) // start of "user data" in RTC RAM | ||
|
||
|
||
static bool g_spiInitialized = false; | ||
static int g_lastSPIRead = -1; | ||
|
||
/** | ||
* Transmit all the characters in the transmit buffer. | ||
* | ||
|
@@ -159,6 +164,8 @@ void jshReset() { | |
jshPinSetState(i, JSHPINSTATE_GPIO_IN); | ||
} | ||
*/ | ||
g_spiInitialized = false; // Flag the hardware SPI interface as un-initialized. | ||
g_lastSPIRead = -1; | ||
os_printf("< jshReset\n"); | ||
} // End of jshReset | ||
|
||
|
@@ -588,26 +595,69 @@ void jshUSARTKick( | |
//===== SPI ===== | ||
|
||
/** | ||
* Unknown | ||
* Initialize the hardware SPI device. | ||
* On the ESP8266, hardware SPI is implemented via a set of pins defined | ||
* as follows: | ||
* | ||
* | GPIO | NodeMCU | Name | Function | | ||
* |--------|---------|-------|----------| | ||
* | GPIO12 | D6 | HMISO | MISO | | ||
* | GPIO13 | D7 | HMOSI | MOSI | | ||
* | GPIO14 | D5 | HSCLK | CLK | | ||
* | GPIO15 | D8 | HCS | CS | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to have more than one device on the SPI bus, i.e., by specifying a second chip select pin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good question. This table and documentation comes from ESP8266 "stuff". I think that when one "drives" the ESP8266 hardware SPI, the SPI hardware knows to drive CS. I suspect that if we wanted to drive alternate devices, we would ignore the hardware CS and dedicate alternate pins to CS. |
||
* | ||
*/ | ||
void jshSPISetup( | ||
IOEventFlags device, //!< Unknown | ||
JshSPIInfo *inf //!< Unknown | ||
IOEventFlags device, //!< The identity of the SPI device being initialized. | ||
JshSPIInfo *inf //!< Flags for the SPI device. | ||
) { | ||
os_printf("ESP8266: jshSPISetup: device=%d, inf=0x%x\n", device, (int)inf); | ||
// The device should be one of EV_SPI1, EV_SPI2 or EV_SPI3. | ||
os_printf("> jshSPISetup - jshSPISetup: device=%d\n", device); | ||
switch(device) { | ||
case EV_SPI1: | ||
os_printf(" - Device is SPI1\n"); | ||
// EV_SPI1 is the ESP8266 hardware SPI ... | ||
spi_init(HSPI); // Initialize the hardware SPI components. | ||
spi_clock(HSPI, CPU_CLK_FREQ / (inf->baudRate * 2), 2); | ||
g_spiInitialized = true; | ||
g_lastSPIRead = -1; | ||
break; | ||
case EV_SPI2: | ||
os_printf(" - Device is SPI2\n"); | ||
break; | ||
case EV_SPI3: | ||
os_printf(" - Device is SPI3\n"); | ||
break; | ||
default: | ||
os_printf(" - Device is Unknown!!\n"); | ||
break; | ||
} | ||
if (inf != NULL) { | ||
os_printf("baudRate=%d, baudRateSpec=%d, pinSCK=%d, pinMISO=%d, pinMOSI=%d, spiMode=%d, spiMSB=%d\n", | ||
inf->baudRate, inf->baudRateSpec, inf->pinSCK, inf->pinMISO, inf->pinMOSI, inf->spiMode, inf->spiMSB); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems it would be good to return an error if the user specifies parameters that the implementation doesn't support. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These guards are an over-abundance of caution. The identies of available SPI interfaces are not user configurable but instead defined in the ESP8266_xxx.py files. Today we only have SPI1 but it is possible to have SPI2 and SPI3 as well. However, nothing a user can code at their JS level could result in triggering these alternate code paths. |
||
} | ||
os_printf("< jshSPISetup\n"); | ||
} | ||
|
||
/** Send data through the given SPI device (if data>=0), and return the result | ||
* of the previous send (or -1). If data<0, no data is sent and the function | ||
* waits for data to be returned */ | ||
int jshSPISend( | ||
IOEventFlags device, //!< Unknown | ||
int data //!< Unknown | ||
IOEventFlags device, //!< The identity of the SPI device through which data is being sent. | ||
int data //!< The data to be sent or an indication that no data is to be sent. | ||
) { | ||
os_printf("ESP8266: jshSPISend\n"); | ||
return NAN; | ||
if (device != EV_SPI1) { | ||
return -1; | ||
} | ||
//os_printf("> jshSPISend - device=%d, data=%x\n", device, data); | ||
int retData = g_lastSPIRead; | ||
if (data >=0) { | ||
g_lastSPIRead = spi_tx8(HSPI, data); | ||
} else { | ||
g_lastSPIRead = -1; | ||
} | ||
//os_printf("< jshSPISend\n"); | ||
return retData; | ||
} | ||
|
||
|
||
|
@@ -618,9 +668,15 @@ void jshSPISend16( | |
IOEventFlags device, //!< Unknown | ||
int data //!< Unknown | ||
) { | ||
os_printf("ESP8266: jshSPISend16\n"); | ||
jshSPISend(device, data >> 8); | ||
jshSPISend(device, data & 255); | ||
//os_printf("> jshSPISend16 - device=%d, data=%x\n", device, data); | ||
//jshSPISend(device, data >> 8); | ||
//jshSPISend(device, data & 255); | ||
if (device != EV_SPI1) { | ||
return; | ||
} | ||
|
||
spi_tx16(HSPI, data); | ||
//os_printf("< jshSPISend16\n"); | ||
} | ||
|
||
|
||
|
@@ -631,7 +687,8 @@ void jshSPISet16( | |
IOEventFlags device, //!< Unknown | ||
bool is16 //!< Unknown | ||
) { | ||
os_printf("ESP8266: jshSPISet16\n"); | ||
os_printf("> jshSPISet16 - device=%d, is16=%d\n", device, is16); | ||
os_printf("< jshSPISet16\n"); | ||
} | ||
|
||
|
||
|
@@ -641,11 +698,15 @@ void jshSPISet16( | |
void jshSPIWait( | ||
IOEventFlags device //!< Unknown | ||
) { | ||
os_printf("ESP8266: jshSPIWait\n"); | ||
os_printf("> jshSPIWait - device=%d\n", device); | ||
while(spi_busy(HSPI)) ; | ||
os_printf("< jshSPIWait\n"); | ||
} | ||
|
||
/** Set whether to use the receive interrupt or not */ | ||
void jshSPISetReceive(IOEventFlags device, bool isReceive) { | ||
os_printf("> jshSPISetReceive - device=%d, isReceive=%d\n", device, isReceive); | ||
os_printf("< jshSPISetReceive\n"); | ||
} | ||
|
||
//===== I2C ===== | ||
|
@@ -948,8 +1009,17 @@ void jshUtilTimerReschedule(JsSysTime period) { | |
//===== Miscellaneous ===== | ||
|
||
bool jshIsDeviceInitialised(IOEventFlags device) { | ||
os_printf("ESP8266: jshIsDeviceInitialised %d\n", device); | ||
return true; | ||
os_printf("> jshIsDeviceInitialised - %d\n", device); | ||
bool retVal = true; | ||
switch(device) { | ||
case EV_SPI1: | ||
retVal = g_spiInitialized; | ||
break; | ||
default: | ||
break; | ||
} | ||
os_printf("< jshIsDeviceInitialised - %d\n", retVal); | ||
return retVal; | ||
} // End of jshIsDeviceInitialised | ||
|
||
// the esp8266 doesn't have any temperature sensor | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my build system I found that during build, my Python environment was writing python cache information. I can't imagine that we would want that in our build system so added a gitignore for it. If that is wrong, we can remove it.