From 48f5256789369bd5a4d8d9b9a663982aea717c7d Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Fri, 1 Aug 2014 05:38:27 -0700 Subject: SPI Transactions for AVR --- libraries/SPI/SPI.cpp | 102 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 18 deletions(-) (limited to 'libraries/SPI/SPI.cpp') diff --git a/libraries/SPI/SPI.cpp b/libraries/SPI/SPI.cpp index 5e48073..9a7a400 100644 --- a/libraries/SPI/SPI.cpp +++ b/libraries/SPI/SPI.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2010 by Cristian Maglie + * Copyright (c) 2014 by Paul Stoffregen (Transaction API) * SPI Master library for arduino. * * This file is free software; you can redistribute it and/or modify @@ -8,13 +9,20 @@ * published by the Free Software Foundation. */ -#include "pins_arduino.h" #include "SPI.h" +#include "pins_arduino.h" SPIClass SPI; -void SPIClass::begin() { +uint8_t SPIClass::interruptMode = 0; +uint8_t SPIClass::interruptMask = 0; +uint8_t SPIClass::interruptSave = 0; +#ifdef SPI_TRANSACTION_MISMATCH_LED +uint8_t SPIClass::inTransactionFlag = 0; +#endif +void SPIClass::begin() +{ // Set SS to high so a connected chip will be "deselected" by default digitalWrite(SS, HIGH); @@ -39,28 +47,86 @@ void SPIClass::begin() { pinMode(MOSI, OUTPUT); } - void SPIClass::end() { SPCR &= ~_BV(SPE); } -void SPIClass::setBitOrder(uint8_t bitOrder) -{ - if(bitOrder == LSBFIRST) { - SPCR |= _BV(DORD); - } else { - SPCR &= ~(_BV(DORD)); - } -} +// mapping of interrupt numbers to bits within SPI_AVR_EIMSK +#if defined(__AVR_ATmega32U4__) + #define SPI_INT0_MASK (1<> 2) & SPI_2XCLOCK_MASK); + if (interruptMode > 1) return; + + noInterrupts(); + switch (interruptNumber) { + #ifdef SPI_INT0_MASK + case 0: mask = SPI_INT0_MASK; break; + #endif + #ifdef SPI_INT1_MASK + case 1: mask = SPI_INT1_MASK; break; + #endif + #ifdef SPI_INT2_MASK + case 2: mask = SPI_INT2_MASK; break; + #endif + #ifdef SPI_INT3_MASK + case 3: mask = SPI_INT3_MASK; break; + #endif + #ifdef SPI_INT4_MASK + case 4: mask = SPI_INT4_MASK; break; + #endif + #ifdef SPI_INT5_MASK + case 5: mask = SPI_INT5_MASK; break; + #endif + #ifdef SPI_INT6_MASK + case 6: mask = SPI_INT6_MASK; break; + #endif + #ifdef SPI_INT7_MASK + case 7: mask = SPI_INT7_MASK; break; + #endif + default: + interruptMode = 2; + interrupts(); + return; + } + interruptMode = 1; + interruptMask |= mask; + interrupts(); } -- cgit v1.2.3-18-g5258