uTL
micro Template library
_1wire_uart.h
Go to the documentation of this file.
1 
29 #ifndef __utl_com_1wire_uart_h__
30 #define __utl_com_1wire_uart_h__
31 
32 #include <utl/core/impl.h>
33 #include <utl/core/crtp.h>
34 #include <utl/com/_1wire.h>
35 
36 namespace utl {
43 
51  template <typename Impl_t>
52  class _1wire_uart_i : public _1wire_i<_1wire_uart_i<Impl_t>> {
53  _CRTP_IMPL(Impl_t);
55  public:
57  using Speed = typename _1wire_i<type>::Speed;
58 
62  protected:
64  _1wire_uart_i () = default;
65  ~_1wire_uart_i () = default;
66 
74  private:
89  byte_t UART_RW (byte_t byte) { return impl().UART_RW (byte); }
90 
96  void UART_BR (uint32_t br) { impl().UART_BR (br); }
98 
101  private:
103  enum BR {
104  BR_STD_RST =9600,
105  BR_OVR_RST =57600,
106  BR_STD =115200,
107  BR_OVR =921600
108  };
109  Speed _speed {Speed::STD};
110 
111  Speed speed () const { return _speed; }
112  void speed (Speed s);
113 
136  bool bit (bool b) {
137  if (b)
138  return (UART_RW (0xFF) < 0xFF) ? 0 : 1;
139  else {
140  UART_RW (0x00);
141  return 0;
142  }
143  }
144  bool _reset (Speed s);
146  };
147 
155  template <typename Impl_t>
157  switch (_speed = s) {
158  case Speed::STD: UART_BR (BR_STD); break;
159  case Speed::OVDR: UART_BR (BR_OVR); break;
160  }
161  }
162 
180  template <typename Impl_t>
182  // Select frame to send
183  uint8_t w = ((_speed = s) == Speed::STD) ? 0xF0 : 0xF8;
184  // Select baudrate
185  impl().UART_BR ((_speed == Speed::STD) ? BR_STD_RST : BR_OVR_RST);
186  // Send frame and check the result
187  return (impl().UART_RW (w) < w);
188  }
189 
197  template <>
198  class _1wire_uart_i<virtual_tag> : public _1wire_i<virtual_tag> {
199  public:
202 
206  protected:
208  _1wire_uart_i () = default;
209  ~_1wire_uart_i () = default;
210 
215  private:
230  virtual byte_t UART_RW (byte_t byte) =0;
231 
237  virtual void UART_BR (uint32_t br) =0;
239 
242  private:
244  enum BR {
245  BR_STD_RST =9600,
246  BR_OVR_RST =57600,
247  BR_STD =115200,
248  BR_OVR =921600
249  };
250  Speed _speed {Speed::STD};
251 
252  Speed speed () const { return _speed; }
253  void speed (Speed s);
254 
277  bool bit (bool b) {
278  if (b)
279  return (UART_RW (0xFF) < 0xFF) ? 0 : 1;
280  else {
281  UART_RW (0x00);
282  return 0;
283  }
284  }
285  bool _reset (Speed s);
287  };
288 
297  switch (_speed = s) {
298  case Speed::STD: UART_BR (BR_STD); break;
299  case Speed::OVDR: UART_BR (BR_OVR); break;
300  }
301  }
302 
321  // Select frame to send
322  uint8_t w = ((_speed = s) == Speed::STD) ? 0xF0 : 0xF8;
323  // Select baudrate
324  UART_BR ((_speed == Speed::STD) ? BR_STD_RST : BR_OVR_RST);
325  // Send frame and check the result
326  return (UART_RW (w) < w);
327  }
328 
330 
331 } // namespace utl
332 
333 #endif /* __utl_com_1wire_uart_h__ */
bool bit(bool b)
Send a 1-Wire write bit and read the response.
Definition: _1wire_uart.h:277
typename _1wire_i< type >::Speed Speed
Bring bus speed.
Definition: _1wire_uart.h:57
void UART_BR(uint32_t br)
Implementers&#39;s (driver) baudrate function.
Definition: _1wire_uart.h:96
Speed
1-wire bus speed
Definition: _1wire.h:64
Speed speed() const
Get the 1-wire bus speed.
Definition: _1wire_uart.h:252
A virtual base class interface implementation. Using the private virtual interface we provide the int...
Definition: _1wire_uart.h:198
An 1-wire interface implementation.
A virtual base class implementation.
Definition: _1wire.h:260
bool bit(bool b)
Send a 1-Wire write bit and read the response.
Definition: _1wire_uart.h:136
STL&#39;s core language concepts.
Definition: _1wire.h:30
~_1wire_uart_i()=default
BR
1-wire UART baudrates
Definition: _1wire_uart.h:103
BR
1-wire UART baudrates
Definition: _1wire_uart.h:244
bool _reset(Speed s)
Generate a 1-wire reset Reset \ / \ X X X / RS: | | | | | | | | | | | bit: ST 0 1 2 3 4 5 6 7 SP < ...
Definition: _1wire_uart.h:181
byte_t UART_RW(byte_t byte)
Implementers&#39;s (driver) read-write function. We expect the following USART configuration: ...
Definition: _1wire_uart.h:89
Template base class for 1-wire communication interface using CRTP.
Definition: _1wire.h:57
uint8_t byte_t
8 bits wide
Definition: types.h:31
virtual support tag type
Definition: crtp.h:40
_1wire_uart_i()=default
Allow constructor from derived only.
Speed speed() const
Get the 1-wire bus speed.
Definition: _1wire_uart.h:111
1-wire UART interface template class using CRTP Using the private virtual interface we provide the in...
Definition: _1wire_uart.h:52
Implementation detail main forward header.
Speed
1-wire bus speed
Definition: _1wire.h:265