From 30c79a66d0abd4ab1437d5ff6e06f01e170ee6ee Mon Sep 17 00:00:00 2001
From: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
Date: Thu, 24 May 2018 19:28:46 +0200
Subject: [PATCH] drivers: Replaced magic numbers in cc110x

Replaced magic undocumented numbers in the code by human readable preprocessor
macros.
---
 drivers/cc110x/cc110x-netdev.c           |  2 +-
 drivers/cc110x/cc110x-rxtx.c             | 17 ++++++-----
 drivers/cc110x/cc110x.c                  |  2 +-
 drivers/cc110x/include/cc110x-defines.h  | 36 ++++++++++++++++++++++++
 drivers/cc110x/include/cc110x-internal.h |  1 +
 5 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/drivers/cc110x/cc110x-netdev.c b/drivers/cc110x/cc110x-netdev.c
index 8d4bc1dbde..99f3b58dac 100644
--- a/drivers/cc110x/cc110x-netdev.c
+++ b/drivers/cc110x/cc110x-netdev.c
@@ -201,7 +201,7 @@ static int _init(netdev_t *dev)
     cc110x_t *cc110x = &((netdev_cc110x_t*) dev)->cc110x;
 
     gpio_init_int(cc110x->params.gdo2, GPIO_IN, GPIO_BOTH,
-            &_netdev_cc110x_isr, (void*)dev);
+                  &_netdev_cc110x_isr, (void*)dev);
 
     gpio_set(cc110x->params.gdo2);
     gpio_irq_disable(cc110x->params.gdo2);
diff --git a/drivers/cc110x/cc110x-rxtx.c b/drivers/cc110x/cc110x-rxtx.c
index b5618e3c74..91fbc408a3 100644
--- a/drivers/cc110x/cc110x-rxtx.c
+++ b/drivers/cc110x/cc110x-rxtx.c
@@ -80,7 +80,8 @@ static void _rx_start(cc110x_t *dev)
     pkt_buf->pos = 0;
 
     gpio_irq_disable(dev->params.gdo2);
-    cc110x_write_reg(dev, CC110X_IOCFG2, 0x01);
+    cc110x_write_reg(dev, CC110X_IOCFG2,
+                     CC110X_GDO_HIGH_ON_RX_FIFO_FILLED_OR_PKT_END);
     gpio_irq_enable(dev->params.gdo2);
 }
 
