forked from JChristensen/tinySPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinySPI.cpp
53 lines (46 loc) · 1.93 KB
/
tinySPI.cpp
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
/*----------------------------------------------------------------------*
* tinySPI.h - Arduino hardware SPI master library for ATtiny44/84, *
* and ATtiny45/85. *
* *
* Jack Christensen 24Oct2013 *
* *
* CC BY-SA: *
* This work is licensed under the Creative Commons Attribution- *
* ShareAlike 3.0 Unported License. To view a copy of this license, *
* visit http://creativecommons.org/licenses/by-sa/3.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