Skip to content

Commit 8e38d02

Browse files
author
Jamie Smith
authored
Fix issues with new async SPI changes that broke compiling for B_U585I_IOT2A (ARMmbed#204)
* Fix issues with new async SPI changes that broke compiling for B_U585I_IOT2A * Few more incorrect nullptrs * Fix style
1 parent c1effb1 commit 8e38d02

File tree

5 files changed

+93
-49
lines changed

5 files changed

+93
-49
lines changed

connectivity/drivers/wifi/TARGET_STM/COMPONENT_EMW3080B/EMW3080B_SPI.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ void EMW3080B_SPI::spi_handler(int event)
195195
}
196196

197197

198-
int32_t EMW3080B_SPI::TransmitReceive(uint8_t *txdata, uint8_t *rxdata, uint32_t datalen,
198+
int32_t EMW3080B_SPI::TransmitReceive(uint8_t *txdata, CacheAlignedBuffer<uint8_t> &rxdata, uint32_t datalen,
199199
uint32_t timeout)
200200
{
201201
int32_t ret = 0;
202202
debug_if(_debug_level >= DEBUG_LOG, "EMW3080B_SPI : Spi Tx Rx %" PRIu32 "\n", datalen);
203-
SPI::transfer((const uint8_t *) txdata, (int) datalen, rxdata, (int) datalen, callback(this, &EMW3080B_SPI::spi_handler), SPI_EVENT_COMPLETE);
203+
SPI::transfer(txdata, (int) datalen, rxdata, (int) datalen, callback(this, &EMW3080B_SPI::spi_handler), SPI_EVENT_COMPLETE);
204204
if (SEM_WAIT(spi_transfer_done_sem, timeout, NULL) != SEM_OK) {
205205
debug_if(_debug_level >= DEBUG_WARNING, "EMW3080B_SPI : Timeout on TransmitReceive %d\n", spi_handler_count);
206206
ret = -1;
@@ -210,23 +210,23 @@ int32_t EMW3080B_SPI::TransmitReceive(uint8_t *txdata, uint8_t *rxdata, uint32_t
210210
}
211211

212212

213-
int32_t EMW3080B_SPI::Transmit(uint8_t *txdata, uint32_t datalen, uint32_t timeout)
213+
int32_t EMW3080B_SPI::Transmit(uint8_t *const txdata, uint32_t datalen, uint32_t timeout)
214214
{
215215
int32_t ret = 0;
216216
debug_if(_debug_level >= DEBUG_LOG, "EMW3080B_SPI : Spi Tx %" PRIu32 "\n", datalen);
217-
SPI::transfer((const uint8_t *) txdata, (int) datalen, (uint8_t *)NULL, (int) datalen, callback(this, &EMW3080B_SPI::spi_handler), SPI_EVENT_COMPLETE);
217+
SPI::transfer(txdata, (int) datalen, nullptr, (int) datalen, callback(this, &EMW3080B_SPI::spi_handler), SPI_EVENT_COMPLETE);
218218
if (SEM_WAIT(spi_transfer_done_sem, timeout, NULL) != SEM_OK) {
219219
debug_if(_debug_level >= DEBUG_WARNING, "EMW3080B_SPI : Timeout on Transmit\n");
220220
ret = -1;
221221
}
222222
return ret;
223223
}
224224

225-
int32_t EMW3080B_SPI::Receive(uint8_t *rxdata, uint32_t datalen, uint32_t timeout)
225+
int32_t EMW3080B_SPI::Receive(CacheAlignedBuffer<uint8_t> &rxdata, uint32_t datalen, uint32_t timeout)
226226
{
227227
int32_t ret = 0;
228228
debug_if(_debug_level >= DEBUG_LOG, "EMW3080B_SPI : Spi Rx %" PRIu32 "\n", datalen);
229-
SPI::transfer((const uint8_t *) NULL, (int) datalen, rxdata, (int) datalen, callback(this, &EMW3080B_SPI::spi_handler), SPI_EVENT_COMPLETE);
229+
SPI::transfer(nullptr, (int) datalen, rxdata, (int) datalen, callback(this, &EMW3080B_SPI::spi_handler), SPI_EVENT_COMPLETE);
230230
if (SEM_WAIT(spi_transfer_done_sem, timeout, NULL) != SEM_OK) {
231231
debug_if(_debug_level >= DEBUG_WARNING, "EMW3080B_SPI : Timeout on Receive\n");
232232
ret = -1;
@@ -291,10 +291,13 @@ void EMW3080B_SPI::process_txrx_poll(uint32_t timeout)
291291
sheader.type = 0;
292292
sheader.len = 0;
293293

294-
if (TransmitReceive((uint8_t *)&mheader, (uint8_t *)&sheader, sizeof(mheader), SPI_MAX_TRANSMIT_DURATION)) {
294+
StaticCacheAlignedBuffer<uint8_t, sizeof(mheader)> rxBuffer;
295+
if (TransmitReceive((uint8_t *)&mheader, rxBuffer, sizeof(mheader), SPI_MAX_TRANSMIT_DURATION)) {
295296
MX_WIFI_SPI_CS_HIGH();
296297
debug_if(_debug_level >= DEBUG_WARNING, "EMW3080B_SPI : Send mheader error\r\n");
297298
}
299+
memcpy(&sheader, rxBuffer.data(), sizeof(mheader));
300+
298301
if (sheader.type != SPI_READ) {
299302
MX_WIFI_SPI_CS_HIGH();
300303
debug_if(_debug_level >= DEBUG_WARNING, "EMW3080B_SPI : Invalid SPI type %02x\r\n", sheader.type);
@@ -342,12 +345,12 @@ void EMW3080B_SPI::process_txrx_poll(uint32_t timeout)
342345
spi_tx_data = NULL;
343346
spi_tx_len = 0;
344347
if (NULL != p) {
345-
ret = TransmitReceive(txdata, p, datalen, SPI_MAX_TRANSMIT_DURATION);
348+
ret = TransmitReceive(txdata, netb->buffer, datalen, SPI_MAX_TRANSMIT_DURATION);
346349
} else {
347350
ret = Transmit(txdata, datalen, SPI_MAX_TRANSMIT_DURATION);
348351
}
349352
} else {
350-
ret = Receive(p, datalen, SPI_MAX_TRANSMIT_DURATION);
353+
ret = Receive(netb->buffer, datalen, SPI_MAX_TRANSMIT_DURATION);
351354
}
352355

353356
if (ret) {

connectivity/drivers/wifi/TARGET_STM/COMPONENT_EMW3080B/EMW3080B_SPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ class EMW3080B_SPI : public SPI {
6161
uint8_t *spi_tx_data = NULL;
6262
uint16_t spi_tx_len = 0;
6363

64-
int32_t TransmitReceive(uint8_t *txdata, uint8_t *rxdata, uint32_t datalen, uint32_t timeout);
64+
int32_t TransmitReceive(uint8_t *txdata, CacheAlignedBuffer<uint8_t> &rxdata, uint32_t datalen, uint32_t timeout);
6565
int32_t Transmit(uint8_t *txdata, uint32_t datalen, uint32_t timeout);
66-
int32_t Receive(uint8_t *rxdata, uint32_t datalen, uint32_t timeout);
66+
int32_t Receive(CacheAlignedBuffer<uint8_t> &rxdata, uint32_t datalen, uint32_t timeout);
6767
void spi_handler(int event);
6868

6969
int8_t mx_wifi_spi_txrx_start(void);

connectivity/drivers/wifi/TARGET_STM/COMPONENT_EMW3080B/mx_wifi_mbed_os.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,43 @@
2020
extern "C"
2121
{
2222

23-
#if BASIC_MALLOC == 0
23+
#if BASIC_MALLOC
24+
25+
26+
// CacheAlignedBuffer-based implementation
27+
28+
mx_buf_t *mx_buf_alloc(uint32_t len)
29+
{
30+
return new mx_buf_t(len);
31+
}
32+
33+
void mx_buf_free(mx_buf_t *p)
34+
{
35+
delete p;
36+
}
37+
38+
void mx_buf_hide_header(mx_buf_t *p, int32_t n)
39+
{
40+
p->header_len += n;
41+
}
42+
43+
uint8_t *mx_buf_payload(mx_buf_t *p)
44+
{
45+
return p->buffer.data() + p->header_len;
46+
}
47+
48+
uint32_t mx_buf_get_size(mx_buf_t *p)
49+
{
50+
return p->len;
51+
}
52+
53+
void mx_buf_set_size(mx_buf_t *p, int32_t n)
54+
{
55+
p->len = n;
56+
}
57+
58+
#else
59+
// Memory manager implementation
2460

2561
mx_buf_t *mx_buf_alloc(uint32_t len)
2662
{

connectivity/drivers/wifi/TARGET_STM/COMPONENT_EMW3080B/mx_wifi_mbed_os.h

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#ifndef MX_WIFI_CMSIS_OS_H
1616
#define MX_WIFI_CMSIS_OS_H
1717

18+
#ifdef __cplusplus
19+
#include "CacheAlignedBuffer.h"
20+
#endif
21+
1822
#ifdef __cplusplus
1923
extern "C" {
2024
#endif
@@ -34,56 +38,57 @@ extern "C" {
3438
#define MX_WIFI_FREE free
3539
#endif /* MX_WIFI_FREE */
3640

41+
// Set to 1 to use basic memory allocation (new and delete).
42+
// Set to 0 to use emac3080b_global_memory_manager for memory allocations.
3743
#define BASIC_MALLOC 1
38-
/* Assume that OS is not used when bypass is disabled */
3944

4045
#if BASIC_MALLOC
41-
typedef struct mx_buf {
42-
uint32_t len;
43-
uint32_t header_len;
44-
uint8_t data[1];
45-
} mx_buf_t;
4646

47-
static inline mx_buf_t *mx_buf_alloc(uint32_t len)
47+
#ifdef __cplusplus
48+
49+
/**
50+
* Structure for an MX wifi buffer.
51+
* Only visible to C++ files -- to C files, it is an opaque void pointer.
52+
*/
53+
struct mx_buf
4854
{
49-
mx_buf_t *p = (mx_buf_t *) MX_WIFI_MALLOC(len + sizeof(mx_buf_t) -1U);
50-
if (NULL != p) {
51-
p->len = len;
52-
p->header_len = 0;
53-
}
54-
return p;
55+
DynamicCacheAlignedBuffer<uint8_t> buffer;
56+
uint32_t header_len = 0;
57+
uint32_t len;
5558

56-
}
59+
mx_buf(size_t max_len):
60+
buffer(max_len),
61+
len(max_len)
62+
{}
63+
};
64+
typedef mx_buf mx_buf_t;
5765

58-
#define MX_NET_BUFFER_ALLOC(len) mx_buf_alloc(len)
59-
#define MX_NET_BUFFER_FREE(p) MX_WIFI_FREE(p)
60-
#define MX_NET_BUFFER_HIDE_HEADER(p, n) (p)->header_len += (n)
61-
#define MX_NET_BUFFER_PAYLOAD(p) &(p)->data[(p)->header_len]
62-
#define MX_NET_BUFFER_SET_PAYLOAD_SIZE(p, size) (p)->len = (size)
63-
#define MX_NET_BUFFER_GET_PAYLOAD_SIZE(p) (p)->len
66+
#else
67+
68+
typedef void mx_buf_t;
6469

65-
#else /* BASIC_ALLOC */
70+
#endif
71+
72+
#else /* BASIC_MALLOC */
73+
74+
typedef void mx_buf_t;
75+
76+
#endif /* BASIC_MALLOC */
6677

67-
#define MX_NET_BUFFER_ALLOC(len) mx_buf_alloc((len))
78+
#define MX_NET_BUFFER_ALLOC(len) mx_buf_alloc(len)
6879
#define MX_NET_BUFFER_FREE(p) mx_buf_free((p))
6980
#define MX_NET_BUFFER_HIDE_HEADER(p, n) mx_buf_hide_header((p),(n))
7081
#define MX_NET_BUFFER_PAYLOAD(p) mx_buf_payload((p))
7182
#define MX_NET_BUFFER_SET_PAYLOAD_SIZE(p, size) mx_buf_set_size((p),(size))
7283
#define MX_NET_BUFFER_GET_PAYLOAD_SIZE(p) mx_buf_get_size((p))
7384

74-
75-
typedef void mx_buf_t;
76-
7785
mx_buf_t *mx_buf_alloc(uint32_t len);
7886
void mx_buf_free(mx_buf_t *p);
7987
void mx_buf_hide_header(mx_buf_t *p, int32_t n);
8088
uint8_t *mx_buf_payload(mx_buf_t *p);
8189
uint32_t mx_buf_get_size(mx_buf_t *p);
8290
void mx_buf_set_size(mx_buf_t *p, int32_t n);
8391

84-
#endif /* BASIC_ALLOC */
85-
86-
8792
void mbed_delay(uint32_t delay);
8893
void *mbed_mutex_new(void);
8994
void mbed_mutex_delete(void *);

drivers/include/drivers/SPI.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -390,16 +390,16 @@ class SPI : private NonCopyable<SPI> {
390390
// Overloads of the above to support passing nullptr
391391
template<typename WordT>
392392
typename std::enable_if<std::is_integral<WordT>::value, int>::type
393-
write(const std::nullptr_t *tx_buffer, int tx_length, WordT *rx_buffer, int rx_length)
393+
write(const std::nullptr_t tx_buffer, int tx_length, WordT *rx_buffer, int rx_length)
394394
{
395-
return write_internal(reinterpret_cast<char const *>(tx_buffer), tx_length, reinterpret_cast<char *>(rx_buffer), rx_length);
395+
return write_internal(tx_buffer, tx_length, reinterpret_cast<char *>(rx_buffer), rx_length);
396396
}
397397

398398
template<typename WordT>
399399
typename std::enable_if<std::is_integral<WordT>::value, int>::type
400-
write(const WordT *tx_buffer, int tx_length, std::nullptr_t *rx_buffer, int rx_length)
400+
write(const WordT *tx_buffer, int tx_length, std::nullptr_t rx_buffer, int rx_length)
401401
{
402-
return write_internal(reinterpret_cast<char const *>(tx_buffer), tx_length, reinterpret_cast<char *>(rx_buffer), rx_length);
402+
return write_internal(reinterpret_cast<char const *>(tx_buffer), tx_length, rx_buffer, rx_length);
403403
}
404404

405405
/**
@@ -487,14 +487,14 @@ class SPI : private NonCopyable<SPI> {
487487
// Overloads of the above to support passing nullptr
488488
template<typename WordT>
489489
typename std::enable_if<std::is_integral<WordT>::value, int>::type
490-
transfer(const std::nullptr_t *tx_buffer, int tx_length, CacheAlignedBuffer<WordT> &rx_buffer, int rx_length, const event_callback_t &callback, int event = SPI_EVENT_COMPLETE)
490+
transfer(const std::nullptr_t tx_buffer, int tx_length, CacheAlignedBuffer<WordT> &rx_buffer, int rx_length, const event_callback_t &callback, int event = SPI_EVENT_COMPLETE)
491491
{
492492
MBED_ASSERT(rx_length <= static_cast<int>(rx_buffer.capacity()));
493-
return transfer_internal(tx_buffer, tx_length, rx_buffer, rx_length, callback, event);
493+
return transfer_internal(tx_buffer, tx_length, rx_buffer.data(), rx_length, callback, event);
494494
}
495495
template<typename WordT>
496496
typename std::enable_if<std::is_integral<WordT>::value, int>::type
497-
transfer(const WordT *tx_buffer, int tx_length, std::nullptr_t *rx_buffer, int rx_length, const event_callback_t &callback, int event = SPI_EVENT_COMPLETE)
497+
transfer(const WordT *tx_buffer, int tx_length, std::nullptr_t rx_buffer, int rx_length, const event_callback_t &callback, int event = SPI_EVENT_COMPLETE)
498498
{
499499
return transfer_internal(tx_buffer, tx_length, rx_buffer, rx_length, callback, event);
500500
}
@@ -535,14 +535,14 @@ class SPI : private NonCopyable<SPI> {
535535
// Overloads of the above to support passing nullptr
536536
template<typename WordT>
537537
typename std::enable_if<std::is_integral<WordT>::value, int>::type
538-
transfer_and_wait(const std::nullptr_t *tx_buffer, int tx_length, CacheAlignedBuffer<WordT> &rx_buffer, int rx_length, rtos::Kernel::Clock::duration_u32 timeout = rtos::Kernel::wait_for_u32_forever)
538+
transfer_and_wait(const std::nullptr_t tx_buffer, int tx_length, CacheAlignedBuffer<WordT> &rx_buffer, int rx_length, rtos::Kernel::Clock::duration_u32 timeout = rtos::Kernel::wait_for_u32_forever)
539539
{
540540
MBED_ASSERT(rx_length <= static_cast<int>(rx_buffer.capacity()));
541541
return transfer_and_wait_internal(tx_buffer, tx_length, rx_buffer.data(), rx_length, timeout);
542542
}
543543
template<typename WordT>
544544
typename std::enable_if<std::is_integral<WordT>::value, int>::type
545-
transfer_and_wait(const WordT *tx_buffer, int tx_length, std::nullptr_t *rx_buffer, int rx_length, rtos::Kernel::Clock::duration_u32 timeout = rtos::Kernel::wait_for_u32_forever)
545+
transfer_and_wait(const WordT *tx_buffer, int tx_length, std::nullptr_t rx_buffer, int rx_length, rtos::Kernel::Clock::duration_u32 timeout = rtos::Kernel::wait_for_u32_forever)
546546
{
547547
return transfer_and_wait_internal(tx_buffer, tx_length, rx_buffer, rx_length, timeout);
548548
}

0 commit comments

Comments
 (0)