/* * SPIDevice.h Created on: 22 May 2014 * Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie) * Made available for the book "Exploring BeagleBone" * See: www.exploringbeaglebone.com * Licensed under the EUPL V.1.1 * * This Software is provided to You under the terms of the European * Union Public License (the "EUPL") version 1.1 as published by the * European Union. Any use of this Software, other than as authorized * under this License is strictly prohibited (to the extent such use * is covered by a right of the copyright holder of this Software). * * This Software is provided under the License on an "AS IS" basis and * without warranties of any kind concerning the Software, including * without limitation merchantability, fitness for a particular purpose, * absence of defects or errors, accuracy, and non-infringement of * intellectual property rights other than copyright. This disclaimer * of warranty is an essential part of the License and a condition for * the grant of any rights to this Software. * * For more details, see http://www.derekmolloy.ie/ */ #pragma once #include class spi_device { public: /// SPI modes. enum mode { /// Low at idle, capture on rising clock edge mode0 = 0, /// Low at idle, capture on falling clock edge mode1 = 1, /// High at idle, capture on falling clock edge mode2 = 2, /// High at idle, capture on rising clock edge mode3 = 3 }; private: /// Base path of SPI devices. static constexpr const char* base_path = "/dev/spidev"; /// Bus number. unsigned int bus_; /// Device number on the bus. unsigned int device_; /// Device file descriptor. int fd_ = -1; /// The precise filename for the SPI device. std::string filename_; /// The current SPI mode. mode mode_ = mode3; /// The number of bits per word uint8_t bits_ = 8; /// The speed of transfer in Hz. uint32_t speed_ = 500000; /// The transfer delay in usecs. uint16_t delay_ = 0; public: spi_device(unsigned bus, unsigned device); unsigned char read(unsigned char register_address); unsigned char read_register(unsigned char register_address); int write_register(unsigned char register_address, unsigned char value); int write(unsigned char value); int write(unsigned char value[], unsigned length); int set_speed(uint32_t speed); int set_mode(mode mode); int set_bits_per_word(uint8_t bits); ~spi_device(); int transfer(unsigned char send[], unsigned char receive[], unsigned length); };