打印
[STM32G4]

stm32g431 SPI1复用输出都是浮空的

[复制链接]
251|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
MOSI和SCK测出来都是浮空的

但如果模式改为输出的话又正常

实在想不出哪有问题了

RCC->AHB2ENR|=RCC_AHB2ENR_GPIOAEN;//GPIOA ENABLE

RCC->AHB2RSTR|=0x1<<RCC_AHB2RSTR_GPIOARST_Pos;//GPIOA RST
RCC->AHB2RSTR&=~(0x1<<RCC_AHB2RSTR_GPIOARST_Pos);

//MODE AF
GPIOA->MODER&=~(0x3<<GPIO_MODER_MODE5_Pos);
GPIOA->MODER&=~(0x3<<GPIO_MODER_MODE7_Pos);
GPIOA->MODER|=0x2<<GPIO_MODER_MODE5_Pos;
GPIOA->MODER|=0x2<<GPIO_MODER_MODE7_Pos;
//OUT PUT TYPE PP
GPIOA->OTYPER&=~(0x1<<(GPIO_OTYPER_OT5_Pos));
GPIOA->OTYPER&=~(0x1<<(GPIO_OTYPER_OT7_Pos));
GPIOA->OTYPER|=0x0<<(GPIO_OTYPER_OT5_Pos);
GPIOA->OTYPER|=0x0<<(GPIO_OTYPER_OT7_Pos);

//SPEED VERY HIGH
GPIOA->OSPEEDR&=~(0x3<<(GPIO_OSPEEDR_OSPEED5_Pos));
GPIOA->OSPEEDR&=~(0x3<<(GPIO_OSPEEDR_OSPEED7_Pos));
GPIOA->OSPEEDR|=0x3<<(GPIO_OSPEEDR_OSPEED5_Pos);
GPIOA->OSPEEDR|=0x3<<(GPIO_OSPEEDR_OSPEED7_Pos);

//NO PULL
GPIOA->PUPDR&=~(0x3<<(GPIO_PUPDR_PUPD5_Pos));
GPIOA->PUPDR&=~(0x3<<(GPIO_PUPDR_PUPD7_Pos));
GPIOA->PUPDR|=0x0<<(GPIO_PUPDR_PUPD5_Pos);
GPIOA->PUPDR|=0x0<<(GPIO_PUPDR_PUPD7_Pos);
//AF config
GPIOA->AFR[0]&=~(0xF<<GPIO_AFRL_AFSEL5_Pos);
GPIOA->AFR[0]&=~(0xF<<GPIO_AFRL_AFSEL7_Pos);
GPIOA->AFR[0]|=0x5<<GPIO_AFRL_AFSEL5_Pos;
GPIOA->AFR[0]|=0x5<<GPIO_AFRL_AFSEL7_Pos;

//SPI_INIT
RCC->APB2ENR |= 0x1<<RCC_APB2ENR_SPI1EN_Pos;//Enable SPI1

RCC->APB2RSTR|=0x1<<RCC_APB2RSTR_SPI1RST_Pos;//SPI1 RST
RCC->APB2RSTR&=~(0x1<<RCC_APB2RSTR_SPI1RST_Pos);

SPI1->CR1=0x00000000;//Clear the register
SPI1->CR2=0x00000000;//Clear the register

SPI1->CR1|=0x0<<SPI_CR1_BIDIMODE_Pos;//2 Line Mode
SPI1->CR1|=0x1<<SPI_CR1_BIDIOE_Pos;//transmit-only mode
SPI1->CR1|=0x0<<SPI_CR1_CRCEN_Pos;//disable CRC
SPI1->CR1|=0x1<<SPI_CR1_SSM_Pos;//Software slave management enabled
SPI1->CR1|=0x0<<SPI_CR1_LSBFIRST_Pos;// transmitted / received with the MSB first
SPI1->CR1|=0x0<<SPI_CR1_BR_Pos;//Speed = APB2CLK/2^(BR+1)
SPI1->CR1|=0x1<<SPI_CR1_MSTR_Pos;//Master Mode

SPI1->CR1|=0x0<<SPI_CR1_CPOL_Pos;
SPI1->CR1|=0x0<<SPI_CR1_CPHA_Pos;

SPI1->CR2|=0x7<<SPI_CR2_DS_Pos;//Data length 8 bits
SPI1->CR2|=0x0<<SPI_CR2_FRF_Pos;//Motorola mode

SPI1->CR1|=0x1<<SPI_CR1_SPE_Pos;;//SPI ENABLE

使用特权

评论回复
沙发
香水城| | 2023-7-26 15:01 | 只看该作者
说实话,你这些代码别人没办法看出问题来。

建议你使用ST提供的库函数跑跑例程感受下,然后使用CubeMx进行配置创建工程进行调试。

\Repository\STM32Cube_FW_G4_V1.5.1\Projects\NUCLEO-G431RB\Examples\SPI

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1181

主题

5171

帖子

12

粉丝