|
18 | 18 | #define SPI_MODE2 0x03
|
19 | 19 | #define SPI_MODE3 0x01
|
20 | 20 |
|
| 21 | +#if defined(__SAMD21G18A__) |
| 22 | + // Even if not specified on the datasheet, the SAMD21G18A MCU |
| 23 | + // doesn't operate correctly with clock dividers lower than 4. |
| 24 | + // This allows a theoretical maximum SPI clock speed of 12Mhz |
| 25 | + #define SPI_MIN_CLOCK_DIVIDER 4 |
| 26 | + // Other SAMD21xxxxx MCU may be affected as well |
| 27 | +#else |
| 28 | + #define SPI_MIN_CLOCK_DIVIDER 2 |
| 29 | +#endif |
| 30 | + |
21 | 31 | class SPISettings {
|
22 | 32 | public:
|
23 |
| - SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) { |
24 |
| - if (__builtin_constant_p(clock)) { |
25 |
| - init_AlwaysInline(clock, bitOrder, dataMode); |
26 |
| - } else { |
27 |
| - init_MightInline(clock, bitOrder, dataMode); |
28 |
| - } |
29 |
| - } |
30 |
| - SPISettings() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0); } |
| 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); } |
| 42 | + |
31 | 43 | private:
|
32 |
| - void init_MightInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) { |
33 |
| - init_AlwaysInline(clock, bitOrder, dataMode); |
34 |
| - } |
35 |
| - void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) __attribute__((__always_inline__)) { |
36 |
| - uint8_t div; |
37 |
| - if (clock < (F_CPU / 255)) { |
38 |
| - div = 255; |
39 |
| - } else if (clock >= (F_CPU / 2)) { |
40 |
| - div = 2; |
41 |
| - } else { |
42 |
| - div = (F_CPU / (clock + 1)) + 1; |
43 |
| - } |
44 |
| - this->clockDiv = div; |
45 |
| - this->dataMode = dataMode; |
46 |
| - this->bitOrder = bitOrder; |
47 |
| - } |
48 |
| - uint8_t clockDiv, dataMode; |
49 |
| - BitOrder bitOrder; |
50 |
| - friend class SPIClass; |
| 44 | + void init_MightInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) { |
| 45 | + init_AlwaysInline(clock, bitOrder, dataMode); |
| 46 | + } |
51 | 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; |
52 | 66 | };
|
53 | 67 |
|
54 | 68 | class SPIClass {
|
55 | 69 | public:
|
56 |
| - 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); |
57 | 74 |
|
58 |
| - byte transfer(uint8_t data); |
59 |
| - 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); |
60 | 79 |
|
61 |
| - // Transaction Functions |
62 |
| - void usingInterrupt(uint8_t interruptNumber); |
63 |
| - void beginTransaction(SPISettings settings); |
64 |
| - void endTransaction(void); |
| 80 | + // SPI Configuration methods |
| 81 | + void attachInterrupt(); |
| 82 | + void detachInterrupt(); |
65 | 83 |
|
66 |
| - // SPI Configuration methods |
67 |
| - void attachInterrupt(); |
68 |
| - void detachInterrupt(); |
| 84 | + void begin(); |
| 85 | + void end(); |
69 | 86 |
|
70 |
| - void begin(); |
71 |
| - void end(); |
72 |
| - |
73 |
| - void setBitOrder(BitOrder order); |
74 |
| - void setDataMode(uint8_t uc_mode); |
75 |
| - 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); |
76 | 90 |
|
77 | 91 | private:
|
78 |
| - SERCOM *_p_sercom; |
79 |
| - uint8_t _uc_pinMiso; |
80 |
| - uint8_t _uc_pinMosi; |
81 |
| - uint8_t _uc_pinSCK; |
| 92 | + SERCOM *_p_sercom; |
| 93 | + uint8_t _uc_pinMiso; |
| 94 | + uint8_t _uc_pinMosi; |
| 95 | + uint8_t _uc_pinSCK; |
82 | 96 | };
|
83 | 97 |
|
84 | 98 | void SPIClass::transfer(void *buf, size_t count)
|
85 | 99 | {
|
86 |
| - // TODO: Optimize for faster block-transfer |
87 |
| - uint8_t *buffer = reinterpret_cast<uint8_t *>(buf); |
88 |
| - for (size_t i=0; i<count; i++) |
89 |
| - 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]); |
90 | 104 | }
|
91 | 105 |
|
92 | 106 | #if SPI_INTERFACES_COUNT > 0
|
|
0 commit comments