Skip to content

Commit

Permalink
Tool around with faking the UART with SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
henrychoi committed Oct 18, 2020
1 parent eb872ce commit e2f3a02
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 114 deletions.
6 changes: 3 additions & 3 deletions VESC6-castle/VESC6-castle.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,11 @@ PA10.GPIO_Speed=GPIO_SPEED_FREQ_HIGH
PA10.Locked=true
PA10.Signal=S_TIM1_CH3
PA11.GPIOParameters=GPIO_Label
PA11.GPIO_Label=DOUTM
PA11.GPIO_Label=DGB1
PA11.Locked=true
PA11.Signal=GPIO_Output
PA12.GPIOParameters=GPIO_Label
PA12.GPIO_Label=DOUTP
PA12.GPIO_Label=DBG2
PA12.Locked=true
PA12.Signal=GPIO_Output
PA2.GPIOParameters=GPIO_Label
Expand Down Expand Up @@ -823,7 +823,7 @@ ProjectManager.StackSize=0x400
ProjectManager.TargetToolchain=STM32CubeIDE
ProjectManager.ToolChainLocation=
ProjectManager.UnderRoot=false
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-MX_DMA_Init-DMA-false-HAL-true,3-SystemClock_Config-RCC-false-HAL-false,4-MX_ADC1_Init-ADC1-false-HAL-true,5-MX_ADC2_Init-ADC2-false-HAL-true,6-MX_TIM1_Init-TIM1-false-HAL-true,7-MX_TIM3_Init-TIM3-false-HAL-true,8-MX_MotorControl_Init-MotorControl-false-HAL-false,9-MX_CAN1_Init-CAN1-false-HAL-true,10-MX_SPI3_Init-SPI3-false-HAL-true,11-MX_TIM2_Init-TIM2-false-HAL-true,12-MX_TIM4_Init-TIM4-false-HAL-true,13-MX_SPI1_Init-SPI1-false-HAL-true
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_ADC1_Init-ADC1-false-HAL-true,4-MX_ADC2_Init-ADC2-false-HAL-true,5-MX_TIM1_Init-TIM1-false-HAL-true,6-MX_TIM3_Init-TIM3-false-HAL-true,7-MX_MotorControl_Init-MotorControl-false-HAL-false,8-MX_CAN1_Init-CAN1-false-HAL-true,9-MX_SPI3_Init-SPI3-false-HAL-true,10-MX_TIM2_Init-TIM2-false-HAL-true,11-MX_TIM4_Init-TIM4-false-HAL-true,12-MX_SPI1_Init-SPI1-false-HAL-true
RCC.48MHZClocksFreq_Value=84000000
RCC.AHBFreq_Value=168000000
RCC.APB1CLKDivider=RCC_HCLK_DIV4
Expand Down
18 changes: 9 additions & 9 deletions f446-ihm08-castle.stmcx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</var>
<var version="1.0" type="MC_Int" name="Pole Pairs" enable="true" visible="true" readonly="false" value="2" />
<var version="1.0" type="MC_Int" name="Max Rated Speed" enable="true" visible="true" readonly="false" value="14868" />
<var version="1.0" type="MC_Double" name="Nominal Current" enable="true" visible="true" readonly="false" value="5" />
<var version="1.0" type="MC_Double" name="Nominal Current" enable="true" visible="true" readonly="false" value="30" />
<var version="1.0" type="MC_Double" name="Nominal DC Voltage" enable="true" visible="true" readonly="false" value="12" />
<var version="1.0" type="MC_Double" name="Rs" enable="true" visible="true" readonly="false" value="0.1" />
<var version="1.0" type="MC_Double" name="Ld" enable="true" visible="true" readonly="false" value="0.016" />
Expand Down Expand Up @@ -1989,9 +1989,9 @@
<registers version="1.0">
<reg version="1.0" id="0" name="Target motor" description="$$DESCR_AS_NAME$$" unit="" type="8" access_mode="2" value="0" value_previous="0" minimum="0" maximum="255" period="0" modified="false" visible="true" enable="true" />
<reg version="1.0" id="1" name="Flags" description="$$DESCR_AS_NAME$$" unit="" type="32" access_mode="0" value="0" value_previous="0" minimum="0" maximum="4294967295" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="2" name="Status" description="$$DESCR_AS_NAME$$" unit="" type="8" access_mode="0" value="6" value_previous="6" minimum="0" maximum="255" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="2" name="Status" description="$$DESCR_AS_NAME$$" unit="" type="8" access_mode="0" value="0" value_previous="0" minimum="0" maximum="255" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="3" name="Control mode" description="$$DESCR_AS_NAME$$" unit="" type="8" access_mode="2" value="1" value_previous="1" minimum="0" maximum="255" period="500" modified="false" visible="true" enable="true" />
<reg version="1.0" id="4" name="Speed reference" description="$$DESCR_AS_NAME$$" unit="RPM" type="160" access_mode="0" value="2376" value_previous="2376" minimum="-14868" maximum="14868" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="4" name="Speed reference" description="$$DESCR_AS_NAME$$" unit="RPM" type="160" access_mode="0" value="1080" value_previous="1080" minimum="-14868" maximum="14868" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="5" name="Speed Kp" description="$$DESCR_AS_NAME$$" unit="" type="16" access_mode="2" value="3678" value_previous="3678" minimum="0" maximum="65535" period="0" modified="false" visible="true" enable="true" />
<reg version="1.0" id="6" name="Speed Ki" description="$$DESCR_AS_NAME$$" unit="" type="16" access_mode="2" value="829" value_previous="829" minimum="0" maximum="65535" period="0" modified="false" visible="true" enable="true" />
<reg version="1.0" id="7" name="Speed Kd" description="$$DESCR_AS_NAME$$" unit="" type="16" access_mode="2" value="0" value_previous="0" minimum="0" maximum="65535" period="0" modified="false" visible="true" enable="true" />
Expand All @@ -2013,13 +2013,13 @@
<reg version="1.0" id="23" name="Flux weakening Ki" description="$$DESCR_AS_NAME$$" unit="" type="16" access_mode="2" value="0" value_previous="0" minimum="0" maximum="65535" period="0" modified="false" visible="true" enable="false" />
<reg version="1.0" id="24" name="Flux weakening BUS Voltage allowed percentage reference" description="$$DESCR_AS_NAME$$" unit="" type="16" access_mode="2" value="0" value_previous="0" minimum="0" maximum="65535" period="0" modified="false" visible="true" enable="false" />
<reg version="1.0" id="25" name="Bus Voltage" description="$$DESCR_AS_NAME$$" unit="Volt" type="16" access_mode="0" value="12" value_previous="12" minimum="0" maximum="50" period="500" modified="false" visible="true" enable="true" />
<reg version="1.0" id="26" name="Heatsink Temperature" description="$$DESCR_AS_NAME$$" unit="°C" type="16" access_mode="0" value="23" value_previous="23" minimum="0" maximum="100" period="500" modified="false" visible="true" enable="true" />
<reg version="1.0" id="27" name="Motor Power" description="$$DESCR_AS_NAME$$" unit="W" type="16" access_mode="0" value="2" value_previous="2" minimum="0" maximum="200" period="500" modified="false" visible="true" enable="true" />
<reg version="1.0" id="26" name="Heatsink Temperature" description="$$DESCR_AS_NAME$$" unit="°C" type="16" access_mode="0" value="18" value_previous="18" minimum="0" maximum="100" period="500" modified="false" visible="true" enable="true" />
<reg version="1.0" id="27" name="Motor Power" description="$$DESCR_AS_NAME$$" unit="W" type="16" access_mode="0" value="0" value_previous="0" minimum="0" maximum="200" period="500" modified="false" visible="true" enable="true" />
<reg version="1.0" id="28" name="DAC Out 1" description="$$DESCR_AS_NAME$$" unit="" type="8" access_mode="2" value="0" value_previous="0" minimum="0" maximum="255" period="0" modified="false" visible="true" enable="true" />
<reg version="1.0" id="29" name="DAC Out 2" description="$$DESCR_AS_NAME$$" unit="" type="8" access_mode="2" value="0" value_previous="0" minimum="0" maximum="255" period="0" modified="false" visible="true" enable="true" />
<reg version="1.0" id="30" name="Speed measured" description="$$DESCR_AS_NAME$$" unit="RPM" type="160" access_mode="0" value="-768" value_previous="-768" minimum="-14868" maximum="14868" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="31" name="Torque measured (Iq)" description="$$DESCR_AS_NAME$$" unit="" type="144" access_mode="0" value="5169" value_previous="5234" minimum="-32768" maximum="32767" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="32" name="Flux measured (Id)" description="$$DESCR_AS_NAME$$" unit="" type="144" access_mode="0" value="45" value_previous="-103" minimum="-32768" maximum="32767" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="30" name="Speed measured" description="$$DESCR_AS_NAME$$" unit="RPM" type="160" access_mode="0" value="0" value_previous="0" minimum="-14868" maximum="14868" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="31" name="Torque measured (Iq)" description="$$DESCR_AS_NAME$$" unit="" type="144" access_mode="0" value="0" value_previous="0" minimum="-32768" maximum="32767" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="32" name="Flux measured (Id)" description="$$DESCR_AS_NAME$$" unit="" type="144" access_mode="0" value="0" value_previous="0" minimum="-32768" maximum="32767" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="33" name="Flux weakening BUS Voltage allowed percentage measured" description="$$DESCR_AS_NAME$$" unit="" type="16" access_mode="0" value="0" value_previous="0" minimum="0" maximum="65535" period="200" modified="false" visible="true" enable="false" />
<reg version="1.0" id="34" name="Revup stage numbers" description="$$DESCR_AS_NAME$$" unit="" type="8" access_mode="0" value="0" value_previous="0" minimum="0" maximum="255" period="0" modified="false" visible="true" enable="true" />
<reg version="1.0" id="35" name="Ia" description="$$DESCR_AS_NAME$$" unit="" type="144" access_mode="0" value="0" value_previous="0" minimum="-32768" maximum="32767" period="0" modified="false" visible="true" enable="false" />
Expand Down Expand Up @@ -2078,7 +2078,7 @@
<reg version="1.0" id="88" name="PFC Voltage loop Kd" description="$$DESCR_AS_NAME$$" unit="" type="16" access_mode="2" value="0" value_previous="0" minimum="0" maximum="65535" period="0" modified="false" visible="true" enable="false" />
<reg version="1.0" id="89" name="PFC startup duration" description="$$DESCR_AS_NAME$$" unit="ms" type="16" access_mode="2" value="0" value_previous="0" minimum="0" maximum="65535" period="0" modified="false" visible="true" enable="false" />
<reg version="1.0" id="90" name="PFC abilitation status" description="$$DESCR_AS_NAME$$" unit="" type="8" access_mode="0" value="0" value_previous="0" minimum="0" maximum="1" period="0" modified="false" visible="true" enable="false" />
<reg version="1.0" id="91" name="Ramp final speed" description="$$DESCR_AS_NAME$$" unit="RMS" type="160" access_mode="2" value="2376" value_previous="2376" minimum="-14868" maximum="14868" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="91" name="Ramp final speed" description="$$DESCR_AS_NAME$$" unit="RMS" type="160" access_mode="2" value="1080" value_previous="1080" minimum="-14868" maximum="14868" period="200" modified="false" visible="true" enable="true" />
<reg version="1.0" id="92" name="Ramp duration" description="$$DESCR_AS_NAME$$" unit="ms" type="16" access_mode="2" value="0" value_previous="0" minimum="0" maximum="65535" period="0" modified="false" visible="true" enable="false" />
<reg version="1.0" id="93" name="HFI electrical angle" description="$$DESCR_AS_NAME$$" unit="" type="144" access_mode="0" value="0" value_previous="0" minimum="-32768" maximum="32767" period="0" modified="false" visible="true" enable="false" />
<reg version="1.0" id="94" name="HFI rotor speed" description="$$DESCR_AS_NAME$$" unit="" type="144" access_mode="0" value="0" value_previous="0" minimum="-32768" maximum="32767" period="0" modified="false" visible="true" enable="false" />
Expand Down
4 changes: 2 additions & 2 deletions f446-ihm08-castle/Inc/drive_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#define HALL_AVERAGING_FIFO_DEPTH 6 /*!< depth of the FIFO used to
average mechanical speed in
0.1Hz resolution */
#define HALL_MTPA 1
#define HALL_MTPA true

