OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_neoled.c] - Blame information for rev 66

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 52 zero_gravi
// #################################################################################################
2
// # << NEORV32: neorv32_neoled.c - Smart LED Interface (NEOLED) HW Driver >>                      #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6
// # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
7
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
// #################################################################################################
34
 
35
 
36
/**********************************************************************//**
37
 * @file neorv32_neoled.c
38
 * @author Stephan Nolting
39
 * @brief Smart LED Interface (NEOLED) HW driver source file.
40
 *
41
 * @note These functions should only be used if the NEOLED unit was synthesized (IO_NEOLED_EN = true).
42
 **************************************************************************/
43
 
44
#include "neorv32.h"
45
#include "neorv32_neoled.h"
46
 
47
 
48
/**********************************************************************//**
49
 * Check if NEOLED unit was synthesized.
50
 *
51
 * @return 0 if NEOLED was not synthesized, 1 if NEOLED is available.
52
 **************************************************************************/
53
int neorv32_neoled_available(void) {
54
 
55 64 zero_gravi
  if (NEORV32_SYSINFO.SOC & (1 << SYSINFO_SOC_IO_NEOLED)) {
56 52 zero_gravi
    return 1;
57
  }
58
  else {
59
    return 0;
60
  }
61
}
62
 
63
 
64
/**********************************************************************//**
65 64 zero_gravi
 * Enable and configure NEOLED controller. The NEOLED control register bits are listed in #NEORV32_NEOLED_CTRL_enum.
66 52 zero_gravi
 * This function performs a "raw" configuration (just configuraing the according control register bit).
67
 *
68
 * @param[in] prsc Clock prescaler select (0..7). See #NEORV32_CLOCK_PRSC_enum.
69
 * @param[in] t_total Number of pre-scaled clock ticks for total bit period (0..31).
70
 * @param[in] t_high_zero Number of pre-scaled clock ticks to generate high-time for sending a '0' (0..31).
71
 * @param[in] t_high_one Number of pre-scaled clock ticks to generate high-time for sending a '1' (0..31).
72
 **************************************************************************/
73 62 zero_gravi
void neorv32_neoled_setup(uint32_t prsc, uint32_t t_total, uint32_t t_high_zero, uint32_t t_high_one) {
74 52 zero_gravi
 
75 64 zero_gravi
  NEORV32_NEOLED.CTRL = 0; // reset
76 52 zero_gravi
 
77
  // module enable
78 64 zero_gravi
  uint32_t ct_enable = 1 << NEOLED_CTRL_EN;
79 52 zero_gravi
 
80
  // clock pre-scaler
81 64 zero_gravi
  uint32_t ct_prsc = (prsc & 0x7) << NEOLED_CTRL_PRSC0;
82 52 zero_gravi
 
83
  // serial data output: total period length for one bit
84 64 zero_gravi
  uint32_t ct_t_total = (t_total & 0x1f) << NEOLED_CTRL_T_TOT_0;
85 52 zero_gravi
 
86
  // serial data output: high-time for sending a '0'
87 64 zero_gravi
  uint32_t ct_t_zero = (t_high_zero & 0x1f) << NEOLED_CTRL_T_ZERO_H_0;
88 52 zero_gravi
 
89
  // serial data output: high-time for sending a '1'
90 64 zero_gravi
  uint32_t ct_t_one = (t_high_one & 0x1f) << NEOLED_CTRL_T_ONE_H_0;
91 52 zero_gravi
 
92
  // set new configuration
93 64 zero_gravi
  NEORV32_NEOLED.CTRL = ct_enable | ct_prsc | ct_t_total | ct_t_zero | ct_t_one;
94 52 zero_gravi
}
95
 
96
 
97
/**********************************************************************//**
98
 * Configure NEOLED controller for using WS2812 LEDs (NeoPixel-compatible). This function computes
99 66 zero_gravi
 * all the required timings and finally calls #neorv32_neoled_setup.
100 52 zero_gravi
 *
101
 * @note WS2812 timing: T_period = 1.2us, T_high_zero = 0.4us, T_high_one = 0.8us. Change the constants if required.
102
 * @note This function uses the SYSINFO_CLK value (from the SYSINFO HW module) to do the timing computations.
103
 **************************************************************************/
