📄 comm_lin.c
字号:
//====================================================================
//--- COPYRIGHT
//====================================================================
// Copyright 2007 Microchip Technology Inc.
//
// Microchip Technology Inc. ("Microchip") licenses this software to
// you solely for use with Microchip products. The software is owned
// by Microchip and is protected under applicable copyright laws. All
// rights reserved.
//
// SOFTWARE IS PROVIDED IN AN "AS IS." MICROCHIP EXPRESSLY DISCLAIMS ANY
// WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL MICROCHIP
// BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL
// DAMAGES, LOST PROFITS OR LOST DATA, HARM TO YOUR EQUIPMENT, COST OF
// PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
// BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),
// ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER SIMILAR COSTS.
//
//====================================================================
//--- TITLE
//====================================================================
// Filename: comm_lin.c
// Microcontroller: PIC18F2550
// Compiled using: C18 V3.00
//
// Author: Mark Enochson
// Company: Microchip Technology Inc.
//
//====================================================================
//--- DESCRIPTION
//====================================================================
//
//====================================================================
//--- HISTORY
//====================================================================
//
// 0001 - 03-01-07 - ME
// -- initial release
//
// 0002 - 03-01-08 - ME
// -- overhauled module
// -- changed to interrupt-driven TX
// -- modified rx_isr
// -- autobaud detect causes interrupt
// (previously stayed in ISR, can now with simultaneous
// MASTER and SLAVE)
// -- created/defined variable using shared RAM
// -- eliminated byte of bit flags ... moved to shared RAM
// -- added SLAVE capability
// -- set TXEN in INIT function, logic toggles TXIE as necessary
//
//
// 0003 - 04-28-08 - ME
// -- added definition of bit flag in slave profile:
// COMM_LIN_SP_IDDEF_ENHANCED_CS_FLAG to indicate a selection
// of checksum 'type' for applicable node register/identifier
// -- modified checksum calculation to use
// COMM_LIN_SP_IDDEF_ENHANCED_CS_FLAG in the slave profile
// to select checksum 'type' - was selected by control block
//
// 0004 - 05-12-08 - ME
// -- modified rx_isr to avoid posting a SYNC byte event (only data)
// this allows the DLL/GUI to run more efficiently/quickly
//
//====================================================================
//==========================================================
//--- INCLUDES
//==========================================================
#include "project.h"
#include "comm_gen.h"
#include "cbuf.h"
#include "timer0.h"
#include "timer3.h"
#include "comm_lin.h"
//==========================================================
//--- DEFINITIONS / DECLARATIONS
//==========================================================
// (none)
#ifdef __DEBUG
//#define __DEBUG_LIN
#endif
#ifdef __DEBUG_LIN
//#define __DEBUG_LIN_SLAVE_ENABLE
//#define __DEBUG_LIN_AUX1
//#define __DEBUG_LIN_AUX2
#endif
//==========================================================
//==========================================================
#ifdef __DEBUG_LIN
//==========================================================
//--- comm_lin_test()
//==========================================================
void comm_lin_test(void)
{
TD_BYTE i, j, PARITY;
Nop();
cbuf3_init();
cbuf3_put(0x92);
cbuf3_put(0x10);
cbuf3_put(0x11);
cbuf3_put(0x22);
// cbuf3_put(0xA9);
cbuf3_put(0x93);
cbuf3_put(0x24);
cbuf3_put(0x12);
cbuf3_put(0x34);
cbuf3_put(0x56);
cbuf3_put(0x81);
cbuf3_put(0x38);
cbuf3_put(0x12);
cbuf3_put(0x00);
COMM_LIN_1_FLAGS = 0;
comm_flags1_B._b = 0;
comm_state = 0x86;
cbuf1_put(0x19);
cbuf1_put(0x02);
cbuf1_put(0x17);
cbuf1_put(0x18);
while(1)
{
Nop();
comm_lin_svc();
}
return;
}
#endif
//==========================================================
//==========================================================
//==========================================================
//--- comm_lin_init()
//==========================================================
// initialize per CONTROL_BLOCK
//==========================================================
TD_BYTE comm_lin_init(void)
{
comm_lin_reset(); // idles/disables all hardware used by this module
comm_lin_config(); // configures hardware
comm_lin_clear(); // clears dynamic variables/hardware
cbuf3_init();
cbuf3_data[1] = 0; // set slave profile to NULL
#ifdef __DEBUG_LIN_AUX1
LIN_AUX1_PORT = 0;
LIN_AUX1_TRIS = 0;
#endif
#ifdef __DEBUG_LIN_AUX2
LIN_AUX2_PORT = 0;
LIN_AUX2_TRIS = 0;
#endif
return(0);
}
//==========================================================
//--- comm_lin_reset()
//==========================================================
// RETURN ALL HARDWARE TO RESET/BENIGN STATE
//==========================================================
void comm_lin_reset(void)
{
//--- DISABLE INTERRUPTS
PIE1bits.RCIE = 0;
PIE1bits.TXIE = 0;
//--- CLEAR H/W REGISTERS
TXSTA = 0;
RCSTA = 0;
BAUDCON = 0;
//--- SET I/O DIRECTION
LIN_TX_TRIS = 1;
LIN_RX_TRIS = 1;
LIN_CS_TRIS = 1;
LIN_AFAULT_TRIS = 1;
}
//==========================================================
//--- comm_lin_config()
//==========================================================
// one-time configuration
//==========================================================
void comm_lin_config(void)
{
//--- I/O
LIN_TX_TRIS = 0;
LIN_RX_TRIS = 1;
LIN_CS_TRIS = 0;
LIN_AFAULT_TRIS = 1;
//--- USART
BAUDCONbits.BRG16 = 1;
TXSTAbits.BRGH = 1;
RCSTAbits.SPEN = 1;
//--- INTERRUPTS
IPR1bits.RCIP = 0;
IPR1bits.TXIP = 0;
TXSTAbits.TXEN = 1;
}
//==========================================================
//--- comm_lin_clear()
//==========================================================
// assumes mode/hardware has been properly initialized
// clears any status/errors without a cold re-init
//
// anything that could have been changed during operation
// is returned to default
// anything that is initialized that never changed
// is left alone/unaltered
// call post processor to set status block
//==========================================================
void comm_lin_clear(void)
{
TD_BYTE i;
//-----------------------------------
//--- DISABLE INTERRUPT(S)
//-----------------------------------
PIE1bits.RCIE = 0;
PIE1bits.TXIE = 0;
//-----------------------------------
//--- BAUD RATE
//-----------------------------------
SPBRG = COMM_CB_LIN_BRG_LO_REG;
SPBRGH = COMM_CB_LIN_BRG_HI_REG;
//-----------------------------------
//--- AUTOBAUD
//-----------------------------------
if(BAUDCONbits.ABDEN)
BAUDCONbits.ABDEN = 0;
BAUDCONbits.ABDOVF = 0;
//-----------------------------------
//--- INTERRUPT ENABLE(S)
//-----------------------------------
if(PIR1bits.RCIF)
i = RCREG;
PIE1bits.RCIE = 1;
//-----------------------------------
//--- RX ENABLE
//-----------------------------------
if(RCSTAbits.OERR)
RCSTAbits.CREN = 0;
if(COMM_CB_LIN_RCV_ENABLE_FLAG)
RCSTAbits.CREN = 1;
else
RCSTAbits.CREN = 0;
//-----------------------------------
//--- LIN CHIP SELECT
//-----------------------------------
#ifndef __DEBUG_LIN_AUX1
if(COMM_CB_LIN_CS_FLAG)
LIN_CS_PORT = 1;
else
LIN_CS_PORT = 0;
#endif
//-----------------------------------
//--- OPERATIONAL FLAGS
//-----------------------------------
COMM_LIN_0_FLAGS = 0;
COMM_LIN_1_FLAGS = 0;
//-----------------------------------
//--- STATUS BLOCK
//-----------------------------------
COMM_SB_LIN_REG12 = 0;
COMM_SB_LIN_REG13 = 0;
COMM_SB_LIN_REG14 = 0;
COMM_SB_LIN_REG15 = 0;
COMM_SB_LIN_REG16 = 0;
COMM_SB_LIN_REG17 = 0;
COMM_SB_LIN_REG18 = 0;
COMM_SB_LIN_REG19 = 0;
//--- EXECUTE POST PROCESSOR (SETS STATUS)
comm_lin_postproc();
#ifdef __DEBUG_LIN_SLAVE_ENABLE
// COMM_CB_LIN_SLAVE_FLAG = 0;
COMM_CB_LIN_SLAVE_FLAG = 1;
#endif
}
//==========================================================
//==========================================================
//--- comm_lin_svc()
//==========================================================
//==========================================================
void comm_lin_svc(void)
{
//---------------------------------------------
//--- DISABLE INTERRUPTS
//--- FLAG SET TO REPRESENT EXISTING STATE
//---------------------------------------------
COMM_LIN_GIEL1_FLAG = 0;
if(INTCONbits.GIEL)
{
while(INTCONbits.GIEL)
{
INTCONbits.GIEL = 0;
}
COMM_LIN_GIEL1_FLAG = 1;
}
//---------------------------------------------
//---
#ifndef __DEBUG_LIN_AUX1
if(COMM_CB_LIN_CS_FLAG)
LIN_CS_PORT = 1;
else
LIN_CS_PORT = 0;
#endif
//---------------------------------------------
//--- SWITCH CONTROLLER STATE
//---------------------------------------------
switch(comm_state)
{
Nop();
//====================================================
case COMM_TAG_LIN_INIT:
comm_lin_init();
break;
//====================================================
case COMM_TAG_LIN_XMT_BRK:
case COMM_TAG_LIN_XMT_BRK_DATA:
case COMM_TAG_LIN_XMT_FRAME:
//---------------------------------------
//--- XMIT BREAK (ALL CMNDS REQUIRE IT)
//---------------------------------------
if(not COMM_0_FLAG)
{
#ifdef __DEBUG_LIN_AUX2
Nop();
LIN_AUX2_PORT = 1;
#endif
if(TXSTAbits.TRMT == 0)
break;
COMM_LIN_ABAUD_DISABLE_FLAG = 1; // DISABLE AUTOBAUD
COMM_0_FLAG = 1; // SET OP FLAG
TXSTAbits.SENDB = 1; // SET 'BREAK' CONTROL
PIE1bits.TXIE = 1; // ENABLE INT(S)
TXREG = 0; // LOAD TXREG WITH DUMMY ZERO
COMM_LIN_TX_SVC_FLAG = 1; // ISR FLAG TO CALL SERVICE
comm_led_busy_data(); // SET LED BUSY
if(COMM_CB_LIN_EVENT_EN_BREAK_TX_FLAG)
comm_post_event(COMM_TAG_LIN_EVENT_BREAK_TX);
#ifdef __DEBUG_LIN_AUX2
LIN_AUX2_PORT = 0;
#endif
break;
}
//---------------------------------------
//--- IF BREAK ONLY, WAIT UNTIL DONE ...
//---------------------------------------
if(comm_state == COMM_TAG_LIN_XMT_BRK)
{
if(PIR1bits.TXIF)
{
PIE1bits.TXIE = 0; // DISABLE INT(S) - DONE!
COMM_LIN_TX_SVC_FLAG = 0; // RST FLAG: TX SERVICE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -