This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123GH6PM: Trouble Understanding Setting Clock

Part Number: TM4C123GH6PM

Hi All,

This is my first time posting in these forums so I apologize if this is a bad post. In short, I'm having trouble understanding and picking a clock rate for my application. I want to use timers to sample the adc at 44100hz (44.1KHz cd audio rate) and to store into an SD card, and then read later to a DAC. I've spent hours online trying to find an explanation, but there isn't much of a good answer for me to completely understand it, I guess. I'm using Keil v5 and using the driverlib.

For example, in this code I'm running, there is this command

ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

There is a comment saying that this runs the clock directly from the crystal, but I don't understand how this code does that? And when they say crystal, do they mean the main oscillator? I'd like to have the most precise timing possible to ensure fidelity and that the frequency of my audio remains stable.

Bonus points if some one can tell me the difference between using the commands with/without the "ROM_" prefix.

Thanks!

  • Hello Joseph,

    Thanks for providing a lot of detail about what you are trying to achieve. I'll address your questions first and then loopback to offer advice on how to get the ADC results you want.

    Joseph Sarni21 said:

    There is a comment saying that this runs the clock directly from the crystal, but I don't understand how this code does that?

    The code referenced is to set the System Clocks for the entire device. All the peripherals, including the ADC, will be based of the system clock settings. So you need to start with setting the system clock first and then you can handle the peripheral clocks subsequently.

    Joseph Sarni21 said:

    And when they say crystal, do they mean the main oscillator? I'd like to have the most precise timing possible to ensure fidelity and that the frequency of my audio remains stable.

    Yes, the main crystal oscillator feeding into the TM4C. That is what is typically used to give the System Clock a high level of precision provided the crystal is chosen correctly (for tolerances etc.) and has a good layout.

    Joseph Sarni21 said:

    Bonus points if some one can tell me the difference between using the commands with/without the "ROM_" prefix.

    The difference is whether or not the TivaWare ROM API is used. TM4C devices have an older version of TivaWare included in ROM, and many functions have been unchanged, so the ROM versions can be used to save on code space.

    The best method to take advantage of this feature though, is to use the "MAP_" prefix which automatically handles knowing if the ROM API can be used or if a newer Flash based API must be called instead!

    Alright now then, onto the ADC!

    The way the ADC peripheral is setup, there isn't a specific clock being fed into it for the purposes of sampling rate. Rather, the ADC has the ability to be set where it triggers samples based on a Timer. The Timer is then configured to interrupt at a specific interval to allow the ADC to sample at specific data rates.

    As a rough example of what this would look like, you could reference this E2E post which has some Timer code included. Please read the replies as well as the original code posted had some errors you wouldn't want to repeat!: e2e.ti.com/.../506504

  • Hi Ralph, thank you for reaching out and helping me.

    1. After I had made this post, I managed to learn a little bit more about this command and how the clock works - I'm mostly just confused on the XTAL part. For example, I finally got the clock to respond and return a value for me in the debugger that I could analyze. Why would I need XTAL when I'm driving it from the main osc? Isn't that an internal oscillator, not external? I would think that since XTAL stands for external, it would be referencing an oscillator that's not already on the board. For example, the code I'm running is

    SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

    This uses the main oscillator to drive the PLL, and then divides the PLL value to make the clock run at 80MHz, and my entire system would run off this clock, correct?
    I was using this line of code, but changing the XTAL value around and it gave me different rates of blinking leds (blinking from the timers). I used XTAL 4Mhz and XTAL 25MHz, as these were the only 2 I was able to get a response at all with. When I set XTAL to 25Mhz, the leds blinked significantly slower, and when I set it to 4MHz, they blinked significantly faster. I think this is because how I said earlier where this command tells the PLL what frequency osc is driving it. Since I'm using the main osc, I should set it to 16MHz., but if I was using another external oscillator (say, one that's not on the board), I would used the respective frequency in the XTAL value.

    2. Thank you for the ROM_ clarification, I couldn't find much about it online! I will definitely try using the MAP_ prefix.

    3. That ADC post is amazing, thank you for linking it, I never found it in my searches. I was concerned about the FIFO with the adc sampling, but it doesn't actually record the sample unless triggered to.

  • Joseph Sarni21 said:
    Why would I need XTAL when I'm driving it from the main osc? Isn't that an internal oscillator, not external?

    No, that would be the PIOSC (Precision Internal Oscillator)

    Joseph Sarni21 said:
    I would think that since XTAL stands for external,

    XTAL is an abbreviation for crystal

    Joseph Sarni21 said:
    I was using this line of code, but changing the XTAL value around and it gave me different rates of blinking leds (blinking from the timers). I used XTAL 4Mhz and XTAL 25MHz, as these were the only 2 I was able to get a response at all with.

    This value must match the actual crystal you are using.

    Robert

  • So if I wanted to use PIOSC, I wouldn't need the XTAL statement, correct?
  • Correct, but you would need to include the appropriate define for the PIOSC. Keep in mind that the PIOSC in less accurate and less stable than an external crystal.

    I think you can still use the PIOSC and the PLL but you would need to check the documentation.

    Robert