2023-08-31 03:16:47 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <cmocka.h>
|
|
|
|
#include "gy521_driver.h"
|
|
|
|
|
2023-08-31 06:31:45 +00:00
|
|
|
#define WRITE_BIT (1<<7)
|
|
|
|
|
2023-09-01 00:07:13 +00:00
|
|
|
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;
|
2023-08-31 03:16:47 +00:00
|
|
|
|
|
|
|
/* Fake Object for TWI_TX*/
|
|
|
|
void fake_twi_tx(uint8_t slave_addr, uint8_t *data, uint8_t size)
|
|
|
|
{
|
2023-09-01 00:07:13 +00:00
|
|
|
/*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);
|
2023-08-31 07:08:03 +00:00
|
|
|
}
|
2023-09-01 00:07:13 +00:00
|
|
|
/*Post increment the global index*/
|
|
|
|
idx++;
|
2023-08-31 06:31:45 +00:00
|
|
|
}
|
2023-08-31 03:16:47 +00:00
|
|
|
|
2023-08-31 06:31:45 +00:00
|
|
|
/* Fake Object for TWI_RX*/
|
|
|
|
void fake_twi_rx(uint8_t slave_addr, uint8_t *data, uint8_t size)
|
|
|
|
{
|
2023-09-01 00:07:13 +00:00
|
|
|
/*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;
|
2023-08-31 06:31:45 +00:00
|
|
|
}
|
2023-09-01 00:07:13 +00:00
|
|
|
/*Post increment the global index*/
|
|
|
|
idx++;
|
2023-08-31 03:16:47 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 06:31:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*Setup the fake twi*/
|
|
|
|
void (*gy521_twi_tx)(uint8_t, uint8_t*, uint8_t) = &fake_twi_tx;
|
|
|
|
void (*gy521_twi_rx)(uint8_t, uint8_t*, uint8_t) = &fake_twi_rx;
|
|
|
|
|
2023-08-31 03:16:47 +00:00
|
|
|
/* Tests the donothing function */
|
|
|
|
static void test_gy521_init(void **state) {
|
2023-08-31 06:31:45 +00:00
|
|
|
gy521_module *m = gy521_new();
|
|
|
|
assert_false(gy521_init(m, 0x0));
|
|
|
|
assert_false(gy521_init(m, 0x67));
|
|
|
|
assert_false(gy521_init(m, 0x6A));
|
2023-08-31 03:16:47 +00:00
|
|
|
|
2023-08-31 06:31:45 +00:00
|
|
|
/*Check for it's confirmation of the right twi device*/
|
|
|
|
assert_false(gy521_init(m, TWI_GY521_ADDR1));
|
2023-08-31 03:16:47 +00:00
|
|
|
|
2023-09-01 00:07:13 +00:00
|
|
|
/*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*/
|
2023-08-31 06:31:45 +00:00
|
|
|
assert_true(gy521_init(m, TWI_GY521_ADDR1));
|
2023-09-01 00:07:13 +00:00
|
|
|
assert_true(TWI_GY521_ADDR1 == reg_addr_arr[0].dev_addr);
|
2023-08-31 03:16:47 +00:00
|
|
|
|
2023-08-31 06:31:45 +00:00
|
|
|
|
2023-09-01 00:07:13 +00:00
|
|
|
reg_addr_arr[0].addr = TWI_GY521_ADDR2;
|
|
|
|
reg_addr_arr[0].value = TWI_GY521_ADDR2;
|
|
|
|
idx = 0;
|
|
|
|
|
2023-08-31 06:31:45 +00:00
|
|
|
assert_true(gy521_init(m, TWI_GY521_ADDR2));
|
2023-09-01 00:07:13 +00:00
|
|
|
assert_true(TWI_GY521_ADDR2 == reg_addr_arr[0].dev_addr);
|
2023-08-31 06:31:45 +00:00
|
|
|
|
|
|
|
gy521_free(m);
|
2023-08-31 03:16:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-31 07:08:03 +00:00
|
|
|
static void test_gy521_update_accel(void **sate)
|
2023-08-31 06:31:45 +00:00
|
|
|
{
|
2023-08-31 07:08:03 +00:00
|
|
|
/*check it reads the accel registers*/
|
|
|
|
gy521_module *m = gy521_new();
|
2023-09-01 00:07:13 +00:00
|
|
|
reg_addr_arr[0].addr = TWI_GY521_ADDR1;
|
2023-08-31 07:08:03 +00:00
|
|
|
gy521_init(m, TWI_GY521_ADDR1);
|
|
|
|
|
2023-09-01 00:07:13 +00:00
|
|
|
/*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);
|
|
|
|
|
|
|
|
|
2023-08-31 07:08:03 +00:00
|
|
|
/*Ensure the correct registers are read*/
|
2023-09-01 00:07:13 +00:00
|
|
|
_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);
|
2023-08-31 07:08:03 +00:00
|
|
|
|
|
|
|
/*Check that the values are assembled correctly*/
|
|
|
|
|
|
|
|
gy521_free(m);
|
2023-08-31 06:31:45 +00:00
|
|
|
}
|
2023-08-31 03:16:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test(test_gy521_init),
|
2023-08-31 07:08:03 +00:00
|
|
|
cmocka_unit_test(test_gy521_update_accel),
|
2023-08-31 03:16:47 +00:00
|
|
|
};
|
|
|
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
|
|
|
}
|