Ejemplo n.º 1
0
Archivo: CANL.c Proyecto: baw959/RTOS
void BCAN_init(unsigned long bitRate)
{
  tCANMsgObject sCANMessage;
  //unsigned char ucMsgData[8];
  
  GPIO_PORTE_AFSEL_R |= 0x30; //PORTE AFSEL bits 5,4
  GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R&0xFF00FFFF)|0x00880000;
  GPIO_PORTE_DEN_R |= 0x30;
  GPIO_PORTE_DIR_R |= 0x20;
  
  CAN_RX_FIFOFifo_Init();
  OS_InitSemaphore(&CANmessagesReceived,0);
  
  CANInit(CAN0_BASE);
  CANBitRateSet(CAN0_BASE, 80000000, 250000);
  //CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);    //these are enabled later with a seperate call
  //IntEnable(INT_CAN0);                              //since the filesystem can't seem to initialize while the ECU is shooting out CAN messages
  //CANEnable(CAN0_BASE);
  
  sCANMessage.ulMsgID = 0; // CAN msg ID - 0 for any
  sCANMessage.ulMsgIDMask = 0; // mask is 0 for any ID
  sCANMessage.ulFlags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER;
  sCANMessage.ulMsgLen = 8; // allow up to 8 bytes
  
  CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_RX);
}
Ejemplo n.º 2
0
// Initialize CAN port
void CAN0_Open(void){
	uint32_t volatile delay;
	int32_t sr;
	
	sr = StartCritical();
  MailFlag = false;

  SYSCTL_RCGCCAN_R |= 0x00000001;  // CAN0 enable bit 0
  SYSCTL_RCGCGPIO_R |= 0x00000010;  // RCGC2 portE bit 4
  for(delay=0; delay<10; delay++){};
  GPIO_PORTE_AFSEL_R |= 0x30; //PORTE AFSEL bits 5,4
// PORTE PCTL 88 into fields for pins 5,4
  GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R&0xFF00FFFF)|0x00880000;
  GPIO_PORTE_DEN_R |= 0x30;
  GPIO_PORTE_DIR_R |= 0x20;
      
  CANInit(CAN0_BASE);
  CANBitRateSet(CAN0_BASE, 80000000, CAN_BITRATE);
  CANEnable(CAN0_BASE);
// make sure to enable STATUS interrupts
  CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);
// Set up filter to receive these IDs
// in this case there is just one type, but you could accept multiple ID types
  CAN0_Setup_Message_Object(Ping1_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Ping1_ID, MSG_OBJ_TYPE_RX);
	CAN0_Setup_Message_Object(Ping2_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Ping2_ID, MSG_OBJ_TYPE_RX);
	CAN0_Setup_Message_Object(Ping3_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Ping3_ID, MSG_OBJ_TYPE_RX);
	CAN0_Setup_Message_Object(Ping4_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Ping4_ID, MSG_OBJ_TYPE_RX);
	CAN0_Setup_Message_Object(Button1_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Button1_ID, MSG_OBJ_TYPE_RX);
	CAN0_Setup_Message_Object(Button2_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Button2_ID, MSG_OBJ_TYPE_RX);
	CAN0_Setup_Message_Object(IR_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, IR_ID, MSG_OBJ_TYPE_RX);
  NVIC_EN1_R = (1 << (INT_CAN0 - 48)); //IntEnable(INT_CAN0);
		
	EndCritical(sr);
  return;
}
Ejemplo n.º 3
0
void NetworkInit(void)
{
	// configure GPIO pins for CAN
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    // set up the CAN controller
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
    CANInit(CAN0_BASE);
    CANBitRateSet(CAN0_BASE, 8000000, CAN_BIT_RATE);
    CANEnable(CAN0_BASE);
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);
    NetworkMsgInit();

//    volatile int i;
//    CAN0_CTL_R;
//    for (i=0;i<8;i++);
//    CAN0_CTL_R |= CAN_CTL_TEST; // enable CAN test mode
//    for (i=0;i<8;i++);
//    CAN0_TST_R;
//    for (i=0;i<8;i++);
//    CAN0_TST_R |= CAN_TST_LBACK; // enable loopback mode

    // removed because SYS/BIOS configures this
//    IntPrioritySet(INT_CAN0, 96);
//    IntEnable(INT_CAN0);
}
Ejemplo n.º 4
0
// Initialize CAN port
void CAN0_Open(void){uint32_t volatile delay;
	CAN0_Fifo_Init(16);
  OS_InitSemaphore(&CAN0ReceiveFree, 1);

	PacketLost = 0;                            
	MailFlag = false;
 
  SYSCTL_RCGCCAN_R |= 0x00000001;  // CAN0 enable bit 0
  SYSCTL_RCGCGPIO_R |= 0x00000010;  // RCGC2 portE bit 4
  for(delay=0; delay<10; delay++){};
  GPIO_PORTE_AFSEL_R |= 0x30; //PORTE AFSEL bits 5,4
// PORTE PCTL 88 into fields for pins 5,4
  GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R&0xFF00FFFF)|0x00880000;
  GPIO_PORTE_DEN_R |= 0x30;
  GPIO_PORTE_DIR_R |= 0x20;
      
  CANInit(CAN0_BASE);
  CANBitRateSet(CAN0_BASE, 80000000, CAN_BITRATE);
  CANEnable(CAN0_BASE);
// make sure to enable STATUS interrupts
  CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);
