Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of LG2 by
CyclesSync.c
00001 /**--------------File Info--------------------------------------------------------------------------------- 00002 ** File name: CycleSync.c 00003 ** Last modified Date: 2011-09-06 00004 ** Last Version: V1.00 00005 ** Descriptions: 00006 ** 00007 **-------------------------------------------------------------------------------------------------------- 00008 ** Created by: Electrooptica Inc. 00009 ** Created date: 2011-09-06 00010 ** Version: V1.00 00011 ** Descriptions: There is the routines for device synchronization 00012 ** 00013 **-------------------------------------------------------------------------------------------------------- 00014 *********************************************************************************************************/ 00015 #include "CyclesSync.h" 00016 #include "CntrlGLD.h" 00017 #include "SIP.h" 00018 #include "el_lin.h" 00019 #include "InputOutput.h" 00020 //#include "Global.h" 00021 uint32_t Sys_Clock; //e. counter of system ticks //r. счетчик временной сетки системы 00022 uint32_t Seconds = 0; //e. seconds from power on //r. число секунд от момента включения питания 00023 int32_t time_1_Sec = DEVICE_SAMPLE_RATE_uks; //e. pseudosecond timer //r. псевдосекундный таймер 00024 uint32_t trm_cycl; 00025 uint32_t count; 00026 int32_t PrevPeriod = 0; 00027 uint32_t num; 00028 volatile uint32_t Latch_Rdy = 0; 00029 00030 int32_t LatchPhase = INT32_MAX; 00031 uint32_t Ext_Latch_ResetEnable = 1; 00032 volatile uint32_t data_Rdy = 0; 00033 00034 uint32_t Delay_UART_Enbl = DELAY_UART_ENBL; 00035 //uint32_t Delay_UART_Disbl = DELAY_UART_ENBL; 00036 00037 uint32_t main_cycle_latch = 0; 00038 uint32_t Out_main_cycle_latch; //e. counter of main cycles between external latch pulse appearence 00039 uint32_t T_latch, Out_T_latch, temp_T_latch; 00040 00041 /****************************************************************************** 00042 ** Function name: Latch_Event 00043 ** 00044 ** Descriptions: Routine for latch appearing 00045 ** 00046 ** parameters: None 00047 ** Returned value: None 00048 ** 00049 ******************************************************************************/ 00050 /*void Latch_Event() 00051 { 00052 static unsigned PreLatch = 0; 00053 // static int cc = 0; 00054 if (LatchPhase < INT32_MAX) { //Если LatchPhase(флаг защелки) < меньше максимального 32-х битного инта (при инициализации int32_t LatchPhase = INT32_MAX;) то: 00055 Latch_Rdy = 1; //e. установить флаг для обработки ниже 00056 if (RgConB) { //Если RgConB (дополнительный регистр управления, установлен в еденицу при инициализации в мэйне) утановлен в еденицу то: 00057 if (PreLatch) //e. если мы откладываем защелку 00058 PreLatch = 0; 00059 00060 else if ((LatchPhase < LPC_PWM1->TC) && (num == Sys_Clock)) { //e. latch have appeared in current cycle 00061 Latch_Rdy = 0; //e. bring it to the next cycle 00062 PreLatch = 1; 00063 } 00064 } 00065 } else 00066 Latch_Rdy = 0; //e. latch is absent*/ 00067 //---------------------------temp------------------------------- 00068 /* Latch_Rdy = 0; //e. latch is absent 00069 if (cc++ == 19) 00070 { 00071 cc = 0; 00072 Latch_Rdy = 1; 00073 LatchPhase = 2500; 00074 } */ 00075 //----------------------temp-------------------------------- 00076 //} 00077 00078 /****************************************************************************** 00079 ** Function name: QEI_IRQHandler 00080 ** 00081 ** Descriptions: Latch counters by reference meander 00082 ** 00083 ** parameters: None 00084 ** Returned value: None 00085 ** 00086 ******************************************************************************/ 00087 __irq void QEI_IRQHandler (void) 00088 { 00089 static uint32_t halfQEIPeriod = 0; 00090 00091 Cnt_curr = LPC_QEI->POS; //e. read accumulated value of counter 00092 if (LPC_QEI->INTSTAT & 0x0008) { 00093 data_Rdy = 0x0004; //e. data for Cnt_Pls or Cnt_Mns calculation are ready 00094 00095 if (++halfQEIPeriod & 0x0001) { //e. period elapsed, we can calculate Cnt_Dif 00096 data_Rdy = 0x000C; 00097 } 00098 } 00099 LPC_QEI->CLR = 0x1fff; //e. reset interrupt request //r. сбросить запрос прерывания 00100 } 00101 /****************************************************************************** 00102 ** Function name: SetIntLatch 00103 ** 00104 ** Descriptions: Set timer for accumulation period 00105 ** 00106 ** parameters: Period of accumulation 00107 ** Returned value: None 00108 ** 00109 ******************************************************************************/ 00110 void SetIntLatch(uint32_t cycle) 00111 { 00112 LPC_TIM3->TCR = 0x2; //switch off and reset timer3 00113 if (cycle != 0) { 00114 LPC_TIM3->MR0 = (cycle<<2); //load new value 00115 LPC_TIM3->TCR = 1; //switch on timer3 00116 } 00117 } 00118 /****************************************************************************** 00119 ** Function name: SwitchRefMeandInt 00120 ** 00121 ** Descriptions: Enable/disable interrupt from reference meander 00122 ** 00123 ** parameters: switcher 00124 ** Returned value: None 00125 ** 00126 ******************************************************************************/ 00127 void SwitchRefMeandInt(uint32_t s) 00128 { 00129 LPC_QEI->CLR = 0x1fff; //e. reset all interrupts //r. сбросить все прерывания 00130 if (s) 00131 LPC_QEI->IEC = 0x1fff; //e. disable direction changing interrupt //r. запретить прерывание при изменении направления 00132 else 00133 LPC_QEI->IES = 0x0008; //e. enable direction changing interrupt //r. разрешить прерывание при изменении направления 00134 } 00135 /****************************************************************************** 00136 ** Function name: ExtLatch_IRQHandler 00137 ** 00138 ** Descriptions: Routine for external latch appearence processing 00139 ** 00140 ** parameters: None 00141 ** Returned value: None 00142 ** 00143 ******************************************************************************/ 00144 /*__irq void EINT3_IRQHandler (void) 00145 { 00146 //LPC_GPIO2->FIOSET = 0x00000020; //e. turn on the LED 00147 LatchPhase = LPC_PWM1->TC; //e. read moment of latch 00148 LPC_TIM0->TCR = 1; //e. start Mltdrop delay timer 00149 LPC_GPIOINT->IO0IntClr |= 0x0000800;//e. clean interrupt request 00150 //LPC_GPIO2->FIOCLR = 0x00000020; //e. turn off the LED 00151 }*/ 00152 /****************************************************************************** 00153 ** Function name: IntLatch_IRQHandler 00154 ** 00155 ** Descriptions: Routine for Internal latch appearence processing 00156 ** 00157 ** parameters: None 00158 ** Returned value: None 00159 ** 00160 ******************************************************************************/ 00161 __irq void IntLatch_IRQHandler (void) 00162 { 00163 //LPC_GPIO2->FIOSET |= 0x00000080; // turn on the LED 00164 LatchPhase =(int)LPC_PWM1->TC; //e. read moment of latch 00165 LPC_TIM3->IR = 0x0001; //e. clear interrupt flag 00166 num = Sys_Clock;// 00167 00168 /* if(Gyro.EXT_Latch) Gyro.EXT_Latch=0; 00169 else Gyro.EXT_Latch=1;*/ 00170 // count++; 00171 //LPC_GPIO2->FIOCLR |= 0x00000080; // turn off the LED 00172 } 00173 00174 /****************************************************************************** 00175 ** Function name: SwitchMode 00176 ** 00177 ** Descriptions: Switch mode of device functionality 00178 ** 00179 ** parameters: None 00180 ** Returned value: None 00181 ** 00182 ******************************************************************************/ 00183 int SwitchMode() 00184 { 00185 //-----------------------disable latch sources --------------------- 00186 SetIntLatch(0); //e. disable internal latch 00187 LPC_TIM3->IR = 0x0001; //e. clear internal latch interrupt request 00188 00189 LPC_GPIOINT->IO0IntEnR &= ~0x0000800; //e. disable external latch 00190 LPC_GPIOINT->IO0IntClr |= 0x0000800; //e. clean external latch interrupt request 00191 00192 LPC_TIM0->TCR = 2; //e. stop and reset the multidrop delay timer 00193 LPC_TIM0->IR = 0x03F; //e. clear internal latch interrupt request 00194 //----------------------wait while UART and DMA are active------------- 00195 if ( LPC_GPDMACH1->CConfig & DMA_BUSY) //e. if DMA channel is busy, wait //r. если канал передачи занят, ждать 00196 return 0; 00197 LPC_GPDMACH1->CConfig &= ~DMAChannelEn; //e. disable DMA for UART transmition 00198 LPC_GPDMACH2->CConfig &= ~DMAChannelEn; 00199 00200 if (!(LPC_UART1->LSR & TRANS_SHIFT_BUF_EMPTY)) //e. transmit buffer is not empty //r. передающий буфер не пуст 00201 return 0; 00202 LPC_UART1->FCR |= 0x4; //e. reset TX FIFO 00203 00204 LPC_TIM0->IR = 0x3F; //e. clear all interrupt flags 00205 //---------------------configure a new exchanging parameters------------ 00206 if (Device_Mode > 3) { //e. external latch mode enabled 00207 LPC_TIM0->MR0 = 10; 00208 LPC_TIM0->MR1 = 50; //e. /10 = delay before enable signal (us) 00209 // LPC_UART1->FCR &= ~0x08; //e. TX FIFO is not source for DMA request 00210 00211 // LPC_SC->DMAREQSEL = 0xC; //e. external latch delay timer is source for DMA request 00212 // LPC_GPIOINT->IO0IntEnR |= 0x0000800; //e. enable rising edge interrupt 00213 } else { //e. internal latch mode enabled 00214 LPC_TIM0->MR0 = 10; 00215 LPC_TIM0->MR1 = 5000; //e. /10 = delay before enable signal (us) 00216 // LPC_SC->DMAREQSEL = 0x3; //e. FIFO generate DMA request 00217 00218 // LPC_SC->EXTINT = 0x8; //e. clean interrupt request 00219 } 00220 00221 UART_SwitchSpeed(SRgR & 0x0030); 00222 00223 if (Device_Mode == DM_INT_LATCH_DELTA_PS) 00224 SetIntLatch(50000); 00225 return 1; 00226 } 00227 /****************************************************************************** 00228 ** Function name: ServiceTime 00229 ** 00230 ** Descriptions: Routine for pseudoseconds counting 00231 ** 00232 ** parameters: None 00233 ** Returned value: None 00234 ** 00235 ******************************************************************************/ 00236 void ServiceTime(void)// псевдосекундный счетчик. 00237 { 00238 time_1_Sec -= PrevPeriod; 00239 00240 if (time_1_Sec < (PrevPeriod>>1)) { //1 sec elapsed with accurate 0.5 of main period 00241 time_1_Sec = DEVICE_SAMPLE_RATE_uks; 00242 Seconds++; 00243 } 00244 if (Seconds == 3) { 00245 Seconds++; 00246 // close_all_loops(); 00247 } 00248 Sys_Clock++; //e. increment of the system clock register //r. инкремент регистра временной сетки системы 00249 PrevPeriod = LPC_PWM1->MR0; 00250 } // ServiceTime 00251 00252 /****************************************************************************** 00253 ** Function name: WDTFeed 00254 ** 00255 ** Descriptions: Feed watchdog timer to prevent it from timeout 00256 ** 00257 ** parameters: None 00258 ** Returned value: None 00259 ** 00260 ******************************************************************************/ 00261 void WDTFeed( void ) 00262 { 00263 LPC_WDT->FEED = 0xAA; /* Feeding sequence */ 00264 LPC_WDT->FEED = 0x55; 00265 return; 00266 } 00267 00268 /****************************************************************************** 00269 ** Function name: CounterIquiryCycle_Init 00270 ** 00271 ** Descriptions: CounterIquiryCycle_Init setup demanded GPIOs for PWM1, 00272 ** reset counter, all latches are enabled, interrupt 00273 ** on PWMMR0, install PWM interrupt to the VIC table. 00274 ** 00275 ** parameters: 00276 ** Returned value: 00277 ** 00278 ******************************************************************************/ 00279 void CounterIquiryCycle_Init(uint32_t cycle) 00280 { 00281 //PWM1 enabled after reset by default 00282 LPC_SC->PCLKSEL0 &= ~(3<<12); 00283 LPC_SC->PCLKSEL0 |= (1<<12); //PWM1 is synchronized by CCLK (100 MHz)(CCLK) PCLKSEL0 - Peripheral Clock Selection registers. 00284 00285 LPC_PWM1->TCR = TCR_RESET; /* Counter Reset */ //#define TCR_RESET 0x00000002 инкрементировать счетчик при спадении импульса ПВМ. 00286 LPC_PWM1->MCR = PWMMR0I |PWMMR0R; //generate interrupt and reset timer counter ( PWMMR0I - прерывание генерируется когда PWMMR0 совподает со значением PWMTC. ) 00287 // ( PWMMR0R - сбрасывает PWMTC при совподении PWMTC и PWMMR0. ) 00288 00289 LPC_PWM1->CCR = 0x1; //разрешить прерывание только при совпадении PWMTC с PWMMR0 (еще что то связано с загрузкой ТС) 00290 00291 00292 PrevPeriod = 10000; 00293 LPC_PWM1->MR0 = PrevPeriod; /* set PWM cycle */// задание периода сброса счетчика. 00294 LPC_PWM1->PCR = PWMENA1; //e. single edge control mode, PWM1 out enabled only включение выхода ШИМ 1. 00295 LPC_PWM1->LER = LER0_EN ; //e. enable updating of register 00296 LPC_PWM1->TCR = TCR_CNT_EN | TCR_PWM_EN; /* counter enable, PWM enable */ //включение счетчика и ШИМ 00297 00298 NVIC_DisableIRQ(PWM1_IRQn );//запретить прерывание. 00299 00300 00301 return ; 00302 } 00303 00304 /*__irq void TIMER0_IRQHandler() 00305 { 00306 int val = LPC_TIM0->IR; 00307 LPC_TIM0->IR |= 3; 00308 00309 if (val & 1) { //MAT 1.0 interrupt 00310 LPC_GPIO2->FIOSET |= 1<<6; // turn on the LED 00311 LPC_TIM0->IR |= 1; 00312 return; 00313 } 00314 if (val & 2) { //MAT 1.1 interrupt 00315 LPC_GPIO2->FIOCLR |= 1<<6; // turn on the LED 00316 LPC_TIM0->IR |= 2; 00317 return; 00318 } 00319 if (val & 4) { //MAT 0.2 interrupt 00320 // LPC_GPIO0->FIOSET2 |= 0xFF; //set P0.23 00321 // LPC_GPIO2->FIOSET |= 0x00000040; // turn on the LED 00322 // LPC_TIM0->IR |= 4; 00323 return; 00324 } 00325 return; 00326 }*/ 00327 00328 /****************************************************************************** 00329 ** Function name: ExtLatch_Init 00330 ** 00331 ** Descriptions: Initialization of external latch 00332 ** 00333 ** parameters: None 00334 ** Returned value: None 00335 ** 00336 ******************************************************************************/ 00337 void ExtLatch_Init() 00338 { 00339 LPC_PINCON->PINSEL4 &= ~0xC000000; //e. использовать P2.13 как GPIO. 00340 LPC_PINCON->PINSEL0 &= ~0x0C00000; //e. использовать P0.11 как GPIO. 00341 LPC_GPIO0->FIODIR &= ~0x0000800; //e. использовать P0.11 вхлод. 00342 LPC_GPIOINT->IO0IntEnR &= ~0x0000800; //e. отключить внутреннюю защелку. 00343 LPC_GPIOINT->IO0IntClr |= 0x0000800; //e. сбросить запросы на прерывания 00344 00345 NVIC_EnableIRQ(EINT3_IRQn ); // разрешить прерывания. 00346 00347 //+++++++ initialization of timer for multidrop delay generation+++++++++++++++++++++++ 00348 //e. TIMER0 enabled by default 00349 LPC_SC->PCLKSEL0 &= ~(3<<2); //e. сбросить частоту таймера 0 00350 LPC_SC->PCLKSEL0 |= (3<<2); //e. установить частоту таймера 0 на CLK/8. 00351 LPC_TIM0->PR = 0; //e. установить делитель частоты в 0. 00352 LPC_TIM0->IR = 0x3F; //e. очистить флаги прерываний. 00353 LPC_TIM0->MCR = 1 |(1<<3)|MR1_RESET |MR1_STOP; //e. сбросить и остановить таймер после совпадения MR1 и TC. 00354 LPC_TIM0->CCR = 0; //e. содержимое таймера загружается при появлении переднего фронта защелки. 00355 LPC_TIM0->CTCR = 0; //e. timer0 в режиме таймера. 00356 00357 LPC_TIM0->MR0 = /*Device_blk.Address**/10; //e. delay before UART transmitter loading 00358 LPC_TIM0->MR1 = /*Device_blk.Address**/5000; //e. delay before UART transmitter start 00359 //e. set first empty) event of timer 00360 NVIC_DisableIRQ(TIMER0_IRQn ); 00361 } 00362 /****************************************************************************** 00363 ** Function name: IntLatch_Init 00364 ** 00365 ** Descriptions: Initialization of iternal latch cycle generation 00366 ** 00367 ** parameters: None 00368 ** Returned value: None 00369 ** 00370 ******************************************************************************/ 00371 void IntLatch_Init() 00372 { 00373 LPC_SC->PCONP |= (1<<23); //включит TIME3(таймер) 00374 //e. by default timer3 mode is not counter (T3CTCR[1,0]=0) 00375 00376 LPC_SC->PCLKSEL1 &= SYNC_CLCK4; // частота входного сигнала равна CLCK/4 00377 LPC_SC->PCLKSEL1 |= (1<<14); // timer 3 input frequency equal to CLCK/4 00378 00379 // LPC_PINCON->PINSEL1 |= (3<<16); //e. CAP 3.1 is connected to P0.24 00380 //e. by default CAP3.0 is connected to timer3 00381 LPC_TIM3->PR = 0; //e. установить предделитель таймера3 на 0. 00382 LPC_TIM3->IR = 0x0001; //e. очистить флаги прерываний. 00383 LPC_TIM3->MCR = MR0_RESET |MR0_INT_EN |MR0_NO_STOP; // установка флагов прерываний которые выполняются при совпадении таймера и МСR. 00384 LPC_TIM3->CCR = 0x0001; //содержимое счетчика загружается по приходу переднего фронта защелки. 00385 00386 NVIC_EnableIRQ(TIMER3_IRQn ); //разрешить прерывания. 00387 } 00388 /***************************************************************************** 00389 ** Function name: WDTInit 00390 ** 00391 ** Descriptions: Initialize watchdog timer, install the 00392 ** watchdog timer interrupt handler 00393 ** 00394 ** parameters: None 00395 ** Returned value: true or false, return false if the VIC table 00396 ** is full and WDT interrupt handler can be 00397 ** installed. 00398 ** 00399 *****************************************************************************/ 00400 uint32_t WDTInit( void ) 00401 { 00402 NVIC_DisableIRQ(WDT_IRQn ); 00403 00404 LPC_WDT->TC = WDT_FEED_VALUE; // once WDEN is set, the WDT will start after feeding 00405 LPC_WDT->MOD = WDEN; 00406 00407 LPC_WDT->FEED = 0xAA; // Feeding sequence 00408 LPC_WDT->FEED = 0x55; 00409 00410 return 1; 00411 } 00412 /****************************************************************************** 00413 ** End Of File 00414 ******************************************************************************/
Generated on Thu Jul 14 2022 02:34:42 by
1.7.2
