Fixes #3492: delay_01us() and delay_ms() functions were generating wrong delays (6 times shorter on Taranis, 8 times on X9E and Horus). Important delays adjusted, function names and code unified. (#3496)

This commit is contained in:
Damjan Adamic
2016-05-14 21:56:20 +02:00
committed by Bertrand Songis
parent 98798c1e42
commit 59653f9ca6
6 changed files with 60 additions and 63 deletions
+3 -3
View File
@@ -165,9 +165,9 @@ uint32_t adcReadNextSPIChannel(uint8_t index)
//
// delay value A5 change Time needed for adcRead()
// 1 16 0.154ms - 0.156ms
// 300 5 0.197ms - 0.199ms
// 500 0 0.225ms - 0.243ms
delay_01us(300);
// 38 5 0.197ms - 0.199ms
// 62 0 0.225ms - 0.243ms
delay_01us(40);
for (uint8_t i = 0; i < 4; i++) {
ADC_CS_LOW();
+1 -1
View File
@@ -112,7 +112,7 @@ extern "C" {
#endif
void delaysInit(void);
void delay_01us(uint16_t nb);
void delay_ms(uint16_t nb);
void delay_ms(uint32_t ms);
#ifdef __cplusplus
}
#endif
+15 -17
View File
@@ -25,25 +25,23 @@ void delaysInit(void)
// Timer13
RCC->APB1ENR |= RCC_APB1ENR_TIM13EN; // Enable clock
TIM13->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 10000000 - 1; // 0.1uS
TIM13->CCER = 0;
TIM13->CCMR1 = 0;
TIM13->CR1 = 0x02;
TIM13->DIER = 0;
}
void delay_01us(uint16_t nb)
{
TIM13->EGR = 1;
TIM13->CNT = 0;
TIM13->CR1 = 0x03;
while (TIM13->CNT < nb);
TIM13->CR1 = 0x02;
TIM13->ARR = 0xFFFF;
TIM13->EGR = TIM_EGR_UG; // generate update event
}
void delay_ms(uint16_t nb)
void delay_01us(uint16_t nb)
{
uint16_t i;
for (i=0; i<nb; i++) {
delay_01us(10000);
TIM13->EGR = TIM_EGR_UG; // generate update event (this also resets counter)
TIM13->SR &= ~TIM_SR_CC1IF; // clear CC flag
TIM13->CCR1 = nb; // set CC value
TIM13->CR1 |= TIM_CR1_CEN; // start timer
while((TIM13->SR & TIM_SR_CC1IF) == 0);
TIM13->CR1 &= ~TIM_CR1_CEN; // stop timer
}
void delay_ms(uint32_t ms)
{
while(ms--) {
delay_01us(10500); // delay adjusted for the fact that ideal prescaler would be 8.4 but 8 is used
}
}