公開済みのRP1-GPIOライブラリを利用したソフトウェアSPIライブラリを作ってみた。
Raspberry Pi 5のRP1-Chipを使い倒す(GPIO)
【仕様】
・最大通信速度はwrite()のみ5MHz、他は500KHz程度まで。
・データビット数は、1-32ビットに対応
・ビットオーダー対応
・チップセレクト対応(ポラリティ付き)
begin()にて、主要な動作を指定する。
void begin(MODE mode, long speed, int databit = 8, BITORDER bitorder = MSBFIRST, long cs_delay = 1000000);
databit <= 8
write/read/writeAndRead/transferメソッドのbufには1バイト型配列アドレスを指定し、lenにその配列数を指定する。
databit <= 16
write/read/writeAndRead/transferメソッドのbufには2バイト型配列アドレスを指定し、lenにその配列数を指定する。
databit <= 32
write/read/writeAndRead/transferメソッドのbufには4バイト型配列アドレスを指定し、lenにその配列数を指定する。
【使い方のサンプル】
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 |
#include <stdio.h> #include "rp1-gpio.h" #include "spi-gpio.h" RP1_GPIO rp1; /* with chip-select ピン番号を正数指定したときは、Select=LOW, Deselect=HIGH ピン番号が負数指定したときは、Select=HIGH, Deselect=LOW ※ピン番号0は負数指定できないので注意! */ int CS[] = { 8, -7 }; SPI_GPIO spi(rp1, 11, 10, 9, CS, sizeof(CS) / sizeof(CS[0])); /* without chip-select */ SPI_GPIO spi(rp1, 11, 10, 9); int main(int argc, char **argv) { char wbuf[16], rbuf[16]; rp1.begin(); spi.begin(spi.MODE3, 1000000); /* MODE3, 1MHz, 8bit, MSBFIRST, CS_DELAY:1000000ns */ /* simple call (with chip-select) */ spi.write(0, wbuf, sizeof(wbuf)); spi.read(0, rbuf, sizeof(rbuf)); spi.writeAndRead(0, wbuf, sizeof(wbuf), rbuf, sizeof(rbuf)); spi.transfer(0, wbuf, sizeof(wbuf)); /* multiple call (with chip-select) */ spi.select(0); spi.write(wbuf, sizeof(wbuf)); spi.read(rbuf, sizeof(rbuf)); spi.writeAndRead(wbuf, sizeof(wbuf), rbuf, sizeof(rbuf)); spi.transfer(wbuf, sizeof(wbuf)); spi.deselect(0); /* multiple call (without chip-select) */ rp1.pinMode(CS[0], rp1.OUTPUT); rp1.digitalWrite(CS[0], rp1.LOW); spi.select(); spi.write(wbuf, sizeof(wbuf)); spi.read(rbuf, sizeof(rbuf)); spi.writeAndRead(wbuf, sizeof(wbuf), rbuf, sizeof(rbuf)); spi.transfer(wbuf, sizeof(wbuf)); spi.deselect(); rp1.digitalWrite(CS[0], rp1.HIGH); 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 |
/* spi-gpio.h - Software SPI 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 <stdlib.h> #include <limits.h> #include "rp1-gpio.h" class SPI_GPIO { public: typedef enum { MODE0, MODE1, MODE2, MODE3, } MODE; typedef enum { LSBFIRST, MSBFIRST, } BITORDER; SPI_GPIO(RP1_GPIO &gpio, int sclk, int mosi, int miso, const int *cspins = nullptr, size_t cssize = 0) : _gpio(&gpio), _sclk(sclk), _mosi(mosi), _miso(miso) { setupCS(cspins, cssize); } virtual ~SPI_GPIO(void) { } void begin(MODE mode, long speed, int databit = 8, BITORDER bitorder = MSBFIRST, long delay = 1000000) { _mode = mode; _clock = 1000000000UL / 2 / speed; _dbit = databit > 0 ? databit - 1 : 0; _dmsb = 1 << _dbit; _bord = bitorder; _delay = delay; initIO(); } int write(int cs, const void *buf, size_t len) { select(cs); int rv = write(buf, len); deselect(cs); return rv; } int read(int cs, void *buf, size_t len) { select(cs); int rv = read(buf, len); deselect(cs); return rv; } int writeAndRead(int cs, const void *wbuf, size_t wlen, void *rbuf, size_t rlen) { select(cs); int rv = writeAndRead(wbuf, wlen, rbuf, rlen); deselect(cs); return rv; } void transfer(int cs, void *buf, size_t len) { select(cs); transfer(buf, len); deselect(cs); } void select(int cs = -1) { select0(cs, true); } void deselect(int cs = -1) { select0(cs, false); } int write(const void *buf, size_t len) { if (_dbit < 8) { const uint8_t *p = (const uint8_t *)buf; for (size_t i = 0; i < len; ++i) transfer0(p[i], false); } else if (_dbit < 16) { const uint16_t *p = (const uint16_t *)buf; for (size_t i = 0; i < len; ++i) transfer0(p[i], false); } else { const uint32_t *p = (const uint32_t *)buf; for (size_t i = 0; i < len; ++i) transfer0(p[i], false); } return len; } int read(void *buf, size_t len) { if (_dbit < 8) { uint8_t *p = (uint8_t *)buf; for (size_t i = 0; i < len; ++i) p[i] = transfer0(0xFFFFFFFF); } else if (_dbit < 16) { uint16_t *p = (uint16_t *)buf; for (size_t i = 0; i < len; ++i) p[i] = transfer0(0xFFFFFFFF); } else { uint32_t *p = (uint32_t *)buf; for (size_t i = 0; i < len; ++i) p[i] = transfer0(0xFFFFFFFF); } return len; } int writeAndRead(const void *wbuf, size_t wlen, void *rbuf, size_t rlen) { int rv = 0; if (wbuf && wlen) write(wbuf, wlen); if (rbuf && rlen) rv = read(rbuf, rlen); return rv; } void transfer(void *buf, size_t len) { if (_dbit < 8) { uint8_t *p = (uint8_t *)buf; for (size_t i = 0; i < len; ++i) p[i] = transfer0(p[i]); } else if (_dbit < 16) { uint16_t *p = (uint16_t *)buf; for (size_t i = 0; i < len; ++i) p[i] = transfer0(p[i]); } else { uint32_t *p = (uint32_t *)buf; for (size_t i = 0; i < len; ++i) p[i] = transfer0(p[i]); } } private: static constexpr int CPHA = 1; static constexpr int CPOL = 2; static constexpr int LOW = 0; static constexpr int HIGH = 1; static constexpr long DELAY_AD = 120L; /* 120ns */ RP1_GPIO *_gpio; int _sclk; int _mosi; int _miso; int _cs[32]; MODE _mode; long _clock; int _dbit; uint32_t _dmsb; BITORDER _bord; long _delay; inline void output(int port, int data) { _gpio->digitalWrite(port, data ? _gpio->HIGH : _gpio->LOW); } inline int input(int port) { return _gpio->digitalRead(port) ? HIGH : LOW; } void initIO(void) { _gpio->pinMode(_sclk, _gpio->INPUT); output(_sclk, _mode & CPOL ? HIGH : LOW); _gpio->pinMode(_sclk, _gpio->OUTPUT); _gpio->pinMode(_mosi, _gpio->OUTPUT); _gpio->pinMode(_miso, _gpio->INPUT); for (size_t i = 0; i < sizeof(_cs) / sizeof(_cs[0]); ++i) { int cs = _cs[i]; if (cs != INT_MIN) { _gpio->pinMode(abs(cs), _gpio->INPUT); outputCS(cs, false); _gpio->pinMode(abs(cs), _gpio->OUTPUT); } } } void outputCS(int port, bool enable) { if (port != INT_MIN) { int data; if (port >= 0) data = enable ? LOW : HIGH; else data = enable ? HIGH : LOW; output(abs(port), data); } } void setupCS(const int *pins, size_t size) { for (size_t i = 0; i < sizeof(_cs) / sizeof(_cs[0]); ++i) _cs[i] = pins && (i < size) ? pins[i] : INT_MIN; } inline void delayNanoseconds(long ns) { if ((ns -= DELAY_AD) > 0) _gpio->delayNanoseconds(ns); } void select0(int cs, bool enable) { if (!enable) { delayNanoseconds(_clock); output(_sclk, _mode & CPOL ? HIGH : LOW); delayNanoseconds(_delay); } if ((cs >= 0) && (cs < (int)(sizeof(_cs) / sizeof(_cs[0])))) outputCS(_cs[cs], enable); if (enable) { delayNanoseconds(_delay); output(_sclk, LOW); } } uint32_t transfer0(uint32_t data, bool read = true) { input(_sclk); /* good luck charm */ if ((_mode == MODE0) || (_mode == MODE3)) { delayNanoseconds(_clock); for (int i = 0; i <= _dbit; ++i) { output(_mosi, data & (_bord ? _dmsb : 1)); delayNanoseconds(_clock); output(_sclk, HIGH); if (_bord) data = (data << 1) | (read ? input(_miso) : 0); else data = (data >> 1) | (read ? input(_miso) << _dbit : 0); delayNanoseconds(_clock); output(_sclk, LOW); } } else { for (int i = 0; i <= _dbit; ++i) { delayNanoseconds(_clock); output(_mosi, data & (_bord ? _dmsb : 1)); output(_sclk, HIGH); delayNanoseconds(_clock); if (_bord) data = (data << 1) | (read ? input(_miso) : 0); else data = (data >> 1) | (read ? input(_miso) << _dbit : 0); output(_sclk, LOW); } } return data & (_dmsb | (_dmsb - 1)); } }; |