summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomasz Moń <desowin@gmail.com>2021-06-27 13:14:11 +0200
committerTomasz Moń <desowin@gmail.com>2021-06-27 13:14:11 +0200
commit1b81bd8a6153314aadc1144d55a7c02eb11111c5 (patch)
treefd0ac1e911b1f7406dbb2f0279a6b2fbf7ad599f
parenta4ab6364236b4453e93b12ecba1e9f389f6edd2f (diff)
downloadrockbox-1b81bd8a6153314aadc1144d55a7c02eb11111c5.tar.gz
rockbox-1b81bd8a6153314aadc1144d55a7c02eb11111c5.zip
Sansa Connect: Discard invalid monotime reads
Read monotime twice in a row and only accept the value if it matches or the two reads are 1 second apart. Change-Id: Ibd289103a20404dd1b2bbd131fdfa8905852c788
-rw-r--r--firmware/target/arm/tms320dm320/sansa-connect/avr-sansaconnect.c61
1 files changed, 39 insertions, 22 deletions
diff --git a/firmware/target/arm/tms320dm320/sansa-connect/avr-sansaconnect.c b/firmware/target/arm/tms320dm320/sansa-connect/avr-sansaconnect.c
index a72faea7be..94ffcdf195 100644
--- a/firmware/target/arm/tms320dm320/sansa-connect/avr-sansaconnect.c
+++ b/firmware/target/arm/tms320dm320/sansa-connect/avr-sansaconnect.c
@@ -120,9 +120,10 @@ static bool input_interrupt_pending;
120#define MONOTIME_OFFSET_FILE ROCKBOX_DIR "/monotime_offset.dat" 120#define MONOTIME_OFFSET_FILE ROCKBOX_DIR "/monotime_offset.dat"
121static uint32_t monotime_offset; 121static uint32_t monotime_offset;
122/* Buffer last read monotime value. Reading monotime takes 122/* Buffer last read monotime value. Reading monotime takes
123 * atleast 700 us so the tick counter is used together with 123 * atleast 1400 us so the tick counter is used together with
124 * last read monotime value to return current time. 124 * last read monotime value to return current time.
125 */ 125 */
126static bool monotime_available;
126static uint32_t monotime_value; 127static uint32_t monotime_value;
127static unsigned long monotime_value_tick; 128static unsigned long monotime_value_tick;
128 129
@@ -521,16 +522,6 @@ bool charging_state(void)
521 return (avr_battery_status & BATTERY_STATUS_CHARGING) != 0; 522 return (avr_battery_status & BATTERY_STATUS_CHARGING) != 0;
522} 523}
523 524
524static uint32_t avr_hid_get_monotime(void)
525{
526 uint8_t tmp[4];
527 if (avr_execute_command(CMD_MONOTIME, tmp, sizeof(tmp)))
528 {
529 return (tmp[0]) | (tmp[1] << 8) | (tmp[2] << 16) | (tmp[3] << 24);
530 }
531 return 0;
532}
533
534static void avr_hid_enable_wheel(void) 525static void avr_hid_enable_wheel(void)
535{ 526{
536 uint8_t enable = 0x01; 527 uint8_t enable = 0x01;
@@ -651,22 +642,48 @@ static bool write_monotime_offset(void)
651 642
652static void read_monotime(void) 643static void read_monotime(void)
653{ 644{
654 uint32_t value = avr_hid_get_monotime(); 645 uint8_t tmp[4];
655 int flags = disable_irq_save(); 646 uint32_t t1, t2;
656 monotime_value = value; 647
657 monotime_value_tick = current_tick; 648 if (!avr_execute_command(CMD_MONOTIME, tmp, sizeof(tmp)))
658 restore_irq(flags); 649 {
650 return;
651 }
652 t1 = (tmp[0]) | (tmp[1] << 8) | (tmp[2] << 16) | (tmp[3] << 24);
653
654 if (!avr_execute_command(CMD_MONOTIME, tmp, sizeof(tmp)))
655 {
656 return;
657 }
658 t2 = (tmp[0]) | (tmp[1] << 8) | (tmp[2] << 16) | (tmp[3] << 24);
659
660 if ((t1 == t2) || (t1 + 1 == t2))
661 {
662 int flags = disable_irq_save();
663 monotime_value = t1;
664 monotime_value_tick = current_tick;
665 restore_irq(flags);
666 monotime_available = true;
667 }
659} 668}
660 669
661static time_t get_timestamp(void) 670static time_t get_timestamp(void)
662{ 671{
663 time_t timestamp; 672 time_t timestamp;
664 int flags = disable_irq_save(); 673 if (!monotime_available)
665 timestamp = monotime_value; 674 {
666 timestamp += monotime_offset; 675 read_monotime();
667 timestamp += ((current_tick - monotime_value_tick) / HZ); 676 }
668 restore_irq(flags); 677 if (monotime_available)
669 return timestamp; 678 {
679 int flags = disable_irq_save();
680 timestamp = monotime_value;
681 timestamp += monotime_offset;
682 timestamp += ((current_tick - monotime_value_tick) / HZ);
683 restore_irq(flags);
684 return timestamp;
685 }
686 return 0;
670} 687}
671 688
672void rtc_init(void) 689void rtc_init(void)