Skip to content

Commit 46236ce

Browse files
authored
[docs] Improve halconf/mcuconf code examples (qmk#24597)
1 parent a8a47c4 commit 46236ce

File tree

8 files changed

+221
-135
lines changed

8 files changed

+221
-135
lines changed

docs/drivers/audio.md

+52-36
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,32 @@ This driver needs one Timer per enabled/used DAC channel, to trigger conversion;
5757

5858
Additionally, in the board config, you'll want to make changes to enable the DACs, GPT for Timers 6, 7 and 8:
5959

60-
```c
61-
//halconf.h:
62-
#define HAL_USE_DAC TRUE
63-
#define HAL_USE_GPT TRUE
60+
::: code-group
61+
```c [halconf.h]
62+
#pragma once
63+
64+
#define HAL_USE_DAC TRUE // [!code focus]
65+
#define HAL_USE_GPT TRUE // [!code focus]
66+
6467
#include_next <halconf.h>
6568
```
69+
```c [mcuconf.h]
70+
#pragma once
6671

67-
```c
68-
// mcuconf.h:
6972
#include_next <mcuconf.h>
70-
#undef STM32_DAC_USE_DAC1_CH1
71-
#define STM32_DAC_USE_DAC1_CH1 TRUE
72-
#undef STM32_DAC_USE_DAC1_CH2
73-
#define STM32_DAC_USE_DAC1_CH2 TRUE
74-
#undef STM32_GPT_USE_TIM6
75-
#define STM32_GPT_USE_TIM6 TRUE
76-
#undef STM32_GPT_USE_TIM7
77-
#define STM32_GPT_USE_TIM7 TRUE
78-
#undef STM32_GPT_USE_TIM8
79-
#define STM32_GPT_USE_TIM8 TRUE
73+
74+
#undef STM32_DAC_USE_DAC1_CH1 // [!code focus]
75+
#define STM32_DAC_USE_DAC1_CH1 TRUE // [!code focus]
76+
#undef STM32_DAC_USE_DAC1_CH2 // [!code focus]
77+
#define STM32_DAC_USE_DAC1_CH2 TRUE // [!code focus]
78+
#undef STM32_GPT_USE_TIM6 // [!code focus]
79+
#define STM32_GPT_USE_TIM6 TRUE // [!code focus]
80+
#undef STM32_GPT_USE_TIM7 // [!code focus]
81+
#define STM32_GPT_USE_TIM7 TRUE // [!code focus]
82+
#undef STM32_GPT_USE_TIM8 // [!code focus]
83+
#define STM32_GPT_USE_TIM8 TRUE // [!code focus]
8084
```
85+
:::
8186
8287
::: tip
8388
Note: DAC1 (A4) uses TIM6, DAC2 (A5) uses TIM7, and the audio state timer uses TIM8 (configurable).
@@ -95,23 +100,28 @@ only needs one timer (GPTD6, Tim6) to trigger the DAC unit to do a conversion; t
95100

96101
Additionally, in the board config, you'll want to make changes to enable the DACs, GPT for Timer 6:
97102

98-
```c
99-
//halconf.h:
100-
#define HAL_USE_DAC TRUE
101-
#define HAL_USE_GPT TRUE
103+
::: code-group
104+
```c [halconf.h]
105+
#pragma once
106+
107+
#define HAL_USE_DAC TRUE // [!code focus]
108+
#define HAL_USE_GPT TRUE // [!code focus]
109+
102110
#include_next <halconf.h>
103111
```
112+
```c [mcuconf.h]
113+
#pragma once
104114

105-
```c
106-
// mcuconf.h:
107115
#include_next <mcuconf.h>
108-
#undef STM32_DAC_USE_DAC1_CH1
109-
#define STM32_DAC_USE_DAC1_CH1 TRUE
110-
#undef STM32_DAC_USE_DAC1_CH2
111-
#define STM32_DAC_USE_DAC1_CH2 TRUE
112-
#undef STM32_GPT_USE_TIM6
113-
#define STM32_GPT_USE_TIM6 TRUE
116+
117+
#undef STM32_DAC_USE_DAC1_CH1 // [!code focus]
118+
#define STM32_DAC_USE_DAC1_CH1 TRUE // [!code focus]
119+
#undef STM32_DAC_USE_DAC1_CH2 // [!code focus]
120+
#define STM32_DAC_USE_DAC1_CH2 TRUE // [!code focus]
121+
#undef STM32_GPT_USE_TIM6 // [!code focus]
122+
#define STM32_GPT_USE_TIM6 TRUE // [!code focus]
114123
```
124+
:::
115125
116126
### DAC Config
117127
@@ -170,19 +180,25 @@ This driver uses the ChibiOS-PWM system to produce a square-wave on specific out
170180
The hardware directly toggles the pin via its alternate function. See your MCU's data-sheet for which pin can be driven by what timer - looking for TIMx_CHy and the corresponding alternate function.
171181

172182
A configuration example for the STM32F103C8 would be:
173-
```c
174-
//halconf.h:
175-
#define HAL_USE_PWM TRUE
176-
#define HAL_USE_PAL TRUE
183+
184+
::: code-group
185+
```c [halconf.h]
186+
#pragma once
187+
188+
#define HAL_USE_PWM TRUE // [!code focus]
189+
#define HAL_USE_PAL TRUE // [!code focus]
190+
177191
#include_next <halconf.h>
178192
```
193+
```c [mcuconf.h]
194+
#pragma once
179195

180-
```c
181-
// mcuconf.h:
182196
#include_next <mcuconf.h>
183-
#undef STM32_PWM_USE_TIM1
184-
#define STM32_PWM_USE_TIM1 TRUE
197+
198+
#undef STM32_PWM_USE_TIM1 // [!code focus]
199+
#define STM32_PWM_USE_TIM1 TRUE // [!code focus]
185200
```
201+
:::
186202
187203
If we now target pin A8, looking through the data-sheet of the STM32F103C8, for the timers and alternate functions
188204
- TIM1_CH1 = PA8 <- alternate0

docs/drivers/i2c.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,25 @@ The ATmega16/32U2 does not possess I2C functionality, and so cannot use this dri
5454
5555
You'll need to determine which pins can be used for I2C -- a an example, STM32 parts generally have multiple I2C peripherals, labeled I2C1, I2C2, I2C3 etc.
5656
57-
To enable I2C, modify your board's `halconf.h` to enable I2C:
57+
To enable I2C, modify your board's `halconf.h` to enable I2C, then modify your board's `mcuconf.h` to enable the peripheral you've chosen:
5858
59-
```c
60-
#define HAL_USE_I2C TRUE
59+
::: code-group
60+
```c [halconf.h]
61+
#pragma once
62+
63+
#define HAL_USE_I2C TRUE // [!code focus]
64+
65+
#include_next <halconf.h>
6166
```
67+
```c [mcuconf.h]
68+
#pragma once
6269

63-
Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
70+
#include_next <mcuconf.h>
6471

65-
```c
66-
#undef STM32_I2C_USE_I2C2
67-
#define STM32_I2C_USE_I2C2 TRUE
72+
#undef STM32_I2C_USE_I2C2 // [!code focus]
73+
#define STM32_I2C_USE_I2C2 TRUE // [!code focus]
6874
```
75+
:::
6976
7077
|`mcuconf.h` Setting |Description |Default|
7178
|----------------------------|----------------------------------------------------------------------------------|-------|

docs/drivers/serial.md

+65-49
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ The Serial driver powers the [Split Keyboard](../features/split_keyboard) featur
1212
Serial in this context should be read as **sending information one bit at a time**, rather than implementing UART/USART/RS485/RS232 standards.
1313
:::
1414

15-
<hr>
16-
1715
## Bitbang
1816

1917
This is the Default driver, absence of configuration assumes this driver. It works by [bit banging](https://en.wikipedia.org/wiki/Bit_banging) a GPIO pin using the CPU. It is therefore not as efficient as a dedicated hardware peripheral, which the Half-duplex and Full-duplex drivers use.
@@ -53,11 +51,15 @@ SERIAL_DRIVER = bitbang
5351
#define SOFT_SERIAL_PIN D0 // or D1, D2, D3, E6
5452
```
5553
56-
3. On ARM platforms you must turn on ChibiOS `PAL_USE_CALLBACKS` feature:
54+
3. On ARM platforms you must turn on ChibiOS PAL callbacks:
5755
58-
* In `halconf.h` add the line `#define PAL_USE_CALLBACKS TRUE`.
56+
```c
57+
#pragma once
5958
60-
<hr>
59+
#define PAL_USE_CALLBACKS TRUE // [!code focus]
60+
61+
#include_next <halconf.h>
62+
```
6163

6264
## USART Half-duplex
6365

@@ -117,8 +119,6 @@ For STM32 MCUs several GPIO configuration options can be changed as well. See th
117119
118120
4. Decide either for `SERIAL`, `SIO`, or `PIO` subsystem. See section ["Choosing a driver subsystem"](#choosing-a-driver-subsystem).
119121
120-
<hr>
121-
122122
## USART Full-duplex
123123
124124
Targeting ARM boards based on ChibiOS where communication is offloaded to an USART hardware device. The advantages over bitbanging are fast, accurate timings and reduced CPU usage; therefore it is advised to choose this driver over all others where possible. Due to its internal design Full-duplex is slightly more efficient than the Half-duplex driver, but Full-duplex should be primarily chosen if Half-duplex operation is not supported by the controller's USART peripheral.
@@ -179,70 +179,88 @@ For STM32 MCUs several GPIO configuration options, including the ability for `TX
179179

180180
4. Decide either for `SERIAL`, `SIO`, or `PIO` subsystem. See section ["Choosing a driver subsystem"](#choosing-a-driver-subsystem).
181181

182-
<hr>
183-
184182
## Choosing a driver subsystem
185183

186184
### The `SERIAL` driver
187185

188186
The `SERIAL` Subsystem is supported for the majority of ChibiOS MCUs and should be used whenever supported. Follow these steps in order to activate it:
189187

190-
1. In your keyboards `halconf.h` add:
188+
1. Enable the SERIAL subsystem in the ChibiOS HAL.
191189

192-
```c
193-
#define HAL_USE_SERIAL TRUE
194-
```
190+
Add the following to your keyboard's `halconf.h`, creating it if necessary:
195191
196-
2. In your keyboards `mcuconf.h`: activate the USART peripheral that is used on your MCU. The shown example is for an STM32 MCU, so this will not work on MCUs by other manufacturers. You can find the correct names in the `mcuconf.h` files of your MCU that ship with ChibiOS.
197-
198-
Just below `#include_next <mcuconf.h>` add:
192+
```c
193+
#pragma once
199194
200-
```c
201-
#include_next <mcuconf.h>
195+
#define HAL_USE_SERIAL TRUE // [!code focus]
202196
203-
#undef STM32_SERIAL_USE_USARTn
204-
#define STM32_SERIAL_USE_USARTn TRUE
205-
```
197+
#include_next <halconf.h>
198+
```
206199
207-
Where 'n' matches the peripheral number of your selected USART on the MCU.
200+
2. Activate the USART peripheral that is used on your MCU. The shown example is for an STM32 MCU, so this will not work on MCUs by other manufacturers. You can find the correct names in the `mcuconf.h` files of your MCU that ship with ChibiOS.
208201
209-
3. In you keyboards `config.h`: override the default USART `SERIAL` driver if you use a USART peripheral that does not belong to the default selected `SD1` driver. For instance, if you selected `STM32_SERIAL_USE_USART3` the matching driver would be `SD3`.
202+
Add the following to your keyboard's `mcuconf.h`, creating it if necessary:
210203

211-
```c
212-
#define SERIAL_USART_DRIVER SD3
213-
```
204+
```c
205+
#pragma once
206+
207+
#include_next <mcuconf.h>
208+
209+
#undef STM32_SERIAL_USE_USARTn // [!code focus]
210+
#define STM32_SERIAL_USE_USARTn TRUE // [!code focus]
211+
```
212+
213+
Where *n* matches the peripheral number of your selected USART on the MCU.
214+
215+
3. Override the default USART `SERIAL` driver if you use a USART peripheral that does not belong to the default selected `SD1` driver. For instance, if you selected `STM32_SERIAL_USE_USART3` the matching driver would be `SD3`.
216+
217+
Add the following to your keyboard's `config.h`:
218+
219+
```c
220+
#define SERIAL_USART_DRIVER SD3
221+
```
214222

215223
### The `SIO` driver
216224

217225
The `SIO` Subsystem was added to ChibiOS with the 21.11 release and is only supported on selected MCUs. It should only be chosen when the `SERIAL` subsystem is not supported by your MCU.
218226

219227
Follow these steps in order to activate it:
220228

221-
1. In your keyboards `halconf.h` add:
229+
1. Enable the SIO subsystem in the ChibiOS HAL.
222230

223-
```c
224-
#define HAL_USE_SIO TRUE
225-
```
231+
Add the following to your keyboard's `halconf.h`, creating it if necessary:
226232
227-
2. In your keyboards `mcuconf.h:` activate the USART peripheral that is used on your MCU. The shown example is for an STM32 MCU, so this will not work on MCUs by other manufacturers. You can find the correct names in the `mcuconf.h` files of your MCU that ship with ChibiOS.
228-
229-
Just below `#include_next <mcuconf.h>` add:
233+
```c
234+
#pragma once
230235
231-
```c
232-
#include_next <mcuconf.h>
236+
#define HAL_USE_SIO TRUE // [!code focus]
233237
234-
#undef STM32_SIO_USE_USARTn
235-
#define STM32_SIO_USE_USARTn TRUE
236-
```
238+
#include_next <halconf.h>
239+
```
237240
238-
Where 'n' matches the peripheral number of your selected USART on the MCU.
241+
2. Activate the USART peripheral that is used on your MCU. The shown example is for an STM32 MCU, so this will not work on MCUs by other manufacturers. You can find the correct names in the `mcuconf.h` files of your MCU that ship with ChibiOS.
239242
240-
3. In the keyboard's `config.h` file: override the default USART `SIO` driver if you use a USART peripheral that does not belong to the default selected `SIOD1` driver. For instance, if you selected `STM32_SERIAL_USE_USART3` the matching driver would be `SIOD3`.
243+
Add the following to your keyboard's `mcuconf.h`, creating it if necessary:
244+
245+
```c
246+
#pragma once
247+
248+
#include_next <mcuconf.h>
249+
250+
#undef STM32_SIO_USE_USARTn // [!code focus]
251+
#define STM32_SIO_USE_USARTn TRUE // [!code focus]
252+
```
253+
254+
Where *n* matches the peripheral number of your selected USART on the MCU.
255+
256+
3. Override the default USART `SIO` driver if you use a USART peripheral that does not belong to the default selected `SIOD1` driver. For instance, if you selected `STM32_SERIAL_USE_USART3` the matching driver would be `SIOD3`.
257+
258+
Add the following to your keyboard's `config.h`:
259+
260+
```c
261+
#define SERIAL_USART_DRIVER SIOD3
262+
```
241263

242-
```c
243-
#define SERIAL_USART_DRIVER SIOD3
244-
```
245-
246264
### The `PIO` driver
247265

248266
The `PIO` subsystem is a Raspberry Pi RP2040 specific implementation, using an integrated PIO peripheral and is therefore only available on this MCU. Because of the flexible nature of PIO peripherals, **any** GPIO pin can be used as a `TX` or `RX` pin. Half-duplex and Full-duplex operation modes are fully supported with this driver. Half-duplex uses the built-in pull-ups and GPIO manipulation of the RP2040 to drive the line high by default, thus an external pull-up resistor **is not required**.
@@ -254,8 +272,6 @@ Optionally, the PIO peripheral utilized for split communication can be changed w
254272
255273
The Serial PIO program uses 2 state machines, 13 instructions and the complete interrupt handler of the PIO peripheral it is running on.
256274
257-
<hr>
258-
259275
## Advanced Configuration
260276
261277
There are several advanced configuration options that can be defined in your keyboards `config.h` file:
@@ -265,9 +281,11 @@ There are several advanced configuration options that can be defined in your key
265281
If you're having issues or need a higher baudrate with serial communication, you can change the baudrate which in turn controls the communication speed for serial. You want to lower the baudrate if you experience failed transactions.
266282
267283
```c
268-
#define SELECT_SOFT_SERIAL_SPEED {#}
284+
#define SELECT_SOFT_SERIAL_SPEED n
269285
```
270286

287+
Where *n* is one of:
288+
271289
| Speed | Bitbang | Half-duplex and Full-duplex |
272290
| ----- | -------------------------- | --------------------------- |
273291
| `0` | 189000 baud (experimental) | 460800 baud |
@@ -287,8 +305,6 @@ This is the default time window in milliseconds in which a successful communicat
287305
#define SERIAL_USART_TIMEOUT 20 // USART driver timeout. default 20
288306
```
289307
290-
<hr>
291-
292308
## Troubleshooting
293309
294310
If you're having issues withe serial communication, you can enable debug messages that will give you insights which part of the communication failed. The enable these messages add to your keyboards `config.h` file:

docs/drivers/spi.md

+16-9
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,27 @@ You may use more than one slave select pin, not just the `SS` pin. This is usefu
3232

3333
You'll need to determine which pins can be used for SPI -- as an example, STM32 parts generally have multiple SPI peripherals, labeled SPI1, SPI2, SPI3 etc.
3434

35-
To enable SPI, modify your board's `halconf.h` to enable SPI:
35+
To enable SPI, modify your board's `halconf.h` to enable SPI, then modify your board's `mcuconf.h` to enable the peripheral you've chosen:
3636

37-
```c
38-
#define HAL_USE_SPI TRUE
39-
#define SPI_USE_WAIT TRUE
40-
#define SPI_SELECT_MODE SPI_SELECT_MODE_PAD
37+
::: code-group
38+
```c [halconf.h]
39+
#pragma once
40+
41+
#define HAL_USE_SPI TRUE // [!code focus]
42+
#define SPI_USE_WAIT TRUE // [!code focus]
43+
#define SPI_SELECT_MODE SPI_SELECT_MODE_PAD // [!code focus]
44+
45+
#include_next <halconf.h>
4146
```
47+
```c [mcuconf.h]
48+
#pragma once
4249

43-
Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
50+
#include_next <mcuconf.h>
4451

45-
```c
46-
#undef STM32_SPI_USE_SPI2
47-
#define STM32_SPI_USE_SPI2 TRUE
52+
#undef STM32_SPI_USE_SPI2 // [!code focus]
53+
#define STM32_SPI_USE_SPI2 TRUE // [!code focus]
4854
```
55+
:::
4956
5057
Configuration-wise, you'll need to set up the peripheral as per your MCU's datasheet -- the defaults match the pins for a Proton-C, i.e. STM32F303.
5158

0 commit comments

Comments
 (0)