aboutsummaryrefslogtreecommitdiff
path: root/include/peripheralmanager/spi_device.h
blob: 631dae68e19b0b150ab5e6e63c5ae311a94923e1 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SYSTEM_PERIPHERALMANAGER_SPI_DEVICE_H_
#define SYSTEM_PERIPHERALMANAGER_SPI_DEVICE_H_

#include <sys/cdefs.h>
#include <sys/types.h>

__BEGIN_DECLS

/// @defgroup Spi Spi device interface
/// @brief Functions to control an SPI device.
///
/// These functions can be used to control an SPI device.
/// @{

/// Endianness.
enum {
  SPI_LSB_FIRST,  /**< Least significant bits first */
  SPI_MSB_FIRST  /**< Most significant bits first */
};

/// SPI phase modes.
enum {
  SPI_CPHA = 0x01,  /**< Clock phase */
  SPI_CPOL = 0x02  /**< Clock polarity */
};

/// SPI modes (similar to the Linux kernel's modes).
enum {
  SPI_MODE0 = 0,  /**< CPHA=0, CPOL=0 */
  SPI_MODE1 = SPI_CPHA,  /**< CPHA=1, CPOL=0 */
  SPI_MODE2 = SPI_CPOL,  /**< CPHA=0, CPOL=1 */
  SPI_MODE3 = SPI_CPHA + SPI_CPOL  /**< CPHA=1, CPOL=1 */
};

typedef struct BSpiDevice BSpiDevice;

/// Writes a byte to the device.
/// @param device Pointer to the BSpiDevice struct.
/// @param val Value to write.
/// @return Error code.
int BSpiDevice_writeByte(const BSpiDevice* device, uint8_t val);

/// Writes a buffer to the device.
/// @param device Pointer to the BSpiDevice struct.
/// @param data Buffer to write.
/// @param len Length of the buffer.
/// @return Error code.
int BSpiDevice_writeBuffer(const BSpiDevice* device,
                           const void* data,
                           size_t len);

/// Transfer data to the device.
/// @param device Pointer to the BSpiDevice struct.
/// @param tx_data Buffer to write. If NULL, no data will be written.
/// @param rx_data Buffer to read data in. If NULL, no data will be read.
/// @param len Length of the buffers.
/// @return Error code.
int BSpiDevice_transfer(const BSpiDevice* device,
                        const void* tx_data,
                        void* rx_data,
                        size_t len);

/// Sets the frequency in Hertz.
/// @param device Pointer to the BSpiDevice struct.
/// @param freq_hz Frequency to set.
/// @return Error code.
int BSpiDevice_setFrequency(const BSpiDevice* device, uint32_t freq_hz);

/// Sets the SPI mode.
/// @param device Pointer to the BSpiDevice struct.
/// @param mode Mode to use. One of SPI_MODE0, SPI_MODE1, SPI_MODE2, SPI_MODE3.
/// @return Error code.
int BSpiDevice_setMode(const BSpiDevice* device, int mode);

/// Sets the bit justification.
/// @param device Pointer to the BSpiDevice struct.
/// @param bit_justification One of SPI_LSB_FIRST OR SPI_MSB_FIRST.
/// @return Error code.
int BSpiDevice_setBitJustification(const BSpiDevice* device,
                                   int bit_justification);

/// Sets the number of bits per words.
/// @param device Pointer to the BSpiDevice struct.
/// @param bits_per_word Number of bits per word.
/// @return Error code.
int BSpiDevice_setBitsPerWord(const BSpiDevice* device, uint8_t bits_per_word);

/// Destroys a BSpiDevice struct.
/// @param device Pointer to the BSpiDevice struct.
void BSpiDevice_delete(BSpiDevice* device);

/// @}

__END_DECLS

#endif  // SYSTEM_PERIPHERALMANAGER_SPI_DEVICE_H_