1
0
mirror of https://github.com/rfivet/stm32bringup.git synced 2024-12-18 23:06:28 -05:00
stm32bringup/docs/22_board.html

147 lines
4.0 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2.2 Another Day, Another Board</title>
<link type="text/css" rel="stylesheet" href="style.css">
</head>
<body>
<h1>2.2 Another Day, Another Board</h1>
As I switched to a new board, I need to check that the code I wrote so
far is still working as expected. Unfortunately the user LED on the new
board is wired differently than the first one.
<h2>Board Schematics</h2>
<img src="img/22_ledv200.png">
<p>
The new board uses <b>PA4</b> to turn the LED ON when <b>high</b>. Also, by
taking off a jumper, <b>PA4</b> can be used for other purposes.
<p>
The first board uses <b>PB1</b> to turn the LED ON when <b>low</b> as I have
seen previously <a href="14_ledon.html">here</a>.
<p>
I can adapt the code by adding the base address of <b>GPIOA</b> and
shifting pin location accordingly, but this type of variations is so
common that I want to make sure adaptations can be done easily without
errors.
<h2>Board Description</h2>
I want to be able to capture the board variations through simple definitions:
<p>
For the new board:
<pre>
#define LED_IOP A
#define LED_PIN 4
#define LED_ON 1
</pre>
And for the vcc-gnd board:
<pre>
#define LED_IOP B
#define LED_PIN 1
#define LED_ON 0
</pre>
<h2>Implementation</h2>
I make a copy of <b>init.c</b> into <b>board.c</b> and rework the preprocessor
macroes.
<p>
As I have several GPIO peripheral GPIOA .. GPIOF, I switch notation,
instead of writing, say, <code>GPIOA_MODER</code>, I will write either
<code>GPIOA[ MODER]</code> or <code>GPIO( A)[ MODER]</code>. This way I could
refer directly to <code>GPIO( LED_IOP)[ MODER]</code>.
<p>
I use conditional compilation based on <code>LED_ON</code>. If
<code>LED_ON</code> is high, I need an extra step during initialization
compare to <code>LED_ON</code> low. On the other hand, if <code>LED_ON</code>
is undefined, the code would be removed for a board that doesnt have a user
LED.
<pre>
#define SYSTICK ((volatile long *) 0xE000E010)
#define SYSTICK_CSR SYSTICK[ 0]
#define SYSTICK_RVR SYSTICK[ 1]
#define SYSTICK_CVR SYSTICK[ 2]
#define CAT( a, b) a##b
#define HEXA( a) CAT( 0x, a)
#define RCC ((volatile long *) 0x40021000)
#define RCC_AHBENR RCC[ 5]
#define RCC_AHBENR_IOP( h) (1 << (17 + HEXA( h) - 0xA))
#define GPIOA ((volatile long *) 0x48000000)
#define GPIOB ((volatile long *) 0x48000400)
#define GPIO( x) CAT( GPIO, x)
#define MODER 0
#define ODR 5
/* user LED ON when PA4 is high */
#define LED_IOP A
#define LED_PIN 4
#define LED_ON 1
void SysTick_Handler( void) {
#ifdef LED_ON
GPIO( LED_IOP)[ ODR] ^= 1 << LED_PIN ; /* Toggle User LED */
#endif
}
int init( void) {
/* By default SYSCLK == HSI [8MHZ] */
/* SYSTICK */
SYSTICK_RVR = 1000000 - 1 ; /* HBA / 8 */
SYSTICK_CVR = 0 ;
SYSTICK_CSR = 3 ; /* HBA / 8, Interrupt ON, Enable */
/* SysTick_Handler will execute every 1s from now on */
#ifdef LED_ON
/* User LED ON */
RCC_AHBENR |= RCC_AHBENR_IOP( LED_IOP) ; /* Enable IOPx periph */
GPIO( LED_IOP)[ MODER] |= 1 << (LED_PIN * 2) ; /* LED_IO Output [01],
** over default 00 */
/* OTYPER Push-Pull by default */
/* Pxn output default LOW at reset */
# if LED_ON
SysTick_Handler() ;
# endif
#endif
return 0 ;
}
</pre>
<h2>Build and Test</h2>
I just need to add the <code>SRCS</code> definition in <b>Makefile</b>.
<pre>SRCS = startup.c board.c success.c</pre>
and build.
<pre>
$ make
f030f4.elf
text data bss dec hex filename
224 0 0 224 e0 f030f4.elf
f030f4.hex
f030f4.bin
</pre>
Once I have flashed the board with this new binary, I put back the BOOT0
jumper and press reset. This board user LED is red. &#x1F60E;
<h2>Checkpoint</h2>
I made sure the code I have evolved so far works on the board with the
serial connection.
<p>
<a href="23_hello.html">Next</a>, I will do some serial transmission.
<hr>© 2020-2024 Renaud Fivet
</body>
</html>