|
|
@@ -1,51 +1,80 @@
|
|
|
#include "nbus_slave.h"
|
|
|
-
|
|
|
#if MODULE_SLAVE == 1
|
|
|
|
|
|
void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
|
|
|
{
|
|
|
switch (nbus->function_code.function)
|
|
|
{
|
|
|
-
|
|
|
case CMD_ECHO: {
|
|
|
- for (uint8_t i = 3; i < nbus->rx_length - 1; i++)
|
|
|
+ for (uint8_t i = RX_DT; i < nbus->rx_length - 1; i++)
|
|
|
{
|
|
|
nbus->tx_buffer[i + 1] = nbus->rx_buffer[i];
|
|
|
}
|
|
|
- nbus->tx_length += (nbus->rx_length - 4);
|
|
|
+ nbus->tx_length += (nbus->rx_length - RX_META);
|
|
|
}
|
|
|
-
|
|
|
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)
|
|
|
+
|
|
|
+ switch (nbus->rx_length)
|
|
|
{
|
|
|
- setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
|
|
|
- break;
|
|
|
+ case RX_META: // get all params
|
|
|
+ {
|
|
|
+ for (uint8_t i = 0, j = 0; i < _NBUS_PARAM_COUNT; i++)
|
|
|
+ {
|
|
|
+ if (nbus->interface->hasParam(0, i))
|
|
|
+ {
|
|
|
+ int32_t param_value = nbus->interface->getParam(0, i);
|
|
|
+ nbus->tx_buffer[4 + 5 * j] = i; // param id
|
|
|
+ nbus->tx_buffer[5 + 5 * j] = (uint8_t)(param_value & 0xFF); // param value 4B
|
|
|
+ nbus->tx_buffer[6 + 5 * j] = (uint8_t)((param_value >> 8) & 0xFF);
|
|
|
+ nbus->tx_buffer[7 + 5 * j] = (uint8_t)((param_value >> 16) & 0xFF);
|
|
|
+ nbus->tx_buffer[8 + 5 * j] = (uint8_t)((param_value >> 24) & 0xFF);
|
|
|
+ nbus->tx_length += 5;
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+ break;
|
|
|
|
|
|
- nbus->tx_buffer[4] = (uint8_t)(param_value & 0xFF);
|
|
|
- nbus->tx_buffer[5] = (uint8_t)((param_value >> 8) & 0xFF);
|
|
|
- nbus->tx_buffer[6] = (uint8_t)((param_value >> 16) & 0xFF);
|
|
|
- nbus->tx_buffer[7] = (uint8_t)((param_value >> 24) & 0xFF);
|
|
|
+ case RX_META + 1: // get specified parameter: 4 meta + 1 param id
|
|
|
+ {
|
|
|
+ if (nbus->interface->hasParam(0, (nBus_param_t)nbus->rx_buffer[RX_DT]) == 0) // handle non-existing param
|
|
|
+ {
|
|
|
+ setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
- nbus->tx_length += 4;
|
|
|
+ nbus->tx_buffer[4] = nbus->rx_buffer[RX_DT]; // param id
|
|
|
+ int32_t param_value = nbus->interface->getParam(0, (nBus_param_t)nbus->rx_buffer[RX_DT]); // param value 4B
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ setErrorResponse(nbus, ILLEGAL_FUNCTION_PARAM);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case CMD_SENSOR_CNT: {
|
|
|
- nbus->tx_buffer[4] = nbus->interface->getSensorCount();
|
|
|
- nbus->tx_length += 1;
|
|
|
+ nBus_sensorCount_t sensor_count = nbus->interface->getSensorCount();
|
|
|
+ nbus->tx_buffer[4] = sensor_count.read_only_count;
|
|
|
+ nbus->tx_buffer[5] = sensor_count.read_write_count;
|
|
|
+ nbus->tx_length += 2;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case CMD_DATA: {
|
|
|
- if (nbus->measure_active == MEASURE_RUNNING)
|
|
|
+ if (nbus->interface->device_ready())
|
|
|
{
|
|
|
// response: sensor1_index:sensor1_data | sensor2_index:sensor2_data | ...
|
|
|
- uint8_t cnt = nbus->interface->getData(0, &nbus->tx_buffer[4]);
|
|
|
+ uint8_t cnt = nbus->interface->getData(0, &nbus->tx_buffer[TX_DT]);
|
|
|
if (cnt == 0)
|
|
|
{
|
|
|
setErrorResponse(nbus, DEVICE_BUSY);
|
|
|
@@ -60,65 +89,110 @@ void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
|
|
|
break;
|
|
|
|
|
|
case CMD_SENSOR_TYPE: {
|
|
|
+
|
|
|
+ nBus_sensorCount_t sensor_count = nbus->interface->getSensorCount();
|
|
|
+
|
|
|
// response: sensor1_index:sensor1_type | sensor2_index:sensor2_type | ...
|
|
|
- for (uint8_t i = 0; i < nbus->interface->getSensorCount(); i++)
|
|
|
+
|
|
|
+ // add read_only sensors
|
|
|
+ uint8_t i = 0;
|
|
|
+ for (; i < sensor_count.read_only_count; i++)
|
|
|
{
|
|
|
- nbus->tx_buffer[4 + 2 * i] = i;
|
|
|
- nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(i);
|
|
|
+ nbus->tx_buffer[4 + 2 * i] = i + SENSOR_RO_ADDR;
|
|
|
+ nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(i + SENSOR_RO_ADDR);
|
|
|
+ nbus->tx_length += 2;
|
|
|
+ }
|
|
|
+
|
|
|
+ // add read_write sensors
|
|
|
+ for (uint8_t j = 0; j < sensor_count.read_write_count; j++, i++)
|
|
|
+ {
|
|
|
+ nbus->tx_buffer[4 + 2 * i] = j + SENSOR_RW_ADDR;
|
|
|
+ nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(j + SENSOR_RW_ADDR);
|
|
|
nbus->tx_length += 2;
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case CMD_INFO: {
|
|
|
- switch (nbus->rx_buffer[3])
|
|
|
- {
|
|
|
- case INFO_MODULE_NAME:
|
|
|
- nbus->tx_buffer[4] = MODULE_NAME[0];
|
|
|
- nbus->tx_buffer[5] = MODULE_NAME[1];
|
|
|
- nbus->tx_buffer[6] = MODULE_NAME[2];
|
|
|
- nbus->tx_buffer[7] = MODULE_NAME[3];
|
|
|
- nbus->tx_buffer[8] = MODULE_NAME[4];
|
|
|
- nbus->tx_buffer[9] = MODULE_NAME[5];
|
|
|
- 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];
|
|
|
- nbus->tx_length += 3;
|
|
|
- break;
|
|
|
- case INFO_MODULE_UUID: {
|
|
|
-// Reference manual: Unique device ID registers
|
|
|
+ // name
|
|
|
+ nbus->tx_buffer[4] = MODULE_NAME[0];
|
|
|
+ nbus->tx_buffer[5] = MODULE_NAME[1];
|
|
|
+ nbus->tx_buffer[6] = MODULE_NAME[2];
|
|
|
+ nbus->tx_buffer[7] = MODULE_NAME[3];
|
|
|
+ nbus->tx_buffer[8] = MODULE_NAME[4];
|
|
|
+ nbus->tx_buffer[9] = MODULE_NAME[5];
|
|
|
+ nbus->tx_buffer[10] = MODULE_NAME[6];
|
|
|
+ nbus->tx_buffer[11] = MODULE_NAME[7];
|
|
|
+ nbus->tx_length += 8;
|
|
|
+ // 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;
|
|
|
+
|
|
|
+ // uuid - reference manual: Unique device ID registers
|
|
|
#if defined(STM32)
|
|
|
- uint32_t(*unique_id_3) = (uint32_t *)(0x1FF80064); // BASE address + 0x14 0ffset
|
|
|
+ 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[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;
|
|
|
+ // fw version
|
|
|
+ nbus->tx_buffer[19] = VERSION_FW[0];
|
|
|
+ nbus->tx_buffer[20] = '.';
|
|
|
+ nbus->tx_buffer[21] = VERSION_FW[1];
|
|
|
+ nbus->tx_length += 3;
|
|
|
+ // 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;
|
|
|
+ // sensor count
|
|
|
+ nBus_sensorCount_t sensor_count = nbus->interface->getSensorCount();
|
|
|
+ nbus->tx_buffer[33] = sensor_count.read_only_count;
|
|
|
+ nbus->tx_buffer[34] = sensor_count.read_write_count;
|
|
|
+ nbus->tx_length += 2;
|
|
|
+ }
|
|
|
+ break;
|
|
|
|
|
|
- *(nbus->tx_buffer) = (uint32_t)unique_id_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];
|
|
|
- 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];
|
|
|
- nbus->tx_length += 3;
|
|
|
- break;
|
|
|
- case INFO_MODULE_MEMORY_ID: {
|
|
|
- uint8_t n = nbus->memoryInterface->getId(&nbus->tx_buffer[4]);
|
|
|
- nbus->tx_length += n;
|
|
|
+ case CMD_FORMAT: {
|
|
|
+ nBus_sensorCount_t sensor_count = nbus->interface->getSensorCount();
|
|
|
+ nBus_sensorFormat_t format;
|
|
|
+ uint8_t *format_ptr;
|
|
|
+
|
|
|
+ // add read_only sensors
|
|
|
+ uint8_t i = 0;
|
|
|
+ for (; i < sensor_count.read_only_count; ++i)
|
|
|
+ {
|
|
|
+ format = nbus->interface->getSensorFormat(i + SENSOR_RO_ADDR);
|
|
|
+ format_ptr = (uint8_t *)&format;
|
|
|
+ nbus->tx_buffer[4 * i + 4] = i + SENSOR_RO_ADDR;
|
|
|
+ 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_count.read_only_count;
|
|
|
+
|
|
|
+ // add read_write sensors
|
|
|
+ for (uint8_t j = 0; j < sensor_count.read_write_count; ++j, ++i)
|
|
|
+ {
|
|
|
+ format = nbus->interface->getSensorFormat(j + SENSOR_RW_ADDR);
|
|
|
+ format_ptr = (uint8_t *)&format;
|
|
|
+ nbus->tx_buffer[4 * i + 4] = j + SENSOR_RW_ADDR;
|
|
|
+ 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_count.read_write_count;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -132,29 +206,50 @@ void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
|
|
|
{
|
|
|
switch (nbus->function_code.function)
|
|
|
{
|
|
|
- case CMD_PARAM: {
|
|
|
- // same as nbus_unicastToSensorSet
|
|
|
- int32_t param_value =
|
|
|
- nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 | nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
|
|
|
- nBus_param_t p = nbus->interface->setParam(0, (nBus_param_t)nbus->rx_buffer[3], param_value);
|
|
|
- if (p == PARAM_NONE)
|
|
|
- {
|
|
|
- setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
|
|
|
- break;
|
|
|
- }
|
|
|
- nbus->tx_buffer[4] = OK_CODE;
|
|
|
- nbus->tx_length += 1;
|
|
|
+ case CMD_STOP: {
|
|
|
+ nbus->measure_active = MEASURE_STOPPED;
|
|
|
+ nbus->tx_buffer[TX_DT] = nbus->interface->stop();
|
|
|
+ nbus->tx_length++;
|
|
|
+ nbus->hw_platform->led_off();
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
- case CMD_DATA: {
|
|
|
- nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
|
|
|
- if (nbus->tx_buffer[4] != OK_CODE)
|
|
|
+ case CMD_START: {
|
|
|
+ nbus->measure_active = MEASURE_RUNNING;
|
|
|
+ nbus->tx_buffer[TX_DT] = nbus->interface->start();
|
|
|
+ nbus->tx_length++;
|
|
|
+ nbus->hw_platform->led_on();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case CMD_PARAM: {
|
|
|
+
|
|
|
+ uint8_t rx_payload = nbus->rx_length - RX_META;
|
|
|
+
|
|
|
+ // empty or wrong number of parameters
|
|
|
+ if (rx_payload == 0 || rx_payload % 5 != 0)
|
|
|
{
|
|
|
- nbus->function_code.error = 1;
|
|
|
+ setErrorResponse(nbus, ILLEGAL_FUNCTION_PARAM);
|
|
|
+ break;
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ uint8_t param_id;
|
|
|
+ uint32_t param_value;
|
|
|
|
|
|
- nbus->tx_length += 1;
|
|
|
+ uint8_t i = 0;
|
|
|
+
|
|
|
+ for (; rx_payload > 0; rx_payload -= 5)
|
|
|
+ {
|
|
|
+ param_id = nbus->rx_buffer[3 + 5 * i];
|
|
|
+ param_value = *(int32_t *)&nbus->rx_buffer[4 + 5 * i];
|
|
|
+
|
|
|
+ nbus->tx_buffer[4 + 2 * i] = param_id;
|
|
|
+ nbus->tx_buffer[5 + 2 * i] = nbus->interface->setParam(0, param_id, param_value);
|
|
|
+ nbus->tx_length += 2;
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -172,12 +267,19 @@ void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
|
|
|
|
|
|
case CMD_CALIBRATE: {
|
|
|
nbus->hw_platform->led_on();
|
|
|
- nbus->tx_buffer[4] = nbus->interface->calibrate(0, 0, NULL);
|
|
|
+ nbus->tx_buffer[TX_DT] = nbus->interface->calibrate(0);
|
|
|
nbus->tx_length += 1;
|
|
|
nbus->hw_platform->led_off();
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
+ case CMD_DATA: {
|
|
|
+
|
|
|
+ nbus->tx_length +=
|
|
|
+ nbus->interface->setData(&nbus->rx_buffer[RX_DT], nbus->rx_length - RX_META, &nbus->tx_buffer[TX_DT]);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
case CMD_RESET: {
|
|
|
// POZOR! cas fomatovania: 0.3s (pri 1W pamati)
|
|
|
memory_params_format();
|