104 62 zero_gravi
void neorv32_neoled_setup_ws2812(void) {
105 52 zero_gravi
 
106
  // WS2812 timing
107
  const uint32_t T_TOTAL_C  = 1200; // ns
108
  const uint32_t T_H_ZERO_C =  400; // ns
109
  const uint32_t T_H_ONE_C  =  800; // ns
110
 
111
  // processor clock pre-scalers
112
  const uint32_t CLK_PRSC_FACTOR_LUT[8] = {2, 4, 8, 64, 128, 1024, 2048, 4096};
113
 
114
  // get base clock period in multiples of 0.5ns
115 64 zero_gravi
  uint32_t t_clock_x500ps = (2 * 1000 * 1000 * 1000) / NEORV32_SYSINFO.CLK;
116 52 zero_gravi
 
117
  // compute LED interface timing parameters
118
  uint32_t t_base         = 0;
119
  uint32_t t_total        = 0;
120
  uint32_t t_high_zero    = 0;
121
  uint32_t t_high_one     = 0;
122
  uint32_t clk_prsc_sel   = CLK_PRSC_2; // initial prsc = CLK/2
123
  uint32_t clk_prsc_fac   = 0; // corresponding clock scaling factor
124
 
125
//neorv32_uart0_printf("\nNEOLED.T_clk: %u x 500ps\n", t_clock_x500ps); // DEBUG
126
 
127
  while (clk_prsc_sel < 7) {
128
    clk_prsc_fac = CLK_PRSC_FACTOR_LUT[clk_prsc_sel & 7];
129
 
130
//neorv32_uart0_printf("NEOLED.clk_prsc: %u\n", clk_prsc_fac); // DEBUG
131
 
132
    t_base = t_clock_x500ps * clk_prsc_fac;
133
 
134
//neorv32_uart0_printf("NEOLED.t_base: %u x 0.5ns\n", t_base); // DEBUG
135
 
136
    // compute bit period and high-times for sending a 0 or 1
137
    t_total     = (2*T_TOTAL_C)  / t_base;
138
    t_high_zero = (2*T_H_ZERO_C) / t_base;
139
    t_high_one  = (2*T_H_ONE_C)  / t_base;
140
 
141
//neorv32_uart0_printf("NEOLED.t_total:     %u\n", t_total); // DEBUG
142
//neorv32_uart0_printf("NEOLED.t_high_zero: %u\n", t_high_zero); // DEBUG
143
//neorv32_uart0_printf("NEOLED.t_high_one:  %u\n", t_high_one); // DEBUG
144
 
145
    if ((t_base == 0) || (t_total >= 32) || (t_high_zero == 0) || (t_high_one == 0)) { // out of range or invalid resolution
146
      clk_prsc_sel++; // try next-higher clock prescaler
147
    }
148
    else {
149
      break;
150
    }
151
  }
152
 
153
  // set raw configuration
154 62 zero_gravi
  neorv32_neoled_setup(clk_prsc_sel, t_total, t_high_zero, t_high_one);
155 52 zero_gravi
}
156
 
157
 
158
/**********************************************************************//**
159 62 zero_gravi
 * Set NEOLED mode (24-bit RGB / 32-bit RGBW).
160
 *
161
 * @param[in] mode 0 = 24-bit mode (RGB), 1 = 32-bit mode (RGBW)
162 52 zero_gravi
 **************************************************************************/
163 62 zero_gravi
void neorv32_neoled_set_mode(uint32_t mode) {
164 52 zero_gravi
 
165 64 zero_gravi
  uint32_t ctrl = NEORV32_NEOLED.CTRL;
166
  ctrl &= ~(0b1 << NEOLED_CTRL_MODE); // clear current mode
167
  ctrl |= ((mode & 1) << NEOLED_CTRL_MODE); // set new mode
168
  NEORV32_NEOLED.CTRL = ctrl;
169 52 zero_gravi
}
170
 
