NEQTO Docs
  • Languages iconEnglish
    • 日本語
  • Docs
  • API
  • FAQ

›neqto.js

Getting Started

  • NEQTO Hello World!
  • Tutorial Step 1. NEQTO Console Setting
  • Tutorial Step 2. Device Settings & Launching Service (NEQTO Bridge)
  • Tutorial Step 2. Device Settings & Launching Service (STM32 Discovery)
  • Tutorial Step 2. Device Settings & Launching Service (SPRESENSE)
  • Tutorial Step 3. Application development using scripts

NEQTO

  • NEQTO Account Registration
  • Sub-accounts
  • API Usage
  • NEQTO CloudSync for GCP
  • NEQTO Engine Firmware List
  • Support Guidelines

NEQTO Console

  • Introduction
  • Fundamentals
  • Administrative Actions
  • Device Management
  • Linux-based Device Management
  • Batch Registration
  • Scripts
  • Actions and Contacts
  • View Data from the Console
  • NEQTO Apps

    • About NEQTO Apps
    • NEQTO Infinitypool
    • NEQTO Insights
    • NEQTO Custodia
    • NEQTO Flow
  • Machine Driver
  • Recommended Browsers
  • Billing Information

SPRESENSE

    Hardware Specifications

    • 01. About Spresense

    Software Specifications

    • 01. Operational Flow
    • 02. Initial Installation
    • 03. Spresense Wi-Fi Initial Setup
    • 04. Spresense LTE-M Initial Setup
    • 05. Debug Log Acquisition
    • 06. System LED Indications
    • 07. Event Messages
    • 08. Updating Firmware

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. Sleep
    • 10. RTC
    • 11. GPIO
    • 12. UART
    • 13. SPI
    • 14. I2C
    • 15. ADC
    • 16. GNSS
    • 17. Camera
    • 18. Utils
    • 19. nqSpresense
    • 20. nqService
    • 21. nqMqtt
    • 22. nqFOTA
    • 23. nqWiFi
    • 24. nqLte

STM32 Discovery

    Hardware Specifications

    • 01. About STM32 Discovery Kit (B-L4S5I-IOT01A)

    Software Specifications

    • 01. Operational Flow
    • 02. Initial Installation
    • 03. STM32 Discovery Wi-Fi Initial Setup
    • 04. Debug Log Acquisition
    • 05. System LED Indications
    • 06. Event Messages
    • 07. Updating Firmware

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. Sleep
    • 10. RTC
    • 11. UserSW
    • 12. GPIO
    • 13. UART
    • 14. SPI
    • 15. I2C
    • 16. ADC
    • 18. Utils
    • 19. nqDiscovery
    • 20. nqService
    • 21. nqMqtt
    • 22. nqFOTA
    • 23. nqWiFi

NEQTO Bridge

    Hardware Specifications

    • 01. NEQTO Bridge Module
    • 02. NEQTO Bridge Wi-Fi Module
    • 03. NEQTO Bridge LTE-1 Module
    • 04. NEQTO Bridge LTE-M/NB Module
    • 05. NEQTO Bridge IO Board
    • 06. NEQTO Bridge Digital IO Board
    • 07. NEQTO Bridge Connector Board

    Software Specifications

    • 01. Operational Flow
    • 02. NEQTO Bridge Wi-Fi Module Initial Setup
    • 03. NEQTO Bridge LTE Module Initial Setup
    • 04. Debug Log Acquisition
    • 05. System LED Indications
    • 06. Event Messages
    • 07. Updating Firmware

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. Sleep
    • 10. RTC
    • 11. UserSW
    • 12. GPIO
    • 13. UART
    • 14. SPI
    • 15. I2C
    • 16. ADC
    • 17. BLE
    • 18. Utils
    • 19. nqBridge
    • 20. nqService
    • 21. nqMqtt
    • 22. nqFOTA
    • 23. nqWiFi
    • 24. nqLte
    • 25. nqLAN
    • 26. nqEx

Linux-based device

    Software Specifications

    • 01. System Requirements
    • 02. Installation
    • 03. Software Configurations
    • 04. Operational Flow
    • 05. Debug Log Acquisition
    • 06. Event Messages
    • 07. Updating Software

    neqto.js

    • 01. About neqto.js
    • 02. Log
    • 03. Timers
    • 04. HTTP
    • 05. HTTPS
    • 06. MQTT
    • 07. Secure
    • 08. Storage
    • 09. RTC
    • 10. UNIXSocket
    • 11. FileSystem
    • 12. SubProcess
    • 13. Utils
    • 14. nqLinux
    • 15. nqService
    • 16. nqMqtt
    • 17. nqFOTA
    • 18. nqLAN

