Skip to content

Commit e4c429c

Browse files
committed
atsam4s implemented sendStop in i2c and removed repeated start flag
1 parent 5e1564b commit e4c429c

1 file changed

Lines changed: 37 additions & 31 deletions

File tree

targets/core/atmel/atsam4s/i2c.hpp

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ namespace klib::core::atsam4s::io {
1818
*
1919
* @tparam Read
2020
* @param address
21+
* @param device_address_size
2122
*/
2223
template <bool Read>
23-
constexpr static void read_write_set_address(const uint8_t address) {
24+
constexpr static void read_write_set_address(const uint8_t address, const uint8_t device_address_size = 0) {
2425
// set the address we want to write to
25-
I2c::port->MMR = ((address & 0x7f) << 16) | (Read << 12);
26+
I2c::port->MMR = ((address & 0x7f) << 16) | (Read << 12) | ((device_address_size & 0x3) << 8);
2627
}
2728

2829
/**
@@ -51,18 +52,19 @@ namespace klib::core::atsam4s::io {
5152

5253
/**
5354
* @brief Internal write implementation
55+
*
56+
* @note software doesnt check if stop is correctly send
5457
*
5558
* @tparam SendStop
56-
* @tparam RepeatedStart
5759
* @param address
5860
* @param data
5961
* @param size
6062
* @return true
6163
* @return false
6264
*/
63-
template <bool SendStop = true, bool RepeatedStart = false, typename T = std::span<const uint8_t>>
65+
template <bool SendStop = true, typename T = std::span<const uint8_t>>
6466
constexpr static bool write_impl(const uint8_t address, const T& data) {
65-
// hardware does not writes with less than 1 byte
67+
// hardware does not support writes with less than 1 byte
6668
if (!data.size()) {
6769
// return we could not transmit the data
6870
return false;
@@ -73,7 +75,7 @@ namespace klib::core::atsam4s::io {
7375

7476
// start the transaction (also set the end flag
7577
// if we have less or equal than 1 byte)
76-
I2c::port->CR = 0x1 | (data.size() <= 1 ? (0x1 << 1) : 0x00);
78+
I2c::port->CR = 0x1 | ((data.size() <= 1 && SendStop) ? (0x1 << 1) : 0x00);
7779

7880
// wait until we can write into the transmit holding register
7981
const uint32_t s = wait_for_status((0x1 << 8) | (0x1 << 2));
@@ -100,7 +102,7 @@ namespace klib::core::atsam4s::io {
100102
}
101103

102104
// do not send the stop again if less or equal to 1 byte
103-
if (data.size() > 1) {
105+
if (data.size() > 1 && SendStop) {
104106
// mark we are done transmitting (by setting the stop bit)
105107
I2c::port->CR = (0x1 << 1);
106108
}
@@ -112,7 +114,19 @@ namespace klib::core::atsam4s::io {
112114
return (wait_for_status(mask) & mask) == 0x1;
113115
}
114116

115-
template <bool SendStop = true, bool RepeatedStart = false, typename T = std::span<const uint8_t>>
117+
/**
118+
* @brief Internal read implementation
119+
*
120+
* @note software doesnt check if stop is correctly send
121+
*
122+
* @tparam SendStop
123+
* @tparam std::span<const uint8_t>
124+
* @param address
125+
* @param data
126+
* @return true
127+
* @return false
128+
*/
129+
template <bool SendStop = true, typename T = std::span<const uint8_t>>
116130
constexpr static bool read_impl(const uint8_t address, const T& data) {
117131
// hardware does not writes with less than 1 byte
118132
if (!data.size()) {
@@ -125,7 +139,7 @@ namespace klib::core::atsam4s::io {
125139

126140
// start the transaction (also set the end flag
127141
// if we have less or equal than 1 byte)
128-
I2c::port->CR = 0x1 | (data.size() <= 1 ? (0x1 << 1) : 0x00);
142+
I2c::port->CR = 0x1 | ((data.size() <= 1 && SendStop) ? (0x1 << 1) : 0x00);
129143

130144
// read all the data
131145
for (uint32_t i = 0; i < data.size(); i++) {
@@ -142,7 +156,7 @@ namespace klib::core::atsam4s::io {
142156
data[i] = I2c::port->RHR & 0xff;
143157

144158
// check if we need to send the stop condition
145-
if ((data.size() > 1) && ((i + 1) >= data.size())) {
159+
if ((data.size() > 1) && ((i + 1) >= data.size()) && SendStop) {
146160
// mark the next byte is the last
147161
I2c::port->CR = (0x1 << 1);
148162
}
@@ -156,6 +170,11 @@ namespace klib::core::atsam4s::io {
156170
}
157171

158172
public:
173+
/**
174+
* @brief Init the i2c bus
175+
*
176+
* @tparam Speed
177+
*/
159178
template <klib::io::i2c::speed Speed = klib::io::i2c::speed::fast>
160179
constexpr static void init() {
161180
// enable the clocks on the i2c peripheral
@@ -179,69 +198,56 @@ namespace klib::core::atsam4s::io {
179198
* @brief Read from a device on the i2c bus
180199
*
181200
* @tparam SendStop
182-
* @tparam RepeatedStart
183201
* @param address
184202
* @param data
185203
* @param size
186204
* @return state if read was successfull
187205
*/
188-
template <bool SendStop = true, bool RepeatedStart = false>
206+
template <bool SendStop = true>
189207
constexpr static bool read(const uint8_t address, const std::span<uint8_t>& data) {
190-
return read_impl<SendStop, RepeatedStart>(address, data);
208+
return read_impl<SendStop>(address, data);
191209
}
192210

193211
/**
194212
* @brief Read from a device on the i2c bus
195213
*
196214
* @tparam SendStop
197-
* @tparam RepeatedStart
198215
* @param address
199216
* @param data
200217
* @param size
201218
* @return state if read was successfull
202219
*/
203-
template <bool SendStop = true, bool RepeatedStart = false>
220+
template <bool SendStop = true>
204221
constexpr static bool read(const uint8_t address, const multispan<uint8_t>& data) {
205-
return read_impl<SendStop, RepeatedStart>(address, data);
222+
return read_impl<SendStop>(address, data);
206223
}
207224

208225
/**
209226
* @brief Write to a device on the i2c bus
210227
*
211228
* @tparam SendStop
212-
* @tparam RepeatedStart
213229
* @param address
214230
* @param data
215231
* @param size
216232
* @return state if read was successfull
217233
*/
218-
template <bool SendStop = true, bool RepeatedStart = false>
234+
template <bool SendStop = true>
219235
constexpr static bool write(const uint8_t address, const std::span<const uint8_t>& data) {
220-
return write_impl<SendStop, RepeatedStart>(address, data);
236+
return write_impl<SendStop>(address, data);
221237
}
222238

223239
/**
224240
* @brief Write to a device on the i2c bus
225241
*
226242
* @tparam SendStop
227-
* @tparam RepeatedStart
228243
* @param address
229244
* @param data
230245
* @param size
231246
* @return state if read was successfull
232247
*/
233-
template <bool SendStop = true, bool RepeatedStart = false>
248+
template <bool SendStop = true>
234249
constexpr static bool write(const uint8_t address, const multispan<const uint8_t>& data) {
235-
return write_impl<SendStop, RepeatedStart>(address, data);
236-
}
237-
238-
/**
239-
* @brief Stop a transmission that has been started did
240-
* not send a stop
241-
*
242-
*/
243-
constexpr static void stop() {
244-
250+
return write_impl<SendStop>(address, data);
245251
}
246252
};
247253
}

0 commit comments

Comments
 (0)