STM32F3xx IO¶
Below is the implementation documentation for the IO supported by the STM32F302x8 and the STM32F334x8. Both platforms use the same HAL and thus share similar implementations in EVT-core.
CAN¶
-
class EVT::core::IO::CANf3xx : public EVT::core::IO::CAN¶
STMF3xx implementation of the CAN protocol.
The STM32f3xx has an on board CAN controller which adds additional features.
Ability to generate interrupts which allows users to add custom call backs
Hardware based message filtering which allows the filtering of messages to be handled by hardware not software.
Public Functions
-
CANf3xx(Pin txPin, Pin rxPin, bool loopbackEnabled = false)¶
Create a new instance of an STM32f3xx CAN interface.
-
virtual CANStatus connect(bool autoBusOff = false) override¶
Attempt to join the CAN network.
For the STM32f3xx this involves attempting to startup the CAN interface. This could cause an error in the case of invalid parameters.
- Parameters
autoBusOff – [in] Indicates the state halCAN.Init.AutoBusOff should be in
- Returns
CANStatus::OK on success, CANStatus::ERROR otherwise
-
virtual CANStatus disconnect()¶
Disconnect from the CAN network.
For the STM32f3xx this involves disconnect from the CAN interface.
- Returns
CANStatus::OK if we successfully de-init the CAN interface
-
virtual CANStatus transmit(CANMessage &message)¶
Transmit the message over CAN.
- Parameters
message – [in] The message to send over CAN.
- Returns
The status associated with sending the message
-
virtual CANStatus receive(CANMessage *message, bool blocking = false)¶
Receive a message over CAN.
The user can either receive in blocking or non-blocking mode. In blocking mode, the code will hang until a message is received then return a pointer to the message that was passed in. In non-blocking, a nullptr will be returned if no message is currently in the mailbox.
- Parameters
message – [out] The message to populate with data
blocking – [in] Used to determine if received should block or not, by default receive is blocking
- Returns
A pointer to the passed in message, nullptr if message not received.
- Returns
The status of the receive call, CANStatus::TIMEOUT returned if no message received
-
virtual CANStatus addCANFilter(uint16_t filterExplicitId, uint16_t filterMask, uint8_t filterBank)¶
Instantiates a new CAN filter using the 16-bit ID-Mask mode.
If a filter bank that is already in use is provided, the existing filter will be overwritten. A second pair of filter Id and mask can be given to the same filter bank.
- Parameters
filterExplicitId – [in] 11-bit identifier that must be an exact match to pass
filterMask – [in] 16-bit mask where [1] means care and [0] means don’t care
filterBank – [in] value between 0-13 where the filter info is stored
- Returns
The status associated with adding a new CAN message filter
-
virtual CANStatus enableEmergencyFilter(uint32_t state)¶
Enable or disable a filter that lets through any CAN messages that, following the CANopen standard, begin with the Emergency code of 001.
- Parameters
state – [in] Enum passed to set filter functional state
- Returns
The status associated with setting the energency filter state
-
void addCANMessage(CANMessage &message)¶
Add a message to the CAN receive queue.
NOTE: This is public for use with the STM HAL interrupt handler. This method should not be used outside of that application.
- Parameters
message – [in] The CANmessage to add to the receive queue
-
bool triggerIRQ(CANMessage &message)¶
Manually trigger the user specified interrupt handler.
This is intended to be used by the STM HAL interrupt handler and generally should not be used beyound that use case.
NOTE: This is public for use with the STM HAL interrupt handler. This method should not be used outside of that application.
- Parameters
message – [in] The message to pass to the interrupt handler
- Returns
True if the interrupt handler exists and has handled the message
GPIO¶
-
class EVT::core::IO::GPIOf3xx : public EVT::core::IO::GPIO¶
Public Functions
-
GPIOf3xx(Pin pin)¶
Create an instance of the STMF3xx GPIO pin using the provided pin.
The direction will have to be set manually before use.
- Parameters
pin – [in] The pin for the GPIO instance to use.
-
GPIOf3xx(Pin pin, Direction direction, Pull pull = Pull::PULL_DOWN)¶
Create an instance of the STMF3xx GPIO pin using the provided pin and direction.
- Parameters
pin – [in] The pin for the GPIO instance to use.
direction – [in] The flow of data (either input or output).
pull – [in] The direction of the internal pull resistor
-
virtual void setDirection(Direction direction) override¶
Set the direction of the pin.
- Parameters
direction – [in] The direction of information.
-
virtual void writePin(State state) override¶
Used for writing a state to a pin.
- Parameters
state – [in] The state to write to the pin
-
virtual State readPin() override¶
Used for reading the state of a pin.
- Returns
The state of the pin.
-
virtual void registerIRQ(TriggerEdge edge, void (*irqHandler)(GPIO *pin)) override¶
Registers the IRQHandler for this instances GPIO pin on the given edge condition.
- Parameters
edge – [in] The edge trigger event to trigger the interrupt
irqHandler – [in] The function pointer to handle the GPIO interrupt
Public Static Functions
-
static void gpioStateInit(GPIO_InitTypeDef *targetGpio, Pin *pins, uint8_t numOfPins, uint32_t mode, uint32_t pull, uint32_t speed, uint8_t alternate = 0x0DU)¶
Condenses gpio settings initialization into a single function.
- Parameters
targetGpio – gpio instance to initialize
pins – array of pins used by gpio instance
numOfPins – size of the pin array (either 1 or 2)
mode – gpio configuration mode
pull – pull-up or pull-down activation
speed – maximum gpio output frequency Possible values for Mode, Pull, and Speed can be found in “stm32f3xx_hal_gpio.h”
alternate – gpio alternate function selection
-
GPIOf3xx(Pin pin)¶
I2C¶
-
class EVT::core::IO::I2Cf3xx : public EVT::core::IO::I2C¶
Public Functions
-
I2Cf3xx(Pin sclPin, Pin sdaPin)¶
Make an instance of an I2C interface for the F3.
Will determine which I2C bus of the STM to use based on the provided pins.
-
virtual I2C::I2CStatus write(uint8_t addr, uint8_t byte) override¶
Write a single byte out over I2C.
-
virtual I2C::I2CStatus read(uint8_t addr, uint8_t *output) override¶
Read a single byte back from the I2C bus.
- Parameters
addr – [in] The 7 bit unshifted I2C address to read from
output – [out] The location to store the result from the read
- Returns
The status of making the read request
-
virtual I2C::I2CStatus write(uint8_t addr, uint8_t *bytes, uint8_t length) override¶
Write out multiple bytes over I2C.
Each byte will be written one by one.
-
virtual I2C::I2CStatus read(uint8_t addr, uint8_t *bytes, uint8_t length) override¶
Read multiple bytes from an I2C device.
- Parameters
addr – [in] The 7 bit unshifted I2C address to write to
bytes – [out] The buffer to fill with the read in bytes
length – [in] The number of bytes to read
- Returns
The status of making the read request
-
virtual I2C::I2CStatus writeMemReg(uint8_t addr, uint32_t memAddress, uint8_t byte, uint16_t memAddSize, uint8_t maxWriteTime) override¶
Write a single byte to a register in memory.
This is a separate method from normal I2C communication because memory read/write methods use a slightly different I2C pattern.
- Parameters
addr – [in] The 7 bit unshifted I2C address to write to
memAddress – [in] The word containing the register to write to
byte – [in] The data to write out
memAddSize – [in] The number of bytes in the memory address (1 or 2)
- Returns
The status of writing out a memory register
-
virtual I2C::I2CStatus readMemReg(uint8_t addr, uint32_t memAddress, uint8_t *byte, uint16_t memAddSize) override¶
Read a single byte from a register in memory.
This is a separate method from normal I2C communication because memory read/write methods use a slightly different I2C pattern.
- Parameters
addr – [in] The 7 bit unshifted I2C address to read from
memAddress – [in] The word containing the register to read from
byte – [out] The byte read from memory
memAddSize – [in] The number of bytes in the memory address (1 or 2)
- Returns
The result of attempting to read from memory
-
virtual I2C::I2CStatus writeMemReg(uint8_t addr, uint32_t memAddress, uint8_t *bytes, uint8_t size, uint16_t memAddSize, uint8_t maxWriteTime) override¶
Write a number of bytes to consecutive registers in memory, starting at a specified register.
This is a separate method from normal I2C communication because memory read/write methods use a slightly different I2C pattern.
- Parameters
addr – [in] The 7 bit unshifted I2C address to write to
memAddress – [in] The word containing the register to start writing to
byte – [in] The list of data to write out
size – [in] The number of bytes to be written
memAddSize – [in] The number of bytes in the memory address (1 or 2)
- Returns
The status of writing out to the memory address
-
virtual I2C::I2CStatus readMemReg(uint8_t addr, uint32_t memAddress, uint8_t *bytes, uint8_t size, uint16_t memAddSize) override¶
Read a number of consecutive bytes, starting at a specified register in memory.
This is a separate method from normal I2C communication because memory read/write methods use a slightly different I2C pattern.
- Parameters
addr – [in] The 7 bit unshifted I2C address to read from
memAddress – [in] The word containing the register to start reading from
byte – [out] The list of bytes read from memory
size – [in] The number of bytes to be read
memAddSize – [in] The number of bytes in the memory address (1 or 2)
output – [out] The value to store the read back memory
- Returns
The status of reading from the memory address;
-
I2Cf3xx(Pin sclPin, Pin sdaPin)¶
PWM¶
-
class EVT::core::IO::PWMf3xx : public EVT::core::IO::PWM¶
Public Functions
-
virtual void setDutyCycle(uint32_t dutyCycle)¶
Set the duty cycle for the pin to operate at.
- Parameters
dutyCycle – [in] Duty cycle as a whole number to set the pin to.
-
virtual void setPeriod(uint32_t period)¶
Set the period for the PWM in microseconds.
- Parameters
period – [in] The period of the PWM in microseconds.
-
virtual void setDutyCycle(uint32_t dutyCycle)¶
SPI¶
-
class EVT::core::IO::SPIf3xx : public EVT::core::IO::SPI¶
Public Functions
-
SPIf3xx(GPIO *CSPins[], uint8_t pinLength, Pin sckPin, Pin mosiPin, Pin misoPin)¶
Constructs an SPI instance in full duplex mode to send and receive data.
- Parameters
CSPins – an array of chip select pins for selecting which device to communicate with.
pinLength – the number of pins in the chip select array
sckPin – the pin for the clk line
mosiPin – the mosi pin for sending data
misoPin – the miso pin for receiving data
-
SPIf3xx(GPIO *CSPins[], uint8_t pinLength, Pin sckPin, Pin mosiPin)¶
Constructs an SPI instance in half duplex mode to only send data.
- Parameters
CSPins – an array of chip select pins for selecting which device to communicate with.
pinLength – the number of pins in the chip select array
sckPin – the pin for the clk line
mosiPin – the mosi pin for sending data
-
virtual void configureSPI(uint32_t baudRate, uint8_t mode, uint8_t order) override¶
Configures the SPI transmit mode.
- Parameters
baudRate – the baudrate to transmit at (4MHz to 31.25KHz)
mode – The SPIMode to use when sending (0-3)
order – MSB first or LSB first
-
virtual bool startTransmission(uint8_t device) override¶
Begin a device transmission.
Call before each set of read and write interactions.
- Parameters
device – the device number in the CSPins array
- Returns
true if valid device, false if device not in CSPins
-
virtual bool endTransmission(uint8_t device) override¶
Toggle the state of the chip select pin of a device back at the end of a transmission.
Call when finished reading or writing a data packet.
- Parameters
device – the device index in the CSPins
- Returns
true if valid device, false if device not in CSPins
-
virtual SPI::SPIStatus write(uint8_t byte) override¶
Writes a single byte out to the SPI device.
Call startTransmission() first to initiate device communication.
- Parameters
byte – the byte to write
- Returns
the status after calling the function
-
virtual SPI::SPIStatus read(uint8_t *out) override¶
Reads a single byte from a SPI device.
Call startTransmission() first to initiate device communication.
- Parameters
out – the byte read
- Returns
the status after calling the function
-
virtual SPI::SPIStatus write(uint8_t *bytes, uint8_t length) override¶
Writes an array of bytes to the SPI device.
Call startTransmission() first to initiate device communication.
- Parameters
bytes – an array of bytes of length n to write to SPI device
length – the length of the array
- Returns
the status after calling the function
-
virtual SPI::SPIStatus read(uint8_t *bytes, uint8_t length) override¶
Reads an array of bytes from a SPI device.
- Parameters
device – the device to write to in CSPins
bytes – an array of length n to receive the bytes from an SPI device
length – the number of bytes to receive
- Returns
SPIStatus of the HAL function call
-
SPIf3xx(GPIO *CSPins[], uint8_t pinLength, Pin sckPin, Pin mosiPin, Pin misoPin)¶
UART¶
-
class EVT::core::IO::UARTf3xx : public EVT::core::IO::UART¶
Public Functions
-
UARTf3xx(Pin txPin, Pin rxPin, uint32_t baudrate, bool isSwapped)¶
Create an instance of the STMF3xx UART interface using the provided TX and RX pins.
-
virtual void setBaudrate(uint32_t baudrate) override¶
Set the baudrate that the UART will operate with.
- Parameters
baudrate – [in] The new baudrate to use
-
virtual void setFormat(WordLength wordLength = WordLength::EIGHT, Parity parity = Parity::NONE, NumStopBits numStopBits = NumStopBits::ONE) override¶
Set the format that data will be communicated with over UART.
DEFAULT: 8 bit words, no parity, 1 stop bit
- Parameters
wordLength – [in] The number of bits in a work (5-8)
parity – [in] The parity settings to use.
numStopBits – [in] The number of stop bits (1-2)
-
virtual bool isReadable() override¶
Determines if the UART is currently readable.
- Returns
True if the UART is readable.
-
virtual bool isWritable() override¶
Determines if the UART is currently writable.
- Returns
True if the UART is writable
-
virtual void putc(char c) override¶
Put a single character to the UART module.
- Parameters
c – [in] The character to send over UART.
-
virtual void puts(const char *s) override¶
Put a null-terminal string out over UART.
- Parameters
s – [in] The null-terminated string to put over UART.
-
virtual char getc() override¶
Blocking call to read a single character from the UART module.
- Returns
The character read in over UART.
-
virtual void printf(const char *format, ...) override¶
Print a formatted string over UART, not great performance so best in test situations not production.
- Parameters
format – [in] The format string to print out.
-
virtual void write(uint8_t byte) override¶
Write out a single byte over UART.
- Parameters
byte – [in] The byte to write out over UART.
-
virtual uint8_t read() override¶
Blocking reading of a single byte from the UART.
- Returns
The byte read in from UART.
-
UARTf3xx(Pin txPin, Pin rxPin, uint32_t baudrate, bool isSwapped)¶