Skip to content

Commit

Permalink
WIP commit
Browse files Browse the repository at this point in the history
Added lorawan_send()
Checks whether the Ack is received from Gateway or not for OTAA. Note
LoRaMacProcess() in lorawan_join_network().

Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
  • Loading branch information
Mani-Sadhasivam committed Feb 15, 2020
1 parent 3dcd8e0 commit b617e35
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 23 deletions.
22 changes: 13 additions & 9 deletions drivers/lora/sx1276.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <zephyr.h>

#include <sx1276/sx1276.h>
#include <timer.h>

#define LOG_LEVEL CONFIG_LORA_LOG_LEVEL
#include <logging/log.h>
Expand All @@ -28,10 +29,11 @@ LOG_MODULE_REGISTER(sx1276);
#define BOARD_TCXO_WAKEUP_TIME 5

/* TODO: Use RTC backup */
static uint32_t backup_reg[2] = { 0 ,0 };
static uint32_t saved_time;
static volatile uint32_t backup_reg[2] = { 0 ,0 };
static volatile uint32_t saved_time;

extern DioIrqHandler *DioIrq[];
static struct k_timer lora_timer;

int sx1276_dio_pins[SX1276_MAX_DIO] = {
DT_INST_0_SEMTECH_SX1276_DIO_GPIOS_PIN_0,
Expand Down Expand Up @@ -115,6 +117,12 @@ void BoardCriticalSectionEnd(uint32_t *mask)
irq_unlock(*mask);
}

static void lora_callback(struct k_timer *timer)
{
k_timer_stop(&lora_timer);
TimerIrqHandler();
}

uint32_t RtcGetTimerValue(void)
{
return counter_read(dev_data.counter);
Expand All @@ -128,17 +136,12 @@ uint32_t RtcGetTimerElapsedTime(void)
u32_t RtcGetMinimumTimeout(void)
{
/* TODO: Get this value from counter driver */
return 3;
return 1;
}

void RtcSetAlarm(uint32_t timeout)
{
struct counter_alarm_cfg alarm_cfg;

alarm_cfg.flags = 0;
alarm_cfg.ticks = timeout;

counter_set_channel_alarm(dev_data.counter, 0, &alarm_cfg);
k_timer_start(&lora_timer, K_SECONDS(timeout), K_NO_WAIT);
}

uint32_t RtcSetTimerContext(void)
Expand Down Expand Up @@ -534,6 +537,7 @@ static int sx1276_lora_init(struct device *dev)

#ifdef CONFIG_LORAWAN
counter_start(dev_data.counter);
k_timer_init(&lora_timer, lora_callback, NULL);
#endif

k_sem_init(&dev_data.data_sem, 0, UINT_MAX);
Expand Down
18 changes: 18 additions & 0 deletions include/lorawan.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ enum lorawan_datarate {
LORAWAN_DR_15,
};

/* TODO: Remove if not req */
enum lorawan_mac_status {
LORAWAN_MAC_STATUS_OK,
LORAWAN_MAC_STATUS_BUSY,
LORAWAN_MAC_STATUS_SERVICE_UNKNOWN,
LORAWAN_MAC_STATUS_PARAMETER_INVALID,
LORAWAN_MAC_STATUS_FREQUENCY_INVALID,
LORAWAN_MAC_STATUS_DATARATE_INVALID,
LORAWAN_MAC_STATUS_FREQ_AND_DR_INVALID,
LORAWAN_MAC_STATUS_NO_NETWORK_JOINED,
LORAWAN_MAC_STATUS_LENGTH_ERROR,
LORAWAN_MAC_STATUS_DEVICE_OFF,
LORAWAN_MAC_STATUS_REGION_NOT_SUPPORTED,
};

struct lorawan_mib_config {
enum lorawan_class lw_class;
enum lorawan_datarate rx2_datarate;
Expand All @@ -57,8 +72,11 @@ struct lorawan_mib_config {
bool adr_enable;
};

/* TODO: Convert to APIs */
extern int lorawan_config(struct lorawan_mib_config *mib_config);
extern int lorawan_join_network(enum lorawan_datarate datarate,
enum lorawan_act_type mode);
extern int lorawan_send(u8_t port, enum lorawan_datarate datarate,
u8_t *data, u8_t len, bool confirm, u8_t tries);

#endif /* ZEPHYR_INCLUDE_LORAWAN_H_ */
2 changes: 1 addition & 1 deletion samples/drivers/counter/alarm/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void test_counter_interrupt_fn(struct device *counter_dev,
int err;

printk("!!! Alarm !!!\n");
printk("Now: %u\n", now_sec);
printk("Now: %u ticks: %d\n", now_sec, counter_read(counter_dev));

/* Set a new alarm with a double length duration */
config->ticks = config->ticks * 2U;
Expand Down
26 changes: 17 additions & 9 deletions samples/lorawan/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@

#define LORAWAN_DEV_EUI { 0xDD, 0xEE, 0xAA, 0xDD, 0xBB, 0xEE,\
0xEE, 0xFF }
#define LORAWAN_JOIN_EUI { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
0x00, 0x00 }
#define LORAWAN_APP_EUI { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
0x00, 0x00 }
#define LORAWAN_APP_KEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE,\
0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88,\
0x09, 0xCF, 0x4F, 0x3C }
#define LORAWAN_JOIN_EUI { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x02,\
0x4E, 0x50 }
#define LORAWAN_APP_EUI { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x02,\
0x4E, 0x50 }
#define LORAWAN_APP_KEY { 0xB3, 0x58, 0xA9, 0xB5, 0x7E, 0xBB,\
0x34, 0x3F, 0x2B, 0x72, 0x10, 0x11,\
0xAF, 0x1C, 0x50, 0x41 }
#define LORAWAN_DEFAULT_DATARATE LORAWAN_DR_0

#define MAX_DATA_LEN 10

#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(lora_receive);

char data[MAX_DATA_LEN] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'};

void main(void)
{
struct device *lora_dev;
Expand All @@ -34,7 +38,6 @@ void main(void)
u8_t join_eui[] = LORAWAN_JOIN_EUI;
u8_t app_eui[] = LORAWAN_APP_EUI;
u8_t app_key[] = LORAWAN_APP_KEY;
// u8_t data[MAX_DATA_LEN] = {0};

lora_dev = device_get_binding(DT_INST_0_SEMTECH_SX1276_LABEL);
if (!lora_dev) {
Expand All @@ -61,6 +64,11 @@ void main(void)
ret = lorawan_join_network(LORAWAN_DEFAULT_DATARATE, LORAWAN_ACT_OTAA);
if (ret < 0)
return;

ret = lorawan_send(2, LORAWAN_DEFAULT_DATARATE, data, MAX_DATA_LEN,
true, 5);
if (ret < 0)
return;

LOG_INF("Network join request sent!");
LOG_INF("Data sent!");
}
2 changes: 1 addition & 1 deletion subsys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

add_subdirectory(debug)
add_subdirectory(logging)
add_subdirectory(lorawan)
add_subdirectory_ifdef(CONFIG_LORA lorawan)
add_subdirectory_ifdef(CONFIG_BT bluetooth)
add_subdirectory_ifdef(CONFIG_CONSOLE_SUBSYS console)
add_subdirectory_ifdef(CONFIG_SHELL shell)
Expand Down
67 changes: 64 additions & 3 deletions subsys/lorawan/lorawan.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include <logging/log.h>
LOG_MODULE_REGISTER(lorawan);

K_SEM_DEFINE(lorawan_config_sem, 0, 1);

/* MAC status strings */
const char *to_status_str[] = {
"OK", /* LORAMAC_STATUS_OK */
Expand Down Expand Up @@ -101,7 +103,7 @@ static void McpsConfirm(McpsConfirm_t *mcpsConfirm)
{
if (mcpsConfirm->Status != LORAMAC_EVENT_INFO_STATUS_OK) {
LOG_ERR("McpsRequest failed : %s",
log_strdup(to_status_str[mcpsConfirm->Status]));
log_strdup(to_event_info_status_str[mcpsConfirm->Status]));
} else {
LOG_DBG("McpsRequest success!");
}
Expand All @@ -111,7 +113,7 @@ static void McpsIndication(McpsIndication_t *mcpsIndication)
{
if (mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK) {
LOG_ERR("McpsIndication failed : %s",
log_strdup(to_status_str[mcpsIndication->Status]));
log_strdup(to_event_info_status_str[mcpsIndication->Status]));
return;
}

Expand All @@ -131,7 +133,7 @@ static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )

if (mlmeConfirm->Status != LORAMAC_EVENT_INFO_STATUS_OK) {
LOG_ERR("McpsIndication failed : %s",
log_strdup(to_status_str[mlmeConfirm->Status]));
log_strdup(to_event_info_status_str[mlmeConfirm->Status]));
return;
}

Expand All @@ -147,6 +149,8 @@ static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
default:
break;
}

k_sem_give(&lorawan_config_sem);
}

static void MlmeIndication( MlmeIndication_t *mlmeIndication )
Expand Down Expand Up @@ -224,6 +228,63 @@ int lorawan_join_network(enum lorawan_datarate datarate, enum lorawan_act_type m
log_strdup(to_status_str[status]));
return -EINVAL;
}

LOG_INF("Network join request sent!");

/* HACK: See if we are getting Join Accept from server */
while(1) {
LoRaMacProcess();
k_sleep(500);
}

k_sem_take(&lorawan_config_sem, K_FOREVER);
}

return 0;
}

int lorawan_send(u8_t port, enum lorawan_datarate datarate, u8_t *data,
u8_t len, bool confirm, u8_t tries)
{
LoRaMacStatus_t status;
McpsReq_t mcpsReq;
LoRaMacTxInfo_t txInfo;

if (data == NULL) {
return -EINVAL;
}

/* TODO: Do we need to make a copy here? */
//memcpy(data, data, len);

if( LoRaMacQueryTxPossible( len, &txInfo ) != LORAMAC_STATUS_OK ) {
/* Send empty frame in order to flush MAC commands */
mcpsReq.Type = MCPS_UNCONFIRMED;
mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mcpsReq.Req.Unconfirmed.Datarate = DR_0;
} else {
if (confirm == false) {
mcpsReq.Type = MCPS_UNCONFIRMED;
mcpsReq.Req.Unconfirmed.fPort = port;
mcpsReq.Req.Unconfirmed.fBuffer = data;
mcpsReq.Req.Unconfirmed.fBufferSize = len;
mcpsReq.Req.Unconfirmed.Datarate = datarate;
} else {
mcpsReq.Type = MCPS_CONFIRMED;
mcpsReq.Req.Confirmed.fPort = port;
mcpsReq.Req.Confirmed.fBuffer = data;
mcpsReq.Req.Confirmed.fBufferSize = len;
mcpsReq.Req.Confirmed.NbTrials = tries;
mcpsReq.Req.Confirmed.Datarate = datarate;
}
}

status = LoRaMacMcpsRequest(&mcpsReq);
if (status != LORAMAC_STATUS_OK) {
LOG_ERR("LoRaWAN Send failed: %s",
log_strdup(to_status_str[status]));
return -EINVAL;
}

return 0;
Expand Down

0 comments on commit b617e35

Please sign in to comment.