When I type "0", I print out hello I want to print out the world when I input "1".

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

When I type "0", I print out hello I want to print out the world when I input "1".

Jump to solution
1,934 Views
hyeonsunjang
Contributor I

I'd like to use this example to solve it.

This is an example of lpuart, and I want to solve it using interrupt method.

I am using keil uVisun5 program
I am using mpas-ks22.

/*
* The Clear BSD License
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted (subject to the limitations in the disclaimer below) provided
* that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "board.h"
#include "fsl_lpuart.h"
#include "fsl_device_registers.h"
#include "pin_mux.h"
#include "clock_config.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/* UART instance and clock */
#define DEMO_LPUART LPUART0
#define DEMO_LPUART_CLKSRC kCLOCK_PllFllSelClk
#define DEMO_LPUART_CLK_FREQ CLOCK_GetFreq(kCLOCK_PllFllSelClk)
#define DEMO_LPUART_IRQn LPUART0_IRQn
#define DEMO_LPUART_IRQHandler LPUART0_IRQHandler


/*! @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[] =
"Lpuart 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_LPUART_IRQHandler(void)
{
uint8_t data;

/* If new data arrived. */
if ((kLPUART_RxDataRegFullFlag)&LPUART_GetStatusFlags(DEMO_LPUART))
{
data = LPUART_ReadByte(DEMO_LPUART);

/* 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;
}
}
/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
exception return operation might vector to incorrect interrupt */
#if defined __CORTEX_M && (__CORTEX_M == 4U)
__DSB();
#endif
}

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

BOARD_InitPins();
BOARD_BootClockHSRUN();

CLOCK_SetLpuart0Clock(0x1U); /* PLLFLLSEL is used as LPUART */

/*
* config.baudRate_Bps = 115200U;
* config.parityMode = kLPUART_ParityDisabled;
* config.stopBitCount = kLPUART_OneStopBit;
* config.txFifoWatermark = 0;
* config.rxFifoWatermark = 0;
* config.enableTx = false;
* config.enableRx = false;
*/
LPUART_GetDefaultConfig(&config);
config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
config.enableTx = true;
config.enableRx = true;

LPUART_Init(DEMO_LPUART, &config, DEMO_LPUART_CLK_FREQ);

/* Send g_tipString out. */
LPUART_WriteBlocking(DEMO_LPUART, g_tipString, sizeof(g_tipString) / sizeof(g_tipString[0]));

/* Enable RX interrupt. */
LPUART_EnableInterrupts(DEMO_LPUART, kLPUART_RxDataRegFullInterruptEnable);
EnableIRQ(DEMO_LPUART_IRQn);

while (1)
{
/* Send data only when LPUART TX register is empty and ring buffer has data to send out. */
while ((kLPUART_TxDataRegEmptyFlag & LPUART_GetStatusFlags(DEMO_LPUART)) && (rxIndex != txIndex))
{
LPUART_WriteByte(DEMO_LPUART, demoRingBuffer[txIndex]);
txIndex++;
txIndex %= DEMO_RING_BUFFER_SIZE;
}
}
}

hyeonsunjang_0-1671780456991.png

 

0 Kudos
1 Solution
1,920 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi hyeonsunjang,

The example Send data only when LPUART TX register is empty and ring buffer has data to send out.
Please add code to check the value of the received data in main, and then send "hello" or "world" based on the received value.

For example:
if(demoRingBuffer[0]==0x30)LPUART_WriteBlocking(DEMO_LPUART, hello_tipString, sizeof(hello_tipString) / sizeof(hello_tipString[0]));
else LPUART_WriteBlocking(DEMO_LPUART, world_tipString, sizeof(world_tipString) / sizeof(world_tipString[0]));

#define DEMO_RING_BUFFER_SIZE 1
uint8_t hello_tipString[] ="hello";
uint8_t world_tipString[] ="world";


Best Regards,
Robin
-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

View solution in original post

2 Replies
1,921 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi hyeonsunjang,

The example Send data only when LPUART TX register is empty and ring buffer has data to send out.
Please add code to check the value of the received data in main, and then send "hello" or "world" based on the received value.

