canif.c File Reference


Detailed Description

CANIF driver for AVR32 UC3.

This file contains basic functions for the AVR32 CAN, with support for all modes, settings and clock speeds.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file canif.c.

#include "compiler.h"
#include "preprocessor.h"
#include "canif.h"

Go to the source code of this file.

Functions

U8 canif_auto_baudrate (U8 ch)
 Config Baudrate in Autobaud mode.
void canif_clear_all_mob (U8 ch, U8 nb_mob)
 Clear all mob of a channel.
U8 canif_fixed_baudrate (U8 ch)
 Config Baudrate with fixed baudrate.
U8 canif_get_mob_free (U8 ch)
U8 canif_get_mob_status (U8 ch, U8 mob)


Function Documentation

U8 canif_auto_baudrate ( U8  ch  ) 

Config Baudrate in Autobaud mode.

Definition at line 109 of file canif.c.

00110 {
00111 /*
00112     U8  u8_temp0;                               //! Temporary variable
00113     U8  brp, prs, ntq, phs1, phs2;              //! Bit timing segment variables
00114     U8  phs1_inc;                               //! Computing needed
00115     U8  bt_not_found, wait_for_rx, evaluate;    //! Keys for "while()" loops
00116     U8  try_conf;                               //! Key for configurate CAN
00117     U8  ovrtim_flag=0;                          //! Timer overflow count
00118     U16 conf_index;                             //! Count of bit timing configuration tried
00119     U8  bt_performed;                           //! Return flag
00120 
00122     phs1_inc = evaluate = 0;
00123     bt_performed = 0;
00124     conf_index = 0;
00125     bt_not_found = 1;
00126 
00129     if (mode==0)
00130     {
00131         brp  = BRP_MIN;
00132         ntq  = NTQ_MIN;
00133         phs1 = PHS1_MIN;
00134         phs2 = PHS2_MIN;
00135         prs  = ntq - ( phs1 + phs2 + 1 );
00136         try_conf = 1;       //! Try this configuration
00137         wait_for_rx = 1;    //! Enable "while (wait_for_rx ..." loop
00138     }
00139     else //! mode = 1
00140     {
00141         brp  = Max ((((PRES &  BRP_MSK) >> 1) +1) , BRP_MIN );
00142         prs  = Max ((((PRS &  PRS_MSK) >> 1) +1) , PRS_MIN );
00143         phs1 = Max ((((PHS1 & PHS1_MSK) >> 1) +1) , PHS1_MIN);
00144         phs2 = Max ((((PHS2 & PHS2_MSK) >> 4) +1) , PHS2_MIN);
00145         ntq  = Max ((prs + phs1 + phs2 + 1) , NTQ_MIN);
00146         phs1_inc = evaluate = 1;   //! To enter in "while (evaluate ..." loop
00147         try_conf = 0;       //! Look for the next configuration
00148         wait_for_rx = 0;    //! Skip "while (wait_for_rx ..." loop
00149     }
00150 
00152     can_clear_all_mob(mob_ram);
00153 
00154     while (bt_not_found == 1)
00155     {
00156         if (try_conf == 1)
00157         {
00158             Can_reset();
00159             conf_index++;
00160             ovrtim_flag=0;
00161 
00163             PRES = ((brp-1) << PRES);
00164             PHS1 = (((phs2 >> 1)-1) << SJW) |((prs-1) << PRS);
00165             PHS2 = (((phs2-1) << PHS2) | ((phs1-1) << PHS1) | (1<<SMP));
00166 
00171             CANTCON = (U8)(conf_index >> 3);
00172 
00174             Can_set_mob(MOB_0);                 //! Use MOb-0
00175             CANSTMOB = 0;                       //! Reset MOb status (undone by "Can_reset()")
00176             CANCDMOB = (MOB_Rx_ENA  << CONMOB); //! MOb 0 in receive mode
00177 
00179             CANGCON = (1<<LISTEN) | (1<<ENASTB);//! Enable CAN controller in "listen" mode
00180             while ((CANGSTA & (1<<ENFG)) == 0); //! Wait for Enable OK
00181             CANGIT = 0xFF;                      //! Reset General errors and OVRTIM flag
00182         }
00183 
00189         while (wait_for_rx == 1)
00190         {
00191             u8_temp0 = CANSTMOB;
00193             if ((u8_temp0 & (1<<RXOK)) != 0)
00194             {   //! --- It is the successful output of "can_auto_baudrate" function
00195                 wait_for_rx = 0;    //! Out of "while (wait_for_rx ..." loop
00196                 evaluate = 0;       //! Will skip "while (evaluate ..." loop
00197                 bt_not_found = 0;   //! Out of "while (bt_not_found ..." loop
00198                 bt_performed = 1;   //! Return flag = TRUE
00199                 DISABLE_MOB;        //! Disable MOb-0
00200                 CANGCON = 0x00;     //! Disable CAN controller & reset "listen" mode
00201                 while ((CANGSTA & (1<<ENFG)) != 0); //! Wait for Disable OK
00202             }
00204              else
00205             {
00207                 if ((u8_temp0 & ((1<<BERR)|(1<<SERR)|(1<<CERR)|(1<<FERR)|(1<<AERR))) !=0)
00208                 {
00209                     evaluate = 1;       //! Will enter in "while (evaluate ..." loop
00210                     wait_for_rx = 0;    //! Out of "while (wait_for_rx ..." loop
00211                 }
00212 
00213                 u8_temp0 = CANGIT;
00214 
00216                 if ((u8_temp0 & (1<<OVRTIM)) !=0 )
00217                 {
00218                     if (ovrtim_flag==0)
00219                     {
00221                         CANGIT |= (1<<OVRTIM);  // Reset OVRTIM
00222                         ovrtim_flag++;
00223                     }
00224                     else
00225                     {
00227                         CANGIT |= (1<<OVRTIM);  // Reset OVRTIM
00228                         evaluate = 1;           //! Will enter in "while (evaluate ..." loop
00229                         wait_for_rx = 0;        //! Out of "while (wait_for_rx ..." loop
00230                     }
00231                 }
00232 
00234                 if ((u8_temp0 & ((1<<SERG)|(1<<CERG)|(1<<FERG)|(1<<AERG))) !=0)
00235                 {
00236                     evaluate = 1;       //! Will enter in "while (evaluate ..." loop
00237                     wait_for_rx = 0;    //! Out of "while (wait_for_rx ..." loop
00238                     try_conf = 1;       //! Try this configuration
00239                 }
00240             }
00241         } // while wait_for_rx ...
00242 
00251         while (evaluate == 1)
00252         {
00253             if (phs1_inc != 0) phs1++;
00254             phs1_inc = 1;
00255 
00256             // --- The following test takes into account the previous incrementation of phs1
00257             if ((phs1 > PHS1_MAX) && (phs2 >= PHS2_MAX))
00258             {
00259                 phs1 = PHS1_MIN;
00260                 phs2 = PHS2_MIN;
00261                 phs1_inc = 0;
00262                 if (ntq != NTQ_MAX) ntq++;
00263                 else
00264                 {
00265                     ntq = NTQ_MIN;
00266                     if (brp != BRP_MAX) brp++;
00267                     else
00268                     {
00270                         evaluate = 0;       //! Out of "while (evaluate ..." loop
00271                         bt_performed = 0;   //! Return flag = FALSE
00272                         bt_not_found = 0;   //! Out of "while (bt_not_found ..." loop
00273                         DISABLE_MOB;        //! Disable MOb-0
00274                         CANGCON = 0x00;     //! Disable CAN controller & reset "listen" mode
00275                         while ((CANGSTA & (1<<ENFG)) != 0); //! Wait for Disable OK
00276                     }
00277                 }
00278             }
00279             else    // if (phs1 > PHS1_MAX ...
00280             {
00282                 if (phs1>5)
00283                 {
00284                     if (phs1>(phs2+1)) phs1=(++phs2);
00285                 }
00286                 else
00287                 {
00288                 phs2=phs1;
00289                 }
00290                 prs = ntq - ( phs1 + phs2 + 1 );
00291 
00293                 if ((prs <= PRS_MAX) && (prs >= PRS_MIN))
00294                 {
00296                     if (((phs2<<2) >= (1+prs+phs1)) && ((phs2+phs2+phs2) <= (1+prs+phs1)))
00297                     {
00298                         evaluate = 0;     //! Out of "while (evaluate ..." loop &
00299                         wait_for_rx = 1;  //!    new "while (bt_not_found ..." loop
00300                     }
00301                 }
00302             }
00303         } // while (evaluate ...
00304     } // while (bt_not_found ...
00305 
00306     return (bt_performed);
00307 */
00308   return 1;
00309 }

