Skip to content

Commit 2ff5726

Browse files
committed
[NRF5]
HAL driver: Add changes from needad for nrf52840 support us_ticker, spi, sleep, serial, pwmout, pinmap, object, i2c, gpio, analogin Add compatibility patches for: - SoftDevice headers renamed (redirec by a few h files) - sdk configuration (redirect by sdk_config.h files) - renaming of func in softdevice handler module
1 parent c62a33e commit 2ff5726

File tree

35 files changed

+253
-78
lines changed

35 files changed

+253
-78
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_err.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_gap.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_gatt.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_gattc.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_gatts.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_hci.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_l2cap.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_ranges.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_types.h"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef _SDK_CONFIG_H_
2+
#define _SDK_CONFIG_H_
3+
4+
#include "nrf_drv_config.h"
5+
6+
#define UART_DEFAULT_CONFIG_BAUDRATE UART0_CONFIG_BAUDRATE
7+
#define UART_DEFAULT_CONFIG_PARITY UART0_CONFIG_PARITY
8+
#define UART_DEFAULT_CONFIG_HWFC UART0_CONFIG_HWFC
9+
#define CTS_PIN_NUMBER UART0_CONFIG_PSEL_CTS
10+
#define RTS_PIN_NUMBER UART0_CONFIG_PSEL_RTS
11+
12+
#define SPIS_DEFAULT_ORC NRF_DRV_SPIS_DEFAULT_ORC
13+
#define SPIS_DEFAULT_CONFIG_IRQ_PRIORITY SPIS1_CONFIG_IRQ_PRIORITY;
14+
#define SPIS_DEFAULT_ORC NRF_DRV_SPIS_DEFAULT_ORC
15+
#define SPIS_DEFAULT_DEF NRF_DRV_SPIS_DEFAULT_DEF
16+
17+
#define SPI_DEFAULT_CONFIG_IRQ_PRIORITY SPI1_CONFIG_IRQ_PRIORITY
18+
19+
#endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_err.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_gap.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_gatt.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_gattc.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_gatts.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_hci.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_l2cap.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_ranges.h"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include ".\nrf_ble_types.h"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef _SDK_CONFIG_H_
2+
#define _SDK_CONFIG_H_
3+
4+
#include "nrf_drv_config.h"
5+
6+
#define UART_DEFAULT_CONFIG_BAUDRATE UART0_CONFIG_BAUDRATE
7+
#define UART_DEFAULT_CONFIG_PARITY UART0_CONFIG_PARITY
8+
#define UART_DEFAULT_CONFIG_HWFC UART0_CONFIG_HWFC
9+
#define CTS_PIN_NUMBER UART0_CONFIG_PSEL_CTS
10+
#define RTS_PIN_NUMBER UART0_CONFIG_PSEL_RTS
11+
12+
#define SPIS_DEFAULT_ORC NRF_DRV_SPIS_DEFAULT_ORC
13+
#define SPIS_DEFAULT_CONFIG_IRQ_PRIORITY SPIS1_CONFIG_IRQ_PRIORITY;
14+
#define SPIS_DEFAULT_ORC NRF_DRV_SPIS_DEFAULT_ORC
15+
#define SPIS_DEFAULT_DEF NRF_DRV_SPIS_DEFAULT_DEF
16+
17+
#define SPI_DEFAULT_CONFIG_IRQ_PRIORITY SPI1_CONFIG_IRQ_PRIORITY
18+
19+
#define PWM_DEFAULT_CONFIG_IRQ_PRIORITY PWM0_CONFIG_IRQ_PRIORITY
20+
21+
#endif

targets/TARGET_NORDIC/TARGET_NRF5/TARGET_SDK11/softdevice/common/softdevice_handler/softdevice_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ uint32_t softdevice_enable(ble_enable_params_t * p_ble_enable_params);
232232
/**@cond NO_DOXYGEN */
233233
void intern_softdevice_events_execute(void);
234234

235-
235+
#define softdevice_handler_is_enabled softdevice_handler_isEnabled
236236
/**@endcond */
237237

238238
#endif // SOFTDEVICE_HANDLER_H__

