Skip to content

Commit 38b3ed9

Browse files
facchinmpennam
authored andcommitted
Portenta: WHD: port to STM32H7 SDIO interface
1 parent e39f57b commit 38b3ed9

File tree

18 files changed

+5331
-0
lines changed

18 files changed

+5331
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/***********************************************************************************************//**
2+
* \file cy_network_buffer.c
3+
*
4+
* \brief
5+
* Basic set of APIs for dealing with network packet buffers. This is used by WHD
6+
* for relaying data between the network stack and the connectivity chip.
7+
*
8+
***************************************************************************************************
9+
* \copyright
10+
* Copyright 2018-2020 Cypress Semiconductor Corporation
11+
* SPDX-License-Identifier: Apache-2.0
12+
*
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
*
17+
* http://www.apache.org/licenses/LICENSE-2.0
18+
*
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
**************************************************************************************************/
25+
26+
#include <stdlib.h>
27+
#include "cy_network_buffer.h"
28+
#include "cy_utils.h"
29+
#include "lwip/pbuf.h"
30+
#include "lwip/memp.h"
31+
32+
#define SDIO_BLOCK_SIZE (64U)
33+
34+
//--------------------------------------------------------------------------------------------------
35+
// cy_host_buffer_get
36+
//--------------------------------------------------------------------------------------------------
37+
whd_result_t cy_host_buffer_get(whd_buffer_t* buffer, whd_buffer_dir_t direction,
38+
unsigned short size, unsigned long timeout_ms)
39+
{
40+
CY_UNUSED_PARAMETER(direction);
41+
CY_UNUSED_PARAMETER(timeout_ms);
42+
struct pbuf* p = NULL;
43+
if ((direction == WHD_NETWORK_TX) && (size <= PBUF_POOL_BUFSIZE))
44+
{
45+
p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL);
46+
}
47+
else
48+
{
49+
p = pbuf_alloc(PBUF_RAW, size+SDIO_BLOCK_SIZE, PBUF_RAM);
50+
if (p != NULL)
51+
{
52+
p->len = size;
53+
p->tot_len -= SDIO_BLOCK_SIZE;
54+
}
55+
}
56+
if (p != NULL)
57+
{
58+
*buffer = p;
59+
return WHD_SUCCESS;
60+
}
61+
else
62+
{
63+
return WHD_BUFFER_ALLOC_FAIL;
64+
}
65+
}
66+
67+
68+
//--------------------------------------------------------------------------------------------------
69+
// cy_buffer_release
70+
//--------------------------------------------------------------------------------------------------
71+
void cy_buffer_release(whd_buffer_t buffer, whd_buffer_dir_t direction)
72+
{
73+
CY_UNUSED_PARAMETER(direction);
74+
(void)pbuf_free((struct pbuf*)buffer);
75+
}
76+
77+
78+
//--------------------------------------------------------------------------------------------------
79+
// cy_buffer_get_current_piece_data_pointer
80+
//--------------------------------------------------------------------------------------------------
81+
uint8_t* cy_buffer_get_current_piece_data_pointer(whd_buffer_t buffer)
82+
{
83+
CY_ASSERT(buffer != NULL);
84+
struct pbuf* pbuffer= (struct pbuf*)buffer;
85+
return (uint8_t*)pbuffer->payload;
86+
}
87+
88+
89+
//--------------------------------------------------------------------------------------------------
90+
// cy_buffer_get_current_piece_size
91+
//--------------------------------------------------------------------------------------------------
92+
uint16_t cy_buffer_get_current_piece_size(whd_buffer_t buffer)
93+
{
94+
CY_ASSERT(buffer != NULL);
95+
struct pbuf* pbuffer = (struct pbuf*)buffer;
96+
return (uint16_t)pbuffer->len;
97+
}
98+
99+
100+
//--------------------------------------------------------------------------------------------------
101+
// cy_buffer_set_size
102+
//--------------------------------------------------------------------------------------------------
103+
whd_result_t cy_buffer_set_size(whd_buffer_t buffer, unsigned short size)
104+
{
105+
CY_ASSERT(buffer != NULL);
106+
struct pbuf* pbuffer = (struct pbuf*)buffer;
107+
108+
if (size > ((unsigned short)WHD_LINK_MTU +
109+
LWIP_MEM_ALIGN_SIZE(LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))) +
110+
LWIP_MEM_ALIGN_SIZE(size)))
111+
{
112+
return WHD_PMK_WRONG_LENGTH;
113+
}
114+
115+
pbuffer->tot_len = size;
116+
pbuffer->len = size;
117+
118+
return CY_RSLT_SUCCESS;
119+
}
120+
121+
122+
//--------------------------------------------------------------------------------------------------
123+
// cy_buffer_add_remove_at_front
124+
//--------------------------------------------------------------------------------------------------
125+
whd_result_t cy_buffer_add_remove_at_front(whd_buffer_t* buffer, int32_t add_remove_amount)
126+
{
127+
CY_ASSERT(buffer != NULL);
128+
struct pbuf** pbuffer = (struct pbuf**)buffer;
129+
130+
if ((u8_t)0 != pbuf_header(*pbuffer, (s16_t)(-add_remove_amount)))
131+
{
132+
return WHD_PMK_WRONG_LENGTH;
133+
}
134+
135+
return WHD_SUCCESS;
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/***********************************************************************************************//**
2+
* \file cy_network_buffer.h
3+
*
4+
* \brief
5+
* Basic set of APIs for dealing with network packet buffers. This is used by WHD
6+
* for relaying data between the network stack and the connectivity chip.
7+
*
8+
***************************************************************************************************
9+
* \copyright
10+
* Copyright 2018-2020 Cypress Semiconductor Corporation
11+
* SPDX-License-Identifier: Apache-2.0
12+
*
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
*
17+
* http://www.apache.org/licenses/LICENSE-2.0
18+
*
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
**************************************************************************************************/
25+
26+
/**
27+
* \addtogroup group_bsp_network_buffer Buffer management
28+
* \{
29+
* Basic set of APIs for dealing with network packet buffers
30+
*/
31+
32+
#pragma once
33+
34+
#include <stdint.h>
35+
#include <stdbool.h>
36+
#include "cy_result.h"
37+
#include "whd.h"
38+
#include "whd_network_types.h"
39+
40+
#if defined(__cplusplus)
41+
extern "C" {
42+
#endif
43+
44+
45+
/** Allocates a packet buffer
46+
*
47+
* Attempts to allocate a packet buffer of the size requested. It can do this
48+
* by allocating a pre-existing packet from a pool, using a static buffer,
49+
* or by dynamically allocating memory. The method of allocation does not
50+
* concern WHD, however it must match the way the network stack expects packet
51+
* buffers to be allocated. Usually WHD requires packet of size of WHD_LINK_MTU
52+
* which includes the MTU. Refer to whd_types.h to find the size of WHD_LINK_MTU.
53+
*
54+
* @param buffer : A pointer which receives the allocated packet buffer handle
55+
* @param direction : Indicates transmit/receive direction that the packet buffer is
56+
* used for. This may be needed if tx/rx pools are separate.
57+
* @param size : The number of bytes to allocate.
58+
* @param timeout_ms: Time to wait for a packet buffer to be available
59+
*
60+
* @return : CY_RSLT_SUCCESS or WHD_BUFFER_ALLOC_FAIL if the buffer could not be allocated
61+
*/
62+
whd_result_t cy_host_buffer_get(whd_buffer_t* buffer, whd_buffer_dir_t direction,
63+
unsigned short size, unsigned long timeout_ms);
64+
65+
/** Releases a packet buffer
66+
*
67+
* This function is used by WHD to indicate that it no longer requires
68+
* a packet buffer. The buffer can then be released back into a pool for
69+
* reuse, or the dynamically allocated memory can be freed, according to
70+
* how the packet was allocated.
71+
* Returns void since WHD cannot do anything about failures
72+
*
73+
* @param buffer : The handle of the packet buffer to be released
74+
* @param direction : Indicates transmit/receive direction that the packet buffer has
75+
* been used for. This might be needed if tx/rx pools are separate.
76+
*/
77+
void cy_buffer_release(whd_buffer_t buffer, whd_buffer_dir_t direction);
78+
79+
/** Retrieves the current pointer of a packet buffer
80+
*
81+
* Since packet buffers usually need to be created with space at the
82+
* front for additional headers, this function allows WHD to get
83+
* the current 'front' location pointer.
84+
*
85+
* @param buffer : The handle of the packet buffer whose pointer is to be retrieved
86+
*
87+
* @return : The packet buffer's current pointer.
88+
*/
89+
uint8_t* cy_buffer_get_current_piece_data_pointer(whd_buffer_t buffer);
90+
91+
/** Retrieves the size of a packet buffer
92+
*
93+
* Since packet buffers usually need to be created with space at the
94+
* front for additional headers, the memory block used to contain a packet buffer
95+
* will often be larger than the current size of the packet buffer data.
96+
* This function allows WHD to retrieve the current size of a packet buffer's data.
97+
*
98+
* @param buffer : The handle of the packet buffer whose size is to be retrieved
99+
*
100+
* @return : The size of the packet buffer.
101+
*/
102+
uint16_t cy_buffer_get_current_piece_size(whd_buffer_t buffer);
103+
104+
/** Sets the current size of a WHD packet
105+
*
106+
* This function sets the current length of a WHD packet buffer
107+
*
108+
* @param buffer : The packet to be modified
109+
* @param size : The new size of the packet buffer
110+
*
111+
* @return : CY_RSLT_SUCCESS or WHD_PMK_WRONG_LENGTH if the requested size is not valid
112+
*/
113+
whd_result_t cy_buffer_set_size(whd_buffer_t buffer, unsigned short size);
114+
115+
/** Moves the current pointer of a packet buffer
116+
*
117+
* Since packet buffers usually need to be created with space at the front for additional headers,
118+
* this function allows WHD to move the current 'front' location pointer so that it has space to
119+
* add headers to transmit packets, and so that the network stack does not see the internal WHD
120+
* headers on received packets.
121+
*
122+
* @param buffer : A pointer to the handle of the current packet buffer for which the
123+
* current pointer will be moved. On return this may contain a pointer
124+
* to a newly allocated packet buffer which has been daisy chained to
125+
* the front of the given one. This would be the case if the given
126+
* packet buffer didn't have enough space at the front.
127+
* @param add_remove_amount : This is the number of bytes to move the current pointer of the packet
128+
* buffer - a negative value increases the space for headers at the
129+
* front of the packet, a positive value decreases the space.
130+
*
131+
* @return : CY_RSLT_SUCCESS or WHD_PMK_WRONG_LENGTH if the added amount is
132+
* outside the size of the buffer
133+
*/
134+
whd_result_t cy_buffer_add_remove_at_front(whd_buffer_t* buffer, int32_t add_remove_amount);
135+
136+
137+
/** Called by WHD to pass received data to the network stack
138+
*
139+
* Packets received from the Wi-Fi network by WHD are forwarded to by calling function ptr which
140+
* must be implemented in the network interface. Ethernet headers
141+
* are present at the start of these packet buffers.
142+
*
143+
* This function is called asynchronously in the context of the
144+
* WHD thread whenever new data has arrived.
145+
* Packet buffers are allocated within WHD, and ownership is transferred
146+
* to the network stack. The network stack or application is thus
147+
* responsible for releasing the packet buffers.
148+
* Most packet buffering systems have a pointer to the 'current point' within
149+
* the packet buffer. When this function is called, the pointer points
150+
* to the start of the Ethernet header. There is other inconsequential data
151+
* before the Ethernet header.
152+
*
153+
* It is preferable that the (whd_network_process_ethernet_data)() function simply puts
154+
* the received packet on a queue for processing by another thread. This avoids the
155+
* WHD thread being unnecessarily tied up which would delay other packets
156+
* being transmitted or received.
157+
*
158+
* @param interface : The interface on which the packet was received.
159+
* @param buffer : Handle of the packet which has just been received. Responsibility for
160+
* releasing this buffer is transferred from WHD at this point.
161+
*
162+
*/
163+
void cy_network_process_ethernet_data(whd_interface_t interface, whd_buffer_t buffer);
164+
165+
166+
#ifdef __cplusplus
167+
}
168+
#endif // __cplusplus
169+
170+
/** \} group_bsp_network_buffer */

0 commit comments

Comments
 (0)