Author Topic: Issues with the TI-Nspire CX timer  (Read 4173 times)

0 Members and 1 Guest are viewing this topic.

Offline hoffa

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 322
  • Rating: +131/-13
    • View Profile
Issues with the TI-Nspire CX timer
« on: March 14, 2012, 02:20:08 pm »
Hello,

I've been trying to get the CX timer working for a few days now, but I still haven't succeeded. I based my code on the official documentation; here's the said code:

Code: [Select]
#include <os.h>

#define BASE 0x900C0000
#define LOAD 0x00
#define VALUE 0x04
#define CTRL 0x08

#define CTRL_32BIT 0b00000010
#define CTRL_PERIODIC 0b01000000
#define CTRL_ENABLE 0b10000000

int main(void) {
volatile unsigned *load = (unsigned *)(BASE + LOAD);
volatile unsigned *value = (unsigned *)(BASE + VALUE);
volatile unsigned *control = (unsigned *)(BASE + CTRL);
*(volatile unsigned *)0x900B0018 &= ~(1 << 11);
*(volatile unsigned *)(BASE + 0x80) = 0xA;
*control = 0;
*load = 0xFFFFFFFF;
*control = CTRL_32BIT | CTRL_PERIODIC | CTRL_ENABLE;
printf("%u\n", *value);
sleep(3000);
printf("%u\n", *value);
return 0;
}

The timer just stays the same, and seems like it breaks the sleep() function after a first run of the program (I checked, and sleep() uses the second timer, while my code uses the first one). If anyone knows how to fix that, I'd greatly appreciate.

Thanks.
« Last Edit: March 14, 2012, 04:13:42 pm by hoffa »

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Issues with the TI-Nspire CX timer
« Reply #1 on: March 14, 2012, 03:34:20 pm »
I think I mentioned this in a PM earlier, but you'll need to write the TI-specific register *(volatile unsigned*)(BASE + 0x80) = 0xA; after enabling bus access in order to set the 32768Hz timer speed.

Edit: Also, the VALUE register is read-only, and you set it by writing to the LOAD register.
« Last Edit: March 14, 2012, 03:35:29 pm by calc84maniac »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline hoffa

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 322
  • Rating: +131/-13
    • View Profile
Re: Issues with the TI-Nspire CX timer
« Reply #2 on: March 14, 2012, 04:36:23 pm »
I tried setting BASE+0x80 to 0xA later but it didn't do anything. Also I checked the ARM Linux code to see how it dealt with the same timer, and it set the Value register; tried doing the same, that's why it's there. Actually I checked the code I posted in PM, and it did work to a certain extent, but now I remember the issue was with setting the Load register. It's as if it took some time before the timer became aware of the new value, and consequently measuring intervals became a mess. Well now I just decided not to play with the Load register at all. Here's the code that works for any future wanderer that might end up in this thread:

Code: [Select]
#include <os.h>

int main(void) {
volatile unsigned *value = (unsigned *)0x900C0004;
volatile unsigned *control = (unsigned *)0x900C0008;
int i;
*(volatile unsigned *)0x900B0018 &= ~(1 << 11);
*(volatile unsigned *)0x900C0080 = 0xA;
*control = 0b10100010;
unsigned start = *value;
for(i = 0; i < 10; ++i) {
printf("timer: %u\n", *value);
sleep(100);
}
printf("diff: %u\n", start - *value);
return 0;
}

Thanks again.
« Last Edit: March 14, 2012, 04:45:46 pm by hoffa »