31#ifndef __SHIFTREGISTER_H__
32#define __SHIFTREGISTER_H__
35#include "atomic_utils.h"
40#ifndef HIDE_FROM_DOXYGEN
49 friend class AdcManager;
50 friend class DigitalInAnalogIn;
51 friend class DigitalInOutAnalogOut;
52 friend class DigitalIn;
53 friend class DigitalInOut;
54 friend class DigitalInOutHBridge;
55 friend class CcioBoardManager;
56 friend class LedDriver;
57 friend class MotorDriver;
58 friend class MotorManager;
59 friend class SerialDriver;
60 friend class StatusManager;
61 friend class SysManager;
83 uint32_t A_CTRL_3 : 1;
84 uint32_t A_CTRL_2 : 1;
85 uint32_t LED_IO_5 : 1;
86 uint32_t LED_IO_4 : 1;
87 uint32_t LED_IO_3 : 1;
88 uint32_t LED_IO_2 : 1;
89 uint32_t LED_IO_1 : 1;
90 uint32_t LED_IO_0 : 1;
91 uint32_t EN_OUT_3 : 1;
92 uint32_t EN_OUT_2 : 1;
93 uint32_t EN_OUT_1 : 1;
94 uint32_t EN_OUT_0 : 1;
95 uint32_t UART_TTL_1 : 1;
96 uint32_t UART_TTL_0 : 1;
97 uint32_t UNDERGLOW : 1;
99 uint32_t UART_SPI_SEL_1 : 1;
100 uint32_t UART_SPI_SEL_0 : 1;
101 uint32_t LED_COM_0 : 1;
102 uint32_t LED_COM_1 : 1;
103 uint32_t CFG00_AOUT : 1;
104 uint32_t LED_DI_6 : 1;
105 uint32_t LED_DI_7 : 1;
106 uint32_t LED_DI_8 : 1;
107 uint32_t LED_ADI_12 : 1;
108 uint32_t LED_ADI_11 : 1;
109 uint32_t LED_ADI_10 : 1;
110 uint32_t LED_ADI_09 : 1;
111 uint32_t ANAIN_DIGITAL_12 : 1;
112 uint32_t ANAIN_DIGITAL_11 : 1;
113 uint32_t ANAIN_DIGITAL_10 : 1;
114 uint32_t ANAIN_DIGITAL_09 : 1;
123 ShiftChain(uint32_t val) {
134 SR_NO_FEEDBACK_MASK = 0,
135 SR_A_CTRL_3_MASK = 1U << 0,
136 SR_A_CTRL_2_MASK = 1U << 1,
137 SR_LED_IO_5_MASK = 1U << 2,
138 SR_LED_IO_4_MASK = 1U << 3,
139 SR_LED_IO_3_MASK = 1U << 4,
140 SR_LED_IO_2_MASK = 1U << 5,
141 SR_LED_IO_1_MASK = 1U << 6,
142 SR_LED_IO_0_MASK = 1U << 7,
143 SR_EN_OUT_3_MASK = 1U << 8,
144 SR_EN_OUT_2_MASK = 1U << 9,
145 SR_EN_OUT_1_MASK = 1U << 10,
146 SR_EN_OUT_0_MASK = 1U << 11,
147 SR_UART_TTL_1_MASK = 1U << 12,
148 SR_UART_TTL_0_MASK = 1U << 13,
149 SR_UNDERGLOW_MASK = 1U << 14,
150 SR_LED_USB_MASK = 1U << 15,
151 SR_UART_SPI_SEL_1_MASK = 1U << 16,
152 SR_UART_SPI_SEL_0_MASK = 1U << 17,
153 SR_LED_COM_0_MASK = 1U << 18,
154 SR_LED_COM_1_MASK = 1U << 19,
155 SR_CFG00_AOUT_MASK = 1U << 20,
156 SR_LED_DI_6_MASK = 1U << 21,
157 SR_LED_DI_7_MASK = 1U << 22,
158 SR_LED_DI_8_MASK = 1U << 23,
159 SR_LED_ADI_12_MASK = 1U << 24,
160 SR_LED_ADI_11_MASK = 1U << 25,
161 SR_LED_ADI_10_MASK = 1U << 26,
162 SR_LED_ADI_09_MASK = 1U << 27,
163 SR_ANAIN_DIGITAL_12_MASK = 1U << 28,
164 SR_ANAIN_DIGITAL_11_MASK = 1U << 29,
165 SR_ANAIN_DIGITAL_10_MASK = 1U << 30,
166 SR_ANAIN_DIGITAL_09_MASK = 1U << 31
175 enum LED_BLINK_CODE {
177 LED_BLINK_IO_SET = 0,
180 LED_BLINK_FAST_STROBE,
183 LED_BLINK_PWM = LED_BLINK_FADE,
185 LED_BLINK_OVERLOAD = LED_BLINK_FAST_STROBE,
186 LED_BLINK_CCIO_COMM_ERR = LED_BLINK_FADE,
187 LED_BLINK_CCIO_ONLINE = LED_BLINK_BREATHING
204 volatile const uint32_t &OverloadActive() {
205 return m_patternMasks[LED_BLINK_OVERLOAD];
211 volatile const bool &Ready() {
212 return m_initialized;
220 ShiftChain ShifterState() {
221 return atomic_load_n(&m_patternOutputs[LED_BLINK_IO_SET]);
229 bool ShifterState(Masks bitToGet) {
230 return atomic_load_n(&m_patternOutputs[LED_BLINK_IO_SET]) & bitToGet;
238 volatile const uint32_t &LastOutput() {
249 void ShifterState(
bool setFlds,
const ShiftChain &fldsToChange) {
250 setFlds ? ShifterStateSet(fldsToChange)
251 : ShifterStateClear(fldsToChange);
261 void ShifterState(
bool setFlds, Masks bitsToChange) {
262 setFlds ? ShifterStateSet(bitsToChange)
263 : ShifterStateClear(bitsToChange);
272 void ShifterStateToggle(ShiftChain fldsToToggle) {
273 atomic_xor_fetch(&m_patternOutputs[LED_BLINK_IO_SET], fldsToToggle.reg);
281 void ShifterStateReplace(uint32_t value) {
282 atomic_exchange_n(&m_patternOutputs[LED_BLINK_IO_SET], value);
292 void LedInFault(uint32_t ledMask,
bool state) {
294 m_patternMasks[LED_BLINK_OVERLOAD] |= ledMask;
297 m_patternMasks[LED_BLINK_OVERLOAD] &= ~ledMask;
310 void LedInPwm(Masks ledMask,
bool state, uint8_t index) {
312 m_fadeCounter.m_analogMasks[index] = ledMask;
313 state ? m_fadeCounter.m_activeMask |= 1 << index
314 : m_fadeCounter.m_activeMask &= ~(1 << index);
315 LedPattern(ledMask, LED_BLINK_FADE, state);
324 void LedPwmValue(uint8_t index, uint32_t value) {
326 m_fadeCounter.m_valuesBuf[index] = value;
337 void LedPattern(uint32_t ledMask, LED_BLINK_CODE pattern,
339 state ? m_patternMasks[pattern] |= ledMask
340 : m_patternMasks[pattern] &= ~ledMask;
365 TickCounter(uint32_t period, uint32_t cc)
370 const uint32_t returnTable[2] = {0x00000000, 0xffffffff};
376 return returnTable[count < cc];
386 class AnalogLedDriver {
388 uint16_t m_activeMask;
389 uint32_t m_lastOutput;
391 uint8_t m_values[16];
392 uint8_t m_valuesBuf[16];
393 Masks m_analogMasks[16];
398 m_count(UINT8_MAX - 1),
401 m_analogMasks{SR_NO_FEEDBACK_MASK} {}
404 uint32_t retVal = m_lastOutput;
406 m_count = UINT8_MAX - 1;
410 if (++m_count > UINT8_MAX >> 2) {
413 for (uint8_t i = 0; i < 16; i++) {
414 if (m_activeMask & (1 << i) && m_valuesBuf[i]) {
415 m_values[i] = m_valuesBuf[i];
416 retVal |= m_analogMasks[i];
421 uint8_t compare = m_count << 2;
422 for (uint8_t i = 0; i < 16; i++) {
423 if ((retVal & m_analogMasks[i]) &&
424 (m_values[i] < compare)) {
425 retVal &= ~m_analogMasks[i];
429 return m_lastOutput = retVal;
440 struct FadeInOutCounter {
448 const uint32_t returnTable[2] = {0x00000000, 0xffffffff};
451 FadeInOutCounter(uint8_t maxValue = UINT8_MAX >> 1,
452 uint8_t minValue = 0x08)
453 : m_maxValue(maxValue),
454 m_minValue(minValue),
460 if (++m_count >= UINT8_MAX >> 2) {
463 if (++m_compare >= m_maxValue) {
468 if (--m_compare <= m_minValue) {
473 return returnTable[m_count << 2 < m_compare];
478 static const uint8_t LED_BANK_0_LEN = 6;
479 const Masks LED_BANK_0[LED_BANK_0_LEN] = {SR_LED_IO_0_MASK,
480 SR_LED_IO_1_MASK, SR_LED_IO_2_MASK, SR_LED_IO_3_MASK, SR_LED_IO_4_MASK,
485 static const uint8_t LED_BANK_1_LEN = 7;
486 const Masks LED_BANK_1[LED_BANK_1_LEN] = {SR_LED_ADI_12_MASK,
487 SR_LED_ADI_11_MASK, SR_LED_ADI_10_MASK, SR_LED_ADI_09_MASK,
488 SR_LED_DI_8_MASK, SR_LED_DI_7_MASK, SR_LED_DI_6_MASK
492 static const uint8_t LED_BANK_2_LEN = 4;
493 const Masks LED_BANK_2[LED_BANK_2_LEN] = {SR_UNDERGLOW_MASK,
494 SR_LED_USB_MASK, SR_LED_COM_0_MASK, SR_LED_COM_1_MASK
497 static const uint16_t DELAY_TIME = 25;
501 const uint32_t SAFE_LED_MASK = SR_LED_IO_0_MASK | SR_LED_IO_1_MASK |
502 SR_LED_IO_2_MASK | SR_LED_IO_3_MASK | SR_LED_IO_4_MASK |
503 SR_LED_IO_5_MASK | SR_LED_DI_6_MASK | SR_LED_DI_7_MASK |
504 SR_LED_DI_8_MASK | SR_LED_ADI_09_MASK | SR_LED_ADI_10_MASK |
505 SR_LED_ADI_11_MASK | SR_LED_ADI_12_MASK | SR_LED_USB_MASK;
512 const uint32_t FAST_COUNTER_PERIOD = 500;
513 const uint32_t FAST_COUNTER_CC = 200;
514 TickCounter m_fastCounter;
515 FadeInOutCounter m_breathingCounter;
516 AnalogLedDriver m_fadeCounter;
520 ShiftChain m_shiftInversions;
522 uint32_t m_patternMasks[LED_BLINK_CODE_MAX];
523 uint32_t m_patternOutputs[LED_BLINK_CODE_MAX];
524 uint32_t m_altOutput;
528 bool m_blinkCodeActive;
529 bool m_blinkCodeState;
533 uint32_t m_pendingOutput;
535 uint32_t m_lastOutput;
537 uint32_t m_latchedOutput;
554 void ShifterStateSet(ShiftChain fldsToSet) {
555 atomic_or_fetch(&m_patternOutputs[LED_BLINK_IO_SET], fldsToSet.reg);
563 void ShifterStateSet(Masks bitsToSet) {
564 atomic_or_fetch(&m_patternOutputs[LED_BLINK_IO_SET], bitsToSet);
573 void ShifterStateClear(ShiftChain fldsToClr) {
574 atomic_and_fetch(&m_patternOutputs[LED_BLINK_IO_SET], ~fldsToClr.reg);
582 void ShifterStateClear(Masks bitsToClr) {
583 atomic_and_fetch(&m_patternOutputs[LED_BLINK_IO_SET], ~bitsToClr);
594 void DiagnosticLedSweep();
596 void BlinkCode(
bool blinkCodeActive,
bool blinkCodeState) {
597 m_blinkCodeActive = blinkCodeActive;
598 m_blinkCodeState = blinkCodeState;
Status blink code driver.
Namespace to encompass the ClearCore board API.
Definition AdcManager.h:36