08MULTICAN-LB - 图1

    1. /*********************************************************************************************************************/
    2. /*-----------------------------------------------------Includes------------------------------------------------------*/
    3. /*********************************************************************************************************************/
    4. #include "MULTICAN.h"
    5. /*********************************************************************************************************************/
    6. /*-------------------------------------------------Global variables--------------------------------------------------*/
    7. /*********************************************************************************************************************/
    8. AppMulticanType g_multican; /* Global MULTICAN configuration and control structure */
    9. AppLedType g_led; /* Global LED configuration and control structure */
    10. /*********************************************************************************************************************/
    11. /*---------------------------------------------Function Implementations----------------------------------------------*/
    12. /*********************************************************************************************************************/
    13. /* Macro to define Interrupt Service Routine.
    14. * This macro:
    15. * - defines linker section as .intvec_tc<vector number>_<interrupt priority>.
    16. * - defines compiler specific attribute for the interrupt functions.
    17. * - defines the Interrupt service routine as ISR function.
    18. *
    19. * IFX_INTERRUPT(isr, vectabNum, priority)
    20. * - isr: Name of the ISR function.
    21. * - vectabNum: Vector table number.
    22. * - priority: Interrupt priority. Refer Usage of Interrupt Macro for more details.
    23. */
    24. IFX_INTERRUPT(canIsrTxHandler, 0, ISR_PRIORITY_CAN_TX);
    25. IFX_INTERRUPT(canIsrRxHandler, 0, ISR_PRIORITY_CAN_RX);
    26. /* Interrupt Service Routine (ISR) called once the TX interrupt has been generated.
    27. * Turns on the LED1 to indicate successful CAN message transmission.
    28. */
    29. void canIsrTxHandler(void)
    30. {
    31. /* Just to indicate that the CAN message has been transmitted by turning on LED1 */
    32. IfxPort_setPinLow(g_led.led1.port, g_led.led1.pinIndex);
    33. }
    34. /* Interrupt Service Routine (ISR) called once the RX interrupt has been generated.
    35. * Compares the content of the received CAN message with the content of the transmitted CAN message
    36. * and in case of success, turns on the LED2 to indicate successful CAN message reception.
    37. */
    38. void canIsrRxHandler(void)
    39. {
    40. IfxMultican_Status readStatus;
    41. /* Read the received CAN message and store the status of the operation */
    42. readStatus = IfxMultican_Can_MsgObj_readMessage(&g_multican.canDstMsgObj, &g_multican.rxMsg);
    43. /* If no new data has been received, report an error */
    44. if( !( readStatus & IfxMultican_Status_newData ) )
    45. {
    46. while(1)
    47. {
    48. }
    49. }
    50. /* If new data has been received but with one message lost, report an error */
    51. if( readStatus == IfxMultican_Status_newDataButOneLost )
    52. {
    53. while(1)
    54. {
    55. }
    56. }
    57. /* Finally, check if the received data matches with the transmitted one */
    58. if( ( g_multican.rxMsg.data[0] == g_multican.txMsg.data[0] ) &&
    59. ( g_multican.rxMsg.data[1] == g_multican.txMsg.data[1] ) &&
    60. ( g_multican.rxMsg.id == g_multican.txMsg.id ) )
    61. {
    62. /* Turn on the LED2 to indicate correctness of the received message */
    63. IfxPort_setPinLow(g_led.led2.port, g_led.led2.pinIndex);
    64. }
    65. }
    66. /* Function to initialize MULTICAN module, nodes and message objects related for this application use case */
    67. void initMultican(void)
    68. {
    69. /* ==========================================================================================
    70. * CAN module configuration and initialization:
    71. * ==========================================================================================
    72. * - load default CAN module configuration into configuration structure
    73. *
    74. * - define the interrupt priority for both interrupt node pointers used in the example
    75. *
    76. * - initialize CAN module with the modified configuration
    77. * ==========================================================================================
    78. */
    79. IfxMultican_Can_initModuleConfig(&g_multican.canConfig, &MODULE_CAN);
    80. g_multican.canConfig.nodePointer[TX_INTERRUPT_SRC_ID].priority = ISR_PRIORITY_CAN_TX;
    81. g_multican.canConfig.nodePointer[RX_INTERRUPT_SRC_ID].priority = ISR_PRIORITY_CAN_RX;
    82. IfxMultican_Can_initModule(&g_multican.can, &g_multican.canConfig);
    83. /* ==========================================================================================
    84. * Source CAN node configuration and initialization:源CAN节点配置和初始化:
    85. * ==========================================================================================
    86. * - load default CAN node configuration into configuration structure
    87. *
    88. * - set source CAN node in the "Loop-Back" mode (no external pins will be used)
    89. * - assign source CAN node to CAN node 0
    90. *
    91. * - initialize the source CAN node with the modified configuration
    92. * ==========================================================================================
    93. */
    94. IfxMultican_Can_Node_initConfig(&g_multican.canNodeConfig, &g_multican.can);
    95. g_multican.canNodeConfig.loopBackMode = TRUE;//将源CAN节点设置为“环回”模式(不使用外部引脚)
    96. g_multican.canNodeConfig.nodeId = IfxMultican_NodeId_0;//将源CAN节点分配给CAN节点0
    97. IfxMultican_Can_Node_init(&g_multican.canSrcNode, &g_multican.canNodeConfig);
    98. /* ==========================================================================================
    99. * Destination CAN node configuration and initialization:
    100. * ==========================================================================================
    101. * - load default CAN node configuration into configuration structure
    102. *
    103. * - set destination CAN node in the "Loop-Back" mode (no external pins will be used)
    104. * - assign destination CAN node to CAN node 1
    105. *
    106. * - initialize the destination CAN node with the modified configuration
    107. * ==========================================================================================
    108. */
    109. IfxMultican_Can_Node_initConfig(&g_multican.canNodeConfig, &g_multican.can);
    110. g_multican.canNodeConfig.loopBackMode = TRUE;
    111. g_multican.canNodeConfig.nodeId = IfxMultican_NodeId_1;
    112. IfxMultican_Can_Node_init(&g_multican.canDstNode, &g_multican.canNodeConfig);
    113. /* ==========================================================================================
    114. * Source message object configuration and initialization:源消息对象配置和初始化
    115. * ==========================================================================================
    116. * - load default CAN message object configuration into configuration structure
    117. * 将默认CAN消息对象配置加载到配置结构中
    118. * - define the message object ID
    119. * - define the CAN message ID used during arbitration phase
    120. * - define the message object as a transmit message object
    121. * - enable interrupt generation in case of CAN message transmission
    122. * - define interrupt node pointer to be used
    123. *
    124. * - initialize the source CAN message object with the modified configuration
    125. * ==========================================================================================
    126. */
    127. IfxMultican_Can_MsgObj_initConfig(&g_multican.canMsgObjConfig, &g_multican.canSrcNode);
    128. g_multican.canMsgObjConfig.msgObjId = SRC_MESSAGE_OBJECT_ID;//源消息的对象ID
    129. g_multican.canMsgObjConfig.messageId = CAN_MESSAGE_ID;//仲裁段ID,它决定着数据帧发送的优先级,也决定着其它节点是否会接收这个数据帧
    130. g_multican.canMsgObjConfig.frame = IfxMultican_Frame_transmit;//发送模式
    131. g_multican.canMsgObjConfig.txInterrupt.enabled = TRUE;//发送中断使能
    132. g_multican.canMsgObjConfig.txInterrupt.srcId = TX_INTERRUPT_SRC_ID;//发送中断的服务请求ID,定义要使用的中断节点指针
    133. IfxMultican_Can_MsgObj_init(&g_multican.canSrcMsgObj, &g_multican.canMsgObjConfig);
    134. /* ==========================================================================================
    135. * Destination message object configuration and initialization:
    136. * ==========================================================================================
    137. * - load default CAN message object configuration into configuration structure
    138. *
    139. * - define the message object ID (different than the ID used for source MO)
    140. * - define the CAN message ID used during arbitration phase (same as ID used for source MO)
    141. * - define the message object as a receive message object
    142. * - enable interrupt generation in case of CAN message transmission
    143. * - define interrupt node pointer to be used (different than the one used for source MO)
    144. *
    145. * - initialize the destination CAN message object with the modified configuration
    146. * ==========================================================================================
    147. */
    148. IfxMultican_Can_MsgObj_initConfig(&g_multican.canMsgObjConfig, &g_multican.canDstNode);
    149. g_multican.canMsgObjConfig.msgObjId = DST_MESSAGE_OBJECT_ID;//消息对象ID
    150. g_multican.canMsgObjConfig.messageId = CAN_MESSAGE_ID;//仲裁段ID,它决定着数据帧发送的优先级,也决定着其它节点是否会接收这个数据帧
    151. g_multican.canMsgObjConfig.frame = IfxMultican_Frame_receive;//接收模式
    152. g_multican.canMsgObjConfig.rxInterrupt.enabled = TRUE;
    153. g_multican.canMsgObjConfig.rxInterrupt.srcId = RX_INTERRUPT_SRC_ID;//定义要使用的中断节点指针(不同于用于源MO的指针)
    154. IfxMultican_Can_MsgObj_init(&g_multican.canDstMsgObj, &g_multican.canMsgObjConfig);
    155. }
    156. /* Function to initialize both TX and RX messages with the default data values.
    157. * After initialization of the messages, the TX message will be transmitted.
    158. */
    159. void transmitCanMessage(void)
    160. {
    161. /* Define the content of the data to be transmitted */
    162. const uint32 dataLow = 0xC0CAC01A;
    163. const uint32 dataHigh = 0xBA5EBA11;
    164. /* Invalidation of the RX message */
    165. IfxMultican_Message_init(&g_multican.rxMsg,
    166. INVALID_ID_VALUE,
    167. INVALID_DATA_VALUE,
    168. INVALID_DATA_VALUE,
    169. g_multican.canMsgObjConfig.control.messageLen);
    170. /* Initialization of the TX message */
    171. IfxMultican_Message_init(&g_multican.txMsg,
    172. g_multican.canMsgObjConfig.messageId,
    173. dataLow,//要传输的数据低位
    174. dataHigh,//要传输的数据高位
    175. g_multican.canMsgObjConfig.control.messageLen);
    176. /* Send the CAN message with the previously defined TX message content */
    177. while( IfxMultican_Status_notSentBusy ==
    178. IfxMultican_Can_MsgObj_sendMessage(&g_multican.canSrcMsgObj, &g_multican.txMsg) )
    179. {
    180. }
    181. }
    182. void initLed(void)
    183. {
    184. /* ======================================================================
    185. * Configuration of the pins connected to the LEDs:
    186. * ======================================================================
    187. * - define the GPIO port
    188. * - define the GPIO pin that is the connected to the LED
    189. * - define the general GPIO pin usage (no alternate function used)
    190. * - define the pad driver strength
    191. * ======================================================================
    192. */
    193. g_led.led1.port = &MODULE_P00;
    194. g_led.led1.pinIndex = PIN5;
    195. g_led.led1.mode = IfxPort_OutputIdx_general;
    196. g_led.led1.padDriver = IfxPort_PadDriver_cmosAutomotiveSpeed1;
    197. g_led.led2.port = &MODULE_P00;
    198. g_led.led2.pinIndex = PIN6;
    199. g_led.led2.mode = IfxPort_OutputIdx_general;
    200. g_led.led2.padDriver = IfxPort_PadDriver_cmosAutomotiveSpeed1;
    201. /* Initialize the pins connected to LEDs to level "HIGH"; will keep the LEDs turned off as default state */
    202. IfxPort_setPinHigh(g_led.led1.port, g_led.led1.pinIndex);
    203. IfxPort_setPinHigh(g_led.led2.port, g_led.led2.pinIndex);
    204. /* Set the pin input/output mode for both pins connected to the LEDs */
    205. IfxPort_setPinModeOutput(g_led.led1.port, g_led.led1.pinIndex, IfxPort_OutputMode_pushPull, g_led.led1.mode);
    206. IfxPort_setPinModeOutput(g_led.led2.port, g_led.led2.pinIndex, IfxPort_OutputMode_pushPull, g_led.led2.mode);
    207. /* Set the pad driver mode for both pins connected to the LEDs */
    208. IfxPort_setPinPadDriver(g_led.led1.port, g_led.led1.pinIndex, g_led.led1.padDriver);
    209. IfxPort_setPinPadDriver(g_led.led2.port, g_led.led2.pinIndex, g_led.led2.padDriver);
    210. }
    1. #ifndef MULTICAN_H_
    2. #define MULTICAN_H_ 1
    3. /*********************************************************************************************************************/
    4. /*-----------------------------------------------------Includes------------------------------------------------------*/
    5. /*********************************************************************************************************************/
    6. #include "Ifx_Types.h"
    7. #include "IfxMultican_Can.h"
    8. #include "IfxMultican.h"
    9. #include "IfxPort.h" /* For GPIO Port Pin Control */
    10. /*********************************************************************************************************************/
    11. /*------------------------------------------------------Macros-------------------------------------------------------*/
    12. /*********************************************************************************************************************/
    13. #define SRC_MESSAGE_OBJECT_ID (IfxMultican_MsgObjId)0 /* Source message object ID */
    14. #define DST_MESSAGE_OBJECT_ID (IfxMultican_MsgObjId)1 /* Destination message object ID */
    15. #define CAN_MESSAGE_ID 0x777 /* Message ID that will be used in arbitration phase */
    16. #define TX_INTERRUPT_SRC_ID IfxMultican_SrcId_0 /* Transmit interrupt service request ID */
    17. #define RX_INTERRUPT_SRC_ID IfxMultican_SrcId_1 /* Receive interrupt service request ID */
    18. #define PIN5 5 /* LED1 used in TX ISR is connected to this pin */
    19. #define PIN6 6 /* LED2 used in RX ISR is connected to this pin */
    20. #define INVALID_DATA_VALUE (uint32)0xDEADBEEF /* Used to invalidate RX message data content */
    21. #define INVALID_ID_VALUE (uint32)0xFFFFFFFF /* Used to invalidate RX message ID value */
    22. #define ISR_PRIORITY_CAN_TX 2 /* Define the CAN TX interrupt priority */
    23. #define ISR_PRIORITY_CAN_RX 1 /* Define the CAN RX interrupt priority */
    24. /*********************************************************************************************************************/
    25. /*--------------------------------------------------Data Structures--------------------------------------------------*/
    26. /*********************************************************************************************************************/
    27. typedef struct
    28. {
    29. IfxMultican_Can can; /* CAN module handle to HW module SFR set */
    30. IfxMultican_Can_Config canConfig; /* CAN module configuration structure */
    31. IfxMultican_Can_Node canSrcNode; /* CAN source node handle data structure */
    32. IfxMultican_Can_Node canDstNode; /* CAN destination node handle data structure */
    33. IfxMultican_Can_NodeConfig canNodeConfig; /* CAN node configuration structure */
    34. IfxMultican_Can_MsgObj canSrcMsgObj; /* CAN source message object handle data structure */
    35. IfxMultican_Can_MsgObj canDstMsgObj; /* CAN destination message object handle data structure */
    36. IfxMultican_Can_MsgObjConfig canMsgObjConfig; /* CAN message object configuration structure */
    37. IfxMultican_Message txMsg; /* Transmitted CAN message structure */
    38. IfxMultican_Message rxMsg; /* Received CAN message structure */
    39. } AppMulticanType;
    40. typedef struct
    41. {
    42. IfxPort_Pin_Config led1; /* LED1 configuration structure */
    43. IfxPort_Pin_Config led2; /* LED2 configuration structure */
    44. } AppLedType;
    45. /*********************************************************************************************************************/
    46. /*-----------------------------------------------Function Prototypes-------------------------------------------------*/
    47. /*********************************************************************************************************************/
    48. void canIsrTxHandler(void);
    49. void canIsrRxHandler(void);
    50. void initMultican(void);
    51. void transmitCanMessage(void);
    52. void initLed(void);
    53. #endif /* MULTICAN_H_ */