--[[
ģƣӲŹ
ģ鹦ܣ֧ӲŹ
ģ޸ʱ䣺2017.02.16
ĵο doc\СGPSλĵ\Watchdog descritption.doc
]]

module(...,package.seeall)

--ģ鸴λƬ
local RST_SCMWD_PIN = pio.P0_30
--ģ͵Ƭ໥ι
local WATCHDOG_PIN = pio.P0_31

--scm_activeƬǷtrueʾfalsenilʾ쳣
--get_scm_cntⵥƬģιǷʣ
local scm_active,get_scm_cnt = true,20
--testcntιԹѾιĴ
--testingǷι
local testcnt,testing = 0

--[[
getscm
  ȡ"Ƭģι"ƽ
  
		tag"normal"ʾι"test"ʾι
ֵ
]]
local function getscm(tag)
	--ڽιԣι
	if tag=="normal" and testing then return end
	--ʣһ
	get_scm_cnt = get_scm_cnt - 1
	--ιԣͣι
	if tag=="test" then
		sys.timer_stop(getscm,"normal")
	end
	--ʣΪ0
	if get_scm_cnt > 0 then
		--ι
		if tag=="test" then
			--⵽ߵƽ
			if pio.pin.getval(WATCHDOG_PIN) == 1 then				
				testcnt = testcnt+1
				--û3ι100󣬼´ι
				if testcnt<3 then
					sys.timer_start(feed,100,"test")
					get_scm_cnt = 20
					return
				--ιԽ3ιƬḴλģ
				else
					testing = nil
				end
			end
		end
		--100֮ż
		sys.timer_start(getscm,100,tag)
	--
	else
		get_scm_cnt = 20
		if tag=="test" then
			testing = nil
		end
		--ι  Ƭ쳣
		if tag=="normal" and not scm_active then
			--λƬ
			pio.pin.setval(0,RST_SCMWD_PIN)
			sys.timer_start(pio.pin.setval,100,1,RST_SCMWD_PIN)
			print("wdt reset 153b")
			scm_active = true
		end
	end
	--⵽͵ƽʾƬ
	if pio.pin.getval(WATCHDOG_PIN) == 0 and not scm_active then
		scm_active = true
		print("wdt scm_active = true")
	end
end

--[[
feedend
  "Ƭģι"Ƿ
  
		tag"normal"ʾι"test"ʾι
ֵ
]]
local function feedend(tag)
	--ڽιԣι
	if tag=="normal" and testing then return end
	--໥ιΪ
	pio.pin.close(WATCHDOG_PIN)
	pio.pin.setdir(pio.INPUT,WATCHDOG_PIN)
	print("wdt feedend",tag)
	--ιԣͣι
	if tag=="test" then
		sys.timer_stop(getscm,"normal")
	end
	--100ȥһιŵƽ
	--ÿ100ȥһΣ20ΣֻҪһζ͵ƽΪ"Ƭģι"
	sys.timer_start(getscm,100,tag)
end

--[[
feed
  ģ鿪ʼԵƬι
  
		tag"normal"ʾι"test"ʾι
ֵ
]]
function feed(tag)
	--ڽιԣι
	if tag=="normal" and testing then return end
	--Ƭ  ڽι
	if scm_active or tag=="test" then
		scm_active = false
	end

	--໥ιΪ"ģ鿪ʼԵƬι"2ĵ͵ƽ
	pio.pin.close(WATCHDOG_PIN)
	pio.pin.setdir(pio.OUTPUT,WATCHDOG_PIN)
	pio.pin.setval(0,WATCHDOG_PIN)
	print("wdt feed",tag)
	--2´ι
	sys.timer_start(feed,120000,"normal")
	--ιԣͣι
	if tag=="test" then
		sys.timer_stop(feedend,"normal")
	end
	--2ʼ"Ƭģι"Ƿ
	sys.timer_start(feedend,2000,tag)
end

--[[
open
  򿪿ϵӲŹܣι
  
ֵ
]]
function open()
	pio.pin.setdir(pio.OUTPUT,WATCHDOG_PIN)
	pio.pin.setval(1,WATCHDOG_PIN)
	feed("normal")
end

--[[
close
  رտϵӲŹ
  
ֵ
]]
function close()
	sys.timer_stop_all(feedend)
	sys.timer_stop_all(feed)
	sys.timer_stop_all(getscm)
	sys.timer_stop(pio.pin.setval,1,RST_SCMWD_PIN)
	pio.pin.close(RST_SCMWD_PIN)
	pio.pin.close(WATCHDOG_PIN)
	scm_active,get_scm_cnt,testcnt,testing = true,20,0
end

--[[
test
  ԡϵӲŹλAirģ顱Ĺ
  
ֵ
]]
function test()
	if not testing then
		testcnt,testing = 0,true
		feed("test")
	end
end


--[[
begin
  ι
  
ֵ
]]
local function begin()
	--ģ鸴λƬţĬߵƽ
	pio.pin.setdir(pio.OUTPUT1,RST_SCMWD_PIN)
	pio.pin.setval(1,RST_SCMWD_PIN)
	open()
end

--[[
setup
  ιʹõ
  
		rstģ鸴λƬ
		wdģ͵Ƭ໥ι
ֵ
]]
function setup(rst,wd)
	RST_SCMWD_PIN,WATCHDOG_PIN = rst,wd
	sys.timer_stop(begin)
	begin()
end

sys.timer_start(begin,2000)
