422 lines
12 KiB
C
422 lines
12 KiB
C
/*
|
|
* This file is part of the stmbl project.
|
|
*
|
|
* Copyright (C) 2013-2018 Rene Hopf <renehopf@mac.com>
|
|
* Copyright (C) 2013-2018 Nico Stute <crinq@crinq.de>
|
|
*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "defines.h"
|
|
|
|
void MX_GPIO_Init(void)
|
|
{
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
/* GPIO Ports Clock Enable */
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
|
|
GPIO_InitStruct.Pin = LEFT_HALL_U_PIN;
|
|
HAL_GPIO_Init(LEFT_HALL_U_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LEFT_HALL_V_PIN;
|
|
HAL_GPIO_Init(LEFT_HALL_V_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LEFT_HALL_W_PIN;
|
|
HAL_GPIO_Init(LEFT_HALL_W_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_HALL_U_PIN;
|
|
HAL_GPIO_Init(RIGHT_HALL_U_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_HALL_V_PIN;
|
|
HAL_GPIO_Init(RIGHT_HALL_V_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_HALL_W_PIN;
|
|
HAL_GPIO_Init(RIGHT_HALL_W_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = CHARGER_PIN;
|
|
HAL_GPIO_Init(CHARGER_PORT, &GPIO_InitStruct);
|
|
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
|
|
GPIO_InitStruct.Pin = LED_PIN;
|
|
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = BUZZER_PIN;
|
|
HAL_GPIO_Init(BUZZER_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = OFF_PIN;
|
|
HAL_GPIO_Init(OFF_PORT, &GPIO_InitStruct);
|
|
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
|
|
|
GPIO_InitStruct.Pin = LEFT_DC_CUR_PIN;
|
|
HAL_GPIO_Init(LEFT_DC_CUR_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LEFT_U_CUR_PIN;
|
|
HAL_GPIO_Init(LEFT_U_CUR_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LEFT_V_CUR_PIN;
|
|
HAL_GPIO_Init(LEFT_V_CUR_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_DC_CUR_PIN;
|
|
HAL_GPIO_Init(RIGHT_DC_CUR_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_U_CUR_PIN;
|
|
HAL_GPIO_Init(RIGHT_U_CUR_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_V_CUR_PIN;
|
|
HAL_GPIO_Init(RIGHT_V_CUR_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = DCLINK_PIN;
|
|
HAL_GPIO_Init(DCLINK_PORT, &GPIO_InitStruct);
|
|
|
|
// GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
|
|
GPIO_InitStruct.Pin = LEFT_TIM_UH_PIN;
|
|
HAL_GPIO_Init(LEFT_TIM_UH_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LEFT_TIM_VH_PIN;
|
|
HAL_GPIO_Init(LEFT_TIM_VH_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LEFT_TIM_WH_PIN;
|
|
HAL_GPIO_Init(LEFT_TIM_WH_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LEFT_TIM_UL_PIN;
|
|
HAL_GPIO_Init(LEFT_TIM_UL_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LEFT_TIM_VL_PIN;
|
|
HAL_GPIO_Init(LEFT_TIM_VL_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LEFT_TIM_WL_PIN;
|
|
HAL_GPIO_Init(LEFT_TIM_WL_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_TIM_UH_PIN;
|
|
HAL_GPIO_Init(RIGHT_TIM_UH_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_TIM_VH_PIN;
|
|
HAL_GPIO_Init(RIGHT_TIM_VH_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_TIM_WH_PIN;
|
|
HAL_GPIO_Init(RIGHT_TIM_WH_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_TIM_UL_PIN;
|
|
HAL_GPIO_Init(RIGHT_TIM_UL_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_TIM_VL_PIN;
|
|
HAL_GPIO_Init(RIGHT_TIM_VL_PORT, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = RIGHT_TIM_WL_PIN;
|
|
HAL_GPIO_Init(RIGHT_TIM_WL_PORT, &GPIO_InitStruct);
|
|
}
|
|
|
|
TIM_HandleTypeDef htim_right;
|
|
TIM_HandleTypeDef htim_left;
|
|
|
|
void MX_TIM_Init(void){
|
|
__HAL_RCC_TIM1_CLK_ENABLE();
|
|
__HAL_RCC_TIM8_CLK_ENABLE();
|
|
|
|
TIM_MasterConfigTypeDef sMasterConfig;
|
|
TIM_OC_InitTypeDef sConfigOC;
|
|
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
|
|
TIM_SlaveConfigTypeDef sTimConfig;
|
|
|
|
htim_right.Instance = RIGHT_TIM;
|
|
htim_right.Init.Prescaler = 0;
|
|
htim_right.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
|
|
htim_right.Init.Period = 64000000 / 2 / PWM_FREQ;
|
|
htim_right.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
htim_right.Init.RepetitionCounter = 0;
|
|
htim_right.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
if (HAL_TIM_PWM_Init(&htim_right) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim_right, &sMasterConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
|
sConfigOC.Pulse = 0;
|
|
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
|
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
|
|
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
|
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
|
|
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_SET;
|
|
if (HAL_TIM_PWM_ConfigChannel(&htim_right, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
if (HAL_TIM_PWM_ConfigChannel(&htim_right, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
if (HAL_TIM_PWM_ConfigChannel(&htim_right, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE;
|
|
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
|
|
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
|
|
sBreakDeadTimeConfig.DeadTime = DEAD_TIME;
|
|
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
|
|
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW;
|
|
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
|
|
if (HAL_TIMEx_ConfigBreakDeadTime(&htim_right, &sBreakDeadTimeConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
htim_left.Instance = LEFT_TIM;
|
|
htim_left.Init.Prescaler = 0;
|
|
htim_left.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
|
|
htim_left.Init.Period = 64000000 / 2 / PWM_FREQ;
|
|
htim_left.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
htim_left.Init.RepetitionCounter = 0;
|
|
htim_left.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
if (HAL_TIM_PWM_Init(&htim_left) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim_left, &sMasterConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
sTimConfig.InputTrigger = TIM_TS_ITR0;
|
|
sTimConfig.SlaveMode = TIM_SLAVEMODE_GATED;
|
|
HAL_TIM_SlaveConfigSynchronization(&htim_left, &sTimConfig);
|
|
|
|
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
|
sConfigOC.Pulse = 0;
|
|
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
|
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
|
|
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
|
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
|
|
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_SET;
|
|
if (HAL_TIM_PWM_ConfigChannel(&htim_left, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
if (HAL_TIM_PWM_ConfigChannel(&htim_left, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
if (HAL_TIM_PWM_ConfigChannel(&htim_left, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE;
|
|
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
|
|
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
|
|
sBreakDeadTimeConfig.DeadTime = DEAD_TIME;
|
|
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
|
|
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW;
|
|
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
|
|
if (HAL_TIMEx_ConfigBreakDeadTime(&htim_left, &sBreakDeadTimeConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
HAL_TIM_PWM_Start(&htim_left, TIM_CHANNEL_1);
|
|
HAL_TIM_PWM_Start(&htim_left, TIM_CHANNEL_2);
|
|
HAL_TIM_PWM_Start(&htim_left, TIM_CHANNEL_3);
|
|
HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_1);
|
|
HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_2);
|
|
HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_3);
|
|
|
|
HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_1);
|
|
HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_2);
|
|
HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_3);
|
|
HAL_TIMEx_PWMN_Start(&htim_right, TIM_CHANNEL_1);
|
|
HAL_TIMEx_PWMN_Start(&htim_right, TIM_CHANNEL_2);
|
|
HAL_TIMEx_PWMN_Start(&htim_right, TIM_CHANNEL_3);
|
|
|
|
htim_left.Instance->RCR = 1;
|
|
|
|
__HAL_TIM_ENABLE(&htim_right);
|
|
}
|
|
|
|
ADC_HandleTypeDef hadc1;
|
|
ADC_HandleTypeDef hadc2;
|
|
|
|
volatile adc_buf_t adc_buffer[3];
|
|
|
|
void MX_ADC1_Init(void)
|
|
{
|
|
ADC_MultiModeTypeDef multimode;
|
|
ADC_ChannelConfTypeDef sConfig;
|
|
|
|
__HAL_RCC_ADC1_CLK_ENABLE();
|
|
|
|
// HAL_ADC_DeInit(&hadc1);
|
|
// hadc1.Instance->CR2 = 0;
|
|
/**Common config
|
|
*/
|
|
hadc1.Instance = ADC1;
|
|
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
|
|
hadc1.Init.ContinuousConvMode = DISABLE;
|
|
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
|
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO;
|
|
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
hadc1.Init.NbrOfConversion = 3;
|
|
if (HAL_ADC_Init(&hadc1) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
/**Enable or disable the remapping of ADC1_ETRGREG:
|
|
* ADC1 External Event regular conversion is connected to TIM8 TRG0
|
|
*/
|
|
__HAL_AFIO_REMAP_ADC1_ETRGREG_ENABLE();
|
|
|
|
/**Configure the ADC multi-mode
|
|
*/
|
|
multimode.Mode = ADC_DUALMODE_REGSIMULT;
|
|
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
/**Configure Regular Channel
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_10;
|
|
sConfig.Rank = 1;
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
/**Configure Regular Channel
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_10;
|
|
sConfig.Rank = 2;
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
/**Configure Regular Channel
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_10;
|
|
sConfig.Rank = 3;
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
sConfig.Channel = ADC_CHANNEL_10;
|
|
sConfig.Rank = 4;
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
hadc1.Instance->CR2 |= ADC_CR2_DMA;
|
|
|
|
__HAL_ADC_ENABLE(&hadc1);
|
|
|
|
__HAL_RCC_DMA1_CLK_ENABLE();
|
|
|
|
DMA1_Channel1->CCR = 0;
|
|
DMA1_Channel1->CNDTR = 3;
|
|
DMA1_Channel1->CPAR = (uint32_t)&(ADC1->DR);
|
|
DMA1_Channel1->CMAR = (uint32_t)adc_buffer;
|
|
DMA1_Channel1->CCR = DMA_CCR_MSIZE_1 | DMA_CCR_PSIZE_1 | DMA_CCR_MINC | DMA_CCR_CIRC | DMA_CCR_TCIE;
|
|
DMA1_Channel1->CCR |= DMA_CCR_EN;
|
|
|
|
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
|
|
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
|
}
|
|
|
|
/* ADC2 init function */
|
|
void MX_ADC2_Init(void)
|
|
{
|
|
ADC_ChannelConfTypeDef sConfig;
|
|
|
|
__HAL_RCC_ADC2_CLK_ENABLE();
|
|
|
|
// HAL_ADC_DeInit(&hadc2);
|
|
// hadc2.Instance->CR2 = 0;
|
|
/**Common config
|
|
*/
|
|
hadc2.Instance = ADC2;
|
|
hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
|
|
hadc2.Init.ContinuousConvMode = DISABLE;
|
|
hadc2.Init.DiscontinuousConvMode = DISABLE;
|
|
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
|
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
hadc2.Init.NbrOfConversion = 3;
|
|
if (HAL_ADC_Init(&hadc2) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
/**Configure Regular Channel
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_14;
|
|
sConfig.Rank = 1;
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
|
|
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
/**Configure Regular Channel
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_15;
|
|
sConfig.Rank = 2;
|
|
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
/**Configure Regular Channel
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_11;
|
|
sConfig.Rank = 3;
|
|
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
|
|
{
|
|
_Error_Handler(__FILE__, __LINE__);
|
|
}
|
|
|
|
hadc2.Instance->CR2 |= ADC_CR2_DMA;
|
|
__HAL_ADC_ENABLE(&hadc2);
|
|
}
|