Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #54 #55

Merged
merged 1 commit into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src_sw/axi_adxl.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,126 @@
#include "axi_adxl.h"
#include <math.h>
//
//enum output_rule_enum{
// XYZ_INTEGER,
// XYZ_GRAVITY,
// ROLL_PITCH
//};

int axi_adxl_set_output_rule(axi_adxl *ptr, enum output_rule_enum output_rule){
if (!axi_adxl_has_init(ptr)){
#ifdef AXI_ADXL_LOGGING_SW
textcolor(DEFAULT, RED, STD);
printf("\t\t[ADXL_SET_OUTPUT_RULE] : unitialized software structure");
textcolor(DEFAULT, STD, STD);
printf("\r\n");
#endif
return ADXL_UNINIT;
}

if ((output_rule != XYZ_INTEGER) && (output_rule != XYZ_GRAVITY) && (output_rule != ROLL_PITCH)){
#ifdef AXI_ADXL_LOGGING_SW
textcolor(DEFAULT, RED, STD);
printf("\t\t[ADXL_SET_OUTPUT_RULE] : uncorrect selection : %d", output_rule);
textcolor(DEFAULT, STD, STD);
printf("\r\n");
#endif
return ADXL_UNCORRECT_VALUE;
}

ptr->output_rule = output_rule;

return ADXL_OK;
}



int axi_adxl_get_output_rule(axi_adxl *ptr, int *output_rule){

if (!axi_adxl_has_init(ptr)){
#ifdef AXI_ADXL_LOGGING_SW
textcolor(DEFAULT, RED, STD);
printf("\t\t[ADXL_GET_OUTPUT_RULE] : unitialized software structure");
textcolor(DEFAULT, STD, STD);
printf("\r\n");
#endif
return ADXL_UNINIT;
}

*output_rule = ptr->output_rule;

return ADXL_OK;
}



int axi_adxl_is_output_rule(axi_adxl *ptr, enum output_rule_enum output_rule){
return ((ptr->output_rule == output_rule) ? TRUE : FALSE);
}


int axi_adxl_print(axi_adxl *ptr){
int output_rule;
adxl_data data;
adxl_data_float data_float;
adxl_data_pitch_roll pitch_roll;

int status = axi_adxl_get_output_rule(ptr, &output_rule);


switch(output_rule){
case XYZ_INTEGER:
status = axi_adxl_get_data(ptr, &data);
if (status != ADXL_OK){
return status;
}
printf("X : %5d \tY : %5d \tZ : %d\r\n", data.x, data.y, data.z);
break;

case XYZ_GRAVITY:
status = axi_adxl_get_data_float(ptr, &data_float);
if (status != ADXL_OK){
return status;
}
printf("X : %4.6f \tY : %4.6f \tZ : %4.6f\r\n", data_float.x, data_float.y, data_float.z);
break;

case ROLL_PITCH :
status = axi_adxl_get_pitch_roll(ptr, &pitch_roll);
if (status != ADXL_OK){
return status;
}
printf("\t%3.2f \t%3.2f\r\n", pitch_roll.pitch, pitch_roll.roll);
break;

default :
return ADXL_UNCORRECT_VALUE;
}


return status;
}



int axi_adxl_get_pitch_roll(axi_adxl *ptr, adxl_data_pitch_roll *pitch_roll_ptr){

if (!axi_adxl_has_init(ptr)){
#ifdef AXI_ADXL_LOGGING_SW
textcolor(DEFAULT, RED, STD);
printf("\t\t[ADXL_GET_PITCH_ROLL] : unitialized software structure");
textcolor(DEFAULT, STD, STD);
printf("\r\n");
#endif
return ADXL_UNINIT;
}

adxl_data_float gravity;
int status = axi_adxl_get_data_float(ptr, &gravity);
pitch_roll_ptr->roll = atan(gravity.y/ sqrt(pow(gravity.x, 2) + pow(gravity.z, 2))) * 180 / PI;
pitch_roll_ptr->pitch = atan(-1 * gravity.x / sqrt(pow(gravity.y, 2) + pow(gravity.z, 2))) * 180 / PI;
return status;
}



