Example #1
0
U8 can_bus_init_hardware(U8 ch,
            U32 can_msg_ram_add,
            U8 operating_mode,
            void (*can_msg_callback_channel) (U8 handle, U8 event))
{
   if ( ch > 1)
         return  CAN_CMD_REFUSED;

   // Initialize CAN channel
   CANIF_set_reset(ch);
   while(CANIF_channel_enable_status(ch));
   CANIF_clr_reset(ch);

   CANIF_set_ram_add(ch,(unsigned long) can_msg_ram_add);
   if ((CANIF_bit_timing(ch))==0) return (0);
   switch(operating_mode)
   {
    case CANIF_CHANNEL_MODE_NORMAL:
      CANIF_set_channel_mode(ch,0);
      CANIF_clr_overrun_mode(ch);
      break;
    case CANIF_CHANNEL_MODE_LISTENING:
      CANIF_set_channel_mode(ch,1);
      CANIF_set_overrun_mode(ch);
      break;
    case CANIF_CHANNEL_MODE_LOOPBACK:
      CANIF_set_channel_mode(ch,2);
      CANIF_clr_overrun_mode(ch);
      break;
   }
   canif_clear_all_mob(ch,NB_MOB_CHANNEL);
   CANIF_enable(ch);

/* 
 * The maximum delay time to wait is the time to transmit 128-bits 
 * (CAN extended frame at baudrate speed configured by the user).
 * - 10x bits number of previous undetected message,
 * - 128x bits MAX length,
 * - 3x bits of interframe.
 */
#define DELAY_HZ         (BAUDRATE_HZ/141.0)   /*Compute Maximum delay time*/
#define DELAY            (1000000 / DELAY_HZ)  /*Compute Delay in µs*/
   delay_us(DELAY);
   if(!CANIF_channel_enable_status(ch)) {
            return CAN_CMD_REFUSED;
   }

	can_bus_enable_interrupt(ch);

   return CAN_CMD_ACCEPTED;
}
Example #2
0
/** Main function to configure the CAN, and begin transfers. */
int main(void)
{
	/* Initialize the system clocks */
	sysclk_init();

	/* Setup the generic clock for CAN */
	scif_gc_setup(AVR32_SCIF_GCLK_CANIF,
				SCIF_GCCTRL_OSC0,
				AVR32_SCIF_GC_NO_DIV_CLOCK,
				0);
	/* Now enable the generic clock */
	scif_gc_enable(AVR32_SCIF_GCLK_CANIF);

	init_dbg_rs232(FPBA_HZ);

	/* Disable all interrupts. */
	Disable_global_interrupt();

	/* Initialize interrupt vectors. */
	INTC_init_interrupts();

	static const gpio_map_t CAN_GPIO_MAP = {
		{AVR32_CANIF_RXLINE_0_0_PIN, AVR32_CANIF_RXLINE_0_0_FUNCTION},
		{AVR32_CANIF_TXLINE_0_0_PIN, AVR32_CANIF_TXLINE_0_0_FUNCTION}
	};
	/* Assign GPIO to CAN. */
	gpio_enable_module(CAN_GPIO_MAP,
		sizeof(CAN_GPIO_MAP) / sizeof(CAN_GPIO_MAP[0]));

	/* Initialize channel 0 */
	can_init(CAN_CHANNEL_EXAMPLE, ((uint32_t)&mob_ram_ch0[0]), 
		CANIF_CHANNEL_MODE_LISTENING, can_out_callback_channel0);

	/* Enable all interrupts. */
	Enable_global_interrupt();

	print_dbg("\r\nUC3C CAN Examples 1\r\n");

	print_dbg(CAN_Wakeup);

	/* Initialize CAN Channel 0 */
	can_init(CAN_CHANNEL_EXAMPLE, ((uint32_t)&mob_ram_ch0[0]), 
		CANIF_CHANNEL_MODE_NORMAL, can_out_callback_channel0);

	/* Allocate one mob for RX */
	appli_rx_msg.handle = can_mob_alloc(CAN_CHANNEL_EXAMPLE);

	/* Initialize RX message */
	can_rx(CAN_CHANNEL_EXAMPLE, appli_rx_msg.handle, appli_rx_msg.req_type,
		appli_rx_msg.can_msg);

	/* Enable Async Wake Up Mode */
	pm_asyn_wake_up_enable(CAN_WAKEUP_MASK_EXAMPLE);

	/* ---------SLEEP MODE PROCEDURE------------- */
	/* Disable CAN Channel 0 */
	CANIF_disable(CAN_CHANNEL_EXAMPLE);
	/* Wait CAN Channel 0 is disabled */
	while(!CANIF_channel_enable_status(CAN_CHANNEL_EXAMPLE));
	/* Enable Wake-Up Mode */
	CANIF_enable_wakeup(CAN_CHANNEL_EXAMPLE);
	/* Go to sleep mode. */
	SLEEP(AVR32_PM_SMODE_STATIC);
	/* ---------SLEEP MODE PROCEDURE------------- */
	print_dbg(CAN_WakeupD);
	/* Initialize again CAN Channel 0 */
	can_init(CAN_CHANNEL_EXAMPLE, ((uint32_t)&mob_ram_ch0[0]),
		CANIF_CHANNEL_MODE_NORMAL, can_out_callback_channel0);

	/* Allocate one mob for RX */
	appli_rx_msg.handle = can_mob_alloc(CAN_CHANNEL_EXAMPLE);

	/* Initialize RX message */
	can_rx(CAN_CHANNEL_EXAMPLE, appli_rx_msg.handle, appli_rx_msg.req_type,
		appli_rx_msg.can_msg);

	for (;;) {
		/* Do nothing; interrupts handle the DAC conversions */
	}

}