Skip to content

Commit 471b8e1

Browse files
committed
Merge pull request #692 from nkolban/master
#606. Initial framework for hardware SPI for ESP8266.
2 parents 11ec021 + 71d1975 commit 471b8e1

File tree

14 files changed

+950
-29
lines changed

14 files changed

+950
-29
lines changed

Makefile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ else ifdef ESP8266_512KB
506506
# into ram (dram+iram) and the last 16KB are reserved for the SDK. That leaves 432KB (0x6C000).
507507
EMBEDDED=1
508508
USE_NET=1
509+
#USE_FILESYSTEM=1
509510
BOARD=ESP8266_BOARD
510511
# We have to disable inlining to keep code size in check
511512
OPTIMIZEFLAGS+=-Os -fno-inline-functions -std=gnu11 -fgnu89-inline -Wl,--allow-multiple-definition
@@ -973,7 +974,8 @@ libs/network/js/network_js.c
973974

974975
ifdef USE_ESP8266
975976
DEFINES += -DUSE_ESP8266
976-
WRAPPERSOURCES += libs/network/esp8266/jswrap_esp8266.c
977+
WRAPPERSOURCES += libs/network/esp8266/jswrap_esp8266_network.c \
978+
targets/esp8266/jswrap_esp8266.c
977979
INCLUDE += -I$(ROOT)/libs/network/esp8266
978980
SOURCES += \
979981
libs/network/esp8266/network_esp8266.c\
@@ -1497,11 +1499,12 @@ LDFLAGS += -L$(ESP8266_SDK_ROOT)/lib \
14971499

14981500
# Extra source files specific to the ESP8266
14991501
SOURCES += targets/esp8266/uart.c \
1500-
targets/esp8266/user_main.c \
1501-
targets/esp8266/jshardware.c \
1502-
targets/esp8266/i2c_master.c \
1503-
targets/esp8266/esp8266_board_utils.c \
1504-
libs/network/esp8266/network_esp8266.c
1502+
targets/esp8266/spi.c \
1503+
targets/esp8266/user_main.c \
1504+
targets/esp8266/jshardware.c \
1505+
targets/esp8266/i2c_master.c \
1506+
targets/esp8266/esp8266_board_utils.c \
1507+
libs/network/esp8266/network_esp8266.c
15051508
# if using the hw_timer: targets/esp8266/hw_timer.c \
15061509
15071510
# The tool used for building the firmware and flashing
@@ -1606,6 +1609,7 @@ proj: $(PROJ_NAME).elf $(PROJ_NAME)_0x00000.bin $(PROJ_NAME)_0x10000.bin $(PROJ_
16061609
$(PROJ_NAME).elf: $(OBJS) $(LINKER_FILE)
16071610
$(Q)$(LD) $(OPTIMIZEFLAGS) -nostdlib -Wl,--no-check-sections -Wl,-static -r -o partial.o $(OBJS)
16081611
$(Q)$(OBJCOPY) --rename-section .text=.irom0.text --rename-section .literal=.irom0.literal partial.o
1612+
$(Q)$(OBJCOPY) --rename-section .force.text=.text partial.o
16091613
$(Q)$(LD) $(LDFLAGS) -Ttargets/esp8266/eagle.app.v6.0x10000.ld -o $@ partial.o -Wl,--start-group $(LIBS) -Wl,--end-group
16101614
$(Q)rm partial.o
16111615
$(Q)$(OBJDUMP) --headers -j .irom0.text -j .text $(PROJ_NAME).elf | tail -n +4

boards/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/__pycache__/

boards/ESP8266_12.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
'flash' : 1024,
3636
'speed' : 80,
3737
'usart' : 1,
38-
'spi' : 0,
38+
'spi' : 1,
3939
'i2c' : 1,
4040
'adc' : 1,
4141
'dac' : 0,

boards/ESP8266_BOARD.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
'flash' : 512,
3030
'speed' : 80,
3131
'usart' : 1,
32-
'spi' : 0,
32+
'spi' : 1,
3333
'i2c' : 1,
3434
'adc' : 1,
3535
'dac' : 0,

libs/network/esp8266/jswrap_esp8266.c renamed to libs/network/esp8266/jswrap_esp8266_network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ of beta. */
3434
#define _GCC_WRAP_STDINT_H
3535
typedef long long int64_t;
3636

37-
#include "jswrap_esp8266.h"
37+
#include "jswrap_esp8266_network.h"
3838
#include "jsinteractive.h" // Pull inn the jsiConsolePrint function
3939
#include "network.h"
4040
#include "network_esp8266.h"

libs/network/esp8266/jswrap_esp8266.h renamed to libs/network/esp8266/jswrap_esp8266_network.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* ----------------------------------------------------------------------------
1515
*/
1616

17-
#ifndef LIBS_NETWORK_ESP8266_JSWRAP_ESP8266_H_
18-
#define LIBS_NETWORK_ESP8266_JSWRAP_ESP8266_H_
17+
#ifndef LIBS_NETWORK_ESP8266_JSWRAP_ESP8266_NETWORK_H_
18+
#define LIBS_NETWORK_ESP8266_JSWRAP_ESP8266_NETWORK_H_
1919
#include "jsvar.h"
2020

2121
// Deprecated
@@ -67,4 +67,4 @@ void jswrap_ESP8266_updateCPUFreq(JsVar *jsFreq);
6767

6868
void jswrap_ESP8266_init();
6969

70-
#endif /* LIBS_NETWORK_ESP8266_JSWRAP_ESP8266_H_ */
70+
#endif /* LIBS_NETWORK_ESP8266_JSWRAP_ESP8266_NETWORK_H_ */

src/jsdevices.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ volatile unsigned char ioHead=0, ioTail=0;
7171
* Initialize all the devices.
7272
*/
7373
void jshInitDevices() { // called from jshInit
74-
int i;
74+
unsigned int i;
7575
// setup flow control
7676
for (i=0;i<sizeof(jshSerialDeviceStates) / sizeof(JshSerialDeviceState);i++)
7777
jshSerialDeviceStates[i] = SDS_NONE;

targets/esp8266/jshardware.c

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <espmissingincludes.h>
2525
#include <uart.h>
2626
#include <i2c_master.h>
27+
#include <spi.h> // Include the MetalPhreak/ESP8266_SPI_Library headers.
2728

2829
//#define FAKE_STDLIB
2930
#define _GCC_WRAP_STDINT_H
@@ -53,6 +54,10 @@ typedef long long int64_t;
5354
// Address in RTC RAM where we save the time
5455
#define RTC_TIME_ADDR (256/4) // start of "user data" in RTC RAM
5556

57+
58+
static bool g_spiInitialized = false;
59+
static int g_lastSPIRead = -1;
60+
5661
/**
5762
* Transmit all the characters in the transmit buffer.
5863
*
@@ -159,6 +164,8 @@ void jshReset() {
159164
jshPinSetState(i, JSHPINSTATE_GPIO_IN);
160165
}
161166
*/
167+
g_spiInitialized = false; // Flag the hardware SPI interface as un-initialized.
168+
g_lastSPIRead = -1;
162169
os_printf("< jshReset\n");
163170
} // End of jshReset
164171

@@ -588,26 +595,69 @@ void jshUSARTKick(
588595
//===== SPI =====
589596

590597
/**
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:
592601
*
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 |
593608
*
594609
*/
595610
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.
598613
) {
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");
600640
}
601641

602642
/** Send data through the given SPI device (if data>=0), and return the result
603643
* of the previous send (or -1). If data<0, no data is sent and the function
604644
* waits for data to be returned */
605645
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.
608648
) {
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;
611661
}
612662

