Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- *****************************************************************************/
- #include "config.h"
- #include "lpc2294.h"
- #include "lcd.h"
- #include "uart.h"
- #include "armVIC.h"
- #include "can.h"
- #define STATE_0 0
- #define STATE_1 1
- #define STATE_2 2
- #define STATE_3 3
- #define STATE_4 4
- #define STATE_5 5
- #define STATE_6 6
- #define STATE_7 7
- #define STATE_8 8
- #define STATE_9 9
- #define STATE_10 10
- #define STATE_MAX STATE_10
- unsigned char sendSymulPacket = _FALSE;
- unsigned char startSymulation = _FALSE;
- #ifdef uIP_IS_USED
- #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
- #endif //uIP_IS_USED
- void __attribute__ ((interrupt("IRQ"))) timer0_cmp(void);
- void systemInit(void);
- void gpioInit(void);
- const char AppTitle[] = " Magistrala CAN ";
- #define UIP_ARP_TIMER 1000
- #define UIP_TX_TIMER 1
- #define HALF_SEK 50
- #define LED_TIMER 50
- #define ONE_SEC 100
- #define THREE_SEC 300
- #define TWO_SEC 200
- #define TEST_TIMER 100
- int TX_EventPending = _FALSE; // the hardware receive event
- int ARP_EventPending = _FALSE; // trigger the arp timer event
- unsigned long half_Sec = HALF_SEK;
- unsigned long two_Sec = 0;
- unsigned long three_Sec = THREE_SEC;
- unsigned long Uip_Timer = UIP_TX_TIMER;
- unsigned long ten_Secs = UIP_ARP_TIMER;
- unsigned long LedTimer = LED_TIMER;
- unsigned long one_Sec = ONE_SEC;
- unsigned long TestTimer = TEST_TIMER;
- //=====================================================================
- //
- //=====================================================================
- void systemInit(void)
- {
- // --- enable and connect the PLL (Phase Locked Loop) ---
- // a. set multiplier and divider
- SCB_PLLCFG = MSEL | (1<<PSEL1) | (0<<PSEL0);
- // b. enable PLL
- SCB_PLLCON = (1<<PLLE);
- // c. feed sequence
- SCB_PLLFEED = PLL_FEED1;
- SCB_PLLFEED = PLL_FEED2;
- // d. wait for PLL lock (PLOCK bit is set if locked)
- while (!(SCB_PLLSTAT & (1<<PLOCK)));
- // e. connect (and enable) PLL
- SCB_PLLCON = (1<<PLLE) | (1<<PLLC);
- // f. feed sequence
- SCB_PLLFEED = PLL_FEED1;
- SCB_PLLFEED = PLL_FEED2;
- // --- setup and enable the MAM (Memory Accelerator Module) ---
- // a. start change by turning of the MAM (redundant)
- MAM_MAMCR = 0;
- // b. set MAM-Fetch cycle to 3 cclk as recommended for >40MHz
- MAM_MAMTIM = MAM_FETCH;
- // c. enable MAM
- MAM_MAMCR = MAM_MODE;
- // --- set VPB speed ---
- SCB_VPBDIV = VPBDIV_VAL;
- // --- map INT-vector ---
- #if defined(RAM_RUN)
- SCB_MEMMAP = MEMMAP_USER_RAM_MODE;
- #elif defined(ROM_RUN)
- SCB_MEMMAP = MEMMAP_USER_FLASH_MODE;
- #else
- #error RUN_MODE not defined!
- #endif
- }
- //=====================================================================
- //
- //=====================================================================
- void gpioInit(void)
- {
- PCB_PINSEL0 = 0x00000000; //1000 0000 0000 0000 0000 0000 0000 0000
- PCB_PINSEL1 = 0;
- BCFG1 = 0x20000400;
- PCB_PINSEL2 = 0x0FE169EC;
- GPIO0_IODIR = 0x714024F1; //0111 0001 0100 0000 0010 0100 1111 0001 - E,R_W,RS,TD4,TD3,TD2,LIGHT_LCD,D4-D7,TXD0
- GPIO1_IODIR = 0x02000003; //0000 0010 0000 0000 0000 0000 0000 0011 - PULL_PIN_P1, CS0, OE
- GPIO0_IODIR &= ~((1<<BUT1PIN)|(1<<BUT2PIN)); // define Button-Pins as inputs
- GPIO0_IODIR |= (1<<SCL_TEST_PIN);
- }
- //=====================================================================
- //
- //=====================================================================
- #define TxTCR_COUNTER_ENABLE (1<<0)
- #define TxMCR_INT_ON_MR0 (1<<0)
- #define TxMCR_RESET_ON_MR0 (1<<1)
- #define TxIR_MR0_FLAG (1<<0)
- void timer0_init(void)
- {
- T0_MR0 = ((FOSC*PLL_M)/(1000/10))-1; // Compare-hit at 10mSec (-1 reset "tick")
- T0_MCR = TxMCR_INT_ON_MR0 | TxMCR_RESET_ON_MR0; // Interrupt and Reset on MR0
- T0_TCR = TxTCR_COUNTER_ENABLE; // Timer0 Enable
- }
- void ShowButtonLCD(unsigned char button)
- {
- switch(button){
- case 15: LCD_WriteString(" Button 1 ",LCD_LINE_2,0);break;
- case 16: LCD_WriteString(" Button 2 ",LCD_LINE_2,0);break;
- default: return;
- }
- two_Sec=TWO_SEC;
- }
- //=====================================================================
- //
- //=====================================================================
- void timer0_cmp(void)
- {
- UART0_Timeout();
- LCD_RefreshTimer();
- three_Sec--;
- if (three_Sec == 0)
- {
- three_Sec = THREE_SEC;
- sendSymulPacket = _TRUE;
- }
- half_Sec--; // Decrement half sec counter
- if (half_Sec == 0) // Count 1ms intervals for half a second
- {
- half_Sec = HALF_SEK;
- }
- if(two_Sec)two_Sec--;
- if (two_Sec == 1)
- {
- LCD_WriteString(" ",LCD_LINE_2,0);
- }
- Uip_Timer--; // Decrement half sec counter
- if (Uip_Timer == 0) // Count 1ms intervals for half a second
- {
- Uip_Timer = UIP_TX_TIMER; // and then set the receive poll flag
- TX_EventPending = _TRUE;
- }
- ten_Secs--;
- if (ten_Secs == 0) // Count 1ms intervals for ten seconds
- {
- ten_Secs = UIP_ARP_TIMER; // and then set the event required to
- ARP_EventPending = _TRUE; // trigger the arp timer if necessary
- }
- TestTimer--;
- if(TestTimer == 0)
- {
- TestTimer = TEST_TIMER;
- }
- T0_IR = TxIR_MR0_FLAG; // Clear interrupt flag by writing 1 to Bit 0
- VICVectAddr = 0; // Acknowledge Interrupt (rough?)
- }
- //=====================================================================
- // Obsluguje odbiorniki CAN
- //=====================================================================
- void CAN_HandleCanReceivers(unsigned char canNumExt)
- {
- CAN_MSG localMsgBufRec; // Buffers one CAN message
- unsigned char canBufferRec[12];
- int bytesCntRec;
- unsigned char canNumLocal;
- int i;
- canNumLocal = canNumExt;
- for(i=0; i<12; i++)
- canBufferRec[i] = 0;
- //funkcja sprawdza czy są dane odebrane w przerwaniu
- if (CAN_PullMessage(canNumLocal,&localMsgBufRec) == _TRUE)
- {
- bytesCntRec = (int)((localMsgBufRec.Frame >> 16) & 0x0000000F);
- UART0_Puts("\r\n CAN ");
- UART0_SendNumber(canNumLocal);
- UART0_Puts(" received ");
- UART0_SendNumber(bytesCntRec);
- UART0_Puts(" byte data,");
- UART0_Puts(" MsgId ");
- UART0_SendNumber((short)localMsgBufRec.MsgID);
- // jesli id ramki 0 to nie wysylaj
- if(localMsgBufRec.MsgID & 0x0080)
- {
- UART0_Puts("\r\n Handling simulation CAN packet");
- //***************************************************************//
- switch(canNumLocal){
- case CAN_PORT_1:
- case CAN_PORT_2:
- case CAN_PORT_3:
- canBufferRec[0] = 0x08;
- canBufferRec[1] = (unsigned char)(localMsgBufRec.DatA & 0x000000FF);
- canBufferRec[2] = 0x00;
- canBufferRec[3] = canBufferRec[1] + 1;
- break;
- case CAN_PORT_4:
- canBufferRec[1] = 0;
- CAN_ChangeSymulationState();
- UART0_Puts("\r\n Simulation stopped\r\n");
- break;
- }
- //***************************************************************//
- if(CAN_SendPacketToCan((canBufferRec[1] & (0x0F)),canBufferRec) == _TRUE)
- {
- UART0_Puts("\r\n Send SYM packet to CAN");
- UART0_SendNumber(canNumLocal+1);
- UART0_Puts(" ,msg id= ");
- UART0_SendNumber(canBufferRec[1]);
- }
- return;
- }
- }
- }
- //=====================================================================
- //
- //=====================================================================
- void CAN_SymulPacket(void)
- {
- unsigned char canBufferTest[12];
- int i;
- if (startSymulation == _FALSE) return;
- for(i=0; i<12; i++)
- canBufferTest[i] = 0;
- //****************************************************************//
- canBufferTest[0] = 0x08;
- canBufferTest[1] = CAN_PORT_1 + 0x80;
- canBufferTest[2] = 0x00;
- canBufferTest[3] = canBufferTest[1] + 1;
- if(CAN_SendPacketToCan(CAN_PORT_1, canBufferTest) == _TRUE){
- UART0_Puts("\r\n SendSYM packet to CAN1, msg id = ");
- UART0_SendNumber(canBufferTest[1]);
- }else UART0_Puts("\r\n ERROR send TEST packet to CAN");
- }
- //=====================================================================
- // funkcja główna
- //=====================================================================
- int main(void)
- {
- //*************************************************//
- systemInit();
- gpioInit();
- VIC_Init();
- LCD_Init();
- UART0_Init(UART_BAUD(UART0_BAUD), UART_8N1, UART_FIFO_8);
- enableIRQ();
- timer0_init();
- VIC_Install_IRQ(VIC_TIMER0, (void*)timer0_cmp);
- //*************************************************//
- UART0_Puts("\r\n RESET");
- LCD_WriteString(AppTitle,LCD_LINE_1,1);
- //*************************************************//
- if((CAN_Init(CAN_PORT_1, CANBitrate1000k_60MHz) == 0) ||
- (CAN_Init(CAN_PORT_2, CANBitrate1000k_60MHz) == 0) ||
- (CAN_Init(CAN_PORT_3, CANBitrate1000k_60MHz) == 0) ||
- (CAN_Init(CAN_PORT_4, CANBitrate1000k_60MHz)) == 0)
- UART0_Puts("\r\n CAN bus is not initialized!");
- CAN_SetFilter(CAN_PORT_1, 0x0022);
- CAN_SetFilter(CAN_PORT_1, 0x0033);
- CAN_SetFilter(CAN_PORT_1, 0x0044);
- CAN_SetFilter(CAN_PORT_2, 0x0002);
- CAN_SetFilter(CAN_PORT_3, 0x0003);
- CAN_SetFilter(CAN_PORT_4, 0x0004);
- CAN_SetFilter(CAN_PORT_1, 0x0081);
- CAN_SetFilter(CAN_PORT_2, 0x0082);
- CAN_SetFilter(CAN_PORT_3, 0x0083);
- CAN_SetFilter(CAN_PORT_4, 0x0084);
- //*************************************************//
- while(1)
- {
- // Obsluga transmisji RS232
- UART0_HandleProtocol();
- UART0_HandlePacket();
- UART0_CheckProtocolTimeout();
- //*************************************************//
- if(!(GPIO0_IOPIN & (1<<BUT1PIN))){
- ShowButtonLCD(BUT1PIN);
- Delay_us(500000);
- CAN_ChangeSymulationState();
- }
- if(!(GPIO0_IOPIN & (1<<BUT2PIN))){
- ShowButtonLCD(BUT2PIN);
- Delay_us(500000);
- ////
- }
- CAN_HandleCanReceivers(CAN_PORT_1);
- CAN_HandleCanReceivers(CAN_PORT_2);
- CAN_HandleCanReceivers(CAN_PORT_3);
- CAN_HandleCanReceivers(CAN_PORT_4);
- //*************************************************//
- if(CAN_GetSymulationState() == _TRUE)
- {
- if (sendSymulPacket == _TRUE)
- {
- sendSymulPacket = _FALSE;
- CAN_SymulPacket();
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement