Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
天中雨水
rt-thread
提交
f177f11e
R
rt-thread
项目概览
天中雨水
/
rt-thread
该项目与 Fork 源项目分叉
Fork自
RT-Thread / rt-thread
通知
2
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
f177f11e
编写于
8月 21, 2020
作者:
B
Bernard Xiong
提交者:
GitHub
8月 21, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3835 from guohp1128/master
add nrf5x adc driver
上级
8f0be7cf
614f0a76
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
438 addition
and
1 deletion
+438
-1
bsp/nrf5x/libraries/drivers/SConscript
bsp/nrf5x/libraries/drivers/SConscript
+3
-0
bsp/nrf5x/libraries/drivers/drv_adc.c
bsp/nrf5x/libraries/drivers/drv_adc.c
+261
-0
bsp/nrf5x/libraries/drivers/drv_adc.h
bsp/nrf5x/libraries/drivers/drv_adc.h
+46
-0
bsp/nrf5x/libraries/drivers/drv_gpio.c
bsp/nrf5x/libraries/drivers/drv_gpio.c
+119
-0
bsp/nrf5x/nrf52840/board/Kconfig
bsp/nrf5x/nrf52840/board/Kconfig
+9
-1
未找到文件。
bsp/nrf5x/libraries/drivers/SConscript
浏览文件 @
f177f11e
...
...
@@ -22,6 +22,9 @@ if GetDepend(['BSP_USING_SPI']):
if
GetDepend
([
'BSP_USING_GPIO'
]):
src
+=
[
'drv_gpio.c'
]
if
GetDepend
([
'BSP_USING_SAADC'
]):
src
+=
[
'drv_adc.c'
]
if
GetDepend
([
'BSP_USING_PWM'
]):
src
+=
[
'drv_pwm.c'
]
...
...
bsp/nrf5x/libraries/drivers/drv_adc.c
0 → 100644
浏览文件 @
f177f11e
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-08-18 guohp1128 the first version
*/
#include "drv_adc.h"
#ifdef RT_USING_ADC
struct
rt_adc_device
nrf5x_adc_device
;
drv_nrfx_saadc_result_t
results
;
nrf_saadc_value_t
result_buff_cache
[
8
];
static
void
nrf5x_saadc_event_hdr
(
nrfx_saadc_evt_t
const
*
p_event
)
{
uint8_t
i
,
j
;
if
(
p_event
->
type
==
NRFX_SAADC_EVT_DONE
)
{
j
=
0
;
for
(
i
=
0
;
i
<
8
;
i
++
)
{
if
(
results
.
channels
[
i
].
channel_index
==
i
)
{
results
.
result_buffer
[
i
]
=
result_buff_cache
[
j
];
j
++
;
}
}
results
.
done
=
1
;
}
}
static
uint32_t
get_channels_mask
(
void
)
{
uint8_t
i
;
uint32_t
mask
=
0
;
for
(
i
=
0
;
i
<
8
;
i
++
)
{
if
(
results
.
channels
[
i
].
channel_index
!=
0xff
)
{
mask
|=
(
1
<<
results
.
channels
[
i
].
channel_index
);
}
}
return
mask
;
}
static
void
set_channels
(
drv_nrfx_saadc_channel_t
*
channel
)
{
uint8_t
i
;
if
(
channel
->
mode
==
NRF_SAADC_MODE_SINGLE_ENDED
)
{
results
.
channels
[
channel
->
channel_num
]
=
(
nrfx_saadc_channel_t
)
NRFX_SAADC_DEFAULT_CHANNEL_SE
(
channel
->
pin_p
+
1
,
channel
->
channel_num
);
}
else
if
(
channel
->
mode
==
NRF_SAADC_MODE_DIFFERENTIAL
)
{
results
.
channels
[
channel
->
channel_num
]
=
(
nrfx_saadc_channel_t
)
NRFX_SAADC_DEFAULT_CHANNEL_DIFFERENTIAL
(
channel
->
pin_p
+
1
,
channel
->
pin_n
+
1
,
channel
->
channel_num
);
}
results
.
channel_count
=
0
;
for
(
i
=
0
;
i
<
8
;
i
++
)
{
if
(
results
.
channels
[
i
].
channel_index
!=
0xff
)
{
results
.
channel_count
++
;
}
}
}
/* channel: 0-7 */
static
rt_err_t
nrf5x_adc_enabled
(
struct
rt_adc_device
*
device
,
rt_uint32_t
channel
,
rt_bool_t
enabled
)
{
nrfx_err_t
err_code
=
NRFX_SUCCESS
;
uint8_t
i
,
j
;
if
(
enabled
)
{
RT_ASSERT
(
device
!=
RT_NULL
);
RT_ASSERT
(
device
->
parent
.
user_data
!=
RT_NULL
);
drv_nrfx_saadc_channel_t
*
drv_channel_config
=
NULL
;
drv_channel_config
=
(
drv_nrfx_saadc_channel_t
*
)
device
->
parent
.
user_data
;
set_channels
(
drv_channel_config
);
nrfx_saadc_channel_t
channels_cache
[
results
.
channel_count
];
j
=
0
;
for
(
i
=
0
;
i
<
8
;
i
++
)
{
if
(
results
.
channels
[
i
].
channel_index
!=
0xff
)
{
channels_cache
[
j
]
=
results
.
channels
[
i
];
j
++
;
}
}
err_code
=
nrfx_saadc_channels_config
(
channels_cache
,
results
.
channel_count
);
err_code
=
nrfx_saadc_simple_mode_set
(
get_channels_mask
(),
NRF_SAADC_RESOLUTION_12BIT
,
NRF_SAADC_OVERSAMPLE_DISABLED
,
nrf5x_saadc_event_hdr
);
err_code
=
nrfx_saadc_buffer_set
(
result_buff_cache
,
results
.
channel_count
);
}
else
{
results
.
channels
[
channel
].
channel_index
=
0xff
;
results
.
channel_count
=
0
;
for
(
i
=
0
;
i
<
8
;
i
++
)
{
if
(
results
.
channels
[
i
].
channel_index
!=
0xff
)
{
results
.
channel_count
++
;
}
}
if
(
results
.
channel_count
==
0
)
{
nrfx_saadc_channel_t
channels_cache
[
1
];
err_code
=
nrfx_saadc_channels_config
(
channels_cache
,
0
);
return
err_code
;
}
else
{
nrfx_saadc_channel_t
channels_cache
[
results
.
channel_count
];
j
=
0
;
for
(
i
=
0
;
i
<
8
;
i
++
)
{
if
(
results
.
channels
[
i
].
channel_index
!=
0xff
)
{
channels_cache
[
j
]
=
results
.
channels
[
i
];
j
++
;
}
}
err_code
=
nrfx_saadc_channels_config
(
channels_cache
,
results
.
channel_count
);
err_code
=
nrfx_saadc_simple_mode_set
(
get_channels_mask
(),
NRF_SAADC_RESOLUTION_12BIT
,
NRF_SAADC_OVERSAMPLE_DISABLED
,
nrf5x_saadc_event_hdr
);
err_code
=
nrfx_saadc_buffer_set
(
result_buff_cache
,
results
.
channel_count
);
}
}
return
err_code
;
}
static
rt_err_t
nrf5x_get_adc_value
(
struct
rt_adc_device
*
device
,
rt_uint32_t
channel
,
rt_uint32_t
*
value
)
{
nrfx_err_t
err_code
=
NRFX_SUCCESS
;
if
(
results
.
channels
[
channel
].
channel_index
!=
0xff
)
{
results
.
done
=
0
;
err_code
=
nrfx_saadc_mode_trigger
();
while
(
results
.
done
==
0
)
{
;
}
*
value
=
results
.
result_buffer
[
channel
];
results
.
done
=
0
;
}
return
err_code
;
}
static
const
struct
rt_adc_ops
nrf5x_adc_ops
=
{
.
enabled
=
nrf5x_adc_enabled
,
.
convert
=
nrf5x_get_adc_value
,
};
int
rt_hw_adc_init
(
void
)
{
int
result
=
RT_EOK
;
uint8_t
i
;
char
name_buf
[
6
]
=
{
'S'
,
'A'
,
'A'
,
'D'
,
'C'
,
0
};
for
(
i
=
0
;
i
<
8
;
i
++
)
{
results
.
channels
[
i
].
channel_index
=
0xff
;
results
.
result_buffer
[
i
]
=
0
;
results
.
channel_count
=
0
;
results
.
done
=
0
;
}
/* initializing SAADC interrupt priority */
if
(
nrfx_saadc_init
(
NRFX_SAADC_CONFIG_IRQ_PRIORITY
)
!=
NRFX_SUCCESS
)
{
rt_kprintf
(
"%s init failed"
,
name_buf
);
rt_kprintf
(
"The driver is already initialized."
);
result
=
-
RT_ERROR
;
}
else
{
/* register ADC device */
if
(
rt_hw_adc_register
(
&
nrf5x_adc_device
,
name_buf
,
&
nrf5x_adc_ops
,
nrf5x_adc_device
.
parent
.
user_data
)
==
RT_EOK
)
{
rt_kprintf
(
"%s init success"
,
name_buf
);
}
else
{
rt_kprintf
(
"%s register failed"
,
name_buf
);
result
=
-
RT_ERROR
;
}
}
return
result
;
}
INIT_BOARD_EXPORT
(
rt_hw_adc_init
);
/*test saadc*/
#include <drv_adc.h>
void
saadc_sample
(
void
)
{
drv_nrfx_saadc_channel_t
channel_config
;
rt_uint32_t
result
;
rt_adc_device_t
adc_dev
;
adc_dev
=
(
rt_adc_device_t
)
rt_device_find
(
"SAADC"
);
adc_dev
->
parent
.
user_data
=
&
channel_config
;
channel_config
=
(
drv_nrfx_saadc_channel_t
){.
mode
=
0
,
.
pin_p
=
1
,
.
pin_n
=
1
,
.
channel_num
=
0
};
rt_adc_enable
(
adc_dev
,
channel_config
.
channel_num
);
channel_config
=
(
drv_nrfx_saadc_channel_t
){.
mode
=
0
,
.
pin_p
=
2
,
.
pin_n
=
1
,
.
channel_num
=
1
};
rt_adc_enable
(
adc_dev
,
channel_config
.
channel_num
);
channel_config
=
(
drv_nrfx_saadc_channel_t
){.
mode
=
0
,
.
pin_p
=
7
,
.
pin_n
=
1
,
.
channel_num
=
5
};
rt_adc_enable
(
adc_dev
,
channel_config
.
channel_num
);
int
count
=
1
;
while
(
count
++
)
{
result
=
rt_adc_read
(
adc_dev
,
0
);
rt_kprintf
(
"saadc channel 0 value = %d, "
,
result
);
result
=
rt_adc_read
(
adc_dev
,
1
);
rt_kprintf
(
"saadc channel 1 value = %d, "
,
result
);
result
=
rt_adc_read
(
adc_dev
,
5
);
rt_kprintf
(
"saadc channel 5 value = %d"
,
result
);
rt_kprintf
(
"
\r\n
"
);
rt_thread_mdelay
(
1000
);
}
}
MSH_CMD_EXPORT
(
saadc_sample
,
saadc
sample
);
#endif
/* RT_USING_ADC */
bsp/nrf5x/libraries/drivers/drv_adc.h
0 → 100644
浏览文件 @
f177f11e
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-08-18 guohp1128 the first version
*/
#ifndef __DRV_ADC_H__
#define __DRV_ADC_H__
#include <board.h>
#include "rtdevice.h"
#include <hal/nrf_saadc.h>
#include <drivers/include/nrfx_saadc.h>
/*
previous definition in application
set single-ended mode or differential mode.
selection ADC input pin, and config the number of Channel.
mode: 0 single-ended mode,1 differential mode
pin_p: 0-7
pin_n: 0-7,if single-ended mode, pin_n invalid
channel_num: 0-7
*/
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.
}
drv_nrfx_saadc_channel_t
;
typedef
struct
{
nrfx_saadc_channel_t
channels
[
8
];
uint8_t
channel_count
;
nrf_saadc_value_t
result_buffer
[
8
];
uint8_t
done
;
}
drv_nrfx_saadc_result_t
;
#endif
/* __DRV_ADC_H__ */
bsp/nrf5x/libraries/drivers/drv_gpio.c
浏览文件 @
f177f11e
...
...
@@ -46,6 +46,24 @@ static const struct pin_index pins[] =
__NRF5X_PIN
(
29
,
0
,
29
),
__NRF5X_PIN
(
30
,
0
,
30
),
__NRF5X_PIN
(
31
,
0
,
31
),
#ifdef SOC_NRF52840
__NRF5X_PIN
(
32
,
1
,
0
),
__NRF5X_PIN
(
33
,
1
,
1
),
__NRF5X_PIN
(
34
,
1
,
2
),
__NRF5X_PIN
(
35
,
1
,
3
),
__NRF5X_PIN
(
36
,
1
,
4
),
__NRF5X_PIN
(
37
,
1
,
5
),
__NRF5X_PIN
(
38
,
1
,
6
),
__NRF5X_PIN
(
39
,
1
,
7
),
__NRF5X_PIN
(
40
,
1
,
8
),
__NRF5X_PIN
(
41
,
1
,
9
),
__NRF5X_PIN
(
42
,
1
,
10
),
__NRF5X_PIN
(
43
,
1
,
11
),
__NRF5X_PIN
(
44
,
1
,
12
),
__NRF5X_PIN
(
45
,
1
,
13
),
__NRF5X_PIN
(
46
,
1
,
14
),
__NRF5X_PIN
(
47
,
1
,
15
),
#endif
/* SOC_NRF52840 */
};
/* EVENTS_IN[n](n=0..7) and EVENTS_PORT */
...
...
@@ -356,4 +374,105 @@ int rt_hw_pin_init(void)
}
INIT_BOARD_EXPORT
(
rt_hw_pin_init
);
/* test GPIO write, read, input interrupt */
#define DK_BOARD_LED_1 13
#define DK_BOARD_LED_2 14
#define DK_BOARD_LED_3 15
#define DK_BOARD_LED_4 16
#define DK_BOARD_BUTTON_1 11
#define DK_BOARD_BUTTON_2 12
#define DK_BOARD_BUTTON_3 24
#define DK_BOARD_BUTTON_4 25
void
button_1_callback
(
void
*
args
)
{
static
int
flag1
=
0
;
if
(
flag1
==
0
)
{
flag1
=
1
;
rt_pin_write
(
DK_BOARD_LED_1
,
PIN_LOW
);
}
else
{
flag1
=
0
;
rt_pin_write
(
DK_BOARD_LED_1
,
PIN_HIGH
);
}
}
void
button_2_callback
(
void
*
args
)
{
static
int
flag2
=
0
;
if
(
flag2
==
0
)
{
flag2
=
1
;
rt_pin_write
(
DK_BOARD_LED_2
,
PIN_LOW
);
}
else
{
flag2
=
0
;
rt_pin_write
(
DK_BOARD_LED_2
,
PIN_HIGH
);
}
}
void
button_3_callback
(
void
*
args
)
{
static
int
flag3
=
0
;
if
(
flag3
==
0
)
{
flag3
=
1
;
rt_pin_write
(
DK_BOARD_LED_3
,
PIN_LOW
);
}
else
{
flag3
=
0
;
rt_pin_write
(
DK_BOARD_LED_3
,
PIN_HIGH
);
}
}
void
button_4_callback
(
void
*
args
)
{
static
int
flag4
=
0
;
if
(
flag4
==
0
)
{
flag4
=
1
;
rt_pin_write
(
DK_BOARD_LED_4
,
PIN_LOW
);
}
else
{
flag4
=
0
;
rt_pin_write
(
DK_BOARD_LED_4
,
PIN_HIGH
);
}
}
void
gpio_sample
(
void
)
{
rt_err_t
err_code
;
rt_pin_mode
(
DK_BOARD_LED_1
,
PIN_MODE_OUTPUT
);
rt_pin_mode
(
DK_BOARD_LED_2
,
PIN_MODE_OUTPUT
);
rt_pin_mode
(
DK_BOARD_LED_3
,
PIN_MODE_OUTPUT
);
rt_pin_mode
(
DK_BOARD_LED_4
,
PIN_MODE_OUTPUT
);
rt_pin_write
(
DK_BOARD_LED_1
,
PIN_HIGH
);
rt_pin_write
(
DK_BOARD_LED_2
,
PIN_HIGH
);
rt_pin_write
(
DK_BOARD_LED_3
,
PIN_HIGH
);
rt_pin_write
(
DK_BOARD_LED_4
,
PIN_HIGH
);
err_code
=
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)
rt_pin_irq_enable
(
DK_BOARD_BUTTON_1
,
PIN_IRQ_ENABLE
);
err_code
=
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)
rt_pin_irq_enable
(
DK_BOARD_BUTTON_2
,
PIN_IRQ_ENABLE
);
err_code
=
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)
rt_pin_irq_enable
(
DK_BOARD_BUTTON_3
,
PIN_IRQ_ENABLE
);
err_code
=
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)
rt_pin_irq_enable
(
DK_BOARD_BUTTON_4
,
PIN_IRQ_ENABLE
);
}
MSH_CMD_EXPORT
(
gpio_sample
,
gpio
sample
);
#endif
/* RT_USING_PIN */
bsp/nrf5x/nrf52840/board/Kconfig
浏览文件 @
f177f11e
...
...
@@ -12,7 +12,6 @@ config SOC_NORDIC
config SOC_NORDIC
default y
menu "Onboard Peripheral Drivers"
config BSP_USING_JLINK_TO_USART
bool "Enable JLINK TO USART (uart0|RX_PIN:8|TX_PIN:6)"
...
...
@@ -61,6 +60,15 @@ menu "On-chip Peripheral Drivers"
bool "Enable GPIO"
select RT_USING_PIN
default y
config BSP_USING_SAADC
bool "Enable SAADC"
select RT_USING_ADC
default n
if BSP_USING_SAADC
config NRFX_SAADC_ENABLED
int
default 1
endif
menuconfig BSP_USING_PWM
bool "Enable PWM"
select RT_USING_PWM
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录