マスターモード専用のTWIライブラリ。Arduino互換クラスのオマケ付き。(笑)
【仕様】
・マルチマスター
・リピーテッドスタート
・7/10ビットスレーブアドレス
・クロックストレッチ
・任意の通信速度(50KHz~1MHz)
※バス状態により永久ループしてしまうことがある。これを防ぐには、
[avr8_config.h] #define CONFIG_RTC_USE 1
に設定したうえで、先にRtc::begin()とRtc::run()を実行しRTCを起動しておくこと。タイムアウトの規定値は200ms。begin()でタイムアウト指定しても良いが最長でも200usなので短すぎるかも。
【サンプルコード (Microchip Studio)】
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include "avr8_twi.h" #include "avr8_port.h" #define TWI_SDA Port::PA01 /* ATtiny202/402 */ #define TWI_SCL Port::PA02 /* ATtiny202/402 */ #define TWI_SADDR 0x12 Twi twi0(TWI0); int main(void) { sei(); Clock::setup(); uint8_t wbuf[...] = { ... }; uint8_t rbuf[...]; Port::pinMode(TWI_SDA, Port::INPUT_PULLUP); /* Internal Pullup */ Port::pinMode(TWI_SCL, Port::INPUT_PULLUP); /* Internal Pullup */ twi0.begin(100000UL); twi0.write(TWI_SADDR, wbuf, sizeof(wbuf), false); /* false is repeated start */ twi0.read(TWI_SADDR, rbuf, sizeof(rbuf)); return 0; } |
【サンプルコード (Arduino)】
データバッファサイズは、
[avr8_config.h] #define CONFIG_TWI_IOBUF_SIZE 16
に設定する。
|
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 |
#include "avr8_twi.h" #include "avr8_port.h" #define TWI_SDA Port::PA01 /* ATtiny202/402 */ #define TWI_SCL Port::PA02 /* ATtiny202/402 */ #define TWI_SADDR 0x12 TwoWire Wire(TWI0); void setup(void) { Port::pinMode(TWI_SDA, Port::INPUT_PULLUP); /* Internal Pullup */ Port::pinMode(TWI_SCL, Port::INPUT_PULLUP); /* Internal Pullup */ Wire.begin(100000UL); } void loop(void) { Wire.beginTransmission(TWI_SADDR); Wire.write(...); Wire.endTransmission(); Wire.requestFrom(TWI_SADDR, ...); if (Wire.available()) { Wire.read(...); ... } } |
【ライブラリ】
|
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
/* avr8_twi.h - TWI Driver for Microchip AVR8 Series Copyright (c) 2025 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #pragma once #include "avr8_config.h" #include "avr8_clock.h" #if CONFIG_RTC_USE #include "avr8_rtc.h" #endif #if defined(TWI1) #define TWI_MAX_CH 2 #elif defined(TWI0) #define TWI_MAX_CH 1 #else #define TWI_MAX_CH 0 #endif #if !defined(CONFIG_TWI_BUS_TIMEOUT) #define CONFIG_TWI_BUS_TIMEOUT 200000 #endif class Twi { public: /* SDA Setup Time select */ typedef enum { SDASETUP_4CYC = TWI_SDASETUP_4CYC_gc, /* SDA setup time is 4 clock cycles */ SDASETUP_8CYC = TWI_SDASETUP_8CYC_gc, /* SDA setup time is 8 clock cycles */ } SDASETUP; /* SDA Hold Time select */ typedef enum { SDAHOLD_OFF = TWI_SDAHOLD_OFF_gc, /* SDA hold time off */ SDAHOLD_50NS = TWI_SDAHOLD_50NS_gc, /* Typical 50ns hold time */ SDAHOLD_300NS = TWI_SDAHOLD_300NS_gc, /* Typical 300ns hold time */ SDAHOLD_500NS = TWI_SDAHOLD_500NS_gc, /* Typical 500ns hold time */ } SDAHOLD; /* Inactive Bus Timeout select */ typedef enum { TIMEOUT_DISABLED = TWI_TIMEOUT_DISABLED_gc, /* Bus Timeout Disabled */ TIMEOUT_50US = TWI_TIMEOUT_50US_gc, /* 50 Microseconds */ TIMEOUT_100US = TWI_TIMEOUT_100US_gc, /* 100 Microseconds */ TIMEOUT_200US = TWI_TIMEOUT_200US_gc, /* 200 Microseconds */ } TIMEOUT; /* Error Code */ typedef enum { ERROR_NO_SLAVE = -1, ERROR_TRANSFER = -2, } ERROR; Twi(TWI_t& twi, SDAHOLD hold = SDAHOLD_OFF, SDASETUP setup = SDASETUP_4CYC) : _twi(&twi), _ch((&twi - &TWI0) / sizeof(twi)) { _instances[_ch] = this; twi.CTRLA = setup | hold; twi.DBGCTRL = 0; } /* virtual */ ~Twi(void) { end(); _instances[_ch] = 0; } void begin(uint32_t speed = 100000UL, TIMEOUT timeout = TIMEOUT_DISABLED) { end(); _twi->CTRLA = (_twi->CTRLA & ~TWI_FMPEN_bm) | (speed >= 700000UL ? TWI_FMPEN_bm : 0); uint16_t baud = Clock::frequency() / speed / 2; _twi->MBAUD = baud <= 7 ? 1 : (baud > 0xFF + 7 ? 0xFF : baud - 7); _twi->MSTATUS = ~TWI_BUSSTATE_gm | TWI_BUSSTATE_IDLE_gc; _twi->MCTRLB = 0; _twi->MCTRLA = timeout | TWI_ENABLE_bm; } void end(void) { _twi->MCTRLA = 0; } void dbgctrl(bool dbgrun = true) { _twi->DBGCTRL = dbgrun; } int write(uint16_t addr, const void* buf, uint8_t len, bool stop = true) { int cnt = ERROR_NO_SLAVE; STATE state = start(addr, 0); if (state == STATE_ERROR) cnt = ERROR_TRANSFER; else if (state == STATE_ACK) { const uint8_t* p = (const uint8_t*)buf; for (cnt = 0; cnt < len; ++cnt) { _twi->MDATA = *p++; STATE state = wait(); if (state == STATE_NAK) break; if (state == STATE_ERROR) { cnt = ERROR_TRANSFER; break; } } cmd(stop || (state != STATE_ACK) ? CMD_STOP : CMD_REPSTART); } return cnt; } int read(uint16_t addr, void* buf, uint8_t len, bool stop = true) { int cnt = ERROR_NO_SLAVE; STATE state = start(addr, 1); if (state == STATE_ERROR) cnt = ERROR_TRANSFER; else if (state == STATE_ACK) { uint8_t* p = (uint8_t*)buf; for (cnt = 0; cnt < len; ++cnt) { if (cnt) cmd(CMD_RECVTRANS); STATE state = wait(); if (state == STATE_ERROR) { cnt = ERROR_TRANSFER; break; } *p++ = _twi->MDATA; } cmd(stop || (state == STATE_ERROR) ? CMD_STOP : CMD_REPSTART, ACKACT_NACK); } return cnt; } protected: /* Acknowledge Action select */ typedef enum { ACKACT_ACK = TWI_ACKACT_ACK_gc, /* Send ACK */ ACKACT_NACK = TWI_ACKACT_NACK_gc, /* Send NACK */ } ACKACT; /* Command select */ typedef enum { CMD_NOACT = TWI_MCMD_NOACT_gc, /* No Action */ CMD_REPSTART = TWI_MCMD_REPSTART_gc, /* Issue Repeated Start Condition */ CMD_RECVTRANS = TWI_MCMD_RECVTRANS_gc, /* Receive or Transmit Data, depending on DIR */ CMD_STOP = TWI_MCMD_STOP_gc, /* Issue Stop Condition */ } CMD; typedef enum { STATE_ACK, STATE_NAK, STATE_ERROR, } STATE; static Twi* _instances[TWI_MAX_CH]; TWI_t* _twi; uint8_t _ch; void cmd(CMD cmd, ACKACT ack = ACKACT_ACK) { _twi->MCTRLB = (_twi->MCTRLB & ~(TWI_ACKACT_bm | TWI_MCMD_gm)) | cmd | ack; } STATE start(uint16_t addr, bool read) { STATE state; if (addr & ~0x03FF) return STATE_NAK; cmd(CMD_NOACT); if (addr <= 0x7F) { _twi->MADDR = (addr << 1) | read; // write or read mode state = wait(); } else { _twi->MADDR = 0xF0 | ((addr >> 8) << 1) | 0; // write mode state = wait(); if (state == STATE_ACK) { _twi->MDATA = (uint8_t)addr; state = wait(); if ((state == STATE_ACK) && read) { cmd(CMD_REPSTART); _twi->MADDR |= 1; // read mode state = wait(); } } } if (state != STATE_ACK) cmd(CMD_STOP); return state; } STATE wait(void) { #if CONFIG_RTC_USE uint32_t t = Rtc::read(); while (Rtc::read() - t < RTC_US2TICK(CONFIG_TWI_BUS_TIMEOUT)) #else while (1) #endif { uint8_t status = _twi->MSTATUS; if (status & (TWI_BUSERR_bm | TWI_ARBLOST_bm)) break; if (status & (TWI_WIF_bm | TWI_RIF_bm)) return status & TWI_RXACK_bm ? STATE_NAK : STATE_ACK; } return STATE_ERROR; } }; /* Arduino compatible class */ class TwoWire : Twi { public: TwoWire(TWI_t& twi, SDAHOLD hold = SDAHOLD_300NS, SDASETUP setup = SDASETUP_4CYC) : Twi(twi, hold, setup) { } void begin(uint32_t speed = 100000UL, TIMEOUT timeout = TIMEOUT_DISABLED) { Twi::begin(speed, timeout); } void end(void) { Twi::end(); } void beginTransmission(uint16_t address) { _addr = address; _count = 0; _index = 0; _error = 0; } size_t write(uint8_t data) { if (_index >= sizeof(_buffer)) { _error = 1; return 0; } _buffer[_index++] = data; return 1; } size_t write(const void *buf, size_t len) { uint8_t cnt = 0; const uint8_t *p = (const uint8_t *)buf; while (len--) cnt += write(*p++); return cnt; } int endTransmission(bool sendStop = true) { if (_error) return 1; // buffer overflow int len = Twi::write(_addr, _buffer, _index, sendStop); return len == (int)_index ? 0 : (len < 0 ? 2 : 3); } int requestFrom(uint16_t address, size_t quantity, bool sendStop = true) { if (quantity > sizeof(_buffer)) quantity = sizeof(_buffer); int len = Twi::read(address, _buffer, quantity, sendStop); _index = 0; return _count = len > 0 ? len : 0; } size_t available(void) { return _index < _count ? _count - _index : 0; } int read(void) { return _index < _count ? _buffer[_index++] : -1; } size_t readBytes(void *buf, size_t len) { size_t cnt = 0; uint8_t* p = (uint8_t*)buf; for (; cnt < len; ++cnt) { int c = read(); if (c < 0) break; *p++ = c; } return cnt; } int peek(void) { return _index < _count ? _buffer[_index] : -1; } private: uint16_t _addr = 0; int _error = 0; size_t _count = 0; size_t _index = 0; uint8_t _buffer[CONFIG_TWI_IOBUF_SIZE]; }; |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/* avr8_twi.cpp - TWI Driver for Microchip AVR8 Series Copyright (c) 2025 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #include "avr8_twi.h" Twi* Twi::_instances[]; |
