Page 1 of 1

Problem with chThdSleep

Posted: Wed Aug 28, 2013 2:53 pm
by Stomper85
Hello,
first of all I want to describe my Problem.
I run two threads, one makes a LED blinking, the other should make a sound through a piezo buzzer (buzzer has no electronic).

Here is my code:

board.h

Code: Select all

.
.
.
/*
 * MCU type, supported types are defined in ./os/hal/platforms/hal_lld.h.
 */
#define STM32F10X_MD

/*
 * IO pins assignments.
 */
#define GPIOA_XBEERESET         4
#define GPIOA_LED1              12

#define GPIOB_LED2              4
#define GPIOB_LED3              5
#define GPIOB_BUZZER            12


#define VAL_GPIOACRL            0xB4B34BB4      /*  PA7...PA0 */
#define VAL_GPIOACRH            0x11113333      /* PA15...PA8 */
#define VAL_GPIOAODR            0b0000000000010000

/*
 * Port B setup.

 * PB5  - Push pull output  (LED3). Low 6th Bit set 1
.
.
.
 * PB12 - Push pull output  (Buzzer)High 5th Bit set 1

 */
#define VAL_GPIOBCRL            0x44111111      /*  PB7...PB0 */
#define VAL_GPIOBCRH            0xB4B14444      /* PB15...PB8 */
#define VAL_GPIOBODR            0b0000000000000000


main.c

Code: Select all

#include "ch.h"
#include "hal.h"
#include "test.h"


static WORKING_AREA(waThBuzzer, 64);
static WORKING_AREA(waLED1, 64);

static msg_t thBuzzer(void *arg) {
  (void)arg;
  chRegSetThreadName("Buzzer");
  while (TRUE)
  {
    palClearPad(GPIOB, GPIOB_BUZZER);
    chThdSleepMicroseconds(250);
    palSetPad(GPIOB, GPIOB_BUZZER);
    chThdSleepMicroseconds(250);
  }
  return (msg_t)0;
}


static msg_t thLED1(void *arg) {
  (void)arg;
  chRegSetThreadName("LED1");
  while (TRUE)
  {
    palClearPad(GPIOB, GPIOB_LED3);
    chThdSleepMilliseconds(25);
    palSetPad(GPIOB, GPIOB_LED3);
    chThdSleepMilliseconds(25);
  }
  return (msg_t)0;
}





/*
 * Application entry point.
 */
int main(int argc, char *argv[]) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();



  chThdCreateStatic(waThBuzzer, sizeof(waThBuzzer), NORMALPRIO, thBuzzer, NULL);
  chThdCreateStatic(waLED1, sizeof(waLED1), NORMALPRIO, thLED1, NULL);
  /
*
   * Normal main() thread activity, in this demo it does nothing except
   * sleeping in a loop.
   */
  while (TRUE) {
    chThdSleepMilliseconds(500);
  }
}



The Problem is, that the Buzzer Pin (PB12) does not output the expected 2 kHz (T = 2x250 us). But it outputs exactly 500 Hz.
The other Thread with the LED is doing what I expect, it pulses the LED with a Frequency of 20 Hz (T = 2x25 ms).

What I am doing wrong?!

Best regards
Stomper

Re: Problem with chThdSleep

Posted: Wed Aug 28, 2013 2:58 pm
by Giovanni
Hi,

You should consider the resolution of the system tick, the default is 1ms. All timings are approximated to the next 1ms boundary so your 250us all become 1ms.

You may try to change CH_FREQUENCY from 1000 to 4000 in chconf.h.

Giovanni

Re: Problem with chThdSleep

Posted: Wed Aug 28, 2013 5:40 pm
by Stomper85
Great,

that works!

Thanks Giovanni!