From a50e79990011e2ce79b2a6f6049b6aa4c146b4fd Mon Sep 17 00:00:00 2001 From: Michal Horn Date: Fri, 21 Sep 2012 15:59:08 +0200 Subject: [PATCH] LIN Driver anabled --- CommandStoring.dil | 6 +- CommandStoring.hcg | 6 +- source/lin.c | 638 ++++++++++++++++++++++++++++++++++++++++++ source/notification.c | 12 + 4 files changed, 657 insertions(+), 5 deletions(-) create mode 100644 source/lin.c diff --git a/CommandStoring.dil b/CommandStoring.dil index 58edb2c..01d6725 100644 --- a/CommandStoring.dil +++ b/CommandStoring.dil @@ -1,4 +1,4 @@ -# TMS570LS3137ZWT 09/19/12 09:02:37 +# TMS570LS3137ZWT 09/21/12 15:55:41 # ARCH=TMS570LS3137ZWT # @@ -483,7 +483,7 @@ DRIVER.SYSTEM.VAR.SAFETY_INIT_MIBSPI1_RAMPARITYCHECK_ENA.VALUE=1 DRIVER.SYSTEM.VAR.SAFETY_INIT_USB_SP_PBISTCHECK_ENA.VALUE=0x00000000 DRIVER.SYSTEM.VAR.SAFETY_INIT_PBIST_SELFCHECK_ENA.VALUE=1 DRIVER.SYSTEM.VAR.SCI2_ENABLE.VALUE=0 -DRIVER.SYSTEM.VAR.LIN_ENABLE.VALUE=0 +DRIVER.SYSTEM.VAR.LIN_ENABLE.VALUE=1 DRIVER.SYSTEM.VAR.CLKT_RTI1_FREQ.VALUE=80.000 DRIVER.SYSTEM.VAR.CORE_MPU_REGION_4_SIZE_VALUE.VALUE=0x11 DRIVER.SYSTEM.VAR.VIM_CHANNEL_6_MAPPING.VALUE=6 @@ -5538,7 +5538,7 @@ DRIVER.DMM.VAR.DMM_PORT_BIT9_PSL.VALUE=1 DRIVER.DMM.VAR.DMM_PORT_BIT3_DOUT.VALUE=0 DRIVER.DMM.VAR.DMM_PORT_BIT11_PULL.VALUE=2 DRIVER.DMM.VAR.DMM_PORT_BIT14_DOUT.VALUE=0 -DRIVER.DMM.VAR.DMM_PORT_BIT13_FUN.VALUE=1 +DRIVER.DMM.VAR.DMM_PORT_BIT13_FUN.VALUE=0 DRIVER.DMM.VAR.DMM_PORT_BIT11_PSL.VALUE=1 DRIVER.DMM.VAR.DMM_PORT_BIT15_DIR.VALUE=0 DRIVER.DMM.VAR.DMM_PORT_BIT13_PDR.VALUE=0 diff --git a/CommandStoring.hcg b/CommandStoring.hcg index 66c53b3..b883584 100644 --- a/CommandStoring.hcg +++ b/CommandStoring.hcg @@ -191,7 +191,9 @@ lin.h - + + lin.c + mibspi.h @@ -404,7 +406,7 @@ include\lin.h - + source\lin.c diff --git a/source/lin.c b/source/lin.c new file mode 100644 index 0000000..cb82bad --- /dev/null +++ b/source/lin.c @@ -0,0 +1,638 @@ +/** @file lin.c +* @brief LIN Driver Implementation File +* @date 20.Mar.2012 +* @version 03.01.00 +* +*/ + +/* (c) Texas Instruments 2009-2012, All rights reserved. */ + +/* USER CODE BEGIN (0) */ +/* USER CODE END */ + +#include "lin.h" + +/* USER CODE BEGIN (1) */ +/* USER CODE END */ + +/** @fn void linInit(void) +* @brief Initializes the lin Driver +* +* This function initializes the lin module. +*/ +void linInit(void) +{ +/* USER CODE BEGIN (2) */ +/* USER CODE END */ + /** @b intalise @b LIN */ + + /** - Release from reset */ + linREG->GCR0 = 1U; + + /** - Start LIN configuration + * - Keep state machine in software reset + */ + linREG->GCR1 = 00U; + + /** - Enable LIN Mode */ + linREG->GCR1 = 0x40U; + + /** - Setup control register 1 + * - Enable transmitter + * - Enable receiver + * - Stop when debug mode is entered + * - Disable Loopback mode + * - Disable / Enable HGENCTRL (Mask filtering with ID-Byte) + * - Use enhance checksum + * - Enable multi buffer mode + * - Disable automatic baudrate adjustment + * - Disable sleep mode + * - Set LIN module as master + * - Enable/Disable parity + * - Disable data length control in ID4 and ID5 + */ + linREG->GCR1 |= 0x03000C60U + | (1U << 12U) + | (0U << 2U); + + /** - Setup maximum baud rate prescaler */ + linREG->MBRSR = 3600U; + + /** - Setup baud rate prescaler */ + linREG->BRSR = 249U; + + /** - Setup RX and TX reception masks */ + linREG->MASK = (0xFFU << 16U) | 0xFFU; + + /** - Setup compare + * - Sync delimiter + * - Sync break extension + */ + linREG->COMP = ((1U - 1U) << 8U) | (13U - 13U); + + /** - Setup response length */ + linREG->LENGTH = (8U - 1U); + + /** - Set LIN pins functional mode + * - TX + * - RX + * - CLK + */ + linREG->FUN = 4U | 2U | 0U; + + /** - Set LIN pins default output value + * - TX + * - RX + * - CLK + */ + linREG->DOUT = 0U | 0U | 0U; + + /** - Set LIN pins output direction + * - TX + * - RX + * - CLK + */ + linREG->DIR = 0U | 0U | 0U; + + /** - Set LIN pins open drain enable + * - TX + * - RX + * - CLK + */ + linREG->ODR = 0U | 0U | 0U; + + /** - Set LIN pins pullup/pulldown enable + * - TX + * - RX + * - CLK + */ + linREG->PD = 0U | 0U | 0U; + + /** - Set LIN pins pullup/pulldown select + * - TX + * - RX + * - CLK + */ + linREG->PSL = 4U | 2U | 1U; + + /** - Set interrupt level + * - Bit error level + * - Physical bus error level + * - Checksum error level + * - Inconsistent sync field error level + * - No response error level + * - Framing error level + * - Overrun error level + * - Parity error level + * - Identifier level + * - RX level + * - TX level + * - Timeout after 3 wakeup signals level + * - Timeout after wakeup signal level + * - Timeout level + * - Wakeup level + * - Break detect level + */ + linREG->SETINTLVL = 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U; + + /** - Set interrupt enable + * - Enable/Disable bit error + * - Enable/Disable physical bus error level + * - Enable/Disable checksum error level + * - Enable/Disable inconsistent sync field error level + * - Enable/Disable no response error level + * - Enable/Disable framing error level + * - Enable/Disable overrun error level + * - Enable/Disable parity error level + * - Enable/Disable identifier level + * - Enable/Disable RX level + * - Enable/Disable TX level + * - Enable/Disable timeout after 3 wakeup signals level + * - Enable/Disable timeout after wakeup signal level + * - Enable/Disable timeout level + * - Enable/Disable wakeup level + * - Enable/Disable break detect level + */ + linREG->SETINT = 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U + | 0x00000000U; + + /** - Finaly start LIN */ + linREG->GCR1 |= 0x00000080U; + +/* USER CODE BEGIN (3) */ +/* USER CODE END */ +} + + +/** @fn void linSetFunctional(linBASE_t *lin, uint32_t port) +* @brief Change functional behavoiur of pins at runtime. +* @param[in] lin - lin module base address +* @param[in] port - Value to write to FUN register +* +* Change the value of the PCFUN register at runtime, this allows to +* dynaimcaly change the functionality of the LIN pins between functional +* and GIO mode. +*/ +void linSetFunctional(linBASE_t *lin, uint32_t port) +{ +/* USER CODE BEGIN (4) */ +/* USER CODE END */ + + lin->FUN = port; + +/* USER CODE BEGIN (5) */ +/* USER CODE END */ +} + + +/** @fn void linSendHeader(linBASE_t *lin, uint8_t identifier) +* @brief Send lin header. +* @param[in] lin - lin module base address +* @param[in] identifier - lin header id +* +* Send lin header including sync break field, sync field and identifier. +*/ +void linSendHeader(linBASE_t *lin, uint8_t identifier) +{ +/* USER CODE BEGIN (6) */ +/* USER CODE END */ + + lin->IDBYTE = identifier; + +/* USER CODE BEGIN (7) */ +/* USER CODE END */ +} + + +/** @fn void linSendWakupSignal(linBASE_t *lin) +* @brief Send lin wakeup signal. +* @param[in] lin - lin module base address +* +* Send lin wakeup signal to terminate the sleep mode of any lin node connected to the BUS. +*/ +void linSendWakupSignal(linBASE_t *lin) +{ +/* USER CODE BEGIN (8) */ +/* USER CODE END */ + + lin->TDx[0] = 0xF0; + lin->GCR2 |= 0x00000100U; + +/* USER CODE BEGIN (9) */ +/* USER CODE END */ +} + +/** @fn void linEnterSleep(linBASE_t *lin) +* @brief Take Module to Sleep. +* @param[in] lin - lin module base address +* +* Application must call this function to take Module to Sleep when Sleep command is received. +* This function can also be called to forcefully enter Sleep when no activity on BUS. +*/ +void linEnterSleep(linBASE_t *lin) +{ +/* USER CODE BEGIN (10) */ +/* USER CODE END */ + lin->GCR2 |= 0x00000001U; +/* USER CODE BEGIN (11) */ +/* USER CODE END */ +} + +/** @fn void linSoftwareReset(linBASE_t *lin) +* @brief Perform sofware reset. +* @param[in] lin - lin module base address +* +* Perform software reset of lin module. +* This function will reset the lin state machine and clear all pending flags. +* It is required to call this function after a wakeup signal has been sent. +*/ +void linSoftwareReset(linBASE_t *lin) +{ +/* USER CODE BEGIN (12) */ +/* USER CODE END */ + + lin->GCR1 &= ~0x00000080U; + lin->GCR1 |= 0x00000080U; + +/* USER CODE BEGIN (13) */ +/* USER CODE END */ +} + +/** @fn uint32_t linIsTxReady(linBASE_t *lin) +* @brief Check if Tx buffer empty +* @param[in] lin - lin module base address +* +* @return The TX ready flag +* +* Checks to see if the Tx buffer ready flag is set, returns +* 0 is flags not set otherwise will return the Tx flag itself. +*/ +uint32_t linIsTxReady(linBASE_t *lin) +{ +/* USER CODE BEGIN (14) */ +/* USER CODE END */ + + return lin->FLR & LIN_TX_READY; +} + +/** @fn void linSetLength(linBASE_t *lin, uint32_t length) +* @brief Send Data +* @param[in] lin - lin module base address +* @param[in] length - number of data words in bytes. Range: 1-8. +* +* Send data response length in bytes. +*/ +void linSetLength(linBASE_t *lin, uint32_t length) +{ +/* USER CODE BEGIN (15) */ +/* USER CODE END */ + + lin->LENGTH = length - 1U; + +/* USER CODE BEGIN (16) */ +/* USER CODE END */ +} + +/** @fn void linSend(linBASE_t *lin, const uint8_t *data) +* @brief Send Data +* @param[in] lin - lin module base address +* @param[in] data - pointer to data to send +* +* Send a block of data pointed to by 'data'. +* The number of data to transmit must be set with 'linSetLength' before. +*/ +void linSend(linBASE_t *lin, const uint8_t *data) +{ + int i; + int length = lin->LENGTH; + uint8_t *pData = (uint8_t *)data + length; + +/* USER CODE BEGIN (17) */ +/* USER CODE END */ + + for (i = length; i >= 0; i--) + { + lin->TDx[i] = *pData--; + } + +/* USER CODE BEGIN (18) */ +/* USER CODE END */ +} + +/** @fn uint32_t linIsRxReady(linBASE_t *lin) +* @brief Check if Rx buffer full +* @param[in] lin - lin module base address +* +* @return The Rx ready flag +* +* Checks to see if the Rx buffer full flag is set, returns +* 0 is flags not set otherwise will return the Rx flag itself. +*/ +uint32_t linIsRxReady(linBASE_t *lin) +{ +/* USER CODE BEGIN (19) */ +/* USER CODE END */ + + return lin->FLR & LIN_RX_INT; +} + + +/** @fn uint32_t linTxRxError(linBASE_t *lin) +* @brief Return Tx and Rx Error flags +* @param[in] lin - lin module base address +* +* @return The Tx and Rx error flags +* +* Returns the bit, physical bus, checksum, inconsisten sync field, +* no response, framing, overun, parity and timeout error flags. +* It also clears the error flags before returning. +*/ +uint32_t linTxRxError(linBASE_t *lin) +{ + uint32_t status = lin->FLR & (LIN_BE_INT + | LIN_PBE_INT + | LIN_CE_INT + | LIN_ISFE_INT + | LIN_NRE_INT + | LIN_FE_INT + | LIN_OE_INT + | LIN_PE_INT + | LIN_TOA3WUS_INT + | LIN_TOAWUS_INT + | LIN_TO_INT); + + lin->FLR = LIN_BE_INT + | LIN_PBE_INT + | LIN_CE_INT + | LIN_ISFE_INT + | LIN_NRE_INT + | LIN_FE_INT + | LIN_OE_INT + | LIN_PE_INT + | LIN_TOA3WUS_INT + | LIN_TOAWUS_INT + | LIN_TO_INT; + +/* USER CODE BEGIN (20) */ +/* USER CODE END */ + + return status; +} + + +/** @fn uint32_t linGetIdentifier(linBASE_t *lin) +* @brief Get last received identifier +* @param[in] lin - lin module base address +* +* @return Identifier +* +* Read last received identifier. +*/ +uint32_t linGetIdentifier(linBASE_t *lin) +{ +/* USER CODE BEGIN (21) */ +/* USER CODE END */ + return lin->RXID; +} + + +/** @fn void linGetData(linBASE_t *lin, uint8_t * const data) +* @brief Read received data +* @param[in] lin - lin module base address +* @param[in] data - pointer to data buffer +* +* Read a block of bytes and place it into the data buffer pointed to by 'data'. +*/ +void linGetData(linBASE_t *lin, uint8_t * const data) +{ + uint32_t i; + uint32_t length = lin->LENGTH; + uint8_t *pData = (uint8_t *)data; + +/* USER CODE BEGIN (22) */ +/* USER CODE END */ + + for (i = 0U; i <= length; i++) + { + *pData++ = lin->RDx[i]; + } + +/* USER CODE BEGIN (23) */ +/* USER CODE END */ +} + + +/** @fn void linEnableLoopback(linBASE_t *lin, Loopbacktype_t Loopbacktype) +* @brief Enable Loopback mode for self test +* @param[in] lin - lin module base address +* @param[in] Loopbacktype - Digital or Analog +* +* This function enables the Loopback mode for self test. +*/ +void linEnableLoopback(linBASE_t *lin, Loopbacktype_t Loopbacktype) +{ +/* USER CODE BEGIN (24) */ +/* USER CODE END */ + + /* Clear Loopback incase enbaled already */ + lin->IODFTCTRL = 0; + + /* Enable Loopback either in Analog or Digital Mode */ + lin->IODFTCTRL = 0x00000A00 + | Loopbacktype << 1; + +/* USER CODE BEGIN (25) */ +/* USER CODE END */ +} + +/** @fn void linDisableLoopback(linBASE_t *lin) +* @brief Enable Loopback mode for self test +* @param[in] lin - lin module base address +* +* This function disable the Loopback mode. +*/ +void linDisableLoopback(linBASE_t *lin) +{ +/* USER CODE BEGIN (26) */ +/* USER CODE END */ + + /* Disable Loopback Mode */ + lin->IODFTCTRL = 0x000005000; + +/* USER CODE BEGIN (27) */ +/* USER CODE END */ +} + + +/** @fn linEnableNotification(linBASE_t *lin, uint32_t flags) +* @brief Enable interrupts +* @param[in] lin - lin module base address +* @param[in] flags - Interrupts to be enabled, can be ored value of: +* LIN_BE_INT - bit error, +* LIN_PBE_INT - physical bus error, +* LIN_CE_INT - checksum error, +* LIN_ISFE_INT - inconsistent sync field error, +* LIN_NRE_INT - no response error, +* LIN_FE_INT - framming error, +* LIN_OE_INT - overrun error, +* LIN_PE_INT - parity error, +* LIN_ID_INT - received matching identifier, +* LIN_RX_INT - receive buffer ready, +* LIN_TOA3WUS_INT - time out after 3 wakeup signals, +* LIN_TOAWUS_INT - time out after wakeup signal, +* LIN_TO_INT - time out signal, +* LIN_WAKEUP_INT - wakeup, +* LIN_BREAK_INT - break detect +*/ +void linEnableNotification(linBASE_t *lin, uint32_t flags) +{ +/* USER CODE BEGIN (28) */ +/* USER CODE END */ + + lin->SETINT = flags; + +/* USER CODE BEGIN (29) */ +/* USER CODE END */ +} + + +/** @fn linDisableNotification(linBASE_t *lin, uint32_t flags) +* @brief Disable interrupts +* @param[in] lin - lin module base address +* @param[in] flags - Interrupts to be disabled, can be ored value of: +* LIN_BE_INT - bit error, +* LIN_PBE_INT - physical bus error, +* LIN_CE_INT - checksum error, +* LIN_ISFE_INT - inconsistent sync field error, +* LIN_NRE_INT - no response error, +* LIN_FE_INT - framming error, +* LIN_OE_INT - overrun error, +* LIN_PE_INT - parity error, +* LIN_ID_INT - received matching identifier, +* LIN_RX_INT - receive buffer ready, +* LIN_TOA3WUS_INT - time out after 3 wakeup signals, +* LIN_TOAWUS_INT - time out after wakeup signal, +* LIN_TO_INT - time out signal, +* LIN_WAKEUP_INT - wakeup, +* LIN_BREAK_INT - break detect +*/ +void linDisableNotification(linBASE_t *lin, uint32_t flags) +{ +/* USER CODE BEGIN (30) */ +/* USER CODE END */ + + lin->CLRINT = flags; + +/* USER CODE BEGIN (31) */ +/* USER CODE END */ +} + + +/** @fn void linHighLevelInterrupt(void) +* @brief Level 0 Interrupt for LIN +*/ +#pragma INTERRUPT(linHighLevelInterrupt, IRQ) + +void linHighLevelInterrupt(void) +{ + uint32_t vec = linREG->INTVECT0; + +/* USER CODE BEGIN (32) */ +/* USER CODE END */ + + switch (vec) + { + case 1: linNotification(linREG, LIN_WAKEUP_INT); break; + case 2: linNotification(linREG, LIN_ISFE_INT); break; + case 3: linNotification(linREG, LIN_PE_INT); break; + case 4: linNotification(linREG, LIN_ID_INT); break; + case 5: linNotification(linREG, LIN_PBE_INT); break; + case 6: linNotification(linREG, LIN_FE_INT); break; + case 7: linNotification(linREG, LIN_BREAK_INT); break; + case 8: linNotification(linREG, LIN_CE_INT); break; + case 9: linNotification(linREG, LIN_OE_INT); break; + case 10: linNotification(linREG, LIN_BE_INT); break; + case 11: linNotification(linREG, LIN_RX_INT); break; + case 13: linNotification(linREG, LIN_NRE_INT); break; + case 14: linNotification(linREG, LIN_TOAWUS_INT); break; + case 15: linNotification(linREG, LIN_TOA3WUS_INT); break; + case 16: linNotification(linREG, LIN_TO_INT); break; + default: + /* phantom interrupt, perform software reset */ + linREG->GCR1 &= ~0x00000080U; + linREG->GCR1 |= 0x00000080U; + break; + } +/* USER CODE BEGIN (33) */ +/* USER CODE END */ +} + + +/** @fn void linLowLevelInterrupt(void) +* @brief Level 1 Interrupt for LIN +*/ +#pragma INTERRUPT(linLowLevelInterrupt, IRQ) + +void linLowLevelInterrupt(void) +{ + uint32_t vec = linREG->INTVECT1; + +/* USER CODE BEGIN (34) */ +/* USER CODE END */ + + switch (vec) + { + case 1: linNotification(linREG, LIN_WAKEUP_INT); break; + case 2: linNotification(linREG, LIN_ISFE_INT); break; + case 3: linNotification(linREG, LIN_PE_INT); break; + case 4: linNotification(linREG, LIN_ID_INT); break; + case 5: linNotification(linREG, LIN_PBE_INT); break; + case 6: linNotification(linREG, LIN_FE_INT); break; + case 7: linNotification(linREG, LIN_BREAK_INT); break; + case 8: linNotification(linREG, LIN_CE_INT); break; + case 9: linNotification(linREG, LIN_OE_INT); break; + case 10: linNotification(linREG, LIN_BE_INT); break; + case 11: linNotification(linREG, LIN_RX_INT); break; + case 13: linNotification(linREG, LIN_NRE_INT); break; + case 14: linNotification(linREG, LIN_TOAWUS_INT); break; + case 15: linNotification(linREG, LIN_TOA3WUS_INT); break; + case 16: linNotification(linREG, LIN_TO_INT); break; + default: + /* phantom interrupt, perform software reset */ + linREG->GCR1 &= ~0x00000080U; + linREG->GCR1 |= 0x00000080U; + break; + } +/* USER CODE BEGIN (35) */ +/* USER CODE END */ +} + diff --git a/source/notification.c b/source/notification.c index 3397803..4d04ada 100644 --- a/source/notification.c +++ b/source/notification.c @@ -16,6 +16,7 @@ #include "adc.h" #include "can.h" #include "gio.h" +#include "lin.h" #include "mibspi.h" #include "sci.h" #include "het.h" @@ -158,6 +159,17 @@ void gioNotification(int bit) /* USER CODE BEGIN (20) */ /* USER CODE END */ +void linNotification(linBASE_t *lin, uint32_t flags) +{ +/* enter user code and remove the while loop... */ + while(1); +/* USER CODE BEGIN (23) */ +/* USER CODE END */ + +} + +/* USER CODE BEGIN (24) */ +/* USER CODE END */ void mibspiNotification(mibspiBASE_t *mibspi, uint32_t flags) { /* enter user code and remove the while loop... */ -- 2.39.2