39#define RX_BUFFER_LEN 64
41#define uart_rx_write_ptr (RX_BUFFER_LEN - hdma_usart1_rx.Instance->CNDTR)
87int _write(
int file,
char const *buf,
int n)
90 HAL_UART_Transmit(&
huart1, (uint8_t*)(buf), n, HAL_MAX_DELAY);
121 uint32_t cmd_reg = 0;
122 float set_position = 0;
192 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
193 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
198 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
199 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
200 RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
201 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
202 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
203 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
204 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
205 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
212 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
213 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
214 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
215 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
216 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
217 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
219 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
241 hspi2.Instance = SPI2;
242 hspi2.Init.Mode = SPI_MODE_MASTER;
243 hspi2.Init.Direction = SPI_DIRECTION_2LINES;
244 hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
245 hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
246 hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
247 hspi2.Init.NSS = SPI_NSS_SOFT;
248 hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
249 hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
250 hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
251 hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
252 hspi2.Init.CRCPolynomial = 10;
253 if (HAL_SPI_Init(&
hspi2) != HAL_OK)
279 huart1.Init.BaudRate = 9600;
280 huart1.Init.WordLength = UART_WORDLENGTH_8B;
281 huart1.Init.StopBits = UART_STOPBITS_1;
282 huart1.Init.Parity = UART_PARITY_NONE;
283 huart1.Init.Mode = UART_MODE_TX_RX;
284 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
285 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
286 if (HAL_UART_Init(&
huart1) != HAL_OK)
303 __HAL_RCC_DMA1_CLK_ENABLE();
307 HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
308 HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
319 GPIO_InitTypeDef GPIO_InitStruct = {0};
325 __HAL_RCC_GPIOC_CLK_ENABLE();
326 __HAL_RCC_GPIOD_CLK_ENABLE();
327 __HAL_RCC_GPIOA_CLK_ENABLE();
328 __HAL_RCC_GPIOB_CLK_ENABLE();
341 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
342 GPIO_InitStruct.Pull = GPIO_NOPULL;
343 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
348 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
349 GPIO_InitStruct.Pull = GPIO_NOPULL;
350 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
351 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
355 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
356 GPIO_InitStruct.Pull = GPIO_NOPULL;
361 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
362 GPIO_InitStruct.Pull = GPIO_NOPULL;
363 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
389#ifdef USE_FULL_ASSERT
397void assert_failed(uint8_t *file, uint32_t line)
driver mpu9250 basic header file
settings_t set_data
Main system settings.
sensor_data_t measured_data
Current sensor measured data.
void uart_byte_available(uint8_t c, uint32_t *p_reg)
Recognizes commands in the received characters and sets the cmd register accordingly.
void valve_fcn(uint32_t *p_reg, float *p_set_pos)
It handles the execution of motion commands.
void autoread_fcn(uint32_t reg)
It handles the periodic transmission of the sensor data message.
void regul_fcn(uint32_t *p_reg, float set_pos)
It handles the regulation of the orientation.
void sensor_get_value(float *gyr_offset)
It handles reading the value from the sensor.
void sensor_init(float *gyr_offset)
It initiates communication with the sensor.
static volatile uint16_t uart_rx_read_ptr
Received read pointer for UART.
void Error_Handler(void)
This function is executed in case of error occurrence.
UART_HandleTypeDef huart1
DMA_HandleTypeDef hdma_usart1_rx
static uint8_t uart_rx_buf[RX_BUFFER_LEN]
Received buffer fo UART.
static void MX_DMA_Init(void)
static void MX_USART1_UART_Init(void)
USART1 Initialization Function.
void SystemClock_Config(void)
System Clock Configuration.
int main(void)
The application entry point.
#define uart_rx_write_ptr
void setting_init(void)
Set default values of set_data global variable.
void valve_init(void)
Initialize valves. By default close.
int _write(int file, char const *buf, int n)
static void MX_SPI2_Init(void)
SPI2 Initialization Function.
static void MX_GPIO_Init(void)
GPIO Initialization Function.
: Header for main.c file. This file contains the common defines of the application.
#define BOARD_LED_GPIO_Port
#define VALVE_R_GPIO_Port
#define SPI2_CS_GPIO_Port
#define VALVE_L_GPIO_Port
uint8_t time_r
Time [10*ms], right valve is open.
uint8_t angle
Angle [deg], cube rotate after turn cmd.
int16_t home
Angle [deg], default orientation.
uint8_t time_l
Time [10*ms], left valve is open.
uint8_t time_a
Period [100*ms], sensor report will be sended.