support for advanced SPI functions

This commit is contained in:
Peli de Halleux
2017-05-25 17:19:25 -07:00
parent fb5a8cf64d
commit d1c00ae3ab
9 changed files with 162 additions and 8 deletions

View File

@ -242,6 +242,12 @@
"pins.setPull|param|name": "pin to set the pull mode on, eg: DigitalPin.P0",
"pins.setPull|param|pull": "one of the mbed pull configurations, eg: PinPullMode.PullUp",
"pins.sizeOf": "Get the size in bytes of specified number format.",
"pins.spiFormat": "Sets the SPI bits and mode",
"pins.spiFormat|param|bits": "the number of bits, eg: 8",
"pins.spiFormat|param|mode": "the mode, eg: 3",
"pins.spiFrequency": "Sets the SPI frequency",
"pins.spiFrequency|param|frequency": "the clock frequency, eg: 1000000",
"pins.spiPins": "Sets the MOSI, MISO, SCK pins used by the SPI instance",
"pins.spiWrite": "Write to the SPI slave and return the response",
"pins.spiWrite|param|value": "Data to be sent to the SPI slave",
"serial": "Reading and writing data over a serial connection.",

View File

@ -274,6 +274,9 @@
"pins.servoWritePin|block": "servo write|pin %name|to %value",
"pins.setEvents|block": "set pin %pin|to emit %type|events",
"pins.setPull|block": "set pull|pin %pin|to %pull",
"pins.spiFormat|block": "spi format|bits %bits|mode %mode",
"pins.spiFrequency|block": "spi frequency %frequency",
"pins.spiPins|block": "spi set pins|MOSI %mosi|MISO %miso|SCK %sck",
"pins.spiWrite|block": "spi write %value",
"pins|block": "pins",
"serial.delimiters|block": "%del",

View File

@ -383,7 +383,7 @@ namespace pins {
SPI* spi = NULL;
SPI* allocSPI() {
if (spi == NULL)
if (NULL == spi)
spi = new SPI(MOSI, MISO, SCK);
return spi;
}
@ -399,4 +399,41 @@ namespace pins {
return p->write(value);
}
/**
* Sets the SPI frequency
* @param frequency the clock frequency, eg: 1000000
*/
//% help=pins/spi-frequency weight=4 advanced=true
//% blockId=spi_frequency block="spi frequency %frequency"
void spiFrequency(int frequency) {
auto p = allocSPI();
p->frequency(frequency);
}
/**
* Sets the SPI bits and mode
* @param bits the number of bits, eg: 8
* @param mode the mode, eg: 3
*/
//% help=pins/spi-format weight=3 advanced=true
//% blockId=spi_format block="spi format|bits %bits|mode %mode"
void spiFormat(int bits, int mode) {
auto p = allocSPI();
p->format(bits, mode);
}
/**
* Sets the MOSI, MISO, SCK pins used by the SPI instance
*
*/
//% help=pins/spi-pins weight=2 advanced=true
//% blockId=spi_pins block="spi set pins|MOSI %mosi|MISO %miso|SCK %sck"
void spiPins(DigitalPin mosi, DigitalPin miso, DigitalPin sck) {
if (NULL != spi) {
delete spi;
spi = NULL;
}
spi = new SPI(getPin((int)mosi)->name, getPin((int)miso)->name, getPin((int)sck)->name);
}
}

25
libs/core/shims.d.ts vendored
View File

@ -700,6 +700,31 @@ declare namespace pins {
//% help=pins/spi-write weight=5 advanced=true
//% blockId=spi_write block="spi write %value" shim=pins::spiWrite
function spiWrite(value: number): number;
/**
* Sets the SPI frequency
* @param frequency the clock frequency, eg: 1000000
*/
//% help=pins/spi-frequency weight=4 advanced=true
//% blockId=spi_frequency block="spi frequency %frequency" shim=pins::spiFrequency
function spiFrequency(frequency: number): void;
/**
* Sets the SPI bits and mode
* @param bits the number of bits, eg: 8
* @param mode the mode, eg: 3
*/
//% help=pins/spi-format weight=3 advanced=true
//% blockId=spi_format block="spi format|bits %bits|mode %mode" shim=pins::spiFormat
function spiFormat(bits: number, mode: number): void;
/**
* Sets the MOSI, MISO, SCK pins used by the SPI instance
*
*/
//% help=pins/spi-pins weight=2 advanced=true
//% blockId=spi_pins block="spi set pins|MOSI %mosi|MISO %miso|SCK %sck" shim=pins::spiPins
function spiPins(mosi: DigitalPin, miso: DigitalPin, sck: DigitalPin): void;
}