コード例 #1
0
        public bool InitCAN(enBaudRate baudrate, UInt16 filter, UInt16 mask)
        {
            // Configure SPI
            var configSPI = new SPI.Configuration(Pins.GPIO_NONE, LOW, 0, 0, HIGH, HIGH, 10000, SPI.SPI_module.SPI1);  //a0 D10

            spi = new SPI(configSPI);

            // Write reset to the CAN transceiver.
            //           spi.Write(new byte[] { RESET });
            //Read mode and make sure it is config
            Thread.Sleep(100);
            // BECKER WHAT IS HAPPENING HERE.
//            byte mode = (byte)(ReadRegister(CANSTAT));  // >> 5);
//            if (mode != 0x04)
//            {
//                return false;
//            }
//            else

            //  Must set the filters and masks while the 2515 is in reset state.

            //SetMask(mask, filter);
            //SetCANBaud(baudrate);
            return(true);
        }
コード例 #2
0
        /// <summary>Set the CAN baud rate.</summary>
        /// <param name="baudrate">The configured baudrate.</param>
        /// <returns>True if configured.</returns>
        ///
        public bool SetCANBaud(enBaudRate baudrate)
        {
            byte brp;

            //BRP<5:0> = 00h, so divisor (0+1)*2 for 125ns per quantum at 16MHz for 500K
            //SJW<1:0> = 00h, Sync jump width = 1
            switch (baudrate)
            {
            case enBaudRate.CAN_BAUD_10K:
                brp = 5;
                break;

            case enBaudRate.CAN_BAUD_50K:
                brp = 4;
                break;

            case enBaudRate.CAN_BAUD_100K:
                brp = 3;
                break;

            case enBaudRate.CAN_BAUD_125K:
                brp = 2;
                break;

            case enBaudRate.CAN_BAUD_250K:
                brp = 1;
                break;

            case enBaudRate.CAN_BAUD_500K:
                brp = 0;
                break;

            default:
                return(false);

                break;
            }

            byte[] cmdBuffer = new byte[] { WRITE, CNF1, (byte)(brp & 0x3F) };
            spi.Write(cmdBuffer);

            //PRSEG<2:0> = 0x01, 2 time quantum for prop
            //PHSEG<2:0> = 0x06, 7 time constants to PS1 sample
            //SAM = 0, just 1 sampling
            //BTLMODE = 1, PS2 determined by CNF3
            //spi.Write(new byte[] { WRITE, CNF2, 0xB1 });

            //PHSEG2<2:0> = 5 for 6 time constants after sample
            //spi.Write(new byte[] { WRITE, CNF3, 0x05 });


            //spi.Write(new byte[] { WRITE, CNF1, 0x00 });
            spi.Write(new byte[] { WRITE, CNF2, 0x90 });
            spi.Write(new byte[] { WRITE, CNF3, 0x02 });

            //SyncSeg + PropSeg + PS1 + PS2 = 1 + 2 + 7 + 6 = 16
            return(true);
        }
コード例 #3
0
        public bool InitCAN(enBaudRate baudrate, UInt16 filter0, UInt16 filter1, UInt16 mask)
        {
            // Configure SPI
            var configSPI = new SPI.Configuration(Pins.GPIO_PIN_D2, LOW, 0, 0, HIGH, HIGH, 10000, SPI.SPI_module.SPI1);  //a0 D10

            spi = new SPI(configSPI);

            // Write reset to the CAN transceiver.
            spi.Write(new byte[] { RESET });
            //Read mode and make sure it is config


            Thread.Sleep(100);
            {
                SetMask(mask, filter0, false);
                SetMask(mask, filter1, true);
                SetCANBaud(baudrate);
                return(true);
            }
        }
コード例 #4
0
ファイル: MCP2515.cs プロジェクト: meikeric/netduino-mcp2515
        /// <summary>Initialize the CAN transceiver.</summary>
        /// <param name="baudrate">The selected baud rate.</param>
        /// <returns>True if configuration was successful.</returns>
        /// <remarks>Transceiver needs to be set to normal mode before starting TX/RX operations.</remarks>
        public bool InitCAN(enBaudRate baudrate)
        {
            // Configure SPI
            var configSPI = new SPI.Configuration(SLAVESELECT, LOW, 0, 0, HIGH, HIGH, 10000, SPI.SPI_module.SPI1);

            spi = new SPI(configSPI);

            // Write reset to the CAN transceiver.
            spi.Write(new byte[] { RESET });
            //Read mode and make sure it is config
            Thread.Sleep(100);

            byte mode = (byte)(ReadRegister(CANSTAT) >> 5);

            if (mode != 0x04)
            {
                return(false);
            }
            else
            {
                SetCANBaud(baudrate);
                return(true);
            }
        }