void canif_clear_all_mob ( U8  ch,
U8  nb_mob 
)

Clear all mob of a channel.

Definition at line 57 of file canif.c.

References CANIF_clr_mob.

Referenced by can_init().

00059 {
00060     unsigned char  mob_number;
00061     for (mob_number = 0; mob_number < nb_mob; mob_number++)
00062     {
00063         CANIF_clr_mob(ch,mob_number)
00064     }
00065 }

U8 canif_fixed_baudrate ( U8  ch  ) 

Config Baudrate with fixed baudrate.

Definition at line 311 of file canif.c.

References CANIF_conf_bt.

00312 {
00313   CANIF_conf_bt(ch);
00314   /*CAN_enable(ch);   
00315   while(!CAN_channel_enable_status(ch));*/ 
00316   return 1;
00317 }

U8 canif_get_mob_free ( U8  ch  ) 

Definition at line 67 of file canif.c.

References CANIF_mob_get_mob_free, and NO_MOB.

00068 {
00069     unsigned char mob_number;
00070     mob_number = CANIF_mob_get_mob_free(ch);
00071     if ((mob_number&0x20) == 0x20) 
00072     {
00073         return (NO_MOB);
00074     }
00075     return (mob_number);
00076 }

U8 canif_get_mob_status ( U8  ch,
U8  mob 
)

