From d1c00ae3ab8b9f2dce3befe19ac4f6a617adbeec Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 25 May 2017 17:19:25 -0700 Subject: [PATCH] support for advanced SPI functions --- docs/reference/pins.md | 31 +++++++++++++---- docs/reference/pins/spi-format.md | 16 +++++++++ docs/reference/pins/spi-frequency.md | 19 +++++++++++ docs/reference/pins/spi-pins.md | 17 ++++++++++ libs/core/_locales/core-jsdoc-strings.json | 6 ++++ libs/core/_locales/core-strings.json | 3 ++ libs/core/pins.cpp | 39 +++++++++++++++++++++- libs/core/shims.d.ts | 25 ++++++++++++++ sim/state/misc.ts | 14 +++++++- 9 files changed, 162 insertions(+), 8 deletions(-) create mode 100644 docs/reference/pins/spi-format.md create mode 100644 docs/reference/pins/spi-frequency.md create mode 100644 docs/reference/pins/spi-pins.md diff --git a/docs/reference/pins.md b/docs/reference/pins.md index d3696892..9c396fb9 100644 --- a/docs/reference/pins.md +++ b/docs/reference/pins.md @@ -14,16 +14,35 @@ pins.onPulsed(DigitalPin.P0, PulseValue.High, () => { }); pins.pulseDuration(); pins.pulseIn(DigitalPin.P0, PulseValue.High); -pins.servoWritePin(AnalogPin.P0, 180); -pins.servoSetPulse(AnalogPin.P0, 1500); -pins.i2cReadNumber(0, NumberFormat.Int8LE); -pins.i2cWriteNumber(0, 0, NumberFormat.Int8LE); -pins.spiWrite(0); pins.setPull(DigitalPin.P0, PinPullMode.PullDown); pins.analogPitch(0, 0); pins.analogSetPitchPin(AnalogPin.P0); ``` +### Servos + +```cards +pins.servoWritePin(AnalogPin.P0, 180); +pins.servoSetPulse(AnalogPin.P0, 1500); +``` + +### I2C + +```cards +pins.i2cReadNumber(0, NumberFormat.Int8LE); +pins.i2cWriteNumber(0, 0, NumberFormat.Int8LE); +``` + +### SPI + +```cards +pins.spiWrite(0); +pins.spiFrequency(1000000); +pins.spiFormat(8,3); +pins.spiPins(DigitalPin.P0, DigitalPin.P1, DigitalPin.P2); +``` + ### See Also -[digitalReadPin](/reference/pins/digital-read-pin), [digitalWritePin](/reference/pins/digital-write-pin), [analogReadPin](/reference/pins/analog-read-pin), [analogWritePin](/reference/pins/analog-write-pin), [analogSetPeriod](/reference/pins/analog-set-period), [map](/reference/pins/map), [onPulsed](/reference/pins/on-pulsed), [pulseDuration](/reference/pins/pulse-duration), [pulseIn](/reference/pins/pulse-in), [servoWritePin](/reference/pins/servo-write-pin), [servoSetPulse](/reference/pins/servo-set-pulse), [i2cReadNumber](/reference/pins/i2c-read-number), [i2cWriteNumber](/reference/pins/i2c-write-number), [setPull](/reference/pins/set-pull), [analogPitch](/reference/pins/analog-pitch), [analogSetPitchPin](/reference/pins/analog-set-pitch-pin), [spiWrite](/reference/pins/spi-write) +[digitalReadPin](/reference/pins/digital-read-pin), [digitalWritePin](/reference/pins/digital-write-pin), [analogReadPin](/reference/pins/analog-read-pin), [analogWritePin](/reference/pins/analog-write-pin), [analogSetPeriod](/reference/pins/analog-set-period), [map](/reference/pins/map), [onPulsed](/reference/pins/on-pulsed), [pulseDuration](/reference/pins/pulse-duration), [pulseIn](/reference/pins/pulse-in), [servoWritePin](/reference/pins/servo-write-pin), [servoSetPulse](/reference/pins/servo-set-pulse), [i2cReadNumber](/reference/pins/i2c-read-number), [i2cWriteNumber](/reference/pins/i2c-write-number), [setPull](/reference/pins/set-pull), [analogPitch](/reference/pins/analog-pitch), [analogSetPitchPin](/reference/pins/analog-set-pitch-pin), [spiWrite](/reference/pins/spi-write), +[spiPins](/reference/pins/spi-pins),[spiFormat](/reference/pins/spi-format),[spiFrequency](/reference/pins/spi-frequency) diff --git a/docs/reference/pins/spi-format.md b/docs/reference/pins/spi-format.md new file mode 100644 index 00000000..06f427aa --- /dev/null +++ b/docs/reference/pins/spi-format.md @@ -0,0 +1,16 @@ +# SPI Format + +Sets the SPI format + +```sig +pins.spiFormat(8, 3); +``` + +### Parameters + +* ``bits``, +* ``mode``, + +### See also + +[SPI](https://developer.mbed.org/handbook/SPI) diff --git a/docs/reference/pins/spi-frequency.md b/docs/reference/pins/spi-frequency.md new file mode 100644 index 00000000..5e0ab570 --- /dev/null +++ b/docs/reference/pins/spi-frequency.md @@ -0,0 +1,19 @@ +# SPI Write + +Write to the SPI Slave and return the response. + +```sig +pins.spiWrite(0); +``` + +### Parameters + +* ``value``: value Data to be sent to the SPI slave + +### Returns + +* a [number](/types/number) Response from the SPI slave + +### See also + +[SPI](https://developer.mbed.org/handbook/SPI) diff --git a/docs/reference/pins/spi-pins.md b/docs/reference/pins/spi-pins.md new file mode 100644 index 00000000..c683f3f8 --- /dev/null +++ b/docs/reference/pins/spi-pins.md @@ -0,0 +1,17 @@ +# SPI Pins + +Sets the SPI MOSI, MISO and SCK pins + +```sig +pins.spiPins(DigitalPin.P0, DigitalPin.P1, DigitalPin.P2); +``` + +### Parameters + +* ``MOSI``, the ``MOSI`` pin +* ``MISO``, the ``MISO`` pin +* ``SCK``, the ``SCK`` pin + +### See also + +[SPI](https://developer.mbed.org/handbook/SPI) diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 5514dd40..6b91e893 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -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.", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index f60fdbee..1e114fb6 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -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", diff --git a/libs/core/pins.cpp b/libs/core/pins.cpp index 5027fc20..09ff484e 100644 --- a/libs/core/pins.cpp +++ b/libs/core/pins.cpp @@ -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); + } } diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index 059b2e08..111c0cda 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -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; } diff --git a/sim/state/misc.ts b/sim/state/misc.ts index 83ea7707..19804c42 100644 --- a/sim/state/misc.ts +++ b/sim/state/misc.ts @@ -107,6 +107,18 @@ namespace pxsim.pins { return 0; } + export function spiFrequency(f: number): void { + // TODO + } + + export function spiFormat(bits: number, mode: number): void { + // TODO + } + + export function spiPins(mosi: number, miso: number, sck: number) { + // TODO + } + export function i2cReadBuffer(address: number, size: number, repeat?: boolean): RefBuffer { // fake reading zeros return createBuffer(size) @@ -188,6 +200,6 @@ namespace pxsim.bluetooth { export function advertiseUrl(url: string, power: number, connectable: boolean) { } export function advertiseUidBuffer(nsAndInstance: RefBuffer, power: number, connectable: boolean) { } export function stopAdvertising() { } - export function setTransmitPower(power: number) {} + export function setTransmitPower(power: number) { } }