公開済みのRP1-GPIOライブラリを利用したソフトウェアI2Cライブラリを作ってみた。
Raspberry Pi 5のRP1-Chipを使い倒す(GPIO)
簡単そうと思って始めたもののGPIOが非同期実行なためにマイクロ秒レベルで時間制御するのがやっかいすぎておおハマリ。(-_-;)
それと1MHz程度まで行けると予想していたのにGPIO入力のウェイトが大きすぎて350KHz程度の速度しかでない...とか、想定外のことがありすぎだったがなんとか完成。
【仕様】
・100KHz/400KHzのみ
・7bitアドレスのみ
・リピーテッド・スタート対応
・クロック・ストレッチ対応
・マルチ・マスター対応
・ArduinoのWire準拠クラス対応
【I2Cデバイスを列挙するサンプル・プログラム】
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 <stdio.h> #include "rp1-gpio.h" #include "i2c-gpio.h" RP1_GPIO rp1; I2C_GPIO i2c(rp1, 3, 2); int main(int argc, char **argv) { rp1.begin(); i2c.begin(); int cnt = 0; for (int i = 1; i < 0x7F; ++i) { if (i2c.write(i, nullptr, 0) == 0) { printf("%02X ", i); ++cnt; } } printf("\n%d devices.\n", cnt); rp1.end(); return 0; } |
【I2Cデバイスを列挙するサンプル・プログラム(Arduinoふう)】
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 |
#include <stdio.h> #include "rp1-gpio.h" #include "i2c-gpio.h" RP1_GPIO rp1; TwoWire Wire(rp1, 3, 2); int main(int argc, char **argv) { rp1.begin(); Wire.begin(); int cnt = 0; for (int i = 1; i < 0x7F; ++i) { Wire.beginTransmission(i); if (Wire.endTransmission() == 0) { printf("%02X ", i); ++cnt; } } printf("\n%d devices.\n", cnt); rp1.end(); return 0; } |
【ライブラリ】
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
/* i2c-gpio.h - Software I2C Driver for RP1-GPIO 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 "rp1-gpio.h" class I2C_GPIO { public: typedef enum { MODE_100KHz, MODE_400KHz, } MODE; typedef enum { STATUS_OK = 0, STATUS_NAK = -1, STATUS_BUSY = -2, STATUS_TIMEOUT = -3, STATUS_NO_DEVICE = -4, } STATUS; I2C_GPIO(RP1_GPIO &gpio, int scl, int sda) : _gpio(&gpio), _scl(scl), _sda(sda), _spec(SPECS) { } virtual ~I2C_GPIO(void) { } void begin(MODE mode = MODE_100KHz) { _spec = &SPECS[mode]; initIO(); } int write(int addr, const void *buf, size_t len, bool sendStop = true) { STATUS rv = STATUS_NO_DEVICE; size_t cnt = 0; if ((addr & ~0x7F) == 0) { if ((rv = start()) == STATUS_OK) { if ((rv = write((addr << 1) | 0)) == STATUS_OK) { const uint8_t *p = (const uint8_t *)buf; for (; cnt < len; ++cnt) { if ((rv = write(*p++)) != STATUS_OK) break; } } rv = stop(sendStop, rv); } } return rv ? rv : cnt; } int read(int addr, void *buf, size_t len, bool sendStop = true) { STATUS rv = STATUS_NO_DEVICE; size_t cnt = 0; if ((addr & ~0x7F) == 0) { if ((rv = start()) == STATUS_OK) { if ((rv = write((addr << 1) | 1)) == STATUS_OK) { uint8_t b, *p = (uint8_t *)buf; for (; cnt < len; ++cnt) { if ((rv = read(b, cnt + 1 < len)) != STATUS_OK) break; *p++ = b; } } rv = stop(sendStop, rv); } } return rv ? rv : cnt; } int writeAndRead(int addr, const void *wbuf, size_t wlen, void *rbuf, size_t rlen) { int rv = 0; if (wbuf && wlen) { if (write(addr, wbuf, wlen, rbuf && rlen ? false : true) < 0) return 0; } if (rbuf && rlen) rv = read(addr, rbuf, rlen); return rv; } private: typedef struct { uint16_t THDSTA, THDDAT, TLOW, THIGH, TSUSTA, TSUSTO, TSUDAT, TBUF; } spec_t; static constexpr spec_t SPECS[3] = { { 4000, 300, 4700, 4000, 4700, 4000, 250, 4700 }, /* Standard Mode (100KHz) */ { 600, 0, 1300, 600, 600, 600, 100, 1300 }, /* Fast Mode (400KHz) */ }; static constexpr int LOW = 0; static constexpr int HIGH = 1; static constexpr long DELAY_AD = 300L; /* 300ns */ static constexpr long TIMEOUT_CS = 200000000L; /* 200ms */ RP1_GPIO *_gpio; int _scl; int _sda; const spec_t *_spec; inline void initIO(void) { /* Keep signal lines glitch-free */ _gpio->pinMode(_sda, _gpio->INPUT); _gpio->pinMode(_scl, _gpio->INPUT); output(_sda, HIGH); output(_scl, HIGH); _gpio->setPullUpDown(_sda, _gpio->PULL_UP); _gpio->setPullUpDown(_scl, _gpio->PULL_UP); _gpio->pinMode(_sda, _gpio->OPEN_DRAIN); _gpio->pinMode(_scl, _gpio->OPEN_DRAIN); } inline int input(int port) { return _gpio->digitalRead(port) ? HIGH : LOW; } inline void output(int port, int data) { _gpio->digitalWrite(port, data ? _gpio->HIGH : _gpio->LOW); } inline void outputL(int port) { output(port, LOW); } STATUS outputH(int port) { output(port, HIGH); /* clock stretching */ if (port == _scl) { for (unsigned long t = _gpio->nanos(); input(port) == LOW; ) { if (_gpio->nanos() - t >= TIMEOUT_CS) return STATUS_TIMEOUT; } } /* check bus conflicts */ else if (port == _sda) { if (input(port) == LOW) return STATUS_BUSY; } return STATUS_OK; } inline void delayNanoseconds(long ns) { if ((ns -= DELAY_AD) > 0) _gpio->delayNanoseconds(ns); } STATUS start(void) { if ((input(_sda) == LOW) || (input(_scl) == LOW)) { delayNanoseconds(_spec->TBUF); return STATUS_BUSY; } output(_scl, HIGH); outputL(_sda); delayNanoseconds(_spec->THDSTA); outputL(_scl); delayNanoseconds(_spec->THDDAT); return STATUS_OK; } STATUS stop(bool stop, STATUS status) { STATUS rv1, rv2; if (status < STATUS_NAK) { output(_sda, HIGH); output(_scl, HIGH); delayNanoseconds(_spec->TBUF); } else if (stop || (status != STATUS_OK)) { outputL(_sda); delayNanoseconds(_spec->TLOW); rv1 = outputH(_scl); delayNanoseconds(_spec->TSUSTO); rv2 = outputH(_sda); delayNanoseconds(_spec->TBUF); } else { output(_sda, HIGH); delayNanoseconds(_spec->TSUSTA); rv1 = outputH(_scl); delayNanoseconds(_spec->TSUSTO); rv2 = STATUS_OK; } return status ? status : (rv1 ? rv1 : rv2); } STATUS write(uint8_t b) { STATUS rv; for (uint8_t i = 0x80; i; i >>= 1) { output(_sda, b & i); delayNanoseconds(_spec->TLOW); if ((rv = outputH(_scl)) != STATUS_OK) return rv; if ((b & i) && (input(_sda) == LOW)) return STATUS_BUSY; delayNanoseconds(_spec->THIGH); outputL(_scl); delayNanoseconds(_spec->THDDAT); } output(_sda, HIGH); delayNanoseconds(_spec->TLOW); if ((rv = outputH(_scl)) != STATUS_OK) return rv; delayNanoseconds(_spec->THIGH); if (input(_sda)) rv = STATUS_NAK; outputL(_scl); delayNanoseconds(_spec->THDDAT); return rv; } STATUS read(uint8_t &b, bool ack = true) { STATUS rv; b = 0; output(_sda, HIGH); for (uint8_t i = 0x80; i; i >>= 1) { delayNanoseconds(_spec->TLOW); if ((rv = outputH(_scl)) != STATUS_OK) return rv; delayNanoseconds(_spec->THIGH); b |= input(_sda) ? i : 0; outputL(_scl); delayNanoseconds(_spec->THDDAT); } if (ack) outputL(_sda); delayNanoseconds(_spec->TLOW); if ((rv = outputH(_scl)) != STATUS_OK) return rv; delayNanoseconds(_spec->THIGH); outputL(_scl); delayNanoseconds(_spec->THDDAT); return rv; } }; class TwoWire : public I2C_GPIO { public: TwoWire(RP1_GPIO &gpio, int scl, int sda) : I2C_GPIO(gpio, scl, sda) { } virtual ~TwoWire(void) { } void setClock(long speed) { _speed = speed; begin(); } void begin(void) { if (_speed <= 100000) I2C_GPIO::begin(MODE_100KHz); else I2C_GPIO::begin(MODE_400KHz); } void end(void) { } void beginTransmission(int address) { _addr = address; _count = 0; _index = 0; _error = 0; } int endTransmission(bool sendStop = true) { if (_error) return 1; // buffer overflow int rv = I2C_GPIO::write(_addr, _buffer, _index, sendStop); if (rv == (int)_index) return 0; // success return (rv < 0 ? 2 : 3); } int requestFrom(int address, size_t quantity, bool sendStop = true) { if (quantity > sizeof(_buffer)) quantity = sizeof(_buffer); int read = I2C_GPIO::read(address, _buffer, quantity, sendStop); _count = read > 0 ? read : 0; _index = 0; return _count; } virtual size_t write(uint8_t data) { if (_index >= sizeof(_buffer)) { _error = 1; return 0; } _buffer[_index++] = data; return 1; } virtual 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; } virtual size_t available(void) { return (_index < _count ? _count - _index : 0); } virtual int read(void) { int value = -1; if(_index < _count) value = _buffer[_index++]; return value; } virtual size_t readBytes(void *buf, size_t len) { uint8_t *p = (uint8_t *)buf; size_t cnt; for (cnt = 0; cnt < len; ++cnt) { int c = read(); if (c < 0) break; *p++ = c; } return cnt; } virtual int peek(void) { int value = -1; if (_index < _count) value = _buffer[_index]; return value; } private: long _speed = 100000; int _addr = 0; int _error = 0; size_t _count = 0; size_t _index = 0; uint8_t _buffer[256]; }; |