Skip to content

Commit 2fdb320

Browse files
committed
Merge branch 'ide-1.5.x-library-to-new-format' into ide-1.5.x
2 parents 6e8d821 + 63d6c4c commit 2fdb320

File tree

12 files changed

+277
-1
lines changed

12 files changed

+277
-1
lines changed

hardware/arduino/avr/libraries/SPI/SPI.cpp renamed to libraries/SPI/arch/avr/SPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
#include "pins_arduino.h"
12-
#include "SPI.h"
12+
#include "SPI_Class.h"
1313

1414
SPIClass SPI;
1515

libraries/SPI/arch/sam/SPI.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) 2010 by Cristian Maglie <[email protected]>
3+
* SPI Master library for arduino.
4+
*
5+
* This file is free software; you can redistribute it and/or modify
6+
* it under the terms of either the GNU General Public License version 2
7+
* or the GNU Lesser General Public License version 2.1, both as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
#include "SPI_Class.h"
12+
13+
SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) :
14+
spi(_spi), id(_id), initCb(_initCb)
15+
{
16+
// Empty
17+
}
18+
19+
void SPIClass::begin() {
20+
initCb();
21+
SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);
22+
SPI_Enable(spi);
23+
24+
// NPCS control is left to the user
25+
26+
// Default speed set to 4Mhz
27+
setClockDivider(BOARD_SPI_DEFAULT_SS, 21);
28+
setDataMode(BOARD_SPI_DEFAULT_SS, SPI_MODE0);
29+
setBitOrder(BOARD_SPI_DEFAULT_SS, MSBFIRST);
30+
}
31+
32+
void SPIClass::begin(uint8_t _pin) {
33+
uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
34+
PIO_Configure(
35+
g_APinDescription[spiPin].pPort,
36+
g_APinDescription[spiPin].ulPinType,
37+
g_APinDescription[spiPin].ulPin,
38+
g_APinDescription[spiPin].ulPinConfiguration);
39+
40+
// Default speed set to 4Mhz
41+
setClockDivider(_pin, 21);
42+
setDataMode(_pin, SPI_MODE0);
43+
setBitOrder(_pin, MSBFIRST);
44+
}
45+
46+
void SPIClass::end(uint8_t _pin) {
47+
uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);
48+
// Setting the pin as INPUT will disconnect it from SPI peripheral
49+
pinMode(spiPin, INPUT);
50+
}
51+
52+
void SPIClass::end() {
53+
SPI_Disable(spi);
54+
}
55+
56+
void SPIClass::setBitOrder(uint8_t _pin, BitOrder _bitOrder) {
57+
uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
58+
bitOrder[ch] = _bitOrder;
59+
}
60+
61+
void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) {
62+
uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
63+
mode[ch] = _mode | SPI_CSR_CSAAT;
64+
// SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
65+
// transfer. Some device needs that for working properly.
66+
SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
67+
}
68+
69+
void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) {
70+
uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
71+
divider[ch] = _divider;
72+
// SPI_CSR_DLYBCT(1) keeps CS enabled for 32 MCLK after a completed
73+
// transfer. Some device needs that for working properly.
74+
SPI_ConfigureNPCS(spi, ch, mode[ch] | SPI_CSR_SCBR(divider[ch]) | SPI_CSR_DLYBCT(1));
75+
}
76+
77+
byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) {
78+
uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin);
79+
// Reverse bit order
80+
if (bitOrder[ch] == LSBFIRST)
81+
_data = __REV(__RBIT(_data));
82+
uint32_t d = _data | SPI_PCS(ch);
83+
if (_mode == SPI_LAST)
84+
d |= SPI_TDR_LASTXFER;
85+
86+
// SPI_Write(spi, _channel, _data);
87+
while ((spi->SPI_SR & SPI_SR_TDRE) == 0)
88+
;
89+
spi->SPI_TDR = d;
90+
91+
// return SPI_Read(spi);
92+
while ((spi->SPI_SR & SPI_SR_RDRF) == 0)
93+
;
94+
d = spi->SPI_RDR;
95+
// Reverse bit order
96+
if (bitOrder[ch] == LSBFIRST)
97+
d = __REV(__RBIT(d));
98+
return d & 0xFF;
99+
}
100+
101+
void SPIClass::attachInterrupt(void) {
102+
// Should be enableInterrupt()
103+
}
104+
105+
void SPIClass::detachInterrupt(void) {
106+
// Should be disableInterrupt()
107+
}
108+
109+
#if SPI_INTERFACES_COUNT > 0
110+
static void SPI_0_Init(void) {
111+
PIO_Configure(
112+
g_APinDescription[PIN_SPI_MOSI].pPort,
113+
g_APinDescription[PIN_SPI_MOSI].ulPinType,
114+
g_APinDescription[PIN_SPI_MOSI].ulPin,
115+
g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
116+
PIO_Configure(
117+
g_APinDescription[PIN_SPI_MISO].pPort,
118+
g_APinDescription[PIN_SPI_MISO].ulPinType,
119+
g_APinDescription[PIN_SPI_MISO].ulPin,
120+
g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
121+
PIO_Configure(
122+
g_APinDescription[PIN_SPI_SCK].pPort,
123+
g_APinDescription[PIN_SPI_SCK].ulPinType,
124+
g_APinDescription[PIN_SPI_SCK].ulPin,
125+
g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
126+
}
127+
128+
SPIClass SPI(SPI_INTERFACE, SPI_INTERFACE_ID, SPI_0_Init);
129+
#endif

libraries/SPI/arch/sam/SPI_Class.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2010 by Cristian Maglie <[email protected]>
3+
* SPI Master library for arduino.
4+
*
5+
* This file is free software; you can redistribute it and/or modify
6+
* it under the terms of either the GNU General Public License version 2
7+
* or the GNU Lesser General Public License version 2.1, both as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
#ifndef _SPI_H_INCLUDED
12+
#define _SPI_H_INCLUDED
13+
14+
#include "variant.h"
15+
#include <stdio.h>
16+
17+
#define SPI_MODE0 0x02
18+
#define SPI_MODE1 0x00
19+
#define SPI_MODE2 0x03
20+
#define SPI_MODE3 0x01
21+
22+
enum SPITransferMode {
23+
SPI_CONTINUE,
24+
SPI_LAST
25+
};
26+
27+
class SPIClass {
28+
public:
29+
SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void));
30+
31+
byte transfer(uint8_t _data, SPITransferMode _mode = SPI_LAST) { return transfer(BOARD_SPI_DEFAULT_SS, _data, _mode); }
32+
byte transfer(byte _channel, uint8_t _data, SPITransferMode _mode = SPI_LAST);
33+
34+
// SPI Configuration methods
35+
36+
void attachInterrupt(void);
37+
void detachInterrupt(void);
38+
39+
void begin(void);
40+
void end(void);
41+
42+
// Attach/Detach pin to/from SPI controller
43+
void begin(uint8_t _pin);
44+
void end(uint8_t _pin);
45+
46+
// These methods sets a parameter on a single pin
47+
void setBitOrder(uint8_t _pin, BitOrder);
48+
void setDataMode(uint8_t _pin, uint8_t);
49+
void setClockDivider(uint8_t _pin, uint8_t);
50+
51+
// These methods sets the same parameters but on default pin BOARD_SPI_DEFAULT_SS
52+
void setBitOrder(BitOrder _order) { setBitOrder(BOARD_SPI_DEFAULT_SS, _order); };
53+
void setDataMode(uint8_t _mode) { setDataMode(BOARD_SPI_DEFAULT_SS, _mode); };
54+
void setClockDivider(uint8_t _div) { setClockDivider(BOARD_SPI_DEFAULT_SS, _div); };
55+
56+
private:
57+
Spi *spi;
58+
uint32_t id;
59+
BitOrder bitOrder[SPI_CHANNELS_NUM];
60+
uint32_t divider[SPI_CHANNELS_NUM];
61+
uint32_t mode[SPI_CHANNELS_NUM];
62+
void (*initCb)(void);
63+
};
64+
65+
#if SPI_INTERFACES_COUNT > 0
66+
extern SPIClass SPI;
67+
#endif
68+
69+
#endif

libraries/SPI/arch/sam/keywords.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#######################################
2+
# Syntax Coloring Map SPI
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
SPI KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
begin KEYWORD2
15+
end KEYWORD2
16+
transfer KEYWORD2
17+
#setBitOrder KEYWORD2
18+
setDataMode KEYWORD2
19+
setClockDivider KEYWORD2
20+
21+
22+
#######################################
23+
# Constants (LITERAL1)
24+
#######################################
25+
SPI_MODE0 LITERAL1
26+
SPI_MODE1 LITERAL1
27+
SPI_MODE2 LITERAL1
28+
SPI_MODE3 LITERAL1
29+
30+
SPI_CONTINUE LITERAL1
31+
SPI_LAST LITERAL1

libraries/SPI/keywords.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#######################################
2+
# Syntax Coloring Map SPI
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
SPI KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
begin KEYWORD2
15+
end KEYWORD2
16+
transfer KEYWORD2
17+
setBitOrder KEYWORD2
18+
setDataMode KEYWORD2
19+
setClockDivider KEYWORD2
20+
21+
22+
#######################################
23+
# Constants (LITERAL1)
24+
#######################################
25+
SPI_CLOCK_DIV4 LITERAL1
26+
SPI_CLOCK_DIV16 LITERAL1
27+
SPI_CLOCK_DIV64 LITERAL1
28+
SPI_CLOCK_DIV128 LITERAL1
29+
SPI_CLOCK_DIV2 LITERAL1
30+
SPI_CLOCK_DIV8 LITERAL1
31+
SPI_CLOCK_DIV32 LITERAL1
32+
SPI_CLOCK_DIV64 LITERAL1
33+
SPI_MODE0 LITERAL1
34+
SPI_MODE1 LITERAL1
35+
SPI_MODE2 LITERAL1
36+
SPI_MODE3 LITERAL1

0 commit comments

Comments
 (0)