マスターモード専用のSPIライブラリ。スレーブモードはやる気がでたらということで...(-_-;)
【サンプルコード (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 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 |
#include "avr8_spi.h" #define SPI_SS Port::PA0 /* for ATtiny202/402 */ #define SPI_MOSI Port::PA1 /* for ATtiny202/402 */ #define SPI_MISO Port::PA2 /* for ATtiny202/402 */ #define SPI_SCK Port::PA3 /* for ATtiny202/402 */ #define SPI_SS2 Port::PA4 #define SPI_SS3 Port::PA5 Spi spi0(SPI0, SPI_MOSI, SPI_MISO, SPI_SCK, SPI_SS); int main(void) { sei(); Clock::setup(); /* SPI Mode 3, 4MHz, Delay 1us */ spi0.begin(4000000); uint8_t wdata[8] = { ... }; uint8_t rdata[8]; /* write and read (exchange) */ spi0.select(); spi0.xfer(wdata, rdata, sizeof(wdata)); spi0.deselect(); /* write */ spi0.select(); spi0.write(wdata, sizeof(data); spi0.deselect(); /* read */ spi0.select(); spi0.read(rdata, sizeof(data); spi0.deselect(); /* multi slave select */ /* init pin (setup pin mode) */ spi0.slave(SPI_SS2); spi0.slave(SPI_SS3, true); /* select SS2 */ spi0.slave(SPI_SS2); spi0.select(); ... spi0.deselect(); /* select SS3 */ spi0.slave(SPI_SS3, true); spi0.select(); ... spi0.deselect(); return 0; } |
【修正履歴】
2025-12-15
マルチ・スレーブ対応の仕様変更。slave()を追加しチップセレクトピンを極性付きで初期化できるように改良。
2025-12-13
マルチ・スレーブ対応のためselect()に省略可能なチップセレクトピンを指定できるようにしてみた。但し、マスター動作のために本来のSSピン制御は必要であるためスルチスレーブで使うさいは本来のSSピンは未接続とし他のピンをチップセレクトとして使う必要がある。
2025-12-12
コンストラクタにMOSI/MISO/SCK/SSピン指定を追加。SS制御用のselect()/deselect()/を追加。
2025-12-11
バッファリングモードに対応。転送データ間の間隔が短くなった分だけ転送速度が速くなった。
【ライブラリ】
|
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 |
/* avr8_spi.h - SPI 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 <util/delay.h> #include "avr8_config.h" #include "avr8_clock.h" #include "avr8_port.h" #if defined(SPI1) #define SPI_MAX_CH 2 #elif defined(SPI0) #define SPI_MAX_CH 1 #else #define SPI_MAX_CH 0 #endif class Spi { public: /* SPI Mode select */ typedef enum { MODE_0 = SPI_MODE_0_gc, /* SPI Mode 0 */ MODE_1 = SPI_MODE_1_gc, /* SPI Mode 1 */ MODE_2 = SPI_MODE_2_gc, /* SPI Mode 2 */ MODE_3 = SPI_MODE_3_gc, /* SPI Mode 3 */ } MODE; /* Prescaler select */ typedef enum { PRESC_DIV2 = SPI_PRESC_DIV4_gc | SPI_CLK2X_bm, /* System Clock / 2 */ PRESC_DIV4 = SPI_PRESC_DIV4_gc, /* System Clock / 4 */ PRESC_DIV8 = SPI_PRESC_DIV16_gc | SPI_CLK2X_bm, /* System Clock / 8 */ PRESC_DIV16 = SPI_PRESC_DIV16_gc, /* System Clock / 16 */ PRESC_DIV32 = SPI_PRESC_DIV64_gc | SPI_CLK2X_bm, /* System Clock / 32 */ PRESC_DIV64 = SPI_PRESC_DIV64_gc, /* System Clock / 64 */ PRESC_DIV128 = SPI_PRESC_DIV128_gc, /* System Clock / 128 */ } PRESC; Spi(SPI_t& spi, Port::PIN mosi, Port::PIN miso, Port::PIN sck, Port::PIN ss) : _spi(&spi), _ss(ss), _cs(Port::PNC), _ah(false) { Port::pinMode(mosi, Port::OUTPUT); Port::pinMode(miso, Port::INPUT); Port::pinMode(sck , Port::OUTPUT); Port::pinMode(ss , Port::OUTPUT, Port::HIGH); } /* virtual */ ~Spi(void) { end(); } bool begin(uint32_t frq, MODE mode = MODE_3, uint16_t delay = 0) { if (frq) { static const uint8_t CLKDIV[] = { 2, 4, 8, 16, 32, 64, 128 }; static const PRESC PRESCS[] = { PRESC_DIV2, PRESC_DIV4, PRESC_DIV8, PRESC_DIV16, PRESC_DIV32, PRESC_DIV64, PRESC_DIV128 }; uint32_t clk = Clock::frequency(); uint32_t div = clk / frq; uint32_t dly = ((clk / 1000) * delay + 3999) / 4000; delay = dly <= 0xFFFF ? dly : 0xFFFF; for (uint8_t i = 0; i < sizeof(CLKDIV) / sizeof(CLKDIV[0]); ++i) { if (div <= CLKDIV[i]) { begin(PRESCS[i], mode, delay); return true; } } begin(PRESCS[sizeof(PRESCS) / sizeof(PRESCS[0]) - 1], mode, delay); } return false; } void begin(PRESC presc, MODE mode, uint16_t delay = 0) { end(); _delay = delay; _spi->INTCTRL = 0; _spi->CTRLB = SPI_BUFEN_bm | SPI_SSD_bm | mode; _spi->CTRLA = SPI_MASTER_bm | presc | SPI_ENABLE_bm; } void end(void) { _spi->CTRLA = 0; } void lsbfirst(bool enable = true) { _spi->CTRLA = (_spi->CTRLA & ~SPI_DORD_bm) | (enable ? SPI_DORD_bm : 0); } void slave(Port::PIN cs = Port::PNC, bool ah = false) { if (_ss != cs) { _cs = cs; _ah = ah; if (cs != Port::PNC) Port::pinMode(cs, Port::OUTPUT, ah ? Port::LOW : Port::HIGH); } } void select(void) { Port::digitalWrite(_ss, Port::LOW); if (_cs != Port::PNC) Port::digitalWrite(_cs, _ah ? Port::HIGH : Port::LOW); delay(); } void deselect(void) { delay(); if (_cs != Port::PNC) Port::digitalWrite(_cs, _ah ? Port::LOW : Port::HIGH); Port::digitalWrite(_ss, Port::HIGH); } uint8_t xfer(uint8_t data) { _spi->DATA = data; while ((_spi->INTFLAGS & SPI_RXCIF_bm) == 0) continue; return _spi->DATA; } uint8_t read(void) { return xfer(0xFF); } void write(uint8_t data) { xfer(data); } void xfer(const void *wbuf, void *rbuf, size_t len) { if (len) { size_t wcnt = len; const uint8_t *wptr = (const uint8_t*)wbuf; uint8_t *rptr = (uint8_t*)rbuf; while (1) { uint8_t data, flags = _spi->INTFLAGS; if (flags & SPI_RXCIF_bm) { data = _spi->DATA; if (rptr) *rptr++ = data; if (--len == 0) break; } if (wcnt && (flags & SPI_DREIF_bm)) { data = wptr ? *wptr++ : 0xFF; --wcnt; _spi->DATA = data; } } } } void read(void *buf, size_t len) { xfer(0, buf, len); } void write(const void *buf, size_t len) { xfer(buf, 0, len); } protected: SPI_t* _spi; Port::PIN _ss; Port::PIN _cs; bool _ah; uint16_t _delay; void delay(void) { if (_delay) _delay_loop_2(_delay); } }; |
【関連投稿】
Microchip AVR8 用のライブラリを自作する。(GPIO)
Microchip AVR8 用のライブラリを自作する。(FUSE)
Microchip AVR8 用のライブラリを自作する。(CLOCK)
Microchip AVR8 用のライブラリを自作する。(RESET)
Microchip AVR8 用のライブラリを自作する。(PORTMUX)
Microchip AVR8 用のライブラリを自作する。(USART)
Microchip AVR8 用のライブラリを自作する。(RTC)
Microchip AVR8 用のライブラリを自作する。(TCA)
Microchip AVR8 用のライブラリを自作する。(TCB)
Microchip AVR8 用のライブラリを自作する。(VREF)
Microchip AVR8 用のライブラリを自作する。(DAC)
Microchip AVR8 用のライブラリを自作する。(AC)
Microchip AVR8 用のライブラリを自作する。(ADC)
Microchip AVR8 用のライブラリを自作する。(ZCD)
Microchip AVR8 用のライブラリを自作する。(SPI)
Microchip AVR8 用のライブラリを自作する。(TWI)
Microchip AVR8 用のライブラリを自作する。(MAIN)
Microchip AVR8 用のライブラリを自作する。(CONFIG)
Microchip AVR8 用のライブラリを自作する。(ALARM)
Microchip AVR8 用のライブラリを自作する。(TASK)
Microchip AVR8 用のライブラリを自作する。(DOWNLOAD)
