lpc55s16 usart read related issue

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

lpc55s16 usart read related issue

350 次查看
firmwareteam
Contributor I

Hello ,

i am using lpc55s16 usart interrupt sample code in this code i can read 16 bytes data correct and i want to read more than 16 bytes but i got some garbage value while print in console 

i change DEMO_RING_BUFFER_SIZE  also but still getting same data

can you guide me how to read and print correct data on console .

 

my code :

/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "pin_mux.h"
#include "board.h"
#include "fsl_usart.h"

#include <stdbool.h>
#include "fsl_power.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define DEMO_USART USART0
#define DEMO_USART_CLK_SRC kCLOCK_Flexcomm0
#define DEMO_USART_CLK_FREQ CLOCK_GetFlexCommClkFreq(0U)
#define DEMO_USART_IRQHandler FLEXCOMM0_IRQHandler
#define DEMO_USART_IRQn FLEXCOMM0_IRQn

/*! @brief Ring buffer size (Unit: Byte). */
#define DEMO_RING_BUFFER_SIZE 16

/*! @brief Ring buffer to save received data. */

/*******************************************************************************
* Prototypes
******************************************************************************/

/*******************************************************************************
* Variables
******************************************************************************/

uint8_t g_tipString[] =
"Usart functional API interrupt example\r\nBoard receives characters then sends them out\r\nNow please input:\r\n";

/*
Ring buffer for data input and output, in this example, input data are saved
to ring buffer in IRQ handler. The main function polls the ring buffer status,
if there are new data, then send them out.
Ring buffer full: (((rxIndex + 1) % DEMO_RING_BUFFER_SIZE) == txIndex)
Ring buffer empty: (rxIndex == txIndex)
*/
uint8_t demoRingBuffer[DEMO_RING_BUFFER_SIZE];
volatile uint16_t txIndex; /* Index of the data to send out. */
volatile uint16_t rxIndex; /* Index of the memory to save new arrived data. */

/*******************************************************************************
* Code
******************************************************************************/

void DEMO_USART_IRQHandler(void)
{
uint8_t data;

/* If new data arrived. */
if ((kUSART_RxFifoNotEmptyFlag | kUSART_RxError) & USART_GetStatusFlags(DEMO_USART))
{
data = USART_ReadByte(DEMO_USART);
/* If ring buffer is not full, add data to ring buffer. */
if (((rxIndex + 1) % DEMO_RING_BUFFER_SIZE) != txIndex)
{
demoRingBuffer[rxIndex] = data;
rxIndex++;
rxIndex %= DEMO_RING_BUFFER_SIZE;

}
}
//printf("%s \n",demoRingBuffer);
SDK_ISR_EXIT_BARRIER;
}

/*!
* @brief Main function
*/
int main(void)
{
usart_config_t config;

/* set BOD VBAT level to 1.65V */
POWER_SetBodVbatLevel(kPOWER_BodVbatLevel1650mv, kPOWER_BodHystLevel50mv, false);
/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();

/*
* config.baudRate_Bps = 115200U;
* config.parityMode = kUSART_ParityDisabled;
* config.stopBitCount = kUSART_OneStopBit;
* config.loopback = false;
* config.enableTxFifo = false;
* config.enableRxFifo = false;
*/
USART_GetDefaultConfig(&config);
config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
config.enableTx = true;
config.enableRx = true;

USART_Init(DEMO_USART, &config, DEMO_USART_CLK_FREQ);

/* Send g_tipString out. */
USART_WriteBlocking(DEMO_USART, g_tipString, (sizeof(g_tipString) / sizeof(g_tipString[0])) - 1);

/* Enable RX interrupt. */
USART_EnableInterrupts(DEMO_USART, kUSART_RxLevelInterruptEnable | kUSART_RxErrorInterruptEnable);
EnableIRQ(DEMO_USART_IRQn);

while (1)
{
/* Send data only when USART TX register is empty and ring buffer has data to send out. */
while ((kUSART_TxFifoNotFullFlag & USART_GetStatusFlags(DEMO_USART)) && (rxIndex != txIndex))
{
USART_WriteByte(DEMO_USART, demoRingBuffer[txIndex]);
txIndex++;
txIndex %= DEMO_RING_BUFFER_SIZE;

}
}
}

BR

Dharm

 

0 项奖励
回复
0 回复数