Skip to content

Commit 3c2ed16

Browse files
committed
Fix TT_M3HQ build problem with SPI
1 parent ce58505 commit 3c2ed16

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

targets/TARGET_TT/TARGET_TT_M3HQ/spi_api.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
#include "pinmap.h"
3333
#include "gpio_include.h"
3434

35+
#if DEVICE_SPI_ASYNCH
36+
#define SPI_S(obj) (struct spi_s *) (&((obj)->spi))
37+
#else
38+
#define SPI_S(obj) (struct spi_s *) (obj)
39+
#endif
40+
3541
static const PinMap PinMap_SPI_SCLK[] = {
3642
{PM0, SPI_0, PIN_DATA(3, 1)},
3743
{PB2, SPI_1, PIN_DATA(3, 1)},
@@ -69,6 +75,7 @@ static const PinMap PinMap_SPI_SSEL[] = {
6975

7076
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
7177
{
78+
struct spi_s *obj_s = SPI_S(obj);
7279
TSB_TSPI_TypeDef* spi;
7380
// Check pin parameters
7481
SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
@@ -78,9 +85,9 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
7885
SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
7986
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
8087

81-
obj->module = (SPIName)pinmap_merge(spi_data, spi_cntl);
82-
spi = obj->spi;
83-
switch ((int)obj->module) {
88+
obj_s->module = (SPIName)pinmap_merge(spi_data, spi_cntl);
89+
spi = obj_s->spi;
90+
switch ((int)obj_s->module) {
8491
case SPI_0:
8592
TSB_CG_FSYSENA_IPENA11 = ENABLE;
8693
TSB_CG_FSYSENB_IPENB00 = ENABLE;
@@ -110,7 +117,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
110117
error("Cannot found SPI module corresponding with input pins.");
111118
break;
112119
}
113-
obj->spi = spi;
120+
obj_s->spi = spi;
114121
// pin out the SPI pins
115122
pinmap_pinout(mosi, PinMap_SPI_MOSI);
116123
pinmap_pinout(miso, PinMap_SPI_MISO);
@@ -146,20 +153,22 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
146153

147154
void spi_free(spi_t *obj)
148155
{
156+
struct spi_s *obj_s = SPI_S(obj);
149157
TSB_TSPI_TypeDef* spi;
150158

151-
spi = obj->spi;
159+
spi = obj_s->spi;
152160
spi->CR0 |= TSPI_DISABLE;
153161
spi->CR2 = TSPI_INT_ALL; // Disable all interrupt
154162
}
155163

156164
void spi_format(spi_t *obj, int bits, int mode, int slave)
157165
{
166+
struct spi_s *obj_s = SPI_S(obj);
158167
TSB_TSPI_TypeDef* spi;
159168

160-
obj->bits = bits;
161-
spi = obj->spi;
162-
obj->bits = bits;
169+
obj_s->bits = bits;
170+
spi = obj_s->spi;
171+
obj_s->bits = bits;
163172
spi->CR0 |= TSPI_DISABLE;
164173

165174
if (bits >= 8 || bits <= 32) {
@@ -174,10 +183,11 @@ void spi_format(spi_t *obj, int bits, int mode, int slave)
174183

175184
void spi_frequency(spi_t *obj, int hz)
176185
{
186+
struct spi_s *obj_s = SPI_S(obj);
177187
TSB_TSPI_TypeDef* spi;
178188
int clk_div = 1;
179189
uint32_t clocks = ((SystemCoreClock / 2) / hz);
180-
obj->spi->CR0 |= TSPI_DISABLE;
190+
obj_s->spi->CR0 |= TSPI_DISABLE;
181191

182192
while (clk_div < 10) {
183193
if (clocks < 16) {
@@ -190,17 +200,18 @@ void spi_frequency(spi_t *obj, int hz)
190200
if (clk_div == 0) {
191201
clocks++;
192202
}
193-
spi = obj->spi;
203+
spi = obj_s->spi;
194204
spi->CR0 |= TSPI_DISABLE;
195205
spi->BR = ((clk_div << 4) | clocks);
196206
spi->CR0 |= TSPI_ENABLE;
197207
}
198208

199209
int spi_master_write(spi_t *obj, int value)
200210
{
211+
struct spi_s *obj_s = SPI_S(obj);
201212
TSB_TSPI_TypeDef* spi;
202-
MBED_ASSERT(obj != NULL);
203-
spi = obj->spi;
213+
MBED_ASSERT(obj_s != NULL);
214+
spi = obj_s->spi;
204215
spi->CR3 |= TSPI_TX_BUFF_CLR_DONE; // FIFO Cear
205216
// Check if the TSPI is already enabled
206217
if((spi->CR0 & TSPI_ENABLE) != TSPI_ENABLE) {
@@ -258,10 +269,11 @@ int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
258269

259270
int spi_busy(spi_t *obj)
260271
{
272+
struct spi_s *obj_s = SPI_S(obj);
261273
TSB_TSPI_TypeDef* spi;
262274
uint8_t result = 0;
263275

264-
spi = obj->spi;
276+
spi = obj_s->spi;
265277
if( (spi->SR & (1<<7)) || (spi->SR & (1<<23))) {
266278
result = 1;
267279
} else {
@@ -272,7 +284,8 @@ int spi_busy(spi_t *obj)
272284

273285
uint8_t spi_get_module(spi_t *obj)
274286
{
275-
return (uint8_t)(obj->module);
287+
struct spi_s *obj_s = SPI_S(obj);
288+
return (uint8_t)(obj_s->module);
276289
}
277290

278291
const PinMap *spi_master_mosi_pinmap()

0 commit comments

Comments
 (0)