def _cleanup(self): while sysfs_interface.get_state() != "idle": try: sysfs_interface.set_stop() except Exception as e: print(e) try: sysfs_interface.wait_for_state("idle", 3.0) except SysfsInterfaceException: logger.warning("CleanupRoutine - send stop-command and waiting for PRU to go to idle") if self.shared_mem is not None: self.shared_mem.__exit__() if self.rpmsg_fd is not None: os.close(self.rpmsg_fd) try: self.set_ldo_voltage(False) self.ldo.__exit__() except Exception as e: print(e) self.set_mppt(False) self.set_harvester(False) self.set_lvl_conv(False) self._adc_set_power(False) self._set_power(False) logger.debug("Shepherd hardware is powered down")
def __enter__(self): try: for name, pin in gpio_pin_nums.items(): self.gpios[name] = GPIO(pin, "out") self._set_shepherd_pcb_power(True) self.set_target_io_level_conv(False) logger.debug("Shepherd hardware is powered up") # If shepherd hasn't been terminated properly if sysfs_interface.get_state() != "idle": sysfs_interface.set_stop() sysfs_interface.wait_for_state("idle", 5) logger.debug(f"Switching to '{ self.mode }'-mode") sysfs_interface.write_mode(self.mode) # clean up msg-channel provided by kernel module self._flush_msgs() # Ask PRU for base address of shared mem (reserved with remoteproc) mem_address = sysfs_interface.get_mem_address() # Ask PRU for size of shared memory (reserved with remoteproc) mem_size = sysfs_interface.get_mem_size() logger.debug( f"Shared memory address: \t0x{mem_address:08X}, size: {mem_size} byte" ) # Ask PRU for size of individual buffers samples_per_buffer = sysfs_interface.get_samples_per_buffer() logger.debug(f"Samples per buffer: \t{ samples_per_buffer }") self.n_buffers = sysfs_interface.get_n_buffers() logger.debug(f"Number of buffers: \t{ self.n_buffers }") self.buffer_period_ns = sysfs_interface.get_buffer_period_ns() logger.debug(f"Buffer period: \t\t{ self.buffer_period_ns } ns") self.shared_mem = SharedMem( mem_address, mem_size, self.n_buffers, samples_per_buffer ) self.shared_mem.__enter__() except Exception: self._cleanup() raise sysfs_interface.wait_for_state("idle", 3) return self
def select_main_target_for_power(self, sel_target_a: bool) -> NoReturn: """ choose which targets gets the supply with current-monitor, True = Target A, False = Target B shepherd hw-rev2 has two ports for targets and two separate power supplies, but only one is able to measure current, the other is considered "auxiliary" Args: sel_target_a: True to select A, False for B """ current_state = sysfs_interface.get_state() if current_state != "idle": raise ShepherdIOException(f"Can't switch target-power when shepherd-state is {current_state}") if sel_target_a is None: # Target A is Default sel_target_a = True target = "A" if sel_target_a else "B" logger.debug(f"Set routing for (main) supply with current-monitor to target {target}") self.gpios["target_pwr_sel"].write(sel_target_a)
def _cleanup(self): logger.debug("ShepherdIO is commanded to power down / cleanup") while sysfs_interface.get_state() != "idle": try: sysfs_interface.set_stop() except Exception as e: print(e) try: sysfs_interface.wait_for_state("idle", 3.0) except SysfsInterfaceException: logger.warning("CleanupRoutine - send stop-command and waiting for PRU to go to idle") self.set_aux_target_voltage(None, 0.0) if self.shared_mem is not None: self.shared_mem.__exit__() self.set_target_io_level_conv(False) self._set_shepherd_pcb_power(False) logger.debug("Shepherd hardware is now powered down")
def test_start(shepherd_up): sysfs_interface.start() time.sleep(5) assert sysfs_interface.get_state() == "running" with pytest.raises(sysfs_interface.SysfsInterfaceException): sysfs_interface.start()
def __enter__(self): try: for name, pin in gpio_pin_nums.items(): self.gpios[name] = GPIO(pin, "out") self._set_power(True) self.set_mppt(False) self.set_harvester(False) self.set_lvl_conv(False) self._adc_set_power(True) logger.debug("Shepherd hardware is powered up") # If shepherd hasn't been terminated properly if sysfs_interface.get_state() != "idle": sysfs_interface.set_stop() sysfs_interface.wait_for_state("idle", 5) logger.debug(f"Switching to '{ self.mode }' mode") sysfs_interface.set_mode(self.mode) # Open the RPMSG channel provided by rpmsg_pru kernel module rpmsg_dev = Path("/dev/rpmsg_pru0") self.rpmsg_fd = os.open(str(rpmsg_dev), os.O_RDWR | os.O_SYNC) os.set_blocking(self.rpmsg_fd, False) self._flush_msgs() # Ask PRU for base address of shared mem (reserved with remoteproc) mem_address = sysfs_interface.get_mem_address() # Ask PRU for size of shared memory (reserved with remoteproc) mem_size = sysfs_interface.get_mem_size() logger.debug( f"Shared memory address: \t0x{mem_address:08X}, size: {mem_size} byte" ) # Ask PRU for size of individual buffers samples_per_buffer = sysfs_interface.get_samples_per_buffer() logger.debug(f"Samples per buffer: \t{ samples_per_buffer }") self.n_buffers = sysfs_interface.get_n_buffers() logger.debug(f"Number of buffers: \t{ self.n_buffers }") self.buffer_period_ns = sysfs_interface.get_buffer_period_ns() logger.debug(f"Buffer period: \t\t{ self.buffer_period_ns } ns") self.shared_mem = SharedMem( mem_address, mem_size, self.n_buffers, samples_per_buffer ) self.shared_mem.__enter__() logger.debug(f"Setting load to '{ self.load }'") self.set_load(self.load) self.ldo.__enter__() self.set_ldo_voltage(False) except Exception: self._cleanup() raise sysfs_interface.wait_for_state("idle", 3) return self
def get_shepherd_state(self) -> bool: return sysfs_interface.get_state()