Skip to content

Commit c1ffac9

Browse files
authored
Merge pull request #1 from ajbaabf/nrf5_no_sdk
added support for hardware temperature sensor
2 parents 382f718 + 0e72309 commit c1ffac9

18 files changed

+253
-1
lines changed

nrf5/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ SRC_HAL = $(addprefix hal/,\
119119
hal_twi.c \
120120
hal_adc.c \
121121
hal_adce.c \
122+
hal_temp.c \
122123
)
123124

124125
ifeq ($(MCU_VARIANT), nrf52)
@@ -161,6 +162,7 @@ DRIVERS_SRC_C += $(addprefix modules/,\
161162
machine/rtc.c \
162163
machine/pwm.c \
163164
machine/led.c \
165+
machine/temp.c \
164166
usocket/modusocket.c \
165167
network/modnetwork.c \
166168
uos/moduos.c \

nrf5/boards/pca10000/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#define MICROPY_PY_MACHINE_HW_SPI (0)
3434
#define MICROPY_PY_MACHINE_PWM (0)
35+
#define MICROPY_PY_MACHINE_TEMP (1)
3536

3637
#define MICROPY_HW_HAS_SWITCH (0)
3738
#define MICROPY_HW_HAS_FLASH (0)

nrf5/boards/pca10000/nrf51_hal_conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
#define HAL_UART_MODULE_ENABLED
55
#define HAL_TIME_MODULE_ENABLED
6+
#define HAL_TEMP_MODULE_ENABLED
67

78
#endif // NRF51_HAL_CONF_H__

nrf5/boards/pca10001/mpconfigboard.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#define MICROPY_PY_MACHINE_HW_SPI (0)
3434
#define MICROPY_PY_MACHINE_PWM (0)
35+
#define MICROPY_PY_MACHINE_TEMP (1)
3536

3637
#define MICROPY_HW_HAS_SWITCH (0)
3738
#define MICROPY_HW_HAS_FLASH (0)
@@ -46,6 +47,7 @@
4647
#define MICROPY_HW_ENABLE_DAC (0)
4748
#define MICROPY_HW_ENABLE_CAN (0)
4849

50+
4951
#define MICROPY_HW_LED_COUNT (2)
5052
#define MICROPY_HW_LED_PULLUP (0)
5153

@@ -57,6 +59,6 @@
5759
#define MICROPY_HW_UART1_TX (pin_A9)
5860
#define MICROPY_HW_UART1_CTS (pin_A10)
5961
#define MICROPY_HW_UART1_RTS (pin_A8)
60-
#define MICROPY_HW_UART1_HWFC (1)
62+
#define MICROPY_HW_UART1_HWFC (0)
6163

6264
#define HELP_TEXT_BOARD_LED "1,2"

nrf5/boards/pca10001/nrf51_hal_conf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@
44
#define HAL_UART_MODULE_ENABLED
55
// #define HAL_SPI_MODULE_ENABLED
66
#define HAL_TIME_MODULE_ENABLED
7+
#define HAL_RTC_MODULE_ENABLED
8+
#define HAL_TIMER_MODULE_ENABLED
9+
#define HAL_TWI_MODULE_ENABLED
10+
#define HAL_ADC_MODULE_ENABLED
11+
#define HAL_TEMP_MODULE_ENABLED
712

813
#endif // NRF51_HAL_CONF_H__

nrf5/boards/pca10028/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#define MICROPY_PY_MACHINE_RTC (1)
4444
#define MICROPY_PY_MACHINE_I2C (1)
4545
#define MICROPY_PY_MACHINE_ADC (1)
46+
#define MICROPY_PY_MACHINE_TEMP (1)
4647

4748
#define MICROPY_PY_USOCKET (0)
4849
#define MICROPY_PY_NETWORK (0)

nrf5/boards/pca10028/nrf51_hal_conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
#define HAL_TIMER_MODULE_ENABLED
99
#define HAL_TWI_MODULE_ENABLED
1010
#define HAL_ADC_MODULE_ENABLED
11+
#define HAL_TEMP_MODULE_ENABLED
1112

1213
#endif // NRF51_HAL_CONF_H__

nrf5/boards/pca10031/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#define MICROPY_PY_SYS_PLATFORM "nrf51-dongle"
3232

3333
#define MICROPY_PY_MACHINE_PWM (0)
34+
#define MICROPY_PY_MACHINE_TEMP (1)
3435

3536
#define MICROPY_HW_HAS_SWITCH (0)
3637
#define MICROPY_HW_HAS_FLASH (0)

nrf5/boards/pca10031/nrf51_hal_conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
#define HAL_UART_MODULE_ENABLED
55
#define HAL_SPI_MODULE_ENABLED
66
#define HAL_TIME_MODULE_ENABLED
7+
#define HAL_TEMP_MODULE_ENABLED
78

89
#endif // NRF51_HAL_CONF_H__

nrf5/boards/pca10040/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#define MICROPY_PY_MACHINE_RTC (1)
3737
#define MICROPY_PY_MACHINE_I2C (1)
3838
#define MICROPY_PY_MACHINE_ADC (1)
39+
#define MICROPY_PY_MACHINE_TEMP (1)
3940

4041
#define MICROPY_PY_DISPLAY (1)
4142
#define MICROPY_PY_DISPLAY_EPAPER_SLD00200P (1)

nrf5/boards/pca10040/nrf52_hal_conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define HAL_TIMER_MODULE_ENABLED
1010
#define HAL_TWI_MODULE_ENABLED
1111
#define HAL_ADCE_MODULE_ENABLED
12+
#define HAL_TEMP_MODULE_ENABLED
1213
// #define HAL_UARTE_MODULE_ENABLED
1314
// #define HAL_SPIE_MODULE_ENABLED
1415
// #define HAL_TWIE_MODULE_ENABLED

nrf5/boards/pca10056/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#define MICROPY_PY_MACHINE_HW_SPI (1)
3535
#define MICROPY_PY_MACHINE_I2C (1)
3636
#define MICROPY_PY_MACHINE_ADC (1)
37+
#define MICROPY_PY_MACHINE_TEMP (1)
3738

3839
#define MICROPY_PY_DISPLAY (1)
3940
#define MICROPY_PY_DISPLAY_EPAPER_SLD00200P (1)

nrf5/boards/pca10056/nrf52_hal_conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define HAL_TIMER_MODULE_ENABLED
1010
#define HAL_TWI_MODULE_ENABLED
1111
#define HAL_ADCE_MODULE_ENABLED
12+
#define HAL_TEMP_MODULE_ENABLED
1213
// #define HAL_UARTE_MODULE_ENABLED
1314
// #define HAL_SPIE_MODULE_ENABLED
1415
// #define HAL_TWIE_MODULE_ENABLED

nrf5/hal/hal_temp.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Bander F. Ajba
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include "mphalport.h"
29+
#include "hal_temp.h"
30+
31+
#ifdef HAL_TEMP_MODULE_ENABLED
32+
33+
void hal_temp_init(void) {
34+
// @note Workaround for PAN_028 rev2.0A anomaly 31 - TEMP: Temperature offset value has to be manually loaded to the TEMP module
35+
*(uint32_t *) 0x4000C504 = 0;
36+
}
37+
38+
int32_t hal_temp_read(void) {
39+
int32_t volatile temp;
40+
hal_temp_init();
41+
42+
NRF_TEMP->TASKS_START = 1; // Start the temperature measurement.
43+
44+
while (NRF_TEMP->EVENTS_DATARDY == 0) {
45+
// Do nothing.
46+
}
47+
48+
NRF_TEMP->EVENTS_DATARDY = 0;
49+
50+
// @note Workaround for PAN_028 rev2.0A anomaly 29 - TEMP: Stop task clears the TEMP register.
51+
temp = (((NRF_TEMP->TEMP & MASK_SIGN) != 0) ? (NRF_TEMP->TEMP | MASK_SIGN_EXTENSION) : (NRF_TEMP->TEMP) / 4);
52+
53+
// @note Workaround for PAN_028 rev2.0A anomaly 30 - TEMP: Temp module analog front end does not power down when DATARDY event occurs.
54+
NRF_TEMP->TASKS_STOP = 1; // Stop the temperature measurement.
55+
return temp;
56+
}
57+
58+
#endif // HAL_TEMP_MODULE_ENABLED

nrf5/hal/hal_temp.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Bander F. Ajba
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef HAL_TEMP_H__
28+
#define HAL_TEMP_H__
29+
30+
#include "nrf.h"
31+
32+
#define MASK_SIGN (0x00000200UL)
33+
#define MASK_SIGN_EXTENSION (0xFFFFFC00UL)
34+
35+
void hal_temp_init(void);
36+
37+
int32_t hal_temp_read(void);
38+
39+
#endif

nrf5/modules/machine/modmachine.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
#if MICROPY_PY_MACHINE_ADC
4747
#include "adc.h"
4848
#endif
49+
#if MICROPY_PY_MACHINE_TEMP
50+
#include "temp.h"
51+
#endif
4952

5053

5154
#define PYB_RESET_HARD (0)
@@ -180,6 +183,9 @@ STATIC const mp_map_elem_t machine_module_globals_table[] = {
180183
#endif
181184
#if MICROPY_PY_MACHINE_PWM
182185
{ MP_OBJ_NEW_QSTR(MP_QSTR_PWM), (mp_obj_t)&machine_hard_pwm_type },
186+
#endif
187+
#if MICROPY_PY_MACHINE_TEMP
188+
{ MP_OBJ_NEW_QSTR(MP_QSTR_Temp), (mp_obj_t)&machine_temp_type },
183189
#endif
184190
{ MP_OBJ_NEW_QSTR(MP_QSTR_HARD_RESET), MP_OBJ_NEW_SMALL_INT(PYB_RESET_HARD) },
185191
{ MP_OBJ_NEW_QSTR(MP_QSTR_WDT_RESET), MP_OBJ_NEW_SMALL_INT(PYB_RESET_WDT) },

nrf5/modules/machine/temp.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Bander F. Ajba
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <string.h>
29+
30+
#include "py/nlr.h"
31+
#include "py/runtime.h"
32+
#include "py/mphal.h"
33+
#include "temp.h"
34+
#include "hal_temp.h"
35+
36+
#if MICROPY_PY_MACHINE_TEMP
37+
38+
typedef struct _machine_temp_obj_t {
39+
mp_obj_base_t base;
40+
} machine_temp_obj_t;
41+
42+
/// \method __str__()
43+
/// Return a string describing the Temp object.
44+
STATIC void machine_temp_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
45+
machine_temp_obj_t *self = o;
46+
47+
(void)self;
48+
49+
mp_printf(print, "Temp");
50+
}
51+
52+
/******************************************************************************/
53+
/* MicroPython bindings for machine API */
54+
55+
STATIC mp_obj_t machine_temp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
56+
static const mp_arg_t allowed_args[] = {
57+
{ },
58+
};
59+
60+
// parse args
61+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
62+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
63+
64+
machine_temp_obj_t *self = m_new_obj(machine_temp_obj_t);
65+
66+
self->base.type = &machine_temp_type;
67+
68+
return MP_OBJ_FROM_PTR(self);
69+
}
70+
71+
/// \method read()
72+
/// Get temperature.
73+
STATIC mp_obj_t machine_temp_read(mp_uint_t n_args, const mp_obj_t *args) {
74+
75+
return MP_OBJ_NEW_SMALL_INT(hal_temp_read());
76+
}
77+
78+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_temp_read_obj, 0, 1, machine_temp_read);
79+
80+
STATIC const mp_map_elem_t machine_temp_locals_dict_table[] = {
81+
// instance methods
82+
// class methods
83+
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_machine_temp_read_obj },
84+
};
85+
86+
STATIC MP_DEFINE_CONST_DICT(machine_temp_locals_dict, machine_temp_locals_dict_table);
87+
88+
const mp_obj_type_t machine_temp_type = {
89+
{ &mp_type_type },
90+
.name = MP_QSTR_Temp,
91+
.make_new = machine_temp_make_new,
92+
.locals_dict = (mp_obj_t)&machine_temp_locals_dict,
93+
.print = machine_temp_print,
94+
};
95+
96+
#endif // MICROPY_PY_MACHINE_TEMP

nrf5/modules/machine/temp.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Bander F. Ajba
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef TEMP_H__
28+
#define TEMP_H__
29+
30+
extern const mp_obj_type_t machine_temp_type;
31+
32+
int32_t temp_read(void);
33+
34+
#endif // TEMP_H__

0 commit comments

Comments
 (0)