Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 22 additions & 30 deletions pysense-2/lib/pycoproc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
#!/usr/bin/env python
#
# Copyright (c) 2020, Pycom Limited.
#
# This software is licensed under the GNU GPL version 3 or any
# later version, with permitted additional terms. For more information
# see the Pycom Licence v1.0 document supplied with this file, or
# available at https://www.pycom.io/opensource/licensing
#

# See https://docs.pycom.io for more information regarding library specifics

from machine import Pin
from machine import I2C
import time
import pycom

__version__ = '0.0.3'
__version__ = '0.0.4'

""" PIC MCU wakeup reason types """
WAKE_REASON_ACCELEROMETER = 1
Expand Down Expand Up @@ -100,8 +112,7 @@ def __init__(self, i2c=None, sda='P22', scl='P21'):
self.poke_memory(ADCON1_ADDR, (0x06 << _ADCON1_ADCS_POSN))
# enable the pull-up on RA3
self.poke_memory(WPUA_ADDR, (1 << 3))
# make RC5 an input
self.set_bits_in_memory(TRISC_ADDR, 1 << 5)

# set RC6 and RC7 as outputs and enable power to the sensors and the GPS
self.mask_bits_in_memory(TRISC_ADDR, ~(1 << 6))
self.mask_bits_in_memory(TRISC_ADDR, ~(1 << 7))
Expand All @@ -111,7 +122,8 @@ def __init__(self, i2c=None, sda='P22', scl='P21'):
self.sensor_power()
self.sd_power()

if self.read_fw_version() < 6:
# for Pysense/Pytrack 2.0, the minimum firmware version is 15
if self.read_fw_version() < 15:
raise ValueError('Firmware out of date')


Expand Down Expand Up @@ -170,24 +182,6 @@ def mask_bits_in_memory(self, addr, mask):
def set_bits_in_memory(self, addr, bits):
self.magic_write_read(addr, _or=bits)

def get_wake_reason(self):
""" returns the wakeup reason, a value out of constants WAKE_REASON_* """
return self.peek_memory(WAKE_REASON_ADDR)

def get_sleep_remaining(self):
""" returns the remaining time from sleep, as an interrupt (wakeup source) might have triggered """
c3 = self.peek_memory(WAKE_REASON_ADDR + 3)
c2 = self.peek_memory(WAKE_REASON_ADDR + 2)
c1 = self.peek_memory(WAKE_REASON_ADDR + 1)
time_device_s = (c3 << 16) + (c2 << 8) + c1
# this time is from PIC internal oscilator, so it needs to be adjusted with the calibration value
try:
self.calibrate_rtc()
except Exception:
pass
time_s = int((time_device_s / self.clk_cal_factor) + 0.5) # 0.5 used for round
return time_s

def setup_sleep(self, time_s):
try:
self.calibrate_rtc()
Expand All @@ -200,21 +194,21 @@ def setup_sleep(self, time_s):

def go_to_sleep(self, gps=True):
# enable or disable back-up power to the GPS receiver
if gps:
self.set_bits_in_memory(PORTC_ADDR, 1 << 7)
else:
self.mask_bits_in_memory(PORTC_ADDR, ~(1 << 7))
self.gps_standby(gps)
self.sensor_power(False)
self.sd_power(False)

# disable the ADC
self.poke_memory(ADCON0_ADDR, 0)

if self.wake_int:
# Don't touch RA3, RA5 or RC1 so that interrupt wake-up works
self.poke_memory(ANSELA_ADDR, ~((1 << 3) | (1 << 5)))
self.poke_memory(ANSELA_ADDR, ~(1 << 3))
self.poke_memory(ANSELC_ADDR, ~((1 << 6) | (1 << 7) | (1 << 1)))
else:
# disable power to the accelerometer, and don't touch RA3 so that button wake-up works
self.poke_memory(ANSELA_ADDR, ~(1 << 3))
self.poke_memory(ANSELC_ADDR, ~(1 << 7))
self.poke_memory(ANSELC_ADDR, ~(0))

self.poke_memory(ANSELB_ADDR, 0xFF)

Expand All @@ -230,8 +224,6 @@ def go_to_sleep(self, gps=True):
self.set_bits_in_memory(INTCON_ADDR, 1 << 4) # enable interrupt; set INTE)

self._write(bytes([CMD_GO_SLEEP]), wait=False)
# kill the run pin
Pin('P3', mode=Pin.OUT, value=0)

def calibrate_rtc(self):
# the 1.024 factor is because the PIC LF operates at 31 KHz
Expand Down
56 changes: 22 additions & 34 deletions pytrack-2/lib/pycoproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import time
import pycom

__version__ = '0.0.3'
__version__ = '0.0.4'

""" PIC MCU wakeup reason types """
WAKE_REASON_ACCELEROMETER = 1
Expand Down Expand Up @@ -112,17 +112,18 @@ def __init__(self, i2c=None, sda='P22', scl='P21'):
self.poke_memory(ADCON1_ADDR, (0x06 << _ADCON1_ADCS_POSN))
# enable the pull-up on RA3
self.poke_memory(WPUA_ADDR, (1 << 3))
# make RC5 an input
self.set_bits_in_memory(TRISC_ADDR, 1 << 5)

# set RC6 and RC7 as outputs and enable power to the sensors and the GPS
self.mask_bits_in_memory(TRISC_ADDR, ~(1 << 6))
self.mask_bits_in_memory(TRISC_ADDR, ~(1 << 7))