// Set up filter to receive these IDs
// in this case there is just one type, but you could accept multiple ID types
  CAN0_Setup_Message_Object(RCV_ID, MSG_OBJ_RX_INT_ENABLE, 8, NULL, RCV_ID, MSG_OBJ_TYPE_RX);
  NVIC_EN1_R = (1 << (INT_CAN0 - 48)); //IntEnable(INT_CAN0);
  return;
}
Ejemplo n.º 5
0
void setup(){
	Serial.begin(9600);
	Serial.println("DEBUG1");
	SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
	SysCtlPeripheralEnable(GPIO_PORTE_BASE);
	GPIOPinTypeCAN(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5);

	GPIOPinConfigure(GPIO_PE4_CAN0RX);
	GPIOPinConfigure(GPIO_PE5_CAN0TX);

	tCANMsgObject sCANMessage;
    uint8_t ucMsgData[8];

			Serial.println("DEBUG2");

	uint8_t pui8BufferIn[8];
	uint8_t pui8BufferOut[8];

			Serial.println("DEBUG3");

	CANInit(CAN0_BASE);

		    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000);

	CANEnable(CAN0_BASE);

  sCANMessage.ui32MsgID = 0;                        // CAN msg ID - 0 for any
    sCANMessage.ui32MsgIDMask = 0;                    // mask is 0 for any ID
    sCANMessage.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER;
    sCANMessage.ui32MsgLen = 8;                       // allow up to 8 bytes

    //
    // Now load the message object into the CAN peripheral.  Once loaded the
    // CAN will receive any message on the bus, and an interrupt will occur.
    // Use message object 1 for receiving messages (this is not the same as
    // the CAN ID which can be any value in this example).
    //
    CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_RX);

			Serial.println("DEBUG5");

	while((CANStatusGet(CAN0_BASE, CAN_STS_NEWDAT) & 1) == 0)
	{
        //
        // Read the message out of the message object.
        //
		CANMessageGet(CAN0_BASE, 1, &sCANMessage, true);
			Serial.println(sCANMessage.ui32MsgLen);

	}
				Serial.println(sCANMessage.ui32MsgLen);

			Serial.println(sCANMessage.ui32MsgID);

}
Ejemplo n.º 6
0
void CAN::init() {
	// Initialize the CAN controller
	CANInit(_CAN_BASE);
	CANBitRateSet(_CAN_BASE, SysCtlClockGet(), 125000);
	CANIntEnable(_CAN_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);
	IntEnable(_CAN_INT);
	HWREG(CAN0_BASE + CAN_O_CTL) |= CAN_CTL_TEST;
	HWREG(CAN0_BASE + CAN_O_TST) = CAN_TST_LBACK | CAN_TST_SILENT;
	CANEnable(_CAN_BASE);
	// Configure LoopBack mode
}
Ejemplo n.º 7
0
//*****************************************************************************
//
// This function configures the CAN hardware and the message objects so that
// they are ready to use once the application returns from this function.
//
//*****************************************************************************
void
CANConfigure(void)
{
    //
    // Configure CAN Pins
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Enable the CAN controllers.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

    //
    // Reset the state of all the message object and the state of the CAN
    // module to a known state.
    //
    CANInit(CAN0_BASE);

    //
    // Configure the bit rate for the CAN device, the clock rate to the CAN
    // controller is fixed at 8MHz for this class of device and the bit rate is
    // set to 250000.
    //
    CANBitRateSet(CAN0_BASE, 8000000, 250000);

    //
    // Take the CAN1 device out of INIT state.
    //
    CANEnable(CAN0_BASE);

    //
    // Enable interrups from CAN controller.
    //
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR);

    //
    // Set up the message object that will receive all messages on the CAN
    // bus.
    //
    CANConfigureNetwork();

    //
    // Enable interrupts for the CAN in the NVIC.
    //
    IntEnable(INT_CAN0);
}
Ejemplo n.º 8
0
void main() {
    struct CANMessage CANRXMsg;
    unsigned char byte1, byte2;
    int battv;
    float batt;
    char batts[50];

    TRISBbits.RB3 = 1;
    TRISBbits.RB4 = 0;
    TRISBbits.RB5 = 0;
    LATBbits.LATB4 = 0;
    LATBbits.LATB5 = 0;
    //OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT &
    //        USART_CONT_RX & USART_BRGH_LOW, 0x33);


    PIE3bits.RXB0IE = 1;
    PORTBbits.RB4 = 0;
    PORTBbits.RB5 = 0;

    CANInit();
    
    enable_interrupts();
   
    while(1)
    {
        PORTBbits.RB4 = 1;
        if(CANRXMessageIsPending())
        {
            
            CANRXMsg = CANGet();
            if (CANRXMsg.Address == 1520)
            {
                byte1 = CANRXMsg.Data[4];
                byte2 = CANRXMsg.Data[5];
                battv = (byte1 << 8) + byte2;
                batt = battv * 0.1;
                //sprintf(batts, "Bat: %f V.", batt);
                //putsUSART(batts);
            }

        }
//        else
//            PORTBbits.RB5 = 1;
    }

}
Ejemplo n.º 9
0
void init(){
	// Init OLED
  RIT128x96x4Init(1000000);
	
	// Set the clock to 50 MHz
	SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ);
	
	// Select
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);	
	GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_1);
	GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
	
	// Navigation Switches
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
	GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
	GPIOPadConfigSet(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
	
	// CAN Connection
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
	GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);
	CANInit(CAN0_BASE);
	CANBitRateSet(CAN0_BASE, 8000000, 250000);
	CANIntEnable(CAN0_BASE, CAN_INT_MASTER);
	IntEnable(INT_CAN0);
	CANEnable(CAN0_BASE);
	
	// CAN Objects
	transmit.ulMsgID= 0x200;
	transmit.ulMsgIDMask= 0;
	transmit.ulMsgLen= 2*sizeof(unsigned long);
	
	// IR Receiver
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);	
	GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_4);
	
	// Timer
	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);	
	TimerConfigure(TIMER0_BASE, TIMER_CFG_A_CAP_TIME);
	TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_NEG_EDGE);
	TimerLoadSet(TIMER0_BASE, TIMER_A, TIME_WIDTH);
	TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT | TIMER_TIMA_TIMEOUT);
	TimerEnable(TIMER0_BASE, TIMER_A);
	IntEnable(INT_TIMER0A);
}
Ejemplo n.º 10
0
int main(int argc,char *argv[]) {
  Bus *bus;
  int i;
  ArrayAuto(char,data,ARR(1,2,3,4,5,6,7));

  can = CANInit(0);
  if (!can) {
    printf("No CAN object found\n");
    return 1;
  }
  if (SignalCapture(2,alarmsig) < 0) {
    fprintf(stderr,"signal capture error:%m\n");
    return 1;
  }
  can->BaudSet(can,1000000);
  can->Tx(can,0,0x234,data);
  return 0;
}
Ejemplo n.º 11
0
void vComTaskInitImpl(void)
{
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
 
    GPIOPinTypeCAN(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7);
    
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
    CANInit(CAN0_BASE);

    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 1000000);
	
    CANEnable(CAN0_BASE);

    CANIntEnable(CAN0_BASE, CAN_INT_MASTER);
   
	IntEnable(INT_CAN0);

    IntMasterEnable();

}
Ejemplo n.º 12
0
static int can_init(const can_cfg_t *cfg)
{
	tCANMsgObject MsgObjectRx;
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
	GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);
	CANInit(CAN0_BASE);

	//PLLclock is 400M, and can module clock is 8M(400/50)
	if (CANBitRateSet(CAN0_BASE, 8000000, cfg->baud) == 0)
		return 1;

	//can receive setting,set MsgIDMask value to zero, receive all!
	MsgObjectRx.ulFlags = MSG_OBJ_NO_FLAGS | MSG_OBJ_USE_ID_FILTER;
	MsgObjectRx.ulMsgIDMask = 0;
	CANMessageSet(CAN0_BASE, LM3S_RxMsgObjNr, &MsgObjectRx, MSG_OBJ_TYPE_RX);
	CANEnable(CAN0_BASE);

	return 0;
}
Ejemplo n.º 13
0
int main(void)
{
	// Declaration and initialization of message-related data
	unsigned int txMSGSID = 0;
	unsigned char txMsgData[12] = {'1', '2', '3', '4', '5', 'S', 'X', 'U', 'C', 'L', 'D', 'R'};
	unsigned char txMsgDLC;

	// Initialize keyboard and CAN
	KeybInit();
	CANInit(NORMAL_MODE, 0x0000, 0x0000);
	LCDInit();

	// Clear LCD
	LCDClear();

	while(1)
	{

	}
}
void app_can_init()
{
    /*
    //27.2.2.34 SysCtlPeripheralPowerOn
    	//Powers on CAN peripheral.
    	//Not used in CAN example.
    SysCtlPeripheralPowerOn(SYSCTL_PERIPH_CAN0);
    */

    //27.2.2.32 SysCtlPeripheralEnable
    // GPIO port B and E needs to be enabled so these pins can be used
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);	//enable GPIO port B
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);	//enable GPIO port E

    //15.2.3.17 GPIOPinConfigure
    // Configures pins for use as a CAN device.
    // Configure the GPIO pin muxing to select CAN0 functions for these pins.
    // This step selects which alternate function is available for these pins.
    GPIOPinConfigure(GPIO_PE4_CAN0RX);
    GPIOPinConfigure(GPIO_PE5_CAN0TX);

    //15.2.3.20 GPIOPinTypeCAN
    // Enable the alternate function on the GPIO pins.  The above step selects
    // which alternate function is available.  This step actually enables the
    // alternate function instead of GPIO for these pins.
    // Configure GPIO Port E pins 4 and 5 to be used as CAN0.
    GPIOPinTypeCAN(GPIO_PORTE_BASE,GPIO_PIN_4 | GPIO_PIN_5);

    //15.2.3.27 GPIOPinTypeGPIOOutput
    // Set GPIO PORTB pins 6 and 7 as output, SW controlled.
    // CAN_RS=PB6, CAN_Enable=PB7
    GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,GPIO_PIN_6 | GPIO_PIN_7);

    //15.2.3.48 GPIOPinWrite
    // Writes a "0" to pin PB6 (CAN_RS pin of CAN transceiver TI SN65HVD64).
    GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_6,0);		//enable CAN driver slope control through value of RS resistor.
    // If a high-level input (> 0.75 VCC) is applied to CAN_RS pin (TI SN65HVD64, pin 8), the circuit enters
    // a low-current, listen only standby mode during which the driver is switched off and the receiver
    // remains active.
    // Uncomment following line (and comment previous line) if a listen-only standby-mode is desired.
    //
    // GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_6,GPIO_PIN_6); //CAN driver switched off and CAN receiver active
    //
    //
    // Writes a "1" to pin PB7 CAN_Enable pin of CAN transceiver TI SN65HVD64, enabling the external HW.
    GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_7,GPIO_PIN_7);	//enable CAN transceiver

    //27.2.2.32 SysCtlPeripheralEnable
    // The GPIO port and pins have been set up for CAN.  The CAN peripheral
    // must be enabled.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);				//enable CAN 0
    // It takes five clock cycles after the write to enable a peripheral before
    // the the peripheral is actually enabled. Uncomment following line if needed.
    //
    SysCtlDelay(2);	//it generates a delay by executing (two times) a 3 instruction cycle loop

    // 6.2.5.7 CANInit
    // Initialize the CAN controller
    CANInit(CAN0_BASE);

    // 6.2.5.1 CANBitRateSet
    // Sets the CAN bit timing values to a nominal setting based on a desired bit rate.
    // If tighter timing requirements or longer network lengths are needed, then the
    // CANBitTimingSet() function is available for full customization of all of the CAN b
    // it timing values.
    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), CAN_BITRATE);

    // 6.2.5.10 CANIntEnable & 6.2.5.11 CANIntRegister
    // - Registers an interrupt handler for the CAN controller and
    // - enable interrupts on the CAN peripheral.
    // This code uses dynamic allocation of the vector table. If you want static allocation
    // of interrupt handlers which means the name of the handler is in the vector table of
    // startup code you must also comment "CANIntRegister(CAN0_BASE, CANIntHandler)".
    CANIntRegister(CAN0_BASE, CANIntHandler); // 	needed only using dynamic vectors
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);

    //18.2.3.2 IntEnable
    //Enables CAN0 interrupts.
    IntEnable(INT_CAN0);

    //6.2.5.5 CANEnable
    //Enables the CAN controller.
    CANEnable(CAN0_BASE);
}
Ejemplo n.º 15
0
//*****************************************************************************
//
// Setup CAN0 to both send and receive at 500KHz.
// Interrupts on
// Use PE4 / PE5
//
//*****************************************************************************
void
InitCAN0(void)
{
    //
    // For this example CAN0 is used with RX and TX pins on port E4 and E5.
    // GPIO port E needs to be enabled so these pins can be used.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

    //
    // Configure the GPIO pin muxing to select CAN0 functions for these pins.
    // This step selects which alternate function is available for these pins.
    //
    GPIOPinConfigure(GPIO_PE4_CAN0RX);
    GPIOPinConfigure(GPIO_PE5_CAN0TX);

    //
    // Enable the alternate function on the GPIO pins.  The above step selects
    // which alternate function is available.  This step actually enables the
    // alternate function instead of GPIO for these pins.
    //
    GPIOPinTypeCAN(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5);

    //
    // The GPIO port and pins have been set up for CAN.  The CAN peripheral
    // must be enabled.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

    //
    // Initialize the CAN controller
    //
    CANInit(CAN0_BASE);

    //
    // Set up the bit rate for the CAN bus.  This function sets up the CAN
    // bus timing for a nominal configuration.  You can achieve more control
    // over the CAN bus timing by using the function CANBitTimingSet() instead
    // of this one, if needed.
    // In this example, the CAN bus is set to 500 kHz.
    //
    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000);

    //
    // Enable interrupts on the CAN peripheral.  This example uses static
    // allocation of interrupt handlers which means the name of the handler
    // is in the vector table of startup code.
    //
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);

    //
    // Enable the CAN interrupt on the processor (NVIC).
    //
    IntEnable(INT_CAN0);

    //
    // Enable the CAN for operation.
    //
    CANEnable(CAN0_BASE);

    //
    // Initialize a message object to be used for receiving CAN messages with
    // any CAN ID.  In order to receive any CAN ID, the ID and mask must both
    // be set to 0, and the ID filter enabled.
    //
    g_sCAN0RxMessage.ui32MsgID = CAN0RXID;
    g_sCAN0RxMessage.ui32MsgIDMask = 0;
    g_sCAN0RxMessage.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER;
    g_sCAN0RxMessage.ui32MsgLen = sizeof(g_ui8RXMsgData);

    //
    // Now load the message object into the CAN peripheral.  Once loaded the
    // CAN will receive any message on the bus, and an interrupt will occur.
    // Use message object RXOBJECT for receiving messages (this is not the
    //same as the CAN ID which can be any value in this example).
    //
    CANMessageSet(CAN0_BASE, RXOBJECT, &g_sCAN0RxMessage, MSG_OBJ_TYPE_RX);

    //
    // Initialize the message object that will be used for sending CAN
    // messages.  The message will be 1 bytes that will contain the character
    // received from the other controller. Initially it will be set to 0.
    //
    g_ui8TXMsgData = 0;
    g_sCAN0TxMessage.ui32MsgID = CAN0TXID;
    g_sCAN0TxMessage.ui32MsgIDMask = 0;
    g_sCAN0TxMessage.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
    g_sCAN0TxMessage.ui32MsgLen = sizeof(g_ui8TXMsgData);
    g_sCAN0TxMessage.pui8MsgData = (uint8_t *)&g_ui8TXMsgData;
}
Ejemplo n.º 16
0
void main_init(){

	dashboard_state=DASHBOARD_STATE_STARTING;
	
	ports_init();
	
	Timer0_init(TMR0_PRESCALER);
	
	CANInit();

	#if HAS_50HZ|HAS_200HZ|HAS_50HZ
	Timer1_init(TMR1_PRESCALER,FALSE);
	#endif

	#if HAS_10HZ|HAS_5HZ|HAS_4HZ
	Timer3_init(TMR3_PRESCALER,FALSE);
	#endif

	#if HAS_50HZ
	TIMER_Timer1_OCR1A_on();
	#endif

	#if HAS_25HZ
	TIMER_Timer1_OCR1B_on();
	#endif

	#if HAS_200HZ
	TIMER_Timer1_OCR1C_on();
	#endif

	#if HAS_10HZ
	TIMER_Timer3_OCR3A_on();
	#endif

	#if HAS_BUZZER
	buzzer_init();
	TIMER_Timer3_OCR3C_on();
	#endif
	
	#if HAS_LEDS
	led_init();
	#endif
	
	#if HAS_BUTTONS
	button_init();
	#endif
	
	#if HAS_DISPLAY
	display_init();
	#endif
	
	#if HAS_RADIO
	radio_init();
	#endif
	
	InitWDT();
	
	EventAddEvent(EVENT_INIT);
	
		
}
Ejemplo n.º 17
0
//*****************************************************************************
//
// Configure the CAN and enter a loop to receive CAN messages.
//
//*****************************************************************************
int
main(void)
{
#if defined(TARGET_IS_TM4C129_RA0) ||                                         \
    defined(TARGET_IS_TM4C129_RA1) ||                                         \
    defined(TARGET_IS_TM4C129_RA2)
    uint32_t ui32SysClock;
#endif

    tCANMsgObject sCANMessage;
    uint8_t pui8MsgData[8];

    //
    // Set the clocking to run directly from the external crystal/oscillator.
    // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
    // crystal used on your board.
    //
#if defined(TARGET_IS_TM4C129_RA0) ||                                         \
    defined(TARGET_IS_TM4C129_RA1) ||                                         \
    defined(TARGET_IS_TM4C129_RA2)
    ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                       SYSCTL_OSC_MAIN |
                                       SYSCTL_USE_OSC)
                                       25000000);
