|
30 | 30 |
|
31 | 31 | class SPISettings {
|
32 | 32 | public:
|
33 |
| - SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) { |
34 |
| - if (__builtin_constant_p(clock)) { |
35 |
| - init_AlwaysInline(clock, bitOrder, dataMode); |
36 |
| - } else { |
37 |
| - init_MightInline(clock, bitOrder, dataMode); |
38 |
| - } |
39 |
| - } |
40 |
| - SPISettings() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0); } |
41 |
| - private: |
42 |
| - void init_MightInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) { |
43 |
| - init_AlwaysInline(clock, bitOrder, dataMode); |
44 |
| - } |
45 |
| - void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) __attribute__((__always_inline__)) { |
46 |
| - uint8_t div; |
47 |
| - if (clock < (F_CPU / 255)) { |
48 |
| - div = 255; |
49 |
| - } else if (clock >= (F_CPU / SPI_MIN_CLOCK_DIVIDER)) { |
50 |
| - div = SPI_MIN_CLOCK_DIVIDER; |
51 |
| - } else { |
52 |
| - div = (F_CPU / (clock + 1)) + 1; |
53 |
| - } |
54 |
| - this->clockDiv = div; |
55 |
| - this->dataMode = dataMode; |
56 |
| - this->bitOrder = bitOrder; |
57 |
| - } |
58 |
| - uint8_t clockDiv, dataMode; |
59 |
| - BitOrder bitOrder; |
60 |
| - friend class SPIClass; |
| 33 | + SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) { |
| 34 | + if (__builtin_constant_p(clock)) { |
| 35 | + init_AlwaysInline(clock, bitOrder, dataMode); |
| 36 | + } else { |
| 37 | + init_MightInline(clock, bitOrder, dataMode); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + SPISettings() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0); } |
61 | 42 |
|
| 43 | + private: |
| 44 | + void init_MightInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) { |
| 45 | + init_AlwaysInline(clock, bitOrder, dataMode); |
| 46 | + } |
| 47 | + |
| 48 | + void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) __attribute__((__always_inline__)) { |
| 49 | + uint8_t div; |
| 50 | + if (clock < (F_CPU / 255)) { |
| 51 | + div = 255; |
| 52 | + } else if (clock >= (F_CPU / SPI_MIN_CLOCK_DIVIDER)) { |
| 53 | + div = SPI_MIN_CLOCK_DIVIDER; |
| 54 | + } else { |
| 55 | + div = (F_CPU / (clock + 1)) + 1; |
| 56 | + } |
| 57 | + this->clockDiv = div; |
| 58 | + this->dataMode = dataMode; |
| 59 | + this->bitOrder = bitOrder; |
| 60 | + } |
| 61 | + |
| 62 | + uint8_t clockDiv, dataMode; |
| 63 | + BitOrder bitOrder; |
| 64 | + |
| 65 | + friend class SPIClass; |
62 | 66 | };
|
63 | 67 |
|
64 | 68 | class SPIClass {
|
65 | 69 | public:
|
66 |
| - SPIClass(SERCOM *p_sercom, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI); |
| 70 | + SPIClass(SERCOM *p_sercom, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI); |
| 71 | + |
| 72 | + byte transfer(uint8_t data); |
| 73 | + inline void transfer(void *buf, size_t count); |
67 | 74 |
|
68 |
| - byte transfer(uint8_t data); |
69 |
| - inline void transfer(void *buf, size_t count); |
| 75 | + // Transaction Functions |
| 76 | + void usingInterrupt(uint8_t interruptNumber); |
| 77 | + void beginTransaction(SPISettings settings); |
| 78 | + void endTransaction(void); |
70 | 79 |
|
71 |
| - // Transaction Functions |
72 |
| - void usingInterrupt(uint8_t interruptNumber); |
73 |
| - void beginTransaction(SPISettings settings); |
74 |
| - void endTransaction(void); |
| 80 | + // SPI Configuration methods |
| 81 | + void attachInterrupt(); |
| 82 | + void detachInterrupt(); |
75 | 83 |
|
76 |
| - // SPI Configuration methods |
77 |
| - void attachInterrupt(); |
78 |
| - void detachInterrupt(); |
| 84 | + void begin(); |
| 85 | + void end(); |
79 | 86 |
|
80 |
| - void begin(); |
81 |
| - void end(); |
82 |
| - |
83 |
| - void setBitOrder(BitOrder order); |
84 |
| - void setDataMode(uint8_t uc_mode); |
85 |
| - void setClockDivider(uint8_t uc_div); |
| 87 | + void setBitOrder(BitOrder order); |
| 88 | + void setDataMode(uint8_t uc_mode); |
| 89 | + void setClockDivider(uint8_t uc_div); |
86 | 90 |
|
87 | 91 | private:
|
88 |
| - SERCOM *_p_sercom; |
89 |
| - uint8_t _uc_pinMiso; |
90 |
| - uint8_t _uc_pinMosi; |
91 |
| - uint8_t _uc_pinSCK; |
| 92 | + SERCOM *_p_sercom; |
| 93 | + uint8_t _uc_pinMiso; |
| 94 | + uint8_t _uc_pinMosi; |
| 95 | + uint8_t _uc_pinSCK; |
92 | 96 | };
|
93 | 97 |
|
94 | 98 | void SPIClass::transfer(void *buf, size_t count)
|
95 | 99 | {
|
96 |
| - // TODO: Optimize for faster block-transfer |
97 |
| - uint8_t *buffer = reinterpret_cast<uint8_t *>(buf); |
98 |
| - for (size_t i=0; i<count; i++) |
99 |
| - buffer[i] = transfer(buffer[i]); |
| 100 | + // TODO: Optimize for faster block-transfer |
| 101 | + uint8_t *buffer = reinterpret_cast<uint8_t *>(buf); |
| 102 | + for (size_t i=0; i<count; i++) |
| 103 | + buffer[i] = transfer(buffer[i]); |
100 | 104 | }
|
101 | 105 |
|
102 | 106 | #if SPI_INTERFACES_COUNT > 0
|
|
0 commit comments