Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.72 KB | None | 0 0
  1. /*
  2. ===============================================================================
  3. Name : EM4_CAN_OUT.c
  4. Author : Nephtaly Aniceta
  5. Version : 0.9
  6. Copyright : GPLv3
  7. Description : Main CAN OUT Function
  8. Inspired by work from previous years.
  9. Rewritten for Visual Studio and LPCOpen v2.xx
  10. ===============================================================================
  11. */
  12.  
  13. #include <chip.h>
  14.  
  15. #define DEVICE_NR 0b0000
  16. #define EM_04_CAN_RANGE 0x100
  17.  
  18. //TODO: Update adressing
  19. #define ALL_ADDRESS (0x000 + EM_04_CAN_RANGE)
  20. #define FAN_ADDRESS (0x010 + EM_04_CAN_RANGE)
  21. #define COUT_ADDRESS (0x020 + EM_04_CAN_RANGE)
  22. #define LIGHT_ADDRESS (0x001 + COUT_ADDRESS)
  23. #define FRONT_ADDRESS (0x002 + COUT_ADDRESS)
  24. #define REAR_ADDRESS (0x003 + COUT_ADDRESS)
  25. #define LEFT_ADDRESS (0x004 + COUT_ADDRESS)
  26. #define RIGHT_ADDRESS (0x005 + COUT_ADDRESS)
  27. #define WIPER_ADDRESS (0x006 + COUT_ADDRESS)
  28. #define BLOWER_ADDRESS (0x007 + COUT_ADDRESS)
  29. #define HUD_ADDRESS (0x030 + COUT_ADDRESS)
  30. #define SPEED_ADDRESS (0x001 + HUD_ADDRESS)
  31. #define WARNING_ADDRESS (0x002 + HUD_ADDRESS)
  32. #define TEMPERATURE_ADDRESS (0x003 + HUD_ADDRESS)
  33. #define BATTERY_ADDRESS (0x004 + HUD_ADDRESS)
  34. #define DIMMER_ADDRESS (0x005 + HUD_ADDRESS)
  35. #define CLOCK_ADDRES (0x00a + HUD_ADDRESS)
  36. #define MC_ADDRESS (0x040 + EM_04_CAN_RANGE)
  37. #define MC_SIGNAL1 (0x001 + MC_ADDRESS)
  38. #define MC_SIGNAL2 (0x002 + MC_ADDRESS)
  39. #define MC_I2C (0x003 + MC_ADDRESS)
  40. #define BROADCAST_ADDRESS (0x700)
  41.  
  42. #define ALL_MESSAGE 1
  43. #define FRONT_MESSAGE 2
  44. #define REAR_MESSAGE 3
  45. #define LEFT_MESSAGE 4
  46. #define RIGHT_MESSAGE 5
  47. #define PERSNOAL_MESSAGE 6
  48. #define LIGHT_MESSAGE 7
  49. #define WIPER_MESSAGE 8
  50. #define BLOWER_MESSAGE 9
  51. #define TOTAL_MESSAGE 10
  52.  
  53. #ifndef LPC_GPIO
  54. #define LPC_GPIO LPC_GPIO_PORT
  55. #endif
  56.  
  57. #ifdef __cplusplus
  58. extern "C"
  59. #endif
  60.  
  61. volatile unsigned long SysTickCnt;
  62.  
  63. const uint32_t ExtRateIn = 0;
  64. const uint32_t OscRateIn = 12000000;
  65. const uint32_t RTCOscRateIn = 32768;
  66.  
  67. CCAN_MSG_OBJ_T msg_obj;
  68.  
  69. /**
  70. * @brief Handle interrupt from SysTick timer
  71. * @return Nothing
  72. */
  73. void SysTick_Handler(void) {
  74. SysTickCnt++;
  75. }
  76.  
  77. /**
  78. * @brief Handle interrupt from 32-bit timer
  79. * @return Nothing
  80. */
  81. void TIMER32_0_IRQHandler(void){
  82. if (Chip_TIMER_MatchPending(LPC_TIMER32_0, 1)) {
  83. Chip_TIMER_ClearMatch(LPC_TIMER32_0, 1);
  84. Chip_GPIO_WritePortBit(LPC_GPIO, 2, 10, false); //led 2 (yellow)
  85. }
  86. }
  87.  
  88. /**
  89. * @brief Delay function in (SysTick / x) (default x = 1000, Delay in ms)
  90. * @return Nothing
  91. */
  92. void Delay(unsigned long tick) {
  93. unsigned long systickcnt;
  94.  
  95. systickcnt = SysTickCnt;
  96. while ((SysTickCnt - systickcnt) < tick)
  97. ;
  98. }
  99.  
  100. /**
  101. * @brief CCAN Interrupt Handler
  102. * @return Nothing
  103. * @note The CCAN interrupt handler must be provided by the user application.
  104. * It's function is to call the isr() API located in the ROM
  105. */
  106. void CAN_IRQHandler(void) {
  107. LPC_CCAN_API->isr();
  108. }
  109.  
  110. /**
  111. * @brief Baudrate calculator
  112. * @return Baudrates on for the CAN
  113. */
  114. void baudrateCalculate(uint32_t baud_rate, uint32_t *can_api_timing_cfg){
  115. uint32_t pClk, div, quanta, segs, seg1, seg2, clk_per_bit, can_sjw;
  116. Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_CAN);
  117. pClk = Chip_Clock_GetMainClockRate();
  118.  
  119. clk_per_bit = pClk / baud_rate;
  120.  
  121. for (div = 0; div <= 15; div++) {
  122. for (quanta = 1; quanta <= 32; quanta++) {
  123. for (segs = 3; segs <= 17; segs++) {
  124. if (clk_per_bit == (segs * quanta * (div + 1))) {
  125. segs -= 3;
  126. seg1 = segs / 2;
  127. seg2 = segs - seg1;
  128. can_sjw = seg1 > 3 ? 3 : seg1;
  129. can_api_timing_cfg[0] = div;
  130. can_api_timing_cfg[1] =
  131. ((quanta - 1) & 0x3F) | (can_sjw & 0x03) << 6 | (seg1 & 0x0F) << 8 | (seg2 & 0x07) << 12;
  132. return;
  133. }
  134. }
  135. }
  136. }
  137. }
  138.  
  139. /* Callback function prototypes */
  140. void CAN_rx(uint8_t msg_obj_num);
  141. void CAN_tx(uint8_t msg_obj_num);
  142. void CAN_error(uint32_t error_info);
  143.  
  144. void setPort(int port, bool onoff);
  145.  
  146. /* CAN Initialise */
  147. /* Initialises the CAN and configures the filters*/
  148. void CAN_init() {
  149. /* Publish CAN Callback Functions */
  150. CCAN_CALLBACKS_T callbacks = { CAN_rx, CAN_tx, CAN_error, NULL, NULL, NULL,
  151. NULL,
  152. NULL, };
  153.  
  154. /* Initialize CAN Controller */
  155. uint32_t CanApiClkInitTable[2];
  156. baudrateCalculate(500000, CanApiClkInitTable); //500kbits
  157.  
  158. LPC_CCAN_API->init_can(&CanApiClkInitTable[0], TRUE);
  159. /* Configure the CAN callback functions */
  160. LPC_CCAN_API->config_calb(&callbacks);
  161.  
  162. msg_obj.msgobj = ALL_MESSAGE;
  163. msg_obj.mode_id = ALL_ADDRESS;
  164. msg_obj.mask = 0xFFF;
  165. LPC_CCAN_API->config_rxmsgobj(&msg_obj);
  166.  
  167. if(DEVICE_NR == 0b0000) // A1
  168. {
  169. msg_obj.msgobj = LIGHT_MESSAGE;
  170. msg_obj.mode_id = LIGHT_ADDRESS;
  171. msg_obj.mask = 0xFFF;
  172. LPC_CCAN_API -> config_rxmsgobj(&msg_obj);
  173. }
  174. if(DEVICE_NR == 0b0111) // Front/Mid
  175. {
  176. msg_obj.msgobj = WIPER_MESSAGE;
  177. msg_obj.mode_id = WIPER_ADDRESS;
  178. msg_obj.mask = 0xFFF;
  179. LPC_CCAN_API->config_rxmsgobj(&msg_obj);
  180.  
  181. msg_obj.msgobj = BLOWER_MESSAGE;
  182. msg_obj.mode_id = BLOWER_ADDRESS;
  183. msg_obj.mask = 0xFFF;
  184. LPC_CCAN_API->config_rxmsgobj(&msg_obj);
  185. }
  186. if (DEVICE_NR & 0b0001) //Select only the front bit of the twins [bug for mid, solve by shifting]
  187. {
  188. msg_obj.msgobj = FRONT_MESSAGE;
  189. msg_obj.mode_id = FRONT_ADDRESS;
  190. msg_obj.mask = 0xFFF;
  191. LPC_CCAN_API->config_rxmsgobj(&msg_obj);
  192. }
  193.  
  194. if (!(DEVICE_NR & 0b0001)) //Select only the rear bit (=not front bit) of the twins [bug for mid, solve by shifting]
  195. {
  196. msg_obj.msgobj = REAR_MESSAGE;
  197. msg_obj.mode_id = REAR_ADDRESS;
  198. msg_obj.mask = 0xFFF;
  199. LPC_CCAN_API->config_rxmsgobj(&msg_obj);
  200. }
  201.  
  202. if (DEVICE_NR & 0b0010) //Select only the twin bit
  203. {
  204. msg_obj.msgobj = LEFT_MESSAGE;
  205. msg_obj.mode_id = LEFT_ADDRESS;
  206. msg_obj.mask = 0xFFF;
  207. LPC_CCAN_API->config_rxmsgobj(&msg_obj);
  208.  
  209. msg_obj.msgobj = RIGHT_MESSAGE;
  210. msg_obj.mode_id = RIGHT_ADDRESS;
  211. msg_obj.mask = 0xFFF;
  212. LPC_CCAN_API->config_rxmsgobj(&msg_obj);
  213. }
  214.  
  215. msg_obj.msgobj = PERSNOAL_MESSAGE;
  216. msg_obj.mode_id = DEVICE_NR;
  217. msg_obj.mask = 0xFFF;
  218. LPC_CCAN_API->config_rxmsgobj(&msg_obj);
  219.  
  220. /* Enable the CAN Interrupt */
  221. NVIC_EnableIRQ(CAN_IRQn);
  222. }
  223.  
  224. /* CAN receive callback */
  225. /* Function is executed by the Callback handler after
  226. a CAN message has been received */
  227. void CAN_rx(uint8_t msg_obj_num) {
  228. // Disable interupts while receiving
  229. //NVIC_DisableIRQ(CAN_IRQn);
  230. /* Determine which CAN message has been received */
  231. msg_obj.msgobj = msg_obj_num;
  232.  
  233. /* Now load up the msg_obj structure with the CAN message */
  234. LPC_CCAN_API->can_receive(&msg_obj);
  235. if (msg_obj_num < TOTAL_MESSAGE || msg_obj_num > 0)
  236. {
  237. //Message "Inbox" for all the FRONT_MESSAGES {...}
  238. if (msg_obj_num == FRONT_MESSAGE)
  239. {
  240. setPort(0, msg_obj.data[0]);
  241. }
  242.  
  243. if (msg_obj_num == REAR_MESSAGE)
  244. {
  245. setPort(0, msg_obj.data[0]);
  246. }
  247.  
  248. if (msg_obj_num == LEFT_MESSAGE)
  249. {
  250. setPort(1, msg_obj.data[0]);
  251. }
  252.  
  253. if (msg_obj_num == RIGHT_MESSAGE)
  254. {
  255. setPort(2, msg_obj.data[0]);
  256. }
  257.  
  258. if (msg_obj_num == LIGHT_MESSAGE)
  259. {
  260. //LOW Beam 3,3
  261. setPort(0, msg_obj.data[0]);
  262. //Indicator 1,5
  263. setPort(1, msg_obj.data[1]);
  264. //HIGH Beam 1,11
  265. setPort(2, msg_obj.data[2]);
  266. //breedtelicht 3,2
  267. setPort(3, msg_obj.data[3]);
  268. }
  269.  
  270. if(msg_obj_num == WIPER_MESSAGE)
  271. {
  272. setPort(6, msg_obj.data[0]);
  273. }
  274.  
  275. if(msg_obj_num == BLOWER_MESSAGE)
  276. {
  277. setPort(7, msg_obj.data[0]);
  278. }
  279.  
  280. if (msg_obj_num == PERSNOAL_MESSAGE)
  281. {
  282. setPort(0, msg_obj.data[0]);
  283. setPort(1, msg_obj.data[1]);
  284. setPort(2, msg_obj.data[2]);
  285. setPort(3, msg_obj.data[3]);
  286. setPort(4, msg_obj.data[4]);
  287. setPort(5, msg_obj.data[5]);
  288. setPort(6, msg_obj.data[6]);
  289. setPort(7, msg_obj.data[7]);
  290. }
  291.  
  292. if (msg_obj_num == ALL_MESSAGE)
  293. {
  294. if(msg_obj.data[0])
  295. {
  296. setPort(0, true);
  297. setPort(1, true);
  298. setPort(2, true);
  299. setPort(3, true);
  300. setPort(4, true);
  301. setPort(5, true);
  302. setPort(6, true);
  303. setPort(7, true);
  304. }
  305. else
  306. {
  307. setPort(0, false);
  308. setPort(1, false);
  309. setPort(2, false);
  310. setPort(3, false);
  311. setPort(4, false);
  312. setPort(5, false);
  313. setPort(6, false);
  314. setPort(7, false);
  315. }
  316.  
  317. }
  318. // Turn on the yellow led and Enable timer interrupt
  319. Chip_GPIO_WritePortBit(LPC_GPIO, 2, 10, true); //led 2 (yellow)
  320. NVIC_ClearPendingIRQ(TIMER_32_0_IRQn);
  321. NVIC_EnableIRQ(TIMER_32_0_IRQn);
  322. }
  323. NVIC_EnableIRQ(CAN_IRQn);
  324. }
  325.  
  326. /* CAN transmit callback */
  327. /* Function is executed by the Callback handler after
  328. a CAN message has been transmitted */
  329. void CAN_tx(uint8_t msg_obj_num){
  330. Chip_GPIO_WritePortBit(LPC_GPIO, 2, 10, true); //led 2 (yellow)
  331. NVIC_ClearPendingIRQ(TIMER_32_0_IRQn);
  332. NVIC_EnableIRQ(TIMER_32_0_IRQn);
  333. }
  334.  
  335. /* CAN error callback */
  336. /* Function is executed by the Callback handler after
  337. an error has occured on the CAN bus */
  338. void CAN_error(uint32_t error_info){
  339. Chip_GPIO_WritePortBit(LPC_GPIO, 2, 2, true); //led 3 (red)
  340. }
  341.  
  342. void setPort(int port, bool onoff){
  343. //LOW Beam 3,3
  344. if (port == 0) Chip_GPIO_WritePortBit(LPC_GPIO, 3, 3, onoff);
  345. //Indicator 1,5
  346. if (port == 1) Chip_GPIO_WritePortBit(LPC_GPIO, 1, 5, onoff);
  347. //HIGH Beam 1,11
  348. if (port == 2) Chip_GPIO_WritePortBit(LPC_GPIO, 1, 11, onoff);
  349. //breedtelicht 3,2
  350. if (port == 3) Chip_GPIO_WritePortBit(LPC_GPIO, 3, 2, onoff);
  351.  
  352. if (port == 4) Chip_GPIO_WritePortBit(LPC_GPIO, 2, 1, onoff);
  353. if (port == 5) Chip_GPIO_WritePortBit(LPC_GPIO, 0, 3, onoff);
  354. if (port == 6) Chip_GPIO_WritePortBit(LPC_GPIO, 2, 11, onoff);
  355. if (port == 7) Chip_GPIO_WritePortBit(LPC_GPIO, 1, 10, onoff);
  356.  
  357.  
  358.  
  359.  
  360. }
  361.  
  362.  
  363. int main(void){
  364.  
  365. CAN_init();
  366.  
  367. SystemCoreClockUpdate();
  368. //Enable and setup SysTick Timer at 1/1000 seconds (1ms)
  369. SysTick_Config(SystemCoreClock / 1000);
  370.  
  371. //Enable timer 1 clock
  372. Chip_TIMER_Init(LPC_TIMER32_0);
  373.  
  374. //Timer setup for match and interrupt at 1/10 seconds (100ms)
  375. Chip_TIMER_Reset(LPC_TIMER32_0);
  376. Chip_TIMER_MatchEnableInt(LPC_TIMER32_0, 1);
  377. Chip_TIMER_SetMatch(LPC_TIMER32_0, 1, (SystemCoreClock / 10));
  378. Chip_TIMER_ResetOnMatchEnable(LPC_TIMER32_0, 1);
  379. Chip_TIMER_Enable(LPC_TIMER32_0);
  380.  
  381. //setup GPIO
  382. Chip_GPIO_Init(LPC_GPIO);
  383. Chip_GPIO_SetPortDIROutput(LPC_GPIO, 0, 1 << 7);
  384. Chip_GPIO_SetPortDIROutput(LPC_GPIO, 1, 1 << 5 | 1 << 11);
  385. Chip_GPIO_SetPortDIROutput(LPC_GPIO, 2, 1 << 2 | 1 << 10);
  386. Chip_GPIO_SetPortDIROutput(LPC_GPIO, 3, 1 << 2 | 1 << 3);
  387.  
  388. unsigned long lastSystickcnt = 0;
  389.  
  390. setPort(0, false);
  391. setPort(1, false);
  392. setPort(2, false);
  393. setPort(3, false);
  394. setPort(4, false);
  395. setPort(5, false);
  396. setPort(6, false);
  397. setPort(7, false);
  398.  
  399. for (;;)
  400. {
  401. if ((SysTickCnt - lastSystickcnt) >= 1000)
  402. {
  403. lastSystickcnt = SysTickCnt;
  404.  
  405. msg_obj.msgobj = 0;
  406. msg_obj.mode_id = (BROADCAST_ADDRESS + DEVICE_NR) | CAN_MSGOBJ_STD;
  407. msg_obj.mask = 0x0;
  408. msg_obj.dlc = 1;
  409. msg_obj.data[0] = DEVICE_NR;
  410. LPC_CCAN_API->can_transmit(&msg_obj);
  411.  
  412. Chip_GPIO_SetPinToggle(LPC_GPIO, 0, 7); //led 4 (blue)
  413. }
  414. }
  415. return 0;
  416. }
  417.  
  418.  
  419. /*
  420. Ouput pins:
  421. -------------
  422. 8 1,7
  423. 7 3,3
  424. 1 2,7
  425. 2 2,8
  426. 3 2,1
  427. 4 0,3
  428. 5 0,4
  429. 6 0,5
  430.  
  431. LOW Beam 3,3
  432. Indicator 1,5
  433. HIGH Beam 1,11
  434. breedtelicht 3,2
  435.  
  436. Led pins:
  437. -------------
  438. led 1 (green) power light
  439. led 2 (yellow) 2,10
  440. led 3 (red) 2,2
  441. led 4 (blue) 0,7
  442.  
  443. DEVICE ID config:
  444. -------------
  445. name | ID || 2^2 | 2^1 | 2^0 | dec | 0b0000
  446. =====================|====||================================
  447. Front Lights |-A1-||--0--|--0--|--0--| 0 | 0b0000
  448. Back Lights |-A2-||--0--|--0--|--1--| 1 | 0b0001
  449. RDW screen |-B--||--0--|--1--|--0--| 2 | 0b0010
  450. Front window Periph |-C--||--0--|--1--|--1--| 3 | 0b0011
  451. 3d Brake Lights |-D--||--1--|--0--|--0--| 4 | 0b0100
  452. Battery Trans |-E--||--1--|--0--|--1--| 5 | 0b0101
  453. License plate Lights |-F--||--1--|--1--|--0--| 6 | 0b0110
  454.  
  455. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement