| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 3 | * 2013-2022 by the Falcon Player Developers. | ||
| 4 | * | ||
| 5 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 6 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 7 | * file for descriptions of what files are covered by each license. | ||
| 8 | * | ||
| 9 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 10 | * included LICENSE.LGPL file. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "fpp-pch.h" | ||
| 14 | #include <cstdio> | ||
| 15 | #include <cstring> | ||
| 16 | |||
| 17 | #include <fcntl.h> | ||
| 18 | #include <unistd.h> | ||
| 19 | |||
| 20 | #include "../config.h" | ||
| 21 | |||
| 22 | #ifdef HAS_SPI | ||
| 23 | #include <linux/spi/spidev.h> | ||
| 24 | #endif | ||
| 25 | #include <sys/ioctl.h> | ||
| 26 | |||
| 27 | #include "SPIUtils.h" | ||
| 28 | |||
| 29 | 68 | SPIUtils::SPIUtils(int c, int baud) { | |
| 30 | 68 | channel = c; | |
| 31 | 68 | speed = baud; | |
| 32 | 68 | bitsPerWord = 8; | |
| 33 | 68 | file = -1; | |
| 34 | #ifdef HAS_SPI | ||
| 35 | char spiFileName[64]; | ||
| 36 | 68 | snprintf(spiFileName, sizeof(spiFileName), "/dev/spidev0.%d", channel); | |
| 37 |
1/2✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
|
68 | file = open(spiFileName, O_RDWR); |
| 38 |
1/2✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
|
68 | if (file >= 0) { |
| 39 | 68 | int mode = 0; | |
| 40 | 68 | ioctl(file, SPI_IOC_WR_MODE, &mode); | |
| 41 | 68 | ioctl(file, SPI_IOC_WR_BITS_PER_WORD, &bitsPerWord); | |
| 42 | 68 | ioctl(file, SPI_IOC_WR_MAX_SPEED_HZ, &speed); | |
| 43 | } | ||
| 44 | #endif | ||
| 45 | 68 | } | |
| 46 | 68 | SPIUtils::~SPIUtils() { | |
| 47 |
1/2✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
|
68 | if (file != -1) { |
| 48 | 68 | close(file); | |
| 49 | } | ||
| 50 | 68 | } | |
| 51 | |||
| 52 | 348 | int SPIUtils::xfer(uint8_t* tx, uint8_t* rx, int count) { | |
| 53 |
1/2✓ Branch 0 taken 348 times.
✗ Branch 1 not taken.
|
348 | if (file != -1) { |
| 54 | #ifdef HAS_SPI | ||
| 55 | struct spi_ioc_transfer spi; | ||
| 56 | 348 | channel &= 1; | |
| 57 | 348 | memset(&spi, 0, sizeof(spi)); | |
| 58 | |||
| 59 | 348 | spi.tx_buf = (unsigned long)tx; | |
| 60 | // A null rx is a send-only transfer. Do not receive into tx: the | ||
| 61 | // outputs pass the live sequence buffer as tx, and spidev would write | ||
| 62 | // whatever is on MISO back over it. | ||
| 63 | 348 | spi.rx_buf = (unsigned long)rx; | |
| 64 | 348 | spi.len = count; | |
| 65 | 348 | spi.delay_usecs = 0; | |
| 66 | 348 | spi.speed_hz = speed; | |
| 67 | 348 | spi.bits_per_word = bitsPerWord; | |
| 68 | |||
| 69 | 348 | return ioctl(file, SPI_IOC_MESSAGE(1), &spi); | |
| 70 | #endif | ||
| 71 | } | ||
| 72 | ✗ | return -1; | |
| 73 | } | ||
| 74 |