24
24
#include <espmissingincludes.h>
25
25
#include <uart.h>
26
26
#include <i2c_master.h>
27
+ #include <spi.h> // Include the MetalPhreak/ESP8266_SPI_Library headers.
27
28
28
29
//#define FAKE_STDLIB
29
30
#define _GCC_WRAP_STDINT_H
@@ -53,6 +54,10 @@ typedef long long int64_t;
53
54
// Address in RTC RAM where we save the time
54
55
#define RTC_TIME_ADDR (256/4) // start of "user data" in RTC RAM
55
56
57
+
58
+ static bool g_spiInitialized = false;
59
+ static int g_lastSPIRead = -1 ;
60
+
56
61
/**
57
62
* Transmit all the characters in the transmit buffer.
58
63
*
@@ -159,6 +164,8 @@ void jshReset() {
159
164
jshPinSetState(i, JSHPINSTATE_GPIO_IN);
160
165
}
161
166
*/
167
+ g_spiInitialized = false; // Flag the hardware SPI interface as un-initialized.
168
+ g_lastSPIRead = -1 ;
162
169
os_printf ("< jshReset\n" );
163
170
} // End of jshReset
164
171
@@ -588,26 +595,69 @@ void jshUSARTKick(
588
595
//===== SPI =====
589
596
590
597
/**
591
- * Unknown
598
+ * Initialize the hardware SPI device.
599
+ * On the ESP8266, hardware SPI is implemented via a set of pins defined
600
+ * as follows:
592
601
*
602
+ * | GPIO | NodeMCU | Name | Function |
603
+ * |--------|---------|-------|----------|
604
+ * | GPIO12 | D6 | HMISO | MISO |
605
+ * | GPIO13 | D7 | HMOSI | MOSI |
606
+ * | GPIO14 | D5 | HSCLK | CLK |
607
+ * | GPIO15 | D8 | HCS | CS |
593
608
*
594
609
*/
595
610
void jshSPISetup (
596
- IOEventFlags device , //!< Unknown
597
- JshSPIInfo * inf //!< Unknown
611
+ IOEventFlags device , //!< The identity of the SPI device being initialized.
612
+ JshSPIInfo * inf //!< Flags for the SPI device.
598
613
) {
599
- os_printf ("ESP8266: jshSPISetup: device=%d, inf=0x%x\n" , device , (int )inf );
614
+ // The device should be one of EV_SPI1, EV_SPI2 or EV_SPI3.
615
+ os_printf ("> jshSPISetup - jshSPISetup: device=%d\n" , device );
616
+ switch (device ) {
617
+ case EV_SPI1 :
618
+ os_printf (" - Device is SPI1\n" );
619
+ // EV_SPI1 is the ESP8266 hardware SPI ...
620
+ spi_init (HSPI ); // Initialize the hardware SPI components.
621
+ spi_clock (HSPI , CPU_CLK_FREQ / (inf -> baudRate * 2 ), 2 );
622
+ g_spiInitialized = true;
623
+ g_lastSPIRead = -1 ;
624
+ break ;
625
+ case EV_SPI2 :
626
+ os_printf (" - Device is SPI2\n" );
627
+ break ;
628
+ case EV_SPI3 :
629
+ os_printf (" - Device is SPI3\n" );
630
+ break ;
631
+ default :
632
+ os_printf (" - Device is Unknown!!\n" );
633
+ break ;
634
+ }
635
+ if (inf != NULL ) {
636
+ os_printf ("baudRate=%d, baudRateSpec=%d, pinSCK=%d, pinMISO=%d, pinMOSI=%d, spiMode=%d, spiMSB=%d\n" ,
637
+ inf -> baudRate , inf -> baudRateSpec , inf -> pinSCK , inf -> pinMISO , inf -> pinMOSI , inf -> spiMode , inf -> spiMSB );
638
+ }
639
+ os_printf ("< jshSPISetup\n" );
600
640
}
601
641
602
642
/** Send data through the given SPI device (if data>=0), and return the result
603
643
* of the previous send (or -1). If data<0, no data is sent and the function
604
644
* waits for data to be returned */
605
645
int jshSPISend (
606
- IOEventFlags device , //!< Unknown
607
- int data //!< Unknown
646
+ IOEventFlags device , //!< The identity of the SPI device through which data is being sent.
647
+ int data //!< The data to be sent or an indication that no data is to be sent.
608
648
) {
609
- os_printf ("ESP8266: jshSPISend\n" );
610
- return NAN ;
649
+ if (device != EV_SPI1 ) {
650
+ return -1 ;
651
+ }
652
+ //os_printf("> jshSPISend - device=%d, data=%x\n", device, data);
653
+ int retData = g_lastSPIRead ;
654
+ if (data >=0 ) {
655
+ g_lastSPIRead = spi_tx8 (HSPI , data );
656
+ } else {
657
+ g_lastSPIRead = -1 ;
658
+ }
659
+ //os_printf("< jshSPISend\n");
660
+ return retData ;
611
661
}
612
662
613
663
@@ -618,9 +668,15 @@ void jshSPISend16(
618
668
IOEventFlags device , //!< Unknown
619
669
int data //!< Unknown
620
670
) {
621
- os_printf ("ESP8266: jshSPISend16\n" );
622
- jshSPISend (device , data >> 8 );
623
- jshSPISend (device , data & 255 );
671
+ //os_printf("> jshSPISend16 - device=%d, data=%x\n", device, data);
672
+ //jshSPISend(device, data >> 8);
673
+ //jshSPISend(device, data & 255);
674
+ if (device != EV_SPI1 ) {
675
+ return ;
676
+ }
677
+
678
+ spi_tx16 (HSPI , data );
679
+ //os_printf("< jshSPISend16\n");
624
680
}
625
681
626
682
@@ -631,7 +687,8 @@ void jshSPISet16(
631
687
IOEventFlags device , //!< Unknown
632
688
bool is16 //!< Unknown
633
689
) {
634
- os_printf ("ESP8266: jshSPISet16\n" );
690
+ os_printf ("> jshSPISet16 - device=%d, is16=%d\n" , device , is16 );
691
+ os_printf ("< jshSPISet16\n" );
635
692
}
636
693
637
694
@@ -641,11 +698,15 @@ void jshSPISet16(
641
698
void jshSPIWait (
642
699
IOEventFlags device //!< Unknown
643
700
) {
644
- os_printf ("ESP8266: jshSPIWait\n" );
701
+ os_printf ("> jshSPIWait - device=%d\n" , device );
702
+ while (spi_busy (HSPI )) ;
703
+ os_printf ("< jshSPIWait\n" );
645
704
}
646
705
647
706
/** Set whether to use the receive interrupt or not */
648
707
void jshSPISetReceive (IOEventFlags device , bool isReceive ) {
708
+ os_printf ("> jshSPISetReceive - device=%d, isReceive=%d\n" , device , isReceive );
709
+ os_printf ("< jshSPISetReceive\n" );
649
710
}
650
711
651
712
//===== I2C =====
@@ -948,8 +1009,17 @@ void jshUtilTimerReschedule(JsSysTime period) {
948
1009
//===== Miscellaneous =====
949
1010
950
1011
bool jshIsDeviceInitialised (IOEventFlags device ) {
951
- os_printf ("ESP8266: jshIsDeviceInitialised %d\n" , device );
952
- return true;
1012
+ os_printf ("> jshIsDeviceInitialised - %d\n" , device );
1013
+ bool retVal = true;
1014
+ switch (device ) {
1015
+ case EV_SPI1 :
1016
+ retVal = g_spiInitialized ;
1017
+ break ;
1018
+ default :
1019
+ break ;
1020
+ }
1021
+ os_printf ("< jshIsDeviceInitialised - %d\n" , retVal );
1022
+ return retVal ;
953
1023
} // End of jshIsDeviceInitialised
954
1024
955
1025
// the esp8266 doesn't have any temperature sensor
0 commit comments