period 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <math.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include "rp.h"
  7. #include "rp_asg_axi.h"
  8. // g++ axi_gen.c -std=c++20 -Wall -I/opt/redpitaya/include -L/opt/redpitaya/lib -static -lrp-hw-can -lrp -lrp-hw-calib -lrp-hw-profiles -lrp-gpio -lrp-i2c -lrp-hw -lm -lstdc++ -lpthread -li2c -lsocketcan -o axi_gen
  9. int main(int argc, char **argv) {
  10. // Select channel
  11. rp_channel_t channel = RP_CH_1;
  12. // Data size in samples (16 bit)
  13. uint32_t bufferSize = 10; // Should be twice the actual number of samples
  14. uint32_t dec_factor = 1;
  15. uint32_t num_buffers = 1;
  16. uint32_t ncyc = 3;
  17. uint32_t nor = 3;
  18. uint32_t period = 100;
  19. // Start addresses of buffers must be multiple of 4096 (DDR page size)
  20. uint32_t min_addr_diff = 4096;
  21. // Initialize the interface
  22. if (rp_Init() != RP_OK) {
  23. fprintf(stderr, "Rp api init failed!\n");
  24. return 1;
  25. }
  26. // Reset generator
  27. rp_GenReset();
  28. /* Setting up the Deep Memory Generation (DMG) */
  29. uint32_t dac_axi_start, dac_axi_size;
  30. // Get the reserved memory region start and size
  31. if (rp_AcqAxiGetMemoryRegion(&dac_axi_start, &dac_axi_size) != RP_OK) {
  32. fprintf(stderr, "Error get memory!\n");
  33. return 1;
  34. }
  35. // Lower the buffer size to the available memory size (if needed)
  36. uint32_t dmg_min_size = (uint32_t) (ceil((float) bufferSize / (float) min_addr_diff) * (float) min_addr_diff * (float) num_buffers);
  37. printf("Buffer size: %d\n", bufferSize);
  38. printf("Minimum size: %d\n", dmg_min_size);
  39. if (dmg_min_size > dac_axi_size) {
  40. printf("Buffer size is too large, reducing to available memory size\n");
  41. bufferSize = dac_axi_size / num_buffers;
  42. printf("Buffer size reduced to %d\n", bufferSize);
  43. }
  44. // Calculate the start address
  45. uint32_t gen1_start_addr = dac_axi_start;
  46. // Reserve memory for the generator
  47. printf("Reserved memory for OUT1 Start: %x Size: %x End: %x\n", gen1_start_addr, bufferSize, gen1_start_addr + bufferSize);
  48. if (rp_GenAxiReserveMemory(channel, gen1_start_addr, gen1_start_addr + bufferSize) != RP_OK) {
  49. fprintf(stderr, "Error setting address for DMG mode for OUT1\n");
  50. return 1;
  51. }
  52. // Set decimation factor
  53. if (rp_GenAxiSetDecimationFactor(channel, dec_factor) != RP_OK) {
  54. fprintf(stderr, "Error setting decimation for generator OUT1\n");
  55. return 1;
  56. }
  57. // Enable DMG mode
  58. if (rp_GenAxiSetEnable(channel, true) != RP_OK) {
  59. fprintf(stderr, "Error enable axi mode for OUT1\n");
  60. return 1;
  61. }
  62. // Define output waveform for both channels
  63. bufferSize /= 2; // Samples are float32 which is 4 bytes, int16 is 2 byte
  64. float *t = (float *)malloc(bufferSize * sizeof(float));
  65. float *x = (float *)malloc(bufferSize * sizeof(float));
  66. for (uint32_t i = 1; i < bufferSize; i++) {
  67. t[i] = (2 * M_PI) / bufferSize * i;
  68. }
  69. for (uint32_t i = 0; i < bufferSize; ++i) {
  70. x[i] = sin(t[i]) + ((1.0 / 3.0) * sin(t[i] * 3));
  71. }
  72. // Configure calibration and offset
  73. rp_GenSetAmplitudeAndOffsetOrigin(channel);
  74. // Set burst parameters
  75. rp_GenMode(channel, RP_GEN_MODE_BURST);
  76. rp_GenBurstCount(channel, ncyc);
  77. rp_GenBurstRepetitions(channel, nor);
  78. rp_GenBurstPeriod(channel, period);
  79. rp_GenAxiWriteWaveform(channel, x, bufferSize);
  80. rp_GenOutEnable(channel);
  81. rp_GenTriggerOnly(channel);
  82. // Release resources
  83. free(t);
  84. free(x);
  85. rp_Release();
  86. return 0;
  87. }