Subversion Repositories HomeAutomation

Rev

Rev 73 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * @file    test.c
  3.  * Very basic test program for the MCP2515 driver. This currently tests only
  4.  * the SPI communication with the chip.
  5.  *
  6.  * @author  Jimmy Myhrman (jimmy@myhrman.org)
  7.  * @date    2005-11-03
  8.  */
  9.  
  10.  
  11. /*------------------------------------------------------------------------------
  12.  * Includes
  13.  *----------------------------------------------------------------------------*/
  14. #include <avr/interrupt.h>
  15. #include "mcp2510.h"
  16. #include "uart.h"
  17.  
  18.  
  19. /*------------------------------------------------------------------------------
  20.  * Defines
  21.  *----------------------------------------------------------------------------*/
  22.  
  23.  
  24. /*------------------------------------------------------------------------------
  25.  * Globals
  26.  *----------------------------------------------------------------------------*/
  27.  
  28.  
  29. /*------------------------------------------------------------------------------
  30.  * Prototypes
  31.  *----------------------------------------------------------------------------*/
  32.  
  33.  
  34. /*------------------------------------------------------------------------------
  35.  * Main Program
  36.  *----------------------------------------------------------------------------*/
  37. int main(void) {
  38.     uartInit(12);       /* baudrate 38400 */
  39.     sei();
  40.     MCP_init();
  41.    
  42.     uartPutString("MCP2515 CAN Test\n");
  43.    
  44.     /* request configuration mode */
  45.     MCP_WRITE_B(CANCTRL, CANCTRL_REQOP, REQOP_CONFIG);
  46.    
  47.     /* configure SJW and Tq duration */
  48.     MCP_WRITE_B(CNF1, CNF1_SJW, 4-1);   /* SJW = X-1 [Tq] */
  49.     MCP_WRITE_B(CNF1, CNF1_BRP, 0);     /* Tq = 2*(X+1)/FOSC [s] */
  50.    
  51.     /* configure phase seg1 and propseg */
  52.     MCP_WRITE_B(CNF2, CNF2_BTLMODE, 1);
  53.     MCP_WRITE_B(CNF2, CNF2_SAM, 1);
  54.     MCP_WRITE_B(CNF2, CNF2_PHSEG1, 4-1);    /* PHSEG1 = X-1 [Tq] */
  55.     MCP_WRITE_B(CNF2, CNF2_PRSEG, 4-1);     /* PRSEG = X-1 [Tq] */
  56.    
  57.     /* configure phase seg2 and wakeup filter */
  58.     MCP_WRITE_B(CNF3, CNF3_WAKFIL, 1);
  59.     MCP_WRITE_B(CNF3, CNF3_PHSEG2, 4-1);    /* PHSEG2 = X-1 [Tq] */
  60.    
  61.     /* configure interrupts */
  62.     MCP_WRITE_R(CANINTE, 0);
  63.    
  64.     // Mark all filter bits as don't care:
  65.     mcp_write_can_id(RXMnSIDH(0), 1, 0);
  66.     mcp_write_can_id(RXMnSIDH(1), 1, 0);
  67.     // Anyway, set all filters to 0:
  68.     mcp_write_can_id(RXF0SIDH, 0, 0);
  69.     mcp_write_can_id(RXF1SIDH, 0, 0);
  70.     mcp_write_can_id(RXF2SIDH, 0, 0);
  71.     mcp_write_can_id(RXF3SIDH, 0, 0);
  72.     mcp_write_can_id(RXF4SIDH, 0, 0);
  73.     mcp_write_can_id(RXF5SIDH, 0, 0);
  74.    
  75.     /* request loopback mode */
  76.     MCP_WRITE_B(CANCTRL, CANCTRL_REQOP, REQOP_LOOPBACK);
  77.    
  78.     uint8_t tmp = MCP_read(CANCTRL);
  79.     uartPutString("CANCTRL = ");
  80.     uartPutUInt8(tmp, 2);
  81.     uartPutString("\n");
  82.     tmp = MCP_read(CANSTAT);
  83.     uartPutString("CANSTAT = ");
  84.     uartPutUInt8(tmp, 2);
  85.     uartPutString("\n");
  86.     tmp = MCP_read(CNF1);
  87.     uartPutString("CNF1 = ");
  88.     uartPutUInt8(tmp, 2);
  89.     uartPutString("\n");
  90.     tmp = MCP_read(CNF2);
  91.     uartPutString("CNF2 = ");
  92.     uartPutUInt8(tmp, 2);
  93.     uartPutString("\n");
  94.     tmp = MCP_read(CNF3);
  95.     uartPutString("CNF3 = ");
  96.     uartPutUInt8(tmp, 2);
  97.     uartPutString("\n");
  98.     tmp = MCP_read(CANINTE);
  99.     uartPutString("CANINTE = ");
  100.     uartPutUInt8(tmp, 2);
  101.     uartPutString("\n");
  102.    
  103.     /* main loop */
  104.     while(1) {
  105.         /* do nothing */
  106.        
  107.     }
  108. }
  109.