neqto.js Libraries

  • About neqto.js Libraries
  • UART

    • GM65 Barcode Reader
    • SRF Ultrasonic Range Finder - Serial Mode

    I2C

    • HTS221 v2 Temperature and Humidity Sensor
    • LIS2DW12 v2 Accelerometer
    • SRF Ultrasonic Range Finder - I2C Mode
    • [Archive] HTS221 Temperature and Humidity Sensor
    • [Archive] LIS2DW12 Accelerometer

    Utils

    • RTC Alarm Synchronized Scheduler

    Integration

    • AWS S3 v2
    • AWS IoT Core v2
    • Azure IoT v2
    • GCP IoT Core
    • [Archive] AWS S3
    • [Archive] AWS IoT Core

neqto.js Snippets

  • About neqto.js Snippets
  • DataDog
  • Dropbox
  • Google Sheets
  • InfluxDB
  • Oracle Cloud Object Storage
  • Salesforce
  • SAP Cloud Platform Internet of Things
  • Splunk
  • Niagara

Release Notes

  • NEQTO Console Updates
  • NEQTO Firmware (Bridge Wi-Fi/LTE Module) Releases
  • NEQTO Firmware (STM32 Discovery Wi-Fi) Releases
  • NEQTO Firmware (Spresense Wi-Fi/LTE-M) Releases
  • NEQTO Engine for Linux Releases

14. SPI

The SPI object is a built-in object that provides SPI communication functions.

Functional overview:

  • Provides SPI master mode function.

Limitations:

  • The write and read methods of the SPI instance are block processing.
    In particular, if you read or write a large amount of data at one time with a low communication speed setting, a long block time will occur until the communication is completed, so please take it into consideration.



Abstracts

Methods()/PropertiesSummaryVersionNote
new SPI()Creates an SPI instance.01.04.00+

{SPI} Instance

Methods()/PropertiesSummaryVersionNote
.open()Starts the communication.01.04.00+
.close()Ends the communication.01.04.00+
.write()Writes the data.01.04.00+
.read()Reads the data.01.04.00+
.writeRead()Reads the data after writing the data.01.04.00+
.writeReadFullDup()Writes data and reads data at the same time. (Full-duplex communication)01.04.00+
.readAndCompare()Repeats reading data until the specified target value is read.01.04.00+
.setChipSelect()Manually controls the chip select signal.01.04.00+


Details

new SPI(nodeNo)

Creates an SPI instance.

NameTypeM/OSummaryNote
nodeNonumbermandatorySpecifies the node number of the SPI interface to be used.
Please refer to Pinout.
return{SPI}-{SPI} : Generated {SPI}

.open(baudrate,clkmode,bitorder,csmode)

Starts the communication.

NameTypeM/OSummaryNote
baudratenumbermandatoryCommunication speed
0: 20Mbps
1: 10Mbps
2: 5Mbps
3: 2.5Mbps
4: 1.25Mbps
5: 0.625Mbps
6: 0.3125Mbps
7: 0.15625Mbps
clkmodenumbermandatoryClock mode
0: Low level when idle, Rising edge sampling
1: Low level when idle, Falling edge sampling
2: High level when idle, Falling edge sampling
3: High level when idle, Rising edge sampling
Please refer to Clock mode details.
bitordernumbermandatoryBit order
0: MSB first
1: LSB first
csmodenumbermandatoryChip select mode
0: Automatic (Automatically controlled to High level when idle and Low level when active. Set to the Low level while each write and read method is being executed.)
1: Manual (Use .setChipSelect() to control the chip select signal. Note that when the SPI is opened and closed, the High level is forcibly set.)
returnboolean-true: Success
false: Failure

Clock mode details

.close()

Ends the communication.

NameTypeM/OSummaryNote
returnboolean-true: Success
false: Failure

.write(buff)

Writes the data.

NameTypeM/OSummaryNote
buffArrayBuffermandatoryWrite data
The data size that can be specified is 1 to 4096 bytes.
ArrayBuffer size will be written.
returnboolean-true: Success
false: Failure
If the parameter is abnormal, an exception is raised.

When the data length is 1 byte, use Uint8Array as follows:

var buff = new ArrayBuffer(1);
var arr = new Uint8Array(buff);
arr[0] = your_data;
var ret = spi.write(buff);

.read(len[,dummyData])

Reads the data.

NameTypeM/OSummaryNote
lennumbermandatorySpecify the read data length.
Range: 1 - 4096
dummyDatanumberoptionalSpecify the dummy data to be sent when reading.
Range: 0x00 - 0xFF
The default value is 0xFF.
returnArraybuffer-Read data
If read function fails, the returned size will be 0.
If the parameter is abnormal, an exception is raised.

.writeRead(buff,len[,dummyData])