targets/TARGET_NORDIC/TARGET_NRF5/TARGET_MCU_NRF52832/analogin_api.c renamed to targets/TARGET_NORDIC/TARGET_NRF5/analogin_api.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
#ifndef TARGET_MCU_NRF51822
1618

1719
#include "mbed_assert.h"
1820
#include "analogin_api.h"
@@ -26,6 +28,11 @@
2628
#define ADC_12BIT_RANGE 0xFFF
2729
#define ADC_RANGE ADC_12BIT_RANGE
2830

31+
#ifdef TARGET_SDK13
32+
__STATIC_INLINE nrf_saadc_input_t nrf_drv_saadc_gpio_to_ain(uint32_t pin);
33+
#endif
34+
35+
2936
static void analog_in_event_handler(nrf_drv_saadc_evt_t const *p_event)// type of nrf_drv_saadc_event_handler_t
3037
{
3138
(void) p_event;
@@ -88,4 +95,36 @@ float analogin_read(analogin_t *obj)
8895
return (float)value * (1.0f / (float)ADC_RANGE);
8996
}
9097

98+
#ifdef TARGET_SDK13
99+
/**
100+
* @brief Function for converting a GPIO pin number to an analog input pin number used in the channel
101+
* configuration.
102+
*
103+
* @param[in] pin GPIO pin.
104+
*
105+
* @return Value representing an analog input pin. The function returns @ref NRF_SAADC_INPUT_DISABLED
106+
* if the specified pin is not an analog input.
107+
*/
108+
__STATIC_INLINE nrf_saadc_input_t nrf_drv_saadc_gpio_to_ain(uint32_t pin)
109+
{
110+
// AIN0 - AIN3
111+
if (pin >= 2 && pin <= 5)
112+
{
113+
//0 means "not connected", hence this "+ 1"
114+
return (nrf_saadc_input_t)(pin - 2 + 1);
115+
}
116+
// AIN4 - AIN7
117+
else if (pin >= 28 && pin <= 31)
118+
{
119+
return (nrf_saadc_input_t)(pin - 24 + 1);
120+
}
121+
else
122+
{
123+
return NRF_SAADC_INPUT_DISABLED;
124+
}
125+
}
126+
#endif // TARGET_SDK13
127+
91128
#endif // DEVICE_ANALOGIN
129+
130+
#endif // !TARGET_MCU_NRF51822

targets/TARGET_NORDIC/TARGET_NRF5/gpio_api.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222

2323
#if defined(TARGET_MCU_NRF51822)
2424
#define GPIO_PIN_COUNT 31
25-
#else
25+
#elif defined(TARGET_MCU_NRF52832)
2626
#define GPIO_PIN_COUNT 32
27+
#elif defined(TARGET_MCU_NRF52840)
28+
#define GPIO_PIN_COUNT 48
29+
#else
30+
#error not recognized gpio count for mcu
2731
#endif
2832