#else
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);
#endif

    //
    // Set up the serial console to use for displaying messages.  This is
    // just for this example program and is not needed for CAN operation.
    //
    InitConsole();

    //
    // For this example CAN0 is used with RX and TX pins on port B4 and B5.
    // The actual port and pins used may be different on your part, consult
    // the data sheet for more information.
    // GPIO port B needs to be enabled so these pins can be used.
    // TODO: change this to whichever GPIO port you are using
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    //
    // Configure the GPIO pin muxing to select CAN0 functions for these pins.
    // This step selects which alternate function is available for these pins.
    // This is necessary if your part supports GPIO pin function muxing.
    // Consult the data sheet to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using
    //
    GPIOPinConfigure(GPIO_PB4_CAN0RX);
    GPIOPinConfigure(GPIO_PB5_CAN0TX);

    //
    // Enable the alternate function on the GPIO pins.  The above step selects
    // which alternate function is available.  This step actually enables the
    // alternate function instead of GPIO for these pins.
    // TODO: change this to match the port/pin you are using
    //
    GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5);

    //
    // The GPIO port and pins have been set up for CAN.  The CAN peripheral
    // must be enabled.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

    //
    // Initialize the CAN controller
    //
    CANInit(CAN0_BASE);

    //
    // Set up the bit rate for the CAN bus.  This function sets up the CAN
    // bus timing for a nominal configuration.  You can achieve more control
    // over the CAN bus timing by using the function CANBitTimingSet() instead
    // of this one, if needed.
    // In this example, the CAN bus is set to 500 kHz.  In the function below,
    // the call to SysCtlClockGet() or ui32SysClock is used to determine the 
    // clock rate that is used for clocking the CAN peripheral.  This can be 
    // replaced with a  fixed value if you know the value of the system clock, 
    // saving the extra function call.  For some parts, the CAN peripheral is 
    // clocked by a fixed 8 MHz regardless of the system clock in which case 
    // the call to SysCtlClockGet() or ui32SysClock should be replaced with 
    // 8000000.  Consult the data sheet for more information about CAN 
    // peripheral clocking.
    //
#if defined(TARGET_IS_TM4C129_RA0) ||                                         \
    defined(TARGET_IS_TM4C129_RA1) ||                                         \
    defined(TARGET_IS_TM4C129_RA2)
    CANBitRateSet(CAN0_BASE, ui32SysClock, 500000);
#else
    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000);