/* USER CODE BEGIN angle reconstruction M1 */
#define REV_PARK_ANGLE_COMPENSATION_FACTOR 0
Expand Down Expand Up @@ -103,7 +103,7 @@
/* USER CODE END PID_SPEED_INTEGRAL_INIT_DIV */

#define SPD_DIFFERENTIAL_TERM_ENABLING DISABLE
#define IQMAX 5143
#define IQMAX 30860

/* Default settings */
#define DEFAULT_CONTROL_MODE STC_SPEED_MODE /*!< STC_TORQUE_MODE or
Expand Down
20 changes: 18 additions & 2 deletions f446-ihm08-castle/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ void spilog(uint8_t file, uint16_t loc, const char* fmt, ...);

// @precondition a static Module declaration is necessary for all callers
// @note ## is necessary to support 0 argument
#define SPI_LOG(fmt_, ...) spilog(Q_this_module_, __LINE__, fmt_, ##__VA_ARGS__)
#define SPI_LOG(fmt_, ...) //spilog(Q_this_module_, __LINE__, fmt_, ##__VA_ARGS__)

// @brief Just a "I am here" message
// @precondition a static File declaration is necessary in all files that calls SPI_LOG
#define SPI_TRACE() spilog(Q_this_module_, __LINE__, NULL)
#define SPI_TRACE() //spilog(Q_this_module_, __LINE__, NULL)
/* USER CODE END EM */

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
Expand Down Expand Up @@ -133,6 +133,22 @@ void Error_Handler(void);
# define GREEN_ON()
# define GREEN_OFF()
#endif

