Skip to content

bsp/nrf5x: nrf52840添加spim驱动 #10576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions bsp/nrf5x/libraries/drivers/drv_adc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -221,13 +221,13 @@ INIT_BOARD_EXPORT(rt_hw_adc_init);
/*test saadc*/
#include <drv_adc.h>

#define SAMPLE_ADC_MODE_SINGLE_ENDED 0 //single-ended mode
#define SAMPLE_ADC_MODE_DIFFERENTIAL 1 //differential mode
#define SAMPLE_ADC_MODE_SINGLE_ENDED 0 /* single-ended mode */
#define SAMPLE_ADC_MODE_DIFFERENTIAL 1 /* differential mode */

#define SAMPLE_ADC_AIN1 1
#define SAMPLE_ADC_AIN2 2
#define SAMPLE_ADC_AIN7 7
#define SAMPLE_ADC_AIN_NC 0 //disable input of AINx
#define SAMPLE_ADC_AIN_NC 0 /* disable input of AINx */

#define SAMPLE_ADC_CHANNEL_0 0
#define SAMPLE_ADC_CHANNEL_1 1
Expand All @@ -242,19 +242,22 @@ void saadc_sample(void)
adc_dev = (rt_adc_device_t)rt_device_find(ADC_NAME);
adc_dev->parent.user_data = &channel_config;

channel_config = (drv_nrfx_saadc_channel_t){.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
channel_config = (drv_nrfx_saadc_channel_t)
{.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
.pin_p = SAMPLE_ADC_AIN1,
.pin_n = SAMPLE_ADC_AIN_NC,
.channel_num = SAMPLE_ADC_CHANNEL_0};
rt_adc_enable(adc_dev, channel_config.channel_num);

channel_config = (drv_nrfx_saadc_channel_t){.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
channel_config = (drv_nrfx_saadc_channel_t)
{.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
.pin_p = SAMPLE_ADC_AIN2,
.pin_n = SAMPLE_ADC_AIN_NC,
.channel_num = SAMPLE_ADC_CHANNEL_1};
rt_adc_enable(adc_dev, channel_config.channel_num);

channel_config = (drv_nrfx_saadc_channel_t){.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
channel_config = (drv_nrfx_saadc_channel_t)
{.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
.pin_p = SAMPLE_ADC_AIN7,
.pin_n = SAMPLE_ADC_AIN_NC,
.channel_num = SAMPLE_ADC_CHANNEL_5};
Expand Down
11 changes: 6 additions & 5 deletions bsp/nrf5x/libraries/drivers/drv_adc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -31,10 +31,10 @@
*/
typedef struct
{
nrf_saadc_mode_t mode; ///< SAADC mode. Single-ended or differential.
uint8_t pin_p; ///< Input positive pin selection.
uint8_t pin_n; ///< Input negative pin selection.
uint8_t channel_num; ///< Channel number.
nrf_saadc_mode_t mode; /* /< SAADC mode. Single-ended or differential. */
uint8_t pin_p; /* /< Input positive pin selection. */
uint8_t pin_n; /* /< Input negative pin selection. */
uint8_t channel_num; /* /< Channel number. */
} drv_nrfx_saadc_channel_t;

typedef struct
Expand All @@ -46,3 +46,4 @@ typedef struct
} drv_nrfx_saadc_result_t;

#endif /* __DRV_ADC_H__ */

8 changes: 5 additions & 3 deletions bsp/nrf5x/libraries/drivers/drv_flash.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -95,15 +95,16 @@ int mcu_flash_write(rt_uint32_t addr, const uint8_t *buf, size_t size)
if (addr % 4 != 0)
{
LOG_E("write addr should be 4-byte alignment");
//4byte write
//else byts
/* 4byte write */
/* else byts */
return -RT_EINVAL;
}

if (size < 1)
{
return -RT_ERROR;
}

if (size % 4 != 0)
{
nrfx_nvmc_bytes_write(addr, buf, size);
Expand Down Expand Up @@ -186,3 +187,4 @@ const struct fal_flash_dev mcu_onchip_flash =
};
#endif
#endif /* BSP_USING_ON_CHIP_FLASH */

16 changes: 11 additions & 5 deletions bsp/nrf5x/libraries/drivers/drv_gpio.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -223,6 +223,7 @@ static rt_err_t nrf5x_pin_attach_irq(struct rt_device *device, rt_base_t pin,
break;
}
}

if(irqindex == -1)
{
return -RT_ENOMEM;
Expand Down Expand Up @@ -298,6 +299,7 @@ static rt_err_t nrf5x_pin_dettach_irq(struct rt_device *device, rt_base_t pin)
break;
}
}

if(i >= irq_quantity)
{
return -RT_ENOSYS;
Expand Down Expand Up @@ -414,6 +416,7 @@ void button_1_callback(void *args)
rt_pin_write(DK_BOARD_LED_1, PIN_HIGH);
}
}

void button_2_callback(void *args)
{
static int flag2 = 0;
Expand All @@ -428,6 +431,7 @@ void button_2_callback(void *args)
rt_pin_write(DK_BOARD_LED_2, PIN_HIGH);
}
}

void button_3_callback(void *args)
{
static int flag3 = 0;
Expand All @@ -442,6 +446,7 @@ void button_3_callback(void *args)
rt_pin_write(DK_BOARD_LED_3, PIN_HIGH);
}
}

void button_4_callback(void *args)
{
static int flag4 = 0;
Expand Down Expand Up @@ -470,21 +475,22 @@ void gpio_sample(void)
rt_pin_write(DK_BOARD_LED_4, PIN_HIGH);

rt_pin_attach_irq(DK_BOARD_BUTTON_1, PIN_IRQ_MODE_FALLING,
button_1_callback, (void*) true); //true: hi_accuracy(IN_EVENT),false: lo_accuracy(PORT_EVENT)
button_1_callback, (void*) true); /* true: hi_accuracy(IN_EVENT),false: lo_accuracy(PORT_EVENT) */
rt_pin_irq_enable(DK_BOARD_BUTTON_1, PIN_IRQ_ENABLE);

rt_pin_attach_irq(DK_BOARD_BUTTON_2, PIN_IRQ_MODE_FALLING,
button_2_callback, (void*) true); //true: hi_accuracy(IN_EVENT),false: lo_accuracy(PORT_EVENT)
button_2_callback, (void*) true); /* true: hi_accuracy(IN_EVENT),false: lo_accuracy(PORT_EVENT) */
rt_pin_irq_enable(DK_BOARD_BUTTON_2, PIN_IRQ_ENABLE);

rt_pin_attach_irq(DK_BOARD_BUTTON_3, PIN_IRQ_MODE_FALLING,
button_3_callback, (void*) true); //true: hi_accuracy(IN_EVENT),false: lo_accuracy(PORT_EVENT)
button_3_callback, (void*) true); /* true: hi_accuracy(IN_EVENT),false: lo_accuracy(PORT_EVENT) */
rt_pin_irq_enable(DK_BOARD_BUTTON_3, PIN_IRQ_ENABLE);

rt_pin_attach_irq(DK_BOARD_BUTTON_4, PIN_IRQ_MODE_FALLING,
button_4_callback, (void*) false); //true: hi_accuracy(IN_EVENT),false: lo_accuracy(PORT_EVENT)
button_4_callback, (void*) false); /* true: hi_accuracy(IN_EVENT),false: lo_accuracy(PORT_EVENT) */
rt_pin_irq_enable(DK_BOARD_BUTTON_4, PIN_IRQ_ENABLE);
}
MSH_CMD_EXPORT(gpio_sample, gpio sample);

#endif /* RT_USING_PIN */

