Skip to content

Commit bf2deb8

Browse files
committed
Adds SPI implementation
*Adds `writeWord()` and `writeRegion()`
1 parent ae3e1ca commit bf2deb8

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/sfeTkArdSPI.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ sfeTkError_t sfeTkArdSPI::init(bool bInit)
8282
}
8383

8484
//---------------------------------------------------------------------------------
85-
// writeRegisterByte()
85+
// writeByte()
8686
//
8787
// Writes a single byte to the device.
8888
//
@@ -108,6 +108,46 @@ sfeTkError_t sfeTkArdSPI::writeByte(uint8_t dataToWrite)
108108
return kSTkErrOk;
109109
}
110110

111+
//---------------------------------------------------------------------------------
112+
// writeWord()
113+
//
114+
// Writes a word to the device without indexing to a register.
115+
//
116+
// Returns kSTkErrOk on success
117+
//
118+
sfeTkError_t sfeTkArdSPI::writeWord(uint16_t dataToWrite)
119+
{
120+
return writeRegion((uint8_t *)&dataToWrite, sizeof(uint8_t)) > 0;
121+
}
122+
123+
124+
//---------------------------------------------------------------------------------
125+
// writeRegion()
126+
//
127+
// Writes an array of data to the device without indexing to a register.
128+
//
129+
// Returns kSTkErrOk on success
130+
//
131+
sfeTkError_t sfeTkArdSPI::writeRegion(const uint8_t *dataToWrite, size_t length)
132+
{
133+
134+
if (!_spiPort)
135+
return kSTkErrBusNotInit;
136+
137+
_spiPort->beginTransaction(_sfeSPISettings);
138+
// Signal communication start
139+
digitalWrite(cs(), LOW);
140+
141+
for (size_t i = 0; i < length; i++)
142+
_spiPort->transfer(*dataToWrite++);
143+
144+
// End communication
145+
digitalWrite(cs(), HIGH);
146+
_spiPort->endTransaction();
147+
148+
return kSTkErrOk;
149+
}
150+
111151
//---------------------------------------------------------------------------------
112152
// writeRegisterByte()
113153
//
@@ -164,6 +204,7 @@ sfeTkError_t sfeTkArdSPI::writeRegisterRegion(uint8_t devReg, const uint8_t *dat
164204

165205
// Signal communication start
166206
digitalWrite(cs(), LOW);
207+
167208
_spiPort->transfer(devReg);
168209

169210
for (size_t i = 0; i < length; i++)
@@ -278,4 +319,4 @@ sfeTkError_t sfeTkArdSPI::readRegister16Region(uint16_t devReg, uint8_t *data, s
278319
readBytes = numBytes;
279320

280321
return kSTkErrOk;
281-
}
322+
}

src/sfeTkArdSPI.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ class sfeTkArdSPI : public sfeTkISPI
115115
*/
116116
sfeTkError_t writeByte(uint8_t data);
117117

118+
/**
119+
@brief Write a word to the device without indexing to a register.
120+
121+
@param data Data to write.
122+
123+
@retval sfeTkError_t - kSTkErrOk on success
124+
*/
125+
sfeTkError_t writeWord(uint16_t data);
126+
127+
/**
128+
@brief Write an array of data to the device without indexing to a register.
129+
130+
@param data Data to write
131+
@param length Length of Data
132+
133+
@retval sfeTkError_t - kSTkErrOk on success
134+
*/
135+
sfeTkError_t writeRegion(const uint8_t *data, size_t length);
136+
118137
/**
119138
@brief Write a single byte to the given register
120139

0 commit comments

Comments
 (0)