Mouse code for the MacroRat
mbed-dev/targets/TARGET_NUVOTON/TARGET_NUC472/device/StdDriver/nuc472_can.c@46:b156ef445742, 2017-06-03 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sat Jun 03 00:22:44 2017 +0000
- Revision:
- 46:b156ef445742
- Parent:
- 18:6a4db94011d3
Final code for internal battlebot competition.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /**************************************************************************//** |
sahilmgandhi | 18:6a4db94011d3 | 2 | * @file CAN.c |
sahilmgandhi | 18:6a4db94011d3 | 3 | * @version V1.00 |
sahilmgandhi | 18:6a4db94011d3 | 4 | * $Revision: 14 $ |
sahilmgandhi | 18:6a4db94011d3 | 5 | * $Date: 14/10/06 5:38p $ |
sahilmgandhi | 18:6a4db94011d3 | 6 | * @brief NUC472/NUC442 CAN driver source file |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * @note |
sahilmgandhi | 18:6a4db94011d3 | 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. |
sahilmgandhi | 18:6a4db94011d3 | 10 | *****************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 11 | |
sahilmgandhi | 18:6a4db94011d3 | 12 | #include "NUC472_442.h" |
sahilmgandhi | 18:6a4db94011d3 | 13 | /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver |
sahilmgandhi | 18:6a4db94011d3 | 14 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | |
sahilmgandhi | 18:6a4db94011d3 | 17 | /** @addtogroup NUC472_442_CAN_Driver CAN Driver |
sahilmgandhi | 18:6a4db94011d3 | 18 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 19 | */ |
sahilmgandhi | 18:6a4db94011d3 | 20 | |
sahilmgandhi | 18:6a4db94011d3 | 21 | |
sahilmgandhi | 18:6a4db94011d3 | 22 | /** @addtogroup NUC472_442_CAN_EXPORTED_FUNCTIONS CAN Exported Functions |
sahilmgandhi | 18:6a4db94011d3 | 23 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 24 | */ |
sahilmgandhi | 18:6a4db94011d3 | 25 | |
sahilmgandhi | 18:6a4db94011d3 | 26 | #include <stdio.h> |
sahilmgandhi | 18:6a4db94011d3 | 27 | |
sahilmgandhi | 18:6a4db94011d3 | 28 | |
sahilmgandhi | 18:6a4db94011d3 | 29 | /// @cond HIDDEN_SYMBOLS |
sahilmgandhi | 18:6a4db94011d3 | 30 | |
sahilmgandhi | 18:6a4db94011d3 | 31 | |
sahilmgandhi | 18:6a4db94011d3 | 32 | static uint32_t GetFreeIF(CAN_T *tCAN); |
sahilmgandhi | 18:6a4db94011d3 | 33 | |
sahilmgandhi | 18:6a4db94011d3 | 34 | |
sahilmgandhi | 18:6a4db94011d3 | 35 | //#define DEBUG_PRINTF printf |
sahilmgandhi | 18:6a4db94011d3 | 36 | #define DEBUG_PRINTF(...) |
sahilmgandhi | 18:6a4db94011d3 | 37 | |
sahilmgandhi | 18:6a4db94011d3 | 38 | /** |
sahilmgandhi | 18:6a4db94011d3 | 39 | * @brief Check if SmartCard slot is presented. |
sahilmgandhi | 18:6a4db94011d3 | 40 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 41 | * @retval 0 IF0 is free |
sahilmgandhi | 18:6a4db94011d3 | 42 | * @retval 1 IF1 is free |
sahilmgandhi | 18:6a4db94011d3 | 43 | * @retval 2 No IF is free |
sahilmgandhi | 18:6a4db94011d3 | 44 | * @details Search the first free message interface, starting from 0. |
sahilmgandhi | 18:6a4db94011d3 | 45 | */ |
sahilmgandhi | 18:6a4db94011d3 | 46 | static uint32_t GetFreeIF(CAN_T *tCAN) |
sahilmgandhi | 18:6a4db94011d3 | 47 | { |
sahilmgandhi | 18:6a4db94011d3 | 48 | if((tCAN->IF[0].CREQ & CAN_IF_CREQ_BUSY_Msk) == 0) |
sahilmgandhi | 18:6a4db94011d3 | 49 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 50 | else if((tCAN->IF[1].CREQ & CAN_IF_CREQ_BUSY_Msk) == 0) |
sahilmgandhi | 18:6a4db94011d3 | 51 | return 1; |
sahilmgandhi | 18:6a4db94011d3 | 52 | else |
sahilmgandhi | 18:6a4db94011d3 | 53 | return 2; |
sahilmgandhi | 18:6a4db94011d3 | 54 | } |
sahilmgandhi | 18:6a4db94011d3 | 55 | |
sahilmgandhi | 18:6a4db94011d3 | 56 | /** |
sahilmgandhi | 18:6a4db94011d3 | 57 | * @brief Enter initialization mode |
sahilmgandhi | 18:6a4db94011d3 | 58 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 59 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 60 | * @details This function is used to set CAN to enter initialization mode and enable access bit timing |
sahilmgandhi | 18:6a4db94011d3 | 61 | * register. After bit timing configuration ready, user must call CAN_LeaveInitMode() |
sahilmgandhi | 18:6a4db94011d3 | 62 | * to leave initialization mode and lock bit timing register to let new configuration |
sahilmgandhi | 18:6a4db94011d3 | 63 | * take effect. |
sahilmgandhi | 18:6a4db94011d3 | 64 | */ |
sahilmgandhi | 18:6a4db94011d3 | 65 | void CAN_EnterInitMode(CAN_T *tCAN) |
sahilmgandhi | 18:6a4db94011d3 | 66 | { |
sahilmgandhi | 18:6a4db94011d3 | 67 | tCAN->CON |= CAN_CON_INIT_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 68 | tCAN->CON |= CAN_CON_CCE_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 69 | } |
sahilmgandhi | 18:6a4db94011d3 | 70 | |
sahilmgandhi | 18:6a4db94011d3 | 71 | |
sahilmgandhi | 18:6a4db94011d3 | 72 | /** |
sahilmgandhi | 18:6a4db94011d3 | 73 | * @brief Leave initialization mode |
sahilmgandhi | 18:6a4db94011d3 | 74 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 75 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 76 | * @details This function is used to set CAN to leave initialization mode to let |
sahilmgandhi | 18:6a4db94011d3 | 77 | * bit timing configuration take effect after configuration ready. |
sahilmgandhi | 18:6a4db94011d3 | 78 | */ |
sahilmgandhi | 18:6a4db94011d3 | 79 | void CAN_LeaveInitMode(CAN_T *tCAN) |
sahilmgandhi | 18:6a4db94011d3 | 80 | { |
sahilmgandhi | 18:6a4db94011d3 | 81 | tCAN->CON &= (~(CAN_CON_INIT_Msk | CAN_CON_CCE_Msk)); |
sahilmgandhi | 18:6a4db94011d3 | 82 | |
sahilmgandhi | 18:6a4db94011d3 | 83 | while(tCAN->CON & CAN_CON_INIT_Msk); /* Check INIT bit is released */ |
sahilmgandhi | 18:6a4db94011d3 | 84 | } |
sahilmgandhi | 18:6a4db94011d3 | 85 | |
sahilmgandhi | 18:6a4db94011d3 | 86 | /** |
sahilmgandhi | 18:6a4db94011d3 | 87 | * @brief Wait message into message buffer in basic mode. |
sahilmgandhi | 18:6a4db94011d3 | 88 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 89 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 90 | * @details This function is used to wait message into message buffer in basic mode. Please notice the |
sahilmgandhi | 18:6a4db94011d3 | 91 | * function is polling NEWDAT bit of MCON register by while loop and it is used in basic mode. |
sahilmgandhi | 18:6a4db94011d3 | 92 | */ |
sahilmgandhi | 18:6a4db94011d3 | 93 | void CAN_WaitMsg(CAN_T *tCAN) |
sahilmgandhi | 18:6a4db94011d3 | 94 | { |
sahilmgandhi | 18:6a4db94011d3 | 95 | tCAN->STATUS = 0x0; /* clr status */ |
sahilmgandhi | 18:6a4db94011d3 | 96 | |
sahilmgandhi | 18:6a4db94011d3 | 97 | while (1) { |
sahilmgandhi | 18:6a4db94011d3 | 98 | if(tCAN->IF[1].MCON & CAN_IF_MCON_NEWDAT_Msk) { /* check new data */ |
sahilmgandhi | 18:6a4db94011d3 | 99 | DEBUG_PRINTF("New Data IN\n"); |
sahilmgandhi | 18:6a4db94011d3 | 100 | break; |
sahilmgandhi | 18:6a4db94011d3 | 101 | } |
sahilmgandhi | 18:6a4db94011d3 | 102 | if(tCAN->STATUS & CAN_STATUS_RXOK_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 103 | DEBUG_PRINTF("Rx OK\n"); |
sahilmgandhi | 18:6a4db94011d3 | 104 | } |
sahilmgandhi | 18:6a4db94011d3 | 105 | if(tCAN->STATUS & CAN_STATUS_LEC_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 106 | DEBUG_PRINTF("Error\n"); |
sahilmgandhi | 18:6a4db94011d3 | 107 | } |
sahilmgandhi | 18:6a4db94011d3 | 108 | } |
sahilmgandhi | 18:6a4db94011d3 | 109 | } |
sahilmgandhi | 18:6a4db94011d3 | 110 | |
sahilmgandhi | 18:6a4db94011d3 | 111 | /** |
sahilmgandhi | 18:6a4db94011d3 | 112 | * @brief Get current bit rate |
sahilmgandhi | 18:6a4db94011d3 | 113 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 114 | * @return Current Bit-Rate (kilo bit per second) |
sahilmgandhi | 18:6a4db94011d3 | 115 | * @details Return current CAN bit rate according to the user bit-timing parameter settings |
sahilmgandhi | 18:6a4db94011d3 | 116 | */ |
sahilmgandhi | 18:6a4db94011d3 | 117 | uint32_t CAN_GetCANBitRate(CAN_T *tCAN) |
sahilmgandhi | 18:6a4db94011d3 | 118 | { |
sahilmgandhi | 18:6a4db94011d3 | 119 | uint8_t u8Tseg1,u8Tseg2; |
sahilmgandhi | 18:6a4db94011d3 | 120 | uint32_t u32Bpr; |
sahilmgandhi | 18:6a4db94011d3 | 121 | |
sahilmgandhi | 18:6a4db94011d3 | 122 | u8Tseg1 = (tCAN->BTIME & CAN_BTIME_TSEG1_Msk) >> CAN_BTIME_TSEG1_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 123 | u8Tseg2 = (tCAN->BTIME & CAN_BTIME_TSEG2_Msk) >> CAN_BTIME_TSEG2_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 124 | u32Bpr = (tCAN->BTIME & CAN_BTIME_BRP_Msk) | (tCAN->BRPE << 6); |
sahilmgandhi | 18:6a4db94011d3 | 125 | |
sahilmgandhi | 18:6a4db94011d3 | 126 | return (SystemCoreClock/(u32Bpr+1)/(u8Tseg1 + u8Tseg2 + 3)); |
sahilmgandhi | 18:6a4db94011d3 | 127 | } |
sahilmgandhi | 18:6a4db94011d3 | 128 | |
sahilmgandhi | 18:6a4db94011d3 | 129 | /** |
sahilmgandhi | 18:6a4db94011d3 | 130 | * @brief Switch the CAN into test mode. |
sahilmgandhi | 18:6a4db94011d3 | 131 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 132 | * @param[in] u8TestMask Specifies the configuration in test modes |
sahilmgandhi | 18:6a4db94011d3 | 133 | * CAN_TEST_BASIC_Msk : Enable basic mode of test mode |
sahilmgandhi | 18:6a4db94011d3 | 134 | * CAN_TESTR_SILENT_Msk : Enable silent mode of test mode |
sahilmgandhi | 18:6a4db94011d3 | 135 | * CAN_TESTR_LBACK_Msk : Enable Loop Back Mode of test mode |
sahilmgandhi | 18:6a4db94011d3 | 136 | * CAN_TESTR_TX0_Msk/CAN_TESTR_TX1_Msk: Control CAN_TX pin bit field |
sahilmgandhi | 18:6a4db94011d3 | 137 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 138 | * @details Switch the CAN into test mode. There are four test mode (BASIC/SILENT/LOOPBACK/ |
sahilmgandhi | 18:6a4db94011d3 | 139 | * LOOPBACK combined SILENT/CONTROL_TX_PIN)could be selected. After setting test mode,user |
sahilmgandhi | 18:6a4db94011d3 | 140 | * must call CAN_LeaveInitMode() to let the setting take effect. |
sahilmgandhi | 18:6a4db94011d3 | 141 | */ |
sahilmgandhi | 18:6a4db94011d3 | 142 | void CAN_EnterTestMode(CAN_T *tCAN, uint8_t u8TestMask) |
sahilmgandhi | 18:6a4db94011d3 | 143 | { |
sahilmgandhi | 18:6a4db94011d3 | 144 | tCAN->CON |= CAN_CON_TEST_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 145 | tCAN->TEST = u8TestMask; |
sahilmgandhi | 18:6a4db94011d3 | 146 | } |
sahilmgandhi | 18:6a4db94011d3 | 147 | |
sahilmgandhi | 18:6a4db94011d3 | 148 | |
sahilmgandhi | 18:6a4db94011d3 | 149 | /** |
sahilmgandhi | 18:6a4db94011d3 | 150 | * @brief Leave the test mode |
sahilmgandhi | 18:6a4db94011d3 | 151 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 152 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 153 | * @details This function is used to Leave the test mode (switch into normal mode). |
sahilmgandhi | 18:6a4db94011d3 | 154 | */ |
sahilmgandhi | 18:6a4db94011d3 | 155 | void CAN_LeaveTestMode(CAN_T *tCAN) |
sahilmgandhi | 18:6a4db94011d3 | 156 | { |
sahilmgandhi | 18:6a4db94011d3 | 157 | tCAN->CON |= CAN_CON_TEST_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 158 | tCAN->TEST &= ~(CAN_TEST_LBACK_Msk | CAN_TEST_SILENT_Msk | CAN_TEST_BASIC_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 159 | tCAN->CON &= (~CAN_CON_TEST_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 160 | } |
sahilmgandhi | 18:6a4db94011d3 | 161 | |
sahilmgandhi | 18:6a4db94011d3 | 162 | /** |
sahilmgandhi | 18:6a4db94011d3 | 163 | * @brief Get the waiting status of a received message. |
sahilmgandhi | 18:6a4db94011d3 | 164 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 165 | * @param[in] u8MsgObj Specifies the Message object number, from 0 to 31. |
sahilmgandhi | 18:6a4db94011d3 | 166 | * @retval non-zero The corresponding message object has a new data bit is set. |
sahilmgandhi | 18:6a4db94011d3 | 167 | * @retval 0 No message object has new data. |
sahilmgandhi | 18:6a4db94011d3 | 168 | * @details This function is used to get the waiting status of a received message. |
sahilmgandhi | 18:6a4db94011d3 | 169 | */ |
sahilmgandhi | 18:6a4db94011d3 | 170 | uint32_t CAN_IsNewDataReceived(CAN_T *tCAN, uint8_t u8MsgObj) |
sahilmgandhi | 18:6a4db94011d3 | 171 | { |
sahilmgandhi | 18:6a4db94011d3 | 172 | return (u8MsgObj < 16 ? tCAN->NDAT1 & (1 << u8MsgObj) : tCAN->NDAT2 & (1 << (u8MsgObj-16))); |
sahilmgandhi | 18:6a4db94011d3 | 173 | } |
sahilmgandhi | 18:6a4db94011d3 | 174 | |
sahilmgandhi | 18:6a4db94011d3 | 175 | |
sahilmgandhi | 18:6a4db94011d3 | 176 | /** |
sahilmgandhi | 18:6a4db94011d3 | 177 | * @brief Send CAN message in BASIC mode of test mode |
sahilmgandhi | 18:6a4db94011d3 | 178 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 179 | * @param[in] pCanMsg Pointer to the message structure containing data to transmit. |
sahilmgandhi | 18:6a4db94011d3 | 180 | * @return TRUE: Transmission OK |
sahilmgandhi | 18:6a4db94011d3 | 181 | * FALSE: Check busy flag of interface 0 is timeout |
sahilmgandhi | 18:6a4db94011d3 | 182 | * @details The function is used to send CAN message in BASIC mode of test mode. Before call the API, |
sahilmgandhi | 18:6a4db94011d3 | 183 | * the user should be call CAN_EnterTestMode(CAN_TESTR_BASIC) and let CAN controller enter |
sahilmgandhi | 18:6a4db94011d3 | 184 | * basic mode of test mode. Please notice IF1 Registers used as Tx Buffer in basic mode. |
sahilmgandhi | 18:6a4db94011d3 | 185 | */ |
sahilmgandhi | 18:6a4db94011d3 | 186 | int32_t CAN_BasicSendMsg(CAN_T *tCAN, STR_CANMSG_T* pCanMsg) |
sahilmgandhi | 18:6a4db94011d3 | 187 | { |
sahilmgandhi | 18:6a4db94011d3 | 188 | uint32_t i=0; |
sahilmgandhi | 18:6a4db94011d3 | 189 | while(tCAN->IF[0].CREQ & CAN_IF_CREQ_BUSY_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 190 | |
sahilmgandhi | 18:6a4db94011d3 | 191 | tCAN->STATUS &= (~CAN_STATUS_TXOK_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 192 | |
sahilmgandhi | 18:6a4db94011d3 | 193 | tCAN->IF[0].CMASK = CAN_IF_CMASK_WRRD_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 194 | |
sahilmgandhi | 18:6a4db94011d3 | 195 | if (pCanMsg->IdType == CAN_STD_ID) { |
sahilmgandhi | 18:6a4db94011d3 | 196 | /* standard ID*/ |
sahilmgandhi | 18:6a4db94011d3 | 197 | tCAN->IF[0].ARB1 = 0; |
sahilmgandhi | 18:6a4db94011d3 | 198 | tCAN->IF[0].ARB2 = (((pCanMsg->Id)&0x7FF)<<2) ; |
sahilmgandhi | 18:6a4db94011d3 | 199 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 200 | /* extended ID*/ |
sahilmgandhi | 18:6a4db94011d3 | 201 | tCAN->IF[0].ARB1 = (pCanMsg->Id)&0xFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 202 | tCAN->IF[0].ARB2 = ((pCanMsg->Id)&0x1FFF0000)>>16 | CAN_IF_ARB2_XTD_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 203 | |
sahilmgandhi | 18:6a4db94011d3 | 204 | } |
sahilmgandhi | 18:6a4db94011d3 | 205 | |
sahilmgandhi | 18:6a4db94011d3 | 206 | if(pCanMsg->FrameType) |
sahilmgandhi | 18:6a4db94011d3 | 207 | tCAN->IF[0].ARB2 |= CAN_IF_ARB2_DIR_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 208 | else |
sahilmgandhi | 18:6a4db94011d3 | 209 | tCAN->IF[0].ARB2 &= (~CAN_IF_ARB2_DIR_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 210 | |
sahilmgandhi | 18:6a4db94011d3 | 211 | tCAN->IF[0].MCON = (tCAN->IF[0].MCON & (~CAN_IF_MCON_DLC_Msk)) | pCanMsg->DLC; |
sahilmgandhi | 18:6a4db94011d3 | 212 | tCAN->IF[0].DAT_A1 = ((uint16_t)pCanMsg->Data[1]<<8) | pCanMsg->Data[0]; |
sahilmgandhi | 18:6a4db94011d3 | 213 | tCAN->IF[0].DAT_A2 = ((uint16_t)pCanMsg->Data[3]<<8) | pCanMsg->Data[2]; |
sahilmgandhi | 18:6a4db94011d3 | 214 | tCAN->IF[0].DAT_B1 = ((uint16_t)pCanMsg->Data[5]<<8) | pCanMsg->Data[4]; |
sahilmgandhi | 18:6a4db94011d3 | 215 | tCAN->IF[0].DAT_B2 = ((uint16_t)pCanMsg->Data[7]<<8) | pCanMsg->Data[6]; |
sahilmgandhi | 18:6a4db94011d3 | 216 | |
sahilmgandhi | 18:6a4db94011d3 | 217 | /* request transmission*/ |
sahilmgandhi | 18:6a4db94011d3 | 218 | tCAN->IF[0].CREQ &= (~CAN_IF_CREQ_BUSY_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 219 | if(tCAN->IF[0].CREQ & CAN_IF_CREQ_BUSY_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 220 | DEBUG_PRINTF("Cannot clear busy for sending ...\n"); |
sahilmgandhi | 18:6a4db94011d3 | 221 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 222 | } |
sahilmgandhi | 18:6a4db94011d3 | 223 | |
sahilmgandhi | 18:6a4db94011d3 | 224 | tCAN->IF[0].CREQ |= CAN_IF_CREQ_BUSY_Msk; // sending |
sahilmgandhi | 18:6a4db94011d3 | 225 | |
sahilmgandhi | 18:6a4db94011d3 | 226 | for ( i=0; i<0xFFFFF; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 227 | if((tCAN->IF[0].CREQ & CAN_IF_CREQ_BUSY_Msk) == 0) break; |
sahilmgandhi | 18:6a4db94011d3 | 228 | } |
sahilmgandhi | 18:6a4db94011d3 | 229 | |
sahilmgandhi | 18:6a4db94011d3 | 230 | if ( i >= 0xFFFFF ) { |
sahilmgandhi | 18:6a4db94011d3 | 231 | DEBUG_PRINTF("Cannot send out...\n"); |
sahilmgandhi | 18:6a4db94011d3 | 232 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 233 | } |
sahilmgandhi | 18:6a4db94011d3 | 234 | |
sahilmgandhi | 18:6a4db94011d3 | 235 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 236 | } |
sahilmgandhi | 18:6a4db94011d3 | 237 | |
sahilmgandhi | 18:6a4db94011d3 | 238 | |
sahilmgandhi | 18:6a4db94011d3 | 239 | /** |
sahilmgandhi | 18:6a4db94011d3 | 240 | * @brief Get a message information in BASIC mode. |
sahilmgandhi | 18:6a4db94011d3 | 241 | * |
sahilmgandhi | 18:6a4db94011d3 | 242 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 243 | * @param[out] pCanMsg Pointer to the message structure where received data is copied. |
sahilmgandhi | 18:6a4db94011d3 | 244 | * |
sahilmgandhi | 18:6a4db94011d3 | 245 | * @return FALSE No any message received. \n |
sahilmgandhi | 18:6a4db94011d3 | 246 | * TRUE Receive a message success. |
sahilmgandhi | 18:6a4db94011d3 | 247 | * |
sahilmgandhi | 18:6a4db94011d3 | 248 | */ |
sahilmgandhi | 18:6a4db94011d3 | 249 | int32_t CAN_BasicReceiveMsg(CAN_T *tCAN, STR_CANMSG_T* pCanMsg) |
sahilmgandhi | 18:6a4db94011d3 | 250 | { |
sahilmgandhi | 18:6a4db94011d3 | 251 | if((tCAN->IF[1].MCON & CAN_IF_MCON_NEWDAT_Msk) == 0) { /* In basic mode, receive data always save in IF2 */ |
sahilmgandhi | 18:6a4db94011d3 | 252 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 253 | } |
sahilmgandhi | 18:6a4db94011d3 | 254 | |
sahilmgandhi | 18:6a4db94011d3 | 255 | tCAN->STATUS &= (~CAN_STATUS_RXOK_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 256 | |
sahilmgandhi | 18:6a4db94011d3 | 257 | tCAN->IF[1].CMASK = CAN_IF_CMASK_ARB_Msk |
sahilmgandhi | 18:6a4db94011d3 | 258 | | CAN_IF_CMASK_CONTROL_Msk |
sahilmgandhi | 18:6a4db94011d3 | 259 | | CAN_IF_CMASK_DATAA_Msk |
sahilmgandhi | 18:6a4db94011d3 | 260 | | CAN_IF_CMASK_DATAB_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 261 | |
sahilmgandhi | 18:6a4db94011d3 | 262 | if((tCAN->IF[1].ARB2 & CAN_IF_ARB2_XTD_Msk) == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 263 | /* standard ID*/ |
sahilmgandhi | 18:6a4db94011d3 | 264 | pCanMsg->IdType = CAN_STD_ID; |
sahilmgandhi | 18:6a4db94011d3 | 265 | pCanMsg->Id = (tCAN->IF[1].ARB2 >> 2) & 0x07FF; |
sahilmgandhi | 18:6a4db94011d3 | 266 | |
sahilmgandhi | 18:6a4db94011d3 | 267 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 268 | /* extended ID*/ |
sahilmgandhi | 18:6a4db94011d3 | 269 | pCanMsg->IdType = CAN_EXT_ID; |
sahilmgandhi | 18:6a4db94011d3 | 270 | pCanMsg->Id = (tCAN->IF[1].ARB2 & 0x1FFF)<<16; |
sahilmgandhi | 18:6a4db94011d3 | 271 | pCanMsg->Id |= (uint32_t)tCAN->IF[1].ARB1; |
sahilmgandhi | 18:6a4db94011d3 | 272 | } |
sahilmgandhi | 18:6a4db94011d3 | 273 | |
sahilmgandhi | 18:6a4db94011d3 | 274 | pCanMsg->FrameType = !((tCAN->IF[1].ARB2 & CAN_IF_ARB2_DIR_Msk) >> CAN_IF_ARB2_DIR_Pos); |
sahilmgandhi | 18:6a4db94011d3 | 275 | |
sahilmgandhi | 18:6a4db94011d3 | 276 | pCanMsg->DLC = tCAN->IF[1].MCON & CAN_IF_MCON_DLC_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 277 | pCanMsg->Data[0] = tCAN->IF[1].DAT_A1 & CAN_IF_DAT_A1_DATA0_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 278 | pCanMsg->Data[1] = (tCAN->IF[1].DAT_A1 & CAN_IF_DAT_A1_DATA1_Msk) >> CAN_IF_DAT_A1_DATA1_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 279 | pCanMsg->Data[2] = tCAN->IF[1].DAT_A2 & CAN_IF_DAT_A2_DATA2_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 280 | pCanMsg->Data[3] = (tCAN->IF[1].DAT_A2 & CAN_IF_DAT_A2_DATA3_Msk) >> CAN_IF_DAT_A2_DATA3_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 281 | pCanMsg->Data[4] = tCAN->IF[1].DAT_B1 & CAN_IF_DAT_B1_DATA4_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 282 | pCanMsg->Data[5] = (tCAN->IF[1].DAT_B1 & CAN_IF_DAT_B1_DATA5_Msk) >> CAN_IF_DAT_B1_DATA5_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 283 | pCanMsg->Data[6] = tCAN->IF[1].DAT_B2 & CAN_IF_DAT_B2_DATA6_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 284 | pCanMsg->Data[7] = (tCAN->IF[1].DAT_B2 & CAN_IF_DAT_B2_DATA7_Msk) >> CAN_IF_DAT_B2_DATA7_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 285 | |
sahilmgandhi | 18:6a4db94011d3 | 286 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 287 | } |
sahilmgandhi | 18:6a4db94011d3 | 288 | |
sahilmgandhi | 18:6a4db94011d3 | 289 | /** |
sahilmgandhi | 18:6a4db94011d3 | 290 | * @brief Set Rx message object |
sahilmgandhi | 18:6a4db94011d3 | 291 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 292 | * @param[in] u8MsgObj Specifies the Message object number, from 0 to 31. |
sahilmgandhi | 18:6a4db94011d3 | 293 | * @param[in] u8idType Specifies the identifier type of the frames that will be transmitted |
sahilmgandhi | 18:6a4db94011d3 | 294 | * This parameter can be one of the following values: |
sahilmgandhi | 18:6a4db94011d3 | 295 | * CAN_STD_ID (standard ID, 11-bit) |
sahilmgandhi | 18:6a4db94011d3 | 296 | * CAN_EXT_ID (extended ID, 29-bit) |
sahilmgandhi | 18:6a4db94011d3 | 297 | * @param[in] u32id Specifies the identifier used for acceptance filtering. |
sahilmgandhi | 18:6a4db94011d3 | 298 | * @param[in] u8singleOrFifoLast Specifies the end-of-buffer indicator. |
sahilmgandhi | 18:6a4db94011d3 | 299 | * This parameter can be one of the following values: |
sahilmgandhi | 18:6a4db94011d3 | 300 | * TRUE: for a single receive object or a FIFO receive object that is the last one of the FIFO. |
sahilmgandhi | 18:6a4db94011d3 | 301 | * FALSE: for a FIFO receive object that is not the last one. |
sahilmgandhi | 18:6a4db94011d3 | 302 | * @retval TRUE SUCCESS |
sahilmgandhi | 18:6a4db94011d3 | 303 | * @retval FALSE No useful interface |
sahilmgandhi | 18:6a4db94011d3 | 304 | * @details The function is used to configure a receive message object. |
sahilmgandhi | 18:6a4db94011d3 | 305 | */ |
sahilmgandhi | 18:6a4db94011d3 | 306 | int32_t CAN_SetRxMsgObj(CAN_T *tCAN, uint8_t u8MsgObj, uint8_t u8idType, uint32_t u32id, uint8_t u8singleOrFifoLast) |
sahilmgandhi | 18:6a4db94011d3 | 307 | { |
sahilmgandhi | 18:6a4db94011d3 | 308 | uint8_t u8MsgIfNum=0; |
sahilmgandhi | 18:6a4db94011d3 | 309 | |
sahilmgandhi | 18:6a4db94011d3 | 310 | if ((u8MsgIfNum = GetFreeIF(tCAN)) == 2) { /* Check Free Interface for configure */ |
sahilmgandhi | 18:6a4db94011d3 | 311 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 312 | } |
sahilmgandhi | 18:6a4db94011d3 | 313 | /* Command Setting */ |
sahilmgandhi | 18:6a4db94011d3 | 314 | tCAN->IF[u8MsgIfNum].CMASK = CAN_IF_CMASK_WRRD_Msk | CAN_IF_CMASK_MASK_Msk | CAN_IF_CMASK_ARB_Msk | |
sahilmgandhi | 18:6a4db94011d3 | 315 | CAN_IF_CMASK_CONTROL_Msk | CAN_IF_CMASK_DATAA_Msk | CAN_IF_CMASK_DATAB_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 316 | |
sahilmgandhi | 18:6a4db94011d3 | 317 | if (u8idType == CAN_STD_ID) { /* According STD/EXT ID format,Configure Mask and Arbitration register */ |
sahilmgandhi | 18:6a4db94011d3 | 318 | tCAN->IF[u8MsgIfNum].ARB1 = 0; |
sahilmgandhi | 18:6a4db94011d3 | 319 | tCAN->IF[u8MsgIfNum].ARB2 = CAN_IF_ARB2_MSGVAL_Msk | (u32id & 0x7FF)<< 2; |
sahilmgandhi | 18:6a4db94011d3 | 320 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 321 | tCAN->IF[u8MsgIfNum].ARB1 = u32id & 0xFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 322 | tCAN->IF[u8MsgIfNum].ARB2 = CAN_IF_ARB2_MSGVAL_Msk | CAN_IF_ARB2_XTD_Msk | (u32id & 0x1FFF0000)>>16; |
sahilmgandhi | 18:6a4db94011d3 | 323 | } |
sahilmgandhi | 18:6a4db94011d3 | 324 | |
sahilmgandhi | 18:6a4db94011d3 | 325 | tCAN->IF[u8MsgIfNum].MCON |= CAN_IF_MCON_UMASK_Msk | CAN_IF_MCON_RXIE_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 326 | if(u8singleOrFifoLast) |
sahilmgandhi | 18:6a4db94011d3 | 327 | tCAN->IF[u8MsgIfNum].MCON |= CAN_IF_MCON_EOB_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 328 | else |
sahilmgandhi | 18:6a4db94011d3 | 329 | tCAN->IF[u8MsgIfNum].MCON &= (~CAN_IF_MCON_EOB_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 330 | |
sahilmgandhi | 18:6a4db94011d3 | 331 | tCAN->IF[u8MsgIfNum].DAT_A1 = 0; |
sahilmgandhi | 18:6a4db94011d3 | 332 | tCAN->IF[u8MsgIfNum].DAT_A2 = 0; |
sahilmgandhi | 18:6a4db94011d3 | 333 | tCAN->IF[u8MsgIfNum].DAT_B1 = 0; |
sahilmgandhi | 18:6a4db94011d3 | 334 | tCAN->IF[u8MsgIfNum].DAT_B2 = 0; |
sahilmgandhi | 18:6a4db94011d3 | 335 | |
sahilmgandhi | 18:6a4db94011d3 | 336 | tCAN->IF[u8MsgIfNum].CREQ = 1 + u8MsgObj; |
sahilmgandhi | 18:6a4db94011d3 | 337 | |
sahilmgandhi | 18:6a4db94011d3 | 338 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 339 | } |
sahilmgandhi | 18:6a4db94011d3 | 340 | |
sahilmgandhi | 18:6a4db94011d3 | 341 | |
sahilmgandhi | 18:6a4db94011d3 | 342 | /** |
sahilmgandhi | 18:6a4db94011d3 | 343 | * @brief Gets the message |
sahilmgandhi | 18:6a4db94011d3 | 344 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 345 | * @param[in] u8MsgObj Specifies the Message object number, from 0 to 31. |
sahilmgandhi | 18:6a4db94011d3 | 346 | * @param[in] u8Release Specifies the message release indicator. |
sahilmgandhi | 18:6a4db94011d3 | 347 | * This parameter can be one of the following values: |
sahilmgandhi | 18:6a4db94011d3 | 348 | * TRUE: the message object is released when getting the data. |
sahilmgandhi | 18:6a4db94011d3 | 349 | * FALSE:the message object is not released. |
sahilmgandhi | 18:6a4db94011d3 | 350 | * @param[out] pCanMsg Pointer to the message structure where received data is copied. |
sahilmgandhi | 18:6a4db94011d3 | 351 | * @retval TRUE Success |
sahilmgandhi | 18:6a4db94011d3 | 352 | * @retval FALSE No any message received |
sahilmgandhi | 18:6a4db94011d3 | 353 | * @details Gets the message, if received. |
sahilmgandhi | 18:6a4db94011d3 | 354 | */ |
sahilmgandhi | 18:6a4db94011d3 | 355 | int32_t CAN_ReadMsgObj(CAN_T *tCAN, uint8_t u8MsgObj, uint8_t u8Release, STR_CANMSG_T* pCanMsg) |
sahilmgandhi | 18:6a4db94011d3 | 356 | { |
sahilmgandhi | 18:6a4db94011d3 | 357 | if (!CAN_IsNewDataReceived(tCAN, u8MsgObj)) { |
sahilmgandhi | 18:6a4db94011d3 | 358 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 359 | } |
sahilmgandhi | 18:6a4db94011d3 | 360 | |
sahilmgandhi | 18:6a4db94011d3 | 361 | tCAN->STATUS &= (~CAN_STATUS_RXOK_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 362 | |
sahilmgandhi | 18:6a4db94011d3 | 363 | /* read the message contents*/ |
sahilmgandhi | 18:6a4db94011d3 | 364 | tCAN->IF[1].CMASK = CAN_IF_CMASK_MASK_Msk |
sahilmgandhi | 18:6a4db94011d3 | 365 | | CAN_IF_CMASK_ARB_Msk |
sahilmgandhi | 18:6a4db94011d3 | 366 | | CAN_IF_CMASK_CONTROL_Msk |
sahilmgandhi | 18:6a4db94011d3 | 367 | | CAN_IF_CMASK_CLRINTPND_Msk |
sahilmgandhi | 18:6a4db94011d3 | 368 | | (u8Release ? CAN_IF_CMASK_TXRQSTNEWDAT_Msk : 0) |
sahilmgandhi | 18:6a4db94011d3 | 369 | | CAN_IF_CMASK_DATAA_Msk |
sahilmgandhi | 18:6a4db94011d3 | 370 | | CAN_IF_CMASK_DATAB_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 371 | |
sahilmgandhi | 18:6a4db94011d3 | 372 | tCAN->IF[1].CREQ = 1 + u8MsgObj; |
sahilmgandhi | 18:6a4db94011d3 | 373 | |
sahilmgandhi | 18:6a4db94011d3 | 374 | while (tCAN->IF[1].CREQ & CAN_IF_CREQ_BUSY_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 375 | /*Wait*/ |
sahilmgandhi | 18:6a4db94011d3 | 376 | } |
sahilmgandhi | 18:6a4db94011d3 | 377 | |
sahilmgandhi | 18:6a4db94011d3 | 378 | if ((tCAN->IF[1].ARB2 & CAN_IF_ARB2_XTD_Msk) == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 379 | /* standard ID*/ |
sahilmgandhi | 18:6a4db94011d3 | 380 | pCanMsg->IdType = CAN_STD_ID; |
sahilmgandhi | 18:6a4db94011d3 | 381 | pCanMsg->Id = (tCAN->IF[1].ARB2 & CAN_IF_ARB2_ID_Msk) >> 2; |
sahilmgandhi | 18:6a4db94011d3 | 382 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 383 | /* extended ID*/ |
sahilmgandhi | 18:6a4db94011d3 | 384 | pCanMsg->IdType = CAN_EXT_ID; |
sahilmgandhi | 18:6a4db94011d3 | 385 | pCanMsg->Id = (((tCAN->IF[1].ARB2) & 0x1FFF)<<16) | tCAN->IF[1].ARB1; |
sahilmgandhi | 18:6a4db94011d3 | 386 | } |
sahilmgandhi | 18:6a4db94011d3 | 387 | |
sahilmgandhi | 18:6a4db94011d3 | 388 | pCanMsg->DLC = tCAN->IF[1].MCON & CAN_IF_MCON_DLC_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 389 | pCanMsg->Data[0] = tCAN->IF[1].DAT_A1 & CAN_IF_DAT_A1_DATA0_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 390 | pCanMsg->Data[1] = (tCAN->IF[1].DAT_A1 & CAN_IF_DAT_A1_DATA1_Msk) >> CAN_IF_DAT_A1_DATA1_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 391 | pCanMsg->Data[2] = tCAN->IF[1].DAT_A2 & CAN_IF_DAT_A2_DATA2_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 392 | pCanMsg->Data[3] = (tCAN->IF[1].DAT_A2 & CAN_IF_DAT_A2_DATA3_Msk) >> CAN_IF_DAT_A2_DATA3_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 393 | pCanMsg->Data[4] = tCAN->IF[1].DAT_B1 & CAN_IF_DAT_B1_DATA4_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 394 | pCanMsg->Data[5] = (tCAN->IF[1].DAT_B1 & CAN_IF_DAT_B1_DATA5_Msk) >> CAN_IF_DAT_B1_DATA5_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 395 | pCanMsg->Data[6] = tCAN->IF[1].DAT_B2 & CAN_IF_DAT_B2_DATA6_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 396 | pCanMsg->Data[7] = (tCAN->IF[1].DAT_B2 & CAN_IF_DAT_B2_DATA7_Msk) >> CAN_IF_DAT_B2_DATA7_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 397 | |
sahilmgandhi | 18:6a4db94011d3 | 398 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 399 | } |
sahilmgandhi | 18:6a4db94011d3 | 400 | |
sahilmgandhi | 18:6a4db94011d3 | 401 | /// @endcond HIDDEN_SYMBOLS |
sahilmgandhi | 18:6a4db94011d3 | 402 | |
sahilmgandhi | 18:6a4db94011d3 | 403 | |
sahilmgandhi | 18:6a4db94011d3 | 404 | /** |
sahilmgandhi | 18:6a4db94011d3 | 405 | * @brief The function is used to set bus timing parameter according current clock and target baud-rate. |
sahilmgandhi | 18:6a4db94011d3 | 406 | * |
sahilmgandhi | 18:6a4db94011d3 | 407 | * @param[in] tCAN The base address of can module |
sahilmgandhi | 18:6a4db94011d3 | 408 | * @param[in] u32BaudRate The target CAN baud-rate. The range of u32BaudRate is 1~1000KHz |
sahilmgandhi | 18:6a4db94011d3 | 409 | * |
sahilmgandhi | 18:6a4db94011d3 | 410 | * @return u32CurrentBitRate Real baud-rate value |
sahilmgandhi | 18:6a4db94011d3 | 411 | */ |
sahilmgandhi | 18:6a4db94011d3 | 412 | uint32_t CAN_SetBaudRate(CAN_T *tCAN, uint32_t u32BaudRate) |
sahilmgandhi | 18:6a4db94011d3 | 413 | { |
sahilmgandhi | 18:6a4db94011d3 | 414 | uint8_t u8Tseg1,u8Tseg2; |
sahilmgandhi | 18:6a4db94011d3 | 415 | uint32_t u32Brp; |
sahilmgandhi | 18:6a4db94011d3 | 416 | uint32_t u32Value; |
sahilmgandhi | 18:6a4db94011d3 | 417 | |
sahilmgandhi | 18:6a4db94011d3 | 418 | CAN_EnterInitMode(tCAN); |
sahilmgandhi | 18:6a4db94011d3 | 419 | |
sahilmgandhi | 18:6a4db94011d3 | 420 | SystemCoreClockUpdate(); |
sahilmgandhi | 18:6a4db94011d3 | 421 | |
sahilmgandhi | 18:6a4db94011d3 | 422 | #if 0 // original implementation got 5% inaccuracy. |
sahilmgandhi | 18:6a4db94011d3 | 423 | u32Value = SystemCoreClock; |
sahilmgandhi | 18:6a4db94011d3 | 424 | |
sahilmgandhi | 18:6a4db94011d3 | 425 | if(u32BaudRate * 8 < (u32Value/2)) { |
sahilmgandhi | 18:6a4db94011d3 | 426 | u8Tseg1 = 2; |
sahilmgandhi | 18:6a4db94011d3 | 427 | u8Tseg2 = 3; |
sahilmgandhi | 18:6a4db94011d3 | 428 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 429 | u8Tseg1 = 2; |
sahilmgandhi | 18:6a4db94011d3 | 430 | u8Tseg2 = 1; |
sahilmgandhi | 18:6a4db94011d3 | 431 | } |
sahilmgandhi | 18:6a4db94011d3 | 432 | #else |
sahilmgandhi | 18:6a4db94011d3 | 433 | u32Value = SystemCoreClock / u32BaudRate; |
sahilmgandhi | 18:6a4db94011d3 | 434 | /* Fix for most standard baud rates, include 125K */ |
sahilmgandhi | 18:6a4db94011d3 | 435 | |
sahilmgandhi | 18:6a4db94011d3 | 436 | u8Tseg1 = 3; |
sahilmgandhi | 18:6a4db94011d3 | 437 | u8Tseg2 = 2; |
sahilmgandhi | 18:6a4db94011d3 | 438 | while(1) |
sahilmgandhi | 18:6a4db94011d3 | 439 | { |
sahilmgandhi | 18:6a4db94011d3 | 440 | if(((u32Value % (u8Tseg1 + u8Tseg2 + 3)) == 0) | (u8Tseg1 >= 15)) |
sahilmgandhi | 18:6a4db94011d3 | 441 | break; |
sahilmgandhi | 18:6a4db94011d3 | 442 | |
sahilmgandhi | 18:6a4db94011d3 | 443 | u8Tseg1++; |
sahilmgandhi | 18:6a4db94011d3 | 444 | |
sahilmgandhi | 18:6a4db94011d3 | 445 | if((u32Value % (u8Tseg1 + u8Tseg2 + 3)) == 0) |
sahilmgandhi | 18:6a4db94011d3 | 446 | break; |
sahilmgandhi | 18:6a4db94011d3 | 447 | |
sahilmgandhi | 18:6a4db94011d3 | 448 | if(u8Tseg2 < 7) |
sahilmgandhi | 18:6a4db94011d3 | 449 | u8Tseg2++; |
sahilmgandhi | 18:6a4db94011d3 | 450 | } |
sahilmgandhi | 18:6a4db94011d3 | 451 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 452 | u32Brp = SystemCoreClock/(u32BaudRate) / (u8Tseg1 + u8Tseg2 + 3) -1; |
sahilmgandhi | 18:6a4db94011d3 | 453 | |
sahilmgandhi | 18:6a4db94011d3 | 454 | u32Value = ((uint32_t)u8Tseg2 << CAN_BTIME_TSEG2_Pos) | ((uint32_t)u8Tseg1 << CAN_BTIME_TSEG1_Pos) | |
sahilmgandhi | 18:6a4db94011d3 | 455 | (u32Brp & CAN_BTIME_BRP_Msk) | (tCAN->BTIME & CAN_BTIME_SJW_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 456 | tCAN->BTIME = u32Value; |
sahilmgandhi | 18:6a4db94011d3 | 457 | tCAN->BRPE = (u32Brp >> 6) & 0x0F; |
sahilmgandhi | 18:6a4db94011d3 | 458 | |
sahilmgandhi | 18:6a4db94011d3 | 459 | CAN_LeaveInitMode(tCAN); |
sahilmgandhi | 18:6a4db94011d3 | 460 | |
sahilmgandhi | 18:6a4db94011d3 | 461 | return (CAN_GetCANBitRate(tCAN)); |
sahilmgandhi | 18:6a4db94011d3 | 462 | |
sahilmgandhi | 18:6a4db94011d3 | 463 | } |
sahilmgandhi | 18:6a4db94011d3 | 464 | |
sahilmgandhi | 18:6a4db94011d3 | 465 | /** |
sahilmgandhi | 18:6a4db94011d3 | 466 | * @brief The function is used to disable all CAN interrupt. |
sahilmgandhi | 18:6a4db94011d3 | 467 | * |
sahilmgandhi | 18:6a4db94011d3 | 468 | * @param[in] tCAN The base address of can module |
sahilmgandhi | 18:6a4db94011d3 | 469 | * |
sahilmgandhi | 18:6a4db94011d3 | 470 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 471 | */ |
sahilmgandhi | 18:6a4db94011d3 | 472 | void CAN_Close(CAN_T *tCAN) |
sahilmgandhi | 18:6a4db94011d3 | 473 | { |
sahilmgandhi | 18:6a4db94011d3 | 474 | CAN_DisableInt(tCAN, (CAN_CON_IE_Msk|CAN_CON_SIE_Msk|CAN_CON_EIE_Msk)); |
sahilmgandhi | 18:6a4db94011d3 | 475 | } |
sahilmgandhi | 18:6a4db94011d3 | 476 | |
sahilmgandhi | 18:6a4db94011d3 | 477 | /** |
sahilmgandhi | 18:6a4db94011d3 | 478 | * @brief The function is sets bus timing parameter according current clock and target baud-rate. And set CAN operation mode. |
sahilmgandhi | 18:6a4db94011d3 | 479 | * |
sahilmgandhi | 18:6a4db94011d3 | 480 | * @param[in] tCAN The base address of can module |
sahilmgandhi | 18:6a4db94011d3 | 481 | * @param[in] u32BaudRate The target CAN baud-rate. The range of u32BaudRate is 1~1000KHz |
sahilmgandhi | 18:6a4db94011d3 | 482 | * @param[in] u32Mode The CAN operation mode. ( \ref CAN_NORMAL_MODE / \ref CAN_BASIC_MODE) |
sahilmgandhi | 18:6a4db94011d3 | 483 | * |
sahilmgandhi | 18:6a4db94011d3 | 484 | * @return u32CurrentBitRate Real baud-rate value |
sahilmgandhi | 18:6a4db94011d3 | 485 | */ |
sahilmgandhi | 18:6a4db94011d3 | 486 | uint32_t CAN_Open(CAN_T *tCAN, uint32_t u32BaudRate, uint32_t u32Mode) |
sahilmgandhi | 18:6a4db94011d3 | 487 | { |
sahilmgandhi | 18:6a4db94011d3 | 488 | uint32_t u32CurrentBitRate; |
sahilmgandhi | 18:6a4db94011d3 | 489 | |
sahilmgandhi | 18:6a4db94011d3 | 490 | u32CurrentBitRate = CAN_SetBaudRate(tCAN, u32BaudRate); |
sahilmgandhi | 18:6a4db94011d3 | 491 | |
sahilmgandhi | 18:6a4db94011d3 | 492 | if(u32Mode == CAN_BASIC_MODE) |
sahilmgandhi | 18:6a4db94011d3 | 493 | CAN_EnterTestMode(tCAN, CAN_TEST_BASIC_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 494 | |
sahilmgandhi | 18:6a4db94011d3 | 495 | return u32CurrentBitRate; |
sahilmgandhi | 18:6a4db94011d3 | 496 | } |
sahilmgandhi | 18:6a4db94011d3 | 497 | |
sahilmgandhi | 18:6a4db94011d3 | 498 | /** |
sahilmgandhi | 18:6a4db94011d3 | 499 | * @brief The function is used to configure a transmit object. |
sahilmgandhi | 18:6a4db94011d3 | 500 | * |
sahilmgandhi | 18:6a4db94011d3 | 501 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 502 | * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31 |
sahilmgandhi | 18:6a4db94011d3 | 503 | * @param[in] pCanMsg Pointer to the message structure where received data is copied. |
sahilmgandhi | 18:6a4db94011d3 | 504 | * |
sahilmgandhi | 18:6a4db94011d3 | 505 | * @return FALSE: No useful interface. \n |
sahilmgandhi | 18:6a4db94011d3 | 506 | * TRUE : Config message object success. |
sahilmgandhi | 18:6a4db94011d3 | 507 | * |
sahilmgandhi | 18:6a4db94011d3 | 508 | */ |
sahilmgandhi | 18:6a4db94011d3 | 509 | int32_t CAN_SetTxMsg(CAN_T *tCAN, uint32_t u32MsgNum , STR_CANMSG_T* pCanMsg) |
sahilmgandhi | 18:6a4db94011d3 | 510 | { |
sahilmgandhi | 18:6a4db94011d3 | 511 | uint8_t u8MsgIfNum=0; |
sahilmgandhi | 18:6a4db94011d3 | 512 | uint32_t i=0; |
sahilmgandhi | 18:6a4db94011d3 | 513 | |
sahilmgandhi | 18:6a4db94011d3 | 514 | while((u8MsgIfNum = GetFreeIF(tCAN)) == 2) { |
sahilmgandhi | 18:6a4db94011d3 | 515 | i++; |
sahilmgandhi | 18:6a4db94011d3 | 516 | if(i > 0x10000000) |
sahilmgandhi | 18:6a4db94011d3 | 517 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 518 | } |
sahilmgandhi | 18:6a4db94011d3 | 519 | |
sahilmgandhi | 18:6a4db94011d3 | 520 | /* update the contents needed for transmission*/ |
sahilmgandhi | 18:6a4db94011d3 | 521 | tCAN->IF[u8MsgIfNum].CMASK = 0xF3; /*CAN_CMASK_WRRD_Msk | CAN_CMASK_MASK_Msk | CAN_CMASK_ARB_Msk |
sahilmgandhi | 18:6a4db94011d3 | 522 | | CAN_CMASK_CONTROL_Msk | CAN_CMASK_DATAA_Msk | CAN_CMASK_DATAB_Msk ; */ |
sahilmgandhi | 18:6a4db94011d3 | 523 | |
sahilmgandhi | 18:6a4db94011d3 | 524 | if (pCanMsg->IdType == CAN_STD_ID) { |
sahilmgandhi | 18:6a4db94011d3 | 525 | /* standard ID*/ |
sahilmgandhi | 18:6a4db94011d3 | 526 | tCAN->IF[u8MsgIfNum].ARB1 = 0; |
sahilmgandhi | 18:6a4db94011d3 | 527 | tCAN->IF[u8MsgIfNum].ARB2 = (((pCanMsg->Id)&0x7FF)<<2) | CAN_IF_ARB2_DIR_Msk | CAN_IF_ARB2_MSGVAL_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 528 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 529 | /* extended ID*/ |
sahilmgandhi | 18:6a4db94011d3 | 530 | tCAN->IF[u8MsgIfNum].ARB1 = (pCanMsg->Id)&0xFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 531 | tCAN->IF[u8MsgIfNum].ARB2 = ((pCanMsg->Id)&0x1FFF0000)>>16 | CAN_IF_ARB2_DIR_Msk |
sahilmgandhi | 18:6a4db94011d3 | 532 | | CAN_IF_ARB2_XTD_Msk | CAN_IF_ARB2_MSGVAL_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 533 | } |
sahilmgandhi | 18:6a4db94011d3 | 534 | |
sahilmgandhi | 18:6a4db94011d3 | 535 | if(pCanMsg->FrameType) |
sahilmgandhi | 18:6a4db94011d3 | 536 | tCAN->IF[u8MsgIfNum].ARB2 |= CAN_IF_ARB2_DIR_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 537 | else |
sahilmgandhi | 18:6a4db94011d3 | 538 | tCAN->IF[u8MsgIfNum].ARB2 &= (~CAN_IF_ARB2_DIR_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 539 | |
sahilmgandhi | 18:6a4db94011d3 | 540 | tCAN->IF[u8MsgIfNum].DAT_A1 = ((uint16_t)pCanMsg->Data[1]<<8) | pCanMsg->Data[0]; |
sahilmgandhi | 18:6a4db94011d3 | 541 | tCAN->IF[u8MsgIfNum].DAT_A2 = ((uint16_t)pCanMsg->Data[3]<<8) | pCanMsg->Data[2]; |
sahilmgandhi | 18:6a4db94011d3 | 542 | tCAN->IF[u8MsgIfNum].DAT_B1 = ((uint16_t)pCanMsg->Data[5]<<8) | pCanMsg->Data[4]; |
sahilmgandhi | 18:6a4db94011d3 | 543 | tCAN->IF[u8MsgIfNum].DAT_B2 = ((uint16_t)pCanMsg->Data[7]<<8) | pCanMsg->Data[6]; |
sahilmgandhi | 18:6a4db94011d3 | 544 | |
sahilmgandhi | 18:6a4db94011d3 | 545 | tCAN->IF[u8MsgIfNum].MCON = CAN_IF_MCON_NEWDAT_Msk | pCanMsg->DLC |CAN_IF_MCON_TXIE_Msk | CAN_IF_MCON_EOB_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 546 | tCAN->IF[u8MsgIfNum].CREQ = 1 + u32MsgNum; |
sahilmgandhi | 18:6a4db94011d3 | 547 | |
sahilmgandhi | 18:6a4db94011d3 | 548 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 549 | } |
sahilmgandhi | 18:6a4db94011d3 | 550 | |
sahilmgandhi | 18:6a4db94011d3 | 551 | /** |
sahilmgandhi | 18:6a4db94011d3 | 552 | * @brief Set transmit request bit |
sahilmgandhi | 18:6a4db94011d3 | 553 | * |
sahilmgandhi | 18:6a4db94011d3 | 554 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 555 | * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31. |
sahilmgandhi | 18:6a4db94011d3 | 556 | * |
sahilmgandhi | 18:6a4db94011d3 | 557 | * @return TRUE: Start transmit message. |
sahilmgandhi | 18:6a4db94011d3 | 558 | */ |
sahilmgandhi | 18:6a4db94011d3 | 559 | int32_t CAN_TriggerTxMsg(CAN_T *tCAN, uint32_t u32MsgNum) |
sahilmgandhi | 18:6a4db94011d3 | 560 | { |
sahilmgandhi | 18:6a4db94011d3 | 561 | STR_CANMSG_T rMsg; |
sahilmgandhi | 18:6a4db94011d3 | 562 | CAN_ReadMsgObj(tCAN, u32MsgNum,TRUE, &rMsg); |
sahilmgandhi | 18:6a4db94011d3 | 563 | tCAN->IF[0].CMASK = CAN_IF_CMASK_WRRD_Msk |CAN_IF_CMASK_TXRQSTNEWDAT_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 564 | tCAN->IF[0].CREQ = 1 + u32MsgNum; |
sahilmgandhi | 18:6a4db94011d3 | 565 | |
sahilmgandhi | 18:6a4db94011d3 | 566 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 567 | } |
sahilmgandhi | 18:6a4db94011d3 | 568 | |
sahilmgandhi | 18:6a4db94011d3 | 569 | /** |
sahilmgandhi | 18:6a4db94011d3 | 570 | * @brief Enable CAN interrupt |
sahilmgandhi | 18:6a4db94011d3 | 571 | * |
sahilmgandhi | 18:6a4db94011d3 | 572 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 573 | * @param[in] u32Mask Interrupt Mask. ( \ref CAN_CON_IE_Msk / \ref CAN_CON_SIE_Msk / \ref CAN_CON_EIE_Msk) |
sahilmgandhi | 18:6a4db94011d3 | 574 | * |
sahilmgandhi | 18:6a4db94011d3 | 575 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 576 | */ |
sahilmgandhi | 18:6a4db94011d3 | 577 | void CAN_EnableInt(CAN_T *tCAN, uint32_t u32Mask) |
sahilmgandhi | 18:6a4db94011d3 | 578 | { |
sahilmgandhi | 18:6a4db94011d3 | 579 | CAN_EnterInitMode(tCAN); |
sahilmgandhi | 18:6a4db94011d3 | 580 | |
sahilmgandhi | 18:6a4db94011d3 | 581 | tCAN->CON = (tCAN->CON & 0xF1) | ((u32Mask & CAN_CON_IE_Msk )? CAN_CON_IE_Msk :0) |
sahilmgandhi | 18:6a4db94011d3 | 582 | | ((u32Mask & CAN_CON_SIE_Msk )? CAN_CON_SIE_Msk:0) |
sahilmgandhi | 18:6a4db94011d3 | 583 | | ((u32Mask & CAN_CON_EIE_Msk )? CAN_CON_EIE_Msk:0); |
sahilmgandhi | 18:6a4db94011d3 | 584 | |
sahilmgandhi | 18:6a4db94011d3 | 585 | |
sahilmgandhi | 18:6a4db94011d3 | 586 | CAN_LeaveInitMode(tCAN); |
sahilmgandhi | 18:6a4db94011d3 | 587 | } |
sahilmgandhi | 18:6a4db94011d3 | 588 | |
sahilmgandhi | 18:6a4db94011d3 | 589 | /** |
sahilmgandhi | 18:6a4db94011d3 | 590 | * @brief Disable CAN interrupt |
sahilmgandhi | 18:6a4db94011d3 | 591 | * |
sahilmgandhi | 18:6a4db94011d3 | 592 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 593 | * @param[in] u32Mask Interrupt Mask. ( \ref CAN_CON_IE_Msk / \ref CAN_CON_SIE_Msk / \ref CAN_CON_EIE_Msk) |
sahilmgandhi | 18:6a4db94011d3 | 594 | * |
sahilmgandhi | 18:6a4db94011d3 | 595 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 596 | */ |
sahilmgandhi | 18:6a4db94011d3 | 597 | void CAN_DisableInt(CAN_T *tCAN, uint32_t u32Mask) |
sahilmgandhi | 18:6a4db94011d3 | 598 | { |
sahilmgandhi | 18:6a4db94011d3 | 599 | CAN_EnterInitMode(tCAN); |
sahilmgandhi | 18:6a4db94011d3 | 600 | |
sahilmgandhi | 18:6a4db94011d3 | 601 | tCAN->CON = tCAN->CON & ~(CAN_CON_IE_Msk | ((u32Mask & CAN_CON_SIE_Msk)?CAN_CON_SIE_Msk:0) |
sahilmgandhi | 18:6a4db94011d3 | 602 | | ((u32Mask & CAN_CON_EIE_Msk)?CAN_CON_EIE_Msk:0)); |
sahilmgandhi | 18:6a4db94011d3 | 603 | |
sahilmgandhi | 18:6a4db94011d3 | 604 | CAN_LeaveInitMode(tCAN); |
sahilmgandhi | 18:6a4db94011d3 | 605 | } |
sahilmgandhi | 18:6a4db94011d3 | 606 | |
sahilmgandhi | 18:6a4db94011d3 | 607 | |
sahilmgandhi | 18:6a4db94011d3 | 608 | /** |
sahilmgandhi | 18:6a4db94011d3 | 609 | * @brief The function is used to configure a receive message object |
sahilmgandhi | 18:6a4db94011d3 | 610 | * |
sahilmgandhi | 18:6a4db94011d3 | 611 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 612 | * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31 |
sahilmgandhi | 18:6a4db94011d3 | 613 | * @param[in] u32IDType Specifies the identifier type of the frames that will be transmitted. ( \ref CAN_STD_ID / \ref CAN_EXT_ID) |
sahilmgandhi | 18:6a4db94011d3 | 614 | * @param[in] u32ID Specifies the identifier used for acceptance filtering. |
sahilmgandhi | 18:6a4db94011d3 | 615 | * |
sahilmgandhi | 18:6a4db94011d3 | 616 | * @return FALSE: No useful interface \n |
sahilmgandhi | 18:6a4db94011d3 | 617 | * TRUE : Configure a receive message object success. |
sahilmgandhi | 18:6a4db94011d3 | 618 | * |
sahilmgandhi | 18:6a4db94011d3 | 619 | */ |
sahilmgandhi | 18:6a4db94011d3 | 620 | int32_t CAN_SetRxMsg(CAN_T *tCAN, uint32_t u32MsgNum , uint32_t u32IDType, uint32_t u32ID) |
sahilmgandhi | 18:6a4db94011d3 | 621 | { |
sahilmgandhi | 18:6a4db94011d3 | 622 | uint32_t u32TimeOutCount = 0; |
sahilmgandhi | 18:6a4db94011d3 | 623 | |
sahilmgandhi | 18:6a4db94011d3 | 624 | while(CAN_SetRxMsgObj(tCAN, u32MsgNum, u32IDType, u32ID, TRUE) == FALSE) { |
sahilmgandhi | 18:6a4db94011d3 | 625 | u32TimeOutCount++; |
sahilmgandhi | 18:6a4db94011d3 | 626 | |
sahilmgandhi | 18:6a4db94011d3 | 627 | if(u32TimeOutCount >= 0x10000000) return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 628 | } |
sahilmgandhi | 18:6a4db94011d3 | 629 | |
sahilmgandhi | 18:6a4db94011d3 | 630 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 631 | } |
sahilmgandhi | 18:6a4db94011d3 | 632 | |
sahilmgandhi | 18:6a4db94011d3 | 633 | /** |
sahilmgandhi | 18:6a4db94011d3 | 634 | * @brief The function is used to configure several receive message objects |
sahilmgandhi | 18:6a4db94011d3 | 635 | * |
sahilmgandhi | 18:6a4db94011d3 | 636 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 637 | * @param[in] u32MsgNum The starting MSG RAM number. (0 ~ 31) |
sahilmgandhi | 18:6a4db94011d3 | 638 | * @param[in] u32MsgCount the number of MSG RAM of the FIFO. |
sahilmgandhi | 18:6a4db94011d3 | 639 | * @param[in] u32IDType Specifies the identifier type of the frames that will be transmitted. ( \ref CAN_STD_ID / \ref CAN_EXT_ID) |
sahilmgandhi | 18:6a4db94011d3 | 640 | * @param[in] u32ID Specifies the identifier used for acceptance filtering. |
sahilmgandhi | 18:6a4db94011d3 | 641 | * |
sahilmgandhi | 18:6a4db94011d3 | 642 | * @return FALSE: No useful interface \n |
sahilmgandhi | 18:6a4db94011d3 | 643 | * TRUE : Configure receive message objects success. |
sahilmgandhi | 18:6a4db94011d3 | 644 | * |
sahilmgandhi | 18:6a4db94011d3 | 645 | */ |
sahilmgandhi | 18:6a4db94011d3 | 646 | int32_t CAN_SetMultiRxMsg(CAN_T *tCAN, uint32_t u32MsgNum , uint32_t u32MsgCount, uint32_t u32IDType, uint32_t u32ID) |
sahilmgandhi | 18:6a4db94011d3 | 647 | { |
sahilmgandhi | 18:6a4db94011d3 | 648 | uint32_t i = 0; |
sahilmgandhi | 18:6a4db94011d3 | 649 | uint32_t u32TimeOutCount; |
sahilmgandhi | 18:6a4db94011d3 | 650 | uint32_t u32EOB_Flag = 0; |
sahilmgandhi | 18:6a4db94011d3 | 651 | |
sahilmgandhi | 18:6a4db94011d3 | 652 | for(i= 1; i < u32MsgCount; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 653 | u32TimeOutCount = 0; |
sahilmgandhi | 18:6a4db94011d3 | 654 | |
sahilmgandhi | 18:6a4db94011d3 | 655 | u32MsgNum += (i - 1); |
sahilmgandhi | 18:6a4db94011d3 | 656 | |
sahilmgandhi | 18:6a4db94011d3 | 657 | if(i == u32MsgCount) u32EOB_Flag = 1; |
sahilmgandhi | 18:6a4db94011d3 | 658 | |
sahilmgandhi | 18:6a4db94011d3 | 659 | while(CAN_SetRxMsgObj(tCAN, u32MsgNum, u32IDType, u32ID, u32EOB_Flag) == FALSE) { |
sahilmgandhi | 18:6a4db94011d3 | 660 | u32TimeOutCount++; |
sahilmgandhi | 18:6a4db94011d3 | 661 | |
sahilmgandhi | 18:6a4db94011d3 | 662 | if(u32TimeOutCount >= 0x10000000) return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 663 | } |
sahilmgandhi | 18:6a4db94011d3 | 664 | } |
sahilmgandhi | 18:6a4db94011d3 | 665 | |
sahilmgandhi | 18:6a4db94011d3 | 666 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 667 | } |
sahilmgandhi | 18:6a4db94011d3 | 668 | |
sahilmgandhi | 18:6a4db94011d3 | 669 | |
sahilmgandhi | 18:6a4db94011d3 | 670 | /** |
sahilmgandhi | 18:6a4db94011d3 | 671 | * @brief Send CAN message. |
sahilmgandhi | 18:6a4db94011d3 | 672 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 673 | * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31 |
sahilmgandhi | 18:6a4db94011d3 | 674 | * @param[in] pCanMsg Pointer to the message structure where received data is copied. |
sahilmgandhi | 18:6a4db94011d3 | 675 | * |
sahilmgandhi | 18:6a4db94011d3 | 676 | * @return FALSE: When operation in basic mode: Transmit message time out, or when operation in normal mode: No useful interface. \n |
sahilmgandhi | 18:6a4db94011d3 | 677 | * TRUE : Transmit Message success. |
sahilmgandhi | 18:6a4db94011d3 | 678 | */ |
sahilmgandhi | 18:6a4db94011d3 | 679 | int32_t CAN_Transmit(CAN_T *tCAN, uint32_t u32MsgNum , STR_CANMSG_T* pCanMsg) |
sahilmgandhi | 18:6a4db94011d3 | 680 | { |
sahilmgandhi | 18:6a4db94011d3 | 681 | if((tCAN->CON & CAN_CON_TEST_Msk) && (tCAN->TEST & CAN_TEST_BASIC_Msk)) { |
sahilmgandhi | 18:6a4db94011d3 | 682 | return (CAN_BasicSendMsg(tCAN, pCanMsg)); |
sahilmgandhi | 18:6a4db94011d3 | 683 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 684 | if(CAN_SetTxMsg(tCAN, u32MsgNum, pCanMsg) == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 685 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 686 | CAN_TriggerTxMsg(tCAN, u32MsgNum); |
sahilmgandhi | 18:6a4db94011d3 | 687 | } |
sahilmgandhi | 18:6a4db94011d3 | 688 | |
sahilmgandhi | 18:6a4db94011d3 | 689 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 690 | } |
sahilmgandhi | 18:6a4db94011d3 | 691 | |
sahilmgandhi | 18:6a4db94011d3 | 692 | |
sahilmgandhi | 18:6a4db94011d3 | 693 | /** |
sahilmgandhi | 18:6a4db94011d3 | 694 | * @brief Gets the message, if received. |
sahilmgandhi | 18:6a4db94011d3 | 695 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 696 | * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31 |
sahilmgandhi | 18:6a4db94011d3 | 697 | * @param[out] pCanMsg Pointer to the message structure where received data is copied. |
sahilmgandhi | 18:6a4db94011d3 | 698 | * |
sahilmgandhi | 18:6a4db94011d3 | 699 | * @return FALSE: No any message received. \n |
sahilmgandhi | 18:6a4db94011d3 | 700 | * TRUE : Receive Message success. |
sahilmgandhi | 18:6a4db94011d3 | 701 | */ |
sahilmgandhi | 18:6a4db94011d3 | 702 | int32_t CAN_Receive(CAN_T *tCAN, uint32_t u32MsgNum , STR_CANMSG_T* pCanMsg) |
sahilmgandhi | 18:6a4db94011d3 | 703 | { |
sahilmgandhi | 18:6a4db94011d3 | 704 | if((tCAN->CON & CAN_CON_TEST_Msk) && (tCAN->TEST & CAN_TEST_BASIC_Msk)) { |
sahilmgandhi | 18:6a4db94011d3 | 705 | return (CAN_BasicReceiveMsg(tCAN, pCanMsg)); |
sahilmgandhi | 18:6a4db94011d3 | 706 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 707 | return CAN_ReadMsgObj(tCAN, u32MsgNum, TRUE, pCanMsg); |
sahilmgandhi | 18:6a4db94011d3 | 708 | } |
sahilmgandhi | 18:6a4db94011d3 | 709 | } |
sahilmgandhi | 18:6a4db94011d3 | 710 | |
sahilmgandhi | 18:6a4db94011d3 | 711 | /** |
sahilmgandhi | 18:6a4db94011d3 | 712 | * @brief Clear interrupt pending bit. |
sahilmgandhi | 18:6a4db94011d3 | 713 | * @param[in] tCAN The base address of can module. |
sahilmgandhi | 18:6a4db94011d3 | 714 | * @param[in] u32MsgNum Specifies the Message object number, from 0 to 31 |
sahilmgandhi | 18:6a4db94011d3 | 715 | * |
sahilmgandhi | 18:6a4db94011d3 | 716 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 717 | * |
sahilmgandhi | 18:6a4db94011d3 | 718 | */ |
sahilmgandhi | 18:6a4db94011d3 | 719 | void CAN_CLR_INT_PENDING_BIT(CAN_T *tCAN, uint8_t u32MsgNum) |
sahilmgandhi | 18:6a4db94011d3 | 720 | { |
sahilmgandhi | 18:6a4db94011d3 | 721 | uint32_t u32MsgIfNum = 0; |
sahilmgandhi | 18:6a4db94011d3 | 722 | uint32_t u32IFBusyCount = 0; |
sahilmgandhi | 18:6a4db94011d3 | 723 | |
sahilmgandhi | 18:6a4db94011d3 | 724 | while(u32IFBusyCount < 0x10000000) { |
sahilmgandhi | 18:6a4db94011d3 | 725 | if((tCAN->IF[0].CREQ & CAN_IF_CREQ_BUSY_Msk) == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 726 | u32MsgIfNum = 0; |
sahilmgandhi | 18:6a4db94011d3 | 727 | break; |
sahilmgandhi | 18:6a4db94011d3 | 728 | } else if((tCAN->IF[1].CREQ & CAN_IF_CREQ_BUSY_Msk) == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 729 | u32MsgIfNum = 1; |
sahilmgandhi | 18:6a4db94011d3 | 730 | break; |
sahilmgandhi | 18:6a4db94011d3 | 731 | } |
sahilmgandhi | 18:6a4db94011d3 | 732 | |
sahilmgandhi | 18:6a4db94011d3 | 733 | u32IFBusyCount++; |
sahilmgandhi | 18:6a4db94011d3 | 734 | } |
sahilmgandhi | 18:6a4db94011d3 | 735 | |
sahilmgandhi | 18:6a4db94011d3 | 736 | tCAN->IF[u32MsgIfNum].CMASK = CAN_IF_CMASK_CLRINTPND_Msk | CAN_IF_CMASK_TXRQSTNEWDAT_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 737 | tCAN->IF[u32MsgIfNum].CREQ = 1 + u32MsgNum; |
sahilmgandhi | 18:6a4db94011d3 | 738 | |
sahilmgandhi | 18:6a4db94011d3 | 739 | } |
sahilmgandhi | 18:6a4db94011d3 | 740 | |
sahilmgandhi | 18:6a4db94011d3 | 741 | |
sahilmgandhi | 18:6a4db94011d3 | 742 | /*@}*/ /* end of group NUC472_442_CAN_EXPORTED_FUNCTIONS */ |
sahilmgandhi | 18:6a4db94011d3 | 743 | |
sahilmgandhi | 18:6a4db94011d3 | 744 | /*@}*/ /* end of group NUC472_442_CAN_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 745 | |
sahilmgandhi | 18:6a4db94011d3 | 746 | /*@}*/ /* end of group NUC472_442_Device_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 747 | |
sahilmgandhi | 18:6a4db94011d3 | 748 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ |
sahilmgandhi | 18:6a4db94011d3 | 749 | |
sahilmgandhi | 18:6a4db94011d3 | 750 |