Miscellaneous algorithm and hardware libraries for STM32
This library contains files implementing circular buffer using DMA with UART. It allows to read data from UART in non--blocking mode and setting a timeout. This timeout allows to measure time gap between burst of transmissions and this is most likely a single command, line of text, response, etc. from external device.
You can define circ buff logic and hardware structures:
circ_buff_hw_t cb_hw;
circ_buff_t cb;
Initialization of the interface is following
//initialize hardware
circ_buff_hw_init(&cb_hw, &hdma_usart2_rx, &huart2);
//initialize circ buff logic
circ_buff_init(&cb, &cb_hw, buffer_uart_rx, 512, 50);
//start receiving
circ_buff_start_rx(&cb);
Checking if there is a new portion of data and reading it to a buffer is following
if (circ_buff_new_data(&cb)) {
len = circ_buff_new_data_amount(&cb);
if (len > 0) {
circ_buff_read(&cb, buffer, len);
}
}
Library facilitating implementation of PID controller. It comes with two flavours: float and int32.
Create data structures and initialize them:
pid_f32_t pid_data;
float P, I, D, DT_S;
float u, mv, dv;
P = 1.0000f;
I = 0.0000f;
D = 0.1234f;
DT_S = 0.010f; //freq = 100Hz, period = 10 ms
Initialize PID data structure and set limits:
pid_f32_init(&pid_data, P, I, D, DT_S);
pid_data.p_max = 3000;
pid_data.p_min = -3000;
pid_data.i_max = 3000;
pid_data.i_min = -3000;
pid_data.d_max = 3000;
pid_data.d_min = -3000;
pid_data.total_max = 3000;
pid_data.total_min = -3000;
To reset integral term (sets integral sum to 0). It only make sense when you use integral term; I coefficient is different from 0.
pid_f32_reset_int(&pid_data);
Calculate control signal with PID controller:
mv = 10.0f;
dv = -5.0f;
u = pid_f32_calc(&pid_dat, mv, dv);
Library offering an implementation of embedded printf(), sprintf(), etc. functions. Not all functions from stdio.h are implemented and they do not offer complete list of features.
Each function from stdio.h has its counter part in eprintfs library. Embedded implementation has an e prefix.
Available functions are:
- int eabs(int val);
- float efabs(float val);
- unsigned int epower(unsigned int base, unsigned int power);
- unsigned int edigits(int number);
- int estrlen(const char *str);
- int estrncpy(const char *str, char *out, unsigned int len);
- int estrncmp(const char *str1, const char *str2, unsigned int num);
- char *eitoa(long int val, char *str, int base);
- int eatoi(char *str, int base, char **stop);
- float eatof(char *str, char **stop);
- int esprintf(char *str, const char *format, ...);
- int esscanf(char *str, const char *format, ...);
- const char *estrchr(const char *str, int character);
- char * estrstr (char * str1, const char * str2);
- char *efind_char_right(char *str, int len, const char sign, int * position);
Library supporting parsing of NMEA sentences. Currently only two are supported:
- GPGGA,
- GPVTG.
Use gps_parse() to parse buffer with NMEA sentences and populate gps_t structure.
int gps_parse(gps_t * gps, char * buffer, unsigned int length);