2933
typedef struct {
@@ -36,8 +40,14 @@ typedef struct {
3640
bool irq_rise : 1;
3741
} gpio_cfg_t;
3842

39-
uint32_t m_gpio_initialized;
40-
gpio_cfg_t m_gpio_cfg[GPIO_PIN_COUNT];
43+
#if GPIO_PIN_COUNT > 32
44+
typedef uint64_t gpio_mask_t;
45+
#else
46+
typedef uint32_t gpio_mask_t;
47+
#endif
48+
49+
static gpio_mask_t m_gpio_initialized;
50+
static gpio_cfg_t m_gpio_cfg[GPIO_PIN_COUNT];
4151

4252

4353
/***********
@@ -46,15 +56,15 @@ gpio_cfg_t m_gpio_cfg[GPIO_PIN_COUNT];
4656

4757
static gpio_irq_handler m_irq_handler;
4858
static uint32_t m_channel_ids[GPIO_PIN_COUNT] = {0};
49-
uint32_t m_gpio_irq_enabled;
59+
static uint32_t m_gpio_irq_enabled;
5060

5161

5262
static void gpiote_irq_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
5363
{
5464
nrf_gpio_pin_sense_t sense = nrf_gpio_pin_sense_get(pin);
5565
gpio_irq_event event = (sense == NRF_GPIO_PIN_SENSE_LOW) ? IRQ_RISE : IRQ_FALL;
5666

57-
if (m_gpio_irq_enabled & (1UL << pin)) {
67+
if (m_gpio_irq_enabled & ((gpio_mask_t)1 << pin)) {
5868
if (((event == IRQ_RISE) && m_gpio_cfg[pin].irq_rise)
5969
|| ((event == IRQ_FALL) && m_gpio_cfg[pin].irq_fall)) {
6070
m_irq_handler(m_channel_ids[pin], event);
@@ -79,20 +89,26 @@ void gpio_init(gpio_t *obj, PinName pin)
7989
m_gpio_cfg[obj->pin].used_as_gpio = true;
8090
}
8191

92+
#ifdef TARGET_SDK11
93+
__STATIC_INLINE uint32_t nrf_gpio_pin_out_read(uint32_t pin)
94+
{
95+
return (NRF_GPIO->OUTSET & (1UL << (pin)));
96+
}
97+
#endif
8298

8399
int gpio_read(gpio_t *obj)
84100
{
85101
MBED_ASSERT(obj->pin != (PinName)NC);
86102
if (m_gpio_cfg[obj->pin].direction == PIN_OUTPUT) {
87-
return ((NRF_GPIO->OUTSET & (1UL << obj->pin)) ? 1 : 0);
103+
return (nrf_gpio_pin_out_read(obj->pin) ? 1 : 0);
88104
} else {
89105
return nrf_gpio_pin_read(obj->pin);
90106
}
91107
}
92108

93109
static void gpiote_pin_uninit(uint8_t pin)
94110
{
95-
if (m_gpio_initialized & (1UL << pin)) {
111+
if (m_gpio_initialized & ((gpio_mask_t)1UL << pin)) {
96112
if ((m_gpio_cfg[pin].direction == PIN_OUTPUT) && (!m_gpio_cfg[pin].used_as_irq)) {
97113
nrf_drv_gpiote_out_uninit(pin);
98114
}
@@ -116,7 +132,7 @@ static void gpio_apply_config(uint8_t pin)
116132
if (m_gpio_cfg[pin].used_as_irq) {
117133
cfg.pull = NRF_GPIO_PIN_PULLUP;
118134
nrf_drv_gpiote_in_init(pin, &cfg, gpiote_irq_handler);
119-
if ((m_gpio_irq_enabled & (1 << pin))
135+
if ((m_gpio_irq_enabled & ((gpio_mask_t)1 << pin))
120136
&& (m_gpio_cfg[pin].irq_rise || m_gpio_cfg[pin].irq_fall))
121137
{
122138
nrf_drv_gpiote_in_event_enable(pin, true);
@@ -142,17 +158,17 @@ static void gpio_apply_config(uint8_t pin)
142158
nrf_drv_gpiote_out_config_t cfg = GPIOTE_CONFIG_OUT_SIMPLE(m_gpio_cfg[pin].init_high);
143159
nrf_drv_gpiote_out_init(pin, &cfg);
144160
}
145-
m_gpio_initialized |= (1UL << pin);
161+
m_gpio_initialized |= ((gpio_mask_t)1UL << pin);
146162
}
147163
else {
148-
m_gpio_initialized &= ~(1UL << pin);
164+
m_gpio_initialized &= ~((gpio_mask_t)1UL << pin);
149165
}
150166
}
151167

152168

153169
void gpio_mode(gpio_t *obj, PinMode mode)
154170
{
155-
MBED_ASSERT(obj->pin <= GPIO_PIN_COUNT);
171+
MBED_ASSERT(obj->pin != (PinName)NC);
156172

157173
gpiote_pin_uninit(obj->pin); // try to uninitialize gpio before a change.
158174

@@ -163,7 +179,7 @@ void gpio_mode(gpio_t *obj, PinMode mode)
163179

164180
void gpio_dir(gpio_t *obj, PinDirection direction)
165181
{
166-
MBED_ASSERT(obj->pin <= GPIO_PIN_COUNT);
182+
MBED_ASSERT(obj->pin != (PinName)NC);
167183

168184
gpiote_pin_uninit(obj->pin); // try to uninitialize gpio before a change.
169185

@@ -211,7 +227,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
211227
{
212228
gpio_cfg_t* cfg = &m_gpio_cfg[obj->ch];
213229
bool irq_enabled_before =
214-
(m_gpio_irq_enabled & (1 << obj->ch)) &&
230+
(m_gpio_irq_enabled & ((gpio_mask_t)1 << obj->ch)) &&
215231
(cfg->irq_rise || cfg->irq_fall);
216232

217233
if (event == IRQ_RISE) {
@@ -235,7 +251,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
235251

236252
void gpio_irq_enable(gpio_irq_t *obj)
237253
{
238-
m_gpio_irq_enabled |= (1 << obj->ch);
254+
m_gpio_irq_enabled |= ((gpio_mask_t)1 << obj->ch);
239255
if (m_gpio_cfg[obj->ch].irq_rise || m_gpio_cfg[obj->ch].irq_fall) {
240256
nrf_drv_gpiote_in_event_enable(obj->ch, true);
241257
}
@@ -244,6 +260,6 @@ void gpio_irq_enable(gpio_irq_t *obj)
244260

245261
void gpio_irq_disable(gpio_irq_t *obj)
246262
{
247-
m_gpio_irq_enabled &= ~(1 << obj->ch);
263+
m_gpio_irq_enabled &= ~((gpio_mask_t)1 << obj->ch);
248264
nrf_drv_gpiote_in_event_disable(obj->ch);
249265
}

targets/TARGET_NORDIC/TARGET_NRF5/i2c_api.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include "mbed_error.h"
4646
#include "nrf_twi.h"
4747
#include "nrf_drv_common.h"
48-
#include "nrf_drv_config.h"
48+
#include "sdk_config.h"
4949
#include "app_util_platform.h"
5050
#include "nrf_gpio.h"
5151
#include "nrf_delay.h"
@@ -62,6 +62,11 @@
6262
#endif
6363
#define TWI_INFO(obj) (&m_twi_info[TWI_IDX(obj)])
6464

65+
#ifdef TARGET_SDK13
66+
#define TWI0_INSTANCE_INDEX 0
67+
#define TWI1_INSTANCE_INDEX TWI0_INSTANCE_INDEX+TWI0_ENABLED
68+
#endif
69+
6570
typedef struct {
6671
bool initialized;
6772
uint32_t pselsda;
@@ -113,7 +118,7 @@ static const peripheral_handler_desc_t twi_handlers[TWI_COUNT] =
113118
};
114119
#ifdef NRF51
115120
#define TWI_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
116-
#elif defined(NRF52)
121+
#elif defined(NRF52) || defined(NRF52840_XXAA)
117122
#define TWI_IRQ_PRIORITY APP_IRQ_PRIORITY_LOWEST
118123
#endif
119124

targets/TARGET_NORDIC/TARGET_NRF5/objects.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ struct spi_s {
5757
};
5858

5959
struct port_s {
60-
__IO uint32_t *reg_cnf;
61-
__IO uint32_t *reg_out;
62-
__I uint32_t *reg_in;
6360
PortName port;
6461
uint32_t mask;
6562
};

targets/TARGET_NORDIC/TARGET_NRF5/pinmap.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mbed_assert.h"
1717
#include "mbed_error.h"
1818
#include "pinmap.h"
19+
#include "nrf_gpio.h"
1920

2021
void pin_function(PinName pin, int function)
2122
{
@@ -29,7 +30,12 @@ void pin_mode(PinName pin, PinMode mode)
2930
MBED_ASSERT(pin != (PinName)NC);
3031

3132
uint32_t pin_number = (uint32_t)pin;
32-
33-
NRF_GPIO->PIN_CNF[pin_number] &= ~GPIO_PIN_CNF_PULL_Msk;
34-
NRF_GPIO->PIN_CNF[pin_number] |= (mode << GPIO_PIN_CNF_PULL_Pos);
33+
34+
#ifdef TARGET_SDK11
35+
NRF_GPIO_Type * reg = NRF_GPIO;
36+
#else
37+
NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
38+
#endif
39+
reg->PIN_CNF[pin_number] &= ~GPIO_PIN_CNF_PULL_Msk;
40+
reg->PIN_CNF[pin_number] |= (mode << GPIO_PIN_CNF_PULL_Pos);
3541
}

0 commit comments

Comments
 (0)