Reads the data after writing the data.
If chip select mode is set to automatic, the chip select signal holds the Low level from the start of writing to the end of reading.

NameTypeM/OSummaryNote
buffArrayBuffermandatoryWrite data
The data size that can be specified is 1 to 4096 bytes.
ArrayBuffer size will be written.
lennumbermandatorySpecify the read data length.
Range: 1 - 4096
dummyDatanumberoptionalSpecify the dummy data to be sent when reading.
Range: 0x00 - 0xFF
The default value is 0xFF.
returnArraybuffer-Read data
If writing or reading fails, the returned size will be 0.
If the parameter is abnormal, an exception is raised.

.writeReadFullDup(buff)

Writes data and reads data at the same time. (Full-duplex communication)

NameTypeM/OSummaryNote
buffArrayBuffermandatoryWrite data
The data size that can be specified is 1 to 4096 bytes.
ArrayBuffer size will be written.
returnArraybuffer-Read data
The data size will be the same as the write data length.
If writing or reading fails, the returned size will be 0.
If the parameter is abnormal, an exception is raised.

.readAndCompare(targetValue,dataMask,readTimeout[,dummyData])

Repeats reading data until the specified target value is read.
1-byte data read is 'AND'ed with dataMask and compared with targetValue.
If chip select mode is set to automatic, the chip select signal will be low level every time 1-byte data is read.
Note that all processing is blocked until the target value is found.

NameTypeM/OSummaryNote
targetValuenumbermandatoryTarget value
Specify 1 byte of target data.
Range: 0x00 - 0xFF
dataMasknumbermandatoryData mask
Specify a 1-byte data mask.
Range: 0x00 - 0xFF
readTimeoutnumbermandatoryDetection processing timeout[ms]
Range: 1 - 100
The detection process can be continued for up to 100ms.
dummyDatanumberoptionalSpecify the dummy data to be sent when reading.
Range: 0x00 - 0xFF
The default value is 0xFF.
returnboolean-Detection result
true: Detected
false: Not detected (A timeout has occurred)
If the parameter is abnormal, an exception is raised.

.setChipSelect(sts)

Manually controls the chip select signal.
It can be used only when chip select mode is set to manual.

NameTypeM/OSummaryNote
stsbooleanmandatorytrue: Active (Low level output)
false: Idle (High level output)
returnboolean-true: Success
false: Failure
It will fail if the SPI is not open or if the chip select mode is set to automatic.



Object Usage Examples

Sample 1

This is a sample that writes command data 0x9F and then reads 3 bytes.
Uses automatic control of chip select signals.

var NODE_NO = 1;
var SPI_DUMMY = 0xFF;

var spi = new SPI(NODE_NO);
if(!(spi instanceof SPI)) {
    throw new TypeError("spi is not SPI Instance");
}

if(!spi.open(1,3,0,0)) { //10Mbps,Mode3,MSB,Auto
    print('spi open error');
    while(1); //Infinite loop
}; 

var sendCmdAndRecvRsp = function(cmd,len) {
    var buf = new ArrayBuffer(1);
    var arr = new Uint8Array(buf);
    arr[0] = cmd;
    return spi.writeRead(buf,len,SPI_DUMMY); //ArrayBuffer
}

var COMMAND = 0x9F;
var RECVLEN = 3;

var rBuf = sendCmdAndRecvRsp(COMMAND,RECVLEN);
if(!rBuf.byteLength) { 
    print('command error');
} else {
    var rArr = new Uint8Array(rBuf);
    for(var i=0; i<rArr.length; i++) {
        print(i + ':' + '0x' + rArr[i].toString(16));
    }
}

spi.close();

Sample 2

This is a sample that writes command data 0x9F and then reads 3 bytes.
Uses manual control of chip select signals.

var NODE_NO = 1;
var SPI_DUMMY = 0xFF;

var spi = new SPI(NODE_NO);
if(!(spi instanceof SPI)) {
    throw new TypeError("spi is not SPI Instance");
}

if(!spi.open(1,3,0,1)) { //10Mbps,Mode3,MSB,Manual
    print('spi open error');
    while(1); //Infinite loop
}; 

var sendCmdAndRecvRsp = function(cmd,len) {
    var buf = new ArrayBuffer(1);
    var arr = new Uint8Array(buf);
    arr[0] = cmd;
    var rbuf;
    spi.setChipSelect(true); //chip select manual control
    if(spi.write(buf)) {
        rbuf = spi.read(len,SPI_DUMMY); //ArrayBuffer
    } else {
        rbuf = new ArrayBuffer(0); //write error
    }
    spi.setChipSelect(false); //chip select manual control
    return rbuf;
}

var COMMAND = 0x9F;
var RECVLEN = 3;

