可以读取到SPI数据了

This commit is contained in:
许晟昊 2024-12-23 15:17:31 +08:00
parent 87a7d76f6d
commit 5dd03a2628
11 changed files with 1252 additions and 1285 deletions

View File

@ -89,10 +89,6 @@ void Error_Handler(void);
#define TMC2240_MISO_GPIO_Port GPIOA
#define TMC2240_MOSI_Pin LL_GPIO_PIN_7
#define TMC2240_MOSI_GPIO_Port GPIOA
#define TMC2240_STEP_Pin LL_GPIO_PIN_0
#define TMC2240_STEP_GPIO_Port GPIOB
#define TMC2240_DIR_Pin LL_GPIO_PIN_1
#define TMC2240_DIR_GPIO_Port GPIOB
#define OLED_SDA_Pin LL_GPIO_PIN_12
#define OLED_SDA_GPIO_Port GPIOB
#define OLDE_SCK_Pin LL_GPIO_PIN_13

View File

@ -59,7 +59,7 @@ void MX_GPIO_Init(void)
LL_GPIO_ResetOutputPin(GPIOA, TMC2240_EN_Pin|TMC2240_CS_Pin);
/**/
LL_GPIO_ResetOutputPin(GPIOB, TMC2240_STEP_Pin|TMC2240_DIR_Pin|OLED_SDA_Pin|OLDE_SCK_Pin);
LL_GPIO_ResetOutputPin(GPIOB, OLED_SDA_Pin|OLDE_SCK_Pin);
/**/
GPIO_InitStruct.Pin = LED_BLUE_Pin;
@ -80,32 +80,17 @@ void MX_GPIO_Init(void)
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/**/
GPIO_InitStruct.Pin = TMC2240_EN_Pin;
GPIO_InitStruct.Pin = TMC2240_EN_Pin|TMC2240_CS_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
LL_GPIO_Init(TMC2240_EN_GPIO_Port, &GPIO_InitStruct);
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/**/
GPIO_InitStruct.Pin = TMC2240_CS_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(TMC2240_CS_GPIO_Port, &GPIO_InitStruct);
/**/
GPIO_InitStruct.Pin = TMC2240_STEP_Pin|TMC2240_DIR_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/**/
GPIO_InitStruct.Pin = LL_GPIO_PIN_2|LL_GPIO_PIN_10|LL_GPIO_PIN_11|LL_GPIO_PIN_14
|LL_GPIO_PIN_15|LL_GPIO_PIN_3|LL_GPIO_PIN_4|LL_GPIO_PIN_5
|LL_GPIO_PIN_6|LL_GPIO_PIN_7|LL_GPIO_PIN_8|LL_GPIO_PIN_9;
GPIO_InitStruct.Pin = LL_GPIO_PIN_0|LL_GPIO_PIN_1|LL_GPIO_PIN_2|LL_GPIO_PIN_10
|LL_GPIO_PIN_11|LL_GPIO_PIN_14|LL_GPIO_PIN_15|LL_GPIO_PIN_3
|LL_GPIO_PIN_4|LL_GPIO_PIN_5|LL_GPIO_PIN_6|LL_GPIO_PIN_7
|LL_GPIO_PIN_8|LL_GPIO_PIN_9;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);

View File

@ -47,7 +47,7 @@ void MX_SPI1_Init(void)
*/
GPIO_InitStruct.Pin = TMC2240_SCK_Pin|TMC2240_MOSI_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
@ -64,7 +64,7 @@ void MX_SPI1_Init(void)
SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_HIGH;
SPI_InitStruct.ClockPhase = LL_SPI_PHASE_2EDGE;
SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV256;
SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV16;
SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
SPI_InitStruct.CRCPoly = 10;

View File

@ -125,7 +125,7 @@
<SetRegEntry>
<Number>0</Number>
<Key>CMSIS_AGDI</Key>
<Name>-X"Any" -UAny -O206 -S0 -C0 -P00000000 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO65554 -TC10000000 -TT10000000 -TP20 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0CS32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:CS32F103C8$Flash\CS32F10x_128.FLM)</Name>
<Name>-X"Any" -UAny -O206 -S8 -C0 -P00000000 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO65554 -TC10000000 -TT10000000 -TP20 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0CS32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:CS32F103C8$Flash\CS32F10x_128.FLM)</Name>
</SetRegEntry>
<SetRegEntry>
<Number>0</Number>
@ -153,7 +153,7 @@
<Ww>
<count>0</count>
<WinNumber>1</WinNumber>
<ItemText>_tmc2240</ItemText>
<ItemText>_tmc2240,0x10</ItemText>
</Ww>
<Ww>
<count>1</count>

View File

