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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_neoled.c] - Diff between revs 52 and 62

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 52 Rev 62
Line 63... Line 63...
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Enable and configure NEOLED controller. The NEOLED control register bits are listed in #NEORV32_NEOLED_CT_enum.
 * Enable and configure NEOLED controller. The NEOLED control register bits are listed in #NEORV32_NEOLED_CT_enum.
 * This function performs a "raw" configuration (just configuraing the according control register bit).
 * This function performs a "raw" configuration (just configuraing the according control register bit).
 *
 *
 * @param[in] bs_config Busy flag / IRQ configuration (0 = at least one free entry, 1 = whole buffer empty).
 
 * @param[in] prsc Clock prescaler select (0..7). See #NEORV32_CLOCK_PRSC_enum.
 * @param[in] prsc Clock prescaler select (0..7). See #NEORV32_CLOCK_PRSC_enum.
 * @param[in] t_total Number of pre-scaled clock ticks for total bit period (0..31).
 * @param[in] t_total Number of pre-scaled clock ticks for total bit period (0..31).
 * @param[in] t_high_zero Number of pre-scaled clock ticks to generate high-time for sending a '0' (0..31).
 * @param[in] t_high_zero Number of pre-scaled clock ticks to generate high-time for sending a '0' (0..31).
 * @param[in] t_high_one Number of pre-scaled clock ticks to generate high-time for sending a '1' (0..31).
 * @param[in] t_high_one Number of pre-scaled clock ticks to generate high-time for sending a '1' (0..31).
 **************************************************************************/
 **************************************************************************/
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) {
void neorv32_neoled_setup(uint32_t prsc, uint32_t t_total, uint32_t t_high_zero, uint32_t t_high_one) {
 
 
  NEOLED_CT = 0; // reset
  NEOLED_CT = 0; // reset
 
 
  // module enable
  // module enable
  uint32_t ct_enable = 1 << NEOLED_CT_EN;
  uint32_t ct_enable = 1 << NEOLED_CT_EN;
 
 
  // busy flag / IRQ config
 
  uint32_t ct_bs_config = (bs_config & 0x1) << NEOLED_CT_BSCON;
 
 
 
  // clock pre-scaler
  // clock pre-scaler
  uint32_t ct_prsc = (prsc & 0x7) << NEOLED_CT_PRSC0;
  uint32_t ct_prsc = (prsc & 0x7) << NEOLED_CT_PRSC0;
 
 
  // serial data output: total period length for one bit
  // serial data output: total period length for one bit
  uint32_t ct_t_total = (t_total & 0x1f) << NEOLED_CT_T_TOT_0;
  uint32_t ct_t_total = (t_total & 0x1f) << NEOLED_CT_T_TOT_0;
Line 92... Line 88...
 
 
  // serial data output: high-time for sending a '1'
  // serial data output: high-time for sending a '1'
  uint32_t ct_t_one = (t_high_one & 0x1f) << NEOLED_CT_T_ONE_H_0;
  uint32_t ct_t_one = (t_high_one & 0x1f) << NEOLED_CT_T_ONE_H_0;
 
 
  // set new configuration
  // set new configuration
  NEOLED_CT = ct_enable | ct_bs_config | ct_prsc | ct_t_total | ct_t_zero | ct_t_one;
  NEOLED_CT = ct_enable | ct_prsc | ct_t_total | ct_t_zero | ct_t_one;
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Configure NEOLED controller for using WS2812 LEDs (NeoPixel-compatible). This function computes
 * Configure NEOLED controller for using WS2812 LEDs (NeoPixel-compatible). This function computes
 * all the required timings and finally calls #neorv32_neoled_setup_raw.
 * all the required timings and finally calls #neorv32_neoled_setup_raw.
 *
 *
 * @note WS2812 timing: T_period = 1.2us, T_high_zero = 0.4us, T_high_one = 0.8us. Change the constants if required.
 * @note WS2812 timing: T_period = 1.2us, T_high_zero = 0.4us, T_high_one = 0.8us. Change the constants if required.
 * @note This function uses the SYSINFO_CLK value (from the SYSINFO HW module) to do the timing computations.
 * @note This function uses the SYSINFO_CLK value (from the SYSINFO HW module) to do the timing computations.
 *
 
 * @param[in] bs_config Busy flag / IRQ configuration (0 = at least one free entry, 1 = whole buffer empty).
 
 **************************************************************************/
 **************************************************************************/
void neorv32_neoled_setup_ws2812(uint32_t bs_config) {
void neorv32_neoled_setup_ws2812(void) {
 
 
  // WS2812 timing
  // WS2812 timing
  const uint32_t T_TOTAL_C  = 1200; // ns
  const uint32_t T_TOTAL_C  = 1200; // ns
  const uint32_t T_H_ZERO_C =  400; // ns
  const uint32_t T_H_ZERO_C =  400; // ns
  const uint32_t T_H_ONE_C  =  800; // ns
  const uint32_t T_H_ONE_C  =  800; // ns
Line 155... Line 149...
      break;
      break;
    }
    }
  }
  }
 
 
  // set raw configuration
  // set raw configuration
  neorv32_neoled_setup_raw(bs_config, clk_prsc_sel, t_total, t_high_zero, t_high_one);
  neorv32_neoled_setup(clk_prsc_sel, t_total, t_high_zero, t_high_one);
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Enable NEOLED controller.
 * Set NEOLED mode (24-bit RGB / 32-bit RGBW).
 
 *
 
 * @param[in] mode 0 = 24-bit mode (RGB), 1 = 32-bit mode (RGBW)
 **************************************************************************/
 **************************************************************************/
