配置串口相关参数stack
配置串口相关接口参数
配置串口硬件引脚
配置时钟
增加串口函数处理
bsp_debug_uart.c
#include"bsp_debug_uart.h"
#include"ws2812LED/bsp_ws2812LED.h"
/* 调试串口 UART0 初始化 */
voidDebug_UART9_Init(void)
{
fsp_err_terr=FSP_SUCCESS;
err=R_SCI_UART_Open(&g_uart9_ctrl,&g_uart9_cfg);
assert(FSP_SUCCESS==err);
}
/* 发送完成标志 */
volatile bool uart_send_complete_flag = false;
/* 串口中断回调 */
voiddebug_uart9_callback(uart_callback_args_t*p_args)
{
switch(p_args->event)
{
caseUART_EVENT_RX_CHAR:
{
/* 根据字符指令控制RGB彩灯颜色 */
switch(p_args->data)
{
case'1':
WS2812B_Task1();
break;
case'2':
WS2812B_Task2();
break;
case'3':
WS2812B_Task3();
break;
case'4':
WS2812B_Task4();
break;
case'5':
WS2812B_Task5();
break;
default:
break;
}
break;
}
caseUART_EVENT_TX_COMPLETE:
{
uart_send_complete_flag = true;
break;
}
default:
break;
}
}
/* 重定向 printf 输出 */
#if defined __GNUC__ && !defined __clang__
int_write(intfd,char*pBuffer,intsize);//防止编译警告
int_write(intfd,char*pBuffer,intsize)
{
(void)fd;
R_SCI_UART_Write(&g_uart9_ctrl,(uint8_t*)pBuffer,(uint32_t)size);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
returnsize;
}
#else
int fputc(int ch, FILE *f)
{
(void)f;
R_SCI_UART_Write(&g_uart0_ctrl,(uint8_t *)&ch,1);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return ch;
}
#endif
bsp_debug_uart.h
#ifndef __BSP_DEBUG_UART_H
#define __BSP_DEBUG_UART_H
#include"hal_data.h"
#include"stdio.h"
voidDebug_UART9_Init(void);
#endif
ws2812驱动更改如下:
bsp_ws2812LED.c
/*
* ws2812.c
*
* Created on: 2023年10月31日
* Author: Administrator
*/
#include "bsp_ws2812LED.h"
#include "hal_data.h"
extern fsp_err_t err ;
extern volatile bool g_transfer_complete ;
//灯条显存SPI数据缓存
uint8_t gWs2812bDat_SPI[WS2812B_AMOUNT * 24+88] = {0};
//灯条显存
tWs2812bCache_TypeDef gWs2812bDat[WS2812B_AMOUNT] = {
//R G B
0XFF, 0X00, 0X00, //0
0X00, 0XFF, 0X00, //1
0X00, 0X00, 0XFF, //2
0X00, 0XFF, 0XFF, //3
0XFF, 0X00, 0XFF, //4
0XFF, 0XFF, 0X00, //5
0XFF, 0XFF, 0XFF, //6
0X0F, 0X0F, 0X0F, //7
0XFF, 0X00, 0X00, //0
0X00, 0XFF, 0X00, //1
0X00, 0X00, 0XFF, //2
0X00, 0XFF, 0XFF, //3
0XFF, 0X00, 0XFF, //4
0XFF, 0XFF, 0X00, //5
0XFF, 0XFF, 0XFF, //6
0X0F, 0X0F, 0X0F, //7
};
//红色灯条显存
tWs2812bCache_TypeDef gWs2812bDat1[WS2812B_AMOUNT] = {
//R G B
0XFF, 0X00, 0X00, //0
0XFF, 0X00, 0X00, //1
0XFF, 0X00, 0X00, //2
0XFF, 0X00, 0X00, //3
0XFF, 0X00, 0X00, //4
0XFF, 0X00, 0X00, //5
0XFF, 0X00, 0X00, //6
0XFF, 0X00, 0X00, //7
0XFF, 0X00, 0X00, //0
0XFF, 0X00, 0X00, //1
0XFF, 0X00, 0X00, //2
0XFF, 0X00, 0X00, //3
0XFF, 0X00, 0X00, //4
0XFF, 0X00, 0X00, //5
0XFF, 0X00, 0X00, //6
0XFF, 0X00, 0X00, //7
};
//黄色灯条显存
tWs2812bCache_TypeDef gWs2812bDat2[WS2812B_AMOUNT] = {
//R G B
0XFF, 0XFF, 0X00, //0
0XFF, 0XFF, 0X00, //1
0XFF, 0XFF, 0X00, //2
0XFF, 0XFF, 0X00, //3
0XFF, 0XFF, 0X00, //4
0XFF, 0XFF, 0X00, //5
0XFF, 0XFF, 0X00, //6
0XFF, 0XFF, 0X00, //7
0XFF, 0XFF, 0X00, //0
0XFF, 0XFF, 0X00, //1
0XFF, 0XFF, 0X00, //2
0XFF, 0XFF, 0X00, //3
0XFF, 0XFF, 0X00, //4
0XFF, 0XFF, 0X00, //5
0XFF, 0XFF, 0X00, //6
0XFF, 0XFF, 0X00, //7
};
//绿色灯条显存
tWs2812bCache_TypeDef gWs2812bDat3[WS2812B_AMOUNT] = {
//R G B
0X00, 0XFF, 0X00, //0
0X00, 0XFF, 0X00, //1
0X00, 0XFF, 0X00, //2
0X00, 0XFF, 0X00, //3
0X00, 0XFF, 0X00, //4
0X00, 0XFF, 0X00, //5
0X00, 0XFF, 0X00, //6
0X00, 0XFF, 0X00, //7
0X00, 0XFF, 0X00, //0
0X00, 0XFF, 0X00, //1
0X00, 0XFF, 0X00, //2
0X00, 0XFF, 0X00, //3
0X00, 0XFF, 0X00, //4
0X00, 0XFF, 0X00, //5
0X00, 0XFF, 0X00, //6
0X00, 0XFF, 0X00, //7
};
//青绿色灯条显存
tWs2812bCache_TypeDef gWs2812bDat4[WS2812B_AMOUNT] = {
//R G B
0X00, 0XFF, 0XFF, //0
0X00, 0XFF, 0XFF, //1
0X00, 0XFF, 0XFF, //2
0X00, 0XFF, 0XFF, //3
0X00, 0XFF, 0XFF, //4
0X00, 0XFF, 0XFF, //5
0X00, 0XFF, 0XFF, //6
0X00, 0XFF, 0XFF, //7
0X00, 0XFF, 0XFF, //0
0X00, 0XFF, 0XFF, //1
0X00, 0XFF, 0XFF, //2
0X00, 0XFF, 0XFF, //3
0X00, 0XFF, 0XFF, //4
0X00, 0XFF, 0XFF, //5
0X00, 0XFF, 0XFF, //6
0X00, 0XFF, 0XFF, //7
};
//灭灯灯条显存
tWs2812bCache_TypeDef gWs2812bDat5[WS2812B_AMOUNT] = {
//R G B
0X00, 0X00, 0X00, //0
0X00, 0X00, 0X00, //1
0X00, 0X00, 0X00, //2
0X00, 0X00, 0X00, //3
0X00, 0X00, 0X00, //4
0X00, 0X00, 0X00, //5
0X00, 0X00, 0X00, //6
0X00, 0X00, 0X00, //7
0X00, 0X00, 0X00, //0
0X00, 0X00, 0X00, //1
0X00, 0X00, 0X00, //2
0X00, 0X00, 0X00, //3
0X00, 0X00, 0X00, //4
0X00, 0X00, 0X00, //5
0X00, 0X00, 0X00, //6
0X00, 0X00, 0X00, //7
};
void WS2812b_Set(uint16_t Ws2b812b_NUM, uint8_t r,uint8_t g,uint8_t b)
{
uint8_t *pR = &gWs2812bDat_SPI[88+(Ws2b812b_NUM) * 24 + 8];
uint8_t *pG = &gWs2812bDat_SPI[88+(Ws2b812b_NUM) * 24];
uint8_t *pB = &gWs2812bDat_SPI[88+(Ws2b812b_NUM) * 24 + 16];
for(uint8_t i = 0; i < 8; i++) {
if(g & 0x80) {
*pG = CODE_1;
}
else {
*pG = CODE_0;
}
if(r & 0x80) {
*pR = CODE_1;
}
else {
*pR = CODE_0;
}
if(b & 0x80) {
*pB = CODE_1;
}
else {
*pB = CODE_0;
}
r <<= 1;
g <<= 1;
b <<= 1;
pR++;
pG++;
pB++;
}
}
void WS2812B_Task(void)
{
uint8_t dat = 0;
for(int i=0;i<88;i++)
{
gWs2812bDat_SPI[i]=0;
}
//将gWs2812bDat数据解析成SPI数据
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat[iLED].R, gWs2812bDat[iLED].G, gWs2812bDat[iLED].B);
}
//总线输出数据
/* Send the reset command */
g_transfer_complete = false;
err = R_SCI_SPI_Write(&g_spi0_ctrl, gWs2812bDat_SPI, sizeof(gWs2812bDat_SPI), SPI_BIT_WIDTH_8_BITS);
assert(FSP_SUCCESS == err);
/* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
while ( g_transfer_complete==false)
{
;
}
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS);
}
void WS2812B_Task1(void)
{
uint8_t dat = 0;
for(int i=0;i<88;i++)
{
gWs2812bDat_SPI[i]=0;
}
//将gWs2812bDat数据解析成SPI数据
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat1[iLED].R, gWs2812bDat1[iLED].G, gWs2812bDat1[iLED].B);
}
//总线输出数据
/* Send the reset command */
g_transfer_complete = false;
err = R_SCI_SPI_Write(&g_spi0_ctrl, gWs2812bDat_SPI, sizeof(gWs2812bDat_SPI), SPI_BIT_WIDTH_8_BITS);
assert(FSP_SUCCESS == err);
/* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
// R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS);
}
void WS2812B_Task2(void)
{
uint8_t dat = 0;
for(int i=0;i<88;i++)
{
gWs2812bDat_SPI[i]=0;
}
//将gWs2812bDat数据解析成SPI数据
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat2[iLED].R, gWs2812bDat2[iLED].G, gWs2812bDat2[iLED].B);
}
//总线输出数据
/* Send the reset command */
g_transfer_complete = false;
err = R_SCI_SPI_Write(&g_spi0_ctrl, gWs2812bDat_SPI, sizeof(gWs2812bDat_SPI), SPI_BIT_WIDTH_8_BITS);
assert(FSP_SUCCESS == err);
/* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
//R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS);
}
void WS2812B_Task3(void)
{
uint8_t dat = 0;
for(int i=0;i<88;i++)
{
gWs2812bDat_SPI[i]=0;
}
//将gWs2812bDat数据解析成SPI数据
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat3[iLED].R, gWs2812bDat3[iLED].G, gWs2812bDat3[iLED].B);
}
//总线输出数据
/* Send the reset command */
g_transfer_complete = false;
err = R_SCI_SPI_Write(&g_spi0_ctrl, gWs2812bDat_SPI, sizeof(gWs2812bDat_SPI), SPI_BIT_WIDTH_8_BITS);
assert(FSP_SUCCESS == err);
/* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
// R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS);
}
void WS2812B_Task4(void)
{
uint8_t dat = 0;
for(int i=0;i<88;i++)
{
gWs2812bDat_SPI[i]=0;
}
//将gWs2812bDat数据解析成SPI数据
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat4[iLED].R, gWs2812bDat4[iLED].G, gWs2812bDat4[iLED].B);
}
//总线输出数据
/* Send the reset command */
g_transfer_complete = false;
err = R_SCI_SPI_Write(&g_spi0_ctrl, gWs2812bDat_SPI, sizeof(gWs2812bDat_SPI), SPI_BIT_WIDTH_8_BITS);
assert(FSP_SUCCESS == err);
/* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
// R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS);
}
void WS2812B_Task5(void)
{
uint8_t dat = 0;
for(int i=0;i<88;i++)
{
gWs2812bDat_SPI[i]=0;
}
//将gWs2812bDat数据解析成SPI数据
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat5[iLED].R, gWs2812bDat5[iLED].G, gWs2812bDat5[iLED].B);
}
//总线输出数据
/* Send the reset command */
g_transfer_complete = false;
err = R_SCI_SPI_Write(&g_spi0_ctrl, gWs2812bDat_SPI, sizeof(gWs2812bDat_SPI), SPI_BIT_WIDTH_8_BITS);
assert(FSP_SUCCESS == err);
/* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
// R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS);
}
bsp_ws2812LED.h
#ifndef BSP_WS2812LED_H_
#defineBSP_WS2812LED_H_
#include<stdint.h>
// 编码 0 : 11000000
#defineCODE_0 0xc0
// 编码 1 : 11111000
#defineCODE_1 0xF8
/*ws2812b灯珠数量*/
#defineWS2812B_AMOUNT 16
typedefstruct
{
uint8_tR;
uint8_tG;
uint8_tB;
}tWs2812bCache_TypeDef;
externtWs2812bCache_TypeDef gWs2812bDat[WS2812B_AMOUNT];
externtWs2812bCache_TypeDef gWs2812bDat1[WS2812B_AMOUNT];
externtWs2812bCache_TypeDef gWs2812bDat2[WS2812B_AMOUNT];
externtWs2812bCache_TypeDef gWs2812bDat3[WS2812B_AMOUNT];
externtWs2812bCache_TypeDef gWs2812bDat4[WS2812B_AMOUNT];
externtWs2812bCache_TypeDef gWs2812bDat5[WS2812B_AMOUNT];
voidWS2812b_Set(uint16_tWs2b812b_NUM,uint8_tr,uint8_tg,uint8_tb);
voidWS2812B_Task(void);
voidWS2812B_Task1(void);
voidWS2812B_Task2(void);
voidWS2812B_Task3(void);
voidWS2812B_Task4(void);
voidWS2812B_Task5(void);
#endif/* WS2812_H_ */
主函数如下:
hal_entry.c
#include "hal_data.h"
#include "ws2812LED/bsp_ws2812LED.h"
#include "debug_uart/bsp_debug_uart.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
fsp_err_t err = FSP_SUCCESS;
volatile bool g_transfer_complete = false;
void sci_spi_callback (spi_callback_args_t * p_args)
{
if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
{
g_transfer_complete = true;
}
}
extern tWs2812bCache_TypeDef gWs2812bDat[WS2812B_AMOUNT];
void move_Front()
{
uint8_t i;
uint8_t temp[3];
temp[0] = gWs2812bDat[0].R;
temp[1] = gWs2812bDat[0].G;
temp[2] = gWs2812bDat[0].B;
for (i = 0; i < WS2812B_AMOUNT-1; i++)
{
gWs2812bDat[i].R = gWs2812bDat[i+1].R;
gWs2812bDat[i].G = gWs2812bDat[i+1].G;
gWs2812bDat[i].B = gWs2812bDat[i+1].B;
}
gWs2812bDat[15].R = temp[0];
gWs2812bDat[15].G = temp[1];
gWs2812bDat[15].B = temp[2];
}
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
err = R_SCI_SPI_Open(&g_spi0_ctrl, &g_spi0_cfg);
assert(FSP_SUCCESS == err);
sci_spi_extended_cfg_t sci_spi_extended_cfg_t1;
WS2812B_Task();
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS);
Debug_UART9_Init();
while (1)
{
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
/*******************************************************************************************************************//**
* This function is called at various points during the startup process. This implementation uses the event that is
* called right before main() to set up the pins.
*
* @param[in] event Where at in the start up process the code is currently at
**********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event)
{
if (BSP_WARM_START_RESET == event)
{
#if BSP_FEATURE_FLASH_LP_VERSION != 0
/* Enable reading from data flash. */
R_FACI_LP->DFLCTL = 1U;
/* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
* C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
}
if (BSP_WARM_START_POST_C == event)
{
/* C runtime environment and system clocks are setup. */
/* Configure pins. */
R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
}
}
#if BSP_TZ_SECURE_BUILD
FSP_CPP_HEADER
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();
/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{
}
FSP_CPP_FOOTER
#endif
附件为工程文件*附件:ws2812.zip
更多回帖