forked from xuos/xiuos
				
			Merge branch 'prepare_for_master' of https://gitlink.org.cn/xuos/xiuos into prepare_for_master
This commit is contained in:
		
						commit
						1d582ebcfd
					
				| 
						 | 
				
			
			@ -22,9 +22,6 @@
 | 
			
		|||
[submodule "Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong-riscv64/kendryte-sdk/kendryte-sdk-source"]
 | 
			
		||||
	path = Ubiquitous/RT-Thread_Fusion_XiUOS/aiit_board/xidatong-riscv64/kendryte-sdk/kendryte-sdk-source
 | 
			
		||||
	url = https://www.gitlink.org.cn/chunyexixiaoyu/kendryte-sdk-source.git
 | 
			
		||||
[submodule "APP_Framework/lib/lorawan/lora_radio_driver"]
 | 
			
		||||
	path = APP_Framework/lib/lorawan/lora_radio_driver
 | 
			
		||||
	url = https://gitlink.org.cn/xuos/lora_radio_driver
 | 
			
		||||
[submodule "APP_Framework/lib/lorawan/lorawan_devicenode"]
 | 
			
		||||
	path = APP_Framework/lib/lorawan/lorawan_devicenode
 | 
			
		||||
	url = https://gitlink.org.cn/xuos/lorawan_devicenode.git
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,7 +22,7 @@
 | 
			
		|||
#include <transform.h>
 | 
			
		||||
#ifdef ADD_XIZI_FEATURES
 | 
			
		||||
 | 
			
		||||
#define BSP_LED_PIN 29
 | 
			
		||||
#define BSP_LED_PIN 134
 | 
			
		||||
#define NULL_PARAMETER 0
 | 
			
		||||
 | 
			
		||||
static uint16_t pin_fd=0;
 | 
			
		||||
| 
						 | 
				
			
			@ -30,6 +30,7 @@ static struct PinStat pin_led;
 | 
			
		|||
 | 
			
		||||
void LedFlip(void *parameter)
 | 
			
		||||
{
 | 
			
		||||
    printf("%s val %d time %d\n", __func__, pin_led.val, PrivGetTickTime());
 | 
			
		||||
    pin_led.pin = BSP_LED_PIN;
 | 
			
		||||
    pin_led.val = !pin_led.val;
 | 
			
		||||
    PrivWrite(pin_fd, &pin_led, NULL_PARAMETER);
 | 
			
		||||
| 
						 | 
				
			
			@ -37,7 +38,7 @@ void LedFlip(void *parameter)
 | 
			
		|||
 | 
			
		||||
void TestHwTimer(void)
 | 
			
		||||
{
 | 
			
		||||
    x_ticks_t period = 1;
 | 
			
		||||
    uint32_t period_ms = 500;
 | 
			
		||||
    
 | 
			
		||||
    pin_fd = PrivOpen(HWTIMER_PIN_DEV_DRIVER, O_RDWR);
 | 
			
		||||
    if(pin_fd<0) {
 | 
			
		||||
| 
						 | 
				
			
			@ -75,7 +76,7 @@ void TestHwTimer(void)
 | 
			
		|||
        return;
 | 
			
		||||
    }    
 | 
			
		||||
 | 
			
		||||
    ioctl_cfg.args = (void *).
 | 
			
		||||
    ioctl_cfg.args = (void *)&period_ms;
 | 
			
		||||
    if (0 != PrivIoctl(timer_fd, OPE_CFG, &ioctl_cfg)) {
 | 
			
		||||
        printf("timer pin fd error %d\n", pin_fd);
 | 
			
		||||
        PrivClose(pin_fd);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,9 +22,13 @@ extern void ApplicationOtaTaskInit(void);
 | 
			
		|||
extern int OtaTask(void);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_MONGOOSE
 | 
			
		||||
extern int webserver(void);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
int main(void)
 | 
			
		||||
{
 | 
			
		||||
    printf("Hello, world! \n");
 | 
			
		||||
    printf("\nHello, world!\n");
 | 
			
		||||
    FrameworkInit();
 | 
			
		||||
#ifdef APPLICATION_OTA
 | 
			
		||||
    ApplicationOtaTaskInit();
 | 
			
		||||
| 
						 | 
				
			
			@ -33,6 +37,11 @@ int main(void)
 | 
			
		|||
#ifdef OTA_BY_PLATFORM
 | 
			
		||||
    OtaTask();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_MONGOOSE
 | 
			
		||||
    webserver();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
// int cppmain(void);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,11 @@
 | 
			
		|||
menuconfig USE_MONGOOSE
 | 
			
		||||
    bool "Use mongoose as webserver"
 | 
			
		||||
    default n
 | 
			
		||||
    select BSP_USING_SDIO
 | 
			
		||||
    select BSP_USING_W5500
 | 
			
		||||
    select BSP_USING_ETHERNET
 | 
			
		||||
    select SUPPORT_CONNECTION_FRAMEWORK
 | 
			
		||||
    select CONNECTION_ADAPTER_4G
 | 
			
		||||
    select ADAPTER_EC200A
 | 
			
		||||
    select LIB_USING_LORAWAN
 | 
			
		||||
    select LIB_USING_LORA_RADIO
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,3 @@
 | 
			
		|||
SRC_FILES += project.c
 | 
			
		||||
SRC_FILES += webserver_project.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
mongoose.a基于工具链arm-none-eabi-gcc (15:6.3.1+svn253039-1build1) 6.3.1 20170620编译而来,若使用的arm工具链版本存在差异或使用的是RISC-V工具链,则需重新编译mongoose.a,避免遇到异常问题。
 | 
			
		||||
| 
						 | 
				
			
			@ -1,3 +0,0 @@
 | 
			
		|||
SRC_FILES += mongoose.c 
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -1,142 +0,0 @@
 | 
			
		|||
// Copyright (c) 2022 Cesanta Software Limited
 | 
			
		||||
// All rights reserved
 | 
			
		||||
//
 | 
			
		||||
// UI example
 | 
			
		||||
// It implements the following endpoints:
 | 
			
		||||
//    /api/config/get - respond with current config
 | 
			
		||||
//    /api/config/set - POST a config change
 | 
			
		||||
//    any other URI serves static files from s_root_dir
 | 
			
		||||
// Data and results are JSON strings
 | 
			
		||||
 | 
			
		||||
#include "ip_addr.h"
 | 
			
		||||
#include "mongoose.h"
 | 
			
		||||
#include "netdev.h"
 | 
			
		||||
 | 
			
		||||
char index_path[] = "login.html";
 | 
			
		||||
 | 
			
		||||
static const char* s_http_addr = "http://192.168.131.88:8000"; // HTTP port
 | 
			
		||||
static const char* s_root_dir = "webserver";
 | 
			
		||||
static const char* s_enable_hexdump = "no";
 | 
			
		||||
static const char* s_ssi_pattern = "#.html";
 | 
			
		||||
 | 
			
		||||
static const char* device_type = "Edu-ARM32";
 | 
			
		||||
static const char* web_version = "XUOS Webserver 1.0";
 | 
			
		||||
static int enable_4g = 0;
 | 
			
		||||
 | 
			
		||||
static struct netdev* p_netdev;
 | 
			
		||||
 | 
			
		||||
static struct config {
 | 
			
		||||
    char *ip, *mask, *gw, *dns;
 | 
			
		||||
} s_config;
 | 
			
		||||
 | 
			
		||||
// Try to update a single configuration value
 | 
			
		||||
static void update_config(struct mg_str json, const char* path, char** value)
 | 
			
		||||
{
 | 
			
		||||
    char* jval;
 | 
			
		||||
    if ((jval = mg_json_get_str(json, path)) != NULL) {
 | 
			
		||||
        free(*value);
 | 
			
		||||
        *value = strdup(jval);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data)
 | 
			
		||||
{
 | 
			
		||||
    if (ev == MG_EV_HTTP_MSG) {
 | 
			
		||||
        struct mg_http_message *hm = (struct mg_http_message*)ev_data, tmp = { 0 };
 | 
			
		||||
        if (mg_http_match_uri(hm, "/getSystemInfo")) {
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n",
 | 
			
		||||
                "{%m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%d}\n",
 | 
			
		||||
                MG_ESC("ip"), MG_ESC(s_config.ip),
 | 
			
		||||
                MG_ESC("deviceType"), MG_ESC(device_type),
 | 
			
		||||
                MG_ESC("deviceNo"), MG_ESC("0"),
 | 
			
		||||
                MG_ESC("systemTime"), MG_ESC("YYYY:MM:DD hh:mm:ss"),
 | 
			
		||||
                MG_ESC("webVersion"), MG_ESC(web_version),
 | 
			
		||||
                MG_ESC("statusOf4g"), enable_4g);
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/get4gInfo")) {
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n",
 | 
			
		||||
                "{%m:%m}\n",
 | 
			
		||||
                MG_ESC("enable4g"), MG_ESC(enable_4g));
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/set4gInfo")) {
 | 
			
		||||
            struct mg_str json = hm->body;
 | 
			
		||||
            enable_4g = mg_json_get_long(json, "$.enable4g", 0);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
            printf("Get enable 4g setting: %d\n", enable_4g);
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/getEthernetInfo")) {
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n",
 | 
			
		||||
                "{%m:%m, %m:%m, %m:%m, %m:%m}\n",
 | 
			
		||||
                MG_ESC("ip"), MG_ESC(s_config.ip),
 | 
			
		||||
                MG_ESC("netmask"), MG_ESC(s_config.mask),
 | 
			
		||||
                MG_ESC("gateway"), MG_ESC(s_config.gw),
 | 
			
		||||
                MG_ESC("dns"), MG_ESC(s_config.dns));
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/setEthernetInfo")) {
 | 
			
		||||
            struct mg_str json = hm->body;
 | 
			
		||||
            printf("json: %s\n", json.ptr);
 | 
			
		||||
            update_config(json, "$.ip", &s_config.ip);
 | 
			
		||||
            update_config(json, "$.netmask", &s_config.mask);
 | 
			
		||||
            update_config(json, "$.gateway", &s_config.gw);
 | 
			
		||||
            update_config(json, "$.dns", &s_config.dns);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
 | 
			
		||||
            ip_addr_t ipaddr, maskaddr, gwaddr;
 | 
			
		||||
            inet_aton(s_config.ip, &ipaddr);
 | 
			
		||||
            inet_aton(s_config.mask, &maskaddr);
 | 
			
		||||
            inet_aton(s_config.gw, &gwaddr);
 | 
			
		||||
            p_netdev->ops->set_addr_info(p_netdev, &ipaddr, &maskaddr, &gwaddr);
 | 
			
		||||
 | 
			
		||||
            printf("Board Net Configuration changed to [IP: %s, Mask: %s, GW: %s, DNS: %s]\n",
 | 
			
		||||
                s_config.ip,
 | 
			
		||||
                s_config.mask,
 | 
			
		||||
                s_config.gw,
 | 
			
		||||
                s_config.dns);
 | 
			
		||||
        } else {
 | 
			
		||||
            struct mg_str unknown = mg_str_n("?", 1), *cl;
 | 
			
		||||
            struct mg_http_serve_opts opts = { .root_dir = s_root_dir, .ssi_pattern = s_ssi_pattern };
 | 
			
		||||
            mg_http_serve_dir(c, hm, &opts);
 | 
			
		||||
            mg_http_parse((char*)c->send.buf, c->send.len, &tmp);
 | 
			
		||||
            cl = mg_http_get_header(&tmp, "Content-Length");
 | 
			
		||||
            if (cl == NULL)
 | 
			
		||||
                cl = &unknown;
 | 
			
		||||
            MG_INFO(("%.*s %.*s %.*s %.*s", (int)hm->method.len, hm->method.ptr,
 | 
			
		||||
                (int)hm->uri.len, hm->uri.ptr, (int)tmp.uri.len, tmp.uri.ptr,
 | 
			
		||||
                (int)cl->len, cl->ptr));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    (void)fn_data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void* do_webserver_demo(void* none)
 | 
			
		||||
{
 | 
			
		||||
    p_netdev = netdev_get_by_name("wz");
 | 
			
		||||
    if (p_netdev == NULL) {
 | 
			
		||||
        MG_INFO(("Did nto find wz netdev, use default.\n"));
 | 
			
		||||
        p_netdev = NETDEV_DEFAULT;
 | 
			
		||||
    }
 | 
			
		||||
    MG_INFO(("Use Netdev %s", p_netdev->name));
 | 
			
		||||
    s_config.ip = strdup(inet_ntoa(*p_netdev->ip_addr));
 | 
			
		||||
    s_config.mask = strdup(inet_ntoa(*p_netdev->netmask));
 | 
			
		||||
    s_config.gw = strdup(inet_ntoa(*p_netdev->gw));
 | 
			
		||||
    s_config.dns = strdup(inet_ntoa(p_netdev->dns_servers[0]));
 | 
			
		||||
 | 
			
		||||
    struct mg_mgr mgr; // Event manager
 | 
			
		||||
    // mg_log_set(MG_LL_INFO); // Set to 3 to enable debug
 | 
			
		||||
    mg_log_set(MG_LL_DEBUG); // Set to 3 to enable debug
 | 
			
		||||
    mg_mgr_init(&mgr); // Initialise event manager
 | 
			
		||||
    mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener
 | 
			
		||||
    for (;;)
 | 
			
		||||
        mg_mgr_poll(&mgr, 50); // Infinite event loop
 | 
			
		||||
    mg_mgr_free(&mgr);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int webserver_demo(int argc, char* argv[])
 | 
			
		||||
{
 | 
			
		||||
    pthread_t tid = -1;
 | 
			
		||||
    pthread_attr_t attr;
 | 
			
		||||
    attr.schedparam.sched_priority = 30;
 | 
			
		||||
    attr.stacksize = 0x2000;
 | 
			
		||||
 | 
			
		||||
    PrivTaskCreate(&tid, &attr, do_webserver_demo, NULL);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(5), Webserver, webserver_demo, webserver for project);
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,723 @@
 | 
			
		|||
// Copyright (c) 2022 Cesanta Software Limited
 | 
			
		||||
// All rights reserved
 | 
			
		||||
//
 | 
			
		||||
// UI example
 | 
			
		||||
// It implements the following endpoints:
 | 
			
		||||
//    /api/config/get - respond with current config
 | 
			
		||||
//    /api/config/set - POST a config change
 | 
			
		||||
//    any other URI serves static files from s_root_dir
 | 
			
		||||
// Data and results are JSON strings
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file webserver_project.c
 | 
			
		||||
* @brief support webserver_project for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-11-07
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: webserver_project.c
 | 
			
		||||
Description: support webserver_project for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-11-07
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、support xishutong-arm32 board, using W5500 to support webserver.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#include "ip_addr.h"
 | 
			
		||||
#include "mongoose.h"
 | 
			
		||||
#include "netdev.h"
 | 
			
		||||
#include <sys_arch.h>
 | 
			
		||||
#include <lwip/sockets.h>
 | 
			
		||||
#include "lwip/sys.h"
 | 
			
		||||
#include <adapter.h>
 | 
			
		||||
 | 
			
		||||
/*******************************************************************************
 | 
			
		||||
 * Local variable definitions ('static')
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
char index_path[] = "login.html";
 | 
			
		||||
 | 
			
		||||
static const char* s_http_addr = "http://192.168.131.88:8000"; // HTTP port
 | 
			
		||||
static const char* s_root_dir = "webserver";
 | 
			
		||||
static const char* s_enable_hexdump = "no";
 | 
			
		||||
static const char* s_ssi_pattern = "#.html";
 | 
			
		||||
static const char* web_version = "XiUOS WebServer 1.0";
 | 
			
		||||
 | 
			
		||||
static struct netdev* p_netdev_webserver;
 | 
			
		||||
static struct netdev* p_netdev_ethernet;
 | 
			
		||||
static pthread_t tid;
 | 
			
		||||
 | 
			
		||||
#define WB_EVENT_TASK_STACK_SIZE      4096
 | 
			
		||||
#define WB_EVENT_TASK_PRIO            20
 | 
			
		||||
 | 
			
		||||
#define WB_4G_CONNECT                 0x0001
 | 
			
		||||
#define WB_4G_DISCONNECT              0x0002
 | 
			
		||||
#define WB_MQTT_CONNECT               0x0004
 | 
			
		||||
#define WB_MQTT_DISCONNECT            0x0008
 | 
			
		||||
#define WB_LORA_CONNECT               0x0010
 | 
			
		||||
#define WB_LORA_DISCONNECT            0x0020
 | 
			
		||||
#define WB_ETHERNET_CONNECT           0x0040
 | 
			
		||||
#define WB_ETHERNET_DISCONNECT        0x0080
 | 
			
		||||
#define WB_EVENT_ALL                 (WB_4G_CONNECT | WB_4G_DISCONNECT |            \
 | 
			
		||||
                                      WB_MQTT_CONNECT | WB_MQTT_DISCONNECT |        \
 | 
			
		||||
                                      WB_LORA_CONNECT | WB_LORA_DISCONNECT |        \
 | 
			
		||||
                                      WB_ETHERNET_CONNECT | WB_ETHERNET_DISCONNECT) 
 | 
			
		||||
 | 
			
		||||
static int wb_event;
 | 
			
		||||
static unsigned int status = 0;
 | 
			
		||||
static pthread_t wb_event_task;
 | 
			
		||||
 | 
			
		||||
/*define device info*/
 | 
			
		||||
static const char* device_name = "矽数通4G"; 
 | 
			
		||||
static const char* device_type = "xishutong-arm32";
 | 
			
		||||
static const char* device_serial_num = "123456789";
 | 
			
		||||
 | 
			
		||||
/*define webserver info*/
 | 
			
		||||
static struct webserver_config {
 | 
			
		||||
    char *ip, *mask, *gw, *dns;
 | 
			
		||||
} webserver_config;
 | 
			
		||||
 | 
			
		||||
/*define interface info*/
 | 
			
		||||
static struct rs485_config {
 | 
			
		||||
    int baud_rate;
 | 
			
		||||
    int data_bit;
 | 
			
		||||
    int stop_bit;
 | 
			
		||||
    int parity;
 | 
			
		||||
} rs485_config;
 | 
			
		||||
int rs485_uart_fd = -1;
 | 
			
		||||
 | 
			
		||||
#define RS485_DEVICE_PATH "/dev/usart4_dev4"
 | 
			
		||||
 | 
			
		||||
/*define net 4G info*/
 | 
			
		||||
static struct net_4g_info {
 | 
			
		||||
    char map_ip[20];
 | 
			
		||||
    char connect_ip[20];
 | 
			
		||||
    char operator[20];
 | 
			
		||||
    char signal_strength[20];
 | 
			
		||||
    char connect_port[20];
 | 
			
		||||
} net_4g_info;
 | 
			
		||||
 | 
			
		||||
struct Adapter* adapter;
 | 
			
		||||
 | 
			
		||||
static struct net_4g_mqtt_info {
 | 
			
		||||
    char topic[20];
 | 
			
		||||
    char username[20];
 | 
			
		||||
    char password[20];
 | 
			
		||||
    int client_id;
 | 
			
		||||
    int connect_status;
 | 
			
		||||
} net_4g_mqtt_info;
 | 
			
		||||
 | 
			
		||||
/*define net LoRa info*/
 | 
			
		||||
struct net_lora_info 
 | 
			
		||||
{
 | 
			
		||||
    uint32_t frequency;         // frequency
 | 
			
		||||
    uint8_t sf;                 // spreadfactor
 | 
			
		||||
    uint8_t bw;                 // bandwidth
 | 
			
		||||
 | 
			
		||||
    uint32_t connect_status;    //connect status
 | 
			
		||||
    uint8_t lora_init_flag;     //if 1 means already init
 | 
			
		||||
} net_lora_info;
 | 
			
		||||
 | 
			
		||||
/*define net Ethernet info*/
 | 
			
		||||
static struct net_ethernet_info {
 | 
			
		||||
    char ethernetIp[20];
 | 
			
		||||
    char ethernetNetmask[20];
 | 
			
		||||
    char ethernetGateway[20];
 | 
			
		||||
    char ethernetDNS[20];
 | 
			
		||||
    char targetIp[20];
 | 
			
		||||
    char targetPort[20];
 | 
			
		||||
    char targetGateway[20];
 | 
			
		||||
    char targetDNS[20];
 | 
			
		||||
    int connect_status;
 | 
			
		||||
} net_ethernet_info;
 | 
			
		||||
 | 
			
		||||
static int socket_fd = -1;
 | 
			
		||||
 | 
			
		||||
static char tcp_ethernet_ipaddr[] = {192, 168, 130, 77};
 | 
			
		||||
static char tcp_ethernet_netmask[] = {255, 255, 254, 0};
 | 
			
		||||
static char tcp_ethernet_gwaddr[] = {192, 168, 130, 1};
 | 
			
		||||
 | 
			
		||||
static uint16_t tcp_socket_port = 8888;
 | 
			
		||||
 | 
			
		||||
/*define PLC info*/
 | 
			
		||||
static char *plc_json;
 | 
			
		||||
#define JSON_FILE_NAME "test_recipe.json"
 | 
			
		||||
 | 
			
		||||
/*******************************************************************************
 | 
			
		||||
 * Function implementation - define interface info
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
static void Rs485InitConfigure(void)
 | 
			
		||||
{
 | 
			
		||||
    rs485_uart_fd = PrivOpen(RS485_DEVICE_PATH, O_RDWR);
 | 
			
		||||
    if (rs485_uart_fd < 0) {
 | 
			
		||||
        printf("open rs485 %s fd error:%d\n", RS485_DEVICE_PATH, rs485_uart_fd);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    printf("uart %s open success\n", RS485_DEVICE_PATH);
 | 
			
		||||
 | 
			
		||||
    struct SerialDataCfg uart_cfg;
 | 
			
		||||
    memset(&uart_cfg, 0, sizeof(struct SerialDataCfg));
 | 
			
		||||
 | 
			
		||||
    rs485_config.baud_rate = BAUD_RATE_115200;
 | 
			
		||||
    rs485_config.data_bit = DATA_BITS_8;
 | 
			
		||||
    rs485_config.stop_bit = STOP_BITS_1;
 | 
			
		||||
    rs485_config.parity = PARITY_NONE;
 | 
			
		||||
 | 
			
		||||
    uart_cfg.serial_baud_rate = rs485_config.baud_rate;
 | 
			
		||||
    uart_cfg.serial_data_bits = rs485_config.data_bit;
 | 
			
		||||
    uart_cfg.serial_stop_bits = rs485_config.stop_bit;
 | 
			
		||||
    uart_cfg.serial_parity_mode = rs485_config.parity;
 | 
			
		||||
    uart_cfg.serial_bit_order = BIT_ORDER_LSB;
 | 
			
		||||
    uart_cfg.serial_invert_mode = NRZ_NORMAL;
 | 
			
		||||
    uart_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
 | 
			
		||||
    uart_cfg.serial_timeout = -1;
 | 
			
		||||
    uart_cfg.is_ext_uart = 0;
 | 
			
		||||
    uart_cfg.dev_recv_callback = NULL;
 | 
			
		||||
 | 
			
		||||
    struct PrivIoctlCfg ioctl_cfg;
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
 | 
			
		||||
    ioctl_cfg.args = (void *)&uart_cfg;
 | 
			
		||||
 | 
			
		||||
    if (0 != PrivIoctl(rs485_uart_fd, OPE_INT, &ioctl_cfg)) {
 | 
			
		||||
        printf("ioctl uart fd error %d\n", rs485_uart_fd);
 | 
			
		||||
        PrivClose(rs485_uart_fd);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void Rs485Configure(int baud_rate, int data_bit, int stop_bit, int parity)
 | 
			
		||||
{
 | 
			
		||||
    if (rs485_uart_fd < 0) {
 | 
			
		||||
        rs485_uart_fd = PrivOpen(RS485_DEVICE_PATH, O_RDWR);
 | 
			
		||||
        if (rs485_uart_fd < 0) {
 | 
			
		||||
            printf("open rs485 %s fd error:%d\n", RS485_DEVICE_PATH, rs485_uart_fd);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        printf("uart %s open success\n", RS485_DEVICE_PATH);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    struct SerialDataCfg uart_cfg;
 | 
			
		||||
    memset(&uart_cfg, 0, sizeof(struct SerialDataCfg));
 | 
			
		||||
 | 
			
		||||
    uart_cfg.serial_baud_rate = baud_rate;
 | 
			
		||||
    uart_cfg.serial_data_bits = data_bit;
 | 
			
		||||
    uart_cfg.serial_stop_bits = stop_bit;
 | 
			
		||||
    uart_cfg.serial_parity_mode = parity;
 | 
			
		||||
    uart_cfg.serial_bit_order = BIT_ORDER_LSB;
 | 
			
		||||
    uart_cfg.serial_invert_mode = NRZ_NORMAL;
 | 
			
		||||
    uart_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
 | 
			
		||||
    uart_cfg.serial_timeout = -1;
 | 
			
		||||
    uart_cfg.is_ext_uart = 0;
 | 
			
		||||
    uart_cfg.dev_recv_callback = NULL;
 | 
			
		||||
 | 
			
		||||
    struct PrivIoctlCfg ioctl_cfg;
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
 | 
			
		||||
    ioctl_cfg.args = (void *)&uart_cfg;
 | 
			
		||||
 | 
			
		||||
    if (0 != PrivIoctl(rs485_uart_fd, OPE_INT, &ioctl_cfg)) {
 | 
			
		||||
        printf("ioctl uart fd error %d\n", rs485_uart_fd);
 | 
			
		||||
        PrivClose(rs485_uart_fd);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    printf("Board RS485 changed to [br: %d, data: %d, stop: %d, party: %d]\n",
 | 
			
		||||
        baud_rate, data_bit, stop_bit, parity);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*******************************************************************************
 | 
			
		||||
 * Function implementation - define net 4G info
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
static void Net4gGetInfo(char *ip, char *operator, char *signal_strength)
 | 
			
		||||
{
 | 
			
		||||
    //to do
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void Net4gConnect(void)
 | 
			
		||||
{
 | 
			
		||||
    int ec200a_baud_rate = 115200;
 | 
			
		||||
    const char *send_msg = "Adapter_4G Test";
 | 
			
		||||
    adapter->socket.socket_id = 0;
 | 
			
		||||
 | 
			
		||||
    AdapterDeviceOpen(adapter);
 | 
			
		||||
    AdapterDeviceControl(adapter, OPE_INT, &ec200a_baud_rate);
 | 
			
		||||
    AdapterDeviceConnect(adapter, CLIENT, net_4g_info.connect_ip, net_4g_info.connect_port, IPV4);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void Net4gDisconnect(void)
 | 
			
		||||
{
 | 
			
		||||
    uint8_t priv_net_group = IP_PROTOCOL;
 | 
			
		||||
    AdapterDeviceDisconnect(adapter, &priv_net_group);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*******************************************************************************
 | 
			
		||||
 * Function implementation - define net 4G MQTT info
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
static void NetMqttConnect(void)
 | 
			
		||||
{
 | 
			
		||||
    //to do
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void NetMqttDisconnect(void)
 | 
			
		||||
{
 | 
			
		||||
    //to do
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*******************************************************************************
 | 
			
		||||
 * Function implementation - define net LoRa info
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
static void NetLoraConnect(void)
 | 
			
		||||
{
 | 
			
		||||
    char* init_params[2] = {"TestLoraRadio", "probe"};
 | 
			
		||||
    char* tx_params[4] = {"TestLoraRadio", "tx", "1", "2000"};
 | 
			
		||||
    extern int TestLoraRadio(int argc, char *argv[]);
 | 
			
		||||
 | 
			
		||||
    if (0 == net_lora_info.lora_init_flag) {
 | 
			
		||||
        TestLoraRadio(2, init_params);
 | 
			
		||||
        net_lora_info.lora_init_flag = 1;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    TestLoraRadio(4, tx_params);
 | 
			
		||||
    net_lora_info.connect_status = 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void NetLoraDisconnect(void)
 | 
			
		||||
{
 | 
			
		||||
    char* disconnect_params[2] = {"TestLoraRadio", "txdone"};
 | 
			
		||||
    extern int TestLoraRadio(int argc, char *argv[]);
 | 
			
		||||
    TestLoraRadio(2, disconnect_params);
 | 
			
		||||
    net_lora_info.connect_status = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*******************************************************************************
 | 
			
		||||
 * Function implementation - define net Ethernet info
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
static void TcpClientConnect(void)
 | 
			
		||||
{
 | 
			
		||||
    int cnt = 20;
 | 
			
		||||
    int ret;
 | 
			
		||||
    char send_msg[128];
 | 
			
		||||
 | 
			
		||||
    sscanf(net_ethernet_info.targetPort, "%d", &tcp_socket_port);
 | 
			
		||||
 | 
			
		||||
    memset(send_msg, 0, sizeof(send_msg));
 | 
			
		||||
    socket_fd = socket(AF_INET, SOCK_STREAM, 0);
 | 
			
		||||
    if (socket_fd < 0) {
 | 
			
		||||
        printf("Socket error\n");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    struct sockaddr_in tcp_sock;
 | 
			
		||||
    tcp_sock.sin_family = AF_INET;
 | 
			
		||||
    tcp_sock.sin_port = htons(tcp_socket_port);
 | 
			
		||||
    tcp_sock.sin_addr.s_addr = inet_addr(net_ethernet_info.targetIp);
 | 
			
		||||
 | 
			
		||||
    memset(&(tcp_sock.sin_zero), 0, sizeof(tcp_sock.sin_zero));
 | 
			
		||||
 | 
			
		||||
    int keepalive = 1;
 | 
			
		||||
    int keepidle = 30;
 | 
			
		||||
    int keepinterval = 5;
 | 
			
		||||
    int keepcount = 5;
 | 
			
		||||
    setsockopt(socket_fd,
 | 
			
		||||
        IPPROTO_TCP,           /* set option at TCP level */
 | 
			
		||||
        TCP_KEEPALIVE,         /* name of option */
 | 
			
		||||
        (void*)&keepalive,     /* the cast is historical cruft */
 | 
			
		||||
        sizeof(keepalive));    /* length of option value */
 | 
			
		||||
 | 
			
		||||
    setsockopt(socket_fd,
 | 
			
		||||
        IPPROTO_TCP,           /* set option at TCP level */
 | 
			
		||||
        TCP_KEEPIDLE,          /* name of option */
 | 
			
		||||
        (void*)&keepidle,      /* the cast is historical cruft */
 | 
			
		||||
        sizeof(keepidle));     /* length of option value */
 | 
			
		||||
 | 
			
		||||
    setsockopt(socket_fd,
 | 
			
		||||
        IPPROTO_TCP,           /* set option at TCP level */
 | 
			
		||||
        TCP_KEEPINTVL,         /* name of option */
 | 
			
		||||
        (void*)&keepinterval,  /* the cast is historical cruft */
 | 
			
		||||
        sizeof(keepinterval)); /* length of option value */
 | 
			
		||||
 | 
			
		||||
    setsockopt(socket_fd,
 | 
			
		||||
        IPPROTO_TCP,           /* set option at TCP level */
 | 
			
		||||
        TCP_KEEPCNT,           /* name of option */
 | 
			
		||||
        (void*)&keepcount,     /* the cast is historical cruft */
 | 
			
		||||
        sizeof(keepcount));    /* length of option value */
 | 
			
		||||
 | 
			
		||||
    ret = connect(socket_fd, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr));
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        printf("Unable to connect %s:%d = %d\n", net_ethernet_info.targetIp, tcp_socket_port, ret);
 | 
			
		||||
        closesocket(socket_fd);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    printf("TCP connect %s:%d success, start to send.\n", net_ethernet_info.targetIp, tcp_socket_port);
 | 
			
		||||
    net_ethernet_info.connect_status = 1;
 | 
			
		||||
 | 
			
		||||
    while (cnt --) {
 | 
			
		||||
        printf("Lwip client is running.\n");
 | 
			
		||||
        snprintf(send_msg, sizeof(send_msg), "TCP test package times %d\r\n", cnt);
 | 
			
		||||
        send(socket_fd, send_msg, strlen(send_msg), 0);
 | 
			
		||||
        printf("Send tcp msg: %s ", send_msg);
 | 
			
		||||
        PrivTaskDelay(1000);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void TcpClientDisconnect(void)
 | 
			
		||||
{
 | 
			
		||||
    printf("TCP disconnect\n");
 | 
			
		||||
    closesocket(socket_fd);
 | 
			
		||||
    net_ethernet_info.connect_status = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*******************************************************************************
 | 
			
		||||
 * Function implementation - define plc info
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
static void PlcInfoWriteToSd(const char *json)
 | 
			
		||||
{
 | 
			
		||||
    extern int GetSdMountStatus(void);
 | 
			
		||||
    if(GetSdMountStatus()) {
 | 
			
		||||
        KPrintf("------Start download json file !------\r\n");
 | 
			
		||||
 | 
			
		||||
        FILE *fp = fopen(JSON_FILE_NAME, "w");
 | 
			
		||||
        if(fp == NULL) {
 | 
			
		||||
            printf("%s file create failed,please check!\r\n", JSON_FILE_NAME);
 | 
			
		||||
        } else {
 | 
			
		||||
            printf("%s file create success!\r\n", JSON_FILE_NAME); 
 | 
			
		||||
            fprintf(fp, "%s", json);
 | 
			
		||||
            fclose(fp);
 | 
			
		||||
        }
 | 
			
		||||
        printf("------download %s file done!------\r\n", JSON_FILE_NAME);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*******************************************************************************
 | 
			
		||||
 * Function implementation - WebserverEventTask
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
static void *WebserverEventTask(void *arg)
 | 
			
		||||
{
 | 
			
		||||
    wb_event = PrivEventCreate(LINKLIST_FLAG_FIFO);
 | 
			
		||||
    while(1) {
 | 
			
		||||
        if (0 == PrivEventProcess(wb_event, WB_EVENT_ALL, EVENT_OR | EVENT_AUTOCLEAN, 0, &status)) {
 | 
			
		||||
            switch( status ) {
 | 
			
		||||
                case WB_4G_CONNECT:
 | 
			
		||||
                    Net4gConnect();
 | 
			
		||||
                    break;
 | 
			
		||||
                case WB_4G_DISCONNECT:
 | 
			
		||||
                    Net4gDisconnect();
 | 
			
		||||
                    break; 
 | 
			
		||||
                case WB_MQTT_CONNECT:
 | 
			
		||||
                    NetMqttConnect();
 | 
			
		||||
                    break;
 | 
			
		||||
                case WB_MQTT_DISCONNECT:
 | 
			
		||||
                    NetMqttDisconnect();
 | 
			
		||||
                    break; 
 | 
			
		||||
                case WB_LORA_CONNECT:
 | 
			
		||||
                    NetLoraConnect();
 | 
			
		||||
                    break;
 | 
			
		||||
                case WB_LORA_DISCONNECT:
 | 
			
		||||
                    NetLoraDisconnect();
 | 
			
		||||
                    break; 
 | 
			
		||||
                case WB_ETHERNET_CONNECT:
 | 
			
		||||
                    TcpClientConnect();
 | 
			
		||||
                    break;
 | 
			
		||||
                case WB_ETHERNET_DISCONNECT:
 | 
			
		||||
                    TcpClientDisconnect();
 | 
			
		||||
                    break;   
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void WbEventInit(void)
 | 
			
		||||
{
 | 
			
		||||
    char task_name[] = "wb_event_task";
 | 
			
		||||
    pthread_args_t args;
 | 
			
		||||
    args.pthread_name = task_name;
 | 
			
		||||
    
 | 
			
		||||
    pthread_attr_t attr;
 | 
			
		||||
    attr.schedparam.sched_priority = WB_EVENT_TASK_PRIO;
 | 
			
		||||
    attr.stacksize = WB_EVENT_TASK_STACK_SIZE;
 | 
			
		||||
 | 
			
		||||
    PrivTaskCreate(&wb_event_task, &attr, &WebserverEventTask, (void *)&args);
 | 
			
		||||
    PrivTaskStartup(&wb_event_task);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*******************************************************************************
 | 
			
		||||
 * Function implementation - webserver
 | 
			
		||||
 ******************************************************************************/
 | 
			
		||||
// Try to update a single configuration value
 | 
			
		||||
static void update_config(struct mg_str json, const char* path, char** value)
 | 
			
		||||
{
 | 
			
		||||
    char* jval;
 | 
			
		||||
    if ((jval = mg_json_get_str(json, path)) != NULL) {
 | 
			
		||||
        free(*value);
 | 
			
		||||
        *value = strdup(jval);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void update_config_array(struct mg_str json, const char* path, char* value)
 | 
			
		||||
{
 | 
			
		||||
    char* jval;
 | 
			
		||||
    if ((jval = mg_json_get_str(json, path)) != NULL) {
 | 
			
		||||
        sprintf(value, "%s", jval); 
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data)
 | 
			
		||||
{
 | 
			
		||||
    if (ev == MG_EV_HTTP_MSG) {
 | 
			
		||||
        struct mg_http_message *hm = (struct mg_http_message*)ev_data, tmp = { 0 };
 | 
			
		||||
        /*define device info*/
 | 
			
		||||
        if (mg_http_match_uri(hm, "/getSystemInfo")) {
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n",
 | 
			
		||||
                "{%m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%m}\n",
 | 
			
		||||
                MG_ESC("deviceName"), MG_ESC(device_name),
 | 
			
		||||
                MG_ESC("deviceType"), MG_ESC(device_type),
 | 
			
		||||
                MG_ESC("deviceNo"), MG_ESC(device_serial_num),
 | 
			
		||||
                MG_ESC("ip"), MG_ESC(webserver_config.ip),
 | 
			
		||||
                MG_ESC("netmask"), MG_ESC(webserver_config.mask),
 | 
			
		||||
                MG_ESC("gateway"), MG_ESC(webserver_config.gw));
 | 
			
		||||
        } 
 | 
			
		||||
        /*define webserver info*/
 | 
			
		||||
        else if (mg_http_match_uri(hm, "/setNetInfo")) {
 | 
			
		||||
            struct mg_str json = hm->body;
 | 
			
		||||
            printf("json: %s\n", json.ptr);
 | 
			
		||||
            update_config(json, "$.ip", &webserver_config.ip);
 | 
			
		||||
            update_config(json, "$.netmask", &webserver_config.mask);
 | 
			
		||||
            update_config(json, "$.gateway", &webserver_config.gw);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
 | 
			
		||||
            ip_addr_t ipaddr, maskaddr, gwaddr;
 | 
			
		||||
            inet_aton(webserver_config.ip, &ipaddr);
 | 
			
		||||
            inet_aton(webserver_config.mask, &maskaddr);
 | 
			
		||||
            inet_aton(webserver_config.gw, &gwaddr);
 | 
			
		||||
            p_netdev_webserver->ops->set_addr_info(p_netdev_webserver, &ipaddr, &maskaddr, &gwaddr);
 | 
			
		||||
 | 
			
		||||
            printf("Board Webserver Net changed to [IP: %s, Mask: %s, GW: %s]\n",
 | 
			
		||||
                webserver_config.ip,
 | 
			
		||||
                webserver_config.mask,
 | 
			
		||||
                webserver_config.gw);
 | 
			
		||||
        }
 | 
			
		||||
        /*define interface info*/
 | 
			
		||||
        else if (mg_http_match_uri(hm, "/interface/get485Info")) {
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n",
 | 
			
		||||
                "{%m:%d, %m:%d, %m:%d, %m:%d}\n",
 | 
			
		||||
                MG_ESC("baudRate"), rs485_config.baud_rate,
 | 
			
		||||
                MG_ESC("wordLength"), rs485_config.data_bit,
 | 
			
		||||
                MG_ESC("stopBits"), rs485_config.stop_bit,
 | 
			
		||||
                MG_ESC("parity"), rs485_config.parity);
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/interface/set485Info")) {
 | 
			
		||||
            struct mg_str json = hm->body;
 | 
			
		||||
            printf("json: %s\n", json.ptr);
 | 
			
		||||
            rs485_config.baud_rate = mg_json_get_long(json, "$.baudRate", 0);
 | 
			
		||||
            rs485_config.data_bit = mg_json_get_long(json, "$.wordLength", 0);
 | 
			
		||||
            rs485_config.stop_bit = mg_json_get_long(json, "$.stopBits", 0);
 | 
			
		||||
            rs485_config.parity = mg_json_get_long(json, "$.parity", 0);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
 | 
			
		||||
            Rs485Configure(rs485_config.baud_rate, rs485_config.data_bit, rs485_config.stop_bit, rs485_config.parity);
 | 
			
		||||
        }
 | 
			
		||||
        /*define net 4G info*/
 | 
			
		||||
        else if (mg_http_match_uri(hm, "/net/get4gInfo")) {
 | 
			
		||||
 | 
			
		||||
            Net4gGetInfo(net_4g_info.map_ip, net_4g_info.operator, net_4g_info.signal_strength);
 | 
			
		||||
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n",
 | 
			
		||||
                "{%m:%m, %m:%m, %m:%d}\n",
 | 
			
		||||
                MG_ESC("mapIp"), MG_ESC(net_4g_info.map_ip),
 | 
			
		||||
                MG_ESC("operator"), MG_ESC(net_4g_info.operator),
 | 
			
		||||
                MG_ESC("signalIntensity"), MG_ESC(net_4g_info.signal_strength));
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/set4gInfo")) {
 | 
			
		||||
            struct mg_str json = hm->body;
 | 
			
		||||
            update_config_array(json, "$.publicIp", net_4g_info.connect_ip);
 | 
			
		||||
            update_config_array(json, "$.publicPort", net_4g_info.connect_port);
 | 
			
		||||
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/connect4G")) {
 | 
			
		||||
            //enable 4G connect function
 | 
			
		||||
            PrivEvenTrigger(wb_event, WB_4G_CONNECT);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/disconnect4G")) {
 | 
			
		||||
            //disable 4G connect function
 | 
			
		||||
            PrivEvenTrigger(wb_event, WB_4G_CONNECT);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/getMQTTInfo")) {
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n",
 | 
			
		||||
                "{%m:%m, %m:%m, %m:%m, %m:%d, %m:%d}\n",
 | 
			
		||||
                MG_ESC("topic"), MG_ESC(net_4g_mqtt_info.topic),
 | 
			
		||||
                MG_ESC("username"), MG_ESC(net_4g_mqtt_info.username),
 | 
			
		||||
                MG_ESC("password"), MG_ESC(net_4g_mqtt_info.password),
 | 
			
		||||
                MG_ESC("client_id"), net_4g_mqtt_info.client_id,
 | 
			
		||||
                MG_ESC("status"), net_4g_mqtt_info.connect_status);
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/setMQTTInfo")) {
 | 
			
		||||
            struct mg_str json = hm->body;
 | 
			
		||||
            update_config_array(json, "$.topic", net_4g_mqtt_info.topic);
 | 
			
		||||
            update_config_array(json, "$.username", net_4g_mqtt_info.username);
 | 
			
		||||
            update_config_array(json, "$.password", net_4g_mqtt_info.password);
 | 
			
		||||
            net_4g_mqtt_info.client_id = mg_json_get_long(json, "$.client_id", 0);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/connectMQTT")) {
 | 
			
		||||
            //enable 4G MQTT connect function
 | 
			
		||||
            PrivEvenTrigger(wb_event, WB_MQTT_CONNECT);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/disconnectMQTT")) {
 | 
			
		||||
            //disable 4G MQTT connect function
 | 
			
		||||
            PrivEvenTrigger(wb_event, WB_MQTT_DISCONNECT);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        }
 | 
			
		||||
        /*define net LoRa info*/
 | 
			
		||||
        else if (mg_http_match_uri(hm, "/net/getLoraInfo")) {
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n",
 | 
			
		||||
                "{%m:%d, %m:%d, %m:%d, %m:%d}\n",
 | 
			
		||||
                MG_ESC("range"), net_lora_info.frequency,
 | 
			
		||||
                MG_ESC("bandwidth"), net_lora_info.bw,
 | 
			
		||||
                MG_ESC("factor"), net_lora_info.sf,
 | 
			
		||||
                MG_ESC("status"), net_lora_info.connect_status);
 | 
			
		||||
        }
 | 
			
		||||
        else if (mg_http_match_uri(hm, "/net/setLoraInfo")) {
 | 
			
		||||
            struct mg_str json = hm->body;
 | 
			
		||||
            printf("json: %s\n", json.ptr);
 | 
			
		||||
            net_lora_info.frequency = mg_json_get_long(json, "$.range", 0);
 | 
			
		||||
            net_lora_info.sf = mg_json_get_long(json, "$.factor", 0);
 | 
			
		||||
            net_lora_info.bw = mg_json_get_long(json, "$.bandwidth", 0);
 | 
			
		||||
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
 | 
			
		||||
            extern void LoraRadioParamsUpdate(uint32_t frequency, uint8_t sf, uint8_t bw);
 | 
			
		||||
            LoraRadioParamsUpdate(net_lora_info.frequency, net_lora_info.sf, net_lora_info.bw);
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/ConnectLora")) {
 | 
			
		||||
            //enable LoRa connect function
 | 
			
		||||
            PrivEvenTrigger(wb_event, WB_LORA_CONNECT);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/DisonnectLora")) {
 | 
			
		||||
            //disable LoRa connect function
 | 
			
		||||
            PrivEvenTrigger(wb_event, WB_LORA_DISCONNECT);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        }
 | 
			
		||||
        /*define net Ethernet info*/
 | 
			
		||||
        else if (mg_http_match_uri(hm, "/net/getEthernetInfo")) {
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n",
 | 
			
		||||
                "{%m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%d}\n",
 | 
			
		||||
                MG_ESC("ethernetIp"), MG_ESC(net_ethernet_info.ethernetIp),
 | 
			
		||||
                MG_ESC("ethernetNetmask"), MG_ESC(net_ethernet_info.ethernetNetmask),
 | 
			
		||||
                MG_ESC("ethernetGateway"), MG_ESC(net_ethernet_info.ethernetGateway),
 | 
			
		||||
                MG_ESC("ethernetDNS"), MG_ESC(net_ethernet_info.ethernetDNS),
 | 
			
		||||
                MG_ESC("targetIp"), MG_ESC(net_ethernet_info.targetIp),
 | 
			
		||||
                MG_ESC("targetPort"), MG_ESC(net_ethernet_info.targetPort),
 | 
			
		||||
                MG_ESC("targetGateway"), MG_ESC(net_ethernet_info.targetGateway),
 | 
			
		||||
                MG_ESC("targetDNS"), MG_ESC(net_ethernet_info.targetDNS),
 | 
			
		||||
                MG_ESC("ethernetStatus"), net_ethernet_info.connect_status);
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/setEthernetInfo")) {
 | 
			
		||||
            struct mg_str json = hm->body;
 | 
			
		||||
            printf("json: %s\n", json.ptr);
 | 
			
		||||
            update_config_array(json, "$.ethernetIp", net_ethernet_info.ethernetIp);
 | 
			
		||||
            update_config_array(json, "$.ethernetNetmask", net_ethernet_info.ethernetNetmask);
 | 
			
		||||
            update_config_array(json, "$.ethernetGateway", net_ethernet_info.ethernetGateway);
 | 
			
		||||
            update_config_array(json, "$.ethernetDNS", net_ethernet_info.ethernetDNS);
 | 
			
		||||
            update_config_array(json, "$.targetIp", net_ethernet_info.targetIp);
 | 
			
		||||
            update_config_array(json, "$.targetPort", net_ethernet_info.targetPort);
 | 
			
		||||
            update_config_array(json, "$.targetGateway", net_ethernet_info.targetGateway);
 | 
			
		||||
            update_config_array(json, "$.targetDNS", net_ethernet_info.targetDNS);
 | 
			
		||||
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
 | 
			
		||||
            p_netdev_ethernet = netdev_get_by_name("hd");
 | 
			
		||||
            if (p_netdev_ethernet) {
 | 
			
		||||
                ip_addr_t ipaddr, maskaddr, gwaddr;
 | 
			
		||||
                inet_aton(net_ethernet_info.ethernetIp, &ipaddr);
 | 
			
		||||
                inet_aton(net_ethernet_info.ethernetNetmask, &maskaddr);
 | 
			
		||||
                inet_aton(net_ethernet_info.ethernetGateway, &gwaddr);
 | 
			
		||||
                p_netdev_ethernet->ops->set_addr_info(p_netdev_ethernet, &ipaddr, &maskaddr, &gwaddr);
 | 
			
		||||
 | 
			
		||||
                printf("Ethernet Configuration changed to [IP: %s, Mask: %s, GW: %s]\n",
 | 
			
		||||
                    net_ethernet_info.ethernetIp, net_ethernet_info.ethernetNetmask,
 | 
			
		||||
                    net_ethernet_info.ethernetGateway);
 | 
			
		||||
            }
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/connectEthernet")) {
 | 
			
		||||
            //enable Ethernet connect function
 | 
			
		||||
            PrivEvenTrigger(wb_event, WB_ETHERNET_CONNECT);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        } else if (mg_http_match_uri(hm, "/net/disconnectEthernet")) {
 | 
			
		||||
            //disable Ethernet connect function
 | 
			
		||||
            PrivEvenTrigger(wb_event, WB_ETHERNET_DISCONNECT);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        }
 | 
			
		||||
        /*define plc info*/
 | 
			
		||||
        else if (mg_http_match_uri(hm, "/control/setPLCInfo")) {
 | 
			
		||||
            struct mg_str json = hm->body;
 | 
			
		||||
            printf("json: %s\n", json.ptr);
 | 
			
		||||
            PlcInfoWriteToSd(json.ptr);
 | 
			
		||||
            mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
 | 
			
		||||
        } else {
 | 
			
		||||
            struct mg_str unknown = mg_str_n("?", 1), *cl;
 | 
			
		||||
            struct mg_http_serve_opts opts = { .root_dir = s_root_dir, .ssi_pattern = s_ssi_pattern };
 | 
			
		||||
            mg_http_serve_dir(c, hm, &opts);
 | 
			
		||||
            mg_http_parse((char*)c->send.buf, c->send.len, &tmp);
 | 
			
		||||
            cl = mg_http_get_header(&tmp, "Content-Length");
 | 
			
		||||
            if (cl == NULL)
 | 
			
		||||
                cl = &unknown;
 | 
			
		||||
            MG_INFO(("%.*s %.*s %.*s %.*s", (int)hm->method.len, hm->method.ptr,
 | 
			
		||||
                (int)hm->uri.len, hm->uri.ptr, (int)tmp.uri.len, tmp.uri.ptr,
 | 
			
		||||
                (int)cl->len, cl->ptr));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    (void)fn_data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void* do_webserver(void* args)
 | 
			
		||||
{
 | 
			
		||||
    p_netdev_webserver = netdev_get_by_name("wz");
 | 
			
		||||
    if (p_netdev_webserver == NULL) {
 | 
			
		||||
        MG_INFO(("Did not find wz netdev, use default.\n"));
 | 
			
		||||
        p_netdev_webserver = NETDEV_DEFAULT;
 | 
			
		||||
    }
 | 
			
		||||
    MG_INFO(("Webserver Use Netdev %s", p_netdev_webserver->name));
 | 
			
		||||
    webserver_config.ip = strdup(inet_ntoa(*p_netdev_webserver->ip_addr));
 | 
			
		||||
    webserver_config.mask = strdup(inet_ntoa(*p_netdev_webserver->netmask));
 | 
			
		||||
    webserver_config.gw = strdup(inet_ntoa(*p_netdev_webserver->gw));
 | 
			
		||||
    webserver_config.dns = strdup(inet_ntoa(p_netdev_webserver->dns_servers[0]));
 | 
			
		||||
 | 
			
		||||
#ifdef BSP_USING_RS485
 | 
			
		||||
    Rs485InitConfigure();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    adapter =  AdapterDeviceFindByName(ADAPTER_4G_NAME);
 | 
			
		||||
    net_lora_info.lora_init_flag = 0;
 | 
			
		||||
    WbEventInit();
 | 
			
		||||
 | 
			
		||||
    struct mg_mgr mgr; // Event manager
 | 
			
		||||
    // mg_log_set(MG_LL_INFO); // Set to 3 to enable debug
 | 
			
		||||
    mg_log_set(MG_LL_ERROR); // Set to 3 to enable debug
 | 
			
		||||
    mg_mgr_init(&mgr); // Initialise event manager
 | 
			
		||||
    mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener
 | 
			
		||||
    for (;;)
 | 
			
		||||
        mg_mgr_poll(&mgr, 50); // Infinite event loop
 | 
			
		||||
    mg_mgr_free(&mgr);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int webserver(void)
 | 
			
		||||
{
 | 
			
		||||
    char* params[2] = {"LwipNetworkActive", "-a"};
 | 
			
		||||
    extern void LwipNetworkActive(int argc, char* argv[]);
 | 
			
		||||
    LwipNetworkActive(2, params);
 | 
			
		||||
 | 
			
		||||
    pthread_attr_t attr;
 | 
			
		||||
    attr.schedparam.sched_priority = 30;
 | 
			
		||||
    attr.stacksize = 0x4000;
 | 
			
		||||
 | 
			
		||||
    char task_name[] = "do_webserver";
 | 
			
		||||
    pthread_args_t args;
 | 
			
		||||
    args.pthread_name = task_name;
 | 
			
		||||
 | 
			
		||||
    PrivTaskCreate(&tid, &attr, &do_webserver, (void *)&args);
 | 
			
		||||
    PrivTaskStartup(&tid);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(5), 
 | 
			
		||||
    Webserver, webserver, webserver for project);
 | 
			
		||||
| 
						 | 
				
			
			@ -1,8 +1,16 @@
 | 
			
		|||
config ADAPTER_EC200T
 | 
			
		||||
        bool "Using 4G adapter device EC200T"
 | 
			
		||||
        default y
 | 
			
		||||
        default n
 | 
			
		||||
 | 
			
		||||
if ADAPTER_EC200T
 | 
			
		||||
    source "$APP_DIR/Framework/connection/4g/ec200t/Kconfig"
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
config ADAPTER_EC200A
 | 
			
		||||
        bool "Using 4G adapter device EC200A"
 | 
			
		||||
        default n
 | 
			
		||||
 | 
			
		||||
if ADAPTER_EC200A
 | 
			
		||||
    source "$APP_DIR/Framework/connection/4g/ec200a/Kconfig"
 | 
			
		||||
endif
 | 
			
		||||
                
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
config ADAPTER_4G_EC200A
 | 
			
		||||
        string "EC200A adapter name"
 | 
			
		||||
        default "ec200a"
 | 
			
		||||
 | 
			
		||||
if ADD_XIZI_FEATURES
 | 
			
		||||
        config ADAPTER_EC200A_DRIVER
 | 
			
		||||
                string "EC200A device uart driver path"
 | 
			
		||||
                default "/dev/usart6_dev6"
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_FILES := ec200a.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,24 @@
 | 
			
		|||
/*
 | 
			
		||||
* 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 ec200a.c
 | 
			
		||||
 * @brief Implement the connection 4G adapter function, using EC200A device
 | 
			
		||||
 * @version 3.0
 | 
			
		||||
 * @author AIIT XUOS Lab
 | 
			
		||||
 * @date 2023.12.22
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <adapter.h>
 | 
			
		||||
#include <at_agent.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -36,7 +36,7 @@ menuconfig LIB_USING_LORAWAN
 | 
			
		|||
            select BSP_USING_SPI
 | 
			
		||||
 | 
			
		||||
        if LIB_USING_LORA_RADIO
 | 
			
		||||
 | 
			
		||||
            source "$APP_DIR/lib/lorawan/lora_radio_driver/Kconfig"
 | 
			
		||||
        endif
 | 
			
		||||
    endif
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1 +0,0 @@
 | 
			
		|||
Subproject commit 2d8abd3eff2a2d5257e17c7f59acb2ba938cb90e
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,18 @@
 | 
			
		|||
choice
 | 
			
		||||
    prompt "choose rtos for lora radio driver"
 | 
			
		||||
    default LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
 | 
			
		||||
    config LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
        bool "rtos select RT-Thread"
 | 
			
		||||
 | 
			
		||||
    config LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
        bool "rtos select XiUOS"
 | 
			
		||||
endchoice
 | 
			
		||||
 | 
			
		||||
config LORA_RADIO_DRIVER_USING_LORA_RADIO_DEBUG
 | 
			
		||||
    bool "lora radio driver debug printf"
 | 
			
		||||
    default y
 | 
			
		||||
 | 
			
		||||
if LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    source "$APP_DIR/lib/lorawan/lora_radio_driver/ports/lora-module/hc32_adapter/Kconfig"
 | 
			
		||||
endif
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_DIR := lora-radio ports samples
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
# LoRa-Radio-Driver软件包 简介
 | 
			
		||||
  LoRa-Radio-Driver软件包是基于RTOS( RT-Thread、XiUOS ) 实现的LoRa Tranceiver芯片的驱动文件包,可用于快速搭建基于LoRa等通信的应用产品。
 | 
			
		||||
 | 
			
		||||
更多详细信息请查看[LoRa-Radio-Driver/doc](https://github.com/Forest-Rain/lora-radio-driver/tree/master/doc)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,12 @@
 | 
			
		|||
import os
 | 
			
		||||
from building import *
 | 
			
		||||
 | 
			
		||||
objs = []
 | 
			
		||||
cwd  = GetCurrentDir()
 | 
			
		||||
list = os.listdir(cwd)
 | 
			
		||||
 | 
			
		||||
for item in list:
 | 
			
		||||
    if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
 | 
			
		||||
        objs = objs + SConscript(os.path.join(item, 'SConscript'))
 | 
			
		||||
 | 
			
		||||
Return('objs')
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,381 @@
 | 
			
		|||
# LoRa-Radio-Driver软件包使用说明
 | 
			
		||||
 | 
			
		||||
# 1 简介
 | 
			
		||||
LoRa-Radio-Driver软件包是基于RTOS( RT-Thread ) 实现的LoRa Tranceiver芯片(SX126x、SX127x等)的驱动文件,该驱动文件通过SPI访问LoRa Tranceiver芯片,可用于快速搭建基于LoRa等通信的应用产品。
 | 
			
		||||
LoRa-Radio-Driver软件包在LoRaWAN开源协议栈[LoRaMAC-Node中的radio](https://github.com/Forest-Rain/lora-radio-driver/blob/master/doc)基础上,进一步封装实现。
 | 
			
		||||
 | 
			
		||||
> LoRaMac\Radio
 | 
			
		||||
> [https://github.com/Lora-net/LoRaMac-node/tree/master/src/radio](https://github.com/Lora-net/LoRaMac-node/tree/master/src/radio)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
- 主要特点:
 | 
			
		||||
   - 当前支持LoRa Transceiver(sx126x\sx127x )
 | 
			
		||||
      - 支持调制方式
 | 
			
		||||
         - [x] LoRa
 | 
			
		||||
         - [ ] FSK
 | 
			
		||||
   - 可通过EVN工具menuconfig直接定义LoRa模块的对外接口,降低入门门槛
 | 
			
		||||
     - 支持使用引脚号来定义GPIO
 | 
			
		||||
     - 支持使用引脚名来定义GPIO
 | 
			
		||||
   - 提供常用实例代码,可用于射频性能测试、空口数据包监听、双向通信测试等
 | 
			
		||||
   - 可作为phy层对接到LoRaWAN End-Device协议栈
 | 
			
		||||
   - 当前测试的LoRa 模块\芯片
 | 
			
		||||
      - LoRa Transceiver (SPI)
 | 
			
		||||
         - SX126X (SX1262\ASR6500S\LLCC68\SX1268..)
 | 
			
		||||
            - SX1268
 | 
			
		||||
               - [x] [LSD4RF-2R717N40](http://bbs.lierda.com/forum.php?mod=viewthread&tid=87)
 | 
			
		||||
            - SX1262
 | 
			
		||||
             - [x] ASR6500S
 | 
			
		||||
            - LLCC68
 | 
			
		||||
            - LR1110
 | 
			
		||||
         - SX127X (SX1272\SX1276\SX1278..)
 | 
			
		||||
            - SX1278
 | 
			
		||||
               - [x] [LSD4RF-2F717N20](http://bbs.lierda.com/forum.php?mod=viewthread&tid=87)
 | 
			
		||||
               - [x] [Ra-01](http://wiki.ai-thinker.com/lora/man)
 | 
			
		||||
         - [ ] SX1276
 | 
			
		||||
      - LoRa SIP\SoC
 | 
			
		||||
   - 当前测试的MCU平台
 | 
			
		||||
      - LoRa Radio Driver当前功能主要在STM32L平台测试通过,未来计划将适配更多的MCU平台(华大MCU、nRF、BK)
 | 
			
		||||
         - [x] STM32L0系列
 | 
			
		||||
         - [x] STM32L4系列
 | 
			
		||||
   - 当前支持的RTOS
 | 
			
		||||
      - [x] RT-Thread
 | 
			
		||||
      - [ ] RT-Thread Nano
 | 
			
		||||
 | 
			
		||||
# 2 LoRa Radio Driver 软件包组织结构
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
- lora-radio
 | 
			
		||||
   - sx126x
 | 
			
		||||
      - lora-radio-sx126x.c
 | 
			
		||||
         - 对外提供了上层访问接口实现
 | 
			
		||||
      - lora-spi-sx126x.c 
 | 
			
		||||
         - sx126x芯片的spi读写接口实现,独立于MCU平台
 | 
			
		||||
            - [x] rt_device
 | 
			
		||||
            - [ ] SPI裸机方式
 | 
			
		||||
      - sx126x.c
 | 
			
		||||
         - lora芯片sx126x底层驱动
 | 
			
		||||
   - sx127x
 | 
			
		||||
      - lora-radio-sx127x.c 
 | 
			
		||||
         - 对外提供了上层访问接口
 | 
			
		||||
      - lora-spi-sx127x.c
 | 
			
		||||
         - sx127x芯片的spi读写接口实现,独立于MCU平台
 | 
			
		||||
            - [x] rt_device
 | 
			
		||||
            - [ ] SPI裸机方式
 | 
			
		||||
      - sx127x.c
 | 
			
		||||
         - lora芯片sx127x底层驱动
 | 
			
		||||
   - common
 | 
			
		||||
      - lora-radio-timer.c
 | 
			
		||||
         - 提供了lora-radio所需的定时服务接口,用于发送与接收超时等,基于RT-Thread内核rt_timer实现
 | 
			
		||||
            - 注意这种方式提供的定时最小颗粒度取决于系统tick RT_TICK_PER_SECOND
 | 
			
		||||
            - 注:如果使能了Multi-Rtimer软件包,则优先使用Multi-Rtimer提供定时\超时服务
 | 
			
		||||
   - include
 | 
			
		||||
      - lora-radio.h
 | 
			
		||||
         - 上层服务接口
 | 
			
		||||
      - lora-radio-debug.h
 | 
			
		||||
         - 根据需要使能输出lora-radio不同层级的调试信息
 | 
			
		||||
      - lora-radio-rtos-config.h
 | 
			
		||||
         - rtos适配层,选择,当前默认为RT-Thread
 | 
			
		||||
         - 未来支持RT-Thread-Nano、以及其他RTOS....
 | 
			
		||||
- samples
 | 
			
		||||
   - lora radio driver示例文件
 | 
			
		||||
      - lora-radio-test-shell
 | 
			
		||||
         - shell示例,主要实现了射频性能测试、空口数据包监听、双向通信测试等shell命令,便于日常测试
 | 
			
		||||
- port
 | 
			
		||||
   - 主要包含当前在不同MCU平台下支持的lora模块,lora-module文件夹中的xxxx-borad.c包含了与LoRa模块直接相关的主要硬件接口配置:
 | 
			
		||||
      - lora-module
 | 
			
		||||
         - stm32_adapter 
 | 
			
		||||
            - lora-board-spi.c
 | 
			
		||||
               - STM32平台的SPI外设初始化等通用接口
 | 
			
		||||
            - LSD4RF-2F717N20 (SX1278 LoRa模块)
 | 
			
		||||
            - LSD4RF-2R717N40 (SX1268 LoRa模块)
 | 
			
		||||
            - Ra-01 (SX1278 LoRa模块)
 | 
			
		||||
               - xxxx-borad.c
 | 
			
		||||
                  - LoRa模块功率输出方式(PA\RFO...)
 | 
			
		||||
                  - LoRa模块的RF高频开关控制方式(TXE、RXE、无..)
 | 
			
		||||
                  - LoRa模块的DIO口(DIO0、DIO1、DIO2....)
 | 
			
		||||
                  - LoRa模块的工作频率限制等
 | 
			
		||||
         - xxx_adapter
 | 
			
		||||
            - 其他mcu平台下的硬件接口实现
 | 
			
		||||
# 3 LoRa Radio Driver软件包使用说明
 | 
			
		||||
## 3.1 依赖
 | 
			
		||||
 | 
			
		||||
- SPI外设——用户需根据实际MCU平台,自定义LoRa模块实际所需要使用的SPI外设
 | 
			
		||||
   - 选择SPI外设 
 | 
			
		||||
```
 | 
			
		||||
  Hardware Drivers Config --->
 | 
			
		||||
     On-chip Peripheral Drivers --->
 | 
			
		||||
         [*] Enable SPI  --->
 | 
			
		||||
                 --- Enable SPI
 | 
			
		||||
                 [ ]   Enable SPI1
 | 
			
		||||
                 [ ]   Enable SPI2
 | 
			
		||||
                 [ ]   Enable SPI3
 | 
			
		||||
                 [ ]   Enable SPI4
 | 
			
		||||
                 [ ]   Enable SPI5
 | 
			
		||||
```
 | 
			
		||||
   - 在bsp\目标板XX\board\Kconfig增加如下定义
 | 
			
		||||
```c
 | 
			
		||||
  menuconfig BSP_USING_SPI
 | 
			
		||||
       bool "Enable SPI"
 | 
			
		||||
       select RT_USING_SPI
 | 
			
		||||
 | 
			
		||||
       if BSP_USING_SPI
 | 
			
		||||
         config BSP_USING_SPI1
 | 
			
		||||
            bool "Enable SPI1"
 | 
			
		||||
            default n
 | 
			
		||||
            if BSP_USING_SPI1
 | 
			
		||||
              config BSP_SPI1_RX_USING_DMA
 | 
			
		||||
                bool "Enable SPI1 RX DMA"
 | 
			
		||||
                default n
 | 
			
		||||
              config BSP_SPI1_TX_USING_DMA
 | 
			
		||||
                bool "Enable SPI1 TX DMA"
 | 
			
		||||
                default n
 | 
			
		||||
            endif
 | 
			
		||||
            
 | 
			
		||||
            # 根据实际需要,增加其他BSP_USING_SPI2、BSP_USING_SPI3...
 | 
			
		||||
        endif
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
- 定时服务——用于提供射频通信所需的定时\超时服务,目前支持以下两种方式,二选一
 | 
			
		||||
   - 内核 SOFT_TIMER
 | 
			
		||||
      - 若未选用Multi-Rtimer软件包,则默认采用内核的rt_timer来提供定时服务(lora-radio-timer.c)
 | 
			
		||||
      - 注意检测是否**开启RT-Thread内核的SOFT_TIMER**
 | 
			
		||||
   - M[ulti-Rtimer](https://github.com/Forest-Rain/multi-rtimer)软件包
 | 
			
		||||
      - 若使能multi-rtimer,lora-radio-driver优先使用multi-rtimer提供定时\超时服务。 
 | 
			
		||||
> 注:如果应用在工业温度范围、时间精度要求高(us\ms级别)的场景,建议使用multi-rtimer,并设置RTC时钟源为外部32768晶振,否则可能会出现下行丢包的情况。
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
RT-Thread online packages --->
 | 
			
		||||
    peripheral libraries and drivers --->
 | 
			
		||||
        [*] multi_rtimer: a real-time and low power software timer module. --->
 | 
			
		||||
                Version (latest)  --->
 | 
			
		||||
                multi_rtimer options --->
 | 
			
		||||
                    [] multi_rtimer demo example
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
- 可选内核组件
 | 
			
		||||
   - ulog组件——用于打印日志信息
 | 
			
		||||
      - 使能ulog
 | 
			
		||||
         - ulog缓存大小设置≥ 128 Byte
 | 
			
		||||
         - lora-raido-driver内部可看到更多LoRa底层的调试信息
 | 
			
		||||
         - lora-radio-test-shell.c使用ulog接口,用于打印调试信息、原始16进制数据等
 | 
			
		||||
      - 如果没有使用ulog,默认使用rt_kprintf来实现信息输出功能
 | 
			
		||||
```
 | 
			
		||||
RT-Thread Components --->
 | 
			
		||||
   Utiliess --->
 | 
			
		||||
       [*] Enable ulog
 | 
			
		||||
           [*] Enable ISR log.
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## 3.2 获取软件包
 | 
			
		||||
使用 lora-radio-driver 软件包,需要在 RT-Thread 的包管理中选中它,具体路径如下:
 | 
			
		||||
```c
 | 
			
		||||
RT-Thread online packages --->
 | 
			
		||||
    peripheral libraries and drivers --->
 | 
			
		||||
        [*] lora_radio_driver: lora chipset(sx126x\sx127x.)driver. --->
 | 
			
		||||
            Select LoRa Radio Object Type (LoRa Radio Single-Instance)
 | 
			
		||||
                (lora-radio0)Setup LoRa Radio Device Name       
 | 
			
		||||
                (spi3)  Setup LoRa Radio Spi Name (Define BSP_USING_SPIx in [Target Platform]\Board\Kconfig)
 | 
			
		||||
                        Select LoRa Chip Type (LoRa Transceiver [SX126X])  --->
 | 
			
		||||
                        Select Supported LoRa Module [SX126X]  --->
 | 
			
		||||
                [ ]     Enable LoRa Radio Debug
 | 
			
		||||
                        Select LoRa Radio Driver Sample --->  
 | 
			
		||||
                    Version (latest)  --->
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
1. Select LoRa Chip \ LoRa Module
 | 
			
		||||
   1. "Setup LoRa Radio Device Name"
 | 
			
		||||
      1. 设置LoRa Radio设备名称,缺省为"lora-radio0"
 | 
			
		||||
   2. "Setup LoRa Radio Spi Name"
 | 
			
		||||
      1. 设置LoRa Radio Spi名称
 | 
			
		||||
      1. 若在 [Target Platform]\Board\Kconfig提前设定好所使用的BSP_USING_SPIx,则会自动配置
 | 
			
		||||
   3. "Select LoRa Radio Single-Instance"
 | 
			
		||||
      1. 选择为单实例对象,当前只支持单个lora设备
 | 
			
		||||
   4. "Select LoRa Chip Type"
 | 
			
		||||
      1. 选择实际使用的LoRa芯片类型
 | 
			
		||||
         - 当前支持 SX126X、SX127x Transceiver
 | 
			
		||||
   5. "Select Supported LoRa Module"
 | 
			
		||||
      1. 选择lora模块,根据实际使用的MCU硬件平台与lora模块,配置关联的GPIO引脚等功能选项
 | 
			
		||||
         1. 设定LoRa模块的GPIO口(比如 RESET、NSS、BUSY、DIO1、TXE、RXE...)
 | 
			
		||||
            - " Select LoRa Chip GPIO by Pin Number  "
 | 
			
		||||
               - 支持使用引脚号来定义GPIO,比如 输入 10 代表 A10 
 | 
			
		||||
            - "Select LoRa Chip GPIO by Pin Name"
 | 
			
		||||
               - 支持使用引脚名来定义GPIO,比如 输入 A10 代表引脚GPIOA的PIN10脚 (STM32)
 | 
			
		||||
2. Select LoRa Radio Driver Sample
 | 
			
		||||
   1. 根据实际情况,可选择测试示例
 | 
			
		||||
## 3.3 新增LoRa模块
 | 
			
		||||
在 lora-radio-driver\ports\lora-module文件下,参考已有模板,根据实际需要增加新的mcu平台适配文件、新的lora模块驱动文件xxxx-board.c
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 4 使用示例
 | 
			
		||||
 | 
			
		||||
## 4.1 硬件测试平台
 | 
			
		||||
当前所使用的硬件测试平台如下所示
 | 
			
		||||
| 序号 | 硬件平台 | MCU | LoRa模块 | 主要用户接口 |
 | 
			
		||||
| --- | --- | --- | --- | --- |
 | 
			
		||||
| 1 | LSD4RF-TEST2002  | STM32L476VG | [LSD4RF-2R717N40](http://bbs.lierda.com/forum.php?mod=viewthread&tid=87)<br />[ ( SX1268 )](http://bbs.lierda.com/forum.php?mod=viewthread&tid=87) | <br />- 用户接口定义<br />   - VCC  - 3.3V<br />   - GND<br />   - SCK    - PC10 (SPI3)<br />   - MISO  - PC11 (SPI3)<br />   - MOSI  - PC12 (SPI3)<br />   - NSS    - PA15<br />   - RESET - PA7<br />   - DIO0  - PB1<br />   - BUSY - PB2<br />   - RFSW1 - PB0<br />   - RFSW2 - PC5<br />- 射频开关TX trace<br />   - TX: RFSW1 = 1 , RFSW2 = 0<br />   - TX: RFSW1 = 0 , RFSW2 = 1<br /> |
 | 
			
		||||
| 2 | LSD4RF-TEST2002  | STM32L476VG | [LSD4RF-2F717N20](http://bbs.lierda.com/forum.php?mod=viewthread&tid=87)<br />[ ( SX1278 )](http://bbs.lierda.com/forum.php?mod=viewthread&tid=87) | <br />- 用户接口定义<br />   - VCC   - 3.3V<br />   - GND<br />   - SCK    - PC10 (SPI3)<br />   - MISO  - PC11 (SPI3)<br />   - MOSI  - PC12 (SPI3)<br />   - NSS    - PB6<br />   - RESET - PA7<br />   - DIO0  - PB1<br />   - DIO1  - PC4<br />   - DIO2  - PB2<br />   - DIO3  - NC<br />   - DIO4  - NC<br />   - RFSW1 - PB0<br />   - RFSW2 - PC5<br />- 射频开关TX trace<br />   - TX: RFSW1 = 1 , RFSW2 = 0<br />   - TX: RFSW1 = 0 , RFSW2 = 1<br /> |
 | 
			
		||||
| 3 | Nucleo-L476RG | STM32L476RG | [Ra-01](http://wiki.ai-thinker.com/lora/man)<br />(RT-thread LoRa Adruino扩展板V1) | <br />- 用户接口定义<br />   - VCC    - 3.3V<br />   - GND<br />   - SCK    - PA5(SPI1)<br />   - MISO  - PA6(SPI1)<br />   - MOSI  - PA7(SPI1)<br />   - NSS    - PB6<br />   - RESET - PC7<br />   - DIO0  - PA9<br />   - DIO1  - PA8<br /> |
 | 
			
		||||
 | 
			
		||||
## 4.2 Shell测试命令
 | 
			
		||||
若使能 [* ] LoRa Radio Test Shell,则可以通过shell(finish)命令直接进行LoRa相关测试
 | 
			
		||||
```c
 | 
			
		||||
[*] Enable LoRa Radio Test Shell                                                                                              │ │
 | 
			
		||||
     Select the RF frequency (Region CN470)  --->                                                                            │ │
 | 
			
		||||
     Select RF Modem (Modem LoRa)  --->
 | 
			
		||||
```
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
| 序号 | finish命令 | 说明 |
 | 
			
		||||
| --- | --- | --- |
 | 
			
		||||
| 1 | lora probe | 测试lora设备(SPI)访问是否正常 |
 | 
			
		||||
| 2 | lora cw <para1> <para2> | 输出CW,可用于测试发射功率、频点等<br />\<para1\>:频点,单位Hz<br>\<para2\>:功率,单位dBm |
 | 
			
		||||
| 3 | lora ping <para1> <para2> | 单向\双向通信测试<br />\<para1\> : 主机\从机<br>-m 主机<br>-s 从机<br> \<para2\>: 发送数据包个数 |
 | 
			
		||||
| 4 | lora rx  | 接收(监听)数据包,同时以16进制格式与ASCII码显示数据内容 |
 | 
			
		||||
| 5 | lora config <para1> <para2> | 配置射频参数<br />\<para1\>:radio参数,字符表示<br/>  freq 表示频率,单位Hz<br/>  power 表示发射功率,单位dbm<br/>  sf 表示扩频因子,有效值: 7~12<br/>  bw表示带宽,有效值: 0 (125kHz)、1 (250KHz)、2 (500KHz)<br/>  public表示同步字,有效值: 0 (sync = 0x12), 1  (sync = 0x34)<br/>  iq 表示iq反转,有效值: 0 (iq不反转),1 (iq反转)<br/>\<para2\>:radio参数的具体值 |
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
lora ping 双向通信测试示例(SX1278 <-> SX1268)
 | 
			
		||||

 | 
			
		||||
lora rx 单向接收(监听)lora数据包测试示例 (SX1278 <- 或-> SX1268)
 | 
			
		||||
 | 
			
		||||
## 4.3 应用层调用说明
 | 
			
		||||
用户层调用可以参考如下步骤
 | 
			
		||||
 | 
			
		||||
1. 定义射频DIO中断服务回调函数
 | 
			
		||||
```c
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Function to be executed on Radio Tx Done event
 | 
			
		||||
 */
 | 
			
		||||
void OnTxDone( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Function to be executed on Radio Rx Done event
 | 
			
		||||
 */
 | 
			
		||||
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Function executed on Radio Tx Timeout event
 | 
			
		||||
 */
 | 
			
		||||
void OnTxTimeout( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Function executed on Radio Rx Timeout event
 | 
			
		||||
 */
 | 
			
		||||
void OnRxTimeout( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Function executed on Radio Rx Error event
 | 
			
		||||
 */
 | 
			
		||||
void OnRxError( void );
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
2. 调用lora-radio初始化
 | 
			
		||||
```c
 | 
			
		||||
void main(void)
 | 
			
		||||
{
 | 
			
		||||
    // Radio initialization
 | 
			
		||||
    RadioEvents.TxDone = OnTxDone;
 | 
			
		||||
    RadioEvents.RxDone = OnRxDone;
 | 
			
		||||
    RadioEvents.TxTimeout = OnTxTimeout;
 | 
			
		||||
    RadioEvents.RxTimeout = OnRxTimeout;
 | 
			
		||||
    RadioEvents.RxError = OnRxError;
 | 
			
		||||
 | 
			
		||||
    if(Radio.Init(&RadioEvents))
 | 
			
		||||
    {
 | 
			
		||||
        Radio.SetPublicNetwork( false );
 | 
			
		||||
        lora_chip_initialized = true;
 | 
			
		||||
    }
 | 
			
		||||
    //.....
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
3. 配置射频通信参数
 | 
			
		||||
```c
 | 
			
		||||
{
 | 
			
		||||
    Radio.SetChannel( lora_radio_test_paras.frequency );
 | 
			
		||||
 | 
			
		||||
    if( lora_radio_test_paras.modem == MODEM_LORA )
 | 
			
		||||
    {
 | 
			
		||||
        Radio.SetTxConfig( MODEM_LORA, lora_radio_test_paras.txpower, 0, lora_radio_test_paras.bw,
 | 
			
		||||
                          lora_radio_test_paras.sf, lora_radio_test_paras.cr,
 | 
			
		||||
                          LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON_DISABLE,
 | 
			
		||||
                          true, 0, 0, LORA_IQ_INVERSION_ON_DISABLE, 3000 );
 | 
			
		||||
 | 
			
		||||
        Radio.SetRxConfig( MODEM_LORA, lora_radio_test_paras.bw, lora_radio_test_paras.sf,
 | 
			
		||||
                          lora_radio_test_paras.cr, 0, LORA_PREAMBLE_LENGTH,
 | 
			
		||||
                          LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON_DISABLE,
 | 
			
		||||
                          0, true, 0, 0, LORA_IQ_INVERSION_ON_DISABLE, true );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
4. 数据发送
 | 
			
		||||
```c
 | 
			
		||||
Radio.Send( Buffer, len );
 | 
			
		||||
```
 | 
			
		||||
5. 数据接收
 | 
			
		||||
```c
 | 
			
		||||
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
 | 
			
		||||
{
 | 
			
		||||
    Radio.Sleep( );
 | 
			
		||||
    BufferSize = size;
 | 
			
		||||
    rt_memcpy( Buffer, payload, BufferSize );
 | 
			
		||||
    rssi_value = rssi;
 | 
			
		||||
    snr_value = snr;
 | 
			
		||||
    // .....
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
# 5 版本更新历史
 | 
			
		||||
 | 
			
		||||
- V1.0.0 版本 2020-06-20
 | 
			
		||||
   - 主体功能实现基于STM32平台
 | 
			
		||||
      - 支持SX126x、SX127x系列芯片
 | 
			
		||||
         - 测试LoRa芯片支持LSD4RF-2R717N40(SX1268)、SX1278、ASR6500S @ [**zyk6271**](https://github.com/zyk6271)
 | 
			
		||||
   - 支持基于RT-Thread内核rt_timer的lora-radio-timer接口@ [**AnswerInTheWind** ](https://github.com/AnswerInTheWind)
 | 
			
		||||
   - 优化日志换行功能@[**zyk6271**](https://github.com/zyk6271)
 | 
			
		||||
- V1.1.0 版本 2020-08-30
 | 
			
		||||
   - 完善用户使用指南
 | 
			
		||||
   - .lora-radio-driver软件包
 | 
			
		||||
      - 新增日志输出选择 lora-radio-debug.h,可以按需开启调试日志,也可以用于适配不同日志输出方式
 | 
			
		||||
      - 新增rtos适配选择 lora-radio-rtos-config.h,便于未来适配RT-Thread-Nano、不同的RTOS平台
 | 
			
		||||
      - lora-radio(sx126x\sx127x)
 | 
			
		||||
         - 同步更新到lorawan4.4.4 release版本的radio
 | 
			
		||||
            - sx126x更新 SX126xSetLoRaSymbNumTimeout(同步到loramac-node-master)
 | 
			
		||||
            - sx126x更新 RadioRandom 与 SX126xGetRandom
 | 
			
		||||
            - 更新 RadioIrqProcess
 | 
			
		||||
            - 更新RadioTimeOnAir
 | 
			
		||||
         - RadioIrqProcess 增加 临界区保护,防止出现硬件异常
 | 
			
		||||
      - 调整lora-radio-driver软件包架构,便于未来适配不同的MCU平台
 | 
			
		||||
         - port目录下新增mcu平台适配层,如stm32_adapter
 | 
			
		||||
   - lora-radio-test-shell
 | 
			
		||||
      - 修复 PHY CRC Error后,没有重新进入接收问题
 | 
			
		||||
      - lora ping命令
 | 
			
		||||
         - 新增发送空口数据包的TOA时间显示
 | 
			
		||||
         - 新增主机侧接收到数据包后,seqno显示
 | 
			
		||||
   - [Kconfig](https://github.com/Forest-Rain/packages/blob/master/peripherals/lora_radio_driver)
 | 
			
		||||
      - 更新[lora-radio-driver\Kconfig](https://github.com/Forest-Rain/packages) 软件包配置文件
 | 
			
		||||
         - 区分单实例(单lora模块)与多实例(多lora模块)情况,目前支持单实例
 | 
			
		||||
         - 移除了Kconfig中对BSP_USING_SPIx的直接定义,BSP_USING_SPIx定义调整到[Target Platform]\Board\Kconfig)
 | 
			
		||||
         - 重命名宏定义REGION_X为PHY_REGION_X(如REGION_CN470 -> PHY_REGION_CN470),以便与LoRaWAN协议栈中缺省REGION_X共存
 | 
			
		||||
 | 
			
		||||
- V1.1.2 版本 2020-10-12
 | 
			
		||||
  - 修复Ra-01未同步与v1.1.1更新导致的问题
 | 
			
		||||
  - 优化 drv_gpio.h使用,兼容RT-Thread Studio
 | 
			
		||||
  - 优化 lora-radio-test-shell.c 功能
 | 
			
		||||
  - 新增接收超时时间设置
 | 
			
		||||
- V1.2 版本 2020-10-14
 | 
			
		||||
  - 新增硬件测试平台
 | 
			
		||||
  - ART-Pi+LSD4RF-2F717N30(SX1268)模块平台 (470~510MHz频段)
 | 
			
		||||
  - ART-Pi+LSD4RF-2R717N40(SX1268)模块平台 (470~510MHz频段)
 | 
			
		||||
  - ART-Pi+LSD4RF-2R822N30(SX1262)模块平台 (868/915MHz频段)
 | 
			
		||||
- V1.4.0 版本 2021-04-25
 | 
			
		||||
  - 重设计lora config命令,便于快速配置单个radio参数
 | 
			
		||||
  - ping数据包长度最大支持255Byte,可通过shell自定义ping测试数据包长度
 | 
			
		||||
  - shell新增加iq version、public network参数设置
 | 
			
		||||
  - 使用LORA_RADIO_DEBUG_LOG代替rt_kprintf
 | 
			
		||||
- V1.5.0 版本 2021-11-15
 | 
			
		||||
  - 新增doxygen支持
 | 
			
		||||
 | 
			
		||||
# 6 问题和建议
 | 
			
		||||
如果有什么问题或者建议欢迎提交 [Issue](https://github.com/Forest-Rain/lora-radio-driver/issues) 进行讨论。
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 53 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 25 KiB  | 
| 
						 | 
				
			
			@ -0,0 +1,41 @@
 | 
			
		|||
/*
 | 
			
		||||
 * This file is only used for doxygen document generation.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * @mainpage Introduce
 | 
			
		||||
 * @author wsn-hxj
 | 
			
		||||
 * @version v1.5.0
 | 
			
		||||
 * @date 2021-11-15
 | 
			
		||||
 * 
 | 
			
		||||
 * lora-radio-driver软件包是基于RTOS( RT-Thread ) 实现的LoRa Tranceiver芯片(SX126x、SX127x等)的驱动文件,该驱动文件通过SPI读写LoRa Tranceiver芯片,可用于快速搭建基于LoRa等通信的应用产品。
 | 
			
		||||
 * 主要功能如下:
 | 
			
		||||
 *  - 1. 当前支持LoRa Transceiver(sx126x\sx127x等)
 | 
			
		||||
 *   - 支持Sub-1GHz频段
 | 
			
		||||
 *   - 支持LoRa调制方式(SF5~SF12、BW125~BW500)
 | 
			
		||||
 *   - 支持FSK调制方式
 | 
			
		||||
 *  - 2. 可作为phy层对接到LoRaWAN End-Device协议栈
 | 
			
		||||
 *  - 3. 提供常用实例代码tester,可用于射频性能测试、空口数据包监听、双向通信测试等
 | 
			
		||||
 *  - 4. 当前支持的LoRa 模块\芯片,如
 | 
			
		||||
 *   - LSD4RF-2R717N40 (SX1268 CN470频段)
 | 
			
		||||
 *   - LSD4RF-2R822N300 (SX1262 868/915MHz频段)
 | 
			
		||||
 *   - LSD4RF-2F717N30(SX1278 CN470频段)
 | 
			
		||||
 *   - Ra-01(SX1278 CN470频段)等等
 | 
			
		||||
 * @section application_arch 01 功能框图
 | 
			
		||||
 * 
 | 
			
		||||
 * lora-radio-driver功能框图:
 | 
			
		||||
 * @image html lora-radio-driver_func_arch.png "Figure 1: 功能框图"
 | 
			
		||||
 *
 | 
			
		||||
 * @section file_arch 02 文件结构
 | 
			
		||||
 * 
 | 
			
		||||
 * lora-radio-driver文件结构:
 | 
			
		||||
 * @image html lora-radio-driver_file_arch.png "Figure 1: 文件结构"
 | 
			
		||||
 * 
 | 
			
		||||
 * @section lora-radio-driver 03 用户接口API
 | 
			
		||||
 * 更多详细信息,请参考 @ref LORA_RADIO_UPPER_API
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 79 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 21 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 261 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 263 KiB  | 
| 
						 | 
				
			
			@ -0,0 +1,15 @@
 | 
			
		|||
SRC_DIR := common 
 | 
			
		||||
 | 
			
		||||
ifeq ($(CONFIG_LORA_RADIO_DRIVER_USING_RTOS_XIUOS),y)
 | 
			
		||||
 | 
			
		||||
ifeq ($(CONFIG_LORA_RADIO_DRIVER_USING_SX1268_E22400M30S),y)
 | 
			
		||||
	SRC_DIR += sx126x
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
ifeq ($(CONFIG_LORA_RADIO_DRIVER_USING_SX1278_RA01),y)
 | 
			
		||||
	SRC_DIR += sx127x
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,29 @@
 | 
			
		|||
from building import *
 | 
			
		||||
 | 
			
		||||
src   = []
 | 
			
		||||
cwd   = GetCurrentDir()
 | 
			
		||||
include_path = [cwd]
 | 
			
		||||
include_path += [cwd+'/include']
 | 
			
		||||
include_path += [cwd+'/common']
 | 
			
		||||
 | 
			
		||||
# add lora radio driver.
 | 
			
		||||
if GetDepend('LORA_RADIO_DRIVER_USING_LORA_CHIP_SX126X'):
 | 
			
		||||
    src = Split('''
 | 
			
		||||
    sx126x/lora-radio-sx126x.c
 | 
			
		||||
    sx126x/lora-spi-sx126x.c
 | 
			
		||||
    sx126x/sx126x.c
 | 
			
		||||
    ''')
 | 
			
		||||
    include_path += [cwd+'/sx126x']
 | 
			
		||||
if GetDepend('LORA_RADIO_DRIVER_USING_LORA_CHIP_SX127X'):
 | 
			
		||||
    src = Split('''
 | 
			
		||||
    sx127x/lora-radio-sx127x.c
 | 
			
		||||
    sx127x/lora-spi-sx127x.c
 | 
			
		||||
    sx127x/sx127x.c
 | 
			
		||||
    ''')
 | 
			
		||||
    include_path += [cwd+'/sx127x']
 | 
			
		||||
 | 
			
		||||
src += ['common/lora-radio-timer.c']
 | 
			
		||||
 | 
			
		||||
group = DefineGroup('lora-radio-driver', src, depend = ['PKG_USING_LORA_RADIO_DRIVER'], CPPPATH = include_path)
 | 
			
		||||
 | 
			
		||||
Return('group')
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_FILES := lora-radio-timer.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,181 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-radio-timer.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     lora-radio timer 
 | 
			
		||||
 *
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * \author    DerekChen
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    AIIT XUOS Lab
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file lora-radio-timer.c
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-08
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: lora-radio-timer.c
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-08
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include "lora-radio-timer.h"
 | 
			
		||||
 | 
			
		||||
#if defined ( LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD ) || defined ( LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD_NANO )
 | 
			
		||||
#ifndef PKG_USING_MULTI_RTIMER
 | 
			
		||||
 | 
			
		||||
void rtick_timer_init( rtick_timer_event_t *obj, void ( *callback )( void ) )
 | 
			
		||||
{
 | 
			
		||||
    int count = 0;
 | 
			
		||||
    
 | 
			
		||||
    char name[RT_NAME_MAX];
 | 
			
		||||
    rt_snprintf(name,8,"rtk_%d",count++);
 | 
			
		||||
 | 
			
		||||
    rt_timer_init(&(obj->timer),name,(void (*)(void*))callback,RT_NULL,1000,RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_SOFT_TIMER);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rtick_timer_start( rtick_timer_event_t *obj )
 | 
			
		||||
{
 | 
			
		||||
    rt_timer_start(&(obj->timer));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rtick_timer_stop( rtick_timer_event_t *obj )
 | 
			
		||||
{
 | 
			
		||||
    rt_timer_stop(&(obj->timer));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rtick_timer_reset( rtick_timer_event_t *obj )
 | 
			
		||||
{
 | 
			
		||||
    rtick_timer_stop(obj);
 | 
			
		||||
    rtick_timer_start(obj);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rtick_timer_set_value( rtick_timer_event_t *obj, uint32_t value )
 | 
			
		||||
{
 | 
			
		||||
    uint32_t tick = rt_tick_from_millisecond(value);
 | 
			
		||||
    rt_timer_control(&(obj->timer),RT_TIMER_CTRL_SET_TIME,&tick);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TimerTime_t rtick_timer_get_current_time( void )
 | 
			
		||||
{
 | 
			
		||||
    uint32_t now = rt_tick_get();
 | 
			
		||||
    return  now;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TimerTime_t rtick_timer_get_elapsed_time( TimerTime_t past )
 | 
			
		||||
{
 | 
			
		||||
    return rt_tick_get() - past;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif // End Of  ( LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD ) || defined ( LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD_NANO )
 | 
			
		||||
 | 
			
		||||
#if defined LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
 | 
			
		||||
#define TIMER_NUM 20
 | 
			
		||||
 | 
			
		||||
typedef void (*timer_input_callback_entry)(void);
 | 
			
		||||
typedef struct {
 | 
			
		||||
    timer_input_callback_entry callback_entry;
 | 
			
		||||
}timer_input_callback_entry_fun;
 | 
			
		||||
 | 
			
		||||
static timer_input_callback_entry_fun g_timer_input_callback_entry_fun[TIMER_NUM];
 | 
			
		||||
 | 
			
		||||
static void timer_callback(union sigval sig_val) 
 | 
			
		||||
{
 | 
			
		||||
    uint8_t timer_id = *((uint8_t *)sig_val.sival_ptr);
 | 
			
		||||
    g_timer_input_callback_entry_fun[timer_id].callback_entry();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rtick_timer_init( rtick_timer_event_t *obj, void ( *callback )( void ) )
 | 
			
		||||
{
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
    int timer_flags;
 | 
			
		||||
    static int count = 0; 
 | 
			
		||||
 | 
			
		||||
    struct sigevent evp;
 | 
			
		||||
    memset(&evp, 0, sizeof(struct sigevent));
 | 
			
		||||
    
 | 
			
		||||
    timer_flags = TIMER_TRIGGER_ONCE;
 | 
			
		||||
 | 
			
		||||
    count++;
 | 
			
		||||
 | 
			
		||||
    g_timer_input_callback_entry_fun[count].callback_entry = callback;
 | 
			
		||||
    evp.sigev_notify = SIGEV_THREAD; 
 | 
			
		||||
    evp.sigev_notify_function = timer_callback;
 | 
			
		||||
    evp.sigev_notify_attributes = &timer_flags;
 | 
			
		||||
 | 
			
		||||
    ret = PrivTimerCreate(count, &evp, &(obj->timer_id));
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        printf("%s create timer failed ret %d\n", __func__, ret);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rtick_timer_start( rtick_timer_event_t *obj )
 | 
			
		||||
{
 | 
			
		||||
    PrivTimerStartRun(obj->timer_id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rtick_timer_stop( rtick_timer_event_t *obj )
 | 
			
		||||
{
 | 
			
		||||
    PrivTimerQuitRun(obj->timer_id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rtick_timer_reset( rtick_timer_event_t *obj )
 | 
			
		||||
{
 | 
			
		||||
    rtick_timer_stop(obj);
 | 
			
		||||
    rtick_timer_start(obj);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rtick_timer_set_value( rtick_timer_event_t *obj, uint32_t value )
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    uint32_t input_value, ms_value, s_value;
 | 
			
		||||
    struct itimerspec it_value; 
 | 
			
		||||
    //active time interval
 | 
			
		||||
    input_value = value;
 | 
			
		||||
    if (input_value >= 1000) {
 | 
			
		||||
        ms_value = input_value % 1000;
 | 
			
		||||
        s_value = (input_value - ms_value) / 1000;
 | 
			
		||||
    } else {
 | 
			
		||||
        ms_value = input_value;
 | 
			
		||||
        s_value = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    it_value.it_interval.tv_sec = s_value; 
 | 
			
		||||
    it_value.it_interval.tv_nsec = ms_value * 1000000;//1ms = 1000000ns
 | 
			
		||||
 | 
			
		||||
    ret = PrivTimerModify(obj->timer_id, 0, &it_value, NULL);
 | 
			
		||||
    if (ret < 0) { 
 | 
			
		||||
        printf("%s set timer time failed ret %d\n", __func__, ret);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TimerTime_t rtick_timer_get_current_time( void )
 | 
			
		||||
{
 | 
			
		||||
    uint32_t now = PRIV_SYSTICK_GET;
 | 
			
		||||
    return now;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TimerTime_t rtick_timer_get_elapsed_time( TimerTime_t past )
 | 
			
		||||
{
 | 
			
		||||
    return PRIV_SYSTICK_GET - past;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,216 @@
 | 
			
		|||
#ifndef __LORA_RADIO_TIMER_H__
 | 
			
		||||
#define __LORA_RADIO_TIMER_H__
 | 
			
		||||
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
#ifdef PKG_USING_MULTI_RTIMER
 | 
			
		||||
 | 
			
		||||
#include "multi_rtimer.h"
 | 
			
		||||
#include "hw_rtc_stm32.h"
 | 
			
		||||
 | 
			
		||||
#define LORA_RADIO_TIMESTAMP_TO_MS(x) (x)
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
 | 
			
		||||
/** @addtogroup LORA_RADIO_TIMER
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#define TimerInit            rtick_timer_init
 | 
			
		||||
#define TimerStart           rtick_timer_start
 | 
			
		||||
#define TimerStop            rtick_timer_stop   
 | 
			
		||||
#define TimerReset           rtick_timer_reset
 | 
			
		||||
#define TimerSetValue        rtick_timer_set_value
 | 
			
		||||
#define TimerSetTimestamp    rtick_timer_set_timestamp
 | 
			
		||||
#define TimerGetCurrentTime  rtick_timer_get_current_time
 | 
			
		||||
#define TimerGetElapsedTime  rtick_timer_get_elapsed_time
 | 
			
		||||
#define TimerEvent_t         rtick_timer_event_t
 | 
			
		||||
 | 
			
		||||
#define LORA_RADIO_TIMESTAMP_TO_MS(x) ((x) * 1000 / RT_TICK_PER_SECOND)
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Timer object description
 | 
			
		||||
 */
 | 
			
		||||
typedef struct TimerEvent_s
 | 
			
		||||
{
 | 
			
		||||
    struct rt_timer timer; 
 | 
			
		||||
}rtick_timer_event_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Timer time variable definition
 | 
			
		||||
 */
 | 
			
		||||
#ifndef TimerTime_t
 | 
			
		||||
typedef uint32_t TimerTime_t;
 | 
			
		||||
#define TIMERTIME_T_MAX                             ( ( uint32_t )~0 )
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the timer object
 | 
			
		||||
 *
 | 
			
		||||
 * \remark TimerSetValue function must be called before starting the timer.
 | 
			
		||||
 *         this function initializes timestamp and reload value at 0.
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] obj          Structure containing the timer object parameters
 | 
			
		||||
 * \param [IN] callback     Function callback called at the end of the timeout
 | 
			
		||||
 */
 | 
			
		||||
void rtick_timer_init( rtick_timer_event_t *obj, void ( *callback )( void ) );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Starts and adds the timer object to the list of timer events
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] obj Structure containing the timer object parameters
 | 
			
		||||
 */
 | 
			
		||||
void rtick_timer_start( rtick_timer_event_t *obj );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Stops and removes the timer object from the list of timer events
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] obj Structure containing the timer object parameters
 | 
			
		||||
 */
 | 
			
		||||
void rtick_timer_stop( rtick_timer_event_t *obj );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Resets the timer object
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] obj Structure containing the timer object parameters
 | 
			
		||||
 */
 | 
			
		||||
void rtick_timer_reset( rtick_timer_event_t *obj );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Set timer new timeout value
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] obj   Structure containing the timer object parameters
 | 
			
		||||
 * \param [IN] value New timer timeout value
 | 
			
		||||
 */
 | 
			
		||||
void rtick_timer_set_value( rtick_timer_event_t *obj, uint32_t value );
 | 
			
		||||
 | 
			
		||||
void rtick_timer_set_timestamp( rtick_timer_event_t *obj, uint32_t value );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Read the current time
 | 
			
		||||
 *
 | 
			
		||||
 * \retval time returns current time
 | 
			
		||||
 */
 | 
			
		||||
TimerTime_t rtick_timer_get_current_time( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Return the Time elapsed since a fix moment in Time
 | 
			
		||||
 *
 | 
			
		||||
 * \remark TimerGetElapsedTime will return 0 for argument 0.
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] past         fix moment in Time
 | 
			
		||||
 * \retval time             returns elapsed time
 | 
			
		||||
 */
 | 
			
		||||
TimerTime_t rtick_timer_get_elapsed_time( TimerTime_t past );
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
 | 
			
		||||
/** @addtogroup LORA_RADIO_TIMER
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#define TimerInit            rtick_timer_init
 | 
			
		||||
#define TimerStart           rtick_timer_start
 | 
			
		||||
#define TimerStop            rtick_timer_stop   
 | 
			
		||||
#define TimerReset           rtick_timer_reset
 | 
			
		||||
#define TimerSetValue        rtick_timer_set_value
 | 
			
		||||
#define TimerSetTimestamp    rtick_timer_set_timestamp
 | 
			
		||||
#define TimerGetCurrentTime  rtick_timer_get_current_time
 | 
			
		||||
#define TimerGetElapsedTime  rtick_timer_get_elapsed_time
 | 
			
		||||
#define TimerEvent_t         rtick_timer_event_t
 | 
			
		||||
 | 
			
		||||
#define LORA_RADIO_TIMESTAMP_TO_MS(x) ((x) * 1000 / TICK_PER_SECOND)
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Timer object description
 | 
			
		||||
 */
 | 
			
		||||
typedef struct TimerEvent_s
 | 
			
		||||
{
 | 
			
		||||
    timer_t timer_id; 
 | 
			
		||||
}rtick_timer_event_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Timer time variable definition
 | 
			
		||||
 */
 | 
			
		||||
#ifndef TimerTime_t
 | 
			
		||||
typedef uint32_t TimerTime_t;
 | 
			
		||||
#define TIMERTIME_T_MAX                             ( ( uint32_t )~0 )
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the timer object
 | 
			
		||||
 *
 | 
			
		||||
 * \remark TimerSetValue function must be called before starting the timer.
 | 
			
		||||
 *         this function initializes timestamp and reload value at 0.
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] obj          Structure containing the timer object parameters
 | 
			
		||||
 * \param [IN] callback     Function callback called at the end of the timeout
 | 
			
		||||
 */
 | 
			
		||||
void rtick_timer_init( rtick_timer_event_t *obj, void ( *callback )( void ) );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Starts and adds the timer object to the list of timer events
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] obj Structure containing the timer object parameters
 | 
			
		||||
 */
 | 
			
		||||
void rtick_timer_start( rtick_timer_event_t *obj );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Stops and removes the timer object from the list of timer events
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] obj Structure containing the timer object parameters
 | 
			
		||||
 */
 | 
			
		||||
void rtick_timer_stop( rtick_timer_event_t *obj );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Resets the timer object
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] obj Structure containing the timer object parameters
 | 
			
		||||
 */
 | 
			
		||||
void rtick_timer_reset( rtick_timer_event_t *obj );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Set timer new timeout value
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] obj   Structure containing the timer object parameters
 | 
			
		||||
 * \param [IN] value New timer timeout value
 | 
			
		||||
 */
 | 
			
		||||
void rtick_timer_set_value( rtick_timer_event_t *obj, uint32_t value );
 | 
			
		||||
 | 
			
		||||
void rtick_timer_set_timestamp( rtick_timer_event_t *obj, uint32_t value );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Read the current time
 | 
			
		||||
 *
 | 
			
		||||
 * \retval time returns current time
 | 
			
		||||
 */
 | 
			
		||||
TimerTime_t rtick_timer_get_current_time( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Return the Time elapsed since a fix moment in Time
 | 
			
		||||
 *
 | 
			
		||||
 * \remark TimerGetElapsedTime will return 0 for argument 0.
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] past         fix moment in Time
 | 
			
		||||
 * \retval time             returns elapsed time
 | 
			
		||||
 */
 | 
			
		||||
TimerTime_t rtick_timer_get_elapsed_time( TimerTime_t past );
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,122 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-radio-debug.h
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     control all LoRa Radio Driver debug features.
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    AIIT XUOS Lab
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file lora-radio-debug.h
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-07
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: lora-radio-debug.h
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-07
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 
 | 
			
		||||
#ifndef __LORA_RADIO_DEBUG_H__
 | 
			
		||||
#define __LORA_RADIO_DEBUG_H__
 | 
			
		||||
 | 
			
		||||
#include "lora-radio-rtos-config.h"
 | 
			
		||||
 | 
			
		||||
#if ( defined LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD ) || ( defined LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD_NANO )
 | 
			
		||||
#include "rtconfig.h"
 | 
			
		||||
#ifdef RT_USING_ULOG
 | 
			
		||||
#include <rtdbg.h>
 | 
			
		||||
#include <ulog.h> 
 | 
			
		||||
#else
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
//#define LORA_RADIO_DRIVER_USING_LORA_RADIO_DEBUG
 | 
			
		||||
/* Using this macro to control all LoRa Radio Driver debug features. */
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_LORA_RADIO_DEBUG
 | 
			
		||||
 | 
			
		||||
/* Turn on some of these (set to non-zero) to debug LoRa Radio */
 | 
			
		||||
 | 
			
		||||
/* API interface */
 | 
			
		||||
#ifndef LR_DBG_INTERFACE
 | 
			
		||||
#define LR_DBG_INTERFACE                    1
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/*lora chip driver, eg: sx126x.c\sx27x.c*/
 | 
			
		||||
#ifndef LR_DBG_CHIP
 | 
			
		||||
#define LR_DBG_CHIP                         1
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* spi driver ,eg: lora-spi-board.c*/
 | 
			
		||||
#ifndef LR_DBG_SPI
 | 
			
		||||
#define LR_DBG_SPI                          1
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* board driver ,eg: sx1268-board.c*/
 | 
			
		||||
#ifndef LR_DBG_BOARD
 | 
			
		||||
#define LR_DBG_BOARD                        1
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if ( defined LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD ) || ( defined LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD_NANO )
 | 
			
		||||
 | 
			
		||||
#if defined RT_USING_ULOG
 | 
			
		||||
#define LORA_RADIO_DEBUG_LOG(type, level, ...)                                \
 | 
			
		||||
do                                                                            \
 | 
			
		||||
{                                                                             \
 | 
			
		||||
    if (type)                                                                 \
 | 
			
		||||
    {                                                                         \
 | 
			
		||||
        ulog_output(level, LOG_TAG, RT_TRUE, __VA_ARGS__);                    \
 | 
			
		||||
    }                                                                         \
 | 
			
		||||
}                                                                             \
 | 
			
		||||
while (0)
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
 | 
			
		||||
#define LORA_RADIO_DEBUG_LOG(type, level, ...)                                \
 | 
			
		||||
do                                                                            \
 | 
			
		||||
{                                                                             \
 | 
			
		||||
    if (type)                                                                 \
 | 
			
		||||
    {                                                                         \
 | 
			
		||||
        rt_kprintf(__VA_ARGS__);                                              \
 | 
			
		||||
        rt_kprintf("\r\n");                                                   \
 | 
			
		||||
    }                                                                         \
 | 
			
		||||
}                                                                             \
 | 
			
		||||
while (0)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#elif ( defined LORA_RADIO_DRIVER_USING_RTOS_XIUOS )
 | 
			
		||||
 | 
			
		||||
#define LORA_RADIO_DEBUG_LOG(type, level, ...)                                \
 | 
			
		||||
do                                                                            \
 | 
			
		||||
{                                                                             \
 | 
			
		||||
    if (type)                                                                 \
 | 
			
		||||
    {                                                                         \
 | 
			
		||||
        printf(__VA_ARGS__);                                                  \
 | 
			
		||||
        printf("\r\n");                                                       \
 | 
			
		||||
    }                                                                         \
 | 
			
		||||
}                                                                             \
 | 
			
		||||
while (0)  
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#else /* LORA_RADIO_DEBUG */
 | 
			
		||||
 | 
			
		||||
#define LORA_RADIO_DEBUG_LOG(type, level, ...)
 | 
			
		||||
 | 
			
		||||
#endif /* LORA_RADIO_DEBUG */
 | 
			
		||||
 | 
			
		||||
#endif /* __LORA_RADIO_DEBUG_H__ */
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,72 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-radio-rtos-config.h
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     adapter to different RTOS implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file lora-radio-rtos-config.h
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-07
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: lora-radio-rtos-config.h
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-07
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 
 | 
			
		||||
#ifndef __LORA_RADIO_RTOS_CONFIG_H_
 | 
			
		||||
#define __LORA_RADIO_RTOS_CONFIG_H_
 | 
			
		||||
 | 
			
		||||
#include <transform.h>
 | 
			
		||||
 | 
			
		||||
// default based on rt-thread
 | 
			
		||||
//#define LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD 
 | 
			
		||||
//#define LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD_NANO
 | 
			
		||||
//#define LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#elif defined LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD_NANO
 | 
			
		||||
#elif defined LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
#else
 | 
			
		||||
    #error "Please Choose your RTOS setup!"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#include "rtconfig.h"
 | 
			
		||||
#include <rtthread.h>
 | 
			
		||||
#include <rtdevice.h>
 | 
			
		||||
#include "drv_spi.h"
 | 
			
		||||
 | 
			
		||||
#define LOG_LVL_ASSERT                 0
 | 
			
		||||
#define LOG_LVL_ERROR                  3
 | 
			
		||||
#define LOG_LVL_WARNING                4
 | 
			
		||||
#define LOG_LVL_INFO                   6
 | 
			
		||||
#define LOG_LVL_DBG                    7
 | 
			
		||||
 | 
			
		||||
#elif defined LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD_NANO
 | 
			
		||||
#include <rtthread.h>
 | 
			
		||||
 | 
			
		||||
#elif defined LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
 | 
			
		||||
#define LOG_LVL_ASSERT                 0
 | 
			
		||||
#define LOG_LVL_ERROR                  3
 | 
			
		||||
#define LOG_LVL_WARNING                4
 | 
			
		||||
#define LOG_LVL_INFO                   6
 | 
			
		||||
#define LOG_LVL_DBG                    7
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif // end of __LORA_RADIO_RTOS_CONFIG_H_
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,549 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-radio.h
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     LoRa Radio driver API definition
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
  *
 | 
			
		||||
 * \author    forest-rain
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    AIIT XUOS Lab
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file lora-radio.h
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-07
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: lora-radio.h
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-07
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#ifndef __LORA_RADIO_H__
 | 
			
		||||
#define __LORA_RADIO_H__
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <transform.h>
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * LoRa Radio software version number
 | 
			
		||||
 */
 | 
			
		||||
#define LORA_RADIO_SW_VERSION                 "1.4.5"
 | 
			
		||||
#define LORA_RADIO_SW_VERSION_NUM              0x10405
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
/*!
 | 
			
		||||
 * Begins critical section
 | 
			
		||||
 */
 | 
			
		||||
#define LORA_RADIO_CRITICAL_SECTION_BEGIN( )  register rt_base_t level; level = rt_hw_interrupt_disable()
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Ends critical section
 | 
			
		||||
 */
 | 
			
		||||
#define LORA_RADIO_CRITICAL_SECTION_END( ) rt_hw_interrupt_enable(level)
 | 
			
		||||
 | 
			
		||||
#elif defined LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
/*!
 | 
			
		||||
 * Begins critical section
 | 
			
		||||
 */
 | 
			
		||||
#define LORA_RADIO_CRITICAL_SECTION_BEGIN( ) 
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Ends critical section
 | 
			
		||||
 */
 | 
			
		||||
#define LORA_RADIO_CRITICAL_SECTION_END( ) 
 | 
			
		||||
#else
 | 
			
		||||
/*!
 | 
			
		||||
 * Begins critical section
 | 
			
		||||
 */
 | 
			
		||||
#define LORA_RADIO_CRITICAL_SECTION_BEGIN(x)  register uint32_t level; level = __get_PRIMASK(); __disable_irq();
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Ends critical section
 | 
			
		||||
 */
 | 
			
		||||
#define LORA_RADIO_CRITICAL_SECTION_END(x)    __set_PRIMASK(level)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/** @addtogroup LORA_RADIO_UPPER_API
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio driver supported modems
 | 
			
		||||
 */
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
    MODEM_FSK = 0,
 | 
			
		||||
    MODEM_LORA,
 | 
			
		||||
    MODEM_BPSK,       //!<for STM32WL
 | 
			
		||||
    MODEM_SIGFOX_TX,  //!<for STM32WL
 | 
			
		||||
    MODEM_SIGFOX_RX,  //!<for STM32WL    
 | 
			
		||||
}RadioModems_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio driver internal state machine states definition
 | 
			
		||||
 */
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
    RF_IDLE = 0,   //!< The radio is idle
 | 
			
		||||
    RF_RX_RUNNING, //!< The radio is in reception state
 | 
			
		||||
    RF_TX_RUNNING, //!< The radio is in transmission state
 | 
			
		||||
    RF_CAD,        //!< The radio is doing channel activity detection
 | 
			
		||||
}RadioState_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio Dio Irq event definition
 | 
			
		||||
 */
 | 
			
		||||
typedef enum
 | 
			
		||||
{
 | 
			
		||||
    EV_LORA_RADIO_IRQ0_FIRED       =   0x0001,
 | 
			
		||||
    EV_LORA_RADIO_IRQ1_FIRED       =   0x0002,
 | 
			
		||||
    EV_LORA_RADIO_IRQ2_FIRED       =   0x0004,
 | 
			
		||||
    EV_LORA_RADIO_IRQ3_FIRED       =   0x0008,
 | 
			
		||||
    EV_LORA_RADIO_IRQ4_FIRED       =   0x0010,
 | 
			
		||||
    EV_LORA_RADIO_IRQ5_FIRED       =   0x0020,
 | 
			
		||||
    EV_LORA_RADIO_TIMEOUT_FIRED    =   0x0040, //!< sx127x tx\rx timeout
 | 
			
		||||
}RadioDioIrqEvent_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Radio driver callback functions
 | 
			
		||||
 */
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief  Tx Done callback prototype.
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *TxDone )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief  Tx Timeout callback prototype.
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *TxTimeout )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Rx Done callback prototype.
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] payload Received buffer pointer
 | 
			
		||||
     * \param [IN] size    Received buffer size
 | 
			
		||||
     * \param [IN] rssi    RSSI value computed while receiving the frame [dBm]
 | 
			
		||||
     * \param [IN] snr     SNR value computed while receiving the frame [dB]
 | 
			
		||||
     *                     FSK : N/A ( set to 0 )
 | 
			
		||||
     *                     LoRa: SNR value in dB
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *RxDone )( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief  Rx Timeout callback prototype.
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *RxTimeout )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Rx Error callback prototype.
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *RxError )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief  FHSS Change Channel callback prototype.
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] currentChannel   Index number of the current channel
 | 
			
		||||
     */
 | 
			
		||||
    void ( *FhssChangeChannel )( uint8_t currentChannel );
 | 
			
		||||
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief CAD Done callback prototype.
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] channelDetected    Channel Activity detected during the CAD
 | 
			
		||||
     */
 | 
			
		||||
    void ( *CadDone ) ( bool channelActivityDetected );
 | 
			
		||||
 | 
			
		||||
}RadioEvents_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Radio driver definition
 | 
			
		||||
 */
 | 
			
		||||
struct Radio_s
 | 
			
		||||
{
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Initializes the radio
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] events Structure containing the driver callback functions
 | 
			
		||||
     */
 | 
			
		||||
    bool    ( *Init )( RadioEvents_t *events );
 | 
			
		||||
    /*!
 | 
			
		||||
     * Return current radio status
 | 
			
		||||
     *
 | 
			
		||||
     * \param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
 | 
			
		||||
     */
 | 
			
		||||
    RadioState_t ( *GetStatus )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Configures the radio with the given modem
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] modem Modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *SetModem )( RadioModems_t modem );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the channel frequency
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] freq         Channel RF frequency
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *SetChannel )( uint32_t freq );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Checks if the channel is free for the given time
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
     * \param [IN] freq       Channel RF frequency
 | 
			
		||||
     * \param [IN] rssiThresh RSSI threshold
 | 
			
		||||
     * \param [IN] maxCarrierSenseTime Max time while the RSSI is measured
 | 
			
		||||
     *
 | 
			
		||||
     * \retval isFree         [true: Channel is free, false: Channel is not free]
 | 
			
		||||
     */
 | 
			
		||||
    bool    ( *IsChannelFree )( RadioModems_t modem, uint32_t freq, int16_t rssiThresh, uint32_t maxCarrierSenseTime );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Generates a 32 bits random value based on the RSSI readings
 | 
			
		||||
     *
 | 
			
		||||
     * \remark This function sets the radio in LoRa modem mode and disables
 | 
			
		||||
     *         all interrupts.
 | 
			
		||||
     *         After calling this function either Radio.SetRxConfig or
 | 
			
		||||
     *         Radio.SetTxConfig functions must be called.
 | 
			
		||||
     *
 | 
			
		||||
     * \retval randomValue    32 bits random value
 | 
			
		||||
     */
 | 
			
		||||
    uint32_t ( *Random )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the reception parameters
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
     * \param [IN] bandwidth    Sets the bandwidth
 | 
			
		||||
     *                          FSK : >= 2600 and <= 250000 Hz
 | 
			
		||||
     *                          LoRa: [0: 125 kHz, 1: 250 kHz,
 | 
			
		||||
     *                                 2: 500 kHz, 3: Reserved]
 | 
			
		||||
     * \param [IN] datarate     Sets the Datarate
 | 
			
		||||
     *                          FSK : 600..300000 bits/s
 | 
			
		||||
     *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
 | 
			
		||||
     *                                10: 1024, 11: 2048, 12: 4096  chips]
 | 
			
		||||
     * \param [IN] coderate     Sets the coding rate (LoRa only)
 | 
			
		||||
     *                          FSK : N/A ( set to 0 )
 | 
			
		||||
     *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
 | 
			
		||||
     * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only)
 | 
			
		||||
     *                          FSK : >= 2600 and <= 250000 Hz
 | 
			
		||||
     *                          LoRa: N/A ( set to 0 )
 | 
			
		||||
     * \param [IN] preambleLen  Sets the Preamble length
 | 
			
		||||
     *                          FSK : Number of bytes
 | 
			
		||||
     *                          LoRa: Length in symbols (the hardware adds 4 more symbols)
 | 
			
		||||
     * \param [IN] symbTimeout  Sets the RxSingle timeout value
 | 
			
		||||
     *                          FSK : timeout in number of bytes
 | 
			
		||||
     *                          LoRa: timeout in symbols
 | 
			
		||||
     * \param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
 | 
			
		||||
     * \param [IN] payloadLen   Sets payload length when fixed length is used
 | 
			
		||||
     * \param [IN] crcOn        Enables/Disables the CRC [0: OFF, 1: ON]
 | 
			
		||||
     * \param [IN] freqHopOn    Enables disables the intra-packet frequency hopping
 | 
			
		||||
     *                          FSK : N/A ( set to 0 )
 | 
			
		||||
     *                          LoRa: [0: OFF, 1: ON]
 | 
			
		||||
     * \param [IN] hopPeriod    Number of symbols between each hop
 | 
			
		||||
     *                          FSK : N/A ( set to 0 )
 | 
			
		||||
     *                          LoRa: Number of symbols
 | 
			
		||||
     * \param [IN] iqInverted   Inverts IQ signals (LoRa only)
 | 
			
		||||
     *                          FSK : N/A ( set to 0 )
 | 
			
		||||
     *                          LoRa: [0: not inverted, 1: inverted]
 | 
			
		||||
     * \param [IN] rxContinuous Sets the reception in continuous mode
 | 
			
		||||
     *                          [false: single mode, true: continuous mode]
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *SetRxConfig )( RadioModems_t modem, uint32_t bandwidth,
 | 
			
		||||
                              uint32_t datarate, uint8_t coderate,
 | 
			
		||||
                              uint32_t bandwidthAfc, uint16_t preambleLen,
 | 
			
		||||
                              uint16_t symbTimeout, bool fixLen,
 | 
			
		||||
                              uint8_t payloadLen,
 | 
			
		||||
                              bool crcOn, bool freqHopOn, uint8_t hopPeriod,
 | 
			
		||||
                              bool iqInverted, bool rxContinuous );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the transmission parameters
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
     * \param [IN] power        Sets the output power [dBm]
 | 
			
		||||
     * \param [IN] fdev         Sets the frequency deviation (FSK only)
 | 
			
		||||
     *                          FSK : [Hz]
 | 
			
		||||
     *                          LoRa: 0
 | 
			
		||||
     * \param [IN] bandwidth    Sets the bandwidth (LoRa only)
 | 
			
		||||
     *                          FSK : 0
 | 
			
		||||
     *                          LoRa: [0: 125 kHz, 1: 250 kHz,
 | 
			
		||||
     *                                 2: 500 kHz, 3: Reserved]
 | 
			
		||||
     * \param [IN] datarate     Sets the Datarate
 | 
			
		||||
     *                          FSK : 600..300000 bits/s
 | 
			
		||||
     *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
 | 
			
		||||
     *                                10: 1024, 11: 2048, 12: 4096  chips]
 | 
			
		||||
     * \param [IN] coderate     Sets the coding rate (LoRa only)
 | 
			
		||||
     *                          FSK : N/A ( set to 0 )
 | 
			
		||||
     *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
 | 
			
		||||
     * \param [IN] preambleLen  Sets the preamble length
 | 
			
		||||
     *                          FSK : Number of bytes
 | 
			
		||||
     *                          LoRa: Length in symbols (the hardware adds 4 more symbols)
 | 
			
		||||
     * \param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
 | 
			
		||||
     * \param [IN] crcOn        Enables disables the CRC [0: OFF, 1: ON]
 | 
			
		||||
     * \param [IN] freqHopOn    Enables disables the intra-packet frequency hopping
 | 
			
		||||
     *                          FSK : N/A ( set to 0 )
 | 
			
		||||
     *                          LoRa: [0: OFF, 1: ON]
 | 
			
		||||
     * \param [IN] hopPeriod    Number of symbols between each hop
 | 
			
		||||
     *                          FSK : N/A ( set to 0 )
 | 
			
		||||
     *                          LoRa: Number of symbols
 | 
			
		||||
     * \param [IN] iqInverted   Inverts IQ signals (LoRa only)
 | 
			
		||||
     *                          FSK : N/A ( set to 0 )
 | 
			
		||||
     *                          LoRa: [0: not inverted, 1: inverted]
 | 
			
		||||
     * \param [IN] timeout      Transmission timeout [ms]
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *SetTxConfig )( RadioModems_t modem, int8_t power, uint32_t fdev,
 | 
			
		||||
                              uint32_t bandwidth, uint32_t datarate,
 | 
			
		||||
                              uint8_t coderate, uint16_t preambleLen,
 | 
			
		||||
                              bool fixLen, bool crcOn, bool freqHopOn,
 | 
			
		||||
                              uint8_t hopPeriod, bool iqInverted, uint32_t timeout );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Checks if the given RF frequency is supported by the hardware
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] frequency RF frequency to be checked
 | 
			
		||||
     * \retval isSupported [true: supported, false: unsupported]
 | 
			
		||||
     */
 | 
			
		||||
    bool    ( *CheckRfFrequency )( uint32_t frequency );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Computes the packet time on air in ms for the given payload
 | 
			
		||||
     *
 | 
			
		||||
     * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
     * \param [IN] bandwidth    Sets the bandwidth
 | 
			
		||||
     *                          FSK : >= 2600 and <= 250000 Hz
 | 
			
		||||
     *                          LoRa: [0: 125 kHz, 1: 250 kHz,
 | 
			
		||||
     *                                 2: 500 kHz, 3: Reserved]
 | 
			
		||||
     * \param [IN] datarate     Sets the Datarate
 | 
			
		||||
     *                          FSK : 600..300000 bits/s
 | 
			
		||||
     *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
 | 
			
		||||
     *                                10: 1024, 11: 2048, 12: 4096  chips]
 | 
			
		||||
     * \param [IN] coderate     Sets the coding rate (LoRa only)
 | 
			
		||||
     *                          FSK : N/A ( set to 0 )
 | 
			
		||||
     *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
 | 
			
		||||
     * \param [IN] preambleLen  Sets the Preamble length
 | 
			
		||||
     *                          FSK : Number of bytes
 | 
			
		||||
     *                          LoRa: Length in symbols (the hardware adds 4 more symbols)
 | 
			
		||||
     * \param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
 | 
			
		||||
     * \param [IN] payloadLen   Sets payload length when fixed length is used
 | 
			
		||||
     * \param [IN] crcOn        Enables/Disables the CRC [0: OFF, 1: ON]
 | 
			
		||||
     *
 | 
			
		||||
     * \retval airTime        Computed airTime (ms) for the given packet payload length
 | 
			
		||||
     */
 | 
			
		||||
    uint32_t  ( *TimeOnAir )( RadioModems_t modem, uint32_t bandwidth,
 | 
			
		||||
                              uint32_t datarate, uint8_t coderate,
 | 
			
		||||
                              uint16_t preambleLen, bool fixLen, uint8_t payloadLen,
 | 
			
		||||
                              bool crcOn );
 | 
			
		||||
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sends the buffer of size. Prepares the packet to be sent and sets
 | 
			
		||||
     *        the radio in transmission
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN]: buffer     Buffer pointer
 | 
			
		||||
     * \param [IN]: size       Buffer size
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *Send )( uint8_t *buffer, uint8_t size );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the radio in sleep mode
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *Sleep )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the radio in standby mode
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *Standby )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the radio in reception mode for the given time
 | 
			
		||||
     * \param [IN] timeout Reception timeout [ms]
 | 
			
		||||
     *                     [0: continuous, others timeout]
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *Rx )( uint32_t timeout );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Start a Channel Activity Detection
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *StartCad )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the radio in continuous wave transmission mode
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN]: freq       Channel RF frequency
 | 
			
		||||
     * \param [IN]: power      Sets the output power [dBm]
 | 
			
		||||
     * \param [IN]: time       Transmission mode timeout [s]
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *SetTxContinuousWave )( uint32_t freq, int8_t power, uint16_t time );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Reads the current RSSI value
 | 
			
		||||
     *
 | 
			
		||||
     * \retval rssiValue Current RSSI value in [dBm]
 | 
			
		||||
     */
 | 
			
		||||
    int16_t ( *Rssi )( RadioModems_t modem );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Writes the radio register at the specified address
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN]: addr Register address
 | 
			
		||||
     * \param [IN]: data New register value
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *Write )( uint16_t addr, uint8_t data );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Reads the radio register at the specified address
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN]: addr Register address
 | 
			
		||||
     * \retval data Register value
 | 
			
		||||
     */
 | 
			
		||||
    uint8_t ( *Read )( uint16_t addr );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Writes multiple radio registers starting at address
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] addr   First Radio register address
 | 
			
		||||
     * \param [IN] buffer Buffer containing the new register's values
 | 
			
		||||
     * \param [IN] size   Number of registers to be written
 | 
			
		||||
     */
 | 
			
		||||
    //void    ( *WriteBuffer )( uint16_t addr, uint8_t *buffer, uint8_t size );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Reads multiple radio registers starting at address
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] addr First Radio register address
 | 
			
		||||
     * \param [OUT] buffer Buffer where to copy the registers data
 | 
			
		||||
     * \param [IN] size Number of registers to be read
 | 
			
		||||
     */
 | 
			
		||||
    //void    ( *ReadBuffer )( uint16_t addr, uint8_t *buffer, uint8_t size );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the maximum payload length.
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
     * \param [IN] max        Maximum payload length in bytes
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *SetMaxPayloadLength )( RadioModems_t modem, uint8_t max );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the network to public or private. Updates the sync byte.
 | 
			
		||||
     *
 | 
			
		||||
     * \remark Applies to LoRa modem only
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] enable if true, it enables a public network
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *SetPublicNetwork )( bool enable );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Gets the time required for the board plus radio to get out of sleep.[ms]
 | 
			
		||||
     *
 | 
			
		||||
     * \retval time Radio plus board wakeup time in ms.
 | 
			
		||||
     */
 | 
			
		||||
    uint32_t  ( *GetWakeupTime )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Process radio irq
 | 
			
		||||
     */
 | 
			
		||||
    void ( *IrqProcess )( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief radio spi check
 | 
			
		||||
     */
 | 
			
		||||
    uint8_t ( *Check )( void );
 | 
			
		||||
 
 | 
			
		||||
    /*
 | 
			
		||||
     * The next functions are available only on SX126x radios.
 | 
			
		||||
     */
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the radio in reception mode with Max LNA gain for the given time
 | 
			
		||||
     *
 | 
			
		||||
     * \remark Available on SX126x radios only.
 | 
			
		||||
     *
 | 
			
		||||
     * \param [IN] timeout Reception timeout [ms]
 | 
			
		||||
     *                     [0: continuous, others timeout]
 | 
			
		||||
     */
 | 
			
		||||
    void    ( *RxBoosted )( uint32_t timeout );
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief Sets the Rx duty cycle management parameters
 | 
			
		||||
     *
 | 
			
		||||
     * \remark Available on SX126x radios only.
 | 
			
		||||
     *
 | 
			
		||||
     * \param [in]  rxTime        Structure describing reception timeout value
 | 
			
		||||
     * \param [in]  sleepTime     Structure describing sleep timeout value
 | 
			
		||||
     */
 | 
			
		||||
    void ( *SetRxDutyCycle ) ( uint32_t rxTime, uint32_t sleepTime );
 | 
			
		||||
    
 | 
			
		||||
    /*!
 | 
			
		||||
     * @brief Sets the Transmitter in continuous PRBS mode for STM32WL
 | 
			
		||||
     *
 | 
			
		||||
     * \remark power and datarate shall be configured prior calling TxPrbs
 | 
			
		||||
     */
 | 
			
		||||
    void (*TxPrbs) ( void );
 | 
			
		||||
    /*!
 | 
			
		||||
     * @brief Sets the Transmitter in continuous unmodulated Carrier mode for STM32WL
 | 
			
		||||
     */
 | 
			
		||||
    void (*TxCw) ( int8_t power );
 | 
			
		||||
 
 | 
			
		||||
    /*!
 | 
			
		||||
     * \brief radio rxdone timestamp
 | 
			
		||||
     */
 | 
			
		||||
    uint32_t ( *GetRxdoneTimestamp )( void );
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Radio Parameter config definition
 | 
			
		||||
 */
 | 
			
		||||
typedef struct LoRaRadioConfig_s
 | 
			
		||||
{
 | 
			
		||||
    RadioModems_t modem; // LoRa Modem \ FSK modem
 | 
			
		||||
    uint32_t frequency;
 | 
			
		||||
    int8_t txpower;
 | 
			
		||||
    
 | 
			
		||||
    // LoRa
 | 
			
		||||
    uint8_t sf;    // spreadfactor
 | 
			
		||||
    uint8_t bw;    // bandwidth
 | 
			
		||||
    uint8_t cr;    // coderate
 | 
			
		||||
    uint8_t hop_period;
 | 
			
		||||
    uint8_t symb_timeout;
 | 
			
		||||
    
 | 
			
		||||
    union 
 | 
			
		||||
    {
 | 
			
		||||
        struct
 | 
			
		||||
        {
 | 
			
		||||
            uint8_t  iq_inverted:1;
 | 
			
		||||
            //uint8_t  public_network:1;
 | 
			
		||||
            uint8_t  fix_len_enable:1;
 | 
			
		||||
            uint8_t crc_on:1;
 | 
			
		||||
            uint8_t freq_hop_on:1;
 | 
			
		||||
            uint8_t rx_continuous:1;
 | 
			
		||||
        }bits;
 | 
			
		||||
        uint8_t byte;
 | 
			
		||||
    }rf_misc;
 | 
			
		||||
 | 
			
		||||
    // FSK
 | 
			
		||||
    uint32_t fdev;
 | 
			
		||||
    uint32_t datarate;
 | 
			
		||||
    uint32_t fsk_bandwidth;
 | 
			
		||||
    uint32_t fsk_afc_bandwidth;
 | 
			
		||||
    
 | 
			
		||||
    uint16_t preamble_len;
 | 
			
		||||
 | 
			
		||||
}LoRaRadioConfig_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Radio driver
 | 
			
		||||
 *
 | 
			
		||||
 * \remark This variable is defined and initialized in the specific radio
 | 
			
		||||
 *         board implementation
 | 
			
		||||
 */
 | 
			
		||||
extern const struct Radio_s Radio;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#endif // __RADIO_H__
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_FILES := sx126x.c lora-radio-sx126x.c lora-spi-sx126x.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -0,0 +1,324 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-spi-SX126x.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     spi driver implementation for SX126X
 | 
			
		||||
 *
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    AIIT XUOS Lab
 | 
			
		||||
 */
 | 
			
		||||
 
 | 
			
		||||
/**
 | 
			
		||||
* @file lora-spi-SX126x.c
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-13
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: lora-spi-SX126x.c
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-13
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#include <sx126x-board.h>
 | 
			
		||||
 | 
			
		||||
#define LOG_TAG "LoRa.SX126X.SPI"
 | 
			
		||||
#define LOG_LEVEL  LOG_LVL_DBG 
 | 
			
		||||
#include <lora-radio-debug.h>
 | 
			
		||||
 | 
			
		||||
void SX126xWakeup( void )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#ifdef RT_USING_SPI
 | 
			
		||||
    uint8_t msg[2] = { RADIO_GET_STATUS, 0x00 };
 | 
			
		||||
    
 | 
			
		||||
    rt_spi_transfer(SX126x.spi,msg,RT_NULL,2);
 | 
			
		||||
    
 | 
			
		||||
    // Wait for chip to be ready.
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
    
 | 
			
		||||
    // Update operating mode context variable
 | 
			
		||||
    SX126xSetOperatingMode( MODE_STDBY_RC );
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
    
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    int spi_fd;
 | 
			
		||||
    uint8_t msg[2] = { RADIO_GET_STATUS, 0x00 };
 | 
			
		||||
    
 | 
			
		||||
    spi_fd = PrivOpen(LORA_SPI_DEV_NAME, O_RDWR);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, msg, 2);
 | 
			
		||||
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
    
 | 
			
		||||
    // Wait for chip to be ready.
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
    
 | 
			
		||||
    // Update operating mode context variable
 | 
			
		||||
    SX126xSetOperatingMode( MODE_STDBY_RC );
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xWriteCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD        
 | 
			
		||||
#ifdef RT_USING_SPI
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
 | 
			
		||||
    rt_spi_send_then_send(SX126x.spi,&command,1,buffer,size);
 | 
			
		||||
    
 | 
			
		||||
    if( command != RADIO_SET_SLEEP )
 | 
			
		||||
    {
 | 
			
		||||
        SX126xWaitOnBusy( );
 | 
			
		||||
    }
 | 
			
		||||
#endif 
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    int spi_fd;
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
 | 
			
		||||
    spi_fd = PrivOpen(LORA_SPI_DEV_NAME, O_RDWR);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, &command, 1);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, buffer, size);
 | 
			
		||||
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
    
 | 
			
		||||
    if( command != RADIO_SET_SLEEP )
 | 
			
		||||
    {
 | 
			
		||||
        SX126xWaitOnBusy( );
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t SX126xReadCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#ifdef RT_USING_SPI
 | 
			
		||||
    uint8_t status = 0;
 | 
			
		||||
    uint8_t buffer_temp[16] = {0}; // command size is 2 size
 | 
			
		||||
    
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
    
 | 
			
		||||
    rt_spi_send_then_recv(SX126x.spi,&command,1,buffer_temp,size + 1);
 | 
			
		||||
    
 | 
			
		||||
    status = buffer_temp[0];
 | 
			
		||||
    
 | 
			
		||||
    rt_memcpy(buffer,buffer_temp+1,size);
 | 
			
		||||
    
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
    
 | 
			
		||||
    return status;
 | 
			
		||||
#endif
 | 
			
		||||
#endif    
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    int spi_fd;
 | 
			
		||||
    uint8_t status = 0;
 | 
			
		||||
    uint8_t buffer_temp[16] = {0}; // command size is 2 size
 | 
			
		||||
    
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
 | 
			
		||||
    spi_fd = PrivOpen(LORA_SPI_DEV_NAME, O_RDWR);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, &command, 1);
 | 
			
		||||
 | 
			
		||||
    PrivRead(spi_fd, buffer_temp, size + 1);
 | 
			
		||||
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
    
 | 
			
		||||
    status = buffer_temp[0];
 | 
			
		||||
    
 | 
			
		||||
    memcpy(buffer, buffer_temp + 1, size);
 | 
			
		||||
    
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
    
 | 
			
		||||
    return status;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xWriteRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#ifdef RT_USING_SPI
 | 
			
		||||
    uint8_t msg[3] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = RADIO_WRITE_REGISTER;
 | 
			
		||||
    msg[1] = ( address & 0xFF00 ) >> 8;
 | 
			
		||||
    msg[2] = address & 0x00FF;
 | 
			
		||||
    
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
    
 | 
			
		||||
    rt_spi_send_then_send(SX126x.spi,msg,3,buffer,size);
 | 
			
		||||
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
#endif
 | 
			
		||||
#endif    
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    int spi_fd;
 | 
			
		||||
    uint8_t msg[3] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = RADIO_WRITE_REGISTER;
 | 
			
		||||
    msg[1] = ( address & 0xFF00 ) >> 8;
 | 
			
		||||
    msg[2] = address & 0x00FF;
 | 
			
		||||
    
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
    
 | 
			
		||||
    spi_fd = PrivOpen(LORA_SPI_DEV_NAME, O_RDWR);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, msg, 3);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, buffer, size);
 | 
			
		||||
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xWriteRegister( uint16_t address, uint8_t value )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteRegisters( address, &value, 1 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xReadRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
 | 
			
		||||
{ 
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#ifdef RT_USING_SPI
 | 
			
		||||
    uint8_t msg[4] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = RADIO_READ_REGISTER;
 | 
			
		||||
    msg[1] = ( address & 0xFF00 ) >> 8;
 | 
			
		||||
    msg[2] = address & 0x00FF;
 | 
			
		||||
    msg[3] = 0;
 | 
			
		||||
    
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
 | 
			
		||||
    rt_spi_send_then_recv(SX126x.spi,msg,4,buffer,size);
 | 
			
		||||
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    int spi_fd;
 | 
			
		||||
    uint8_t msg[4] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = RADIO_READ_REGISTER;
 | 
			
		||||
    msg[1] = ( address & 0xFF00 ) >> 8;
 | 
			
		||||
    msg[2] = address & 0x00FF;
 | 
			
		||||
    msg[3] = 0;
 | 
			
		||||
    
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
 | 
			
		||||
    spi_fd = PrivOpen(LORA_SPI_DEV_NAME, O_RDWR);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, msg, 4);
 | 
			
		||||
 | 
			
		||||
    PrivRead(spi_fd, buffer, size);
 | 
			
		||||
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t SX126xReadRegister( uint16_t address )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t data;
 | 
			
		||||
    SX126xReadRegisters( address, &data, 1 );
 | 
			
		||||
    return data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xWriteBuffer( uint8_t offset, uint8_t *buffer, uint8_t size )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#ifdef RT_USING_SPI
 | 
			
		||||
 | 
			
		||||
    uint8_t msg[2] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = RADIO_WRITE_BUFFER;
 | 
			
		||||
    msg[1] = offset;
 | 
			
		||||
    
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
    
 | 
			
		||||
    rt_spi_send_then_send(SX126x.spi,msg,2,buffer,size);
 | 
			
		||||
    
 | 
			
		||||
    SX126xWaitOnBusy( );  
 | 
			
		||||
 | 
			
		||||
#endif    
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    int spi_fd;
 | 
			
		||||
    uint8_t msg[2] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = RADIO_WRITE_BUFFER;
 | 
			
		||||
    msg[1] = offset;
 | 
			
		||||
    
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
 | 
			
		||||
    spi_fd = PrivOpen(LORA_SPI_DEV_NAME, O_RDWR);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, msg, 2);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, buffer, size);
 | 
			
		||||
    
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
 | 
			
		||||
    SX126xWaitOnBusy( );  
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xReadBuffer( uint8_t offset, uint8_t *buffer, uint8_t size )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#ifdef RT_USING_SPI
 | 
			
		||||
    uint8_t msg[3] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = RADIO_READ_BUFFER;
 | 
			
		||||
    msg[1] = offset;
 | 
			
		||||
    msg[2] = 0;
 | 
			
		||||
    
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
 | 
			
		||||
    rt_spi_send_then_recv(SX126x.spi,msg,3,buffer,size);
 | 
			
		||||
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
#endif    
 | 
			
		||||
#endif
 | 
			
		||||
    
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    int spi_fd;
 | 
			
		||||
    uint8_t msg[3] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = RADIO_READ_BUFFER;
 | 
			
		||||
    msg[1] = offset;
 | 
			
		||||
    msg[2] = 0;
 | 
			
		||||
    
 | 
			
		||||
    SX126xCheckDeviceReady( );
 | 
			
		||||
 | 
			
		||||
    spi_fd = PrivOpen(LORA_SPI_DEV_NAME, O_RDWR);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, msg, 3);
 | 
			
		||||
 | 
			
		||||
    PrivRead(spi_fd, buffer, size);
 | 
			
		||||
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,111 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-spi-sx126x.h
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     SX126x spi interface
 | 
			
		||||
 *
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
#ifndef __LORA_SPI_SX126X_H__
 | 
			
		||||
#define __LORA_SPI_SX126X_H__
 | 
			
		||||
 | 
			
		||||
#include "sx126x.h"
 | 
			
		||||
 | 
			
		||||
/** @addtogroup LORA_RADIO_SPI
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @addtogroup SX126X_SPI
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Wakes up the radio
 | 
			
		||||
 */
 | 
			
		||||
void SX126xWakeup( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Send a command that write data to the radio
 | 
			
		||||
 *
 | 
			
		||||
 * \param [in]  opcode        Opcode of the command
 | 
			
		||||
 * \param [in]  buffer        Buffer to be send to the radio
 | 
			
		||||
 * \param [in]  size          Size of the buffer to send
 | 
			
		||||
 */
 | 
			
		||||
void SX126xWriteCommand( RadioCommands_t opcode, uint8_t *buffer, uint16_t size );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Send a command that read data from the radio
 | 
			
		||||
 *
 | 
			
		||||
 * \param [in]  opcode        Opcode of the command
 | 
			
		||||
 * \param [out] buffer        Buffer holding data from the radio
 | 
			
		||||
 * \param [in]  size          Size of the buffer
 | 
			
		||||
 *
 | 
			
		||||
 * \retval status Return command radio status
 | 
			
		||||
 */
 | 
			
		||||
uint8_t SX126xReadCommand( RadioCommands_t opcode, uint8_t *buffer, uint16_t size );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Write a single byte of data to the radio memory
 | 
			
		||||
 *
 | 
			
		||||
 * \param [in]  address       The address of the first byte to write in the radio
 | 
			
		||||
 * \param [in]  value         The data to be written in radio's memory
 | 
			
		||||
 */
 | 
			
		||||
void SX126xWriteRegister( uint16_t address, uint8_t value );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Read a single byte of data from the radio memory
 | 
			
		||||
 *
 | 
			
		||||
 * \param [in]  address       The address of the first byte to write in the radio
 | 
			
		||||
 *
 | 
			
		||||
 * \retval      value         The value of the byte at the given address in radio's memory
 | 
			
		||||
 */
 | 
			
		||||
uint8_t SX126xReadRegister( uint16_t address );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Write data to the radio memory
 | 
			
		||||
 *
 | 
			
		||||
 * \param [in]  address       The address of the first byte to write in the radio
 | 
			
		||||
 * \param [in]  buffer        The data to be written in radio's memory
 | 
			
		||||
 * \param [in]  size          The number of bytes to write in radio's memory
 | 
			
		||||
 */
 | 
			
		||||
void SX126xWriteRegisters( uint16_t address, uint8_t *buffer, uint16_t size );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Read data from the radio memory
 | 
			
		||||
 *
 | 
			
		||||
 * \param [in]  address       The address of the first byte to read from the radio
 | 
			
		||||
 * \param [out] buffer        The buffer that holds data read from radio
 | 
			
		||||
 * \param [in]  size          The number of bytes to read from radio's memory
 | 
			
		||||
 */
 | 
			
		||||
void SX126xReadRegisters( uint16_t address, uint8_t *buffer, uint16_t size );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Write data to the buffer holding the payload in the radio
 | 
			
		||||
 *
 | 
			
		||||
 * \param [in]  offset        The offset to start writing the payload
 | 
			
		||||
 * \param [in]  buffer        The data to be written (the payload)
 | 
			
		||||
 * \param [in]  size          The number of byte to be written
 | 
			
		||||
 */
 | 
			
		||||
void SX126xWriteBuffer( uint8_t offset, uint8_t *buffer, uint8_t size );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Read data from the buffer holding the payload in the radio
 | 
			
		||||
 *
 | 
			
		||||
 * \param [in]  offset        The offset to start reading the payload
 | 
			
		||||
 * \param [out] buffer        A pointer to a buffer holding the data from the radio
 | 
			
		||||
 * \param [in]  size          The number of byte to be read
 | 
			
		||||
 */
 | 
			
		||||
void SX126xReadBuffer( uint8_t offset, uint8_t *buffer, uint8_t size );
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#endif /* __LORA_SPI_SX126X_H__ */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,806 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      sx126x.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     SX126x driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 */
 | 
			
		||||
 
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
#include <board.h>
 | 
			
		||||
#include <lora-radio-timer.h>
 | 
			
		||||
#include <lora-radio.h>
 | 
			
		||||
#include <lora-spi-sx126x.h>
 | 
			
		||||
#include <sx126x-board.h>
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Radio registers definition
 | 
			
		||||
 */
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    uint16_t      Addr;                             //!< The address of the register
 | 
			
		||||
    uint8_t       Value;                            //!< The value of the register
 | 
			
		||||
}RadioRegisters_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Holds the internal operating mode of the radio
 | 
			
		||||
 */
 | 
			
		||||
static RadioOperatingModes_t OperatingMode;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Stores the current packet type set in the radio
 | 
			
		||||
 */
 | 
			
		||||
static RadioPacketTypes_t PacketType;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Stores the current packet header type set in the radio
 | 
			
		||||
 */
 | 
			
		||||
static volatile RadioLoRaPacketLengthsMode_t LoRaHeaderType;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Stores the last frequency error measured on LoRa received packet
 | 
			
		||||
 */
 | 
			
		||||
volatile uint32_t FrequencyError = 0;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Hold the status of the Image calibration
 | 
			
		||||
 */
 | 
			
		||||
static bool ImageCalibrated = false;
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * SX126x DIO IRQ callback functions prototype
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 0 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX126xOnDioIrq( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 0 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX126xSetPollingMode( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 0 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX126xSetInterruptMode( void );
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * \brief Process the IRQ if handled by the driver
 | 
			
		||||
 */
 | 
			
		||||
void SX126xProcessIrqs( void );
 | 
			
		||||
 | 
			
		||||
void SX126xInit( DioIrqHandler dioIrq )
 | 
			
		||||
{   
 | 
			
		||||
    SX126xReset( );
 | 
			
		||||
 | 
			
		||||
    SX126xIoIrqInit( dioIrq );
 | 
			
		||||
 | 
			
		||||
    SX126xWakeup( );
 | 
			
		||||
    SX126xSetStandby( STDBY_RC );
 | 
			
		||||
    
 | 
			
		||||
    // Initialize TCXO control
 | 
			
		||||
    SX126xIoTcxoInit( );
 | 
			
		||||
    
 | 
			
		||||
#ifdef LORA_RADIO_USE_DIO2_AS_RF_SWITCH_CTRL
 | 
			
		||||
    SX126xSetDio2AsRfSwitchCtrl( true );
 | 
			
		||||
#endif
 | 
			
		||||
    
 | 
			
		||||
    SX126xSetOperatingMode( MODE_STDBY_RC );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
RadioOperatingModes_t SX126xGetOperatingMode( void )
 | 
			
		||||
{
 | 
			
		||||
    return OperatingMode;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetOperatingMode( RadioOperatingModes_t mode )
 | 
			
		||||
{
 | 
			
		||||
    OperatingMode = mode;
 | 
			
		||||
 | 
			
		||||
#if defined( LORA_RADIO_RFSW2_PIN ) && defined( LORA_RADIO_RFSW1_PIN )      
 | 
			
		||||
    SX126xSetAntSw( mode );
 | 
			
		||||
#endif
 | 
			
		||||
    
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
    switch( mode )
 | 
			
		||||
    {
 | 
			
		||||
        case MODE_TX:
 | 
			
		||||
            SX126xDbgPinTxWrite( 1 );
 | 
			
		||||
            SX126xDbgPinRxWrite( 0 );
 | 
			
		||||
            break;
 | 
			
		||||
        case MODE_RX:
 | 
			
		||||
        case MODE_RX_DC:
 | 
			
		||||
            SX126xDbgPinTxWrite( 0 );
 | 
			
		||||
            SX126xDbgPinRxWrite( 1 );
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            SX126xDbgPinTxWrite( 0 );
 | 
			
		||||
            SX126xDbgPinRxWrite( 0 );
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xCheckDeviceReady( void )
 | 
			
		||||
{
 | 
			
		||||
    if( ( SX126xGetOperatingMode( ) == MODE_SLEEP ) || ( SX126xGetOperatingMode( ) == MODE_RX_DC ) )
 | 
			
		||||
    {
 | 
			
		||||
        SX126xWakeup( );
 | 
			
		||||
        // Switch is turned off when device is in sleep mode and turned on is all other modes
 | 
			
		||||
        SX126xAntSwOn( );
 | 
			
		||||
    }
 | 
			
		||||
    SX126xWaitOnBusy( );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetPayload( uint8_t *payload, uint8_t size )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteBuffer( 0x00, payload, size );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t SX126xGetPayload( uint8_t *buffer, uint8_t *size,  uint8_t maxSize )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t offset = 0;
 | 
			
		||||
 | 
			
		||||
    SX126xGetRxBufferStatus( size, &offset );
 | 
			
		||||
    if( *size > maxSize )
 | 
			
		||||
    {
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    SX126xReadBuffer( offset, buffer, *size );
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSendPayload( uint8_t *payload, uint8_t size, uint32_t timeout )
 | 
			
		||||
{
 | 
			
		||||
    SX126xSetPayload( payload, size );
 | 
			
		||||
    SX126xSetTx( timeout );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t SX126xSetSyncWord( uint8_t *syncWord )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteRegisters( REG_LR_SYNCWORDBASEADDRESS, syncWord, 8 );
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetCrcSeed( uint16_t seed )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[2];
 | 
			
		||||
 | 
			
		||||
    buf[0] = ( uint8_t )( ( seed >> 8 ) & 0xFF );
 | 
			
		||||
    buf[1] = ( uint8_t )( seed & 0xFF );
 | 
			
		||||
 | 
			
		||||
    switch( SX126xGetPacketType( ) )
 | 
			
		||||
    {
 | 
			
		||||
        case PACKET_TYPE_GFSK:
 | 
			
		||||
            SX126xWriteRegisters( REG_LR_CRCSEEDBASEADDR, buf, 2 );
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
        default:
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetCrcPolynomial( uint16_t polynomial )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[2];
 | 
			
		||||
 | 
			
		||||
    buf[0] = ( uint8_t )( ( polynomial >> 8 ) & 0xFF );
 | 
			
		||||
    buf[1] = ( uint8_t )( polynomial & 0xFF );
 | 
			
		||||
 | 
			
		||||
    switch( SX126xGetPacketType( ) )
 | 
			
		||||
    {
 | 
			
		||||
        case PACKET_TYPE_GFSK:
 | 
			
		||||
            SX126xWriteRegisters( REG_LR_CRCPOLYBASEADDR, buf, 2 );
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
        default:
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetWhiteningSeed( uint16_t seed )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t regValue = 0;
 | 
			
		||||
    
 | 
			
		||||
    switch( SX126xGetPacketType( ) )
 | 
			
		||||
    {
 | 
			
		||||
        case PACKET_TYPE_GFSK:
 | 
			
		||||
            regValue = SX126xReadRegister( REG_LR_WHITSEEDBASEADDR_MSB ) & 0xFE;
 | 
			
		||||
            regValue = ( ( seed >> 8 ) & 0x01 ) | regValue;
 | 
			
		||||
            SX126xWriteRegister( REG_LR_WHITSEEDBASEADDR_MSB, regValue ); // only 1 bit.
 | 
			
		||||
            SX126xWriteRegister( REG_LR_WHITSEEDBASEADDR_LSB, ( uint8_t )seed );
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
        default:
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t SX126xGetRandom( void )
 | 
			
		||||
{
 | 
			
		||||
    uint32_t number = 0;
 | 
			
		||||
    uint8_t regAnaLna = 0;
 | 
			
		||||
    uint8_t regAnaMixer = 0;
 | 
			
		||||
 | 
			
		||||
    regAnaLna = SX126xReadRegister( REG_ANA_LNA );
 | 
			
		||||
    SX126xWriteRegister( REG_ANA_LNA, regAnaLna & ~( 1 << 0 ) );
 | 
			
		||||
 | 
			
		||||
    regAnaMixer = SX126xReadRegister( REG_ANA_MIXER );
 | 
			
		||||
    SX126xWriteRegister( REG_ANA_MIXER, regAnaMixer & ~( 1 << 7 ) );
 | 
			
		||||
 | 
			
		||||
    // Set radio in continuous reception
 | 
			
		||||
    SX126xSetRx( 0xFFFFFF ); // Rx Continuous
 | 
			
		||||
 | 
			
		||||
    SX126xReadRegisters( RANDOM_NUMBER_GENERATORBASEADDR, ( uint8_t* )&number, 4 );
 | 
			
		||||
 | 
			
		||||
    SX126xSetStandby( STDBY_RC );
 | 
			
		||||
 | 
			
		||||
    SX126xWriteRegister( REG_ANA_LNA, regAnaLna );
 | 
			
		||||
    SX126xWriteRegister( REG_ANA_MIXER, regAnaMixer );
 | 
			
		||||
 | 
			
		||||
    return number;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetSleep( SleepParams_t sleepConfig )
 | 
			
		||||
{
 | 
			
		||||
    SX126xAntSwOff( );
 | 
			
		||||
    uint8_t value = ( ( ( uint8_t )sleepConfig.Fields.WarmStart << 2 ) |
 | 
			
		||||
                      ( ( uint8_t )sleepConfig.Fields.Reset << 1 ) |
 | 
			
		||||
                      ( ( uint8_t )sleepConfig.Fields.WakeUpRTC ) );
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_SLEEP, &value, 1 ); 
 | 
			
		||||
    SX126xSetOperatingMode( MODE_SLEEP );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetStandby( RadioStandbyModes_t standbyConfig )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_STANDBY, ( uint8_t* )&standbyConfig, 1 );
 | 
			
		||||
    if( standbyConfig == STDBY_RC )
 | 
			
		||||
    {
 | 
			
		||||
        SX126xSetOperatingMode( MODE_STDBY_RC );
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        SX126xSetOperatingMode( MODE_STDBY_XOSC );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetFs( void )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_FS, 0, 0 );
 | 
			
		||||
    SX126xSetOperatingMode( MODE_FS );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetTx( uint32_t timeout )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[3];
 | 
			
		||||
 | 
			
		||||
    SX126xSetOperatingMode( MODE_TX );
 | 
			
		||||
 | 
			
		||||
    buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
 | 
			
		||||
    buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
 | 
			
		||||
    buf[2] = ( uint8_t )( timeout & 0xFF );
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_TX, buf, 3 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetRx( uint32_t timeout )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[3];
 | 
			
		||||
 | 
			
		||||
    SX126xSetOperatingMode( MODE_RX );
 | 
			
		||||
 | 
			
		||||
    buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
 | 
			
		||||
    buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
 | 
			
		||||
    buf[2] = ( uint8_t )( timeout & 0xFF );
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_RX, buf, 3 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetRxBoosted( uint32_t timeout )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[3];
 | 
			
		||||
 | 
			
		||||
    SX126xSetOperatingMode( MODE_RX );
 | 
			
		||||
 | 
			
		||||
    SX126xWriteRegister( REG_RX_GAIN, 0x96 ); // max LNA gain, increase current by ~2mA for around ~3dB in sensivity
 | 
			
		||||
 | 
			
		||||
    buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
 | 
			
		||||
    buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
 | 
			
		||||
    buf[2] = ( uint8_t )( timeout & 0xFF );
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_RX, buf, 3 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[6];
 | 
			
		||||
 | 
			
		||||
    buf[0] = ( uint8_t )( ( rxTime >> 16 ) & 0xFF );
 | 
			
		||||
    buf[1] = ( uint8_t )( ( rxTime >> 8 ) & 0xFF );
 | 
			
		||||
    buf[2] = ( uint8_t )( rxTime & 0xFF );
 | 
			
		||||
    buf[3] = ( uint8_t )( ( sleepTime >> 16 ) & 0xFF );
 | 
			
		||||
    buf[4] = ( uint8_t )( ( sleepTime >> 8 ) & 0xFF );
 | 
			
		||||
    buf[5] = ( uint8_t )( sleepTime & 0xFF );
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_RXDUTYCYCLE, buf, 6 );
 | 
			
		||||
    SX126xSetOperatingMode( MODE_RX_DC );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetCad( void )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_CAD, 0, 0 );
 | 
			
		||||
    SX126xSetOperatingMode( MODE_CAD );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetTxContinuousWave( void )
 | 
			
		||||
{
 | 
			
		||||
    SX126xSetAntSw(MODE_TX);
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_TXCONTINUOUSWAVE, 0, 0 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetTxInfinitePreamble( void )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_TXCONTINUOUSPREAMBLE, 0, 0 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetStopRxTimerOnPreambleDetect( bool enable )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_STOPRXTIMERONPREAMBLE, ( uint8_t* )&enable, 1 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetLoRaSymbNumTimeout( uint8_t symbNum )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_LORASYMBTIMEOUT, &symbNum, 1 );
 | 
			
		||||
    
 | 
			
		||||
    if( symbNum >= 64 )
 | 
			
		||||
    {
 | 
			
		||||
        uint8_t mant = symbNum >> 1;
 | 
			
		||||
        uint8_t exp  = 0;
 | 
			
		||||
        uint8_t reg  = 0;
 | 
			
		||||
 | 
			
		||||
        while( mant > 31 )
 | 
			
		||||
        {
 | 
			
		||||
            mant >>= 2;
 | 
			
		||||
            exp++;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        reg = exp + ( mant << 3 );
 | 
			
		||||
        SX126xWriteRegister( REG_LR_SYNCH_TIMEOUT, reg );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetRegulatorMode( RadioRegulatorMode_t mode )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_REGULATORMODE, ( uint8_t* )&mode, 1 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xCalibrate( CalibrationParams_t calibParam )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t value = ( ( ( uint8_t )calibParam.Fields.ImgEnable << 6 ) |
 | 
			
		||||
                      ( ( uint8_t )calibParam.Fields.ADCBulkPEnable << 5 ) |
 | 
			
		||||
                      ( ( uint8_t )calibParam.Fields.ADCBulkNEnable << 4 ) |
 | 
			
		||||
                      ( ( uint8_t )calibParam.Fields.ADCPulseEnable << 3 ) |
 | 
			
		||||
                      ( ( uint8_t )calibParam.Fields.PLLEnable << 2 ) |
 | 
			
		||||
                      ( ( uint8_t )calibParam.Fields.RC13MEnable << 1 ) |
 | 
			
		||||
                      ( ( uint8_t )calibParam.Fields.RC64KEnable ) );
 | 
			
		||||
 | 
			
		||||
    SX126xWriteCommand( RADIO_CALIBRATE, &value, 1 );  
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xCalibrateImage( uint32_t freq )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t calFreq[2];
 | 
			
		||||
 | 
			
		||||
    if( freq > 900000000 )
 | 
			
		||||
    {
 | 
			
		||||
        calFreq[0] = 0xE1;
 | 
			
		||||
        calFreq[1] = 0xE9;
 | 
			
		||||
    }
 | 
			
		||||
    else if( freq > 850000000 )
 | 
			
		||||
    {
 | 
			
		||||
        calFreq[0] = 0xD7;
 | 
			
		||||
        calFreq[1] = 0xDB;
 | 
			
		||||
    }
 | 
			
		||||
    else if( freq > 770000000 )
 | 
			
		||||
    {
 | 
			
		||||
        calFreq[0] = 0xC1;
 | 
			
		||||
        calFreq[1] = 0xC5;
 | 
			
		||||
    }
 | 
			
		||||
    else if( freq > 460000000 )
 | 
			
		||||
    {
 | 
			
		||||
        calFreq[0] = 0x75;
 | 
			
		||||
        calFreq[1] = 0x81;
 | 
			
		||||
    }
 | 
			
		||||
    else if( freq > 425000000 )
 | 
			
		||||
    {
 | 
			
		||||
        calFreq[0] = 0x6B;
 | 
			
		||||
        calFreq[1] = 0x6F;
 | 
			
		||||
    }
 | 
			
		||||
    SX126xWriteCommand( RADIO_CALIBRATEIMAGE, calFreq, 2 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetPaConfig( uint8_t paDutyCycle, uint8_t hpMax, uint8_t deviceSel, uint8_t paLut )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[4];
 | 
			
		||||
 | 
			
		||||
    buf[0] = paDutyCycle;
 | 
			
		||||
    buf[1] = hpMax;
 | 
			
		||||
    buf[2] = deviceSel;
 | 
			
		||||
    buf[3] = paLut;
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_PACONFIG, buf, 4 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetRxTxFallbackMode( uint8_t fallbackMode )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_TXFALLBACKMODE, &fallbackMode, 1 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetDioIrqParams( uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[8];
 | 
			
		||||
 | 
			
		||||
    buf[0] = ( uint8_t )( ( irqMask >> 8 ) & 0x00FF );
 | 
			
		||||
    buf[1] = ( uint8_t )( irqMask & 0x00FF );
 | 
			
		||||
    buf[2] = ( uint8_t )( ( dio1Mask >> 8 ) & 0x00FF );
 | 
			
		||||
    buf[3] = ( uint8_t )( dio1Mask & 0x00FF );
 | 
			
		||||
    buf[4] = ( uint8_t )( ( dio2Mask >> 8 ) & 0x00FF );
 | 
			
		||||
    buf[5] = ( uint8_t )( dio2Mask & 0x00FF );
 | 
			
		||||
    buf[6] = ( uint8_t )( ( dio3Mask >> 8 ) & 0x00FF );
 | 
			
		||||
    buf[7] = ( uint8_t )( dio3Mask & 0x00FF );
 | 
			
		||||
    SX126xWriteCommand( RADIO_CFG_DIOIRQ, buf, 8 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t SX126xGetIrqStatus( void )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t irqStatus[2];
 | 
			
		||||
 | 
			
		||||
    SX126xReadCommand( RADIO_GET_IRQSTATUS, irqStatus, 2 );
 | 
			
		||||
    return ( irqStatus[0] << 8 ) | irqStatus[1];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetDio2AsRfSwitchCtrl( uint8_t enable )
 | 
			
		||||
{
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_RFSWITCHMODE, &enable, 1 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetDio3AsTcxoCtrl( RadioTcxoCtrlVoltage_t tcxoVoltage, uint32_t timeout )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[4];
 | 
			
		||||
 | 
			
		||||
    buf[0] = tcxoVoltage & 0x07;
 | 
			
		||||
    buf[1] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
 | 
			
		||||
    buf[2] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
 | 
			
		||||
    buf[3] = ( uint8_t )( timeout & 0xFF );
 | 
			
		||||
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_TCXOMODE, buf, 4 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetRfFrequency( uint32_t frequency )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[4];
 | 
			
		||||
    uint32_t freq = 0;
 | 
			
		||||
 | 
			
		||||
    if( ImageCalibrated == false )
 | 
			
		||||
    {
 | 
			
		||||
        SX126xCalibrateImage( frequency );
 | 
			
		||||
        ImageCalibrated = true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    freq = ( uint32_t )( ( double )frequency / ( double )FREQ_STEP );
 | 
			
		||||
    buf[0] = ( uint8_t )( ( freq >> 24 ) & 0xFF );
 | 
			
		||||
    buf[1] = ( uint8_t )( ( freq >> 16 ) & 0xFF );
 | 
			
		||||
    buf[2] = ( uint8_t )( ( freq >> 8 ) & 0xFF );
 | 
			
		||||
    buf[3] = ( uint8_t )( freq & 0xFF );
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_RFFREQUENCY, buf, 4 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetPacketType( RadioPacketTypes_t packetType )
 | 
			
		||||
{
 | 
			
		||||
    // Save packet type internally to avoid questioning the radio
 | 
			
		||||
    PacketType = packetType;
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_PACKETTYPE, ( uint8_t* )&packetType, 1 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
RadioPacketTypes_t SX126xGetPacketType( void )
 | 
			
		||||
{
 | 
			
		||||
    return PacketType;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetTxParams( int8_t power, RadioRampTimes_t rampTime )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[2];
 | 
			
		||||
 | 
			
		||||
////    if( SX126xGetDeviceId( ) == SX1261 )
 | 
			
		||||
////    {
 | 
			
		||||
////        if( power == 15 )
 | 
			
		||||
////        {
 | 
			
		||||
////            SX126xSetPaConfig( 0x06, 0x00, 0x01, 0x01 );
 | 
			
		||||
////        }
 | 
			
		||||
////        else
 | 
			
		||||
////        {
 | 
			
		||||
////            SX126xSetPaConfig( 0x04, 0x00, 0x01, 0x01 );
 | 
			
		||||
////        }
 | 
			
		||||
////        if( power >= 14 )
 | 
			
		||||
////        {
 | 
			
		||||
////            power = 14;
 | 
			
		||||
////        }
 | 
			
		||||
////        else if( power < -17 )
 | 
			
		||||
////        {
 | 
			
		||||
////            power = -17;
 | 
			
		||||
////        }
 | 
			
		||||
////        SX126xWriteRegister( REG_OCP, 0x18 ); // current max is 80 mA for the whole device
 | 
			
		||||
////    }
 | 
			
		||||
////    else // sx1262
 | 
			
		||||
    {
 | 
			
		||||
        // WORKAROUND - Better Resistance of the SX1262 Tx to Antenna Mismatch, see DS_SX1261-2_V1.2 datasheet chapter 15.2
 | 
			
		||||
        // RegTxClampConfig = @address 0x08D8
 | 
			
		||||
        SX126xWriteRegister( 0x08D8, SX126xReadRegister( 0x08D8 ) | ( 0x0F << 1 ) );
 | 
			
		||||
        // WORKAROUND END
 | 
			
		||||
 | 
			
		||||
        SX126xSetPaConfig( 0x04, 0x07, 0x00, 0x01 );
 | 
			
		||||
        if( power > 22 )
 | 
			
		||||
        {
 | 
			
		||||
            power = 22;
 | 
			
		||||
        }
 | 
			
		||||
        else if( power < -9 ) 
 | 
			
		||||
        {
 | 
			
		||||
            power = -9;
 | 
			
		||||
        }
 | 
			
		||||
        SX126xWriteRegister( REG_OCP, 0x38 ); // current max 160mA for the whole device
 | 
			
		||||
    }
 | 
			
		||||
    buf[0] = power;
 | 
			
		||||
    buf[1] = ( uint8_t )rampTime;
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_TXPARAMS, buf, 2 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetRfTxPower( int8_t power )
 | 
			
		||||
{
 | 
			
		||||
    SX126xSetTxParams( power, RADIO_RAMP_40_US );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetModulationParams( ModulationParams_t *modulationParams )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t n;
 | 
			
		||||
    uint32_t tempVal = 0;
 | 
			
		||||
    uint8_t buf[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 | 
			
		||||
 | 
			
		||||
    // Check if required configuration corresponds to the stored packet type
 | 
			
		||||
    // If not, silently update radio packet type
 | 
			
		||||
    if( PacketType != modulationParams->PacketType )
 | 
			
		||||
    {
 | 
			
		||||
        SX126xSetPacketType( modulationParams->PacketType );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    switch( modulationParams->PacketType )
 | 
			
		||||
    {
 | 
			
		||||
    case PACKET_TYPE_GFSK:
 | 
			
		||||
        n = 8;
 | 
			
		||||
        tempVal = ( uint32_t )( 32 * ( ( double )XTAL_FREQ / ( double )modulationParams->Params.Gfsk.BitRate ) );
 | 
			
		||||
        buf[0] = ( tempVal >> 16 ) & 0xFF;
 | 
			
		||||
        buf[1] = ( tempVal >> 8 ) & 0xFF;
 | 
			
		||||
        buf[2] = tempVal & 0xFF;
 | 
			
		||||
        buf[3] = modulationParams->Params.Gfsk.ModulationShaping;
 | 
			
		||||
        buf[4] = modulationParams->Params.Gfsk.Bandwidth;
 | 
			
		||||
        tempVal = ( uint32_t )( ( double )modulationParams->Params.Gfsk.Fdev / ( double )FREQ_STEP );
 | 
			
		||||
        buf[5] = ( tempVal >> 16 ) & 0xFF;
 | 
			
		||||
        buf[6] = ( tempVal >> 8 ) & 0xFF;
 | 
			
		||||
        buf[7] = ( tempVal& 0xFF );
 | 
			
		||||
        SX126xWriteCommand( RADIO_SET_MODULATIONPARAMS, buf, n );
 | 
			
		||||
        break;
 | 
			
		||||
    case PACKET_TYPE_LORA:
 | 
			
		||||
        n = 4;
 | 
			
		||||
        buf[0] = modulationParams->Params.LoRa.SpreadingFactor;
 | 
			
		||||
        buf[1] = modulationParams->Params.LoRa.Bandwidth;
 | 
			
		||||
        buf[2] = modulationParams->Params.LoRa.CodingRate;
 | 
			
		||||
        buf[3] = modulationParams->Params.LoRa.LowDatarateOptimize;
 | 
			
		||||
 | 
			
		||||
        SX126xWriteCommand( RADIO_SET_MODULATIONPARAMS, buf, n );
 | 
			
		||||
 | 
			
		||||
        break;
 | 
			
		||||
    default:
 | 
			
		||||
    case PACKET_TYPE_NONE:
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetPacketParams( PacketParams_t *packetParams )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t n;
 | 
			
		||||
    uint8_t crcVal = 0;
 | 
			
		||||
    uint8_t buf[9] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 | 
			
		||||
 | 
			
		||||
    // Check if required configuration corresponds to the stored packet type
 | 
			
		||||
    // If not, silently update radio packet type
 | 
			
		||||
    if( PacketType != packetParams->PacketType )
 | 
			
		||||
    {
 | 
			
		||||
        SX126xSetPacketType( packetParams->PacketType );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    switch( packetParams->PacketType )
 | 
			
		||||
    {
 | 
			
		||||
    case PACKET_TYPE_GFSK:
 | 
			
		||||
        if( packetParams->Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES_IBM )
 | 
			
		||||
        {
 | 
			
		||||
            SX126xSetCrcSeed( CRC_IBM_SEED );
 | 
			
		||||
            SX126xSetCrcPolynomial( CRC_POLYNOMIAL_IBM );
 | 
			
		||||
            crcVal = RADIO_CRC_2_BYTES;
 | 
			
		||||
        }
 | 
			
		||||
        else if( packetParams->Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES_CCIT )
 | 
			
		||||
        {
 | 
			
		||||
            SX126xSetCrcSeed( CRC_CCITT_SEED );
 | 
			
		||||
            SX126xSetCrcPolynomial( CRC_POLYNOMIAL_CCITT );
 | 
			
		||||
            crcVal = RADIO_CRC_2_BYTES_INV;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            crcVal = packetParams->Params.Gfsk.CrcLength;
 | 
			
		||||
        }
 | 
			
		||||
        n = 9;
 | 
			
		||||
        buf[0] = ( packetParams->Params.Gfsk.PreambleLength >> 8 ) & 0xFF;
 | 
			
		||||
        buf[1] = packetParams->Params.Gfsk.PreambleLength;
 | 
			
		||||
        buf[2] = packetParams->Params.Gfsk.PreambleMinDetect;
 | 
			
		||||
        buf[3] = ( packetParams->Params.Gfsk.SyncWordLength /*<< 3*/ ); // convert from byte to bit
 | 
			
		||||
        buf[4] = packetParams->Params.Gfsk.AddrComp;
 | 
			
		||||
        buf[5] = packetParams->Params.Gfsk.HeaderType;
 | 
			
		||||
        buf[6] = packetParams->Params.Gfsk.PayloadLength;
 | 
			
		||||
        buf[7] = crcVal;
 | 
			
		||||
        buf[8] = packetParams->Params.Gfsk.DcFree;
 | 
			
		||||
        break;
 | 
			
		||||
    case PACKET_TYPE_LORA:
 | 
			
		||||
        n = 6;
 | 
			
		||||
        buf[0] = ( packetParams->Params.LoRa.PreambleLength >> 8 ) & 0xFF;
 | 
			
		||||
        buf[1] = packetParams->Params.LoRa.PreambleLength;
 | 
			
		||||
        buf[2] = LoRaHeaderType = packetParams->Params.LoRa.HeaderType;
 | 
			
		||||
        buf[3] = packetParams->Params.LoRa.PayloadLength;
 | 
			
		||||
        buf[4] = packetParams->Params.LoRa.CrcMode;
 | 
			
		||||
        buf[5] = packetParams->Params.LoRa.InvertIQ;
 | 
			
		||||
        break;
 | 
			
		||||
    default:
 | 
			
		||||
    case PACKET_TYPE_NONE:
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_PACKETPARAMS, buf, n );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetCadParams( RadioLoRaCadSymbols_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin, RadioCadExitModes_t cadExitMode, uint32_t cadTimeout )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[7];
 | 
			
		||||
 | 
			
		||||
    buf[0] = ( uint8_t )cadSymbolNum;
 | 
			
		||||
    buf[1] = cadDetPeak;
 | 
			
		||||
    buf[2] = cadDetMin;
 | 
			
		||||
    buf[3] = ( uint8_t )cadExitMode;
 | 
			
		||||
    buf[4] = ( uint8_t )( ( cadTimeout >> 16 ) & 0xFF );
 | 
			
		||||
    buf[5] = ( uint8_t )( ( cadTimeout >> 8 ) & 0xFF );
 | 
			
		||||
    buf[6] = ( uint8_t )( cadTimeout & 0xFF );
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_CADPARAMS, buf, 7 );
 | 
			
		||||
    SX126xSetOperatingMode( MODE_CAD );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetBufferBaseAddress( uint8_t txBaseAddress, uint8_t rxBaseAddress )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[2];
 | 
			
		||||
 | 
			
		||||
    buf[0] = txBaseAddress;
 | 
			
		||||
    buf[1] = rxBaseAddress;
 | 
			
		||||
    SX126xWriteCommand( RADIO_SET_BUFFERBASEADDRESS, buf, 2 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
RadioStatus_t SX126xGetStatus( void )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t stat = 0;
 | 
			
		||||
    RadioStatus_t status = { .Value = 0 };
 | 
			
		||||
 | 
			
		||||
    stat = SX126xReadCommand( RADIO_GET_STATUS, NULL, 0 );
 | 
			
		||||
    status.Fields.CmdStatus = ( stat & ( 0x07 << 1 ) ) >> 1;
 | 
			
		||||
    status.Fields.ChipMode = ( stat & ( 0x07 << 4 ) ) >> 4;
 | 
			
		||||
    return status;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int8_t SX126xGetRssiInst( void )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[1];
 | 
			
		||||
    int8_t rssi = 0;
 | 
			
		||||
 | 
			
		||||
    SX126xReadCommand( RADIO_GET_RSSIINST, buf, 1 );
 | 
			
		||||
    rssi = -buf[0] >> 1;
 | 
			
		||||
    return rssi;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xGetRxBufferStatus( uint8_t *payloadLength, uint8_t *rxStartBufferPointer )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t status[2];
 | 
			
		||||
 | 
			
		||||
    SX126xReadCommand( RADIO_GET_RXBUFFERSTATUS, status, 2 );
 | 
			
		||||
 | 
			
		||||
    // In case of LORA fixed header, the payloadLength is obtained by reading
 | 
			
		||||
    // the register REG_LR_PAYLOADLENGTH
 | 
			
		||||
    if( ( SX126xGetPacketType( ) == PACKET_TYPE_LORA ) && ( LoRaHeaderType == LORA_PACKET_FIXED_LENGTH ) )
 | 
			
		||||
    {
 | 
			
		||||
        *payloadLength = SX126xReadRegister( REG_LR_PAYLOADLENGTH );
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        *payloadLength = status[0];
 | 
			
		||||
    }
 | 
			
		||||
    *rxStartBufferPointer = status[1];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xGetPacketStatus( PacketStatus_t *pktStatus )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t status[3];
 | 
			
		||||
 | 
			
		||||
    SX126xReadCommand( RADIO_GET_PACKETSTATUS, status, 3 );
 | 
			
		||||
 | 
			
		||||
    pktStatus->packetType = SX126xGetPacketType( );
 | 
			
		||||
    switch( pktStatus->packetType )
 | 
			
		||||
    {
 | 
			
		||||
        case PACKET_TYPE_GFSK:
 | 
			
		||||
            pktStatus->Params.Gfsk.RxStatus = status[0];
 | 
			
		||||
            pktStatus->Params.Gfsk.RssiSync = -status[1] >> 1;
 | 
			
		||||
            pktStatus->Params.Gfsk.RssiAvg = -status[2] >> 1;
 | 
			
		||||
            pktStatus->Params.Gfsk.FreqError = 0;
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
        case PACKET_TYPE_LORA:
 | 
			
		||||
            pktStatus->Params.LoRa.RssiPkt = -status[0] >> 1;
 | 
			
		||||
            // Returns SNR value [dB] rounded to the nearest integer value
 | 
			
		||||
            pktStatus->Params.LoRa.SnrPkt = ( ( ( int8_t )status[1] ) + 2 ) >> 2;
 | 
			
		||||
            pktStatus->Params.LoRa.SignalRssiPkt = -status[2] >> 1;
 | 
			
		||||
            pktStatus->Params.LoRa.FreqError = FrequencyError;
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
        default:
 | 
			
		||||
        case PACKET_TYPE_NONE:
 | 
			
		||||
            // In that specific case, we set everything in the pktStatus to zeros
 | 
			
		||||
            // and reset the packet type accordingly
 | 
			
		||||
            memset( pktStatus, 0, sizeof( PacketStatus_t ) );
 | 
			
		||||
            pktStatus->packetType = PACKET_TYPE_NONE;
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
RadioError_t SX126xGetDeviceErrors( void )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t err[] = { 0, 0 };
 | 
			
		||||
    RadioError_t error = { .Value = 0 };
 | 
			
		||||
 | 
			
		||||
    SX126xReadCommand( RADIO_GET_ERROR, ( uint8_t* )err, 2 );
 | 
			
		||||
    error.Fields.PaRamp     = ( err[0] & ( 1 << 0 ) ) >> 0;
 | 
			
		||||
    error.Fields.PllLock    = ( err[1] & ( 1 << 6 ) ) >> 6;
 | 
			
		||||
    error.Fields.XoscStart  = ( err[1] & ( 1 << 5 ) ) >> 5;
 | 
			
		||||
    error.Fields.ImgCalib   = ( err[1] & ( 1 << 4 ) ) >> 4;
 | 
			
		||||
    error.Fields.AdcCalib   = ( err[1] & ( 1 << 3 ) ) >> 3;
 | 
			
		||||
    error.Fields.PllCalib   = ( err[1] & ( 1 << 2 ) ) >> 2;
 | 
			
		||||
    error.Fields.Rc13mCalib = ( err[1] & ( 1 << 1 ) ) >> 1;
 | 
			
		||||
    error.Fields.Rc64kCalib = ( err[1] & ( 1 << 0 ) ) >> 0;
 | 
			
		||||
    return error;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xClearDeviceErrors( void )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[2] = { 0x00, 0x00 };
 | 
			
		||||
    SX126xWriteCommand( RADIO_CLR_ERROR, buf, 2 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xClearIrqStatus( uint16_t irq )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t buf[2];
 | 
			
		||||
 | 
			
		||||
    buf[0] = ( uint8_t )( ( ( uint16_t )irq >> 8 ) & 0x00FF );
 | 
			
		||||
    buf[1] = ( uint8_t )( ( uint16_t )irq & 0x00FF );
 | 
			
		||||
    SX126xWriteCommand( RADIO_CLR_IRQSTATUS, buf, 2 );
 | 
			
		||||
}
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_FILES := sx127x.c lora-radio-sx127x.c lora-spi-sx127x.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,300 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-radio.c for sx127x
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     LoRa Radio interface
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    AIIT XUOS Lab
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file lora-radio-sx127x.c
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-13
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: lora-radio-sx127x.c
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-13
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <lora-radio.h>
 | 
			
		||||
#include "sx127x.h"
 | 
			
		||||
#include <sx127x-board.h>
 | 
			
		||||
 | 
			
		||||
#ifdef RT_USING_PM
 | 
			
		||||
#include "drivers/pm.h"
 | 
			
		||||
#ifndef PM_RADIO_ID
 | 
			
		||||
#define PM_RADIO_ID  ( PM_MODULE_MAX_ID - 3 )
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define LOG_TAG "PHY.LoRa.SX127X"
 | 
			
		||||
#define LOG_LEVEL  LOG_LVL_DBG
 | 
			
		||||
#include <lora-radio-debug.h>
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
 | 
			
		||||
#define EV_LORA_RADIO_IRQ_MASK         0x0007 // DIO0 | DIO1 | DIO2 depend on board
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
static struct rt_event lora_radio_event;
 | 
			
		||||
static struct rt_thread lora_radio_thread;
 | 
			
		||||
static rt_uint8_t rt_lora_radio_thread_stack[4096];
 | 
			
		||||
                            
 | 
			
		||||
extern struct rt_spi_device *lora_radio_spi_init(const char *bus_name, const char *lora_device_name, rt_uint8_t param);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif // end of LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
static pthread_t lora_radio_task;
 | 
			
		||||
extern int lora_radio_spi_init(void);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
bool SX127xRadioInit( RadioEvents_t *events );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio driver structure initialization
 | 
			
		||||
 */
 | 
			
		||||
const struct Radio_s Radio =
 | 
			
		||||
{
 | 
			
		||||
    SX127xRadioInit,
 | 
			
		||||
    SX127xGetStatus,
 | 
			
		||||
    SX127xSetModem,
 | 
			
		||||
    SX127xSetChannel,
 | 
			
		||||
    SX127xIsChannelFree,
 | 
			
		||||
    SX127xRandom,
 | 
			
		||||
    SX127xSetRxConfig,
 | 
			
		||||
    SX127xSetTxConfig,
 | 
			
		||||
    SX127xCheckRfFrequency,
 | 
			
		||||
    SX127xGetTimeOnAir,
 | 
			
		||||
    SX127xSend,
 | 
			
		||||
    SX127xSetSleep,
 | 
			
		||||
    SX127xSetStby,
 | 
			
		||||
    SX127xSetRx,
 | 
			
		||||
    SX127xStartCad,
 | 
			
		||||
    SX127xSetTxContinuousWave,
 | 
			
		||||
    SX127xReadRssi,
 | 
			
		||||
    SX127xWrite,
 | 
			
		||||
    SX127xRead,
 | 
			
		||||
    //SX127xWriteBuffer,
 | 
			
		||||
    //SX127xReadBuffer,
 | 
			
		||||
    SX127xSetMaxPayloadLength,
 | 
			
		||||
    SX127xSetPublicNetwork,
 | 
			
		||||
    SX127xGetWakeupTime,
 | 
			
		||||
    NULL, // void ( *IrqProcess )( void )
 | 
			
		||||
    SX127xCheck,
 | 
			
		||||
    //SX126x Only
 | 
			
		||||
    NULL, // void ( *RxBoosted )( uint32_t timeout ) 
 | 
			
		||||
    NULL, // void ( *SetRxDutyCycle )( uint32_t rxTime, uint32_t sleepTime ) 
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static bool lora_radio_init = false;
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
 | 
			
		||||
static uint8_t get_irq_index(uint32_t ev)
 | 
			
		||||
{
 | 
			
		||||
    uint32_t i = 0;
 | 
			
		||||
    for(i = 0; i < 32; i++)
 | 
			
		||||
    {
 | 
			
		||||
        if(ev & 0x01)
 | 
			
		||||
        {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        ev >>= 1;
 | 
			
		||||
    }
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
/**
 | 
			
		||||
  * @brief  lora_radio_thread_entry
 | 
			
		||||
  * @param  None
 | 
			
		||||
  * @retval None
 | 
			
		||||
  */
 | 
			
		||||
void lora_radio_thread_entry(void* parameter)
 | 
			
		||||
{
 | 
			
		||||
    rt_uint32_t ev;
 | 
			
		||||
    
 | 
			
		||||
    while(1)
 | 
			
		||||
    {
 | 
			
		||||
        if (rt_event_recv(&lora_radio_event, EV_LORA_RADIO_IRQ_MASK | EV_LORA_RADIO_TIMEOUT_FIRED,
 | 
			
		||||
                                (RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR),
 | 
			
		||||
                                RT_WAITING_FOREVER, &ev) == RT_EOK)
 | 
			
		||||
        {
 | 
			
		||||
#ifdef RT_USING_PM
 | 
			
		||||
            rt_pm_module_request(PM_RADIO_ID, PM_SLEEP_MODE_NONE);
 | 
			
		||||
            RadioIrqProcess(get_irq_index(ev));
 | 
			
		||||
            rt_pm_module_release(PM_RADIO_ID, PM_SLEEP_MODE_NONE);
 | 
			
		||||
#else
 | 
			
		||||
            RadioIrqProcess(get_irq_index(ev));
 | 
			
		||||
#endif
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
bool SX127xRadioInit( RadioEvents_t *events )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
    if( lora_radio_init == false )
 | 
			
		||||
    {
 | 
			
		||||
        // Initialize spi bus
 | 
			
		||||
        SX127x.spi = lora_radio_spi_init(LORA_RADIO0_SPI_BUS_NAME, LORA_RADIO0_DEVICE_NAME, RT_NULL);
 | 
			
		||||
        if (SX127x.spi == RT_NULL)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_INTERFACE, LOG_LEVEL, "SX127x SPI Init Failed\n");
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        LORA_RADIO_DEBUG_LOG(LR_DBG_INTERFACE, LOG_LEVEL, "SX127x SPI Init Succeed\n");
 | 
			
		||||
 | 
			
		||||
        rt_event_init(&lora_radio_event, "ev_lora_phy", RT_IPC_FLAG_PRIO);//RT_IPC_FLAG_FIFO);
 | 
			
		||||
 | 
			
		||||
        rt_thread_init(&lora_radio_thread,               	  
 | 
			
		||||
                       "lora-phy",                     	  
 | 
			
		||||
                       lora_radio_thread_entry,          	  
 | 
			
		||||
                       RT_NULL,                    	  
 | 
			
		||||
                       &rt_lora_radio_thread_stack[0],       
 | 
			
		||||
                       sizeof(rt_lora_radio_thread_stack),  
 | 
			
		||||
                       1,   // highest priority                       	  
 | 
			
		||||
                       20);                           
 | 
			
		||||
                                   
 | 
			
		||||
        rt_thread_startup(&lora_radio_thread);   
 | 
			
		||||
                       
 | 
			
		||||
       lora_radio_init = true;
 | 
			
		||||
   }
 | 
			
		||||
#endif /* LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD */ 
 | 
			
		||||
   
 | 
			
		||||
   SX127xIoInit();   
 | 
			
		||||
   SX127xInit(events);
 | 
			
		||||
   
 | 
			
		||||
   return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xOnDio0IrqEvent( void *args )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
    rt_event_send(&lora_radio_event, EV_LORA_RADIO_IRQ0_FIRED);
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
void SX127xOnDio1IrqEvent( void *args )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
    rt_event_send(&lora_radio_event, EV_LORA_RADIO_IRQ1_FIRED);
 | 
			
		||||
#endif    
 | 
			
		||||
}
 | 
			
		||||
void SX127xOnDio2IrqEvent( void *args )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
    rt_event_send(&lora_radio_event, EV_LORA_RADIO_IRQ2_FIRED);
 | 
			
		||||
#endif  
 | 
			
		||||
}
 | 
			
		||||
void SX127xOnDio3IrqEvent( void *args )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
    rt_event_send(&lora_radio_event, EV_LORA_RADIO_IRQ3_FIRED);
 | 
			
		||||
#endif    
 | 
			
		||||
}
 | 
			
		||||
void SX127xOnDio4IrqEvent( void *args )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
    rt_event_send(&lora_radio_event, EV_LORA_RADIO_IRQ4_FIRED);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
void SX127xOnDio5IrqEvent( void *args )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
    rt_event_send(&lora_radio_event, EV_LORA_RADIO_IRQ5_FIRED);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
void SX127xOnTimeoutIrqEvent( void )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
    rt_event_send(&lora_radio_event, EV_LORA_RADIO_TIMEOUT_FIRED);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif 
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
/**
 | 
			
		||||
  * @brief  lora_radio_thread_entry
 | 
			
		||||
  * @param  None
 | 
			
		||||
  * @retval None
 | 
			
		||||
  */
 | 
			
		||||
static void *lora_radio_thread_entry(void* parameter)
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    
 | 
			
		||||
    while(1) {
 | 
			
		||||
        RadioIrqProcess(ret);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool SX127xRadioInit( RadioEvents_t *events )
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    if( lora_radio_init == false ) {
 | 
			
		||||
        // Initialize spi bus
 | 
			
		||||
        ret = lora_radio_spi_init();
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_INTERFACE, LOG_LEVEL, "SX127x SPI Init Failed\n");
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        LORA_RADIO_DEBUG_LOG(LR_DBG_INTERFACE, LOG_LEVEL, "SX127x SPI Init Succeed\n"); 
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    SX127xInit(events);
 | 
			
		||||
 | 
			
		||||
    if ( lora_radio_init == false ) {
 | 
			
		||||
        pthread_attr_t attr;
 | 
			
		||||
        attr.schedparam.sched_priority = 20;
 | 
			
		||||
        attr.stacksize = 4096;
 | 
			
		||||
 | 
			
		||||
        char task_name[] = "lora_radio_task";
 | 
			
		||||
        pthread_args_t args;
 | 
			
		||||
        args.pthread_name = task_name;
 | 
			
		||||
 | 
			
		||||
        PrivTaskCreate(&lora_radio_task, &attr, &lora_radio_thread_entry, (void *)&args);
 | 
			
		||||
        PrivTaskStartup(&lora_radio_task);
 | 
			
		||||
                        
 | 
			
		||||
        lora_radio_init = true;        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,120 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-radio-spi-sx127x.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     sx127x spi driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    AIIT XUOS Lab
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file lora-radio-spi-sx127x.c
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-17
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: lora-radio-spi-sx127x.c
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-17
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#include "sx127x.h"
 | 
			
		||||
#include <sx127x-board.h>
 | 
			
		||||
 | 
			
		||||
void SX127xWriteBuffer( uint16_t addr, uint8_t *buffer, uint8_t size )
 | 
			
		||||
{   
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#ifdef RT_USING_SPI
 | 
			
		||||
    uint8_t msg[1] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = (addr | 0x80);
 | 
			
		||||
 | 
			
		||||
    rt_spi_send_then_send(SX127x.spi,msg,1,buffer,size);
 | 
			
		||||
#else
 | 
			
		||||
//    uint8_t i;
 | 
			
		||||
 | 
			
		||||
//    //NSS = 0;
 | 
			
		||||
//    GpioWrite( &SX127x.Spi.Nss, 0 );
 | 
			
		||||
 | 
			
		||||
//    SpiInOut( &SX127x.Spi, addr | 0x80 );
 | 
			
		||||
//    for( i = 0; i < size; i++ )
 | 
			
		||||
//    {
 | 
			
		||||
//        SpiInOut( &SX127x.Spi, buffer[i] );
 | 
			
		||||
//    }
 | 
			
		||||
 | 
			
		||||
//    //NSS = 1;
 | 
			
		||||
//    GpioWrite( &SX127x.Spi.Nss, 1 );
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    int spi_fd;
 | 
			
		||||
    uint8_t msg[1] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = (addr | 0x80);
 | 
			
		||||
 | 
			
		||||
    spi_fd = PrivOpen(LORA_SPI_DEV_NAME, O_RDWR);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, msg, 1);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, buffer, size);
 | 
			
		||||
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xReadBuffer( uint16_t addr, uint8_t *buffer, uint8_t size )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#ifdef RT_USING_SPI
 | 
			
		||||
    uint8_t msg[1] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = (addr & 0x7F);
 | 
			
		||||
 | 
			
		||||
    rt_spi_send_then_recv(SX127x.spi,msg,1,buffer,size);
 | 
			
		||||
#else
 | 
			
		||||
//    uint8_t i;
 | 
			
		||||
 | 
			
		||||
//    //NSS = 0;
 | 
			
		||||
//    GpioWrite( &SX127x.Spi.Nss, 0 );
 | 
			
		||||
 | 
			
		||||
//    SpiInOut( &SX127x.Spi, addr & 0x7F );
 | 
			
		||||
 | 
			
		||||
//    for( i = 0; i < size; i++ )
 | 
			
		||||
//    {
 | 
			
		||||
//        buffer[i] = SpiInOut( &SX127x.Spi, 0 );
 | 
			
		||||
//    }
 | 
			
		||||
 | 
			
		||||
//    //NSS = 1;
 | 
			
		||||
//    GpioWrite( &SX127x.Spi.Nss, 1 );
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    int spi_fd;
 | 
			
		||||
    uint8_t msg[1] = {0};
 | 
			
		||||
    
 | 
			
		||||
    msg[0] = (addr & 0x7F);
 | 
			
		||||
 | 
			
		||||
    spi_fd = PrivOpen(LORA_SPI_DEV_NAME, O_RDWR);
 | 
			
		||||
 | 
			
		||||
    PrivWrite(spi_fd, msg, 1);
 | 
			
		||||
 | 
			
		||||
    PrivRead(spi_fd, buffer, size);
 | 
			
		||||
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,64 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-spi-sx127x.h
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     SX127x spi interface
 | 
			
		||||
 *
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
#ifndef __LORA_SPI_SX127x_H__
 | 
			
		||||
#define __LORA_SPI_SX127x_H__
 | 
			
		||||
 | 
			
		||||
/** @addtogroup LORA_RADIO_SPI
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @addtogroup SX127X_SPI
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Writes the radio register at the specified address
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN]: addr Register address
 | 
			
		||||
 * \param [IN]: data New register value
 | 
			
		||||
 */
 | 
			
		||||
void SX127xWrite( uint16_t addr, uint8_t data );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Reads the radio register at the specified address
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN]: addr Register address
 | 
			
		||||
 * \retval data Register value
 | 
			
		||||
 */
 | 
			
		||||
uint8_t SX127xRead( uint16_t addr );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Writes multiple radio registers starting at address
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] addr   First Radio register address
 | 
			
		||||
 * \param [IN] buffer Buffer containing the new register's values
 | 
			
		||||
 * \param [IN] size   Number of registers to be written
 | 
			
		||||
 */
 | 
			
		||||
void SX127xWriteBuffer( uint16_t addr, uint8_t *buffer, uint8_t size );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Reads multiple radio registers starting at address
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] addr First Radio register address
 | 
			
		||||
 * \param [OUT] buffer Buffer where to copy the registers data
 | 
			
		||||
 * \param [IN] size Number of registers to be read
 | 
			
		||||
 */
 | 
			
		||||
void SX127xReadBuffer( uint16_t addr, uint8_t *buffer, uint8_t size );
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif // __LORA_SPI_SX127x_H__
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -0,0 +1,530 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      SX127x.h
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     SX127x driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
#ifndef __SX127x_H__
 | 
			
		||||
#define __SX127x_H__
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <lora-radio.h>
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
#include "sx127xRegs-Fsk.h"
 | 
			
		||||
#include "sx127xRegs-LoRa.h"
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio wake-up time from sleep
 | 
			
		||||
 */
 | 
			
		||||
#define RADIO_WAKEUP_TIME                           1 // [ms]
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Sync word for Private LoRa networks
 | 
			
		||||
 */
 | 
			
		||||
#define LORA_MAC_PRIVATE_SYNCWORD                   0x12
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Sync word for Public LoRa networks
 | 
			
		||||
 */
 | 
			
		||||
#define LORA_MAC_PUBLIC_SYNCWORD                    0x34
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio FSK modem parameters
 | 
			
		||||
 */
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    int8_t   Power;
 | 
			
		||||
    uint32_t Fdev;
 | 
			
		||||
    uint32_t Bandwidth;
 | 
			
		||||
    uint32_t BandwidthAfc;
 | 
			
		||||
    uint32_t Datarate;
 | 
			
		||||
    uint16_t PreambleLen;
 | 
			
		||||
    bool     FixLen;
 | 
			
		||||
    uint8_t  PayloadLen;
 | 
			
		||||
    bool     CrcOn;
 | 
			
		||||
    bool     IqInverted;
 | 
			
		||||
    bool     RxContinuous;
 | 
			
		||||
    uint32_t TxTimeout;
 | 
			
		||||
    uint32_t RxSingleTimeout;
 | 
			
		||||
}RadioFskSettings_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio FSK packet handler state
 | 
			
		||||
 */
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    uint8_t  PreambleDetected;
 | 
			
		||||
    uint8_t  SyncWordDetected;
 | 
			
		||||
    int8_t   RssiValue;
 | 
			
		||||
    int32_t  AfcValue;
 | 
			
		||||
    uint8_t  RxGain;
 | 
			
		||||
    uint16_t Size;
 | 
			
		||||
    uint16_t NbBytes;
 | 
			
		||||
    uint8_t  FifoThresh;
 | 
			
		||||
    uint8_t  ChunkSize;
 | 
			
		||||
}RadioFskPacketHandler_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio LoRa modem parameters
 | 
			
		||||
 */
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    int8_t   Power;
 | 
			
		||||
    uint32_t Bandwidth;
 | 
			
		||||
    uint32_t Datarate;
 | 
			
		||||
    bool     LowDatarateOptimize;
 | 
			
		||||
    uint8_t  Coderate;
 | 
			
		||||
    uint16_t PreambleLen;
 | 
			
		||||
    bool     FixLen;
 | 
			
		||||
    uint8_t  PayloadLen;
 | 
			
		||||
    bool     CrcOn;
 | 
			
		||||
    bool     FreqHopOn;
 | 
			
		||||
    uint8_t  HopPeriod;
 | 
			
		||||
    bool     IqInverted;
 | 
			
		||||
    bool     RxContinuous;
 | 
			
		||||
    uint32_t TxTimeout;
 | 
			
		||||
    bool     PublicNetwork;
 | 
			
		||||
}RadioLoRaSettings_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio LoRa packet handler state
 | 
			
		||||
 */
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    int8_t SnrValue;
 | 
			
		||||
    int16_t RssiValue;
 | 
			
		||||
    uint8_t Size;
 | 
			
		||||
}RadioLoRaPacketHandler_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio Settings
 | 
			
		||||
 */
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    RadioState_t             State;
 | 
			
		||||
    RadioModems_t            Modem;
 | 
			
		||||
    uint32_t                 Channel;
 | 
			
		||||
#if defined( LORA_RADIO_DRIVER_USING_RF_MODEM_FSK ) 
 | 
			
		||||
    RadioFskSettings_t       Fsk;
 | 
			
		||||
    RadioFskPacketHandler_t  FskPacketHandler;
 | 
			
		||||
#endif    
 | 
			
		||||
    RadioLoRaSettings_t      LoRa;
 | 
			
		||||
    RadioLoRaPacketHandler_t LoRaPacketHandler;
 | 
			
		||||
}RadioSettings_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio hardware and global parameters
 | 
			
		||||
 */
 | 
			
		||||
typedef struct SX127x_s
 | 
			
		||||
{
 | 
			
		||||
//    Gpio_t        Reset;
 | 
			
		||||
//    Gpio_t        DIO0;
 | 
			
		||||
//    Gpio_t        DIO1;
 | 
			
		||||
//    Gpio_t        DIO2;
 | 
			
		||||
//    Gpio_t        DIO3;
 | 
			
		||||
//    Gpio_t        DIO4;
 | 
			
		||||
//    Gpio_t        DIO5;
 | 
			
		||||
//    Spi_t         Spi;
 | 
			
		||||
    
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio Spi bus
 | 
			
		||||
 */
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
    struct rt_spi_device *spi;
 | 
			
		||||
#endif
 | 
			
		||||
    
 | 
			
		||||
    RadioSettings_t Settings;
 | 
			
		||||
}SX127x_t;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Hardware IO IRQ callback function definition
 | 
			
		||||
 */
 | 
			
		||||
typedef void ( DioIrqHandler )( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * SX127x definitions
 | 
			
		||||
 */
 | 
			
		||||
#define XTAL_FREQ                                   32000000
 | 
			
		||||
#define FREQ_STEP                                   61.03515625
 | 
			
		||||
 | 
			
		||||
#define RX_BUFFER_SIZE                              256
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Radio hardware registers initialization definition
 | 
			
		||||
 *
 | 
			
		||||
 * \remark Can be automatically generated by the SX127x GUI (not yet implemented)
 | 
			
		||||
 */
 | 
			
		||||
#define RADIO_INIT_REGISTERS_VALUE                \
 | 
			
		||||
{                                                 \
 | 
			
		||||
    { MODEM_FSK , REG_LNA                , 0x23 },\
 | 
			
		||||
    { MODEM_FSK , REG_RXCONFIG           , 0x1E },\
 | 
			
		||||
    { MODEM_FSK , REG_RSSICONFIG         , 0xD2 },\
 | 
			
		||||
    { MODEM_FSK , REG_AFCFEI             , 0x01 },\
 | 
			
		||||
    { MODEM_FSK , REG_PREAMBLEDETECT     , 0xAA },\
 | 
			
		||||
    { MODEM_FSK , REG_OSC                , 0x07 },\
 | 
			
		||||
    { MODEM_FSK , REG_SYNCCONFIG         , 0x12 },\
 | 
			
		||||
    { MODEM_FSK , REG_SYNCVALUE1         , 0xC1 },\
 | 
			
		||||
    { MODEM_FSK , REG_SYNCVALUE2         , 0x94 },\
 | 
			
		||||
    { MODEM_FSK , REG_SYNCVALUE3         , 0xC1 },\
 | 
			
		||||
    { MODEM_FSK , REG_PACKETCONFIG1      , 0xD8 },\
 | 
			
		||||
    { MODEM_FSK , REG_FIFOTHRESH         , 0x8F },\
 | 
			
		||||
    { MODEM_FSK , REG_IMAGECAL           , 0x02 },\
 | 
			
		||||
    { MODEM_FSK , REG_DIOMAPPING1        , 0x00 },\
 | 
			
		||||
    { MODEM_FSK , REG_DIOMAPPING2        , 0x30 },\
 | 
			
		||||
    { MODEM_LORA, REG_LR_PAYLOADMAXLENGTH, 0x40 },\
 | 
			
		||||
}                                                 \
 | 
			
		||||
 | 
			
		||||
#define RF_MID_BAND_THRESH                          525000000
 | 
			
		||||
 | 
			
		||||
/** @addtogroup LORA_RADIO_CHIP_DRIVER
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @addtogroup SX127X_DRIVER
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * ============================================================================
 | 
			
		||||
 * Public functions prototypes
 | 
			
		||||
 * ============================================================================
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the radio
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] events Structure containing the driver callback functions
 | 
			
		||||
 */
 | 
			
		||||
void SX127xInit( RadioEvents_t *events );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Return current radio status
 | 
			
		||||
 *
 | 
			
		||||
 * \param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
 | 
			
		||||
 */
 | 
			
		||||
RadioState_t SX127xGetStatus( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Configures the radio with the given modem
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] modem Modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetModem( RadioModems_t modem );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sets the channel configuration
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] freq         Channel RF frequency
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetChannel( uint32_t freq );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sets the radio output power.
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] power Sets the RF output power
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetRfTxPower( int8_t power );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Checks if the channel is free for the given time
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
 * \param [IN] freq       Channel RF frequency
 | 
			
		||||
 * \param [IN] rssiThresh RSSI threshold
 | 
			
		||||
 * \param [IN] maxCarrierSenseTime Max time while the RSSI is measured
 | 
			
		||||
 *
 | 
			
		||||
 * \retval isFree         [true: Channel is free, false: Channel is not free]
 | 
			
		||||
 */
 | 
			
		||||
bool SX127xIsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh, uint32_t maxCarrierSenseTime );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Generates a 32 bits random value based on the RSSI readings
 | 
			
		||||
 *
 | 
			
		||||
 * \remark This function sets the radio in LoRa modem mode and disables
 | 
			
		||||
 *         all interrupts.
 | 
			
		||||
 *         After calling this function either SX127xSetRxConfig or
 | 
			
		||||
 *         SX127xSetTxConfig functions must be called.
 | 
			
		||||
 *
 | 
			
		||||
 * \retval randomValue    32 bits random value
 | 
			
		||||
 */
 | 
			
		||||
uint32_t SX127xRandom( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sets the reception parameters
 | 
			
		||||
 *
 | 
			
		||||
 * \remark When using LoRa modem only bandwidths 125, 250 and 500 kHz are supported
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
 * \param [IN] bandwidth    Sets the bandwidth
 | 
			
		||||
 *                          FSK : >= 2600 and <= 250000 Hz
 | 
			
		||||
 *                          LoRa: [0: 125 kHz, 1: 250 kHz,
 | 
			
		||||
 *                                 2: 500 kHz, 3: Reserved]
 | 
			
		||||
 * \param [IN] datarate     Sets the Datarate
 | 
			
		||||
 *                          FSK : 600..300000 bits/s
 | 
			
		||||
 *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
 | 
			
		||||
 *                                10: 1024, 11: 2048, 12: 4096  chips]
 | 
			
		||||
 * \param [IN] coderate     Sets the coding rate (LoRa only)
 | 
			
		||||
 *                          FSK : N/A ( set to 0 )
 | 
			
		||||
 *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
 | 
			
		||||
 * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only)
 | 
			
		||||
 *                          FSK : >= 2600 and <= 250000 Hz
 | 
			
		||||
 *                          LoRa: N/A ( set to 0 )
 | 
			
		||||
 * \param [IN] preambleLen  Sets the Preamble length
 | 
			
		||||
 *                          FSK : Number of bytes
 | 
			
		||||
 *                          LoRa: Length in symbols (the hardware adds 4 more symbols)
 | 
			
		||||
 * \param [IN] symbTimeout  Sets the RxSingle timeout value
 | 
			
		||||
 *                          FSK : timeout number of bytes
 | 
			
		||||
 *                          LoRa: timeout in symbols
 | 
			
		||||
 * \param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
 | 
			
		||||
 * \param [IN] payloadLen   Sets payload length when fixed length is used
 | 
			
		||||
 * \param [IN] crcOn        Enables/Disables the CRC [0: OFF, 1: ON]
 | 
			
		||||
 * \param [IN] freqHopOn    Enables disables the intra-packet frequency hopping
 | 
			
		||||
 *                          FSK : N/A ( set to 0 )
 | 
			
		||||
 *                          LoRa: [0: OFF, 1: ON]
 | 
			
		||||
 * \param [IN] hopPeriod    Number of symbols between each hop
 | 
			
		||||
 *                          FSK : N/A ( set to 0 )
 | 
			
		||||
 *                          LoRa: Number of symbols
 | 
			
		||||
 * \param [IN] iqInverted   Inverts IQ signals (LoRa only)
 | 
			
		||||
 *                          FSK : N/A ( set to 0 )
 | 
			
		||||
 *                          LoRa: [0: not inverted, 1: inverted]
 | 
			
		||||
 * \param [IN] rxContinuous Sets the reception in continuous mode
 | 
			
		||||
 *                          [false: single mode, true: continuous mode]
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetRxConfig( RadioModems_t modem, uint32_t bandwidth,
 | 
			
		||||
                         uint32_t datarate, uint8_t coderate,
 | 
			
		||||
                         uint32_t bandwidthAfc, uint16_t preambleLen,
 | 
			
		||||
                         uint16_t symbTimeout, bool fixLen,
 | 
			
		||||
                         uint8_t payloadLen,
 | 
			
		||||
                         bool crcOn, bool freqHopOn, uint8_t hopPeriod,
 | 
			
		||||
                         bool iqInverted, bool rxContinuous );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sets the transmission parameters
 | 
			
		||||
 *
 | 
			
		||||
 * \remark When using LoRa modem only bandwidths 125, 250 and 500 kHz are supported
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
 * \param [IN] power        Sets the output power [dBm]
 | 
			
		||||
 * \param [IN] fdev         Sets the frequency deviation (FSK only)
 | 
			
		||||
 *                          FSK : [Hz]
 | 
			
		||||
 *                          LoRa: 0
 | 
			
		||||
 * \param [IN] bandwidth    Sets the bandwidth (LoRa only)
 | 
			
		||||
 *                          FSK : 0
 | 
			
		||||
 *                          LoRa: [0: 125 kHz, 1: 250 kHz,
 | 
			
		||||
 *                                 2: 500 kHz, 3: Reserved]
 | 
			
		||||
 * \param [IN] datarate     Sets the Datarate
 | 
			
		||||
 *                          FSK : 600..300000 bits/s
 | 
			
		||||
 *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
 | 
			
		||||
 *                                10: 1024, 11: 2048, 12: 4096  chips]
 | 
			
		||||
 * \param [IN] coderate     Sets the coding rate (LoRa only)
 | 
			
		||||
 *                          FSK : N/A ( set to 0 )
 | 
			
		||||
 *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
 | 
			
		||||
 * \param [IN] preambleLen  Sets the preamble length
 | 
			
		||||
 *                          FSK : Number of bytes
 | 
			
		||||
 *                          LoRa: Length in symbols (the hardware adds 4 more symbols)
 | 
			
		||||
 * \param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
 | 
			
		||||
 * \param [IN] crcOn        Enables disables the CRC [0: OFF, 1: ON]
 | 
			
		||||
 * \param [IN] freqHopOn    Enables disables the intra-packet frequency hopping
 | 
			
		||||
 *                          FSK : N/A ( set to 0 )
 | 
			
		||||
 *                          LoRa: [0: OFF, 1: ON]
 | 
			
		||||
 * \param [IN] hopPeriod    Number of symbols between each hop
 | 
			
		||||
 *                          FSK : N/A ( set to 0 )
 | 
			
		||||
 *                          LoRa: Number of symbols
 | 
			
		||||
 * \param [IN] iqInverted   Inverts IQ signals (LoRa only)
 | 
			
		||||
 *                          FSK : N/A ( set to 0 )
 | 
			
		||||
 *                          LoRa: [0: not inverted, 1: inverted]
 | 
			
		||||
 * \param [IN] timeout      Transmission timeout [ms]
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
 | 
			
		||||
                        uint32_t bandwidth, uint32_t datarate,
 | 
			
		||||
                        uint8_t coderate, uint16_t preambleLen,
 | 
			
		||||
                        bool fixLen, bool crcOn, bool freqHopOn,
 | 
			
		||||
                        uint8_t hopPeriod, bool iqInverted, uint32_t timeout );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Computes the packet time on air in ms for the given payload
 | 
			
		||||
 *
 | 
			
		||||
 * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
 * \param [IN] bandwidth    Sets the bandwidth
 | 
			
		||||
 *                          FSK : >= 2600 and <= 250000 Hz
 | 
			
		||||
 *                          LoRa: [0: 125 kHz, 1: 250 kHz,
 | 
			
		||||
 *                                 2: 500 kHz, 3: Reserved]
 | 
			
		||||
 * \param [IN] datarate     Sets the Datarate
 | 
			
		||||
 *                          FSK : 600..300000 bits/s
 | 
			
		||||
 *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
 | 
			
		||||
 *                                10: 1024, 11: 2048, 12: 4096  chips]
 | 
			
		||||
 * \param [IN] coderate     Sets the coding rate (LoRa only)
 | 
			
		||||
 *                          FSK : N/A ( set to 0 )
 | 
			
		||||
 *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
 | 
			
		||||
 * \param [IN] preambleLen  Sets the Preamble length
 | 
			
		||||
 *                          FSK : Number of bytes
 | 
			
		||||
 *                          LoRa: Length in symbols (the hardware adds 4 more symbols)
 | 
			
		||||
 * \param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
 | 
			
		||||
 * \param [IN] payloadLen   Sets payload length when fixed length is used
 | 
			
		||||
 * \param [IN] crcOn        Enables/Disables the CRC [0: OFF, 1: ON]
 | 
			
		||||
 *
 | 
			
		||||
 * \retval airTime        Computed airTime (ms) for the given packet payload length
 | 
			
		||||
 */
 | 
			
		||||
uint32_t SX127xGetTimeOnAir( RadioModems_t modem, uint32_t bandwidth,
 | 
			
		||||
                              uint32_t datarate, uint8_t coderate,
 | 
			
		||||
                              uint16_t preambleLen, bool fixLen, uint8_t payloadLen,
 | 
			
		||||
                              bool crcOn );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sends the buffer of size. Prepares the packet to be sent and sets
 | 
			
		||||
 *        the radio in transmission
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN]: buffer     Buffer pointer
 | 
			
		||||
 * \param [IN]: size       Buffer size
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSend( uint8_t *buffer, uint8_t size );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sets the radio in sleep mode
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetSleep( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sets the radio in standby mode
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetStby( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sets the radio in reception mode for the given time
 | 
			
		||||
 * \param [IN] timeout Reception timeout [ms] [0: continuous, others timeout]
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetRx( uint32_t timeout );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Start a Channel Activity Detection
 | 
			
		||||
 */
 | 
			
		||||
void SX127xStartCad( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sets the radio in continuous wave transmission mode
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN]: freq       Channel RF frequency
 | 
			
		||||
 * \param [IN]: power      Sets the output power [dBm]
 | 
			
		||||
 * \param [IN]: time       Transmission mode timeout [s]
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetTxContinuousWave( uint32_t freq, int8_t power, uint16_t time );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Reads the current RSSI value
 | 
			
		||||
 *
 | 
			
		||||
 * \retval rssiValue Current RSSI value in [dBm]
 | 
			
		||||
 */
 | 
			
		||||
int16_t SX127xReadRssi( RadioModems_t modem );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Writes the radio register at the specified address
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN]: addr Register address
 | 
			
		||||
 * \param [IN]: data New register value
 | 
			
		||||
 */
 | 
			
		||||
void SX127xWrite( uint16_t addr, uint8_t data );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Reads the radio register at the specified address
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN]: addr Register address
 | 
			
		||||
 * \retval data Register value
 | 
			
		||||
 */
 | 
			
		||||
uint8_t SX127xRead( uint16_t addr );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Writes multiple radio registers starting at address
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] addr   First Radio register address
 | 
			
		||||
 * \param [IN] buffer Buffer containing the new register's values
 | 
			
		||||
 * \param [IN] size   Number of registers to be written
 | 
			
		||||
 */
 | 
			
		||||
void SX127xWriteBuffer( uint16_t addr, uint8_t *buffer, uint8_t size );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Reads multiple radio registers starting at address
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] addr First Radio register address
 | 
			
		||||
 * \param [OUT] buffer Buffer where to copy the registers data
 | 
			
		||||
 * \param [IN] size Number of registers to be read
 | 
			
		||||
 */
 | 
			
		||||
void SX127xReadBuffer( uint16_t addr, uint8_t *buffer, uint8_t size );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sets the maximum payload length.
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
 | 
			
		||||
 * \param [IN] max        Maximum payload length in bytes
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetMaxPayloadLength( RadioModems_t modem, uint8_t max );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Sets the network to public or private. Updates the sync byte.
 | 
			
		||||
 *
 | 
			
		||||
 * \remark Applies to LoRa modem only
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] enable if true, it enables a public network
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetPublicNetwork( bool enable );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Gets the time required for the board plus radio to get out of sleep.[ms]
 | 
			
		||||
 *
 | 
			
		||||
 * \retval time Radio plus board wakeup time in ms.
 | 
			
		||||
 */
 | 
			
		||||
uint32_t SX127xGetWakeupTime( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Check spi access
 | 
			
		||||
 *
 | 
			
		||||
 * \retval the .
 | 
			
		||||
 */
 | 
			
		||||
uint8_t SX127xCheck( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes DIO IRQ handlers
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] irqHandlers Array containing the IRQ callback functions
 | 
			
		||||
 */
 | 
			
		||||
void SX127xIoIrqInit( DioIrqHandler **irqHandlers );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Process radio irq
 | 
			
		||||
 */
 | 
			
		||||
void RadioIrqProcess( uint8_t irq_index );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio hardware and global parameters
 | 
			
		||||
 */
 | 
			
		||||
extern SX127x_t SX127x;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#endif // __SX127x_H__
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -0,0 +1,573 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      sx1276Regs-LoRa.h
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     SX1276 LoRa modem registers and bits definitions
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 */
 | 
			
		||||
#ifndef __SX1276_REGS_LORA_H__
 | 
			
		||||
#define __SX1276_REGS_LORA_H__
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * ============================================================================
 | 
			
		||||
 * SX1276 Internal registers Address
 | 
			
		||||
 * ============================================================================
 | 
			
		||||
 */
 | 
			
		||||
#define REG_LR_FIFO                                 0x00
 | 
			
		||||
// Common settings
 | 
			
		||||
#define REG_LR_OPMODE                               0x01
 | 
			
		||||
#define REG_LR_FRFMSB                               0x06
 | 
			
		||||
#define REG_LR_FRFMID                               0x07
 | 
			
		||||
#define REG_LR_FRFLSB                               0x08
 | 
			
		||||
// Tx settings
 | 
			
		||||
#define REG_LR_PACONFIG                             0x09
 | 
			
		||||
#define REG_LR_PARAMP                               0x0A
 | 
			
		||||
#define REG_LR_OCP                                  0x0B
 | 
			
		||||
// Rx settings
 | 
			
		||||
#define REG_LR_LNA                                  0x0C
 | 
			
		||||
// LoRa registers
 | 
			
		||||
#define REG_LR_FIFOADDRPTR                          0x0D
 | 
			
		||||
#define REG_LR_FIFOTXBASEADDR                       0x0E
 | 
			
		||||
#define REG_LR_FIFORXBASEADDR                       0x0F
 | 
			
		||||
#define REG_LR_FIFORXCURRENTADDR                    0x10
 | 
			
		||||
#define REG_LR_IRQFLAGSMASK                         0x11
 | 
			
		||||
#define REG_LR_IRQFLAGS                             0x12
 | 
			
		||||
#define REG_LR_RXNBBYTES                            0x13
 | 
			
		||||
#define REG_LR_RXHEADERCNTVALUEMSB                  0x14
 | 
			
		||||
#define REG_LR_RXHEADERCNTVALUELSB                  0x15
 | 
			
		||||
#define REG_LR_RXPACKETCNTVALUEMSB                  0x16
 | 
			
		||||
#define REG_LR_RXPACKETCNTVALUELSB                  0x17
 | 
			
		||||
#define REG_LR_MODEMSTAT                            0x18
 | 
			
		||||
#define REG_LR_PKTSNRVALUE                          0x19
 | 
			
		||||
#define REG_LR_PKTRSSIVALUE                         0x1A
 | 
			
		||||
#define REG_LR_RSSIVALUE                            0x1B
 | 
			
		||||
#define REG_LR_HOPCHANNEL                           0x1C
 | 
			
		||||
#define REG_LR_MODEMCONFIG1                         0x1D
 | 
			
		||||
#define REG_LR_MODEMCONFIG2                         0x1E
 | 
			
		||||
#define REG_LR_SYMBTIMEOUTLSB                       0x1F
 | 
			
		||||
#define REG_LR_PREAMBLEMSB                          0x20
 | 
			
		||||
#define REG_LR_PREAMBLELSB                          0x21
 | 
			
		||||
#define REG_LR_PAYLOADLENGTH                        0x22
 | 
			
		||||
#define REG_LR_PAYLOADMAXLENGTH                     0x23
 | 
			
		||||
#define REG_LR_HOPPERIOD                            0x24
 | 
			
		||||
#define REG_LR_FIFORXBYTEADDR                       0x25
 | 
			
		||||
#define REG_LR_MODEMCONFIG3                         0x26
 | 
			
		||||
#define REG_LR_FEIMSB                               0x28
 | 
			
		||||
#define REG_LR_FEIMID                               0x29
 | 
			
		||||
#define REG_LR_FEILSB                               0x2A
 | 
			
		||||
#define REG_LR_RSSIWIDEBAND                         0x2C
 | 
			
		||||
#define REG_LR_IFFREQ1                              0x2F
 | 
			
		||||
#define REG_LR_IFFREQ2                              0x30
 | 
			
		||||
#define REG_LR_DETECTOPTIMIZE                       0x31
 | 
			
		||||
#define REG_LR_INVERTIQ                             0x33
 | 
			
		||||
#define REG_LR_HIGHBWOPTIMIZE1                      0x36
 | 
			
		||||
#define REG_LR_DETECTIONTHRESHOLD                   0x37
 | 
			
		||||
#define REG_LR_SYNCWORD                             0x39
 | 
			
		||||
#define REG_LR_HIGHBWOPTIMIZE2                      0x3A
 | 
			
		||||
#define REG_LR_INVERTIQ2                            0x3B
 | 
			
		||||
 | 
			
		||||
// end of documented register in datasheet
 | 
			
		||||
// I/O settings
 | 
			
		||||
#define REG_LR_DIOMAPPING1                          0x40
 | 
			
		||||
#define REG_LR_DIOMAPPING2                          0x41
 | 
			
		||||
// Version
 | 
			
		||||
#define REG_LR_VERSION                              0x42
 | 
			
		||||
// Additional settings
 | 
			
		||||
#define REG_LR_PLLHOP                               0x44
 | 
			
		||||
#define REG_LR_TCXO                                 0x4B
 | 
			
		||||
#define REG_LR_PADAC                                0x4D
 | 
			
		||||
#define REG_LR_FORMERTEMP                           0x5B
 | 
			
		||||
#define REG_LR_BITRATEFRAC                          0x5D
 | 
			
		||||
#define REG_LR_AGCREF                               0x61
 | 
			
		||||
#define REG_LR_AGCTHRESH1                           0x62
 | 
			
		||||
#define REG_LR_AGCTHRESH2                           0x63
 | 
			
		||||
#define REG_LR_AGCTHRESH3                           0x64
 | 
			
		||||
#define REG_LR_PLL                                  0x70
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * ============================================================================
 | 
			
		||||
 * SX1276 LoRa bits control definition
 | 
			
		||||
 * ============================================================================
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFifo
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegOpMode
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_OPMODE_LONGRANGEMODE_MASK              0x7F
 | 
			
		||||
#define RFLR_OPMODE_LONGRANGEMODE_OFF               0x00 // Default
 | 
			
		||||
#define RFLR_OPMODE_LONGRANGEMODE_ON                0x80
 | 
			
		||||
 | 
			
		||||
#define RFLR_OPMODE_ACCESSSHAREDREG_MASK            0xBF
 | 
			
		||||
#define RFLR_OPMODE_ACCESSSHAREDREG_ENABLE          0x40
 | 
			
		||||
#define RFLR_OPMODE_ACCESSSHAREDREG_DISABLE         0x00 // Default
 | 
			
		||||
 | 
			
		||||
#define RFLR_OPMODE_FREQMODE_ACCESS_MASK            0xF7
 | 
			
		||||
#define RFLR_OPMODE_FREQMODE_ACCESS_LF              0x08 // Default
 | 
			
		||||
#define RFLR_OPMODE_FREQMODE_ACCESS_HF              0x00
 | 
			
		||||
 | 
			
		||||
#define RFLR_OPMODE_MASK                            0xF8
 | 
			
		||||
#define RFLR_OPMODE_SLEEP                           0x00
 | 
			
		||||
#define RFLR_OPMODE_STANDBY                         0x01 // Default
 | 
			
		||||
#define RFLR_OPMODE_SYNTHESIZER_TX                  0x02
 | 
			
		||||
#define RFLR_OPMODE_TRANSMITTER                     0x03
 | 
			
		||||
#define RFLR_OPMODE_SYNTHESIZER_RX                  0x04
 | 
			
		||||
#define RFLR_OPMODE_RECEIVER                        0x05
 | 
			
		||||
// LoRa specific modes
 | 
			
		||||
#define RFLR_OPMODE_RECEIVER_SINGLE                 0x06
 | 
			
		||||
#define RFLR_OPMODE_CAD                             0x07
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFrf (MHz)
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_FRFMSB_434_MHZ                         0x6C // Default
 | 
			
		||||
#define RFLR_FRFMID_434_MHZ                         0x80 // Default
 | 
			
		||||
#define RFLR_FRFLSB_434_MHZ                         0x00 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPaConfig
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_PACONFIG_PASELECT_MASK                 0x7F
 | 
			
		||||
#define RFLR_PACONFIG_PASELECT_PABOOST              0x80
 | 
			
		||||
#define RFLR_PACONFIG_PASELECT_RFO                  0x00 // Default
 | 
			
		||||
 | 
			
		||||
#define RFLR_PACONFIG_MAX_POWER_MASK                0x8F
 | 
			
		||||
 | 
			
		||||
#define RFLR_PACONFIG_OUTPUTPOWER_MASK              0xF0
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPaRamp
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_PARAMP_TXBANDFORCE_MASK                0xEF
 | 
			
		||||
#define RFLR_PARAMP_TXBANDFORCE_BAND_SEL            0x10
 | 
			
		||||
#define RFLR_PARAMP_TXBANDFORCE_AUTO                0x00 // Default
 | 
			
		||||
 | 
			
		||||
#define RFLR_PARAMP_MASK                            0xF0
 | 
			
		||||
#define RFLR_PARAMP_3400_US                         0x00
 | 
			
		||||
#define RFLR_PARAMP_2000_US                         0x01
 | 
			
		||||
#define RFLR_PARAMP_1000_US                         0x02
 | 
			
		||||
#define RFLR_PARAMP_0500_US                         0x03
 | 
			
		||||
#define RFLR_PARAMP_0250_US                         0x04
 | 
			
		||||
#define RFLR_PARAMP_0125_US                         0x05
 | 
			
		||||
#define RFLR_PARAMP_0100_US                         0x06
 | 
			
		||||
#define RFLR_PARAMP_0062_US                         0x07
 | 
			
		||||
#define RFLR_PARAMP_0050_US                         0x08
 | 
			
		||||
#define RFLR_PARAMP_0040_US                         0x09 // Default
 | 
			
		||||
#define RFLR_PARAMP_0031_US                         0x0A
 | 
			
		||||
#define RFLR_PARAMP_0025_US                         0x0B
 | 
			
		||||
#define RFLR_PARAMP_0020_US                         0x0C
 | 
			
		||||
#define RFLR_PARAMP_0015_US                         0x0D
 | 
			
		||||
#define RFLR_PARAMP_0012_US                         0x0E
 | 
			
		||||
#define RFLR_PARAMP_0010_US                         0x0F
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegOcp
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_OCP_MASK                               0xDF
 | 
			
		||||
#define RFLR_OCP_ON                                 0x20 // Default
 | 
			
		||||
#define RFLR_OCP_OFF                                0x00
 | 
			
		||||
 | 
			
		||||
#define RFLR_OCP_TRIM_MASK                          0xE0
 | 
			
		||||
#define RFLR_OCP_TRIM_045_MA                        0x00
 | 
			
		||||
#define RFLR_OCP_TRIM_050_MA                        0x01
 | 
			
		||||
#define RFLR_OCP_TRIM_055_MA                        0x02
 | 
			
		||||
#define RFLR_OCP_TRIM_060_MA                        0x03
 | 
			
		||||
#define RFLR_OCP_TRIM_065_MA                        0x04
 | 
			
		||||
#define RFLR_OCP_TRIM_070_MA                        0x05
 | 
			
		||||
#define RFLR_OCP_TRIM_075_MA                        0x06
 | 
			
		||||
#define RFLR_OCP_TRIM_080_MA                        0x07
 | 
			
		||||
#define RFLR_OCP_TRIM_085_MA                        0x08
 | 
			
		||||
#define RFLR_OCP_TRIM_090_MA                        0x09
 | 
			
		||||
#define RFLR_OCP_TRIM_095_MA                        0x0A
 | 
			
		||||
#define RFLR_OCP_TRIM_100_MA                        0x0B  // Default
 | 
			
		||||
#define RFLR_OCP_TRIM_105_MA                        0x0C
 | 
			
		||||
#define RFLR_OCP_TRIM_110_MA                        0x0D
 | 
			
		||||
#define RFLR_OCP_TRIM_115_MA                        0x0E
 | 
			
		||||
#define RFLR_OCP_TRIM_120_MA                        0x0F
 | 
			
		||||
#define RFLR_OCP_TRIM_130_MA                        0x10
 | 
			
		||||
#define RFLR_OCP_TRIM_140_MA                        0x11
 | 
			
		||||
#define RFLR_OCP_TRIM_150_MA                        0x12
 | 
			
		||||
#define RFLR_OCP_TRIM_160_MA                        0x13
 | 
			
		||||
#define RFLR_OCP_TRIM_170_MA                        0x14
 | 
			
		||||
#define RFLR_OCP_TRIM_180_MA                        0x15
 | 
			
		||||
#define RFLR_OCP_TRIM_190_MA                        0x16
 | 
			
		||||
#define RFLR_OCP_TRIM_200_MA                        0x17
 | 
			
		||||
#define RFLR_OCP_TRIM_210_MA                        0x18
 | 
			
		||||
#define RFLR_OCP_TRIM_220_MA                        0x19
 | 
			
		||||
#define RFLR_OCP_TRIM_230_MA                        0x1A
 | 
			
		||||
#define RFLR_OCP_TRIM_240_MA                        0x1B
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegLna
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_LNA_GAIN_MASK                          0x1F
 | 
			
		||||
#define RFLR_LNA_GAIN_G1                            0x20 // Default
 | 
			
		||||
#define RFLR_LNA_GAIN_G2                            0x40
 | 
			
		||||
#define RFLR_LNA_GAIN_G3                            0x60
 | 
			
		||||
#define RFLR_LNA_GAIN_G4                            0x80
 | 
			
		||||
#define RFLR_LNA_GAIN_G5                            0xA0
 | 
			
		||||
#define RFLR_LNA_GAIN_G6                            0xC0
 | 
			
		||||
 | 
			
		||||
#define RFLR_LNA_BOOST_LF_MASK                      0xE7
 | 
			
		||||
#define RFLR_LNA_BOOST_LF_DEFAULT                   0x00 // Default
 | 
			
		||||
 | 
			
		||||
#define RFLR_LNA_BOOST_HF_MASK                      0xFC
 | 
			
		||||
#define RFLR_LNA_BOOST_HF_OFF                       0x00 // Default
 | 
			
		||||
#define RFLR_LNA_BOOST_HF_ON                        0x03
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFifoAddrPtr
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_FIFOADDRPTR                            0x00 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFifoTxBaseAddr
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_FIFOTXBASEADDR                         0x80 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFifoTxBaseAddr
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_FIFORXBASEADDR                         0x00 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFifoRxCurrentAddr (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegIrqFlagsMask
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_IRQFLAGS_RXTIMEOUT_MASK                0x80
 | 
			
		||||
#define RFLR_IRQFLAGS_RXDONE_MASK                   0x40
 | 
			
		||||
#define RFLR_IRQFLAGS_PAYLOADCRCERROR_MASK          0x20
 | 
			
		||||
#define RFLR_IRQFLAGS_VALIDHEADER_MASK              0x10
 | 
			
		||||
#define RFLR_IRQFLAGS_TXDONE_MASK                   0x08
 | 
			
		||||
#define RFLR_IRQFLAGS_CADDONE_MASK                  0x04
 | 
			
		||||
#define RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL_MASK       0x02
 | 
			
		||||
#define RFLR_IRQFLAGS_CADDETECTED_MASK              0x01
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegIrqFlags
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_IRQFLAGS_RXTIMEOUT                     0x80
 | 
			
		||||
#define RFLR_IRQFLAGS_RXDONE                        0x40
 | 
			
		||||
#define RFLR_IRQFLAGS_PAYLOADCRCERROR               0x20
 | 
			
		||||
#define RFLR_IRQFLAGS_VALIDHEADER                   0x10
 | 
			
		||||
#define RFLR_IRQFLAGS_TXDONE                        0x08
 | 
			
		||||
#define RFLR_IRQFLAGS_CADDONE                       0x04
 | 
			
		||||
#define RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL            0x02
 | 
			
		||||
#define RFLR_IRQFLAGS_CADDETECTED                   0x01
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFifoRxNbBytes (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegRxHeaderCntValueMsb (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegRxHeaderCntValueLsb (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegRxPacketCntValueMsb (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegRxPacketCntValueLsb (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegModemStat (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_MODEMSTAT_RX_CR_MASK                   0x1F
 | 
			
		||||
#define RFLR_MODEMSTAT_MODEM_STATUS_MASK            0xE0
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPktSnrValue (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPktRssiValue (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegRssiValue (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegHopChannel (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_HOPCHANNEL_PLL_LOCK_TIMEOUT_MASK       0x7F
 | 
			
		||||
#define RFLR_HOPCHANNEL_PLL_LOCK_FAIL               0x80
 | 
			
		||||
#define RFLR_HOPCHANNEL_PLL_LOCK_SUCCEED            0x00 // Default
 | 
			
		||||
 | 
			
		||||
#define RFLR_HOPCHANNEL_CRCONPAYLOAD_MASK           0xBF
 | 
			
		||||
#define RFLR_HOPCHANNEL_CRCONPAYLOAD_ON             0x40
 | 
			
		||||
#define RFLR_HOPCHANNEL_CRCONPAYLOAD_OFF            0x00 // Default
 | 
			
		||||
 | 
			
		||||
#define RFLR_HOPCHANNEL_CHANNEL_MASK                0x3F
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegModemConfig1
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_MASK                   0x0F
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_7_81_KHZ               0x00
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_10_41_KHZ              0x10
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_15_62_KHZ              0x20
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_20_83_KHZ              0x30
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_31_25_KHZ              0x40
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_41_66_KHZ              0x50
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_62_50_KHZ              0x60
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_125_KHZ                0x70 // Default
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_250_KHZ                0x80
 | 
			
		||||
#define RFLR_MODEMCONFIG1_BW_500_KHZ                0x90
 | 
			
		||||
 | 
			
		||||
#define RFLR_MODEMCONFIG1_CODINGRATE_MASK           0xF1
 | 
			
		||||
#define RFLR_MODEMCONFIG1_CODINGRATE_4_5            0x02
 | 
			
		||||
#define RFLR_MODEMCONFIG1_CODINGRATE_4_6            0x04 // Default
 | 
			
		||||
#define RFLR_MODEMCONFIG1_CODINGRATE_4_7            0x06
 | 
			
		||||
#define RFLR_MODEMCONFIG1_CODINGRATE_4_8            0x08
 | 
			
		||||
 | 
			
		||||
#define RFLR_MODEMCONFIG1_IMPLICITHEADER_MASK       0xFE
 | 
			
		||||
#define RFLR_MODEMCONFIG1_IMPLICITHEADER_ON         0x01
 | 
			
		||||
#define RFLR_MODEMCONFIG1_IMPLICITHEADER_OFF        0x00 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegModemConfig2
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_MODEMCONFIG2_SF_MASK                   0x0F
 | 
			
		||||
#define RFLR_MODEMCONFIG2_SF_6                      0x60
 | 
			
		||||
#define RFLR_MODEMCONFIG2_SF_7                      0x70 // Default
 | 
			
		||||
#define RFLR_MODEMCONFIG2_SF_8                      0x80
 | 
			
		||||
#define RFLR_MODEMCONFIG2_SF_9                      0x90
 | 
			
		||||
#define RFLR_MODEMCONFIG2_SF_10                     0xA0
 | 
			
		||||
#define RFLR_MODEMCONFIG2_SF_11                     0xB0
 | 
			
		||||
#define RFLR_MODEMCONFIG2_SF_12                     0xC0
 | 
			
		||||
 | 
			
		||||
#define RFLR_MODEMCONFIG2_TXCONTINUOUSMODE_MASK     0xF7
 | 
			
		||||
#define RFLR_MODEMCONFIG2_TXCONTINUOUSMODE_ON       0x08
 | 
			
		||||
#define RFLR_MODEMCONFIG2_TXCONTINUOUSMODE_OFF      0x00
 | 
			
		||||
 | 
			
		||||
#define RFLR_MODEMCONFIG2_RXPAYLOADCRC_MASK         0xFB
 | 
			
		||||
#define RFLR_MODEMCONFIG2_RXPAYLOADCRC_ON           0x04
 | 
			
		||||
#define RFLR_MODEMCONFIG2_RXPAYLOADCRC_OFF          0x00 // Default
 | 
			
		||||
 | 
			
		||||
#define RFLR_MODEMCONFIG2_SYMBTIMEOUTMSB_MASK       0xFC
 | 
			
		||||
#define RFLR_MODEMCONFIG2_SYMBTIMEOUTMSB            0x00 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegSymbTimeoutLsb
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_SYMBTIMEOUTLSB_SYMBTIMEOUT             0x64 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPreambleLengthMsb
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_PREAMBLELENGTHMSB                      0x00 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPreambleLengthLsb
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_PREAMBLELENGTHLSB                      0x08 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPayloadLength
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_PAYLOADLENGTH                          0x0E // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPayloadMaxLength
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_PAYLOADMAXLENGTH                       0xFF // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegHopPeriod
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_HOPPERIOD_FREQFOPPINGPERIOD            0x00 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFifoRxByteAddr (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegModemConfig3
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_MODEMCONFIG3_LOWDATARATEOPTIMIZE_MASK  0xF7
 | 
			
		||||
#define RFLR_MODEMCONFIG3_LOWDATARATEOPTIMIZE_ON    0x08
 | 
			
		||||
#define RFLR_MODEMCONFIG3_LOWDATARATEOPTIMIZE_OFF   0x00 // Default
 | 
			
		||||
 | 
			
		||||
#define RFLR_MODEMCONFIG3_AGCAUTO_MASK              0xFB
 | 
			
		||||
#define RFLR_MODEMCONFIG3_AGCAUTO_ON                0x04 // Default
 | 
			
		||||
#define RFLR_MODEMCONFIG3_AGCAUTO_OFF               0x00
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFeiMsb (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFeiMid (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFeiLsb (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegRssiWideband (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegDetectOptimize
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_DETECTIONOPTIMIZE_MASK                 0xF8
 | 
			
		||||
#define RFLR_DETECTIONOPTIMIZE_SF7_TO_SF12          0x03 // Default
 | 
			
		||||
#define RFLR_DETECTIONOPTIMIZE_SF6                  0x05
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegInvertIQ
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_INVERTIQ_RX_MASK                       0xBF
 | 
			
		||||
#define RFLR_INVERTIQ_RX_OFF                        0x00
 | 
			
		||||
#define RFLR_INVERTIQ_RX_ON                         0x40
 | 
			
		||||
#define RFLR_INVERTIQ_TX_MASK                       0xFE
 | 
			
		||||
#define RFLR_INVERTIQ_TX_OFF                        0x01
 | 
			
		||||
#define RFLR_INVERTIQ_TX_ON                         0x00
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegDetectionThreshold
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_DETECTIONTHRESH_SF7_TO_SF12            0x0A // Default
 | 
			
		||||
#define RFLR_DETECTIONTHRESH_SF6                    0x0C
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegInvertIQ2
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_INVERTIQ2_ON                           0x19
 | 
			
		||||
#define RFLR_INVERTIQ2_OFF                          0x1D
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegDioMapping1
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO0_MASK                  0x3F
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO0_00                    0x00  // Default
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO0_01                    0x40
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO0_10                    0x80
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO0_11                    0xC0
 | 
			
		||||
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO1_MASK                  0xCF
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO1_00                    0x00  // Default
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO1_01                    0x10
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO1_10                    0x20
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO1_11                    0x30
 | 
			
		||||
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO2_MASK                  0xF3
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO2_00                    0x00  // Default
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO2_01                    0x04
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO2_10                    0x08
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO2_11                    0x0C
 | 
			
		||||
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO3_MASK                  0xFC
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO3_00                    0x00  // Default
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO3_01                    0x01
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO3_10                    0x02
 | 
			
		||||
#define RFLR_DIOMAPPING1_DIO3_11                    0x03
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegDioMapping2
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_DIOMAPPING2_DIO4_MASK                  0x3F
 | 
			
		||||
#define RFLR_DIOMAPPING2_DIO4_00                    0x00  // Default
 | 
			
		||||
#define RFLR_DIOMAPPING2_DIO4_01                    0x40
 | 
			
		||||
#define RFLR_DIOMAPPING2_DIO4_10                    0x80
 | 
			
		||||
#define RFLR_DIOMAPPING2_DIO4_11                    0xC0
 | 
			
		||||
 | 
			
		||||
#define RFLR_DIOMAPPING2_DIO5_MASK                  0xCF
 | 
			
		||||
#define RFLR_DIOMAPPING2_DIO5_00                    0x00  // Default
 | 
			
		||||
#define RFLR_DIOMAPPING2_DIO5_01                    0x10
 | 
			
		||||
#define RFLR_DIOMAPPING2_DIO5_10                    0x20
 | 
			
		||||
#define RFLR_DIOMAPPING2_DIO5_11                    0x30
 | 
			
		||||
 | 
			
		||||
#define RFLR_DIOMAPPING2_MAP_MASK                   0xFE
 | 
			
		||||
#define RFLR_DIOMAPPING2_MAP_PREAMBLEDETECT         0x01
 | 
			
		||||
#define RFLR_DIOMAPPING2_MAP_RSSI                   0x00  // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegVersion (Read Only)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPllHop
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_PLLHOP_FASTHOP_MASK                    0x7F
 | 
			
		||||
#define RFLR_PLLHOP_FASTHOP_ON                      0x80
 | 
			
		||||
#define RFLR_PLLHOP_FASTHOP_OFF                     0x00 // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegTcxo
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_TCXO_TCXOINPUT_MASK                    0xEF
 | 
			
		||||
#define RFLR_TCXO_TCXOINPUT_ON                      0x10
 | 
			
		||||
#define RFLR_TCXO_TCXOINPUT_OFF                     0x00  // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPaDac
 | 
			
		||||
 */
 | 
			
		||||
#define RFLR_PADAC_20DBM_MASK                       0xF8
 | 
			
		||||
#define RFLR_PADAC_20DBM_ON                         0x07
 | 
			
		||||
#define RFLR_PADAC_20DBM_OFF                        0x04  // Default
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegFormerTemp
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegBitrateFrac
 | 
			
		||||
 */
 | 
			
		||||
#define RF_BITRATEFRAC_MASK                         0xF0
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegAgcRef
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegAgcThresh1
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegAgcThresh2
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegAgcThresh3
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * RegPll
 | 
			
		||||
 */
 | 
			
		||||
#define RF_PLL_BANDWIDTH_MASK                       0x3F
 | 
			
		||||
#define RF_PLL_BANDWIDTH_75                         0x00
 | 
			
		||||
#define RF_PLL_BANDWIDTH_150                        0x40
 | 
			
		||||
#define RF_PLL_BANDWIDTH_225                        0x80
 | 
			
		||||
#define RF_PLL_BANDWIDTH_300                        0xC0  // Default
 | 
			
		||||
 | 
			
		||||
#endif // __SX1276_REGS_LORA_H__
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_DIR := lora-module
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,41 @@
 | 
			
		|||
from building import *
 | 
			
		||||
 | 
			
		||||
src   = []
 | 
			
		||||
cwd   = GetCurrentDir()
 | 
			
		||||
include_path = [cwd]
 | 
			
		||||
include_path += [cwd+'/lora-module/inc']
 | 
			
		||||
 | 
			
		||||
# add lora radio driver.
 | 
			
		||||
if GetDepend('LORA_RADIO_DRIVER_USING_LORA_MODULE_LSD4RF_2F717N30'):
 | 
			
		||||
    src += Split('''
 | 
			
		||||
    lora-module/stm32_adapter/lora-spi-board.c
 | 
			
		||||
    lora-module/stm32_adapter/SX1278_LSD4RF-2F717N30/sx1278-board.c
 | 
			
		||||
    ''')
 | 
			
		||||
 | 
			
		||||
if GetDepend('LORA_RADIO_DRIVER_USING_LORA_MODULE_LSD4RF_2R822N30'):
 | 
			
		||||
    src += Split('''
 | 
			
		||||
    lora-module/stm32_adapter/lora-spi-board.c
 | 
			
		||||
    lora-module/stm32_adapter/SX1262_LSD4RF-2R822N30/sx1262-board.c
 | 
			
		||||
    ''')
 | 
			
		||||
 | 
			
		||||
if GetDepend('LORA_RADIO_DRIVER_USING_LORA_MODULE_LSD4RF_2R717N40'):
 | 
			
		||||
    src += Split('''
 | 
			
		||||
    lora-module/stm32_adapter/lora-spi-board.c
 | 
			
		||||
    lora-module/stm32_adapter/SX1268_LSD4RF-2R717N40/sx1268-board.c
 | 
			
		||||
    ''')
 | 
			
		||||
 | 
			
		||||
if GetDepend('LORA_RADIO_DRIVER_USING_LORA_MODULE_RA_01'):
 | 
			
		||||
    src += Split('''
 | 
			
		||||
    lora-module/stm32_adapter/lora-spi-board.c
 | 
			
		||||
    lora-module/stm32_adapter/SX1278_Ra-01/sx1278-board.c
 | 
			
		||||
    ''')
 | 
			
		||||
 | 
			
		||||
if GetDepend('LORA_RADIO_DRIVER_USING_LORA_MODULE_ASR6500S'):
 | 
			
		||||
    src += Split('''
 | 
			
		||||
    lora-module/stm32_adapter/lora-spi-board.c
 | 
			
		||||
    lora-module/stm32_adapter/SX1262_ASR6500S/sx126x-board.c
 | 
			
		||||
    ''')
 | 
			
		||||
 | 
			
		||||
group = DefineGroup('lora-radio-driver/board', src, depend = ['PKG_USING_LORA_RADIO_DRIVER'], CPPPATH = include_path)
 | 
			
		||||
 | 
			
		||||
Return('group')
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
ifeq ($(CONFIG_LORA_RADIO_DRIVER_USING_RTOS_XIUOS),y)
 | 
			
		||||
	SRC_DIR := hc32_adapter
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,18 @@
 | 
			
		|||
choice
 | 
			
		||||
    prompt "choose lora device module"
 | 
			
		||||
    default LORA_RADIO_DRIVER_USING_SX1268_E22400M30S
 | 
			
		||||
 | 
			
		||||
    config LORA_RADIO_DRIVER_USING_SX1268_E22400M30S
 | 
			
		||||
        bool "lora device module select E22-400M30S(SX1268)"
 | 
			
		||||
 | 
			
		||||
    config LORA_RADIO_DRIVER_USING_SX1278_RA01
 | 
			
		||||
        bool "lora device module select RA01(SX1278)"
 | 
			
		||||
endchoice
 | 
			
		||||
 | 
			
		||||
if LORA_RADIO_DRIVER_USING_SX1268_E22400M30S
 | 
			
		||||
    source "$APP_DIR/lib/lorawan/lora_radio_driver/ports/lora-module/hc32_adapter/SX1268_E22-400M30S/Kconfig"
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
if LORA_RADIO_DRIVER_USING_SX1278_RA01
 | 
			
		||||
    source "$APP_DIR/lib/lorawan/lora_radio_driver/ports/lora-module/hc32_adapter/SX1278_Ra-01/Kconfig"
 | 
			
		||||
endif    
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
ifeq ($(CONFIG_LORA_RADIO_DRIVER_USING_SX1268_E22400M30S),y)
 | 
			
		||||
	SRC_DIR := SX1268_E22-400M30S
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
ifeq ($(CONFIG_LORA_RADIO_DRIVER_USING_SX1278_RA01),y)
 | 
			
		||||
	SRC_DIR := SX1278_Ra-01
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
SRC_FILES := lora-spi-board.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,45 @@
 | 
			
		|||
choice
 | 
			
		||||
    prompt "choose board for lora radio driver"
 | 
			
		||||
    default LORA_RADIO_DRIVER_USING_XISHUTONG_ARM32
 | 
			
		||||
 | 
			
		||||
    config LORA_RADIO_DRIVER_USING_EDU_ARM32
 | 
			
		||||
        bool "board select edu-arm32 bsp"
 | 
			
		||||
 | 
			
		||||
    config LORA_RADIO_DRIVER_USING_XISHUTONG_ARM32
 | 
			
		||||
        bool "board select xishutong-arm32 bsp"
 | 
			
		||||
endchoice
 | 
			
		||||
 | 
			
		||||
if LORA_RADIO_DRIVER_USING_EDU_ARM32
 | 
			
		||||
        config LORA_SPI_DEV_NAME
 | 
			
		||||
                string "lora device pin driver path"
 | 
			
		||||
                default "/dev/spi1_dev0"
 | 
			
		||||
 | 
			
		||||
        config LORA_RADIO_RESET_PIN
 | 
			
		||||
                int "hc32 board lora chip reset pin[PI02]"
 | 
			
		||||
                default "133"
 | 
			
		||||
 | 
			
		||||
        config LORA_RADIO_BUSY_PIN
 | 
			
		||||
                int "hc32 board lora chip busy pin[PE03]"
 | 
			
		||||
                default "2"
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
if LORA_RADIO_DRIVER_USING_XISHUTONG_ARM32
 | 
			
		||||
        config LORA_SPI_DEV_NAME
 | 
			
		||||
                string "lora device pin driver path"
 | 
			
		||||
                default "/dev/spi2_dev0"
 | 
			
		||||
 | 
			
		||||
        config LORA_RADIO_RESET_PIN
 | 
			
		||||
                int "xishutong-arm32 board lora chip reset pin[PD02]"
 | 
			
		||||
                default "144"
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
config LORA_RADIO_DRIVER_USING_LORA_CHIP_SX126X
 | 
			
		||||
        bool "hc32 board using lora chip sx126x"
 | 
			
		||||
        default y
 | 
			
		||||
 | 
			
		||||
if LORA_RADIO_DRIVER_USING_LORA_CHIP_SX126X
 | 
			
		||||
        config LORA_RADIO_DRIVER_USING_LORA_CHIP_SX1268
 | 
			
		||||
                bool "hc32 board using lora chip sx1268"
 | 
			
		||||
                default y
 | 
			
		||||
endif
 | 
			
		||||
        
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_FILES := sx1268-board.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,394 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      sx1268-board.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     Target board SX126x shield driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file sx126x-board.c
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-06-06
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: sx126x-board.c
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-06-06
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
#include <lora-radio.h>
 | 
			
		||||
#include <sx126x-board.h>
 | 
			
		||||
 | 
			
		||||
#define LOG_TAG "LoRa.Board.E22-400M30S(SX1268)" // E22-400M30S
 | 
			
		||||
#define LOG_LEVEL  LOG_LVL_DBG 
 | 
			
		||||
#include <lora-radio-debug.h>
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Debug GPIO pins objects
 | 
			
		||||
 */
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
Gpio_t DbgPinTx;
 | 
			
		||||
Gpio_t DbgPinRx;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#if ( RT_VER_NUM <= 0x40004 )
 | 
			
		||||
int stm32_pin_get(char *pin_name)
 | 
			
		||||
{
 | 
			
		||||
    /* eg: pin_name : "PA.4"  ( GPIOA, GPIO_PIN_4 )--> drv_gpio.c pin */
 | 
			
		||||
    char pin_index = strtol(&pin_name[3],0,10);
 | 
			
		||||
    
 | 
			
		||||
    if(pin_name[1] < 'A' || pin_name[1] > 'Z')
 | 
			
		||||
    {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return (16 * (pin_name[1]-'A') + pin_index);
 | 
			
		||||
}
 | 
			
		||||
#endif 
 | 
			
		||||
#endif /* LORA_RADIO_GPIO_SETUP_BY_PIN_NAME */
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
void SX126xIoInit( void )
 | 
			
		||||
{
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_NSS_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_BUSY_PIN, PIN_MODE_INPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#if defined( LORA_RADIO_DIO2_PIN ) 
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO2_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#endif
 | 
			
		||||
#if defined( LORA_RADIO_RFSW1_PIN ) && defined ( LORA_RADIO_RFSW2_PIN )   
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RFSW1_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RFSW2_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoIrqInit( DioIrqHandler dioIrq )
 | 
			
		||||
{ 
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO1_PIN, PIN_IRQ_MODE_RISING, dioIrq,(void*)"rf-dio1");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO1_PIN, PIN_IRQ_ENABLE);  
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
////    GpioInit( &SX126x.Spi.Nss, RADIO_NSS, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_PULL_UP, 1 );
 | 
			
		||||
////    GpioInit( &SX126x.BUSY, RADIO_BUSY, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
////    GpioInit( &SX126x.DIO1, RADIO_DIO_1, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
    SX126xAntSwOff();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoDbgInit( void )
 | 
			
		||||
{
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
    GpioInit( &DbgPinTx, RADIO_DBG_PIN_TX, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
    GpioInit( &DbgPinRx, RADIO_DBG_PIN_RX, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoTcxoInit( void )
 | 
			
		||||
{
 | 
			
		||||
    // Initialize TCXO control
 | 
			
		||||
    CalibrationParams_t calibParam;
 | 
			
		||||
    
 | 
			
		||||
    // +clear OSC_START_ERR for reboot or cold-start from sleep
 | 
			
		||||
    SX126xClearDeviceErrors();
 | 
			
		||||
    
 | 
			
		||||
    // TCXO_CTRL_1_7V -> TCXO_CTRL_2_7V 64*15.0625US
 | 
			
		||||
    SX126xSetDio3AsTcxoCtrl( TCXO_CTRL_2_7V, 320);//SX126xGetBoardTcxoWakeupTime( ) << 6 ); // convert from ms to SX126x time base
 | 
			
		||||
    
 | 
			
		||||
    calibParam.Value = 0x7F;
 | 
			
		||||
    SX126xCalibrate( calibParam );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t SX126xGetBoardTcxoWakeupTime( void )
 | 
			
		||||
{
 | 
			
		||||
    return BOARD_TCXO_WAKEUP_TIME;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xReset( void )
 | 
			
		||||
{   
 | 
			
		||||
    SX126X_DELAY_MS( 10 );
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_OUTPUT); 
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RESET_PIN, PIN_LOW);
 | 
			
		||||
    SX126X_DELAY_MS( 20 );
 | 
			
		||||
     // internal pull-up
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_INPUT); 
 | 
			
		||||
    SX126X_DELAY_MS( 10 ); 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xWaitOnBusy( void )
 | 
			
		||||
{
 | 
			
		||||
    while( rt_pin_read( LORA_RADIO_BUSY_PIN ) == PIN_HIGH );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xAntSwOn( void )
 | 
			
		||||
{
 | 
			
		||||
   // No need
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xAntSwOff( void )
 | 
			
		||||
{
 | 
			
		||||
    ////GpioInit( &AntPow, RADIO_ANT_SWITCH_POWER, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
    
 | 
			
		||||
#if defined( LORA_RADIO_RFSW1_PIN ) && defined ( LORA_RADIO_RFSW2_PIN )   
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_LOW);
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_LOW);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetAntSw( RadioOperatingModes_t mode )
 | 
			
		||||
{
 | 
			
		||||
    if( mode == MODE_TX )
 | 
			
		||||
    { // Transmit
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_HIGH);
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_LOW);
 | 
			
		||||
    }
 | 
			
		||||
    else 
 | 
			
		||||
    {
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_LOW);
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_HIGH);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
static int pin_fd = -1;
 | 
			
		||||
void SX126xIoInit( void )
 | 
			
		||||
{ 
 | 
			
		||||
    if (pin_fd < 0) {
 | 
			
		||||
        pin_fd = PrivOpen("/dev/pin_dev", O_RDWR);
 | 
			
		||||
        if (pin_fd < 0) {
 | 
			
		||||
            printf("open %s error\n", "/dev/pin_dev");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_EDU_ARM32
 | 
			
		||||
    struct PinParam pin_param;
 | 
			
		||||
    pin_param.cmd = GPIO_CONFIG_MODE;
 | 
			
		||||
    pin_param.mode = GPIO_CFG_OUTPUT;
 | 
			
		||||
    pin_param.pin = LORA_RADIO_RFSW1_PIN;
 | 
			
		||||
 | 
			
		||||
    struct PrivIoctlCfg ioctl_cfg;
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = PIN_TYPE;
 | 
			
		||||
    ioctl_cfg.args = &pin_param;
 | 
			
		||||
    PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
 | 
			
		||||
 | 
			
		||||
    struct PinStat pin_stat;
 | 
			
		||||
    pin_stat.pin = LORA_RADIO_RFSW1_PIN;
 | 
			
		||||
    pin_stat.val = GPIO_LOW;
 | 
			
		||||
    PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
 | 
			
		||||
    pin_param.cmd = GPIO_CONFIG_MODE;
 | 
			
		||||
    pin_param.mode = GPIO_CFG_OUTPUT;
 | 
			
		||||
    pin_param.pin = LORA_RADIO_RFSW2_PIN;
 | 
			
		||||
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = PIN_TYPE;
 | 
			
		||||
    ioctl_cfg.args = &pin_param;
 | 
			
		||||
    PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
 | 
			
		||||
 | 
			
		||||
    pin_stat.pin = LORA_RADIO_RFSW2_PIN;
 | 
			
		||||
    pin_stat.val = GPIO_LOW;
 | 
			
		||||
    PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
 | 
			
		||||
    pin_param.cmd = GPIO_CONFIG_MODE;
 | 
			
		||||
    pin_param.mode = GPIO_CFG_INPUT;
 | 
			
		||||
    pin_param.pin = LORA_RADIO_BUSY_PIN;
 | 
			
		||||
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = PIN_TYPE;
 | 
			
		||||
    ioctl_cfg.args = &pin_param;
 | 
			
		||||
    PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
 | 
			
		||||
 | 
			
		||||
    pin_stat.pin = LORA_RADIO_BUSY_PIN;
 | 
			
		||||
    pin_stat.val = GPIO_LOW;
 | 
			
		||||
    PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoIrqInit( DioIrqHandler dioIrq )
 | 
			
		||||
{ 
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
    SX126xAntSwOff();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoDbgInit( void )
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoTcxoInit( void )
 | 
			
		||||
{
 | 
			
		||||
    // Initialize TCXO control
 | 
			
		||||
    CalibrationParams_t calibParam;
 | 
			
		||||
    
 | 
			
		||||
    // +clear OSC_START_ERR for reboot or cold-start from sleep
 | 
			
		||||
    SX126xClearDeviceErrors();
 | 
			
		||||
    
 | 
			
		||||
    // TCXO_CTRL_1_7V -> TCXO_CTRL_2_7V 64*15.0625US
 | 
			
		||||
    SX126xSetDio3AsTcxoCtrl( TCXO_CTRL_2_7V, 320);//SX126xGetBoardTcxoWakeupTime( ) << 6 ); // convert from ms to SX126x time base
 | 
			
		||||
    
 | 
			
		||||
    calibParam.Value = 0x7F;
 | 
			
		||||
    SX126xCalibrate( calibParam );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t SX126xGetBoardTcxoWakeupTime( void )
 | 
			
		||||
{
 | 
			
		||||
    return BOARD_TCXO_WAKEUP_TIME;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xReset( void )
 | 
			
		||||
{   
 | 
			
		||||
    SX126X_DELAY_MS( 10 );
 | 
			
		||||
    
 | 
			
		||||
    if (pin_fd < 0) {
 | 
			
		||||
        pin_fd = PrivOpen("/dev/pin_dev", O_RDWR);
 | 
			
		||||
        if (pin_fd < 0) {
 | 
			
		||||
            printf("open %s error\n", "/dev/pin_dev");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    struct PinParam pin_param;
 | 
			
		||||
    pin_param.cmd = GPIO_CONFIG_MODE;
 | 
			
		||||
    pin_param.mode = GPIO_CFG_OUTPUT;
 | 
			
		||||
    pin_param.pin = LORA_RADIO_RESET_PIN;
 | 
			
		||||
 | 
			
		||||
    struct PrivIoctlCfg ioctl_cfg;
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = PIN_TYPE;
 | 
			
		||||
    ioctl_cfg.args = &pin_param;
 | 
			
		||||
    PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
 | 
			
		||||
 | 
			
		||||
    struct PinStat pin_stat;
 | 
			
		||||
    pin_stat.pin = LORA_RADIO_RESET_PIN;
 | 
			
		||||
    pin_stat.val = GPIO_LOW;
 | 
			
		||||
    PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
 | 
			
		||||
    // Wait 20 ms
 | 
			
		||||
    SX126X_DELAY_MS( 20 );
 | 
			
		||||
 | 
			
		||||
    // Configure RESET as input
 | 
			
		||||
    pin_param.cmd = GPIO_CONFIG_MODE;
 | 
			
		||||
    pin_param.mode = GPIO_CFG_INPUT;
 | 
			
		||||
    pin_param.pin = LORA_RADIO_RESET_PIN;
 | 
			
		||||
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = PIN_TYPE;
 | 
			
		||||
    ioctl_cfg.args = &pin_param;
 | 
			
		||||
    PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
 | 
			
		||||
 | 
			
		||||
    // Wait 10 ms
 | 
			
		||||
    SX126X_DELAY_MS( 10 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xWaitOnBusy( void )
 | 
			
		||||
{
 | 
			
		||||
    if (pin_fd < 0) {
 | 
			
		||||
        pin_fd = PrivOpen("/dev/pin_dev", O_RDWR);
 | 
			
		||||
        if (pin_fd < 0) {
 | 
			
		||||
            printf("open %s error\n", "/dev/pin_dev");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_EDU_ARM32
 | 
			
		||||
    struct PinStat pin_stat;
 | 
			
		||||
    pin_stat.pin = LORA_RADIO_BUSY_PIN;
 | 
			
		||||
    PrivRead(pin_fd, &pin_stat, 1);
 | 
			
		||||
 | 
			
		||||
    while (GPIO_HIGH == pin_stat.val) {
 | 
			
		||||
        PrivRead(pin_fd, &pin_stat, 1);
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xAntSwOn( void )
 | 
			
		||||
{
 | 
			
		||||
   // No need
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xAntSwOff( void )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_EDU_ARM32
 | 
			
		||||
    struct PinStat pin_stat;
 | 
			
		||||
    pin_stat.pin = LORA_RADIO_RFSW1_PIN;
 | 
			
		||||
    pin_stat.val = GPIO_LOW;
 | 
			
		||||
    PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
 | 
			
		||||
    pin_stat.pin = LORA_RADIO_RFSW2_PIN;
 | 
			
		||||
    pin_stat.val = GPIO_LOW;
 | 
			
		||||
    PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetAntSw( RadioOperatingModes_t mode )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_EDU_ARM32
 | 
			
		||||
    struct PinStat pin_stat;
 | 
			
		||||
    if( mode == MODE_TX ) { // Transmit
 | 
			
		||||
        pin_stat.pin = LORA_RADIO_RFSW1_PIN;
 | 
			
		||||
        pin_stat.val = GPIO_HIGH;
 | 
			
		||||
        PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
 | 
			
		||||
        pin_stat.pin = LORA_RADIO_RFSW2_PIN;
 | 
			
		||||
        pin_stat.val = GPIO_LOW;
 | 
			
		||||
        PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
    } else {
 | 
			
		||||
        pin_stat.pin = LORA_RADIO_RFSW1_PIN;
 | 
			
		||||
        pin_stat.val = GPIO_LOW;
 | 
			
		||||
        PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
 | 
			
		||||
        pin_stat.pin = LORA_RADIO_RFSW2_PIN;
 | 
			
		||||
        pin_stat.val = GPIO_HIGH;
 | 
			
		||||
        PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
bool SX126xCheckRfFrequency( uint32_t frequency )
 | 
			
		||||
{
 | 
			
		||||
    // Implement check. Currently all frequencies are supported
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
void SX126xDbgPinTxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinTx, state );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xDbgPinRxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinRx, state );
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,18 @@
 | 
			
		|||
config LORA_SPI_DEV_NAME
 | 
			
		||||
        string "lora device pin driver path"
 | 
			
		||||
        default "/dev/spi1_dev0"
 | 
			
		||||
 | 
			
		||||
config LORA_RADIO_RESET_PIN
 | 
			
		||||
        int "hc32 board lora chip reset pin[PI02]"
 | 
			
		||||
        default "133"
 | 
			
		||||
 | 
			
		||||
config LORA_RADIO_DRIVER_USING_LORA_CHIP_SX127X
 | 
			
		||||
        bool "hc32 board using lora chip sx127x"
 | 
			
		||||
        default y
 | 
			
		||||
 | 
			
		||||
if LORA_RADIO_DRIVER_USING_LORA_CHIP_SX127X
 | 
			
		||||
        config LORA_RADIO_DRIVER_USING_LORA_CHIP_SX1278
 | 
			
		||||
                bool "hc32 board using lora chip sx1278"
 | 
			
		||||
                default y
 | 
			
		||||
endif
 | 
			
		||||
        
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_FILES := sx1278-board.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,283 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      sx1278-board.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     Target board SX127x driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    AIIT XUOS Lab
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file sx127x-board.c
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-13
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: sx127x-board.c
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-13
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
#include <lora-radio.h>
 | 
			
		||||
#include <sx127x.h>
 | 
			
		||||
#include <sx127x-board.h>
 | 
			
		||||
 | 
			
		||||
#define LOG_TAG "LoRa.Board.Ra-01(SX1278)"
 | 
			
		||||
#define LOG_LEVEL  LOG_LVL_DBG 
 | 
			
		||||
#include <lora-radio-debug.h>
 | 
			
		||||
/*!
 | 
			
		||||
 * Flag used to set the RF switch control pins in low power mode when the radio is not active.
 | 
			
		||||
 */
 | 
			
		||||
static bool RadioIsActive = false;
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
static int pin_fd = -1;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 0 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio0IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 1 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio1IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 2 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio2IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 3 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio3IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 4 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio4IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 5 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio5IrqEvent( void *args );
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Debug GPIO pins objects
 | 
			
		||||
 */
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
Gpio_t DbgPinTx;
 | 
			
		||||
Gpio_t DbgPinRx;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#if ( RT_VER_NUM <= 0x40004 )
 | 
			
		||||
int stm32_pin_get(char *pin_name)
 | 
			
		||||
{
 | 
			
		||||
    /* eg: pin_name : "PA.4"  ( GPIOA, GPIO_PIN_4 )--> drv_gpio.c pin */
 | 
			
		||||
    char pin_index = strtol(&pin_name[3],0,10);
 | 
			
		||||
    
 | 
			
		||||
    if(pin_name[1] < 'A' || pin_name[1] > 'Z')
 | 
			
		||||
    {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return (16 * (pin_name[1]-'A') + pin_index);
 | 
			
		||||
}
 | 
			
		||||
#endif 
 | 
			
		||||
#endif /* LORA_RADIO_GPIO_SETUP_BY_PIN_NAME */
 | 
			
		||||
 | 
			
		||||
void SX127xIoInit( void )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
    // RT-Thread
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_NSS_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO0_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#ifdef LORA_RADIO_DIO1_PIN     
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#endif    
 | 
			
		||||
#ifdef LORA_RADIO_DIO2_PIN      
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO2_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#endif    
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xIoIrqInit( DioIrqHandler **irqHandlers )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
    #ifdef LORA_RADIO_DIO0_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO0_PIN, PIN_IRQ_MODE_RISING,SX127xOnDio0IrqEvent,(void*)"rf-dio0");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO0_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifdef LORA_RADIO_DIO1_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO1_PIN, PIN_IRQ_MODE_RISING,SX127xOnDio1IrqEvent,(void*)"rf-dio1");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO1_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifdef LORA_RADIO_DIO2_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO2_PIN, PIN_IRQ_MODE_RISING,SX127xOnDio2IrqEvent,(void*)"rf-dio2");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO2_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
    #endif
 | 
			
		||||
#endif    
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xIoDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xIoDbgInit( void )
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void SX127xReset( void )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
    // Set RESET pin to 0
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RESET_PIN, PIN_LOW);
 | 
			
		||||
 | 
			
		||||
    // Wait 1 ms
 | 
			
		||||
    SX127X_DELAY_MS( 1 );
 | 
			
		||||
 | 
			
		||||
    // Configure RESET as input
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_INPUT);
 | 
			
		||||
 | 
			
		||||
    // Wait 6 ms
 | 
			
		||||
    SX127X_DELAY_MS( 6 );
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
    if (pin_fd < 0) {
 | 
			
		||||
        pin_fd = PrivOpen("/dev/pin_dev", O_RDWR);
 | 
			
		||||
        if (pin_fd < 0) {
 | 
			
		||||
            printf("open %s error\n", "/dev/pin_dev");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    struct PinParam pin_param;
 | 
			
		||||
    pin_param.cmd = GPIO_CONFIG_MODE;
 | 
			
		||||
    pin_param.mode = GPIO_CFG_OUTPUT;
 | 
			
		||||
    pin_param.pin = LORA_RADIO_RESET_PIN;
 | 
			
		||||
 | 
			
		||||
    struct PrivIoctlCfg ioctl_cfg;
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = PIN_TYPE;
 | 
			
		||||
    ioctl_cfg.args = &pin_param;
 | 
			
		||||
    PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
 | 
			
		||||
 | 
			
		||||
    struct PinStat pin_stat;
 | 
			
		||||
    pin_stat.pin = LORA_RADIO_RESET_PIN;
 | 
			
		||||
    pin_stat.val = GPIO_LOW;
 | 
			
		||||
    PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
 | 
			
		||||
    // Wait 1 ms
 | 
			
		||||
    SX127X_DELAY_MS( 1 );
 | 
			
		||||
 | 
			
		||||
    // Configure RESET as input
 | 
			
		||||
    pin_param.cmd = GPIO_CONFIG_MODE;
 | 
			
		||||
    pin_param.mode = GPIO_CFG_INPUT;
 | 
			
		||||
    pin_param.pin = LORA_RADIO_RESET_PIN;
 | 
			
		||||
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = PIN_TYPE;
 | 
			
		||||
    ioctl_cfg.args = &pin_param;
 | 
			
		||||
    PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
 | 
			
		||||
 | 
			
		||||
    // Wait 6 ms
 | 
			
		||||
    SX127X_DELAY_MS( 6 );
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xSetAntSwLowPower( bool status )
 | 
			
		||||
{
 | 
			
		||||
    if( RadioIsActive != status )
 | 
			
		||||
    {
 | 
			
		||||
        RadioIsActive = status;
 | 
			
		||||
 | 
			
		||||
        if( status == false )
 | 
			
		||||
        {
 | 
			
		||||
            SX127xAntSwInit( );
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            SX127xAntSwDeInit( );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xAntSwInit( void )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xAntSwDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* TX and RX ANT switch */
 | 
			
		||||
 | 
			
		||||
void SX127xSetAntSw( uint8_t opMode )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
void SX127xDbgPinTxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinTx, state );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xDbgPinRxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinRx, state );
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
uint8_t SX127xGetPaSelect( int8_t power )
 | 
			
		||||
{
 | 
			
		||||
    return RF_PACONFIG_PASELECT_PABOOST;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool SX127xCheckRfFrequency( uint32_t frequency )
 | 
			
		||||
{
 | 
			
		||||
    // Implement check. Currently all frequencies are supported,todo depend on board
 | 
			
		||||
    return true; 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,158 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-spi-board.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     spi peripheral initlize,it depend on mcu platform.
 | 
			
		||||
 *
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    AIIT XUOS Lab
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file lora-spi-board.c
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-13
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: lora-spi-board.c
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-13
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_LORA_CHIP_SX126X
 | 
			
		||||
#include <sx126x-board.h>
 | 
			
		||||
#elif defined LORA_RADIO_DRIVER_USING_LORA_CHIP_SX127X
 | 
			
		||||
#include <sx127x-board.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define LOG_TAG "LoRa.HC32.SPI"
 | 
			
		||||
#define LOG_LEVEL  LOG_LVL_DBG 
 | 
			
		||||
#include <lora-radio-debug.h>
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
/* for get GPIO fort,eg GPIOA */
 | 
			
		||||
#ifndef GET_GPIO_PORT
 | 
			
		||||
#define GET_GPIO_PORT(pin) (GPIO_TypeDef *)( GPIOA_BASE + (uint32_t) ( pin >> 4 ) * 0x0400UL ) 
 | 
			
		||||
#endif
 | 
			
		||||
#ifndef GET_GPIO_PIN
 | 
			
		||||
#define GET_GPIO_PIN(pin) (rt_uint16_t)( 1 << ( pin & 0x0F ) ) // for get GPIO_PIN, eg: GPIO_PIN_6
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#if ( RT_VER_NUM <= 0x40002 )
 | 
			
		||||
extern int stm32_pin_get(char *pin_name);
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
struct rt_spi_device *lora_radio_spi_init(const char *bus_name, const char *lora_device_name, rt_uint8_t param)
 | 
			
		||||
{
 | 
			
		||||
    rt_err_t res;
 | 
			
		||||
    struct rt_spi_device *lora_radio_spi_device;
 | 
			
		||||
    
 | 
			
		||||
    RT_ASSERT(bus_name);
 | 
			
		||||
    
 | 
			
		||||
    {
 | 
			
		||||
        res = rt_hw_spi_device_attach( bus_name, lora_device_name, LORA_RADIO_NSS_PIN);
 | 
			
		||||
        
 | 
			
		||||
        if (res != RT_EOK)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"rt_spi_bus_attach_device failed!\r\n");
 | 
			
		||||
            return RT_NULL;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        lora_radio_spi_device = (struct rt_spi_device *)rt_device_find(lora_device_name);
 | 
			
		||||
        if (!lora_radio_spi_device)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"spi sample run failed! cant't find %s device!\n", lora_device_name);
 | 
			
		||||
            return RT_NULL;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"find %s device!\n", lora_device_name);
 | 
			
		||||
    
 | 
			
		||||
    /* config spi */
 | 
			
		||||
    {
 | 
			
		||||
        struct rt_spi_configuration cfg;
 | 
			
		||||
        cfg.data_width = 8;
 | 
			
		||||
        cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0. */
 | 
			
		||||
        cfg.max_hz = 8 * 1000000;             /* max 10M */
 | 
			
		||||
        
 | 
			
		||||
        res = rt_spi_configure(lora_radio_spi_device, &cfg);
 | 
			
		||||
        
 | 
			
		||||
        if (res != RT_EOK)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"rt_spi_configure failed!\r\n");
 | 
			
		||||
        }
 | 
			
		||||
        res = rt_spi_take_bus(lora_radio_spi_device);
 | 
			
		||||
        if (res != RT_EOK)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"rt_spi_take_bus failed!\r\n");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        res = rt_spi_release_bus(lora_radio_spi_device);
 | 
			
		||||
        
 | 
			
		||||
        if(res != RT_EOK)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"rt_spi_release_bus failed!\r\n");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
	
 | 
			
		||||
    return lora_radio_spi_device;
 | 
			
		||||
} 
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This function releases memory
 | 
			
		||||
 *
 | 
			
		||||
 * @param dev the pointer of device driver structure
 | 
			
		||||
 */
 | 
			
		||||
void lora_radio_spi_deinit(struct rt_spi_device *dev)
 | 
			
		||||
{
 | 
			
		||||
    RT_ASSERT(dev);
 | 
			
		||||
    rt_spi_release_bus(dev);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
int lora_radio_spi_init(void)
 | 
			
		||||
{
 | 
			
		||||
    int spi_fd;
 | 
			
		||||
    spi_fd = PrivOpen(LORA_SPI_DEV_NAME, O_RDWR);
 | 
			
		||||
    if (spi_fd < 0) {
 | 
			
		||||
        printf("open %s error\n", LORA_SPI_DEV_NAME);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }    
 | 
			
		||||
    
 | 
			
		||||
    struct SpiMasterParam spi_master_param;
 | 
			
		||||
    spi_master_param.spi_data_bit_width = 8;
 | 
			
		||||
    spi_master_param.spi_work_mode = SPI_MODE_0 | SPI_MSB;
 | 
			
		||||
 | 
			
		||||
    struct PrivIoctlCfg ioctl_cfg;
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = SPI_TYPE;
 | 
			
		||||
    ioctl_cfg.args = &spi_master_param;
 | 
			
		||||
    PrivIoctl(spi_fd, OPE_CFG, &ioctl_cfg);
 | 
			
		||||
 | 
			
		||||
    ioctl_cfg.args = NULL;
 | 
			
		||||
    PrivIoctl(spi_fd, OPE_INT, &ioctl_cfg);
 | 
			
		||||
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void lora_radio_spi_deinit(int spi_fd)
 | 
			
		||||
{
 | 
			
		||||
    PrivClose(spi_fd);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,256 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      sx126x-board.h
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     Target board SX126x driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    AIIT XUOS Lab
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file sx126x-board.h
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-13
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: sx126x-board.h
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-13
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#ifndef __SX126x_BOARD_H__
 | 
			
		||||
#define __SX126x_BOARD_H__
 | 
			
		||||
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <sx126x.h>
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#if ( RT_VER_NUM <= 0x40002 )
 | 
			
		||||
    #define LORA_RADIO_NSS_PIN       stm32_pin_get(LORA_RADIO_NSS_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_BUSY_PIN      stm32_pin_get(LORA_RADIO_BUSY_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_DIO1_PIN      stm32_pin_get(LORA_RADIO_DIO1_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_RESET_PIN     stm32_pin_get(LORA_RADIO_RESET_PIN_NAME)
 | 
			
		||||
    #if defined( LORA_RADIO_DIO2_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO2_PIN      stm32_pin_get(LORA_RADIO_DIO2_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_RFSW1_PIN_NAME ) && defined ( LORA_RADIO_RFSW2_PIN_NAME )  
 | 
			
		||||
    #define LORA_RADIO_RFSW1_PIN     stm32_pin_get(LORA_RADIO_RFSW1_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_RFSW2_PIN     stm32_pin_get(LORA_RADIO_RFSW2_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
#else
 | 
			
		||||
    #define LORA_RADIO_NSS_PIN       rt_pin_get(LORA_RADIO_NSS_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_BUSY_PIN      rt_pin_get(LORA_RADIO_BUSY_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_DIO1_PIN      rt_pin_get(LORA_RADIO_DIO1_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_RESET_PIN     rt_pin_get(LORA_RADIO_RESET_PIN_NAME)
 | 
			
		||||
    #if defined( LORA_RADIO_DIO2_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO2_PIN      rt_pin_get(LORA_RADIO_DIO2_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_RFSW1_PIN_NAME ) && defined ( LORA_RADIO_RFSW2_PIN_NAME )  
 | 
			
		||||
    #define LORA_RADIO_RFSW1_PIN     rt_pin_get(LORA_RADIO_RFSW1_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_RFSW2_PIN     rt_pin_get(LORA_RADIO_RFSW2_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
#endif/* ( RT_VER_NUM <= 0x40002) */
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
    /* if not use env\menuconfig,define Radio GPIO directly.*/
 | 
			
		||||
    #ifndef LORA_RADIO_NSS_PIN
 | 
			
		||||
    #define LORA_RADIO_NSS_PIN    GET_PIN(A,15)
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifndef LORA_RADIO_RESET_PIN
 | 
			
		||||
    #define LORA_RADIO_RESET_PIN  GET_PIN(A,7)
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifndef LORA_RADIO_DIO1_PIN
 | 
			
		||||
    #define LORA_RADIO_DIO1_PIN   GET_PIN(B,1)
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifndef LORA_RADIO_BUSY_PIN
 | 
			
		||||
    #define LORA_RADIO_BUSY_PIN   GET_PIN(B,2)
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifndef LORA_RADIO_RFSW1_PIN
 | 
			
		||||
    #define LORA_RADIO_RFSW1_PIN   GET_PIN(B,0)
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifndef LORA_RADIO_RFSW2_PIN
 | 
			
		||||
    #define LORA_RADIO_RFSW2_PIN   GET_PIN(C,5)
 | 
			
		||||
    #endif
 | 
			
		||||
#endif // end of LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
  #ifdef LORA_RADIO_DRIVER_USING_EDU_ARM32
 | 
			
		||||
    #ifndef LORA_RADIO_RESET_PIN
 | 
			
		||||
    #define LORA_RADIO_RESET_PIN  133//PI02 on HC32F4A0_I
 | 
			
		||||
    #endif
 | 
			
		||||
 | 
			
		||||
    #ifndef LORA_RADIO_BUSY_PIN
 | 
			
		||||
    #define LORA_RADIO_BUSY_PIN   2//PE03 on HC32F4A0_I
 | 
			
		||||
    #endif
 | 
			
		||||
 | 
			
		||||
    #ifndef LORA_RADIO_RFSW1_PIN
 | 
			
		||||
    #define LORA_RADIO_RFSW1_PIN  140//PC11 on HC32F4A0_I TX_EN
 | 
			
		||||
    #endif
 | 
			
		||||
 | 
			
		||||
    #ifndef LORA_RADIO_RFSW2_PIN
 | 
			
		||||
    #define LORA_RADIO_RFSW2_PIN  143//PD01 on HC32F4A0_I RX_EN
 | 
			
		||||
    #endif
 | 
			
		||||
 | 
			
		||||
    #ifndef LORA_SPI_DEV_NAME
 | 
			
		||||
    #define LORA_SPI_DEV_NAME "/dev/spi1_dev0"
 | 
			
		||||
    #endif
 | 
			
		||||
  #endif
 | 
			
		||||
 | 
			
		||||
  #ifdef LORA_RADIO_DRIVER_USING_XISHUTONG_ARM32
 | 
			
		||||
    #ifndef LORA_RADIO_RESET_PIN
 | 
			
		||||
    #define LORA_RADIO_RESET_PIN  144//PD02 on xishutong-arm32
 | 
			
		||||
    #endif
 | 
			
		||||
 | 
			
		||||
    #ifndef LORA_SPI_DEV_NAME
 | 
			
		||||
    #define LORA_SPI_DEV_NAME "/dev/spi2_dev0"
 | 
			
		||||
    #endif
 | 
			
		||||
  #endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/** @addtogroup LORA_RADIO_BOARD
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/** @addtogroup LORA_RADIO_BOARD_SX126X
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Defines the time required for the TCXO to wakeup [ms].
 | 
			
		||||
 */
 | 
			
		||||
#define BOARD_TCXO_WAKEUP_TIME                          2
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#define SX126X_DELAY_MS( ms )                           rt_thread_mdelay(ms)
 | 
			
		||||
#define SX126X_BLOCK_DELAY_1MS()                        rt_hw_us_delay(999)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
#define SX126X_DELAY_MS( ms )                           PrivTaskDelay(ms)
 | 
			
		||||
#define SX126X_BLOCK_DELAY_1MS()                        PrivTaskDelay(1)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the radio I/Os pins interface
 | 
			
		||||
 */
 | 
			
		||||
void SX126xIoInit( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes DIO IRQ handlers
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] irqHandlers Array containing the IRQ callback functions
 | 
			
		||||
 */
 | 
			
		||||
void SX126xIoIrqInit( DioIrqHandler dioIrq );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief De-initializes the radio I/Os pins interface.
 | 
			
		||||
 *
 | 
			
		||||
 * \remark Useful when going in MCU low power modes
 | 
			
		||||
 */
 | 
			
		||||
void SX126xIoDeInit( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the TCXO power pin.
 | 
			
		||||
 */
 | 
			
		||||
void SX126xIoTcxoInit( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the radio debug pins.
 | 
			
		||||
 */
 | 
			
		||||
void SX126xIoDbgInit( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief HW Reset of the radio
 | 
			
		||||
 */
 | 
			
		||||
void SX126xReset( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Blocking loop to wait while the Busy pin in high
 | 
			
		||||
 */
 | 
			
		||||
void SX126xWaitOnBusy( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the RF Switch I/Os pins interface
 | 
			
		||||
 */
 | 
			
		||||
void SX126xAntSwOn( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief De-initializes the RF Switch I/Os pins interface
 | 
			
		||||
 *
 | 
			
		||||
 * \remark Needed to decrease the power consumption in MCU low power modes
 | 
			
		||||
 */
 | 
			
		||||
void SX126xAntSwOff( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Set the RF Switch I/Os pins interface
 | 
			
		||||
 */
 | 
			
		||||
void SX126xSetAntSw( RadioOperatingModes_t mode );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Checks if the given RF frequency is supported by the hardware
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] frequency RF frequency to be checked
 | 
			
		||||
 * \retval isSupported [true: supported, false: unsupported]
 | 
			
		||||
 */
 | 
			
		||||
bool SX126xCheckRfFrequency( uint32_t frequency );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Gets the Defines the time required for the TCXO to wakeup [ms].
 | 
			
		||||
 *
 | 
			
		||||
 * \retval time Board TCXO wakeup time in ms.
 | 
			
		||||
 */
 | 
			
		||||
uint32_t SX126xGetBoardTcxoWakeupTime( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Writes new Tx debug pin state
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] state Debug pin state
 | 
			
		||||
 */
 | 
			
		||||
void SX126xDbgPinTxWrite( uint8_t state );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Writes new Rx debug pin state
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] state Debug pin state
 | 
			
		||||
 */
 | 
			
		||||
void SX126xDbgPinRxWrite( uint8_t state );
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#endif // __SX126x_BOARD_H__
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,261 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      sx127x-board.h
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     Target board SX127x driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    AIIT XUOS Lab
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @file sx127x-board.h
 | 
			
		||||
* @brief support lora-radio-driver for XiUOS
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-03-13
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: sx127x-board.h
 | 
			
		||||
Description: support lora-radio-driver for XiUOS
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-03-13
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、add LORA_RADIO_DRIVER_USING_RTOS_XIUOS to support XiUOS.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#ifndef __SX127x_BOARD_H__
 | 
			
		||||
#define __SX127x_BOARD_H__
 | 
			
		||||
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <sx127x.h>
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#if ( RT_VER_NUM <= 0x40002 )
 | 
			
		||||
    #define LORA_RADIO_NSS_PIN       stm32_pin_get(LORA_RADIO_NSS_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_RESET_PIN     stm32_pin_get(LORA_RADIO_RESET_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_DIO0_PIN      stm32_pin_get(LORA_RADIO_DIO0_PIN_NAME)
 | 
			
		||||
    #if defined( LORA_RADIO_DIO1_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO1_PIN      stm32_pin_get(LORA_RADIO_DIO1_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_DIO2_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO2_PIN      stm32_pin_get(LORA_RADIO_DIO2_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_DIO3_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO1_PIN      stm32_pin_get(LORA_RADIO_DIO3_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_DIO4_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO2_PIN      stm32_pin_get(LORA_RADIO_DIO4_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_DIO5_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO1_PIN      stm32_pin_get(LORA_RADIO_DIO5_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_RFSW1_PIN_NAME ) && defined ( LORA_RADIO_RFSW2_PIN_NAME )  
 | 
			
		||||
    #define LORA_RADIO_RFSW1_PIN     stm32_pin_get(LORA_RADIO_RFSW1_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_RFSW2_PIN     stm32_pin_get(LORA_RADIO_RFSW2_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
#else
 | 
			
		||||
    #define LORA_RADIO_NSS_PIN       rt_pin_get(LORA_RADIO_NSS_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_RESET_PIN     rt_pin_get(LORA_RADIO_RESET_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_DIO0_PIN      rt_pin_get(LORA_RADIO_DIO0_PIN_NAME)
 | 
			
		||||
    #if defined( LORA_RADIO_DIO1_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO1_PIN      rt_pin_get(LORA_RADIO_DIO1_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_DIO2_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO2_PIN      rt_pin_get(LORA_RADIO_DIO2_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_DIO3_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO1_PIN      rt_pin_get(LORA_RADIO_DIO3_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_DIO4_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO2_PIN      rt_pin_get(LORA_RADIO_DIO4_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_DIO5_PIN_NAME ) 
 | 
			
		||||
    #define LORA_RADIO_DIO1_PIN      rt_pin_get(LORA_RADIO_DIO5_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
    #if defined( LORA_RADIO_RFSW1_PIN_NAME ) && defined ( LORA_RADIO_RFSW2_PIN_NAME )  
 | 
			
		||||
    #define LORA_RADIO_RFSW1_PIN     rt_pin_get(LORA_RADIO_RFSW1_PIN_NAME)
 | 
			
		||||
    #define LORA_RADIO_RFSW2_PIN     rt_pin_get(LORA_RADIO_RFSW2_PIN_NAME)
 | 
			
		||||
    #endif
 | 
			
		||||
#endif /* ( RT_VER_NUM <= 0x40002) */
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
    /* if not use env\menuconfig,define Radio GPIO directly.*/
 | 
			
		||||
    #ifndef LORA_RADIO_NSS_PIN
 | 
			
		||||
    #define LORA_RADIO_NSS_PIN    GET_PIN(A,15)
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifndef LORA_RADIO_RESET_PIN
 | 
			
		||||
    #define LORA_RADIO_RESET_PIN  GET_PIN(A,7)
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifndef LORA_RADIO_DIO0_PIN
 | 
			
		||||
    #define LORA_RADIO_DIO0_PIN   GET_PIN(B,1)
 | 
			
		||||
    #endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif // end of LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_EDU_ARM32
 | 
			
		||||
#ifndef LORA_RADIO_RESET_PIN
 | 
			
		||||
#define LORA_RADIO_RESET_PIN  133//PI02 on HC32F4A0
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifndef LORA_SPI_DEV_NAME
 | 
			
		||||
#define LORA_SPI_DEV_NAME "/dev/spi1_dev0"
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/** @addtogroup LORA_RADIO_BOARD_SX127X
 | 
			
		||||
  * @{
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_RT_THREAD
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief delayms for radio access
 | 
			
		||||
 */
 | 
			
		||||
#define SX127X_DELAY_MS(ms)              rt_thread_mdelay(ms) 
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_RTOS_XIUOS
 | 
			
		||||
#define SX127X_DELAY_MS(ms)              PrivTaskDelay(ms)
 | 
			
		||||
#endif 
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the radio I/Os pins interface
 | 
			
		||||
 */
 | 
			
		||||
void SX127xIoInit( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief De-initializes the radio I/Os pins interface.
 | 
			
		||||
 *
 | 
			
		||||
 * \remark Useful when going in MCU low power modes
 | 
			
		||||
 */
 | 
			
		||||
void SX127xIoDeInit( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the TCXO power pin.
 | 
			
		||||
 */
 | 
			
		||||
void SX127xIoTcxoInit( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the radio debug pins.
 | 
			
		||||
 */
 | 
			
		||||
void SX127xIoDbgInit( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Resets the radio
 | 
			
		||||
 */
 | 
			
		||||
void SX127xReset( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Gets the board PA selection configuration
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] power Selects the right PA according to the wanted power.
 | 
			
		||||
 * \retval PaSelect RegPaConfig PaSelect value
 | 
			
		||||
 */
 | 
			
		||||
uint8_t SX127xGetPaSelect( int8_t power );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Set the RF Switch I/Os pins in low power mode
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] status enable or disable
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetAntSwLowPower( bool status );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Initializes the RF Switch I/Os pins interface
 | 
			
		||||
 */
 | 
			
		||||
void SX127xAntSwInit( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief De-initializes the RF Switch I/Os pins interface
 | 
			
		||||
 *
 | 
			
		||||
 * \remark Needed to decrease the power consumption in MCU low power modes
 | 
			
		||||
 */
 | 
			
		||||
void SX127xAntSwDeInit( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Controls the antenna switch if necessary.
 | 
			
		||||
 *
 | 
			
		||||
 * \remark see errata note
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] opMode Current radio operating mode
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetAntSw( uint8_t opMode );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Checks if the given RF frequency is supported by the hardware
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] frequency RF frequency to be checked
 | 
			
		||||
 * \retval isSupported [true: supported, false: unsupported]
 | 
			
		||||
 */
 | 
			
		||||
bool SX127xCheckRfFrequency( uint32_t frequency );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Enables/disables the TCXO if available on board design.
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] state TCXO enabled when true and disabled when false.
 | 
			
		||||
 */
 | 
			
		||||
void SX127xSetBoardTcxo( uint8_t state );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Gets the Defines the time required for the TCXO to wakeup [ms].
 | 
			
		||||
 *
 | 
			
		||||
 * \retval time Board TCXO wakeup time in ms.
 | 
			
		||||
 */
 | 
			
		||||
uint32_t SX127xGetBoardTcxoWakeupTime( void );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Writes new Tx debug pin state
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] state Debug pin state
 | 
			
		||||
 */
 | 
			
		||||
void SX127xDbgPinTxWrite( uint8_t state );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief Writes new Rx debug pin state
 | 
			
		||||
 *
 | 
			
		||||
 * \param [IN] state Debug pin state
 | 
			
		||||
 */
 | 
			
		||||
void SX127xDbgPinRxWrite( uint8_t state );
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
// todo PR to drv_gpio.c
 | 
			
		||||
int stm32_pin_get(char *pin_name);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Radio hardware and global parameters
 | 
			
		||||
 */
 | 
			
		||||
extern SX127x_t SX127x;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
  * @}
 | 
			
		||||
  */
 | 
			
		||||
 | 
			
		||||
#endif // __SX127x_BOARD_H__
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,79 @@
 | 
			
		|||
/*
 | 
			
		||||
 * author    Rick Zhang
 | 
			
		||||
 * date      2020.07.20
 | 
			
		||||
 */
 | 
			
		||||
#include "lora-radio-rtos-config.h"
 | 
			
		||||
#include "lora-radio.h"
 | 
			
		||||
#include "sx126x-board.h"
 | 
			
		||||
 | 
			
		||||
#define DRV_DEBUG
 | 
			
		||||
#define LOG_TAG             "LoRa.Board.ASR6500S(SX1262)" // ASR6500S
 | 
			
		||||
#include <drv_log.h>
 | 
			
		||||
 | 
			
		||||
extern void RadioOnDioIrq( void* context );
 | 
			
		||||
 | 
			
		||||
void SX126xIoInit( void )
 | 
			
		||||
{
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_NSS_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_BUSY_PIN, PIN_MODE_INPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoIrqInit( DioIrqHandler dioIrq )
 | 
			
		||||
{
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT);
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO1_PIN, PIN_IRQ_MODE_RISING, RadioOnDioIrq,(void*)"callback args");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO1_PIN, PIN_IRQ_ENABLE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoDbgInit( void )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoTcxoInit( void )
 | 
			
		||||
{
 | 
			
		||||
    CalibrationParams_t calibParam;
 | 
			
		||||
    SX126xSetDio3AsTcxoCtrl( TCXO_CTRL_1_8V, SX126xGetBoardTcxoWakeupTime( ) << 6 ); // convert from ms to SX126x time base
 | 
			
		||||
    calibParam.Value = 0x7F;
 | 
			
		||||
    SX126xCalibrate( calibParam );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t SX126xGetBoardTcxoWakeupTime( void )
 | 
			
		||||
{
 | 
			
		||||
    return BOARD_TCXO_WAKEUP_TIME;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xReset( void )
 | 
			
		||||
{
 | 
			
		||||
    SX126X_DELAY_MS( 20 );
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RESET_PIN, PIN_LOW);
 | 
			
		||||
    SX126X_DELAY_MS( 40 );
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_INPUT);
 | 
			
		||||
    SX126X_DELAY_MS( 20 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xWaitOnBusy( void )
 | 
			
		||||
{
 | 
			
		||||
    while( rt_pin_read( LORA_RADIO_BUSY_PIN ) == PIN_HIGH );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xAntSwOn( void )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xAntSwOff( void )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool SX126xCheckRfFrequency( uint32_t frequency )
 | 
			
		||||
{
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
void SX126xSetAntSw( RadioOperatingModes_t mode )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,174 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      sx1262-board.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     Target board SX126x shield driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
#include "lora-radio-rtos-config.h"
 | 
			
		||||
#include "lora-radio.h"
 | 
			
		||||
#include "sx126x-board.h"
 | 
			
		||||
 | 
			
		||||
#define LOG_TAG "LoRa.Board.LSD4RF-2R822N30(SX1262)"
 | 
			
		||||
#define LOG_LEVEL  LOG_LVL_DBG 
 | 
			
		||||
#include "lora-radio-debug.h"
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Debug GPIO pins objects
 | 
			
		||||
 */
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
Gpio_t DbgPinTx;
 | 
			
		||||
Gpio_t DbgPinRx;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#if ( RT_VER_NUM <= 0x40004 )
 | 
			
		||||
int stm32_pin_get(char *pin_name)
 | 
			
		||||
{
 | 
			
		||||
    /* eg: pin_name : "PA.4"  ( GPIOA, GPIO_PIN_4 )--> drv_gpio.c pin */
 | 
			
		||||
    char pin_index = strtol(&pin_name[3],0,10);
 | 
			
		||||
    
 | 
			
		||||
    if(pin_name[1] < 'A' || pin_name[1] > 'Z')
 | 
			
		||||
    {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return (16 * (pin_name[1]-'A') + pin_index);
 | 
			
		||||
}
 | 
			
		||||
#endif 
 | 
			
		||||
#endif /* LORA_RADIO_GPIO_SETUP_BY_PIN_NAME */
 | 
			
		||||
 | 
			
		||||
void SX126xIoInit( void )
 | 
			
		||||
{
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_NSS_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_BUSY_PIN, PIN_MODE_INPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#if defined( LORA_RADIO_DIO2_PIN ) 
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO2_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#endif
 | 
			
		||||
#if defined( LORA_RADIO_RFSW1_PIN ) && defined ( LORA_RADIO_RFSW2_PIN )   
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RFSW1_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RFSW2_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoIrqInit( DioIrqHandler dioIrq )
 | 
			
		||||
{ 
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO1_PIN, PIN_IRQ_MODE_RISING, dioIrq,(void*)"rf-dio1");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO1_PIN, PIN_IRQ_ENABLE);  
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
    SX126xAntSwOff();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoDbgInit( void )
 | 
			
		||||
{
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
    GpioInit( &DbgPinTx, RADIO_DBG_PIN_TX, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
    GpioInit( &DbgPinRx, RADIO_DBG_PIN_RX, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoTcxoInit( void )
 | 
			
		||||
{
 | 
			
		||||
    // Initialize TCXO control
 | 
			
		||||
    CalibrationParams_t calibParam;
 | 
			
		||||
    
 | 
			
		||||
    // +clear OSC_START_ERR for reboot or cold-start from sleep
 | 
			
		||||
    SX126xClearDeviceErrors();
 | 
			
		||||
    
 | 
			
		||||
    // TCXO_CTRL_2_7V 64*15.0625US
 | 
			
		||||
    SX126xSetDio3AsTcxoCtrl( TCXO_CTRL_2_7V, 320);//SX126xGetBoardTcxoWakeupTime( ) << 6 ); // convert from ms to SX126x time base
 | 
			
		||||
    
 | 
			
		||||
    calibParam.Value = 0x7F;
 | 
			
		||||
    SX126xCalibrate( calibParam );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t SX126xGetBoardTcxoWakeupTime( void )
 | 
			
		||||
{
 | 
			
		||||
    return BOARD_TCXO_WAKEUP_TIME;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xReset( void )
 | 
			
		||||
{   
 | 
			
		||||
    SX126X_DELAY_MS( 10 );
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_OUTPUT); 
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RESET_PIN, PIN_LOW);
 | 
			
		||||
    SX126X_DELAY_MS( 20 );
 | 
			
		||||
     // internal pull-up
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_INPUT); 
 | 
			
		||||
    SX126X_DELAY_MS( 10 ); 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xWaitOnBusy( void )
 | 
			
		||||
{
 | 
			
		||||
    while( rt_pin_read( LORA_RADIO_BUSY_PIN ) == PIN_HIGH );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xAntSwOn( void )
 | 
			
		||||
{
 | 
			
		||||
   // No need
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xAntSwOff( void )
 | 
			
		||||
{
 | 
			
		||||
    ////GpioInit( &AntPow, RADIO_ANT_SWITCH_POWER, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
    
 | 
			
		||||
#if defined( LORA_RADIO_RFSW1_PIN ) && defined ( LORA_RADIO_RFSW2_PIN )   
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_LOW);
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_LOW);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetAntSw( RadioOperatingModes_t mode )
 | 
			
		||||
{
 | 
			
		||||
    if( mode == MODE_TX )
 | 
			
		||||
    { // Transmit
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_HIGH);
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_LOW);
 | 
			
		||||
    }
 | 
			
		||||
    else 
 | 
			
		||||
    {
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_LOW);
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_HIGH);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool SX126xCheckRfFrequency( uint32_t frequency )
 | 
			
		||||
{
 | 
			
		||||
    // Implement check. Currently all frequencies are supported
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
void SX126xDbgPinTxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinTx, state );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xDbgPinRxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinRx, state );
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,177 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      sx1268-board.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     Target board SX126x shield driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
#include "lora-radio-rtos-config.h"
 | 
			
		||||
#include "lora-radio.h"
 | 
			
		||||
#include "sx126x-board.h"
 | 
			
		||||
 | 
			
		||||
#define LOG_TAG "LoRa.Board.LSD4RF-2R717N40(SX1268)" // LSD4RF-2R717N40
 | 
			
		||||
#define LOG_LEVEL  LOG_LVL_DBG 
 | 
			
		||||
#include "lora-radio-debug.h"
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Debug GPIO pins objects
 | 
			
		||||
 */
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
Gpio_t DbgPinTx;
 | 
			
		||||
Gpio_t DbgPinRx;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#if ( RT_VER_NUM <= 0x40004 )
 | 
			
		||||
int stm32_pin_get(char *pin_name)
 | 
			
		||||
{
 | 
			
		||||
    /* eg: pin_name : "PA.4"  ( GPIOA, GPIO_PIN_4 )--> drv_gpio.c pin */
 | 
			
		||||
    char pin_index = strtol(&pin_name[3],0,10);
 | 
			
		||||
    
 | 
			
		||||
    if(pin_name[1] < 'A' || pin_name[1] > 'Z')
 | 
			
		||||
    {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return (16 * (pin_name[1]-'A') + pin_index);
 | 
			
		||||
}
 | 
			
		||||
#endif 
 | 
			
		||||
#endif /* LORA_RADIO_GPIO_SETUP_BY_PIN_NAME */
 | 
			
		||||
 | 
			
		||||
void SX126xIoInit( void )
 | 
			
		||||
{
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_NSS_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_BUSY_PIN, PIN_MODE_INPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#if defined( LORA_RADIO_DIO2_PIN ) 
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO2_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#endif
 | 
			
		||||
#if defined( LORA_RADIO_RFSW1_PIN ) && defined ( LORA_RADIO_RFSW2_PIN )   
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RFSW1_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RFSW2_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoIrqInit( DioIrqHandler dioIrq )
 | 
			
		||||
{ 
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO1_PIN, PIN_IRQ_MODE_RISING, dioIrq,(void*)"rf-dio1");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO1_PIN, PIN_IRQ_ENABLE);  
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
////    GpioInit( &SX126x.Spi.Nss, RADIO_NSS, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_PULL_UP, 1 );
 | 
			
		||||
////    GpioInit( &SX126x.BUSY, RADIO_BUSY, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
////    GpioInit( &SX126x.DIO1, RADIO_DIO_1, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
    SX126xAntSwOff();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoDbgInit( void )
 | 
			
		||||
{
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
    GpioInit( &DbgPinTx, RADIO_DBG_PIN_TX, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
    GpioInit( &DbgPinRx, RADIO_DBG_PIN_RX, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xIoTcxoInit( void )
 | 
			
		||||
{
 | 
			
		||||
    // Initialize TCXO control
 | 
			
		||||
    CalibrationParams_t calibParam;
 | 
			
		||||
    
 | 
			
		||||
    // +clear OSC_START_ERR for reboot or cold-start from sleep
 | 
			
		||||
    SX126xClearDeviceErrors();
 | 
			
		||||
    
 | 
			
		||||
    // TCXO_CTRL_1_7V -> TCXO_CTRL_2_7V 64*15.0625US
 | 
			
		||||
    SX126xSetDio3AsTcxoCtrl( TCXO_CTRL_2_7V, 320);//SX126xGetBoardTcxoWakeupTime( ) << 6 ); // convert from ms to SX126x time base
 | 
			
		||||
    
 | 
			
		||||
    calibParam.Value = 0x7F;
 | 
			
		||||
    SX126xCalibrate( calibParam );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t SX126xGetBoardTcxoWakeupTime( void )
 | 
			
		||||
{
 | 
			
		||||
    return BOARD_TCXO_WAKEUP_TIME;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xReset( void )
 | 
			
		||||
{   
 | 
			
		||||
    SX126X_DELAY_MS( 10 );
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_OUTPUT); 
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RESET_PIN, PIN_LOW);
 | 
			
		||||
    SX126X_DELAY_MS( 20 );
 | 
			
		||||
     // internal pull-up
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_INPUT); 
 | 
			
		||||
    SX126X_DELAY_MS( 10 ); 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xWaitOnBusy( void )
 | 
			
		||||
{
 | 
			
		||||
    while( rt_pin_read( LORA_RADIO_BUSY_PIN ) == PIN_HIGH );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xAntSwOn( void )
 | 
			
		||||
{
 | 
			
		||||
   // No need
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xAntSwOff( void )
 | 
			
		||||
{
 | 
			
		||||
    ////GpioInit( &AntPow, RADIO_ANT_SWITCH_POWER, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
    
 | 
			
		||||
#if defined( LORA_RADIO_RFSW1_PIN ) && defined ( LORA_RADIO_RFSW2_PIN )   
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_LOW);
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_LOW);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xSetAntSw( RadioOperatingModes_t mode )
 | 
			
		||||
{
 | 
			
		||||
    if( mode == MODE_TX )
 | 
			
		||||
    { // Transmit
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_HIGH);
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_LOW);
 | 
			
		||||
    }
 | 
			
		||||
    else 
 | 
			
		||||
    {
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_LOW);
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_HIGH);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool SX126xCheckRfFrequency( uint32_t frequency )
 | 
			
		||||
{
 | 
			
		||||
    // Implement check. Currently all frequencies are supported
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
void SX126xDbgPinTxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinTx, state );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX126xDbgPinRxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinRx, state );
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,292 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      sx1278-board.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     Target board SX127x driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
#include "lora-radio-rtos-config.h"
 | 
			
		||||
#include "lora-radio.h"
 | 
			
		||||
#include "sx127x\sx127x.h"
 | 
			
		||||
#include "sx127x-board.h"
 | 
			
		||||
 | 
			
		||||
#define LOG_TAG "LoRa.Board.LSD4RF-2F717N30(SX1278)"
 | 
			
		||||
#define LOG_LEVEL  LOG_LVL_DBG 
 | 
			
		||||
#include "lora-radio-debug.h"
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Flag used to set the RF switch control pins in low power mode when the radio is not active.
 | 
			
		||||
 */
 | 
			
		||||
static bool RadioIsActive = false;
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 0 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio0IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 1 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio1IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 2 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio2IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 3 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio3IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 4 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio4IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 5 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio5IrqEvent( void *args );
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Debug GPIO pins objects
 | 
			
		||||
 */
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
Gpio_t DbgPinTx;
 | 
			
		||||
Gpio_t DbgPinRx;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#if ( RT_VER_NUM <= 0x40004 )
 | 
			
		||||
int stm32_pin_get(char *pin_name)
 | 
			
		||||
{
 | 
			
		||||
    /* eg: pin_name : "PA.4"  ( GPIOA, GPIO_PIN_4 )--> drv_gpio.c pin */
 | 
			
		||||
    char pin_index = strtol(&pin_name[3],0,10);
 | 
			
		||||
    
 | 
			
		||||
    if(pin_name[1] < 'A' || pin_name[1] > 'Z')
 | 
			
		||||
    {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return (16 * (pin_name[1]-'A') + pin_index);
 | 
			
		||||
}
 | 
			
		||||
#endif 
 | 
			
		||||
#endif /* LORA_RADIO_GPIO_SETUP_BY_PIN_NAME */
 | 
			
		||||
 | 
			
		||||
void SX127xIoInit( void )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_NSS_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO0_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO2_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#else
 | 
			
		||||
//    GpioInit( &SX127x.Spi.Nss, RADIO_NSS, PIN_OUTPUT, PIN_PUSH_PULL, PIN_PULL_UP, 1 );
 | 
			
		||||
 | 
			
		||||
//    GpioInit( &SX127x.DIO0, RADIO_DIO_0, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 0 );
 | 
			
		||||
//    GpioInit( &SX127x.DIO1, RADIO_DIO_1, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 0 );
 | 
			
		||||
//    GpioInit( &SX127x.DIO2, RADIO_DIO_2, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 0 );
 | 
			
		||||
//    GpioInit( &SX127x.DIO3, RADIO_DIO_3, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 0 );
 | 
			
		||||
//    GpioInit( &SX127x.DIO4, RADIO_DIO_4, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 0 );
 | 
			
		||||
//    GpioInit( &SX127x.DIO5, RADIO_DIO_5, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, 0 );
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xIoIrqInit( DioIrqHandler **irqHandlers )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
    
 | 
			
		||||
#ifdef LORA_RADIO_DIO0_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO0_PIN, PIN_IRQ_MODE_RISING,SX127xOnDio0IrqEvent,(void*)"rf-dio0");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO0_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DIO1_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO1_PIN, PIN_IRQ_MODE_RISING,SX127xOnDio1IrqEvent,(void*)"rf-dio1");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO1_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
#endif
 | 
			
		||||
    
 | 
			
		||||
#ifdef LORA_RADIO_DIO2_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO2_PIN, PIN_IRQ_MODE_RISING,SX127xOnDio2IrqEvent,(void*)"rf-dio2");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO2_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
#endif
 | 
			
		||||
    
 | 
			
		||||
#else
 | 
			
		||||
    
 | 
			
		||||
#ifdef LORA_RADIO_DIO0_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO0_PIN, PIN_IRQ_MODE_RISING,irqHandlers[0],(void*)"rf-dio0");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO0_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
#endif
 | 
			
		||||
    
 | 
			
		||||
#ifdef LORA_RADIO_DIO1_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO1_PIN, PIN_IRQ_MODE_RISING,irqHandlers[1],(void*)"rf-dio1");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO1_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DIO2_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO2_PIN, PIN_IRQ_MODE_RISING,irqHandlers[2],(void*)"rf-dio2");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO2_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
#endif 
 | 
			
		||||
//    GpioSetInterrupt( &SX127x.DIO0, IRQ_RISING_EDGE, IRQ_HIGH_PRIORITY, irqHandlers[0] );
 | 
			
		||||
//    GpioSetInterrupt( &SX127x.DIO1, IRQ_RISING_EDGE, IRQ_HIGH_PRIORITY, irqHandlers[1] );
 | 
			
		||||
//    GpioSetInterrupt( &SX127x.DIO2, IRQ_RISING_EDGE, IRQ_HIGH_PRIORITY, irqHandlers[2] );
 | 
			
		||||
//    GpioSetInterrupt( &SX127x.DIO3, IRQ_RISING_EDGE, IRQ_HIGH_PRIORITY, irqHandlers[3] );
 | 
			
		||||
//    GpioSetInterrupt( &SX127x.DIO4, IRQ_RISING_EDGE, IRQ_HIGH_PRIORITY, irqHandlers[4] );
 | 
			
		||||
//    GpioSetInterrupt( &SX127x.DIO5, IRQ_RISING_EDGE, IRQ_HIGH_PRIORITY, irqHandlers[5] );  
 | 
			
		||||
#endif /*end of USING_LORA_RADIO_DRIVER_RTOS_SUPPORT */     
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xIoDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
//    rt_pin_mode(LORA_RADIO_DIO0_PIN, PIN_MODE_INPUT);
 | 
			
		||||
//    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT);
 | 
			
		||||
//    rt_pin_mode(LORA_RADIO_DIO2_PIN, PIN_MODE_INPUT);
 | 
			
		||||
#else
 | 
			
		||||
//    GpioInit( &SX127x.Spi.Nss, RADIO_NSS, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 1 );
 | 
			
		||||
 | 
			
		||||
//    GpioInit( &SX127x.DIO0, RADIO_DIO_0, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
//    GpioInit( &SX127x.DIO1, RADIO_DIO_1, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
//    GpioInit( &SX127x.DIO2, RADIO_DIO_2, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
//    GpioInit( &SX127x.DIO3, RADIO_DIO_3, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
//    GpioInit( &SX127x.DIO4, RADIO_DIO_4, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
//    GpioInit( &SX127x.DIO5, RADIO_DIO_5, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
#endif /*end of USING_LORA_RADIO_DRIVER_RTOS_SUPPORT */
 | 
			
		||||
    SX127xAntSwDeInit();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xIoDbgInit( void )
 | 
			
		||||
{
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
    GpioInit( &DbgPinTx, RADIO_DBG_PIN_TX, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
    GpioInit( &DbgPinRx, RADIO_DBG_PIN_RX, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xReset( void )
 | 
			
		||||
{
 | 
			
		||||
    // Set RESET pin to 0
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RESET_PIN, PIN_LOW);
 | 
			
		||||
    
 | 
			
		||||
    // Wait 1 ms
 | 
			
		||||
    SX127X_DELAY_MS( 1 );
 | 
			
		||||
 | 
			
		||||
    // Configure RESET as input
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_INPUT);
 | 
			
		||||
 | 
			
		||||
    // Wait 6 ms
 | 
			
		||||
    SX127X_DELAY_MS( 6 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xSetAntSwLowPower( bool status )
 | 
			
		||||
{
 | 
			
		||||
    if( RadioIsActive != status )
 | 
			
		||||
    {
 | 
			
		||||
        RadioIsActive = status;
 | 
			
		||||
 | 
			
		||||
        if( status == false )
 | 
			
		||||
        {
 | 
			
		||||
            SX127xAntSwInit( );
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            SX127xAntSwDeInit( );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xAntSwInit( void )
 | 
			
		||||
{
 | 
			
		||||
////    GpioInit( &AntSwitchRx, RADIO_ANT_SWITCH_RX, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
////    GpioInit( &AntSwitchTxBoost, RADIO_ANT_SWITCH_TX_BOOST, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
////    GpioInit( &AntSwitchTxRfo, RADIO_ANT_SWITCH_TX_RFO, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
 | 
			
		||||
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RFSW1_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RFSW2_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_LOW);
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_LOW);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xAntSwDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
////    GpioInit( &AntSwitchRx, RADIO_ANT_SWITCH_RX, PIN_ANALOGIC, PIN_OPEN_DRAIN, PIN_NO_PULL, 0 );
 | 
			
		||||
////    GpioInit( &AntSwitchTxBoost, RADIO_ANT_SWITCH_TX_BOOST, PIN_ANALOGIC, PIN_OPEN_DRAIN, PIN_NO_PULL, 0 );
 | 
			
		||||
////    GpioInit( &AntSwitchTxRfo, RADIO_ANT_SWITCH_TX_RFO, PIN_ANALOGIC, PIN_OPEN_DRAIN, PIN_NO_PULL, 0 );
 | 
			
		||||
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_LOW);
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_LOW);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* TX/RX ANT Swich */
 | 
			
		||||
void SX127xSetAntSw( uint8_t opMode )
 | 
			
		||||
{
 | 
			
		||||
    uint8_t paConfig =  SX127xRead( REG_PACONFIG );
 | 
			
		||||
    switch( opMode )
 | 
			
		||||
    {
 | 
			
		||||
    case RFLR_OPMODE_TRANSMITTER:
 | 
			
		||||
        if( ( paConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
 | 
			
		||||
        {
 | 
			
		||||
            ///GpioWrite( &AntSwitchTxBoost, 1 );
 | 
			
		||||
            rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_HIGH);
 | 
			
		||||
            rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_LOW);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case RFLR_OPMODE_RECEIVER:
 | 
			
		||||
    case RFLR_OPMODE_RECEIVER_SINGLE:
 | 
			
		||||
    case RFLR_OPMODE_CAD:
 | 
			
		||||
    default:
 | 
			
		||||
        ////GpioWrite( &AntSwitchRx, 1 );
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW1_PIN, PIN_LOW);
 | 
			
		||||
        rt_pin_write(LORA_RADIO_RFSW2_PIN, PIN_HIGH);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
void SX127xDbgPinTxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinTx, state );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xDbgPinRxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinRx, state );
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
uint8_t SX127xGetPaSelect( int8_t power )
 | 
			
		||||
{
 | 
			
		||||
    return RF_PACONFIG_PASELECT_PABOOST;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool SX127xCheckRfFrequency( uint32_t frequency )
 | 
			
		||||
{
 | 
			
		||||
    // Implement check. Currently all frequencies are supported,todo depend on board
 | 
			
		||||
    return true; 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,225 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      sx1278-board.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     Target board SX127x driver implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright Revised BSD License, see section \ref LICENSE.
 | 
			
		||||
 *
 | 
			
		||||
 * \code
 | 
			
		||||
 *                ______                              _
 | 
			
		||||
 *               / _____)             _              | |
 | 
			
		||||
 *              ( (____  _____ ____ _| |_ _____  ____| |__
 | 
			
		||||
 *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 | 
			
		||||
 *               _____) ) ____| | | || |_| ____( (___| | | |
 | 
			
		||||
 *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
 | 
			
		||||
 *              (C)2013-2017 Semtech
 | 
			
		||||
 *
 | 
			
		||||
 * \endcode
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Miguel Luis ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Gregory Cristian ( Semtech )
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
#include "lora-radio-rtos-config.h"
 | 
			
		||||
#include "lora-radio.h"
 | 
			
		||||
#include "sx127x.h"
 | 
			
		||||
#include "sx127x-board.h"
 | 
			
		||||
 | 
			
		||||
#define LOG_TAG "LoRa.Board.Ra-01(SX1278)"
 | 
			
		||||
#define LOG_LEVEL  LOG_LVL_DBG 
 | 
			
		||||
#include "lora-radio-debug.h"
 | 
			
		||||
/*!
 | 
			
		||||
 * Flag used to set the RF switch control pins in low power mode when the radio is not active.
 | 
			
		||||
 */
 | 
			
		||||
static bool RadioIsActive = false;
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 0 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio0IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 1 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio1IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 2 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio2IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 3 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio3IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 4 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio4IrqEvent( void *args );
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * \brief DIO 5 IRQ callback
 | 
			
		||||
 */
 | 
			
		||||
void SX127xOnDio5IrqEvent( void *args );
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
 * Debug GPIO pins objects
 | 
			
		||||
 */
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
Gpio_t DbgPinTx;
 | 
			
		||||
Gpio_t DbgPinRx;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#if ( RT_VER_NUM <= 0x40004 )
 | 
			
		||||
int stm32_pin_get(char *pin_name)
 | 
			
		||||
{
 | 
			
		||||
    /* eg: pin_name : "PA.4"  ( GPIOA, GPIO_PIN_4 )--> drv_gpio.c pin */
 | 
			
		||||
    char pin_index = strtol(&pin_name[3],0,10);
 | 
			
		||||
    
 | 
			
		||||
    if(pin_name[1] < 'A' || pin_name[1] > 'Z')
 | 
			
		||||
    {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return (16 * (pin_name[1]-'A') + pin_index);
 | 
			
		||||
}
 | 
			
		||||
#endif 
 | 
			
		||||
#endif /* LORA_RADIO_GPIO_SETUP_BY_PIN_NAME */
 | 
			
		||||
 | 
			
		||||
void SX127xIoInit( void )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
    // RT-Thread
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_NSS_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO0_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#ifdef LORA_RADIO_DIO1_PIN     
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO1_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#endif    
 | 
			
		||||
#ifdef LORA_RADIO_DIO2_PIN      
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_DIO2_PIN, PIN_MODE_INPUT_PULLDOWN);
 | 
			
		||||
#endif    
 | 
			
		||||
#else
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xIoIrqInit( DioIrqHandler **irqHandlers )
 | 
			
		||||
{
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_ON_RTOS_RT_THREAD
 | 
			
		||||
    #ifdef LORA_RADIO_DIO0_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO0_PIN, PIN_IRQ_MODE_RISING,SX127xOnDio0IrqEvent,(void*)"rf-dio0");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO0_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifdef LORA_RADIO_DIO1_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO1_PIN, PIN_IRQ_MODE_RISING,SX127xOnDio1IrqEvent,(void*)"rf-dio1");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO1_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifdef LORA_RADIO_DIO2_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO2_PIN, PIN_IRQ_MODE_RISING,SX127xOnDio2IrqEvent,(void*)"rf-dio2");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO2_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
    #endif
 | 
			
		||||
#else
 | 
			
		||||
    #ifdef LORA_RADIO_DIO0_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO0_PIN, PIN_IRQ_MODE_RISING,irqHandlers[0],(void*)"rf-dio0");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO0_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifdef LORA_RADIO_DIO1_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO1_PIN, PIN_IRQ_MODE_RISING,irqHandlers[1],(void*)"rf-dio1");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO1_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
    #endif
 | 
			
		||||
    #ifdef LORA_RADIO_DIO2_PIN
 | 
			
		||||
    rt_pin_attach_irq(LORA_RADIO_DIO2_PIN, PIN_IRQ_MODE_RISING,irqHandlers[2],(void*)"rf-dio2");
 | 
			
		||||
    rt_pin_irq_enable(LORA_RADIO_DIO2_PIN, PIN_IRQ_ENABLE);    
 | 
			
		||||
    #endif
 | 
			
		||||
#endif    
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xIoDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xIoDbgInit( void )
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void SX127xReset( void )
 | 
			
		||||
{
 | 
			
		||||
    // Set RESET pin to 0
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_OUTPUT);
 | 
			
		||||
    rt_pin_write(LORA_RADIO_RESET_PIN, PIN_LOW);
 | 
			
		||||
    
 | 
			
		||||
    // Wait 1 ms
 | 
			
		||||
    SX127X_DELAY_MS( 1 );
 | 
			
		||||
 | 
			
		||||
    // Configure RESET as input
 | 
			
		||||
    rt_pin_mode(LORA_RADIO_RESET_PIN, PIN_MODE_INPUT);
 | 
			
		||||
 | 
			
		||||
    // Wait 6 ms
 | 
			
		||||
    SX127X_DELAY_MS( 6 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xSetAntSwLowPower( bool status )
 | 
			
		||||
{
 | 
			
		||||
    if( RadioIsActive != status )
 | 
			
		||||
    {
 | 
			
		||||
        RadioIsActive = status;
 | 
			
		||||
 | 
			
		||||
        if( status == false )
 | 
			
		||||
        {
 | 
			
		||||
            SX127xAntSwInit( );
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            SX127xAntSwDeInit( );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xAntSwInit( void )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xAntSwDeInit( void )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* TX and RX ANT switch */
 | 
			
		||||
 | 
			
		||||
void SX127xSetAntSw( uint8_t opMode )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if defined( USE_RADIO_DEBUG )
 | 
			
		||||
void SX127xDbgPinTxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinTx, state );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX127xDbgPinRxWrite( uint8_t state )
 | 
			
		||||
{
 | 
			
		||||
    GpioWrite( &DbgPinRx, state );
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
uint8_t SX127xGetPaSelect( int8_t power )
 | 
			
		||||
{
 | 
			
		||||
    return RF_PACONFIG_PASELECT_PABOOST;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool SX127xCheckRfFrequency( uint32_t frequency )
 | 
			
		||||
{
 | 
			
		||||
    // Implement check. Currently all frequencies are supported,todo depend on board
 | 
			
		||||
    return true; 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,100 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-spi-board.c
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     spi peripheral initlize,it depend on mcu platform.
 | 
			
		||||
 *
 | 
			
		||||
 * SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
#include "lora-radio-rtos-config.h"
 | 
			
		||||
#ifdef LORA_RADIO_DRIVER_USING_LORA_CHIP_SX126X
 | 
			
		||||
#include "sx126x-board.h"
 | 
			
		||||
#elif defined LORA_RADIO_DRIVER_USING_LORA_CHIP_SX127X
 | 
			
		||||
#include "sx127x-board.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define LOG_TAG "LoRa.STM32.SPI"
 | 
			
		||||
#define LOG_LEVEL  LOG_LVL_DBG 
 | 
			
		||||
#include "lora-radio-debug.h"
 | 
			
		||||
 | 
			
		||||
/* for get GPIO fort,eg GPIOA */
 | 
			
		||||
#ifndef GET_GPIO_PORT
 | 
			
		||||
#define GET_GPIO_PORT(pin) (GPIO_TypeDef *)( GPIOA_BASE + (uint32_t) ( pin >> 4 ) * 0x0400UL ) 
 | 
			
		||||
#endif
 | 
			
		||||
#ifndef GET_GPIO_PIN
 | 
			
		||||
#define GET_GPIO_PIN(pin) (rt_uint16_t)( 1 << ( pin & 0x0F ) ) // for get GPIO_PIN, eg: GPIO_PIN_6
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef LORA_RADIO_GPIO_SETUP_BY_PIN_NAME
 | 
			
		||||
#if ( RT_VER_NUM <= 0x40002 )
 | 
			
		||||
extern int stm32_pin_get(char *pin_name);
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
struct rt_spi_device *lora_radio_spi_init(const char *bus_name, const char *lora_device_name, rt_uint8_t param)
 | 
			
		||||
{
 | 
			
		||||
    rt_err_t res;
 | 
			
		||||
    struct rt_spi_device *lora_radio_spi_device;
 | 
			
		||||
    
 | 
			
		||||
    RT_ASSERT(bus_name);
 | 
			
		||||
    
 | 
			
		||||
    {
 | 
			
		||||
        res = rt_hw_spi_device_attach( bus_name, lora_device_name, LORA_RADIO_NSS_PIN);
 | 
			
		||||
        
 | 
			
		||||
        if (res != RT_EOK)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"rt_spi_bus_attach_device failed!\r\n");
 | 
			
		||||
            return RT_NULL;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        lora_radio_spi_device = (struct rt_spi_device *)rt_device_find(lora_device_name);
 | 
			
		||||
        if (!lora_radio_spi_device)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"spi sample run failed! cant't find %s device!\n", lora_device_name);
 | 
			
		||||
            return RT_NULL;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"find %s device!\n", lora_device_name);
 | 
			
		||||
    
 | 
			
		||||
    /* config spi */
 | 
			
		||||
    {
 | 
			
		||||
        struct rt_spi_configuration cfg;
 | 
			
		||||
        cfg.data_width = 8;
 | 
			
		||||
        cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0. */
 | 
			
		||||
        cfg.max_hz = 8 * 1000000;             /* max 10M */
 | 
			
		||||
        
 | 
			
		||||
        res = rt_spi_configure(lora_radio_spi_device, &cfg);
 | 
			
		||||
        
 | 
			
		||||
        if (res != RT_EOK)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"rt_spi_configure failed!\r\n");
 | 
			
		||||
        }
 | 
			
		||||
        res = rt_spi_take_bus(lora_radio_spi_device);
 | 
			
		||||
        if (res != RT_EOK)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"rt_spi_take_bus failed!\r\n");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        res = rt_spi_release_bus(lora_radio_spi_device);
 | 
			
		||||
        
 | 
			
		||||
        if(res != RT_EOK)
 | 
			
		||||
        {
 | 
			
		||||
            LORA_RADIO_DEBUG_LOG(LR_DBG_SPI, LOG_LEVEL,"rt_spi_release_bus failed!\r\n");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
	
 | 
			
		||||
    return lora_radio_spi_device;
 | 
			
		||||
} 
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This function releases memory
 | 
			
		||||
 *
 | 
			
		||||
 * @param dev the pointer of device driver structure
 | 
			
		||||
 */
 | 
			
		||||
void lora_radio_spi_deinit(struct rt_spi_device *dev)
 | 
			
		||||
{
 | 
			
		||||
    RT_ASSERT(dev);
 | 
			
		||||
    rt_spi_release_bus(dev);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_DIR := lora-radio-tester
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,14 @@
 | 
			
		|||
from building import *
 | 
			
		||||
 | 
			
		||||
src   = []
 | 
			
		||||
cwd   = GetCurrentDir()
 | 
			
		||||
include_path = [cwd]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if GetDepend('LORA_RADIO_DRIVER_USING_LORA_RADIO_TESTER'):
 | 
			
		||||
    src += Glob('lora-radio-tester/lora-radio-tester.c')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
group = DefineGroup('lora-radio-driver/sample', src, depend = ['PKG_USING_LORA_RADIO_DRIVER'], CPPPATH = include_path)
 | 
			
		||||
 | 
			
		||||
Return('group')
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_FILES := lora-radio-tester.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -0,0 +1,158 @@
 | 
			
		|||
/*!
 | 
			
		||||
 * \file      lora-radio-tester.h
 | 
			
		||||
 *
 | 
			
		||||
 * \brief     lora radio test implementation
 | 
			
		||||
 *
 | 
			
		||||
 * \copyright SPDX-License-Identifier: Apache-2.0
 | 
			
		||||
 * 
 | 
			
		||||
 * \author    Forest-Rain
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef __LORA_RADIO_TESTER_H__
 | 
			
		||||
#define __LORA_RADIO_TESTER_H__
 | 
			
		||||
 | 
			
		||||
#include <lora-radio-rtos-config.h>
 | 
			
		||||
 | 
			
		||||
/* application debug */
 | 
			
		||||
#ifndef LR_DBG_SHELL
 | 
			
		||||
#define LR_DBG_SHELL                          1
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define PHY_REGION_EU433
 | 
			
		||||
 | 
			
		||||
#if defined( PHY_REGION_AS923 )
 | 
			
		||||
 | 
			
		||||
#define RF_FREQUENCY                                923000000 // Hz
 | 
			
		||||
 | 
			
		||||
#elif defined( PHY_REGION_AU915 )
 | 
			
		||||
 | 
			
		||||
#define RF_FREQUENCY                                915000000 // Hz
 | 
			
		||||
 | 
			
		||||
#elif defined( PHY_REGION_CN470 ) || defined ( PHY_REGION_CN470S )
 | 
			
		||||
 | 
			
		||||
#define RF_FREQUENCY                                470300000 // Hz
 | 
			
		||||
 | 
			
		||||
#elif defined( PHY_REGION_CN779 )
 | 
			
		||||
 | 
			
		||||
#define RF_FREQUENCY                                779000000 // Hz
 | 
			
		||||
 | 
			
		||||
#elif defined( PHY_REGION_EU433 )
 | 
			
		||||
 | 
			
		||||
#define RF_FREQUENCY                                433000000 // Hz
 | 
			
		||||
 | 
			
		||||
#elif defined( PHY_REGION_EU868 )
 | 
			
		||||
 | 
			
		||||
#define RF_FREQUENCY                                868000000 // Hz
 | 
			
		||||
 | 
			
		||||
#elif defined( PHY_REGION_KR920 )
 | 
			
		||||
 | 
			
		||||
#define RF_FREQUENCY                                920000000 // Hz
 | 
			
		||||
 | 
			
		||||
#elif defined( PHY_REGION_IN865 )
 | 
			
		||||
 | 
			
		||||
#define RF_FREQUENCY                                865000000 // Hz
 | 
			
		||||
 | 
			
		||||
#elif defined( PHY_REGION_US915 )
 | 
			
		||||
 | 
			
		||||
#define RF_FREQUENCY                                915000000 // Hz
 | 
			
		||||
 | 
			
		||||
#elif defined( PHY_REGION_RU864 )
 | 
			
		||||
 | 
			
		||||
#define RF_FREQUENCY                                864000000 // Hz
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
    #error "Please define a frequency band in the compiler options."
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define TX_RX_FREQUENCE_OFFSET                      0   // 0           TX = RX
 | 
			
		||||
                                                        // 1800000     RX = TX+1.8M
 | 
			
		||||
#define TX_OUTPUT_POWER                             20//14    // dBm
 | 
			
		||||
 | 
			
		||||
#define LORA_BANDWIDTH                              2         // [0: 125 kHz,
 | 
			
		||||
                                                              //  1: 250 kHz,
 | 
			
		||||
                                                              //  2: 500 kHz,
 | 
			
		||||
                                                              //  3: Reserved]
 | 
			
		||||
#define LORA_SPREADING_FACTOR                       12        // [SF7..SF12]
 | 
			
		||||
#define LORA_CODINGRATE                             2         // [1: 4/5,
 | 
			
		||||
                                                              //  2: 4/6,
 | 
			
		||||
                                                              //  3: 4/7,
 | 
			
		||||
                                                              //  4: 4/8]
 | 
			
		||||
#define LORA_PREAMBLE_LENGTH                        8         // Same for Tx and Rx
 | 
			
		||||
#define LORA_SYMBOL_TIMEOUT                         0         // Symbols
 | 
			
		||||
#define LORA_FIX_LENGTH_PAYLOAD_ON_DISABLE          false
 | 
			
		||||
#define LORA_IQ_INVERSION_ON_DISABLE                false
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define FSK_FDEV                                    25000     // Hz
 | 
			
		||||
#define FSK_DATARATE                                50000     // bps
 | 
			
		||||
 | 
			
		||||
#if defined( LORA_RADIO_DRIVER_USING_LORA_CHIP_SX127X )
 | 
			
		||||
 | 
			
		||||
#define FSK_BANDWIDTH                               50000     // Hz >> SSB in sx127x
 | 
			
		||||
#define FSK_AFC_BANDWIDTH                           83333     // Hz
 | 
			
		||||
 | 
			
		||||
#elif defined( LORA_RADIO_DRIVER_USING_LORA_CHIP_SX126X) || defined( LORA_RADIO_DRIVER_USING_LORA_CHIP_LLCC68 )
 | 
			
		||||
 | 
			
		||||
#define FSK_BANDWIDTH                               100000    // Hz >> DSB in sx126x 
 | 
			
		||||
#define FSK_AFC_BANDWIDTH                           166666    // Hz >> Unused in sx126x
 | 
			
		||||
 | 
			
		||||
#elif defined( LORA_RADIO_DRIVER_USING_LORA_SOC_STM32WL )
 | 
			
		||||
 | 
			
		||||
#define FSK_BANDWIDTH                               100000    // Hz >> DSB in sx126x
 | 
			
		||||
#define FSK_AFC_BANDWIDTH                           166666    // Hz >> Unused in sx126x
 | 
			
		||||
#elif defined( LORA_RADIO_DRIVER_USING_LORA_CHIP_SX128X )
 | 
			
		||||
 | 
			
		||||
#define FSK_BANDWIDTH                               100000    // Hz >> DSB in sx126x
 | 
			
		||||
#define FSK_AFC_BANDWIDTH                           166666    // Hz >> Unused in sx126x
 | 
			
		||||
#else
 | 
			
		||||
    #error "Please define a lora-shield in the compiler options."
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define FSK_PREAMBLE_LENGTH                         5         // Same for Tx and Rx
 | 
			
		||||
#define FSK_FIX_LENGTH_PAYLOAD_ON                   false
 | 
			
		||||
 | 
			
		||||
#define TX_TIMEOUT_VALUE                            1000
 | 
			
		||||
#define RX_TIMEOUT_VALUE                            1000
 | 
			
		||||
#define BUFFER_SIZE                                 256 // Define the payload size here
 | 
			
		||||
#define MIN_TETS_APP_DATA_SIZE                      17 // for PING protocol
 | 
			
		||||
 | 
			
		||||
#define LORA_MASTER_DEVADDR 0x11223344
 | 
			
		||||
#define LORA_SLAVER_DEVADDR 0x01020304
 | 
			
		||||
#define MAC_HEADER_OVERHEAD 13
 | 
			
		||||
 | 
			
		||||
// Ping pong event
 | 
			
		||||
#define EV_RADIO_INIT            0x0001
 | 
			
		||||
#define EV_RADIO_TX_START        0x0002
 | 
			
		||||
#define EV_RADIO_TX_DONE         0x0004
 | 
			
		||||
#define EV_RADIO_TX_TIMEOUT      0x0008
 | 
			
		||||
#define EV_RADIO_RX_DONE         0x0010
 | 
			
		||||
#define EV_RADIO_RX_TIMEOUT      0x0020
 | 
			
		||||
#define EV_RADIO_RX_ERROR        0x0040
 | 
			
		||||
#define EV_RADIO_ALL             (EV_RADIO_INIT | EV_RADIO_TX_START | EV_RADIO_TX_DONE | EV_RADIO_TX_TIMEOUT | EV_RADIO_RX_DONE | EV_RADIO_RX_TIMEOUT | EV_RADIO_RX_ERROR)
 | 
			
		||||
 | 
			
		||||
typedef struct 
 | 
			
		||||
{
 | 
			
		||||
    RadioModems_t modem; // LoRa Modem \ FSK modem
 | 
			
		||||
    uint32_t tx_frequency;
 | 
			
		||||
    uint32_t rx_frequency;
 | 
			
		||||
    int32_t trx_frequency_offset;
 | 
			
		||||
    int8_t txpower;
 | 
			
		||||
    
 | 
			
		||||
    // LoRa
 | 
			
		||||
    uint8_t sf;    // spreadfactor
 | 
			
		||||
    uint8_t bw;    // bandwidth
 | 
			
		||||
    uint8_t cr;    // coderate
 | 
			
		||||
    uint8_t iq_inversion;
 | 
			
		||||
    bool public_network;
 | 
			
		||||
 | 
			
		||||
    // FSK
 | 
			
		||||
    uint32_t fdev;
 | 
			
		||||
    uint32_t datarate;
 | 
			
		||||
    uint32_t fsk_bandwidth;
 | 
			
		||||
    uint32_t fsk_afc_bandwidth;
 | 
			
		||||
    uint16_t preamble_len;
 | 
			
		||||
 | 
			
		||||
}lora_radio_test_t;
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
/*
 | 
			
		||||
* 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 lorawan_gateway_update.c
 | 
			
		||||
* @brief support lorawan-gateway update function
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-11-29
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/*************************************************
 | 
			
		||||
File name: lorawan_gateway_update.c
 | 
			
		||||
Description: support lorawan-gateway update function
 | 
			
		||||
Others: 
 | 
			
		||||
History: 
 | 
			
		||||
1. Date: 2023-11-29
 | 
			
		||||
Author: AIIT XUOS Lab
 | 
			
		||||
Modification: 
 | 
			
		||||
1、first version.
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -20,13 +20,12 @@
 | 
			
		|||
 | 
			
		||||
#include <connect_hwtimer.h>
 | 
			
		||||
 | 
			
		||||
#define TMR0_CMP_VAL 1000
 | 
			
		||||
#define TMR0_CMP_VAL                   (100000000U / 1024U / 2 - 1U)  
 | 
			
		||||
#define TMR0x                          ((CM_TMR0_TypeDef *)CM_TMR0_1_BASE)
 | 
			
		||||
#define TMR0_CH_x                      (TMR0_CH_A)
 | 
			
		||||
#define INTSEL_REG                     ((uint32_t)(&CM_INTC->SEL0))
 | 
			
		||||
#define TIMER0_IRQn                    (18)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void (*callback_function)(void *) ;
 | 
			
		||||
 | 
			
		||||
static void Timer0Callback(int vector, void *param)
 | 
			
		||||
| 
						 | 
				
			
			@ -47,10 +46,10 @@ static uint32 HwtimerOpen(void *dev)
 | 
			
		|||
 | 
			
		||||
    /* TIMER0 basetimer function initialize */
 | 
			
		||||
    (void)TMR0_StructInit(&stcTmr0Init);
 | 
			
		||||
    stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV128;        /* Config clock division */
 | 
			
		||||
    stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_INTERN_CLK;          /* Chose clock source */
 | 
			
		||||
    stcTmr0Init.u32Func = TMR0_FUNC_CMP;            /* Timer0 compare mode */
 | 
			
		||||
    stcTmr0Init.u16CompareValue = TMR0_CMP_VAL;             /* Set compare register data */
 | 
			
		||||
    stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV1024;         /* Config clock division */
 | 
			
		||||
    stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_INTERN_CLK; /* Chose clock source */
 | 
			
		||||
    stcTmr0Init.u32Func = TMR0_FUNC_CMP;               /* Timer0 compare mode */
 | 
			
		||||
    stcTmr0Init.u16CompareValue = TMR0_CMP_VAL;        /* Set compare register data */
 | 
			
		||||
    (void)TMR0_Init(TMR0x, TMR0_CH_x, &stcTmr0Init);
 | 
			
		||||
 | 
			
		||||
    // DelayKTask(1);
 | 
			
		||||
| 
						 | 
				
			
			@ -98,7 +97,8 @@ static uint32 HwtimerDrvConfigure(void *drv, struct BusConfigureInfo *configure_
 | 
			
		|||
            break;
 | 
			
		||||
        case OPE_CFG:
 | 
			
		||||
            TMR0_ClearStatus(TMR0x, TMR0_FLAG_CMP_A);
 | 
			
		||||
            TMR0_SetCompareValue(TMR0x, TMR0_CH_x, *((int *)configure_info->private_data) );
 | 
			
		||||
            uint32_t cmp_value = *((uint32_t *)configure_info->private_data) * 100;//1ms 100, max 655ms 65535 
 | 
			
		||||
            TMR0_SetCompareValue(TMR0x, TMR0_CH_x, (uint16_t)cmp_value);
 | 
			
		||||
            /* Timer0 interrupt function Enable */
 | 
			
		||||
            TMR0_SetCountValue(TMR0x, TMR0_CH_x, 0x0000); 
 | 
			
		||||
            TMR0_Start(TMR0x, TMR0_CH_x);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -82,6 +82,10 @@ Modification:
 | 
			
		|||
#include <connect_ethernet.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef BSP_USING_EXTSDRAM
 | 
			
		||||
#include <connect_sdram.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
extern void entry(void);
 | 
			
		||||
extern int HwUsartInit();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -180,6 +184,9 @@ struct InitSequenceDesc _board_init[] = {
 | 
			
		|||
#ifdef BSP_USING_GPIO
 | 
			
		||||
    { "hw_pin", HwGpioInit },
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef BSP_USING_EXTSDRAM
 | 
			
		||||
    { "ext_sdram", ExtSdramInit},
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef BSP_USING_SDIO
 | 
			
		||||
    { "sdio", HwSdioInit },
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -49,6 +49,8 @@ MEMORY
 | 
			
		|||
    OTP_LOCK    (rx): ORIGIN = __OTP_LOCK_START, LENGTH = __OTP_LOCK_SIZE
 | 
			
		||||
    RAM        (rwx): ORIGIN = 0x1FFE0000, LENGTH = 512K
 | 
			
		||||
    RAMB       (rwx): ORIGIN = 0x200F0000, LENGTH = 4K
 | 
			
		||||
 | 
			
		||||
    SDRAM      (rwx): ORIGIN = 0x80000000, LENGTH = 8M
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ENTRY(Reset_Handler)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -25,6 +25,7 @@ menuconfig BSP_USING_ETHERNET
 | 
			
		|||
menuconfig BSP_USING_W5500
 | 
			
		||||
    bool "Using W5500 ethernet device"
 | 
			
		||||
    default n
 | 
			
		||||
    select BSP_USING_ETHERNET
 | 
			
		||||
    select BSP_USING_LWIP
 | 
			
		||||
    select BSP_USING_SPI
 | 
			
		||||
    select BSP_USING_SPI1
 | 
			
		||||
| 
						 | 
				
			
			@ -52,3 +53,10 @@ menuconfig BSP_USING_SPI
 | 
			
		|||
    if BSP_USING_SPI
 | 
			
		||||
        source "$BSP_DIR/third_party_driver/spi/Kconfig"
 | 
			
		||||
    endif
 | 
			
		||||
 | 
			
		||||
menuconfig BSP_USING_SDRAM
 | 
			
		||||
    bool "Using SDRAM IS42S16400J"
 | 
			
		||||
    default n
 | 
			
		||||
    if BSP_USING_SDRAM
 | 
			
		||||
        source "$BSP_DIR/third_party_driver/sdram/Kconfig"
 | 
			
		||||
    endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,4 +24,8 @@ ifeq ($(CONFIG_BSP_USING_SPI),y)
 | 
			
		|||
  SRC_DIR += spi
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
ifeq ($(CONFIG_BSP_USING_EXTSDRAM),y)
 | 
			
		||||
  SRC_DIR += sdram
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -48,4 +48,8 @@ ifeq ($(CONFIG_BSP_USING_CAN),y)
 | 
			
		|||
  SRC_FILES += hc32_ll_can.c
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
ifeq ($(CONFIG_BSP_USING_EXTSDRAM),y)
 | 
			
		||||
  SRC_FILES += hc32_ll_dmc.c
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -303,7 +303,7 @@ static int GpioPinIndex(uint16_t pin)
 | 
			
		|||
    int ret = 0;
 | 
			
		||||
    for (; ret <= MAX_PIN_INDEX; ret++) { // ret must be 16-bit
 | 
			
		||||
        if ((0x0001U << ret) & pin) {
 | 
			
		||||
            KPrintf("the int pin is %d\n", ret);
 | 
			
		||||
            // KPrintf("the int pin is %d\n", ret);
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,37 @@
 | 
			
		|||
/*
 | 
			
		||||
* 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_sdram.h
 | 
			
		||||
* @brief define xishutong-arm32-board sdram function and struct
 | 
			
		||||
* @version 2.0
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-02-09
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include <device.h>
 | 
			
		||||
#include <hc32_ll_fcg.h>
 | 
			
		||||
#include <hc32_ll_gpio.h>
 | 
			
		||||
#include <hc32_ll_dmc.h>
 | 
			
		||||
#include <hc32_ll_clk.h>
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
int ExtSdramInit(void);
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -75,6 +75,7 @@ Modification:
 | 
			
		|||
 | 
			
		||||
static stc_sd_handle_t gSdHandle;
 | 
			
		||||
static int sd_lock = -1;
 | 
			
		||||
static int is_mount_ok = 0;
 | 
			
		||||
 | 
			
		||||
static void SdCardConfig(void)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -213,11 +214,13 @@ static struct SdioDevDone dev_done = {
 | 
			
		|||
 */
 | 
			
		||||
static int MountSDCardFs(enum FilesystemType fs_type)
 | 
			
		||||
{
 | 
			
		||||
    if (MountFilesystem(SDIO_BUS_NAME, SDIO_DEVICE_NAME, SDIO_DRIVER_NAME, fs_type, "/") == 0)
 | 
			
		||||
        KPrintf("Sd card mount to '/'");
 | 
			
		||||
    else
 | 
			
		||||
        KPrintf("Sd card mount to '/' failed!");
 | 
			
		||||
 | 
			
		||||
    if (MountFilesystem(SDIO_BUS_NAME, SDIO_DEVICE_NAME, SDIO_DRIVER_NAME, fs_type, "/") == 0) {
 | 
			
		||||
        KPrintf("Sd card mount to '/'\n");
 | 
			
		||||
        is_mount_ok = 1;
 | 
			
		||||
    } else {
 | 
			
		||||
        KPrintf("Sd card mount to '/' failed!\n");
 | 
			
		||||
        is_mount_ok = 0;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -252,9 +255,17 @@ static void SdCardDetach(void)
 | 
			
		|||
 | 
			
		||||
#ifdef MOUNT_SDCARD_FS
 | 
			
		||||
    UnmountFileSystem("/");
 | 
			
		||||
    is_mount_ok = 0;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int GetSdMountStatus(void)
 | 
			
		||||
{
 | 
			
		||||
    if(!is_mount_ok) 
 | 
			
		||||
        KPrintf("SD card is not inserted or failed to mount, please check!\r\n");
 | 
			
		||||
    return is_mount_ok;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static uint8 SdCardReadCd(void)
 | 
			
		||||
{
 | 
			
		||||
    en_pin_state_t sd_cd_state = GPIO_ReadInputPins(SDIOC_CD_PORT, SDIOC_CD_PIN);
 | 
			
		||||
| 
						 | 
				
			
			@ -291,7 +302,7 @@ int MountSDCard()
 | 
			
		|||
{
 | 
			
		||||
    int sd_card_task = 0;
 | 
			
		||||
    sd_card_task = KTaskCreate("sd_card", SdCardTask, NONE,
 | 
			
		||||
        SD_CARD_STACK_SIZE, 8);
 | 
			
		||||
        SD_CARD_STACK_SIZE, 17);
 | 
			
		||||
    if (sd_card_task < 0) {
 | 
			
		||||
        KPrintf("sd_card_task create failed ...%s %d.\n", __FUNCTION__, __LINE__);
 | 
			
		||||
        return ERROR;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,12 @@
 | 
			
		|||
config BSP_USING_EXTSDRAM
 | 
			
		||||
    bool "config extern sdram"
 | 
			
		||||
    default y
 | 
			
		||||
    select MEM_EXTERN_SRAM
 | 
			
		||||
    if BSP_USING_EXTSDRAM
 | 
			
		||||
        config EXTSRAM_MAX_NUM
 | 
			
		||||
            int
 | 
			
		||||
            default 1
 | 
			
		||||
        config EXTSRAM_TEST
 | 
			
		||||
            bool "extern sdram write/read test"
 | 
			
		||||
            default n
 | 
			
		||||
    endif
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_FILES := connect_sdram.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,533 @@
 | 
			
		|||
/*
 | 
			
		||||
* 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_sdram.c
 | 
			
		||||
* @brief support xishutong-arm32 sdram function, using IS42S16400J
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-12-6
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include <connect_sdram.h>
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @defgroup IS42S16400J_Mode_Register_Field IS42S16400J Mode Register Field
 | 
			
		||||
 * @{
 | 
			
		||||
 */
 | 
			
		||||
/* IS42S16400J burst length definition */
 | 
			
		||||
#define IS42S16400J_MR_BURST_1BEAT                  (0UL)
 | 
			
		||||
#define IS42S16400J_MR_BURST_2BEAT                  (1UL)
 | 
			
		||||
#define IS42S16400J_MR_BURST_4BEAT                  (2UL)
 | 
			
		||||
#define IS42S16400J_MR_BURST_8BEAT                  (3UL)
 | 
			
		||||
#define IS42S16400J_MR_BURST_LEN_FULLPAGE           (7UL)
 | 
			
		||||
 | 
			
		||||
/* IS42S16400J burst type definition */
 | 
			
		||||
#define IS42S16400J_MR_BURST_TYPE_SEQUENTIAL        (0UL)
 | 
			
		||||
#define IS42S16400J_MR_BURST_TYPE_INTERLEAVED       (1UL << 3)
 | 
			
		||||
 | 
			
		||||
/* IS42S16400J CAS latency definition */
 | 
			
		||||
#define IS42S16400J_MR_CAS_LATENCY_2                (2UL << 4)
 | 
			
		||||
#define IS42S16400J_MR_CAS_LATENCY_3                (3UL << 4)
 | 
			
		||||
 | 
			
		||||
/* IS42S16400J write burst mode definition */
 | 
			
		||||
#define IS42S16400J_MR_WRITEBURST_PROGRAMMED        (0UL)
 | 
			
		||||
#define IS42S16400J_MR_WRITEBURST_SINGLE            (1UL << 9)
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @defgroup IS42S16400J_Memory_Size IS42S16400J Memory Size
 | 
			
		||||
 * @{
 | 
			
		||||
 */
 | 
			
		||||
#define BSP_IS42S16400J_SIZE                        (8UL * 1024UL * 1024UL)     /* 8MBytes*/
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @defgroup DMC_Max_Timeout DMC Max Timeout
 | 
			
		||||
 * @{
 | 
			
		||||
 */
 | 
			
		||||
#define DMC_MAX_TIMEOUT                             (0x100000UL)
 | 
			
		||||
 | 
			
		||||
#ifdef EXTSRAM_TEST
 | 
			
		||||
/**
 | 
			
		||||
 * @defgroup SDRAM write/read byte test
 | 
			
		||||
 * @{
 | 
			
		||||
 */
 | 
			
		||||
#define DATA_BUF_LEN                                (0x8000UL)
 | 
			
		||||
static uint8_t m_au8ReadData[DATA_BUF_LEN];
 | 
			
		||||
static uint8_t m_au8WriteData[DATA_BUF_LEN];
 | 
			
		||||
static uint32_t m_u32ByteTestErrorCnt = 0UL;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @defgroup DMC_Interface_Pin DMC Interface Pin
 | 
			
		||||
 * @{
 | 
			
		||||
 */
 | 
			
		||||
#define BSP_IS42S16400J_CKE_PORT        (GPIO_PORT_C)   /* PC03 - EXMC_ALE */
 | 
			
		||||
#define BSP_IS42S16400J_CKE_PIN         (GPIO_PIN_03)
 | 
			
		||||
 | 
			
		||||
#define BSP_IS42S16400J_CLK_PORT        (GPIO_PORT_G)   /* PD03 - EXMC_CLK */
 | 
			
		||||
#define BSP_IS42S16400J_CLK_PIN         (GPIO_PIN_08)
 | 
			
		||||
 | 
			
		||||
#define BSP_IS42S16400J_DQM0_PORT       (GPIO_PORT_E)   /* PE00 - EXMC_CE4 */
 | 
			
		||||
#define BSP_IS42S16400J_DQM0_PIN        (GPIO_PIN_00)
 | 
			
		||||
#define BSP_IS42S16400J_DQM1_PORT       (GPIO_PORT_E)   /* PE01 - EXMC_CE5 */
 | 
			
		||||
#define BSP_IS42S16400J_DQM1_PIN        (GPIO_PIN_01)
 | 
			
		||||
 | 
			
		||||
#define BSP_IS42S16400J_BA0_PORT        (GPIO_PORT_D)   /* PD11 - EXMC_ADD16 */
 | 
			
		||||
#define BSP_IS42S16400J_BA0_PIN         (GPIO_PIN_11)
 | 
			
		||||
#define BSP_IS42S16400J_BA1_PORT        (GPIO_PORT_D)   /* PD12 - EXMC_ADD17 */
 | 
			
		||||
#define BSP_IS42S16400J_BA1_PIN         (GPIO_PIN_12)
 | 
			
		||||
 | 
			
		||||
#define BSP_IS42S16400J_CS1_PORT        (GPIO_PORT_G)   /* PG09 - EXMC_CE1 */
 | 
			
		||||
#define BSP_IS42S16400J_CS1_PIN         (GPIO_PIN_09)
 | 
			
		||||
 | 
			
		||||
#define BSP_IS42S16400J_RAS_PORT        (GPIO_PORT_F)   /* PF11 - EXMC_OE */
 | 
			
		||||
#define BSP_IS42S16400J_RAS_PIN         (GPIO_PIN_11)
 | 
			
		||||
 | 
			
		||||
#define BSP_IS42S16400J_CAS_PORT        (GPIO_PORT_G)   /* PG15 - EXMC_BAA */
 | 
			
		||||
#define BSP_IS42S16400J_CAS_PIN         (GPIO_PIN_15)
 | 
			
		||||
 | 
			
		||||
#define BSP_IS42S16400J_WE_PORT         (GPIO_PORT_C)   /* PC00 - EXMC_WE */
 | 
			
		||||
#define BSP_IS42S16400J_WE_PIN          (GPIO_PIN_00)
 | 
			
		||||
 | 
			
		||||
#define BSP_IS42S16400J_ADD0_PORT       (GPIO_PORT_F)   /* PF00 - EXMC_ADD0 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD0_PIN        (GPIO_PIN_00)
 | 
			
		||||
#define BSP_IS42S16400J_ADD1_PORT       (GPIO_PORT_F)   /* PF01 - EXMC_ADD1 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD1_PIN        (GPIO_PIN_01)
 | 
			
		||||
#define BSP_IS42S16400J_ADD2_PORT       (GPIO_PORT_F)   /* PF02 - EXMC_ADD2 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD2_PIN        (GPIO_PIN_02)
 | 
			
		||||
#define BSP_IS42S16400J_ADD3_PORT       (GPIO_PORT_F)   /* PF03 - EXMC_ADD3 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD3_PIN        (GPIO_PIN_03)
 | 
			
		||||
#define BSP_IS42S16400J_ADD4_PORT       (GPIO_PORT_F)   /* PF04 - EXMC_ADD4 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD4_PIN        (GPIO_PIN_04)
 | 
			
		||||
#define BSP_IS42S16400J_ADD5_PORT       (GPIO_PORT_F)   /* PF05 - EXMC_ADD5 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD5_PIN        (GPIO_PIN_05)
 | 
			
		||||
#define BSP_IS42S16400J_ADD6_PORT       (GPIO_PORT_F)   /* PF12 - EXMC_ADD6 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD6_PIN        (GPIO_PIN_12)
 | 
			
		||||
#define BSP_IS42S16400J_ADD7_PORT       (GPIO_PORT_F)   /* PF13 - EXMC_ADD7 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD7_PIN        (GPIO_PIN_13)
 | 
			
		||||
#define BSP_IS42S16400J_ADD8_PORT       (GPIO_PORT_F)   /* PF14 - EXMC_ADD8 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD8_PIN        (GPIO_PIN_14)
 | 
			
		||||
#define BSP_IS42S16400J_ADD9_PORT       (GPIO_PORT_F)   /* PF15 - EXMC_ADD9 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD9_PIN        (GPIO_PIN_15)
 | 
			
		||||
#define BSP_IS42S16400J_ADD10_PORT      (GPIO_PORT_G)   /* PG00 - EXMC_ADD10 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD10_PIN       (GPIO_PIN_00)
 | 
			
		||||
#define BSP_IS42S16400J_ADD11_PORT      (GPIO_PORT_G)   /* PG01 - EXMC_ADD11 */
 | 
			
		||||
#define BSP_IS42S16400J_ADD11_PIN       (GPIO_PIN_01)
 | 
			
		||||
 | 
			
		||||
#define BSP_IS42S16400J_DATA0_PORT      (GPIO_PORT_D)   /* PD14 - EXMC_DATA0 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA0_PIN       (GPIO_PIN_14)
 | 
			
		||||
#define BSP_IS42S16400J_DATA1_PORT      (GPIO_PORT_D)   /* PD15 - EXMC_DATA1 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA1_PIN       (GPIO_PIN_15)
 | 
			
		||||
#define BSP_IS42S16400J_DATA2_PORT      (GPIO_PORT_D)   /* PD00 - EXMC_DATA2 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA2_PIN       (GPIO_PIN_00)
 | 
			
		||||
#define BSP_IS42S16400J_DATA3_PORT      (GPIO_PORT_D)   /* PD01 - EXMC_DATA3 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA3_PIN       (GPIO_PIN_01)
 | 
			
		||||
#define BSP_IS42S16400J_DATA4_PORT      (GPIO_PORT_E)   /* PE07 - EXMC_DATA4 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA4_PIN       (GPIO_PIN_07)
 | 
			
		||||
#define BSP_IS42S16400J_DATA5_PORT      (GPIO_PORT_E)   /* PE08 - EXMC_DATA5 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA5_PIN       (GPIO_PIN_08)
 | 
			
		||||
#define BSP_IS42S16400J_DATA6_PORT      (GPIO_PORT_E)   /* PE09 - EXMC_DATA6 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA6_PIN       (GPIO_PIN_09)
 | 
			
		||||
#define BSP_IS42S16400J_DATA7_PORT      (GPIO_PORT_E)   /* PE10 - EXMC_DATA7 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA7_PIN       (GPIO_PIN_10)
 | 
			
		||||
#define BSP_IS42S16400J_DATA8_PORT      (GPIO_PORT_E)   /* PE11 - EXMC_DATA8 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA8_PIN       (GPIO_PIN_11)
 | 
			
		||||
#define BSP_IS42S16400J_DATA9_PORT      (GPIO_PORT_E)   /* PE12 - EXMC_DATA9 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA9_PIN       (GPIO_PIN_12)
 | 
			
		||||
#define BSP_IS42S16400J_DATA10_PORT     (GPIO_PORT_E)   /* PE13 - EXMC_DATA10 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA10_PIN      (GPIO_PIN_13)
 | 
			
		||||
#define BSP_IS42S16400J_DATA11_PORT     (GPIO_PORT_E)   /* PE14 - EXMC_DATA11 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA11_PIN      (GPIO_PIN_14)
 | 
			
		||||
#define BSP_IS42S16400J_DATA12_PORT     (GPIO_PORT_E)   /* PE15 - EXMC_DATA12 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA12_PIN      (GPIO_PIN_15)
 | 
			
		||||
#define BSP_IS42S16400J_DATA13_PORT     (GPIO_PORT_D)   /* PD08 - EXMC_DATA13 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA13_PIN      (GPIO_PIN_08)
 | 
			
		||||
#define BSP_IS42S16400J_DATA14_PORT     (GPIO_PORT_D)   /* PD09 - EXMC_DATA14 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA14_PIN      (GPIO_PIN_09)
 | 
			
		||||
#define BSP_IS42S16400J_DATA15_PORT     (GPIO_PORT_D)   /* PD10 - EXMC_DATA15 */
 | 
			
		||||
#define BSP_IS42S16400J_DATA15_PIN      (GPIO_PIN_10)
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief  Initialize DMC port.
 | 
			
		||||
 * @param  None
 | 
			
		||||
 * @retval None
 | 
			
		||||
 */
 | 
			
		||||
static void BSP_DMC_PortInit(void)
 | 
			
		||||
{
 | 
			
		||||
    stc_gpio_init_t stcGpioInit;
 | 
			
		||||
 | 
			
		||||
    /************************* Set pin drive capacity *************************/
 | 
			
		||||
    (void)GPIO_StructInit(&stcGpioInit);
 | 
			
		||||
    stcGpioInit.u16PinDrv = PIN_HIGH_DRV;
 | 
			
		||||
 | 
			
		||||
    /* DMC_CKE */
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_CKE_PORT, BSP_IS42S16400J_CKE_PIN, &stcGpioInit);
 | 
			
		||||
 | 
			
		||||
    /* DMC_CLK */
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_CLK_PORT, BSP_IS42S16400J_CLK_PIN, &stcGpioInit);
 | 
			
		||||
 | 
			
		||||
    /* DMC_LDQM && DMC_UDQM */
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DQM0_PORT, BSP_IS42S16400J_DQM0_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DQM1_PORT, BSP_IS42S16400J_DQM1_PIN, &stcGpioInit);
 | 
			
		||||
 | 
			
		||||
    /* DMC_BA[0:1] */
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_BA0_PORT, BSP_IS42S16400J_BA0_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_BA1_PORT, BSP_IS42S16400J_BA1_PIN, &stcGpioInit);
 | 
			
		||||
 | 
			
		||||
    /* DMC_CAS && DMC_RAS */
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_CAS_PORT, BSP_IS42S16400J_CAS_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_RAS_PORT, BSP_IS42S16400J_RAS_PIN, &stcGpioInit);
 | 
			
		||||
 | 
			
		||||
    /* DMC_WE */
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_WE_PORT, BSP_IS42S16400J_WE_PIN, &stcGpioInit);
 | 
			
		||||
 | 
			
		||||
    /* DMC_DATA[0:15] */
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA0_PORT, BSP_IS42S16400J_DATA0_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA1_PORT, BSP_IS42S16400J_DATA1_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA2_PORT, BSP_IS42S16400J_DATA2_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA3_PORT, BSP_IS42S16400J_DATA3_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA4_PORT, BSP_IS42S16400J_DATA4_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA5_PORT, BSP_IS42S16400J_DATA5_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA6_PORT, BSP_IS42S16400J_DATA6_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA7_PORT, BSP_IS42S16400J_DATA7_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA8_PORT, BSP_IS42S16400J_DATA8_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA9_PORT, BSP_IS42S16400J_DATA9_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA10_PORT, BSP_IS42S16400J_DATA10_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA11_PORT, BSP_IS42S16400J_DATA11_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA12_PORT, BSP_IS42S16400J_DATA12_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA13_PORT, BSP_IS42S16400J_DATA13_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA14_PORT, BSP_IS42S16400J_DATA14_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_DATA15_PORT, BSP_IS42S16400J_DATA15_PIN, &stcGpioInit);
 | 
			
		||||
 | 
			
		||||
    /* DMC_ADD[0:11]*/
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD0_PORT, BSP_IS42S16400J_ADD0_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD1_PORT, BSP_IS42S16400J_ADD1_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD2_PORT, BSP_IS42S16400J_ADD2_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD3_PORT, BSP_IS42S16400J_ADD3_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD4_PORT, BSP_IS42S16400J_ADD4_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD5_PORT, BSP_IS42S16400J_ADD5_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD6_PORT, BSP_IS42S16400J_ADD6_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD7_PORT, BSP_IS42S16400J_ADD7_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD8_PORT, BSP_IS42S16400J_ADD8_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD9_PORT, BSP_IS42S16400J_ADD9_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD10_PORT, BSP_IS42S16400J_ADD10_PIN, &stcGpioInit);
 | 
			
		||||
    (void)GPIO_Init(BSP_IS42S16400J_ADD11_PORT, BSP_IS42S16400J_ADD11_PIN, &stcGpioInit);
 | 
			
		||||
 | 
			
		||||
    /************************** Set EXMC pin function *************************/
 | 
			
		||||
    /* DMC_CKE */
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_CKE_PORT, BSP_IS42S16400J_CKE_PIN, GPIO_FUNC_12);
 | 
			
		||||
 | 
			
		||||
    /* DMC_CLK */
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_CLK_PORT, BSP_IS42S16400J_CLK_PIN, GPIO_FUNC_12);
 | 
			
		||||
 | 
			
		||||
    /* DMC_LDQM && DMC_UDQM */
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DQM0_PORT, BSP_IS42S16400J_DQM0_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DQM1_PORT, BSP_IS42S16400J_DQM1_PIN, GPIO_FUNC_12);
 | 
			
		||||
 | 
			
		||||
    /* DMC_BA[0:1] */
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_BA0_PORT, BSP_IS42S16400J_BA0_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_BA1_PORT, BSP_IS42S16400J_BA1_PIN, GPIO_FUNC_12);
 | 
			
		||||
 | 
			
		||||
    /* DMC_CS */
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_CS1_PORT, BSP_IS42S16400J_CS1_PIN, GPIO_FUNC_12);
 | 
			
		||||
 | 
			
		||||
    /* DMC_CAS && DMC_RAS */
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_CAS_PORT, BSP_IS42S16400J_CAS_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_RAS_PORT, BSP_IS42S16400J_RAS_PIN, GPIO_FUNC_12);
 | 
			
		||||
 | 
			
		||||
    /* DMC_WE */
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_WE_PORT, BSP_IS42S16400J_WE_PIN, GPIO_FUNC_12);
 | 
			
		||||
 | 
			
		||||
    /* DMC_DATA[0:15] */
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA0_PORT, BSP_IS42S16400J_DATA0_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA1_PORT, BSP_IS42S16400J_DATA1_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA2_PORT, BSP_IS42S16400J_DATA2_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA3_PORT, BSP_IS42S16400J_DATA3_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA4_PORT, BSP_IS42S16400J_DATA4_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA5_PORT, BSP_IS42S16400J_DATA5_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA6_PORT, BSP_IS42S16400J_DATA6_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA7_PORT, BSP_IS42S16400J_DATA7_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA8_PORT, BSP_IS42S16400J_DATA8_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA9_PORT, BSP_IS42S16400J_DATA9_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA10_PORT, BSP_IS42S16400J_DATA10_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA11_PORT, BSP_IS42S16400J_DATA11_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA12_PORT, BSP_IS42S16400J_DATA12_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA13_PORT, BSP_IS42S16400J_DATA13_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA14_PORT, BSP_IS42S16400J_DATA14_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_DATA15_PORT, BSP_IS42S16400J_DATA15_PIN, GPIO_FUNC_12);
 | 
			
		||||
 | 
			
		||||
    /* DMC_ADD[0:11]*/
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD0_PORT, BSP_IS42S16400J_ADD0_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD1_PORT, BSP_IS42S16400J_ADD1_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD2_PORT, BSP_IS42S16400J_ADD2_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD3_PORT, BSP_IS42S16400J_ADD3_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD4_PORT, BSP_IS42S16400J_ADD4_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD5_PORT, BSP_IS42S16400J_ADD5_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD6_PORT, BSP_IS42S16400J_ADD6_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD7_PORT, BSP_IS42S16400J_ADD7_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD8_PORT, BSP_IS42S16400J_ADD8_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD9_PORT, BSP_IS42S16400J_ADD9_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD10_PORT, BSP_IS42S16400J_ADD10_PIN, GPIO_FUNC_12);
 | 
			
		||||
    GPIO_SetFunc(BSP_IS42S16400J_ADD11_PORT, BSP_IS42S16400J_ADD11_PIN, GPIO_FUNC_12);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief  SDRAM IS42S16400J initialization sequence.
 | 
			
		||||
 * @param  [in] u32Chip                     The command chip number.
 | 
			
		||||
 *         This parameter can be one of the following values:
 | 
			
		||||
 *           @arg EXMC_DMC_CHIP0:           Chip 0
 | 
			
		||||
 *           @arg EXMC_DMC_CHIP1:           Chip 1
 | 
			
		||||
 *           @arg EXMC_DMC_CHIP2:           Chip 2
 | 
			
		||||
 *           @arg EXMC_DMC_CHIP3:           Chip 3
 | 
			
		||||
 * @param  [in] u32Bank                     The command bank.
 | 
			
		||||
 *         This parameter can be one of the following values:
 | 
			
		||||
 *           @arg EXMC_DMC_BANK0:           Bank 0
 | 
			
		||||
 *           @arg EXMC_DMC_BANK1:           Bank 1
 | 
			
		||||
 *           @arg EXMC_DMC_BANK2:           Bank 2
 | 
			
		||||
 *           @arg EXMC_DMC_BANK3:           Bank 3
 | 
			
		||||
 * @param  [in] u32MdRegValue               The SDRAM mode register value
 | 
			
		||||
 * @retval None
 | 
			
		||||
 */
 | 
			
		||||
static void BSP_SDRAM_InitSequence(uint32_t u32Chip, uint32_t u32Bank, uint32_t u32MdRegValue)
 | 
			
		||||
{
 | 
			
		||||
    /* SDRAM initialization sequence:
 | 
			
		||||
       CMD NOP->PrechargeAll->AutoRefresh->AutoRefresh->MdRegConfig->NOP */
 | 
			
		||||
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_NOP, 0UL);
 | 
			
		||||
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_PRECHARGE_ALL, 0UL);
 | 
			
		||||
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_AUTO_REFRESH, 0UL);
 | 
			
		||||
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_AUTO_REFRESH, 0UL);
 | 
			
		||||
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_MDREG_CONFIG, u32MdRegValue);
 | 
			
		||||
    (void)EXMC_DMC_SetCommand(u32Chip, u32Bank, EXMC_DMC_CMD_NOP, 0UL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief  Get memory information.
 | 
			
		||||
 * @param  [out] pu32MemoryStartAddr    The pointer for memory start address
 | 
			
		||||
 * @param  [out] pu32MemoryByteSize     The pointer for memory size(unit: Byte)
 | 
			
		||||
 * @retval None
 | 
			
		||||
 */
 | 
			
		||||
void BSP_IS42S16400J_GetMemInfo(uint32_t *pu32MemoryStartAddr, uint32_t *pu32MemoryByteSize)
 | 
			
		||||
{
 | 
			
		||||
    if (NULL != pu32MemoryStartAddr) {
 | 
			
		||||
        *pu32MemoryStartAddr = EXMC_DMC_GetChipStartAddr(EXMC_DMC_CHIP1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (NULL != pu32MemoryByteSize) {
 | 
			
		||||
        *pu32MemoryByteSize = BSP_IS42S16400J_SIZE - 1UL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef EXTSRAM_TEST
 | 
			
		||||
/**
 | 
			
		||||
 * @brief  Write memory for byte.
 | 
			
		||||
 * @param  [in] u32Addr                 Memory address to write
 | 
			
		||||
 * @param  [in] au8Data                 Pointer to source buffer
 | 
			
		||||
 * @param  [in] u32Len                  Length of the buffer to write to memory
 | 
			
		||||
 * @retval int32_t:
 | 
			
		||||
 *           - LL_OK:                   Write successfully.
 | 
			
		||||
 *           - LL_ERR_INVD_PARAM:       If one of following cases matches:
 | 
			
		||||
 *                                      -The pointer au8Data value is NULL.
 | 
			
		||||
 *                                      -The variable u32Len value is 0.
 | 
			
		||||
 */
 | 
			
		||||
static int32_t MEMORY_Write8(uint32_t u32Addr, const uint8_t au8Data[], uint32_t u32Len)
 | 
			
		||||
{
 | 
			
		||||
    uint32_t i;
 | 
			
		||||
    int32_t i32Ret = LL_ERR_INVD_PARAM;
 | 
			
		||||
 | 
			
		||||
    if ((au8Data != NULL) && (0UL != u32Len)) {
 | 
			
		||||
        for (i = 0UL; i < u32Len; i++) {
 | 
			
		||||
            *(uint8_t *)(u32Addr + i) = au8Data[i];
 | 
			
		||||
        }
 | 
			
		||||
        i32Ret = LL_OK;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return i32Ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief  Read memory for byte.
 | 
			
		||||
 * @param  [in] u32Addr                 Memory address to read
 | 
			
		||||
 * @param  [out] au8Data                Pointer to buffer
 | 
			
		||||
 * @param  [in] u32Len                  Length of the buffer to read from memory
 | 
			
		||||
 * @retval int32_t:
 | 
			
		||||
 *           - LL_OK:                   Read successfully.
 | 
			
		||||
 *           - LL_ERR_INVD_PARAM:       If one of following cases matches:
 | 
			
		||||
 *                                      -The pointer au8Data value is NULL.
 | 
			
		||||
 *                                      -The variable u32Len value is 0.
 | 
			
		||||
 */
 | 
			
		||||
static int32_t MEMORY_Read8(uint32_t u32Addr, uint8_t au8Data[], uint32_t u32Len)
 | 
			
		||||
{
 | 
			
		||||
    uint32_t i;
 | 
			
		||||
    int32_t i32Ret = LL_ERR_INVD_PARAM;
 | 
			
		||||
 | 
			
		||||
    if ((NULL != au8Data) && (0UL != u32Len)) {
 | 
			
		||||
        for (i = 0UL; i < u32Len; i++) {
 | 
			
		||||
            au8Data[i] = *(uint8_t *)(u32Addr + i);
 | 
			
		||||
        }
 | 
			
		||||
        i32Ret = LL_OK;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return i32Ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief  Access memory for byte.
 | 
			
		||||
 * @param  [in] u3AccessAddr:           Memory address
 | 
			
		||||
 * @param  [in] NumBytes:               Access size(unit: byte)
 | 
			
		||||
 * @retval count for reading and writing dada error
 | 
			
		||||
 */
 | 
			
		||||
static uint32_t MEMORY_Access8(uint32_t u3AccessAddr, uint32_t NumBytes)
 | 
			
		||||
{
 | 
			
		||||
    uint32_t i;
 | 
			
		||||
    uint32_t j;
 | 
			
		||||
    uint32_t u32TestErrCnt = 0UL;
 | 
			
		||||
    uint32_t u32MemoryAddr = u3AccessAddr;
 | 
			
		||||
 | 
			
		||||
    /* Initialize test data. */
 | 
			
		||||
    for (i = 0UL; i < DATA_BUF_LEN; i++) {
 | 
			
		||||
        m_au8ReadData[i] = 0U;
 | 
			
		||||
        m_au8WriteData[i] = 0x12U;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0UL; i < NumBytes; i += DATA_BUF_LEN) {
 | 
			
		||||
        (void)MEMORY_Write8(u32MemoryAddr, m_au8WriteData, DATA_BUF_LEN);
 | 
			
		||||
        (void)MEMORY_Read8(u32MemoryAddr, m_au8ReadData, DATA_BUF_LEN);
 | 
			
		||||
 | 
			
		||||
        /* Verify data. */
 | 
			
		||||
        for (j = 0UL; j < DATA_BUF_LEN; j++) {
 | 
			
		||||
            if (m_au8WriteData[j] != m_au8ReadData[j]) {
 | 
			
		||||
                u32TestErrCnt++;
 | 
			
		||||
                KPrintf("Byte read/write error: address = 0x%.8x; write data = 0x%x; read data = 0x%x\r\n",
 | 
			
		||||
                           (unsigned int)(u32MemoryAddr + j), (unsigned int)m_au8WriteData[j], (unsigned int)m_au8ReadData[j]);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        u32MemoryAddr += (DATA_BUF_LEN * sizeof(m_au8ReadData[0]));
 | 
			
		||||
        (void)memset(m_au8ReadData, 0, (DATA_BUF_LEN * sizeof(m_au8ReadData[0])));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return u32TestErrCnt;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
int ExtSdramInit(void)
 | 
			
		||||
{
 | 
			
		||||
    __IO uint32_t u32To = 0UL;
 | 
			
		||||
    uint32_t u32MdRegValue;
 | 
			
		||||
    int32_t i32Ret = LL_OK;
 | 
			
		||||
    stc_exmc_dmc_init_t stcDmcInit;
 | 
			
		||||
    stc_exmc_dmc_chip_config_t stcCsConfig;
 | 
			
		||||
 | 
			
		||||
    uint32_t m_u32StartAddr = 0UL;
 | 
			
		||||
    uint32_t m_u32EndAddr = 0UL;
 | 
			
		||||
    uint32_t m_u32ByteSize = 0UL;
 | 
			
		||||
 | 
			
		||||
    /* EXCLK 60MHz */
 | 
			
		||||
    CLK_SetClockDiv(CLK_BUS_EXCLK, CLK_EXCLK_DIV4);
 | 
			
		||||
 | 
			
		||||
    /* Initialization DMC port.*/
 | 
			
		||||
    BSP_DMC_PortInit();
 | 
			
		||||
 | 
			
		||||
    /* Enable DMC clock */
 | 
			
		||||
    FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_DMC, ENABLE);
 | 
			
		||||
 | 
			
		||||
    /* Enable DMC. */
 | 
			
		||||
    EXMC_DMC_Cmd(ENABLE);
 | 
			
		||||
 | 
			
		||||
    /* Configure DMC width && refresh period & chip & timing. */
 | 
			
		||||
    (void)EXMC_DMC_StructInit(&stcDmcInit);
 | 
			
		||||
    stcDmcInit.u32RefreshPeriod = 900UL;
 | 
			
		||||
    stcDmcInit.u32ColumnBitsNumber = EXMC_DMC_COLUMN_BITS_NUM8;
 | 
			
		||||
    stcDmcInit.u32RowBitsNumber = EXMC_DMC_ROW_BITS_NUM12;
 | 
			
		||||
    stcDmcInit.u32MemBurst = EXMC_DMC_BURST_1BEAT;
 | 
			
		||||
    stcDmcInit.u32AutoRefreshChips = EXMC_DMC_AUTO_REFRESH_2CHIPS;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8CASL = 2U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8DQSS = 0U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8MRD = 2U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8RAS = 3U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8RC = 4U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8RCD_B = 1U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8RCD_P = 0U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8RFC_B = 4U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8RFC_P = 0U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8RP_B = 1U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8RP_P = 0U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8RRD = 1U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8WR = 2U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8WTR = 1U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8XP = 1U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8XSR = 5U;
 | 
			
		||||
    stcDmcInit.stcTimingConfig.u8ESR = 5U;
 | 
			
		||||
    (void)EXMC_DMC_Init(&stcDmcInit);
 | 
			
		||||
 | 
			
		||||
    /* Configure DMC address space. */
 | 
			
		||||
    stcCsConfig.u32AddrMask = EXMC_DMC_ADDR_MASK_16MB;
 | 
			
		||||
    stcCsConfig.u32AddrMatch = 0x80UL;
 | 
			
		||||
    stcCsConfig.u32AddrDecodeMode = EXMC_DMC_CS_DECODE_ROWBANKCOL;
 | 
			
		||||
    (void)EXMC_DMC_ChipConfig(EXMC_DMC_CHIP1, &stcCsConfig);
 | 
			
		||||
 | 
			
		||||
    /* SDRAM initialization sequence. */
 | 
			
		||||
    u32MdRegValue = (IS42S16400J_MR_BURST_TYPE_SEQUENTIAL | IS42S16400J_MR_WRITEBURST_PROGRAMMED);
 | 
			
		||||
    if (2U == stcDmcInit.stcTimingConfig.u8CASL) {
 | 
			
		||||
        u32MdRegValue |= IS42S16400J_MR_CAS_LATENCY_2;
 | 
			
		||||
    } else {
 | 
			
		||||
        u32MdRegValue |= IS42S16400J_MR_CAS_LATENCY_3;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (EXMC_DMC_BURST_1BEAT == stcDmcInit.u32MemBurst) {
 | 
			
		||||
        u32MdRegValue |= IS42S16400J_MR_BURST_1BEAT;
 | 
			
		||||
    } else if (EXMC_DMC_BURST_2BEAT == stcDmcInit.u32MemBurst) {
 | 
			
		||||
        u32MdRegValue |= IS42S16400J_MR_BURST_2BEAT;
 | 
			
		||||
    } else if (EXMC_DMC_BURST_4BEAT == stcDmcInit.u32MemBurst) {
 | 
			
		||||
        u32MdRegValue |= IS42S16400J_MR_BURST_4BEAT;
 | 
			
		||||
    } else {
 | 
			
		||||
        u32MdRegValue |= IS42S16400J_MR_BURST_8BEAT;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BSP_SDRAM_InitSequence(EXMC_DMC_CHIP1, EXMC_DMC_BANK0, u32MdRegValue);
 | 
			
		||||
 | 
			
		||||
    /* Switch state from configure to ready */
 | 
			
		||||
    EXMC_DMC_SetState(EXMC_DMC_CTRL_STATE_GO);
 | 
			
		||||
    EXMC_DMC_SetState(EXMC_DMC_CTRL_STATE_WAKEUP);
 | 
			
		||||
    EXMC_DMC_SetState(EXMC_DMC_CTRL_STATE_GO);
 | 
			
		||||
 | 
			
		||||
    /* Check status */
 | 
			
		||||
    while (EXMC_DMC_CURR_STATUS_RDY != EXMC_DMC_GetStatus()) {
 | 
			
		||||
        if (u32To > DMC_MAX_TIMEOUT) {
 | 
			
		||||
            i32Ret = LL_ERR;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        u32To++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BSP_IS42S16400J_GetMemInfo(&m_u32StartAddr, &m_u32ByteSize);
 | 
			
		||||
    m_u32EndAddr = (unsigned int)(m_u32StartAddr + m_u32ByteSize - 1UL);
 | 
			
		||||
 | 
			
		||||
    KPrintf("SDRAM Memory start address: 0x%.8x \r\n", (unsigned int)m_u32StartAddr);
 | 
			
		||||
    KPrintf("SDRAM Memory end   address: 0x%.8x \r\n", (unsigned int)m_u32EndAddr);
 | 
			
		||||
    KPrintf("SDRAM Memory size  (Bytes): 0x%.8x \r\n", (unsigned int)m_u32ByteSize);
 | 
			
		||||
 | 
			
		||||
#ifdef EXTSRAM_TEST
 | 
			
		||||
    /****************** Access data width: Byte *****************/
 | 
			
		||||
    KPrintf("******** Write/read test ********\r\n");
 | 
			
		||||
    m_u32ByteTestErrorCnt = MEMORY_Access8(m_u32StartAddr, m_u32ByteSize);
 | 
			
		||||
    KPrintf("Byte read/write error data count: %u \r\n", (unsigned int)m_u32ByteTestErrorCnt);
 | 
			
		||||
    if (m_u32ByteTestErrorCnt > 0) {
 | 
			
		||||
        return LL_ERR;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    extern void ExtSramInitBoardMemory(void *start_phy_address, void *end_phy_address, uint8 extsram_idx);
 | 
			
		||||
    ExtSramInitBoardMemory((void*)(m_u32StartAddr), (void*)(m_u32EndAddr), 0);
 | 
			
		||||
 | 
			
		||||
    return i32Ret;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -15,19 +15,27 @@ if BSP_USING_SPI
 | 
			
		|||
            default "spi1_drv"
 | 
			
		||||
    endif   
 | 
			
		||||
 | 
			
		||||
    config BSP_USING_SPI6
 | 
			
		||||
    bool "Using spi6"
 | 
			
		||||
    default n
 | 
			
		||||
    config BSP_USING_SPI2
 | 
			
		||||
    bool "Using spi2 for E22 LoRa"
 | 
			
		||||
    default y
 | 
			
		||||
 | 
			
		||||
    if BSP_USING_SPI6
 | 
			
		||||
        config SPI_BUS_NAME_6
 | 
			
		||||
            string "spi bus 6 name"
 | 
			
		||||
            default "spi6"
 | 
			
		||||
        config SPI_6_DEVICE_NAME_0
 | 
			
		||||
            string "spi bus 6 device 0 name"
 | 
			
		||||
            default "spi6_dev0" 
 | 
			
		||||
        config SPI_6_DRV_NAME
 | 
			
		||||
            string "spi bus 6 driver name"
 | 
			
		||||
            default "spi6_drv"
 | 
			
		||||
    if BSP_USING_SPI2
 | 
			
		||||
        config SPI_BUS_NAME_2
 | 
			
		||||
            string "spi bus 2 name"
 | 
			
		||||
            default "spi2"
 | 
			
		||||
        config SPI_2_DEVICE_NAME_0
 | 
			
		||||
            string "spi bus 2 device 0 name"
 | 
			
		||||
            default "spi2_dev0" 
 | 
			
		||||
        config SPI_2_DRV_NAME
 | 
			
		||||
            string "spi bus 2 driver name"
 | 
			
		||||
            default "spi2_drv"
 | 
			
		||||
        menuconfig RESOURCES_SPI_LORA
 | 
			
		||||
            bool "Using spi lora function"
 | 
			
		||||
            default n
 | 
			
		||||
            if RESOURCES_SPI_LORA
 | 
			
		||||
                config SX12XX_DEVICE_NAME
 | 
			
		||||
                string "SX1276 lora device name"
 | 
			
		||||
                default "spi2_lora"
 | 
			
		||||
            endif
 | 
			
		||||
    endif  
 | 
			
		||||
endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,8 @@
 | 
			
		|||
SRC_FILES := connect_spi.c
 | 
			
		||||
 | 
			
		||||
ifeq ($(CONFIG_RESOURCES_SPI_LORA),y)
 | 
			
		||||
	SRC_DIR := third_party_spi_lora
 | 
			
		||||
	SRC_FILES += connect_lora_spi.c
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,457 @@
 | 
			
		|||
/*
 | 
			
		||||
* 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_lora_spi.c
 | 
			
		||||
* @brief support to register spi lora pointer and function for xishutong-arm32
 | 
			
		||||
* @version 3.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2023-12-21
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include <connect_spi_lora.h>
 | 
			
		||||
 | 
			
		||||
/* RST = PD02 */
 | 
			
		||||
#define LORA_RST_PORT                     (GPIO_PORT_D)
 | 
			
		||||
#define LORA_RST_PIN                      (GPIO_PIN_02)
 | 
			
		||||
 | 
			
		||||
static struct HardwareDev *g_spi_lora_dev;
 | 
			
		||||
static tRadioDriver *Radio = NONE;
 | 
			
		||||
 | 
			
		||||
void SX1276InitIo(void)
 | 
			
		||||
{
 | 
			
		||||
    stc_gpio_init_t stcGpioInit;
 | 
			
		||||
 | 
			
		||||
    (void)GPIO_StructInit(&stcGpioInit);
 | 
			
		||||
    stcGpioInit.u16PinState = PIN_STAT_RST;
 | 
			
		||||
    stcGpioInit.u16PinDir = PIN_DIR_OUT;
 | 
			
		||||
    (void)GPIO_Init(LORA_RST_PORT, LORA_RST_PIN, &stcGpioInit);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void SX1276WriteRxTx(uint8_t txEnable)
 | 
			
		||||
{
 | 
			
		||||
    if (txEnable != 0) {
 | 
			
		||||
        /*to do*/
 | 
			
		||||
    } else {
 | 
			
		||||
        /*to do*/
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX1276SetReset(uint8_t state)
 | 
			
		||||
{
 | 
			
		||||
    if (state == RADIO_RESET_ON) {
 | 
			
		||||
        GPIO_ResetPins(LORA_RST_PORT, LORA_RST_PIN);
 | 
			
		||||
    } else {
 | 
			
		||||
        stc_gpio_init_t stcGpioInit;
 | 
			
		||||
        (void)GPIO_StructInit(&stcGpioInit);
 | 
			
		||||
        stcGpioInit.u16PinDir = PIN_DIR_IN;
 | 
			
		||||
        (void)GPIO_Init(LORA_RST_PORT, LORA_RST_PIN, &stcGpioInit);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//Not-necessary Function
 | 
			
		||||
uint8_t SX1276ReadDio0(void)
 | 
			
		||||
{
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t SX1276ReadDio1(void)
 | 
			
		||||
{
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t SX1276ReadDio2(void)
 | 
			
		||||
{
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t SX1276ReadDio3(void)
 | 
			
		||||
{
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t SX1276ReadDio4(void)
 | 
			
		||||
{
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t SX1276ReadDio5(void)
 | 
			
		||||
{
 | 
			
		||||
    return 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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX1276Write(uint8_t addr, uint8_t data)
 | 
			
		||||
{
 | 
			
		||||
    SX1276WriteBuffer(addr, &data, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SX1276Read(uint8_t addr, uint8_t *data)
 | 
			
		||||
{
 | 
			
		||||
    SX1276ReadBuffer(addr, data, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t Sx1276SpiCheck(void)
 | 
			
		||||
{
 | 
			
		||||
    uint8_t test = 0;
 | 
			
		||||
 | 
			
		||||
	tLoRaSettings settings;
 | 
			
		||||
	SX1276Read(REG_LR_VERSION, &test);
 | 
			
		||||
	KPrintf("version code of the chip is 0x%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);
 | 
			
		||||
 | 
			
		||||
    if (write_param->size > 256) {
 | 
			
		||||
        KPrintf("SpiLoraWrite ERROR:The message is too long!\n");
 | 
			
		||||
        return ERROR;
 | 
			
		||||
    } else {
 | 
			
		||||
        SX1276SetTx(write_param->buffer, write_param->size);    
 | 
			
		||||
 | 
			
		||||
        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);
 | 
			
		||||
 | 
			
		||||
    KPrintf("SpiLoraRead Ready!\n");
 | 
			
		||||
 | 
			
		||||
    int ret = SX1276GetRx(read_param->buffer, (uint16 *)&read_param->read_length);
 | 
			
		||||
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return read_param->read_length;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static uint32 SpiLoraOpen(void *dev)
 | 
			
		||||
{
 | 
			
		||||
    NULL_PARAM_CHECK(dev);
 | 
			
		||||
 | 
			
		||||
    KPrintf("SpiLoraOpen start\n");
 | 
			
		||||
 | 
			
		||||
    x_err_t ret = EOK;
 | 
			
		||||
    static x_bool lora_init_status = RET_FALSE;
 | 
			
		||||
 | 
			
		||||
    if (RET_TRUE == lora_init_status) {
 | 
			
		||||
        return 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;
 | 
			
		||||
 | 
			
		||||
    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 != Sx1276SpiCheck()) {
 | 
			
		||||
        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 256 characters\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    lora_init_status = RET_TRUE;
 | 
			
		||||
    
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static uint32 SpiLoraClose(void *dev)
 | 
			
		||||
{
 | 
			
		||||
    NULL_PARAM_CHECK(dev);
 | 
			
		||||
 | 
			
		||||
    return EOK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const struct LoraDevDone lora_done =
 | 
			
		||||
{
 | 
			
		||||
    .open = SpiLoraOpen,
 | 
			
		||||
    .close = SpiLoraClose,
 | 
			
		||||
    .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 lora_name lora 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;
 | 
			
		||||
 | 
			
		||||
    struct Driver *spi_driver = SpiDriverFind(drv_name, TYPE_SPI_DRV);
 | 
			
		||||
    if (NONE == spi_driver) {
 | 
			
		||||
        KPrintf("SpiLoraInit find spi driver %s error! \n", drv_name);
 | 
			
		||||
        free(spi_lora_dev);
 | 
			
		||||
        return NONE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //spi drv get spi dev param (SpiDeviceParam)
 | 
			
		||||
    spi_driver->private_data = spi_lora_dev->spi_dev->haldev.private_data;
 | 
			
		||||
    spi_lora_dev->spi_dev->haldev.owner_bus->owner_driver = spi_driver;
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
    if (NONE == SpiLoraInit(SPI_BUS_NAME_2, SPI_2_DEVICE_NAME_0, SPI_2_DRV_NAME, SX12XX_DEVICE_NAME)) {
 | 
			
		||||
        return ERROR;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    return EOK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define LORA_TEST
 | 
			
		||||
#ifdef LORA_TEST
 | 
			
		||||
/*Just for lora test*/
 | 
			
		||||
static struct Bus *bus;
 | 
			
		||||
static struct HardwareDev *dev;
 | 
			
		||||
 | 
			
		||||
void LoraOpen(void)
 | 
			
		||||
{
 | 
			
		||||
    x_err_t ret = EOK;
 | 
			
		||||
 | 
			
		||||
    bus = BusFind(SPI_BUS_NAME_1);
 | 
			
		||||
    dev = BusFindDevice(bus, SX12XX_DEVICE_NAME);
 | 
			
		||||
 | 
			
		||||
    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)
 | 
			
		||||
{
 | 
			
		||||
    uint32 read_length = 0;
 | 
			
		||||
    struct BusBlockReadParam read_param;
 | 
			
		||||
    memset(&read_param, 0, sizeof(struct BusBlockReadParam));
 | 
			
		||||
 | 
			
		||||
    read_param.buffer = malloc(SPI_LORA_BUFFER_SIZE);
 | 
			
		||||
 | 
			
		||||
    read_length = SpiLoraRead(dev, &read_param);
 | 
			
		||||
 | 
			
		||||
    KPrintf("LoraReceive length %d\n", read_length);
 | 
			
		||||
    for (int i = 0; i < read_length; i ++) {
 | 
			
		||||
        KPrintf("i %d data 0x%x\n", i, ((uint8 *)read_param.buffer)[i]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
 | 
			
		||||
        KPrintf("LoraSend data %s length %d\n", Msg, 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 );
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -28,7 +28,7 @@ Author: AIIT XUOS Lab
 | 
			
		|||
Modification:
 | 
			
		||||
1. support xishutong-arm32-board spi configure, write and read
 | 
			
		||||
2. support xishutong-arm32-board spi bus device and driver register
 | 
			
		||||
3. SPI1 for W5500, SPI6 using J12-pin-header to connect
 | 
			
		||||
3. SPI1 for W5500, SPI2 using J12-pin-header to connect
 | 
			
		||||
*************************************************/
 | 
			
		||||
 | 
			
		||||
#include <connect_spi.h>
 | 
			
		||||
| 
						 | 
				
			
			@ -36,48 +36,48 @@ Modification:
 | 
			
		|||
#define SPI1_MASTER_SLAVE_MODE (SPI_MASTER)
 | 
			
		||||
 | 
			
		||||
/* SPI1 definition */
 | 
			
		||||
#define SPI1_UNIT (CM_SPI1)
 | 
			
		||||
#define SPI1_CLK (FCG1_PERIPH_SPI1)
 | 
			
		||||
#define SPI1_UNIT                   (CM_SPI1)
 | 
			
		||||
#define SPI1_CLK                    (FCG1_PERIPH_SPI1)
 | 
			
		||||
 | 
			
		||||
/* CS = PG3 */
 | 
			
		||||
#define SPI1_SS_PORT (GPIO_PORT_G)
 | 
			
		||||
#define SPI1_SS_PIN (GPIO_PIN_03)
 | 
			
		||||
#define SPI1_SS_PORT                (GPIO_PORT_G)
 | 
			
		||||
#define SPI1_SS_PIN                 (GPIO_PIN_03)
 | 
			
		||||
/* SCLK = PG5 */
 | 
			
		||||
#define SPI1_SCK_PORT (GPIO_PORT_G)
 | 
			
		||||
#define SPI1_SCK_PIN (GPIO_PIN_05)
 | 
			
		||||
#define SPI1_SCK_FUNC (GPIO_FUNC_40)
 | 
			
		||||
#define SPI1_SCK_PORT               (GPIO_PORT_G)
 | 
			
		||||
#define SPI1_SCK_PIN                (GPIO_PIN_05)
 | 
			
		||||
#define SPI1_SCK_FUNC               (GPIO_FUNC_40)
 | 
			
		||||
/* MOSI = PG4 */
 | 
			
		||||
#define SPI1_MOSI_PORT (GPIO_PORT_G)
 | 
			
		||||
#define SPI1_MOSI_PIN (GPIO_PIN_04)
 | 
			
		||||
#define SPI1_MOSI_FUNC (GPIO_FUNC_41)
 | 
			
		||||
#define SPI1_MOSI_PORT              (GPIO_PORT_G)
 | 
			
		||||
#define SPI1_MOSI_PIN               (GPIO_PIN_04)
 | 
			
		||||
#define SPI1_MOSI_FUNC              (GPIO_FUNC_41)
 | 
			
		||||
/* MISO = PG6 */
 | 
			
		||||
#define SPI1_MISO_PORT (GPIO_PORT_G)
 | 
			
		||||
#define SPI1_MISO_PIN (GPIO_PIN_06)
 | 
			
		||||
#define SPI1_MISO_FUNC (GPIO_FUNC_42)
 | 
			
		||||
#define SPI1_MISO_PORT              (GPIO_PORT_G)
 | 
			
		||||
#define SPI1_MISO_PIN               (GPIO_PIN_06)
 | 
			
		||||
#define SPI1_MISO_FUNC              (GPIO_FUNC_42)
 | 
			
		||||
 | 
			
		||||
#define SPI1_DEVICE_SLAVE_ID_0 0
 | 
			
		||||
#define SPI1_DEVICE_SLAVE_ID_0      0
 | 
			
		||||
 | 
			
		||||
/* SPI6 definition */
 | 
			
		||||
#define SPI6_UNIT (CM_SPI6)
 | 
			
		||||
#define SPI6_CLK (FCG1_PERIPH_SPI6)
 | 
			
		||||
/* SPI2 definition */
 | 
			
		||||
#define SPI2_UNIT                   (CM_SPI2)
 | 
			
		||||
#define SPI2_CLK                    (FCG1_PERIPH_SPI2)
 | 
			
		||||
 | 
			
		||||
/* SS = PE02 */
 | 
			
		||||
#define SPI6_SS_PORT (GPIO_PORT_E)
 | 
			
		||||
#define SPI6_SS_PIN (GPIO_PIN_02)
 | 
			
		||||
/* SCK = PE03 */
 | 
			
		||||
#define SPI6_SCK_PORT (GPIO_PORT_E)
 | 
			
		||||
#define SPI6_SCK_PIN (GPIO_PIN_03)
 | 
			
		||||
#define SPI6_SCK_FUNC (GPIO_FUNC_46)
 | 
			
		||||
/* MOSI = PE04 */
 | 
			
		||||
#define SPI6_MOSI_PORT (GPIO_PORT_E)
 | 
			
		||||
#define SPI6_MOSI_PIN (GPIO_PIN_04)
 | 
			
		||||
#define SPI6_MOSI_FUNC (GPIO_FUNC_47)
 | 
			
		||||
/* MISO = PE05 */
 | 
			
		||||
#define SPI6_MISO_PORT (GPIO_PORT_E)
 | 
			
		||||
#define SPI6_MISO_PIN (GPIO_PIN_05)
 | 
			
		||||
#define SPI6_MISO_FUNC (GPIO_FUNC_48)
 | 
			
		||||
/* SS = PI01 */
 | 
			
		||||
#define SPI2_SS_PORT                (GPIO_PORT_I)
 | 
			
		||||
#define SPI2_SS_PIN                 (GPIO_PIN_01)
 | 
			
		||||
/* SCK = PH14 */
 | 
			
		||||
#define SPI2_SCK_PORT               (GPIO_PORT_H)
 | 
			
		||||
#define SPI2_SCK_PIN                (GPIO_PIN_14)
 | 
			
		||||
#define SPI2_SCK_FUNC               (GPIO_FUNC_43)
 | 
			
		||||
/* MOSI = PH15 */
 | 
			
		||||
#define SPI2_MOSI_PORT              (GPIO_PORT_H)
 | 
			
		||||
#define SPI2_MOSI_PIN               (GPIO_PIN_15)
 | 
			
		||||
#define SPI2_MOSI_FUNC              (GPIO_FUNC_44)
 | 
			
		||||
/* MISO = PI0 */
 | 
			
		||||
#define SPI2_MISO_PORT              (GPIO_PORT_I)
 | 
			
		||||
#define SPI2_MISO_PIN               (GPIO_PIN_00)
 | 
			
		||||
#define SPI2_MISO_FUNC              (GPIO_FUNC_45)
 | 
			
		||||
 | 
			
		||||
#define SPI6_DEVICE_SLAVE_ID_0 0
 | 
			
		||||
#define SPI2_DEVICE_SLAVE_ID_0      0
 | 
			
		||||
 | 
			
		||||
static void HwSpiEnable(CM_SPI_TypeDef* SPIx)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -148,20 +148,20 @@ static uint32 SpiSdkInit(struct SpiDriver* spi_drv)
 | 
			
		|||
    SPI_Cmd(SPI1_UNIT, ENABLE);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef BSP_USING_SPI6
 | 
			
		||||
#ifdef BSP_USING_SPI2
 | 
			
		||||
    /* Configure Port */
 | 
			
		||||
    (void)GPIO_StructInit(&stcGpioInit);
 | 
			
		||||
    stcGpioInit.u16PinState = PIN_STAT_RST;
 | 
			
		||||
    stcGpioInit.u16PinDir = PIN_DIR_OUT;
 | 
			
		||||
    (void)GPIO_Init(SPI6_SS_PORT, SPI6_SS_PIN, &stcGpioInit);
 | 
			
		||||
    GPIO_SetPins(SPI6_SS_PORT, SPI6_SS_PIN);
 | 
			
		||||
    (void)GPIO_Init(SPI2_SS_PORT, SPI2_SS_PIN, &stcGpioInit);
 | 
			
		||||
    GPIO_SetPins(SPI2_SS_PORT, SPI2_SS_PIN);
 | 
			
		||||
 | 
			
		||||
    GPIO_SetFunc(SPI6_SCK_PORT, SPI6_SCK_PIN, SPI6_SCK_FUNC);
 | 
			
		||||
    GPIO_SetFunc(SPI6_MOSI_PORT, SPI6_MOSI_PIN, SPI6_MOSI_FUNC);
 | 
			
		||||
    GPIO_SetFunc(SPI6_MISO_PORT, SPI6_MISO_PIN, SPI6_MISO_FUNC);
 | 
			
		||||
    GPIO_SetFunc(SPI2_SCK_PORT, SPI2_SCK_PIN, SPI2_SCK_FUNC);
 | 
			
		||||
    GPIO_SetFunc(SPI2_MOSI_PORT, SPI2_MOSI_PIN, SPI2_MOSI_FUNC);
 | 
			
		||||
    GPIO_SetFunc(SPI2_MISO_PORT, SPI2_MISO_PIN, SPI2_MISO_FUNC);
 | 
			
		||||
 | 
			
		||||
    /* Configuration SPI */
 | 
			
		||||
    FCG_Fcg1PeriphClockCmd(SPI6_CLK, ENABLE);
 | 
			
		||||
    FCG_Fcg1PeriphClockCmd(SPI2_CLK, ENABLE);
 | 
			
		||||
    SPI_StructInit(&stcSpiInit);
 | 
			
		||||
 | 
			
		||||
    stcSpiInit.u32WireMode = SPI_4_WIRE;
 | 
			
		||||
| 
						 | 
				
			
			@ -195,8 +195,8 @@ static uint32 SpiSdkInit(struct SpiDriver* spi_drv)
 | 
			
		|||
 | 
			
		||||
    stcSpiInit.u32FrameLevel = SPI_1_FRAME;
 | 
			
		||||
 | 
			
		||||
    (void)SPI_Init(SPI6_UNIT, &stcSpiInit);
 | 
			
		||||
    SPI_Cmd(SPI6_UNIT, ENABLE);
 | 
			
		||||
    (void)SPI_Init(SPI2_UNIT, &stcSpiInit);
 | 
			
		||||
    SPI_Cmd(SPI2_UNIT, ENABLE);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    return EOK;
 | 
			
		||||
| 
						 | 
				
			
			@ -414,30 +414,30 @@ static int BoardSpiDevBend(void)
 | 
			
		|||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef SPI_6_DEVICE_NAME_0
 | 
			
		||||
    static struct SpiHardwareDevice spi6_device0;
 | 
			
		||||
    memset(&spi6_device0, 0, sizeof(struct SpiHardwareDevice));
 | 
			
		||||
#ifdef SPI_2_DEVICE_NAME_0
 | 
			
		||||
    static struct SpiHardwareDevice spi2_device0;
 | 
			
		||||
    memset(&spi2_device0, 0, sizeof(struct SpiHardwareDevice));
 | 
			
		||||
 | 
			
		||||
    static struct SpiSlaveParam spi6_slaveparam0;
 | 
			
		||||
    memset(&spi6_slaveparam0, 0, sizeof(struct SpiSlaveParam));
 | 
			
		||||
    static struct SpiSlaveParam spi2_slaveparam0;
 | 
			
		||||
    memset(&spi2_slaveparam0, 0, sizeof(struct SpiSlaveParam));
 | 
			
		||||
 | 
			
		||||
    spi6_slaveparam0.spi_slave_id = SPI1_DEVICE_SLAVE_ID_0;
 | 
			
		||||
    spi6_slaveparam0.spi_cs_gpio_pin = SPI6_SS_PIN;
 | 
			
		||||
    spi6_slaveparam0.spi_cs_gpio_port = SPI6_SS_PORT;
 | 
			
		||||
    spi2_slaveparam0.spi_slave_id = SPI1_DEVICE_SLAVE_ID_0;
 | 
			
		||||
    spi2_slaveparam0.spi_cs_gpio_pin = SPI2_SS_PIN;
 | 
			
		||||
    spi2_slaveparam0.spi_cs_gpio_port = SPI2_SS_PORT;
 | 
			
		||||
 | 
			
		||||
    spi6_device0.spi_param.spi_slave_param = &spi6_slaveparam0;
 | 
			
		||||
    spi2_device0.spi_param.spi_slave_param = &spi2_slaveparam0;
 | 
			
		||||
 | 
			
		||||
    spi6_device0.spi_dev_done = &(spi_dev_done);
 | 
			
		||||
    spi2_device0.spi_dev_done = &(spi_dev_done);
 | 
			
		||||
 | 
			
		||||
    ret = SpiDeviceRegister(&spi6_device0, (void*)(&spi6_device0.spi_param), SPI_6_DEVICE_NAME_0);
 | 
			
		||||
    ret = SpiDeviceRegister(&spi2_device0, (void*)(&spi2_device0.spi_param), SPI_2_DEVICE_NAME_0);
 | 
			
		||||
    if (EOK != ret) {
 | 
			
		||||
        KPrintf("BoardSpiDevBend SpiDeviceRegister device %s error %d\n", SPI_6_DEVICE_NAME_0, ret);
 | 
			
		||||
        KPrintf("BoardSpiDevBend SpiDeviceRegister device %s error %d\n", SPI_2_DEVICE_NAME_0, ret);
 | 
			
		||||
        return ERROR;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = SpiDeviceAttachToBus(SPI_6_DEVICE_NAME_0, SPI_BUS_NAME_6);
 | 
			
		||||
    ret = SpiDeviceAttachToBus(SPI_2_DEVICE_NAME_0, SPI_BUS_NAME_2);
 | 
			
		||||
    if (EOK != ret) {
 | 
			
		||||
        KPrintf("BoardSpiDevBend SpiDeviceAttachToBus device %s error %d\n", SPI_6_DEVICE_NAME_0, ret);
 | 
			
		||||
        KPrintf("BoardSpiDevBend SpiDeviceAttachToBus device %s error %d\n", SPI_2_DEVICE_NAME_0, ret);
 | 
			
		||||
        return ERROR;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -467,17 +467,17 @@ int HwSpiInit(void)
 | 
			
		|||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef BSP_USING_SPI6
 | 
			
		||||
    static struct SpiBus spi6_bus;
 | 
			
		||||
    memset(&spi6_bus, 0, sizeof(struct SpiBus));
 | 
			
		||||
#ifdef BSP_USING_SPI2
 | 
			
		||||
    static struct SpiBus spi2_bus;
 | 
			
		||||
    memset(&spi2_bus, 0, sizeof(struct SpiBus));
 | 
			
		||||
 | 
			
		||||
    static struct SpiDriver spi6_driver;
 | 
			
		||||
    memset(&spi6_driver, 0, sizeof(struct SpiDriver));
 | 
			
		||||
    static struct SpiDriver spi2_driver;
 | 
			
		||||
    memset(&spi2_driver, 0, sizeof(struct SpiDriver));
 | 
			
		||||
 | 
			
		||||
    spi6_bus.private_data = SPI6_UNIT;
 | 
			
		||||
    spi6_driver.configure = SpiDrvConfigure;
 | 
			
		||||
    spi2_bus.private_data = SPI2_UNIT;
 | 
			
		||||
    spi2_driver.configure = SpiDrvConfigure;
 | 
			
		||||
 | 
			
		||||
    ret = BoardSpiBusInit(&spi6_bus, &spi6_driver, SPI_BUS_NAME_6, SPI_6_DRV_NAME);
 | 
			
		||||
    ret = BoardSpiBusInit(&spi2_bus, &spi2_driver, SPI_BUS_NAME_2, SPI_2_DRV_NAME);
 | 
			
		||||
    if (EOK != ret) {
 | 
			
		||||
        KPrintf("BoardSpiBusInit error ret %u\n", ret);
 | 
			
		||||
        return ERROR;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
ifeq ($(CONFIG_RESOURCES_SPI_LORA),y)
 | 
			
		||||
	SRC_DIR := sx12xx
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_DIR := src
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,51 @@
 | 
			
		|||
/*
 | 
			
		||||
* 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 spi_lora_sx12xx.h
 | 
			
		||||
* @brief define spi lora driver function
 | 
			
		||||
* @version 2.0 
 | 
			
		||||
* @author AIIT XUOS Lab
 | 
			
		||||
* @date 2022-10-31
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#ifndef SPI_LORA_SX12XX_H
 | 
			
		||||
#define SPI_LORA_SX12XX_H
 | 
			
		||||
 | 
			
		||||
#include <connect_spi_lora.h>
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
uint8_t SX1276ReadDio0(void);
 | 
			
		||||
uint8_t SX1276ReadDio1(void);
 | 
			
		||||
uint8_t SX1276ReadDio2(void);
 | 
			
		||||
uint8_t SX1276ReadDio3(void);
 | 
			
		||||
uint8_t SX1276ReadDio4(void);
 | 
			
		||||
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 Sx1276SpiCheck(void);
 | 
			
		||||
void SX1276WriteRxTx(uint8_t txEnable);
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_DIR := radio
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_FILES := radio.c sx1276-Fsk.c sx1276-FskMisc.c sx1276-LoRa.c sx1276-LoRaMisc.c sx1276.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,96 @@
 | 
			
		|||
/*
 | 
			
		||||
 * 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 <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__
 | 
			
		||||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
}
 | 
			
		||||
Some files were not shown because too many files have changed in this diff Show More
		Loading…
	
		Reference in New Issue