检查RCC和/或MODER寄存器是否正确应用。
尝试在设置RCC寄存器后添加一些延迟。
也许你的编译器优化的东西,下面的条件不满足:
When the peripheral clock is not active, the peripheral registers read or write accesses are not supported.
The enable bit has a synchronization mechanism to create a glitch free clock for the peripheral.
After the enable bit is set, there is a 2 clock cycles delay before the clock be active.
Caution:
Just after enabling the clock for a peripheral, software must wait for a delay before accessing the peripheral registers.
编辑:
注:以下是一个解决的办法,似乎是错误的。 有关详细信息,请参阅注释。
我只是编译上述init_adc_gpio功能和我的编译器生成以下汇编指令(-O3):
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN; // Enable clock for GPIOC
0x080004d0 ldr r3, [pc, 60] ; (0x8000510 <init_adc_gpio+64>)
GPIOE->MODER &= (uint32_t)0x000FFFFFU; // Pins 10-15 of E -> input (6 least significant bits of ADC data)
0x080004d2 ldr r0, [pc, 64] ; (0x8000514 <init_adc_gpio+68>)
0x080004d4 ldr r2, [r3, 76] ; 0x4c
GPIOH->MODER &= (uint32_t)0xFFFFFFFCU; // Pin 0 of H -> input (ADC data ready flag)
0x080004d6 ldr r1, [pc, 64] ; (0x8000518 <init_adc_gpio+72>)
0x080004d8 orr.w r2, r2, 4
void init_adc_gpio(void) {
0x080004dc push {r4}
0x080004de str r2, [r3, 76] ; 0x4c
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOEEN; // GPIOE
0x080004e0 ldr r2, [r3, 76] ; 0x4c
GPIOC->MODER &= (uint32_t)0x0FFFFFFFU; // Pins 14-15 of C -> input (2 most significant bits of ADC data)
0x080004e2 ldr r4, [pc, 56] ; (0x800051c <init_adc_gpio+76>)
0x080004e4 orr.w r2, r2, 16
0x080004e8 str r2, [r3, 76] ; 0x4c
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOHEN; // GPIOH
0x080004ea ldr r2, [r3, 76] ; 0x4c
0x080004ec orr.w r2, r2, 128 ; 0x80
0x080004f0 str r2, [r3, 76] ; 0x4c
0x080004f2 ldr r3, [r4, 0]
0x080004f4 bic.w r3, r3, 4026531840 ; 0xf0000000
0x080004f8 str r3, [r4, 0]
0x080004fa ldr r3, [r0, 0]
}
0x080004fc ldr.w r4, [sp], 4
0x08000500 ubfx r3, r3, 0, 20
0x08000504 str r3, [r0, 0]
0x08000506 ldr r3, [r1, 0]
0x08000508 bic.w r3, r3, 3
0x0800050c str r3, [r1, 0]
0x0800050e bx lr
0x08000510 .word 0x40021000 ; [RCC]
0x08000514 .word 0x48001000 ; [GPIOE]
0x08000518 .word 0x48001c00 ; [GPIOH]
0x0800051c .word 0x48000800 ; [GPIOC]
正如你所看到的编译器重新排序的说明。因此寄存器设置不正确。 注意:其实序列是正确的。反汇编程序如何显示它可能会误导某人。设置寄存器MODER应该做它之前也
void init_adc_gpio(void) {
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN; // Enable clock for GPIOC
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOEEN; // GPIOE
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOHEN; // GPIOH
asm("" ::: "memory"); // prevent instruction reordering
GPIOC->MODER &= (uint32_t)0x0FFFFFFFU; // Pins 14-15 of C -> input (2 most significant bits of ADC data)
GPIOE->MODER &= (uint32_t)0x000FFFFFU; // Pins 10-15 of E -> input (6 least significant bits of ADC data)
GPIOH->MODER &= (uint32_t)0xFFFFFFFCU; // Pin 0 of H -> input (ADC data ready flag)
}
一个简单的NOP指令:
为了解决这个问题,你可以使用一个explicit compiler barrier至极阻止编译器重新排序的命令。
注:编译器障碍导致略有不同的汇编代码。但两者仍然正确。
向上一级搜索。 GPIOx-> IDR寄存器是否包含正确的值? – Jeroen3