Selaa lähdekoodia

+ unified format + bug fixes

DLIMIKO 1 vuosi sitten
vanhempi
commit
88fe3d12a7

+ 3 - 1
include/app_bridge.h

@@ -38,8 +38,10 @@ typedef enum
 
 /**
  * @brief Definuje formát dát senzora.
+ * @note Bajtová reprezentácia (Little Endian) bude nasledovná: | unit_multiplier | sign | value_multiplier | samples |
+ * byte_length
  */
-typedef struct __attribute__((packed))
+typedef struct __attribute__((packed, aligned(1)))
 {
     /** Bit určujúci či sú dáta znamienkové.
      * 0 = ungigned, 1 = signed */

+ 2 - 7
include/nbus_cmd.h

@@ -31,13 +31,8 @@
 #define CMD_INFO 0x0E
 
 // Command  parameters
-#define INFO_MODULE_NAME 0xE1
-#define INFO_MODULE_TYPE 0xE2
-#define INFO_MODULE_UUID 0xE3
-#define INFO_MODULE_HW 0xE4
-#define INFO_MODULE_FW 0xE5 // ?
-#define INFO_MODULE_MEMORY_ID 0xE6
-#define SENSOR_FORMAT 0xE7
+#define INFO_GENERAL 0xE1
+#define INFO_FORMAT 0xE2
 
 #define PARAM_SENSOR_SAMPLERATE 0x41
 #define PARAM_SENSOR_LPF 0x42

+ 1 - 1
src/app_bridge.c

@@ -28,5 +28,5 @@ nBus_sensorType_t *nbus_interface_allTypes()
 
 uint8_t nbus_interface_allTypesCount()
 {
-    return sizeof(nBus_param_t) / sizeof(nBus_sensorType_t);
+    return sizeof(allTypes) / sizeof(nBus_sensorType_t);
 }

+ 79 - 58
src/nbus_slave_module_unicast.c

@@ -5,20 +5,6 @@ void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
 {
     switch (nbus->function_code.function)
     {
-    case CMD_START: {
-        nbus->measure_active = MEASURE_RUNNING;
-        nbus->interface->start();
-        nbus->hw_platform->led_on();
-    }
-    break;
-
-    case CMD_STOP: {
-        nbus->measure_active = MEASURE_STOPPED;
-        nbus->interface->stop();
-        nbus->hw_platform->led_off();
-    }
-    break;
-
     case CMD_ECHO: {
         for (uint8_t i = 3; i < nbus->rx_length - 1; i++)
         {
@@ -30,21 +16,42 @@ void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
     break;
 
     case CMD_PARAM: {
-        // same as nbus_unicastToSensorSet
-        int32_t param_value = nbus->interface->getParam(0, (nBus_param_t)nbus->rx_buffer[3]);
-        if (param_value == PARAM_VALUE_NONE)
+        if (nbus->rx_length >= 5) // get concrete param
         {
-            setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
-            break;
-        }
+            if (nbus->interface->hasParam(0, (nBus_param_t)nbus->rx_buffer[3]) == 0) // handle non-existing param
+            {
+                setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
+                break;
+            }
 
-        nbus->tx_buffer[4] = nbus->rx_buffer[3];
-        nbus->tx_buffer[5] = (uint8_t)(param_value & 0xFF);
-        nbus->tx_buffer[6] = (uint8_t)((param_value >> 8) & 0xFF);
-        nbus->tx_buffer[7] = (uint8_t)((param_value >> 16) & 0xFF);
-        nbus->tx_buffer[8] = (uint8_t)((param_value >> 24) & 0xFF);
+            nbus->tx_buffer[4] = nbus->rx_buffer[3]; // param id
+            int32_t param_value = nbus->interface->getParam(0, (nBus_param_t)nbus->rx_buffer[3]);
+            nbus->tx_buffer[5] = (uint8_t)(param_value & 0xFF);
+            nbus->tx_buffer[6] = (uint8_t)((param_value >> 8) & 0xFF);
+            nbus->tx_buffer[7] = (uint8_t)((param_value >> 16) & 0xFF);
+            nbus->tx_buffer[8] = (uint8_t)((param_value >> 24) & 0xFF);
+            nbus->tx_length += 5;
+        }
+        else // get all params
+        {
+            nBus_param_t *params = nbus_interface_allParams();
+            for (uint8_t i = 0; i < nbus_interface_allParamsCount(); i++)
+            {
+                if (nbus->interface->hasParam(0, params[i]))
+                {
+                    int32_t param_value = nbus->interface->getParam(0, params[i]);
+                    nbus->tx_buffer[4 + 5 * i] = params[i]; // param id
+                    // nbus->tx_buffer[5+2*i] =
+                    // nbus->interface->getParam(0, params[i]);
+                    nbus->tx_buffer[5 + 5 * i] = (uint8_t)(param_value & 0xFF);
+                    nbus->tx_buffer[6 + 5 * i] = (uint8_t)((param_value >> 8) & 0xFF);
+                    nbus->tx_buffer[7 + 5 * i] = (uint8_t)((param_value >> 16) & 0xFF);
+                    nbus->tx_buffer[8 + 5 * i] = (uint8_t)((param_value >> 24) & 0xFF);
 
-        nbus->tx_length += 5;
+                    nbus->tx_length += 5;
+                }
+            }
+        }
     }
     break;
 
@@ -86,7 +93,8 @@ void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
     case CMD_INFO: {
         switch (nbus->rx_buffer[3])
         {
-        case INFO_MODULE_NAME:
+        case INFO_GENERAL: {
+            // name
             nbus->tx_buffer[4] = MODULE_NAME[0];
             nbus->tx_buffer[5] = MODULE_NAME[1];
             nbus->tx_buffer[6] = MODULE_NAME[2];
@@ -96,55 +104,54 @@ void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
             nbus->tx_buffer[10] = MODULE_NAME[6];
             nbus->tx_buffer[11] = MODULE_NAME[7];
             nbus->tx_length += 8;
-            break;
-        case INFO_MODULE_TYPE:
-            nbus->tx_buffer[4] = MODULE_TYPE[0];
-            nbus->tx_buffer[5] = MODULE_TYPE[1];
-            nbus->tx_buffer[6] = MODULE_TYPE[2];
+            // type
+            nbus->tx_buffer[12] = MODULE_TYPE[0];
+            nbus->tx_buffer[13] = MODULE_TYPE[1];
+            nbus->tx_buffer[14] = MODULE_TYPE[2];
             nbus->tx_length += 3;
-            break;
-        case INFO_MODULE_UUID: {
-// Reference manual: Unique device ID registers
+
+            // Reference manual: Unique device ID registers
 #if defined(STM32)
             uint32_t(*unique_id_3) = (uint32_t *)(0x1FF80064); // BASE address + 0x14 0ffset
 #elif defined(ESP32) || defined(NRF)
-            uint32_t unique_id_3[3] = {1, 2, 3};
+            uint32_t unique_id_3 = 0x12345678;
 #endif
-
-            *(nbus->tx_buffer) = (uint32_t)unique_id_3;
+            nbus->tx_buffer[15] = ((uint8_t *)(&unique_id_3))[0];
+            nbus->tx_buffer[16] = ((uint8_t *)(&unique_id_3))[1];
+            nbus->tx_buffer[17] = ((uint8_t *)(&unique_id_3))[2];
+            nbus->tx_buffer[18] = ((uint8_t *)(&unique_id_3))[3];
             nbus->tx_length += 4;
-        }
-        break;
-        case INFO_MODULE_FW:
-            nbus->tx_buffer[4] = VERSION_FW[0];
-            nbus->tx_buffer[5] = '.';
-            nbus->tx_buffer[6] = VERSION_FW[1];
+            // fw version
+            nbus->tx_buffer[19] = VERSION_FW[0];
+            nbus->tx_buffer[20] = '.';
+            nbus->tx_buffer[21] = VERSION_FW[1];
             nbus->tx_length += 3;
-            break;
-        case INFO_MODULE_HW:
-            nbus->tx_buffer[4] = VERSION_HW[0];
-            nbus->tx_buffer[5] = '.';
-            nbus->tx_buffer[6] = VERSION_HW[1];
+            // hw version
+            nbus->tx_buffer[22] = VERSION_HW[0];
+            nbus->tx_buffer[23] = '.';
+            nbus->tx_buffer[24] = VERSION_HW[1];
             nbus->tx_length += 3;
+            // module memory
+            nbus->memoryInterface->getId(&nbus->tx_buffer[25]);
+            nbus->tx_length += 8;
             break;
-        case INFO_MODULE_MEMORY_ID: {
-            uint8_t n = nbus->memoryInterface->getId(&nbus->tx_buffer[4]);
-            nbus->tx_length += n;
         }
-        case SENSOR_FORMAT: {
+
+        case INFO_FORMAT: {
             uint8_t sensor_cnt = nbus->interface->getSensorCount();
-            nBus_sensorFormat_t format;
 
             for (int8_t i = 0; i < sensor_cnt; ++i)
             {
-                format = nbus->interface->getSensorFormat(i + 1);
+                nBus_sensorFormat_t format = nbus->interface->getSensorFormat(i + 1);
+                uint8_t *format_ptr = (uint8_t *)&format;
                 nbus->tx_buffer[4 * i + 4] = i + 1;
-                nbus->tx_buffer[4 * i + 5] = (format.sign << 7) | format.unit_multiplier;
-                nbus->tx_buffer[4 * i + 6] = format.value_multiplier;
-                nbus->tx_buffer[4 * i + 7] = (format.byte_length << 4) | format.samples;
+                nbus->tx_buffer[4 * i + 5] = format_ptr[0];
+                nbus->tx_buffer[4 * i + 6] = format_ptr[1];
+                nbus->tx_buffer[4 * i + 7] = format_ptr[2];
             }
 
             nbus->tx_length += 4 * sensor_cnt;
+            break;
         }
         }
     }
@@ -160,6 +167,20 @@ void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
 {
     switch (nbus->function_code.function)
     {
+    case CMD_START: {
+        nbus->measure_active = MEASURE_RUNNING;
+        nbus->interface->start();
+        nbus->hw_platform->led_on();
+    }
+    break;
+
+    case CMD_STOP: {
+        nbus->measure_active = MEASURE_STOPPED;
+        nbus->interface->stop();
+        nbus->hw_platform->led_off();
+    }
+    break;
+
     case CMD_PARAM: {
         // same as nbus_unicastToSensorSet
         int32_t param_value =

+ 5 - 4
src/nbus_slave_sensor_unicast.c

@@ -82,12 +82,13 @@ void nbus_slave_unicastToSensorGet(nBus_TypeDef *nbus)
     case CMD_INFO: {
         switch (nbus->rx_buffer[3])
         {
-        case SENSOR_FORMAT: {
+        case INFO_FORMAT: {
             nBus_sensorFormat_t format = nbus->interface->getSensorFormat(nbus->sensorInfo.address);
+            uint8_t *format_ptr = (uint8_t *)&format;
             nbus->tx_buffer[4] = nbus->sensorInfo.address;
-            nbus->tx_buffer[5] = (format.sign << 7) | format.unit_multiplier;
-            nbus->tx_buffer[6] = format.value_multiplier;
-            nbus->tx_buffer[7] = (format.byte_length << 4) | format.samples;
+            nbus->tx_buffer[5] = format_ptr[0];
+            nbus->tx_buffer[6] = format_ptr[1];
+            nbus->tx_buffer[7] = format_ptr[2];
             nbus->tx_length += 4;
         }
         }