Skip to content

Commit

Permalink
hw/drivers: Refactor nrf5x PWM driver
Browse files Browse the repository at this point in the history
This renames the driver from pwm_nrf52 to pwm_nrf5x
as it is used with other supported nrf5x devices
  • Loading branch information
m-gorecki committed Jan 29, 2025
1 parent 8731466 commit 24a940d
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

#ifndef __PWM_NRF52_H__
#define __PWM_NRF52_H__
#ifndef __PWM_NRF5X_H__
#define __PWM_NRF5X_H__

#include <pwm/pwm.h>

Expand All @@ -28,10 +28,10 @@
extern "C" {
#endif

int nrf52_pwm_dev_init(struct os_dev *, void *);
int nrf5x_pwm_dev_init(struct os_dev *odev, void *arg);

#ifdef __cplusplus
}
#endif

#endif /* __PWM_NRF52_H__ */
#endif /* __PWM_NRF5X_H__ */
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# under the License.
#

pkg.name: hw/drivers/pwm/pwm_nrf52
pkg.description: PWM driver for the NRF52
pkg.name: hw/drivers/pwm/pwm_nrf5x
pkg.description: PWM driver for the NRF5x devices
pkg.author: "Apache Mynewt <[email protected]>"
pkg.homepage: "http://mynewt.apache.org/"
pkg.keywords:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
#include <nrf_pwm.h>

/* Mynewt Nordic driver */
#include "pwm_nrf52/pwm_nrf52.h"
#include "pwm_nrf5x/pwm_nrf5x.h"

/* Max number on PWM instances on existing nRF52xxx MCUs */
/* Max number on PWM instances on existing nRF5x MCUs */
#if MYNEWT_VAL(BSP_NRF54)
#define NRF52_PWM_MAX_INSTANCES 3
#define NRF_PWM_MAX_INSTANCES 3
#else
#define NRF52_PWM_MAX_INSTANCES 4
#define NRF_PWM_MAX_INSTANCES 4
#endif

#if MYNEWT_VAL(BSP_NRF54)
Expand All @@ -58,7 +58,7 @@
#define PWM_3_ID 3
#endif

