Skip to content

Commit 8333932

Browse files
committed
PortentaH7: implement low power on BLE
1 parent 668cf60 commit 8333932

File tree

5 files changed

+650
-80
lines changed

5 files changed

+650
-80
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-2017 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#if DEVICE_SERIAL && DEVICE_SERIAL_FC
19+
20+
#include "CyH4TransportDriver.h"
21+
22+
namespace ble {
23+
namespace vendor {
24+
namespace cypress_ble {
25+
26+
CyH4TransportDriver::CyH4TransportDriver(PinName tx, PinName rx, PinName cts, PinName rts, int baud, PinName bt_host_wake_name, PinName bt_device_wake_name, uint8_t host_wake_irq, uint8_t dev_wake_irq) :
27+
uart(tx, rx), cts(cts), rts(rts),
28+
bt_host_wake_name(bt_host_wake_name),
29+
bt_device_wake_name(bt_device_wake_name),
30+
bt_host_wake(bt_host_wake_name, PIN_INPUT, PullNone, 0),
31+
bt_device_wake(bt_device_wake_name, PIN_OUTPUT, PullNone, 1),
32+
host_wake_irq_event(host_wake_irq),
33+
dev_wake_irq_event(dev_wake_irq)
34+
{
35+
enabled_powersave = true;
36+
bt_host_wake_active = false;
37+
}
38+
39+
CyH4TransportDriver::CyH4TransportDriver(PinName tx, PinName rx, PinName cts, PinName rts, int baud) :
40+
uart(tx, rx), cts(cts),
41+
rts(rts),
42+
bt_host_wake_name(NC),
43+
bt_device_wake_name(NC),
44+
bt_host_wake(bt_host_wake_name),
45+
bt_device_wake(bt_device_wake_name)
46+
{
47+
enabled_powersave = false;
48+
bt_host_wake_active = false;
49+
sleep_manager_lock_deep_sleep();
50+
holding_deep_sleep_lock = true;
51+
}
52+
53+
CyH4TransportDriver::~CyH4TransportDriver()
54+
{
55+
if (holding_deep_sleep_lock)
56+
{
57+
sleep_manager_unlock_deep_sleep();
58+
holding_deep_sleep_lock = false;
59+
}
60+
}
61+
62+
void CyH4TransportDriver::bt_host_wake_rise_irq_handler(void)
63+
{
64+
if (host_wake_irq_event == WAKE_EVENT_ACTIVE_LOW) {
65+
if(bt_host_wake_active == true)
66+
{
67+
/* lock PSoC 6 DeepSleep entry as long as host_wake is asserted */
68+
sleep_manager_unlock_deep_sleep();
69+
bt_host_wake_active = false;
70+
}
71+
} else {
72+
/* lock PSoC 6 DeepSleep entry as long as host_wake is asserted */
73+
sleep_manager_lock_deep_sleep();
74+
bt_host_wake_active = true;
75+
}
76+
}
77+
78+
void CyH4TransportDriver::bt_host_wake_fall_irq_handler(void)
79+
{
80+
if (host_wake_irq_event == WAKE_EVENT_ACTIVE_LOW) {
81+
/* lock PSoC 6 DeepSleep entry as long as host_wake is asserted */
82+
sleep_manager_lock_deep_sleep();
83+
bt_host_wake_active = true;
84+
} else {
85+
if(bt_host_wake_active == true)
86+
{
87+
/* lock PSoC 6 DeepSleep entry as long as host_wake is asserted */
88+
sleep_manager_unlock_deep_sleep();
89+
bt_host_wake_active = false;
90+
}
91+
}
92+
}
93+
94+
void CyH4TransportDriver::on_controller_irq()
95+
{
96+
sleep_manager_lock_deep_sleep();
97+
98+
while (uart.readable()) {
99+
uint8_t char_received;
100+
if (uart.read(&char_received, 1)) {
101+
CordioHCITransportDriver::on_data_received(&char_received, 1);
102+
}
103+
}
104+
105+
sleep_manager_unlock_deep_sleep();
106+
}
107+
108+
void CyH4TransportDriver::initialize()
109+
{
110+
#if (defined(MBED_TICKLESS) && DEVICE_SLEEP && DEVICE_LPTICKER)
111+
InterruptIn *host_wake_pin;
112+
#endif
113+
114+
sleep_manager_lock_deep_sleep();
115+
116+
uart.format(
117+
/* bits */ 8,
118+
/* parity */ SerialBase::None,
119+
/* stop bit */ 1
120+
);
121+
122+
uart.set_flow_control(
123+
/* flow */ SerialBase::RTSCTS,
124+
/* rts */ rts,
125+
/* cts */ cts
126+
);
127+
128+
uart.attach(
129+
callback(this, &CyH4TransportDriver::on_controller_irq),
130+
SerialBase::RxIrq
131+
);
132+
133+
#if (defined(MBED_TICKLESS) && DEVICE_SLEEP && DEVICE_LPTICKER)
134+
if (bt_host_wake_name != NC) {
135+
//Register IRQ for Host WAKE
136+
host_wake_pin = new InterruptIn(bt_host_wake_name);
137+
host_wake_pin->fall(callback(this, &CyH4TransportDriver::bt_host_wake_fall_irq_handler));
138+
host_wake_pin->rise(callback(this, &CyH4TransportDriver::bt_host_wake_rise_irq_handler));
139+
}
140+
#endif
141+
if (dev_wake_irq_event == WAKE_EVENT_ACTIVE_LOW) {
142+
if (bt_device_wake_name != NC)
143+
bt_device_wake = WAKE_EVENT_ACTIVE_LOW;
144+
} else {
145+
if (bt_device_wake_name != NC)
146+
bt_device_wake = WAKE_EVENT_ACTIVE_HIGH;
147+
}
148+
sleep_manager_unlock_deep_sleep();
149+
rtos::ThisThread::sleep_for(500);
150+
}
151+
152+
void CyH4TransportDriver::terminate() { }
153+
154+
uint16_t CyH4TransportDriver::write(uint8_t type, uint16_t len, uint8_t *pData)
155+
{
156+
uint16_t i = 0;
157+
158+
sleep_manager_lock_deep_sleep();
159+
assert_bt_dev_wake();
160+
161+
while (i < len + 1) {
162+
uint8_t to_write = i == 0 ? type : pData[i - 1];
163+
while (uart.writeable() == 0);
164+
uart.write(&to_write, 1);
165+
++i;
166+
}
167+
168+
deassert_bt_dev_wake();
169+
sleep_manager_unlock_deep_sleep();
170+
return len;
171+
}
172+
173+
void CyH4TransportDriver::assert_bt_dev_wake()
174+
{
175+
#if (defined(MBED_TICKLESS) && DEVICE_SLEEP && DEVICE_LPTICKER)
176+
if (enabled_powersave) {
177+
if (dev_wake_irq_event == WAKE_EVENT_ACTIVE_LOW) {
178+
bt_device_wake = WAKE_EVENT_ACTIVE_LOW;
179+
} else {
180+
bt_device_wake = WAKE_EVENT_ACTIVE_HIGH;
181+
}
182+
}
183+
#endif
184+
}
185+
186+
void CyH4TransportDriver::deassert_bt_dev_wake()
187+
{
188+
#if (defined(MBED_TICKLESS) && DEVICE_SLEEP && DEVICE_LPTICKER)
189+
if (enabled_powersave) {
190+
if (dev_wake_irq_event == WAKE_EVENT_ACTIVE_LOW) {
191+
bt_device_wake = WAKE_EVENT_ACTIVE_HIGH;
192+
} else {
193+
bt_device_wake = WAKE_EVENT_ACTIVE_LOW;
194+
}
195+
}
196+
#endif
197+
}
198+
199+
200+
void CyH4TransportDriver::update_uart_baud_rate(int baud)
201+
{
202+
uart.baud((uint32_t)baud);
203+
}
204+
205+
bool CyH4TransportDriver::get_enabled_powersave()
206+
{
207+
return (enabled_powersave);
208+
}
209+
210+
uint8_t CyH4TransportDriver::get_host_wake_irq_event()
211+
{
212+
return (host_wake_irq_event);
213+
}
214+
215+
uint8_t CyH4TransportDriver::get_dev_wake_irq_event()
216+
{
217+
return (dev_wake_irq_event);
218+
}
219+
220+
221+
} // namespace cypress_ble
222+
} // namespace vendor
223+
} // namespace ble
224+
225+
ble::vendor::cypress_ble::CyH4TransportDriver& ble_cordio_get_default_h4_transport_driver()
226+
{
227+
#if (defined(CYBSP_BT_HOST_WAKE) && defined(CYBSP_BT_DEVICE_WAKE))
228+
static ble::vendor::cypress_ble::CyH4TransportDriver s_transport_driver(
229+
/* TX */ CYBSP_BT_UART_TX, /* RX */ CYBSP_BT_UART_RX,
230+
/* cts */ CYBSP_BT_UART_CTS, /* rts */ CYBSP_BT_UART_RTS, DEF_BT_BAUD_RATE,
231+
CYBSP_BT_HOST_WAKE, CYBSP_BT_DEVICE_WAKE
232+
);
233+
234+
#else
235+
static ble::vendor::cypress_ble::CyH4TransportDriver s_transport_driver(
236+
/* TX */ CYBSP_BT_UART_TX, /* RX */ CYBSP_BT_UART_RX,
237+
/* cts */ CYBSP_BT_UART_CTS, /* rts */ CYBSP_BT_UART_RTS, DEF_BT_BAUD_RATE);
238+
#endif
239+
return s_transport_driver;
240+
}
241+
242+
MBED_WEAK
243+
ble::vendor::cypress_ble::CyH4TransportDriver& ble_cordio_get_h4_transport_driver()
244+
{
245+
return (ble_cordio_get_default_h4_transport_driver());
246+
}
247+
248+
#endif
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-2017 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef CY_H4TRANSPORT_DRIVER_H_
19+
#define CY_H4TRANSPORT_DRIVER_H_
20+
21+
#if (DEVICE_SERIAL && DEVICE_SERIAL_FC) || defined(DOXYGEN_ONLY)
22+
23+
#include <stdint.h>
24+
#include "mbed.h"
25+
#include "CordioHCITransportDriver.h"
26+
#include "drivers/DigitalInOut.h"
27+
28+
namespace ble {
29+
namespace vendor {
30+
namespace cypress_ble {
31+
32+
using namespace ble::vendor;
33+
34+
/**
35+
* Implementation of the H4 driver over Cypress based chips.
36+
*/
37+
class CyH4TransportDriver : public cordio::CordioHCITransportDriver {
38+
public:
39+
/**
40+
* Initialize the transport driver.
41+
*
42+
*/
43+
CyH4TransportDriver(PinName tx, PinName rx, PinName cts, PinName rts, int baud, PinName bt_host_wake_name, PinName bt_device_wake_name,
44+
uint8_t host_wake_irq = 0, uint8_t dev_wake_irq = 0);
45+
CyH4TransportDriver(PinName tx, PinName rx, PinName cts, PinName rts, int baud);
46+
47+
/**
48+
* Destructor
49+
*/
50+
virtual ~CyH4TransportDriver();
51+
52+
/**
53+
* @see CordioHCITransportDriver::initialize
54+
*/
55+
virtual void initialize();
56+
57+
/**
58+
* @see CordioHCITransportDriver::terminate
59+
*/
60+
virtual void terminate();
61+
62+
/**
63+
* @see CordioHCITransportDriver::write
64+
*/
65+
virtual uint16_t write(uint8_t type, uint16_t len, uint8_t *pData);
66+
67+
void bt_host_wake_rise_irq_handler();
68+
void bt_host_wake_fall_irq_handler();
69+
70+
#if (defined(MBED_TICKLESS) && DEVICE_SLEEP && DEVICE_LPTICKER)
71+
void on_host_stack_inactivity();
72+
#endif
73+
74+
void update_uart_baud_rate(int baud);
75+
76+
bool get_enabled_powersave();
77+
uint8_t get_host_wake_irq_event();
78+
uint8_t get_dev_wake_irq_event();
79+
80+
private:
81+
void assert_bt_dev_wake();
82+
void deassert_bt_dev_wake();
83+
void on_controller_irq();
84+
85+
// Use HAL serial because Cypress UART is buffered.
86+
// The PUTC function does not actually blocks until data is fully transmitted,
87+
// it only blocks until data gets into HW buffer.
88+
// The UART APIs prevents sleep while there are data in the HW buffer.
89+
// However UART APIs does not prevent the BT radio from going to sleep.
90+
// Use the HAL APIs to prevent the radio from going to sleep until UART transmition is complete.
91+
// Mbed layer has no API that distinguish between data in HW buffer v.s. data already transmitted.
92+
93+
UnbufferedSerial uart;
94+
PinName cts;
95+
PinName rts;
96+
PinName bt_host_wake_name;
97+
PinName bt_device_wake_name;
98+
99+
DigitalInOut bt_host_wake;
100+
DigitalInOut bt_device_wake;
101+
bool bt_host_wake_active;
102+
103+
bool enabled_powersave;
104+
uint8_t host_wake_irq_event;
105+
uint8_t dev_wake_irq_event;
106+
107+
bool holding_deep_sleep_lock;
108+
109+
};
110+
111+
} // namespace cypress
112+
} // namespace vendor
113+
} // namespace ble
114+
115+
#define DEF_BT_BAUD_RATE (115200)
116+
#define DEF_BT_3M_BAUD_RATE (3000000) /* Both Host and BT device have to be adapt to this */
117+
118+
#define WAKE_EVENT_ACTIVE_HIGH ( 1 ) /* Interrupt Rising Edge */
119+
#define WAKE_EVENT_ACTIVE_LOW ( 0 ) /* Interrupt Falling Edge */
120+
121+
ble::vendor::cypress_ble::CyH4TransportDriver& ble_cordio_get_default_h4_transport_driver();
122+
ble::vendor::cypress_ble::CyH4TransportDriver& ble_cordio_get_h4_transport_driver();
123+
#endif
124+
#endif /* CY_H4TRANSPORT_DRIVER_H_ */

0 commit comments

Comments
 (0)