1#!/bin/bash -e
2
3# Initialize for step motor of sled:
4#   Enable pwm and setup pwm duty
5#   Setup gpio pins for step motor control
6#   Moving step motor back to initial position
7
8export PATH=$PATH:/usr/libexec
9
10# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
11source /usr/libexec/bletchley-common-functions
12
13PWM_CLASS_PATH="/sys/class/pwm/pwmchip0"
14#Sleld 1~6 using bmc pwm8~13 as motor driver stick
15PWM_NUM_OFFSET=7
16PWM_PERIOD=2500000 #400HZ
17PWM_DUTY=250000    #PWM_PERIOD X 10%
18CALIBRATE_TIMEOUT=120
19
20#Enable pwm for sledN
21function open_pwm() {
22    local SLED_NUM="$1"
23    echo "Open pwm of sled$SLED_NUM"
24    PWM_NUM=$(( SLED_NUM + PWM_NUM_OFFSET ))
25    PWM_PATH="${PWM_CLASS_PATH}/pwm${PWM_NUM}"
26    if [ ! -d "$PWM_PATH" ];then
27        echo "$PWM_NUM" > "${PWM_CLASS_PATH}/export"
28    fi
29    if [ -d "$PWM_PATH" ];then
30        echo "set pwm period to $PWM_PERIOD ns"
31        if ! echo "$PWM_PERIOD" > "${PWM_PATH}/period"; then
32            echo "Error: set pwm period fail"
33            return 1
34        fi
35
36        if ! echo 1 > "${PWM_PATH}/enable"; then
37            echo "Error: set pwm enable fail"
38            return 1
39        fi
40
41        if ! echo "$PWM_DUTY" > "${PWM_PATH}/duty_cycle"; then
42            echo "Error: set pwm duty_cycle fail"
43            return 1
44        fi
45    else
46        echo "Error: ${PWM_PATH} not exist, export pwm${PWM_NUM} fail"
47        return 1
48    fi
49}
50
51#Init gpio pins for step motor control
52function init_gpios() {
53    echo "Init GPIOs:"
54    motor_ctrl_gpio_pins_names=(    "SLED${1}_MD_STBY_RESET"
55                                    "SLED${1}_MD_IOEXP_EN_FAULT"
56                                    "SLED${1}_MD_DIR"
57                                    "SLED${1}_MD_DECAY"
58                                    "SLED${1}_MD_MODE1"
59                                    "SLED${1}_MD_MODE2"
60                                    "SLED${1}_MD_MODE3" )
61
62    for  gpio_name in "${motor_ctrl_gpio_pins_names[@]}"; do
63        set_gpio "$gpio_name"   0
64    done
65}
66
67if [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then
68  SLED=$1
69  SLED_NUM=${SLED:4}
70else
71  #show_usage
72  echo "invalid sled name: ${1}"
73  exit 1;
74fi
75
76#Check if sled is present
77if ! is_sled_present "${SLED_NUM}"; then
78    echo "${SLED} is not present, skip motor initialize"
79    exit 1
80fi
81
82#Init gpios
83init_gpios "$SLED_NUM"
84
85#enable pwm
86open_pwm "$SLED_NUM"
87
88#SLED{N}_MS_DETECT1  (initial position)
89DETECT_PIN1="SLED${SLED_NUM}_MS_DETECT1"
90INIT_POS=$(get_gpio "$DETECT_PIN1")
91
92if [ "$INIT_POS" -eq 1 ];then
93    time_count=0
94    echo "Making motor back to initial position..."
95    motor-ctrl "$SLED" r >/dev/null
96    while  [ "$INIT_POS" -eq 1 ]  ;do
97        INIT_POS=$(get_gpio "$DETECT_PIN1")
98        sleep 0.1
99        time_count=$(( time_count + 1 ))
100        if [  $time_count -gt $CALIBRATE_TIMEOUT ];then
101            echo "Error: Step motor run over 1 cycle but switch never triggered"
102            break
103        fi
104    done
105    motor-ctrl "$SLED" s >/dev/null
106fi
107
108if [ "$INIT_POS" -eq 0 ];then
109    echo "Motor calibrated to initial position."
110    exit 0
111else
112    echo "Find motor initial position failed"
113    exit 1
114fi
115