コード例 #5
0
        /// <summary>Set the CAN baud rate.</summary>
        /// <param name="baudrate">The configured baudrate.</param>
        /// <returns>True if configured.</returns>
        /// 
        private bool SetCANBaud(enBaudRate baudrate)
        {
            byte brp;

            //BRP<5:0> = 00h, so divisor (0+1)*2 for 125ns per quantum at 16MHz for 500K
            //SJW<1:0> = 00h, Sync jump width = 1
            switch (baudrate)
            {
                case enBaudRate.CAN_BAUD_10K:
                    brp = 5;
                    break;
                case enBaudRate.CAN_BAUD_50K:
                    brp = 4;
                    break;
                case enBaudRate.CAN_BAUD_100K:
                    brp = 3;
                    break;
                case enBaudRate.CAN_BAUD_125K:
                    brp = 2;
                    break;
                case enBaudRate.CAN_BAUD_250K:
                    brp = 1;
                    break;
                case enBaudRate.CAN_BAUD_500K:
                    brp = 0;
                    break;
                default:
                    return false;

            }

            byte[] cmdBuffer = new byte[] { WRITE, CNF1, (byte)(brp & 0x3F) };
            spi.Write(cmdBuffer);

            //PRSEG<2:0> = 0x01, 2 time quantum for prop
            //PHSEG<2:0> = 0x06, 7 time constants to PS1 sample
            //SAM = 0, just 1 sampling
            //BTLMODE = 1, PS2 determined by CNF3
            spi.Write(new byte[] { WRITE, CNF2, 0xB1 });

            //PHSEG2<2:0> = 5 for 6 time constants after sample
            spi.Write(new byte[] { WRITE, CNF3, 0x05 });
            //1000              spi.Write(new byte[] { WRITE, CNF1, 0x00 });
            //1000                          spi.Write(new byte[] { WRITE, CNF2, 0x90 });
            //1000                          spi.Write(new byte[] { WRITE, CNF3, 0x02 });

            //SyncSeg + PropSeg + PS1 + PS2 = 1 + 2 + 7 + 6 = 16
            return true;
        }
コード例 #6
0
        public bool InitCAN(enBaudRate baudrate, UInt16 filter0, UInt16 filter1, UInt16 mask)
        {
            // Configure SPI
            var configSPI = new SPI.Configuration(Pins.GPIO_PIN_D2, LOW, 0, 0, HIGH, HIGH, 10000, SPI.SPI_module.SPI1);  //a0 D10

            spi = new SPI(configSPI);

            // Write reset to the CAN transceiver.
            spi.Write(new byte[] { RESET });
            //Read mode and make sure it is config

            Thread.Sleep(100);
             {

                 SetMask(mask, filter0, false);
                 SetMask(mask, filter1, true);
                 SetCANBaud(baudrate);
                return true;
            }
        }
コード例 #7
0
        /// <summary>Initialize the CAN transceiver.</summary>
        /// <param name="baudrate">The selected baud rate.</param>
        /// <returns>True if configuration was successful.</returns>
        /// <remarks>Transceiver needs to be set to normal mode before starting TX/RX operations.</remarks>
        public bool InitCAN(enBaudRate baudrate, UInt16 filter, UInt16 mask)
        {
            // Configure SPI
            var configSPI = new SPI.Configuration(Pins.GPIO_PIN_D2, LOW, 0, 0, HIGH, HIGH, 10000, SPI.SPI_module.SPI1);  //a0 D10

            spi = new SPI(configSPI);

            // Write reset to the CAN transceiver.
            spi.Write(new byte[] { RESET });
            //Read mode and make sure it is config

            Thread.Sleep(100);
            // BECKER WHAT IS HAPPENING HERE.
            //            byte mode = (byte)(ReadRegister(CANSTAT));  // >> 5);
            //            if (mode != 0x04)
            //            {
            //                return false;
            //            }
            //            else
            {
                //  Must set the filters and masks while the 2515 is in reset state.

                SetMask(mask, filter,false);
                SetCANBaud(baudrate);
                return true;
            }
        }