reorg
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Wed, 6 Dec 2017 06:53:13 +0000 (22:53 -0800)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Wed, 6 Dec 2017 06:53:13 +0000 (22:53 -0800)
src/pwr/config.h [new file with mode: 0644]
src/pwr/main.c

diff --git a/src/pwr/config.h b/src/pwr/config.h
new file mode 100644 (file)
index 0000000..ef61776
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+
+     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
+                       <http://www.gnu.org/licenses/>.
+
+                For information regarding this software email:
+                  "Joseph Coffland" <joseph@buildbotics.com>
+
+\******************************************************************************/
+
+#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,
+};
index 05b69b2d5e872d00b0ef62cc021a16532c24c3a8..38ffa3097e6037586cc6810256a0d7f95dcdd77a 100644 (file)
@@ -25,7 +25,7 @@
 
 \******************************************************************************/
 
-#include "pins.h"
+#include "config.h"
 
 #include <avr/interrupt.h>
 #include <avr/pgmspace.h>
 
 #include <stdbool.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 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;