summaryrefslogtreecommitdiff
path: root/recipes-bsp/multitech/mts-io/mts-io.mtcap.init
blob: 7b444a1204ba3b9b401e6d87ced6889f330ebe7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash

. /etc/default/mts-io

sysdir=/sys/devices/platform/mts-io

i2c=/sys/bus/i2c/devices/

SYSFS="/usr/sbin/mts-io-sysfs"

GPSCONFIGTYPE="/var/run/config/gpstype"


set_hw_name() {
   hw=$(${SYSFS} show hw-version)
   hw_name=(${hw//-/ })
}

set_batt_charge() {
  if [[ $batcap == 1 ]] ; then
    i2cset -m 0x60 -y 0 0x09 0x01 0x20
    i2cset -m 0x03 -y 0 0x09 0x02 0x02
    i2cset -m 0x0c -y 0 0x09 0x02 0x0c
  fi
}

set_gpslink() {
    [[ -d /var/run/config ]] || mkdir /var/run/config
    gpscap=$(cat ${sysdir}/capability/gps)

    # hw_name must be mtcap, and gpscap == 1
    if ! [[ ${hw_name} =~ ^MTCAP$ ]] || ((gpscap == 0)) ; then
        return
    fi
    ln -sf /dev/ttyS1 /dev/gps0
    echo "u-blox" >"$GPSCONFIGTYPE"
}

setwificap() {
  WIFICAP=0
  if  [[ -f ${sysdir}/capability/wifi ]] && (($(cat ${sysdir}/capability/wifi) == 1)) ; then
    WIFICAP=1
  fi
}

lora_init() {
  # reset lora chip
  mts-io-sysfs store lora/reset 1
  mts-io-sysfs store lora/reset 0
  usleep 100000
  mts-io-sysfs store lora/reset 1
}

eth_init() {
  # reset eth phy
  mts-io-sysfs store eth-reset 1
  mts-io-sysfs store eth-reset 0
  usleep 100000
  mts-io-sysfs store eth-reset 1
}

# Normal state when powered up
wifi_init1() {
  (($WIFICAP)) || return 0
  # reset wlan
  mts-io-sysfs store wlan-rst 0
  # disable wlan
  mts-io-sysfs store wlan-en 0
}
  
wifi_init2() {
  (($WIFICAP)) || return 0
  mts-io-sysfs store wlan-en 1
  usleep 30000  # 30mS according to WILC1000 spec.
  mts-io-sysfs store wlan-rst 1
}

GNSS_RESET=${sysdir}/gnss-reset
gnss_init() {
  ((GPSGNSSRESET)) || return 0
  USLPTIME=60000
  if [[ -f $GNSS_RESET ]] ; then
    echo 0 >$GNSS_RESET
    usleep $USLPTIME
    echo 1 >$GNSS_RESET
  fi
}


cell_init() {
  # remove /dev/modem_at[0,1] symlinks
  rm -f /dev/modem_at[0,1]

  if [ -w /sys/devices/platform/mts-io/radio-power ] ; then
    # power down the cellular chip gracefully
    mts-io-sysfs store radio-power 0
    # power up the cellular chip
    mts-io-sysfs store radio-power 1
  fi
}

start_lora_led_updater() {
  lora-led-updater &
}
gettime() {
  [[ $(cat /proc/uptime) =~ ([^[:space:]]+) ]]
  echo ${BASH_REMATCH[1]}
}
wait_for_supercap() {
  if ! [[ -f ${sysdir}/capability/supercap ]] ; then
      # Driver not loaded, maybe EEPROM not set
      return
  fi
  supercap=$(mts-io-sysfs show capability/supercap 2>/dev/null)
  if ((supercap != 1)) ; then
      return
  fi
  if ((SUPERCAPFULL != 1)) ; then
     return
  fi
  t0=$(gettime)
  maxwait=$(awk "BEGIN {print ${t0}+${SUPERCAPFULL_MAXWAIT}}")
  while : ; do
    if (($(mts-io-sysfs show power-fail) == 1)) ; then
      # If we are booting, then go down.
      # This will not work with systemd.
      if [[ $(ps -h -o cmd -p $PPID) =~ /bin/sh[[:space:]]/etc/init.d/rc[[:space:]]S$ ]] ; then
          logger -s -t 'mts-io' -p daemon.err "No power during boot, so power off"
          exec /sbin/poweroff
          # NOT REACHED
      fi
    fi
    if (($(mts-io-sysfs show supercap-full) == 1)) ; then
      logger -s -t 'mts-io' -p daemon.err 'Supercap Charged'
      return
    fi
    t1=$(gettime)
    if awk "BEGIN { if($t1<$maxwait)exit 1 }" ; then
	break
    fi
    sleep 1
  done
  logger -s -t 'mts-io' -p daemon.err 'Supercap not full but timer expired -- continuing boot'
}

case $1 in
  start)
    # SPI driver for LoRa
    modprobe spidev
    # Point the firmware API at our i2c EEPROMs
    echo -n ${i2c} > /sys/module/firmware_class/parameters/path
    echo "Loading mts-io module"
    modprobe -r atmel_mci
    # Point the firmware API at our i2c EEPROMs
    echo -n ${i2c} > /sys/module/firmware_class/parameters/path
    modprobe mts_io
    batcap=$(mts-io-sysfs show capability/battery)
    setwificap
    set_batt_charge
    wifi_init1
    lora_init
    eth_init
    cell_init &
    start_lora_led_updater
    wifi_init2
    gnss_init
    modprobe atmel_mci
    (($WIFICAP)) && modprobe wilc1000
    (($WIFICAP)) && modprobe wilc1000-sdio
    set_hw_name
    set_gpslink
    wait_for_supercap

    ;;

  stop)
    echo "Unloading mts-io module"
    wifi_init1  # Power down wifi
    modprobe -r wilc1000 >/dev/null 2>&1
    modprobe -r wilc1000-sdio >/dev/null 2>&1
    modprobe -r mts_io >/dev/null 2>&1
    ;;

  *)
    echo "Usage: $0 {start|stop}"
    exit 2
    ;;
esac