00001 /*This file is prepared for Doxygen automatic documentation generation.*/ 00018 /* Copyright (c) 2009 Atmel Corporation. All rights reserved. 00019 * 00020 * Redistribution and use in source and binary forms, with or without 00021 * modification, are permitted provided that the following conditions are met: 00022 * 00023 * 1. Redistributions of source code must retain the above copyright notice, this 00024 * list of conditions and the following disclaimer. 00025 * 00026 * 2. Redistributions in binary form must reproduce the above copyright notice, 00027 * this list of conditions and the following disclaimer in the documentation 00028 * and/or other materials provided with the distribution. 00029 * 00030 * 3. The name of Atmel may not be used to endorse or promote products derived 00031 * from this software without specific prior written permission. 00032 * 00033 * 4. This software may only be redistributed and used in connection with an Atmel 00034 * AVR product. 00035 * 00036 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00037 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00038 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00039 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 00040 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00041 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00042 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00043 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00044 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00045 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00046 * 00047 */ 00048 00049 //_____ I N C L U D E S ________________________________________________________ 00050 #include "compiler.h" 00051 #include "preprocessor.h" 00052 #include "canif.h" 00053 //_____ D E F I N I T I O N S __________________________________________________ 00054 00055 //_____ F U N C T I O N S ______________________________________________________ 00056 00057 void canif_clear_all_mob(U8 ch, 00058 U8 nb_mob) 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 } 00066 00067 U8 canif_get_mob_free(U8 ch) 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 } 00077 00078 U8 canif_get_mob_status(U8 ch, 00079 U8 mob) 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 } 00108 00109 U8 canif_auto_baudrate (U8 ch) 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 } 00310 00311 U8 canif_fixed_baudrate(U8 ch) 00312 { 00313 CANIF_conf_bt(ch); 00314 /*CAN_enable(ch); 00315 while(!CAN_channel_enable_status(ch));*/ 00316 return 1; 00317 }