171
 
172
/**********************************************************************//**
173 62 zero_gravi
 * Send strobe command ("RESET") - blocking.
174 52 zero_gravi
 **************************************************************************/
175 62 zero_gravi
void neorv32_neoled_strobe_blocking(void) {
176 52 zero_gravi
 
177 62 zero_gravi
  while(1) { // wait for FIFO full flag to clear
178 64 zero_gravi
    if ((NEORV32_NEOLED.CTRL & (1 << NEOLED_CTRL_TX_FULL)) == 0) {
179 62 zero_gravi
      break;
180
    }
181
  }
182
 
183
  neorv32_neoled_strobe_nonblocking();
184 52 zero_gravi
}
185
 
186
 
187
/**********************************************************************//**
188 62 zero_gravi
 * Send strobe command ("RESET") - non-blocking.
189 52 zero_gravi
 **************************************************************************/
190 62 zero_gravi
void neorv32_neoled_strobe_nonblocking(void) {
191 52 zero_gravi
 
192 64 zero_gravi
  const uint32_t mask = 1 << NEOLED_CTRL_STROBE; // strobe bit
193
  uint32_t ctrl = NEORV32_NEOLED.CTRL;
194 52 zero_gravi
 
195 64 zero_gravi
  NEORV32_NEOLED.CTRL = ctrl | mask; // set strobe bit
196
  NEORV32_NEOLED.DATA = 0; // send any data to trigger strobe command
197
  NEORV32_NEOLED.CTRL = ctrl & (~mask); // clear strobe bit
198 52 zero_gravi
}
199
 
200
 
201
/**********************************************************************//**
202 62 zero_gravi
 * Enable NEOLED controller.
203
 **************************************************************************/
204
void neorv32_neoled_enable(void) {
205
 
206 64 zero_gravi
  NEORV32_NEOLED.CTRL |= ((uint32_t)(1 << NEOLED_CTRL_EN));
207 62 zero_gravi
}
208
 
209
 
210
/**********************************************************************//**
211
 * Disable NEOLED controller.
212
 **************************************************************************/
213
void neorv32_neoled_disable(void) {
214
 
215 64 zero_gravi
  NEORV32_NEOLED.CTRL &= ~((uint32_t)(1 << NEOLED_CTRL_EN));
216 62 zero_gravi
}
217
 
218
 
219
/**********************************************************************//**
220
 * Send single RGB(W) data word to NEOLED module (blocking).
221 52 zero_gravi
 *
222 62 zero_gravi
 * @warning This function is blocking as it polls the NEOLED FIFO full flag.
223 52 zero_gravi
 *
224 62 zero_gravi
 * @param[in] data LSB-aligned 24-bit RGB or 32-bit RGBW data
225 52 zero_gravi
 **************************************************************************/
226 62 zero_gravi
void neorv32_neoled_write_blocking(uint32_t data) {
227 52 zero_gravi
 
228 62 zero_gravi
  while(1) { // wait for FIFO full flag to clear
229 64 zero_gravi
    if ((NEORV32_NEOLED.CTRL & (1 << NEOLED_CTRL_TX_FULL)) == 0) {
230 62 zero_gravi
      break;
231
    }
232
  }
233 52 zero_gravi
 
234 62 zero_gravi
  neorv32_neoled_write_nonblocking(data); // send new LED data
235 52 zero_gravi
}
236
 
237
 
238
/**********************************************************************//**
239
 * Get NEOLED hardware buffer size.
240
 *
241
 * @return Number of entries in NEOLED TX buffer.
242
 **************************************************************************/
243
uint32_t neorv32_neoled_get_buffer_size(void) {
244
 
245 64 zero_gravi
  uint32_t tmp = NEORV32_NEOLED.CTRL;
246
  tmp = tmp >> NEOLED_CTRL_BUFS_0;
247 62 zero_gravi
  tmp = tmp & 0xf; // isolate buffer size bits
248 52 zero_gravi
 
249
  return (1 << tmp); // num entries = pow(2, buffer size flags)
250
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.