+ DC_MOTOR_UNLOCK;
+}
+
+
+/*
+ * Check if the DC motor run time is expired, if this happend
+ * we turn off motor and reset status.
+ */
+INLINE bool check_timerIsExpired(int index)
+{
+
+ DC_MOTOR_LOCK;
+ bool check = ((dcm_all[index].expire_time - timer_clock()) < 0) &&
+ (dcm_all[index].expire_time != DC_MOTOR_NO_EXPIRE);
+ DC_MOTOR_UNLOCK;
+
+ return check;
+}
+
+/**
+ * Process to poll DC motor status.
+ * To use a Back-EMF technique (see brief for more details),
+ * we turn off a motor for CONFIG_DC_MOTOR_SAMPLE_DELAY, that value are stored
+ * in each DC motor config. For this implementation we assume
+ * that have a common CONFIG_DC_MOTOR_SAMPLE_DELAY, choose among a max delay
+ * to all DC motor configuration.
+ * The DC motor off time is choose to allow the out signal to
+ * be stable, so we can read and process this value for feedback controll loop.
+ * The period (CONFIG_DC_MOTOR_SAMPLE_PERIOD - CONFIG_DC_MOTOR_SAMPLE_DELAY)
+ * that every time we turn off a DC motor is choose to have a feedback controll
+ * more responsive or less responsive.
+ */
+static void NORETURN dc_motor_poll(void)
+{
+ for (;;)
+ {
+ /*
+ * For all DC motor we read and process output singal,
+ * and choose the max value to off time
+ */
+ for (int i = 0; i < CONFIG_NUM_DC_MOTOR; i++)
+ {
+ if (!dcm_all[i].cfg)
+ continue;
+
+ if (check_timerIsExpired(i))
+ dc_motor_stop(i);
+ else
+ dc_motor_do(i);
+
+ /*
+ * If we read speed from trimmer we update the target
+ * speed value when motor is running so we can make
+ * dc motor speed regulation.
+ */
+ if (dcm_all[i].cfg->speed_dev_id != DC_MOTOR_NO_DEV_SPEED)
+ dc_motor_setSpeed(i, dc_motor_readTargetSpeed(i));
+ }
+
+ //Wait for next sampling
+ timer_delay(CONFIG_DC_MOTOR_SAMPLE_PERIOD - CONFIG_DC_MOTOR_SAMPLE_DELAY);
+
+ for (int i = 0; i < CONFIG_NUM_DC_MOTOR; i++)
+ {
+ if (!dcm_all[i].cfg)
+ continue;
+
+ if (check_timerIsExpired(i))
+ dc_motor_stop(i);
+
+ DC_MOTOR_LOCK;
+ if (dcm_all[i].status & DC_MOTOR_ACTIVE)
+ {
+ DC_MOTOR_DISABLE(dcm_all[i].index);
+ pwm_enable(dcm_all[i].cfg->pwm_dev, false);
+ }
+ DC_MOTOR_UNLOCK;
+ }
+
+ //Wait some time to allow signal to stabilize before sampling
+ timer_delay(CONFIG_DC_MOTOR_SAMPLE_DELAY);
+ }