#ifdef DBG1_Pin
# define DBG1_ON() (DBG1_GPIO_Port)->ODR |= DBG1_Pin
# define DBG1_OFF() (DBG1_GPIO_Port)->ODR &= ~DBG1_Pin
#else
# define DBG1_ON()
# define DBG1_OFF()
#endif

#ifdef DBG2_Pin
# define DBG2_ON() (DBG2_GPIO_Port)->ODR |= DBG2_Pin
# define DBG2_OFF() (DBG2_GPIO_Port)->ODR &= ~DBG2_Pin
#else
# define DBG2_ON()
# define DBG2_OFF()
#endif
/* USER CODE END Private defines */

#ifdef __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions f446-ihm08-castle/Inc/pmsm_motor_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
*Amplifying network gain)/(MCU supply voltage/2)
*/

#define NOMINAL_CURRENT 5143*3
#define NOMINAL_CURRENT 30860
#define MOTOR_MAX_SPEED_RPM 14868 /*!< Maximum rated speed */
#define MOTOR_VOLTAGE_CONSTANT 0.2 /*!< Volts RMS ph-ph /kRPM */
#define ID_DEMAG -5143 /*!< Demagnetization current */
#define ID_DEMAG -30860 /*!< Demagnetization current */

/***************** MOTOR SENSORS PARAMETERS ******************************/
/* Motor sensors parameters are always generated but really meaningful only
Expand Down
94 changes: 49 additions & 45 deletions f446-ihm08-castle/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ ADC_HandleTypeDef hadc2;

CAN_HandleTypeDef hcan1;

SPI_HandleTypeDef hspi2;
SPI_HandleTypeDef hspi3;
DMA_HandleTypeDef hdma_spi2_tx;
DMA_HandleTypeDef hdma_spi3_tx;

TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;

UART_HandleTypeDef huart2;
UART_HandleTypeDef huart6;
DMA_HandleTypeDef hdma_usart6_tx;

/* USER CODE BEGIN PV */

