From 4a119100e7693f51eee333f0f90639d0b5ae674c Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 00:07:55 -0700 Subject: [PATCH 01/11] updated with more enums and start of the update code --- src/gy521_driver/gy521_driver.c | 55 ++++++++++++++++++++++++++++++--- src/gy521_driver/gy521_driver.h | 4 +-- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/gy521_driver/gy521_driver.c b/src/gy521_driver/gy521_driver.c index e705c25..8018cf2 100644 --- a/src/gy521_driver/gy521_driver.c +++ b/src/gy521_driver/gy521_driver.c @@ -18,6 +18,8 @@ extern void _test_free(void* const ptr, const char* file, const int line); #define free(ptr) _test_free(ptr, __FILE__, __LINE__) #endif // UNIT_TESTING +#define NUM_ACCEL_REGS 6 + /* * ############################ * REGISTER MAP @@ -25,6 +27,20 @@ extern void _test_free(void* const ptr, const char* file, const int line); */ enum gy521_map { + accel_xouth = 0x3B, + accel_xoutl, + accel_youth, + accel_youtl, + accel_zouth, + accel_zoutl, + temp_outh, + temp_outl, + gyro_xouth, + gyro_xoutl, + gyro_youth, + gyro_youtl, + gyro_zouth, + gyro_zoutl, fifo_couth = 0x72, fifo_contl, fifo_r_w, @@ -55,6 +71,23 @@ struct gy521_module{ accel_values_struct accel; }; +/* + * ############################ + * Helper/Private Functions + * ############################ + */ + +uint8_t read_register(enum gy521_map reg) +{ + return 0; +} + +uint8_t write_register(enum gy521_map reg) +{ + return 0; +} + + /* * ############################ * Function Definitions @@ -75,6 +108,7 @@ void gy521_free(gy521_module *m) free(m); } + _Bool gy521_init(gy521_module *m, uint8_t slave_address) { if((slave_address != TWI_GY521_ADDR1) && (slave_address != TWI_GY521_ADDR2)) { m->slave_address = 0x0; @@ -97,12 +131,25 @@ _Bool gy521_init(gy521_module *m, uint8_t slave_address) { } -_Bool gy521_update_gyro(gy521_module *m) +void gy521_update_gyro(gy521_module *m) { - return 0; + } -_Bool gy521_update_accel(gy521_module *m) +void gy521_update_accel(gy521_module *m) { - return 0; + uint8_t read_regs[NUM_ACCEL_REGS] = {accel_xouth, accel_xoutl, + accel_youth, accel_youth, + accel_zouth, accel_zoutl}; + + /*update individually the structure by communicating with the device.*/ + gy521_twi_rx(m->slave_address, read_regs, NUM_ACCEL_REGS); + + /*bitshift to reassembly the high/low regs into a single 16bit*/ + m->accel.x = (read_regs[0] << 8) | read_regs[1]; + m->accel.y = (read_regs[2] << 8) | read_regs[3]; + m->accel.z = (read_regs[4] << 8) | read_regs[5]; + } + + diff --git a/src/gy521_driver/gy521_driver.h b/src/gy521_driver/gy521_driver.h index 64b0d1c..b27bf84 100644 --- a/src/gy521_driver/gy521_driver.h +++ b/src/gy521_driver/gy521_driver.h @@ -42,8 +42,8 @@ extern void (*gy521_twi_rx)(uint8_t, uint8_t*, uint8_t); struct gy521_module* gy521_new(void); _Bool gy521_init(struct gy521_module *m, uint8_t slave_address); -_Bool gy521_update_gyro(struct gy521_module* m); -_Bool gy521_update_accel(struct gy521_module* m); +void gy521_update_gyro(struct gy521_module* m); +void gy521_update_accel(struct gy521_module* m); void gy521_free(struct gy521_module *m); From 398b525dec0f82c5c632494f46042e37a2e7fd36 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 00:08:03 -0700 Subject: [PATCH 02/11] updated with fake registers --- tests/gy521_driver/test_gy521_driver.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/gy521_driver/test_gy521_driver.c b/tests/gy521_driver/test_gy521_driver.c index e2f12b5..2544bdf 100644 --- a/tests/gy521_driver/test_gy521_driver.c +++ b/tests/gy521_driver/test_gy521_driver.c @@ -10,12 +10,15 @@ uint8_t fake_twi_addr = 0x0; uint8_t fake_twi_data[16] = {0x0}; - +uint8_t fake_regs[117] = {0x0}; /* Fake Object for TWI_TX*/ void fake_twi_tx(uint8_t slave_addr, uint8_t *data, uint8_t size) { - fake_twi_addr = slave_addr; + fake_twi_addr = slave_addr; + for(int i = 0; i < size; i++) { + + } } /* Fake Object for TWI_RX*/ @@ -58,9 +61,20 @@ static void test_gy521_init(void **state) { } -static void test_gy521_update(void **sate) +static void test_gy521_update_accel(void **sate) { - assert_false(1); + /*check it reads the accel registers*/ + gy521_module *m = gy521_new(); + fake_twi_data[0] = TWI_GY521_ADDR1; + gy521_init(m, TWI_GY521_ADDR1); + + /*Ensure the correct registers are read*/ + + /*Check that the values are assembled correctly*/ + + gy521_update_accel(m); + + gy521_free(m); } @@ -68,8 +82,7 @@ int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_gy521_init), - cmocka_unit_test(test_gy521_update), - + cmocka_unit_test(test_gy521_update_accel), }; return cmocka_run_group_tests(tests, NULL, NULL); } From 960c7fda99a03a9b024be0c7502ef404e2a319b2 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 17:07:13 -0700 Subject: [PATCH 03/11] rebuild the mock/fake for the twi, also added a test for the accelerometer update function --- tests/gy521_driver/test_gy521_driver.c | 78 ++++++++++++++++++++------ 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/tests/gy521_driver/test_gy521_driver.c b/tests/gy521_driver/test_gy521_driver.c index 2544bdf..1f0e6c5 100644 --- a/tests/gy521_driver/test_gy521_driver.c +++ b/tests/gy521_driver/test_gy521_driver.c @@ -8,26 +8,46 @@ #define WRITE_BIT (1<<7) -uint8_t fake_twi_addr = 0x0; -uint8_t fake_twi_data[16] = {0x0}; -uint8_t fake_regs[117] = {0x0}; +typedef struct reg_addr{ + uint8_t dev_addr; + uint8_t addr; + uint8_t value; +}reg_addr; + +reg_addr reg_addr_arr[16] = {{0x0, 0x0, 0x0}}; +uint8_t idx = 0; /* Fake Object for TWI_TX*/ void fake_twi_tx(uint8_t slave_addr, uint8_t *data, uint8_t size) { - fake_twi_addr = slave_addr; - for(int i = 0; i < size; i++) { - + /*Save the slave address passed*/ + reg_addr_arr[0].dev_addr = slave_addr; + + /*Now write all the passed data*/ + for(; idx < size; idx++) { + reg_addr_arr[idx].addr = *(data); + reg_addr_arr[idx].value = *(++data); } + /*Post increment the global index*/ + idx++; } /* Fake Object for TWI_RX*/ void fake_twi_rx(uint8_t slave_addr, uint8_t *data, uint8_t size) { - fake_twi_addr = slave_addr; - for(int i = 0; i < size; i++) { - *data++ = fake_twi_data[i]; + /*Save the slave address passed*/ + reg_addr_arr[0].dev_addr = slave_addr; + + /*Read the registers requested*/ + for(; idx < size; idx++) { + /*Wridxte the register read address*/ + reg_addr_arr[idx].addr = *(data + idx); + + /*Read the response value idxnto the pased data ptr*/ + *(data + idx) = reg_addr_arr[idx].value; } + /*Post increment the global index*/ + idx++; } @@ -46,15 +66,23 @@ static void test_gy521_init(void **state) { /*Check for it's confirmation of the right twi device*/ assert_false(gy521_init(m, TWI_GY521_ADDR1)); - /*Now give it the correct response*/ - fake_twi_data[0] = TWI_GY521_ADDR1; + /*Fill the address and the value registers with correct response value*/ + /*The zero element get's read on init of the module*/ + reg_addr_arr[0].addr = TWI_GY521_ADDR1; + reg_addr_arr[0].value = TWI_GY521_ADDR1; + idx = 0; + + /*Check that it worked*/ assert_true(gy521_init(m, TWI_GY521_ADDR1)); - assert_true(TWI_GY521_ADDR1 == fake_twi_addr); + assert_true(TWI_GY521_ADDR1 == reg_addr_arr[0].dev_addr); - fake_twi_data[0] = TWI_GY521_ADDR2; + reg_addr_arr[0].addr = TWI_GY521_ADDR2; + reg_addr_arr[0].value = TWI_GY521_ADDR2; + idx = 0; + assert_true(gy521_init(m, TWI_GY521_ADDR2)); - assert_true(TWI_GY521_ADDR2 == fake_twi_addr); + assert_true(TWI_GY521_ADDR2 == reg_addr_arr[0].dev_addr); gy521_free(m); @@ -65,15 +93,31 @@ static void test_gy521_update_accel(void **sate) { /*check it reads the accel registers*/ gy521_module *m = gy521_new(); - fake_twi_data[0] = TWI_GY521_ADDR1; + reg_addr_arr[0].addr = TWI_GY521_ADDR1; gy521_init(m, TWI_GY521_ADDR1); + /*Setup the fake accel values.*/ + for(uint8_t i = 0; i < 6; i++) { + reg_addr_arr[i].value = i; + } + + idx = 0; + gy521_update_accel(m); + + /*Ensure the correct registers are read*/ + _Bool is_correct = 1; + for(int i = 0; i < 6; i++){ + /*The starting address of the registers is 59 and goes to 64*/ + print_message("expected: %d, actual: %d\n", (59+i), reg_addr_arr[i].addr); + if((59 + i) != reg_addr_arr[i].addr) { + is_correct = 0; + } + } + assert_true(is_correct); /*Check that the values are assembled correctly*/ - gy521_update_accel(m); - gy521_free(m); } From 56e568643b801d27da5b25423364775ad2eb2690 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 17:07:23 -0700 Subject: [PATCH 04/11] updated code to use read_register functions --- src/gy521_driver/gy521_driver.c | 53 +++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/src/gy521_driver/gy521_driver.c b/src/gy521_driver/gy521_driver.c index 8018cf2..429ebc1 100644 --- a/src/gy521_driver/gy521_driver.c +++ b/src/gy521_driver/gy521_driver.c @@ -19,6 +19,7 @@ extern void _test_free(void* const ptr, const char* file, const int line); #endif // UNIT_TESTING #define NUM_ACCEL_REGS 6 +#define NUM_GYRO_REGS 6 /* * ############################ @@ -77,14 +78,21 @@ struct gy521_module{ * ############################ */ -uint8_t read_register(enum gy521_map reg) +uint8_t read_register(gy521_module *m, enum gy521_map reg) { - return 0; + /*Load the data var with athe address of the register to read*/ + uint8_t data = reg; + + /*Indicate we want to read a value from the slave.*/ + gy521_twi_rx(m->slave_address, &data, 1); + + /*The value should now be in the data var*/ + return data; } -uint8_t write_register(enum gy521_map reg) +void write_register(gy521_module *m, enum gy521_map reg, uint8_t data) { - return 0; + gy521_twi_tx(m->slave_address, (uint8_t *) ®, data); } @@ -96,7 +104,6 @@ uint8_t write_register(enum gy521_map reg) gy521_module* gy521_new(void) { - //gy521_module *m = malloc(sizeof(gy521_module)); gy521_module *m = (gy521_module *)malloc(sizeof(gy521_module)); m->slave_address = 0x0; return m; @@ -110,6 +117,7 @@ void gy521_free(gy521_module *m) _Bool gy521_init(gy521_module *m, uint8_t slave_address) { + /*Check to make sure it's a valid address for this module*/ if((slave_address != TWI_GY521_ADDR1) && (slave_address != TWI_GY521_ADDR2)) { m->slave_address = 0x0; return 0; @@ -118,10 +126,8 @@ _Bool gy521_init(gy521_module *m, uint8_t slave_address) { /*Set the slave address of the module*/ m->slave_address = slave_address; - /*Try to talk with i2c module.*/ - uint8_t data = who_am_i; - gy521_twi_tx(m->slave_address, &data, 1); - gy521_twi_rx(m->slave_address, &data, 1); + /*Make sure TWI is working and read the who_am_i register*/ + uint8_t data = read_register(m, who_am_i); if(data != m->slave_address) { return 0; @@ -131,11 +137,6 @@ _Bool gy521_init(gy521_module *m, uint8_t slave_address) { } -void gy521_update_gyro(gy521_module *m) -{ - -} - void gy521_update_accel(gy521_module *m) { uint8_t read_regs[NUM_ACCEL_REGS] = {accel_xouth, accel_xoutl, @@ -143,8 +144,10 @@ void gy521_update_accel(gy521_module *m) accel_zouth, accel_zoutl}; /*update individually the structure by communicating with the device.*/ - gy521_twi_rx(m->slave_address, read_regs, NUM_ACCEL_REGS); - + for(uint8_t i = 0; i < NUM_ACCEL_REGS; i++) { + read_regs[i] = read_register(m, read_regs[i]); + } + /*bitshift to reassembly the high/low regs into a single 16bit*/ m->accel.x = (read_regs[0] << 8) | read_regs[1]; m->accel.y = (read_regs[2] << 8) | read_regs[3]; @@ -152,4 +155,22 @@ void gy521_update_accel(gy521_module *m) } +void gy521_update_gyro(gy521_module *m) +{ + uint8_t read_regs[NUM_GYRO_REGS] = {gyro_xouth, gyro_xoutl, + gyro_youth, gyro_youth, + gyro_zouth, gyro_zoutl}; + + /*update individually the structure by communicating with the device.*/ + for(uint8_t i = 0; i < NUM_GYRO_REGS; i++) { + read_regs[i] = read_register(m, read_regs[i]); + } + + /*bitshift to reassembly the high/low regs into a single 16bit*/ + m->gyro.x = (read_regs[0] << 8) | read_regs[1]; + m->gyro.y = (read_regs[2] << 8) | read_regs[3]; + m->gyro.z = (read_regs[4] << 8) | read_regs[5]; + +} + From 68e1048a7bb3b7c5148566fceae82b2b4428f48b Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 17:42:55 -0700 Subject: [PATCH 05/11] rewrote the gyro and accel update functions to make them faster --- src/gy521_driver/gy521_driver.c | 40 ++++++++++----------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/src/gy521_driver/gy521_driver.c b/src/gy521_driver/gy521_driver.c index 429ebc1..b57ab63 100644 --- a/src/gy521_driver/gy521_driver.c +++ b/src/gy521_driver/gy521_driver.c @@ -139,38 +139,22 @@ _Bool gy521_init(gy521_module *m, uint8_t slave_address) { void gy521_update_accel(gy521_module *m) { - uint8_t read_regs[NUM_ACCEL_REGS] = {accel_xouth, accel_xoutl, - accel_youth, accel_youth, - accel_zouth, accel_zoutl}; - - /*update individually the structure by communicating with the device.*/ - for(uint8_t i = 0; i < NUM_ACCEL_REGS; i++) { - read_regs[i] = read_register(m, read_regs[i]); - } - - /*bitshift to reassembly the high/low regs into a single 16bit*/ - m->accel.x = (read_regs[0] << 8) | read_regs[1]; - m->accel.y = (read_regs[2] << 8) | read_regs[3]; - m->accel.z = (read_regs[4] << 8) | read_regs[5]; - + m->accel.x = read_register(m, accel_xouth) <<8; + m->accel.x |= read_register(m, accel_xoutl); + m->accel.y = read_register(m, accel_youth) <<8; + m->accel.y |= read_register(m, accel_youtl); + m->accel.z = read_register(m, accel_zouth) <<8; + m->accel.z |= read_register(m, accel_zoutl); } void gy521_update_gyro(gy521_module *m) { - uint8_t read_regs[NUM_GYRO_REGS] = {gyro_xouth, gyro_xoutl, - gyro_youth, gyro_youth, - gyro_zouth, gyro_zoutl}; - - /*update individually the structure by communicating with the device.*/ - for(uint8_t i = 0; i < NUM_GYRO_REGS; i++) { - read_regs[i] = read_register(m, read_regs[i]); - } - - /*bitshift to reassembly the high/low regs into a single 16bit*/ - m->gyro.x = (read_regs[0] << 8) | read_regs[1]; - m->gyro.y = (read_regs[2] << 8) | read_regs[3]; - m->gyro.z = (read_regs[4] << 8) | read_regs[5]; - + m->gyro.x = read_register(m, gyro_xouth) <<8; + m->gyro.x |= read_register(m, gyro_xoutl); + m->gyro.y = read_register(m, gyro_youth) <<8; + m->gyro.y |= read_register(m, gyro_youtl); + m->gyro.z = read_register(m, gyro_zouth) <<8; + m->gyro.z |= read_register(m, gyro_zoutl); } From c72415d73d9dd8f7e9fdb8e93e339c183d25957a Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 17:43:12 -0700 Subject: [PATCH 06/11] rewrote the tests to check the accessed addresses. --- tests/gy521_driver/test_gy521_driver.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/gy521_driver/test_gy521_driver.c b/tests/gy521_driver/test_gy521_driver.c index 1f0e6c5..47435cb 100644 --- a/tests/gy521_driver/test_gy521_driver.c +++ b/tests/gy521_driver/test_gy521_driver.c @@ -39,12 +39,12 @@ void fake_twi_rx(uint8_t slave_addr, uint8_t *data, uint8_t size) reg_addr_arr[0].dev_addr = slave_addr; /*Read the registers requested*/ - for(; idx < size; idx++) { + for(uint8_t i = 0; i < size; i++) { /*Wridxte the register read address*/ - reg_addr_arr[idx].addr = *(data + idx); + reg_addr_arr[i + idx].addr = *(data + i); - /*Read the response value idxnto the pased data ptr*/ - *(data + idx) = reg_addr_arr[idx].value; + /*Read the response value into the pased data ptr*/ + *(data + i) = reg_addr_arr[i + idx].value; } /*Post increment the global index*/ idx++; @@ -100,16 +100,16 @@ static void test_gy521_update_accel(void **sate) for(uint8_t i = 0; i < 6; i++) { reg_addr_arr[i].value = i; } - + + /*Zero the global index for the TWI*/ idx = 0; gy521_update_accel(m); - /*Ensure the correct registers are read*/ _Bool is_correct = 1; - for(int i = 0; i < 6; i++){ - /*The starting address of the registers is 59 and goes to 64*/ - print_message("expected: %d, actual: %d\n", (59+i), reg_addr_arr[i].addr); + assert_true(is_correct); + for(uint8_t i = 0; i < 6; i++){ + /*The starting address of the registers is 59 and goes to 64*/ if((59 + i) != reg_addr_arr[i].addr) { is_correct = 0; } @@ -117,7 +117,9 @@ static void test_gy521_update_accel(void **sate) assert_true(is_correct); /*Check that the values are assembled correctly*/ - + + + gy521_free(m); } From 75a28027f5689c75a17ba27e3fa5e837531b5143 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 17:54:50 -0700 Subject: [PATCH 07/11] fixed up issues with structures. Now exposed the gyro and accel structures --- src/gy521_driver/gy521_driver.c | 30 ++++++++++++++++++++---------- src/gy521_driver/gy521_driver.h | 17 +++++++++++++++-- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/gy521_driver/gy521_driver.c b/src/gy521_driver/gy521_driver.c index b57ab63..41c02db 100644 --- a/src/gy521_driver/gy521_driver.c +++ b/src/gy521_driver/gy521_driver.c @@ -54,17 +54,7 @@ enum gy521_map { * ############################ */ -typedef struct{ - uint16_t x; - uint16_t y; - uint16_t z; -}gyro_values_struct; -typedef struct{ - uint16_t x; - uint16_t y; - uint16_t z; -}accel_values_struct; struct gy521_module{ uint8_t slave_address; @@ -139,6 +129,7 @@ _Bool gy521_init(gy521_module *m, uint8_t slave_address) { void gy521_update_accel(gy521_module *m) { + /*We shift the High byte over by 8 for a u16*/ m->accel.x = read_register(m, accel_xouth) <<8; m->accel.x |= read_register(m, accel_xoutl); m->accel.y = read_register(m, accel_youth) <<8; @@ -149,6 +140,7 @@ void gy521_update_accel(gy521_module *m) void gy521_update_gyro(gy521_module *m) { + /*We shift the High byte over by 8 for a u16*/ m->gyro.x = read_register(m, gyro_xouth) <<8; m->gyro.x |= read_register(m, gyro_xoutl); m->gyro.y = read_register(m, gyro_youth) <<8; @@ -158,3 +150,21 @@ void gy521_update_gyro(gy521_module *m) } +struct accel_values gy521_get_accel(struct gy521_module* m) +{ + struct accel_values s; + s.x = m->accel.x; + s.y = m->accel.y; + s.z = m->accel.z; + return s; +} + + +struct gyro_values gy521_get_gyro(struct gy521_module* m) +{ + struct gyro_values s; + s.x = m->gyro.x; + s.y = m->gyro.y; + s.z = m->gyro.z; + return s; +} diff --git a/src/gy521_driver/gy521_driver.h b/src/gy521_driver/gy521_driver.h index b27bf84..32c31a4 100644 --- a/src/gy521_driver/gy521_driver.h +++ b/src/gy521_driver/gy521_driver.h @@ -24,7 +24,19 @@ * ############################ * Types/Structures * ############################ - */ + */ +typedef struct gyro_values{ + uint16_t x; + uint16_t y; + uint16_t z; +}gyro_values_struct; + +typedef struct accel_values{ + uint16_t x; + uint16_t y; + uint16_t z; +}accel_values_struct; + typedef struct gy521_module gy521_module; /* @@ -45,6 +57,7 @@ _Bool gy521_init(struct gy521_module *m, uint8_t slave_address); void gy521_update_gyro(struct gy521_module* m); void gy521_update_accel(struct gy521_module* m); void gy521_free(struct gy521_module *m); - +struct accel_values gy521_get_accel(struct gy521_module* m); +struct gyro_values gy521_get_gyro(struct gy521_module* m); #endif /* GY521_DRIVER_H */ From 48c566501e18354fac42f118afe14e27067a46ab Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 18:03:12 -0700 Subject: [PATCH 08/11] now have test coverage for the gyroscope reading over twi --- tests/gy521_driver/test_gy521_driver.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/gy521_driver/test_gy521_driver.c b/tests/gy521_driver/test_gy521_driver.c index 47435cb..1fd8cf0 100644 --- a/tests/gy521_driver/test_gy521_driver.c +++ b/tests/gy521_driver/test_gy521_driver.c @@ -98,7 +98,7 @@ static void test_gy521_update_accel(void **sate) /*Setup the fake accel values.*/ for(uint8_t i = 0; i < 6; i++) { - reg_addr_arr[i].value = i; + reg_addr_arr[i].value = i * 8; } /*Zero the global index for the TWI*/ @@ -117,8 +117,12 @@ static void test_gy521_update_accel(void **sate) assert_true(is_correct); /*Check that the values are assembled correctly*/ + struct accel_values accel = gy521_get_accel(m); - + /*Make sure to bitshift by 8 and recomine the two u8 into a single u16*/ + assert_true(accel.x == ((reg_addr_arr[0].value<<8) | reg_addr_arr[1].value)); + assert_true(accel.y == ((reg_addr_arr[2].value<<8) | reg_addr_arr[3].value)); + assert_true(accel.z == ((reg_addr_arr[4].value<<8) | reg_addr_arr[5].value)); gy521_free(m); } From bd237b603b0c1c902dbbc9fcd719c95634cb2a31 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 18:09:47 -0700 Subject: [PATCH 09/11] updated with test of updating the gyro and reading it --- tests/gy521_driver/test_gy521_driver.c | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/gy521_driver/test_gy521_driver.c b/tests/gy521_driver/test_gy521_driver.c index 1fd8cf0..2471611 100644 --- a/tests/gy521_driver/test_gy521_driver.c +++ b/tests/gy521_driver/test_gy521_driver.c @@ -128,11 +128,53 @@ static void test_gy521_update_accel(void **sate) } +static void test_gy521_update_gyro(void **sate) +{ + /*check it reads the gyro registers*/ + gy521_module *m = gy521_new(); + reg_addr_arr[0].addr = TWI_GY521_ADDR1; + gy521_init(m, TWI_GY521_ADDR1); + + /*Setup the fake gyro values.*/ + for(uint8_t i = 0; i < 6; i++) { + reg_addr_arr[i].value = i * 8; + } + + /*Zero the global index for the TWI*/ + idx = 0; + gy521_update_gyro(m); + + /*Ensure the correct registers are read*/ + _Bool is_correct = 1; + assert_true(is_correct); + for(uint8_t i = 0; i < 6; i++){ + /*The starting address of the registers is 67 and goes to 72*/ + if((67 + i) != reg_addr_arr[i].addr) { + is_correct = 0; + } + } + assert_true(is_correct); + + /*Check that the values are assembled correctly*/ + struct gyro_values gyro = gy521_get_gyro(m); + + /*Make sure to bitshift by 8 and recomine the two u8 into a single u16*/ + assert_true(gyro.x == ((reg_addr_arr[0].value<<8) | reg_addr_arr[1].value)); + assert_true(gyro.y == ((reg_addr_arr[2].value<<8) | reg_addr_arr[3].value)); + assert_true(gyro.z == ((reg_addr_arr[4].value<<8) | reg_addr_arr[5].value)); + + gy521_free(m); +} + + + + int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_gy521_init), cmocka_unit_test(test_gy521_update_accel), + cmocka_unit_test(test_gy521_update_gyro), }; return cmocka_run_group_tests(tests, NULL, NULL); } From 066e98dd2c7cbcba2e3c25c374d7b754d866146c Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 18:22:46 -0700 Subject: [PATCH 10/11] updated comments --- src/gy521_driver/gy521_driver.c | 2 +- src/gy521_driver/gy521_driver.h | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/gy521_driver/gy521_driver.c b/src/gy521_driver/gy521_driver.c index 41c02db..6b0ab0a 100644 --- a/src/gy521_driver/gy521_driver.c +++ b/src/gy521_driver/gy521_driver.c @@ -1,7 +1,7 @@ /* * Author: Jake Goodwin * Date: 2023 - * Description: + * Description: The implimentation file for the gy521 module, assumes TWI/I2c */ #include "gy521_driver.h" #include diff --git a/src/gy521_driver/gy521_driver.h b/src/gy521_driver/gy521_driver.h index 32c31a4..6349f47 100644 --- a/src/gy521_driver/gy521_driver.h +++ b/src/gy521_driver/gy521_driver.h @@ -1,7 +1,7 @@ /* * Author: Jake Goodwin * Date: 2023 - * Description: + * Description: A Generic driver for gy521 IMU modules */ #ifndef GY521_DRIVER_H @@ -57,6 +57,9 @@ _Bool gy521_init(struct gy521_module *m, uint8_t slave_address); void gy521_update_gyro(struct gy521_module* m); void gy521_update_accel(struct gy521_module* m); void gy521_free(struct gy521_module *m); + +/*These are used instead of direct access; assuming interrupt driven*/ +/*updates to the gy521 then it protects the user from using volatile values*/ struct accel_values gy521_get_accel(struct gy521_module* m); struct gyro_values gy521_get_gyro(struct gy521_module* m); From 89291840ddada3434cf0a2cb3cdf561f953b5fdd Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 31 Aug 2023 18:22:53 -0700 Subject: [PATCH 11/11] updated with the actual info now --- README.md | 105 ++++++++++++------------------------------------------ 1 file changed, 23 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 9d5dc3b..5325533 100644 --- a/README.md +++ b/README.md @@ -1,68 +1,30 @@ -# CMOCKA/CMAKE template +# gy-521 Driver -## Purporse +The gy-521 module is usally interfaced with via TWI/I2C. The actual chip that +is used in the module is the mpu6050 ic. -- Streamline the setup of new C projects -- Make it easy to setup a develoment enviroment -- Allow TDD for embedded systems -- Provide easy LSP usage via compile_commands.json -- correctly use the CTEST command. -- Avoid having to setup ruby/ceedling with it's dependency hell +This driver doesn't make any assumptions about the platform it will run +on beyond that it supports "stdlib.h" and "inttypes.h". -## Organization +The interface handles all the register and bitshift operations in the +the backgorund. The module uses getters and setters to abstract the +actual data. This prevents the end user from trying to directly use the +structures data when it is volatile and may be setup to update in a ISR. -``` -. -├── CMakeLists.txt -├── LICENSE -├── README.md -├── build -├── cmake -│   └── cmocka -│   ├── AddCCompilerFlag.cmake -│   ├── AddCMockaTest.cmake -│   ├── COPYING-CMAKE-SCRIPTS -│   ├── CheckCCompilerFlagSSP.cmake -│   ├── DefineCMakeDefaults.cmake -│   ├── DefineCompilerFlags.cmake -│   ├── DefinePlatformDefaults.cmake -│   ├── FindNSIS.cmake -│   └── MacroEnsureOutOfSourceBuild.cmake -├── compile_commands.json -> ./build/compile_commands.json -├── git_modules -├── src -│   ├── CMakeLists.txt -│   ├── led_driver -│   │   ├── CMakeLists.txt -│   │   ├── led_driver.c -│   │   └── led_driver.h -│   ├── main.c -│   └── main.h -└── tests - ├── CMakeLists.txt - ├── led_driver - │   ├── CMakeLists.txt - │   └── test_led_driver.c - └── simple_test - ├── CMakeLists.txt - └── simple_test.c +## PINs -10 directories, 24 files -``` - -The actual code is all stored inside the src direcotry with any libs/submodules -stored inside their own subdirectories. - -The code we use to test stuff gets stored inside the dir "tests" in the -projects root. - -The compile_commands.json file in the project root is actaully a symlink to -the generated compile_commands.json file inside the build directory. So if -you notice your LSP isn't working correctly it probably means that you haven't -ran cmake yet. +- VCC: 3.3v +- GND: ground or 0v +- SCL: TWI clock +- SDA: TWI data +- XDA: +- XCL: +- AD0: changes the TWI address when pulled high. +- INT: interrupt pin ## Running and building the project +The project uses CMake along with cmocka for unit testing. ```sh # change into the build dir @@ -76,33 +38,12 @@ ctest ``` -# Adding tests - -To actually add tests to the project create a new directory inside the -"tests" dir. From there it will need a source.c and CMAKELists.txt file. - -You will also need to include it inside the tests/CMakeLists.txt file as well. - -```cmake -add_subdirectory(new_test) -``` - ## RoadMap -Things I want to add in the future: - -- Automatic module creation -- template generation -- header --> mocking -- report generation -- embedded specific scripts for common hardware items - - - - -##TDD stuff - -- what does a struct typedef \*struct actually mean? +- add in functions for interrupt driven updates. +- add functions for configuration of module. +- add functions to save configurations in memory. +- add functions to load saved configs