miniFOC 1.0.3
This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.
flash.c
Go to the documentation of this file.
1/**************************************************************************/
11#include "main.h"
12
20#define JUDGE_AVAILABLE(x, minimum, maximum) if (x > maximum || x < minimum) \
21 x = 0
22
27 unsigned int buffer[11] = {machine_angle_offset, phase_sequence, 0x00000000UL, 0x00000000UL, 0x00000000UL,
28 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00000000UL, 0x00000000UL};
38 buffer[10] = 0xA5A5A5A5UL;
40 flash_program_word(0x00000000UL, buffer, 11);
41}
42
48 phase_sequence = flash_read_word(0x00000004UL);
57 if (machine_angle_offset > 4096 || phase_sequence > 1)
59 if (flash_read_word(0x00000040UL) != 0xA5A5A5A5UL)
61
62 JUDGE_AVAILABLE(speed_pid_handler.kp, 10.0f, -10.0f);
63 JUDGE_AVAILABLE(speed_pid_handler.ki, 10.0f, -10.0f);
64 JUDGE_AVAILABLE(speed_pid_handler.kd, 10.0f, -10.0f);
66
67 JUDGE_AVAILABLE(angle_pid_handler.kp, 10.0f, -10.0f);
68 JUDGE_AVAILABLE(angle_pid_handler.ki, 10.0f, -10.0f);
69 JUDGE_AVAILABLE(angle_pid_handler.kd, 10.0f, -10.0f);
71}
72
76void flash_erase_page(void) {
77 /* unlock the flash program/erase controller */
78 fmc_unlock();
79 fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
80
81 /* erase the flash page */
82 fmc_page_erase(FMC_WRITE_START_ADDR);
83 fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
84
85 /* lock the main FMC after the erase operation */
86 fmc_lock();
87}
88
95void flash_program_word(unsigned int addr, unsigned int *data, unsigned char counter) {
96 /* unlock the flash program/erase controller */
97 fmc_unlock();
98
99 /* program flash */
100 for (unsigned char data_cnt = 0; data_cnt < counter; data_cnt++) {
101 fmc_word_program(addr + FMC_WRITE_START_ADDR + 4 * data_cnt, data[data_cnt]);
102 fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
103 }
104
105 /* lock the main FMC after the program operation */
106 fmc_lock();
107}
108
114unsigned int flash_read_word(unsigned int addr) {
115 /* redirect the address to access memory */
116 addr = FMC_WRITE_START_ADDR + addr;
117 unsigned int *ptrd = (unsigned int *) addr;
118 return (*ptrd);
119}
#define FMC_WRITE_START_ADDR
User flash space start address, 1KB user flash.
Definition: config.h:44
volatile unsigned short machine_angle_offset
mechanical angle offset, which is used to align the mechanical angle with the zero point of the elect...
Definition: encoder.c:23
#define JUDGE_AVAILABLE(x, minimum, maximum)
determine whether the read-out variable is within the trusted range
Definition: flash.c:20
unsigned int flash_read_word(unsigned int addr)
read flash word from address
Definition: flash.c:114
void flash_write_parameters(void)
program all parameters to flash
Definition: flash.c:26
void flash_read_parameters(void)
read all parameters from flash
Definition: flash.c:46
void flash_erase_page(void)
erase flash page in last sector
Definition: flash.c:76
void flash_program_word(unsigned int addr, unsigned int *data, unsigned char counter)
program flash word to address
Definition: flash.c:95
unsigned char foc_parameter_available_flag
flag variable for FOC parameter availability
Definition: foc.c:28
volatile unsigned char phase_sequence
motor phase sequence flag variable
Definition: foc.c:24
main function Header File
unsigned char pid_parameter_available_flag
flag variable for PID parameter availability
Definition: pid.c:16
volatile PID_Structure_t angle_pid_handler
algorithm handler of PID angle loop
Definition: pid.c:28
volatile PID_Structure_t speed_pid_handler
algorithm handler of PID speed loop
Definition: pid.c:24
float ki
integral term coefficient in PID
Definition: pid.h:26
float kp
proportional term coefficient in PID
Definition: pid.h:25
float sum_maximum
maximum of anti saturation integral in PID
Definition: pid.h:32
float kd
differential term coefficient in PID
Definition: pid.h:27
unsigned int float_to_int32(float data0)
convert floating point numbers to int32 type data
Definition: system.c:60
float int32_to_float(unsigned int data0)
convert int32 to floating point numbers type data
Definition: system.c:70