613663

@@ -618,9 +668,15 @@ void jshSPISend16(
618668
IOEventFlags device, //!< Unknown
619669
int data //!< Unknown
620670
) {
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");
624680
}
625681

626682

@@ -631,7 +687,8 @@ void jshSPISet16(
631687
IOEventFlags device, //!< Unknown
632688
bool is16 //!< Unknown
633689
) {
634-
os_printf("ESP8266: jshSPISet16\n");
690+
os_printf("> jshSPISet16 - device=%d, is16=%d\n", device, is16);
691+
os_printf("< jshSPISet16\n");
635692
}
636693

637694

@@ -641,11 +698,15 @@ void jshSPISet16(
641698
void jshSPIWait(
642699
IOEventFlags device //!< Unknown
643700
) {
644-
os_printf("ESP8266: jshSPIWait\n");
701+
os_printf("> jshSPIWait - device=%d\n", device);
702+
while(spi_busy(HSPI)) ;
703+
os_printf("< jshSPIWait\n");
645704
}
646705

647706
/** Set whether to use the receive interrupt or not */
648707
void jshSPISetReceive(IOEventFlags device, bool isReceive) {
708+
os_printf("> jshSPISetReceive - device=%d, isReceive=%d\n", device, isReceive);
709+
os_printf("< jshSPISetReceive\n");
649710
}
650711

651712
//===== I2C =====
@@ -948,8 +1009,17 @@ void jshUtilTimerReschedule(JsSysTime period) {
9481009
//===== Miscellaneous =====
9491010

9501011
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;
9531023
} // End of jshIsDeviceInitialised
9541024

9551025
// the esp8266 doesn't have any temperature sensor

0 commit comments

Comments
 (0)