@@ -54,6 +54,7 @@ static uint32_t _ws281x_one_on;
5454static uint32_t _ws281x_one_off ;
5555static uint32_t _ws281x_zero_on ;
5656static uint32_t _ws281x_zero_off ;
57+ static uint8_t channel ;
5758
5859static uint8_t _rmt_channel (ws281x_t * dev )
5960{
@@ -71,23 +72,6 @@ void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size)
7172 assert (dev );
7273
7374#ifdef MODULE_WS281X_ESP32_HW
74- /* determine used RMT channel from configured GPIO */
75- uint8_t channel = _rmt_channel (dev );
76-
77- /* determine the current clock frequency */
78- uint32_t freq ;
79- if (rmt_get_counter_clock (channel , & freq ) != ESP_OK ) {
80- LOG_ERROR ("[ws281x_esp32] Could not get RMT counter clock\n" );
81- return ;
82- }
83-
84- /* compute phase times used in ws2812_rmt_adapter once rmt_write_sample is called */
85- uint32_t total_cycles = freq / (NS_PER_SEC / WS281X_T_DATA_NS );
86- _ws281x_one_on = freq / (NS_PER_SEC / WS281X_T_DATA_ONE_NS );
87- _ws281x_one_off = total_cycles - _ws281x_one_on ;
88- _ws281x_zero_on = freq / (NS_PER_SEC / WS281X_T_DATA_ZERO_NS );
89- _ws281x_zero_off = total_cycles - _ws281x_zero_on ;
90-
9175 rmt_write_sample (channel , (const uint8_t * )buf , size , false);
9276#else
9377 const uint8_t * pos = buf ;
@@ -192,7 +176,7 @@ int ws281x_init(ws281x_t *dev, const ws281x_params_t *params)
192176 "[ws281x_esp32] RMT configuration problem" );
193177
194178 /* determine used RMT channel from configured GPIO */
195- uint8_t channel = _rmt_channel (dev );
179+ channel = _rmt_channel (dev );
196180
197181 rmt_config_t cfg = RMT_DEFAULT_CONFIG_TX (params -> pin , channel );
198182 cfg .clk_div = 2 ;
@@ -203,6 +187,21 @@ int ws281x_init(ws281x_t *dev, const ws281x_params_t *params)
203187 LOG_ERROR ("[ws281x_esp32] RMT initialization failed\n" );
204188 return - EIO ;
205189 }
190+
191+ /* determine the current clock frequency */
192+ uint32_t freq ;
193+ if (rmt_get_counter_clock (channel , & freq ) != ESP_OK ) {
194+ LOG_ERROR ("[ws281x_esp32] Could not get RMT counter clock\n" );
195+ return - EIO ;
196+ }
197+
198+ /* compute phase times used in ws2812_rmt_adapter */
199+ uint32_t total_cycles = freq / (NS_PER_SEC / WS281X_T_DATA_NS );
200+ _ws281x_one_on = freq / (NS_PER_SEC / WS281X_T_DATA_ONE_NS );
201+ _ws281x_one_off = total_cycles - _ws281x_one_on ;
202+ _ws281x_zero_on = freq / (NS_PER_SEC / WS281X_T_DATA_ZERO_NS );
203+ _ws281x_zero_off = total_cycles - _ws281x_zero_on ;
204+
206205#else
207206 if (gpio_init (dev -> params .pin , GPIO_OUT )) {
208207 return - EIO ;
0 commit comments