Skip to content

Commit ee5def9

Browse files
committed
add CPHA support for SPI
- tested with logic analyzer
1 parent 163858d commit ee5def9

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

libraries/SPI/SPI.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void SPIClass::begin() {
5454
pinMode(MISO, SPECIAL); ///< GPIO12
5555
pinMode(MOSI, SPECIAL); ///< GPIO13
5656

57-
GPMUX = 0x105; // note crash if spi flash Frequency < 40MHz
57+
GPMUX = 0x105; // note crash if SPI flash Frequency < 40MHz
5858
SPI1C = 0;
5959
setFrequency(1000000); ///< 1Mhz
6060
SPI1U = SPIUMOSI | SPIUDUPLEX | SPIUSSE;
@@ -78,7 +78,27 @@ void SPIClass::endTransaction() {
7878
}
7979

8080
void SPIClass::setDataMode(uint8_t dataMode) {
81-
// todo find way to set
81+
82+
/**
83+
SPI_MODE0 0x00 - CPOL: 0 CPHA: 0
84+
SPI_MODE1 0x01 - CPOL: 0 CPHA: 1
85+
SPI_MODE2 0x10 - CPOL: 1 CPHA: 0
86+
SPI_MODE3 0x11 - CPOL: 1 CPHA: 1
87+
*/
88+
89+
bool CPOL = (dataMode&0x10); ///< CPOL (Clock Polarity)
90+
bool CPHA = (dataMode&0x01); ///< CPHA (Clock Phase)
91+
92+
if(CPHA) {
93+
SPI1U |= (SPIUSME);
94+
} else {
95+
SPI1U &= ~(SPIUSME);
96+
}
97+
98+
if(CPOL) {
99+
//todo How set CPOL???
100+
}
101+
82102
}
83103

84104
void SPIClass::setBitOrder(uint8_t bitOrder) {

libraries/SPI/SPI.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@
2424
#include <Arduino.h>
2525
#include <stdlib.h>
2626

27-
#define FCPU80 80000000L
28-
29-
30-
31-
32-
const uint8_t SPI_MODE0 = 0x00;
33-
const uint8_t SPI_MODE1 = 0x04;
34-
const uint8_t SPI_MODE2 = 0x08;
35-
const uint8_t SPI_MODE3 = 0x0C;
27+
const uint8_t SPI_MODE0 = 0x00; ///< CPOL: 0 CPHA: 0
28+
const uint8_t SPI_MODE1 = 0x01; ///< CPOL: 0 CPHA: 1
29+
const uint8_t SPI_MODE2 = 0x10; ///< CPOL: 1 CPHA: 0
30+
const uint8_t SPI_MODE3 = 0x11; ///< CPOL: 1 CPHA: 1
3631

3732
class SPISettings {
3833
public:

0 commit comments

Comments
 (0)