Expand All @@ -72,7 +72,7 @@ static void MX_TIM2_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_CAN1_Init(void);
static void MX_SPI3_Init(void);
static void MX_USART6_UART_Init(void);
static void MX_SPI2_Init(void);
static void MX_NVIC_Init(void);
/* USER CODE BEGIN PFP */

Expand Down Expand Up @@ -126,7 +126,7 @@ int main(void)
MX_MotorControl_Init();
MX_CAN1_Init();
MX_SPI3_Init();
MX_USART6_UART_Init();
MX_SPI2_Init();

/* Initialize interrupts */
MX_NVIC_Init();
Expand Down Expand Up @@ -410,11 +410,11 @@ static void MX_CAN1_Init(void)

/* USER CODE END CAN1_Init 1 */
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 3;
hcan1.Init.Prescaler = 5;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_8TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_7TQ;
hcan1.Init.SyncJumpWidth = CAN_SJW_3TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_3TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_3TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = DISABLE;
hcan1.Init.AutoWakeUp = DISABLE;
Expand All @@ -431,6 +431,44 @@ static void MX_CAN1_Init(void)

}

/**
* @brief SPI2 Initialization Function
* @param None
* @retval None
*/
static void MX_SPI2_Init(void)
{

/* USER CODE BEGIN SPI2_Init 0 */

/* USER CODE END SPI2_Init 0 */

/* USER CODE BEGIN SPI2_Init 1 */

/* USER CODE END SPI2_Init 1 */
/* SPI2 parameter configuration*/
hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI2_Init 2 */

/* USER CODE END SPI2_Init 2 */

}