#endif

    //
    // Enable interrupts on the CAN peripheral.  This example uses static
    // allocation of interrupt handlers which means the name of the handler
    // is in the vector table of startup code.  If you want to use dynamic
    // allocation of the vector table, then you must also call CANIntRegister()
    // here.
    //
    // CANIntRegister(CAN0_BASE, CANIntHandler); // if using dynamic vectors
    //
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);

    //
    // Enable the CAN interrupt on the processor (NVIC).
    //
    IntEnable(INT_CAN0);

    //
    // Enable the CAN for operation.
    //
    CANEnable(CAN0_BASE);

    //
    // Initialize a message object to be used for receiving CAN messages with
    // any CAN ID.  In order to receive any CAN ID, the ID and mask must both
    // be set to 0, and the ID filter enabled.
    //
    sCANMessage.ui32MsgID = 0;
    sCANMessage.ui32MsgIDMask = 0;
    sCANMessage.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER;
    sCANMessage.ui32MsgLen = 8;

    //
    // Now load the message object into the CAN peripheral.  Once loaded the
    // CAN will receive any message on the bus, and an interrupt will occur.
    // Use message object 1 for receiving messages (this is not the same as
    // the CAN ID which can be any value in this example).
    //
    CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_RX);

    //
    // Enter loop to process received messages.  This loop just checks a flag
    // that is set by the interrupt handler, and if set it reads out the
    // message and displays the contents.  This is not a robust method for
    // processing incoming CAN data and can only handle one messages at a time.
    // If many messages are being received close together, then some messages
    // may be dropped.  In a real application, some other method should be used
    // for queuing received messages in a way to ensure they are not lost.  You
    // can also make use of CAN FIFO mode which will allow messages to be
    // buffered before they are processed.
    //
    for(;;)
    {
        unsigned int uIdx;

        //
        // If the flag is set, that means that the RX interrupt occurred and
        // there is a message ready to be read from the CAN
        //
        if(g_bRXFlag)
        {
            //
            // Reuse the same message object that was used earlier to configure
            // the CAN for receiving messages.  A buffer for storing the
            // received data must also be provided, so set the buffer pointer
            // within the message object.
            //
            sCANMessage.pui8MsgData = pui8MsgData;

            //
            // Read the message from the CAN.  Message object number 1 is used
            // (which is not the same thing as CAN ID).  The interrupt clearing
            // flag is not set because this interrupt was already cleared in
            // the interrupt handler.
            //
            CANMessageGet(CAN0_BASE, 1, &sCANMessage, 0);

            //
            // Clear the pending message flag so that the interrupt handler can
            // set it again when the next message arrives.
            //
            g_bRXFlag = 0;

            //
            // Check to see if there is an indication that some messages were
            // lost.
            //
            if(sCANMessage.ui32Flags & MSG_OBJ_DATA_LOST)
            {
                UARTprintf("CAN message loss detected\n");
            }

            //
            // Print out the contents of the message that was received.
            //
            UARTprintf("Msg ID=0x%08X len=%u data=0x",
                       sCANMessage.ui32MsgID, sCANMessage.ui32MsgLen);
            for(uIdx = 0; uIdx < sCANMessage.ui32MsgLen; uIdx++)
            {
                UARTprintf("%02X ", pui8MsgData[uIdx]);
            }
            UARTprintf("total count=%u\n", g_ui32MsgCount);
        }
    }

    //
    // Return no errors
    //
    return(0);
}
//*****************************************************************************
//
// This is the main loop for the application.
//
//*****************************************************************************
int
main(void)
{
    //
    // If running on Rev A2 silicon, turn the LDO voltage up to 2.75V.  This is
    // a workaround to allow the PLL to operate reliably.
    //
    if(REVISION_IS_A2)
    {
        SysCtlLDOSet(SYSCTL_LDO_2_75V);
    }

    //
    // Set the clocking to run directly from the PLL at 25MHz.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_8 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_8MHZ);

    //
    // Enable the pull-ups on the JTAG signals.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    GPIOPadConfigSet(GPIO_PORTC_BASE,
                     GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

    //
    // Configure CAN 0 Pins.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Configure GPIO Pins used for the Buttons.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

    //
    // Configure GPIO Pin used for the LED.
    //
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);

    //
    // Turn off the LED.
    //
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);

    //
    // Enable the CAN controller.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

    //
    // Reset the state of all the message object and the state of the CAN
    // module to a known state.
    //
    CANInit(CAN0_BASE);

    //
    // Configure the bit rate for the CAN device, the clock rate to the CAN
    // controller is fixed at 8MHz for this class of device and the bit rate is
    // set to 250000.
    //
    CANBitRateSet(CAN0_BASE, 8000000, 250000);

    //
    // Take the CAN0 device out of INIT state.
    //
    CANEnable(CAN0_BASE);

    //
    // Enable interrups from CAN controller.
    //
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR);

    //
    // Set up the message object that will receive all messages on the CAN
    // bus.
    //
    CANConfigureNetwork();

    //
    // Enable interrupts for the CAN in the NVIC.
    //
    IntEnable(INT_CAN0);

    //
    // Enable processor interrupts.
    //
    IntMasterEnable();

    //
    // Configure SysTick for a 10ms interrupt.
    //
    SysTickPeriodSet(SysCtlClockGet() / 100);
    SysTickEnable();
    SysTickIntEnable();

    //
    // Initialize the button status.
    //
    g_ucButtonStatus = GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Forground handling of interrupts.
        //
        ProcessInterrupts();

        //
        // Handle any incoming commands.
        //
        ProcessCmd();
    }
}
Ejemplo n.º 19
0
Archivo: BusMon.c Proyecto: GBert/misc
void UserInit(void) {
    byte i;

#if defined(USE_USB_BUS_SENSE_IO)
    tris_usb_bus_sense = INPUT_PIN; // See io_cfg.h
#endif

#if defined(USE_SELF_POWER_SENSE_IO)
    tris_self_power = INPUT_PIN;
#endif


    CS_2515_HIGH(); //Drive high
    tris_CS = 0; //Output
    OpenSPI(SPI_FOSC_16, MODE_00, SMPMID);

    TRISBbits.TRISB0 = 1; //SDI
    TRISBbits.TRISB1 = 0; //SCK

    //-------------------------
    // initialize variables
    //-------------------------
    for (i = 0; i < BUF_SIZE; i++) // initialize input and output buffer to 0
    {
        inbuffer[i] = 0;
        outbuffer[i] = 0;
    }

    //Timer 0
    TMR0H = 0; //clear timer
    TMR0L = 0; //clear timer
    T0CONbits.PSA = 0; //Assign prescaler to Timer 0
    T0CONbits.T0PS2 = 1; //Setup prescaler
    T0CONbits.T0PS1 = 1; //Will time out every 51 us based on
    T0CONbits.T0PS0 = 1; //20 MHz Fosc
    T0CONbits.T0CS = 0; //Increment on instuction cycle

    INTCON2 = 0xFF; //INT2 on rising edge
    INTCON3bits.INT2IP = 0; //Low priority
    INTCON3bits.INT2IF = 0; //Clear flag
    INTCON3bits.INT2IE = 1; //Enable INT2 interrupt

    //Outputs for the LEDs
    ADCON1 = 0x0F; //Digital pins
    CMCON = 0x07; //Digital pins

    LED_25PCT_OFF();
    LED_50PCT_OFF();
    LED_75PCT_OFF();
    LED_100PCT_OFF();

    TRISBbits.TRISB6 = INPUT_PIN;
    TRISBbits.TRISB7 = INPUT_PIN;

    tris_LED_25PCT = OUTPUT_PIN;
    tris_LED_50PCT = OUTPUT_PIN;
    tris_LED_75PCT = OUTPUT_PIN;
    tris_LED_100PCT = OUTPUT_PIN;

    UserFlag.CANLoading = OFF;

    LED_RX_OFF();
    LED_TX_OFF();

    tris_LED_TX = OUTPUT_PIN;
    tris_LED_RX = OUTPUT_PIN;

    tris_SW_LOAD = INPUT_PIN;

    //RTS Pin Outputs
    RTS0_2515_HIGH();
    tris_RTS0_pin = OUTPUT_PIN;

    RTS1_2515_HIGH();
    tris_RTS1_pin = OUTPUT_PIN;

    RTS2_2515_HIGH();
    tris_RTS2_pin = OUTPUT_PIN;

    tris_CAN_RES = OUTPUT_PIN;
    CAN_RES_ON(); //JM
    //  CAN_RES_OFF();  //Disconnect the termination resistor by default

    UserFlag.MCP_RXBn = 0; //clear flag
    UserFlag.USBsend = 0; //clear flag
    UserFlag.USBQueue = 0; //Clear message queue

    //Need to set up MCP2515 before enabling interrupts
    CANInit(); // See BusMon.c & .h

    RCONbits.IPEN = 1;
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;
}//end UserInit
//*****************************************************************************
//
// This is the main loop for the application.
//
//*****************************************************************************
int
main(void)
{
    //
    // If running on Rev A2 silicon, turn the LDO voltage up to 2.75V.  This is
    // a workaround to allow the PLL to operate reliably.
    //
    if(REVISION_IS_A2)
    {
        SysCtlLDOSet(SYSCTL_LDO_2_75V);
    }

    //
    // Set the clocking to run directly from the PLL at 50MHz.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_8MHZ);

    //
    // Configure CAN 0 Pins.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Configure LED pin.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    //
    // Configure GPIO Pin used for the LED.
    //
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);

    //
    // Turn off the LED.
    //
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);

    //
    // Enable the CAN controller.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

    //
    // Reset the state of all the message object and the state of the CAN
    // module to a known state.
    //
    CANInit(CAN0_BASE);

    //
    // Configure the bit rate for the CAN device, the clock rate to the CAN
    // controller is fixed at 8MHz for this class of device and the bit rate is
    // set to CAN_BITRATE.
    //
    CANBitRateSet(CAN0_BASE, 8000000, CAN_BITRATE);

    //
    // Take the CAN0 device out of INIT state.
    //
    CANEnable(CAN0_BASE);

    //
    // Enable interrupts from CAN controller.
    //
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR);

    //
    // Enable interrupts for the CAN in the NVIC.
    //
    IntEnable(INT_CAN0);

    //
    // Enable processor interrupts.
    //
    IntMasterEnable();

    //
    // Set the initial state to wait for data.
    //
    g_sCAN.eState = CAN_WAIT_RX;

    //
    // Reset the buffer pointer.
    //
    g_sCAN.MsgObjectRx.pucMsgData = g_sCAN.pucBuffer;

    //
    // Set the total number of bytes expected.
    //
    g_sCAN.ulBytesRemaining = CAN_FIFO_SIZE;

    //
    // Configure the receive message FIFO.
    //
    CANReceiveFIFO(g_sCAN.pucBuffer, CAN_FIFO_SIZE);

    //
    // Initialized the LED toggle count.
    //
    g_ulLEDCount = 0;

    //
    // Loop forever.
    //
    while(1)
    {
        switch(g_sCAN.eState)
        {
            case CAN_IDLE:
            {
                //
                // Switch to sending state.
                //
                g_sCAN.eState = CAN_SENDING;

                //
                // Initialize the transmit count to zero.
                //
                g_sCAN.ulBytesTransmitted = 0;

                //
                // Schedule all of the CAN transmissions.
                //
                CANTransmitFIFO(g_sCAN.pucBuffer, CAN_FIFO_SIZE);

                break;
            }
            case CAN_SENDING:
            {
                //
                // Wait for all bytes to go out.
                //
                if(g_sCAN.ulBytesTransmitted == CAN_FIFO_SIZE)
                {
                    //
                    // Switch to wait for RX state.
                    //
                    g_sCAN.eState = CAN_WAIT_RX;
                }

                break;
            }
            case CAN_WAIT_RX:
            {
                //
                // Wait for all new data to be received.
                //
                if(g_sCAN.ulBytesRemaining == 0)
                {
                    //
                    // Switch to wait for Process data state.
                    //
                    g_sCAN.eState = CAN_PROCESS;

                    //
                    // Reset the buffer pointer.
                    //
                    g_sCAN.MsgObjectRx.pucMsgData = g_sCAN.pucBuffer;

                    //
                    // Reset the number of bytes expected.
                    //
                    g_sCAN.ulBytesRemaining = CAN_FIFO_SIZE;
                }
                break;
            }
            case CAN_PROCESS:
            {
                //
                // Handle the LED toggle.
                //
                ToggleLED();

                //
                // Return to the idle state.
                //
                g_sCAN.eState = CAN_IDLE;

                break;
            }
            default:
            {
                break;
            }
        }
    }
}
Ejemplo n.º 21
0
//*****************************************************************************
//
// Configure the CAN and enter a loop to transmit periodic CAN messages.
//
//*****************************************************************************
int
main(void)
{
    tCANMsgObject sCANMessage;
    uint32_t ui32MsgData;
    uint8_t *pui8MsgData;

    pui8MsgData = (uint8_t *)&ui32MsgData;

    //
    // Set the clocking to run directly from the external crystal/oscillator.
    // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
    // crystal on your board.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);

    //
    // Set up the serial console to use for displaying messages.  This is
    // just for this example program and is not needed for CAN operation.
    //
    InitConsole();

    //
    // For this example CAN0 is used with RX and TX pins on port B4 and B5.
    // The actual port and pins used may be different on your part, consult
    // the data sheet for more information.
    // GPIO port B needs to be enabled so these pins can be used.
    // TODO: change this to whichever GPIO port you are using
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    //
    // Configure the GPIO pin muxing to select CAN0 functions for these pins.
    // This step selects which alternate function is available for these pins.
    // This is necessary if your part supports GPIO pin function muxing.
    // Consult the data sheet to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using
    //
    GPIOPinConfigure(GPIO_PB4_CAN0RX);
    GPIOPinConfigure(GPIO_PB5_CAN0TX);

    //
    // Enable the alternate function on the GPIO pins.  The above step selects
    // which alternate function is available.  This step actually enables the
    // alternate function instead of GPIO for these pins.
    // TODO: change this to match the port/pin you are using
    //
    GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5);

    //
    // The GPIO port and pins have been set up for CAN.  The CAN peripheral
    // must be enabled.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

    //
    // Initialize the CAN controller
    //
    CANInit(CAN0_BASE);

    //
    // Set up the bit rate for the CAN bus.  This function sets up the CAN
    // bus timing for a nominal configuration.  You can achieve more control
    // over the CAN bus timing by using the function CANBitTimingSet() instead
    // of this one, if needed.
    // In this example, the CAN bus is set to 500 kHz.  In the function below,
    // the call to SysCtlClockGet() is used to determine the clock rate that
    // is used for clocking the CAN peripheral.  This can be replaced with a
    // fixed value if you know the value of the system clock, saving the extra
    // function call.  For some parts, the CAN peripheral is clocked by a fixed
    // 8 MHz regardless of the system clock in which case the call to
    // SysCtlClockGet() should be replaced with 8000000.  Consult the data
    // sheet for more information about CAN peripheral clocking.
    //
    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000);

    //
    // Enable interrupts on the CAN peripheral.  This example uses static
    // allocation of interrupt handlers which means the name of the handler
    // is in the vector table of startup code.  If you want to use dynamic
    // allocation of the vector table, then you must also call CANIntRegister()
    // here.
    //
    // CANIntRegister(CAN0_BASE, CAN0_IRQHandler); // if using dynamic vectors
    //
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);

    //
    // Enable the CAN interrupt on the processor (NVIC).
    //
    IntEnable(INT_CAN0);

    //
    // Enable the CAN for operation.
    //
    CANEnable(CAN0_BASE);

    //
    // Initialize the message object that will be used for sending CAN
    // messages.  The message will be 4 bytes that will contain an incrementing
    // value.  Initially it will be set to 0.
    //
    ui32MsgData = 0;
    sCANMessage.ui32MsgID = 1;
    sCANMessage.ui32MsgIDMask = 0;
    sCANMessage.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
    sCANMessage.ui32MsgLen = sizeof(pui8MsgData);
    sCANMessage.pui8MsgData = pui8MsgData;

    //
    // Enter loop to send messages.  A new message will be sent once per
    // second.  The 4 bytes of message content will be treated as an uint32_t
    // and incremented by one each time.
    //
    while(1)
    {
        //
        // Print a message to the console showing the message count and the
        // contents of the message being sent.
        //
        UARTprintf("Sending msg: 0x%02X %02X %02X %02X",
                   pui8MsgData[0], pui8MsgData[1], pui8MsgData[2],
                   pui8MsgData[3]);

        //
        // Send the CAN message using object number 1 (not the same thing as
        // CAN ID, which is also 1 in this example).  This function will cause
        // the message to be transmitted right away.
        //
        CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_TX);

        //
        // Now wait 1 second before continuing
        //
        SimpleDelay();

        //
        // Check the error flag to see if errors occurred
        //
        if(g_bErrFlag)
        {
            UARTprintf(" error - cable connected?\n");
        }
        else
        {
            //
            // If no errors then print the count of message sent
            //
            UARTprintf(" total count = %u\n", g_ui32MsgCount);
        }

        //
        // Increment the value in the message data.
        //
        ui32MsgData++;
    }

    //
    // Return no errors
    //
    return(0);
}
//*****************************************************************************
//
// Configure the CAN and enter a loop to transmit periodic CAN messages.
//
//*****************************************************************************
int
main(void)
{
    //
    // Set the clocking to run directly from the external crystal/oscillator.
    // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
    // crystal on your board.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);

    //
    // Set up the serial console to use for displaying messages.  This is
    // just for this example program and is not needed for CAN operation.
    //
    InitConsole();

    //
    // For this example CAN0 is used with RX and TX pins on port B4 and B5.
    // The actual port and pins used may be different on your part, consult
    // the data sheet for more information.
    // GPIO port B needs to be enabled so these pins can be used.
    // TODO: change this to whichever GPIO port you are using
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    //
    // Configure the GPIO pin muxing to select CAN0 functions for these pins.
    // This step selects which alternate function is available for these pins.
    // This is necessary if your part supports GPIO pin function muxing.
    // Consult the data sheet to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using
    //
    GPIOPinConfigure(GPIO_PB4_CAN0RX);
    GPIOPinConfigure(GPIO_PB5_CAN0TX);

    //
    // Enable the alternate function on the GPIO pins.  The above step selects
    // which alternate function is available.  This step actually enables the
    // alternate function instead of GPIO for these pins.
    // TODO: change this to match the port/pin you are using
    //
    GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5);

    //
    // The GPIO port and pins have been set up for CAN.  The CAN peripheral
    // must be enabled.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

    //
    // Initialize the CAN controller
    //
    CANInit(CAN0_BASE);

    //
    // Set up the bit rate for the CAN bus.  This function sets up the CAN
    // bus timing for a nominal configuration.  You can achieve more control
    // over the CAN bus timing by using the function CANBitTimingSet() instead
    // of this one, if needed.
    // In this example, the CAN bus is set to 500 kHz.  In the function below,
    // the call to SysCtlClockGet() is used to determine the clock rate that
    // is used for clocking the CAN peripheral.  This can be replaced with a
    // fixed value if you know the value of the system clock, saving the extra
    // function call.  For some parts, the CAN peripheral is clocked by a fixed
    // 8 MHz regardless of the system clock in which case the call to
    // SysCtlClockGet() should be replaced with 8000000.  Consult the data
    // sheet for more information about CAN peripheral clocking.
    //
    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000);

    //
    // Enable interrupts on the CAN peripheral.  This example uses static
    // allocation of interrupt handlers which means the name of the handler
    // is in the vector table of startup code.  If you want to use dynamic
    // allocation of the vector table, then you must also call CANIntRegister()
    // here.
    //
    // CANIntRegister(CAN0_BASE, CANIntHandler); // if using dynamic vectors
    //
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);

    //
    // Enable the CAN interrupt on the processor (NVIC).
    //
    IntEnable(INT_CAN0);

    //
    // Enable the CAN for operation.
    //
    CANEnable(CAN0_BASE);

    //
    // Initialize the message object that will be used for sending CAN
    // messages.  The message will be 4 bytes that will contain an incrementing
    // value.  Initially it will be set to 0.
    //

    //
    // Initialize message object 1 to be able to send CAN message 1.  This
    // message object is not shared so it only needs to be initialized one
    // time, and can be used for repeatedly sending the same message ID.
    //
    g_sCANMsgObject1.ui32MsgID = 0x1001;
    g_sCANMsgObject1.ui32MsgIDMask = 0;
    g_sCANMsgObject1.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
    g_sCANMsgObject1.ui32MsgLen = sizeof(g_pui8Msg1);
    g_sCANMsgObject1.pui8MsgData = g_pui8Msg1;

    //
    // Initialize message object 2 to be able to send CAN message 2.  This
    // message object is not shared so it only needs to be initialized one
    // time, and can be used for repeatedly sending the same message ID.
    //
    g_sCANMsgObject2.ui32MsgID = 0x2001;
    g_sCANMsgObject2.ui32MsgIDMask = 0;
    g_sCANMsgObject2.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
    g_sCANMsgObject2.ui32MsgLen = sizeof(g_pui8Msg2);
    g_sCANMsgObject2.pui8MsgData = g_pui8Msg2;

    //
    // Enter loop to send messages.  Four messages will be sent once per
    // second.  The contents of each message will be changed each time.
    //
    for(;;)
    {
        //
        // Send message 1 using CAN controller message object 1.  This is
        // the only message sent using this message object.  The
        // CANMessageSet() function will cause the message to be sent right
        // away.
        //
        PrintCANMessageInfo(&g_sCANMsgObject1, 1);
        CANMessageSet(CAN0_BASE, 1, &g_sCANMsgObject1, MSG_OBJ_TYPE_TX);

        //
        // Send message 2 using CAN controller message object 2.  This is
        // the only message sent using this message object.  The
        // CANMessageSet() function will cause the message to be sent right
        // away.
        //
        PrintCANMessageInfo(&g_sCANMsgObject2, 2);
        CANMessageSet(CAN0_BASE, 2, &g_sCANMsgObject2, MSG_OBJ_TYPE_TX);

        //
        // Load message object 3 with message 3.  This is needs to be done each
        // time because message object 3 is being shared for two different
        // messages.
        //
        g_sCANMsgObject3.ui32MsgID = 0x3001;
        g_sCANMsgObject3.ui32MsgIDMask = 0;
        g_sCANMsgObject3.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
        g_sCANMsgObject3.ui32MsgLen = sizeof(g_pui8Msg3);
        g_sCANMsgObject3.pui8MsgData = g_pui8Msg3;

        //
        // Clear the flag that indicates that message 3 has been sent.  This
        // flag will be set in the interrupt handler when a message has been
        // sent using message object 3.
        //
        g_bMsgObj3Sent = 0;

        //
        // Now send message 3 using CAN controller message object 3.  This is
        // the first message sent using this message object.  The
        // CANMessageSet() function will cause the message to be sent right
        // away.
        //
        PrintCANMessageInfo(&g_sCANMsgObject3, 3);
        CANMessageSet(CAN0_BASE, 3, &g_sCANMsgObject3, MSG_OBJ_TYPE_TX);

        //
        // Wait for the indication from the interrupt handler that message
        // object 3 is done, because we are re-using it for another message.
        //
        while(!g_bMsgObj3Sent)
        {
        }

        //
        // Load message object 3 with message 4.  This is needed because
        // message object 3 is being shared for two different messages.
        //
        g_sCANMsgObject3.ui32MsgID = 0x3002;
        g_sCANMsgObject3.ui32MsgIDMask = 0;
        g_sCANMsgObject3.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
        g_sCANMsgObject3.ui32MsgLen = sizeof(g_pui8Msg4);
        g_sCANMsgObject3.pui8MsgData = g_pui8Msg4;

        //
        // Now send message 4 using CAN controller message object 3.  This is
        // the second message sent using this message object.  The
        // CANMessageSet() function will cause the message to be sent right
        // away.
        //
        PrintCANMessageInfo(&g_sCANMsgObject3, 3);
        CANMessageSet(CAN0_BASE, 3, &g_sCANMsgObject3, MSG_OBJ_TYPE_TX);

        //
        // Wait 1 second before continuing
        //
        SimpleDelay();

        //
        // Check the error flag to see if errors occurred
        //
        if(g_bErrFlag)
        {
            UARTprintf(" error - cable connected?\n");
        }
        else
        {
            //
            // If no errors then print the count of message sent
            //
            UARTprintf(" total count = %u\n",
                       g_ui32Msg1Count + g_ui32Msg2Count + g_ui32Msg3Count);
        }

        //
        // Change the value in the message data for each of the messages.
        //
        (*(uint32_t *)g_pui8Msg1)++;
        (*(uint32_t *)g_pui8Msg2)++;
        (*(uint32_t *)g_pui8Msg3)++;
        (*(uint32_t *)&g_pui8Msg4[0])++;
        (*(uint32_t *)&g_pui8Msg4[4])--;
    }

    //
    // Return no errors
    //
    return(0);
}
Ejemplo n.º 23
0
int main(void) {
  char txtBuffer[32];

  SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                 SYSCTL_XTAL_16MHZ);

  SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

  GPIOPinConfigure(GPIO_PE4_CAN0RX);
  GPIOPinConfigure(GPIO_PE5_CAN0TX);

  GPIOPinTypeCAN(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5);

  CANInit(CAN0_BASE);

  CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000);

  CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);

  IntEnable(INT_CAN0);

  CANEnable(CAN0_BASE);

  //
  // Initialize a message object to be used for receiving CAN messages with
  // any CAN ID.  In order to receive any CAN ID, the ID and mask must both
  // be set to 0, and the ID filter enabled.
  //
  sCANMessage.ui32MsgIDMask = 0x7ff; // mask is 0 for any ID
  sCANMessage.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER;
  sCANMessage.ui32MsgLen = 16; // allow up to 8 bytes

  //
  // Now load the message object into the CAN peripheral.  Once loaded the
  // CAN will receive any message on the bus, and an interrupt will occur.
  // Use message object 1 for receiving messages (this is not the same as
  // the CAN ID which can be any value in this example).
  //
  sCANMessage.ui32MsgID = 0x0b0; // wheels A
  sCANMessage.pui8MsgData = (uint8_t *)&wheel_a_data;
  CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_RX);

  sCANMessage.ui32MsgID = 0x0b2; // wheels B
  sCANMessage.pui8MsgData = (uint8_t *)&wheel_b_data;
  CANMessageSet(CAN0_BASE, 2, &sCANMessage, MSG_OBJ_TYPE_RX);

  sCANMessage.ui32MsgID = 0x2c4; // engine
  sCANMessage.pui8MsgData = (uint8_t *)&engine_data;
  CANMessageSet(CAN0_BASE, 3, &sCANMessage, MSG_OBJ_TYPE_RX);

  sCANMessage.ui32MsgID = 0x398; // fuel
  sCANMessage.pui8MsgData = (uint8_t *)&fuel_data;
  CANMessageSet(CAN0_BASE, 4, &sCANMessage, MSG_OBJ_TYPE_RX);

  lcd_port_setup();
  lcd_init();

  for (;;) {
    unsigned int li, lf, ri, rf, fi, ff; // integral and fractional parts for left, right wheels' speed, and fuel consumption
    unsigned long int v = 0; // Average speed
    float vf, ef; // Float versions of speed and economy
    li = ntohs(wheel_a_data.wheel2);
    v += li;
    lf = li%100;
    li /= 100;
    ri = ntohs(wheel_a_data.wheel1);
    v += ri;
    rf = ri%100;
    ri /= 100;
    snprintf(txtBuffer, 21, "L %3d.%02d -- R %3d.%02d", li, lf, ri, rf);
    lcd_goto(0,0);
    lcd_puts(txtBuffer);
    
    li = ntohs(wheel_b_data.wheel2);
    v += li;
    lf = li%100;
    li /= 100;
    ri = ntohs(wheel_b_data.wheel1);
    v += ri;
    rf = ri%100;
    ri /= 100;
    snprintf(txtBuffer, 21, "L %3d.%02d -- R %3d.%02d", li, lf, ri, rf);
    lcd_goto(1,0);
    lcd_puts(txtBuffer);
    
    vf = v / 400.0;
    snprintf(txtBuffer, 21, "%4d RPM %3d.%02d KM/H", ntohs(engine_data.rpm), (int)vf, ((int)(vf*100))%100);
    lcd_goto(2,0);
    lcd_puts(txtBuffer);
    
    fi = ntohs(fuel_data.fuel);
    ff = fi % 100;
    fi /= 100;
    if (vf >= .01) {
      ef = vf/(fi/100.0);
      snprintf(txtBuffer, 21, "%2d.%02d L/H %2d.%02d KM/L", fi, ff, (int)ef, ((int)(ef*100))%100);
    } else {
      snprintf(txtBuffer, 21, "%2d.%02d L/H --.-- KM/L", fi, ff);
    }
    lcd_goto(3,0);
    lcd_puts(txtBuffer);
  }

  return (0);
}
int main(void)
{
    //
    // If running on Rev A2 silicon, turn the LDO voltage up to 2.75V.  This is
    // a workaround to allow the PLL to operate reliably.
    //
    if(REVISION_IS_A2)
    {
        SysCtlLDOSet(SYSCTL_LDO_2_75V);
    }

    
    // Set the clocking to run directly from the PLL at 25MHz.
    SysCtlClockSet(SYSCTL_SYSDIV_8 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ);

    
    // Configure CAN 0 Pins.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    // Enable the CAN controller.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

    //
    // Reset the state of all the message object and the state of the CAN
    // module to a known state.
    //
    CANInit(CAN0_BASE);

    //
    // Configure the bit rate for the CAN device, the clock rate to the CAN
    // controller is fixed at 8MHz for this class of device and the bit rate is
    // set to 250000.
    //
    CANBitRateSet(CAN0_BASE, 8000000, 1000000);

    //
    // Take the CAN0 device out of INIT state.
    //
    CANEnable(CAN0_BASE);

    //
    // Enable interrups from CAN controller.
    //
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR);


    // Enable interrupts for the CAN in the NVIC.
    IntEnable(INT_CAN0);

    //
    // Enable processor interrupts.
    //
    IntMasterEnable();

    //
    // Configure SysTick for a 10ms interrupt.
    //
    //    1 = 1s - 
    //   10 = 100ms
    //  100 = 10ms
    //  200 = 5ms
    //  500 = 2ms
    // 1000 = 1ms
    //10000 = 100us
    //20000 = 50us  - this is about the time a max CAN packet (50bytes) needs to be sent at max bit rate of 1Mbps
    SysTickPeriodSet(SysCtlClockGet() / 10);
    SysTickEnable();
    SysTickIntEnable();


    //
    // Loop forever.
    //
    while(1)
    {
       
    }
}
Ejemplo n.º 25
0
//*****************************************************************************
//
// Configure the CAN and enter a loop to receive CAN messages.
//
//*****************************************************************************
int
main(void)
{
    tCANMsgObject sCANMessage;
    uint8_t pui8MsgData[8];

    //
    // Set the clocking to run directly from the external crystal/oscillator.
    // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
    // crystal used on your board.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);

    //
    // Set up the serial console to use for displaying messages.  This is
    // just for this example program and is not needed for CAN operation.
    //
    InitConsole();

    //
    // For this example CAN0 is used with RX and TX pins on port B4 and B5.
    // The actual port and pins used may be different on your part, consult
    // the data sheet for more information.
    // GPIO port B needs to be enabled so these pins can be used.
    // TODO: change this to whichever GPIO port you are using
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    //
    // Configure the GPIO pin muxing to select CAN0 functions for these pins.
    // This step selects which alternate function is available for these pins.
    // This is necessary if your part supports GPIO pin function muxing.
    // Consult the data sheet to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using
    //
    GPIOPinConfigure(GPIO_PB4_CAN0RX);
    GPIOPinConfigure(GPIO_PB5_CAN0TX);

    //
    // Enable the alternate function on the GPIO pins.  The above step selects
    // which alternate function is available.  This step actually enables the
    // alternate function instead of GPIO for these pins.
    // TODO: change this to match the port/pin you are using
    //
    GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5);

    //
    // The GPIO port and pins have been set up for CAN.  The CAN peripheral
    // must be enabled.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

    //
    // Initialize the CAN controller
    //
    CANInit(CAN0_BASE);

    //
    // Set up the bit rate for the CAN bus.  This function sets up the CAN
    // bus timing for a nominal configuration.  You can achieve more control
    // over the CAN bus timing by using the function CANBitTimingSet() instead
    // of this one, if needed.
    // In this example, the CAN bus is set to 500 kHz.  In the function below,
    // the call to SysCtlClockGet() is used to determine the clock rate that
    // is used for clocking the CAN peripheral.  This can be replaced with a
    // fixed value if you know the value of the system clock, saving the extra
    // function call.  For some parts, the CAN peripheral is clocked by a fixed
    // 8 MHz regardless of the system clock in which case the call to
    // SysCtlClockGet() should be replaced with 8000000.  Consult the data
    // sheet for more information about CAN peripheral clocking.
    //
    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000);

    //
    // Enable interrupts on the CAN peripheral.  This example uses static
    // allocation of interrupt handlers which means the name of the handler
    // is in the vector table of startup code.  If you want to use dynamic
    // allocation of the vector table, then you must also call CANIntRegister()
    // here.
    //
    // CANIntRegister(CAN0_BASE, CAN0_IRQHandler); // if using dynamic vectors
    //
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);

    //
    // Enable the CAN interrupt on the processor (NVIC).
    //
    IntEnable(INT_CAN0);

    //
    // Enable the CAN for operation.
    //
    CANEnable(CAN0_BASE);

    //
    // Initialize a message object to receive CAN messages with ID 0x1001.
    // The expected ID must be set along with the mask to indicate that all
    // bits in the ID must match.
    //
    sCANMessage.ui32MsgID = 0x1001;
    sCANMessage.ui32MsgIDMask = 0xfffff;
    sCANMessage.ui32Flags = (MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER |
                             MSG_OBJ_EXTENDED_ID);
    sCANMessage.ui32MsgLen = 8;

    //
    // Now load the message object into the CAN peripheral message object 1.
    // Once loaded the CAN will receive any messages with this CAN ID into
    // this message object, and an interrupt will occur.
    //
    CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_RX);

    //
    // Change the ID to 0x2001, and load into message object 2 which will be
    // used for receiving any CAN messages with this ID.  Since only the CAN
    // ID field changes, we don't need to reload all the other fields.
    //
    sCANMessage.ui32MsgID = 0x2001;
    CANMessageSet(CAN0_BASE, 2, &sCANMessage, MSG_OBJ_TYPE_RX);

    //
    // Change the ID to 0x3001, and load into message object 3 which will be
    // used for receiving any CAN messages with this ID.  Since only the CAN
    // ID field changes, we don't need to reload all the other fields.
    //
    sCANMessage.ui32MsgID = 0x3001;
    CANMessageSet(CAN0_BASE, 3, &sCANMessage, MSG_OBJ_TYPE_RX);

    //
    // Enter loop to process received messages.  This loop just checks flags
    // for each of the 3 expected messages.  The flags are set by the interrupt
    // handler, and if set this loop reads out the message and displays the
    // contents to the console.  This is not a robust method for processing
    // incoming CAN data and can only handle one messages at a time per message
    // object.  If many messages are being received close together using the
    // same message object, then some messages may be dropped.  In a real
    // application, some other method should be used for queuing received
    // messages in a way to ensure they are not lost.  You can also make use
    // of CAN FIFO mode which will allow messages to be buffered before they
    // are processed.
    //
    for(;;)
    {
        //
        // If the flag for message object 1 is set, that means that the RX
        // interrupt occurred and there is a message ready to be read from
        // this CAN message object.
        //
        if(g_bRXFlag1)
        {
            //
            // Reuse the same message object that was used earlier to configure
            // the CAN for receiving messages.  A buffer for storing the
            // received data must also be provided, so set the buffer pointer
            // within the message object.  This same buffer is used for all
            // messages in this example, but your application could set a
            // different buffer each time a message is read in order to store
            // different messages in different buffers.
            //
            sCANMessage.pui8MsgData = pui8MsgData;

            //
            // Read the message from the CAN.  Message object number 1 is used
            // (which is not the same thing as CAN ID).  The interrupt clearing
            // flag is not set because this interrupt was already cleared in
            // the interrupt handler.
            //
            CANMessageGet(CAN0_BASE, 1, &sCANMessage, 0);

            //
            // Clear the pending message flag so that the interrupt handler can
            // set it again when the next message arrives.
            //
            g_bRXFlag1 = 0;

            //
            // Print information about the message just received.
            //
            PrintCANMessageInfo(&sCANMessage, 1);
        }

        //
        // Check for message received on message object 2.  If so then
        // read message and print information.
        //
        if(g_bRXFlag2)
        {
            sCANMessage.pui8MsgData = pui8MsgData;
            CANMessageGet(CAN0_BASE, 2, &sCANMessage, 0);
            g_bRXFlag2 = 0;
            PrintCANMessageInfo(&sCANMessage, 2);
        }

        //
        // Check for message received on message object 3.  If so then
        // read message and print information.
        //
        if(g_bRXFlag3)
        {
            sCANMessage.pui8MsgData = pui8MsgData;
            CANMessageGet(CAN0_BASE, 3, &sCANMessage, 0);
            g_bRXFlag3 = 0;
            PrintCANMessageInfo(&sCANMessage, 3);
        }
    }

    //
    // Return no errors
    //
    return(0);
}
Ejemplo n.º 26
0
Archivo: BusMon.c Proyecto: GBert/misc
/******************************************************************************
 * Function:        void ProcessIO
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user routines.
 *                  It is a mixture of both USB and non-USB tasks.
 *
 * Note:            None
 *****************************************************************************/
