You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.0 KiB
97 lines
3.0 KiB
#include "apphal.hpp"
|
|
#include "bsp.h"
|
|
#include "appcfg.h"
|
|
using namespace iflytop;
|
|
|
|
SPI_HandleTypeDef hspi1;
|
|
|
|
#define MOTOR_SPI_SCK_PIN GPIO_PIN_5
|
|
#define MOTOR_SPI_SDO_PIN GPIO_PIN_6
|
|
#define MOTOR_SPI_SDI_PIN GPIO_PIN_5
|
|
|
|
#define MOTOR_SPI_SCK_PORT GPIOA
|
|
#define MOTOR_SPI_SDO_PORT GPIOA
|
|
#define MOTOR_SPI_SDI_PORT GPIOB
|
|
|
|
void AppHal::tmc_spi_init() {
|
|
#if (MOTOR_SPI_INDEX == 1)
|
|
|
|
__HAL_RCC_SPI1_CLK_ENABLE();
|
|
// __HAL_RCC_GPIOA_CLK_ENABLE();
|
|
// 使能相关 GPIO 端口时钟
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
|
|
static_assert(&MOTOR_SPI_INS == &hspi1);
|
|
#if 0
|
|
hspi1.Instance = SPI1;
|
|
hspi1.Init.Mode = SPI_MODE_MASTER;
|
|
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
|
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
|
|
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
|
|
hspi1.Init.NSS = SPI_NSS_SOFT;
|
|
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
|
|
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
|
|
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
|
hspi1.Init.CRCPolynomial = 10;
|
|
#else
|
|
hspi1.Instance = SPI1;
|
|
hspi1.Init.Mode = SPI_MODE_MASTER;
|
|
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
|
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
|
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
|
|
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
|
|
hspi1.Init.NSS = SPI_NSS_SOFT;
|
|
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
|
|
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
|
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
|
|
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
|
hspi1.Init.CRCPolynomial = 10;
|
|
#endif
|
|
if (HAL_SPI_Init(&hspi1) != HAL_OK) {
|
|
Error_Handler();
|
|
}
|
|
|
|
static_assert(MOTOR_SPI_SCK == PA5);
|
|
static_assert(MOTOR_SPI_SDO == PA6);
|
|
static_assert(MOTOR_SPI_SDI == PB5);
|
|
|
|
#if 0
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
#else
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
// 使能相关 GPIO 端口时钟
|
|
// __HAL_RCC_GPIOA_CLK_ENABLE();
|
|
// __HAL_RCC_GPIOB_CLK_ENABLE();
|
|
|
|
// 配置 SCK 引脚
|
|
GPIO_InitStruct.Pin = MOTOR_SPI_SCK_PIN;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
|
|
HAL_GPIO_Init(MOTOR_SPI_SCK_PORT, &GPIO_InitStruct);
|
|
|
|
// 配置 SDO 引脚
|
|
GPIO_InitStruct.Pin = MOTOR_SPI_SDO_PIN;
|
|
HAL_GPIO_Init(MOTOR_SPI_SDO_PORT, &GPIO_InitStruct);
|
|
|
|
// 配置 SDI 引脚
|
|
GPIO_InitStruct.Pin = MOTOR_SPI_SDI_PIN;
|
|
HAL_GPIO_Init(MOTOR_SPI_SDI_PORT, &GPIO_InitStruct);
|
|
#endif
|
|
|
|
|
|
#else
|
|
#error "MOTOR_SPI_INDEX not supported"
|
|
#endif
|
|
}
|