summaryrefslogtreecommitdiff
path: root/drivers/sensor/nrf5/temp_nrf5.c
blob: a44fdaa46151d92be802d0952f8cf911a96fa049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (c) 2016 ARM Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <device.h>
#include <sensor.h>
#include <clock_control.h>

#define SYS_LOG_DOMAIN "TEMPNRF5"
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_SENSOR_LEVEL
#include <logging/sys_log.h>

#include "nrf.h"
#include "nrf5_common.h"


/* The nRF5 temperature device returns measurements in 0.25C
 * increments.  Scale to mDegrees C.
 */
#define TEMP_NRF5_TEMP_SCALE (1000000 / 4)

struct temp_nrf5_data {
	struct k_sem device_sync_sem;
	int32_t sample;
	struct device *clk_m16_dev;
};


static int temp_nrf5_sample_fetch(struct device *dev, enum sensor_channel chan)
{
	volatile NRF_TEMP_Type *temp = NRF_TEMP;
	struct temp_nrf5_data *data = dev->driver_data;
	int r;

	SYS_LOG_DBG("");

	if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_TEMP) {
		return -ENOTSUP;
	}

	/* The clock driver for nrf51 currently overloads the
	 * subsystem parameter with a flag to indicate whether or not
	 * it should block.
	 */
	r = clock_control_on(data->clk_m16_dev, (void *)1);
	__ASSERT_NO_MSG(!r);

	temp->TASKS_START = 1;
	k_sem_take(&data->device_sync_sem, K_FOREVER);

	r = clock_control_off(data->clk_m16_dev, (void *)1);
	__ASSERT_NO_MSG(!r);

	data->sample = temp->TEMP;

	temp->TASKS_STOP = 1;

	return 0;
}

static int temp_nrf5_channel_get(struct device *dev,
				enum sensor_channel chan,
				struct sensor_value *val)
{
	struct temp_nrf5_data *data = dev->driver_data;
	int32_t uval;

	SYS_LOG_DBG("");

	if (chan != SENSOR_CHAN_TEMP) {
		return -ENOTSUP;
	}

	uval = data->sample * TEMP_NRF5_TEMP_SCALE;
	val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
	val->val1 = uval / 1000000;
	val->val2 = uval % 1000000;

	return 0;
}

static void temp_nrf5_isr(void *arg)
{
	volatile NRF_TEMP_Type *temp = NRF_TEMP;
	struct device *dev = (struct device *)arg;
	struct temp_nrf5_data *data = dev->driver_data;

	temp->EVENTS_DATARDY = 0;
	k_sem_give(&data->device_sync_sem);
}

static const struct sensor_driver_api temp_nrf5_driver_api = {
	.sample_fetch = temp_nrf5_sample_fetch,
	.channel_get = temp_nrf5_channel_get,
};

static int temp_nrf5_init(struct device *dev);

static struct temp_nrf5_data temp_nrf5_driver;

DEVICE_AND_API_INIT(temp_nrf5, CONFIG_TEMP_NRF5_NAME, temp_nrf5_init,
		    &temp_nrf5_driver, NULL,
		    POST_KERNEL,
		    CONFIG_SENSOR_INIT_PRIORITY, &temp_nrf5_driver_api);

static int temp_nrf5_init(struct device *dev)
{
	volatile NRF_TEMP_Type *temp = NRF_TEMP;
	struct temp_nrf5_data *data = dev->driver_data;

	SYS_LOG_DBG("");

	data->clk_m16_dev =
		device_get_binding(CONFIG_CLOCK_CONTROL_NRF5_M16SRC_DRV_NAME);
	__ASSERT_NO_MSG(data->clk_m16_dev);

	k_sem_init(&data->device_sync_sem, 0, UINT_MAX);
	IRQ_CONNECT(NRF5_IRQ_TEMP_IRQn, CONFIG_TEMP_NRF5_PRI,
		    temp_nrf5_isr, DEVICE_GET(temp_nrf5), 0);
	irq_enable(NRF5_IRQ_TEMP_IRQn);

	temp->INTENSET = TEMP_INTENSET_DATARDY_Set;

	return 0;
}