4 changes: 2 additions & 2 deletions bsp/nrf5x/libraries/drivers/drv_gpio.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -34,7 +34,7 @@
struct pin_index
{
int index;
NRF_GPIO_Type *gpio;//NRF_P0 or NRF_P1
NRF_GPIO_Type *gpio;/* NRF_P0 or NRF_P1 */
rt_base_t pin;
};

Expand Down
14 changes: 7 additions & 7 deletions bsp/nrf5x/libraries/drivers/drv_hwtimer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -27,7 +27,7 @@
#ifdef RT_USING_HWTIMER

#ifndef TIM_DEV_INFO_CONFIG
// maxfreq and minfreq unit is HZ
/* maxfreq and minfreq unit is HZ */
#define TIM_DEV_INFO_CONFIG \
{ \
.maxfreq = 16000000, \
Expand Down Expand Up @@ -103,7 +103,7 @@ static void timer_callback(nrf_timer_event_t event_type, void* p_context)
{
rt_hwtimer_t *timer_device = (struct rt_hwtimer_device *)p_context;

// no matter what event_type is(NRF_TIMER_EVENT_COMPARE0 or others), call same function "rt_device_hwtimer_isr"
/* no matter what event_type is(NRF_TIMER_EVENT_COMPARE0 or others), call same function "rt_device_hwtimer_isr" */
LOG_D("timer_callback event_type = %d, inst_id = %d, cc conunt = %d\r\n",
event_type, timer_info->timer_inst.instance_id, timer_info->timer_inst.cc_channel_count);
rt_device_hwtimer_isr(timer_device);
Expand Down Expand Up @@ -137,12 +137,12 @@ static rt_err_t timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_

if (opmode == HWTIMER_MODE_ONESHOT)
{
// means TIMER_SHORTS_COMPARE0_STOP_Msk or TIMER_SHORTS_COMPARE1_STOP_Msk ..., according to cc_channel.
/* means TIMER_SHORTS_COMPARE0_STOP_Msk or TIMER_SHORTS_COMPARE1_STOP_Msk ..., according to cc_channel. */
mask = (nrf_timer_short_mask_t)(1 << (timer_info->cc_channel + 8));
}
else
{
// means TIMER_SHORTS_COMPARE0_CLEAR_Msk or TIMER_SHORTS_COMPARE1_CLEAR_Msk ..., according to cc_channel.
/* means TIMER_SHORTS_COMPARE0_CLEAR_Msk or TIMER_SHORTS_COMPARE1_CLEAR_Msk ..., according to cc_channel. */
mask = (nrf_timer_short_mask_t)(1 << timer_info->cc_channel);
}

Expand Down Expand Up @@ -276,8 +276,8 @@ static rt_uint32_t timer_counter_get(rt_hwtimer_t *timer)

timer_info = (nrf5x_timer_info_t *)timer->parent.user_data;

// capture method will copy the current counter register to the specified cc channel (here is NRF_TIMER_CC_CHANNEL5).
// the specified cc channel cannot be same with the already used cc channels
/* capture method will copy the current counter register to the specified cc channel (here is NRF_TIMER_CC_CHANNEL5). */
/* the specified cc channel cannot be same with the already used cc channels */
count = nrfx_timer_capture(&(timer_info->timer_inst), NRF_TIMER_CC_CHANNEL5);
return count;
}
Expand Down
3 changes: 2 additions & 1 deletion bsp/nrf5x/libraries/drivers/drv_i2c.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -173,3 +173,4 @@ int rt_hw_i2c_init(void)

INIT_BOARD_EXPORT(rt_hw_i2c_init);
#endif /* defined(BSP_USING_I2C0) || defined(BSP_USING_I2C1) */

3 changes: 2 additions & 1 deletion bsp/nrf5x/libraries/drivers/drv_i2c.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -21,3 +21,4 @@ extern "C" {


#endif /* __DRV_I2C_H__ */

9 changes: 6 additions & 3 deletions bsp/nrf5x/libraries/drivers/drv_pwm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -223,9 +223,9 @@ static rt_err_t mcu_hw_pwm_init(struct mcu_pwm *device)
nrfx_pwm_config_t config0 =
{
.irq_priority = _PRIO_APP_LOWEST,
.base_clock = NRF_PWM_CLK_1MHz, //default value
.base_clock = NRF_PWM_CLK_1MHz, /* default value */
.count_mode = NRF_PWM_MODE_UP,
.top_value = 5000, //default vaule
.top_value = 5000, /* default vaule */
.load_mode = NRF_PWM_LOAD_INDIVIDUAL,
.step_mode = NRF_PWM_STEP_AUTO
};
Expand Down Expand Up @@ -414,10 +414,12 @@ static int pwm_led_sample(int argc, char *argv[])
{
pulse -= 500;
}

if (pulse >= period)
{
dir = 0;
}

if (0 == pulse)
{
dir = 1;
Expand All @@ -430,3 +432,4 @@ static int pwm_led_sample(int argc, char *argv[])
MSH_CMD_EXPORT(pwm_led_sample, pwm sample);

#endif

12 changes: 7 additions & 5 deletions bsp/nrf5x/libraries/drivers/drv_qspi_flash.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -14,7 +14,7 @@
#if defined(RT_USING_FAL)
#include <fal.h>

//log
/* log */
#include <rtdbg.h>
#define LOG_TAG "drv.qspiflash"

Expand Down Expand Up @@ -47,14 +47,14 @@ static void configure_memory()
.wren = true
};

// Send reset enable
/* Send reset enable */
err_code = nrfx_qspi_cinstr_xfer(&cinstr_cfg, NULL, NULL);
if (NRFX_SUCCESS != err_code)
{
LOG_E("\r\n ERROR: QSPI_STD_CMD_RSTEN:0x%x\n", err_code);
return ;
}
// Send reset command
/* Send reset command */
cinstr_cfg.opcode = QSPI_STD_CMD_RST;
err_code = nrfx_qspi_cinstr_xfer(&cinstr_cfg, NULL, NULL);
if (NRFX_SUCCESS != err_code)
Expand All @@ -63,7 +63,7 @@ static void configure_memory()
return ;
}

// Switch to qspi mode
/* Switch to qspi mode */
cinstr_cfg.opcode = QSPI_STD_CMD_WRSR;
cinstr_cfg.length = NRF_QSPI_CINSTR_LEN_2B;
err_code = nrfx_qspi_cinstr_xfer(&cinstr_cfg, &temporary, NULL);
Expand All @@ -73,6 +73,7 @@ static void configure_memory()
return;
}
}

static int init(void)
{
rt_uint32_t err_code;
Expand Down Expand Up @@ -151,3 +152,4 @@ struct fal_flash_dev nor_flash0 =
};

#endif

8 changes: 4 additions & 4 deletions bsp/nrf5x/libraries/drivers/drv_rtc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -33,7 +33,7 @@
#define RTC_INSTANCE_ID (2)
#endif

#define TICK_FREQUENCE_HZ (RT_TICK_PER_SECOND) // RTC tick frequence, in HZ
#define TICK_FREQUENCE_HZ (RT_TICK_PER_SECOND) /* RTC tick frequence, in HZ */

static struct rt_rtc_device rtc;
static time_t init_time;
Expand Down Expand Up @@ -62,15 +62,15 @@ static rt_err_t rt_rtc_config(struct rt_device *dev)
nrf_clock_lf_src_set(NRF_CLOCK, (nrf_clock_lfclk_t)NRFX_CLOCK_CONFIG_LF_SRC);
nrfx_clock_lfclk_start();

//Initialize RTC instance
/* Initialize RTC instance */
nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
config.prescaler = RTC_PRESCALER;

nrfx_rtc_init(&rtc_instance, &config, rtc_callback);

nrfx_rtc_tick_enable(&rtc_instance, true);

//Power on RTC instance
/* Power on RTC instance */
nrfx_rtc_enable(&rtc_instance);

return RT_EOK;
Expand Down
Loading
Loading