Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SharedSpiClient.h « SharedSpi « Hardware « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 19f56e565380fa135305c9266d03bf51524f494e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
 * SharedSpiDevice.h
 *
 *  Created on: 1 Jul 2019
 *      Author: David
 *
 *  This currently supports only a single SPI channel. To support multiple SPI channels we would need to make the underlying SERCOM device
 *  configured in SPI mode a separate object, and have a pointer or reference to it in SharedSpiDevice.
 */

#ifndef SRC_HARDWARE_SHAREDSPI_SHAREDSPICLIENT_H_
#define SRC_HARDWARE_SHAREDSPI_SHAREDSPICLIENT_H_

#include <RepRapFirmware.h>
#include "SpiMode.h"
#include <RTOSIface/RTOSIface.h>

class SharedSpiDevice;

class SharedSpiClient
{
public:
	SharedSpiClient(SharedSpiDevice& dev, uint32_t clockFreq, SpiMode m, Pin p, bool polarity) noexcept;

	void SetCsPin(Pin p) noexcept { csPin = p; InitCsPin(); }
	void SetCsPolarity(bool b) noexcept { csActivePolarity = b; }
	void SetClockFrequency(uint32_t clockFreq) noexcept { clockFrequency = clockFreq; }

	uint32_t GetFrequency() const noexcept { return clockFrequency; }

	bool Select(uint32_t timeout = Mutex::TimeoutUnlimited) const noexcept;					// get SPI ownership and select the device, return true if successful
	void Deselect() const noexcept;
	bool TransceivePacket(const uint8_t *tx_data, uint8_t *rx_data, size_t len) const noexcept;
	bool ReadPacket(uint8_t *rx_data, size_t len) const noexcept { return TransceivePacket(nullptr, rx_data, len); }
	bool WritePacket(const uint8_t *tx_data, size_t len) const noexcept { return TransceivePacket(tx_data, nullptr, len); }

private:
	void InitCsPin() const noexcept;

	SharedSpiDevice& device;
	uint32_t clockFrequency;
	Pin csPin;
	SpiMode mode;
	bool csActivePolarity;
};

#endif /* SRC_HARDWARE_SHAREDSPI_SHAREDSPICLIENT_H_ */