/*-------------------------------------------------------------------------* * tinySPI.h - Arduino hardware SPI master library for ATtiny24/44/84, * * ATtiny25/45/85, and Attiny2313/4313. * * * * Original version of tinyISP by Jack Christensen 24Oct2013 * * * * Added support for Attiny24/25, and Attiny2313/4313 * * by Leonardo Miliani 28Nov2014 * * * * CC BY-SA-NC: * * This work is licensed under the Creative Commons Attribution- * * ShareAlike- Not Commercial 4.0 Unported License. To view a copy of this * * license, visit * * http://creativecommons.org/licenses/by-sa/4.0/ or send a * * letter to Creative Commons, 171 Second Street, Suite 300, * * San Francisco, California, 94105, USA. * *-------------------------------------------------------------------------*/ #include "tinySPI.h" tinySPI::tinySPI() { } void tinySPI::begin(void) { USICR &= ~(_BV(USISIE) | _BV(USIOIE) | _BV(USIWM1)); USICR |= _BV(USIWM0) | _BV(USICS1) | _BV(USICLK); SPI_DDR_PORT |= _BV(USCK_DD_PIN); //set the USCK pin as output SPI_DDR_PORT |= _BV(DO_DD_PIN); //set the DO pin as output SPI_DDR_PORT &= ~_BV(DI_DD_PIN); //set the DI pin as input } void tinySPI::setDataMode(uint8_t spiDataMode) { if (spiDataMode == SPI_MODE1) USICR |= _BV(USICS0); else USICR &= ~_BV(USICS0); } uint8_t tinySPI::transfer(uint8_t spiData) { USIDR = spiData; USISR = _BV(USIOIF); //clear counter and counter overflow interrupt flag ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { //ensure a consistent clock period while ( !(USISR & _BV(USIOIF)) ) USICR |= _BV(USITC); } return USIDR; } void tinySPI::end(void) { USICR &= ~(_BV(USIWM1) | _BV(USIWM0)); } tinySPI SPI = tinySPI(); //instantiate a tinySPI object