diff --git a/.gitignore b/.gitignore index 6e1cf89..58c2a7e 100644 --- a/.gitignore +++ b/.gitignore @@ -62,4 +62,6 @@ cmake-build-remote **/cmake-build*/ .idea/ dist/ -debug/ \ No newline at end of file +debug/ +/examples/receive_lora_cubeide_stm32/receive_lora_cubeide_stm32.launch +/examples/receive_lora_cubeide_stm32/STM32F103RBTX_FLASH.ld diff --git a/extra_script.py b/extra_script.py index f52d65f..53ccb06 100644 --- a/extra_script.py +++ b/extra_script.py @@ -1,10 +1,6 @@ Import('env') -if env.get("PIOPLATFORM") == "ststm32": - env.Replace(SRC_FILTER=["+<*>", "-<*_spi*>", "+<*_stm32_spi.c>"]) - -if env.get("PIOPLATFORM") == "gd32": - env.Replace(SRC_FILTER=["+<*>", "-<*_spi*>", "+<*_gd32_spi.c>"]) - if env.get("PIOPLATFORM") == "espressif32": env.Replace(SRC_FILTER=["+<*>", "-<*_spi*>", "+<*_esp_spi.c>"]) +else: + env.Replace(SRC_FILTER=["+<*>", "-<*_spi*>"]) \ No newline at end of file diff --git a/src/sx127x_gd32_spi.c b/src/sx127x_gd32_spi.c deleted file mode 100644 index e217994..0000000 --- a/src/sx127x_gd32_spi.c +++ /dev/null @@ -1,123 +0,0 @@ -#if defined(GD32F10x) -#include "gd32f10x.h" -#elif defined(GD32F1x0) -#include "gd32f1x0.h" -#elif defined(GD32F20x) -#include "gd32f20x.h" -#elif defined(GD32F3x0) -#include "gd32f3x0.h" -#elif defined(GD32F30x) -#include "gd32f30x.h" -#elif defined(GD32F4xx) -#include "gd32f4xx.h" -#elif defined(GD32F403) -#include "gd32f403.h" -#elif defined(GD32E10X) -#include "gd32e10x.h" -#elif defined(GD32E23x) -#include "gd32e23x.h" -#elif defined(GD32E50X) -#include "gd32e50x.h" -#elif defined(GD32L23x) -#include "gd32l23x.h" -#elif defined(GD32W51x) -#include "gd32w51x.h" -#else -#error "Unknown chip series" -#endif -#include -#include -#include -#include -#include - -#define SPI_TIMEOUT 10000 - -#define ERROR_CHECK(x) \ - do { \ - int __err_rc = (x); \ - if (__err_rc != 0) { \ - spi_nss_internal_high((uint32_t)spi_device); \ - return __err_rc; \ - } \ - } while (0) - -int spi_master_write(void *spi_device, uint8_t value, uint8_t *result) { - int count = 0; - /* wait the SPI transmit buffer is empty */ - // FIXME potentially remove count++? - while ((0 == spi_i2s_flag_get((uint32_t)spi_device, SPI_FLAG_TBE)) && (count++ < 1000)); - if (count >= 1000) { - *result = 0; - return -1; - } - spi_i2s_data_transmit((uint32_t)spi_device, value); - - count = 0; - /* wait the SPI receive buffer is not empty */ - while ((0 == spi_i2s_flag_get((uint32_t)spi_device, SPI_FLAG_RBNE)) && (count++ < 1000)); - if (count >= 1000) { - *result = 0; - return -1; - } - *result = spi_i2s_data_receive((uint32_t)spi_device); - return 0; -} - -int sx127x_spi_read_registers(int reg, void *spi_device, size_t data_length, uint32_t *result) { - if (data_length == 0 || data_length > 4) { - return -1; - } - spi_nss_internal_low((uint32_t)spi_device); - uint8_t response; - ERROR_CHECK(spi_master_write(spi_device, ((uint8_t)reg & 0x7F), &response)); - for (size_t i = 0; i < data_length; i++) { - ERROR_CHECK(spi_master_write(spi_device, 0, &response)); - *result |= (response << (8 * i)); - } - spi_nss_internal_high((uint32_t)spi_device); - return 0; -} - -int sx127x_spi_read_buffer(int reg, uint8_t *buffer, size_t buffer_length, void *spi_device) { - if (buffer_length < 1) { - return 0; - } - spi_nss_internal_low((uint32_t)spi_device); - uint8_t response; - ERROR_CHECK(spi_master_write(spi_device, ((uint8_t)reg & 0x7F), &response)); - for (size_t i = 0; i < buffer_length; i++) { - ERROR_CHECK(spi_master_write(spi_device, 0, &response)); - buffer[i] = response; - } - spi_nss_internal_high((uint32_t)spi_device); - return 0; -} - -int sx127x_spi_write_register(int reg, const uint8_t *data, size_t data_length, void *spi_device) { - if (data_length == 0 || data_length > 4) { - return -1; - } - spi_nss_internal_low((uint32_t)spi_device); - uint8_t response; - ERROR_CHECK(spi_master_write(spi_device, ((uint8_t)reg | 0x80), &response)); - for (size_t i = 0; i < data_length; i++) { - ERROR_CHECK(spi_master_write(spi_device, data[i], &response)); - } - spi_nss_internal_high((uint32_t)spi_device); - return 0; -} - -int sx127x_spi_write_buffer(int reg, const uint8_t *buffer, size_t buffer_length, void *spi_device) { - if (buffer_length < 1) { - return 0; - } - spi_nss_internal_low((uint32_t)spi_device); - uint8_t response; - ERROR_CHECK(spi_master_write(spi_device, ((uint8_t)reg | 0x80), &response)); - for (size_t i = 0; i < buffer_length; i++) { - ERROR_CHECK(spi_master_write(spi_device, buffer[i], &response)); - } - spi_nss_internal_high((uint32_t)spi_device); - return 0; -} diff --git a/src/sx127x_stm32_spi.c b/src/sx127x_stm32_spi.c deleted file mode 100644 index 27b97a0..0000000 --- a/src/sx127x_stm32_spi.c +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#include -#include -#include - -#define SPI_TIMEOUT 10000 - -// stm32 doesn't have a common header where SPI functions defined -// thus define them as "extern" here -extern int HAL_SPI_Transmit(void *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout); -extern int HAL_SPI_TransmitReceive(void *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout); - -int sx127x_spi_read_registers(int reg, void *spi_device, size_t data_length, uint32_t *result) { - if (data_length == 0 || data_length > 4) { - return -1; - } - uint64_t tx_buf = ((uint8_t)reg & 0x7F); - uint64_t rx_buf = 0; - int code = HAL_SPI_TransmitReceive(spi_device, (uint8_t *)&tx_buf, (uint8_t *)&rx_buf, data_length + 1, SPI_TIMEOUT); - if (code != 0) { - *result = 0; - return code; - } - *result = __builtin_bswap32(rx_buf >> 8) >> (4 - data_length) * 8; - return 0; -} - -int sx127x_spi_read_buffer(int reg, uint8_t *buffer, size_t buffer_length, void *spi_device) { - if (buffer_length < 1) { - return 0; - } - uint8_t *tmp = malloc(buffer_length + 1); - if (tmp == NULL) { - return ENOMEM; - } - uint64_t tx_buf = ((uint8_t)reg & 0x7F); - int code = HAL_SPI_TransmitReceive(spi_device, (uint8_t *)&tx_buf, (uint8_t *)&tmp, buffer_length + 1, SPI_TIMEOUT); - if (code != 0) { - free(tmp); - return code; - } - // shift 1 byte to the right - memcpy(buffer, tmp + 1, buffer_length); - free(tmp); - return 0; -} - -int sx127x_spi_write_register(int reg, const uint8_t *data, size_t data_length, void *spi_device) { - if (data_length == 0 || data_length > 4) { - return -1; - } - uint8_t tmp[5] = {0}; - tmp[0] = reg | 0x80; - memcpy(tmp + 1, data, data_length); - return HAL_SPI_Transmit(spi_device, tmp, data_length + 1, SPI_TIMEOUT); -} - -int sx127x_spi_write_buffer(int reg, const uint8_t *buffer, size_t buffer_length, void *spi_device) { - uint8_t *tmp = malloc(buffer_length + 1); - if (tmp == NULL) { - return ENOMEM; - } - tmp[0] = reg | 0x80; - memcpy(tmp + 1, buffer, buffer_length); - int code = HAL_SPI_Transmit(spi_device, tmp, buffer_length + 1, SPI_TIMEOUT); - free(tmp); - return code; -}