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

Subversion Repositories neorv32

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

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
  if (SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_IO_NEOLED)) {
56
    return 1;
57
  }
58
  else {
59
    return 0;
60
  }
61
}
62
 
63
 
64
/**********************************************************************//**
65
 * Enable and configure NEOLED controller. The NEOLED control register bits are listed in #NEORV32_NEOLED_CT_enum.
66
 * This function performs a "raw" configuration (just configuraing the according control register bit).
67
 *
68
 * @param[in] bs_config Busy flag / IRQ configuration (0 = at least one free entry, 1 = whole buffer empty).
69
 * @param[in] prsc Clock prescaler select (0..7). See #NEORV32_CLOCK_PRSC_enum.
70
 * @param[in] t_total Number of pre-scaled clock ticks for total bit period (0..31).
71
 * @param[in] t_high_zero Number of pre-scaled clock ticks to generate high-time for sending a '0' (0..31).
72
 * @param[in] t_high_one Number of pre-scaled clock ticks to generate high-time for sending a '1' (0..31).
73
 **************************************************************************/
74
void neorv32_neoled_setup_raw(uint32_t bs_config, uint32_t prsc, uint32_t t_total, uint32_t t_high_zero, uint32_t t_high_one) {
75
 
76
  NEOLED_CT = 0; // reset
77
 
78
  // module enable
79
  uint32_t ct_enable = 1 << NEOLED_CT_EN;
80
 
81
  // busy flag / IRQ config
82
  uint32_t ct_bs_config = (bs_config & 0x1) << NEOLED_CT_BSCON;
83
 
84
  // clock pre-scaler
85
  uint32_t ct_prsc = (prsc & 0x7) << NEOLED_CT_PRSC0;
86
 
87
  // serial data output: total period length for one bit
88
  uint32_t ct_t_total = (t_total & 0x1f) << NEOLED_CT_T_TOT_0;
89
 
90
  // serial data output: high-time for sending a '0'
91
  uint32_t ct_t_zero = (t_high_zero & 0x1f) << NEOLED_CT_T_ZERO_H_0;
92
 
93
  // serial data output: high-time for sending a '1'
94
  uint32_t ct_t_one = (t_high_one & 0x1f) << NEOLED_CT_T_ONE_H_0;
95
 
96
  // set new configuration
97
  NEOLED_CT = ct_enable | ct_bs_config | ct_prsc | ct_t_total | ct_t_zero | ct_t_one;
98
}
99
 
100
 
101
/**********************************************************************//**
102
 * Configure NEOLED controller for using WS2812 LEDs (NeoPixel-compatible). This function computes
103
 * all the required timings and finally calls #neorv32_neoled_setup_raw.
104
 *
105
 * @note WS2812 timing: T_period = 1.2us, T_high_zero = 0.4us, T_high_one = 0.8us. Change the constants if required.
106
 * @note This function uses the SYSINFO_CLK value (from the SYSINFO HW module) to do the timing computations.
107
 *
108
 * @param[in] bs_config Busy flag / IRQ configuration (0 = at least one free entry, 1 = whole buffer empty).
109
 **************************************************************************/