self.gps_standby(False)
self.sensor_power()
self.sd_power()

if self.read_fw_version() < 6:
# for Pysense/Pytrack 2.0, the minimum firmware version is 15
if self.read_fw_version() < 15:
raise ValueError('Firmware out of date')


Expand Down Expand Up @@ -181,24 +182,6 @@ def mask_bits_in_memory(self, addr, mask):
def set_bits_in_memory(self, addr, bits):
self.magic_write_read(addr, _or=bits)

def get_wake_reason(self):
""" returns the wakeup reason, a value out of constants WAKE_REASON_* """
return self.peek_memory(WAKE_REASON_ADDR)

def get_sleep_remaining(self):
""" returns the remaining time from sleep, as an interrupt (wakeup source) might have triggered """
c3 = self.peek_memory(WAKE_REASON_ADDR + 3)
c2 = self.peek_memory(WAKE_REASON_ADDR + 2)
c1 = self.peek_memory(WAKE_REASON_ADDR + 1)
time_device_s = (c3 << 16) + (c2 << 8) + c1
# this time is from PIC internal oscilator, so it needs to be adjusted with the calibration value
try:
self.calibrate_rtc()
except Exception:
pass
time_s = int((time_device_s / self.clk_cal_factor) + 0.5) # 0.5 used for round
return time_s

def setup_sleep(self, time_s):
try:
self.calibrate_rtc()
Expand All @@ -210,22 +193,21 @@ def setup_sleep(self, time_s):
self._write(bytes([CMD_SETUP_SLEEP, time_s & 0xFF, (time_s >> 8) & 0xFF, (time_s >> 16) & 0xFF]))

def go_to_sleep(self, gps=True):
# enable or disable back-up power to the GPS receiver
if gps:
self.set_bits_in_memory(PORTC_ADDR, 1 << 7)
else:
self.mask_bits_in_memory(PORTC_ADDR, ~(1 << 7))
self.gps_standby(gps)
self.sensor_power(False)
self.sd_power(False)

# disable the ADC
self.poke_memory(ADCON0_ADDR, 0)

if self.wake_int:
# Don't touch RA3, RA5 or RC1 so that interrupt wake-up works
self.poke_memory(ANSELA_ADDR, ~((1 << 3) | (1 << 5)))
self.poke_memory(ANSELA_ADDR, ~(1 << 3))
self.poke_memory(ANSELC_ADDR, ~((1 << 6) | (1 << 7) | (1 << 1)))
else:
# disable power to the accelerometer, and don't touch RA3 so that button wake-up works
self.poke_memory(ANSELA_ADDR, ~(1 << 3))
self.poke_memory(ANSELC_ADDR, ~(1 << 7))
self.poke_memory(ANSELC_ADDR, ~(0))

self.poke_memory(ANSELB_ADDR, 0xFF)

Expand All @@ -241,8 +223,6 @@ def go_to_sleep(self, gps=True):
self.set_bits_in_memory(INTCON_ADDR, 1 << 4) # enable interrupt; set INTE)

self._write(bytes([CMD_GO_SLEEP]), wait=False)
# kill the run pin
Pin('P3', mode=Pin.OUT, value=0)

def calibrate_rtc(self):
# the 1.024 factor is because the PIC LF operates at 31 KHz
Expand Down Expand Up @@ -302,14 +282,22 @@ def setup_int_pin_wake_up(self, rising_edge = True):
self.wake_int_pin_rising_edge = rising_edge

def gps_standby(self, enabled=True):
# make RC4 an output
self.mask_bits_in_memory(TRISC_ADDR, ~(1 << 4))

if enabled:
# make RC4 input
self.set_bits_in_memory(TRISC_ADDR, 1 << 4)
else:
# make RC4 an output
self.mask_bits_in_memory(TRISC_ADDR, ~(1 << 4))
# drive RC4 high
self.set_bits_in_memory(PORTC_ADDR, 1 << 4)
time.sleep(0.2)
# drive RC4 low
self.mask_bits_in_memory(PORTC_ADDR, ~(1 << 4))
else:
time.sleep(0.2)
# drive RC4 high
self.set_bits_in_memory(PORTC_ADDR, 1 << 4)
time.sleep(0.2)

def sensor_power(self, enabled=True):
# make RC7 an output
Expand Down
16 changes: 15 additions & 1 deletion pytrack-2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
import time
import utime
import gc
import pycom
from machine import RTC
from machine import SD
from L76GNSS import L76GNSS
from pytrack import Pytrack

pycom.heartbeat(False)
pycom.rgbled(0x0A0A08) # white

time.sleep(2)
gc.enable()

Expand All @@ -32,13 +36,23 @@
print('Adjusted from UTC to EST timezone', utime.localtime(), '\n')

py = Pytrack()

time.sleep(1)
l76 = L76GNSS(py, timeout=30, buffer=512)

# sd = SD()
# os.mount(sd, '/sd')
# f = open('/sd/gps-record.txt', 'w')

while (True):
# while (True):
for _ in range(5):
coord = l76.coordinates()
#f.write("{} - {}\n".format(coord, rtc.now()))
print("{} - {} - {}".format(coord, rtc.now(), gc.mem_free()))

"""
# sleep procedure
time.sleep(3)
py.setup_sleep(10)
py.go_to_sleep()
"""