• I want to make use of timer properties beyond that which has been implemented in Espruino. Such things as counting external pulses, shaft encoder input, measure period and frequency, cascading timers for longer counts. The first step is to understand how the timers work and how Espruino uses them.
    First obtain the specification sheet from:
    http://www.espruino.com/datasheets/STM32­F103xC.pdf
    Second obtain the RM0008 Reference manual
    http://www.keil.com/dd/docs/datashts/st/­stm32f10xxx.pdf
    (The link on the Espruino site is broken.)

    From the reference manual memory map Table 3, the locations of the timers were obtained.
    0x4000 0C00 - 0x4000 0FFF TIM5 timer
    0x4000 0800 - 0x4000 0BFF TIM4 timer
    0x4000 0400 - 0x4000 07FF TIM3 timer
    0x4000 0000 - 0x4000 03FF TIM2 timer
    And define the starting addresses:

    var TIM5=0x40000C00;
    var TIM4=0x40000800;
    var TIM3=0x40000400;
    var TIM2=0x40000000;
    
    

    From the reference manual section 15.4 obtain the names and offsets of the timer registers:

    //Timer register offsets
    var CR1=0x0;
    var CR2=0x04;
    var SMCR=0x08;
    var DIER=0x0c;
    var SR=0x10;
    var ERRCW=0x14;
    var CCMR1=0x18;
    var CCMR2=0x1c;
    var CCER=0x20;
    var CNT=0x24;
    var PSC=0x28;
    var ARR=0x2c;
    var CCR1=0x34;
    var CCR2=0x38;
    var CCR3=0x3c;
    var CCR4=0x40;
    var DCR=0x48;
    var DMAR=0x4c;
    
    

    Set up an array containing the names and start addresses of the timers:

    var timers=[{name:"Timer 2",t:TIM2},
                {name:"Timer 3",t:TIM3},
                {name:"Timer 4",t:TIM4},
                {name:"Timer 5",t:TIM5}
               ];
    
    

    From the reference manual section 15.4.1 that defines the bits in control resister 1 we learn that bit 0 enables or disables a timer. Write a function that tests the timers:

    function isTimerEnabled(TIMx){
      var CR1x=peek32(TIMx.t+CR1);
      if(CR1x&1)console.log(TIMx.name+" Enabled");
      else console.log(TIMx.name+" Disabled");
      return CR1x & 1;
    }//end isTimerEnabled
    
    

    And write a function to display the registers of a timer

    function showTimer(TIMx,base){
    //display the timer registers in binary
    console.log("CR1="+peek32(TIMx+CR1).toSt­ring(base));
    console.log("CR2="+peek32(TIMx+CR2).toSt­ring(base));
    console.log("SMCR="+peek32(TIMx+SMCR).to­String(base));
    console.log("DIER="+peek32(TIMx+DIER).to­String(base));
    console.log("SR="+peek32(TIMx+SR).toStri­ng(base));
    console.log("ERRCW="+peek32(TIMx+ERRCW).­toString(base));
    console.log("CCMR1="+peek32(TIMx+CCMR1).­toString(base));
    console.log("CCMR2="+peek32(TIMx+CCMR2).­toString(base));
    
    console.log("CCER="+peek32(TIMx+CCER).to­String(base));
    console.log("CNT="+peek32(TIMx+CNT).toSt­ring(base));
    console.log("PSC="+peek32(TIMx+PSC).toSt­ring(base));
    console.log("ARR="+peek32(TIMx+ARR).toSt­ring(base));
    console.log("CCR1="+peek32(TIMx+CCR1).to­String(base));
    console.log("CCR2="+peek32(TIMx+CCR2).to­String(base));
    console.log("CCR3="+peek32(TIMx+CCR3).to­String(base));
    console.log("CCR4="+peek32(TIMx+CCR4).to­String(base));
    console.log("DCR="+peek32(TIMx+DCR).toSt­ring(base));
    console.log("DMAR="+peek32(TIMx+DMAR).to­String(base));
    }//end showTimer
    
    

    And finally loop through the timer list

    for(var i=0; i in timers;i++){
     if(isTimerEnabled(timers[i])) 
      showTimer(timers[i].t,16);
    }//next i
    analogWrite(A0,0.1,{ freq : 10 }); 
    for(var i=0; i in timers;i++){
     if(isTimerEnabled(timers[i])) 
      showTimer(timers[i].t,16);
    }//next i
    analogWrite(A0,0.5,{ freq : 10 }); // change duty cycle
    for(var i=0; i in timers;i++){
     if(isTimerEnabled(timers[i])) 
      showTimer(timers[i].t,16);
    }//next i
    
    

    Try it with other analogWrite pins


    1 Attachment

About