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    echo "Making motor back to initial position..."
94    motor-ctrl "$SLED" r >/dev/null
95    wait_gpio_falling "$DETECT_PIN1" "$CALIBRATE_TIMEOUT"
96    motor-ctrl "$SLED" s >/dev/null
97fi
98
99INIT_POS=$(get_gpio "$DETECT_PIN1")
100if [ "$INIT_POS" -eq 0 ];then
101    echo "Motor calibrated to initial position."
102    exit 0
103else
104    echo "Error: Step motor run over 1 cycle but switch never triggered"
105    echo "Find motor initial position failed"
106    exit 1
107fi
108