35 changes: 29 additions & 6 deletions src_sw/axi_adxl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include <math.h>
#include "text_color.h"

#define PI 3.14159265358979323846

//#define AXI_ADXL_LOGGING_CFG
//#define AXI_ADXL_LOGGING_DEV
//#define AXI_ADXL_LOGGING_CFG 0
//#define AXI_ADXL_LOGGING_DEV 0
#define AXI_ADXL_LOGGING_SW 1

#define ADXL_OK 0
#define ADXL_UNINIT -1
Expand Down Expand Up @@ -36,7 +38,7 @@ static int rw_address_const[ADXL_DEV_RW_COUNT] = {29, 30, 31, 32, 33, 34, 35, 36
static int ro_address_const[ADXL_DEV_RO_COUNT] = {0, 43, 48, 50, 51, 52, 53, 54, 55, 57};
static int reserved_address_const[ADXL_DEV_RESERVED_COUNT] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,58,59,60,61,62,63};

#define FUNCTIONS_COUNT 89
#define FUNCTIONS_COUNT 91

static int function_index_list[FUNCTIONS_COUNT] = {
0, 1, 2, 3, 4, 5, 6, 7, 8,
Expand All @@ -48,7 +50,8 @@ static int function_index_list[FUNCTIONS_COUNT] = {
451, 452, 453, 454, 455, 456, 457, 458, 459,
461, 460, 470, 471, 480, 490, 491, 492, 493,
494, 495, 496, 497, 498, 500, 501, 560, 561,
562, 563, 564, 565, 570, 571, 100, 120
562, 563, 564, 565, 570, 571, 100, 120, 121,
122
};

typedef struct {
Expand All @@ -70,12 +73,25 @@ typedef struct {
float z;
}adxl_data_float;


typedef struct {
float roll;
float pitch;
}adxl_data_pitch_roll;

enum output_rule_enum{
XYZ_INTEGER,
XYZ_GRAVITY,
ROLL_PITCH
};

typedef struct {
adxl_cfg *cfg;
adxl_dev *dev;
int init_flaq;
adxl_offset offset;
adxl_data data;
int output_rule;
} axi_adxl;


Expand Down Expand Up @@ -220,8 +236,7 @@ enum spi_enum {
#define axi_adxl_get_datay(ptr) (int16_t)(((uint16_t)adxl_dev_get_datay1((ptr)->dev)<<8) + ((uint16_t)adxl_dev_get_datay0((ptr)->dev)))
#define axi_adxl_get_dataz(ptr) (int16_t)(((uint16_t)adxl_dev_get_dataz1((ptr)->dev)<<8) + ((uint16_t)adxl_dev_get_dataz0((ptr)->dev)))


void axi_adxl_dev_debug_register_space(adxl_dev *ptr);
int axi_adxl_dev_debug_register_space(axi_adxl *ptr);

int axi_adxl_init(axi_adxl *ptr, uint32_t baseaddr_cfg, uint32_t baseaddr_dev, uint8_t iic_address);

Expand Down Expand Up @@ -419,4 +434,12 @@ int axi_adxl_has_fifo_sts_trigger(axi_adxl *ptr);

int axi_adxl_get_data(axi_adxl *ptr, adxl_data *data);
int axi_adxl_get_data_float(axi_adxl *ptr, adxl_data_float *data_float);
int axi_adxl_get_pitch_roll(axi_adxl *ptr, adxl_data_pitch_roll *data_pitch_roll);


int axi_adxl_set_output_rule(axi_adxl *ptr, enum output_rule_enum output_rule);
int axi_adxl_get_output_rule(axi_adxl *ptr, int *output_rule);
int axi_adxl_is_output_rule(axi_adxl *ptr, enum output_rule_enum output_rule);
int axi_adxl_print(axi_adxl *ptr);


16 changes: 10 additions & 6 deletions src_sw/axi_adxl_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ const char* function_list[] = {
"GET_FIFO_ENTRIES", // 570
"GET_FIFO_TRIGGER", // 571
"DUMP_DEVICE_REGISTER_SPACE", // 100
"DEBUG_MODE" // 120
"DEBUG_MODE", // 120
"SET_OUTPUT_RULES", // 121
"GET_OUTPUT_RULES" //122
};


Expand Down Expand Up @@ -280,6 +282,8 @@ void print_menu(){
printf("\r\n");
printf("\t100. Dump device register space\r\n");
printf("\t120. Debug mode\r\n");
printf("\t121. Set output rule\r\n");
printf("\t122. Get output rule\r\n");



Expand Down Expand Up @@ -470,7 +474,10 @@ int menu(axi_adxl *ptr, int mode){
case 571 : status = selector_axi_adxl_has_fifo_sts_trigger(ptr); break;

case 100 : status = selector_axi_adxl_dev_debug_register_space(ptr); break;

case 120 : status = dbg_set_reg(ptr); break;
case 121 : status = selector_axi_adxl_set_output_rules(ptr); break;
case 122 : status = selector_axi_adxl_get_output_rules(ptr); break;

default :
printf("[MENU] : incorrect selection : 0x%02x\r\n", mode);
Expand Down Expand Up @@ -529,10 +536,7 @@ void adxl_intr_handler(void *callback){
printf("[IRQ] : bad returning status : %d", status);
}

adxl_data_float data;
axi_adxl_get_data_float(ptr, &data);

if ((axi_adxl_is_int_source(ptr, DATA_READY)) && (axi_adxl_is_int_enable(ptr, DATA_READY))){
if ((axi_adxl_is_int_source(ptr, DATA_READY)) && (axi_adxl_is_int_enable(ptr, DATA_READY))){
printf("[DR] ");
}

Expand Down Expand Up @@ -564,7 +568,7 @@ void adxl_intr_handler(void *callback){
printf("[OV] ");
}

printf("X : %4.6f \tY : %4.6f \tZ : %4.6f\r\n", data.x, data.y, data.z);
axi_adxl_print(ptr);

axi_adxl_irq_ack(ptr);

Expand Down
2 changes: 2 additions & 0 deletions src_sw/axi_adxl_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ int axi_adxl_init(axi_adxl* ptr, uint32_t baseaddr_cfg, uint32_t baseaddr_dev, u
printf("\t\t\tconfiguration address space : 0x%08x\r\n", baseaddr_dev);
#endif

ptr->output_rule = 0;

int timer = TIMER_LIMIT;


Expand Down
13 changes: 9 additions & 4 deletions src_sw/axi_adxl_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@




void axi_adxl_dev_debug_register_space(adxl_dev* ptr) {
int axi_adxl_dev_debug_register_space(axi_adxl* ptr) {
int byte_cnt = 0;

if (!axi_adxl_has_init(ptr)){
return ADXL_UNINIT;
}

printf("\t|| [0] \t| [1] \t| [2] \t| [3] \t|\r\n");
printf("========================================\r\n");

Expand Down Expand Up @@ -34,7 +37,7 @@ void axi_adxl_dev_debug_register_space(adxl_dev* ptr) {
}


printf(" 0x%02x\t", *((uint8_t*)ptr + i));
printf(" 0x%02x\t", *((uint8_t*)(ptr->dev) + i));

textcolor(DEFAULT, STD, STD);

Expand All @@ -49,6 +52,8 @@ void axi_adxl_dev_debug_register_space(adxl_dev* ptr) {
}

}

return ADXL_OK;
}

/// <summary>
Expand Down Expand Up @@ -3859,7 +3864,7 @@ int axi_adxl_get_data_float(axi_adxl* ptr, adxl_data_float* data_float) {

int16_t x = (int16_t)axi_adxl_get_datax(ptr);
int16_t y = (int16_t)axi_adxl_get_datay(ptr);
int16_t z = (int16_t)axi_adxl_get_datax(ptr);
int16_t z = (int16_t)axi_adxl_get_dataz(ptr);

if (adxl_dev_get_data_format(ptr->dev) & DATA_FORMAT_FULL_RES) {
data_float->x = (float)x / SENSITIVITY_FULL_RES;
Expand Down
Loading