diff --git a/conf_general.h b/conf_general.h
index df7fd4d4..dcc5cc4f 100644
--- a/conf_general.h
+++ b/conf_general.h
@@ -27,7 +27,7 @@
// Firmware version
#define FW_VERSION_MAJOR 1
-#define FW_VERSION_MINOR 13
+#define FW_VERSION_MINOR 14
#include "datatypes.h"
@@ -50,11 +50,13 @@
* Select only one hardware version
*/
#if !defined(HW_VERSION_40) && !defined(HW_VERSION_45) && !defined(HW_VERSION_46) && \
- !defined(HW_VERSION_48) && !defined(HW_VERSION_R2) && !defined(HW_VERSION_VICTOR_R1A)
+ !defined(HW_VERSION_48) && !defined(HW_VERSION_49) && !defined(HW_VERSION_R2) && \
+ !defined(HW_VERSION_VICTOR_R1A)
//#define HW_VERSION_40
//#define HW_VERSION_45
//#define HW_VERSION_46 // Also for 4.7
#define HW_VERSION_48
+//#define HW_VERSION_49
//#define HW_VERSION_R2
//#define HW_VERSION_VICTOR_R1A
#endif
diff --git a/eeprom.c b/eeprom.c
index 9c4b0557..6b1a8ce4 100644
--- a/eeprom.c
+++ b/eeprom.c
@@ -25,6 +25,7 @@
/* Includes ------------------------------------------------------------------*/
#include "eeprom.h"
+#include "flash_helper.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
@@ -43,6 +44,7 @@ static FLASH_Status EE_Format(void);
static uint16_t EE_FindValidPage(uint8_t Operation);
static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data);
static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data);
+static uint16_t EE_EraseSectorIfNotEmpty(uint32_t FLASH_Sector, uint8_t VoltageRange);
/**
* @brief Restore the pages to a known good state in case of page's status
@@ -71,7 +73,7 @@ uint16_t EE_Init(void)
if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
{
/* Erase Page0 */
- FlashStatus = FLASH_EraseSector(PAGE0_ID,VOLTAGE_RANGE);
+ FlashStatus = EE_EraseSectorIfNotEmpty(PAGE0_ID,VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
{
@@ -81,7 +83,7 @@ uint16_t EE_Init(void)
else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */
{
/* Erase Page0 */
- FlashStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE);
+ FlashStatus = EE_EraseSectorIfNotEmpty(PAGE0_ID, VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
{
@@ -142,7 +144,7 @@ uint16_t EE_Init(void)
return FlashStatus;
}
/* Erase Page1 */
- FlashStatus = FLASH_EraseSector(PAGE1_ID, VOLTAGE_RANGE);
+ FlashStatus = EE_EraseSectorIfNotEmpty(PAGE1_ID, VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
{
@@ -152,7 +154,7 @@ uint16_t EE_Init(void)
else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */
{
/* Erase Page1 */
- FlashStatus = FLASH_EraseSector(PAGE1_ID, VOLTAGE_RANGE);
+ FlashStatus = EE_EraseSectorIfNotEmpty(PAGE1_ID, VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
{
@@ -192,7 +194,7 @@ uint16_t EE_Init(void)
else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
{
/* Erase Page1 */
- FlashStatus = FLASH_EraseSector(PAGE1_ID, VOLTAGE_RANGE);
+ FlashStatus = EE_EraseSectorIfNotEmpty(PAGE1_ID, VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
{
@@ -233,7 +235,7 @@ uint16_t EE_Init(void)
return FlashStatus;
}
/* Erase Page0 */
- FlashStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE);
+ FlashStatus = EE_EraseSectorIfNotEmpty(PAGE0_ID, VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
{
@@ -354,7 +356,7 @@ static FLASH_Status EE_Format(void)
FLASH_Status FlashStatus = FLASH_COMPLETE;
/* Erase Page0 */
- FlashStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE);
+ FlashStatus = EE_EraseSectorIfNotEmpty(PAGE0_ID, VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
@@ -372,7 +374,7 @@ static FLASH_Status EE_Format(void)
}
/* Erase Page1 */
- FlashStatus = FLASH_EraseSector(PAGE1_ID, VOLTAGE_RANGE);
+ FlashStatus = EE_EraseSectorIfNotEmpty(PAGE1_ID, VOLTAGE_RANGE);
/* Return Page1 erase operation status */
return FlashStatus;
@@ -590,7 +592,7 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
}
/* Erase the old Page: Set old Page status to ERASED status */
- FlashStatus = FLASH_EraseSector(OldPageId, VOLTAGE_RANGE);
+ FlashStatus = EE_EraseSectorIfNotEmpty(OldPageId, VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
if (FlashStatus != FLASH_COMPLETE)
{
@@ -609,6 +611,26 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
return FlashStatus;
}
+#include "hw.h"
+#include "ch.h"
+#include "hal.h"
+
+/*
+ * Erase flash page if it is not already erased. This is to save write cycles and
+ * prevent the memory from getting erased in case of unstable voltage at boot.
+ */
+static uint16_t EE_EraseSectorIfNotEmpty(uint32_t FLASH_Sector, uint8_t VoltageRange) {
+ uint8_t *addr = flash_helper_get_sector_address(FLASH_Sector);
+
+ for (unsigned int i = 0;i < PAGE_SIZE;i++) {
+ if (addr[i] != 0xFF) {
+ return FLASH_EraseSector(FLASH_Sector, VoltageRange);
+ }
+ }
+
+ return FLASH_COMPLETE;
+}
+
/**
* @}
*/
diff --git a/flash_helper.c b/flash_helper.c
index 1859d223..9feb9f6a 100644
--- a/flash_helper.c
+++ b/flash_helper.c
@@ -181,3 +181,16 @@ void flash_helper_jump_to_bootloader(void) {
// Jump to the bootloader
jump_to_bootloader();
}
+
+uint8_t* flash_helper_get_sector_address(uint32_t fsector) {
+ uint8_t *res = 0;
+
+ for (int i = 0;i < FLASH_SECTORS;i++) {
+ if (flash_sector[i] == fsector) {
+ res = (uint8_t *)flash_addr[i];
+ break;
+ }
+ }
+
+ return res;
+}
diff --git a/flash_helper.h b/flash_helper.h
index 9be957d5..e481d2aa 100644
--- a/flash_helper.h
+++ b/flash_helper.h
@@ -31,5 +31,6 @@
uint16_t flash_helper_erase_new_app(uint32_t new_app_size);
uint16_t flash_helper_write_new_app_data(uint32_t offset, uint8_t *data, uint32_t len);
void flash_helper_jump_to_bootloader(void);
+uint8_t* flash_helper_get_sector_address(uint32_t fsector);
#endif /* FLASH_HELPER_H_ */
diff --git a/hwconf/hw.h b/hwconf/hw.h
index d97974f6..110d373d 100644
--- a/hwconf/hw.h
+++ b/hwconf/hw.h
@@ -35,6 +35,8 @@
#include "hw_46.h"
#elif defined HW_VERSION_48
#include "hw_48.h"
+#elif defined HW_VERSION_49
+#include "hw_49.h"
#elif defined HW_VERSION_R2
#include "hw_r2.h"
#elif defined HW_VERSION_BW
diff --git a/hwconf/hw_48.h b/hwconf/hw_48.h
index 79382b85..f1e44ca5 100644
--- a/hwconf/hw_48.h
+++ b/hwconf/hw_48.h
@@ -150,13 +150,13 @@
// NRF pins
#define NRF_PORT_CSN GPIOB
-#define NRF_PIN_CSN 1
+#define NRF_PIN_CSN 11
#define NRF_PORT_SCK GPIOC
#define NRF_PIN_SCK 5
-#define NRF_PORT_MOSI HW_I2C_SDA_PORT
-#define NRF_PIN_MOSI HW_I2C_SDA_PIN
-#define NRF_PORT_MISO HW_I2C_SCL_PORT
-#define NRF_PIN_MISO HW_I2C_SCL_PIN
+#define NRF_PORT_MOSI GPIOB
+#define NRF_PIN_MOSI 10
+#define NRF_PORT_MISO GPIOB
+#define NRF_PIN_MISO 1
// Measurement macros
#define ADC_V_L1 ADC_Value[ADC_IND_SENS1]
diff --git a/hwconf/hw_49.c b/hwconf/hw_49.c
new file mode 100644
index 00000000..704b14c3
--- /dev/null
+++ b/hwconf/hw_49.c
@@ -0,0 +1,245 @@
+/*
+ Copyright 2015 Benjamin Vedder benjamin@vedder.se
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+ */
+
+#include "hw.h"
+#ifdef HW_VERSION_49
+
+#include "ch.h"
+#include "hal.h"
+#include "stm32f4xx_conf.h"
+#include "servo.h"
+
+// Variables
+static volatile bool i2c_running = false;
+
+// I2C configuration
+static const I2CConfig i2cfg = {
+ OPMODE_I2C,
+ 100000,
+ STD_DUTY_CYCLE
+};
+
+void hw_init_gpio(void) {
+ // GPIO clock enable
+ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
+ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
+ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
+
+ // LEDs
+ palSetPadMode(GPIOC, 4,
+ PAL_MODE_OUTPUT_PUSHPULL |
+ PAL_STM32_OSPEED_HIGHEST);
+ palSetPadMode(GPIOC, 5,
+ PAL_MODE_OUTPUT_PUSHPULL |
+ PAL_STM32_OSPEED_HIGHEST);
+
+ // GPIOC (ENABLE_GATE)
+ palSetPadMode(GPIOC, 10,
+ PAL_MODE_OUTPUT_PUSHPULL |
+ PAL_STM32_OSPEED_HIGHEST);
+ DISABLE_GATE();
+
+ // GPIOB (DCCAL)
+ palSetPadMode(GPIOB, 12,
+ PAL_MODE_OUTPUT_PUSHPULL |
+ PAL_STM32_OSPEED_HIGHEST);
+
+ // GPIOA Configuration: Channel 1 to 3 as alternate function push-pull
+ palSetPadMode(GPIOA, 8, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) |
+ PAL_STM32_OSPEED_HIGHEST |
+ PAL_STM32_PUDR_FLOATING);
+ palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) |
+ PAL_STM32_OSPEED_HIGHEST |
+ PAL_STM32_PUDR_FLOATING);
+ palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) |
+ PAL_STM32_OSPEED_HIGHEST |
+ PAL_STM32_PUDR_FLOATING);
+
+ palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) |
+ PAL_STM32_OSPEED_HIGHEST |
+ PAL_STM32_PUDR_FLOATING);
+ palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) |
+ PAL_STM32_OSPEED_HIGHEST |
+ PAL_STM32_PUDR_FLOATING);
+ palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) |
+ PAL_STM32_OSPEED_HIGHEST |
+ PAL_STM32_PUDR_FLOATING);
+
+ // Hall sensors
+ palSetPadMode(HW_HALL_ENC_GPIO1, HW_HALL_ENC_PIN1, PAL_MODE_INPUT_PULLUP);
+ palSetPadMode(HW_HALL_ENC_GPIO2, HW_HALL_ENC_PIN2, PAL_MODE_INPUT_PULLUP);
+ palSetPadMode(HW_HALL_ENC_GPIO3, HW_HALL_ENC_PIN3, PAL_MODE_INPUT_PULLUP);
+
+ // Fault pin
+ palSetPadMode(GPIOC, 12, PAL_MODE_INPUT_PULLUP);
+
+ // ADC Pins
+ palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOA, 1, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_ANALOG);
+
+ palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_ANALOG);
+
+ palSetPadMode(GPIOC, 0, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOC, 1, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOC, 2, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOC, 3, PAL_MODE_INPUT_ANALOG);
+ palSetPadMode(GPIOC, 5, PAL_MODE_INPUT_ANALOG);
+}
+
+void hw_setup_adc_channels(void) {
+ // ADC1 regular channels
+ ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_15Cycles);
+ ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 2, ADC_SampleTime_15Cycles);
+ ADC_RegularChannelConfig(ADC1, ADC_Channel_Vrefint, 3, ADC_SampleTime_15Cycles);
+ ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 4, ADC_SampleTime_15Cycles);
+
+ // ADC2 regular channels
+ ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_15Cycles);
+ ADC_RegularChannelConfig(ADC2, ADC_Channel_9, 2, ADC_SampleTime_15Cycles);
+ ADC_RegularChannelConfig(ADC2, ADC_Channel_6, 3, ADC_SampleTime_15Cycles);
+ ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 4, ADC_SampleTime_15Cycles);
+
+ // ADC3 regular channels
+ ADC_RegularChannelConfig(ADC3, ADC_Channel_2, 1, ADC_SampleTime_15Cycles);
+ ADC_RegularChannelConfig(ADC3, ADC_Channel_3, 2, ADC_SampleTime_15Cycles);
+ ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 3, ADC_SampleTime_15Cycles);
+ ADC_RegularChannelConfig(ADC3, ADC_Channel_10, 4, ADC_SampleTime_15Cycles);
+
+ // Injected channels
+ ADC_InjectedChannelConfig(ADC1, ADC_Channel_9, 1, ADC_SampleTime_15Cycles);
+ ADC_InjectedChannelConfig(ADC1, ADC_Channel_8, 2, ADC_SampleTime_15Cycles);
+ ADC_InjectedChannelConfig(ADC2, ADC_Channel_8, 1, ADC_SampleTime_15Cycles);
+ ADC_InjectedChannelConfig(ADC2, ADC_Channel_9, 2, ADC_SampleTime_15Cycles);
+}
+
+void hw_setup_servo_outputs(void) {
+ // Set up GPIO ports
+ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
+
+ // Set up servo structures
+ servos[0].gpio = GPIOB;
+ servos[0].pin = 5;
+ servos[0].offset = 0;
+ servos[0].pos = 128;
+
+ servos[1].gpio = GPIOB;
+ servos[1].pin = 4;
+ servos[1].offset = 0;
+ servos[1].pos = 0;
+}
+
+void hw_start_i2c(void) {
+ i2cAcquireBus(&HW_I2C_DEV);
+
+ if (!i2c_running) {
+ palSetPadMode(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN,
+ PAL_MODE_ALTERNATE(HW_I2C_GPIO_AF) |
+ PAL_STM32_OTYPE_OPENDRAIN |
+ PAL_STM32_OSPEED_MID1 |
+ PAL_STM32_PUDR_PULLUP);
+ palSetPadMode(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN,
+ PAL_MODE_ALTERNATE(HW_I2C_GPIO_AF) |
+ PAL_STM32_OTYPE_OPENDRAIN |
+ PAL_STM32_OSPEED_MID1 |
+ PAL_STM32_PUDR_PULLUP);
+
+ i2cStart(&HW_I2C_DEV, &i2cfg);
+ i2c_running = true;
+ }
+
+ i2cReleaseBus(&HW_I2C_DEV);
+}
+
+void hw_stop_i2c(void) {
+ i2cAcquireBus(&HW_I2C_DEV);
+
+ if (i2c_running) {
+ palSetPadMode(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN, PAL_MODE_INPUT);
+ palSetPadMode(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN, PAL_MODE_INPUT);
+
+ i2cStop(&HW_I2C_DEV);
+ i2c_running = false;
+
+ }
+
+ i2cReleaseBus(&HW_I2C_DEV);
+}
+
+/**
+ * Try to restore the i2c bus
+ */
+void hw_try_restore_i2c(void) {
+ if (i2c_running) {
+ i2cAcquireBus(&HW_I2C_DEV);
+
+ palSetPadMode(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN,
+ PAL_STM32_OTYPE_OPENDRAIN |
+ PAL_STM32_OSPEED_MID1 |
+ PAL_STM32_PUDR_PULLUP);
+
+ palSetPadMode(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN,
+ PAL_STM32_OTYPE_OPENDRAIN |
+ PAL_STM32_OSPEED_MID1 |
+ PAL_STM32_PUDR_PULLUP);
+
+ palSetPad(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN);
+ palSetPad(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN);
+
+ chThdSleep(1);
+
+ for(int i = 0;i < 16;i++) {
+ palClearPad(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN);
+ chThdSleep(1);
+ palSetPad(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN);
+ chThdSleep(1);
+ }
+
+ // Generate start then stop condition
+ palClearPad(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN);
+ chThdSleep(1);
+ palClearPad(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN);
+ chThdSleep(1);
+ palSetPad(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN);
+ chThdSleep(1);
+ palSetPad(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN);
+
+ palSetPadMode(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN,
+ PAL_MODE_ALTERNATE(HW_I2C_GPIO_AF) |
+ PAL_STM32_OTYPE_OPENDRAIN |
+ PAL_STM32_OSPEED_MID1 |
+ PAL_STM32_PUDR_PULLUP);
+
+ palSetPadMode(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN,
+ PAL_MODE_ALTERNATE(HW_I2C_GPIO_AF) |
+ PAL_STM32_OTYPE_OPENDRAIN |
+ PAL_STM32_OSPEED_MID1 |
+ PAL_STM32_PUDR_PULLUP);
+
+ HW_I2C_DEV.state = I2C_STOP;
+ i2cStart(&HW_I2C_DEV, &i2cfg);
+
+ i2cReleaseBus(&HW_I2C_DEV);
+ }
+}
+
+#endif
diff --git a/hwconf/hw_49.h b/hwconf/hw_49.h
new file mode 100644
index 00000000..8a375185
--- /dev/null
+++ b/hwconf/hw_49.h
@@ -0,0 +1,182 @@
+/*
+ Copyright 2015 Benjamin Vedder benjamin@vedder.se
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+ */
+
+#ifndef HW_49_H_
+#define HW_49_H_
+
+// Macros
+#define ENABLE_GATE() palSetPad(GPIOC, 10)
+#define DISABLE_GATE() palClearPad(GPIOC, 10)
+#define DCCAL_ON() palSetPad(GPIOB, 12)
+#define DCCAL_OFF() palClearPad(GPIOB, 12)
+#define IS_DRV_FAULT() (!palReadPad(GPIOC, 12))
+
+#define LED_GREEN_ON() palSetPad(GPIOC, 4)
+#define LED_GREEN_OFF() palClearPad(GPIOC, 4)
+#define LED_RED_ON() palSetPad(GPIOC, 5)
+#define LED_RED_OFF() palClearPad(GPIOC, 5)
+
+/*
+ * ADC Vector
+ *
+ * 0: IN0 SENS3
+ * 1: IN1 SENS2
+ * 2: IN2 SENS1
+ * 3: IN5 CURR2
+ * 4: IN6 CURR1
+ * 5: IN3 NC
+ * 6: Vrefint
+ * 7: IN9 ADC_EXT2
+ * 8: IN12 AN_IN
+ * 9: IN4 TEMP_MOSFET
+ * 10: IN15 ADC_EXT
+ * 11: IN10 TEMP_MOTOR
+ */
+
+#define HW_ADC_CHANNELS 12
+#define HW_ADC_NBR_CONV 4
+
+// ADC Indexes
+#define ADC_IND_SENS1 2
+#define ADC_IND_SENS2 1
+#define ADC_IND_SENS3 0
+#define ADC_IND_CURR1 4
+#define ADC_IND_CURR2 3
+#define ADC_IND_VIN_SENS 8
+#define ADC_IND_EXT 10
+#define ADC_IND_EXT2 7
+#define ADC_IND_TEMP_MOS1 9
+#define ADC_IND_TEMP_MOS2 9
+#define ADC_IND_TEMP_MOS3 9
+#define ADC_IND_TEMP_MOS4 9
+#define ADC_IND_TEMP_MOS5 9
+#define ADC_IND_TEMP_MOS6 9
+#define ADC_IND_TEMP_PCB 9
+#define ADC_IND_VREFINT 6
+
+// ADC macros and settings
+
+// Component parameters (can be overridden)
+#ifndef V_REG
+//#define V_REG 3.3
+#endif
+#ifndef VIN_R1
+#define VIN_R1 39000.0
+#endif
+#ifndef VIN_R2
+#define VIN_R2 2200.0
+#endif
+#ifndef CURRENT_AMP_GAIN
+#define CURRENT_AMP_GAIN 10.0
+#endif
+#ifndef CURRENT_SHUNT_RES
+#define CURRENT_SHUNT_RES 0.001
+#endif
+
+// Input voltage
+#define GET_INPUT_VOLTAGE() ((V_REG / 4095.0) * (float)ADC_Value[ADC_IND_VIN_SENS] * ((VIN_R1 + VIN_R2) / VIN_R2))
+
+// Voltage on ADC channel
+#define ADC_VOLTS(ch) ((float)ADC_Value[ch] / 4095.0 * V_REG)
+
+// NTC Termistors
+//#define NTC_RES(adc_val) (10000.0 / ((4096.0 / (float)adc_val) - 1.0))
+#define NTC_RES(adc_val) ((4095.0 * 10000.0) / adc_val - 10000.0)
+#define NTC_TEMP(adc_ind) (1.0 / ((logf(NTC_RES(ADC_Value[adc_ind]) / 10000.0) / 3434.0) + (1.0 / 298.15)) - 273.15)
+
+// Double samples in beginning and end for positive current measurement.
+// Useful when the shunt sense traces have noise that causes offset.
+#ifndef CURR1_DOUBLE_SAMPLE
+#define CURR1_DOUBLE_SAMPLE 0
+#endif
+#ifndef CURR2_DOUBLE_SAMPLE
+#define CURR2_DOUBLE_SAMPLE 0
+#endif
+
+// Number of servo outputs
+#define HW_SERVO_NUM 2
+
+// UART Peripheral
+#define HW_UART_DEV UARTD6
+#define HW_UART_GPIO_AF GPIO_AF_USART6
+#define HW_UART_TX_PORT GPIOC
+#define HW_UART_TX_PIN 6
+#define HW_UART_RX_PORT GPIOC
+#define HW_UART_RX_PIN 7
+
+// ICU Peripheral for servo decoding
+#define HW_ICU_CHANNEL ICU_CHANNEL_2
+#define HW_ICU_GPIO_AF GPIO_AF_TIM3
+#define HW_ICU_GPIO GPIOB
+#define HW_ICU_PIN 5
+
+// I2C Peripheral
+#define HW_I2C_DEV I2CD2
+#define HW_I2C_GPIO_AF GPIO_AF_I2C2
+#define HW_I2C_SCL_PORT GPIOB
+#define HW_I2C_SCL_PIN 10
+#define HW_I2C_SDA_PORT GPIOB
+#define HW_I2C_SDA_PIN 11
+
+// Hall/encoder pins
+#define HW_HALL_ENC_GPIO1 GPIOB
+#define HW_HALL_ENC_PIN1 6
+#define HW_HALL_ENC_GPIO2 GPIOB
+#define HW_HALL_ENC_PIN2 7
+#define HW_HALL_ENC_GPIO3 GPIOC
+#define HW_HALL_ENC_PIN3 11
+#define HW_ENC_TIM TIM4
+#define HW_ENC_TIM_AF GPIO_AF_TIM4
+#define HW_ENC_TIM_CLK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE)
+#define HW_ENC_EXTI_PORTSRC EXTI_PortSourceGPIOC
+#define HW_ENC_EXTI_PINSRC EXTI_PinSource11
+#define HW_ENC_EXTI_CH EXTI15_10_IRQn
+#define HW_ENC_EXTI_LINE EXTI_Line11
+#define HW_ENC_EXTI_ISR_VEC EXTI15_10_IRQHandler
+
+// NRF pins
+#define NRF_PORT_CSN GPIOB
+#define NRF_PIN_CSN 11
+#define NRF_PORT_SCK GPIOA
+#define NRF_PIN_SCK 5
+#define NRF_PORT_MOSI GPIOA
+#define NRF_PIN_MOSI 7
+#define NRF_PORT_MISO GPIOA
+#define NRF_PIN_MISO 6
+
+// SPI pins
+#define HW_SPI_PORT_CSN GPIOB
+#define HW_SPI_PIN_CSN 11
+#define HW_SPI_PORT_SCK GPIOA
+#define HW_SPI_PIN_SCK 5
+#define HW_SPI_PORT_MOSI GPIOA
+#define HW_SPI_PIN_MOSI 7
+#define HW_SPI_PORT_MISO GPIOA
+#define HW_SPI_PIN_MISO 6
+
+// Measurement macros
+#define ADC_V_L1 ADC_Value[ADC_IND_SENS1]
+#define ADC_V_L2 ADC_Value[ADC_IND_SENS2]
+#define ADC_V_L3 ADC_Value[ADC_IND_SENS3]
+#define ADC_V_ZERO (ADC_Value[ADC_IND_VIN_SENS] / 2)
+
+// Macros
+#define READ_HALL1() palReadPad(HW_HALL_ENC_GPIO1, HW_HALL_ENC_PIN1)
+#define READ_HALL2() palReadPad(HW_HALL_ENC_GPIO2, HW_HALL_ENC_PIN2)
+#define READ_HALL3() palReadPad(HW_HALL_ENC_GPIO3, HW_HALL_ENC_PIN3)
+
+#endif /* HW_49_H_ */
diff --git a/hwconf/hwconf.mk b/hwconf/hwconf.mk
index 9e764dcd..ab61ccbe 100644
--- a/hwconf/hwconf.mk
+++ b/hwconf/hwconf.mk
@@ -3,6 +3,7 @@ HWSRC = hwconf/hw_40.c \
hwconf/hw_r2.c \
hwconf/hw_46.c \
hwconf/hw_48.c \
+ hwconf/hw_49.c \
hwconf/hw_victor_r1a.c
HWINC = hwconf
diff --git a/main.c b/main.c
index d844d758..88d6bd8d 100644
--- a/main.c
+++ b/main.c
@@ -293,8 +293,11 @@ int main(void) {
chThdSleepMilliseconds(1000);
- conf_general_init();
hw_init_gpio();
+ LED_RED_OFF();
+ LED_GREEN_OFF();
+
+ conf_general_init();
ledpwm_init();
mc_configuration mcconf;
diff --git a/mcpwm.c b/mcpwm.c
index 0c3ddc11..9338591b 100644
--- a/mcpwm.c
+++ b/mcpwm.c
@@ -103,6 +103,9 @@ static volatile bool lock_override_once;
static volatile bool sensorless_now;
static volatile int hall_detect_table[8][7];
+// Function pointers
+static void(*pwn_done_func)(void) = 0;
+
// KV FIR filter
#define KV_FIR_TAPS_BITS 7
#define KV_FIR_LEN (1 << KV_FIR_TAPS_BITS)
@@ -607,6 +610,32 @@ void mcpwm_set_duty(float dutyCycle) {
set_duty_cycle_hl(dutyCycle);
}
+/**
+ * Use duty cycle control. Absolute values less than MCPWM_MIN_DUTY_CYCLE will
+ * stop the motor.
+ *
+ * WARNING: This function does not use ramping. A too large step with a large motor
+ * can destroy hardware.
+ *
+ * @param dutyCycle
+ * The duty cycle to use.
+ */
+void mcpwm_set_duty_noramp(float dutyCycle) {
+ if (try_input()) {
+ return;
+ }
+
+ control_mode = CONTROL_MODE_DUTY;
+
+ if (state != MC_STATE_RUNNING) {
+ set_duty_cycle_hl(dutyCycle);
+ } else {
+ dutycycle_set = dutyCycle;
+ dutycycle_now = dutyCycle;
+ set_duty_cycle_ll(dutyCycle);
+ }
+}
+
/**
* Use PID rpm control. Note that this value has to be multiplied by half of
* the number of motor poles.
@@ -2226,6 +2255,10 @@ void mcpwm_adc_int_handler(void *p, uint32_t flags) {
main_dma_adc_handler();
+ if (pwn_done_func) {
+ pwn_done_func();
+ }
+
if (ENCODER_ENABLE) {
run_pid_control_pos(1.0 / switching_frequency_now);
}
@@ -2364,6 +2397,16 @@ mc_rpm_dep_struct mcpwm_get_rpm_dep(void) {
return rpm_dep;
}
+/**
+ * Set a function that should be called after each PWM cycle.
+ *
+ * @param p_func
+ * The function to be called. 0 will not call any function.
+ */
+void mcpwm_set_pwm_callback(void (*p_func)(void)) {
+ pwn_done_func = p_func;
+}
+
/**
* Reset the hall sensor detection table
*/
diff --git a/mcpwm.h b/mcpwm.h
index a1e20f65..4d5871ab 100644
--- a/mcpwm.h
+++ b/mcpwm.h
@@ -35,6 +35,7 @@ const volatile mc_configuration* mcpwm_get_configuration(void);
void mcpwm_set_configuration(mc_configuration *configuration);
void mcpwm_init_hall_table(int8_t *table);
void mcpwm_set_duty(float dutyCycle);
+void mcpwm_set_duty_noramp(float dutyCycle);
void mcpwm_set_pid_speed(float rpm);
void mcpwm_set_pid_pos(float pos);
void mcpwm_set_current(float current);
@@ -81,6 +82,7 @@ mc_comm_mode mcpwm_get_comm_mode(void);
float mcpwm_get_last_adc_isr_duration(void);
float mcpwm_get_last_inj_adc_isr_duration(void);
mc_rpm_dep_struct mcpwm_get_rpm_dep(void);
+void mcpwm_set_pwm_callback(void (*p_func)(void));
// Interrupt handlers
void mcpwm_adc_inj_int_handler(void);