Advertisement
pabloducato

Untitled

May 28th, 2019
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.39 KB | None | 0 0
  1. /******************************************************************************
  2.  *****************************************************************************/
  3.  
  4.  
  5. #include "config.h"
  6.  
  7.  
  8. #include "lpc2294.h"
  9. #include "lcd.h"
  10. #include "uart.h"
  11. #include "armVIC.h"
  12. #include "can.h"
  13.  
  14.  
  15. #define STATE_0             0
  16. #define STATE_1             1
  17. #define STATE_2             2
  18. #define STATE_3             3
  19. #define STATE_4             4
  20. #define STATE_5             5
  21. #define STATE_6             6
  22. #define STATE_7             7
  23. #define STATE_8             8
  24. #define STATE_9             9
  25. #define STATE_10            10
  26.  
  27. #define STATE_MAX           STATE_10
  28.  
  29. unsigned char sendSymulPacket = _FALSE;
  30. unsigned char startSymulation = _FALSE;
  31. #ifdef uIP_IS_USED
  32. #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
  33. #endif  //uIP_IS_USED
  34.  
  35. void __attribute__ ((interrupt("IRQ"))) timer0_cmp(void);
  36. void systemInit(void);
  37. void gpioInit(void);
  38. const char AppTitle[] = " Magistrala CAN ";
  39.  
  40. #define   UIP_ARP_TIMER     1000
  41. #define   UIP_TX_TIMER      1
  42. #define   HALF_SEK          50
  43. #define   LED_TIMER         50
  44. #define   ONE_SEC           100
  45. #define   THREE_SEC         300
  46. #define   TWO_SEC           200
  47. #define   TEST_TIMER        100
  48.  
  49. int TX_EventPending = _FALSE;           // the hardware receive event
  50. int ARP_EventPending = _FALSE;          // trigger the arp timer event
  51. unsigned long half_Sec = HALF_SEK;
  52. unsigned long two_Sec = 0;
  53. unsigned long three_Sec = THREE_SEC;
  54. unsigned long Uip_Timer = UIP_TX_TIMER;
  55. unsigned long ten_Secs = UIP_ARP_TIMER;
  56. unsigned long LedTimer = LED_TIMER;
  57. unsigned long one_Sec = ONE_SEC;
  58. unsigned long TestTimer = TEST_TIMER;
  59.  
  60.  
  61. //=====================================================================
  62. //
  63. //=====================================================================
  64. void systemInit(void)
  65. {
  66.     // --- enable and connect the PLL (Phase Locked Loop) ---
  67.     // a. set multiplier and divider
  68.     SCB_PLLCFG = MSEL | (1<<PSEL1) | (0<<PSEL0);
  69.     // b. enable PLL
  70.     SCB_PLLCON = (1<<PLLE);
  71.     // c. feed sequence
  72.     SCB_PLLFEED = PLL_FEED1;
  73.     SCB_PLLFEED = PLL_FEED2;
  74.     // d. wait for PLL lock (PLOCK bit is set if locked)
  75.     while (!(SCB_PLLSTAT & (1<<PLOCK)));
  76.     // e. connect (and enable) PLL
  77.     SCB_PLLCON = (1<<PLLE) | (1<<PLLC);
  78.     // f. feed sequence
  79.     SCB_PLLFEED = PLL_FEED1;
  80.     SCB_PLLFEED = PLL_FEED2;
  81.    
  82.     // --- setup and enable the MAM (Memory Accelerator Module) ---
  83.     // a. start change by turning of the MAM (redundant)
  84.     MAM_MAMCR = 0; 
  85.     // b. set MAM-Fetch cycle to 3 cclk as recommended for >40MHz
  86.     MAM_MAMTIM = MAM_FETCH;
  87.     // c. enable MAM
  88.     MAM_MAMCR = MAM_MODE;
  89.        
  90.    
  91.     // --- set VPB speed ---
  92.     SCB_VPBDIV = VPBDIV_VAL;
  93.    
  94.     // --- map INT-vector ---
  95.     #if defined(RAM_RUN)
  96.       SCB_MEMMAP = MEMMAP_USER_RAM_MODE;
  97.     #elif defined(ROM_RUN)
  98.       SCB_MEMMAP = MEMMAP_USER_FLASH_MODE;
  99.     #else
  100.     #error RUN_MODE not defined!
  101.     #endif
  102. }
  103.  
  104. //=====================================================================
  105. //
  106. //=====================================================================
  107. void gpioInit(void)
  108. {
  109.    
  110.     PCB_PINSEL0 = 0x00000000;         //1000 0000 0000 0000 0000 0000 0000 0000
  111.     PCB_PINSEL1 = 0;
  112.    
  113.     BCFG1 = 0x20000400;
  114.     PCB_PINSEL2 = 0x0FE169EC;  
  115.  
  116.     GPIO0_IODIR = 0x714024F1;            //0111 0001 0100 0000 0010 0100 1111 0001 - E,R_W,RS,TD4,TD3,TD2,LIGHT_LCD,D4-D7,TXD0
  117.     GPIO1_IODIR = 0x02000003;            //0000 0010 0000 0000 0000 0000 0000 0011 - PULL_PIN_P1, CS0, OE  
  118.  
  119.     GPIO0_IODIR &= ~((1<<BUT1PIN)|(1<<BUT2PIN));    // define Button-Pins as inputs
  120.     GPIO0_IODIR |= (1<<SCL_TEST_PIN);
  121. }
  122.  
  123. //=====================================================================
  124. //
  125. //=====================================================================
  126. #define TxTCR_COUNTER_ENABLE (1<<0)
  127. #define TxMCR_INT_ON_MR0     (1<<0)
  128. #define TxMCR_RESET_ON_MR0   (1<<1)
  129. #define TxIR_MR0_FLAG        (1<<0)
  130. void timer0_init(void)
  131. {
  132.     T0_MR0 = ((FOSC*PLL_M)/(1000/10))-1;     // Compare-hit at 10mSec (-1 reset "tick")
  133.     T0_MCR = TxMCR_INT_ON_MR0 | TxMCR_RESET_ON_MR0;     // Interrupt and Reset on MR0
  134.     T0_TCR = TxTCR_COUNTER_ENABLE;            // Timer0 Enable
  135. }
  136. void ShowButtonLCD(unsigned char button)
  137. {
  138.     switch(button){
  139.         case 15: LCD_WriteString("    Button 1    ",LCD_LINE_2,0);break;
  140.         case 16: LCD_WriteString("    Button 2    ",LCD_LINE_2,0);break;
  141.         default: return;
  142.     }
  143.     two_Sec=TWO_SEC;
  144. }
  145. //=====================================================================
  146. //
  147. //=====================================================================
  148. void timer0_cmp(void)
  149. {
  150.     UART0_Timeout();
  151.     LCD_RefreshTimer();
  152.  
  153.     three_Sec--;                          
  154.     if (three_Sec == 0)                  
  155.     {          
  156.         three_Sec = THREE_SEC;          
  157.         sendSymulPacket = _TRUE;
  158.     }
  159.    
  160.     half_Sec--;                           // Decrement half sec counter
  161.     if (half_Sec == 0)                    // Count 1ms intervals for half a second
  162.     {          
  163.         half_Sec = HALF_SEK;            
  164.    
  165.     }
  166.     if(two_Sec)two_Sec--;                
  167.     if (two_Sec == 1)                    
  168.     {          
  169.         LCD_WriteString("                ",LCD_LINE_2,0);
  170.     }
  171.     Uip_Timer--;                           // Decrement half sec counter
  172.     if (Uip_Timer == 0)                    // Count 1ms intervals for half a second
  173.     {          
  174.         Uip_Timer = UIP_TX_TIMER;            // and then set the receive poll flag
  175.         TX_EventPending = _TRUE;
  176.     }
  177.  
  178.     ten_Secs--;
  179.     if (ten_Secs == 0)                    // Count 1ms intervals for ten seconds
  180.     {
  181.         ten_Secs = UIP_ARP_TIMER;           // and then set the event required to
  182.         ARP_EventPending = _TRUE;            // trigger the arp timer if necessary
  183.     }
  184.    
  185.     TestTimer--;
  186.     if(TestTimer == 0)
  187.     {
  188.         TestTimer = TEST_TIMER;
  189.  
  190.     }
  191.  
  192.     T0_IR = TxIR_MR0_FLAG; // Clear interrupt flag by writing 1 to Bit 0
  193.     VICVectAddr = 0;       // Acknowledge Interrupt (rough?)
  194. }
  195.  
  196. //=====================================================================
  197. // Obsluguje odbiorniki CAN
  198. //=====================================================================
  199.     void CAN_HandleCanReceivers(unsigned char canNumExt)
  200.     {
  201.         CAN_MSG localMsgBufRec; // Buffers one CAN message
  202.  
  203.         unsigned char canBufferRec[12];
  204.         int bytesCntRec;
  205.         unsigned char canNumLocal;
  206.         int i;
  207.        
  208.         canNumLocal = canNumExt;
  209.         for(i=0; i<12; i++)
  210.             canBufferRec[i] = 0;
  211.                        
  212.         //funkcja sprawdza czy są dane odebrane w przerwaniu
  213.         if (CAN_PullMessage(canNumLocal,&localMsgBufRec) == _TRUE)
  214.         {
  215.             bytesCntRec = (int)((localMsgBufRec.Frame >> 16) & 0x0000000F);
  216.             UART0_Puts("\r\n CAN ");
  217.             UART0_SendNumber(canNumLocal);
  218.             UART0_Puts(" received ");
  219.             UART0_SendNumber(bytesCntRec);
  220.             UART0_Puts(" byte data,");
  221.             UART0_Puts(" MsgId ");
  222.             UART0_SendNumber((short)localMsgBufRec.MsgID);
  223.            
  224.             // jesli id ramki 0 to nie wysylaj
  225.             if(localMsgBufRec.MsgID & 0x0080)
  226.             {
  227.                 UART0_Puts("\r\n Handling simulation CAN packet");
  228.                 //***************************************************************//
  229.                 switch(canNumLocal){
  230.                     case CAN_PORT_1:
  231.                     case CAN_PORT_2:
  232.                     case CAN_PORT_3:
  233.                         canBufferRec[0] = 0x08;
  234.                         canBufferRec[1] = (unsigned char)(localMsgBufRec.DatA & 0x000000FF);
  235.                         canBufferRec[2] = 0x00;
  236.                         canBufferRec[3] = canBufferRec[1] + 1;
  237.                         break;
  238.                     case CAN_PORT_4:
  239.                         canBufferRec[1] = 0;
  240.                         CAN_ChangeSymulationState();
  241.                         UART0_Puts("\r\n Simulation stopped\r\n");
  242.                         break;
  243.                 }
  244.                
  245.                
  246.                 //***************************************************************//
  247.                
  248.                 if(CAN_SendPacketToCan((canBufferRec[1] & (0x0F)),canBufferRec) == _TRUE)
  249.                 {
  250.                     UART0_Puts("\r\n Send SYM packet to CAN");
  251.                     UART0_SendNumber(canNumLocal+1);
  252.                     UART0_Puts(" ,msg id= ");
  253.                     UART0_SendNumber(canBufferRec[1]);
  254.                 }
  255.                 return;
  256.             }
  257.         }
  258.        
  259.     }
  260.  
  261. //=====================================================================
  262. // 
  263. //=====================================================================
  264.  
  265. void CAN_SymulPacket(void)
  266.     {
  267.         unsigned char canBufferTest[12];
  268.         int i;
  269.         if (startSymulation == _FALSE)  return;
  270.        
  271.         for(i=0; i<12; i++)
  272.             canBufferTest[i] = 0;
  273.         //****************************************************************//
  274.        
  275.         canBufferTest[0] = 0x08;
  276.         canBufferTest[1] = CAN_PORT_1 + 0x80;
  277.         canBufferTest[2] = 0x00;
  278.         canBufferTest[3] = canBufferTest[1] + 1;
  279.         if(CAN_SendPacketToCan(CAN_PORT_1, canBufferTest) == _TRUE){
  280.             UART0_Puts("\r\n SendSYM packet to CAN1, msg id = ");
  281.             UART0_SendNumber(canBufferTest[1]);
  282.         }else UART0_Puts("\r\n ERROR send TEST packet to CAN");
  283.     }
  284.  
  285. //=====================================================================
  286. //  funkcja główna
  287. //=====================================================================
  288. int main(void)
  289. {
  290.     //*************************************************//
  291.     systemInit();
  292.     gpioInit();
  293.     VIC_Init();
  294.     LCD_Init();
  295.     UART0_Init(UART_BAUD(UART0_BAUD), UART_8N1, UART_FIFO_8);
  296.     enableIRQ();
  297.     timer0_init();
  298.     VIC_Install_IRQ(VIC_TIMER0, (void*)timer0_cmp);
  299.    
  300.    
  301.     //*************************************************//
  302.     UART0_Puts("\r\n RESET");
  303.     LCD_WriteString(AppTitle,LCD_LINE_1,1);
  304.     //*************************************************//
  305.     if((CAN_Init(CAN_PORT_1, CANBitrate1000k_60MHz) == 0) ||
  306.     (CAN_Init(CAN_PORT_2, CANBitrate1000k_60MHz) == 0) ||
  307.     (CAN_Init(CAN_PORT_3, CANBitrate1000k_60MHz) == 0) ||
  308.     (CAN_Init(CAN_PORT_4, CANBitrate1000k_60MHz)) == 0)
  309.     UART0_Puts("\r\n CAN bus is not initialized!");
  310.    
  311.     CAN_SetFilter(CAN_PORT_1, 0x0022);
  312.     CAN_SetFilter(CAN_PORT_1, 0x0033);
  313.     CAN_SetFilter(CAN_PORT_1, 0x0044);
  314.     CAN_SetFilter(CAN_PORT_2, 0x0002);
  315.     CAN_SetFilter(CAN_PORT_3, 0x0003);
  316.     CAN_SetFilter(CAN_PORT_4, 0x0004);
  317.     CAN_SetFilter(CAN_PORT_1, 0x0081);
  318.     CAN_SetFilter(CAN_PORT_2, 0x0082);
  319.     CAN_SetFilter(CAN_PORT_3, 0x0083);
  320.     CAN_SetFilter(CAN_PORT_4, 0x0084);
  321.    
  322.     //*************************************************//
  323.        
  324.     while(1)
  325.     {
  326.         // Obsluga transmisji RS232
  327.         UART0_HandleProtocol();
  328.         UART0_HandlePacket();
  329.         UART0_CheckProtocolTimeout();
  330.        
  331.         //*************************************************//
  332.         if(!(GPIO0_IOPIN & (1<<BUT1PIN))){
  333.             ShowButtonLCD(BUT1PIN);
  334.             Delay_us(500000);
  335.             CAN_ChangeSymulationState();
  336.         }
  337.         if(!(GPIO0_IOPIN & (1<<BUT2PIN))){
  338.             ShowButtonLCD(BUT2PIN);
  339.             Delay_us(500000);
  340.             ////
  341.         }
  342.        
  343.         CAN_HandleCanReceivers(CAN_PORT_1);
  344.         CAN_HandleCanReceivers(CAN_PORT_2);
  345.         CAN_HandleCanReceivers(CAN_PORT_3);
  346.         CAN_HandleCanReceivers(CAN_PORT_4);
  347.    
  348.    
  349.         //*************************************************//
  350.         if(CAN_GetSymulationState() == _TRUE)
  351.         {
  352.             if (sendSymulPacket == _TRUE)
  353.             {
  354.                 sendSymulPacket = _FALSE;
  355.                 CAN_SymulPacket();
  356.             }
  357.         }
  358.     }
  359.     return 0;
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement