📄 lin_hdw.c
字号:
/******************************************************************************/
/* Renesas Technology America, Inc. Legal Disclaimer */
/******************************************************************************/
/* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY */
/* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR */
/* PURPOSE. */
/* */
/* BY USING THE SOFTWARE HEREIN, YOU (AND YOUR COMPANY) AGREE TO BE BOUND BY */
/* THE TERMS AND CONDITIONS OF RENESAS TECHNOLOGY AMERICA, INC.'S SOURCE CODE */
/* LICENSE AGREEMENT. PLEASE READ THIS AGREEMENT CAREFULLY. IF YOU (OR YOUR */
/* COMPANY) DO NOT AGREE TO ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT, */
/* DO NOT INSTALL OR USE THIS SOFTWARE AND ASSOCIATED DOCUMENTATION. */
/* */
/* Copyright (c) 2003, 2004, 2005 and 2006 Renesas Technology America, Inc. */
/******************************************************************************/
/**********************************************************************
Title : lin_hdw.c
Module Description : This file contains the MCU specific and hardware
specific variable declarations and initializations
for the LIN device.
MCU Family : R8C
Author : Bob Chamberlain
Version : LIN API 2.0 Version 1.1
*********************************************************************/
/**********************************************************************
* Include files
*********************************************************************/
#include "lin_api.h"
#include "rskR8C23def.h"
/**********************************************************************
* Include User Header file
*********************************************************************/
/**********************************************************************
* Constant and Macro Definitions using #define
*********************************************************************/
/**********************************************************************
* Enumerations, Structures and Typedefs
*********************************************************************/
/**********************************************************************
* Global Variable extern Declarations
*********************************************************************/
/* Set a default value for the count used to detect a valid wake-up period.
* This threshold value of 150us detects a wakeup pulse of 250us minimum
* even for an uncalibrated Baudrate, given the values defined in the file
* "lin_hdw.h" for LIN_TIMER_RATE and LIN_MY_FREQUENCY. The break timer
* is used to detect the wake-up period. The value 14 is loaded into the
* timer register to count 15 periods of the prescaler. The count calculated
* below generates a 10us period for the prescaler. This count is
* calculated as follows:
*
* count = ((10 * LIN_MY_FREQUENCY) / LIN_TIMER_RATE) - 1)
*
* */
unsigned char LIN_wake_up_period = 99;
/* Set a default value for the count to be loaded into the break timer
* to generate a break pulse (Master) or to detect a break pulse (Slave).
* For a Master node, this count must equal 13 bit periods of the LIN
* serial port. For a Slave node, this count must equal 11 bit periods
* of the LIN serial port. The total count is broken up into two values:
* one that is loaded into the timer register and one that is loaded into
* the prescaler. In order to achieve the maximum possible accuracy, each
* LIN bit period is broken up into some integer number of "slices",
* (usually 4 for a 19200Baud bit rate). The break timer register is
* loaded with the number of bits (13 or 11) times the number of "slices",
* all minus 1. The prescaler is loaded with a value to produce a period
* equal to the LIN bit period divided by the number of slices. The code
* for AUTO_CALCULATION will calculate the proper values if enabled by
* uncommenting this option in "lin_dev.h" - at least for Baud rates of
* 19200, 9600 and 4800 with system clock frequencies of 20MHz through
* 4MHz (in even 2MHz steps). If AUTO_CALCULATION is enabled, any reasonable
* values may be entered below, since they will never be used anyway. If
* AUTO_CALCULATION is not enabled or if your system is using some odd Baud
* rate or system clock frequency, the correct values must be calculated as
* outlined below. The first step is to calculate the BIT_FACTOR, the number
* of slices in a bit period. This is an integer value calculated as follows:
*
* BIT_FACTOR = (76800 / LIN_MY_SPEED)
*
* Round BIT_FACTOR to the nearest integer.
*
* The count to be loaded into the break timer prescaler register is nr of clock pulses
* per slice (BIT_FACTOR), "LIN_break_prescale" is calculated as follows:
*
* prescale = (((LIN_MY_FREQUENCY*10**6)/(LIN_SPEED*LIN_TIMER_RATE*BIT_FACTOR)) - 1)
*
* The count to be loaded into the * break timer register, "LIN_break_count",
* is then calculated as:
*
* LIN_break_count = ("break period in bits"*BIT_FACTOR - 1)
*
* where the "break period in bits" is 13 for a Master node and 11 for a Slave
* node.
*
* The calculated value should be rounded to the nearest integer before subtracting
* the final 1.
*
* */
#if (LIN_MASTER_NODE == LIN_MY_NODE_NAME)
unsigned char LIN_break_count = 51; /* Count for the Master break period. */
unsigned char LIN_break_prescale = 129; /* Prescale value for Master */
#else /* 129 for 20 MHz Xtal and div by 1 */
unsigned char LIN_break_count = 43; /* Count for the Slave break detect period. */
unsigned char LIN_break_prescale = 64; /* Prescale value for Slave. */
#endif /* 64 for 40 MHz int osc. div by 4 */
/* Set a default value for the register bits in the control register for the Baud
* rate generator that determine the value of the prescaler. In most cases, no
* prescaler will be required, but this must be checked for each MCU, Baud rate
* and MCU clock frequency selected. The value of 0 is valid for all R8C/Tiny MCU's
* operating at 20MHz or less with a 19200 LIN Baud rate. */
unsigned char LIN_smr_value = 0; /* Load value for the prescale select bits. */
/* Set a default value for the Baud rate generator divide value. This system
* clock is divided by 16, divided by the prescale value selected "LIN_smr_value"
* and divided by this BRG value to produce the desired Baud rate. It can be
* calculated as follows:
*
* BRG value = ((LIN_MY_FREQUENCY * 10**6) / (16 * total-uart-clock-prescale * LIN_SPEED)) - 1
*
* The calculated value should be rounded to the nearest integer value before
* subtracting 1. */
#if (LIN_MASTER_NODE == LIN_MY_NODE_NAME)
unsigned char LIN_brr_value = 64; /* 64 for 20 MHz Xtal and div by 1 */
#else
unsigned char LIN_brr_value = 32; /* 32 for 40 MHz int osc. div by 4 */
#endif
/**********************************************************************
* Function Definitions
*********************************************************************/
#if (LIN_MASTER_NODE == LIN_MY_NODE_NAME)
#ifdef DEDICATED_SCHEDULE_TIMER
#pragma INTERRUPT LIN_Schedule_ISR
#endif
#endif
#pragma INTERRUPT LIN_Break_ISR
#pragma INTERRUPT LIN_Xmt_ISR
#pragma INTERRUPT LIN_Rcv_ISR
#if (LIN_MASTER_NODE == LIN_MY_NODE_NAME)
#ifdef DEDICATED_SCHEDULE_TIMER
void LIN_Schedule_ISR(void)
{
l_sch_tick(LIN);
return;
}
#endif
#endif
void LIN_Break_ISR(void)
{
l_ifc_aux(LIN);
/* Turn off all LEDs for demo. Signals turn them on */
RED_LED2 = RED_LED = YLW_LED = GRN_LED = LED_OFF;
return;
}
void LIN_Xmt_ISR(void)
{
l_ifc_tx(LIN);
return;
}
void LIN_Rcv_ISR(void)
{
l_ifc_rx(LIN);
return;
}
/**********************************************************************
*
* Modification Record
*
**********************************************************************
*
* Version 1.1 for R8C 16 Mar 2006 Bob Chamberlain
*
*********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -