Skip to content

Commit 9e377f6

Browse files
lrusinowiczCruz Monrreal II
authored andcommitted
Added BLE support based on CORDIO stack.
1 parent 24fcd69 commit 9e377f6

File tree

4 files changed

+1975
-0
lines changed

4 files changed

+1975
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2017-2017 ARM Limited
4+
* Copyright (c) 2017-2018 Future Electronics
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "hci_api.h"
20+
#include "bstream.h"
21+
#include "driver/CordioHCIDriver.h"
22+
#include "drivers/IPCPipeTransportDriver.h"
23+
#include "psoc6_utils.h"
24+
25+
using namespace ble::vendor::cordio;
26+
using namespace ble::vendor::cypress;
27+
28+
const uint16_t HCI_VEND_SET_BD_ADDR = 0xfda0;
29+
const uint8_t HCI_VEND_SET_BD_ADDR_LEN = 7; /* MAC address + address type */
30+
31+
class Psoc6HCIDriver : public CordioHCIDriver
32+
{
33+
public:
34+
Psoc6HCIDriver(IPCPipeTransportDriver& transport_driver) :
35+
CordioHCIDriver(transport_driver)
36+
{
37+
}
38+
39+
40+
private:
41+
42+
struct BdAddress {
43+
uint8_t mac_address[6];
44+
uint8_t addr_type;
45+
46+
BdAddress() : addr_type(0) {}
47+
};
48+
49+
/**
50+
* Initialize the chip.
51+
* The transport is up at that time.
52+
*/
53+
virtual void do_initialize();
54+
55+
/**
56+
* Terminate the driver
57+
*/
58+
virtual void do_terminate() {}
59+
60+
virtual void handle_reset_sequence(uint8_t *pMsg);
61+
62+
private:
63+
BdAddress bd_address;
64+
};
65+
66+
67+
void Psoc6HCIDriver::do_initialize()
68+
{
69+
cy_get_bd_mac_address(bd_address.mac_address);
70+
}
71+
72+
73+
void Psoc6HCIDriver::handle_reset_sequence(uint8_t *pMsg) {
74+
75+
uint16_t opcode;
76+
77+
/* if event is a command complete event */
78+
if (*pMsg == HCI_CMD_CMPL_EVT) {
79+
/* parse parameters */
80+
uint8_t *pMsg2 = pMsg + HCI_EVT_HDR_LEN;
81+
pMsg2++; /* skip num packets */
82+
BSTREAM_TO_UINT16(opcode, pMsg2);
83+
pMsg2 -= 2;
84+
/* decode opcode */
85+
switch (opcode) {
86+
case HCI_OPCODE_RESET:
87+
/* send next command in sequence */
88+
HciVendorSpecificCmd(HCI_VEND_SET_BD_ADDR,
89+
HCI_VEND_SET_BD_ADDR_LEN,
90+
reinterpret_cast<uint8_t*>(&bd_address));
91+
break;
92+
93+
case HCI_VEND_SET_BD_ADDR:
94+
/* pretend we have just completed reset */
95+
UINT16_TO_BSTREAM(pMsg2, HCI_OPCODE_RESET);
96+
CordioHCIDriver::handle_reset_sequence(pMsg);
97+
break;
98+
99+
default:
100+
/* pass to parent */
101+
CordioHCIDriver::handle_reset_sequence(pMsg);
102+
}
103+
}
104+
}
105+
106+
107+
CordioHCIDriver& ble_cordio_get_hci_driver() {
108+
static IPCPipeTransportDriver transport_driver;
109+
110+
static Psoc6HCIDriver hci_driver(
111+
transport_driver /* other hci driver parameters */
112+
);
113+
114+
return hci_driver;
115+
}
116+
117+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2017-2017 ARM Limited
4+
* Copyright (c) 2017-2018 Future Electronics
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "IPCPipeTransportDriver.h"
20+
#include "ipcpipe_transport.h"
21+
#include "mbed_assert.h"
22+
#include "mbed_error.h"
23+
24+
namespace ble {
25+
namespace vendor {
26+
namespace cypress {
27+
28+
29+
void dump_buffer(uint8_t *buffer, uint32_t len)
30+
{
31+
32+
while (len > 0) {
33+
printf(" %02x", *buffer++);
34+
--len;
35+
}
36+
printf("\n");
37+
}
38+
39+
void ipc_h4_receive(uint32_t *ptr)
40+
{
41+
IpcPipeMessage *message = (IpcPipeMessage *)ptr;
42+
43+
// We don't expect header to be received from M0+ core.
44+
MBED_ASSERT(message->header_length == 0);
45+
46+
// printf("BLE received: ");
47+
// h4_dump_buffer(buffer->message.data, buffer->message.length);
48+
cordio::CordioHCITransportDriver::on_data_received(message->data, message->data_length);
49+
}
50+
51+
IPCPipeTransportDriver::IPCPipeTransportDriver()
52+
{ }
53+
54+
void IPCPipeTransportDriver::initialize()
55+
{
56+
// printf("H4 Transport driver initialization.\n");
57+
ipcpipe_transport_start(IPCPIPE_CLIENT_H4, ipc_h4_receive, NULL);
58+
}
59+
60+
void IPCPipeTransportDriver::terminate()
61+
{
62+
ipcpipe_transport_stop(IPCPIPE_CLIENT_H4);
63+
}
64+
65+
uint16_t IPCPipeTransportDriver::write(uint8_t type, uint16_t len, uint8_t *pData)
66+
{
67+
// printf("BLE sending T<%02x>:", type);
68+
// dump_buffer(pData, len);
69+
ipcpipe_write_data(IPCPIPE_CLIENT_H4, &type, 1, pData, len);
70+
return len;
71+
}
72+
73+
} // namespace cypress
74+
} // namespace vendor
75+
} // namespace ble
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2017-2017 ARM Limited
4+
* Copyright (c) 2017-2018 Future Electronics
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef PSOC6_IPCPIPE_TRANSPORT_DRIVER_H_
20+
#define PSOC6_IPCPIPE_TRANSPORT_DRIVER_H_
21+
22+
#include <stdint.h>
23+
#include "mbed.h"
24+
#include "CordioHCITransportDriver.h"
25+
26+
27+
namespace ble {
28+
namespace vendor {
29+
namespace cypress {
30+
31+
using namespace ble::vendor;
32+
33+
/**
34+
* Implementation of the H4 driver over PSoC6 IPC pipe.
35+
*/
36+
class IPCPipeTransportDriver : public cordio::CordioHCITransportDriver {
37+
public:
38+
/**
39+
* Initialize the transport driver.
40+
*
41+
*/
42+
IPCPipeTransportDriver();
43+
44+
/**
45+
* Destructor
46+
*/
47+
virtual ~IPCPipeTransportDriver() { }
48+
49+
/**
50+
* @see CordioHCITransportDriver::initialize
51+
*/
52+
virtual void initialize();
53+
54+
/**
55+
* @see CordioHCITransportDriver::terminate
56+
*/
57+
virtual void terminate();
58+
59+
/**
60+
* @see CordioHCITransportDriver::write
61+
*/
62+
virtual uint16_t write(uint8_t type, uint16_t len, uint8_t *pData);
63+
64+
private:
65+
66+
};
67+
68+
} // namespace cypress
69+
} // namespace vendor
70+
} // namespace ble
71+
72+
#endif /* PSOC6_IPCPIPE_TRANSPORT_DRIVER_H_ */

0 commit comments

Comments
 (0)