From ed5154b924d004e068d0b9f04cd271400446e69b Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Tue, 5 Dec 2017 22:53:13 -0800 Subject: [PATCH] reorg --- src/pwr/config.h | 124 +++++++++++++++++++++++++++++++++++++++++++++++ src/pwr/main.c | 115 ++++--------------------------------------- 2 files changed, 134 insertions(+), 105 deletions(-) create mode 100644 src/pwr/config.h diff --git a/src/pwr/config.h b/src/pwr/config.h new file mode 100644 index 0000000..ef61776 --- /dev/null +++ b/src/pwr/config.h @@ -0,0 +1,124 @@ +/******************************************************************************\ + + This file is part of the Buildbotics firmware. + + Copyright (c) 2017 Buildbotics LLC + All rights reserved. + + This file ("the software") is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License, + version 2 as published by the Free Software Foundation. You should + have received a copy of the GNU General Public License, version 2 + along with the software. If not, see . + + The software is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the software. If not, see + . + + For information regarding this software email: + "Joseph Coffland" + +\******************************************************************************/ + +#pragma once + +#include "pins.h" + +// Pins +enum { + AREF_PIN = PORT_A << 3, + PA1_PIN, // NC + PA2_PIN, // NC + CS1_PIN, + CS2_PIN, + CS3_PIN, + CS4_PIN, + VOUT_REF_PIN, + + VIN_REF_PIN = PORT_B << 3, + PWR_MOSI_PIN, + PWR_MISO_PIN, + SHUNT_PIN, + + MOTOR_PIN = PORT_C << 3, // IN1 + PWR_SCK_PIN, + PC2_PIN, // NC + PWR_RESET, + LOAD1_PIN, // IN3 + LOAD2_PIN, // IN4 +}; + +// ADC +enum { + CS1_ADC, // Motor current + CS2_ADC, // Vdd current + CS3_ADC, // Load 1 current + CS4_ADC, // Load 2 current + VOUT_ADC, // Motor voltage + VIN_ADC, // Input voltage + NC6_ADC, + NC7_ADC, + NC8_ADC, + NC9_ADC, + NC10_ADC, + NC11_ADC, + NC12_ADC, + NC13_ADC, + TEMP_ADC, // Temperature +}; + + +#define CAP_CHARGE_TIME 100 // ms +#define CAP_CHARGE_MAX_DUTY 0.5 +#define VOLTAGE_MIN 11 +#define VOLTAGE_MAX 39 +#define CURRENT_MAX 25 +#define VOLTAGE_SETTLE_COUNT 5 +#define VOLTAGE_SETTLE_PERIOD 20 // ms +#define VOLTAGE_SETTLE_TOLERANCE 0.01 +#define VOLTAGE_EXP 0.01 +#define FAULT_TIMEOUT 5000 // ms + +#define SHUNT_WATTS_PER_SEC 5 +#define SHUNT_OHMS 10 +#define SHUNT_MIN_V 1 +#define SHUNT_MAX_V 3 + +#define VOLTAGE_REF 1.1 +#define VOLTAGE_REF_R1 34800 // TODO v10 will have 37.4k +#define VOLTAGE_REF_R2 1000 +#define CURRENT_REF_MUL 1970 + +#define I2C_ADDR 0x60 +#define I2C_MASK 0b00000111 + +#define I2C_ERROR_BM (1 << TWBE) +#define I2C_DATA_INT_BM (1 << TWDIF) +#define I2C_READ_BM (1 << TWDIR) +#define I2C_ADDRESS_STOP_INT_BM (1 << TWASIF) +#define I2C_ADDRESS_MATCH_BM (1 << TWAS) + + +typedef enum { + TEMP_REG, + VIN_REG, + VOUT_REG, + MOTOR_REG, + LOAD1_REG, + LOAD2_REG, + VDD_REG, + FLAGS_REG, + NUM_REGS +} regs_t; + + +enum { + UNDER_VOLTAGE_FLAG = 1 << 0, + OVER_VOLTAGE_FLAG = 1 << 1, + OVER_CURRENT_FLAG = 1 << 2, +}; diff --git a/src/pwr/main.c b/src/pwr/main.c index 05b69b2..38ffa30 100644 --- a/src/pwr/main.c +++ b/src/pwr/main.c @@ -25,7 +25,7 @@ \******************************************************************************/ -#include "pins.h" +#include "config.h" #include #include @@ -36,96 +36,6 @@ #include -// Pins -enum { - AREF_PIN = PORT_A << 3, - PA1_PIN, // NC - PA2_PIN, // NC - CS1_PIN, - CS2_PIN, - CS3_PIN, - CS4_PIN, - VOUT_REF_PIN, - - VIN_REF_PIN = PORT_B << 3, - PWR_MOSI_PIN, - PWR_MISO_PIN, - SHUNT_PIN, - - MOTOR_PIN = PORT_C << 3, // IN1 - PWR_SCK_PIN, - PC2_PIN, // NC - PWR_RESET, - LOAD1_PIN, // IN3 - LOAD2_PIN, // IN4 -}; - - -// ADC -enum { - CS1_ADC, // Motor current - CS2_ADC, // Vdd current - CS3_ADC, // Load 1 current - CS4_ADC, // Load 2 current - VOUT_ADC, // Motor voltage - VIN_ADC, // Input voltage - NC6_ADC, - NC7_ADC, - NC8_ADC, - NC9_ADC, - NC10_ADC, - NC11_ADC, - NC12_ADC, - NC13_ADC, - TEMP_ADC, // Temperature -}; - - -#define CAP_CHARGE_TIME 100 // ms -#define CAP_CHARGE_MAX_DUTY 0.5 -#define VOLTAGE_MIN 11 -#define VOLTAGE_MAX 39 -#define CURRENT_MAX 25 -#define VOLTAGE_SETTLE_COUNT 5 -#define VOLTAGE_SETTLE_PERIOD 20 // ms -#define VOLTAGE_SETTLE_TOLERANCE 0.01 -#define VOLTAGE_EXP 0.01 -#define FAULT_TIMEOUT 5000 // ms - -#define SHUNT_WATTS_PER_SEC 5 -#define SHUNT_OHMS 10 -#define SHUNT_MIN_V 1 -#define SHUNT_MAX_V 3 - -#define I2C_ADDR 0x60 -#define I2C_MASK 0b00000111 - -#define I2C_ERROR_BM (1 << TWBE) -#define I2C_DATA_INT_BM (1 << TWDIF) -#define I2C_READ_BM (1 << TWDIR) -#define I2C_ADDRESS_STOP_INT_BM (1 << TWASIF) -#define I2C_ADDRESS_MATCH_BM (1 << TWAS) - - -typedef enum { - TEMP_REG, - VIN_REG, - VOUT_REG, - MOTOR_REG, - LOAD1_REG, - LOAD2_REG, - VDD_REG, - FLAGS_REG, - NUM_REGS -} regs_t; - - -enum { - UNDER_VOLTAGE_FLAG = 1 << 0, - OVER_VOLTAGE_FLAG = 1 << 1, - OVER_CURRENT_FLAG = 1 << 2, -}; - static const uint8_t ch_schedule[] = { TEMP_ADC, VOUT_ADC, @@ -137,7 +47,11 @@ static const uint8_t ch_schedule[] = { 0 }; -volatile uint16_t regs[NUM_REGS] = {0}; + +static volatile uint16_t regs[NUM_REGS] = {0}; +static volatile uint64_t time = 0; // ms +static volatile float shunt_ms_power = 0; +static volatile float vnom = 0; void i2c_ack() {TWSCRB = (1 << TWCMD1) | (1 << TWCMD0);} @@ -205,10 +119,6 @@ static float get_reg(int reg) { } -static volatile uint64_t time = 0; // ms -static volatile float shunt_ms_power = 0; - - static void update_shunt() { static float watts = SHUNT_WATTS_PER_SEC; @@ -248,8 +158,6 @@ static void update_shunt_power(float vout, float vnom) { } -static volatile float vnom = 0; - static void measure_nominal_voltage() { float vin = get_reg(VIN_REG); float v; @@ -282,16 +190,13 @@ static void delay_ms(uint16_t ms) { inline static uint16_t convert_voltage(uint16_t sample) { -#define VREF 1.1 -#define VR1 34800 // TODO v10 will have 37.4k -#define VR2 1000 - - return sample * (VREF / 1024.0 * (VR1 + VR2) / VR2 * 100); + return sample * (VOLTAGE_REF / 1024.0 * + (VOLTAGE_REF_R1 + VOLTAGE_REF_R2) / VOLTAGE_REF_R2 * 100); } inline static uint16_t convert_current(uint16_t sample) { - return sample * (VREF / 1024.0 * 1970); + return sample * (VOLTAGE_REF / 1024.0 * CURRENT_REF_MUL); } @@ -311,7 +216,7 @@ static void read_conversion(uint8_t ch) { static void adc_conversion() { - static int8_t i = 0; + static int i = 0; read_conversion(ch_schedule[i]); if (!ch_schedule[++i]) i = 0; -- 2.27.0