diff --git a/radio/radio.py b/radio/radio.py new file mode 100755 index 0000000..e3b513c --- /dev/null +++ b/radio/radio.py @@ -0,0 +1,28 @@ +''' +Simple class for a two way half duplex radio link. One end of the link is the master device, +meaning the only end which sends unsolicited messages. The slave only transmits in response to +such a message. Modify RadioSetup to match your hardware, channel and payload size (in bytes, 1-32). +master = TwoWayRadio(True, **RadioSetup) +slave = TwoWayRadio(False, **RadioSetup) +''' +import pyb +from nrf24l01 import NRF24L01, POWER_3, SPEED_250K + +RadioSetup = { "channel": 100, "payload_size": 8, "spi_no": 1, "csn_pin": 'X5', "ce_pin": 'Y11'} + +class TwoWayRadio(NRF24L01): + pipes = (b'\xf0\xf0\xf0\xf0\xe1', b'\xf0\xf0\xf0\xf0\xd2') + def __init__(self, master, channel, payload_size, spi_no, csn_pin, ce_pin): + super().__init__(pyb.SPI(spi_no), pyb.Pin(csn_pin), pyb.Pin(ce_pin),channel = channel,payload_size = payload_size) + if master: + self.open_tx_pipe(TwoWayRadio.pipes[0]) + self.open_rx_pipe(1, TwoWayRadio.pipes[1]) + else: + self.open_tx_pipe(TwoWayRadio.pipes[1]) + self.open_rx_pipe(1, TwoWayRadio.pipes[0]) + self.set_power_speed(POWER_3, SPEED_250K) # Best range for point to point links + self.start_listening() + + def start_listening(self): + super().start_listening() + pyb.delay(1) # Seems to improve reliability diff --git a/watchdog/wdog.py b/watchdog/wdog.py new file mode 100644 index 0000000..f82e19c --- /dev/null +++ b/watchdog/wdog.py @@ -0,0 +1,32 @@ +# Class for pybord watchdog timer +import stm, pyb + +@micropython.asm_thumb +def clz(r0): + clz(r0, r0) # return no. of leading zeros in passed integer + +class wdog(object): + def start(self, ms): + assert ms <= 32768 and ms >= 1, "Time value must be from 1 to 32768mS" + prescaler = 23 - clz(ms -1) + div_value = ((ms << 3) -1) >> prescaler + stm.mem16[stm.IWDG + stm.IWDG_KR] = 0x5555 + stm.mem16[stm.IWDG + stm.IWDG_PR] = (stm.mem16[stm.IWDG + stm.IWDG_PR] & 0xfff8) | prescaler + stm.mem16[stm.IWDG + stm.IWDG_RLR] = (stm.mem16[stm.IWDG + stm.IWDG_RLR] & 0xf000) | div_value + stm.mem16[stm.IWDG + stm.IWDG_KR] = 0xcccc + def feed(self): + stm.mem16[stm.IWDG + stm.IWDG_KR] = 0xaaaa + +def test(): + led = pyb.LED(2) + led1 = pyb.LED(3) + dog = wdog() + dog.start(1000) + for x in range(10): + led.toggle() + pyb.delay(500) + dog.feed() + dog.start(4000) + for x in range(20): + led1.toggle() + pyb.delay(500)