| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- #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++) {
- nbus->tx_buffer[i + 1] = nbus->rx_buffer[i];
- }
- nbus->tx_length += (nbus->rx_length - 4);
- }
- 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) {
- setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
- 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);
- nbus->tx_length += 4;
- } break;
- case CMD_SENSOR_CNT: {
- nbus->tx_buffer[4] = nbus->interface->getSensorCount();
- nbus->tx_length += 1;
- } break;
- case CMD_DATA: {
- if (nbus->measure_active == MEASURE_RUNNING) {
- // response: sensor1_index:sensor1_data | sensor2_index:sensor2_data | ...
- uint8_t cnt = nbus->interface->getData(0, &nbus->tx_buffer[4]);
- if (cnt == 0) {
- setErrorResponse(nbus, DEVICE_BUSY);
- }
- nbus->tx_length += cnt;
- } else {
- setErrorResponse(nbus, DEVICE_NOT_READY);
- }
- } break;
- case CMD_SENSOR_TYPE: {
- // response: sensor1_index:sensor1_type | sensor2_index:sensor2_type | ...
- for (uint8_t i = 0; i < nbus->interface->getSensorCount(); i++) {
- nbus->tx_buffer[4 + 2 * i] = i;
- nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(i);
- 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
- uint32_t(*unique_id_3) =
- (uint32_t *)(0x1FF80064); // BASE address + 0x14 0ffset
- *(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;
- }
- }
- } break;
- default: {
- setErrorResponse(nbus, ILLEGAL_FUNCTION);
- }
- }
- }
- 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;
- } break;
- case CMD_DATA: {
- nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
- if (nbus->tx_buffer[4] != OK_CODE) {
- nbus->function_code.error = 1;
- }
- nbus->tx_length += 1;
- } break;
- case CMD_SLEEP: {
- nbus->tx_buffer[4] = OK_CODE;
- nbus->tx_length += 1;
- } break;
- case CMD_WAKEUP: {
- nbus->tx_buffer[4] = OK_CODE;
- nbus->tx_length += 1;
- }
- case CMD_STORE: {
- if (nbus->interface->store != NULL) {
- nbus->interface->store();
- nbus->tx_buffer[4] = OK_CODE;
- } else {
- nbus->tx_buffer[4] = ILLEGAL_FUNCTION;
- }
- nbus->tx_length += 1;
- } break;
- default: {
- setErrorResponse(nbus, ILLEGAL_FUNCTION);
- }
- }
- }
- #endif
|