bartek27210

Untitled

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