14a0948d0SAllen.Wang#!/bin/bash -e
24a0948d0SAllen.Wang
34a0948d0SAllen.Wang# Initialize for step motor of sled:
44a0948d0SAllen.Wang#   Enable pwm and setup pwm duty
54a0948d0SAllen.Wang#   Setup gpio pins for step motor control
64a0948d0SAllen.Wang#   Moving step motor back to initial position
74a0948d0SAllen.Wang
84a0948d0SAllen.Wangexport PATH=$PATH:/usr/libexec
94a0948d0SAllen.Wang
10501f4c78SPotin Lai# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
11501f4c78SPotin Laisource /usr/libexec/bletchley-common-functions
12501f4c78SPotin Lai
136af0dff7SAllen.WangPWM_CLASS_PATH="/sys/class/pwm/pwmchip0"
145ff992efSAllen.Wang#Sleld 1~6 using bmc pwm8~13 as motor driver stick
155ff992efSAllen.WangPWM_NUM_OFFSET=7
166af0dff7SAllen.WangPWM_PERIOD=2500000 #400HZ
176af0dff7SAllen.WangPWM_DUTY=250000    #PWM_PERIOD X 10%
184a0948d0SAllen.WangCALIBRATE_TIMEOUT=120
194a0948d0SAllen.Wang
204a0948d0SAllen.Wang#Enable pwm for sledN
214a0948d0SAllen.Wangfunction open_pwm() {
224a0948d0SAllen.Wang    local SLED_NUM="$1"
234a0948d0SAllen.Wang    echo "Open pwm of sled$SLED_NUM"
246af0dff7SAllen.Wang    PWM_NUM=$(( SLED_NUM + PWM_NUM_OFFSET ))
256af0dff7SAllen.Wang    PWM_PATH="${PWM_CLASS_PATH}/pwm${PWM_NUM}"
266af0dff7SAllen.Wang    if [ ! -d "$PWM_PATH" ];then
276af0dff7SAllen.Wang        echo "$PWM_NUM" > "${PWM_CLASS_PATH}/export"
286af0dff7SAllen.Wang    fi
296af0dff7SAllen.Wang    if [ -d "$PWM_PATH" ];then
306af0dff7SAllen.Wang        echo "set pwm period to $PWM_PERIOD ns"
316af0dff7SAllen.Wang        if ! echo "$PWM_PERIOD" > "${PWM_PATH}/period"; then
326af0dff7SAllen.Wang            echo "Error: set pwm period fail"
336af0dff7SAllen.Wang            return 1
344a0948d0SAllen.Wang        fi
354a0948d0SAllen.Wang
366af0dff7SAllen.Wang        if ! echo 1 > "${PWM_PATH}/enable"; then
376af0dff7SAllen.Wang            echo "Error: set pwm enable fail"
386af0dff7SAllen.Wang            return 1
396af0dff7SAllen.Wang        fi
406af0dff7SAllen.Wang
416af0dff7SAllen.Wang        if ! echo "$PWM_DUTY" > "${PWM_PATH}/duty_cycle"; then
426af0dff7SAllen.Wang            echo "Error: set pwm duty_cycle fail"
436af0dff7SAllen.Wang            return 1
446af0dff7SAllen.Wang        fi
456af0dff7SAllen.Wang    else
466af0dff7SAllen.Wang        echo "Error: ${PWM_PATH} not exist, export pwm${PWM_NUM} fail"
476af0dff7SAllen.Wang        return 1
486af0dff7SAllen.Wang    fi
494a0948d0SAllen.Wang}
504a0948d0SAllen.Wang
514a0948d0SAllen.Wang#Init gpio pins for step motor control
524a0948d0SAllen.Wangfunction init_gpios() {
534a0948d0SAllen.Wang    echo "Init GPIOs:"
544a0948d0SAllen.Wang    motor_ctrl_gpio_pins_names=(    "SLED${1}_MD_STBY_RESET"
554a0948d0SAllen.Wang                                    "SLED${1}_MD_IOEXP_EN_FAULT"
564a0948d0SAllen.Wang                                    "SLED${1}_MD_DIR"
574a0948d0SAllen.Wang                                    "SLED${1}_MD_DECAY"
584a0948d0SAllen.Wang                                    "SLED${1}_MD_MODE1"
594a0948d0SAllen.Wang                                    "SLED${1}_MD_MODE2"
604a0948d0SAllen.Wang                                    "SLED${1}_MD_MODE3" )
614a0948d0SAllen.Wang
624a0948d0SAllen.Wang    for  gpio_name in "${motor_ctrl_gpio_pins_names[@]}"; do
634a0948d0SAllen.Wang        set_gpio "$gpio_name"   0
644a0948d0SAllen.Wang    done
654a0948d0SAllen.Wang}
664a0948d0SAllen.Wang
675ff992efSAllen.Wangif [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then
684a0948d0SAllen.Wang  SLED=$1
694a0948d0SAllen.Wang  SLED_NUM=${SLED:4}
704a0948d0SAllen.Wangelse
714a0948d0SAllen.Wang  #show_usage
724a0948d0SAllen.Wang  echo "invalid sled name: ${1}"
734a0948d0SAllen.Wang  exit 1;
744a0948d0SAllen.Wangfi
754a0948d0SAllen.Wang
764a0948d0SAllen.Wang#Check if sled is present
77501f4c78SPotin Laiif ! is_sled_present "${SLED_NUM}"; then
784a0948d0SAllen.Wang    echo "${SLED} is not present, skip motor initialize"
794a0948d0SAllen.Wang    exit 1
804a0948d0SAllen.Wangfi
814a0948d0SAllen.Wang
824a0948d0SAllen.Wang#Init gpios
834a0948d0SAllen.Wanginit_gpios "$SLED_NUM"
844a0948d0SAllen.Wang
854a0948d0SAllen.Wang#enable pwm
864a0948d0SAllen.Wangopen_pwm "$SLED_NUM"
874a0948d0SAllen.Wang
884a0948d0SAllen.Wang#SLED{N}_MS_DETECT1  (initial position)
894a0948d0SAllen.WangDETECT_PIN1="SLED${SLED_NUM}_MS_DETECT1"
904a0948d0SAllen.WangINIT_POS=$(get_gpio "$DETECT_PIN1")
914a0948d0SAllen.Wang
924a0948d0SAllen.Wangif [ "$INIT_POS" -eq 1 ];then
934a0948d0SAllen.Wang    echo "Making motor back to initial position..."
944a0948d0SAllen.Wang    motor-ctrl "$SLED" r >/dev/null
95*d2e7f202SPotin Lai    wait_gpio_falling "$DETECT_PIN1" "$CALIBRATE_TIMEOUT"
964a0948d0SAllen.Wang    motor-ctrl "$SLED" s >/dev/null
974a0948d0SAllen.Wangfi
984a0948d0SAllen.Wang
99*d2e7f202SPotin LaiINIT_POS=$(get_gpio "$DETECT_PIN1")
1004a0948d0SAllen.Wangif [ "$INIT_POS" -eq 0 ];then
1014a0948d0SAllen.Wang    echo "Motor calibrated to initial position."
1024a0948d0SAllen.Wang    exit 0
1034a0948d0SAllen.Wangelse
104*d2e7f202SPotin Lai    echo "Error: Step motor run over 1 cycle but switch never triggered"
1054a0948d0SAllen.Wang    echo "Find motor initial position failed"
1064a0948d0SAllen.Wang    exit 1
1074a0948d0SAllen.Wangfi
108