Skip to content

Commit 7c953fa

Browse files
committed
Add Serial and UART interface files.
1 parent 2e5e821 commit 7c953fa

File tree

3 files changed

+500
-0
lines changed

3 files changed

+500
-0
lines changed

src/sfTk/sfTkISerial.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* @file sfTkISerial.h
3+
* @brief Header file for the SparkFun Toolkit Base Serial Interface Definition.
4+
*
5+
* This file contains the interface declaration for basic serial read/write.
6+
*
7+
* @author SparkFun Electronics
8+
* @date 2025
9+
* @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License.
10+
*
11+
* SPDX-License-Identifier: MIT
12+
*/
13+
14+
#pragma once
15+
#include "sfTkError.h"
16+
#include <cstdint>
17+
18+
/**
19+
* @brief A base value for serial errors. All serial errors are greater than this value, in the 2000 range
20+
*
21+
*/
22+
const sfTkError_t ksfTkErrBaseSerial = 0x2000;
23+
24+
/**
25+
* @brief Error code for when a serial system is not initalized.
26+
*/
27+
const sfTkError_t ksfTkErrSerialNotInit = ksfTkErrFail * (ksfTkErrBaseSerial + 1);
28+
29+
/**
30+
* @brief Returned when a serial interface times out.
31+
*
32+
*/
33+
const sfTkError_t ksfTkErrSerialTimeout = ksfTkErrFail * (ksfTkErrBaseSerial + 2);
34+
35+
/**
36+
* @brief Returned when a serial interface does not respond.
37+
*
38+
*/
39+
const sfTkError_t ksfTkErrSerialNoResponse = ksfTkErrFail * (ksfTkErrBaseSerial + 3);
40+
41+
/**
42+
* @brief Returned when the data to be sent is too long or received is too short.
43+
*
44+
*/
45+
const sfTkError_t ksfTkErrSerialDataTooLong = ksfTkErrFail * (ksfTkErrBaseSerial + 4);
46+
47+
/**
48+
* @brief Returned when the serial settings are null, invalid, or on set/initialized.
49+
*
50+
*/
51+
const sfTkError_t ksfTkErrSerialNullSettings = ksfTkErrFail * (ksfTkErrBaseSerial + 5);
52+
53+
/**
54+
* @brief Returned when the buffer is null or invalid.
55+
*
56+
*/
57+
const sfTkError_t ksfTkErrSerialNullBuffer = ksfTkErrFail * (ksfTkErrBaseSerial + 6);
58+
59+
/**
60+
* @brief Returned when the bus is under read. Warning.
61+
*
62+
*/
63+
const sfTkError_t ksfTkErrSerialUnderRead = ksfTkErrBaseSerial + 7;
64+
65+
66+
class sfTkISerial
67+
{
68+
public:
69+
sfTkISerial() = default;
70+
virtual ~sfTkISerial() = default;
71+
72+
/**
73+
* @brief Writes an array of bytes to the serial interface.
74+
*
75+
* @param data The data to write
76+
* @param length The length of the data buffer
77+
* @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
78+
*/
79+
virtual sfTkError_t write(const uint8_t *data, size_t length) = 0;
80+
81+
/**
82+
* @brief Writes a single byte to the serial interface.
83+
*
84+
* @param data The data to write
85+
* @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
86+
*/
87+
virtual sfTkError_t write(const uint8_t data)
88+
{
89+
return write(&data, 1);
90+
}
91+
92+
/**
93+
* @brief Reads an array of bytes from the serial interface
94+
*
95+
* @param data The data buffer to read into
96+
* @param length The length of the data buffer
97+
* @param readBytes[out] The number of bytes read
98+
* @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
99+
*/
100+
virtual sfTkError_t read(uint8_t *data, size_t length, size_t &readBytes) = 0;
101+
102+
/**
103+
* @brief Read a single byte from the serial interface
104+
*
105+
* @param data Byte to be read
106+
* @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
107+
*/
108+
virtual sfTkError_t read(uint8_t &data)
109+
{
110+
size_t nRead;
111+
112+
return read(&data, sizeof(data), nRead);
113+
}
114+
115+
};