Definition at line 78 of file canif.c.

References CANIF_get_interrupt_error_status, CANIF_get_interrupt_lastmob_selected, CANIF_mob_enable_status, CANIF_mob_get_status, MOB_DISABLE, MOB_NOT_COMPLETED, MOB_RX_COMPLETED, MOB_RX_COMPLETED_DLCW, and MOB_TX_COMPLETED.

00080 {
00081     U8 mob_status;
00082     // Test if MOb ENABLE or DISABLE
00083     if (!(CANIF_mob_enable_status(ch,mob))){return(MOB_DISABLE);}
00084     
00085     mob_status = CANIF_mob_get_status(ch,mob);
00086 
00087     mob_status = mob_status & ((1<<AVR32_CANIF_MOBSR_DLCW_OFFSET)|
00088                                (1<<AVR32_CANIF_MOBSR_TXOK_OFFSET)|
00089                                (1<<AVR32_CANIF_MOBSR_RXOK_OFFSET));
00090     if ( (mob_status==MOB_RX_COMPLETED) ||   \
00091          (mob_status==MOB_TX_COMPLETED) ||   \
00092          (mob_status==MOB_RX_COMPLETED_DLCW) ) { return(mob_status); }
00093 
00094     // Test if last mob selected is the mob interested
00095     if ( CANIF_get_interrupt_lastmob_selected(ch) != mob) {
00096       return(MOB_NOT_COMPLETED);
00097     }
00098 
00099     // If MOb is ENABLE & NOT_COMPLETED, test if CAN is in ERROR
00100     mob_status = CANIF_get_interrupt_error_status(ch);
00101     if (mob_status != 0) { 
00102       return(mob_status); 
00103     }
00104 
00105     // If CANSTMOB = 0 then MOB_NOT_COMPLETED
00106     return(MOB_NOT_COMPLETED);
00107 }


Generated on Fri Feb 19 02:24:19 2010 for AVR32 - CANIF Driver by  doxygen 1.5.5