/*----------------------------------------------------------------------------- * Name: UART_FRDM-KW40Z.c * Purpose: Buttons interface for FRDM-KW40Z evaluation board * Rev.: 1.00 *----------------------------------------------------------------------------*/ /* Copyright (c) 2015 ARM LIMITED All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - 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. - Neither the name of ARM nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * 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 COPYRIGHT HOLDERS AND 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 "MKW40Z4.h" #include "Board_UART.h" #include #include "stdio.h" static const uint8_t *s_pUART0_TxData = NULL; static uint32_t s_UART0_TxPos = 0U; static uint32_t s_UART0_TxCount = 0U; static uint8_t *s_pUART0_RxBuffer = NULL; static uint32_t s_UART0_RxPos = 0U; static uint32_t s_UART0_RxCount = 0U; int iflag = 0; void UART0_Init(void) { SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK; //PORTC SIM->SCGC5 |= SIM_SCGC5_LPUART0_MASK ; // LPUART 0 //Use OSCERCLK (32 Mhz) for UART0 Clock Source SIM->SOPT2 = (SIM->SOPT2 & ~SIM_SOPT2_LPUART0SRC_MASK) | SIM_SOPT2_LPUART0SRC(2); //Configure PTC6 & PTC7 as UART0 Rx/Tx PORTC->PCR[6] = PORT_PCR_MUX(4U); PORTC->PCR[7] = PORT_PCR_MUX(4U); //NVIC->ICER[0] = 1U <<((INT_UART0 - 16U) & 31U); NVIC->ISER[0] = 1U <<(LPUART0_IRQn & 31U); } void UART0_Startup(void) { PORTC->PCR[6] = PORT_PCR_MUX(4U); PORTC->PCR[7] = PORT_PCR_MUX(4U); //LPUART0_CTRL = 0; //Disable LPUART0_BAUD = LPUART_BAUD_OSR(14U - 1U) | LPUART_BAUD_SBR(20U); // 115200 LPUART0_CTRL = LPUART_CTRL_RIE_MASK | LPUART_CTRL_TE_MASK | LPUART_CTRL_RE_MASK; // Rx int, Tx On, Rx On NVIC->ICPR[0] = 1U <<(LPUART0_IRQn & 31U); NVIC->ISER[0] = 1U <<(LPUART0_IRQn & 31U); } void UART0_Tx(const uint8_t pData[], uint32_t count) { __disable_irq(); s_pUART0_TxData = pData; s_UART0_TxPos = 0U; s_UART0_TxCount = count; LPUART0_CTRL |= LPUART_CTRL_TIE_MASK; __enable_irq(); } void UART0_Rx(uint8_t pBuffer[], uint32_t count) { __disable_irq(); s_pUART0_RxBuffer = pBuffer; LPUART0_CTRL |= LPUART_CTRL_RIE_MASK; __enable_irq(); } void UART0_Flush(void) { for(;;) { if(s_pUART0_TxData == NULL) { if((LPUART0_STAT & LPUART_STAT_TC_MASK) != 0U) { break; } } } } void UART0_Resume(void) { PORTC->PCR[6] = PORT_PCR_MUX(4U); PORTC->PCR[7] = PORT_PCR_MUX(4U); } void LPUART0_IRQHandler(void) { uint32_t stat = LPUART0_STAT; uint32_t ctrl = LPUART0_CTRL; // Detect Receive Data ready if(((stat & LPUART_STAT_RDRF_MASK) != 0U) && //Rx Ready ((ctrl & LPUART_CTRL_RIE_MASK) != 0U)) // RIE Enabled { uint8_t rx = (uint8_t)LPUART0_DATA; if(rx != NULL) { s_pUART0_RxBuffer[s_UART0_RxPos] = rx; s_UART0_RxPos += 1U; UART0_Resume(); if((int)rx == 0x3B) { s_pUART0_RxBuffer[s_UART0_RxPos] = '\0'; s_pUART0_RxBuffer = NULL; s_UART0_RxPos = 0; iflag = 1; } } } // Detect Transmitter Ready if(((stat & LPUART_STAT_TDRE_MASK) != 0U) && //Tx Ready ((ctrl & LPUART_CTRL_TIE_MASK) != 0U)) // TIE Enabled { if(s_UART0_TxPos == s_UART0_TxCount) { LPUART0_CTRL &= ~LPUART_CTRL_TIE_MASK; s_pUART0_TxData = NULL; } else { LPUART0_DATA = (uint32_t)'A'; //s_pUART0_TxData[s_UART0_TxPos]; s_UART0_TxPos += 1U; } } }