@ -54,7 +54,7 @@
<CreateLib>0</CreateLib>
<CreateHexFile>1</CreateHexFile>
<DebugInformation>1</DebugInformation>
<BrowseInformation>1</BrowseInformation>
<BrowseInformation>0</BrowseInformation>
<ListingPath></ListingPath>
<HexFormatSelection>1</HexFormatSelection>
<Merge32K>0</Merge32K>
@ -134,7 +134,7 @@
<RunIndependent>0</RunIndependent>
<UpdateFlashBeforeDebugging>1</UpdateFlashBeforeDebugging>
<Capability>1</Capability>
<DriverSelection>4101</DriverSelection>
<DriverSelection>4096</DriverSelection>
</Flash1>
<bUseTDR>1</bUseTDR>
<Flash2>BIN\UL2CM3.DLL</Flash2>

File diff suppressed because it is too large Load Diff

View File

@ -43,9 +43,6 @@ void board_init(void)
{
GPIO_RESET(TMC2240_EN_GPIO_Port, TMC2240_EN_Pin);
GPIO_SET(TMC2240_CS_GPIO_Port, TMC2240_CS_Pin);
GPIO_RESET(TMC2240_STEP_GPIO_Port, TMC2240_STEP_Pin);
GPIO_RESET(TMC2240_DIR_GPIO_Port, TMC2240_DIR_Pin);
delay_ms(100);
spi_gpio_group_t gpios;
gpios.cs = gpio_create(TMC2240_CS_GPIO_Port, TMC2240_CS_Pin);

View File

@ -20,7 +20,7 @@ static void tmc2240_read(tmc2240_index_e index, uint8_t *wdata, uint8_t *rdata)
tmc2240_t *tmc = &_tmc2240[index];
tmc->spi->gpios.cs->reset(*tmc->spi->gpios.cs);
for (uint16_t i = 0; i < 5; i++) // 循环读取数据
for (uint16_t i = 0; i < 5; i++)
{
rdata[i] = tmc->spi->interface.u.normal.spi_send(tmc->spi, wdata[i]);
}
@ -31,7 +31,7 @@ static void tmc2240_read(tmc2240_index_e index, uint8_t *wdata, uint8_t *rdata)
__NOP();
tmc->spi->gpios.cs->reset(*tmc->spi->gpios.cs);
for (uint16_t i = 0; i < 5; i++) // 循环读取数据
for (uint16_t i = 0; i < 5; i++)
{
rdata[i] = tmc->spi->interface.u.normal.spi_send(tmc->spi, wdata[i]);
}
@ -45,6 +45,7 @@ void tmc2240_init(tmc2240_index_e index, SPI_TypeDef *SPIx, spi_gpio_group_t *gp
tmc->spi = spi_create(SPI_TYPE_NORMAL, *gpios, 0);
DBG_ASSERT(tmc->spi != NULL __DBG_LINE);
tmc->spi->interface.hardware_enable(tmc->spi, SPIx);
}
tmc2240_t *tmc2240_get(tmc2240_index_e index)
@ -60,12 +61,11 @@ void tmc2240_config(tmc2240_index_e index)
wdata[1] = 0x00;
wdata[2] = 0x00;
wdata[3] = 0x00;
wdata[4] = 0x02;
wdata[4] = 0xE4;
tmc2240_write(index, wdata);
osel_memset(wdata, 0, 5);
delay_ms(100);
// 读取 GCONF 寄存器
uint8_t rdata[5] = {0};
osel_memset(wdata, 0, 5);
wdata[0] = TMC2240_REG_GCONF;
tmc2240_read(index, wdata, rdata);
_tmc2240[index].data.gconf = rdata[4];
@ -78,11 +78,10 @@ int32_t tmc2240_position_read(tmc2240_index_e index)
uint8_t wdata[5] = {0};
uint8_t rdata[5] = {0};
wdata[0] = 0x21; // XACTUAL register address
wdata[0] = TMC2240_REG_XACTUAL; // XACTUAL register address
tmc2240_read(index, wdata, rdata);
int32_t response = 0;
response |= (rdata[1]);
response <<= 8;
response |= (rdata[2]);

View File

@ -1,3 +1,12 @@
/**
* @file tmc2240.h
* @author xushenghao
* @brief TMC2240驱动头文件
* @version 0.1
* @note
* 1. VM需要供电SPI无法正常通信
*/
#ifndef __TMC2240_H
#define __TMC2240_H
#include "main.h"

View File

@ -460,19 +460,16 @@ static uint8_t spi_read_write_byte(spi_t *handle, uint8_t tx_data)
}
else
{
if (spi_wait_flag(handle, LL_SPI_SR_TXE, SPI_TIMEOUT) == FALSE)
{
return 0xff;
}
LL_SPI_TransmitData8(handle->spi, tx_data);
if (spi_wait_flag(handle, LL_SPI_SR_TXE, SPI_TIMEOUT) == FALSE)
{
return 0xff;
}
if (spi_wait_flag(handle, LL_SPI_SR_RXNE, SPI_TIMEOUT) == FALSE)
{
return 0xff;
}
if (spi_wait_flag(handle, LL_SPI_SR_TXE, SPI_TIMEOUT) == FALSE)
{
return 0xff;
}
rdata = LL_SPI_ReceiveData8(handle->spi);
return rdata;
}

View File

@ -42,26 +42,24 @@ Mcu.Name=STM32F103C(8-B)Tx
Mcu.Package=LQFP48
Mcu.Pin0=PC13-TAMPER-RTC
Mcu.Pin1=PD0-OSC_IN
Mcu.Pin10=PB1
Mcu.Pin11=PB12
Mcu.Pin12=PB13
Mcu.Pin13=PA9
Mcu.Pin14=PA10
Mcu.Pin15=PA13
Mcu.Pin16=PA14
Mcu.Pin17=VP_SYS_VS_Systick
Mcu.Pin18=VP_TIM1_VS_ClockSourceINT
Mcu.Pin19=VP_TIM2_VS_ClockSourceINT
Mcu.Pin10=PB13
Mcu.Pin11=PA9
Mcu.Pin12=PA10
Mcu.Pin13=PA13
Mcu.Pin14=PA14
Mcu.Pin15=VP_SYS_VS_Systick
Mcu.Pin16=VP_TIM1_VS_ClockSourceINT
Mcu.Pin17=VP_TIM2_VS_ClockSourceINT
Mcu.Pin18=VP_TIM3_VS_ClockSourceINT
Mcu.Pin2=PD1-OSC_OUT
Mcu.Pin20=VP_TIM3_VS_ClockSourceINT
Mcu.Pin3=PA1
Mcu.Pin4=PA3
Mcu.Pin5=PA4
Mcu.Pin6=PA5
Mcu.Pin7=PA6
Mcu.Pin8=PA7
Mcu.Pin9=PB0
Mcu.PinsNb=21
Mcu.Pin9=PB12
Mcu.PinsNb=19
Mcu.ThirdParty0=RealThread.RT-Thread.3.1.5
Mcu.ThirdPartyNb=1
Mcu.UserConstants=
@ -93,34 +91,28 @@ PA14.Mode=Serial_Wire
PA14.Signal=SYS_JTCK-SWCLK
PA3.GPIOParameters=GPIO_PuPd,GPIO_Label
PA3.GPIO_Label=TMC2240_EN
PA3.GPIO_PuPd=GPIO_PULLDOWN
PA3.GPIO_PuPd=GPIO_NOPULL
PA3.Signal=GPIO_Output
PA4.GPIOParameters=GPIO_PuPd,GPIO_Label
PA4.GPIO_Label=TMC2240_CS
PA4.GPIO_PuPd=GPIO_PULLUP
PA4.GPIO_PuPd=GPIO_NOPULL
PA4.Signal=GPIO_Output
PA5.GPIOParameters=GPIO_Label
PA5.GPIOParameters=GPIO_Speed,GPIO_Label
PA5.GPIO_Label=TMC2240_SCK
PA5.GPIO_Speed=GPIO_SPEED_FREQ_MEDIUM
PA5.Mode=Full_Duplex_Master
PA5.Signal=SPI1_SCK
PA6.GPIOParameters=GPIO_Label
PA6.GPIO_Label=TMC2240_MISO
PA6.Mode=Full_Duplex_Master
PA6.Signal=SPI1_MISO
PA7.GPIOParameters=GPIO_Label
PA7.GPIOParameters=GPIO_Speed,GPIO_Label
PA7.GPIO_Label=TMC2240_MOSI
PA7.GPIO_Speed=GPIO_SPEED_FREQ_MEDIUM
PA7.Mode=Full_Duplex_Master
PA7.Signal=SPI1_MOSI
PA9.Mode=Asynchronous
PA9.Signal=USART1_TX
PB0.GPIOParameters=GPIO_Label
PB0.GPIO_Label=TMC2240_STEP
PB0.Locked=true
PB0.Signal=GPIO_Output
PB1.GPIOParameters=GPIO_Label
PB1.GPIO_Label=TMC2240_DIR
PB1.Locked=true
PB1.Signal=GPIO_Output
PB12.GPIOParameters=GPIO_Speed,PinState,GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultOutputPP
PB12.GPIO_Label=OLED_SDA
PB12.GPIO_ModeDefaultOutputPP=GPIO_MODE_OUTPUT_PP
@ -205,10 +197,10 @@ RealThread.RT-Thread.3.1.5.RT_USING_DEVICE=1
RealThread.RT-Thread.3.1.5_SwParameter=RTOSJjkernel\:true;RTOSJjshell\:true;RTOSJjdevice\:true;
SH.S_TIM2_CH2.0=TIM2_CH2,PWM Generation2 CH2
SH.S_TIM2_CH2.ConfNb=1
SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_256
SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_16
SPI1.CLKPhase=SPI_PHASE_2EDGE
SPI1.CLKPolarity=SPI_POLARITY_HIGH
SPI1.CalculateBaudRate=281.25 KBits/s
SPI1.CalculateBaudRate=4.5 MBits/s
SPI1.Direction=SPI_DIRECTION_2LINES
SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,BaudRatePrescaler,CLKPolarity,CLKPhase
SPI1.Mode=SPI_MODE_MASTER