Skip to content
Snippets Groups Projects
Commit 994a2d1b authored by aliya.myaz's avatar aliya.myaz
Browse files

add sources

parent 25fa76ba
Branches
No related tags found
No related merge requests found
# semester_project_sources_myaz_aliya
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitedu.hesge.ch/aliya.myaz/semester_project_sources_myaz_aliya.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitedu.hesge.ch/aliya.myaz/semester_project_sources_myaz_aliya/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
This repository contains both sources for the RedPitaya and guest's software.
You can also find the Makefile to compile the Repitaya program, which differs from the default one.
/*
Author : Aliya Myaz
Project : Semester Project
Description : Main program
Version : 9 (last)
Date : 21.03.25
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#include <netinet/tcp.h> // defines TCP_NODELAY
#define REDPITAYA_IP "10.42.0.101"
#define PORT 5000
#define ACQUIRED_SIGNAL_NB_SAMPLES 16384
#define SPECTRUM_NB_SAMPLES ACQUIRED_SIGNAL_NB_SAMPLES / 2
#define BUFFER_SIZE (ACQUIRED_SIGNAL_NB_SAMPLES * sizeof(float))
#define SPECTRUM_BUFFER_SIZE (SPECTRUM_NB_SAMPLES * sizeof(float))
#define PARAM_BUFFER_SIZE sizeof(float)
// Sinc default parameters
#define F_BW 20000 // Band width [Hz] (Sinc frequency) // 20000.0
#define F_CENTRAL 2000000.0
#define MAX_ACQ 100000
#define MAX_CMD_LEN 8
typedef enum {
UNKNOWN = 0,
NEW_SAMPLE = 1, // guest will send parameters after this command
ACK = 2,
START_ACQ = 3, // one acq. will be done and data will be sent to client
SET_FREQ = 4,
SET_BW = 5,
GET_FREQ = 6,
GET_AMPL = 7,
GET_PHASE = 8,
GET_Q_FACTOR= 9,
GET_SIGNAL = 10,
GET_SPECTRUM= 11,
} cmd_t;
// Commands labels
const char * opcodes[] = {
"",
"new",
"ack", // acknowlege for "new"
"sta",
"set_freq",
"set_band",
"get_freq",
"get_ampl",
"get_phas",
"get_qfac",
"get_sign",
"get_spec"
};
// Configuration
typedef struct parameters {
uint32_t central_freq;
uint32_t band_width;
uint8_t send_f;
uint8_t send_ampl;
uint8_t send_phase;
uint8_t send_q_factor;
uint8_t send_sig;
uint8_t send_spec;
} param_t;
// Last acquisition's data
typedef struct data{
float * signal;
float * spectrum; // amplitudes only
float freq;
float ampl;
float phase;
float q_factor;
} data_t;
data_t data;
// Init with default parameters
param_t init_parameters(void) {
param_t param;
param.band_width = F_BW;
param.central_freq = F_CENTRAL;
param.send_f = 1;
param.send_phase = 1;
param.send_ampl = 1;
param.send_q_factor = 1;
param.send_sig = 1;
param.send_spec = 1;
return param;
}
void send_acquisition_cmd(int client_socket, uint8_t opcode) {
//const char *cmd = "start\n";
char cmd[100]; // Assurez-vous que la taille est suffisante
sprintf(cmd, "%s\n", opcodes[opcode]);
if (send(client_socket, cmd, strlen(cmd), 0) < 0) {
perror("Error : sending command");
}
}
double get_time_ms() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000.0 + ts.tv_nsec / 1.0e6; // convert to ms
}
ssize_t recv_all(int socket, void *buffer, size_t length) {
size_t total_received = 0;
char *buf = (char *)buffer;
while (total_received < length) {
ssize_t bytes_received = recv(socket, buf + total_received, length - total_received, 0);
if (bytes_received <= 0) {
return bytes_received; // error or disconnect
}
total_received += bytes_received;
}
return total_received; // nb bytes received
}
int main() {
param_t param = init_parameters();
// Open files for data
FILE *freq_file = fopen("freq_data.csv", "a");
FILE *ampl_file = fopen("ampl_data.csv", "a");
FILE *phase_file = fopen("phase_data.csv", "a");
FILE *qfactor_file = fopen("qfactor_data.csv", "a");
if (!freq_file || !ampl_file || !phase_file || !qfactor_file) {
perror("Error while opening CSV files");
return EXIT_FAILURE;
}
int client_socket;
struct sockaddr_in server_addr;
char buffer_signal[BUFFER_SIZE];
char buffer_spectrum[SPECTRUM_BUFFER_SIZE];
char buffer_param[PARAM_BUFFER_SIZE];
// --- Create socket ---
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Erreur lors de la création du socket");
return EXIT_FAILURE;
}
// force to send commands immediatly (no bufferisation)
int flag = 1;
//setsockopt(client_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
// --- Configure server address ---
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, REDPITAYA_IP, &server_addr.sin_addr) <= 0) {
perror("Error : invalid address");
close(client_socket);
return EXIT_FAILURE;
}
// --- Connect to server ---
if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Error : connection with server");
close(client_socket);
return EXIT_FAILURE;
}
printf("Connected to Redpitaya server\n");
// "new" command
send_acquisition_cmd(client_socket, NEW_SAMPLE);
// --- Acquisitions ---
int count = 0;
int end = 0;
double total_start_time = get_time_ms();
while (!end) {
// Wait for start command from user (interactive interface)
printf("Press ENTER to start an acquisition...\n");
getchar();
// Acquisition command ("start")
send_acquisition_cmd(client_socket, START_ACQ);
printf("Acquisiton command sent\n");
// Start timer
double start_time = get_time_ms();
// Receive data
if(param.send_f) {
size_t total_received = 0;
while (total_received < PARAM_BUFFER_SIZE) {
ssize_t bytes_received = recv(client_socket, buffer_param + total_received, PARAM_BUFFER_SIZE - total_received, 0);
if (bytes_received <= 0) {
perror("Error : reception or closed connexion");
close(client_socket);
return EXIT_FAILURE;
}
total_received += bytes_received;
}
data.freq = *(float *)buffer_param;
}
if(param.send_ampl) {
size_t total_received = 0;
while (total_received < PARAM_BUFFER_SIZE) {
ssize_t bytes_received = recv(client_socket, buffer_param + total_received, PARAM_BUFFER_SIZE - total_received, 0);
if (bytes_received <= 0) {
perror("Error : reception or closed connexion");
close(client_socket);
return EXIT_FAILURE;
}
total_received += bytes_received;
}
data.ampl = *(float *)buffer_param;
}
if(param.send_phase) {
size_t total_received = 0;
while (total_received < PARAM_BUFFER_SIZE) {
ssize_t bytes_received = recv(client_socket, buffer_param + total_received, PARAM_BUFFER_SIZE - total_received, 0);
if (bytes_received <= 0) {
perror("Error : reception or closed connexion");
close(client_socket);
return EXIT_FAILURE;
}
total_received += bytes_received;
}
data.phase = *(float *)buffer_param;
}
if(param.send_q_factor) {
size_t total_received = 0;
while (total_received < PARAM_BUFFER_SIZE) {
ssize_t bytes_received = recv(client_socket, buffer_param + total_received, PARAM_BUFFER_SIZE - total_received, 0);
if (bytes_received <= 0) {
perror("Error : reception or closed connexion");
close(client_socket);
return EXIT_FAILURE;
}
total_received += bytes_received;
}
data.q_factor = *(float *)buffer_param;
}
if(param.send_sig) {
size_t total_received = 0;
while (total_received < BUFFER_SIZE) {
ssize_t bytes_received = recv(client_socket, buffer_signal + total_received, BUFFER_SIZE - total_received, 0);
if (bytes_received <= 0) {
perror("Error : reception or closed connexion");
close(client_socket);
return EXIT_FAILURE;
}
total_received += bytes_received;
}
data.signal = (float *)buffer_signal;
}
if(param.send_spec) {
size_t total_received = 0;
while (total_received < SPECTRUM_BUFFER_SIZE) {
ssize_t bytes_received = recv(client_socket, buffer_spectrum + total_received, SPECTRUM_BUFFER_SIZE - total_received, 0);
if (bytes_received <= 0) {
perror("Error : reception or closed connexion");
close(client_socket);
return EXIT_FAILURE;
}
total_received += bytes_received;
}
data.spectrum = (float *)buffer_spectrum;
}
double end_time = get_time_ms();
double elapsed_time = end_time - start_time;
// -- Display --
if(param.send_sig){
printf("Received signal (10 first samples) : ");
for (int i = 0; i < 10; i++) {
printf("%f ", data.signal[i]);
}
printf("\n");
}
if(param.send_spec) {
printf("Received spectrum (10 first samples) : ");
for (int i = 0; i < 10; i++) {
printf("%f ", data.spectrum[i]);
}
printf("\n");
}
if(param.send_f)
printf("Peak freq : %f\n", data.freq);
if(param.send_ampl)
printf("Amplitude : %f\n", data.ampl);
if(param.send_phase)
printf("Phase : %f\n", data.phase);
if(param.send_q_factor)
printf("Q factor : %f\n", data.q_factor);
// Display time analysis
printf("Elapsed time during reception : %.3f ms\n\n", elapsed_time);
// Store data in csv file
if (param.send_f) {
fprintf(freq_file, "%f\n", data.freq);
fflush(freq_file);
}
if (param.send_ampl) {
fprintf(ampl_file, "%f\n", data.ampl);
fflush(ampl_file);
}
if (param.send_phase) {
fprintf(phase_file, "%f\n", data.phase);
fflush(phase_file);
}
if (param.send_q_factor) {
fprintf(qfactor_file, "%f\n", data.q_factor);
fflush(qfactor_file);
}
FILE *signal_file = fopen("signal.csv", "w");
if (signal_file) {
for (int i = 0; i < ACQUIRED_SIGNAL_NB_SAMPLES; i++) {
fprintf(signal_file, "%f\n", data.signal[i]);
}
fclose(signal_file);
} else {
perror("Error while opening signal.csv");
}
FILE *spectrum_file = fopen("spectrum.csv", "w");
if (spectrum_file) {
for (int i = 0; i < SPECTRUM_NB_SAMPLES; i++) {
fprintf(spectrum_file, "%f\n", data.spectrum[i]);
}
fclose(spectrum_file);
} else {
perror("Error while opening spectrum.csv");
}
// End condition
if(++count == MAX_ACQ) {end = 1;}
}
double total_end_time = get_time_ms();
double total_elapsed_time = total_end_time - total_start_time;
printf("\nTotal time for %i acquisitons : %.3f ms\n\n", count, total_elapsed_time);
// --- Terminate ---
fclose(freq_file);
fclose(ampl_file);
fclose(phase_file);
fclose(qfactor_file);
close(client_socket);
return 0;
}
CFLAGS = -std=gnu11 -Wall ## -Werror
CFLAGS += -I/opt/redpitaya/include
#LDFLAGS = -L/opt/redpitaya/lib -L/usr/lib/arm-linux-gnueabihf
LDFLAGS = -L/opt/redpitaya/lib -L/usr/lib/arm-linux-gnueabihf -Wl,-rpath,/opt/redpitaya/lib
#LDLIBS = -static -lrp-hw-can -lrp -lrp-hw-calib -lrp-hw-profiles
LDLIBS = -lrp-hw-can -lrp -lrp-hw-calib -lrp-hw-profiles
INCLUDE += -I/opt/redpitaya/include/api250-12
LDLIBS += -lrp-gpio -lrp-i2c
LDLIBS += -lrp-hw -lm -lstdc++ -lpthread -li2c -lsocketcan
LDLIBS += -lfftw3 -lfftw3f
# List of compiled object files (not yet linked to executable)
PRGS = analog_inputs \
analog_outputs \
digital_led_bar \
digital_led_blink \
digital_push_button \
i2c \
spi \
spi_loopback \
test_e_module \
uart \
uart_loopback \
can_pass \
can_loopback \
calibration_api
PRGS += acquire_trigger_software \
acquire_trigger_posedge \
acquire_signal_check \
generate_arbitrary_waveform \
generate_burst_trigger_external \
generate_burst_trigger_software \
generate_continuous \
generate_two_burst_trigger_software \
generate_two_trigger_software_sync \
axi \
axi_4ch
PRGS += i2c_eeprom_read_calib
PRGS += acquire_4ch_trigger_software
PRGS += i2c_switch_max7311
OBJS := $(patsubst %,%.o,$(PRGS))
SRC := $(patsubst %,%.c,$(PRGS))
all: $(PRGS)
$(PRGS): %: %.c
$(CC) $< $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@
clean:
$(RM) *.o
$(RM) $(OBJS)
clean_all: clean
$(RM) $(PRGS)
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment