Skip to content

Commit 661b6ad

Browse files
Laurent MEUNIERLMESTM
authored andcommitted
[STM32] spi_master_write - rely on HAL
ASYNCH SPI transfer support has been added based on STM HAL services. To have both ASYNCH and SYNCH work together, we're also moving the write API to STM HAL instead of direct registers access.
1 parent 0aeea49 commit 661b6ad

File tree

1 file changed

+15
-39
lines changed

1 file changed

+15
-39
lines changed

targets/TARGET_STM/stm_spi_api.c

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -333,42 +333,6 @@ static inline int ssp_writeable(spi_t *obj)
333333
return status;
334334
}
335335

336-
static inline void ssp_write(spi_t *obj, int value)
337-
{
338-
SPI_TypeDef *spi = SPI_INST(obj);
339-
struct spi_s *spiobj = SPI_S(obj);
340-
SPI_HandleTypeDef *handle = &(spiobj->handle);
341-
342-
while (!ssp_writeable(obj));
343-
344-
//spi->DR = (uint16_t)value;
345-
if (handle->Init.DataSize == SPI_DATASIZE_8BIT) {
346-
// Force 8-bit access to the data register
347-
uint8_t *p_spi_dr = 0;
348-
p_spi_dr = (uint8_t *) & (spi->DR);
349-
*p_spi_dr = (uint8_t)value;
350-
} else { // SPI_DATASIZE_16BIT
351-
spi->DR = (uint16_t)value;
352-
}
353-
}
354-
355-
static inline int ssp_read(spi_t *obj)
356-
{
357-
SPI_TypeDef *spi = SPI_INST(obj);
358-
struct spi_s *spiobj = SPI_S(obj);
359-
SPI_HandleTypeDef *handle = &(spiobj->handle);
360-
while (!ssp_readable(obj));
361-
//return (int)spi->DR;
362-
if (handle->Init.DataSize == SPI_DATASIZE_8BIT) {
363-
// Force 8-bit access to the data register
364-
uint8_t *p_spi_dr = 0;
365-
p_spi_dr = (uint8_t *) & (spi->DR);
366-
return (int)(*p_spi_dr);
367-
} else {
368-
return (int)spi->DR;
369-
}
370-
}
371-
372336
static inline int ssp_busy(spi_t *obj)
373337
{
374338
int status;
@@ -380,8 +344,21 @@ static inline int ssp_busy(spi_t *obj)
380344

381345
int spi_master_write(spi_t *obj, int value)
382346
{
383-
ssp_write(obj, value);
384-
return ssp_read(obj);
347+
uint16_t size, Rx, ret;
348+
struct spi_s *spiobj = SPI_S(obj);
349+
SPI_HandleTypeDef *handle = &(spiobj->handle);
350+
351+
size = (handle->Init.DataSize == SPI_DATASIZE_16BIT) ? 2 : 1;
352+
353+
/* Use 10ms timeout */
354+
ret = HAL_SPI_TransmitReceive(handle,(uint8_t*)&value,(uint8_t*)&Rx,size,10);
355+
356+
if(ret == HAL_OK) {
357+
return Rx;
358+
} else {
359+
DEBUG_PRINTF("SPI inst=0x%8X ERROR in write\r\n", (int)handle->Instance);
360+
return -1;
361+
}
385362
}
386363

387364
int spi_slave_receive(spi_t *obj)
@@ -395,7 +372,6 @@ int spi_slave_read(spi_t *obj)
395372
struct spi_s *spiobj = SPI_S(obj);
396373
SPI_HandleTypeDef *handle = &(spiobj->handle);
397374
while (!ssp_readable(obj));
398-
//return (int)spi->DR;
399375
if (handle->Init.DataSize == SPI_DATASIZE_8BIT) {
400376
// Force 8-bit access to the data register
401377
uint8_t *p_spi_dr = 0;

0 commit comments

Comments
 (0)