ESPixelStick Firmware
Firmware for the ESPixelStick
Loading...
Searching...
No Matches
OutputUart.hpp
Go to the documentation of this file.
1#pragma once
2/*
3 * OutputUart.hpp - Uart driver code for ESPixelStick Uart Channel
4 *
5 * Project: ESPixelStick - An ESP8266 / ESP32 and E1.31 based pixel driver
6 * Copyright (c) 2015 Shelby Merrick
7 * http://www.forkineye.com
8 *
9 * This program is provided free for you to use in any way that you wish,
10 * subject to the laws and regulations where you are using it. Due diligence
11 * is strongly suggested before using this code. Please give credit where due.
12 *
13 * The Author makes no warranty of any kind, express or implied, with regard
14 * to this program or the documentation contained in this document. The
15 * Author shall not be liable in any event for incidental or consequential
16 * damages in connection with, or arising out of, the furnishing, performance
17 * or use of these programs.
18 *
19 */
20
21#include "ESPixelStick.h"
22
23#ifdef ARDUINO_ARCH_ESP32
24# include <soc/uart_reg.h>
25# include <driver/uart.h>
26# include <driver/gpio.h>
27#endif
28
29#include "OutputPixel.hpp"
30#include "OutputSerial.hpp"
31
33{
34public:
46
47// TX FIFO trigger level. WS2811 40 bytes gives 100us before the FIFO goes empty
48// We need to fill the FIFO at a rate faster than 0.3us per byte (1.2us/pixel)
49#define DEFAULT_UART_FIFO_TRIGGER_LEVEL (17)
50
57
68
75
77 {
79 gpio_num_t DataPin = gpio_num_t(-1);
80 uart_port_t UartId = uart_port_t(-1);
81 uint32_t IntensityDataWidth = 8; // 8 bits in a byte
83 uint32_t FrameStartBreakUS = 0;
87 uint32_t Baudrate = 57600; // current transmit rate
93 const CitudsArray_t *CitudsArray = nullptr;
94
95#if defined(SUPPORT_OutputType_DMX) || defined(SUPPORT_OutputType_Serial) || defined(SUPPORT_OutputType_Renard)
96 c_OutputSerial *pSerialDataSource = nullptr;
97#endif // defined(SUPPORT_OutputType_DMX) || defined(SUPPORT_OutputType_Serial) || defined(SUPPORT_OutputType_Renard)
98 };
99
100 c_OutputUart ();
101 virtual ~c_OutputUart ();
103 bool SetConfig (ArduinoJson::JsonObject &jsonConfig);
104 void GetConfig (ArduinoJson::JsonObject &jsonConfig);
105 void GetDriverName (String &sDriverName) { sDriverName = CN_OutputUart; }
106 void GetStatus (ArduinoJson::JsonObject &jsonStatus);
107 void PauseOutput (bool State);
108 void StartNewFrame ();
109
110 void IRAM_ATTR ISR_UART_Handler();
111 void IRAM_ATTR ISR_Handler_SendIntensityData();
112
113private:
114 void StartUart ();
117 void set_pin ();
121 void StartBreak ();
122 void EndBreak ();
123 void GenerateBreak (uint32_t DurationInUs, uint32_t MarkDurationInUs);
124 void SetIntensityDataWidth ();
125 void SetIntensity2Uart (uint8_t value, UartDataBitTranslationId_t ID);
126
128
130 bool OutputIsPaused = false;
132 bool HasBeenInitialized = false;
135 uint32_t ActiveIsrMask = 0;
136#if defined(ARDUINO_ARCH_ESP32)
137 intr_handle_t IsrHandle = nullptr;
138 SemaphoreHandle_t WaitFrameDone;
139#endif // defined(ARDUINO_ARCH_ESP32)
140
141 void IRAM_ATTR StartNewDataFrame();
143 inline uint32_t IRAM_ATTR getUartFifoLength();
144 inline bool IRAM_ATTR MoreDataToSend();
145 inline bool IRAM_ATTR GetNextIntensityToSend(uint32_t &DataToSend);
146 inline void IRAM_ATTR enqueueUartData(uint8_t value);
147 inline void IRAM_ATTR EnableUartInterrupts();
148 inline void IRAM_ATTR ClearUartInterrupts();
149 inline void IRAM_ATTR DisableUartInterrupts();
150
151// #define USE_UART_DEBUG_COUNTERS
152#ifdef USE_UART_DEBUG_COUNTERS
153 // debug counters
154 uint32_t DataCallbackCounter = 0;
155 uint32_t DataTaskcounter = 0;
156 uint32_t RxIsr = 0;
157 uint32_t ErrorIsr = 0;
158 uint32_t FiFoISRcounter = 0;
159 uint32_t BreakIsrCounter = 0;
160 uint32_t IdleIsrCounter = 0;
161 uint32_t IsrIsNotForUs = 0;
162 uint32_t UnknownIsr = 0;
163 uint32_t TimerIsrCounter = 0;
164 uint32_t TimerIsrNoDataToSend = 0;
165 uint32_t TimerIsrSendData = 0;
166 uint32_t FrameThresholdCounter = 0;
167 uint32_t FrameEndISRcounter = 0;
168 uint32_t FrameStartCounter = 0;
169 uint32_t IntensityValuesSent = 0;
170 uint32_t IntensityBitsSent = 0;
171 uint32_t IntensityValuesSentLastFrame = 0;
172 uint32_t IntensityBitsSentLastFrame = 0;
173 uint32_t IncompleteFrame = 0;
174 uint32_t EnqueueCounter = 0;
175 uint32_t FiFoNotEmpty = 0;
176 uint32_t FiFoEmpty = 0;
177 uint32_t TxStopped = 0;
178
179#endif // def USE_UART_DEBUG_COUNTERS
180
181#ifndef UART_TX_BRK_DONE_INT_ENA
182# define UART_TX_BRK_DONE_INT_ENA 0
183#endif // ndef | UART_TX_BRK_DONE_INT_ENA
184
185#ifndef UART_TX_BRK_IDLE_DONE_INT_ENA
186# define UART_TX_BRK_IDLE_DONE_INT_ENA 0
187#endif // ndef | UART_TX_BRK_IDLE_DONE_INT_ENA
188
189#ifndef UART_INTR_MASK
190# if defined(ARDUINO_ARCH_ESP32)
191# define UART_INTR_MASK uint32_t((1 << 18) - 1)
192# elif defined(ARDUINO_ARCH_ESP8266)
193# define UART_INTR_MASK uint32_t((1 << 8) - 1)
194# endif // defined(ARDUINO_ARCH_ESP8266)
195#endif // UART_INTR_MASK
196
197#ifndef ESP_INTR_FLAG_IRAM
198# define ESP_INTR_FLAG_IRAM 0
199#endif // ndef ESP_INTR_FLAG_IRAM
200
201// timer interrupt support for GECE on ESP8266
202#if defined(ARDUINO_ARCH_ESP8266)
203
204public:
205
206 void IRAM_ATTR ISR_Timer_Handler();
207
208private:
209 void CalculateInterIntensityMabTime();
210 inline bool IsUartTimerInUse() __attribute__((gnu_inline, const))
211 {
213 } // IsUartTimerInUse
214
215 // Cycle counter
216 // static uint32_t _getCycleCount(void) __attribute__((always_inline));
217 static inline uint32_t _getCycleCount(void)
218 {
219 uint32_t ccount;
220 __asm__ __volatile__("rsr %0,ccount" : "=a"(ccount));
221 return ccount;
222 }
223
224#endif // defined(ARDUINO_ARCH_ESP8266)
225};
const CN_PROGMEM char CN_OutputUart[]
Definition ConstNames.cpp:154
#define DEFAULT_UART_FIFO_TRIGGER_LEVEL
Definition OutputUart.hpp:49
c_OutputMgr::e_OutputChannelIds OID_t
Definition OutputCommon.hpp:39
e_OutputChannelIds
Definition OutputMgr.hpp:67
Definition OutputPixel.hpp:28
Definition OutputUart.hpp:33
bool HasBeenInitialized
Definition OutputUart.hpp:132
bool RegisterUartIsrHandler()
Definition OutputUart.cpp:860
virtual ~c_OutputUart()
Definition OutputUart.cpp:140
OutputUartConfig_t OutputUartConfig
Definition OutputUart.hpp:127
void IRAM_ATTR ISR_UART_Handler()
Definition OutputUart.cpp:606
c_OutputUart()
Definition OutputUart.cpp:130
void IRAM_ATTR ClearUartInterrupts()
Definition OutputUart.cpp:983
void PauseOutput(bool State)
Definition OutputUart.cpp:840
void TerminateSerialPortOperation()
Definition OutputUart.cpp:1140
void RestoreSerialPortOperation()
Definition OutputUart.cpp:1192
void ReportNewFrame()
void Begin(c_OutputUart::OutputUartConfig_t &config)
Definition OutputUart.cpp:177
uint32_t IRAM_ATTR getUartFifoLength()
Definition OutputUart.cpp:583
bool OutputIsPaused
Definition OutputUart.hpp:130
ConvertIntensityToUartDataStreamEntry_t CitudsArray_t
Definition OutputUart.hpp:74
void SetIntensityDataWidth()
Definition OutputUart.cpp:920
void StartUart()
Definition OutputUart.cpp:1103
void SetIntensity2Uart(uint8_t value, UartDataBitTranslationId_t ID)
Definition OutputUart.cpp:914
void IRAM_ATTR ISR_Handler_SendIntensityData()
Definition OutputUart.cpp:750
uint32_t NumUartSlotsPerIntensityValue
Definition OutputUart.hpp:133
uint32_t MarkAfterInterintensityBreakBitCCOUNT
Definition OutputUart.hpp:134
void IRAM_ATTR enqueueUartData(uint8_t value)
Definition OutputUart.cpp:593
void EndBreak()
Definition OutputUart.cpp:243
void CalculateEnableUartInterruptFlags()
Definition OutputUart.cpp:996
uint8_t Intensity2Uart[UartDataBitTranslationId_t::Uart_LIST_END]
Definition OutputUart.hpp:129
void InitializeUart()
bool IRAM_ATTR MoreDataToSend()
Definition OutputUart.cpp:534
void GetDriverName(String &sDriverName)
Definition OutputUart.hpp:105
void IRAM_ATTR EnableUartInterrupts()
Definition OutputUart.cpp:1033
UartDataBitTranslationId_t
Definition OutputUart.hpp:59
@ Uart_NUM_BIT_TYPES
Definition OutputUart.hpp:66
@ Uart_DATA_BIT_10_ID
Definition OutputUart.hpp:62
@ Uart_DATA_BIT_00_ID
Definition OutputUart.hpp:60
@ Uart_LIST_END
Definition OutputUart.hpp:64
@ Uart_INVALID_VALUE
Definition OutputUart.hpp:65
@ Uart_DATA_BIT_01_ID
Definition OutputUart.hpp:61
@ Uart_DATA_BIT_11_ID
Definition OutputUart.hpp:63
void set_pin()
Definition OutputUart.cpp:955
bool SetConfig(ArduinoJson::JsonObject &jsonConfig)
Definition OutputUart.cpp:890
UartDatauint32_t
Definition OutputUart.hpp:36
@ OUTPUT_UART_8N1
Definition OutputUart.hpp:43
@ OUTPUT_UART_7N1
Definition OutputUart.hpp:41
@ OUTPUT_UART_7N2
Definition OutputUart.hpp:42
@ OUTPUT_UART_6N2
Definition OutputUart.hpp:40
@ OUTPUT_UART_5N2
Definition OutputUart.hpp:38
@ OUTPUT_UART_6N1
Definition OutputUart.hpp:39
@ OUTPUT_UART_8N2
Definition OutputUart.hpp:44
@ OUTPUT_UART_5N1
Definition OutputUart.hpp:37
void IRAM_ATTR StartNewDataFrame()
Definition OutputUart.cpp:568
void GetStatus(ArduinoJson::JsonObject &jsonStatus)
Definition OutputUart.cpp:294
void StartNewFrame()
Definition OutputUart.cpp:1058
void GetConfig(ArduinoJson::JsonObject &jsonConfig)
Definition OutputUart.cpp:280
void GenerateBreak(uint32_t DurationInUs, uint32_t MarkDurationInUs)
Definition OutputUart.cpp:259
void IRAM_ATTR DisableUartInterrupts()
Definition OutputUart.cpp:989
uint32_t ActiveIsrMask
Definition OutputUart.hpp:135
bool IRAM_ATTR GetNextIntensityToSend(uint32_t &DataToSend)
Definition OutputUart.cpp:551
TranslateIntensityData_t
Definition OutputUart.hpp:52
@ NoTranslation
Definition OutputUart.hpp:53
@ TwoToOne
Definition OutputUart.hpp:55
@ OneToOne
Definition OutputUart.hpp:54
uint32_t TxIntensityDataStartingMask
Definition OutputUart.hpp:131
void StartBreak()
Definition OutputUart.cpp:226
struct FSEQParsedRangeEntry __attribute__
config_t config
Definition main.cpp:93
Definition OutputUart.hpp:70
uint8_t Translation
Definition OutputUart.hpp:71
c_OutputUart::UartDataBitTranslationId_t Id
Definition OutputUart.hpp:72
Definition OutputUart.hpp:77
uint32_t IntensityDataWidth
Definition OutputUart.hpp:81
uart_port_t UartId
Definition OutputUart.hpp:80
uint16_t NumInterIntensityMABbits
Definition OutputUart.hpp:91
bool InvertOutputPolarity
Definition OutputUart.hpp:86
uint16_t NumInterIntensityBreakBits
Definition OutputUart.hpp:90
uint32_t Baudrate
Definition OutputUart.hpp:87
UartDatauint32_t UartDataSize
Definition OutputUart.hpp:82
uint32_t FrameStartBreakUS
Definition OutputUart.hpp:83
uint32_t FiFoTriggerLevel
Definition OutputUart.hpp:89
const CitudsArray_t * CitudsArray
Definition OutputUart.hpp:93
TranslateIntensityData_t TranslateIntensityData
Definition OutputUart.hpp:85
c_OutputPixel * pPixelDataSource
Definition OutputUart.hpp:88
bool TriggerIsrExternally
Definition OutputUart.hpp:92
uint32_t FrameStartMarkAfterBreakUS
Definition OutputUart.hpp:84
gpio_num_t DataPin
Definition OutputUart.hpp:79
c_OutputCommon::OID_t ChannelId
Definition OutputUart.hpp:78