S3C2440 Development Board Practice (5): Timer Interrupt

Publisher:HeavenlyJoy444Latest update time:2022-07-18 Source: csdnKeywords:S3C2440 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Timer structure

Check the chip manual to find the following timer structure diagram

From left to right, analyze the picture: (not considered)


Prescaler: Timers 0 and 1 share an 8-bit divider, while timers 2, 3, and 4 share another 8-bit divider. The divider divides the input PCLK into: PCLK/(prescaler+1).


Clock divider & MUX: Each timer has a clock divider, which generates 5 different divided signals (1/2, 1/4, 1/8, 1/16, and TCLK). Each timer block receives its own clock signal from the clock divider, and the clock divider receives the clock from the corresponding 8-bit divider. The 8-bit divider is programmable and divides PCLK according to the loaded value, which is stored in the TCFG0 and TCFG1 registers. At this time, the clock frequency of the timer is: Timer input clock Frequency = PCLK / (prescaler+1) / divider value.


Control logic: The count buffer register (TCNTBn) has an initial value that is loaded into the down counter when the timer is enabled. The compare buffer register (TCMPBn) has an initial value that is loaded into the compare register to be compared with the value of the down counter. The double buffering feature of TCNTBn and TCMPBn enables the timer to produce a stable output when changing the frequency and duty cycle. Each timer has its own 16-bit down counter, which is counted by the Timer input clock Frequency. When the down counter reaches 0, a timer interrupt request is generated to notify the CPU of the timer interrupt. When an interrupt occurs, the value of TCNTBn is automatically loaded into the next counter to continue the next operation. The timer can be stopped by clearing the timer enable TCONn bit during the timer operation mode, and the value of TCNTBn will not be reloaded into the counter.


TOUTn & Dead Zone Generator: S3C2440 has five 16-bit timers. Timers 0, 1, 2, and 3 have pulse width modulation (PWM) functions. Timer 4 is only an internal timer without output pins. Timer 0 has a dead zone generator.

*PS: 


The compare buffer register (TCMPBn) is used for PWM generation

The timer reload operation occurs automatically when the down counter reaches 0.

prescaler value = 0~255; divider value = 2, 4, 8, 16 

The content about dead zone will be discussed in the following blog post (digging pit)

2. Timer interrupt program design

I. Initialize timer interrupt

This can be compared to the external interrupt operation (because it is also in irq mode), so the specific role of the duplicate register is mentioned in the previous blog S3C2440 Development Board Practice (4): External Interrupt.


Here, we set the ultimate goal to execute the timer interrupt function once every 0.5 seconds.


Setp 1: Turn on the interrupt master switch in CPSR 


mrs r0, cpsr

 

bic r0, r0, #0xf //Set the mode to user mode

bic r0, r0, (1<<7) //external interrupt IRQ open

 

msr cpsr, r0 //user mode

ldr sp, =0x33f00000 //Set the user stack (useless, just compare with the future interrupt address)

Step 2: INTERRUPT MASK (INTMSK) REGISTER 


void init_EINT(void)

{

    INTMSK &= ~(1<<10);

    // Interrupt corresponding to the button: TIMER -> The corresponding bit is: 10

}


Step 3: Configure timer interrupt parameters


Here we take PCLK=50Mhz as an example. For 2440, the clock configuration is very simple. For the specific clock settings, please refer to the second part of S3C2440 Development Board Practice (2): start.S Initial Understanding + SDRAM Configuration + Relocation.


According to the formula: Timer input clock Frequency = PCLK / (prescaler+1) / divider value. We set prescaler = 99; divider value = 16. It can be calculated that the frequency of the timer clock is 31250hz. So if you want to time 1s, the initial value of the counter should be 31250. According to this parameter configuration, set the timer register as follows:


1. TIMER CONFIGURATION REGISTER0 (TCFG0) 


Function: Configure two 8-bit prescalers and configure the dead zone length


Do: Set prescaler0 to 99


2. TIMER CONFIGURATION REGISTER1 (TCFG1) 


Function: 5-MUX & DMA mode selection


Do: Set the Clock divider corresponding to the Timer to 16 divisions


3. TIMER 0 COUNT BUFFER REGISTER (TCNTB0)


Function: Set the initial value of the down counter


Do: From the ultimate goal, we can see that the initial value is 31250 / 2 = 15625


4. TIMER CONTROL (TCON) REGISTER 


Function: Update the initial value of the down counter, set to automatic loading, start, output inverter


Do: Manually update the initial value of the down counter first, then set it to automatically load and start


* The manual update bit needs to be cleared to 0 after use (cleared to 0 before the next write)


In summary, the timer initialization configuration function is as follows:


void init_timer(void)

{ //TCLK = PCLK/(99+1)/16 = 31250

    TCFG0 = 0x63; //prescaler = 99

    TCFG1 &= ~(0xf<<0);

    TCFG1 |= (3<<0); //1/16 divider

    TCNTB0 = 15625; // count 15625 (0.5s)

 

    TCON |= (1<<1); // update TCNTB0 to coutor

    TCON &= ~(1<<1); // CLEAR

    TCON |= ((1<<0) | (1<<3)); // AUTO RELOAD & START

}

II. Interrupt entry function

Same as the previous article, let’s go straight to the code!


do_irq:

    /* Before executing here:

     * 1. lr_und holds the address of the next instruction to be executed in interrupt mode

     * 2. SPSR_irq saves the CPSR of the interrupt mode

     * 3. M4-M0 in CPSR is set to 10010, entering irq mode

     * 4. Jump to 0x18 to execute the program 

     */

 

    // 1. Protect the scene

    ldr sp, =0x33d00000

    /* r0-r12 may be modified in the irq exception handling function, so save it first*/

    /* lr-4 is the return address after exception handling, also need to be saved*/

    sub lr, lr, #4

    stmdb sp!, {r0-r12, lr}  

 

    // 2. Processing interrupt function

    bl Find_interrupt_source

 

    /* 3. Restore the scene*/

    ldmia sp!, {r0-r12, pc}^ /* ^ will restore the value of spsr to cpsr*/

III. Set up the timer interrupt function

In the previous article, we designed a function template for external interrupts, so we use it here to extend it. So the timer interrupt function


void Find_interrupt_source(void)

{

    puts("begin_interruptnr");

    int bit = INTOFFSET;

    if(bit == 10)

        timer_interrupt_fun();

    /* Clear interrupts: clear from the source*/

    SRCPND = (1<    INTPND = (1<}

 

void timer_interrupt_fun(void)

{

    led_control_NOT(4); 

}

 

void led_control_NOT(int val)

{

    unsigned int val_DATF = GPFDAT;

    if(val_DATF & (1 << val))

        GPFDAT &= ~(1 << val);

    else

        GPFDAT |= (1 << val);

}


After running, you can see a light flashing. This is the first time I have solved the bug by hand and achieved the goal directly. Although there are program modifications for external interrupts, isn't it exciting to do it yourself?


Keywords:S3C2440 Reference address:S3C2440 Development Board Practice (5): Timer Interrupt

Previous article:S3C2440 development board practice (6): network configuration + setting up NFS
Next article:S3C2440 development board practice (3): Compilation concept + LED lighting and flashing

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号