294 update_complete_callback_ =
nullptr;
295 update_complete_callback_context_ =
nullptr;
298 if(transport_.
Init(
config.transport_config) != Transport::Result::OK)
301 sequencer_.Invalidate();
303 for(
size_t device_index = 0; device_index < transport_.GetNumDevices();
358 devices_[device_index].pin_configurations_[
i] =
pin_cfg;
370 auto& device = devices_[device_index];
378 device.pin_configurations_[pin].Defaults();
379 device.pin_configurations_[pin].mode = PinMode::GPI;
382 return SetPinConfig(device_index, pin);
389 auto& device = devices_[device_index];
397 device.pin_configurations_[pin].Defaults();
398 device.pin_configurations_[pin].mode = PinMode::GPO;
401 return SetPinConfig(device_index, pin);
409 auto& device = devices_[device_index];
411 device.pin_configurations_[pin].Defaults();
412 device.pin_configurations_[pin].mode = PinMode::ANALOG_IN;
413 device.pin_configurations_[pin].range.adc = range;
415 return SetPinConfig(device_index, pin);
423 auto& device = devices_[device_index];
425 device.pin_configurations_[pin].Defaults();
426 device.pin_configurations_[pin].mode = PinMode::ANALOG_OUT;
427 device.pin_configurations_[pin].range.dac = range;
429 return SetPinConfig(device_index, pin);
435 auto& device = devices_[device_index];
436 device.pin_configurations_[pin].Defaults();
437 return SetPinConfig(device_index, pin);
450 auto& device = devices_[device_index];
452 if(device.pin_configurations_[pin].value ==
nullptr)
469 auto& device = devices_[device_index];
473 device.pin_configurations_[pin].range.adc);
488 auto& device = devices_[device_index];
490 if(device.pin_configurations_[pin].value !=
nullptr)
492 *device.pin_configurations_[pin].value
510 auto& device = devices_[device_index];
512 auto pin_config = device.pin_configurations_[pin];
529 auto& device = devices_[device_index];
533 return static_cast<bool>((device.gpi_buffer_[4] >> (pin - 16)) & 1);
537 return static_cast<bool>((device.gpi_buffer_[1] >> (pin - 8)) & 1);
541 return static_cast<bool>((device.gpi_buffer_[2] >> pin) & 1);
556 auto& device = devices_[device_index];
564 device.gpo_buffer_[4] |= (1 << (pin - 16));
568 device.gpo_buffer_[1] |= (1 << (pin - 8));
572 device.gpo_buffer_[2] |= (1 << pin);
579 device.gpo_buffer_[4] &= ~(1 << (pin - 16));
583 device.gpo_buffer_[1] &= ~(1 << (pin - 8));
587 device.gpo_buffer_[2] &= ~(1 << pin);
614 if(sequencer_.IsBusy() && run_)
627 sequencer_.current_device_ = 0;
628 sequencer_.current_step_ = UpdateSequencer::first_step_;
768 mode = PinMode::NONE;
784 uint16_t pin_func_cfg = 0x0000;
785 auto& device = devices_[device_index];
787 if(device.pin_configurations_[pin].mode != PinMode::NONE)
800 |
static_cast<uint16_t
>(device.pin_configurations_[pin].mode);
802 if(device.pin_configurations_[pin].mode == PinMode::ANALOG_OUT)
804 pin_func_cfg |=
static_cast<uint16_t
>(
805 device.pin_configurations_[pin].range.dac);
807 else if(device.pin_configurations_[pin].mode == PinMode::ANALOG_IN)
810 pin_func_cfg = pin_func_cfg | 0x00e0
811 |
static_cast<uint16_t
>(
812 device.pin_configurations_[pin].range.adc);
814 else if(device.pin_configurations_[pin].mode == PinMode::GPI)
820 WriteRegister(device_index,
823 device.pin_configurations_[pin].threshold,
824 device.pin_configurations_[pin].range.dac));
826 else if(device.pin_configurations_[pin].mode == PinMode::GPO)
830 WriteRegister(device_index,
833 device.pin_configurations_[pin].threshold,
834 device.pin_configurations_[pin].range.dac));
854 UpdatePinConfig(device_index);
865 auto& device = devices_[device_index];
868 std::memset(device.dac_buffer_, 0,
sizeof(device.dac_buffer_));
869 std::memset(device.adc_buffer_, 0,
sizeof(device.adc_buffer_));
870 std::memset(device.gpi_buffer_, 0,
sizeof(device.gpi_buffer_));
871 std::memset(device.gpo_buffer_, 0,
sizeof(device.gpo_buffer_));
873 device.dac_pin_count_ = 0;
874 device.adc_pin_count_ = 0;
875 device.gpi_pin_count_ = 0;
876 device.gpo_pin_count_ = 0;
883 device.pin_configurations_[i].value =
nullptr;
885 if(device.pin_configurations_[i].mode == PinMode::ANALOG_OUT)
887 device.dac_pin_count_++;
888 if(device.dac_pin_count_ == 1)
897 device.pin_configurations_[i].value
898 =
reinterpret_cast<uint16_t*
>(
899 &device.dac_buffer_[(2 * device.dac_pin_count_) - 1]);
901 else if(device.pin_configurations_[i].mode == PinMode::ANALOG_IN)
903 device.adc_pin_count_++;
904 if(device.adc_pin_count_ == 1)
909 device.adc_first_adress
914 device.pin_configurations_[i].value
915 =
reinterpret_cast<uint16_t*
>(
916 &device.adc_buffer_[(2 * device.adc_pin_count_) - 1]);
918 else if(device.pin_configurations_[i].mode == PinMode::GPI)
920 device.gpi_pin_count_++;
922 else if(device.pin_configurations_[i].mode == PinMode::GPO)
924 device.gpo_pin_count_++;
933 void DelayUs(uint32_t delay)
946 uint16_t ReadRegister(
size_t device_index, uint8_t address)
949 ReadRegister(device_index, address, &val, 1);
965 size_t rx_length = (size * 2) + 1;
968 tx_buff[0] = (address << 1) | 1;
970 if(transport_.TransmitAndReceiveBlocking(
971 device_index, tx_buff, rx_buff, rx_length)
972 != Transport::Result::OK)
978 for(
size_t i = 0; i < size; i++)
980 values[i] =
static_cast<uint16_t
>((rx_buff[rx_idx] << 8)
981 + rx_buff[rx_idx + 1]);
994 WriteRegister(
size_t device_index, uint8_t address, uint16_t value)
996 return WriteRegister(device_index, address, &value, 1);
1011 size_t tx_size = (size * 2) + 1;
1013 tx_buff[0] = (address << 1);
1016 for(
size_t i = 0; i < size; i++)
1018 tx_buff[tx_idx++] =
static_cast<uint8_t
>(values[i] >> 8);
1019 tx_buff[tx_idx++] =
static_cast<uint8_t
>(values[i]);
1022 if(transport_.TransmitBlocking(device_index, tx_buff, tx_size)
1023 == Transport::Result::OK)
1044 uint16_t reg = ReadRegister(device_index, address);
1045 reg = (reg & ~mask) | (uint16_t)(value);
1046 return WriteRegister(device_index, address, reg);
1049 void ContinueUpdate()
1052 switch(sequencer_.current_step_)
1054 case UpdateSequencer::Step::start:
1055 sequencer_.current_device_ = 0;
1056 sequencer_.current_step_ = UpdateSequencer::Step::updateDac;
1058 case UpdateSequencer::Step::updateDac:
1060 sequencer_.current_step_ = UpdateSequencer::Step::updateAdc;
1062 case UpdateSequencer::Step::updateAdc:
1066 = (devices_[sequencer_.current_device_].adc_pin_count_ * 2)
1068 memcpy(devices_[sequencer_.current_device_].adc_buffer_,
1071 sequencer_.current_step_ = UpdateSequencer::Step::updateGpo;
1074 case UpdateSequencer::Step::updateGpo:
1076 sequencer_.current_step_ = UpdateSequencer::Step::updateGpi;
1078 case UpdateSequencer::Step::updateGpi:
1082 =
sizeof(devices_[sequencer_.current_device_].gpi_buffer_);
1083 memcpy(devices_[sequencer_.current_device_].gpi_buffer_,
1087 sequencer_.current_device_++;
1088 sequencer_.current_step_ = UpdateSequencer::Step::updateDac;
1096 if(sequencer_.current_device_ >= num_devices)
1098 sequencer_.Invalidate();
1099 if(update_complete_callback_)
1100 update_complete_callback_(
1101 update_complete_callback_context_);
1105 sequencer_.current_device_ = 0;
1106 sequencer_.current_step_ = UpdateSequencer::Step::updateDac;
1112 auto& device = devices_[sequencer_.current_device_];
1114 switch(sequencer_.current_step_)
1116 case UpdateSequencer::Step::updateDac:
1117 if(device.dac_pin_count_ > 0)
1129 size_t tx_size = (device.dac_pin_count_ * 2) + 1;
1133 transport_.TransmitDma(sequencer_.current_device_,
1136 &DmaCompleteCallback,
1142 sequencer_.current_step_
1143 = UpdateSequencer::Step::updateAdc;
1147 case UpdateSequencer::Step::updateAdc:
1148 if(device.adc_pin_count_ > 0)
1152 size_t size = (device.adc_pin_count_ * 2) + 1;
1153 dma_buffer_->
tx_buffer[0] = device.adc_first_adress;
1155 for(
size_t i = 1; i < size; i++)
1157 transport_.TransmitAndReceiveDma(
1158 sequencer_.current_device_,
1162 &DmaCompleteCallback,
1168 sequencer_.current_step_
1169 = UpdateSequencer::Step::updateGpo;
1173 case UpdateSequencer::Step::updateGpo:
1174 if(device.gpo_pin_count_ > 0)
1181 sizeof(device.gpo_buffer_));
1183 transport_.TransmitDma(sequencer_.current_device_,
1185 sizeof(device.gpo_buffer_),
1186 &DmaCompleteCallback,
1192 sequencer_.current_step_
1193 = UpdateSequencer::Step::updateGpi;
1198 case UpdateSequencer::Step::updateGpi:
1199 if(device.gpi_pin_count_ > 0)
1205 for(
size_t i = 1; i <
sizeof(device.gpi_buffer_); i++)
1207 transport_.TransmitAndReceiveDma(
1208 sequencer_.current_device_,
1211 sizeof(device.gpi_buffer_),
1212 &DmaCompleteCallback,
1218 sequencer_.current_step_
1219 = UpdateSequencer::Step::updateDac;
1235 driver.ContinueUpdate();
1239 driver.sequencer_.Invalidate();
1243 MAX11300Types::DmaBuffer* dma_buffer_;
1247 PinConfig pin_configurations_[20];
1248 uint8_t dac_pin_count_;
1249 uint8_t adc_pin_count_;
1250 uint8_t gpi_pin_count_;
1251 uint8_t gpo_pin_count_;
1252 uint8_t dac_buffer_[41];
1253 uint8_t adc_first_adress;
1254 uint8_t adc_buffer_[41];
1255 uint8_t gpi_buffer_[5];
1256 uint8_t gpo_buffer_[5];
1258 Device devices_[num_devices];
1260 struct UpdateSequencer
1262 size_t current_device_ = 0;
1271 static constexpr auto first_step_ = Step::start;
1272 static constexpr auto last_step_ = Step::updateGpi;
1273 Step current_step_ = first_step_;
1274 bool IsBusy()
const {
return current_device_ < num_devices; }
1277 current_device_ = num_devices;
1278 current_step_ = first_step_;
1282 Transport transport_;
1285 void* update_complete_callback_context_;