#ifndef STA_CORE_BUS_SERIAL_DEVICE_HPP #define STA_CORE_BUS_SERIAL_DEVICE_HPP #include namespace sta { /** * @brief Abstract device for serial communication. */ class Device { public: /** * @param intf %SPI hardware interface */ Device(Interface * intf); /** * @brief Start transmission with device. * * Must be called before any I/O operations. */ void beginTransmission(); /** * @brief End transmission with device. * * Must be called after last I/O operation. */ void endTransmission(); /** * @brief Send single byte of data. * * @param value 8-bit value */ bool transfer(uint8_t value, uint32_t timeout = STA_MAX_TIMEOUT); /** * @brief Send two bytes of data. * * @param value 16-bit value */ bool transfer16(uint16_t value, uint32_t timeout = STA_MAX_TIMEOUT); /** * @brief Send data from buffer. * * @param buffer Source buffer * @param size Number of bytes to transfer */ bool transfer(const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT); /** * @brief Write data to a specific memory address of the peripheral. * * @param regAddr The memory address. * @param buffer The buffer of data to write to the address * @param size The number of bytes to write to the peripheral. */ bool writeMemory(uint8_t regAddr, const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT); /** * @brief Read incoming data to buffer. * * @param buffer Destination buffer * @param size Number of bytes to read */ bool receive(uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT); /** * @brief Read data from a specific memory address of the peripheral. * * @param regAddr The memory address. * @param buffer The buffer of data to write the received data to. * @param size The number of bytes to receive from the peripheral. */ bool readMemory(uint8_t regAddr, uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT); /** * @brief Send byte value repeatedly. * * @param value 8-bit value to repeat * @param count Number of repetitions */ void fill(uint8_t value, size_t count, uint32_t timeout = STA_MAX_TIMEOUT); protected: /** * @brief Activate device.. */ virtual void select() = 0; /** * @brief Deactivate device.. */ virtual void deselect() = 0; private: Interface * intf_; bool selected_ = false; }; } // namespace sta #endif // STA_CORE_BUS_SERIAL_DEVICE_HPP