For example:
if(demoRingBuffer[0]==0x30)LPUART_WriteBlocking(DEMO_LPUART, hello_tipString, sizeof(hello_tipString) / sizeof(hello_tipString[0]));
else LPUART_WriteBlocking(DEMO_LPUART, world_tipString, sizeof(world_tipString) / sizeof(world_tipString[0]));

#define DEMO_RING_BUFFER_SIZE 1
uint8_t hello_tipString[] ="hello";
uint8_t world_tipString[] ="world";


Best Regards,
Robin
-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

1,902 Views
hyeonsunjang
Contributor I

Thank you very much for your reply.

However,I tried as you taught me, but the code didn't work properly.

And I don't understand why "demoRingBuffer[0]" goes into if (demoRingBuffer[0]==0x30). I'd appreciate it if you let me know.

#include "board.h"
#include "fsl_lpuart.h"
#include "fsl_device_registers.h"
#include "pin_mux.h"
#include "clock_config.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/* UART instance and clock */
#define DEMO_LPUART LPUART0
#define DEMO_LPUART_CLKSRC kCLOCK_PllFllSelClk
#define DEMO_LPUART_CLK_FREQ CLOCK_GetFreq(kCLOCK_PllFllSelClk)
#define DEMO_LPUART_IRQn LPUART0_IRQn
#define DEMO_LPUART_IRQHandler LPUART0_IRQHandler


/*! @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[] =
"Lpuart functional API interrupt example\r\nBoard receives characters then sends them out\r\nNow please input:\r\n";

uint8_t hello_tipString[] ="hello";
uint8_t world_tipString[] ="world";

/*
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_LPUART_IRQHandler(void)
{
uint8_t data;

/* If new data arrived. */
if ((kLPUART_RxDataRegFullFlag)&LPUART_GetStatusFlags(DEMO_LPUART))
{
data = LPUART_ReadByte(DEMO_LPUART);

/* 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;
}
}
/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
exception return operation might vector to incorrect interrupt */
#if defined __CORTEX_M && (__CORTEX_M == 4U)
__DSB();
#endif
}

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

BOARD_InitPins();
BOARD_BootClockHSRUN();

CLOCK_SetLpuart0Clock(0x1U); /* PLLFLLSEL is used as LPUART */

/*
* config.baudRate_Bps = 115200U;
* config.parityMode = kLPUART_ParityDisabled;
* config.stopBitCount = kLPUART_OneStopBit;
* config.txFifoWatermark = 0;
* config.rxFifoWatermark = 0;
* config.enableTx = false;
* config.enableRx = false;
*/
LPUART_GetDefaultConfig(&config);
config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
config.enableTx = true;
config.enableRx = true;

LPUART_Init(DEMO_LPUART, &config, DEMO_LPUART_CLK_FREQ);

/* Send g_tipString out. */
LPUART_WriteBlocking(DEMO_LPUART, g_tipString, sizeof(g_tipString) / sizeof(g_tipString[0]));

/* Enable RX interrupt. */
LPUART_EnableInterrupts(DEMO_LPUART, kLPUART_RxDataRegFullInterruptEnable);
EnableIRQ(DEMO_LPUART_IRQn);

while (1)
{
/* Send data only when LPUART TX register is empty and ring buffer has data to send out. */
while ((kLPUART_TxDataRegEmptyFlag & LPUART_GetStatusFlags(DEMO_LPUART)) && (rxIndex != txIndex))
{
LPUART_WriteByte(DEMO_LPUART, demoRingBuffer[txIndex]);
txIndex++;
txIndex %= DEMO_RING_BUFFER_SIZE;

if(demoRingBuffer[0]==0x30)LPUART_WriteBlocking(DEMO_LPUART, hello_tipString, sizeof(hello_tipString) / sizeof(hello_tipString[0]));
else LPUART_WriteBlocking(DEMO_LPUART, world_tipString, sizeof(world_tipString) / sizeof(world_tipString[0]));
}
}
}

hyeonsunjang_0-1672039342157.png

 

0 Kudos