void ProcessIO(void) {
    unsigned char n, b, c, y;
    int a;
    // User Application USB tasks

    if (!(isUsbPowered)) //Only generate traffic if NOT connected to USB
    {
        CheckLoadButton();
        CANLoadTX();
        return;
    }

    if ((usb_device_state < CONFIGURED_STATE) || (UCONbits.SUSPND == 1)) return;

    //----- Read USB buffer if it has data from the host -----
    if (HIDRxReport(inbuffer, 64)) // USB receive buffer has data
    {
        LED_RX_ON(); //Turn LED on
        T0CONbits.TMR0ON = 1; //Start timer for TX LED on time
        gTimeout = 0; //Reset timout

        //---- CANmsgs: Check if host has requested CAN messages to be transmited
        switch (inbuffer[u_CANmsgs]) // interpret command
        {
            case 0x00: // No messages are available
                break;

            case 0x01: // Message 1 is available
                GetUSBData(m1_SIDH, 13, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, 13, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }
                break;

            case 0x02: // Message 1 and 2 are available
                //Message 1
                GetUSBData(m1_SIDH, m1_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m1_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }

                //Message 2
                GetUSBData(m2_SIDH, m2_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m2_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }
                break;

            case 0x03: // Message 1, 2, and 3 are available
                //Message 1
                GetUSBData(m1_SIDH, m1_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m1_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }

                //Message 2
                GetUSBData(m2_SIDH, m2_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m2_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }

                //Message 3
                GetUSBData(m3_SIDH, m3_DLC + 5, DataArray);
                n = SPILoadTX(CAN_LOAD_TX, m3_DLC + 5, DataArray);

                if (n == CAN_LD_TXB0_ID) //if TX0 is loaded
                {
                    RTS0_2515_LOW();
                    RTS0_2515_HIGH();
                } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded
                {
                    RTS1_2515_LOW();
                    RTS1_2515_HIGH();
                } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded
                {
                    RTS2_2515_LOW();
                    RTS2_2515_HIGH();
                }
                break;

            case 0x04: //--FUTURE-- Message 1, 2, 3, and 4 are available
                break;

            default: // unrecognized or null command
                ;
        }// END switch: inbuffer[u_CANmsgs]

        //---- CANCTRL: Write to the CANCTRL register if changed
        if (inbuffer[u_CANCTRL] != old_CANCTRL) //If host sent new CANCTRL value
        {
            SPIByteWrite(CANCTRL, inbuffer[u_CANCTRL]); //Write to CANCTRL
            EEPROMBYTEWrite(CANCTRL, inbuffer[u_CANCTRL]);
            EEPROMCRCWrite(0, 128);
            old_CANCTRL = inbuffer[u_CANCTRL]; //
            outbuffer[u_CANSTAT] = SPIByteRead(CANSTAT);
            while ((outbuffer[u_CANSTAT] & 0xE0) != (inbuffer[u_CANCTRL] & 0xE0))//if didn't change modes yet
            {
                outbuffer[u_CANSTAT] = SPIByteRead(CANSTAT);
            }
            UserFlag.USBsend = 1; //Set flag so will send USB message
        }

        //---- SPI: SPI command from host
        if (inbuffer[u_SPI]) //If host sent SPI command (non-zero)
        {
            switch (inbuffer[u_SPI]) {
                case CAN_RESET: //
                    SPIReset();
                    CANInit();

                    break;

                case CAN_READ: //
                    if (!UserFlag.USBQueue) // If previous message is queued
                    {
                        outbuffer[u_SPI] = CAN_READ; //Send back to host
                        outbuffer[u_REG] = inbuffer[u_REG]; //Send back to host
                        outbuffer[u_DATA] = SPIByteRead(inbuffer[u_REG]); //Send back to host
                    }
                    UserFlag.USBsend = 1; //Set flag so will send USB message
                    UserFlag.USBQueue = 1; //Indicates msg is queued, but not sent
                    break;

                case CAN_WRITE: //
                    //outbuffer[u_SPI] = 0;        //Send back to host //JM
                    SPIByteWrite(inbuffer[u_REG], inbuffer[u_DATA]);
                    EEPROMBYTEWrite(inbuffer[u_REG], inbuffer[u_DATA]);
                    EEPROMCRCWrite(0, 128);
                    break;

                case CAN_RTS: //
                    SPI_RTS(inbuffer[u_DATA]);
                    break;

                case CAN_RD_STATUS: //
                    outbuffer[u_DATA] = SPIReadStatus();
                    UserFlag.USBsend = 1; //Set flag so will send USB message
                    break;
                case FIRMWARE_VER_RD:
                    memmove(&outbuffer[u_STATUS], firmware_version, sizeof (firmware_version));
                    outbuffer[u_STATUS + sizeof (firmware_version)] = 0;
                    UserFlag.USBsend = 1; //Set flag so will send USB message
                    break;

                default: // unrecognized or null command
                    ;
            }// END switch: inbuffer[u_SPI]
        }
    }//END if (HIDRxReport(inbuffer, 1)

    //---- Check RXnBF pins and service messages as needed ---
    switch (CheckCANRX()) // Check if CAN message received
    {
        case 0x01: // Message in RXB0 (Msgs in this buffer are Standard)
            SPIReadRX(CAN_RD_START_RXB0SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            break;

        case 0x02: // Message in RXB1 (Msgs in this buffer are Extended)
            SPIReadRX(CAN_RD_START_RXB1SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            break;

        case 0x03: // Message in RXB0 and RXB1
            SPIReadRX(CAN_RD_START_RXB0SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            SPIReadRX(CAN_RD_START_RXB1SIDH, 13, ReadArray);
            LoadUSBString(ReadArray);
            break;

        default: // unrecognized or null command
            ;
    }// END switch: CheckCANRX()

    //---- The following turns off the TX and RX USB indicator LEDs after some time
    //Inst. cycle = 200 ns; TMR0IF sets every 51 us
    if (INTCONbits.TMR0IF) {
        TimerCounter++;
        if (!TimerCounter) //if rolled over, set flag. User code will handle the rest.
        {
            LED_TX_OFF(); //Turn LED off
            LED_RX_OFF(); //Turn LED off
            T0CONbits.TMR0ON = 0; //Start timer for TX LED on time
            TimerCounter = 0xFE;
            gTimeout = 1; //Reset timout
        }
        INTCONbits.TMR0IF = 0;
    }

    //------ Load USB Data to be transmitted to the host --------
    if (UserFlag.MCP_RXBn | UserFlag.USBsend) {
        if (!mHIDTxIsBusy()) {
            HIDTxReport(outbuffer, 64);

            outbuffer[0] = 0x00; //PKR$$$ Need this??

            UserFlag.MCP_RXBn = 0; //clear flag
            UserFlag.USBsend = 0; //clear flag
            UserFlag.USBQueue = 0; //Clear message queue

            LED_TX_ON(); //Turn LED on
            T0CONbits.TMR0ON = 1; //Start timer for TX LED on time
            gTimeout = 0; //Reset timout

            outbuffer[u_SPI] = 0x00; //clear back to 00h so host doesn't detect "SPI response"
            USB_ptr = 0xFF; //Point to location 0xFF
            outbuffer[u_CANmsgs] = 0x00; //Clear message status
        }
    }
}//end ProcessIO