var rBuf = sendCmdAndRecvRsp(COMMAND,RECVLEN);
if(!rBuf.byteLength) { 
    print('command error');
} else {
    var rArr = new Uint8Array(rBuf);
    for(var i=0; i<rArr.length; i++) {
        print(i + ':' + '0x' + rArr[i].toString(16));
    }
}

spi.close();

Sample 3

This is a sample of full-duplex communication.
Writes command data 0x9F and 3 bytes of dummy data, and reads data at the same time.

var NODE_NO = 1;
var SPI_DUMMY = 0xFF;

var spi = new SPI(NODE_NO);
if(!(spi instanceof SPI)) {
    throw new TypeError("spi is not SPI Instance");
}

if(!spi.open(1,3,0,0)) { //10Mbps,Mode3,MSB,Auto
    print('spi open error');
    while(1); //Infinite loop
}; 

var sendCmdAndRecvRsp = function(cmd) {
    var buf = new ArrayBuffer(4);
    var arr = new Uint8Array(buf);
    arr[0] = cmd;
    arr[1] = SPI_DUMMY;
    arr[2] = SPI_DUMMY;
    arr[3] = SPI_DUMMY;
    return spi.writeReadFullDup(buf); //ArrayBuffer
}

var COMMAND = 0x9F;

var rBuf = sendCmdAndRecvRsp(COMMAND);
if(!rBuf.byteLength) { 
    print('command error');
} else {
    var rArr = new Uint8Array(rBuf);
    for(var i=0; i<rArr.length; i++) {
        print(i + ':' + '0x' + rArr[i].toString(16));
    }
}

spi.close();

Sample 4

This is a sample that writes command data, then checks the status register and waits for a ready.
The .readAndCompare() method is useful for polling for a few milliseconds.

var NODE_NO = 1;
var SPI_DUMMY = 0xFF;

var spi = new SPI(NODE_NO);
if(!(spi instanceof SPI)) {
    throw new TypeError("spi is not SPI Instance");
}

if(!spi.open(1,3,0,1)) { //10Mbps,Mode3,MSB,Manual
    print('spi open error');
    while(1); //Infinite loop
}; 

var sendCmd = function(cmd) {
    var buf = new ArrayBuffer(1);
    var arr = new Uint8Array(buf);
    arr[0] = cmd;
    spi.setChipSelect(true); //chip select manual control
    var ret = spi.write(buf);
    spi.setChipSelect(false); //chip select manual control
    return ret;
}

var checkStatus = function(target,data_mask,timeout_ms) {
    var cmd = 0x05; //read status
    var buf = new ArrayBuffer(1);
    var arr = new Uint8Array(buf);
    arr[0] = cmd;
    var ret = false;
    spi.setChipSelect(true); //chip select manual control
    if(spi.write(buf)) {
        ret = spi.readAndCompare(target,data_mask,timeout_ms,SPI_DUMMY);
        if(!ret) print('timeout!');
    }
    spi.setChipSelect(false); //chip select manual control
    return ret;
}

var COMMAND;
var TARGET_BIT = 0x02;
var TIMEOUT_MS = 100;

COMMAND = 0x06;
if(!sendCmd(COMMAND)) print('command error');
//Wait for bit 1 of the status register to become 1.
print('status: ' + checkStatus(0x02,TARGET_BIT,TIMEOUT_MS));

COMMAND = 0x04;
if(!sendCmd(COMMAND)) print('command error');
//Wait for bit 1 of the status register to become 0.
print('status: ' + checkStatus(0x00,TARGET_BIT,TIMEOUT_MS));

spi.close();



Updated: 2020-11-30
← 13. UART15. I2C →
  • Abstracts
  • {SPI} Instance
  • Details
    • new SPI(nodeNo)
    • .open(baudrate,clkmode,bitorder,csmode)
    • Clock mode details
    • .close()
    • .write(buff)
    • .read(len[,dummyData])
    • .writeRead(buff,len[,dummyData])
    • .writeReadFullDup(buff)
    • .readAndCompare(targetValue,dataMask,readTimeout[,dummyData])
    • .setChipSelect(sts)
  • Object Usage Examples
    • Sample 1
    • Sample 2
    • Sample 3
    • Sample 4
AboutNewsProductsFAQPrivacy Policy}
NEQTO Console
IntroductionFundamentalsAdministrative ActionsDevice Management NEQTO Apps
NEQTO Bridge
NEQTO Bridge ModuleNEQTO Bridge Wi-Fi ModuleNEQTO Bridge LTE-1 ModuleError Logging Event Messages
API Documentation
API UsageGlobal APIRegional APIAPI Terms of Service
Jigsaw, Inc.
© 2023 JIG-SAW INC.