gpt4 book ai didi

c - STM32F103C8/CoIDE/ST-LinkV2 无法更新寄存器

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:48 25 4
gpt4 key购买 nike

我是 STM32 的新手,而不是嵌入式开发(具有 PIC、dsPIC、MSP430 的经验)。

环境

  • CooCox CoIDE
  • arm-none-eabi-gcc
  • ST-LINK V2(与 Segger J-Link 相同)在 SWD 上

问题描述

我正在尝试使用直接寄存器操作编写一个基本库(STMCube HAL 很酷,但我发现无论如何我都必须阅读数据表,因此首选寄存器操作)。本质上,我只是想使 Blue Pill 开发板上的 LED 闪烁,该开发板上的引脚为 C13

我可以编译、刷新和调试,但是当我调试时,LED 不闪烁并且寄存器没有变化。寄存器值本身实际上并没有那么重要,因此您不需要出去查看数据表。我只需要能够操纵它们!

单步执行调试器工作正常,variables 观察窗口也适当更新。

我尝试过的事情

虽然这并不详尽(已经几个小时了,所以我可能忘记包括一些),但这些是我尝试过的一些事情:

  • 更改BOOTx 引脚配置
  • 更改调试器配置(减慢、加速、重置策略)。
  • 将调试器更改为 SEGGER J-Link
  • 锁定/解锁 GPIO 引脚

所有的结果都是一样的。我怀疑我可能在 CMSIS 中遗漏了一些东西或一个必要的包含文件,我似乎找不到它。

我可能遗漏的另一点涉及振荡器设置。据我了解,启动时有一个默认配置,除非我想更改它,否则我不必担心。也许这是有缺陷的?

编辑:适用于 STM32CubeMX HAL

当我尝试使用 STM32CUBEMX HAL 进行基本切换时,它起作用了。这让我觉得我在包含、项目设置、振荡器设置等方面缺少一些基本的东西?

代码

主.c

#include "gpio.h"

#define LED_PIN 13


int main(void)
{
uint32_t i;

// initialize the peripherals
GPIO_init();
GPIOC_setupOutput(LED_PIN, PUSH_PULL, PIN_SPEED_50MHz);

while(1)
{
GPIOC_setPin(LED_PIN);
for(i=0; i<4000000; i++);
GPIOC_clearPin(LED_PIN);
for(i=0; i<4000000; i++);
}
}

gpio.h

#ifndef _GPIO_H
#define _GPIO_H

#include <stdint.h>
#include <stdbool.h>

typedef enum{
ANALOG=0b00, FLOATING=0b01, PULL_UP_PULL_DOWN=0b10
}InputPinMode;

typedef enum{
PUSH_PULL=0b00, OPEN_DRAIN=0b01, AF_PUSH_PULL=0b10, AF_OPEN_DRAIN=0b11
}OutputPinMode;

typedef enum{
PIN_SPEED_2MHz=0b10, PIN_SPEED_10MHz=0b01, PIN_SPEED_50MHz=0b11
}PinSpeed;

void GPIO_init(void);
void GPIOA_setupInput(uint8_t pinNumber, InputPinMode mode);
void GPIOC_setupInput(uint8_t pinNumber, InputPinMode mode);
void GPIOA_setupOutput(uint8_t pinNumber, OutputPinMode mode, PinSpeed speed);
void GPIOC_setupOutput(uint8_t pinNumber, OutputPinMode mode, PinSpeed speed);

bool GPIOA_readPin(uint8_t pinNumber);


void GPIOA_setPin(uint8_t pinNumber);
void GPIOC_setPin(uint8_t pinNumber);

void GPIOA_clearPin(uint8_t pinNumber);
void GPIOC_clearPin(uint8_t pinNumber);

#endif

gpio.c

#include "stm32f1xx.h"
#include "gpio.h"

/**
* @brief Initialize GPIO
*/
void GPIO_init(void){
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
RCC->APB2ENR |= RCC_APB2ENR_IOPDEN;
RCC->APB2ENR |= RCC_APB2ENR_IOPEEN;
}

/**
* @brief Setup pin as an input
* @param pinNumber the pin number
* @param mode the input mode
*/
void GPIOA_setupInput(uint8_t pinNumber, InputPinMode mode){
uint32_t pinNumberLocation, regValue;

if(pinNumber < 8){
pinNumberLocation = pinNumber << 2; // bit location
regValue = (mode << 2) << pinNumberLocation;

GPIOA->CRL &= ~(0b1111 << pinNumberLocation); // clear the register
GPIOA->CRL = regValue;
}else{
pinNumberLocation = (pinNumber - 8) << 2; // bit location
regValue = (mode << 2) << pinNumberLocation;

GPIOA->CRH &= ~(0b1111 << pinNumberLocation); // clear the register
GPIOA->CRH = regValue;
}
}

/**
* @brief Setup port A pin as an output
* @brief pinNumber the pin number
* @brief mode the output mode
* @brief speed the pin speed (lower results in less noise)
*/
void GPIOA_setupOutput(uint8_t pinNumber, OutputPinMode mode, PinSpeed speed){
uint32_t pinNumberLocation, regValue;

if(pinNumber < 8){
pinNumberLocation = pinNumber << 2; // bit location
regValue = ((mode << 2) << pinNumberLocation) + speed;

GPIOA->CRL &= ~(0b1111 << pinNumberLocation); // clear the register
GPIOA->CRL |= regValue;
}else{
pinNumberLocation = (pinNumber - 8) << 2; // bit location
regValue = ((mode << 2) << pinNumberLocation) + speed;

GPIOA->CRH &= ~(0b1111 << pinNumberLocation); // clear the register
GPIOA->CRH |= regValue;
}
}

/**
* @brief Setup port C pin as an output
* @brief pinNumber the pin number
* @brief mode the output mode
* @brief speed the pin speed (lower results in less noise)
*/
void GPIOC_setupOutput(uint8_t pinNumber, OutputPinMode mode, PinSpeed speed){
uint32_t pinNumberLocation, regValue;

if(pinNumber < 8){
pinNumberLocation = pinNumber << 2; // bit location
regValue = ((mode << 2) << pinNumberLocation) + speed;

GPIOC->CRL &= ~(0b1111 << pinNumberLocation); // clear the register
GPIOC->CRL |= regValue;
}else{
pinNumberLocation = (pinNumber - 8) << 2; // bit location
regValue = ((mode << 2) << pinNumberLocation) + speed;

GPIOC->CRH &= ~(0b1111 << pinNumberLocation); // clear the register
GPIOC->CRH |= regValue;
}
}

bool GPIOA_readPin(uint8_t pinNumber){
uint16_t mask = 1 << pinNumber;

if(GPIOA->IDR & mask)
return true;
else
return false;
}

void GPIOA_setPin(uint8_t pinNumber){ GPIOA->BSRR = (1 << pinNumber);}
void GPIOC_setPin(uint8_t pinNumber){ GPIOC->BSRR = (1 << pinNumber);}

void GPIOA_clearPin(uint8_t pinNumber){ GPIOA->BSRR = ~(1 << (pinNumber + 16));}
void GPIOC_clearPin(uint8_t pinNumber){ GPIOC->BSRR = ~(1 << (pinNumber + 16));}

最佳答案

PB2 上的二极管 - 工作代码

volatile uint32_t delay;
#include "stm32f10x.h"
int main(void)
{
RCC->APB2ENR = RCC_APB2ENR_IOPBEN;
GPIOB->CRL |= GPIO_CRL_MODE2_1;
GPIOB->CRL &= ~GPIO_CRL_CNF2_0;

while(1)
{
GPIOB->ODR |= GPIO_ODR_ODR2; //or GPIO_ODR_2 depending of the .h header version
for(delay = 2000000; delay; delay--);
GPIOB->ODR &= ~GPIO_ODR_ODR2; //or GPIO_ODR_2 depending of the .h header version
for(delay = 2000000; delay; delay--);

GPIOB->BSRR = 1 << 2;
for(delay = 2000000; delay; delay--);
GPIOB->BSRR = 1 << (2 + 16);
for(delay = 2000000; delay; delay--);
}
} // change

关于c - STM32F103C8/CoIDE/ST-LinkV2 无法更新寄存器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44973472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com