SPI based communication subclass

Public Types

enum type mono::redpine::ModuleSPICommunication::SpiRegisters

List of available SPI Registers on RS9113


SPI Interrupt occured register

Public Functions


Initialize invalid SPI communication

ModuleSPICommunication::ModuleSPICommunication(mbed::SPI &spi, PinName chipSelect, PinName resetPin, PinName interruptPin)

Create a communication class, and assign a SPI hardware interface

  • spi -

    The initialized and ready SPI hardware module

  • chipSelect -

    The SPI CS pin, active low

  • resetPin -

    The GPIO pin connected to the module reset line (active low)

  • interruptPin -

    The pin where the modules interrupt signal is connected

virtual void ModuleSPICommunication::resetModule()

Trigger the reset line to the module. A reset will put the module into bootloader, where a firmware image can be selected. Before calling initializeInterface, you must reset the module.

The reset command has to wait while the module resets, this means the function will block for approx 3 ms.

virtual bool ModuleSPICommunication::initializeInterface()

Send a interface initialization command to the module.

uint8_t ModuleSPICommunication::readRegister(SpiRegisters reg)

Internal function to read content of a SPI register SPI registers are only available to the modules SPI interface

the content of the 8-bit register
  • reg -

    The register to read

virtual uint16 ModuleSPICommunication::readMemory(uint32_t memoryAddress)

Internal function to read from a memory address. This is used when communicating with the Redpine Modules Bootloader.

The 16-bit content of that address
  • memoryAddress -

    The address position to read from

virtual void ModuleSPICommunication::writeMemory(uint32_t memoryAddress, uint16_t value)

Method to write to the module memory. This can be used when communicating with the bootloader of the module.

  • memoryAddress -

    The address to write to

  • value -

    The 16-bit value to write at the address

virtual bool ModuleSPICommunication::pollInputQueue()

Polls the module for waiting input data. This can be frame responses or any other data, waiting to be read by us.

The method uses the current communication interface method to wait for pending input data. This function blocks, until data is ready.

This function can be used it interrupts are not applicable

true if there is data to read, false otherwise

virtual bool ModuleSPICommunication::interruptActive()

Return true if interrupt is active.

The module will keep the interrupt pin high until no more input is present.

virtual bool ModuleSPICommunication::readFrame(DataReceiveBuffer &rawFrame)

Read the frame header (the first 16 bytes)

Use this to probe what kind of frame is coming from the module and read the frame payload later using the dedicated methods.

True on read success, false otherwise
  • rawFrame -

    A pointer to the pre-alloced memory to hold the header

virtual bool ModuleSPICommunication::readManagementFrame(DataReceiveBuffer &buffer, ManagementFrame &frame)

Read the first available frame from the modules input queue This function should be called when you are sure there is data pending

True on success, false otherwise
  • frame -

    A reference to management frame placeholder object

virtual bool ModuleSPICommunication::readManagementFrameResponse(DataReceiveBuffer &buffer, ManagementFrame &request)

Read a pending frame from the module, and interpret it as a response to an earlier frame.

The response frame (the one that is read) and the provided frame (the request), are compared by commandId and the response status is validated.

This there is any response payload data, this data is passed to the request frames ManagementFrame::responsePayloadHandler function.

When this method returns the request frame object is converted to an response frame (RX_FRAME) with status and any payload data.

true on success, false otherwise.
  • request -

    A reference to the request frame, that is awaiting a response

virtual bool ModuleSPICommunication::readDataFrame(DataReceiveBuffer &buffer, DataPayloadHandler &payloadHandler)

Read a pending frame a Data frame.

Data frame arrive out-of-order with anything else. Also, we expect that they deliver data to any open socket. This method read the data from the module and call the DataPayloadHandler function provided. This function then takes care of the actual data payload!

trueon succs, false otherwise
  • payloadHandler -

    A reference the data payload callback handler

virtual bool ModuleSPICommunication::writeFrame(ManagementFrame *frame)

Send a frame to the module

true on success, false otherwise
  • frame -

    A pointer to the frame to send

virtual bool ModuleSPICommunication::writePayloadData(const uint8_t *data, uint16_t byteLength, bool force4ByteMultiple)

Sends a frame’s payload data to the module.

This payload data must be sent after the command / management frame has been sent to the module.

This format of the data is determined by which command that was sent by the Command/Management frame. The RS9113 Software PRM is not clear on this data frame matter. But this code is from reverse engineering the example code by RSI.

