linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ben Whitten <ben.whitten@gmail.com>
To: afaerber@suse.de
Cc: linux-lpwan@lists.infradead.org,
	Ben Whitten <ben.whitten@lairdtech.com>,
	"David S. Miller" <davem@davemloft.net>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH lora-next 08/11] net: lora: sx130x: add test transmission
Date: Mon, 28 Jan 2019 16:13:02 +0000	[thread overview]
Message-ID: <20190128161306.27805-9-ben.whitten@lairdtech.com> (raw)
In-Reply-To: <20190128161306.27805-1-ben.whitten@lairdtech.com>

First transmission from the SX130x, all metadata values are hardcoded
and will be replaced with a suitable alternative.
Data sent into the socket is sent straight out of the card.

Signed-off-by: Ben Whitten <ben.whitten@lairdtech.com>
---
 drivers/net/lora/sx130x.c | 128 +++++++++++++++++++++++++++++++++++++-
 drivers/net/lora/sx130x.h |  11 ++++
 2 files changed, 137 insertions(+), 2 deletions(-)

diff --git a/drivers/net/lora/sx130x.c b/drivers/net/lora/sx130x.c
index 820ec0220e28..f96f32d2e1ff 100644
--- a/drivers/net/lora/sx130x.c
+++ b/drivers/net/lora/sx130x.c
@@ -49,8 +49,50 @@ static const struct reg_field sx130x_regmap_fields[] = {
 	/* EMERGENCY_FORCE_HOST_CTRL */
 	[F_EMERGENCY_FORCE_HOST_CTRL] =
 		REG_FIELD(SX1301_EMERGENCY_FORCE_HOST_CTRL, 0, 0),
+	/* TX_TRIG */
+	[F_TX_TRIG_IMMEDIATE] = REG_FIELD(SX1301_TX_TRIG, 0, 0),
+	[F_TX_TRIG_DELAYED] = REG_FIELD(SX1301_TX_TRIG, 1, 1),
+	[F_TX_TRIG_GPS] = REG_FIELD(SX1301_TX_TRIG, 2, 2),
 };
 
+struct sx130x_tx_header {
+	u8	tx_freq[3];
+	u32	start;
+	u8	tx_power:4,
+		modulation_type:1,
+		radio_select:1,
+		resered0:2;
+	u8	reserved1;
+
+	union {
+		struct lora_t {
+			u8	sf:4,
+				cr:3,
+				crc16_en:1;
+			u8	payload_len;
+			u8	mod_bw:2,
+				implicit_header:1,
+				ppm_offset:1,
+				invert_pol:1,
+				reserved0:3;
+			u16	preamble;
+			u8	reserved1;
+			u8	reserved2;
+		} lora;
+		struct fsk_t {
+			u8	freq_dev;
+			u8	payload_len;
+			u8	packet_mode:1,
+				crc_en:1,
+				enc_mode:2,
+				crc_mode:1,
+				reserved0:3;
+			u16	preamble;
+			u16	bitrate;
+		} fsk;
+	} u;
+} __packed;
+
 struct sx130x_tx_gain_lut {
 	s8 power;	/* dBm measured at board connector */
 	u8 dig_gain;
@@ -646,6 +688,83 @@ static int sx130x_agc_init(struct sx130x_priv *priv)
 	return ret;
 }
 
