forked from xuos/xiuos
upport e220 for stm32f4discovery on nuttx
This commit is contained in:
@@ -6,6 +6,10 @@ config ADAPTER_E220
|
||||
bool "Using lora adapter device E220-400T22S"
|
||||
default n
|
||||
|
||||
config ADAPTER_E22
|
||||
bool "Using lora adapter device E22-400T33D"
|
||||
default n
|
||||
|
||||
choice
|
||||
prompt "Lora device adapter select net role type "
|
||||
default AS_LORA_CLIENT_ROLE
|
||||
@@ -39,4 +43,8 @@ endif
|
||||
|
||||
if ADAPTER_E220
|
||||
source "$APP_DIR/Framework/connection/lora/e220/Kconfig"
|
||||
endif
|
||||
|
||||
if ADAPTER_E22
|
||||
source "$APP_DIR/Framework/connection/lora/e22/Kconfig"
|
||||
endif
|
||||
|
||||
@@ -28,6 +28,10 @@ extern AdapterProductInfoType Sx1278Attach(struct Adapter *adapter);
|
||||
extern AdapterProductInfoType E220Attach(struct Adapter *adapter);
|
||||
#endif
|
||||
|
||||
#ifdef ADAPTER_E22
|
||||
extern AdapterProductInfoType E22Attach(struct Adapter *adapter);
|
||||
#endif
|
||||
|
||||
//#define CLIENT_UPDATE_MODE
|
||||
#define GATEWAY_CMD_MODE
|
||||
|
||||
@@ -880,6 +884,19 @@ int AdapterLoraInit(void)
|
||||
adapter->done = product_info->model_done;
|
||||
#endif
|
||||
|
||||
#ifdef ADAPTER_E22
|
||||
AdapterProductInfoType product_info = E22Attach(adapter);
|
||||
if (!product_info) {
|
||||
printf("AdapterLoraInit e22 attach error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
adapter->product_info_flag = 1;
|
||||
adapter->info = product_info;
|
||||
adapter->done = product_info->model_done;
|
||||
#endif
|
||||
|
||||
PrivSemaphoreCreate(&adapter->sem, 0, 0);
|
||||
|
||||
PrivSemaphoreCreate(&gateway_recv_data_sem, 0, 0);
|
||||
|
||||
28
APP_Framework/Framework/connection/lora/e22/Kconfig
Normal file
28
APP_Framework/Framework/connection/lora/e22/Kconfig
Normal file
@@ -0,0 +1,28 @@
|
||||
config ADAPTER_LORA_E22
|
||||
string "E22-400T33D adapter name"
|
||||
default "e22"
|
||||
|
||||
if ADD_XIZI_FETURES
|
||||
|
||||
endif
|
||||
|
||||
if ADD_NUTTX_FETURES
|
||||
|
||||
config ADAPTER_E22_M0_PATH
|
||||
string "E22 M0 pin device"
|
||||
default "/dev/gpout0"
|
||||
|
||||
config ADAPTER_E22_M1_PATH
|
||||
string "E22 M1 pin device"
|
||||
default "/dev/gpout1"
|
||||
|
||||
|
||||
config ADAPTER_E22_DRIVER
|
||||
string "E22 device uart driver path"
|
||||
default "/dev/ttyS3"
|
||||
|
||||
endif
|
||||
|
||||
if ADD_RTTHREAD_FETURES
|
||||
|
||||
endif
|
||||
6
APP_Framework/Framework/connection/lora/e22/Make.defs
Normal file
6
APP_Framework/Framework/connection/lora/e22/Make.defs
Normal file
@@ -0,0 +1,6 @@
|
||||
############################################################################
|
||||
# APP_Framework/Framework/connection/lora/e22/Make.defs
|
||||
############################################################################
|
||||
ifneq ($(CONFIG_ADAPTER_E22),)
|
||||
CONFIGURED_APPS += $(APPDIR)/../../../APP_Framework/Framework/connection/lora/e22
|
||||
endif
|
||||
7
APP_Framework/Framework/connection/lora/e22/Makefile
Normal file
7
APP_Framework/Framework/connection/lora/e22/Makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
include $(KERNEL_ROOT)/.config
|
||||
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
|
||||
include $(APPDIR)/Make.defs
|
||||
CSRCS += e22.c
|
||||
include $(APPDIR)/Application.mk
|
||||
|
||||
endif
|
||||
501
APP_Framework/Framework/connection/lora/e22/e22.c
Normal file
501
APP_Framework/Framework/connection/lora/e22/e22.c
Normal file
@@ -0,0 +1,501 @@
|
||||
/*
|
||||
* 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 e22.c
|
||||
* @brief Implement the connection E22-400T22S lora adapter function
|
||||
* @version 2.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022.4.20
|
||||
*/
|
||||
|
||||
#include <adapter.h>
|
||||
|
||||
#define E22_GATEWAY_ADDRESS 0xFFFF
|
||||
#define E22_CHANNEL 0x05
|
||||
|
||||
#ifdef AS_LORA_GATEWAY_ROLE
|
||||
#define E22_ADDRESS E22_GATEWAY_ADDRESS
|
||||
#endif
|
||||
|
||||
#ifdef AS_LORA_CLIENT_ROLE
|
||||
#define E22_ADDRESS ADAPTER_LORA_NET_ROLE_ID
|
||||
#endif
|
||||
|
||||
#define E22_UART_BAUD_RATE 115200
|
||||
|
||||
enum E22LoraMode
|
||||
{
|
||||
DATA_TRANSFER_MODE = 0, //M1 : M0 = 0 : 0
|
||||
WOR_SEND_MODE, //M1 : M0 = 0 : 1
|
||||
CONFIGURE_MODE, //M1 : M0 = 1 : 0
|
||||
SLEEP_MODE, //M1 : M0 = 1 : 1
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: Config E22 work mode by set M1/M0 pin
|
||||
* @param mode Lora working mode
|
||||
* @return NULL
|
||||
*/
|
||||
static void E22LoraModeConfig(enum E22LoraMode mode)
|
||||
{
|
||||
int m0_fd, m1_fd;
|
||||
|
||||
//delay 1s , wait AUX ready
|
||||
PrivTaskDelay(1000);
|
||||
m0_fd = PrivOpen(ADAPTER_E22_M0_PATH, O_RDWR);
|
||||
if (m0_fd < 0) {
|
||||
printf("open %s error\n", ADAPTER_E22_M0_PATH);
|
||||
return;
|
||||
}
|
||||
|
||||
m1_fd = PrivOpen(ADAPTER_E22_M1_PATH, O_RDWR);
|
||||
if (m1_fd < 0) {
|
||||
printf("open %s error\n", ADAPTER_E22_M1_PATH);
|
||||
return;
|
||||
}
|
||||
|
||||
//Both M0 and M1 GPIO are outputs mode, set M0 and M1 high or low
|
||||
switch (mode)
|
||||
{
|
||||
case DATA_TRANSFER_MODE:
|
||||
PrivIoctl(m1_fd, GPIOC_WRITE, (unsigned long)GPIO_LOW);
|
||||
PrivIoctl(m0_fd, GPIOC_WRITE, (unsigned long)GPIO_LOW);
|
||||
break;
|
||||
|
||||
case WOR_SEND_MODE:
|
||||
PrivIoctl(m1_fd, GPIOC_WRITE, (unsigned long)GPIO_LOW);
|
||||
PrivIoctl(m0_fd, GPIOC_WRITE, (unsigned long)GPIO_HIGH);
|
||||
break;
|
||||
|
||||
case CONFIGURE_MODE:
|
||||
PrivIoctl(m1_fd, GPIOC_WRITE, (unsigned long)GPIO_HIGH);
|
||||
PrivIoctl(m0_fd, GPIOC_WRITE,(unsigned long)GPIO_LOW);
|
||||
break;
|
||||
|
||||
case SLEEP_MODE:
|
||||
PrivIoctl(m1_fd, GPIOC_WRITE, (unsigned long)GPIO_HIGH);
|
||||
PrivIoctl(m0_fd, GPIOC_WRITE, (unsigned long)GPIO_HIGH);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
PrivClose(m0_fd);
|
||||
PrivClose(m1_fd);
|
||||
|
||||
//delay 20ms , wait mode switch done
|
||||
PrivTaskDelay(20);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Switch baud rate to register bit
|
||||
* @param baud_rate - baud_rate
|
||||
* @return baud_rate_bit
|
||||
*/
|
||||
static uint8 E22BaudRateSwitch(uint32 baud_rate)
|
||||
{
|
||||
uint8 baud_rate_bit;
|
||||
|
||||
switch (baud_rate)
|
||||
{
|
||||
case 1200:
|
||||
baud_rate_bit = 0x0;
|
||||
break;
|
||||
|
||||
case 2400:
|
||||
baud_rate_bit = 0x1;
|
||||
break;
|
||||
|
||||
case 4800:
|
||||
baud_rate_bit = 0x2;
|
||||
break;
|
||||
|
||||
case 9600:
|
||||
baud_rate_bit = 0x3;
|
||||
break;
|
||||
|
||||
case 19200:
|
||||
baud_rate_bit = 0x4;
|
||||
break;
|
||||
|
||||
case 38400:
|
||||
baud_rate_bit = 0x5;
|
||||
break;
|
||||
|
||||
case 57600:
|
||||
baud_rate_bit = 0x6;
|
||||
break;
|
||||
|
||||
case 115200:
|
||||
baud_rate_bit = 0x7;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return baud_rate_bit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Set E22 register, such as address、channel、baud rate...
|
||||
* @param adapter - lora adapter
|
||||
* @param address - address
|
||||
* @param channel - channel
|
||||
* @param baud_rate - baud_rate
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int E22SetRegisterParam(struct Adapter *adapter, uint16 address, uint8 channel, uint32 baud_rate)
|
||||
{
|
||||
int ret;
|
||||
uint8 buffer[50] = {0};
|
||||
uint8 baud_rate_bit = E22BaudRateSwitch(baud_rate);
|
||||
|
||||
E22LoraModeConfig(CONFIGURE_MODE);
|
||||
PrivTaskDelay(30);
|
||||
|
||||
buffer[0] = 0xC0; //write register order
|
||||
buffer[1] = 0x00; //register start-address
|
||||
buffer[2] = 0x09; //register length
|
||||
|
||||
buffer[3] = (address >> 8) & 0xFF; //high address
|
||||
buffer[4] = address & 0xFF; //low adderss
|
||||
|
||||
buffer[5] = 0x00; //net id
|
||||
|
||||
buffer[6] = ((baud_rate_bit << 5) & 0xE0) | 0x04; // baud、stop bits、air rate
|
||||
|
||||
buffer[7] = 0x00;
|
||||
buffer[8] = channel; //channel
|
||||
buffer[9] = 0x03;
|
||||
buffer[10] = 0; //high-cipher
|
||||
buffer[11] = 0; //low-cipher
|
||||
|
||||
ret = PrivWrite(adapter->fd, (void *)buffer, 12);
|
||||
if(ret < 0){
|
||||
printf("E22SetRegisterParam send failed %d!\n", ret);
|
||||
}
|
||||
|
||||
PrivRead(adapter->fd, buffer, 11);
|
||||
|
||||
E22LoraModeConfig(DATA_TRANSFER_MODE);
|
||||
PrivTaskDelay(1000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Get E22 register, such as address、channel、baud rate...
|
||||
* @param buf - data buf
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int E22GetRegisterParam(uint8 *buf)
|
||||
{
|
||||
int ret;
|
||||
uint8 buffer[3] = {0};
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_LORA_NAME);
|
||||
if (NULL == adapter) {
|
||||
printf("E22GetRegisterParam find lora adapter error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
E22LoraModeConfig(CONFIGURE_MODE);
|
||||
PrivTaskDelay(30);
|
||||
|
||||
buffer[0] = 0xC1; //read register order
|
||||
buffer[1] = 0x00; //register start-address
|
||||
buffer[2] = 0x09; //register length
|
||||
|
||||
ret = PrivWrite(adapter->fd, (void *)buffer, 3);
|
||||
if(ret < 0){
|
||||
printf("E22GetRegisterParam send failed %d!\n", ret);
|
||||
}
|
||||
|
||||
PrivRead(adapter->fd, buf, 11);
|
||||
|
||||
E22LoraModeConfig(DATA_TRANSFER_MODE);
|
||||
PrivTaskDelay(30);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Open E22 uart function
|
||||
* @param adapter - Lora device pointer
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int E22Open(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
struct termios cfg;
|
||||
/*step1: open e22 uart port*/
|
||||
adapter->fd = PrivOpen(ADAPTER_E22_DRIVER, O_RDWR);
|
||||
if (adapter->fd < 0) {
|
||||
printf("E22Open get uart %s fd error\n", ADAPTER_E22_DRIVER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
tcgetattr(adapter->fd, &cfg);
|
||||
cfsetspeed(&cfg, BAUD_RATE_9600);
|
||||
tcsetattr(adapter->fd, TCSANOW, &cfg);
|
||||
|
||||
E22SetRegisterParam(adapter, E22_ADDRESS, E22_CHANNEL, E22_UART_BAUD_RATE);
|
||||
|
||||
|
||||
cfsetspeed(&cfg, E22_UART_BAUD_RATE);
|
||||
tcsetattr(adapter->fd, TCSANOW, &cfg);
|
||||
|
||||
ADAPTER_DEBUG("E22Open done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description: Close E22 uart function
|
||||
* @param adapter - Lora device pointer
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int E22Close(struct Adapter *adapter)
|
||||
{
|
||||
/*step1: close e22 uart port*/
|
||||
int ret;
|
||||
ret = PrivClose(adapter->fd);
|
||||
if(ret < 0) {
|
||||
printf("E22 close failed: %d!\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ADAPTER_DEBUG("E22 Close done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: E22 ioctl function
|
||||
* @param adapter - Lora device pointer
|
||||
* @param cmd - ioctl cmd
|
||||
* @param args - iotl params
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int E22Ioctl(struct Adapter *adapter, int cmd, void *args)
|
||||
{
|
||||
/*to do*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: E22 join lora net group function
|
||||
* @param adapter - Lora device pointer
|
||||
* @param priv_net_group - priv_net_group params
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int E22Join(struct Adapter *adapter, unsigned char *priv_net_group)
|
||||
{
|
||||
int ret;
|
||||
struct AdapterData *priv_net_group_data = (struct AdapterData *)priv_net_group;
|
||||
|
||||
ret = PrivWrite(adapter->fd, (void *)priv_net_group_data->buffer, priv_net_group_data->len);
|
||||
if(ret < 0) {
|
||||
printf("E22 Join net group failed: %d!\n", ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: E22 send data function
|
||||
* @param adapter - Lora device pointer
|
||||
* @param buf - data buffers
|
||||
* @param len - data len
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int E22Send(struct Adapter *adapter, const void *buf, size_t len)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = PrivWrite(adapter->fd, (void *)buf, len);
|
||||
if(ret < 0){
|
||||
printf("send failed %d!\n", ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: E22 receive data function
|
||||
* @param adapter - Lora device pointer
|
||||
* @param buf - data buffers
|
||||
* @param len - data len
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int E22Recv(struct Adapter *adapter, void *buf, size_t len)
|
||||
{
|
||||
int recv_len, recv_len_continue;
|
||||
|
||||
uint8 *recv_buf = PrivMalloc(len);
|
||||
|
||||
recv_len = PrivRead(adapter->fd, recv_buf, len);
|
||||
if (recv_len) {
|
||||
while (recv_len < len) {
|
||||
recv_len_continue = PrivRead(adapter->fd, recv_buf + recv_len, len - recv_len);
|
||||
if (recv_len_continue) {
|
||||
recv_len += recv_len_continue;
|
||||
} else {
|
||||
recv_len = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
memcpy(buf, recv_buf, len);
|
||||
}
|
||||
|
||||
PrivFree(recv_buf);
|
||||
|
||||
return recv_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: E22 quit lora net group function
|
||||
* @param adapter - Lora device pointer
|
||||
* @param priv_net_group - priv_net_group params
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int E22Quit(struct Adapter *adapter, unsigned char *priv_net_group)
|
||||
{
|
||||
int ret;
|
||||
|
||||
struct AdapterData *priv_net_group_data = (struct AdapterData *)priv_net_group;
|
||||
|
||||
ret = PrivWrite(adapter->fd, (void *)priv_net_group_data->buffer, priv_net_group_data->len);
|
||||
if(ret < 0){
|
||||
printf("E22 quit net group failed %d!\n", ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const struct PrivProtocolDone e22_done =
|
||||
{
|
||||
.open = E22Open,
|
||||
.close = E22Close,
|
||||
.ioctl = E22Ioctl,
|
||||
.setup = NULL,
|
||||
.setdown = NULL,
|
||||
.setaddr = NULL,
|
||||
.setdns = NULL,
|
||||
.setdhcp = NULL,
|
||||
.ping = NULL,
|
||||
.netstat = NULL,
|
||||
.join = E22Join,
|
||||
.send = E22Send,
|
||||
.recv = E22Recv,
|
||||
.quit = E22Quit,
|
||||
};
|
||||
|
||||
AdapterProductInfoType E22Attach(struct Adapter *adapter)
|
||||
{
|
||||
struct AdapterProductInfo *product_info = malloc(sizeof(struct AdapterProductInfo));
|
||||
if (!product_info) {
|
||||
printf("E22Attach malloc product_info error\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strncpy(product_info->model_name, ADAPTER_LORA_E22,sizeof(product_info->model_name));
|
||||
product_info->model_done = (void *)&e22_done;
|
||||
|
||||
return product_info;
|
||||
}
|
||||
|
||||
//###################TEST####################
|
||||
static void LoraOpen(void)
|
||||
{
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_LORA_NAME);
|
||||
if (NULL == adapter) {
|
||||
printf("LoraReceive find lora adapter error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
E22Open(adapter);
|
||||
}
|
||||
|
||||
static void LoraRead(void *parameter)
|
||||
{
|
||||
int RevLen;
|
||||
int i, cnt = 0;
|
||||
|
||||
uint8 buffer[256];
|
||||
|
||||
memset(buffer, 0, 256);
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_LORA_NAME);
|
||||
if (NULL == adapter) {
|
||||
printf("LoraRead find lora adapter error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
printf("ready to read lora data\n");
|
||||
|
||||
RevLen = E22Recv(adapter, buffer, 256);
|
||||
if (RevLen) {
|
||||
printf("lora get data %u\n", RevLen);
|
||||
for (i = 0; i < RevLen; i ++) {
|
||||
printf("i %u data 0x%x\n", i, buffer[i]);
|
||||
}
|
||||
|
||||
memset(buffer, 0, 256);
|
||||
|
||||
PrivTaskDelay(1000);
|
||||
|
||||
cnt ++;
|
||||
E22Send(adapter, &cnt, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void E22LoraReceive(void)
|
||||
{
|
||||
int ret;
|
||||
pthread_t thread;
|
||||
pthread_attr_t attr = PTHREAD_ATTR_INITIALIZER;
|
||||
attr.priority = 80;
|
||||
attr.stacksize = 2048;
|
||||
|
||||
LoraOpen();
|
||||
|
||||
ret = PrivTaskCreate(&thread, &attr, (void*)LoraRead, NULL);
|
||||
if (ret < 0) {
|
||||
printf("task lora read create failed, status=%d\n", ret);
|
||||
return;
|
||||
}
|
||||
}
|
||||
void E22LoraSend(int argc, char *argv[])
|
||||
{
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_LORA_NAME);
|
||||
if (NULL == adapter) {
|
||||
printf("LoraRead find lora adapter error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (argc == 2) {
|
||||
char Msg[256] = {0};
|
||||
strncpy(Msg, argv[1], 256);
|
||||
|
||||
E22Open(adapter);
|
||||
E22Send(adapter, Msg, strlen(Msg));
|
||||
E22Close(adapter);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user