true upon success, false otherwise.
  • data -

    A pointer to the raw data to write to the module

  • byteLength -

    The length of the data in bytes

  • force4ByteMultiple -

    Optional: Set to false, to bot enforce payload to be a 4-byte multiple

virtual void ModuleSPICommunication::onSystemPowerOnReset()

Called when the system powers on after a reset.

You can override this method. It gets called right after the system power on for the first time, or after a reset condition.

Use this method to setup / initialize components and data structures. It is only called once for each system reset.

virtual void ModuleSPICommunication::onSystemEnterSleep()

Called right before the MCU goes into sleep mode.

You can override this method to get sleep notifications. Before the CPU stop executing intructions and goes into low power sleep mode, this method gets called.

Use this method to prepare your data or MCU components for sleep. Help preserve battery by power down any peripheral, that is not needed during sleep.

This method can be called many times during your apps life cycle. (That is between resets.) After each sleep period, when the MCU wakes the onSystemWakeFromSleep is guaranteed to be called.

virtual void ModuleSPICommunication::onSystemWakeFromSleep()

Override to get notified when system wakes from sleep.

You can override this method to get wake-up notifications. When the CPU starts executing intructions and the power system has powered up all peripherals - this method gets called.

Use this method to setup your app to resume after sleep mode.

This method can be called many times during your apps life cycle. (That is between resets.)

virtual void ModuleSPICommunication::onSystemBatteryLow()

Override to get notified when the battery voltage reaches a critical level.

You can override this method to get battery low notifications. When this methods gets called, you have some time to finish critical tasks. That might writing state to file system or transfer data over the network.

Depending on the health of the battery, the time between this notification and the actual system enforced power off, might vary.

In contrast to the other power aware methods this is only called once in the application life cycle. After the enforced power off, when the battery is charged, the system will automatically reset.

Protected Functions

CommandStatus ModuleSPICommunication::sendC1C2(spiCommandC1 c1, spiCommandC2 c2)

Auxillary function to transfer C1 and C2 commands

The Redpine command status code (0x58 is success)
  • c1 -

    The C1 command byte, the SPI reponse here is ignored

  • c2 -

    The C2 command byte, the response for this byte is the cmd status

CommandStatus ModuleSPICommunication::sendC1C2(CommandC1 c1, CommandC2 c2)

Auxillary function to transfer C1 and C2 commands

The Redpine command status code (0x58 is success)
  • c1 -

    The C1 command object, the SPI reponse here is ignored

  • c2 -

    The C2 command object, the response for this byte is the cmd status

bool ModuleSPICommunication::waitForStartToken(bool thirtyTwoBitMode)

Auxillary function to poll module for a start token Transfers zeros to the module until some other than zeroes are received

True is module send a START_TOKEN command, false on timeout or unknown response.
  • thirtyTwoBitMode -

    Defaults to false, set TRUE for 32-bit mode SPI

bool ModuleSPICommunication::readFrameDescriptorHeader(frameDescriptorHeader *buffer)

Reads the 4 bytes of a data og management frame header.

The module sends a 4 byte header before sending the 16-byte frame descriptor. Used to query a pending frame for its length.

true is success, false otherwise
  • buffer -

    A pointer to where the length is stored

bool ModuleSPICommunication::readFrameBody(frameDescriptorHeader &frameHeader, SPIReceiveDataBuffer &buffer)

Read a pending frame, based on a frame descriptor header (describing the length), read the real frame - data og mgmt.

true on success, false otherwise
  • frameHeader -

    Reference to the header for the pending frame

  • buffer -

    A reference to the buffer where the frame is to be stored

int ModuleSPICommunication::spiWrite(const uint8_t *data, int byteLength, bool thirtyTwoBitFormat)

Convenience function to write data to mbed SPI The method only return the last received input from SPI, all other read bytes are discarded.

Use this function if you need to write multiple bytes at once. The method can transfer the data as 32-bit values if you choose.

the last read value on the SPI bus
  • data -

    A ponter to the data buffer, that will be written

  • byteLength -

    The length of the data to write, in bytes

  • thirtyTwoBitFormat -

    Set this to true to use 32-bit mode, default is false

void ModuleSPICommunication::setChipSelect(bool active)

Sets the SPI chip select for the module. This must be called before all SPI write og reads. This method automatically handles the value of the chip select pin.

  • active -

    Set this to true to activate the chip select, false otherwise.