+static int sx130x_tx(struct sx130x_priv *priv, void *data, int len)
+{
+	int ret, i;
+	u8 buff[256 + 16];
+	struct sx130x_tx_header *hdr = (struct sx130x_tx_header *)buff;
+	struct net_device *netdev = dev_get_drvdata(priv->dev);
+
+	/* TODO general checks to make sure we CAN send */
+
+	/* TODO Enable notch filter for lora 125 */
+
+	/* TODO get start delay for this TX */
+
+	/* TODO interpret tx power, HACK just set max power */
+
+	/* TODO get TX imbalance for this pow index from calibration step */
+
+	/* TODO set the dig gain */
+
+	/* TODO set TX PLL freq based on radio used to TX */
+
+	memset(buff, 0, sizeof(buff));
+
+	/* HACK set to 868MHz */
+	hdr->tx_freq[0] = 217;
+	hdr->tx_freq[1] = 0;
+	hdr->tx_freq[2] = 0;
+
+	hdr->start = 0; /* Start imediatly */
+	hdr->radio_select = 0; /* HACK Radio A transmit */
+	hdr->modulation_type = 0; /* HACK modulation LORA */
+	hdr->tx_power = 15; /* HACK power entry 15 */
+
+	hdr->u.lora.crc16_en = 1; /* Enable CRC16 */
+	hdr->u.lora.cr = 1; /* CR 4/5 */
+	hdr->u.lora.sf = 7; /* SF7 */
+	hdr->u.lora.payload_len = len; /* Set the data len to the skb len */
+	hdr->u.lora.implicit_header = 0; /* No implicit header */
+	hdr->u.lora.mod_bw = 0; /* Set 125KHz BW */
+	hdr->u.lora.ppm_offset = 0; /* TODO no ppm offset? */
+	hdr->u.lora.invert_pol = 0; /* TODO set no inverted polarity */
+
+	hdr->u.lora.preamble = 8; /* Set the standard preamble */
+
+	/* TODO 2 Msb in tx_freq0 for large narrow filtering, unset for now */
+	hdr->tx_freq[0] &= 0x3F;
+
+	/* Copy the TX data into the buffer ready to go */
+
+	memcpy((void *)&buff[16], data, len);
+
+	/* Reset any transmissions */
+	ret = regmap_write(priv->regmap, SX1301_TX_TRIG, 0);
+	if (ret)
+		return ret;
+
+	/* Put the buffer into the tranmit fifo */
+	ret = regmap_write(priv->regmap, SX1301_TX_DATA_BUF_ADDR, 0);
+	if (ret)
+		return ret;
+	ret = regmap_noinc_write(priv->regmap, SX1301_TX_DATA_BUF_DATA, buff,
+				 len + 16);
+	if (ret)
+		return ret;
+
+	/* HACK just go for immediate transfer */
+	ret = sx130x_field_force_write(priv, F_TX_TRIG_IMMEDIATE, 1);
+	if (ret)
+		return ret;
+
+	netdev_dbg(netdev, "Transmitting packet of size %d: ", len);
+	for (i = 0; i < len + 16; i++)
+		netdev_dbg(netdev, "%X", buff[i]);
+
+	return ret;
+}
+
 static netdev_tx_t sx130x_loradev_start_xmit(struct sk_buff *skb, struct net_device *netdev)
 {
 	struct sx130x_priv *priv = netdev_priv(netdev);
@@ -672,8 +791,13 @@ static void sx130x_tx_work_handler(struct work_struct *ws)
 	netdev_dbg(netdev, "%s\n", __func__);
 
 	if (priv->tx_skb) {
-
-		/* TODO actual tx* */
+		ret = sx130x_tx(priv, priv->tx_skb->data, priv->tx_skb->len);
+		if (ret) {
+			netdev->stats.tx_errors++;
+		} else {
+			netdev->stats.tx_packets++;
+			netdev->stats.tx_bytes += priv->tx_skb->len;
+		}
 
 		if (!(netdev->flags & IFF_ECHO) ||
 		    priv->tx_skb->pkt_type != PACKET_LOOPBACK ||
diff --git a/drivers/net/lora/sx130x.h b/drivers/net/lora/sx130x.h
index 2878dd3cc547..38f1b58b2165 100644
--- a/drivers/net/lora/sx130x.h
+++ b/drivers/net/lora/sx130x.h
@@ -32,6 +32,10 @@
 /* Page independent */
 #define SX1301_PAGE     0x00
 #define SX1301_VER      0x01
+#define SX1301_RX_DATA_BUF_ADDR 0x02 /* 16 wide */
+#define SX1301_RX_DATA_BUF_DATA 0x04
+#define SX1301_TX_DATA_BUF_ADDR 0x05
+#define SX1301_TX_DATA_BUF_DATA 0x06
 #define SX1301_MPA      0x09
 #define SX1301_MPD      0x0A
 #define SX1301_GEN      0x10
@@ -49,6 +53,9 @@
 #define SX1301_FORCE_CTRL   (SX1301_PAGE_BASE(0) + 0x69)
 #define SX1301_MCU_CTRL     (SX1301_PAGE_BASE(0) + 0x6A)
 
+/* Page 1 */
+#define SX1301_TX_TRIG      (SX1301_PAGE_BASE(1) + 0x21)
+
 /* Page 2 */
 #define SX1301_RADIO_A_SPI_DATA     (SX1301_PAGE_BASE(2) + 0x21)
 #define SX1301_RADIO_A_SPI_DATA_RB  (SX1301_PAGE_BASE(2) + 0x22)
@@ -87,6 +94,10 @@ enum sx130x_fields {
 	F_FORCE_DEC_FILTER_GAIN,
 
 	F_EMERGENCY_FORCE_HOST_CTRL,
+
+	F_TX_TRIG_IMMEDIATE,
+	F_TX_TRIG_DELAYED,
+	F_TX_TRIG_GPS,
 };
 
 struct regmap *sx130x_get_regmap(struct device *dev);
-- 
2.17.1


  parent reply	other threads:[~2019-01-28 17:02 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-28 16:12 [PATCH lora-next 00/11] net: lora: Get SX130x to transmit lora packets Ben Whitten
2019-01-28 16:12 ` [PATCH lora-next 01/11] dt-bindings: net: lora: sx130x: add power lut binding Ben Whitten
2019-01-28 18:13   ` Rob Herring
2019-01-28 16:12 ` [PATCH lora-next 02/11] net: lora: sx130x: add loading of tx lut from DT Ben Whitten
2019-01-28 16:12 ` [PATCH lora-next 03/11] net: lora: sx130x: add CHRS to volatile register list Ben Whitten
2019-01-28 16:12 ` [PATCH lora-next 04/11] net: lora: sx130x: add helper function for writing to the SX130x MCU Ben Whitten
2019-01-28 16:12 ` [PATCH lora-next 05/11] net: lora: sx130x: initialise AGC Ben Whitten
2019-01-28 16:13 ` [PATCH lora-next 06/11] net: lora: sx130x: add detail to TODO in setup Ben Whitten
2019-01-28 16:13 ` [PATCH lora-next 07/11] net: lora: sx130x: add work queue to tx path Ben Whitten
2019-01-28 16:13 ` Ben Whitten [this message]
2019-01-28 16:13 ` [PATCH lora-next 09/11] net: lora: introduce lora socket addressing for metadata Ben Whitten
2019-01-28 16:13 ` [PATCH lora-next 10/11] net: lora: sx130x: make use of lora address metadata in transmission Ben Whitten
2019-01-28 16:13 ` [PATCH lora-next 11/11] net: lora: sx130x: add patch to register fields Ben Whitten

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190128161306.27805-9-ben.whitten@lairdtech.com \
    --to=ben.whitten@gmail.com \
    --cc=afaerber@suse.de \
    --cc=ben.whitten@lairdtech.com \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-lpwan@lists.infradead.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).