-
Notifications
You must be signed in to change notification settings - Fork 1k
API
This part describes the STM32 core functions.
CoreCallback functions allows to register a callback function called in the loop of the main() function.
If you need to call as often as possible a function to update your system and you want to be sure this function to be called, you can add it to the callback list. Otherwise, your function should be called inside the loop() function of
the sketch.
-
void registerCoreCallback(void (*func)(void)): register a callback function
Params func pointer to the callback function -
void unregisterCoreCallback(void (*func)(void)): unregister a callback function
Params func pointer to the callback function
analogReadDma()
has been added to use ADC with DMA. This method is non-blocking and increases the ADC sampling rate to the possible maximum (see MCU datasheet for more information).
-
bool analogReadDma(uint32_t ulPin, uint32_t *pData, uint32_t lData, void (*callback)(void *), void *callbackParameter)): read analog pin using ADC with DMA.
Params ulPin analog pin.
Params pData pointer to the buffer where save the samples. Must be large enough.
Params lData number of sample to read.
Params callback pointer to the callback function. Called when the number of sample is reached.
Params callbackParameter pointer to the callback parameters. Can be NULL.
Return true if conversion started else false.
By default, only one Serial
instance is available.
To use a second serial port, a HardwareSerial
object should be declared in the sketch before the setup()
function:
// RX TX
HardwareSerial Serial1(PA10, PA9);
void setup() {
Serial1.begin(115200);
}
void loop() {
Serial1.println("Hello World!");
delay(1000);
}
Another solution is to add a build_opt.h
file alongside your main .ino
file with: -DENABLE_HWSERIAL1
.
This will define the Serial1
instance using the first USART1
instance found in the PeripheralPins.c
of your variant.
Note that only the latter solution allows to use the serialEvent1()
callback in the sketch.
This part describes the STM32 libraries.
STM32 SPI library has been modified with the possibility to manage several CS pins without to stop the SPI interface.
We do not describe here the SPI Arduino API but the functionalities added.
We give to the user 3 possiblities about the management of the CS pin:
- the CS pin is managed directly by the user code before to transfer the data (like the Arduino SPI library)
- or the user gives the CS pin number to the library API and the library manages itself the CS pin (see example below)
- or the user uses a hardware CS pin linked to the SPI peripheral
-
SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel): alternative class constructor
Params SPI mosi pin
Params SPI miso pin
Params SPI sclk pin
Params (optional) SPI ssel pin. This pin must be an hardware CS pin. If you configure this pin, the chip select will be managed by the SPI peripheral. Do not use API functions with CS pin in parameter. -
void SPIClass::begin(uint8_t _pin): initialize the SPI interface and add a CS pin
Params spi CS pin to be managed by the SPI library -
void beginTransaction(uint8_t pin, SPISettings settings): allows to configure the SPI with other parameter. These new parameter are saved this an associated CS pin.
Params SPI CS pin to be managed by the SPI library
Params SPI settings -
void endTransaction(uint8_t pin): removes a CS pin and the SPI settings associated
Params SPI CS pin managed by the SPI library
Note 1 The following functions must be called after initialization of the SPI instance with begin() or beginTransaction().
If you have several device to manage, you can call beginTransaction() several time with different CS pin in parameter.
Then you can call the following functions with different CS pin without call again beginTransaction() (until you call end() or endTransaction()).
Note 2 If the mode is set to SPI_CONTINUE, the CS pin is kept enabled. Be careful in case you use several CS pin.
-
byte transfer(uint8_t pin, uint8_t _data, SPITransferMode _mode = SPI_LAST): write/read one byte
Params SPI CS pin managed by the SPI library
Params data to write
Params (optional) if SPI_LAST CS pin is reset, SPI_CONTINUE the CS pin is kept enabled.
Return byte received -
uint16_t transfer16(uint8_t pin, uint16_t _data, SPITransferMode _mode = SPI_LAST): write/read half-word
Params SPI CS pin managed by the SPI library
Params 16bits data to write
Params (optional) if SPI_LAST CS pin is reset, SPI_CONTINUE the CS pin is kept enabled.
Return 16bits data received -
void transfer(uint8_t pin, void *_buf, size_t _count, SPITransferMode _mode = SPI_LAST): write/read several bytes. Only one buffer used to write and read the data
Params SPI CS pin managed by the SPI library
Params pointer to data to write. The data will be replaced by the data read.
Params number of data to write/read. Params (optional) if SPI_LAST CS pin is reset, SPI_CONTINUE the CS pin is kept enabled. -
void transfer(byte _pin, void *_bufout, void *_bufin, size_t _count, SPITransferMode _mode = SPI_LAST): write/read several bytes. One buffer for the output data and one for the input data
Params SPI CS pin managed by the SPI library
Params pointer to data to write.
Params pointer where to store the data read.
Params number of data to write/read.
Params (optional) if SPI_LAST CS pin is reset, SPI_CONTINUE the CS pin is kept enabled.
This is an example of the use of the CS pin management:
#include <SPI.h>
// MOSI MISO SCLK
SPIClass SPI3(PC12, PC11, PC10);
void setup() {
SPI3.begin(2); //Enables the SPI3 instance with default settings and attaches the CS pin
SPI3.beginTransaction(1, settings); //Attaches another CS pin and configure the SPI3 instance with other settings
SPI3.transfer(2, 0x52); //Transfers data to the first device
SPI3.transfer(1, 0xA4); //Transfers data to the second device. The SPI3 instance is configured with the right settings
SPI3.end() //SPI3 instance is disabled
}
By default, only one Wire
instance is available and it uses the Arduino pins 14 and 15.
To use a second i2c port, a TwoWire
object should be declared in the sketch before the setup()
function:
#include <Wire.h>
// SDA SCL
TwoWire Wire2(PB3, PB10);
void setup() {
Wire2.begin();
}
void loop() {
Wire2.beginTransmission(0x71);
Wire2.write('v');
Wire2.endTransmission();
delay(1000);
}
-
Advanced usages