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

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

View File

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

View File

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

View File

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

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

View File

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