void neorv32_neoled_enable(void) {
void neorv32_neoled_set_mode(uint32_t mode) {
 
 
  NEOLED_CT |= ((uint32_t)(1 << NEOLED_CT_EN));
  uint32_t ctrl = NEOLED_CT;
 
  ctrl &= ~(0b1 << NEOLED_CT_MODE); // clear current mode
 
  ctrl |= ((mode & 1) << NEOLED_CT_MODE); // set new mode
 
  NEOLED_CT = ctrl;
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Disable NEOLED controller.
 * Send strobe command ("RESET") - blocking.
 **************************************************************************/
 **************************************************************************/
void neorv32_neoled_disable(void) {
void neorv32_neoled_strobe_blocking(void) {
 
 
  NEOLED_CT &= ~((uint32_t)(1 << NEOLED_CT_EN));
  while(1) { // wait for FIFO full flag to clear
 
    if ((NEOLED_CT & (1 << NEOLED_CT_TX_FULL)) == 0) {
 
      break;
 
    }
 
  }
 
 
 
  neorv32_neoled_strobe_nonblocking();
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Send single data word to NEOLED module.
 * Send strobe command ("RESET") - non-blocking.
 *
 **************************************************************************/
 * @warning This function is blocking as it polls the NEOLED busy flag.
void neorv32_neoled_strobe_nonblocking(void) {
 *
 
 * @param[in] mode 0 = 24-bit mode (RGB), 1 = 32-bit mode (RGBW)
  const uint32_t mask = 1 << NEOLED_CT_STROBE; // strobe bit
 * @param[in] data 24-bit RGB or 32-bit RGBW data
  uint32_t ctrl = NEOLED_CT;
 
 
 
  NEOLED_CT = ctrl | mask; // set strobe bit
 
  NEOLED_DATA = 0; // send any data to trigger strobe command
 
  NEOLED_CT = ctrl & (~mask); // clear strobe bit
 
}
 
 
 
 
 
/**********************************************************************//**
 
 * Enable NEOLED controller.
 **************************************************************************/
 **************************************************************************/
void neorv32_neoled_send_polling(uint32_t mode, uint32_t data) {
void neorv32_neoled_enable(void) {
 
 
 
  NEOLED_CT |= ((uint32_t)(1 << NEOLED_CT_EN));
 
}
 
 
 
 
  while(NEOLED_CT & (1 << NEOLED_CT_BUSY)); // wait for busy flag to clear
/**********************************************************************//**
 
 * Disable NEOLED controller.
 
 **************************************************************************/
 
void neorv32_neoled_disable(void) {
 
 
  neorv32_neoled_send_direct(mode, data);
  NEOLED_CT &= ~((uint32_t)(1 << NEOLED_CT_EN));
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Send single data word to NEOLED module.
 * Send single RGB(W) data word to NEOLED module (blocking).
 *
 *
 * @warning This function used NO busy checks at all!
 * @warning This function is blocking as it polls the NEOLED FIFO full flag.
 * @note This function can be called several times in a row to fill the TX buffer (when busy_flag is cleared and bscon = 1).
 
 *
 *
 * @param[in] mode 0 = 24-bit mode (RGB), 1 = 32-bit mode (RGBW)
 * @param[in] data LSB-aligned 24-bit RGB or 32-bit RGBW data
 * @param[in] data 24-bit RGB or 32-bit RGBW data
 
 **************************************************************************/
 **************************************************************************/
void neorv32_neoled_send_direct(uint32_t mode, uint32_t data) {
void neorv32_neoled_write_blocking(uint32_t data) {
 
 
  // configure TX mode (data size)
  while(1) { // wait for FIFO full flag to clear
  uint32_t ctrl = NEOLED_CT;
    if ((NEOLED_CT & (1 << NEOLED_CT_TX_FULL)) == 0) {
  ctrl &= ~(0b1 << NEOLED_CT_MODE); // clear current mode
      break;
  ctrl |= ((mode & 1) << NEOLED_CT_MODE); // set new mode
    }
  NEOLED_CT = ctrl;
  }
 
 
  NEOLED_DATA = data; // send new LED data
  neorv32_neoled_write_nonblocking(data); // send new LED data
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Get NEOLED hardware buffer size.
 * Get NEOLED hardware buffer size.
Line 223... Line 242...
 **************************************************************************/
 **************************************************************************/
uint32_t neorv32_neoled_get_buffer_size(void) {
uint32_t neorv32_neoled_get_buffer_size(void) {
 
 
  uint32_t tmp = NEOLED_CT;
  uint32_t tmp = NEOLED_CT;
  tmp = tmp >> NEOLED_CT_BUFS_0;
  tmp = tmp >> NEOLED_CT_BUFS_0;
  tmp = tmp & 0b1111; // insulate buffer size flags
  tmp = tmp & 0xf; // isolate buffer size bits
 
 
  return (1 << tmp); // num entries = pow(2, buffer size flags)
  return (1 << tmp); // num entries = pow(2, buffer size flags)
}
}
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

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