@@ -88,7 +89,7 @@ static void _rx_read_data(cc110x_t *dev, void(*callback)(void*), void*arg)
 {
     int fifo = cc110x_get_reg_robust(dev, 0xfb);
 
-    if (fifo & 0x80) {
+    if (fifo & RXFIFO_OVERFLOW) {
         DEBUG("%s:%s:%u rx overflow\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
         _rx_abort(dev);
         return;
@@ -196,9 +197,9 @@ static void _tx_continue(cc110x_t *dev)
         return;
     }
 
-    int fifo = 64 - cc110x_get_reg_robust(dev, 0xfa);
+    int fifo = CC110X_FIFO_LENGTH - cc110x_get_reg_robust(dev, 0xfa);
 
-    if (fifo & 0x80) {
+    if (fifo & TXFIFO_UNDERFLOW) {
         DEBUG("%s:%s:%u tx underflow!\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
         _tx_abort(dev);
         return;
@@ -224,11 +225,12 @@ static void _tx_continue(cc110x_t *dev)
     if (to_send < left) {
         /* set GDO2 to 0x2 -> will deassert at TX FIFO below threshold */
         gpio_irq_enable(dev->params.gdo2);
-        cc110x_write_reg(dev, CC110X_IOCFG2, 0x02);
+        cc110x_write_reg(dev, CC110X_IOCFG2,
+                         CC110X_GDO_LOW_ON_TX_FIFO_BELOW_THRESHOLD);
     }
     else {
         /* set GDO2 to 0x6 -> will deassert at packet end */
-        cc110x_write_reg(dev, CC110X_IOCFG2, 0x06);
+        cc110x_write_reg(dev, CC110X_IOCFG2, CC110X_GDO_HIGH_ON_SYNC_WORD);
         gpio_irq_enable(dev->params.gdo2);
     }
 }
@@ -300,7 +302,8 @@ int cc110x_send(cc110x_t *dev, cc110x_pkt_t *packet)
     cc110x_hook_tx();
 #endif
 
-    cc110x_write_reg(dev, CC110X_IOCFG2, 0x02);
+    cc110x_write_reg(dev, CC110X_IOCFG2,
+                     CC110X_GDO_LOW_ON_TX_FIFO_BELOW_THRESHOLD);
 
     /* Put CC110x in IDLE mode to flush the FIFO */
     cc110x_strobe(dev, CC110X_SIDLE);
diff --git a/drivers/cc110x/cc110x.c b/drivers/cc110x/cc110x.c
index d66d222d7c..8230f94873 100644
--- a/drivers/cc110x/cc110x.c
+++ b/drivers/cc110x/cc110x.c
@@ -151,7 +151,7 @@ void cc110x_switch_to_rx(cc110x_t *dev)
 
     dev->radio_state = RADIO_RX;
 
-    cc110x_write_reg(dev, CC110X_IOCFG2, 0x6);
+    cc110x_write_reg(dev, CC110X_IOCFG2, CC110X_GDO_HIGH_ON_SYNC_WORD);
     cc110x_strobe(dev, CC110X_SRX);
 
     gpio_irq_enable(dev->params.gdo2);
diff --git a/drivers/cc110x/include/cc110x-defines.h b/drivers/cc110x/include/cc110x-defines.h
index 069076b58d..6eac9fafb2 100644
--- a/drivers/cc110x/include/cc110x-defines.h
+++ b/drivers/cc110x/include/cc110x-defines.h
@@ -208,6 +208,42 @@ extern "C" {
 #define CC110X_RXFIFO       (0x3F)      /**< RX FIFO: Read operations read from the RX FIFO (SB: +0x80; BURST: +0xC0) */
 /** @} */
 
+
+/**
+ * @name GDO configuration values
+ *
+ * Values that can be written to the GDO0, GDO1 and GDO2 configuration registers
+ * @{
+ */
+
+/** @brief GDO goes high when RX FIFO is filled at or above threshold */
+#define CC110X_GDO_HIGH_ON_RX_FIFO_ABOVE_THRESHOLD   (0x00)
+/**
+ * @brief GDO goes high when RX FIFO is filled at or above threshold or when
+ *        packet is fully received
+ */
+#define CC110X_GDO_HIGH_ON_RX_FIFO_FILLED_OR_PKT_END (0x01)
+/** @brief GDO goes low when TX FIFO is filled less than threshold */
+#define CC110X_GDO_LOW_ON_TX_FIFO_BELOW_THRESHOLD    (0x02)
+/** @brief  GDO goes low when TX FIFO becomes empty */
+#define CC110X_GDO_LOW_ON_TX_FIFO_EMPTY              (0x03)
+/** @brief  GDO goes high when RX FIFO overflows */
+#define CC110X_GDO_HIGH_ON_RX_FIFO_OVERFLOW          (0x04)
+/** @brief GDO goes high when TX FIFO underflows */
+#define CC110X_GDO_HIGH_ON_TX_FIFO_UNDERFLOW         (0x05)
+/**
+ * @brief GDO goes high when sync word was just received until the packet is
+ *        fully received, or when sync word has been send until packet is fully
+ *        send
+ */
+#define CC110X_GDO_HIGH_ON_SYNC_WORD                 (0x06)
+/**
+ * @brief GDO goes high when a packet is received and CRC is correct.
+ *        Goes back to low when first byte of RX fifo has been read
+ */
+#define CC110X_GDO_HIGH_ON_PACKET_RECEIVED           (0x07)
+/** @} */
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/drivers/cc110x/include/cc110x-internal.h b/drivers/cc110x/include/cc110x-internal.h
index a6e64f09d7..0875be83e1 100644
--- a/drivers/cc110x/include/cc110x-internal.h
+++ b/drivers/cc110x/include/cc110x-internal.h
@@ -29,6 +29,7 @@ extern "C" {
 #endif
 
 #define CC110X_RXBUF_SIZE           (2)
+#define CC110X_FIFO_LENGTH          (64)
 #define CC110X_MAX_DATA_LENGTH      (58+64)
 
 #define CC110X_HEADER_LENGTH        (3)     /**< Header covers SRC, DST and
-- 
GitLab