different pins for network devices

This commit is contained in:
Gordon Williams 2014-09-12 11:10:10 +01:00
parent 4d4de03804
commit 44c37c3f78
5 changed files with 108 additions and 44 deletions

View File

@ -100,6 +100,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);

View File

@ -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;

View File

@ -24,35 +24,42 @@
#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
/** We need this variable so we know what pins to use to communicate */
JsNetwork *currentNet = 0;
void wizchip_setCurrentNet(JsNetwork *n) {
currentNet = n;
}
void wizchip_deselect(void)
{
jshPinOutput(ETH_CS_PIN, 1); // active low
void wizchip_select(void) {
assert(currentNet);
jshPinOutput(currentNet->data.pinCS, 0); // active low
}
void wizchip_deselect(void) {
assert(currentNet);
jshPinOutput(currentNet->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(currentNet);
int r = jshSPISend(currentNet->data.device, data);
if (r<0) r = jshSPISend(currentNet->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);
}
@ -69,41 +76,64 @@ Library for communication with the WIZnet Ethernet module
"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 SPI cs"]
],
"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);
wizchip_setCurrentNet(&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(cs, false);
jshPinOutput(cs, 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);
wizchip_setCurrentNet(0);
return 0;
}
@ -111,13 +141,14 @@ JsVar *jswrap_wiznet_connect() {
do {
if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) {
jsiConsolePrint("Unknown PHY Link status.\r\n");
networkFree(&net);
wizchip_setCurrentNet(0);
return 0;
}
} while (tmp == PHY_LINK_OFF);
JsNetwork net;
networkCreate(&net, JSNETWORKTYPE_W5500);
networkFree(&net);
wizchip_setCurrentNet(0);
networkState = NETWORKSTATE_ONLINE;
@ -148,6 +179,10 @@ JsVar *jswrap_ethernet_getIP(JsVar *wlanObj) {
return 0;
}
JsNetwork net;
if (!networkGetFromVar(&net)) return false;
wizchip_setCurrentNet(&net);
wiz_NetInfo gWIZNETINFO;
ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
@ -158,6 +193,10 @@ 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);
wizchip_setCurrentNet(0);
return data;
}
@ -192,6 +231,10 @@ bool jswrap_ethernet_setIP(JsVar *wlanObj, JsVar *options) {
return false;
}
JsNetwork net;
if (!networkGetFromVar(&net)) return false;
wizchip_setCurrentNet(&net);
bool success = false;
wiz_NetInfo gWIZNETINFO;
@ -217,6 +260,10 @@ bool jswrap_ethernet_setIP(JsVar *wlanObj, JsVar *options) {
}
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
networkFree(&net);
wizchip_setCurrentNet(0);
return success;
}

View File

@ -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);

View File

@ -15,7 +15,7 @@
#include "network.h"
#include "network_wiznet.h"
void wizchip_setCurrentNet(JsNetwork *n);
#define INVALID_SOCKET ((SOCKET)(-1))
#define SOCKET_ERROR (-1)
@ -53,10 +53,12 @@ uint8_t net_wiznet_getFreeSocket() {
/// Get an IP address from a name. Sets out_ip_addr to 0 on failure
void net_wiznet_gethostbyname(JsNetwork *net, char * hostName, unsigned long* out_ip_addr) {
wizchip_setCurrentNet(net);
NOT_USED(net);
if (dns_query(0, net_wiznet_getFreeSocket(), (uint8_t*)hostName) == 1) {
*out_ip_addr = *(unsigned long*)&Server_IP_Addr[0];
}
wizchip_setCurrentNet(0);
}
/// Called on idle. Do any checks required for this device
@ -73,7 +75,7 @@ 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);
wizchip_setCurrentNet(net);
int sckt = -1;
if (host!=0) { // ------------------------------------------------- host (=client)
@ -81,7 +83,10 @@ 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) {
wizchip_setCurrentNet(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
@ -97,12 +102,13 @@ int net_wiznet_createsocket(JsNetwork *net, unsigned long host, unsigned short p
}
wiznetSocketPorts[sckt&7] = port;
//jsiConsolePrintf("Created socket %d\n", sckt);
wizchip_setCurrentNet(0);
return sckt;
}
/// destroys the given socket
void net_wiznet_closesocket(JsNetwork *net, int sckt) {
NOT_USED(net);
wizchip_setCurrentNet(&net);
// try and close gracefully
disconnect((uint8_t)sckt);
JsSysTime timeout = jshGetSystemTime()+jshGetTimeFromMilliseconds(1000);
@ -120,16 +126,19 @@ void net_wiznet_closesocket(JsNetwork *net, int sckt) {
// Be sure to mark it as not a client socket any more
wiznetSocketAsServerClient = wiznetSocketAsServerClient & (unsigned char)~(1<<(sckt&7));
}
wizchip_setCurrentNet(0);
}
/// 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);
wizchip_setCurrentNet(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)) {
wizchip_setCurrentNet(0);
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 +149,7 @@ 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
wizchip_setCurrentNet(0);
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,31 +158,34 @@ 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));
}
wizchip_setCurrentNet(0);
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);
wizchip_setCurrentNet(net);
int num = 0;
if (getSn_SR((uint8_t)sckt) == SOCK_LISTEN) {
// socket is operating as a TCP server - something has gone wrong.
// just return -1 to close this connection immediately
wizchip_setCurrentNet(0);
return -1;
} else {
// receive data - if none available it'll just return SOCK_BUSY
num = (int)recv((uint8_t)sckt,buf,(uint16_t)len,0);
if (num==SOCK_BUSY) num=0;
}
wizchip_setCurrentNet(0);
if (jspIsInterrupted()) return -1;
return num;
}
/// 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);
wizchip_setCurrentNet(net);
int r = (int)send((uint8_t)sckt, buf, (uint16_t)len, MSG_NOSIGNAL);
wizchip_setCurrentNet(0);
if (jspIsInterrupted()) return -1;
return r;
}