Skip to content

Commit

Permalink
Code changes for SPI testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolban committed Sep 29, 2015
1 parent 07aac1c commit 908ed13
Show file tree
Hide file tree
Showing 16 changed files with 840 additions and 412 deletions.
6 changes: 3 additions & 3 deletions boards/ESP8266_BOARD.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
""";

def get_pins():
pins = pinutils.generate_pins(0,7)
# just fake pins D0 .. D7
return pins
pins = pinutils.generate_pins(0,15)
# just fake pins D0 .. D15
return pins
2 changes: 2 additions & 0 deletions doxygen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/html/
/latex/
41 changes: 31 additions & 10 deletions libs/network/esp8266/jswrap_esp8266.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Because the ESP8266 JS wrapper is assured to be running on an ESP8266 we
// can assume that inclusion of ESP8266 headers will be acceptable.
#include <c_types.h>
#include <user_interface.h>
#include <mem.h>
Expand Down Expand Up @@ -60,7 +62,13 @@ static struct ping_option pingOpt;
["gotIpCallback", "JsVar", "An optional callback invoked when we have an IP"]
]
}*/
void jswrap_ESP8266WiFi_connect(JsVar *jsv_ssid, JsVar *jsv_password, JsVar *gotIpCallback) {
void jswrap_ESP8266WiFi_connect(
JsVar *jsv_ssid, //!< The SSID of the access point to connect.
JsVar *jsv_password, //!< The password for the access point.
JsVar *gotIpCallback //!< The Callback function to be called when we are connected.
) {
os_printf("> jswrap_ESP8266WiFi_connect\n");

// Check that the ssid and password values aren't obviously in error.
if (jsv_ssid == NULL || !jsvIsString(jsv_ssid)) {
jsExceptionHere(JSET_ERROR, "No SSID.");
Expand All @@ -86,6 +94,8 @@ void jswrap_ESP8266WiFi_connect(JsVar *jsv_ssid, JsVar *jsv_password, JsVar *got
jsvUnLock(jsGotIpCallback);
jsGotIpCallback = NULL;
}

// What does this do?
if (gotIpCallback != NULL) {
jsGotIpCallback = jsvLockAgainSafe(gotIpCallback);
}
Expand All @@ -98,8 +108,7 @@ void jswrap_ESP8266WiFi_connect(JsVar *jsv_ssid, JsVar *jsv_password, JsVar *got
len = jsvGetString(jsv_password, password, sizeof(password)-1);
password[len]='\0';

jsiConsolePrintf("jswrap_ESP8266WiFi_connect: %s - %s\r\n", ssid, password);

os_printf("> - ssid=%s, password=%s\n", ssid, password);
// Set the WiFi mode of the ESP8266
wifi_set_opmode_current(STATION_MODE);

Expand All @@ -115,10 +124,11 @@ void jswrap_ESP8266WiFi_connect(JsVar *jsv_ssid, JsVar *jsv_password, JsVar *got
// Set the WiFi configuration
wifi_station_set_config(&stationConfig);

// Register the event handler
// Register the event handler for callbacks from ESP8266
wifi_set_event_handler_cb(wifiEventHandler);

wifi_station_connect();
os_printf("< jswrap_ESP8266WiFi_connect\n");
} // End of jswrap_ESP8266WiFi_connect


Expand Down Expand Up @@ -209,24 +219,26 @@ void jswrap_ESP8266WiFi_beAccessPoint(
void jswrap_ESP8266WiFi_getAccessPoints(
JsVar *callback //!< Function to call back when access points retrieved.
) {
jsiConsolePrint("> ESP8266WiFi_getAccessPoints\n");
os_printf("> ESP8266WiFi_getAccessPoints\n");
if (callback == NULL || !jsvIsFunction(callback)) {
jsExceptionHere(JSET_ERROR, "No callback.");
return;
}

// Save the callback for the scan
// Save the callback for the scan in the global variable called jsScanCallback.
jsScanCallback = jsvLockAgainSafe(callback);

// Ask the ESP8266 to perform a network scan after first entering
// station mode. This will result in an eventual callback which is where
// station mode. The network scan will eventually result in a callback
// being executed (scanCB) which will contain the results.

// Ensure we are in station mode
wifi_set_opmode_current(STATION_MODE);

// Request a scan of the network calling "scanCB" on completion
wifi_station_scan(NULL, scanCB);

jsiConsolePrint("< ESP8266WiFi_getAccessPoints\n");
os_printf("< ESP8266WiFi_getAccessPoints\n");
} // End of jswrap_ESP8266WiFi_getAccessPoints


Expand Down Expand Up @@ -812,6 +824,7 @@ static void scanCB(void *arg, STATUS status) {
* of records.
*/

os_printf(">> scanCB\n");
// Create the Empty JS array that will be passed as a parameter to the callback.
JsVar *accessPointArray = jsvNewArray(NULL, 0);
struct bss_info *bssInfo;
Expand Down Expand Up @@ -851,7 +864,7 @@ static void scanCB(void *arg, STATUS status) {
// Add the new record to the array
jsvArrayPush(accessPointArray, currentAccessPoint);

os_printf("ssid: %s\n", bssInfo->ssid);
os_printf(" - ssid: %s\n", bssInfo->ssid);
bssInfo = STAILQ_NEXT(bssInfo, next);
} // End of loop over the records.

Expand All @@ -860,6 +873,7 @@ static void scanCB(void *arg, STATUS status) {
params[0] = accessPointArray;
jsiQueueEvents(NULL, jsScanCallback, params, 1);
jsvUnLock(jsScanCallback);
os_printf("<< scanCB\n");
} // End of scanCB


Expand Down Expand Up @@ -894,16 +908,19 @@ static void sendWifiEvent(uint32 eventType, JsVar *details) {
/**
* \brief ESP8266 WiFi Event handler.
* This function is called by the ESP8266
* environment when significant events happend related to the WiFi environment.
* environment when significant events happen related to the WiFi environment.
* The event handler is registered with a call to wifi_set_event_handler_cb()
* that is provided by the ESP8266 SDK.
*/
static void wifiEventHandler(System_Event_t *event) {
switch(event->event) {
// We have connected to an access point.
case EVENT_STAMODE_CONNECTED:
os_printf("Event: EVENT_STAMODE_CONNECTED\n");
sendWifiEvent(event->event, jsvNewNull());
break;

// We have disconnected or been disconnected from an access point.
case EVENT_STAMODE_DISCONNECTED:
os_printf("Event: EVENT_STAMODE_DISCONNECTED\n");
JsVar *details = jspNewObject(NULL, "EventDetails");
Expand All @@ -913,10 +930,14 @@ static void wifiEventHandler(System_Event_t *event) {
ssid[ event->event_info.disconnected.ssid_len] = '\0';
sendWifiEvent(event->event, details);
break;

// The authentication information at the access point has changed.
case EVENT_STAMODE_AUTHMODE_CHANGE:
os_printf("Event: EVENT_STAMODE_AUTHMODE_CHANGE\n");
sendWifiEvent(event->event, jsvNewNull());
break;

// We have been allocated an IP address.
case EVENT_STAMODE_GOT_IP:
os_printf("Event: EVENT_STAMODE_GOT_IP\n");
sendWifiEvent(event->event, jsvNewNull());
Expand Down
1 change: 1 addition & 0 deletions libs/network/esp8266/network_esp8266.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*
*/
// ESP8266 specific includes
#define ESPSDK_1_3_0
#include <c_types.h>
#include <user_interface.h>
#include <mem.h>
Expand Down
41 changes: 26 additions & 15 deletions src/jshardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,35 @@ typedef enum {
SPIB_MINIMUM,// baudRate is the minimum we'll choose
} PACKED_FLAGS JshBaudFlags;

/**
* \brief Definition of an SPI interface.
*/
typedef struct {
int baudRate;
JshBaudFlags baudRateSpec;
Pin pinSCK;
Pin pinMISO;
Pin pinMOSI;
unsigned char spiMode;
bool spiMSB; // MSB first?
int baudRate; //!< Baud rate.
JshBaudFlags baudRateSpec; //!<
Pin pinSCK; //!< Pin to use for clock.
Pin pinMISO; //!< Pin to use for Master In/Slave Out.
Pin pinMOSI; //!< Pin to use for Master Out/Slave In.
unsigned char spiMode; //!<
bool spiMSB; //!< MSB first?
} PACKED_FLAGS JshSPIInfo;
static inline void jshSPIInitInfo(JshSPIInfo *inf) {
inf->baudRate = 100000;


/**
* \brief Initialize a JshSPIInfo structure to defaults.
*/
static inline void jshSPIInitInfo(
JshSPIInfo *inf //!< The JshSPIInfo structure to initialize to defaults.
) {
inf->baudRate = 100000;
inf->baudRateSpec = SPIB_DEFAULT;
inf->pinSCK = PIN_UNDEFINED;
inf->pinMISO = PIN_UNDEFINED;
inf->pinMOSI = PIN_UNDEFINED;
inf->spiMode = SPIF_SPI_MODE_0;
inf->spiMSB = true; // MSB first is default
}
inf->pinSCK = PIN_UNDEFINED;
inf->pinMISO = PIN_UNDEFINED;
inf->pinMOSI = PIN_UNDEFINED;
inf->spiMode = SPIF_SPI_MODE_0;
inf->spiMSB = true; // MSB first is default
} // End of jshSPIInitInfo.


/** Set up SPI, if pins are -1 they will be guessed */
void jshSPISetup(IOEventFlags device, JshSPIInfo *inf);
Expand Down
12 changes: 10 additions & 2 deletions src/jsinteractive.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ void jsiDebuggerLine(JsVar *line);

// ----------------------------------------------------------------------------

/**
* \brief Get the device from the class variable.
*/
IOEventFlags jsiGetDeviceFromClass(JsVar *class) {
// Devices have their Object data set up to something special
// See jspNewObject
Expand All @@ -79,7 +82,8 @@ IOEventFlags jsiGetDeviceFromClass(JsVar *class) {
return (IOEventFlags)class->varData.str[3];

return EV_NONE;
}
} // End of jsiGetDeviceFromClass


JsVar *jsiGetClassNameFromDevice(IOEventFlags device) {
const char *deviceName = jshGetDeviceString(device);
Expand Down Expand Up @@ -161,12 +165,16 @@ NO_INLINE void jsiConsolePrint(const char *str) {
}
}

/**
* \brief Perform a printf to the console.
* Execute a printf command to the current JS console.
*/
void jsiConsolePrintf(const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
vcbprintf((vcbprintf_callback)jsiConsolePrint,0, fmt, argp);
va_end(argp);
}
} // End of jsiConsolePrintf

/// Print the contents of a string var from a character position until end of line (adding an extra ' ' to delete a character if there was one)
void jsiConsolePrintStringVarUntilEOL(JsVar *v, size_t fromCharacter, size_t maxChars, bool andBackup) {
Expand Down
Loading

0 comments on commit 908ed13

Please sign in to comment.