Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gen3: SPI.transfer() supports accepting const void* parameter. #2196

Merged
merged 7 commits into from
Dec 22, 2020

Conversation

XuGuohui
Copy link
Member

SPI.transfer takes a void * for the TX buffer. On Gen2 (Electron, etc) SPI.transfer works when casting a const TX buffer (ex. const array, const string, etc) to a non-const TX buffer. On Gen3 this will result in an immediate hard fault and program crash because the nRF SPI DMA function requires the tx buffer to be in RAM.

Change the interface of SPI.transfer to take const void * and to copy the buffer to RAM if it was not in RAM.

Example App

#include "Particle.h"

Serial1LogHandler logHandler(115200, LOG_LEVEL_ALL);

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);

static runtime_info_t heapInfo;

void setup() {
    if (heapInfo.size == 0) {
        heapInfo.size = sizeof(heapInfo);
    }

    HAL_Core_Runtime_Info(&heapInfo, nullptr);
    Log.info("Heapinfo: size :%lu, largest_free_block_heap :%lu", heapInfo.freeheap, heapInfo.largest_free_block_heap);

    SPI.setDataMode(SPI_MODE0);
    SPI.setClockSpeed(100 * 1000);
    SPI.begin();
}

void loop() {
    delay(1000);

    SPI.transfer((uint8_t*) "ABC", NULL, sizeof("ABC"), NULL);
    HAL_Core_Runtime_Info(&heapInfo, nullptr);
    Log.info("Heapinfo: size :%lu, largest_free_block_heap :%lu", heapInfo.freeheap, heapInfo.largest_free_block_heap);
}

Completeness

  • User is totes amazing for contributing!
  • Contributor has signed CLA (Info here)
  • Problem and Solution clearly stated
  • Run unit/integration/application tests on device
  • Added documentation
  • Added to CHANGELOG.md after merging (add links to docs and issues)

Copy link
Member

@avtolstoy avtolstoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transfer() signature should also reflect these changes. The tx pointer should be const.

hal/src/nRF52840/spi_hal.cpp Outdated Show resolved Hide resolved
hal/src/nRF52840/spi_hal.cpp Outdated Show resolved Hide resolved
@XuGuohui XuGuohui added this to the 3.0.0 milestone Sep 22, 2020
hal/src/nRF52840/spi_hal.cpp Outdated Show resolved Hide resolved
hal/src/nRF52840/spi_hal.cpp Outdated Show resolved Hide resolved
hal/src/nRF52840/spi_hal.cpp Outdated Show resolved Hide resolved
hal/src/nRF52840/spi_hal.cpp Outdated Show resolved Hide resolved
user/tests/wiring/no_fixture_spi/spix.cpp Outdated Show resolved Hide resolved
@XuGuohui XuGuohui force-pushed the feature/gen3_spi_transfer/ch50466 branch from 93e97d3 to 83a7afc Compare November 23, 2020 10:26
hal/inc/spi_hal.h Outdated Show resolved Hide resolved
hal/src/nRF52840/spi_hal.cpp Outdated Show resolved Hide resolved
hal/src/nRF52840/spi_hal.cpp Outdated Show resolved Hide resolved
hal/src/nRF52840/spi_hal.cpp Outdated Show resolved Hide resolved
user/tests/wiring/no_fixture_spi/spix.cpp Outdated Show resolved Hide resolved
user/tests/wiring/no_fixture_spi/spix.cpp Outdated Show resolved Hide resolved
hal/src/nRF52840/spi_hal.cpp Show resolved Hide resolved
user/tests/wiring/spi_master_slave/spi_slave/spi_slave.cpp Outdated Show resolved Hide resolved
@XuGuohui XuGuohui force-pushed the feature/gen3_spi_transfer/ch50466 branch from 70589f3 to 80c86d6 Compare December 22, 2020 05:39
@XuGuohui XuGuohui merged commit 74f0224 into develop Dec 22, 2020
@XuGuohui XuGuohui deleted the feature/gen3_spi_transfer/ch50466 branch December 22, 2020 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants