[MCU Framework] [onewire] Use the single-wire protocol to light up the WS2812X analog IO compatible with OS

Publisher:zukeq2009Latest update time:2022-09-16 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

ws2812x data transmission time

T0H 0 code, high level time 220ns~380ns

T0L 0 code, low level time 580ns~1.6µs

T1H 1 code, high level time 580ns~1.6µs

T1L 1 code, low level time 220ns~420µs

RES frame unit, low level time>280µs or more


For ARM_M platform @24M

1 NOP: (1/24000000)100010001000==41.67ns

__NOP(); __NOP(); __NOP(); __NOP(); __NOP();

5 NOPs 41.675 = 208.35ns (plus code execution time, barely reaches the ws2812x standard)


For n76e003 platform @16M

1 nop takes 87ns

while(1) takes 194ns

Function call takes 570ns


Note:


If the light does not change when the white light is turned on or mixed colors are turned on, you need to check whether the power supply is sufficient. If an external battery is connected, the demand is met.

If multiple lights appear to be the wrong color, first check for power supply issues.

This example is written based on 24M. Please recalculate for other frequencies.

[MCU Framework] [app_led] [WS2812x] Using soft timer to implement WS2812x flashing and breathing lighting modes


pseudocode:


    temp[0] = 0xF0;

    temp[1] = 0x3B;

    temp[2] = 0x0F;

    onewire_send_data(0, temp, 3);

1

2

3

4

/********************************************************************************

* @file onewire.c

* @author jianqiang.xue

* @Version V1.0.0

* @Date 2020-12-07

* @brief can be used for ws2812x, etc.

ws2812x data transmission time

T0H 0 code, high level time 220ns~380ns

T0L 0 code, low level time 580ns~1.6µs

T1H 1 code, high level time 580ns~1.6µs

T1L 1 code, low level time 220ns~420µs

RES frame unit, low level time>280µs or more


For ARM_M platform @16M

1 NOP: (1/24000000)*1000*1000*1000==41.67ns

__NOP(); __NOP(); __NOP(); __NOP(); __NOP();     

For n76e003 platform @16M

1 nop takes 87ns

while(1) takes 194ns

Function call takes 570ns


Note: 1. If the light does not change when the white light or mixed colors are turned on, you need to check whether the power supply is sufficient. Connecting an external battery can meet the needs.

2. If multiple lights have incorrect colors, first check for power supply problems.

************************************************************************************/

/* Includes ------------------------------------------------------------------*/

#include "bsp_gpio.h"


#include "sys_api.h"

#include "os_api.h"


#include "onewire.h"

#include "business_gpio.h"

#include "business_function.h"


/* Private Define ------------------------------------------------------------*/

#if 1

#define T0_BIT_SIGNAL(GPIOX, PIN)                     

    GPIOX->ODSET=PIN;                                 

    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();     

    GPIOX->ODCLR=PIN;                                 

    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();     

    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();     


#define T1_BIT_SIGNAL(GPIOX, PIN)                     

    GPIOX->ODSET=PIN;                                 

    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();     

    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();     

    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();     

    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();     

    GPIOX->ODCLR=PIN;                                 

    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();     


#define RESET_SIGNAL(GPIOX, PIN)                     

    bsp_gpio_set_pin(GPIOX, PIN, BSP_GPIO_PIN_RESET);

    delay(500);                                       


#endif

/* Private Function Prototypes -----------------------------------------------*/


static void delay(uint16_t cnt)

{

    while (cnt--)

    {

        __NOP();

    }

}


void onewire_init(void)

{

#if BS_WS2812_CH0_EN

    BS_ONEWIRE_CH0_GPIO_CLK_ENABLE();

    bsp_gpio_init_output(ONEWIRE_CH0_PORT, ONEWIRE_CH0_PIN, BSP_GPIO_PIN_OUT_PP);

    bsp_gpio_set_pin(ONEWIRE_CH0_PORT, ONEWIRE_CH0_PIN, BSP_GPIO_PIN_RESET);

#endif

#if BS_WS2812_CH1_EN

    BS_ONEWIRE_CH1_GPIO_CLK_ENABLE();

    bsp_gpio_init_output(ONEWIRE_CH1_PORT, ONEWIRE_CH1_PIN, BSP_GPIO_PIN_OUT_PP);

    bsp_gpio_set_pin(ONEWIRE_CH1_PORT, ONEWIRE_CH1_PIN, BSP_GPIO_PIN_RESET);

#endif

#if BS_WS2812_CH2_EN

    BS_ONEWIRE_CH2_GPIO_CLK_ENABLE();

    bsp_gpio_init_output(ONEWIRE_CH2_PORT, ONEWIRE_CH2_PIN, BSP_GPIO_PIN_OUT_PP);

    bsp_gpio_set_pin(ONEWIRE_CH2_PORT, ONEWIRE_CH2_PIN, BSP_GPIO_PIN_RESET);

#endif

#if BS_WS2812_CH3_EN

    BS_ONEWIRE_CH3_GPIO_CLK_ENABLE();

    bsp_gpio_init_output(ONEWIRE_CH3_PORT, ONEWIRE_CH3_PIN, BSP_GPIO_PIN_OUT_PP);

    bsp_gpio_set_pin(ONEWIRE_CH3_PORT, ONEWIRE_CH3_PIN, BSP_GPIO_PIN_RESET);

#endif

}


void onewire_deinit(void)

{

#if BS_WS2812_CH0_EN

    bsp_gpio_deinit(ONEWIRE_CH0_PORT, ONEWIRE_CH0_PIN);

#endif

#if BS_WS2812_CH1_EN

    bsp_gpio_deinit(ONEWIRE_CH1_PORT, ONEWIRE_CH1_PIN);

#endif

#if BS_WS2812_CH2_EN

    bsp_gpio_deinit(ONEWIRE_CH2_PORT, ONEWIRE_CH2_PIN);

#endif

#if BS_WS2812_CH3_EN

    bsp_gpio_deinit(ONEWIRE_CH3_PORT, ONEWIRE_CH3_PIN);

#endif

}

bool flag = false;

void onewire_send_data(uint8_t ch, uint8_t *data, uint8_t len)

{

    uint8_t temp = 0;

    if (ch == 0)

    {

#if BS_WS2812_CH0_EN

        if (flag)

        {

            return;

        }

        flag = true;

        uint32_t val = SysTick->CTRL;

        SysTick->CTRL &= ~0x00000003; // Disable SysTick interrupt

        sys_disable_irq();

        RESET_SIGNAL(ONEWIRE_CH0_PORT, ONEWIRE_CH0_PIN);

        for (uint8_t n = 0; n < BS_WS2812_CH0_NUM; n++)

        {

            for (uint8_t i = 0; i < len; i++)

            {

                temp = data[i];

                for (uint8_t j = 0; j < 8; j++)

                {

                    if (temp & 0x80)

                    {

                        T1_BIT_SIGNAL(ONEWIRE_CH0_PORT, ONEWIRE_CH0_PIN)

                    }

                    else

                    {

                        T0_BIT_SIGNAL(ONEWIRE_CH0_PORT, ONEWIRE_CH0_PIN);

                    }

                    temp <<= 1;

                }

            }

        }

        RESET_SIGNAL(ONEWIRE_CH0_PORT, ONEWIRE_CH0_PIN);

        SysTick->CTRL = val;

        sys_enable_irq();

        flag = false;

#endif

    }

}


/********************************************************************************

* @file onewire.h

* @author jianqiang.xue

* @Version V1.0.0

* @Date 2020-11-23

* @brief NULL

************************************************************************************/

#ifndef __ONEWIRE_H

#define __ONEWIRE_H


/* Includes ------------------------------------------------------------------*/

#include

/* Public Function Prototypes ------------------------------------------------*/


void onewire_init(void);

void onewire_deinit(void);

void onewire_send_data(uint8_t ch, uint8_t *data, uint8_t len);

#endif /* __ONEWIRE_H */


Reference address:[MCU Framework] [onewire] Use the single-wire protocol to light up the WS2812X analog IO compatible with OS

Previous article:[MCU Framework] [app_led] [WS2812x] Using soft timer to implement WS2812x flashing and breathing lighting modes
Next article:C language macro definition and macro definition function

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号