mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Merge branch 'master' into ESP8266
This commit is contained in:
commit
09b834f755
@ -1,4 +1,4 @@
|
||||
1v71 : ...
|
||||
1v71 : Allowed WIZnet + CC3000 to be instantiated on any pins
|
||||
|
||||
1v70 : Make pipe remove its drain/close listeners. Stops out of memory for repeated piping.
|
||||
Fix parseInt for values too large to go in an int (#406)
|
||||
|
||||
@ -59,8 +59,8 @@ void SpiWriteDataSynchronous(unsigned char *data, unsigned short size);
|
||||
void SpiPauseSpi(void);
|
||||
void cc3000_spi_resume(void);
|
||||
|
||||
#define ASSERT_CS() jshPinSetValue(WLAN_CS_PIN, 0)
|
||||
#define DEASSERT_CS() jshPinSetValue(WLAN_CS_PIN, 1)
|
||||
#define ASSERT_CS() jshPinSetValue(networkGetCurrent()->data.pinCS, 0)
|
||||
#define DEASSERT_CS() jshPinSetValue(networkGetCurrent()->data.pinCS, 1)
|
||||
|
||||
|
||||
// The magic number that resides at the end of the TX/RX buffer (1 byte after
|
||||
@ -76,23 +76,13 @@ bool cc3000_spi_inited = false;
|
||||
|
||||
void cc3000_spi_open(void)
|
||||
{
|
||||
// SPI config
|
||||
JshSPIInfo inf;
|
||||
jshSPIInitInfo(&inf);
|
||||
inf.pinSCK = WLAN_CLK_PIN;
|
||||
inf.pinMISO = WLAN_MISO_PIN;
|
||||
inf.pinMOSI = WLAN_MOSI_PIN;
|
||||
inf.baudRate = 1000000;
|
||||
inf.spiMode = SPIF_SPI_MODE_1; // Mode 1 CPOL= 0 CPHA= 1
|
||||
jshSPISetup(WLAN_SPI, &inf);
|
||||
|
||||
// WLAN CS, EN and WALN IRQ Configuration
|
||||
jshSetPinStateIsManual(WLAN_CS_PIN, false);
|
||||
jshPinOutput(WLAN_CS_PIN, 1); // de-assert CS
|
||||
jshSetPinStateIsManual(WLAN_EN_PIN, false);
|
||||
jshPinOutput(WLAN_EN_PIN, 0); // disable WLAN
|
||||
jshSetPinStateIsManual(WLAN_IRQ_PIN, true);
|
||||
jshPinSetState(WLAN_IRQ_PIN, JSHPINSTATE_GPIO_IN_PULLUP); // flip into read mode with pullup
|
||||
// WLAN CS, EN and WLAN IRQ Configuration
|
||||
jshSetPinStateIsManual(networkGetCurrent()->data.pinCS, false);
|
||||
jshPinOutput(networkGetCurrent()->data.pinCS, 1); // de-assert CS
|
||||
jshSetPinStateIsManual(networkGetCurrent()->data.pinEN, false);
|
||||
jshPinOutput(networkGetCurrent()->data.pinEN, 0); // disable WLAN
|
||||
jshSetPinStateIsManual(networkGetCurrent()->data.pinIRQ, true);
|
||||
jshPinSetState(networkGetCurrent()->data.pinIRQ, JSHPINSTATE_GPIO_IN_PULLUP); // flip into read mode with pullup
|
||||
|
||||
// wait a little (ensure that WLAN takes effect)
|
||||
jshDelayMicroseconds(500*1000); // force a 500ms delay! FIXME
|
||||
@ -221,7 +211,7 @@ SpiWriteDataSynchronous(unsigned char *data, unsigned short size)
|
||||
|
||||
int bSend = 0, bRecv = 0;
|
||||
while ((bSend<size || bRecv<size) && !jspIsInterrupted()) {
|
||||
int r = jshSPISend(WLAN_SPI, (bSend<size)?data[bSend]:-1);
|
||||
int r = jshSPISend(networkGetCurrent()->data.device, (bSend<size)?data[bSend]:-1);
|
||||
bSend++;
|
||||
if (bSend>0 && r>=0) bRecv++;
|
||||
}
|
||||
@ -237,7 +227,7 @@ SpiReadDataSynchronous(unsigned char *data, unsigned short size)
|
||||
|
||||
int bSend = 0, bRecv = 0;
|
||||
while ((bSend<size || bRecv<size) && !jspIsInterrupted()) {
|
||||
int r = jshSPISend(WLAN_SPI, (bSend<size)?READ:-1);
|
||||
int r = jshSPISend(networkGetCurrent()->data.device, (bSend<size)?READ:-1);
|
||||
bSend++;
|
||||
if (bSend>0 && r>=0) data[bRecv++] = (unsigned char)r;
|
||||
}
|
||||
@ -359,7 +349,7 @@ void cc3000_irq_handler_x(void)
|
||||
|
||||
long cc3000_read_irq_pin(void)
|
||||
{
|
||||
return jshPinGetValue(WLAN_IRQ_PIN);
|
||||
return jshPinGetValue(networkGetCurrent()->data.pinIRQ);
|
||||
}
|
||||
|
||||
void cc3000_irq_enable(void) {
|
||||
@ -431,7 +421,7 @@ const unsigned char *sendNoPatch(unsigned long *Length) {
|
||||
|
||||
void cc3000_write_en_pin( unsigned char val )
|
||||
{
|
||||
jshPinOutput(WLAN_EN_PIN, val == WLAN_ENABLE);
|
||||
jshPinOutput(networkGetCurrent()->data.pinEN, val == WLAN_ENABLE);
|
||||
}
|
||||
|
||||
void cc3000_initialise(JsVar *wlanObj) {
|
||||
|
||||
@ -30,14 +30,6 @@ extern int errno;
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define WLAN_SPI EV_SPI3
|
||||
#define WLAN_CLK_PIN (Pin)(JSH_PORTB_OFFSET + 3)
|
||||
#define WLAN_MISO_PIN (Pin)(JSH_PORTB_OFFSET + 4)
|
||||
#define WLAN_MOSI_PIN (Pin)(JSH_PORTB_OFFSET + 5)
|
||||
#define WLAN_EN_PIN (Pin)(JSH_PORTB_OFFSET + 7) // active high
|
||||
#define WLAN_IRQ_PIN (Pin)(JSH_PORTB_OFFSET + 8) // active low
|
||||
#define WLAN_CS_PIN (Pin)(JSH_PORTB_OFFSET + 6) // active low
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL (uint32_t)0x00
|
||||
#endif
|
||||
|
||||
@ -11,8 +11,7 @@
|
||||
* This file is designed to be parsed during the build process
|
||||
*
|
||||
* Contains built-in functions for CC3000 WiFi Access
|
||||
* EXTREMELY BETA AND LIKELY TO CHANGE DRASTICALLY
|
||||
* ----------------------------------------------------------------------------
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include "jswrap_cc3000.h"
|
||||
@ -26,6 +25,16 @@
|
||||
#include "cc3000/hci.h"
|
||||
|
||||
|
||||
// ------------------------------ defaults
|
||||
#define WLAN_SPI EV_SPI3
|
||||
#define WLAN_CLK_PIN (Pin)(JSH_PORTB_OFFSET + 3)
|
||||
#define WLAN_MISO_PIN (Pin)(JSH_PORTB_OFFSET + 4)
|
||||
#define WLAN_MOSI_PIN (Pin)(JSH_PORTB_OFFSET + 5)
|
||||
#define WLAN_EN_PIN (Pin)(JSH_PORTB_OFFSET + 7) // active high
|
||||
#define WLAN_IRQ_PIN (Pin)(JSH_PORTB_OFFSET + 8) // active low
|
||||
#define WLAN_CS_PIN (Pin)(JSH_PORTB_OFFSET + 6) // active low
|
||||
// -------------------------------------------
|
||||
|
||||
/*JSON{
|
||||
"type" : "library",
|
||||
"class" : "CC3000"
|
||||
@ -38,16 +47,57 @@
|
||||
"name" : "connect",
|
||||
"generate" : "jswrap_cc3000_connect",
|
||||
"params" : [
|
||||
|
||||
["spi", "JsVar", "Device to use for SPI (or undefined to use the default). SPI should be 1,000,000 baud, and set to 'mode 1'"],
|
||||
["cs", "pin", "The pin to use for Chip Select"],
|
||||
["en", "pin", "The pin to use for Enable"],
|
||||
["irq", "pin", "The pin to use for Interrupts"]
|
||||
],
|
||||
"return" : ["JsVar","A WLAN Object"],
|
||||
"return_object" : "WLAN"
|
||||
}
|
||||
Initialise the CC3000 and return a WLAN object
|
||||
*/
|
||||
JsVar *jswrap_cc3000_connect() {
|
||||
JsVar *jswrap_cc3000_connect(JsVar *spi, Pin cs, Pin en, Pin irq) {
|
||||
IOEventFlags spiDevice;
|
||||
if (spi) {
|
||||
spiDevice = jsiGetDeviceFromClass(spi);
|
||||
if (!DEVICE_IS_SPI(spiDevice)) {
|
||||
jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
// SPI config
|
||||
// SPI config
|
||||
JshSPIInfo inf;
|
||||
jshSPIInitInfo(&inf);
|
||||
inf.pinSCK = WLAN_CLK_PIN;
|
||||
inf.pinMISO = WLAN_MISO_PIN;
|
||||
inf.pinMOSI = WLAN_MOSI_PIN;
|
||||
inf.baudRate = 1000000;
|
||||
inf.spiMode = SPIF_SPI_MODE_1; // Mode 1 CPOL= 0 CPHA= 1
|
||||
jshSPISetup(WLAN_SPI, &inf);
|
||||
spiDevice = WLAN_SPI;
|
||||
}
|
||||
if (!jshIsPinValid(cs))
|
||||
cs = WLAN_CS_PIN;
|
||||
if (!jshIsPinValid(en))
|
||||
en = WLAN_EN_PIN;
|
||||
if (!jshIsPinValid(irq))
|
||||
irq = WLAN_IRQ_PIN;
|
||||
|
||||
JsNetwork net;
|
||||
networkCreate(&net, JSNETWORKTYPE_CC3000);
|
||||
net.data.device = spiDevice;
|
||||
net.data.pinCS = cs;
|
||||
net.data.pinEN = en;
|
||||
net.data.pinIRQ = irq;
|
||||
networkSet(&net);
|
||||
|
||||
JsVar *wlanObj = jspNewObject(0, "WLAN");
|
||||
cc3000_initialise(wlanObj);
|
||||
|
||||
networkFree(&net);
|
||||
|
||||
return wlanObj;
|
||||
}
|
||||
|
||||
@ -77,6 +127,10 @@ bool jswrap_wlan_connect(JsVar *wlanObj, JsVar *vAP, JsVar *vKey, JsVar *callbac
|
||||
jsError("Expecting callback Function but got %t", callback);
|
||||
return 0;
|
||||
}
|
||||
|
||||
JsNetwork net;
|
||||
if (!networkGetFromVar(&net)) return false;
|
||||
|
||||
// if previously completely disconnected, try and reconnect
|
||||
if (jsvGetBoolAndUnLock(jsvObjectGetChild(wlanObj,JS_HIDDEN_CHAR_STR"DISC",0))) {
|
||||
cc3000_initialise(wlanObj);
|
||||
@ -101,11 +155,7 @@ bool jswrap_wlan_connect(JsVar *wlanObj, JsVar *vAP, JsVar *vKey, JsVar *callbac
|
||||
// might want to set wlan_ioctl_set_connection_policy
|
||||
bool connected = wlan_connect(security, ap, (long)strlen(ap), NULL, (unsigned char*)key, (long)strlen(key))==0;
|
||||
|
||||
if (connected) {
|
||||
JsNetwork net;
|
||||
networkCreate(&net, JSNETWORKTYPE_CC3000);
|
||||
networkFree(&net);
|
||||
}
|
||||
networkFree(&net);
|
||||
// note that we're only online (for networkState) when DHCP succeeds
|
||||
return connected;
|
||||
}
|
||||
@ -119,10 +169,15 @@ bool jswrap_wlan_connect(JsVar *wlanObj, JsVar *vAP, JsVar *vKey, JsVar *callbac
|
||||
Completely uninitialise and power down the CC3000. After this you'll have to use ```require("CC3000").connect()``` again.
|
||||
*/
|
||||
void jswrap_wlan_disconnect(JsVar *wlanObj) {
|
||||
JsNetwork net;
|
||||
if (!networkGetFromVar(&net)) return;
|
||||
|
||||
jsvUnLock(jsvObjectSetChild(wlanObj,JS_HIDDEN_CHAR_STR"DISC", jsvNewFromBool(true)));
|
||||
networkState = NETWORKSTATE_OFFLINE; // force offline
|
||||
//wlan_disconnect();
|
||||
wlan_stop();
|
||||
|
||||
networkFree(&net);
|
||||
}
|
||||
|
||||
/*JSON{
|
||||
@ -134,6 +189,9 @@ void jswrap_wlan_disconnect(JsVar *wlanObj) {
|
||||
Completely uninitialise and power down the CC3000, then reconnect to the old access point.
|
||||
*/
|
||||
void jswrap_wlan_reconnect(JsVar *wlanObj) {
|
||||
JsNetwork net;
|
||||
if (!networkGetFromVar(&net)) return;
|
||||
|
||||
JsVar *ap = jsvObjectGetChild(wlanObj,JS_HIDDEN_CHAR_STR"AP", 0);
|
||||
JsVar *key = jsvObjectGetChild(wlanObj,JS_HIDDEN_CHAR_STR"KEY", 0);
|
||||
JsVar *cb = jsvObjectGetChild(wlanObj,CC3000_ON_STATE_CHANGE, 0);
|
||||
@ -142,6 +200,8 @@ void jswrap_wlan_reconnect(JsVar *wlanObj) {
|
||||
jsvUnLock(ap);
|
||||
jsvUnLock(key);
|
||||
jsvUnLock(cb);
|
||||
|
||||
networkFree(&net);
|
||||
}
|
||||
|
||||
|
||||
@ -163,8 +223,13 @@ JsVar *jswrap_wlan_getIP(JsVar *wlanObj) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
JsNetwork net;
|
||||
if (!networkGetFromVar(&net)) return 0;
|
||||
|
||||
tNetappIpconfigRetArgs ipconfig;
|
||||
netapp_ipconfig(&ipconfig);
|
||||
|
||||
networkFree(&net);
|
||||
/* If byte 1 is 0 we don't have a valid address */
|
||||
if (ipconfig.aucIP[3] == 0) return 0;
|
||||
JsVar *data = jsvNewWithFlags(JSV_OBJECT);
|
||||
@ -174,6 +239,7 @@ JsVar *jswrap_wlan_getIP(JsVar *wlanObj) {
|
||||
networkPutAddressAsString(data, "dhcp", &ipconfig.aucDHCPServer[0], -4, 10, '.');
|
||||
networkPutAddressAsString(data, "dns", &ipconfig.aucDNSServer[0], -4, 10, '.');
|
||||
networkPutAddressAsString(data, "mac", &ipconfig.uaMacAddr[0], -6, 16, 0);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@ -210,6 +276,9 @@ bool jswrap_wlan_setIP(JsVar *wlanObj, JsVar *options) {
|
||||
return false;
|
||||
}
|
||||
|
||||
JsNetwork net;
|
||||
if (!networkGetFromVar(&net)) return false;
|
||||
|
||||
tNetappIpconfigRetArgs ipconfig;
|
||||
netapp_ipconfig(&ipconfig);
|
||||
|
||||
@ -225,10 +294,14 @@ bool jswrap_wlan_setIP(JsVar *wlanObj, JsVar *options) {
|
||||
*((unsigned long*)&ipconfig.aucDefaultGateway) = 0;
|
||||
}
|
||||
|
||||
return netapp_dhcp(
|
||||
bool result = netapp_dhcp(
|
||||
(unsigned long *)&ipconfig.aucIP[0],
|
||||
(unsigned long *)&ipconfig.aucSubnetMask[0],
|
||||
(unsigned long *)&ipconfig.aucDefaultGateway[0],
|
||||
(unsigned long *)&ipconfig.aucDNSServer[0]) == 0;
|
||||
|
||||
networkFree(&net);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@ -12,8 +12,9 @@
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
#include "jsvar.h"
|
||||
#include "jspin.h"
|
||||
|
||||
JsVar *jswrap_cc3000_connect();
|
||||
JsVar *jswrap_cc3000_connect(JsVar *spi, Pin cs, Pin en, Pin irq);
|
||||
bool jswrap_wlan_connect(JsVar *wlanObj, JsVar *vAP, JsVar *vKey, JsVar *callback);
|
||||
void jswrap_wlan_disconnect(JsVar *wlanObj);
|
||||
void jswrap_wlan_reconnect(JsVar *wlanObj);
|
||||
|
||||
@ -35,6 +35,8 @@ JsNetworkState networkState =
|
||||
#endif
|
||||
;
|
||||
|
||||
JsNetwork *networkCurrentStruct = 0;
|
||||
|
||||
unsigned long networkParseIPAddress(const char *ip) {
|
||||
int n = 0;
|
||||
unsigned long addr = 0;
|
||||
@ -103,6 +105,10 @@ void networkGetHostByName(JsNetwork *net, char * hostName, unsigned long* out_ip
|
||||
void networkCreate(JsNetwork *net, JsNetworkType type) {
|
||||
net->networkVar = jsvNewStringOfLength(sizeof(JsNetworkData));
|
||||
net->data.type = type;
|
||||
net->data.device = EV_NONE;
|
||||
net->data.pinCS = PIN_UNDEFINED;
|
||||
net->data.pinIRQ = PIN_UNDEFINED;
|
||||
net->data.pinEN = PIN_UNDEFINED;
|
||||
jsvUnLock(jsvObjectSetChild(execInfo.hiddenRoot, NETWORK_VAR_NAME, net->networkVar));
|
||||
networkSet(net);
|
||||
networkGetFromVar(net);
|
||||
@ -138,6 +144,7 @@ bool networkGetFromVar(JsNetwork *net) {
|
||||
networkFree(net);
|
||||
return false;
|
||||
}
|
||||
networkCurrentStruct = net;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -156,5 +163,10 @@ void networkSet(JsNetwork *net) {
|
||||
}
|
||||
|
||||
void networkFree(JsNetwork *net) {
|
||||
networkCurrentStruct = 0;
|
||||
jsvUnLock(net->networkVar);
|
||||
}
|
||||
|
||||
JsNetwork *networkGetCurrent() {
|
||||
return networkCurrentStruct;
|
||||
}
|
||||
|
||||
@ -41,8 +41,8 @@ typedef enum {
|
||||
typedef struct {
|
||||
JsNetworkType type;
|
||||
// Info for accessing specific devices
|
||||
//IOEventFlags spi;
|
||||
//Pin pinCS, pinIRQ, pinEN;
|
||||
IOEventFlags device;
|
||||
Pin pinCS, pinIRQ, pinEN;
|
||||
} PACKED_FLAGS JsNetworkData;
|
||||
|
||||
|
||||
@ -78,6 +78,8 @@ bool networkGetFromVar(JsNetwork *net);
|
||||
bool networkGetFromVarIfOnline(JsNetwork *net); // only return true (and network) if we're online, otherwise warn
|
||||
void networkSet(JsNetwork *net);
|
||||
void networkFree(JsNetwork *net);
|
||||
|
||||
JsNetwork *networkGetCurrent(); ///< Get the currently active network structure. can be 0!
|
||||
// ---------------------------------------------------------
|
||||
|
||||
/// Use this for getting the hostname, as it parses the name to see if it is an IP address first
|
||||
|
||||
@ -10,8 +10,7 @@
|
||||
* ----------------------------------------------------------------------------
|
||||
* This file is designed to be parsed during the build process
|
||||
*
|
||||
* Contains built-in functions for CC3000 WiFi Access
|
||||
* EXTREMELY BETA AND LIKELY TO CHANGE DRASTICALLY
|
||||
* Contains built-in functions for WIZnet Ethernet Access
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
@ -25,35 +24,36 @@
|
||||
#include "network_wiznet.h"
|
||||
#include "DHCP/dhcp.h"
|
||||
|
||||
// -------------------- defaults...
|
||||
#define ETH_SPI EV_SPI3
|
||||
#define ETH_CS_PIN (Pin)(JSH_PORTB_OFFSET + 2) // active low
|
||||
#define ETH_CLK_PIN (Pin)(JSH_PORTB_OFFSET + 3)
|
||||
#define ETH_MISO_PIN (Pin)(JSH_PORTB_OFFSET + 4)
|
||||
#define ETH_MOSI_PIN (Pin)(JSH_PORTB_OFFSET + 5)
|
||||
// -------------------------------
|
||||
|
||||
void wizchip_select(void)
|
||||
{
|
||||
jshPinOutput(ETH_CS_PIN, 0); // active low
|
||||
void wizchip_select(void) {
|
||||
assert(networkGetCurrent());
|
||||
jshPinOutput(networkGetCurrent()->data.pinCS, 0); // active low
|
||||
}
|
||||
|
||||
void wizchip_deselect(void)
|
||||
{
|
||||
jshPinOutput(ETH_CS_PIN, 1); // active low
|
||||
void wizchip_deselect(void) {
|
||||
assert(networkGetCurrent());
|
||||
jshPinOutput(networkGetCurrent()->data.pinCS, 1); // active low
|
||||
}
|
||||
|
||||
static uint8_t wizchip_rw(uint8_t data) {
|
||||
int r = jshSPISend(ETH_SPI, data);
|
||||
if (r<0) r = jshSPISend(ETH_SPI, -1);
|
||||
assert(networkGetCurrent());
|
||||
int r = jshSPISend(networkGetCurrent()->data.device, data);
|
||||
if (r<0) r = jshSPISend(networkGetCurrent()->data.device, -1);
|
||||
return (uint8_t)r;
|
||||
}
|
||||
|
||||
void wizchip_write(uint8_t wb)
|
||||
{
|
||||
void wizchip_write(uint8_t wb) {
|
||||
wizchip_rw(wb);
|
||||
}
|
||||
|
||||
uint8_t wizchip_read()
|
||||
{
|
||||
uint8_t wizchip_read() {
|
||||
return wizchip_rw(0xFF);
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ uint8_t wizchip_read()
|
||||
"type" : "library",
|
||||
"class" : "WIZnet"
|
||||
}
|
||||
|
||||
Library for communication with the WIZnet Ethernet module
|
||||
*/
|
||||
/*JSON{
|
||||
"type" : "staticmethod",
|
||||
@ -70,41 +70,62 @@ uint8_t wizchip_read()
|
||||
"name" : "connect",
|
||||
"generate" : "jswrap_wiznet_connect",
|
||||
"params" : [
|
||||
|
||||
["spi", "JsVar", "Device to use for SPI (or undefined to use the default)"],
|
||||
["cs", "pin", "The pin to use for Chip Select"]
|
||||
],
|
||||
"return" : ["JsVar","An Ethernet Object"],
|
||||
"return_object" : "Ethernet"
|
||||
}
|
||||
Initialise the WIZnet module and return an Ethernet object
|
||||
*/
|
||||
JsVar *jswrap_wiznet_connect() {
|
||||
JsVar *jswrap_wiznet_connect(JsVar *spi, Pin cs) {
|
||||
|
||||
IOEventFlags spiDevice;
|
||||
if (spi) {
|
||||
spiDevice = jsiGetDeviceFromClass(spi);
|
||||
if (!DEVICE_IS_SPI(spiDevice)) {
|
||||
jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
// SPI config
|
||||
JshSPIInfo inf;
|
||||
jshSPIInitInfo(&inf);
|
||||
inf.pinSCK = ETH_CLK_PIN;
|
||||
inf.pinMISO = ETH_MISO_PIN;
|
||||
inf.pinMOSI = ETH_MOSI_PIN;
|
||||
inf.baudRate = 1000000;
|
||||
inf.spiMode = SPIF_SPI_MODE_0;
|
||||
jshSPISetup(ETH_SPI, &inf);
|
||||
spiDevice = ETH_SPI;
|
||||
}
|
||||
if (!jshIsPinValid(cs))
|
||||
cs = ETH_CS_PIN;
|
||||
|
||||
JsNetwork net;
|
||||
networkCreate(&net, JSNETWORKTYPE_W5500);
|
||||
net.data.device = spiDevice;
|
||||
net.data.pinCS = cs;
|
||||
networkSet(&net);
|
||||
|
||||
JsVar *ethObj = jspNewObject(0, "Ethernet");
|
||||
|
||||
// SPI config
|
||||
JshSPIInfo inf;
|
||||
jshSPIInitInfo(&inf);
|
||||
inf.pinSCK = ETH_CLK_PIN;
|
||||
inf.pinMISO = ETH_MISO_PIN;
|
||||
inf.pinMOSI = ETH_MOSI_PIN;
|
||||
inf.baudRate = 1000000;
|
||||
inf.spiMode = SPIF_SPI_MODE_0;
|
||||
jshSPISetup(ETH_SPI, &inf);
|
||||
|
||||
// CS Configuration
|
||||
jshSetPinStateIsManual(ETH_CS_PIN, false);
|
||||
jshPinOutput(ETH_CS_PIN, 1); // de-assert CS
|
||||
jshSetPinStateIsManual(net.data.pinCS, false);
|
||||
jshPinOutput(net.data.pinCS, 1); // de-assert CS
|
||||
|
||||
// Wiznet
|
||||
// Initialise WIZnet functions
|
||||
reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
|
||||
reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write);
|
||||
|
||||
/* wizchip initialize*/
|
||||
uint8_t tmp;
|
||||
uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
|
||||
uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2}};
|
||||
|
||||
if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1)
|
||||
{
|
||||
jsiConsolePrint("WIZCHIP Initialized fail.\r\n");
|
||||
jsiConsolePrint("WIZnet Initialize failed.\r\n");
|
||||
networkFree(&net);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -112,12 +133,11 @@ JsVar *jswrap_wiznet_connect() {
|
||||
do {
|
||||
if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) {
|
||||
jsiConsolePrint("Unknown PHY Link status.\r\n");
|
||||
networkFree(&net);
|
||||
return 0;
|
||||
}
|
||||
} while (tmp == PHY_LINK_OFF);
|
||||
|
||||
JsNetwork net;
|
||||
networkCreate(&net, JSNETWORKTYPE_W5500);
|
||||
networkFree(&net);
|
||||
|
||||
networkState = NETWORKSTATE_ONLINE;
|
||||
@ -149,6 +169,9 @@ JsVar *jswrap_ethernet_getIP(JsVar *wlanObj) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
JsNetwork net;
|
||||
if (!networkGetFromVar(&net)) return 0;
|
||||
|
||||
wiz_NetInfo gWIZNETINFO;
|
||||
ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
|
||||
|
||||
@ -159,6 +182,9 @@ JsVar *jswrap_ethernet_getIP(JsVar *wlanObj) {
|
||||
networkPutAddressAsString(data, "gateway", &gWIZNETINFO.gw[0], 4, 10, '.');
|
||||
networkPutAddressAsString(data, "dns", &gWIZNETINFO.dns[0], 4, 10, '.');
|
||||
networkPutAddressAsString(data, "mac", &gWIZNETINFO.mac[0], 6, 16, 0);
|
||||
|
||||
networkFree(&net);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@ -193,6 +219,9 @@ bool jswrap_ethernet_setIP(JsVar *wlanObj, JsVar *options) {
|
||||
return false;
|
||||
}
|
||||
|
||||
JsNetwork net;
|
||||
if (!networkGetFromVar(&net)) return false;
|
||||
|
||||
bool success = false;
|
||||
wiz_NetInfo gWIZNETINFO;
|
||||
|
||||
@ -218,6 +247,9 @@ bool jswrap_ethernet_setIP(JsVar *wlanObj, JsVar *options) {
|
||||
}
|
||||
|
||||
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
|
||||
|
||||
networkFree(&net);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
@ -12,8 +12,9 @@
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
#include "jsvar.h"
|
||||
#include "jspin.h"
|
||||
|
||||
JsVar *jswrap_wiznet_connect();
|
||||
JsVar *jswrap_wiznet_connect(JsVar *spi, Pin cs);
|
||||
|
||||
JsVar *jswrap_ethernet_getIP(JsVar *wlanObj);
|
||||
bool jswrap_ethernet_setIP(JsVar *wlanObj, JsVar *options);
|
||||
|
||||
@ -15,8 +15,6 @@
|
||||
#include "network.h"
|
||||
#include "network_wiznet.h"
|
||||
|
||||
|
||||
|
||||
#define INVALID_SOCKET ((SOCKET)(-1))
|
||||
#define SOCKET_ERROR (-1)
|
||||
|
||||
@ -73,7 +71,6 @@ bool net_wiznet_checkError(JsNetwork *net) {
|
||||
|
||||
/// if host=0, creates a server otherwise creates a client (and automatically connects). Returns >=0 on success
|
||||
int net_wiznet_createsocket(JsNetwork *net, unsigned long host, unsigned short port) {
|
||||
NOT_USED(net);
|
||||
int sckt = -1;
|
||||
if (host!=0) { // ------------------------------------------------- host (=client)
|
||||
|
||||
@ -81,7 +78,9 @@ int net_wiznet_createsocket(JsNetwork *net, unsigned long host, unsigned short p
|
||||
|
||||
sckt = socket(net_wiznet_getFreeSocket(), Sn_MR_TCP, (uint16_t)((rand() & 32767) + 2000), 0); // we set nonblocking later
|
||||
|
||||
if (sckt<0) return sckt; // error
|
||||
if (sckt<0) {
|
||||
return sckt; // error
|
||||
}
|
||||
|
||||
int res = connect((uint8_t)sckt,(uint8_t*)&host, port);
|
||||
// now we set nonblocking - so that connect waited for the connection
|
||||
@ -102,7 +101,6 @@ int net_wiznet_createsocket(JsNetwork *net, unsigned long host, unsigned short p
|
||||
|
||||
/// destroys the given socket
|
||||
void net_wiznet_closesocket(JsNetwork *net, int sckt) {
|
||||
NOT_USED(net);
|
||||
// try and close gracefully
|
||||
disconnect((uint8_t)sckt);
|
||||
JsSysTime timeout = jshGetSystemTime()+jshGetTimeFromMilliseconds(1000);
|
||||
@ -124,12 +122,12 @@ void net_wiznet_closesocket(JsNetwork *net, int sckt) {
|
||||
|
||||
/// If the given server socket can accept a connection, return it (or return < 0)
|
||||
int net_wiznet_accept(JsNetwork *net, int sckt) {
|
||||
NOT_USED(net);
|
||||
|
||||
// On WIZnet the same server socket is reused for clients - keep track so we don't get confused
|
||||
// and try and allocate a new HTTP Server Client
|
||||
if (wiznetSocketAsServerClient & (1<<sckt))
|
||||
if (wiznetSocketAsServerClient & (1<<sckt)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* CC3000/WIZnet works a different way - we set accept as nonblocking,
|
||||
* and then we just call it and see if it works or not...
|
||||
@ -140,7 +138,6 @@ int net_wiznet_accept(JsNetwork *net, int sckt) {
|
||||
int status = getSn_SR((uint8_t)sckt);
|
||||
if (status == SOCK_ESTABLISHED) {
|
||||
wiznetSocketAsServerClient = wiznetSocketAsServerClient | (unsigned char)(1<<sckt); // mark that it's now being used as a client socket
|
||||
|
||||
return ((int)sckt) | WIZNET_SERVER_CLIENT; // we deal with the client on the same socket (we use the flag so we know that it really is different!)
|
||||
}
|
||||
|
||||
@ -149,13 +146,11 @@ int net_wiznet_accept(JsNetwork *net, int sckt) {
|
||||
// make sure we force-close again and re-init as a listener
|
||||
net_wiznet_closesocket(net, (uint16_t)(sckt | WIZNET_SERVER_CLIENT));
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// Receive data if possible. returns nBytes on success, 0 on no data, or -1 on failure
|
||||
int net_wiznet_recv(JsNetwork *net, int sckt, void *buf, size_t len) {
|
||||
NOT_USED(net);
|
||||
int num = 0;
|
||||
if (getSn_SR((uint8_t)sckt) == SOCK_LISTEN) {
|
||||
// socket is operating as a TCP server - something has gone wrong.
|
||||
@ -172,7 +167,6 @@ int net_wiznet_recv(JsNetwork *net, int sckt, void *buf, size_t len) {
|
||||
|
||||
/// Send data if possible. returns nBytes on success, 0 on no data, or -1 on failure
|
||||
int net_wiznet_send(JsNetwork *net, int sckt, const void *buf, size_t len) {
|
||||
NOT_USED(net);
|
||||
int r = (int)send((uint8_t)sckt, buf, (uint16_t)len, MSG_NOSIGNAL);
|
||||
if (jspIsInterrupted()) return -1;
|
||||
return r;
|
||||
|
||||
@ -74,7 +74,7 @@ def dump_pin(pin, pinstrip):
|
||||
not_five_volt = True
|
||||
|
||||
writeHTML(' <DIV class="'+pinstrip+'pin pin">');
|
||||
pinHTML = ' <SPAN class="pinname">'+pin+".</SPAN>";
|
||||
pinHTML = ' <SPAN class="pinname">'+pin+"</SPAN>";
|
||||
pinHTML2 = '';
|
||||
|
||||
if not_five_volt:
|
||||
@ -102,11 +102,17 @@ def dump_pin(pin, pinstrip):
|
||||
if name in pinfuncs:
|
||||
pinfuncs[name]["title"] = pinfuncs[name]["title"] + " " + title
|
||||
else:
|
||||
pinfuncs[name] = { 'cls': cls, 'title': "["+pin+"] "+title, 'name': name, 'id': pin+"_"+func };
|
||||
pinfuncs[name] = { 'cls': cls, 'title': "["+pin+"] "+title, 'name': name, 'id': pin+"_"+func, 'func' : func };
|
||||
|
||||
for func in sorted(pinfuncs.items(),key=lambda x: x[1]['cls']):
|
||||
pf = func[1]
|
||||
url = False
|
||||
if pf["cls"] in pinutils.URLS: url = pinutils.URLS[pf["cls"]]
|
||||
if pf["func"] in pinutils.URLS: url = pinutils.URLS[pf["func"]]
|
||||
|
||||
if url != False: writeHTML(' <A href="'+url+'" class="pinfunctionlink">');
|
||||
writeHTML(' <SPAN class="pinfunction '+pf["cls"]+'" title="'+pf["title"]+'" onMouseOver="showTT(\''+pf["id"]+'\')" onMouseOut="hideTT(\''+pf["id"]+'\')">'+pf["name"]+'</SPAN>')
|
||||
if url != False: writeHTML(' </A>');
|
||||
writeHTML(' <SPAN class="pintooltip" id="'+pf["id"]+'" style="display:none;">'+pf["title"]+'</SPAN>')
|
||||
|
||||
if reverse: writeHTML(pinHTML2+"\n"+pinHTML)
|
||||
@ -136,6 +142,10 @@ writeHTML("""
|
||||
padding-left: 2px;
|
||||
padding-right: 2px;
|
||||
}
|
||||
.pinfunctionlink {
|
||||
color : black;
|
||||
text-decoration: none;
|
||||
}
|
||||
.pintooltip {
|
||||
background-color: #FFD;
|
||||
border:1px solid black;
|
||||
@ -267,7 +277,24 @@ writeHTML(' <TR><TH>SD Card</TH><TD>'+("Yes" if "SD" in board.devices else "No
|
||||
writeHTML(' </TABLE>')
|
||||
writeHTML(' <P>Like this? Please tell your friends, blog, or <a href="http://www.espruino.com/Order">support us by buying our board</a>!</P>')
|
||||
writeHTML(' <H2>Pinout</H2>')
|
||||
writeHTML(' <P>Hover the mouse over a pin function for more information</P>')
|
||||
writeHTML("""
|
||||
<P>Hover the mouse over a pin function for more information. Clicking in a function will tell you how to use it in Espruino.</P>
|
||||
<ul>
|
||||
<li><span class="pinfunction DEVICE">Purple</span> boxes show pins that are used for other functionality on the board. You should avoid using these unless you know that the marked device is not used.</li>
|
||||
<li><span class="pinfunction NOT_5V">3.3v</span> boxes mark pins that are not 5v tolerant (they only take inputs from 0 - 3.3v, not 0 - 5v).</li>
|
||||
<li><span class="pinfunction">3.3</span> is a 3.3v output from the on-board Voltage regulator.</li>
|
||||
<li><span class="pinfunction">GND</span> is ground (0v).</li>
|
||||
<li><span class="pinfunction">VBAT</span> is the battery voltage output (see <a href="/EspruinoBoard">the Espruino Board Reference</a>).</li>
|
||||
<li><span class="pinfunction ADC">ADC</span> is an <a href="/ADC">Analog to Digital Converter</a> (for reading analog voltages)</li>
|
||||
<li><span class="pinfunction DAC">DAC</span> is a <a href="/DAC">Digital to Analog Converter</a> (for creating analog voltages). This is not available on all boards.</li>
|
||||
<li><span class="pinfunction PWM">PWM</span> is for <a href="/PWM">Pulse Width Modulation</a>. This creates analog voltages from a digital output by sending a series of pulses.</li>
|
||||
<li><span class="pinfunction SPI">SPI</span> is the 3 wire <a href="/SPI">Serial Peripheral Interface</a>.</li>
|
||||
<li><span class="pinfunction USART">USART</span> is a 2 wire peripheral for <a href="/USART">Serial Data</a>.</li>
|
||||
<li><span class="pinfunction I2C">I2C</span> is the 2 wire <a href="/I2C">Inter-Integrated Circuit</a> bus.</li>
|
||||
<li><span class="pinfunction CAN">CAN</span> is for the <a href="http://en.wikipedia.org/wiki/CAN_bus">Controller Area Network</a>. It is only available on some devices and is not supported by Espruino.</li>
|
||||
|
||||
</ul>
|
||||
""");
|
||||
writeHTML(' <DIV id="boardcontainer">')
|
||||
writeHTML(' <DIV id="board">')
|
||||
usedpins = []
|
||||
|
||||
@ -92,6 +92,20 @@ for D in DEVICES:
|
||||
CLASSES[D]="DEVICE"
|
||||
NAMES[D]=DEVICES[D]
|
||||
|
||||
URLS = {
|
||||
"PWM":"/PWM",
|
||||
"SPI":"/SPI",
|
||||
"I2C":"/I2S",
|
||||
"USART":"/USART",
|
||||
"DAC":"/DAC",
|
||||
"ADC":"/ADC",
|
||||
"OSC_RTC":"/Clocks",
|
||||
"LED1":"/Flashing+Lights",
|
||||
"LED2":"/Flashing+Lights",
|
||||
"LED3":"/Flashing+Lights",
|
||||
"BLUETOOTH":"/Bluetooth",
|
||||
};
|
||||
|
||||
# is a pin name valid
|
||||
def isvalidpin(pinname):
|
||||
pinport = pinname[1:2]
|
||||
|
||||
@ -18,6 +18,11 @@
|
||||
#include "usb_conf.h"
|
||||
#include "usb_pwr.h"
|
||||
#endif
|
||||
#ifdef STM32F4
|
||||
#include "usb_regs.h"
|
||||
#include "usb_defines.h"
|
||||
#include "usbd_conf.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "jshardware.h"
|
||||
@ -2475,6 +2480,30 @@ bool jshFlashContainsCode() {
|
||||
return (*(int*)FLASH_MAGIC_LOCATION) == (int)FLASH_MAGIC;
|
||||
}
|
||||
|
||||
#ifdef USB
|
||||
void jshSetUSBPower(bool isOn) {
|
||||
#ifdef STM32F1
|
||||
if (isOn) {
|
||||
_SetCNTR(_GetCNTR() & (unsigned)~CNTR_PDWN);
|
||||
USB_Cable_Config(ENABLE);
|
||||
} else {
|
||||
USB_Cable_Config(DISABLE);
|
||||
_SetCNTR(_GetCNTR() | CNTR_PDWN);
|
||||
}
|
||||
#endif // STM32F1
|
||||
#ifdef STM32F4
|
||||
/*if (isOn) {
|
||||
USB_OTG_CoreInit(&USB_OTG_dev);
|
||||
} else {
|
||||
USB_OTG_GCCFG_TypeDef gccfg;
|
||||
gccfg.d32 = USB_OTG_READ_REG32(USB_OTG_dev.regs.GREGS->GCCFG);
|
||||
gccfg.b.pwdn = 0; // 0 = usb off, 1 = usb on
|
||||
USB_OTG_WRITE_REG32 (USB_OTG_dev.regs.GREGS->GCCFG, gccfg.d32);
|
||||
}*/
|
||||
#endif // STM32F4
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Enter simple sleep mode (can be woken up by interrupts). Returns true on success
|
||||
bool jshSleep(JsSysTime timeUntilWake) {
|
||||
#ifdef USE_RTC
|
||||
@ -2506,11 +2535,7 @@ bool jshSleep(JsSysTime timeUntilWake) {
|
||||
ADC_Cmd(ADC4, DISABLE); // ADC off
|
||||
#endif
|
||||
#ifdef USB
|
||||
// PowerOff(); // USB disconnect - brings us down to 0.12mA - but seems to lock Espruino up afterwards!
|
||||
#ifdef STM32F1
|
||||
USB_Cable_Config(DISABLE);
|
||||
_SetCNTR(_GetCNTR() | CNTR_PDWN);
|
||||
#endif // STM32F1
|
||||
jshSetUSBPower(false);
|
||||
#endif // USB
|
||||
|
||||
/* Add EXTI for Serial port */
|
||||
@ -2558,6 +2583,10 @@ bool jshSleep(JsSysTime timeUntilWake) {
|
||||
// set flag in case there happens to be a SysTick
|
||||
hasSystemSlept = true;
|
||||
// -----------------------------------------------
|
||||
#ifdef STM32F4
|
||||
/* FLASH Deep Power Down Mode enabled */
|
||||
PWR_FlashPowerDownCmd(ENABLE);
|
||||
#endif
|
||||
/* Request to enter STOP mode with regulator in low power mode*/
|
||||
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
|
||||
// -----------------------------------------------
|
||||
@ -2589,10 +2618,8 @@ bool jshSleep(JsSysTime timeUntilWake) {
|
||||
}
|
||||
RTC_WaitForSynchro(); // make sure any RTC reads will be done
|
||||
#ifdef USB
|
||||
jshSetUSBPower(true);
|
||||
#ifdef STM32F1
|
||||
_SetCNTR(_GetCNTR() & (unsigned)~CNTR_PDWN);
|
||||
USB_Cable_Config(ENABLE);
|
||||
// PowerOn(); // USB on
|
||||
if (wokenByUSB)
|
||||
jshLastWokenByUSB = jshGetRTCSystemTime();
|
||||
#endif
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user