Create a power management/battery management prototype

This commit is contained in:
Savanni D'Gerinel 2022-07-20 19:19:45 -04:00
parent e03485b1e5
commit ce2a2e98b5
2 changed files with 51 additions and 0 deletions

View File

@ -387,6 +387,37 @@
'';
};
packages."x86_64-linux"."power-management-standalone" = (packages."x86_64-linux"."power-management" attiny85);
packages."x86_64-linux"."power-management" =
{ mcu, chip_select, f_cpu }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
lib = pkgs.lib;
avr = pkgs.pkgsCross.avr.buildPackages;
in pkgs.stdenv.mkDerivation rec {
name = "power-management";
src = ./.;
buildInputs = [
(packages."x86_64-linux"."base" { inherit mcu chip_select f_cpu; })
];
CFLAGS = cflags { inherit mcu chip_select f_cpu; };
INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs);
OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs);
buildPhase = ''
${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o main.o -c ${src}/power-management/main.c
${avr.gcc}/bin/avr-gcc ${CFLAGS} -o power-management.elf main.o ${OBJECT_FILES}
$OBJCOPY -O ihex power-management.elf power-management.hex
'';
installPhase = ''
mkdir $out
cp power-management.elf power-management.hex $out
'';
};
devShell."x86_64-linux" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };

20
power-management/main.c Normal file
View File

@ -0,0 +1,20 @@
#include <base.h>
#define TIMEOUT 1
int main(void) {
dio_t power = { .ddr = &DDRB, .pin = &PINB, .port = &PORTB, .addr = 3 };
dio_set_direction(&power, LINE_OUT);
int timeout_seconds = TIMEOUT;
while (1) {
if (timeout_seconds <= 0) {
dio_set(&power, 1);
_delay_ms(50);
dio_set(&power, 0);
timeout_seconds = TIMEOUT;
}
timeout_seconds--;
_delay_ms(1000);
}
}