ls1c_can.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * File : ls1c_can.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs: (Pelican Mode)
  21. * Date Author Notes
  22. * 2018-01-06 sundm75 first version
  23. */
  24. #ifndef __OPENLOONGSON_CAN_H
  25. #define __OPENLOONGSON_CAN_H
  26. #define CAN0 ( (CAN_TypeDef* )LS1C_REG_BASE_CAN0)
  27. #define CAN1 ( (CAN_TypeDef* )LS1C_REG_BASE_CAN1)
  28. #define CAN_InitStatus_Failed ((unsigned char)0x00) /*!< CAN initialization failed */
  29. #define CAN_InitStatus_Success ((unsigned char)0x01) /*!< CAN initialization OK */
  30. #define CAN_SJW_1tq ((unsigned char)0x00) /*!< 1 time quantum */
  31. #define CAN_SJW_2tq ((unsigned char)0x01) /*!< 2 time quantum */
  32. #define CAN_SJW_3tq ((unsigned char)0x02) /*!< 3 time quantum */
  33. #define CAN_SJW_4tq ((unsigned char)0x03) /*!< 4 time quantum */
  34. #define CAN_BS1_1tq ((unsigned char)0x00) /*!< 1 time quantum */
  35. #define CAN_BS1_2tq ((unsigned char)0x01) /*!< 2 time quantum */
  36. #define CAN_BS1_3tq ((unsigned char)0x02) /*!< 3 time quantum */
  37. #define CAN_BS1_4tq ((unsigned char)0x03) /*!< 4 time quantum */
  38. #define CAN_BS1_5tq ((unsigned char)0x04) /*!< 5 time quantum */
  39. #define CAN_BS1_6tq ((unsigned char)0x05) /*!< 6 time quantum */
  40. #define CAN_BS1_7tq ((unsigned char)0x06) /*!< 7 time quantum */
  41. #define CAN_BS1_8tq ((unsigned char)0x07) /*!< 8 time quantum */
  42. #define CAN_BS1_9tq ((unsigned char)0x08) /*!< 9 time quantum */
  43. #define CAN_BS1_10tq ((unsigned char)0x09) /*!< 10 time quantum */
  44. #define CAN_BS1_11tq ((unsigned char)0x0A) /*!< 11 time quantum */
  45. #define CAN_BS1_12tq ((unsigned char)0x0B) /*!< 12 time quantum */
  46. #define CAN_BS1_13tq ((unsigned char)0x0C) /*!< 13 time quantum */
  47. #define CAN_BS1_14tq ((unsigned char)0x0D) /*!< 14 time quantum */
  48. #define CAN_BS1_15tq ((unsigned char)0x0E) /*!< 15 time quantum */
  49. #define CAN_BS1_16tq ((unsigned char)0x0F) /*!< 16 time quantum */
  50. #define CAN_BS2_1tq ((unsigned char)0x00) /*!< 1 time quantum */
  51. #define CAN_BS2_2tq ((unsigned char)0x01) /*!< 2 time quantum */
  52. #define CAN_BS2_3tq ((unsigned char)0x02) /*!< 3 time quantum */
  53. #define CAN_BS2_4tq ((unsigned char)0x03) /*!< 4 time quantum */
  54. #define CAN_BS2_5tq ((unsigned char)0x04) /*!< 5 time quantum */
  55. #define CAN_BS2_6tq ((unsigned char)0x05) /*!< 6 time quantum */
  56. #define CAN_BS2_7tq ((unsigned char)0x06) /*!< 7 time quantum */
  57. #define CAN_BS2_8tq ((unsigned char)0x07) /*!< 8 time quantum */
  58. #define CAN_Id_Standard 0
  59. #define CAN_Id_Extended 1
  60. #define CAN_RTR_DATA 0
  61. #define CAN_RTR_Remote 1
  62. #define CAN_MODE_NORMAL 0
  63. #define CAN_MODE_LISEN 1
  64. #define CAN_MODE_LOOPBACK 2
  65. #define CAN_MODE_LOOPBACKANLISEN 3
  66. /*!< CAN 控制状态寄存器 */
  67. /************************** CAN_MOD 寄存器位定义*******************************/
  68. #define CAN_Mode_RM ((unsigned char)0x01) /*!< 复位模式 */
  69. #define CAN_Mode_LOM ((unsigned char)0x02) /*!< 只听模式 1:只听 0:正常 */
  70. #define CAN_Mode_STM ((unsigned char)0x04) /*!< 正常工作模式1:自检测 0:正常 */
  71. #define CAN_Mode_AFM ((unsigned char)0x08) /*!< 单/双滤波模式 1:单 0: 双*/
  72. #define CAN_Mode_SM ((unsigned char)0x10) /*!< 睡眠模式1: 睡眠 0: 唤醒 */
  73. /************************** CAN_CMR 寄存器位定义*******************************/
  74. #define CAN_CMR_TR ((unsigned char)0x01) /*!< 发送请求 1: 当前信息被发送 0: 空 */
  75. #define CAN_CMR_AT ((unsigned char)0x02) /*!< 中止发送 1: 等待发送的信息取消 0: 空缺 */
  76. #define CAN_CMR_RRB ((unsigned char)0x04) /*!< 释放接收缓冲器 1:释放 0: 无动作 */
  77. #define CAN_CMR_CDO ((unsigned char)0x08) /*!< 清除数据溢出 1:清除 0: 无动作 */
  78. //#define CAN_CMR_GTS ((unsigned char)0x10) /*!< STD模式< 睡眠: 1:进入睡眠 0: 唤醒 */
  79. #define CAN_CMR_SRR ((unsigned char)0x10) /*!< 自接收请求 1: 0: */
  80. #define CAN_CMR_EFF ((unsigned char)0x80) /*!< 扩展模式 1:扩展帧 0: 标准帧 */
  81. /************************** CAN_SR 寄存器位定义********************************/
  82. #define CAN_SR_BBS ((unsigned char)0x01) /*!< 接收缓存器状态1: 满 0: 空 */
  83. #define CAN_SR_DOS ((unsigned char)0x02) /*!< 数据溢出状态 1: 溢出 0: 空缺 */
  84. #define CAN_SR_TBS ((unsigned char)0x04) /*!< 发送缓存器状态1: 释放 0: 锁定 */
  85. #define CAN_SR_TCS ((unsigned char)0x08) /*!< 发送完毕状态1: 完毕 0: 未完毕 */
  86. #define CAN_SR_RS ((unsigned char)0x10) /*!< 接收状态1: 接收 0: 空闲 */
  87. #define CAN_SR_TS ((unsigned char)0x20) /*!< 发送状态1: 发送 0: 空闲*/
  88. #define CAN_SR_ES ((unsigned char)0x40) /*!< 出错状态1:出错 0: 正常 */
  89. #define CAN_SR_BS ((unsigned char)0x80) /*!< 总线状态1: 关闭 0: 开启 */
  90. /************************** CAN_IR 中断寄存器位定义****************************/
  91. #define CAN_IR_RI ((unsigned char)0x01) /*!< 接收中断 */
  92. #define CAN_IR_TI ((unsigned char)0x02) /*!< 发送中断 */
  93. #define CAN_IR_EI ((unsigned char)0x04) /*!< 错误中断 */
  94. #define CAN_IR_DOI ((unsigned char)0x08) /*!< 数据溢出中断 */
  95. #define CAN_IR_WUI ((unsigned char)0x10) /*!< 唤醒中断 */
  96. #define CAN_IR_EPI ((unsigned char)0x20) /*!< 错误消极中断 */
  97. #define CAN_IR_ALI ((unsigned char)0x40) /*!< 仲裁丢失中断 */
  98. #define CAN_IR_BEI ((unsigned char)0x80) /*!< 总线错误中断 */
  99. /************************* CAN_IER 中断使能寄存器位定义************************/
  100. #define CAN_IER_RIE ((unsigned char)0x01) /*!< 接收中断使能 */
  101. #define CAN_IER_TIE ((unsigned char)0x02) /*!< 发送中断使能 */
  102. #define CAN_IER_EIE ((unsigned char)0x04) /*!< 错误中断使能 */
  103. #define CAN_IER_DOIE ((unsigned char)0x08) /*!< 数据溢出中断使能 */
  104. #define CAN_IER_WUIE ((unsigned char)0x10) /*!< 唤醒中断使能 */
  105. #define CAN_IER_EPIE ((unsigned char)0x20) /*!< 错误消极中断使能 */
  106. #define CAN_IER_ALIE ((unsigned char)0x40) /*!< 仲裁丢失中断使能 */
  107. #define CAN_IER_BEIE ((unsigned char)0x80) /*!< 总线错误中断使能 */
  108. typedef enum
  109. {
  110. LS1C_CAN1MBaud=0, // 1 MBit/sec
  111. LS1C_CAN800kBaud, // 800 kBit/sec
  112. LS1C_CAN500kBaud, // 500 kBit/sec
  113. LS1C_CAN250kBaud, // 250 kBit/sec
  114. LS1C_CAN125kBaud, // 125 kBit/sec
  115. LS1C_CAN100kBaud, // 100 kBit/sec
  116. LS1C_CAN50kBaud, // 50 kBit/sec
  117. LS1C_CAN40kBaud, // 40 kBit/sec
  118. }Ls1c_CanBPS_t;
  119. typedef struct
  120. {
  121. unsigned char MOD;
  122. unsigned char CMR;
  123. unsigned char SR;
  124. unsigned char IR;
  125. unsigned char IER;
  126. unsigned char reserved0;
  127. unsigned char BTR0;
  128. unsigned char BTR1;
  129. unsigned char OCR;
  130. unsigned char reserved[2];
  131. unsigned char ALC;
  132. unsigned char ECC ;
  133. unsigned char EMLR;
  134. unsigned char RXERR;
  135. unsigned char TXERR;
  136. unsigned char IDE_RTR_DLC;
  137. unsigned char ID[4];
  138. unsigned char BUF[8];
  139. unsigned char RMCR;
  140. unsigned char CDR;
  141. } CAN_TypeDef;
  142. typedef struct
  143. {
  144. unsigned char CAN_Prescaler; /* 波特率分频系数1 to 31. */
  145. unsigned char CAN_Mode; /*0x10:睡眠0x08:单,双滤波 0x40:正常工作0x20:只听 0x01:复位*/
  146. unsigned char CAN_SJW; /*同步跳转宽度 */
  147. unsigned char CAN_BS1; /*时间段1计数值*/
  148. unsigned char CAN_BS2; /*时间段2计数值*/
  149. } CAN_InitTypeDef;
  150. typedef struct
  151. {
  152. unsigned char IDE; /*0: 使用标准标识符1: 使用扩展标识符*/
  153. unsigned char RTR; /*0: 数据帧 1: 远程帧*/
  154. unsigned char MODE; /* 0- 双滤波器模式;1-单滤波器模式*/
  155. unsigned long First_Data; /*双滤波器模式下信息第一个数据字节*/
  156. unsigned long Data_Mask; /*双滤波器模式下信息第一个数据字节屏蔽*/
  157. unsigned long ID; /*验收代码*/
  158. /*
  159. 双滤波器- 扩展帧: 2个滤波器的前16位,分别放在ID 的前16位和 ID的后16位.
  160. 双滤波器- 标准帧: 2个滤波器的11位,分别放在ID 的前16位和 ID的后16位,第1个滤波器同时使用First_Data和Data_Mask
  161. 单滤波器- 扩展帧: 使用29位, 放在ID 的后29位.
  162. 单滤波器- 标准帧: 使用11位, 放在ID 的后11位.
  163. */
  164. unsigned long IDMASK; /*验收屏蔽*/
  165. } CAN_FilterInitTypeDef;
  166. typedef struct
  167. {
  168. unsigned long StdId; /* 11位ID*/
  169. unsigned long ExtId; /*29位ID**/
  170. unsigned char IDE; /*IDE: 标识符选择
  171. 该位决定发送邮箱中报文使用的标识符类型
  172. 0: 使用标准标识符
  173. 1: 使用扩展标识符*/
  174. unsigned char RTR; /*远程发送请求
  175. 0: 数据帧
  176. 1: 远程帧*/
  177. unsigned char DLC; /*数据帧长度*/
  178. unsigned char Data[8]; /*8字节数据*/
  179. } CanRxMsg;
  180. typedef struct
  181. {
  182. unsigned long StdId; /* 11位ID*/
  183. unsigned long ExtId; /*29位ID**/
  184. unsigned char IDE; /*IDE: 标识符选择
  185. 该位决定发送邮箱中报文使用的标识符类型
  186. 0: 使用标准标识符
  187. 1: 使用扩展标识符*/
  188. unsigned char RTR; /*远程发送请求
  189. 0: 数据帧
  190. 1: 远程帧*/
  191. unsigned char DLC; /*数据帧长度*/
  192. unsigned char Data[8]; /*8字节数据*/
  193. } CanTxMsg;
  194. unsigned char CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct);
  195. unsigned char CAN_SetBps(CAN_TypeDef* CANx, Ls1c_CanBPS_t Bps);
  196. unsigned char CAN_SetMode(CAN_TypeDef* CANx, unsigned char mode);
  197. void CAN_FilterInit(CAN_TypeDef* CANx, CAN_FilterInitTypeDef* CAN_FilterInitStruct);
  198. unsigned char CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage);
  199. void CAN_Receive(CAN_TypeDef* CANx, CanRxMsg* RxMessage);
  200. unsigned char set_reset_mode(CAN_TypeDef* CANx);
  201. unsigned char set_start(CAN_TypeDef* CANx);
  202. #endif