/******************************************************************************* * DISCLAIMER * This software is supplied by Renesas Electronics Corporation and is only * intended for use with Renesas products. No other uses are authorized. This * software is owned by Renesas Electronics Corporation and is protected under * all applicable laws, including copyright laws. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * Renesas reserves the right, without notice, to make changes to this software * and to discontinue the availability of this software. By using this software, * you agree to the additional terms and conditions found by accessing the * following link: * http://www.renesas.com/disclaimer * (c) 2014 Renesas Electronics Corporation All rights reserved. *******************************************************************************/ /****************************************************************************** * File Name : r_uart.c * Version : 1.0 * Device(s) : R7F701007 RH850/F1L * Tool-Chain : IAR Embedded Workbench 1.30.1 * Description : This file is a sample of the UART (RLIN3). * Operation : - ******************************************************************************* ******************************************************************************* * History : DD.MM.YYYY Version Description * : 20.03.2014 1.00 First Release ******************************************************************************/ /****************************************************************************** Includes ******************************************************************************/ #include "r_typedefs.h" #include "iodefine.h" #ifdef __ghs__ #include "device.h" #endif #ifdef __IAR_SYSTEMS_ICC__ #include #endif /****************************************************************************** Imported global variables and functions (from other files) ******************************************************************************/ /****************************************************************************** Macro definitions ******************************************************************************/ /****************************************************************************** Exported global variables and functions (to be accessed by other files) ******************************************************************************/ void R_UART_Init( void ); /****************************************************************************** Private global variables and functions ******************************************************************************/ /****************************************************************************** * Function Name : void R_UART_Init( void ) * Description : This function initialize UART. (RLIN3-UART) * Argument : none * Return Value : none ******************************************************************************/ void R_UART_Init( void ) { uint32_t reg32_value; /* Source Clock Setting for C_ISO_LIN CKSC_ILINS_CTL - C_ISO_LIN Source Clock Selection Register b31:b 2 - Reserved set to 0 b 1:b 0 ILINSCSID - Source Clock Setting for C_ISO_LIN - CPUCLK2 */ do { reg32_value = 0x00000001UL; PROTCMD1 = 0x000000A5UL; /* Protection release the CKSC_ILINS_CTL register */ CKSC_ILINS_CTL = reg32_value; CKSC_ILINS_CTL = ~reg32_value; CKSC_ILINS_CTL = reg32_value; } while ( PROTS1 != 0x00000000UL ); while ( CKSC_ILINS_ACT != reg32_value ) { /* Wait for CKSC_ILINS_CTL to set. */ } /* Clock Divider Setting for C_ISO_LIN CKSC_ILIND_CTL - C_ISO_LIN Divided Clock Selection Register b31:b 2 - Reserved set to 0 b 1:b 0 ILINDCSID - Source Clock Setting for C_ISO_LIN - CKSC_ILINS_CTL selection / 1 */ do { reg32_value = 0x00000001UL; PROTCMD1 = 0x000000A5UL; /* Protection release the CKSC_ILIND_CTL register */ CKSC_ILIND_CTL = reg32_value; CKSC_ILIND_CTL = ~reg32_value; CKSC_ILIND_CTL = reg32_value; } while ( PROTS1 != 0x00000000UL ); while ( CKSC_ILIND_ACT != reg32_value ) { /* Wait for CKSC_ILIND_CTL to set. */ } /* Alternative function port settings */ /* RLIN33RX(P1_0:CN2B-55pin) 1'st Alternative (input) */ /* RLIN33TX(P1_1:CN2B-56pin) 1'st Alternative (output) */ PFC1 &= 0xFFFCU; /* 1'st Alternative function (input/output), must be set to 0 */ PM1 &= 0xFFFDU; /* 1'st Alternative function (input), must be set to 1 */ /* 1'st Alternative function (output), must be set to 0 */ PMC1 |= 0x0003U; /* 1'st Alternative function (input/output), must be set to 1 */ /* Set the interrupt flags */ MKRLIN33UR0 = 1U; RFRLIN33UR0 = 0U; TBRLIN33UR0 = 0U; MKRLIN33UR1 = 1U; RFRLIN33UR1 = 0U; TBRLIN33UR1 = 0U; MKRLIN33UR2 = 1U; RFRLIN33UR2 = 0U; TBRLIN33UR2 = 0U; /* Choose the shift to a reset mode. RLN3nLCUC - UART Control Register b7:b1 - Reserved set to 0 b0 OM0 - LIN Reset - LIN reset mode is caused */ RLN33.LCUC = 0x00U; while ( (RLN33.LMST & 0x01U) != 0x00U ) { /* Wait for LIN reset mode. */ } /* Select the UART interface mode. RLN3nLMD - UART Mode Register b7:b6 - Reserved set to 0 b5 LRDNFS - LIN Reception Data Noise Filtering Disable - Noise filter is enabled b4:b2 - Reserved set to 0 b1:b0 LMD - LIN/UART Mode Select - UART mode */ RLN33.LMD = 0x01U; /* Select the bit sampling count and prescaler clock. RLN3nLWBR - LIN Wake-Up Baud Rate Select Register b7:b4 NSPB - Bit Sampling Count Select - 16 sampling b3:b1 LPRS - Prescaler Clock Select - 1/1 b0 - Reserved set to 0 */ RLN33.LWBR = 0xF0U; /* Set the baud rate prescaler RLN3nLBRP01 - UART Baud Rate Prescaler 01 Reg b15:b0 BRP - The formula for baud rate is described below. Baud rate for UART = { Frequency of LIN communication clock source} * (RLN3nLWBR.LPRS[2:0] selection clock) / (RLN3nLBRP01 + 1) / the number of selected NSPB[3:0] [bps] = 40MHz * ( 1 / 1 ) / ( 21 + 1 ) / 16 = 11,5200(11,363) */ RLN33.LBRP01.UINT16 = 21U; /* Enables or disables detection of the framing error , overrun error and bit error. RLN3nLEDE - UART Error Detection Enable Register b7:b4 - Reserved set to 0 b3 FERE - Framing Error Detection Enable - Enables framing error detection b2 OERE - Overrun Error Detection Enable - Enables overrun error detection b1 - Reserved set to 0 b0 BERE - Bit Error Detection Enable - Enables bit error detection */ RLN33.LEDE = 0x0DU; /* Enables or disables detection of the framing error , overrun error and bit error. RLN3nLBFC - UART Configuration Register b7 - Reserved set to 0 b6 UTPS - UART Output Polarity Switch - Transmit data normal output b5 URPS - UART Input Polarity Switch - Reception data normal output b4:b3 UPS - UART Parity Select - Even Parity b2 USBLS - UART Stop Bit length Select - Stop bit:1 bit b1 UBOS - UART Transfer Format Order Select - LSB First b0 UBLS - UART Character Length Select - UART 8 bits communication */ RLN33.LBFC = 0x08U; /* Choose the cancellation of the reset mode. RLN3nLCUC - UART Control Register b7:b1 - Reserved set to 0 b0 OM0 - LIN Reset - LIN reset mode is canceled */ RLN33.LCUC = 0x01U; while ( (RLN33.LMST & 0x01U) != 0x01U ) { /* Wait for cancellation of the LIN reset mode. */ } /* Set the interrupt flags */ MKRLIN33UR1 = 0U; /* Enable INTRLIN33UR1 interrupt */ MKRLIN33UR2 = 0U; /* Enable INTRLIN33UR2 interrupt */ /* Enables reception and transmission RLN3nLUOER - UART Operation Enable Register b7:b2 - Reserved set to 0 b1 UROE - Reception Enable - Enables reception b0 UTOE - Transmission Enable - Enables transmission */ RLN33.LUOER = 0x03U; }