配置串口相关参数stack
配置串口相关接口参数
配置串口硬件引脚
配置时钟
增加串口函数处理
bsp_debug_uart.c
#include"bsp_debug_uart.h"
#include"ws2812LED/bsp_ws2812LED.h"
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:
{
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;
}
}
#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
#include "bsp_ws2812LED.h"
#include "hal_data.h"
extern fsp_err_t err ;
extern volatile bool g_transfer_complete ;
uint8_t gWs2812bDat_SPI[WS2812B_AMOUNT * 24+88] = {0};
tWs2812bCache_TypeDef gWs2812bDat[WS2812B_AMOUNT] = {
0XFF, 0X00, 0X00,
0X00, 0XFF, 0X00,
0X00, 0X00, 0XFF,
0X00, 0XFF, 0XFF,
0XFF, 0X00, 0XFF,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0XFF,
0X0F, 0X0F, 0X0F,
0XFF, 0X00, 0X00,
0X00, 0XFF, 0X00,
0X00, 0X00, 0XFF,
0X00, 0XFF, 0XFF,
0XFF, 0X00, 0XFF,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0XFF,
0X0F, 0X0F, 0X0F,
};
tWs2812bCache_TypeDef gWs2812bDat1[WS2812B_AMOUNT] = {
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
0XFF, 0X00, 0X00,
};
tWs2812bCache_TypeDef gWs2812bDat2[WS2812B_AMOUNT] = {
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
0XFF, 0XFF, 0X00,
};
tWs2812bCache_TypeDef gWs2812bDat3[WS2812B_AMOUNT] = {
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
0X00, 0XFF, 0X00,
};
tWs2812bCache_TypeDef gWs2812bDat4[WS2812B_AMOUNT] = {
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
0X00, 0XFF, 0XFF,
};
tWs2812bCache_TypeDef gWs2812bDat5[WS2812B_AMOUNT] = {
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
0X00, 0X00, 0X00,
};
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;
}
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat[iLED].R, gWs2812bDat[iLED].G, gWs2812bDat[iLED].B);
}
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);
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;
}
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat1[iLED].R, gWs2812bDat1[iLED].G, gWs2812bDat1[iLED].B);
}
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);
}
void WS2812B_Task2(void)
{
uint8_t dat = 0;
for(int i=0;i<88;i++)
{
gWs2812bDat_SPI[i]=0;
}
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat2[iLED].R, gWs2812bDat2[iLED].G, gWs2812bDat2[iLED].B);
}
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);
}
void WS2812B_Task3(void)
{
uint8_t dat = 0;
for(int i=0;i<88;i++)
{
gWs2812bDat_SPI[i]=0;
}
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat3[iLED].R, gWs2812bDat3[iLED].G, gWs2812bDat3[iLED].B);
}
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);
}
void WS2812B_Task4(void)
{
uint8_t dat = 0;
for(int i=0;i<88;i++)
{
gWs2812bDat_SPI[i]=0;
}
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat4[iLED].R, gWs2812bDat4[iLED].G, gWs2812bDat4[iLED].B);
}
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);
}
void WS2812B_Task5(void)
{
uint8_t dat = 0;
for(int i=0;i<88;i++)
{
gWs2812bDat_SPI[i]=0;
}
for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
{
WS2812b_Set(iLED, gWs2812bDat5[iLED].R, gWs2812bDat5[iLED].G, gWs2812bDat5[iLED].B);
}
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);
}
bsp_ws2812LED.h
#ifndef BSP_WS2812LED_H_
#defineBSP_WS2812LED_H_
#include<stdint.h>
#defineCODE_0 0xc0
#defineCODE_1 0xF8
#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