src/sfTk/sfTkISerialBus.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @file sfTkISerialBus.h
3+
* @brief Header file for the SparkFun Toolkit Base Serial Bus Interface Definition.
4+
*
5+
* This file contains the interface declaration for connecting sfTkISerial to the sfTkIBus.
6+
*
7+
* @author SparkFun Electronics
8+
* @date 2025
9+
* @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License.
10+
*
11+
* SPDX-License-Identifier: MIT
12+
*/
13+
14+
#pragma once
15+
#include "sfTkError.h"
16+
#include "sfTkIBus.h"
17+
#include "sfTkISerial.h"
18+
#include <cstdint>
19+
20+
const uint8_t ksfTkBusTypeSerialBus = 0x03;
21+
22+
class sfTkISerialBus : sfTkIBus
23+
{
24+
public:
25+
/**
26+
* @brief Constructor for the serial bus
27+
*
28+
*/
29+
sfTkISerialBus()
30+
{
31+
}
32+
33+
virtual ~sfTkISerialBus() = default;
34+
35+
/**
36+
* @brief Writes an array of bytes to the serial interface.
37+
*
38+
* @param data The data to write
39+
* @param length The length of the data buffer
40+
* @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
41+
*/
42+
virtual sfTkError_t write(const uint8_t *data, size_t length) = 0;
43+
44+
/**
45+
* @brief Writes an array of bytes to a register on the target address. Supports any address size
46+
*
47+
* @param devReg The device's register's address - can be any size, If nullptr, address is not sent
48+
* @param regLength The length of the register address. If 0, address is not sent
49+
* @param data The data to write
50+
* @param length The length of the data buffer
51+
* @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
52+
*/
53+
virtual sfTkError_t writeRegister(uint8_t *devReg, size_t regLength, const uint8_t *data, size_t length) override
54+
{
55+
56+
// Do we have a register? If so write it, else skip.
57+
if (devReg != nullptr && regLength > 0)
58+
write(devReg, regLength);
59+
60+
// Write the data.
61+
return write(data, length);
62+
}
63+
64+
/**
65+
* @brief Read an array of bytes from the serial interface
66+
*
67+
* @param data The data buffer to read into
68+
* @param length The length of the data buffer
69+
* @param readBytes[out] The number of bytes read
70+
* @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
71+
*/
72+
virtual sfTkError_t read(uint8_t *data, size_t length, size_t &readBytes) = 0;
73+
74+
/**
75+
* @brief Reads an array of bytes to a register on the target address. Supports any address size
76+
*
77+
* @param devReg The device's register's address - can be any size
78+
* @param regLength The length of the register address
79+
* @param data The data to buffer to read into
80+
* @param numBytes The length of the data buffer
81+
* @param readBytes[out] The number of bytes read
82+
* @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
83+
*/
84+
virtual sfTkError_t readRegister(uint8_t *devReg, size_t regLength, uint8_t *data, size_t numBytes, size_t &readBytes) override
85+
{
86+
// Buffer valid?
87+
if (!data)
88+
return ksfTkErrBusNullBuffer;
89+
90+
sfTkError_t retVal = ksfTkErrOk;
91+
92+
// Do we have a register? If so, write it, else skip.
93+
if (devReg != nullptr && regLength > 0)
94+
retVal = write(devReg, regLength);
95+
96+
if(retVal != ksfTkErrOk)
97+
return retVal;
98+
99+
// Read the data.
100+
retVal = read(data, numBytes, readBytes);
101+
102+
return retVal;
103+
}
104+
105+
/**
106+
* @brief Get the type of the object
107+
*
108+
* @return uint8_t The type of the object
109+
*/
110+
virtual uint8_t type(void) override
111+
{
112+
return ksfTkBusTypeSerialBus;
113+
}
114+
};

0 commit comments

Comments
 (0)