Browse Source

Add LoRa module

crypto_algorithm
yudongdong 1 year ago
parent
commit
849f5d7dd5
  1. 6
      applications/Makefile
  2. 3
      applications/connection_demo/adhoc_lora_demo/Makefile
  3. 108
      applications/connection_demo/adhoc_lora_demo/client.c
  4. 63
      applications/connection_demo/adhoc_lora_demo/gateway.c
  5. 4
      applications/framework_init.c
  6. 5
      board/aiit-arm32-board/Kconfig
  7. 63
      board/aiit-arm32-board/third_party_driver/include/connect_spi_lora.h
  8. 39
      board/aiit-arm32-board/third_party_driver/spi/Kconfig
  9. 5
      board/aiit-arm32-board/third_party_driver/spi/Makefile
  10. 543
      board/aiit-arm32-board/third_party_driver/spi/connect_lora_spi.c
  11. 7
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/Makefile
  12. 95
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/inc/spi_lora_sx12xx.h
  13. 3
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/ports/Makefile
  14. 38
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/ports/sx12xx_device.c
  15. 10
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/Makefile
  16. 97
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/platform.h
  17. 75
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/radio.c
  18. 77
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/radio.h
  19. 616
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-Fsk.c
  20. 1471
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-Fsk.h
  21. 532
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-FskMisc.c
  22. 251
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-FskMisc.h
  23. 170
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-Hal.h
  24. 800
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.c
  25. 820
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.h
  26. 420
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRaMisc.c
  27. 324
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRaMisc.h
  28. 360
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276.c
  29. 93
      board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276.h
  30. 6
      board/aiit-riscv64-board/Kconfig
  31. 63
      board/aiit-riscv64-board/third_party_driver/include/connect_spi_lora.h
  32. 35
      board/aiit-riscv64-board/third_party_driver/spi/Kconfig
  33. 5
      board/aiit-riscv64-board/third_party_driver/spi/Makefile
  34. 535
      board/aiit-riscv64-board/third_party_driver/spi/connect_lora_spi.c
  35. 7
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/Makefile
  36. 95
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/inc/spi_lora_sx12xx.h
  37. 3
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/ports/Makefile
  38. 38
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/ports/sx12xx_device.c
  39. 10
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/Makefile
  40. 97
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/platform.h
  41. 75
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/radio.c
  42. 77
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/radio.h
  43. 616
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-Fsk.c
  44. 1471
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-Fsk.h
  45. 532
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-FskMisc.c
  46. 251
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-FskMisc.h
  47. 170
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-Hal.h
  48. 800
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.c
  49. 820
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRa.h
  50. 420
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRaMisc.c
  51. 324
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-LoRaMisc.h
  52. 360
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276.c
  53. 93
      board/aiit-riscv64-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276.h
  54. 8
      framework/connection/Adapter/Kconfig
  55. 6
      framework/connection/Adapter/Makefile
  56. 184
      framework/connection/Adapter/include/xs_adapter_lora.h
  57. 30
      framework/connection/Adapter/lora/Kconfig
  58. 3
      framework/connection/Adapter/lora/Makefile
  59. 849
      framework/connection/Adapter/lora/xs_adapter_lora.c
  60. 39
      framework/connection/Adapter/src/xs_adapter_manager.c
  61. 4
      path_kernel.mk

6
applications/Makefile

@ -25,9 +25,9 @@ ifeq ($(CONFIG_RESOURCES_SPI_SD), y)
SRC_FILES += spi_sd_card_mount.c
endif
# ifeq ($(CONFIG_CONNECTION_COMMUNICATION_BOOTSTART_LORA_NET_SAMPLE), y)
# SRC_DIR += connection_demo/adhoc_lora_demo
# endif
ifeq ($(CONFIG_CONNECTION_COMMUNICATION_BOOTSTART_LORA_NET_SAMPLE), y)
SRC_DIR += connection_demo/adhoc_lora_demo
endif
ifeq ($(CONFIG_CONNECTION_COMMUNICATION_ZIGBEE), y)
SRC_DIR += connection_demo/zigbee_demo

3
applications/connection_demo/adhoc_lora_demo/Makefile

@ -0,0 +1,3 @@
SRC_FILES := client.c gateway.c
include $(KERNEL_ROOT)/compiler.mk

108
applications/connection_demo/adhoc_lora_demo/client.c

@ -0,0 +1,108 @@
#include <user_api.h>
#include <string.h>
#include <xs_adapter_lora.h>
#include <xs_adapter_manager.h>
#include <stdio.h>
#include <stdlib.h>
char client_name[DEVNAME_LEN_MAX] = "lora_dev_123";
void success_cb(void *param)
{
printf("success_cb, param = %s\n", param);
}
void invert_param(void *param)
{
printf("success_cb02 invoke, invert bool param.\n");
bool *bparam = (bool *)param;
if (*bparam)
{
*bparam = false;
}
else
{
*bparam = true;
}
}
void net_lora_client(int argc, char *argv[])
{
int pkg_count = 10;
if (argc >= 1)
{
memset(client_name, 0, DEVNAME_LEN_MAX);
strncpy(client_name, argv[1], strlen(argv[1]));
printf("lora client set clientName(%s).\n", client_name);
}
if (argc >= 2)
{
pkg_count = atoi(argv[2]);
printf("lora client set pkg_count(%d).\n", pkg_count);
}
// 1.Create an adapter for a specific agreement (LORA)
static struct AdapterLora lora_adapter;
memset(&lora_adapter, 0, sizeof(lora_adapter));
struct AdapterDone lora_example_done = {
.NetAiitOpen = LoraAdapterOpen,
.NetAiitClose = LoraAdapterCose,
.NetAiitSend = LoraAdapterSendc2g,
.NetAiitReceive = NULL,
.NetAiitJoin = LoraAdapterJoin,
.NetAiitIoctl = NULL,
};
lora_adapter.parent.done = lora_example_done; // Bind adapter operation
lora_adapter.name = client_name; // Set adapter name
lora_adapter.spi_lora_fd = -1; // Set adapter information
lora_adapter.deve_ui = "xxx";
lora_adapter.app_key = "yyy";
// 2.Register the adapter in the list
LoraAdapterInit();
LoraAdapterRegister((adapter_t)&lora_adapter);
// 3.Find from the list of registered adapters
adapter_t padapter = LoraAdapterFind(client_name);
if (NONE == padapter)
{
printf("adapter find failed!\n");
return;
}
// 4.Open adapter
if (0 != padapter->done.NetAiitOpen(padapter))
{
printf("adapter open failed!\n");
return;
}
// 5.Join the specified network segment as client
printf("NetAiitJoin start. \n");
padapter->done.NetAiitJoin(padapter, ROLE_TYPE_SLAVE, CONNECTION_COMMUNICATION_LORA_NET_ID);
printf("NetAiitJoin end. \n");
// 6.Point to point sending data to gateway
int i = 0;
while (i < pkg_count)
{
char data[120] = {0};
sprintf(data, "***** I am %s, data_num = %d ******" ,client_name, i);
bool v = false;
padapter->done.NetAiitSend(padapter, data, strlen(data) + 1, true, 10000, 0, invert_param, &v, NULL);
while (!v) // Asynchronous analog synchronization
{
UserTaskDelay(100);
}
printf("send success(main thread)... %s\n" ,data);
i++;
UserTaskDelay(800); // Contract interval
}
printf("all pkg send success(main thread), quit.\n");
padapter->done.NetAiitClose(padapter);
printf("client quit.\n");
}

63
applications/connection_demo/adhoc_lora_demo/gateway.c

@ -0,0 +1,63 @@
#include <user_api.h>
#include <string.h>
#include <xs_klist.h>
#include <xs_adapter_lora.h>
#include <xs_adapter_manager.h>
extern DoubleLinklistType online_user_head;
void net_lora_gateway(int argc, char *argv[])
{
// 1.New specific agreement (LORA) adapter
static struct AdapterLora lora_adapter;
memset(&lora_adapter, 0, sizeof(lora_adapter));
struct AdapterDone lora_example_done = {
.NetAiitOpen = LoraAdapterOpen,
.NetAiitClose = NULL,
.NetAiitSend = NULL,
.NetAiitReceive = LoraAdapterReceive,
.NetAiitJoin = LoraAdapterJoin,
.NetAiitIoctl = NULL,
};
lora_adapter.parent.done = lora_example_done; // Bind adapter operation
lora_adapter.name = "lora_dev_456"; // Set adapter name
lora_adapter.spi_lora_fd = -1; // Set adapter information
lora_adapter.deve_ui = "xxx";
lora_adapter.app_key = "yyy";
// 2.Register the adapter in the list
LoraAdapterInit();
LoraAdapterRegister((adapter_t)&lora_adapter);
// 3.Find from the list of registered adapters
adapter_t padapter = LoraAdapterFind("lora_dev_456");
if (NONE == padapter)
{
printf("adapter find failed!\n");
return;
}
// 4.Open adapter
if (0 != padapter->done.NetAiitOpen(padapter))
{
printf("adapter open failed!\n");
return;
}
// 5.Join the specified network segment as gateway
padapter->done.NetAiitJoin(padapter, ROLE_TYPE_MASTER, CONNECTION_COMMUNICATION_LORA_NET_ID);
}
static void net_lora_connectedlist(int argc, char *argv[])
{
DoubleLinklistType* pNode;
printf("******** connected users *********\n");
DOUBLE_LINKLIST_FOR_EACH(pNode, &online_user_head)
{
OnlineUser* pUser =CONTAINER_OF(pNode, OnlineUser, link);
printf("%s\n", pUser->user_name);
}
printf("*********************************\n");
}

4
applications/framework_init.c

@ -17,7 +17,7 @@ extern int SensorFrameworkInit(void);
extern int RegisterAdapterEthernet(void);
extern int RegisterAdapterWifi(void);
extern int RegisterAdapterZigbee(void);
extern int LORA_sx12xx_spi_device_init();
extern int LoraSx12xxSpiDeviceInit();
extern int D124VoiceInit(void);
extern int Hs300xTemperatureInit(void);
@ -94,7 +94,7 @@ static struct InitDesc connection_desc[] =
#endif
#ifdef CONNECTION_COMMUNICATION_LORA
{ "lora adpter", LORA_sx12xx_spi_device_init},
{ "lora adpter", LoraSx12xxSpiDeviceInit},
#endif
#ifdef CONNECTION_COMMUNICATION_ZIGBEE

5
board/aiit-arm32-board/Kconfig

@ -44,6 +44,11 @@ menu "aiit-arm32-board feature"
string "wifi uart name"
default "/dev/usart3_dev3"
endif
if CONNECTION_COMMUNICATION_LORA
config LORA_SPI_NAME
string "lora spi name"
default "/dev/spi2_lora"
endif
endmenu
endmenu

63
board/aiit-arm32-board/third_party_driver/include/connect_spi_lora.h

@ -0,0 +1,63 @@
/*
* Copyright (c) 2020 AIIT XUOS Lab
* XiUOS is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* @file connect_spi_lora.h
* @brief define spi lora dev function and struct using bus driver framework
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021-04-24
*/
#ifndef CONNECT_SPI_LORA_H
#define CONNECT_SPI_LORA_H
#include <bus.h>
#include <connect_spi.h>
#include <device.h>
#include <radio.h>
#include <hardware_gpio.h>
#include <stm32f4xx.h>
#include <spi_lora_sx12xx.h>
#include <sx1276.h>
#include <sx1276-Hal.h>
#include <sx1276-LoRa.h>
#include <sx1276-LoRaMisc.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SPI_LORA_FREQUENCY 10000000
#define SPI_LORA_BUFFER_SIZE 256
typedef struct SpiLoraDevice *SpiLoraDeviceType;
struct LoraDevDone
{
uint32 (*open) (void *dev);
uint32 (*close) (void *dev);
uint32 (*write) (void *dev, struct BusBlockWriteParam *write_param);
uint32 (*read) (void *dev, struct BusBlockReadParam *read_param);
};
struct SpiLoraDevice
{
struct SpiHardwareDevice *spi_dev;
struct SpiHardwareDevice lora_dev;
};
#ifdef __cplusplus
}
#endif
#endif

39
board/aiit-arm32-board/third_party_driver/spi/Kconfig

@ -22,6 +22,45 @@ if BSP_USING_SPI2
config SPI_2_DRV_NAME
string "spi bus 2 driver name"
default "spi2_drv"
menuconfig CONNECTION_COMMUNICATION_LORA_USING_SX1278
bool "Using spi lora SX1278"
default n
if CONNECTION_COMMUNICATION_LORA_USING_SX1278
config SX12XX_SPI_DEVICE_NAME
string "SX1278 device spi name"
default "spi2_dev0"
config SX12XX_DEVICE_NAME
string "SX1278 device name"
default "spi2_lora"
config CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN
int
default 10
config CONNECTION_COMMUNICATION_LORA_SX12XX_DO0_PIN
int
default 10
config CONNECTION_COMMUNICATION_LORA_SX12XX_DO1_PIN
int
default 10
config CONNECTION_COMMUNICATION_LORA_SX12XX_DO2_PIN
int
default 10
config CONNECTION_COMMUNICATION_LORA_SX12XX_DO3_PIN
int
default 10
config CONNECTION_COMMUNICATION_LORA_SX12XX_DO4_PIN
int
default 10
config CONNECTION_COMMUNICATION_LORA_SX12XX_DO5_PIN
int
default 10
endif
endif
config BSP_USING_SPI3

5
board/aiit-arm32-board/third_party_driver/spi/Makefile

@ -4,4 +4,9 @@ ifeq ($(CONFIG_RESOURCES_SPI_SFUD),y)
SRC_FILES += connect_flash_spi.c
endif
ifeq ($(CONFIG_CONNECTION_COMMUNICATION_LORA_USING_SX1278),y)
SRC_DIR += third_party_spi_lora
SRC_FILES += connect_lora_spi.c
endif
include $(KERNEL_ROOT)/compiler.mk

543
board/aiit-arm32-board/third_party_driver/spi/connect_lora_spi.c

@ -0,0 +1,543 @@
/*
* Original Copyright (c) 2006-2018, RT-Thread Development Team
* Modified Copyright (c) 2020 AIIT XUOS Lab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Change Logs:
* Date Author Notes
* 2019-02-20 XiaojieFan the first version
*/
/*
* Modified by: AIIT XUOS Lab
* Modified date: 2020-09-01
* Description: replace original macro and basic date type with AIIT XUOS Lab's own defination
* start using AIIT XUOS Lab's own functional interfaces
* change some header files
*/
#include <connect_spi_lora.h>
static struct HardwareDev *g_spi_lora_dev;
static BusType buspin;
tRadioDriver *Radio = NONE;
void SX1276InitIo(void)
{
struct PinParam PinCfg;
struct PinStat PinStat;
struct BusConfigureInfo configure_info;
struct BusBlockWriteParam write_param;
write_param.buffer = (void *)&PinStat;
configure_info.configure_cmd = OPE_CFG;
configure_info.private_data = (void *)&PinCfg;
buspin = PinBusInitGet();
PinCfg.cmd = GPIO_CONFIG_MODE;
PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO0_PIN;
PinCfg.mode = GPIO_CFG_INPUT;
BusDrvConfigure(buspin->owner_driver, &configure_info);
PinCfg.cmd = GPIO_CONFIG_MODE;
PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO1_PIN;
PinCfg.mode = GPIO_CFG_INPUT;
BusDrvConfigure(buspin->owner_driver, &configure_info);
PinCfg.cmd = GPIO_CONFIG_MODE;
PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO2_PIN;
PinCfg.mode = GPIO_CFG_INPUT;
BusDrvConfigure(buspin->owner_driver, &configure_info);
}
inline uint8_t SX1276ReadDio0(void)
{
struct PinStat PinStat;
struct BusBlockReadParam read_param;
read_param.buffer = (void *)&PinStat;
PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO0_PIN;
return BusDevReadData(buspin->owner_haldev, &read_param);
}
inline uint8_t SX1276ReadDio1(void)
{
struct PinStat PinStat;
struct BusBlockReadParam read_param;
read_param.buffer = (void *)&PinStat;
PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO1_PIN;
return BusDevReadData(buspin->owner_haldev, &read_param);
}
inline uint8_t SX1276ReadDio2(void)
{
struct PinStat PinStat;
struct BusBlockReadParam read_param;
read_param.buffer = (void *)&PinStat;
PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO2_PIN;
return BusDevReadData(buspin->owner_haldev, &read_param);
}
inline uint8_t SX1276ReadDio3(void)
{
struct PinStat PinStat;
struct BusBlockReadParam read_param;
read_param.buffer = (void *)&PinStat;
PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO3_PIN;
return BusDevReadData(buspin->owner_haldev, &read_param);
}
inline uint8_t SX1276ReadDio4(void)
{
struct PinStat PinStat;
struct BusBlockReadParam read_param;
read_param.buffer = (void *)&PinStat;
PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO4_PIN;
return BusDevReadData(buspin->owner_haldev, &read_param);
}
inline uint8_t SX1276ReadDio5(void)
{
struct PinStat PinStat;
struct BusBlockReadParam read_param;
read_param.buffer = (void *)&PinStat;
PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_DO5_PIN;
return BusDevReadData(buspin->owner_haldev, &read_param);
}
inline void SX1276WriteRxTx(uint8_t txEnable)
{
if (txEnable != 0)
{
}
else
{
}
}
void SX1276SetReset(uint8_t state)
{
struct PinParam PinCfg;
struct PinStat PinStat;
struct BusConfigureInfo configure_info;
struct BusBlockWriteParam write_param;
configure_info.configure_cmd = OPE_CFG;
configure_info.private_data = (void *)&PinCfg;
write_param.buffer = (void *)&PinStat;
if (state == RADIO_RESET_ON)
{
PinCfg.cmd = GPIO_CONFIG_MODE;
PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN;
PinCfg.mode = GPIO_CFG_OUTPUT;
BusDrvConfigure(buspin->owner_driver, &configure_info);
PinStat.val = GPIO_LOW;
PinStat.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN;
BusDevWriteData(buspin->owner_haldev, &write_param);
}
else
{
PinCfg.cmd = GPIO_CONFIG_MODE;
PinCfg.pin = CONNECTION_COMMUNICATION_LORA_SX12XX_RST_PIN;
PinCfg.mode = GPIO_CFG_INPUT;
BusDrvConfigure(buspin->owner_driver, &configure_info);
}
}
void SX1276Write(uint8_t addr, uint8_t data)
{
SX1276WriteBuffer(addr, &data, 1);
}
void SX1276Read(uint8_t addr, uint8_t *data)
{
SX1276ReadBuffer(addr, data, 1);
}
void SX1276WriteBuffer(uint8_t addr, uint8_t *buffer, uint8_t size)
{
struct BusBlockWriteParam write_param;
uint8 write_addr = addr | 0x80;
BusDevOpen(g_spi_lora_dev);
write_param.buffer = (void *)&write_addr;
write_param.size = 1;
BusDevWriteData(g_spi_lora_dev, &write_param);
write_param.buffer = (void *)buffer;
write_param.size = size;
BusDevWriteData(g_spi_lora_dev, &write_param);
BusDevClose(g_spi_lora_dev);
}
void SX1276ReadBuffer(uint8_t addr, uint8_t *buffer, uint8_t size)
{
struct BusBlockWriteParam write_param;
struct BusBlockReadParam read_param;
uint8 write_addr = addr & 0x7F;
BusDevOpen(g_spi_lora_dev);
write_param.buffer = (void *)&write_addr;
write_param.size = 1;
BusDevWriteData(g_spi_lora_dev, &write_param);
read_param.buffer = (void *)buffer;
read_param.size = size;
BusDevReadData(g_spi_lora_dev, &read_param);
BusDevClose(g_spi_lora_dev);
}
void SX1276WriteFifo(uint8_t *buffer, uint8_t size)
{
SX1276WriteBuffer(0, buffer, size);
}
void SX1276ReadFifo(uint8_t *buffer, uint8_t size)
{
SX1276ReadBuffer(0, buffer, size);
}
uint8_t SX1276_Spi_Check()
{
uint8_t test = 0;
KPrintf("SX1276_Spi_Check start\n");
tLoRaSettings settings;
SX1276Read(REG_LR_VERSION,&test);
KPrintf("version code of the chip is %x\n",test);
settings.RFFrequency = SX1276LoRaGetRFFrequency();
KPrintf("SX1278 Lora parameters are :\nRFFrequency is %d\n",settings.RFFrequency);
settings.Power = SX1276LoRaGetRFPower();
KPrintf("RFPower is %d\n",settings.Power);
settings.SignalBw = SX1276LoRaGetSignalBandwidth();
KPrintf("SignalBw is %d\n",settings.SignalBw);
settings.SpreadingFactor = SX1276LoRaGetSpreadingFactor();
KPrintf("SpreadingFactor is %d\n",settings.SpreadingFactor);
/*SPI confirm*/
SX1276Write(REG_LR_HOPPERIOD, 0x91);
SX1276Read(REG_LR_HOPPERIOD, &test);
if (test != 0x91) {
return 0;
}
return test;
}
/**
* This function supports to write data to the lora.
*
* @param dev lora dev descriptor
* @param write_param lora dev write datacfg param
*/
static uint32 SpiLoraWrite(void *dev, struct BusBlockWriteParam *write_param)
{
NULL_PARAM_CHECK(dev);
NULL_PARAM_CHECK(write_param);
uint8 i;
char Msg[SPI_LORA_BUFFER_SIZE] = {0};
if (write_param->size > 120) {
KPrintf("SpiLoraWrite ERROR:The message is too long!\n");
return ERROR;
} else {
//Radio->SetTxPacket(write_param->buffer, write_param->size);
//while(Radio->Process() != RF_TX_DONE);
SX1276SetTxPacket(write_param->buffer, write_param->size);
while(SX1276Process() != RF_TX_DONE);
KPrintf("SpiLoraWrite success!\n");
}
return EOK;
}
/**
* This function supports to read data from the lora.
*
* @param dev lora dev descriptor
* @param read_param lora dev read datacfg param
*/
static uint32 SpiLoraRead(void *dev, struct BusBlockReadParam *read_param)
{
NULL_PARAM_CHECK(dev);
NULL_PARAM_CHECK(read_param);
//Radio->StartRx();
SX1276StartRx();
KPrintf("SpiLoraRead Ready!\n");
//while(Radio->Process() != RF_RX_DONE);
//Radio->GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length);
while(SX1276Process() != RF_RX_DONE);
SX1276GetRxPacket(read_param->buffer, (uint16 *)&read_param->read_length);
KPrintf("SpiLoraRead : %s\n", read_param->buffer);
return EOK;
}
static uint32 SpiLoraOpen(void *dev)
{
NULL_PARAM_CHECK(dev);
KPrintf("SpiLoraOpen start\n");
x_err_t ret = EOK;
struct HardwareDev *haldev = (struct HardwareDev *)dev;
struct SpiHardwareDevice *lora_dev = CONTAINER_OF(haldev, struct SpiHardwareDevice, haldev);
NULL_PARAM_CHECK(lora_dev);
SpiLoraDeviceType spi_lora_dev = CONTAINER_OF(lora_dev, struct SpiLoraDevice, lora_dev);
NULL_PARAM_CHECK(spi_lora_dev);
struct Driver *spi_drv = spi_lora_dev->spi_dev->haldev.owner_bus->owner_driver;
struct BusConfigureInfo configure_info;
struct SpiMasterParam spi_master_param;
spi_master_param.spi_data_bit_width = 8;
spi_master_param.spi_work_mode = SPI_MODE_0 | SPI_MSB;
spi_master_param.spi_maxfrequency = SPI_LORA_FREQUENCY;
spi_master_param.spi_data_endian = 0;
configure_info.configure_cmd = OPE_CFG;
configure_info.private_data = (void *)&spi_master_param;
ret = BusDrvConfigure(spi_drv, &configure_info);
if (ret) {
KPrintf("spi drv OPE_CFG error drv %8p cfg %8p\n", spi_drv, &spi_master_param);
return ERROR;
}
configure_info.configure_cmd = OPE_INT;
ret = BusDrvConfigure(spi_drv, &configure_info);
if (ret) {
KPrintf("spi drv OPE_INT error drv %8p\n", spi_drv);
return ERROR;
}
SX1276Init();
if (0x91 != SX1276_Spi_Check()) {
KPrintf("LoRa check failed!\n!");
} else {
Radio = RadioDriverInit();
KPrintf("LoRa check ok!\nNote: The length of the message that can be sent in a single time is 120 characters\n");
}
return ret;
}
static const struct LoraDevDone lora_done =
{
.open = SpiLoraOpen,
.close = NONE,
.write = SpiLoraWrite,
.read = SpiLoraRead,
};
/**
* This function supports to init spi_lora_dev
*
* @param bus_name spi bus name
* @param dev_name spi dev name
* @param drv_name spi drv name
* @param flash_name flash dev name
*/
SpiLoraDeviceType SpiLoraInit(char *bus_name, char *dev_name, char *drv_name, char *lora_name)
{
NULL_PARAM_CHECK(dev_name);
NULL_PARAM_CHECK(drv_name);
NULL_PARAM_CHECK(lora_name);
NULL_PARAM_CHECK(bus_name);
x_err_t ret;
static HardwareDevType haldev;
haldev = SpiDeviceFind(dev_name, TYPE_SPI_DEV);
if (NONE == haldev) {
KPrintf("SpiLoraInit find spi haldev %s error! \n", dev_name);
return NONE;
}
SpiLoraDeviceType spi_lora_dev = (SpiLoraDeviceType)malloc(sizeof(struct SpiLoraDevice));
if (NONE == spi_lora_dev) {
KPrintf("SpiLoraInit malloc spi_lora_dev failed\n");
free(spi_lora_dev);
return NONE;
}
memset(spi_lora_dev, 0, sizeof(struct SpiLoraDevice));
spi_lora_dev->spi_dev = CONTAINER_OF(haldev, struct SpiHardwareDevice, haldev);
spi_lora_dev->lora_dev.spi_dev_flag = RET_TRUE;
spi_lora_dev->lora_dev.haldev.dev_done = (struct HalDevDone *)&lora_done;
spi_lora_dev->spi_dev->haldev.owner_bus->owner_driver = SpiDriverFind(drv_name, TYPE_SPI_DRV);
if (NONE == spi_lora_dev->spi_dev->haldev.owner_bus->owner_driver) {
KPrintf("SpiLoraInit find spi driver %s error! \n", drv_name);
free(spi_lora_dev);
return NONE;
}
ret = SpiDeviceRegister(&spi_lora_dev->lora_dev, spi_lora_dev->spi_dev->haldev.private_data, lora_name);
if (EOK != ret) {
KPrintf("SpiLoraInit SpiDeviceRegister device %s error %d\n", lora_name, ret);
free(spi_lora_dev);
return NONE;
}
ret = SpiDeviceAttachToBus(lora_name, bus_name);
if (EOK != ret) {
KPrintf("SpiLoraInit SpiDeviceAttachToBus device %s error %d\n", lora_name, ret);
free(spi_lora_dev);
return NONE;
}
g_spi_lora_dev = &spi_lora_dev->spi_dev->haldev;
return spi_lora_dev;
}
/**
* This function supports to release spi_lora_dev
*
* @param spi_lora_dev spi lora descriptor
*/
uint32 SpiLoraRelease(SpiLoraDeviceType spi_lora_dev)
{
NULL_PARAM_CHECK(spi_lora_dev);
x_err_t ret;
DeviceDeleteFromBus(spi_lora_dev->lora_dev.haldev.owner_bus, &spi_lora_dev->lora_dev.haldev);
free(spi_lora_dev);
return EOK;
}
int LoraSx12xxSpiDeviceInit(void)
{
#ifdef BSP_USING_SPI2
__IO uint32_t tmpreg = 0x00U;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
tmpreg = RCC->AHB1ENR & RCC_AHB1ENR_GPIOAEN;
(void)tmpreg;
if (EOK != HwSpiDeviceAttach(SPI_BUS_NAME_2, SX12XX_SPI_DEVICE_NAME, GPIOC, GPIO_Pin_6)) {
return ERROR;
}
if (NONE == SpiLoraInit(SPI_BUS_NAME_2, SX12XX_SPI_DEVICE_NAME, SPI_2_DRV_NAME, SX12XX_DEVICE_NAME)) {
return ERROR;
}
#endif
return EOK;
}
/*Just for lora test*/
static struct Bus *bus;
static struct HardwareDev *dev;
static struct Driver *drv;
void LoraOpen(void)
{
x_err_t ret = EOK;
ret = LoraSx12xxSpiDeviceInit();
if (EOK != ret) {
KPrintf("LoraSx12xxSpiDeviceInit failed\n");
return;
}
bus = BusFind(SPI_BUS_NAME_2);
dev = BusFindDevice(bus, SX12XX_DEVICE_NAME);
drv = BusFindDriver(bus, SPI_2_DRV_NAME);
bus->match(drv, dev);
ret = SpiLoraOpen(dev);
if (EOK != ret) {
KPrintf("LoRa init failed\n");
return;
}
KPrintf("LoRa init succeed\n");
return;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
LoraOpen, LoraOpen, open lora device and read parameters );
static void LoraReceive(void)
{
struct BusBlockReadParam read_param;
memset(&read_param, 0, sizeof(struct BusBlockReadParam));
read_param.buffer = malloc(SPI_LORA_BUFFER_SIZE);
SpiLoraRead(dev, &read_param);
free(read_param.buffer);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),
LoraReceive, LoraReceive, lora wait message );
static void LoraSend(int argc, char *argv[])
{
char Msg[SPI_LORA_BUFFER_SIZE] = {0};
struct BusBlockWriteParam write_param;
memset(&write_param, 0, sizeof(struct BusBlockWriteParam));
if (argc == 2) {
strncpy(Msg, argv[1], SPI_LORA_BUFFER_SIZE);
write_param.buffer = Msg;
write_param.size = strlen(Msg);
SpiLoraWrite(dev, &write_param);
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
LoraSend, LoraSend, lora send message );

7
board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/Makefile

@ -0,0 +1,7 @@
SRC_DIR += src
ifeq ($(ARCH), arm)
SRC_DIR += ports
endif
include $(KERNEL_ROOT)/compiler.mk

95
board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/inc/spi_lora_sx12xx.h

@ -0,0 +1,95 @@
/*
* Original Copyright (c) 2006-2018, RT-Thread Development Team
* Modified Copyright (c) 2020 AIIT XUOS Lab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Change Logs:
* Date Author Notes
* 2019-02-25 XiaojieFan the first version
*/
/*
* Modified by: AIIT XUOS Lab
* Modified date: 2020-09-01
* Description:
*/
/*************************************************
File name: spi_lora_sx12xx.h
Description: support aiit board configure and register function
History:
1. Date: 2021-04-25
Author: AIIT XUOS Lab
Modification:
1. replace original macro and basic date type with AIIT XUOS Lab's own defination
*************************************************/
#ifndef __SPI_LORA_SX12XX_H_
#define __SPI_LORA_SX12XX_H_
#include <xiuos.h>
#include <stdbool.h>
/*!
* \brief Gets the SX1272 DIO0 hardware pin status
*
* \retval status Current hardware pin status [1, 0]
*/
uint8_t SX1276ReadDio0(void);
/*!
* \brief Gets the SX1272 DIO1 hardware pin status
*
* \retval status Current hardware pin status [1, 0]
*/
uint8_t SX1276ReadDio1(void);
/*!
* \brief Gets the SX1272 DIO2 hardware pin status
*
* \retval status Current hardware pin status [1, 0]
*/
uint8_t SX1276ReadDio2(void);
/*!
* \brief Gets the SX1272 DIO3 hardware pin status
*
* \retval status Current hardware pin status [1, 0]
*/
uint8_t SX1276ReadDio3(void);
/*!
* \brief Gets the SX1272 DIO4 hardware pin status
*
* \retval status Current hardware pin status [1, 0]
*/
uint8_t SX1276ReadDio4(void);
/*!
* \brief Gets the SX1272 DIO5 hardware pin status
*
* \retval status Current hardware pin status [1, 0]
*/
uint8_t SX1276ReadDio5(void);
void SX1276Write(uint8_t addr, uint8_t data);
void SX1276Read(uint8_t addr, uint8_t *data);
void SX1276WriteBuffer(uint8_t addr, uint8_t *buffer, uint8_t size);
void SX1276ReadBuffer(uint8_t addr, uint8_t *buffer, uint8_t size);
void SX1276WriteFifo(uint8_t *buffer, uint8_t size);
void SX1276ReadFifo(uint8_t *buffer, uint8_t size);
void SX1276SetReset(uint8_t state);
uint8_t SX1276_Spi_Check(void);
void SX1276WriteRxTx(uint8_t txEnable);
#endif

3
board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/ports/Makefile

@ -0,0 +1,3 @@
SRC_FILES := sx12xx_device.c
include $(KERNEL_ROOT)/compiler.mk

38
board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/ports/sx12xx_device.c

@ -0,0 +1,38 @@
/*
* Original Copyright (c) 2006-2018, RT-Thread Development Team
* Modified Copyright (c) 2020 AIIT XUOS Lab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Change Logs:
* Date Author Notes
* 2019-02-20 XiaojieFan the first version
*/
/*************************************************
File name: sx12xx_device.c
Description: support aiit board configure and register function
History:
1. Date: 2021-04-25
Author: AIIT XUOS Lab
Modification:
1. replace original macro and basic date type with AIIT XUOS Lab's own defination
*************************************************/
#include "connect_spi.h"
#include "board.h"
// #include "gpio.h"
#include "stm32f4xx.h"
#include "hardware_gpio.h"

10
board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/Makefile

@ -0,0 +1,10 @@
SRC_FILES += radio/radio.c \
radio/sx1276-Fsk.c \
radio/sx1276-FskMisc.c \
radio/sx1276-LoRa.c \
radio/sx1276-LoRaMisc.c \
radio/sx1276.c
include $(KERNEL_ROOT)/compiler.mk

97
board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/platform.h

@ -0,0 +1,97 @@
/*
* THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
* (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
* CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
* CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
* CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* Copyright (C) SEMTECH S.A.
*/
/*!
* \file platform.h
* \brief
*
* \version 1.0
* \date Nov 21 2012
* \author Miguel Luis
*/
/*************************************************
File name: platform.h
Description: support aiit board configure and register function
History:
1. Date: 2021-04-25
Author: AIIT XUOS Lab
Modification:
1. replace original macro and basic date type with AIIT XUOS Lab's own defination
*************************************************/
#ifndef __PLATFORM_H__
#define __PLATFORM_H__
#ifndef __GNUC__
#define inline
#endif
/*!
* Platform definition
*/
#define Bleeper 3
#define SX1243ska 2
#define SX12xxEiger 1
#define SX12000DVK 0
/*!
* Platform choice. Please uncoment the PLATFORM define and choose your platform
* or add/change the PLATFORM definition on the compiler Defines option
*/
#define PLATFORM SX12xxEiger
#if( PLATFORM == SX12xxEiger )
/*!
* Radio choice. Please uncomment the wanted radio and comment the others
* or add/change wanted radio definition on the compiler Defines option
*/
//#define USE_SX1232_RADIO
//#define USE_SX1272_RADIO
#define USE_SX1276_RADIO
//#define USE_SX1243_RADIO
/*!
* Module choice. There are three existing module with the SX1276.
* Please set the connected module to the value 1 and set the others to 0
*/
#ifdef USE_SX1276_RADIO
#define MODULE_SX1276RF1IAS 0
#define MODULE_SX1276RF1JAS 0
#define MODULE_SX1276RF1KAS 1
#endif
//#include "sx12xxEiger/sx12xxEiger.h"
#include "spi_lora_sx12xx.h"
#define USE_UART 0
#elif( PLATFORM == SX12000DVK )
/*!
* Radio choice. Please uncomment the wanted radio and comment the others
* or add/change wanted radio definition on the compiler Defines option
*/
//#define USE_SX1232_RADIO
#define USE_SX1272_RADIO
//#define USE_SX1276_RADIO
//#define USE_SX1243_RADIO
#include "sx1200dvk/sx1200dvk.h"
#elif( PLATFORM == SX1243ska )
#elif( PLATFORM == Bleeper )
#define USE_SX1272_RADIO
#include "bleeper/bleeper.h"
#define USE_UART 0
#else
#error "Missing define: Platform (ie. SX12xxEiger)"
#endif
#endif // __PLATFORM_H__

75
board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/radio.c

@ -0,0 +1,75 @@
/*
* THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
* (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
* CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
* CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
* CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* Copyright (C) SEMTECH S.A.
*/
/*!
* \file radio.c
* \brief Generic radio driver ( radio abstraction )
*
* \version 2.0.0
* \date Nov 21 2012
* \author Miguel Luis
*
* Last modified by Gregory Cristian on Apr 25 2013
*/
/*************************************************
File name: radio.c
Description: support aiit board configure and register function
History:
1. Date: 2021-04-25
Author: AIIT XUOS Lab
Modification:
1. replace original macro and basic date type with AIIT XUOS Lab's own defination
*************************************************/
#include <stdint.h>
#include "platform.h"
#include "radio.h"
#if defined( USE_SX1232_RADIO )
#include "sx1232.h"
#elif defined( USE_SX1272_RADIO )
#include "sx1272.h"
#elif defined( USE_SX1276_RADIO )
#include "sx1276.h"
#else
#error "Missing define: USE_XXXXXX_RADIO (ie. USE_SX1272_RADIO)"
#endif
tRadioDriver RadioDriver;
tRadioDriver* RadioDriverInit( void )
{
#if defined( USE_SX1232_RADIO )
RadioDriver.Init = SX1232Init;
RadioDriver.Reset = SX1232Reset;
RadioDriver.StartRx = SX1232StartRx;
RadioDriver.GetRxPacket = SX1232GetRxPacket;
RadioDriver.SetTxPacket = SX1232SetTxPacket;
RadioDriver.Process = SX1232Process;
#elif defined( USE_SX1272_RADIO )
RadioDriver.Init = SX1272Init;
RadioDriver.Reset = SX1272Reset;
RadioDriver.StartRx = SX1272StartRx;
RadioDriver.GetRxPacket = SX1272GetRxPacket;
RadioDriver.SetTxPacket = SX1272SetTxPacket;
RadioDriver.Process = SX1272Process;
#elif defined( USE_SX1276_RADIO )
RadioDriver.Init = SX1276Init;
RadioDriver.Reset = SX1276Reset;
RadioDriver.StartRx = SX1276StartRx;
RadioDriver.GetRxPacket = SX1276GetRxPacket;
RadioDriver.SetTxPacket = SX1276SetTxPacket;
RadioDriver.Process = SX1276Process;
RadioDriver.ChannelEmpty = SX1276ChannelEmpty;
#else
#error "Missing define: USE_XXXXXX_RADIO (ie. USE_SX1272_RADIO)"
#endif
return &RadioDriver;
}

77
board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/radio.h

@ -0,0 +1,77 @@
/*
* THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
* (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
* CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
* CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
* CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* Copyright (C) SEMTECH S.A.
*/
/*!
* \file radio.h
* \brief Generic radio driver ( radio abstraction )
*
* \version 2.0.B2
* \date Nov 21 2012
* \author Miguel Luis
*
* Last modified by Gregory Cristian on Apr 25 2013
*/
/*************************************************
File name: radio.h
Description: support aiit board configure and register function
History:
1. Date: 2021-04-25
Author: AIIT XUOS Lab
Modification:
1. replace original macro and basic date type with AIIT XUOS Lab's own defination
*************************************************/
#ifndef __RADIO_H__
#define __RADIO_H__
/*!
* SX1272 and SX1276 General parameters definition
*/
#define LORA 1 // [0: OFF, 1: ON]
/*!
* RF process function return codes
*/
typedef enum
{
RF_IDLE,
RF_BUSY,
RF_RX_DONE,
RF_RX_TIMEOUT,
RF_TX_DONE,
RF_TX_TIMEOUT,
RF_LEN_ERROR,
RF_CHANNEL_EMPTY,
RF_CHANNEL_ACTIVITY_DETECTED,
}tRFProcessReturnCodes;
/*!
* Radio driver structure defining the different function pointers
*/
typedef struct sRadioDriver
{
void ( *Init )( void );
void ( *Reset )( void );
void ( *StartRx )( void );
void ( *GetRxPacket )( void *buffer, uint16_t *size );
void ( *SetTxPacket )( const void *buffer, uint16_t size );
uint32_t ( *Process )( void );
uint32_t ( *ChannelEmpty )(void );
}tRadioDriver;
/*!
* \brief Initializes the RadioDriver structure with specific radio
* functions.
*
* \retval radioDriver Pointer to the radio driver variable
*/
tRadioDriver* RadioDriverInit( void );
#endif // __RADIO_H__

616
board/aiit-arm32-board/third_party_driver/spi/third_party_spi_lora/src/radio/sx1276-Fsk.c

@ -0,0 +1,616 @@
/*
* THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
* (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
* CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
* CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
* CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* Copyright (C) SEMTECH S.A.
*/
/*!
* \file sx1276.c
* \brief SX1276 RF chip driver
*
* \version 2.0.0
* \date May 6 2013
* \author Gregory Cristian
*
* Last modified by Miguel Luis on Jun 19 2013
*/
/*************************************************
File name: sx1276-Fsk.c
Description: support aiit board configure and register function
History:
1. Date: 2021-04-25
Author: AIIT XUOS Lab
Modification:
1. replace original macro and basic date type with AIIT XUOS Lab's own defination
*************************************************/
#include <string.h>
#include <math.h>
#include "platform.h"
#if defined( USE_SX1276_RADIO )
#include "radio.h"
#include "sx1276-Hal.h"
#include "sx1276.h"
#include "sx1276-FskMisc.h"
#include "sx1276-Fsk.h"
// Default settings
tFskSettings FskSettings =
{
870000000, // RFFrequency
9600, // Bitrate
50000, // Fdev
20, // Power
100000, // RxBw
150000, // RxBwAfc
true, // CrcOn
true, // AfcOn
255 // PayloadLength (set payload size to the maximum for variable mode, else set the exact payload length)
};
/*!
* SX1276 FSK registers variable
*/
tSX1276* SX1276;
/*!
* Local RF buffer for communication support
*/
static uint8_t RFBuffer[RF_BUFFER_SIZE];
/*!
* Chunk size of data write in buffer
*/
static uint8_t DataChunkSize = 32;
/*!
* RF state machine variable
*/
static uint8_t RFState = RF_STATE_IDLE;
/*!
* Rx management support variables
*/
/*!
* PacketTimeout holds the RF packet timeout
* SyncSize = [0..8]
* VariableSize = [0;1]
* AddressSize = [0;1]
* PayloadSize = [0..RF_BUFFER_SIZE]
* CrcSize = [0;2]
* PacketTimeout = ( ( 8 * ( VariableSize + AddressSize + PayloadSize + CrcSize ) / BR ) * 1000.0 ) + 1
* Computed timeout is in miliseconds
*/
static uint32_t PacketTimeout;
/*!
* Preamble2SyncTimeout
* Preamble2SyncTimeout = ( ( 8 * ( PremableSize + SyncSize ) / RFBitrate ) * 1000.0 ) + 1
* Computed timeout is in miliseconds
*/
static uint32_t Preamble2SyncTimeout;
static bool PreambleDetected = false;
static bool SyncWordDetected = false;
static bool PacketDetected = false;
static uint16_t RxPacketSize = 0;
static uint8_t RxBytesRead = 0;
static uint8_t TxBytesSent = 0;
static double RxPacketRssiValue;
static uint32_t RxPacketAfcValue;
static uint8_t RxGain = 1;
static uint32_t RxTimeoutTimer = 0;
static uint32_t Preamble2SyncTimer = 0;
/*!
* Tx management support variables
*/
static uint16_t TxPacketSize = 0;
static uint32_t TxTimeoutTimer = 0;
void SX1276FskInit( void )
{
RFState = RF_STATE_IDLE;
SX1276FskSetDefaults( );
SX1276ReadBuffer( REG_OPMODE, SX1276Regs + 1, 0x70 - 1 );
// Set the device in FSK mode and Sleep Mode
SX1276->RegOpMode = RF_OPMODE_MODULATIONTYPE_FSK | RF_OPMODE_SLEEP;
SX1276Write( REG_OPMODE, SX1276->RegOpMode );
SX1276->RegPaRamp = RF_PARAMP_MODULATIONSHAPING_01;
SX1276Write( REG_PARAMP, SX1276->RegPaRamp );
SX1276->RegLna = RF_LNA_GAIN_G1;
SX1276Write( REG_LNA, SX1276->RegLna );
if( FskSettings.AfcOn == true )
{
SX1276->RegRxConfig = RF_RXCONFIG_RESTARTRXONCOLLISION_OFF | RF_RXCONFIG_AFCAUTO_ON |
RF_RXCONFIG_AGCAUTO_ON | RF_RXCONFIG_RXTRIGER_PREAMBLEDETECT;
}
else
{
SX1276->RegRxConfig = RF_RXCONFIG_RESTARTRXONCOLLISION_OFF | RF_RXCONFIG_AFCAUTO_OFF |
RF_RXCONFIG_AGCAUTO_ON | RF_RXCONFIG_RXTRIGER_PREAMBLEDETECT;
}
SX1276->RegPreambleLsb = 8;
SX1276->RegPreambleDetect = RF_PREAMBLEDETECT_DETECTOR_ON | RF_PREAMBLEDETECT_DETECTORSIZE_2 |
RF_PREAMBLEDETECT_DETECTORTOL_10;
SX1276->RegRssiThresh = 0xFF;
SX1276->RegSyncConfig = RF_SYNCCONFIG_AUTORESTARTRXMODE_WAITPLL_ON | RF_SYNCCONFIG_PREAMBLEPOLARITY_AA |
RF_SYNCCONFIG_SYNC_ON |
RF_SYNCCONFIG_SYNCSIZE_4;
SX1276->RegSyncValue1 = 0x69;
SX1276->RegSyncValue2 = 0x81;
SX1276->RegSyncValue3 = 0x7E;
SX1276->RegSyncValue4 = 0x96;
SX1276->RegPacketConfig1 = RF_PACKETCONFIG1_PACKETFORMAT_VARIABLE | RF_PACKETCONFIG1_DCFREE_OFF |
( FskSettings.CrcOn << 4 ) | RF_PACKETCONFIG1_CRCAUTOCLEAR_ON |
RF_PACKETCONFIG1_ADDRSFILTERING_OFF | RF_PACKETCONFIG1_CRCWHITENINGTYPE_CCITT;
SX1276FskGetPacketCrcOn( ); // Update CrcOn on FskSettings
SX1276->RegPayloadLength = FskSettings.PayloadLength;
// we can now update the registers with our configuration
SX1276WriteBuffer( REG_OPMODE, SX1276Regs + 1, 0x70 - 1 );
// then we need to set the RF settings
SX1276FskSetRFFrequency( FskSettings.RFFrequency );
SX1276FskSetBitrate( FskSettings.Bitrate );
SX1276FskSetFdev( FskSettings.Fdev );
SX1276FskSetDccBw( &SX1276->RegRxBw, 0, FskSettings.RxBw );
SX1276FskSetDccBw( &SX1276->RegAfcBw, 0, FskSettings.RxBwAfc );
SX1276FskSetRssiOffset( 0 );
#if( ( MODULE_SX1276RF1IAS == 1 ) || ( MODULE_SX1276RF1KAS == 1 ) )
if( FskSettings.RFFrequency > 860000000 )
{
SX1276FskSetPAOutput( RF_PACONFIG_PASELECT_RFO );
SX1276FskSetPa20dBm( false );
FskSettings.Power = 14;
SX1276FskSetRFPower( FskSettings.Power );
}
else
{
SX1276FskSetPAOutput( RF_PACONFIG_PASELECT_PABOOST );
SX1276FskSetPa20dBm( true );
FskSettings.Power = 20;
SX1276FskSetRFPower( FskSettings.Power );
}
#elif( MODULE_SX1276RF1JAS == 1 )
if( FskSettings.RFFrequency > 860000000 )
{
SX1276FskSetPAOutput( RF_PACONFIG_PASELECT_PABOOST );
SX1276FskSetPa20dBm( true );
FskSettings.Power = 20;
SX1276FskSetRFPower( FskSettings.Power );
}
else
{
SX1276FskSetPAOutput( RF_PACONFIG_PASELECT_RFO );
SX1276FskSetPa20dBm( false );
FskSettings.Power = 14;
SX1276FskSetRFPower( FskSettings.Power );
}
#endif
SX1276FskSetOpMode( RF_OPMODE_STANDBY );
// Calibrate the HF
SX1276FskRxCalibrate( );
}
void SX1276FskSetDefaults( void )
{
// REMARK: See SX1276 datasheet for modified default values.
SX1276Read( REG_VERSION, &SX1276->RegVersion );
}
void SX1276FskSetOpMode( uint8_t opMode )
{
static uint8_t opModePrev = RF_OPMODE_STANDBY;
static bool antennaSwitchTxOnPrev = true;
bool antennaSwitchTxOn = false;
opModePrev = SX1276->RegOpMode & ~RF_OPMODE_MASK;
if( opMode != opModePrev )
{
if( opMode == RF_OPMODE_TRANSMITTER )
{
antennaSwitchTxOn = true;
}
else
{
antennaSwitchTxOn = false;
}
if( antennaSwitchTxOn != antennaSwitchTxOnPrev )
{
antennaSwitchTxOnPrev = antennaSwitchTxOn;