#c #embedded #stm32 #nucleo
#c #встроенный #stm32 #nucleo
Вопрос:
Я пытаюсь включить внутренний светодиод на моем Nucleo-F446RE, но он всегда выключен.
План состоит в том, чтобы включить синхронизацию для GPIOA, затем установить GPIOA_5 в значение output, а затем установить его на high. Вот файлы (C file, Linkerscript, Bashscript для компиляции и прошивки).
Blink.c:
// Create references to symbols defined in the linker script
extern unsigned int _data_start;
extern unsigned int _data_end;
extern unsigned int _data_load;
extern unsigned int _bss_start;
extern unsigned int _bss_end;
void startup(); // Function prototype (forward declaration) for startup function
int main(); // Function prototype for main function
// Below we create an array of pointers which would form our vector table
// We use __attribute__ ((section(".vectors"))) to tell the compiler that we want the
// array to be placed in a memory section that we call ".vectors"
unsigned int * vectors[2] __attribute__ ((section(".vectors"))) =
{
(unsigned int *) 0x20020000, // Address of top of stack. 20kB = 1024 x 20 = 20480 bytes = 0x5000
(unsigned int *) startup // Address of the reset handler which is also our startup function
};
// The startup function, address was provided in the vector table
void startup()
{
volatile unsigned int *src, *dest;
// Copy data section values from load time memory address (LMA) to their address in SRAM
for (src = amp;_data_load, dest = amp;_data_start; dest < amp;_data_end; src , dest )
*dest = *src;
// Initialize all uninitialized variables (bss section) to 0
for (dest = amp;_bss_start; dest < amp;_bss_end; dest )
*dest = 0;
// Calling the main function
main();
while(1); // Normally main() should never return, but just incase we loop infinitely
}
// LED2 on PA5
#define GPIOA 0x40020000
#define RCC 0x40023800
#define GPIOA_MODER *((volatile char*) GPIOA 0x0)
#define GPIOA_BSRR *((volatile char*) GPIOA 0x18)
#define RCC_AHB1ENR *((volatile char*) RCC 0x30)
int main(){
RCC_AHB1ENR |= (1 << 0);
for(int i = 0; i < 10; i ){ // wait for a few cycles
asm("nop");
}
GPIOA_MODER |= (1 << 10); // set PA5 to output
GPIOA_MODER amp;= ~(1 << 11);
GPIOA_BSRR = (1 << 5); // set pin high
while(1){}
}
linker.ld:
MEMORY
{
rom (rx) : ORIGIN = 0x08000000, LENGTH = 512K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K
}
SECTIONS
{
.text : /* Define output file TEXT section */
{
*(.vectors) /* Vector table */
*(.text) /* Program code */
. = ALIGN(4); /* Make sure data that follows are aligned to 4 byte boundary */
*(.rodata) /* Read only, section set asside for constants */
} >rom
.data : /* Define output file DATA section */
{
_data_start = .; /* Get the memory address (VMA) for start of section .data */
*(.data) /* Initialized static and global variable values */
. = ALIGN(4);
_data_end = .; /* Get the memory address (VMA) for end of section .data */
} >ram AT >rom /* After AT we specify the load-time location */
_data_load = LOADADDR(.data); /* Get the load memory address (LMA) for section .data */
.bss : /* Define output file BSS section */
{
_bss_start = .; /* Get memory address of start of bss section */
*(.bss) /* Uninitialized static and global variables */
*(COMMON) /* Uninitialized variables are placed in COMMON section for object files */
. = ALIGN(4);
_bss_end = .; /* Get memory address of end of bss section */
} >ram
}
compile_flash.sh:
arm-none-eabi-gcc -O0 -Wall -c -g -mcpu=cortex-m4 -mthumb blink.c -o blink.o
arm-none-eabi-ld -o blink.elf -T linker.ld blink.o
arm-none-eabi-objcopy blink.elf blink.bin -O binary
arm-none-eabi-nm --numeric-sort blink.elf
st-flash --reset write blink.bin 0x08000000
Извините за длинный код, но я думаю, что не должно быть ничего скрытого, если вы хотите мне помочь 🙂
Комментарии:
1. Почему что-то мигает? Нет цикла, нет прерывания. Периодическое поведение не ожидается.
2. извините, за неправильное понимание, светодиод должен просто гореть. Я редактирую его
3. Светодиод включен на плате или вы подключили его к контакту? Вы уверены, что он работает при «1» и при «0» полярности?
4. Это встроенный светодиод
Ответ №1:
Проблема в том, что код устанавливает 10-й и 11-й бит, но тип char*
данных.
Изменение типа данных на void*
работает.