From b21240fc10732d5dbe49d39df682a6bb1475d754 Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Tue, 14 May 2019 09:16:04 +0800 Subject: [PATCH 01/13] typo when define LORA_SET_LNAAGC --- LoRa/lora.h | 2 +- test-application/lora-ioctl.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LoRa/lora.h b/LoRa/lora.h index 2fcb413..2ff7de9 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -55,7 +55,7 @@ #define LORA_GET_POWER (_IOR(LORA_IOC_MAGIC, 5, int)) #define LORA_SET_LNA (_IOW(LORA_IOC_MAGIC, 6, int)) #define LORA_GET_LNA (_IOR(LORA_IOC_MAGIC, 7, int)) -#define LORA_SET_LNAAGC (_IOR(LORA_IOC_MAGIC, 8, int)) +#define LORA_SET_LNAAGC (_IOW(LORA_IOC_MAGIC, 8, int)) #define LORA_SET_SPRFACTOR (_IOW(LORA_IOC_MAGIC, 9, int)) #define LORA_GET_SPRFACTOR (_IOR(LORA_IOC_MAGIC, 10, int)) #define LORA_SET_BANDWIDTH (_IOW(LORA_IOC_MAGIC, 11, int)) diff --git a/test-application/lora-ioctl.h b/test-application/lora-ioctl.h index 287346a..0de8c7a 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -52,7 +52,7 @@ #define LORA_GET_POWER (_IOR(LORA_IOC_MAGIC, 5, int)) #define LORA_SET_LNA (_IOW(LORA_IOC_MAGIC, 6, int)) #define LORA_GET_LNA (_IOR(LORA_IOC_MAGIC, 7, int)) -#define LORA_SET_LNAAGC (_IOR(LORA_IOC_MAGIC, 8, int)) +#define LORA_SET_LNAAGC (_IOW(LORA_IOC_MAGIC, 8, int)) #define LORA_SET_SPRFACTOR (_IOW(LORA_IOC_MAGIC, 9, int)) #define LORA_GET_SPRFACTOR (_IOR(LORA_IOC_MAGIC, 10, int)) #define LORA_SET_BANDWIDTH (_IOW(LORA_IOC_MAGIC, 11, int)) From b7bfa3fa0f432b3ecaaaf4c98341b4289350d3d3 Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Wed, 29 May 2019 10:18:07 +0800 Subject: [PATCH 02/13] Add ioctl LORA_SET_CRC --- LoRa/lora.c | 5 +++++ LoRa/lora.h | 3 +++ LoRa/spi-sx1278.c | 27 +++++++++++++++++++++++++++ test-application/lora-ioctl.c | 6 ++++++ test-application/lora-ioctl.h | 4 ++++ 5 files changed, 45 insertions(+) diff --git a/LoRa/lora.c b/LoRa/lora.c index 18901e9..66c0972 100644 --- a/LoRa/lora.c +++ b/LoRa/lora.c @@ -260,6 +260,11 @@ file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (lrdata->ops->getSNR != NULL) ret = lrdata->ops->getSNR(lrdata, pval); break; + /* Enable CRC generation and check on received payload. */ + case LORA_SET_CRC: + if (lrdata->ops->setCRC != NULL) + ret = lrdata->ops->setCRC(lrdata, pval); + break; default: ret = -ENOTTY; } diff --git a/LoRa/lora.h b/LoRa/lora.h index 2ff7de9..c94ab29 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -62,6 +62,7 @@ #define LORA_GET_BANDWIDTH (_IOR(LORA_IOC_MAGIC, 12, int)) #define LORA_GET_RSSI (_IOR(LORA_IOC_MAGIC, 13, int)) #define LORA_GET_SNR (_IOR(LORA_IOC_MAGIC, 14, int)) +#define LORA_SET_CRC (_IOW(LORA_IOC_MAGIC, 15, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -98,6 +99,8 @@ struct lora_operations { long (*getRSSI)(struct lora_struct *, void __user *); /* Get last packet's SNR. */ long (*getSNR)(struct lora_struct *, void __user *); + /* Enable CRC generation and check on received payload. */ + long (*setCRC)(struct lora_struct *, void __user *); /* Read from the LoRa device's communication. */ ssize_t (*read)(struct lora_struct *, const char __user *, size_t); /* Write to the LoRa device's communication. */ diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index 20867b8..c22021b 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -1590,6 +1590,32 @@ loraspi_getsnr(struct lora_struct *lrdata, void __user *arg) return 0; } +/** + * loraspi_setCRC - Enable CRC generation and check on received payload + * @lrdata: LoRa device + * @arg: the buffer holding the check or not check in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_setCRC(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t crc32; + uint8_t crc; + + rm = lrdata->lora_device; + status = copy_from_user(&crc32, arg, sizeof(uint32_t)); + + crc = (crc32 == 1) ? 1 : 0; + mutex_lock(&(lrdata->buf_lock)); + sx127X_setLoRaCRC(rm, crc); + mutex_unlock(&(lrdata->buf_lock)); + + return 0; +} + /** * loraspi_ready2write - Is ready to be written * @lrdata: LoRa device @@ -1657,6 +1683,7 @@ struct lora_operations lrops = { .getBW = loraspi_getbandwidth, .getRSSI = loraspi_getrssi, .getSNR = loraspi_getsnr, + .setCRC = loraspi_setCRC, .ready2write = loraspi_ready2write, .ready2read = loraspi_ready2read, }; diff --git a/test-application/lora-ioctl.c b/test-application/lora-ioctl.c index 10413ff..fa50d06 100644 --- a/test-application/lora-ioctl.c +++ b/test-application/lora-ioctl.c @@ -177,3 +177,9 @@ uint32_t get_bw(int fd) return bw; } + +/* Enable CRC generation and check on received payload. */ +void set_crc(int fd, uint32_t agc) +{ + ioctl(fd, LORA_SET_CRC, &agc); +} diff --git a/test-application/lora-ioctl.h b/test-application/lora-ioctl.h index 0de8c7a..61ac36c 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -59,6 +59,7 @@ #define LORA_GET_BANDWIDTH (_IOR(LORA_IOC_MAGIC, 12, int)) #define LORA_GET_RSSI (_IOR(LORA_IOC_MAGIC, 13, int)) #define LORA_GET_SNR (_IOR(LORA_IOC_MAGIC, 14, int)) +#define LORA_SET_CRC (_IOW(LORA_IOC_MAGIC, 15, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -106,4 +107,7 @@ uint32_t get_sprfactor(int fd); void set_bw(int fd, uint32_t bw); uint32_t get_bw(int fd); +/* Enable CRC generation and check on received payload. */ +void set_crc(int fd, uint32_t crc); + #endif From 4f1a06a1cb6cce9670cb29026475a50d8651eb5e Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Tue, 14 May 2019 16:47:24 +0800 Subject: [PATCH 03/13] Add ioctl LORA_SET_CODINGRATE LORA_GET_CODINGRATE --- LoRa/lora.c | 9 ++++++ LoRa/lora.h | 5 ++++ LoRa/spi-sx1278.c | 55 +++++++++++++++++++++++++++++++++++ test-application/lora-ioctl.c | 14 +++++++++ test-application/lora-ioctl.h | 6 ++++ 5 files changed, 89 insertions(+) diff --git a/LoRa/lora.c b/LoRa/lora.c index 66c0972..fc1c36a 100644 --- a/LoRa/lora.c +++ b/LoRa/lora.c @@ -265,6 +265,15 @@ file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (lrdata->ops->setCRC != NULL) ret = lrdata->ops->setCRC(lrdata, pval); break; + /* Set & get LoRa package's coding rate. */ + case LORA_SET_CODINGRATE: + if (lrdata->ops->setCodingRate != NULL) + ret = lrdata->ops->setCodingRate(lrdata, pval); + break; + case LORA_GET_CODINGRATE: + if (lrdata->ops->getCodingRate != NULL) + ret = lrdata->ops->getCodingRate(lrdata, pval); + break; default: ret = -ENOTTY; } diff --git a/LoRa/lora.h b/LoRa/lora.h index c94ab29..6e4d884 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -63,6 +63,8 @@ #define LORA_GET_RSSI (_IOR(LORA_IOC_MAGIC, 13, int)) #define LORA_GET_SNR (_IOR(LORA_IOC_MAGIC, 14, int)) #define LORA_SET_CRC (_IOW(LORA_IOC_MAGIC, 15, int)) +#define LORA_SET_CODINGRATE (_IOW(LORA_IOC_MAGIC, 16, int)) +#define LORA_GET_CODINGRATE (_IOR(LORA_IOC_MAGIC, 17, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -101,6 +103,9 @@ struct lora_operations { long (*getSNR)(struct lora_struct *, void __user *); /* Enable CRC generation and check on received payload. */ long (*setCRC)(struct lora_struct *, void __user *); + /* Set & get LoRa package's coding rate. */ + long (*setCodingRate)(struct lora_struct *, void __user *); + long (*getCodingRate)(struct lora_struct *, void __user *); /* Read from the LoRa device's communication. */ ssize_t (*read)(struct lora_struct *, const char __user *, size_t); /* Write to the LoRa device's communication. */ diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index c22021b..7b6b18a 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -1616,6 +1616,59 @@ loraspi_setCRC(struct lora_struct *lrdata, void __user *arg) return 0; } +/** + * loraspi_setcodingrate - Set LoRa package's coding rate + * @lrdata: LoRa device + * @arg: the buffer holding the LoRa package's coding rate value in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_setcodingrate(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t codingrate32; + uint8_t codingrate; + + rm = lrdata->lora_device; + status = copy_from_user(&codingrate32, arg, sizeof(uint32_t)); + + codingrate = codingrate32; + mutex_lock(&(lrdata->buf_lock)); + sx127X_setLoRaCR(rm, codingrate); + mutex_unlock(&(lrdata->buf_lock)); + + return 0; +} + +/** + * loraspi_getcodingrate - Get LoRa package's coding rate + * @lrdata: LoRa device + * @arg: the buffer going to hold the LoRa package's coding rate value in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_getcodingrate(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t codingrate32; + uint8_t codingrate; + + rm = lrdata->lora_device; + + mutex_lock(&(lrdata->buf_lock)); + codingrate = sx127X_getLoRaCR(rm); + mutex_unlock(&(lrdata->buf_lock)); + + codingrate32 = codingrate; + status = copy_to_user(arg, &codingrate32, sizeof(uint32_t)); + + return 0; +} + /** * loraspi_ready2write - Is ready to be written * @lrdata: LoRa device @@ -1684,6 +1737,8 @@ struct lora_operations lrops = { .getRSSI = loraspi_getrssi, .getSNR = loraspi_getsnr, .setCRC = loraspi_setCRC, + .setCodingRate = loraspi_setcodingrate, + .getCodingRate = loraspi_getcodingrate, .ready2write = loraspi_ready2write, .ready2read = loraspi_ready2read, }; diff --git a/test-application/lora-ioctl.c b/test-application/lora-ioctl.c index fa50d06..61f8acb 100644 --- a/test-application/lora-ioctl.c +++ b/test-application/lora-ioctl.c @@ -183,3 +183,17 @@ void set_crc(int fd, uint32_t agc) { ioctl(fd, LORA_SET_CRC, &agc); } + +/* Set & get LoRa package's coding rate. */ +void set_codingrate(int fd, uint32_t codingrate) +{ + ioctl(fd, LORA_SET_CODINGRATE, &codingrate); +} + +uint32_t get_codingrate(int fd) { + uint32_t codingrate; + + ioctl(fd, LORA_GET_CODINGRATE, &codingrate); + + return codingrate; +} diff --git a/test-application/lora-ioctl.h b/test-application/lora-ioctl.h index 61ac36c..b7f2e0d 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -60,6 +60,8 @@ #define LORA_GET_RSSI (_IOR(LORA_IOC_MAGIC, 13, int)) #define LORA_GET_SNR (_IOR(LORA_IOC_MAGIC, 14, int)) #define LORA_SET_CRC (_IOW(LORA_IOC_MAGIC, 15, int)) +#define LORA_SET_CODINGRATE (_IOW(LORA_IOC_MAGIC, 16, int)) +#define LORA_GET_CODINGRATE (_IOR(LORA_IOC_MAGIC, 17, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -110,4 +112,8 @@ uint32_t get_bw(int fd); /* Enable CRC generation and check on received payload. */ void set_crc(int fd, uint32_t crc); +/* Set & get LoRa package's coding rate. */ +void set_codingrate(int fd, uint32_t codingrate); +uint32_t get_codingrate(int fd); + #endif From 6e38d3adb3537ce0ef44f1f9672e476180f60ef1 Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Wed, 15 May 2019 08:06:27 +0800 Subject: [PATCH 04/13] Add ioctl LORA_SET_IMPLICIT --- LoRa/lora.c | 5 +++++ LoRa/lora.h | 3 +++ LoRa/spi-sx1278.c | 27 +++++++++++++++++++++++++++ test-application/lora-ioctl.c | 6 ++++++ test-application/lora-ioctl.h | 4 ++++ 5 files changed, 45 insertions(+) diff --git a/LoRa/lora.c b/LoRa/lora.c index fc1c36a..614e78d 100644 --- a/LoRa/lora.c +++ b/LoRa/lora.c @@ -274,6 +274,11 @@ file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (lrdata->ops->getCodingRate != NULL) ret = lrdata->ops->getCodingRate(lrdata, pval); break; + /* Set LoRa packages in Explicit / Implicit Header Mode. */ + case LORA_SET_IMPLICIT: + if (lrdata->ops->setImplicit != NULL) + ret = lrdata->ops->setImplicit(lrdata, pval); + break; default: ret = -ENOTTY; } diff --git a/LoRa/lora.h b/LoRa/lora.h index 6e4d884..c80743a 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -65,6 +65,7 @@ #define LORA_SET_CRC (_IOW(LORA_IOC_MAGIC, 15, int)) #define LORA_SET_CODINGRATE (_IOW(LORA_IOC_MAGIC, 16, int)) #define LORA_GET_CODINGRATE (_IOR(LORA_IOC_MAGIC, 17, int)) +#define LORA_SET_IMPLICIT (_IOW(LORA_IOC_MAGIC, 18, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -106,6 +107,8 @@ struct lora_operations { /* Set & get LoRa package's coding rate. */ long (*setCodingRate)(struct lora_struct *, void __user *); long (*getCodingRate)(struct lora_struct *, void __user *); + /* Set LoRa packages in Explicit / Implicit Header Mode. */ + long (*setImplicit)(struct lora_struct *, void __user *); /* Read from the LoRa device's communication. */ ssize_t (*read)(struct lora_struct *, const char __user *, size_t); /* Write to the LoRa device's communication. */ diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index 7b6b18a..14e0492 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -1669,6 +1669,32 @@ loraspi_getcodingrate(struct lora_struct *lrdata, void __user *arg) return 0; } +/** + * loraspi_setImplicit - Set LoRa packages in Explicit / Implicit Header Mode + * @lrdata: LoRa device + * @arg: the buffer holding the check or not check in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_setImplicit(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t implicit32; + uint8_t implicit; + + rm = lrdata->lora_device; + status = copy_from_user(&implicit32, arg, sizeof(uint32_t)); + + implicit = (implicit32 == 1) ? 1 : 0; + mutex_lock(&(lrdata->buf_lock)); + sx127X_setLoRaImplicit(rm, implicit); + mutex_unlock(&(lrdata->buf_lock)); + + return 0; +} + /** * loraspi_ready2write - Is ready to be written * @lrdata: LoRa device @@ -1739,6 +1765,7 @@ struct lora_operations lrops = { .setCRC = loraspi_setCRC, .setCodingRate = loraspi_setcodingrate, .getCodingRate = loraspi_getcodingrate, + .setImplicit = loraspi_setImplicit, .ready2write = loraspi_ready2write, .ready2read = loraspi_ready2read, }; diff --git a/test-application/lora-ioctl.c b/test-application/lora-ioctl.c index 61f8acb..dbacf1a 100644 --- a/test-application/lora-ioctl.c +++ b/test-application/lora-ioctl.c @@ -197,3 +197,9 @@ uint32_t get_codingrate(int fd) { return codingrate; } + +/* Set LoRa packages in Explicit / Implicit Header Mode. */ +void set_implicit(int fd, uint32_t implicit) +{ + ioctl(fd, LORA_SET_IMPLICIT, &implicit); +} diff --git a/test-application/lora-ioctl.h b/test-application/lora-ioctl.h index b7f2e0d..62c041a 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -62,6 +62,7 @@ #define LORA_SET_CRC (_IOW(LORA_IOC_MAGIC, 15, int)) #define LORA_SET_CODINGRATE (_IOW(LORA_IOC_MAGIC, 16, int)) #define LORA_GET_CODINGRATE (_IOR(LORA_IOC_MAGIC, 17, int)) +#define LORA_SET_IMPLICIT (_IOW(LORA_IOC_MAGIC, 18, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -116,4 +117,7 @@ void set_crc(int fd, uint32_t crc); void set_codingrate(int fd, uint32_t codingrate); uint32_t get_codingrate(int fd); +/* Set LoRa packages in Explicit / Implicit Header Mode. */ +void set_implicit(int fd, uint32_t implicit); + #endif From 5042b801cc35a11d8045c5974c5f1f25eb89d8ef Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Wed, 15 May 2019 08:56:42 +0800 Subject: [PATCH 05/13] Add ioctl LORA_SET_LDRO --- LoRa/lora.c | 5 +++++ LoRa/lora.h | 3 +++ LoRa/spi-sx1278.c | 42 +++++++++++++++++++++++++++++++++++ test-application/lora-ioctl.c | 6 +++++ test-application/lora-ioctl.h | 4 ++++ 5 files changed, 60 insertions(+) diff --git a/LoRa/lora.c b/LoRa/lora.c index 614e78d..ade37c0 100644 --- a/LoRa/lora.c +++ b/LoRa/lora.c @@ -279,6 +279,11 @@ file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (lrdata->ops->setImplicit != NULL) ret = lrdata->ops->setImplicit(lrdata, pval); break; + /* Set RF low data rate optimize. */ + case LORA_SET_LDRO: + if (lrdata->ops->setLDRO != NULL) + ret = lrdata->ops->setLDRO(lrdata, pval); + break; default: ret = -ENOTTY; } diff --git a/LoRa/lora.h b/LoRa/lora.h index c80743a..fd4579f 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -66,6 +66,7 @@ #define LORA_SET_CODINGRATE (_IOW(LORA_IOC_MAGIC, 16, int)) #define LORA_GET_CODINGRATE (_IOR(LORA_IOC_MAGIC, 17, int)) #define LORA_SET_IMPLICIT (_IOW(LORA_IOC_MAGIC, 18, int)) +#define LORA_SET_LDRO (_IOW(LORA_IOC_MAGIC, 19, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -109,6 +110,8 @@ struct lora_operations { long (*getCodingRate)(struct lora_struct *, void __user *); /* Set LoRa packages in Explicit / Implicit Header Mode. */ long (*setImplicit)(struct lora_struct *, void __user *); + /* Set RF low data rate optimize. */ + long (*setLDRO)(struct lora_struct *, void __user *); /* Read from the LoRa device's communication. */ ssize_t (*read)(struct lora_struct *, const char __user *, size_t); /* Write to the LoRa device's communication. */ diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index 14e0492..b2fb6b3 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -903,6 +903,21 @@ sx127X_setBoost(struct regmap *rm, uint8_t yesno) regmap_raw_write(rm, SX127X_REG_PA_CONFIG, &pacf, 1); } +/** + * sx127X_setLoRaLDRO - Set RF low data rate optimize + * @rm: the device as a regmap to communicate with + * @yesno: 1 / 0 for check / not check + */ +void +sx127X_setLoRaLDRO(struct regmap *rm, int32_t yesno) +{ + uint8_t mcf3; + + regmap_raw_read(rm, SX127X_REG_MODEM_CONFIG3, &mcf3, 1); + mcf3 = (yesno) ? (mcf3 | 0x08) : (mcf3 & (~0x08)); + regmap_raw_write(rm, SX127X_REG_MODEM_CONFIG3, &mcf3, 1); +} + /** * sx127X_startLoRaMode - Start the device and set it in LoRa mode * @rm: the device as a regmap to communicate with @@ -1695,6 +1710,32 @@ loraspi_setImplicit(struct lora_struct *lrdata, void __user *arg) return 0; } +/** + * loraspi_setLDRO - Set RF low data rate optimize + * @lrdata: LoRa device + * @arg: the buffer holding the check or not check in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_setLDRO(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t ldro32; + uint8_t ldro; + + rm = lrdata->lora_device; + status = copy_from_user(&ldro32, arg, sizeof(uint32_t)); + + ldro = (ldro32 == 1) ? 1 : 0; + mutex_lock(&(lrdata->buf_lock)); + sx127X_setLoRaLDRO(rm, ldro); + mutex_unlock(&(lrdata->buf_lock)); + + return 0; +} + /** * loraspi_ready2write - Is ready to be written * @lrdata: LoRa device @@ -1766,6 +1807,7 @@ struct lora_operations lrops = { .setCodingRate = loraspi_setcodingrate, .getCodingRate = loraspi_getcodingrate, .setImplicit = loraspi_setImplicit, + .setLDRO = loraspi_setLDRO, .ready2write = loraspi_ready2write, .ready2read = loraspi_ready2read, }; diff --git a/test-application/lora-ioctl.c b/test-application/lora-ioctl.c index dbacf1a..66f16cd 100644 --- a/test-application/lora-ioctl.c +++ b/test-application/lora-ioctl.c @@ -203,3 +203,9 @@ void set_implicit(int fd, uint32_t implicit) { ioctl(fd, LORA_SET_IMPLICIT, &implicit); } + +/* Set LoRa packages in Explicit / Implicit Header Mode. */ +void set_ldro(int fd, uint32_t ldro) +{ + ioctl(fd, LORA_SET_LDRO, &ldro); +} diff --git a/test-application/lora-ioctl.h b/test-application/lora-ioctl.h index 62c041a..69e9c96 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -63,6 +63,7 @@ #define LORA_SET_CODINGRATE (_IOW(LORA_IOC_MAGIC, 16, int)) #define LORA_GET_CODINGRATE (_IOR(LORA_IOC_MAGIC, 17, int)) #define LORA_SET_IMPLICIT (_IOW(LORA_IOC_MAGIC, 18, int)) +#define LORA_SET_LDRO (_IOW(LORA_IOC_MAGIC, 19, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -120,4 +121,7 @@ uint32_t get_codingrate(int fd); /* Set LoRa packages in Explicit / Implicit Header Mode. */ void set_implicit(int fd, uint32_t implicit); +/* Set RF low data rate optimize. */ +void set_ldro(int fd, uint32_t ldro); + #endif From 237bef936e0b5657bd3a3d132921920390d0702f Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Wed, 15 May 2019 09:37:35 +0800 Subject: [PATCH 06/13] Add ioctl LORA_SET_PREAMBLE LORA_GET_PREAMBLE --- LoRa/lora.c | 9 +++++++ LoRa/lora.h | 5 ++++ LoRa/spi-sx1278.c | 51 +++++++++++++++++++++++++++++++++++ test-application/lora-ioctl.c | 14 ++++++++++ test-application/lora-ioctl.h | 6 +++++ 5 files changed, 85 insertions(+) diff --git a/LoRa/lora.c b/LoRa/lora.c index ade37c0..e630325 100644 --- a/LoRa/lora.c +++ b/LoRa/lora.c @@ -284,6 +284,15 @@ file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (lrdata->ops->setLDRO != NULL) ret = lrdata->ops->setLDRO(lrdata, pval); break; + /* Set & get LoRa preamble length. */ + case LORA_SET_PREAMBLE: + if (lrdata->ops->setPreambleLen != NULL) + ret = lrdata->ops->setPreambleLen(lrdata, pval); + break; + case LORA_GET_PREAMBLE: + if (lrdata->ops->getPreambleLen != NULL) + ret = lrdata->ops->getPreambleLen(lrdata, pval); + break; default: ret = -ENOTTY; } diff --git a/LoRa/lora.h b/LoRa/lora.h index fd4579f..2442c44 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -67,6 +67,8 @@ #define LORA_GET_CODINGRATE (_IOR(LORA_IOC_MAGIC, 17, int)) #define LORA_SET_IMPLICIT (_IOW(LORA_IOC_MAGIC, 18, int)) #define LORA_SET_LDRO (_IOW(LORA_IOC_MAGIC, 19, int)) +#define LORA_SET_PREAMBLE (_IOW(LORA_IOC_MAGIC, 20, int)) +#define LORA_GET_PREAMBLE (_IOR(LORA_IOC_MAGIC, 21, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -112,6 +114,9 @@ struct lora_operations { long (*setImplicit)(struct lora_struct *, void __user *); /* Set RF low data rate optimize. */ long (*setLDRO)(struct lora_struct *, void __user *); + /* Set & get LoRa preamble length. */ + long (*setPreambleLen)(struct lora_struct *, void __user *); + long (*getPreambleLen)(struct lora_struct *, void __user *); /* Read from the LoRa device's communication. */ ssize_t (*read)(struct lora_struct *, const char __user *, size_t); /* Write to the LoRa device's communication. */ diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index b2fb6b3..4d7a447 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -1736,6 +1736,55 @@ loraspi_setLDRO(struct lora_struct *lrdata, void __user *arg) return 0; } +/** + * loraspi_setPreambleLen - Set LoRa preamble length + * @lrdata: LoRa device + * @arg: the buffer holding the LoRa preamble length value in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_setPreambleLen(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t len; + + rm = lrdata->lora_device; + status = copy_from_user(&len, arg, sizeof(uint32_t)); + + mutex_lock(&(lrdata->buf_lock)); + sx127X_setLoRaPreambleLen(rm, len); + mutex_unlock(&(lrdata->buf_lock)); + + return 0; +} + +/** + * loraspi_getPreambleLen - Get LoRa preamble length + * @lrdata: LoRa device + * @arg: the buffer going to hold the LoRa preamble length value in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_getPreambleLen(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t len; + + rm = lrdata->lora_device; + + mutex_lock(&(lrdata->buf_lock)); + len = sx127X_getLoRaCR(rm); + mutex_unlock(&(lrdata->buf_lock)); + + status = copy_to_user(arg, &len, sizeof(uint32_t)); + + return 0; +} + /** * loraspi_ready2write - Is ready to be written * @lrdata: LoRa device @@ -1807,6 +1856,8 @@ struct lora_operations lrops = { .setCodingRate = loraspi_setcodingrate, .getCodingRate = loraspi_getcodingrate, .setImplicit = loraspi_setImplicit, + .setPreambleLen = loraspi_setPreambleLen, + .getPreambleLen = loraspi_getPreambleLen, .setLDRO = loraspi_setLDRO, .ready2write = loraspi_ready2write, .ready2read = loraspi_ready2read, diff --git a/test-application/lora-ioctl.c b/test-application/lora-ioctl.c index 66f16cd..0d3f9b8 100644 --- a/test-application/lora-ioctl.c +++ b/test-application/lora-ioctl.c @@ -209,3 +209,17 @@ void set_ldro(int fd, uint32_t ldro) { ioctl(fd, LORA_SET_LDRO, &ldro); } + +/* Set & get LoRa preamble length. */ +void set_preamblelen(int fd, uint32_t preamblelen) +{ + ioctl(fd, LORA_SET_PREAMBLE, &preamblelen); +} + +uint32_t get_preamblelen(int fd) { + uint32_t preamblelen; + + ioctl(fd, LORA_GET_PREAMBLE, &preamblelen); + + return preamblelen; +} diff --git a/test-application/lora-ioctl.h b/test-application/lora-ioctl.h index 69e9c96..c22604f 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -64,6 +64,8 @@ #define LORA_GET_CODINGRATE (_IOR(LORA_IOC_MAGIC, 17, int)) #define LORA_SET_IMPLICIT (_IOW(LORA_IOC_MAGIC, 18, int)) #define LORA_SET_LDRO (_IOW(LORA_IOC_MAGIC, 19, int)) +#define LORA_SET_PREAMBLE (_IOW(LORA_IOC_MAGIC, 20, int)) +#define LORA_GET_PREAMBLE (_IOR(LORA_IOC_MAGIC, 21, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -124,4 +126,8 @@ void set_implicit(int fd, uint32_t implicit); /* Set RF low data rate optimize. */ void set_ldro(int fd, uint32_t ldro); +/* Set & get LoRa preamble length. */ +void set_preamblelen(int fd, uint32_t preamblelen); +uint32_t get_preamblelen(int fd); + #endif From 205371fd9ba6534502760ae3768b0250ab2e96eb Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Tue, 28 May 2019 09:11:48 +0800 Subject: [PATCH 07/13] Fix bandwidth will be set to 0000 in sx127X_setLoRaCR() --- LoRa/spi-sx1278.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index 4d7a447..8d13a35 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -585,7 +585,7 @@ sx127X_setLoRaCR(struct regmap *rm, uint8_t cr) uint8_t mcf1; regmap_raw_read(rm, SX127X_REG_MODEM_CONFIG1, &mcf1, 1); - mcf1 = (mcf1 & 0x0E) | (((cr & 0xF) - 4) << 1); + mcf1 = (mcf1 & 0xFE) | (((cr & 0xF) - 4) << 1); regmap_raw_write(rm, SX127X_REG_MODEM_CONFIG1, &mcf1, 1); } From 8d53f3c9a61f29d4c4cb5b5fce5c34ab1da86b58 Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Tue, 28 May 2019 17:13:34 +0800 Subject: [PATCH 08/13] Add ioctl LORA_SET_PARAMP LORA_GET_PARAMP --- LoRa/lora.c | 9 +++ LoRa/lora.h | 5 ++ LoRa/spi-sx1278.c | 109 ++++++++++++++++++++++++++++++++++ test-application/lora-ioctl.c | 15 +++++ test-application/lora-ioctl.h | 6 ++ 5 files changed, 144 insertions(+) diff --git a/LoRa/lora.c b/LoRa/lora.c index e630325..044e71a 100644 --- a/LoRa/lora.c +++ b/LoRa/lora.c @@ -218,6 +218,15 @@ file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (lrdata->ops->getPower != NULL) ret = lrdata->ops->getPower(lrdata, pval); break; + /* Set & get the RF power rise/fall time of ramp up/down. */ + case LORA_SET_PARAMP: + if (lrdata->ops->setPaRamp != NULL) + ret = lrdata->ops->setPaRamp(lrdata, pval); + break; + case LORA_GET_PARAMP: + if (lrdata->ops->getPaRamp != NULL) + ret = lrdata->ops->getPaRamp(lrdata, pval); + break; /* Set & get the LNA gain. */ case LORA_SET_LNA: if (lrdata->ops->setLNA != NULL) diff --git a/LoRa/lora.h b/LoRa/lora.h index 2442c44..64371cf 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -69,6 +69,8 @@ #define LORA_SET_LDRO (_IOW(LORA_IOC_MAGIC, 19, int)) #define LORA_SET_PREAMBLE (_IOW(LORA_IOC_MAGIC, 20, int)) #define LORA_GET_PREAMBLE (_IOR(LORA_IOC_MAGIC, 21, int)) +#define LORA_SET_PARAMP (_IOW(LORA_IOC_MAGIC, 22, int)) +#define LORA_GET_PARAMP (_IOR(LORA_IOC_MAGIC, 23, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -90,6 +92,9 @@ struct lora_operations { /* Set & get the PA power. */ long (*setPower)(struct lora_struct *, void __user *); long (*getPower)(struct lora_struct *, void __user *); + /* Set & get the RF power rise/fall time of ramp up/down. */ + long (*setPaRamp)(struct lora_struct *, void __user *); + long (*getPaRamp)(struct lora_struct *, void __user *); /* Set & get the LNA gain. */ long (*setLNA)(struct lora_struct *, void __user *); long (*getLNA)(struct lora_struct *, void __user *); diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index 8d13a35..98d50a6 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -366,6 +366,64 @@ sx127X_getLoRaPower(struct regmap *rm) return pout; } +const uint32_t ramp_us[] = { + 3400, + 2000, + 1000, + 500, + 250, + 125, + 100, + 62, + 50, + 40, + 31, + 25, + 20, + 15, + 12, + 10 +}; + +/** + * sx127X_setLoRaPaRamp - Set RF power rise/fall time of ramp up/down + * @rm: the device as a regmap to communicate with + * @us: RF power rise/fall time going to be assigned in us + */ +void +sx127X_setLoRaPaRamp(struct regmap *rm, uint32_t us) +{ + uint8_t i; + uint8_t paramp; + + for (i = 0; i < 15; i++) { + if (ramp_us[i] <= us) + break; + } + + regmap_raw_read(rm, SX127X_REG_PA_RAMP, ¶mp, 1); + paramp = (paramp & 0xF0) | i; + regmap_raw_write(rm, SX127X_REG_PA_RAMP, ¶mp, 1); +} + +/** + * sx127X_getLoRaPaRamp - Get RF power rise/fall time of ramp up/down + * @rm: the device as a regmap to communicate with + * + * Return: RF power rise/fall time in us + */ +uint32_t +sx127X_getLoRaPaRamp(struct regmap *rm) +{ + uint8_t paramp; + uint8_t us; + + regmap_raw_read(rm, SX127X_REG_PA_RAMP, ¶mp, 1); + us = paramp & 0x0F; + + return ramp_us[us]; +} + int8_t lna_gain[] = { 0, -6, @@ -1376,6 +1434,55 @@ loraspi_getpower(struct lora_struct *lrdata, void __user *arg) return 0; } +/** + * loraspi_setparamp - Set the RF power rise/fall time of ramp up/down + * @lrdata: LoRa device + * @arg: the buffer holding the RF power rise/fall time value in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_setparamp(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t paramp; + + rm = lrdata->lora_device; + status = copy_from_user(¶mp, arg, sizeof(uint32_t)); + + mutex_lock(&(lrdata->buf_lock)); + sx127X_setLoRaPaRamp(rm, paramp); + mutex_unlock(&(lrdata->buf_lock)); + + return 0; +} + +/** + * loraspi_getparamp - Get the RF power rise/fall time of ramp up/down + * @lrdata: LoRa device + * @arg: the buffer going to hold the RF power rise/fall time value in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_getparamp(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t paramp; + + rm = lrdata->lora_device; + + mutex_lock(&(lrdata->buf_lock)); + paramp = sx127X_getLoRaPaRamp(rm); + mutex_unlock(&(lrdata->buf_lock)); + + status = copy_to_user(arg, ¶mp, sizeof(uint32_t)); + + return 0; +} + /** * loraspi_setLNA - Set the LNA gain * @lrdata: LoRa device @@ -1843,6 +1950,8 @@ struct lora_operations lrops = { .getFreq = loraspi_getfreq, .setPower = loraspi_setpower, .getPower = loraspi_getpower, + .setPaRamp = loraspi_setparamp, + .getPaRamp = loraspi_getparamp, .setLNA = loraspi_setLNA, .getLNA = loraspi_getLNA, .setLNAAGC = loraspi_setLNAAGC, diff --git a/test-application/lora-ioctl.c b/test-application/lora-ioctl.c index 0d3f9b8..52efdc0 100644 --- a/test-application/lora-ioctl.c +++ b/test-application/lora-ioctl.c @@ -127,6 +127,21 @@ int32_t get_power(int fd) return power; } +/* Set & get the RF power rise/fall time of ramp up/down. */ +void set_paramp(int fd, uint32_t us) +{ + ioctl(fd, LORA_SET_PARAMP, &us); +} + +uint32_t get_paramp(int fd) +{ + uint32_t us; + + ioctl(fd, LORA_GET_PARAMP, &us); + + return us; +} + /* Set & get LNA gain. */ void set_lna(int fd, int32_t lna) { diff --git a/test-application/lora-ioctl.h b/test-application/lora-ioctl.h index c22604f..2a50b97 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -66,6 +66,8 @@ #define LORA_SET_LDRO (_IOW(LORA_IOC_MAGIC, 19, int)) #define LORA_SET_PREAMBLE (_IOW(LORA_IOC_MAGIC, 20, int)) #define LORA_GET_PREAMBLE (_IOR(LORA_IOC_MAGIC, 21, int)) +#define LORA_SET_PARAMP (_IOW(LORA_IOC_MAGIC, 22, int)) +#define LORA_GET_PARAMP (_IOR(LORA_IOC_MAGIC, 23, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -98,6 +100,10 @@ int32_t get_snr(int fd); void set_power(int fd, int32_t power); int32_t get_power(int fd); +/* Set & get the RF power rise/fall time of ramp up/down. */ +void set_paramp(int fd, uint32_t us); +uint32_t get_paramp(int fd); + /* Set & get LNA gain. */ void set_lna(int fd, int32_t lna); int32_t get_lna(int fd); From 85318fda97720ca5f33c8a5a138c9c3b3b68ea75 Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Tue, 28 May 2019 17:14:38 +0800 Subject: [PATCH 09/13] typo --- LoRa/lora.c | 12 ++++++------ LoRa/spi-sx1278.c | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/LoRa/lora.c b/LoRa/lora.c index 044e71a..de87da9 100644 --- a/LoRa/lora.c +++ b/LoRa/lora.c @@ -64,7 +64,7 @@ file_open(struct inode *inode, struct file *filp) int status = -ENXIO; pr_debug("lora: open file\n"); - + mutex_lock(&device_list_lock); /* Find the lora data in device_entry with matched dev_t in inode. */ list_for_each_entry(lrdata, &device_list, device_entry) { @@ -123,17 +123,17 @@ static int file_close(struct inode *inode, struct file *filp) { struct lora_struct *lrdata; - + pr_debug("lora: close file\n"); - + lrdata = filp->private_data; mutex_lock(&device_list_lock); filp->private_data = NULL; - + if (lrdata->users > 0) lrdata->users--; - + /* Last close */ if (lrdata->users == 0) { kfree(lrdata->rx_buf); @@ -447,7 +447,7 @@ int lora_unregister_driver(struct lora_driver *driver) { dev_t dev = MKDEV(driver->major, driver->minor_start); - + pr_debug("lora: unregister %s\n", driver->name); /* Delete device class. */ class_destroy(driver->lora_class); diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index 98d50a6..556cb52 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -178,7 +178,7 @@ sx127X_readVersion(struct regmap *rm) * * Return: LoRa device's register value */ -uint8_t +uint8_t sx127X_getMode(struct regmap *rm) { uint8_t op_mode; @@ -593,7 +593,7 @@ const uint32_t hz[] = { }; /** - * sx127X_getLoRaBW - Set RF bandwidth + * sx127X_setLoRaBW - Set RF bandwidth * @rm: the device as a regmap to communicate with * @bw: RF bandwidth going to be assigned in Hz */ @@ -662,7 +662,7 @@ sx127X_getLoRaCR(struct regmap *rm) regmap_raw_read(rm, SX127X_REG_MODEM_CONFIG1, &mcf1, 1); cr = 0x40 + ((mcf1 & 0x0E) >> 1) + 4; - + return cr; } From b837921a62f0f4e19232ec7713c2171b07dab692 Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Wed, 29 May 2019 08:25:33 +0800 Subject: [PATCH 10/13] Add ioctl LORA_SET_OCPIMAX LORA_GET_OCPIMAX --- LoRa/lora.c | 9 +++ LoRa/lora.h | 5 ++ LoRa/spi-sx1278.c | 121 ++++++++++++++++++++++++++++++++++ test-application/lora-ioctl.c | 15 +++++ test-application/lora-ioctl.h | 6 ++ 5 files changed, 156 insertions(+) diff --git a/LoRa/lora.c b/LoRa/lora.c index de87da9..2d732fd 100644 --- a/LoRa/lora.c +++ b/LoRa/lora.c @@ -227,6 +227,15 @@ file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (lrdata->ops->getPaRamp != NULL) ret = lrdata->ops->getPaRamp(lrdata, pval); break; + /* Set & get the RF max current of overload current protection (OCP) for PA. */ + case LORA_SET_OCPIMAX: + if (lrdata->ops->setOcpImax != NULL) + ret = lrdata->ops->setOcpImax(lrdata, pval); + break; + case LORA_GET_OCPIMAX: + if (lrdata->ops->getOcpImax != NULL) + ret = lrdata->ops->getOcpImax(lrdata, pval); + break; /* Set & get the LNA gain. */ case LORA_SET_LNA: if (lrdata->ops->setLNA != NULL) diff --git a/LoRa/lora.h b/LoRa/lora.h index 64371cf..3eb603a 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -71,6 +71,8 @@ #define LORA_GET_PREAMBLE (_IOR(LORA_IOC_MAGIC, 21, int)) #define LORA_SET_PARAMP (_IOW(LORA_IOC_MAGIC, 22, int)) #define LORA_GET_PARAMP (_IOR(LORA_IOC_MAGIC, 23, int)) +#define LORA_SET_OCPIMAX (_IOW(LORA_IOC_MAGIC, 24, int)) +#define LORA_GET_OCPIMAX (_IOR(LORA_IOC_MAGIC, 25, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -95,6 +97,9 @@ struct lora_operations { /* Set & get the RF power rise/fall time of ramp up/down. */ long (*setPaRamp)(struct lora_struct *, void __user *); long (*getPaRamp)(struct lora_struct *, void __user *); + /* Set & get the RF max current of overload current protection (OCP) for PA. */ + long (*setOcpImax)(struct lora_struct *, void __user *); + long (*getOcpImax)(struct lora_struct *, void __user *); /* Set & get the LNA gain. */ long (*setLNA)(struct lora_struct *, void __user *); long (*getLNA)(struct lora_struct *, void __user *); diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index 556cb52..4e14638 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -424,6 +424,76 @@ sx127X_getLoRaPaRamp(struct regmap *rm) return ramp_us[us]; } +const uint32_t ocp_ma[] = { + 45, + 50, + 55, + 60, + 65, + 70, + 75, + 80, + 85, + 90, + 95, + 100, + 105, + 110, + 115, + 120, + 130, + 140, + 150, + 160, + 170, + 180, + 190, + 200, + 210, + 220, + 230, + 240 +}; + +/** + * sx127X_setLoRaOcpImax - Set RF max current of overload current protection (OCP) for PA + * @rm: the device as a regmap to communicate with + * @mA: RF max current going to be assigned in mA + */ +void +sx127X_setLoRaOcpImax(struct regmap *rm, uint32_t mA) +{ + uint8_t i; + uint8_t ocp; + + for (i = 0; i < 0x1B; i++) { + if (ocp_ma[i] >= mA) + break; + } + + regmap_raw_read(rm, SX127X_REG_OCP, &ocp, 1); + ocp = (ocp & 0xE0) | i; + regmap_raw_write(rm, SX127X_REG_OCP, &ocp, 1); +} + +/** + * sx127X_getLoRaOcpImax - Get RF max current of overload current protection (OCP) for PA + * @rm: the device as a regmap to communicate with + * + * Return: RF max current in mA + */ +uint32_t +sx127X_getLoRaOcpImax(struct regmap *rm) +{ + uint8_t ocp; + uint8_t mA; + + regmap_raw_read(rm, SX127X_REG_OCP, &ocp, 1); + mA = ocp & 0x1F; + + return ocp_ma[mA]; +} + int8_t lna_gain[] = { 0, -6, @@ -1483,6 +1553,55 @@ loraspi_getparamp(struct lora_struct *lrdata, void __user *arg) return 0; } +/** + * loraspi_setocpimax - Set the RF max current of overload current protection (OCP) for PA + * @lrdata: LoRa device + * @arg: the buffer holding the RF max current value in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_setocpimax(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t imax; + + rm = lrdata->lora_device; + status = copy_from_user(&imax, arg, sizeof(uint32_t)); + + mutex_lock(&(lrdata->buf_lock)); + sx127X_setLoRaOcpImax(rm, imax); + mutex_unlock(&(lrdata->buf_lock)); + + return 0; +} + +/** + * loraspi_getbandwidth - Get the RF max current of overload current protection (OCP) for PA + * @lrdata: LoRa device + * @arg: the buffer going to hold the RF max current value in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_getocpimax(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t imax; + + rm = lrdata->lora_device; + + mutex_lock(&(lrdata->buf_lock)); + imax = sx127X_getLoRaOcpImax(rm); + mutex_unlock(&(lrdata->buf_lock)); + + status = copy_to_user(arg, &imax, sizeof(uint32_t)); + + return 0; +} + /** * loraspi_setLNA - Set the LNA gain * @lrdata: LoRa device @@ -1952,6 +2071,8 @@ struct lora_operations lrops = { .getPower = loraspi_getpower, .setPaRamp = loraspi_setparamp, .getPaRamp = loraspi_getparamp, + .setOcpImax = loraspi_setocpimax, + .getOcpImax = loraspi_getocpimax, .setLNA = loraspi_setLNA, .getLNA = loraspi_getLNA, .setLNAAGC = loraspi_setLNAAGC, diff --git a/test-application/lora-ioctl.c b/test-application/lora-ioctl.c index 52efdc0..c610093 100644 --- a/test-application/lora-ioctl.c +++ b/test-application/lora-ioctl.c @@ -142,6 +142,21 @@ uint32_t get_paramp(int fd) return us; } +/* Set & get the RF max current of overload current protection (OCP) for PA. */ +void set_ocpimax(int fd, uint32_t mA) +{ + ioctl(fd, LORA_SET_OCPIMAX, &mA); +} + +uint32_t get_ocpimax(int fd) +{ + uint32_t mA; + + ioctl(fd, LORA_GET_OCPIMAX, &mA); + + return mA; +} + /* Set & get LNA gain. */ void set_lna(int fd, int32_t lna) { diff --git a/test-application/lora-ioctl.h b/test-application/lora-ioctl.h index 2a50b97..4197609 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -68,6 +68,8 @@ #define LORA_GET_PREAMBLE (_IOR(LORA_IOC_MAGIC, 21, int)) #define LORA_SET_PARAMP (_IOW(LORA_IOC_MAGIC, 22, int)) #define LORA_GET_PARAMP (_IOR(LORA_IOC_MAGIC, 23, int)) +#define LORA_SET_OCPIMAX (_IOW(LORA_IOC_MAGIC, 24, int)) +#define LORA_GET_OCPIMAX (_IOR(LORA_IOC_MAGIC, 25, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -104,6 +106,10 @@ int32_t get_power(int fd); void set_paramp(int fd, uint32_t us); uint32_t get_paramp(int fd); +/* Set & get the RF max current of overload current protection (OCP) for PA. */ +void set_ocpimax(int fd, uint32_t mA); +uint32_t get_ocpimax(int fd); + /* Set & get LNA gain. */ void set_lna(int fd, int32_t lna); int32_t get_lna(int fd); From aceb28a08541e5066225148e8881b16ebc0feeef Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Wed, 29 May 2019 09:27:08 +0800 Subject: [PATCH 11/13] Add ioctl LORA_SET_LNABOOSTHF --- LoRa/lora.c | 5 +++++ LoRa/lora.h | 3 +++ LoRa/spi-sx1278.c | 42 +++++++++++++++++++++++++++++++++++ test-application/lora-ioctl.c | 6 +++++ test-application/lora-ioctl.h | 4 ++++ 5 files changed, 60 insertions(+) diff --git a/LoRa/lora.c b/LoRa/lora.c index 2d732fd..0a43a16 100644 --- a/LoRa/lora.c +++ b/LoRa/lora.c @@ -250,6 +250,11 @@ file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (lrdata->ops->setLNAAGC != NULL) ret = lrdata->ops->setLNAAGC(lrdata, pval); break; + /* Set RF low noise amplifier (LNA) boost in High Frequency (RFI_HF) to 150% LNA current. */ + case LORA_SET_LNABOOSTHF: + if (lrdata->ops->setLnaBoostHf != NULL) + ret = lrdata->ops->setLnaBoostHf(lrdata, pval); + break; /* Set & get the RF spreading factor. */ case LORA_SET_SPRFACTOR: if (lrdata->ops->setSPRFactor != NULL) diff --git a/LoRa/lora.h b/LoRa/lora.h index 3eb603a..ca7e1b2 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -73,6 +73,7 @@ #define LORA_GET_PARAMP (_IOR(LORA_IOC_MAGIC, 23, int)) #define LORA_SET_OCPIMAX (_IOW(LORA_IOC_MAGIC, 24, int)) #define LORA_GET_OCPIMAX (_IOR(LORA_IOC_MAGIC, 25, int)) +#define LORA_SET_LNABOOSTHF (_IOW(LORA_IOC_MAGIC, 26, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -105,6 +106,8 @@ struct lora_operations { long (*getLNA)(struct lora_struct *, void __user *); /* Set LNA be auto gain control or manual. */ long (*setLNAAGC)(struct lora_struct *, void __user *); + /* Set low noise amplifier (LNA) boost in High Frequency (RFI_HF) to 150% LNA current. */ + long (*setLnaBoostHf)(struct lora_struct *, void __user *); /* Set & get the RF spreading factor. */ long (*setSPRFactor)(struct lora_struct *, void __user *); long (*getSPRFactor)(struct lora_struct *, void __user *); diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index 4e14638..2c53251 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -561,6 +561,21 @@ sx127X_setLoRaLNAAGC(struct regmap *rm, int32_t yesno) regmap_raw_write(rm, SX127X_REG_MODEM_CONFIG3, &mcf3, 1); } +/** + * sx127X_setLnaBoostHf - Set RF low noise amplifier (LNA) boost in High Frequency (RFI_HF) to 150% LNA current + * @rm: the device as a regmap to communicate with + * @yesno: 1 / 0 for boost / not boost + */ +void +sx127X_setLoRaLnaBoostHf(struct regmap *rm, uint8_t yesno) +{ + uint8_t lnacf; + + regmap_raw_read(rm, SX127X_REG_LNA, &lnacf, 1); + lnacf = (yesno) ? lnacf | 0x3 : lnacf & 0xFC; + regmap_raw_write(rm, SX127X_REG_LNA, &lnacf, 1); +} + /** * sx127X_getLoRaAllFlag - Get all of the LoRa device's IRQ flags' current state * @rm: the device as a regmap to communicate with @@ -1683,6 +1698,32 @@ loraspi_setLNAAGC(struct lora_struct *lrdata, void __user *arg) return 0; } +/** + * loraspi_setLnaBoostHf - Set RF low noise amplifier (LNA) boost in High Frequency (RFI_HF) to 150% LNA current + * @lrdata: LoRa device + * @arg: the buffer holding the check or not check in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_setLnaBoostHf(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t boost32; + uint8_t boost; + + rm = lrdata->lora_device; + status = copy_from_user(&boost32, arg, sizeof(uint32_t)); + + boost = (boost32 == 1) ? 1 : 0; + mutex_lock(&(lrdata->buf_lock)); + sx127X_setLoRaLnaBoostHf(rm, boost); + mutex_unlock(&(lrdata->buf_lock)); + + return 0; +} + /** * loraspi_setsprfactor - Set the RF spreading factor * @lrdata: LoRa device @@ -2076,6 +2117,7 @@ struct lora_operations lrops = { .setLNA = loraspi_setLNA, .getLNA = loraspi_getLNA, .setLNAAGC = loraspi_setLNAAGC, + .setLnaBoostHf = loraspi_setLnaBoostHf, .setSPRFactor = loraspi_setsprfactor, .getSPRFactor = loraspi_getsprfactor, .setBW = loraspi_setbandwidth, diff --git a/test-application/lora-ioctl.c b/test-application/lora-ioctl.c index c610093..c407253 100644 --- a/test-application/lora-ioctl.c +++ b/test-application/lora-ioctl.c @@ -178,6 +178,12 @@ void set_lnaagc(int fd, uint32_t agc) ioctl(fd, LORA_SET_LNAAGC, &agc); } +/* Set low noise amplifier (LNA) boost in High Frequency (RFI_HF) to 150% LNA current. */ +void set_lnaboosthf(int fd, uint32_t boost) +{ + ioctl(fd, LORA_SET_LNABOOSTHF, &boost); +} + /* Set & get the RF spreading factor. */ void set_sprfactor(int fd, uint32_t sprf) { diff --git a/test-application/lora-ioctl.h b/test-application/lora-ioctl.h index 4197609..7a5ca9c 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -70,6 +70,7 @@ #define LORA_GET_PARAMP (_IOR(LORA_IOC_MAGIC, 23, int)) #define LORA_SET_OCPIMAX (_IOW(LORA_IOC_MAGIC, 24, int)) #define LORA_GET_OCPIMAX (_IOR(LORA_IOC_MAGIC, 25, int)) +#define LORA_SET_LNABOOSTHF (_IOW(LORA_IOC_MAGIC, 26, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -117,6 +118,9 @@ int32_t get_lna(int fd); /* Set LNA be auto gain control or manual. */ void set_lnaagc(int fd, uint32_t agc); +/* Set low noise amplifier (LNA) boost in High Frequency (RFI_HF) to 150% LNA current. */ +void set_lnaboosthf(int fd, uint32_t boost); + /* Set & get the RF spreading factor. */ void set_sprfactor(int fd, uint32_t sprf); uint32_t get_sprfactor(int fd); From a13b10b358bc92280cf1f664782291dc85fe054e Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Wed, 29 May 2019 10:15:08 +0800 Subject: [PATCH 12/13] Set LowFrequencyModeOn of SX127X_REG_OP_MODE when sx127X_setLoRaFreq() --- LoRa/spi-sx1278.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index 2c53251..e9b1954 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -235,6 +235,7 @@ sx127X_setLoRaFreq(struct regmap *rm, uint32_t fr) uint8_t buf[3]; int i; uint32_t f_xosc; + uint8_t op_mode; #ifdef CONFIG_OF /* Set the LoRa module's crystal oscillator's clock if OF is defined. */ @@ -258,6 +259,19 @@ sx127X_setLoRaFreq(struct regmap *rm, uint32_t fr) } regmap_raw_write(rm, SX127X_REG_FRF_MSB, buf, 3); + + regmap_raw_read(rm, SX127X_REG_OP_MODE, &op_mode, 1); + if (fr < 525000000) { // Low Frequency Mode (access to LF test registers) + if ((op_mode & 0x08) != 0x08) { + op_mode |= 0x08; + regmap_raw_write(rm, SX127X_REG_OP_MODE, &op_mode, 1); + } + } else { // High Frequency Mode (access to HF test registers) + if (op_mode & 0x08) { + op_mode &= 0xF7; + regmap_raw_write(rm, SX127X_REG_OP_MODE, &op_mode, 1); + } + } } /** From df21cf6f173886e4490bba3c81aab1e1326def93 Mon Sep 17 00:00:00 2001 From: Li Zheng Date: Tue, 4 Jun 2019 13:21:25 +0800 Subject: [PATCH 13/13] Add ioctl LORA_SET_PMAX20DBM --- LoRa/lora.c | 5 +++++ LoRa/lora.h | 3 +++ LoRa/spi-sx1278.c | 41 +++++++++++++++++++++++++++++++++++ test-application/lora-ioctl.c | 6 +++++ test-application/lora-ioctl.h | 4 ++++ 5 files changed, 59 insertions(+) diff --git a/LoRa/lora.c b/LoRa/lora.c index 0a43a16..d610675 100644 --- a/LoRa/lora.c +++ b/LoRa/lora.c @@ -218,6 +218,11 @@ file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) if (lrdata->ops->getPower != NULL) ret = lrdata->ops->getPower(lrdata, pval); break; + /* Set high power +20 dBm capability on PA_BOOST pin. */ + case LORA_SET_PMAX20DBM: + if (lrdata->ops->setPmax20dBm != NULL) + ret = lrdata->ops->setPmax20dBm(lrdata, pval); + break; /* Set & get the RF power rise/fall time of ramp up/down. */ case LORA_SET_PARAMP: if (lrdata->ops->setPaRamp != NULL) diff --git a/LoRa/lora.h b/LoRa/lora.h index ca7e1b2..d214d4e 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -74,6 +74,7 @@ #define LORA_SET_OCPIMAX (_IOW(LORA_IOC_MAGIC, 24, int)) #define LORA_GET_OCPIMAX (_IOR(LORA_IOC_MAGIC, 25, int)) #define LORA_SET_LNABOOSTHF (_IOW(LORA_IOC_MAGIC, 26, int)) +#define LORA_SET_PMAX20DBM (_IOW(LORA_IOC_MAGIC, 27, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -95,6 +96,8 @@ struct lora_operations { /* Set & get the PA power. */ long (*setPower)(struct lora_struct *, void __user *); long (*getPower)(struct lora_struct *, void __user *); + /* Set high power +20 dBm capability on PA_BOOST pin. */ + long (*setPmax20dBm)(struct lora_struct *, void __user *); /* Set & get the RF power rise/fall time of ramp up/down. */ long (*setPaRamp)(struct lora_struct *, void __user *); long (*getPaRamp)(struct lora_struct *, void __user *); diff --git a/LoRa/spi-sx1278.c b/LoRa/spi-sx1278.c index e9b1954..eac0e6c 100644 --- a/LoRa/spi-sx1278.c +++ b/LoRa/spi-sx1278.c @@ -380,6 +380,20 @@ sx127X_getLoRaPower(struct regmap *rm) return pout; } +/** + * sx127X_setLoRaPmax20dBm - Set RF high power +20 dBm capability on PA_BOOST pin + * @rm: the device as a regmap to communicate with + * @yesno: 1 / 0 for 20dBm / 17dBm + */ +void +sx127X_setLoRaPmax20dBm(struct regmap *rm, uint8_t yesno) +{ + uint8_t padac; + + padac = (yesno) ? 0x87 : 0x84; + regmap_raw_write(rm, SX127X_REG_PA_DAC, &padac, 1); +} + const uint32_t ramp_us[] = { 3400, 2000, @@ -1533,6 +1547,32 @@ loraspi_getpower(struct lora_struct *lrdata, void __user *arg) return 0; } +/** + * loraspi_setPmax20dBm - Set RF high power +20 dBm capability on PA_BOOST pin + * @lrdata: LoRa device + * @arg: the buffer holding the check or not check in user space + * + * Return: 0 / other values for success / error + */ +static long +loraspi_setPmax20dBm(struct lora_struct *lrdata, void __user *arg) +{ + struct regmap *rm; + int status; + uint32_t pmax20dBm32; + uint8_t pmax20dBm; + + rm = lrdata->lora_device; + status = copy_from_user(&pmax20dBm32, arg, sizeof(uint32_t)); + + pmax20dBm = (pmax20dBm32 == 1) ? 1 : 0; + mutex_lock(&(lrdata->buf_lock)); + sx127X_setLoRaPmax20dBm(rm, pmax20dBm); + mutex_unlock(&(lrdata->buf_lock)); + + return 0; +} + /** * loraspi_setparamp - Set the RF power rise/fall time of ramp up/down * @lrdata: LoRa device @@ -2124,6 +2164,7 @@ struct lora_operations lrops = { .getFreq = loraspi_getfreq, .setPower = loraspi_setpower, .getPower = loraspi_getpower, + .setPmax20dBm = loraspi_setPmax20dBm, .setPaRamp = loraspi_setparamp, .getPaRamp = loraspi_getparamp, .setOcpImax = loraspi_setocpimax, diff --git a/test-application/lora-ioctl.c b/test-application/lora-ioctl.c index c407253..22a928c 100644 --- a/test-application/lora-ioctl.c +++ b/test-application/lora-ioctl.c @@ -127,6 +127,12 @@ int32_t get_power(int fd) return power; } +/* Set high power +20 dBm capability on PA_BOOST pin. */ +void set_pmax20dbm(int fd, uint32_t pmax20dbm) +{ + ioctl(fd, LORA_SET_PMAX20DBM, &pmax20dbm); +} + /* Set & get the RF power rise/fall time of ramp up/down. */ void set_paramp(int fd, uint32_t us) { diff --git a/test-application/lora-ioctl.h b/test-application/lora-ioctl.h index 7a5ca9c..142aa3c 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -71,6 +71,7 @@ #define LORA_SET_OCPIMAX (_IOW(LORA_IOC_MAGIC, 24, int)) #define LORA_GET_OCPIMAX (_IOR(LORA_IOC_MAGIC, 25, int)) #define LORA_SET_LNABOOSTHF (_IOW(LORA_IOC_MAGIC, 26, int)) +#define LORA_SET_PMAX20DBM (_IOW(LORA_IOC_MAGIC, 27, int)) /* List the state of the LoRa device. */ #define LORA_STATE_SLEEP 0 @@ -103,6 +104,9 @@ int32_t get_snr(int fd); void set_power(int fd, int32_t power); int32_t get_power(int fd); +/* Set high power +20 dBm capability on PA_BOOST pin. */ +void set_pmax20dbm(int fd, uint32_t pmax20dbm); + /* Set & get the RF power rise/fall time of ramp up/down. */ void set_paramp(int fd, uint32_t us); uint32_t get_paramp(int fd);