nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
sx1509b.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright 2018-2019 Peter A. Bigot */
3 
8 #ifndef NRFCXX_MISC_SX1509B_HPP
9 #define NRFCXX_MISC_SX1509B_HPP
10 #pragma once
11 
12 #include <nrfcxx/periph.hpp>
13 #include <nrfcxx/gpio.hpp>
14 
15 namespace nrfcxx {
16 namespace misc {
17 
25 class sx1509b
26 {
27 public:
30  {
33 
35  int8_t resetn_psel = -1;
36 
42  uint8_t address;
43 
45  iface_config_type (const iface_config_type&) = delete;
46  iface_config_type& operator= (const iface_config_type&) = delete;
48  iface_config_type& operator= (iface_config_type&&) = delete;
50  };
51 
54  {
55  return iface_config_;
56  }
57 
67  {
68  uint16_t input_disable = 0U; // 0x00
69  uint16_t long_slew = 0U; // 0x02
70  uint16_t low_drive = 0U; // 0x04
71  uint16_t pull_up = 0U; // 0x06
72  uint16_t pull_down = 0U; // 0x08
73  uint16_t open_drain = 0U; // 0x0A
74  uint16_t polarity = 0U; // 0x0C
75  uint16_t dir = -1; // 0x0E
76  uint16_t data = -1; // 0x10
77  // Subsequent registers deal with input, which is not supported on
78  // the Thingy:52 nor currently via this driver.
79  };
80 
82  const gpio_regs_type& gpio_cache () const noexcept
83  {
84  return gpio_cache_;
85  }
86 
88  uint16_t led_driver_cache () const
89  {
90  return led_driver_cache_;
91  }
92 
94  uint8_t clock_cache () const
95  {
96  return clock_cache_;
97  }
98 
100  uint8_t misc_cache () const
101  {
102  return misc_cache_;
103  }
104 
116  int reload_cache () noexcept;
117 
127  unsigned int configuration (unsigned int psel) const noexcept;
128 
155  int configure (unsigned int psel,
156  unsigned int pin_cnf,
157  int initial = -1) noexcept;
158 
184  int multiconfigure (uint16_t out_set,
185  uint16_t out_clear,
186  uint16_t pull_down,
187  uint16_t pull_up = 0) noexcept;
188 
190  struct led_type
191  {
196  uint8_t ton = 0;
197 
199  uint8_t ion = -1;
200 
202  uint8_t off = 0;
203  };
204 
208  struct led_pwm_type : public led_type
209  {
211  uint8_t trise = 0;
212 
214  uint8_t tfall = 0;
215  };
216 
217  static constexpr auto RegClock_FREQ_Pos = 5U;
218  static constexpr uint16_t RegClock_FREQ_Msk = 0x03 << RegClock_FREQ_Pos;
219  static constexpr uint16_t RegClock_FREQ_Off = 0;
220  static constexpr uint16_t RegClock_FREQ_External = 1;
221  static constexpr uint16_t RegClock_FREQ_2MHz = 2;
222 
223  static constexpr auto RegClock_OSCIO_Pos = 4U;
224  static constexpr uint16_t RegClock_OSCIO_Msk = 0x01 << RegClock_OSCIO_Pos;
225  static constexpr uint16_t RegClock_OSCIO_Input = 0;
226  static constexpr uint16_t RegClock_OSCIO_Output = 1;
227 
228  static constexpr auto RegClock_OUTFREQ_Pos = 0U;
229  static constexpr uint16_t RegClock_OUTFREQ_Msk = 0x0F << RegClock_OUTFREQ_Pos;
230  static constexpr uint16_t RegClock_OUTFREQ_Low = 0;
231  static constexpr uint16_t RegClock_OUTFREQ_High = 0x0F;
232 
233  static constexpr auto RegMisc_LEDB_Pos = 7U;
234  static constexpr uint16_t RegMisc_LEDB_Msk = 1U << RegMisc_LEDB_Pos;
235  static constexpr uint16_t RegMisc_LEDB_Linear = 0;
236  static constexpr uint16_t RegMisc_LEDB_Logarithmic = 1;
237 
238  static constexpr auto RegMisc_LEDFREQ_Pos = 4U;
239  static constexpr uint16_t RegMisc_LEDFREQ_Msk = 0x07U << RegMisc_LEDFREQ_Pos;
240  static constexpr uint16_t RegMisc_LEDFREQ_Off = 0;
241 
242  static constexpr auto RegMisc_LEDA_Pos = 3U;
243  static constexpr uint16_t RegMisc_LEDA_Msk = 1U << RegMisc_LEDA_Pos;
244  static constexpr uint16_t RegMisc_LEDA_Linear = 0;
245  static constexpr uint16_t RegMisc_LEDA_Logarithmic = 1;
246 
247  static constexpr auto RegMisc_NRESET_Pos = 2U;
248  static constexpr uint16_t RegMisc_NRESET_Msk = 1U << RegMisc_NRESET_Pos;
249  static constexpr uint16_t RegMisc_NRESET_POR = 0;
250  static constexpr uint16_t RegMisc_NRESET_ResetCounters = 1;
251 
252  static constexpr auto RegMisc_AUTOINC_Pos = 1U;
253  static constexpr uint16_t RegMisc_AUTOINC_Msk = 1U << RegMisc_AUTOINC_Pos;
254  static constexpr uint16_t RegMisc_AUTOINC_On = 0;
255  static constexpr uint16_t RegMisc_AUTOINC_Off = 1;
256 
257  static constexpr auto RegMisc_AUTOCLR_Pos = 0U;
258  static constexpr uint16_t RegMisc_AUTOCLR_Msk = 1U << RegMisc_AUTOCLR_Pos;
259  static constexpr uint16_t RegMisc_AUTOCLR_On = 0;
260  static constexpr uint16_t RegMisc_AUTOCLR_Off = 1;
261 
262  static constexpr auto RegTOnX_TOn_Pos = 0U;
263  static constexpr uint16_t RegTOnX_TOn_Msk = 0x1F << RegTOnX_TOn_Pos;
264  static constexpr auto RegOffX_TOff_Pos = 3U;
265  static constexpr uint16_t RegOffX_TOff_Msk = 0x1F << RegOffX_TOff_Pos;
266  static constexpr auto RegOffX_IOff_Pos = 0U;
267  static constexpr uint16_t RegOffX_IOff_Msk = 0x07 << RegOffX_IOff_Pos;
268 
277  unsigned int addr = 0) noexcept;
278 
286  int hw_reset () noexcept;
287 
292  int sw_reset () noexcept;
293 
298  int clock () const noexcept;
299 
304  int clock (uint8_t value) noexcept;
305 
310  int misc () const noexcept;
311 
316  int misc (uint8_t value) noexcept;
317 
328  int configure_as_leds (uint16_t leds,
329  bool source_current = false) noexcept;
330 
340  int led_configuration (unsigned int psel,
341  led_type& cfg) const noexcept;
342 
353  int led_configuration (unsigned int psel,
354  led_pwm_type& cfg) const noexcept;
355 
368  int led_configure (unsigned int psel,
369  const led_type& cfg) const noexcept;
370 
381  int led_configure (unsigned int psel,
382  const led_pwm_type& cfg) const noexcept;
383 
397  int output_sct (uint16_t set,
398  uint16_t clear) noexcept;
399 
400 private:
401  void reset_cache_ () noexcept;
402 
403  iface_config_type& iface_config_;
404  gpio::pin_reference resetn_;
405  gpio_regs_type gpio_cache_{};
406  uint16_t led_driver_cache_ = 0;
407  uint8_t clock_cache_ = 0;
408  uint8_t misc_cache_ = 0;
409 };
410 
411 } // ns misc
412 } // namespace
413 #endif /* NRFCXX_MISC_SX1509B_HPP */
nrfcxx::misc::sx1509b
Abstraction around the Semtex SX1509B Level-Shifting GPIO Extender.
Definition: sx1509b.hpp:25
nrfcxx::misc::sx1509b::led_type
Basic structure for LED configuration.
Definition: sx1509b.hpp:190
nrfcxx::misc::sx1509b::led_pwm_type
Extended configuration for fade-capable pins.
Definition: sx1509b.hpp:208
nrfcxx::misc::sx1509b::misc_cache
uint8_t misc_cache() const
Access the internal cache of Misc.
Definition: sx1509b.hpp:100
nrfcxx::misc::sx1509b::iface_config_type::address
uint8_t address
The I2C address used to communicate with the device.
Definition: sx1509b.hpp:42
nrfcxx::misc::sx1509b::gpio_regs_type
Structure for SX1509 registers that are controlled.
Definition: sx1509b.hpp:66
nrfcxx::misc::sx1509b::hw_reset
int hw_reset() noexcept
Reset the device to its power-on state via RESETn.
nrfcxx::misc::sx1509b::iface_config_type
The pieces you need to talk to the device.
Definition: sx1509b.hpp:29
nrfcxx::misc::sx1509b::configure_as_leds
int configure_as_leds(uint16_t leds, bool source_current=false) noexcept
Configure a set of pins for LED operations.
nrfcxx::misc::sx1509b::iface_config_type::resetn_psel
int8_t resetn_psel
GPIO pin selector used for RESETn signal.
Definition: sx1509b.hpp:35
nrfcxx::misc::sx1509b::iface_config_type::twi
periph::TWI & twi
Reference to TWI device used to communicate with sensor.
Definition: sx1509b.hpp:32
nrfcxx::misc::sx1509b::configure
int configure(unsigned int psel, unsigned int pin_cnf, int initial=-1) noexcept
Change the GPIO pin configuration for the given IOX pin.
nrfcxx::misc::sx1509b::led_configuration
int led_configuration(unsigned int psel, led_type &cfg) const noexcept
Read the LED configuration for the given pin.
nrfcxx::periph::TWI
Wrapper around the nRF51 TWI peripheral.
Definition: periph.hpp:1528
gpio.hpp
Core GPIO functionality.
nrfcxx::misc::sx1509b::led_pwm_type::trise
uint8_t trise
Fade in register.
Definition: sx1509b.hpp:211
nrfcxx::misc::sx1509b::clock
int clock() const noexcept
Read the Clock register from the SX1509B.
nrfcxx::misc::sx1509b::gpio_cache
const gpio_regs_type & gpio_cache() const noexcept
Access the internal cache of GPIO registers.
Definition: sx1509b.hpp:82
nrfcxx::misc::sx1509b::led_pwm_type::tfall
uint8_t tfall
Fade out register.
Definition: sx1509b.hpp:214
nrfcxx::misc::sx1509b::multiconfigure
int multiconfigure(uint16_t out_set, uint16_t out_clear, uint16_t pull_down, uint16_t pull_up=0) noexcept
Configure multiple pins at once.
nrfcxx::misc::sx1509b::output_sct
int output_sct(uint16_t set, uint16_t clear) noexcept
Multi-signal set, clear, and toggle API.
nrfcxx::misc::sx1509b::iface_config
const iface_config_type & iface_config() const
Access the interface configuration for the device.
Definition: sx1509b.hpp:53
nrfcxx::misc::sx1509b::led_driver_cache
uint16_t led_driver_cache() const
Access the internal cache of LEDDriverEnable.
Definition: sx1509b.hpp:88
nrfcxx::misc::sx1509b::led_configure
int led_configure(unsigned int psel, const led_type &cfg) const noexcept
Set the LED configuration for the given pin.
nrfcxx::misc::sx1509b::clock_cache
uint8_t clock_cache() const
Access the internal cache of Clock.
Definition: sx1509b.hpp:94
nrfcxx::misc::sx1509b::reload_cache
int reload_cache() noexcept
Read all locally cached registers from the device.
nrfcxx::misc::sx1509b::configuration
unsigned int configuration(unsigned int psel) const noexcept
Return the GPIO pin configuration for the given IOX pin.
nrfcxx::misc::sx1509b::misc
int misc() const noexcept
Read the Misc register from the SX1509B.
periph.hpp
Abstraction of Nordic device peripherals.
nrfcxx
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
nrfcxx::misc::sx1509b::sx1509b
sx1509b(iface_config_type &ifc, unsigned int addr=0) noexcept
Instantiate the device.
nrfcxx::misc::sx1509b::sw_reset
int sw_reset() noexcept
Reset the device to its power-on state via software command.