Teste semf
Kontakt

SpiSlaveDevice

General

The SpiSlaveDevice class is a simple digital copy of an SpiSlave communicating with the internal SpiMasterHardware communication module.

Multiple slave devices can be handled by one master hardware interface.

All device specific settings can be set in the device class:

  • Transmission Mode (polarity and phase),
  • Wire Mode (Full duplex, half duplex, write only or read only),
  • Number of bits per frame,
  • Chip select and
  • Frequency

Definition

In the following example we define a simple SPI client, sending an one byte status and receiving an one byte answer. After the status is sent, the dataWritten signal is emitted and after the answer byte is received, the dataAvailable signal is emitted.

#include "Components/Communication/CommunicationDevice/spislavedevice.h"

class ExampleDevice : public SpiSlaveDevice
{
public:
	/** Constructor.*/
	ExampleDevice(SpiMasterHardware& spiMaster, Gpio& chipSelect);
	
	/** Destructor.*/
	virtual ~ExampleDevice() = default;
	
	/** Writes an one byte status to the i2c chip.*/
	void writeStatus(uint8_t& status);
	
	/** Reads an one byte answer from the i2c chip.*/
	void readAnswer(uint8_t& answer);

	/**Signal is fired after status byte is written.*/
	Signal<> statusWritten;
	/**Signal is fired after answer is read complete.*/
	Signal<> answerAvailable;
	/**Signal is fired after an bus error occured.*/
	Signal<> error;

private:
	/** Spi handling class.*/
	semf::SpiSlaveDevice m_spiDevice;
	/** Buffering data.*/
	uint8_t* m_data;
}

ExampleDevice::ExampleDevice(SpiMasterHardware& spiMaster, Gpio& chipSelect)
	:SpiSlaveDevice(spiMaster, chipSelect)
{
	// bus frequency can be set for every slave specific
	m_spiDevice.setFrequency(2500000);
}

ExampleDevice::writeStatus(uint8_t& status)
{
	m_spiDevice.error.clear();
	m_spiDevice.error.connect(&error, &Signal<>::emitSignal);
	
	m_spiDevice.dataWritten.clear();
	m_spiDevice.dataWritten.connect(&dataWritten, &Signal<>::emitSignal);
	
	m_data = &status;
	m_spiDevice.write(m_data, sizeof(data));
}

ExampleDevice::readAnswer(uint8_t& answer)
{
	m_spiDevice.error.clear();
	m_spiDevice.error.connect(&error, &Signal<>::emitSignal);
	
	m_spiDevice.dataAvailable.clear();
	m_spiDevice.dataAvailable.connect(&dataAvailable, &Signal<>::emitSignal);
	
	m_data = &answer;
	m_spiDevice.read(m_data, sizeof(data));
}

Initialization and Usage

#include "HardwareAbstraction/Stm32/stm32f7spimasterhardware.h"
#include "exampledevice.h"
#include "main.h"
extern SPI_HandleTypeDef hspi2;

semf::Stm32F7SpiMaster spiMaster(hspi2);

ExmapleDevice device(spiMaster);

// Write status
void onDeviceStatusWritten()
{
	// do something
}

device.dataWritten.connect(&onDeviceStatusWritten);
uint8_t status = 0x24;
device.writeStatus(status);

// Read answer
void onDeviceAnswerAvailable()
{
	// do something
}

device.dataAvailable.connect(&onDeviceAnswerAvailable);
uint8_t answer;
device.readAnswer(answer);

zurück zur Dokumentation