110
void neorv32_neoled_setup_ws2812(uint32_t bs_config) {
111
 
112
  // WS2812 timing
113
  const uint32_t T_TOTAL_C  = 1200; // ns
114
  const uint32_t T_H_ZERO_C =  400; // ns
115
  const uint32_t T_H_ONE_C  =  800; // ns
116
 
117
  // processor clock pre-scalers
118
  const uint32_t CLK_PRSC_FACTOR_LUT[8] = {2, 4, 8, 64, 128, 1024, 2048, 4096};
119
 
120
  // get base clock period in multiples of 0.5ns
121
  uint32_t t_clock_x500ps = (2 * 1000 * 1000 * 1000) / SYSINFO_CLK;
122
 
123
  // compute LED interface timing parameters
124
  uint32_t t_base         = 0;
125
  uint32_t t_total        = 0;
126
  uint32_t t_high_zero    = 0;
127
  uint32_t t_high_one     = 0;
128
  uint32_t clk_prsc_sel   = CLK_PRSC_2; // initial prsc = CLK/2
129
  uint32_t clk_prsc_fac   = 0; // corresponding clock scaling factor
130
 
131
//neorv32_uart0_printf("\nNEOLED.T_clk: %u x 500ps\n", t_clock_x500ps); // DEBUG
132
 
133
  while (clk_prsc_sel < 7) {
134
    clk_prsc_fac = CLK_PRSC_FACTOR_LUT[clk_prsc_sel & 7];
135
 
136
//neorv32_uart0_printf("NEOLED.clk_prsc: %u\n", clk_prsc_fac); // DEBUG
137
 
138
    t_base = t_clock_x500ps * clk_prsc_fac;
139
 
140
//neorv32_uart0_printf("NEOLED.t_base: %u x 0.5ns\n", t_base); // DEBUG
141
 
142
    // compute bit period and high-times for sending a 0 or 1
143
    t_total     = (2*T_TOTAL_C)  / t_base;
144
    t_high_zero = (2*T_H_ZERO_C) / t_base;
145
    t_high_one  = (2*T_H_ONE_C)  / t_base;
146
 
147
//neorv32_uart0_printf("NEOLED.t_total:     %u\n", t_total); // DEBUG
148
//neorv32_uart0_printf("NEOLED.t_high_zero: %u\n", t_high_zero); // DEBUG
149
//neorv32_uart0_printf("NEOLED.t_high_one:  %u\n", t_high_one); // DEBUG
150
 
151
    if ((t_base == 0) || (t_total >= 32) || (t_high_zero == 0) || (t_high_one == 0)) { // out of range or invalid resolution
152
      clk_prsc_sel++; // try next-higher clock prescaler
153
    }
154
    else {
155
      break;
156
    }
157
  }
158
 
159
  // set raw configuration
160
  neorv32_neoled_setup_raw(bs_config, clk_prsc_sel, t_total, t_high_zero, t_high_one);
161
}
162
 
163
 
164
/**********************************************************************//**
165
 * Enable NEOLED controller.
166
 **************************************************************************/
167
void neorv32_neoled_enable(void) {
168
 
169
  NEOLED_CT |= ((uint32_t)(1 << NEOLED_CT_EN));
170
}
171
 
172
 
173
/**********************************************************************//**
174
 * Disable NEOLED controller.
175
 **************************************************************************/
176
void neorv32_neoled_disable(void) {
177
 
178
  NEOLED_CT &= ~((uint32_t)(1 << NEOLED_CT_EN));
179
}
180
 
181
 
182
/**********************************************************************//**
183
 * Send single data word to NEOLED module.
184
 *
185
 * @warning This function is blocking as it polls the NEOLED busy flag.
186
 *
187
 * @param[in] mode 0 = 24-bit mode (RGB), 1 = 32-bit mode (RGBW)
188
 * @param[in] data 24-bit RGB or 32-bit RGBW data
189
 **************************************************************************/
190
void neorv32_neoled_send_polling(uint32_t mode, uint32_t data) {
191
 
192
  while(NEOLED_CT & (1 << NEOLED_CT_BUSY)); // wait for busy flag to clear
193
 
194
  neorv32_neoled_send_direct(mode, data);
195
}
196
 
197
 
198
/**********************************************************************//**
199
 * Send single data word to NEOLED module.
200
 *
201
 * @warning This function used NO busy checks at all!
202
 * @note This function can be called several times in a row to fill the TX buffer (when busy_flag is cleared and bscon = 1).
203
 *
204
 * @param[in] mode 0 = 24-bit mode (RGB), 1 = 32-bit mode (RGBW)
205
 * @param[in] data 24-bit RGB or 32-bit RGBW data
206
 **************************************************************************/
207
void neorv32_neoled_send_direct(uint32_t mode, uint32_t data) {
208
 
209
  // configure TX mode (data size)
210
  uint32_t ctrl = NEOLED_CT;
211
  ctrl &= ~(0b1 << NEOLED_CT_MODE); // clear current mode
212
  ctrl |= ((mode & 1) << NEOLED_CT_MODE); // set new mode
213
  NEOLED_CT = ctrl;
214
 
215
  NEOLED_DATA = data; // send new LED data
216
}
217
 
218
 
219
/**********************************************************************//**
220
 * Get NEOLED hardware buffer size.
221
 *
222
 * @return Number of entries in NEOLED TX buffer.
223
 **************************************************************************/
224
uint32_t neorv32_neoled_get_buffer_size(void) {
225
 
226
  uint32_t tmp = NEOLED_CT;
227
  tmp = tmp >> NEOLED_CT_BUFS_0;
228
  tmp = tmp & 0b1111; // insulate buffer size flags
229
 
230
  return (1 << tmp); // num entries = pow(2, buffer size flags)
231
}

powered by: WebSVN 2.1.0

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