Skip to content

Instantly share code, notes, and snippets.

@Staars
Created November 19, 2024 14:01
Show Gist options
  • Save Staars/78bffc2c05c714b9203dc4d1cd818214 to your computer and use it in GitHub Desktop.
Save Staars/78bffc2c05c714b9203dc4d1cd818214 to your computer and use it in GitHub Desktop.
Test for WS2812b, can not work
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/* ULP-RISC-V example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
This code runs on ULP-RISC-V coprocessor
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "ulp_riscv.h"
#include "ulp_riscv_utils.h"
#include "ulp_riscv_gpio.h"
#define EXAMPLE_WS2812B_GPIO GPIO_NUM_4
#define LED_NUM 12
typedef enum {
GPIO_NOT_INITIATED,
GPIO_INITIATED,
} gpio_state_t;
gpio_state_t state = GPIO_NOT_INITIATED;
uint8_t led_buffer[LED_NUM];
void ws2812b_write_bit(bool bit)
{
// time critical section
if (bit) { //High: long high, short low
// time critical section
REG_SET_FIELD(EXAMPLE_WS2812B_GPIO, RTC_GPIO_OUT_DATA_W1TS, BIT(0));
ulp_riscv_delay_cycles(8); // wait additional 8 * 57 nanoseconds
REG_SET_FIELD(EXAMPLE_WS2812B_GPIO, RTC_GPIO_OUT_DATA_W1TC, BIT(0));
// end of time critical section
}
else{ //Low: short high, long low
// time critical section
REG_SET_FIELD(EXAMPLE_WS2812B_GPIO, RTC_GPIO_OUT_DATA_W1TS, BIT(0));
REG_SET_FIELD(EXAMPLE_WS2812B_GPIO, RTC_GPIO_OUT_DATA_W1TC, BIT(0));
// end of time critical section
}
// the "low" period must be shorter than 5 microseconds, and must be at least 900 nanoseconds
}
static void ws2812b_write_byte(uint8_t data)
{
for (int i = 0; i < 8; i++) {
ws2812b_write_bit((data >> i) & 0x1);
}
}
static void ws2812b_write_buffer()
{
for (int i = 0; i < LED_NUM; i++) {
ws2812b_write_bit(led_buffer[i]);
}
}
int main (void)
{
switch (state) {
case GPIO_NOT_INITIATED:
/* Setup GPIO used for WS2812 */
ulp_riscv_gpio_init(EXAMPLE_WS2812B_GPIO);
ulp_riscv_gpio_output_enable(EXAMPLE_WS2812B_GPIO);
ulp_riscv_gpio_set_output_mode(EXAMPLE_WS2812B_GPIO, RTCIO_MODE_OUTPUT_OD);
ulp_riscv_gpio_pullup(EXAMPLE_WS2812B_GPIO);
ulp_riscv_gpio_pulldown_disable(EXAMPLE_WS2812B_GPIO);
ws2812b_write_buffer();
state = GPIO_INITIATED;
break;
case GPIO_INITIATED:
ws2812b_write_buffer();
break;
}
/* ulp_riscv_halt() is called automatically when main exits,
main will be executed again at the next timeout period,
according to ulp_set_wakeup_period()
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment