diff --git a/LoRa/lora.c b/LoRa/lora.c index 18901e9..d610675 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); @@ -218,6 +218,29 @@ 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) + 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 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) @@ -232,6 +255,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) @@ -260,6 +288,39 @@ 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; + /* 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; + /* Set LoRa packages in Explicit / Implicit Header Mode. */ + case LORA_SET_IMPLICIT: + 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; + /* 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; } @@ -405,7 +466,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/lora.h b/LoRa/lora.h index 2fcb413..d214d4e 100644 --- a/LoRa/lora.h +++ b/LoRa/lora.h @@ -55,13 +55,26 @@ #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)) #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)) +#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)) +#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)) +#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 @@ -83,11 +96,21 @@ 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 *); + /* 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 *); /* 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 *); @@ -98,6 +121,18 @@ 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 *); + /* 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 *); + /* 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 20867b8..eac0e6c 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; @@ -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); + } + } } /** @@ -366,6 +380,148 @@ 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, + 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]; +} + +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, @@ -433,6 +589,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 @@ -535,7 +706,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 */ @@ -585,7 +756,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); } @@ -604,7 +775,7 @@ sx127X_getLoRaCR(struct regmap *rm) regmap_raw_read(rm, SX127X_REG_MODEM_CONFIG1, &mcf1, 1); cr = 0x40 + ((mcf1 & 0x0E) >> 1) + 4; - + return cr; } @@ -903,6 +1074,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 @@ -1361,6 +1547,130 @@ 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 + * @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_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 @@ -1442,6 +1752,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 @@ -1590,6 +1926,186 @@ 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_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_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_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_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 @@ -1648,15 +2164,28 @@ 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, + .getOcpImax = loraspi_getocpimax, .setLNA = loraspi_setLNA, .getLNA = loraspi_getLNA, .setLNAAGC = loraspi_setLNAAGC, + .setLnaBoostHf = loraspi_setLnaBoostHf, .setSPRFactor = loraspi_setsprfactor, .getSPRFactor = loraspi_getsprfactor, .setBW = loraspi_setbandwidth, .getBW = loraspi_getbandwidth, .getRSSI = loraspi_getrssi, .getSNR = loraspi_getsnr, + .setCRC = loraspi_setCRC, + .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 10413ff..22a928c 100644 --- a/test-application/lora-ioctl.c +++ b/test-application/lora-ioctl.c @@ -127,6 +127,42 @@ 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) +{ + 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 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) { @@ -148,6 +184,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) { @@ -177,3 +219,49 @@ 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); +} + +/* 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; +} + +/* Set LoRa packages in Explicit / Implicit Header Mode. */ +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); +} + +/* 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 287346a..142aa3c 100644 --- a/test-application/lora-ioctl.h +++ b/test-application/lora-ioctl.h @@ -52,13 +52,26 @@ #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)) #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)) +#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)) +#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)) +#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 @@ -91,6 +104,17 @@ 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); + +/* 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); @@ -98,6 +122,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); @@ -106,4 +133,21 @@ 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); + +/* Set & get LoRa package's coding rate. */ +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); + +/* 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