/*********************************************************************************** FILE NAME : main_Lin_slave.c DESCRIPTION : LIN Module demo. Copyright : 2006 Renesas Technology Europe Ltd. Copyright : 2006 Renesas Solutions Corporation. Copyright : 2006 Renesas Technology Corporation. All Rights Reserved ***********************************************************************************/ /*********************************************************************************** Revision History DD.MM.YYYY OSO-UID Description 07.06.2007 IAR Adapted to IAR Embedded Workbench 10.07.2006 RTE-NPS First Release ***********************************************************************************/ /*********************************************************************************** User Includes ***********************************************************************************/ /* R8C29 SFR Definition */ #include "ior8c28_29.h" /*********************************************************************************** User Program Code Prototypes ***********************************************************************************/ /* Initial setting */ void init (void); void lin_init (void); /********************************************************************************** Global variables ***********************************************************************************/ #define trapre_def (unsigned char)(177) #define tratr_def (unsigned char) (63) unsigned char id_receive; unsigned char flag; /* Bittime table (10080`8936bps)*/ unsigned short brg_tbl[128][2] = { {992, 61}, {993, 61}, {994, 61}, {995, 61}, {996, 61}, {997, 61}, {998, 61}, {999, 61}, {1000, 61}, {1001, 61}, {1002, 61}, {1003, 61}, {1004, 61}, {1005, 61}, {1006, 61}, {1007, 61}, {1008, 62}, {1009, 62}, {1010, 62}, {1011, 62}, {1012, 62}, {1013, 62}, {1014, 62}, {1015, 62}, {1016, 62}, {1017, 62}, {1018, 62}, {1019, 62}, {1020, 62}, {1021, 62}, {1022, 62}, {1023, 62}, {1024, 63}, {1025, 63}, {1026, 63}, {1027, 63}, {1028, 63}, {1029, 63}, {1030, 63}, {1031, 63}, {1032, 63}, {1033, 63}, {1034, 63}, {1035, 63}, {1036, 63}, {1037, 63}, {1038, 63}, {1039, 63}, {1040, 64}, {1041, 64}, {1042, 64}, {1043, 64}, {1044, 64}, {1045, 64}, {1046, 64}, {1047, 64}, {1048, 64}, {1049, 64}, {1050, 64}, {1051, 64}, {1052, 64}, {1053, 64}, {1054, 64}, {1055, 64}, {1056, 65}, {1057, 65}, {1058, 65}, {1059, 65}, {1060, 65}, {1061, 65}, {1062, 65}, {1063, 65}, {1064, 65}, {1065, 65}, {1066, 65}, {1067, 65}, {1068, 65}, {1069, 65}, {1070, 65}, {1071, 65}, {1072, 66}, {1073, 66}, {1074, 66}, {1075, 66}, {1076, 66}, {1077, 66}, {1078, 66}, {1079, 66}, {1080, 66}, {1081, 66}, {1082, 66}, {1083, 66}, {1084, 66}, {1085, 66}, {1086, 66}, {1087, 66}, {1088, 67}, {1089, 67}, {1090, 67}, {1091, 67}, {1092, 67}, {1093, 67}, {1094, 67}, {1095, 67}, {1096, 67}, {1097, 67}, {1098, 67}, {1099, 67}, {1100, 67}, {1101, 67}, {1102, 67}, {1103, 67}, {1104, 68}, {1105, 68}, {1106, 68}, {1107, 68}, {1108, 68}, {1109, 68}, {1110, 68}, {1111, 68}, {1112, 68}, {1113, 68}, {1114, 68}, {1115, 68}, {1116, 68}, {1117, 68}, {1118, 68}, {1119, 68}, }; /*********************************************************************************** User Program Code ***********************************************************************************/ void main () { /* Setting of a Initialize */ init (); /* loop */ while (1) { lin_init (); } } /********************************************************************************** Function Name: init Description: Intialize some value and functions Parameters: none Return value: none ***********************************************************************************/ void init (void) { /* Initialization of a variable led_cnt = 0x00; interrupt_flg = 0; lin_flg = 0; id_receive = 0x00; transmit_data2 = 0x00; */ /* Initialization of UART */ /* UART0 transmission and reception mode register */ u0mr = 0x05; /* UART0 bit rate register */ u0brg = 0x81; /* UART0 transmission and reception control register */ u0c0 = 0x00; /* Prohibited from Transmission and Reception */ re_u0c1 = 0; te_u0c1 = 0; /* Setting of AD Convert */ /* 8 bits mode / Vref connection */ //adcon1 = 0x20; /* Sample & Hold is unused */ //adcon2 = 0x00; /* Repetition mode /AD conversion start / Frequency f4 */ //adcon0 = 0x5f; /* Timer RB setting */ /* Interrupt level 1 */ trbic = 0x01; /* Timer RB I/O control register */ trbioc = 0x00; /* Timer RB mode register */ trbmr = 0x10; /* Prescaler = 1/249 (0.1ms) */ trbpre = 0xf9; /* TimerRB = 1/10 (1ms) */ trbpr = 0xa; /* TimerRB start */ trbcr = 0x03; /* Interrupt permission */ //asm ("FSET I"); } /********************************************************************************** Function Name: lin_init Description: Initialisierung der LIN Hardware Parameters: none Return value: none ***********************************************************************************/ void lin_init (void) { /* Definition of a variable */ unsigned char n; /* Sync Field bittime */ unsigned short count; /* 1 bittime */ unsigned short sbittime; /* Timer value after Synk Field search */ unsigned char trapre_end; /* Prescaler value after Synk Field search */ unsigned char tratr_end; //Timer RA Set to pulse width measurement mode Timer RA Mode Register tmod0_tramr = 1; tmod1_tramr = 1; tmod2_tramr = 0; //Timer RA Set to pulse width measurement level low tedgsel_traioc = 0; //Timer RA Set the RXD0 to P1_5 tiosel_traioc = 1; //Set a count source (f1) tck0_tramr = 0; tck1_tramr = 0; tck2_tramr = 0; //Set width of Synch Break (11bittime) trapre = trapre_def; tra = tratr_def; //stop to LIN operation line_lincr = 0; //slave mode mst_lincr = 0; //sart to LIN operation line_lincr = 1; //RXD0 input timing //0= after Synch Break detection //1= after Synch Field measurement sbe_lincr = 1; //BUS collision detection, Synch Break detection, Synch Field measurement //0= disabled 1=enabled bcie_lincr = 1; sbie_lincr = 1; sfie_lincr = 1; //clear status flags //BUS collision detection, Synch Break detection, Synch Field measurement b0clr_linst = 0; b1clr_linst = 0; b2clr_linst = 0; //start a pulse width measurement tstart_tracr = 1; //read the count status flag while (tcstf_tracr == 0); //no start Synch Break detection lstart_lincr = 1; //read the RXD0 input status flag while (rxdsf_lincr == 0); //read the Synch Break detection flag while (sbdct_linst == 0); flag = sfdct_linst; //read the Synch Field measurement completed flag while (sfdct_linst == 0); trapre_end = trapre; tratr_end = tra; /* Bittime operation processing */ count = (tratr_def - tratr_end) * (trapre_def + 1) + (trapre_def - trapre_end); sbittime = count / 8; for (n = 0; n < 128; n++) { if (brg_tbl[n][0] == sbittime) { u0brg = brg_tbl[n][1]; break; } } /* Re-setting of Timer and Prescaler */ trapre = trapre_def; tra = tratr_def; /* Reception permission */ re_u0c1 = 1; te_u0c1 = 1; /* ID Field */ /* Waited for reception completion of ID Field */ while (ri_u0c1 == 0); /* Reading of reception data */ id_receive = u0rb & 0xff; /* Reception prohibition */ re_u0c1 = 0; te_u0c1 = 0; }