-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPI.cpp
121 lines (105 loc) · 3.57 KB
/
SPI.cpp
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
113
114
115
116
117
118
119
120
/*
* Copyright (c) 2010 by Cristian Maglie <[email protected]>
* Copyright (c) 2014 by Paul Stoffregen <[email protected]> (Transaction API)
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#include "SPI.h"
SPIClass SPI;
/**
* @brief Default constructor. All done in CubeMX
*/
SPIClass::SPIClass()
{
}
/**
* @brief Initialize the SPI instance.
*/
void SPIClass::begin(SPI_TypeDef *SPI_TFT)
{
_spi.spi = SPI_TFT;
SET_BIT(_spi.spi->CR1, SPI_CR1_SPE);
}
/**
* @brief Deinitialize the SPI instance and stop it.
*/
void SPIClass::end(void)
{
CLEAR_BIT(_spi.spi->CR1, SPI_CR1_SPE);
}
/**
* @brief Transfer one byte on the SPI bus.
* begin() or beginTransaction() must be called at least once before.
* @param data: byte to send.
* @param skipReceive: skip receiving data after transmit or not.
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
* Optional, default: SPI_TRANSMITRECEIVE.
* @return byte received from the slave.
*/
uint8_t SPIClass::transfer(uint8_t data, bool skipReceive)
{
spi_transfer(&_spi, &data, (!skipReceive) ? &data : NULL, sizeof(uint8_t));
return data;
}
/**
* @brief Transfer two bytes on the SPI bus in 16 bits format.
* begin() or beginTransaction() must be called at least once before.
* @param data: bytes to send.
* @param skipReceive: skip receiving data after transmit or not.
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
* Optional, default: SPI_TRANSMITRECEIVE.
* @return bytes received from the slave in 16 bits format.
*/
uint16_t SPIClass::transfer16(uint16_t data, bool skipReceive)
{
uint16_t tmp;
spi_transfer(&_spi, (uint8_t *)&data, (!skipReceive) ? (uint8_t *)&data : NULL, sizeof(uint16_t));
return data;
}
/**
* @brief Transfer several bytes. Only one buffer used to send and receive data.
* begin() or beginTransaction() must be called at least once before.
* @param buf: pointer to the bytes to send. The bytes received are copy in
* this buffer.
* @param count: number of bytes to send/receive.
* @param skipReceive: skip receiving data after transmit or not.
* SPI_TRANSMITRECEIVE or SPI_TRANSMITONLY.
* Optional, default: SPI_TRANSMITRECEIVE.
*/
void SPIClass::transfer(void *buf, size_t count, bool skipReceive)
{
spi_transfer(&_spi, (uint8_t *)buf, (!skipReceive) ? (uint8_t *)buf : NULL, count);
}
/**
* @brief Transfer several bytes. One constant buffer used to send and
* one to receive data.
* begin() or beginTransaction() must be called at least once before.
* @param tx_buf: array of Tx bytes that is filled by the user before starting
* the SPI transfer. If NULL, default dummy 0xFF bytes will be
* clocked out.
* @param rx_buf: array of Rx bytes that will be filled by the slave during
* the SPI transfer. If NULL, the received data will be discarded.
* @param count: number of bytes to send/receive.
*/
void SPIClass::transfer(const void *tx_buf, void *rx_buf, size_t count)
{
spi_transfer(&_spi, ((const uint8_t *)tx_buf), ((uint8_t *)rx_buf), count);
}
/**
* @brief Not implemented.
*/
void SPIClass::attachInterrupt(void)
{
// Should be enableInterrupt()
}
/**
* @brief Not implemented.
*/
void SPIClass::detachInterrupt(void)
{
// Should be disableInterrupt()
}