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