1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  * Copyright (c) 2018 Ioannis Glaropoulos
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /* Use the NRF_RTC instance for coarse radio event scheduling */
9 #define NRF_RTC NRF_RTC0
10 
11 /* HAL abstraction of event timer prescaler value */
12 #define HAL_EVENT_TIMER_PRESCALER_VALUE 4U
13 
14 /* TXEN->TXIDLE + TXIDLE->TX in microseconds. */
15 #define HAL_RADIO_NRF51_TXEN_TXIDLE_TX_US 140
16 #define HAL_RADIO_NRF51_TXEN_TXIDLE_TX_NS 140000
17 /* RXEN->RXIDLE + RXIDLE->RX in microseconds. */
18 #define HAL_RADIO_NRF51_RXEN_RXIDLE_RX_US 138
19 #define HAL_RADIO_NRF51_RXEN_RXIDLE_RX_NS 138000
20 
21 #define HAL_RADIO_NRF51_TX_CHAIN_DELAY_US 1 /* ceil(1.0) */
22 #define HAL_RADIO_NRF51_TX_CHAIN_DELAY_NS 1000 /* 1.0 */
23 #define HAL_RADIO_NRF51_RX_CHAIN_DELAY_US 3 /* ceil(3.0) */
24 #define HAL_RADIO_NRF51_RX_CHAIN_DELAY_NS 3000 /* 3.0 */
25 
26 /* HAL abstraction of Radio bitfields */
27 #define HAL_NRF_RADIO_EVENT_END              NRF_RADIO_EVENT_END
28 #define HAL_RADIO_EVENTS_END                 EVENTS_END
29 #define HAL_RADIO_INTENSET_DISABLED_Msk      RADIO_INTENSET_DISABLED_Msk
30 #define HAL_RADIO_SHORTS_TRX_END_DISABLE_Msk RADIO_SHORTS_END_DISABLE_Msk
31 
32 /* HAL abstraction of Radio IRQ number */
33 #define HAL_RADIO_IRQn                          RADIO_IRQn
34 
hal_radio_reset(void)35 static inline void hal_radio_reset(void)
36 {
37 	/* TODO: Add any required setup for each radio event
38 	 */
39 }
40 
hal_radio_stop(void)41 static inline void hal_radio_stop(void)
42 {
43 	/* TODO: Add any required cleanup of actions taken in hal_radio_reset()
44 	 */
45 }
46 
hal_radio_ram_prio_setup(void)47 static inline void hal_radio_ram_prio_setup(void)
48 {
49 }
50 
hal_radio_phy_mode_get(uint8_t phy,uint8_t flags)51 static inline uint32_t hal_radio_phy_mode_get(uint8_t phy, uint8_t flags)
52 {
53 	ARG_UNUSED(flags);
54 	uint32_t mode;
55 
56 	switch (phy) {
57 	case BIT(0):
58 	default:
59 		mode = RADIO_MODE_MODE_Ble_1Mbit;
60 		break;
61 
62 	case BIT(1):
63 		mode = RADIO_MODE_MODE_Nrf_2Mbit;
64 		break;
65 	}
66 
67 	return mode;
68 }
69 
hal_radio_tx_power_min_get(void)70 static inline uint32_t hal_radio_tx_power_min_get(void)
71 {
72 	return RADIO_TXPOWER_TXPOWER_Neg30dBm;
73 }
74 
hal_radio_tx_power_max_get(void)75 static inline uint32_t hal_radio_tx_power_max_get(void)
76 {
77 	return RADIO_TXPOWER_TXPOWER_Pos4dBm;
78 }
79 
hal_radio_tx_power_floor(int8_t tx_power_lvl)80 static inline uint32_t hal_radio_tx_power_floor(int8_t tx_power_lvl)
81 {
82 	if (tx_power_lvl >= (int8_t)RADIO_TXPOWER_TXPOWER_Pos4dBm) {
83 		return RADIO_TXPOWER_TXPOWER_Pos4dBm;
84 	}
85 
86 	if (tx_power_lvl >= (int8_t)RADIO_TXPOWER_TXPOWER_0dBm) {
87 		return RADIO_TXPOWER_TXPOWER_0dBm;
88 	}
89 
90 	if (tx_power_lvl >= (int8_t)RADIO_TXPOWER_TXPOWER_Neg4dBm) {
91 		return RADIO_TXPOWER_TXPOWER_Neg4dBm;
92 	}
93 
94 	if (tx_power_lvl >= (int8_t)RADIO_TXPOWER_TXPOWER_Neg8dBm) {
95 		return RADIO_TXPOWER_TXPOWER_Neg8dBm;
96 	}
97 
98 	if (tx_power_lvl >= (int8_t)RADIO_TXPOWER_TXPOWER_Neg12dBm) {
99 		return RADIO_TXPOWER_TXPOWER_Neg12dBm;
100 	}
101 
102 	if (tx_power_lvl >= (int8_t)RADIO_TXPOWER_TXPOWER_Neg16dBm) {
103 		return RADIO_TXPOWER_TXPOWER_Neg16dBm;
104 	}
105 
106 	if (tx_power_lvl >= (int8_t)RADIO_TXPOWER_TXPOWER_Neg20dBm) {
107 		return RADIO_TXPOWER_TXPOWER_Neg20dBm;
108 	}
109 
110 	/* Note: The -30 dBm power level is deprecated so ignore it! */
111 	return RADIO_TXPOWER_TXPOWER_Neg40dBm;
112 }
113 
hal_radio_tx_ready_delay_us_get(uint8_t phy,uint8_t flags)114 static inline uint32_t hal_radio_tx_ready_delay_us_get(uint8_t phy, uint8_t flags)
115 {
116 	ARG_UNUSED(phy);
117 	ARG_UNUSED(flags);
118 	return HAL_RADIO_NRF51_TXEN_TXIDLE_TX_US;
119 }
120 
hal_radio_rx_ready_delay_us_get(uint8_t phy,uint8_t flags)121 static inline uint32_t hal_radio_rx_ready_delay_us_get(uint8_t phy, uint8_t flags)
122 {
123 	ARG_UNUSED(phy);
124 	ARG_UNUSED(flags);
125 	return HAL_RADIO_NRF51_RXEN_RXIDLE_RX_US;
126 }
127 
hal_radio_tx_chain_delay_us_get(uint8_t phy,uint8_t flags)128 static inline uint32_t hal_radio_tx_chain_delay_us_get(uint8_t phy, uint8_t flags)
129 {
130 	ARG_UNUSED(phy);
131 	ARG_UNUSED(flags);
132 	return HAL_RADIO_NRF51_TX_CHAIN_DELAY_US;
133 }
134 
hal_radio_rx_chain_delay_us_get(uint8_t phy,uint8_t flags)135 static inline uint32_t hal_radio_rx_chain_delay_us_get(uint8_t phy, uint8_t flags)
136 {
137 	ARG_UNUSED(phy);
138 	ARG_UNUSED(flags);
139 	return HAL_RADIO_NRF51_RX_CHAIN_DELAY_US;
140 }
141 
hal_radio_tx_ready_delay_ns_get(uint8_t phy,uint8_t flags)142 static inline uint32_t hal_radio_tx_ready_delay_ns_get(uint8_t phy, uint8_t flags)
143 {
144 	ARG_UNUSED(phy);
145 	ARG_UNUSED(flags);
146 	return HAL_RADIO_NRF51_TXEN_TXIDLE_TX_NS;
147 }
148 
hal_radio_rx_ready_delay_ns_get(uint8_t phy,uint8_t flags)149 static inline uint32_t hal_radio_rx_ready_delay_ns_get(uint8_t phy, uint8_t flags)
150 {
151 	ARG_UNUSED(phy);
152 	ARG_UNUSED(flags);
153 	return HAL_RADIO_NRF51_RXEN_RXIDLE_RX_NS;
154 }
155 
hal_radio_tx_chain_delay_ns_get(uint8_t phy,uint8_t flags)156 static inline uint32_t hal_radio_tx_chain_delay_ns_get(uint8_t phy, uint8_t flags)
157 {
158 	ARG_UNUSED(phy);
159 	ARG_UNUSED(flags);
160 	return HAL_RADIO_NRF51_TX_CHAIN_DELAY_NS;
161 }
162 
hal_radio_rx_chain_delay_ns_get(uint8_t phy,uint8_t flags)163 static inline uint32_t hal_radio_rx_chain_delay_ns_get(uint8_t phy, uint8_t flags)
164 {
165 	ARG_UNUSED(phy);
166 	ARG_UNUSED(flags);
167 	return HAL_RADIO_NRF51_RX_CHAIN_DELAY_NS;
168 }
169