/**
* @brief SPI3 Initialization Function
* @param None
Expand Down Expand Up @@ -649,39 +687,6 @@ static void MX_USART2_UART_Init(void)

}

/**
* @brief USART6 Initialization Function
* @param None
* @retval None
*/
static void MX_USART6_UART_Init(void)
{

/* USER CODE BEGIN USART6_Init 0 */

/* USER CODE END USART6_Init 0 */

/* USER CODE BEGIN USART6_Init 1 */

/* USER CODE END USART6_Init 1 */
huart6.Instance = USART6;
huart6.Init.BaudRate = 3000000;
huart6.Init.WordLength = UART_WORDLENGTH_8B;
huart6.Init.StopBits = UART_STOPBITS_1;
huart6.Init.Parity = UART_PARITY_NONE;
huart6.Init.Mode = UART_MODE_TX;
huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart6.Init.OverSampling = UART_OVERSAMPLING_8;
if (HAL_UART_Init(&huart6) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART6_Init 2 */

/* USER CODE END USART6_Init 2 */

}

/**
* Enable DMA controller clock
*/
Expand All @@ -690,15 +695,14 @@ static void MX_DMA_Init(void)

/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
__HAL_RCC_DMA2_CLK_ENABLE();

/* DMA interrupt init */
/* DMA1_Stream4_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream4_IRQn);
/* DMA1_Stream5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
/* DMA2_Stream6_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 15, 0);
HAL_NVIC_EnableIRQ(DMA2_Stream6_IRQn);

}

Expand Down
Loading

0 comments on commit e2f3a02

Please sign in to comment.