マスターモード専用の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 |
#include "avr8_spi.h" #include "avr8_port.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 */ Spi spi0(SPI0); int main(void) { sei(); Clock::setup(); /* Setup SPI pin's */ Port::pinMode(SPI_MOSI, OUTPUT, Port::HIGH); Port::pinMode(SPI_SCK , OUTPUT, Port::HIGH); Port::pinMode(SPI_SS , OUTPUT, Port::HIGH); /* SPI Mode 3, 5MHz */ spi0.begin(5000000, Spi::MODE_3); uint8_t wdata[8] = { ... }; uint8_t rdata[8]; /* write and read (exchange) */ Port::digitalWrite(SPI_SS, Port::LOW); spi0.xfer(wdata, rdata, sizeof(wdata)); Port::digitalWrite(SPI_SS, Port::HIGH); /* write */ Port::digitalWrite(SPI_SS, Port::LOW); spi0.write(wdata, sizeof(data); Port::digitalWrite(SPI_SS, Port::HIGH); /* read */ Port::digitalWrite(SPI_SS, Port::LOW); spi0.read(rdata, sizeof(data); Port::digitalWrite(SPI_SS, Port::HIGH); 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 |
/* 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 "avr8_config.h" #include "avr8_clock.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) : _spi(&spi), _ch((&spi - &SPI0) / sizeof(spi)) { _instances[_ch] = this; } /* virtual */ ~Spi(void) { end(); _instances[_ch] = 0; } bool begin(uint32_t frq, MODE mode = MODE_3) { 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 div = Clock::frequency() / frq; for (uint8_t i = 0; i < sizeof(CLKDIV) / sizeof(CLKDIV[0]); ++i) { if (div <= CLKDIV[i]) { begin(PRESCS[i], mode); return true; } } } return false; } void begin(PRESC presc, MODE mode) { end(); _spi->INTCTRL = 0; _spi->CTRLB = 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 xfer(const void *wbuf, void *rbuf, size_t len) { const uint8_t* wp = (uint8_t*)wbuf; uint8_t* rp = (uint8_t*)rbuf; while (len) { _spi->DATA = wp ? *wp++ : 0xFF; while ((_spi->INTFLAGS & SPI_IF_bm) == 0) continue; uint8_t d = _spi->DATA; if (rp) *rp++ = d; } } void read(void *buf, size_t len) { xfer(0, buf, len); } void write(const void *buf, size_t len) { xfer(buf, 0, len); } protected: static Spi* _instances[SPI_MAX_CH]; SPI_t* _spi; uint8_t _ch; }; |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/* avr8_spi.cpp - 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/>. */ #include "avr8_spi.h" Spi* Spi::_instances[]; |
