I am using nrf52832 development kit and sdk version 14.2.0 and soft device 5.0.
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "nrf_delay.h"
#include "bsp.h"
#include "nrf_calendar.h"
#include "boards.h"
#include "app_uart.h"
#include "app_error.h"
#include "app_mpu.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
/**@brief Function for initializing the nrf log module.
*/
static bool run_time_updates = false;
void uart_error_handle(app_uart_evt_t * p_event)
{
if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
{
APP_ERROR_HANDLER(p_event->data.error_communication);
}
else if (p_event->evt_type == APP_UART_FIFO_ERROR)
{
APP_ERROR_HANDLER(p_event->data.error_code);
}
}
void uart_init()
{
uint32_t err_code;
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_DISABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud115200
};
APP_UART_FIFO_INIT(&comm_params,
256,
256,
uart_error_handle,
APP_IRQ_PRIORITY_LOWEST,
err_code);
APP_ERROR_CHECK(err_code);
}
int uart_get_parameter(char *query_message, int min_value, int max_value)
{
uint8_t tmp_char, digit_index;
int current_value;
while(1)
{
current_value = 0;
digit_index = 0;
printf("%s: ", query_message);
while(1)
{
while(app_uart_get(&tmp_char) != NRF_SUCCESS);
if(tmp_char >= '0' && tmp_char <= '9' && digit_index < 9)
{
current_value = current_value * 10 + (tmp_char - '0');
app_uart_put(tmp_char);
digit_index++;
}
else if(tmp_char == 8 && digit_index > 0)
{
current_value /= 10;
digit_index--;
printf("\b \b");
}
else if(tmp_char == 13)
{
break;
}
}
if(current_value >= min_value && current_value <= max_value) break;
else
{
printf("\r\nInvalid value! \r\n");
printf("Legal range is %i - %i\r\n", min_value, max_value);
}
}
printf("\r\n");
return current_value;
}
void print_current_time()
{
NRF_LOG_INFO("Uncalibrated time:\t%s\r\n", nrf_cal_get_time_string(false));
NRF_LOG_INFO("Calibrated time:\t%s\r\n", nrf_cal_get_time_string(true));
}
void calendar_updated()
{
if(run_time_updates)
{
print_current_time();
}
}
static void log_init(void)
{
ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
void mpu_init(void)
{
ret_code_t ret_code;
// Initiate MPU driver
ret_code = app_mpu_init();
APP_ERROR_CHECK(ret_code); // Check for errors in return value
// Setup and configure the MPU with intial values
app_mpu_config_t p_mpu_config = MPU_DEFAULT_CONFIG(); // Load default values
p_mpu_config.smplrt_div = 19; // Change sampelrate. Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV). 19 gives a sample rate of 50Hz
p_mpu_config.accel_config.afs_sel = AFS_2G; // Set accelerometer full scale range to 2G
ret_code = app_mpu_config(&p_mpu_config); // Configure the MPU with above values
APP_ERROR_CHECK(ret_code); // Check for errors in return value
}
/**
* @brief Function for main application entry.
*/
int main(void)
{
uint32_t err_code;
uint8_t uart_byte;
// Initialize.
uart_init();
log_init();
NRF_LOG_INFO("\033[2J\033[;H"); // Clear screen
mpu_init();
// Start execution.
NRF_LOG_INFO("MPU Free Fall Interrupt example.");
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
NRF_CLOCK->TASKS_HFCLKSTART = 1;
while(NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
nrf_cal_init();
nrf_cal_set_callback(calendar_updated, 1);
accel_values_t acc_values;
uint32_t sample_number = 0;
while(1)
{
if(NRF_LOG_PROCESS() == false)
{
NRF_LOG_INFO("MPU Free Fall Interrupt example.");
// Read accelerometer sensor values
err_code = app_mpu_read_accel(&acc_values);
APP_ERROR_CHECK(err_code);
// Clear terminal and print values
NRF_LOG_INFO("\033[3;1HSample # %d\r\nX: %06d\r\nY: %06d\r\nZ: %06d", ++sample_number, acc_values.x, acc_values.y, acc_values.z);
print_current_time();
nrf_delay_ms(10);
}
}
}
/** @} */
I am using nrf_calendar.h with mpu9250. I have uploaded my code,and I have used all necessary libraries.my code is compiled with no error,but when I try to observe the output on putty it is not as expected and It is blank.
I have debug my code when I remove uart_init(),its working and it gives output of mpu data and time on putty.
but problem is I want to use uart service to get the character from user.