Skip to content

Commit 5b5135d

Browse files
committed
drv/pwm: move internals to separate header file
This makes the intent clearer and discourages improper use of the API.
1 parent de5d9bd commit 5b5135d

File tree

7 files changed

+55
-30
lines changed

7 files changed

+55
-30
lines changed

lib/pbio/drv/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
#include <pbdrv/battery.h>
77
#include <pbdrv/led.h>
8-
#include <pbdrv/pwm.h>
98

109
#include "counter/counter.h"
10+
#include "pwm/pwm.h"
1111

1212
/** Initializes all enabled drivers. */
1313
void pbdrv_init() {

lib/pbio/drv/pwm/pwm.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2020 The Pybricks Authors
3+
4+
// Pulse width modulation (PWM) drivers
5+
6+
#ifndef _PBDRV_PWM_PWM_H_
7+
#define _PBDRV_PWM_PWM_H_
8+
9+
#include <stdint.h>
10+
11+
#include <pbdrv/config.h>
12+
#include <pbdrv/pwm.h>
13+
#include <pbio/error.h>
14+
15+
/**
16+
* Driver callback functions.
17+
*/
18+
typedef struct {
19+
/** Driver implementation of pbdrv_pwm_set_duty() */
20+
pbio_error_t (*set_duty)(pbdrv_pwm_dev_t *dev, uint32_t ch, uint32_t value);
21+
} pbdrv_pwm_driver_funcs_t;
22+
23+
struct _pbdrv_pwm_dev_t {
24+
/** Platform data. */
25+
const void *pdata;
26+
/** Driver callback functions. */
27+
const pbdrv_pwm_driver_funcs_t *funcs;
28+
/** Private driver data */
29+
void *priv;
30+
};
31+
32+
#if PBDRV_CONFIG_PWM
33+
34+
void pbdrv_pwm_init();
35+
36+
#else // PBDRV_CONFIG_PWM
37+
38+
#define pbdrv_pwm_init()
39+
40+
#endif // PBDRV_CONFIG_PWM
41+
42+
#endif // _PBDRV_PWM_PWM_H_

lib/pbio/drv/pwm/pwm_core.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
#include <pbdrv/pwm.h>
1212
#include <pbio/error.h>
1313

14-
#include "./pwm_stm32_tim.h"
15-
#include "./pwm_test.h"
16-
#include "./pwm_tlc5955_stm32.h"
14+
#include "pwm_stm32_tim.h"
15+
#include "pwm_test.h"
16+
#include "pwm_tlc5955_stm32.h"
17+
#include "pwm.h"
1718

1819

1920
static pbdrv_pwm_dev_t pbdrv_pwm_dev[PBDRV_CONFIG_PWM_NUM_DEV];
2021

21-
/** @cond INTERNAL */
22-
2322
/**
2423
* Initializes all PWM drivers.
2524
*/
@@ -29,8 +28,6 @@ void pbdrv_pwm_init() {
2928
pbdrv_pwm_tlc5955_stm32_init(pbdrv_pwm_dev);
3029
}
3130

32-
/** @endcond */
33-
3431
/**
3532
* Gets the PWM device with the given ID.
3633
* @param [in] id The ID of the PWM device.

lib/pbio/drv/pwm/pwm_stm32_tim.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
#include <pbdrv/pwm.h>
1515
#include <pbio/error.h>
1616

17-
#include "./pwm_stm32_tim.h"
17+
#include "pwm_stm32_tim.h"
18+
#include "pwm.h"
1819

1920
static pbio_error_t pbdrv_pwm_stm32_tim_set_duty(pbdrv_pwm_dev_t *dev, uint32_t ch, uint32_t value) {
2021
const pbdrv_pwm_stm32_tim_platform_data_t *pdata = dev->pdata;

lib/pbio/drv/pwm/pwm_tlc5955_stm32.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
#include <pbio/error.h>
2121
#include <pbio/util.h>
2222

23-
#include "./pwm_tlc5955_stm32.h"
23+
#include "pwm_tlc5955_stm32.h"
24+
#include "pwm.h"
2425

2526
// size in bytes to hold 769-bit shift register data
2627
#define TLC5955_DATA_SIZE ((769 + 7) / 8)

lib/pbio/include/pbdrv/pwm.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,8 @@
2020
*/
2121
typedef struct _pbdrv_pwm_dev_t pbdrv_pwm_dev_t;
2222

23-
/**
24-
* Driver callback functions.
25-
*/
26-
typedef struct {
27-
/** Driver implementation of pbdrv_pwm_set_duty() */
28-
pbio_error_t (*set_duty)(pbdrv_pwm_dev_t *dev, uint32_t ch, uint32_t value);
29-
} pbdrv_pwm_driver_funcs_t;
30-
31-
struct _pbdrv_pwm_dev_t {
32-
/** Platform data. */
33-
const void *pdata;
34-
/** Driver callback functions. */
35-
const pbdrv_pwm_driver_funcs_t *funcs;
36-
/** Private driver data */
37-
void *priv;
38-
};
39-
4023
#if PBDRV_CONFIG_PWM
4124

42-
void pbdrv_pwm_init();
4325
pbio_error_t pbdrv_pwm_get_dev(uint8_t id, pbdrv_pwm_dev_t **dev);
4426
pbio_error_t pbdrv_pwm_set_duty(pbdrv_pwm_dev_t *dev, uint32_t ch, uint32_t value);
4527

lib/pbio/test/pwm.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
#include <stdio.h>
55

6+
#include <tinytest.h>
7+
#include <tinytest_macros.h>
8+
69
#include <pbdrv/pwm.h>
710
#include <pbio/error.h>
811

9-
#include <tinytest.h>
10-
#include <tinytest_macros.h>
12+
#include "../drv/pwm/pwm.h"
1113

1214
typedef struct {
1315
uint32_t duty_channel;

0 commit comments

Comments
 (0)