struct nrf52_pwm_dev_global {
struct nrf5x_pwm_dev_global {
bool in_use;
bool playing;
nrfx_pwm_t drv_instance;
Expand All @@ -73,7 +73,7 @@ struct nrf52_pwm_dev_global {
void* seq_end_data;
};

static struct nrf52_pwm_dev_global instances[] =
static struct nrf5x_pwm_dev_global instances[] =
{
#if MYNEWT_VAL(PWM_0)
[0].in_use = false,
Expand Down Expand Up @@ -307,7 +307,7 @@ cleanup_instance(int inst_id)
}

/**
* Open the NRF52 PWM device
* Open the NRF5x PWM device
*
* This function locks the device for access from other tasks.
*
Expand All @@ -322,7 +322,7 @@ cleanup_instance(int inst_id)
* @return 0 on success, non-zero error code on failure.
*/
static int
nrf52_pwm_open(struct os_dev *odev, uint32_t wait, void *arg)
nrf5x_pwm_open(struct os_dev *odev, uint32_t wait, void *arg)
{
struct pwm_dev *dev;
int stat = 0;
Expand Down Expand Up @@ -359,7 +359,7 @@ nrf52_pwm_open(struct os_dev *odev, uint32_t wait, void *arg)
}

/**
* Close the NRF52 PWM device.
* Close the NRF5x PWM device.
*
* This function unlocks the device.
*
Expand All @@ -368,7 +368,7 @@ nrf52_pwm_open(struct os_dev *odev, uint32_t wait, void *arg)
* @return 0 on success, non-zero error code on failure.
*/
static int
nrf52_pwm_close(struct os_dev *odev)
nrf5x_pwm_close(struct os_dev *odev)
{
struct pwm_dev *dev;
int inst_id;
Expand Down Expand Up @@ -397,7 +397,7 @@ nrf52_pwm_close(struct os_dev *odev)
* Play using current configuration.
*/
static void
play_current_config(struct nrf52_pwm_dev_global *instance)
play_current_config(struct nrf5x_pwm_dev_global *instance)
{
nrf_pwm_sequence_t const seq =
{
Expand All @@ -423,10 +423,10 @@ play_current_config(struct nrf52_pwm_dev_global *instance)
* @return 0 on success, non-zero error code on failure.
*/
int
nrf52_pwm_configure_device(struct pwm_dev *dev, struct pwm_dev_cfg *cfg)
nrf5x_pwm_configure_device(struct pwm_dev *dev, struct pwm_dev_cfg *cfg)
{
int inst_id = dev->pwm_instance_id;
struct nrf52_pwm_dev_global *instance = &instances[inst_id];
struct nrf5x_pwm_dev_global *instance = &instances[inst_id];

instance->n_cycles = (cfg->n_cycles) ? cfg->n_cycles : 1;

Expand Down Expand Up @@ -474,12 +474,12 @@ nrf52_pwm_configure_device(struct pwm_dev *dev, struct pwm_dev_cfg *cfg)
* @return 0 on success, non-zero error code on failure.
*/
static int
nrf52_pwm_configure_channel(struct pwm_dev *dev,
uint8_t cnum,
struct pwm_chan_cfg *cfg)
nrf5x_pwm_configure_channel(struct pwm_dev *dev,
uint8_t cnum,
struct pwm_chan_cfg *cfg)
{
int inst_id = dev->pwm_instance_id;
struct nrf52_pwm_dev_global *instance = &instances[inst_id];
struct nrf5x_pwm_dev_global *instance = &instances[inst_id];
nrfx_pwm_config_t *config = &instance->config;

if (!instance->in_use) {
Expand Down Expand Up @@ -515,7 +515,7 @@ nrf52_pwm_configure_channel(struct pwm_dev *dev,
* @return 0 on success, non-zero error code on failure.
*/
static int
nrf52_pwm_set_duty_cycle(struct pwm_dev *dev, uint8_t cnum, uint16_t fraction)
nrf5x_pwm_set_duty_cycle(struct pwm_dev *dev, uint8_t cnum, uint16_t fraction)
{
int inst_id = dev->pwm_instance_id;
nrfx_pwm_config_t *config;
Expand Down Expand Up @@ -547,10 +547,10 @@ nrf52_pwm_set_duty_cycle(struct pwm_dev *dev, uint8_t cnum, uint16_t fraction)
* @return 0 on success, non-zero error code on failure.
*/
int
nrf52_pwm_enable(struct pwm_dev *dev)
nrf5x_pwm_enable(struct pwm_dev *dev)
{
int inst_id = dev->pwm_instance_id;
struct nrf52_pwm_dev_global *instance = &instances[inst_id];
struct nrf5x_pwm_dev_global *instance = &instances[inst_id];

nrfx_pwm_init(&instance->drv_instance,
&instance->config,
Expand All @@ -571,7 +571,7 @@ nrf52_pwm_enable(struct pwm_dev *dev)
* @return true if enabled, false if not.
*/
static bool
nrf52_pwm_is_enabled(struct pwm_dev *dev)
nrf5x_pwm_is_enabled(struct pwm_dev *dev)
{
return (instances[dev->pwm_instance_id].playing);
}
Expand All @@ -584,7 +584,7 @@ nrf52_pwm_is_enabled(struct pwm_dev *dev)
* @return 0 on success, non-zero error code on failure.
*/
static int
nrf52_pwm_disable(struct pwm_dev *dev)
nrf5x_pwm_disable(struct pwm_dev *dev)
{
int inst_id = dev->pwm_instance_id;
if (!instances[inst_id].in_use) {
Expand Down Expand Up @@ -613,7 +613,7 @@ nrf52_pwm_disable(struct pwm_dev *dev)
* @return A value is in Hz on success, negative error code on failure.
*/
static int
nrf52_pwm_set_frequency(struct pwm_dev *dev, uint32_t freq_hz)
nrf5x_pwm_set_frequency(struct pwm_dev *dev, uint32_t freq_hz)
{
int inst_id = dev->pwm_instance_id;
if (!instances[inst_id].in_use) {
Expand Down Expand Up @@ -676,7 +676,7 @@ nrf52_pwm_set_frequency(struct pwm_dev *dev, uint32_t freq_hz)
* @return value is in Hz on success, error code on failure.
*/
static int
nrf52_pwm_get_clock_freq(struct pwm_dev *dev)
nrf5x_pwm_get_clock_freq(struct pwm_dev *dev)
{
int inst_id = dev->pwm_instance_id;
if (!instances[inst_id].in_use) {
Expand Down Expand Up @@ -713,7 +713,7 @@ nrf52_pwm_get_clock_freq(struct pwm_dev *dev)
* @return value in cycles on success, negative error code on failure.
*/
int
nrf52_pwm_get_top_value(struct pwm_dev *dev)
nrf5x_pwm_get_top_value(struct pwm_dev *dev)
{
int inst_id = dev->pwm_instance_id;
if (!instances[inst_id].in_use) {
Expand All @@ -731,7 +731,7 @@ nrf52_pwm_get_top_value(struct pwm_dev *dev)
* @return The value in bits on success, negative error code on failure.
*/
static int
nrf52_pwm_get_resolution_bits(struct pwm_dev *dev)
nrf5x_pwm_get_resolution_bits(struct pwm_dev *dev)
{
int inst_id = dev->pwm_instance_id;
if (!instances[inst_id].in_use) {
Expand Down Expand Up @@ -825,11 +825,11 @@ pwm_3_irq_handler(void)

/**
* Callback to initialize an adc_dev structure from the os device
* initialization callback. This sets up a nrf52_pwm_device(), so
* initialization callback. This sets up a nrf5x_pwm_device(), so
* that subsequent lookups to this device allow us to manipulate it.
*/
int
nrf52_pwm_dev_init(struct os_dev *odev, void *arg)
nrf5x_pwm_dev_init(struct os_dev *odev, void *arg)
{
struct pwm_dev *dev;
struct pwm_driver_funcs *pwm_funcs;
Expand All @@ -847,29 +847,29 @@ nrf52_pwm_dev_init(struct os_dev *odev, void *arg)
* - if number is valid instance_id, let's use it directly
* - otherwise assume it's a valid pointer
*/
if (POINTER_TO_UINT(arg) < NRF52_PWM_MAX_INSTANCES) {
if (POINTER_TO_UINT(arg) < NRF_PWM_MAX_INSTANCES) {
dev->pwm_instance_id = POINTER_TO_UINT(arg);
} else {
dev->pwm_instance_id = *((int*) arg);
assert(dev->pwm_instance_id < NRF52_PWM_MAX_INSTANCES);
assert(dev->pwm_instance_id < NRF_PWM_MAX_INSTANCES);
}

dev->pwm_chan_count = NRF_PWM_CHANNEL_COUNT;
os_mutex_init(&dev->pwm_lock);

OS_DEV_SETHANDLERS(odev, nrf52_pwm_open, nrf52_pwm_close);
OS_DEV_SETHANDLERS(odev, nrf5x_pwm_open, nrf5x_pwm_close);

pwm_funcs = &dev->pwm_funcs;
pwm_funcs->pwm_configure_device = nrf52_pwm_configure_device;
pwm_funcs->pwm_configure_channel = nrf52_pwm_configure_channel;
pwm_funcs->pwm_set_duty_cycle = nrf52_pwm_set_duty_cycle;
pwm_funcs->pwm_enable = nrf52_pwm_enable;
pwm_funcs->pwm_is_enabled = nrf52_pwm_is_enabled;
pwm_funcs->pwm_set_frequency = nrf52_pwm_set_frequency;
pwm_funcs->pwm_get_clock_freq = nrf52_pwm_get_clock_freq;
pwm_funcs->pwm_get_top_value = nrf52_pwm_get_top_value;
pwm_funcs->pwm_get_resolution_bits = nrf52_pwm_get_resolution_bits;
pwm_funcs->pwm_disable = nrf52_pwm_disable;
pwm_funcs->pwm_configure_device = nrf5x_pwm_configure_device;
pwm_funcs->pwm_configure_channel = nrf5x_pwm_configure_channel;
pwm_funcs->pwm_set_duty_cycle = nrf5x_pwm_set_duty_cycle;
pwm_funcs->pwm_enable = nrf5x_pwm_enable;
pwm_funcs->pwm_is_enabled = nrf5x_pwm_is_enabled;
pwm_funcs->pwm_set_frequency = nrf5x_pwm_set_frequency;
pwm_funcs->pwm_get_clock_freq = nrf5x_pwm_get_clock_freq;
pwm_funcs->pwm_get_top_value = nrf5x_pwm_get_top_value;
pwm_funcs->pwm_get_resolution_bits = nrf5x_pwm_get_resolution_bits;
pwm_funcs->pwm_disable = nrf5x_pwm_disable;

switch (dev->pwm_instance_id) {
#if MYNEWT_VAL(PWM_0)
Expand Down
8 changes: 4 additions & 4 deletions hw/mcu/nordic/nrf52xxx/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ pkg.deps.ADC_0:
- "@apache-mynewt-core/hw/drivers/adc/adc_nrf52"

pkg.deps.PWM_0:
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf52"
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf5x"

pkg.deps.PWM_1:
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf52"
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf5x"

pkg.deps.PWM_2:
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf52"
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf5x"

pkg.deps.PWM_3:
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf52"
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf5x"

pkg.deps.TEMP:
- "@apache-mynewt-core/hw/drivers/temp/temp_nrf52"
Expand Down
10 changes: 5 additions & 5 deletions hw/mcu/nordic/nrf52xxx/src/nrf52_periph.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#endif
#if MYNEWT_VAL(PWM_0) || MYNEWT_VAL(PWM_1) || MYNEWT_VAL(PWM_2) || MYNEWT_VAL(PWM_3)
#include "pwm/pwm.h"
#include "pwm_nrf52/pwm_nrf52.h"
#include "pwm_nrf5x/pwm_nrf5x.h"
#endif
#if MYNEWT_VAL(TRNG)
#include "trng/trng.h"
Expand Down Expand Up @@ -307,25 +307,25 @@ nrf52_periph_create_pwm(void)
#if MYNEWT_VAL(PWM_0)
rc = os_dev_create(&os_bsp_pwm0.pwm_os_dev, "pwm0",
OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
nrf52_pwm_dev_init, UINT_TO_POINTER(0));
nrf5x_pwm_dev_init, UINT_TO_POINTER(0));
assert(rc == 0);
#endif
#if MYNEWT_VAL(PWM_1)
rc = os_dev_create(&os_bsp_pwm1.pwm_os_dev, "pwm1",
OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
nrf52_pwm_dev_init, UINT_TO_POINTER(1));
nrf5x_pwm_dev_init, UINT_TO_POINTER(1));
assert(rc == 0);
#endif
#if MYNEWT_VAL(PWM_2)
rc = os_dev_create(&os_bsp_pwm2.pwm_os_dev, "pwm2",
OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
nrf52_pwm_dev_init, UINT_TO_POINTER(2));
nrf5x_pwm_dev_init, UINT_TO_POINTER(2));
assert(rc == 0);
#endif
#if MYNEWT_VAL(PWM_3)
rc = os_dev_create(&os_bsp_pwm3.pwm_os_dev, "pwm3",
OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
nrf52_pwm_dev_init, UINT_TO_POINTER(3));
nrf5x_pwm_dev_init, UINT_TO_POINTER(3));
assert(rc == 0);
#endif
}
Expand Down
9 changes: 4 additions & 5 deletions hw/mcu/nordic/nrf5340/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,17 @@ pkg.deps.UART_3:
pkg.deps.ADC_0:
- "@apache-mynewt-core/hw/drivers/adc/adc_nrf52"

#nrf52 driver is using nrfx so we can just reuse it
pkg.deps.PWM_0:
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf52"
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf5x"

pkg.deps.PWM_1:
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf52"
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf5x"

pkg.deps.PWM_2:
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf52"
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf5x"

pkg.deps.PWM_3:
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf52"
- "@apache-mynewt-core/hw/drivers/pwm/pwm_nrf5x"

pkg.deps.SPI_0_MASTER:
- "@apache-mynewt-core/hw/bus"
Expand Down
Loading

0 comments on commit 24a940d

Please sign in to comment.