Common.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Common.h - Architecture-independent definitions
8  *
9  * @author: 11 December 2018 - mikee47 <mike@sillyhouse.net>
10  *
11  */
12 
13 #pragma once
14 
15 #include <WString.h>
16 #include <Data/BitSet.h>
17 
24 namespace HSPI
25 {
29 enum class ClockMode : uint8_t {
30  mode0 = 0x00,
31  mode1 = 0x01,
32  mode2 = 0x10,
33  mode3 = 0x11,
34 };
35 
39 enum class IoMode : uint8_t {
40  SPI,
41  SPIHD,
42  DUAL,
43  DIO,
44  SDI,
45  QUAD,
46  QIO,
47  SQI,
48 };
49 
51 
52 inline constexpr IoModes operator|(IoMode a, IoMode b)
53 {
54  return IoModes(IoModes::bitVal(a) | IoModes::bitVal(b));
55 }
56 
57 /*
58  * Details for each IO Mode
59  *
60  * Mode, clock bits, address bits, databits, duplex
61  */
62 #define HSPI_IOMODE_MAP(XX) \
63  XX(SPI, 1, 1, 1, true) \
64  XX(SPIHD, 1, 1, 1, false) \
65  XX(DUAL, 1, 1, 2, false) \
66  XX(DIO, 1, 2, 2, false) \
67  XX(SDI, 2, 2, 2, false) \
68  XX(QUAD, 1, 1, 4, false) \
69  XX(QIO, 1, 4, 4, false) \
70  XX(SQI, 4, 4, 4, false)
71 
72 struct IoModeInfo {
73  const FlashString* name;
75  uint8_t clockBits : 3;
76  uint8_t addrressBits : 3;
77  uint8_t dataBits : 3;
78  bool duplex : 1;
79 };
80 
81 const IoModeInfo getIoModeInfo(IoMode mode);
82 
83 inline String toString(IoMode mode)
84 {
85  return *getIoModeInfo(mode).name;
86 }
87 
88 // 0 for LSBFIRST, non-zero for MSBFIRST
89 using ByteOrder = uint8_t;
90 using BitOrder = uint8_t;
91 
95 enum class PinSet {
96  none,
97  normal,
98  manual,
99  overlap,
100 };
101 
102 // Note: These are faster than __builtin_bswapNN functions
103 
104 inline uint16_t bswap16(uint16_t value)
105 {
106  return (value >> 8) | (value << 8);
107 }
108 
109 inline uint32_t bswap24(uint32_t value)
110 {
111  return ((value >> 16) & 0x0000ff) | (value & 0x00ff00) | ((value << 16) & 0xff0000);
112 }
113 
114 inline uint32_t bswap32(uint32_t value)
115 {
116  return (value >> 24) | ((value >> 8) & 0xff00) | ((value << 8) & 0xff0000) | (value << 24);
117 }
118 
119 } // namespace HSPI
120 
Two bits per clock for Address and Data, 1-bit for Command.
PinSet
How SPI hardware pins are connected.
Definition: Common.h:95
CPOL: 1 CPHA: 1.
One bit per clock, MISO stage follows MOSI (half-duplex)
Two bits per clock for Data, 1-bit for Command and Address.
uint8_t clockBits
Definition: Common.h:75
uint16_t bswap16(uint16_t value)
Definition: Common.h:104
static constexpr S bitVal(E e)
Get the bitmask corresponding to a given value.
Definition: BitSet.h:149
Definition: Common.h:24
The String class.
Definition: WString.h:136
uint32_t bswap32(uint32_t value)
Definition: Common.h:114
One bit per clock, MISO stage concurrent with MISO (full-duplex)
constexpr IoModes operator|(IoMode a, IoMode b)
Definition: Common.h:52
const FlashString * name
Definition: Common.h:73
uint8_t addrressBits
Definition: Common.h:76
uint8_t ByteOrder
Definition: Common.h:89
Four bits per clock for Data, 1-bit for Command and Address.
uint8_t BitOrder
Definition: Common.h:90
Standard HSPI pins.
BitSet< uint8_t, IoMode > IoModes
Definition: Common.h:50
Manage a set of bit values using enumeration.
Definition: BitSet.h:43
Definition: Common.h:72
const IoModeInfo getIoModeInfo(IoMode mode)
describes a counted string stored in flash memory
Definition: String.hpp:173
Four bits per clock for Address and Data, 1-bit for Command.
Two bits per clock for Command, Address and Data.
HSPI pins with manual chip select.
Overlapped with SPI 0.
CPOL: 0 CPHA: 0.
uint32_t bswap24(uint32_t value)
Definition: Common.h:109
IoMode
Mode of data transfer.
Definition: Common.h:39
CPOL: 1 CPHA: 0.
CPOL: 0 CPHA: 1.
String toString(IoMode mode)
Definition: Common.h:83
uint8_t dataBits
Definition: Common.h:77
bool duplex
Definition: Common.h:78
ClockMode
SPI clock polarity (CPOL) and phase (CPHA)
Definition: Common.h:29
IoMode mode
Definition: Common.h:74
Four bits per clock for Command, Address and Data.