-
Notifications
You must be signed in to change notification settings - Fork 76
/
SPI.cpp
82 lines (69 loc) · 2.36 KB
/
SPI.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
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
/*
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
* Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
* Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
* Copyright (c) 2014 by Andrew J. Kroll <xxxajk@gmail.com> (atomicity fixes)
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#include "SPI.h"
#if defined(__PIC32MX1XX__) || defined(__PIC32MX2XX__) || defined(__PIC32MZXX__) || defined(__PIC32MX47X__)
SPIClass SPI(_DSPI0_BASE, _DSPI0_MISO_PIN, _DSPI0_MOSI_PIN, _DSPI0_MISO_IN, _DSPI0_MOSI_OUT);
#else
SPIClass SPI(_DSPI0_BASE);
#endif
#if defined(__PIC32MX1XX__) || defined(__PIC32MX2XX__) || defined(__PIC32MZXX__) || defined(__PIC32MX47X__)
SPIClass::SPIClass(uint32_t base, int pinMI, int pinMO, ppsFunctionType ppsMI, ppsFunctionType ppsMO) {
pspi = (p32_spi *)base;
pinMISO = pinMI;
pinMOSI = pinMO;
ppsMISO = ppsMI;
ppsMOSI = ppsMO;
}
#else
SPIClass::SPIClass(uint32_t base) {
pspi = (p32_spi *)base;
}
#endif
void SPIClass::begin() {
uint32_t sreg = disableInterrupts();
if (!initialized) {
#if defined(__PIC32MX1XX__) || defined(__PIC32MX2XX__) || defined(__PIC32MZXX__) || defined(__PIC32MX47X__)
pinMode(pinMISO, INPUT);
mapPps(pinMISO, ppsMISO);
pinMode(pinMOSI, OUTPUT);
mapPps(pinMOSI, ppsMOSI);
#endif
pspi->sxCon.set = (1<<_SPICON_MSTEN) | (1<<_SPICON_ON);
setClockDivider(SPI_CLOCK_DIV128);
setDataMode(SPI_MODE0);
}
initialized++; // reference count
restoreInterrupts(sreg);
}
void SPIClass::end() {
uint32_t sreg = disableInterrupts();
// Decrease the reference counter
if (initialized) {
initialized--;
// mapPps(pinMISO, 0);
// mapPps(pinMOSI, 0);
}
// If there are no more references disable SPI
if (!initialized) {
pspi->sxCon.clr = 1<<_SPICON_ON;
interruptMode = 0;
#ifdef SPI_TRANSACTION_MISMATCH_LED
inTransactionFlag = 0;
#endif
}
restoreInterrupts(sreg);
}
void SPIClass::usingInterrupt(uint8_t interruptNumber) {
}
void SPIClass::notUsingInterrupt(uint8_t interruptNumber) {
}