drivers/ecos_lpc2138_sja1000/sja1000.c
changeset 93 16c8ceea8f18
parent 92 0d84d95790d9
child 94 bdf4c86be6b2
equal deleted inserted replaced
92:0d84d95790d9 93:16c8ceea8f18
     1 /*
       
     2 This file is part of CanFestival, a library implementing CanOpen Stack.
       
     3 
       
     4  Author: Christian Fortin (canfestival@canopencanada.ca)
       
     5 
       
     6 See COPYING file for copyrights details.
       
     7 
       
     8 This library is free software; you can redistribute it and/or
       
     9 modify it under the terms of the GNU Lesser General Public
       
    10 License as published by the Free Software Foundation; either
       
    11 version 2.1 of the License, or (at your option) any later version.
       
    12 
       
    13 This library is distributed in the hope that it will be useful,
       
    14 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16 Lesser General Public License for more details.
       
    17 
       
    18 You should have received a copy of the GNU Lesser General Public
       
    19 License along with this library; if not, write to the Free Software
       
    20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    21 */
       
    22 
       
    23 #include <cyg/kernel/kapi.h>
       
    24 #include <cyg/hal/hal_arch.h>
       
    25 
       
    26 #include "lpc2138_pinout.h"
       
    27 #include "lpc2138_defs.h"
       
    28 #include "lpc2138.h"
       
    29 
       
    30 #include "sja1000.h"
       
    31 
       
    32 
       
    33 #define CYGNUM_HAL_INTERRUPT_1 CYGNUM_HAL_INTERRUPT_EINT1
       
    34 #define CYGNUM_HAL_PRI_HIGH    0
       
    35 
       
    36 
       
    37 cyg_uint32 interrupt_1_isr(cyg_vector_t vector, cyg_addrword_t data);
       
    38 void interrupt_1_dsr(cyg_vector_t   vector,
       
    39                             cyg_ucount32   count,
       
    40                             cyg_addrword_t data);
       
    41 
       
    42 
       
    43 /* Interrupt for CAN device. */
       
    44 static cyg_interrupt interrupt_1;
       
    45 static cyg_handle_t  interrupt_1_handle;
       
    46 
       
    47 
       
    48 void init_sja1000(void)
       
    49 {
       
    50     do
       
    51     {
       
    52         sja1000_write(MOD, 1<<RM);                  /* demande reset */
       
    53     }
       
    54     while ((sja1000_read(MOD) & (1<<RM)) == 0);   /* loop until reset good */
       
    55 
       
    56 /*
       
    57     sja1000_write(bustiming0, ((0<<SJW1)|(0<<SJW0)|(0<<BRP5)|(0<<BRP4)|(0<<BRP3)|(0<<BRP2)|(0<<BRP1)|(0<<BRP0)));
       
    58     sja1000_write(bustiming1, ((1<<SAM)|(0<<TSEG22)|(1<<TSEG21)|(0<<TSEG20)|(0<<TSEG13)|(1<<TSEG12)|(0<<TSEG11)|(0<<TSEG10)));
       
    59 */
       
    60 
       
    61 /* OUTPUT CONTROL REGISTER */
       
    62     sja1000_write(outputcontrol, ((1<<OCTP1)|(1<<OCTN1)|(0<<OCPOL1)|(1<<OCTP0)|(1<<OCTN0)|(0<<OCPOL0)|(1<<OCMODE1)|(0<<OCMODE0)));
       
    63 
       
    64 
       
    65     sja1000_write(clockdivider, ((1<<CANmode)|(1<<CBP)|(1<<RXINTEN)|(0<<clockoff)|(1<<CD2)|(1<<CD1)|(1<<CD0)));
       
    66     sja1000_write(16,  0x01);   /* 0 code all accept block high bit */
       
    67     sja1000_write(17,  0x00);   /* 0 all accept block high bit */
       
    68     sja1000_write(18,  0x00);   /* 0 all accept block high bit */
       
    69     sja1000_write(19,  0x00);   /* 0 all accept block high bit */
       
    70     sja1000_write(20,  0xFE);   /* 1 mask */
       
    71     sja1000_write(21,  0xFF);
       
    72     sja1000_write(22,  0xFF);
       
    73     sja1000_write(23,  0xFF);
       
    74     sja1000_write(IER, 0x01);
       
    75     sja1000_write(clockdivider, ((1<<CANmode)|(1<<CBP)|(1<<RXINTEN)|(0<<clockoff)|(1<<CD2)|(1<<CD1)|(0<<CD0)));
       
    76 
       
    77     do
       
    78     {
       
    79         sja1000_write(MOD, (1<<AFM)|(0<<STM)|(0<<RM));
       
    80     }
       
    81     while ((sja1000_read(MOD) & (1<<RM)) == 1);   /* loop until reset gone */
       
    82 }
       
    83 
       
    84 
       
    85 /***************************************************************************/
       
    86 
       
    87 
       
    88 void init_interrupts(void)
       
    89 {
       
    90     cyg_vector_t   interrupt_1_vector   = CYGNUM_HAL_INTERRUPT_1;
       
    91     cyg_priority_t interrupt_1_priority = CYGNUM_HAL_PRI_HIGH;
       
    92 
       
    93     cyg_interrupt_create(
       
    94         interrupt_1_vector,
       
    95         interrupt_1_priority,
       
    96         (cyg_addrword_t) 0,
       
    97         (cyg_ISR_t *) &interrupt_1_isr,
       
    98         (cyg_DSR_t *) &interrupt_1_dsr,
       
    99         &interrupt_1_handle,
       
   100         &interrupt_1);
       
   101 
       
   102     cyg_interrupt_attach(interrupt_1_handle);
       
   103 
       
   104     cyg_interrupt_acknowledge(interrupt_1_vector);
       
   105 
       
   106     cyg_interrupt_unmask(interrupt_1_vector);
       
   107 }
       
   108 
       
   109 /* External Interrupt 1 Service */
       
   110 void eint1_srv(void) /*__irq*/ 
       
   111 {
       
   112 	//++intrp_count;                              // increment interrupt count
       
   113 	EXTINT      = 2;                            // Clear EINT1 interrupt flag
       
   114 	VICVectAddr = 0;                            // Acknowledge Interrupt
       
   115 }
       
   116 
       
   117 
       
   118 /* Initialize EINT1 Interrupt Pin */
       
   119 void init_eint1(void)
       
   120 {
       
   121 	EXTMODE       = 0x2;                         // Edge sensitive mode on EINT1
       
   122 	EXTPOLAR      = 0;                           // falling edge sensitive
       
   123 	P0_PINSEL0   |= 2 << 28;                     // Enable EINT1 on GPIO_0.14
       
   124 	VICVectAddr0  = (unsigned long) eint1_srv;   // set interrupt vector in VIC 0
       
   125 	VICVectCntl0  = 0x20 | 15;                   // use VIC 0 for EINT1 Interrupt
       
   126 	EXTINT        = 2;
       
   127 	VICIntEnable  = 1 << 15;                     // Enable EINT1 Interrupt
       
   128 }
       
   129 
       
   130 
       
   131 /*
       
   132 {
       
   133 	unsigned char byte=sja1000_read(0x02);
       
   134 
       
   135 	if (byte & 0x01)
       
   136 	{
       
   137 		// RXFIFO full
       
   138 	}
       
   139 	
       
   140 	if (byte & 0x02)
       
   141 	{
       
   142 		// overrun
       
   143 	}
       
   144 	
       
   145 	if (byte & 0x04)
       
   146 	{
       
   147 		// the cpu may write a msg
       
   148 	}
       
   149 	
       
   150 	if (byte & 0x08)
       
   151 	{
       
   152 		// tx complete
       
   153 	}
       
   154 	
       
   155 	if (byte & 0x10)
       
   156 	{
       
   157 		// receiving
       
   158 	}
       
   159 	
       
   160 	if (byte & 0x20)
       
   161 	{
       
   162 		// transmitting
       
   163 	}
       
   164 	
       
   165 	if (byte & 0x40)
       
   166 	{
       
   167 		// at least one of the error counter has reached or exceeeded
       
   168 		// the CPU warning limit
       
   169 	}
       
   170 	
       
   171 	if (byte & 0x80)
       
   172 	{
       
   173 		// bus off
       
   174 	}
       
   175 }
       
   176 */
       
   177 
       
   178