Skip to content

Commit

Permalink
fixed decoding count to time conversion constant on efm8bb1
Browse files Browse the repository at this point in the history
  • Loading branch information
mightymos committed Jul 2, 2024
1 parent d9fbcc5 commit 6c3b787
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
13 changes: 8 additions & 5 deletions drivers/efm8bb1/src/hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,9 @@ void load_timer1(const uint16_t value)
void pca0_init(void)
{
// default pca acts normally when controller is idle
// default source is system clock divided by 12
// default pca CF overflow is disabled
PCA0MD |= CPS__SYSCLK_DIV_12;
//PCA0MD &= ~CPS__SYSCLK_DIV_12;

// enable both positive and negative edge triggers
PCA0CPM0 |= CAPP__ENABLED;
Expand All @@ -233,12 +234,12 @@ void pca0_init(void)

void pca0_run(void)
{
PCA0CN0 |= CR__RUN;
CR = true;
}

void pca0_halt(void)
{
PCA0CN0 &= ~CR__RUN;
CR = false;
}


Expand All @@ -263,11 +264,13 @@ void clear_capture_flag(void)
CCF0 = 0;
}

// FIXME: explain counts to time conversion constant
// the time constant is explained in the rcswitch.c file

unsigned long countsToTime(const unsigned long duration)
{
unsigned long converted;
converted = duration * 2;

converted = duration / 2;

return converted;
}
2 changes: 1 addition & 1 deletion drivers/ob38s003/inc/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ extern void disable_serial_interrupt(void);
extern bool global_interrupts_are_enabled(void);

void load_timer0(const uint16_t load);
extern uint16_t get_timer2(void);
extern uint16_t get_capture_mode(void);

extern void clear_capture_flag(void);

Expand Down
9 changes: 5 additions & 4 deletions drivers/ob38s003/src/hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ void init_timer2_as_capture(void)
//(i.e., P1.6 or RDATA which is input from D0 of radio)
CCEN = 0x60;

// timer 2 input frequency from prescaler
// timer 2 input frequency from prescaler (i.e., TSPS set to 1/24 below)
// timer 2 mode 0 auto reload (generated by a timer 2 overflow)
// timer 2 is clocked with 1/4 (0x51) or 1/24 (0xD1) of the oscillator frequency (prescaler select bit)
// timer 2 is clocked with 1/24 of the oscillator frequency (prescaler select bit)
// (e.g., 0.25 microseconds per count)
T2CON = 0xD1;
}
Expand All @@ -208,7 +208,7 @@ bool global_interrupts_are_enabled(void)
}


uint16_t get_timer2(void)
uint16_t get_capture_mode(void)
{
return (CCH1 << 8) | CCL1;
}
Expand All @@ -223,10 +223,11 @@ unsigned char get_stack_pointer(void)
return SP;
}

// FIXME: on counts to time conversion
// the time constant is explained in the rcswitch.c file
unsigned long countsToTime(const unsigned long duration)
{
unsigned long converted;

converted = (duration * 3) / 2;

return converted;
Expand Down
2 changes: 1 addition & 1 deletion drivers/ob38s003/src/timer_interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void timer1_isr(void) __interrupt (d_T1_Vector)
//-----------------------------------------------------------------------------
void timer2_isr(void) __interrupt (d_T2_Vector)
{
uint16_t currentCapture = get_timer2();
uint16_t currentCapture = get_capture_mode();

capture_handler(currentCapture);

Expand Down
8 changes: 4 additions & 4 deletions src/rcswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,22 @@ void capture_handler(const uint16_t currentCapture)
// FIXME: no magic numbers
// FIXME: seems like a bad idea to make wrap around calculation depend on variable type, what if it changes
// if overflow, we must compute difference by taking into account wrap around at maximum variable size
duration = ULONG_MAX - previous + current;
duration = UINT_MAX - previous + current;
} else {
duration = current - previous;
}

// FIXME: no magic numbers
// e.g., EFM8BB1
// e.g. (1/(24500000))*(49/2) = 1 microsec
// e.g. (1/(24500000/12))*2 = 0.9796 microsec
// so need to do the inverse to go from counts to time (i.e., counts * 1/2 = time)
// (1/(24500000/12))*dec(0xFFFF) = 32.0987755 millisecs max

// e.g., OBS38S003
// e.g. prescale at (1/4) at 16 MHz, four counts are needed to get one microsecond
// e.g. prescale at (1/24) at 16 MHz
// e.g. prescale at (1/24) at 16 MHz, 2/3 counts are need to get one microsecond
// so inverse is counts * 3/2 = time
// e.g., (1/(16000000/24)) * dec(0xFFFF) = 98.30 milliseconds maximum can be counted
// FIXME: show why 3/2 conversion works
duration = countsToTime(duration);

// from oscillscope readings it appears that first sync pulse of first radio packet is frequently not output properly by receiver
Expand Down

0 comments on commit 6c3b787

Please sign in to comment.