Skip to content
Snippets Groups Projects
Verified Commit 7a27e641 authored by Jakub Vaněk's avatar Jakub Vaněk
Browse files

Handle-ize gyro driver

parent 980edfa4
No related branches found
No related tags found
No related merge requests found
......@@ -6,12 +6,22 @@ and register map: https://invensense.tdk.com/download-pdf/icm-20608-g-register-m
#include <stdio.h>
#include <stdlib.h>
#include <opencube_hw.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "ICM20608.h"
#include "shared_resource.h"
void ICM20608G_init(ICM20608G_t *sensor)
static void ICM20608G_init(mp_obj_t self_in);
DEFINE_SHARED_RESOURCE(ICM20608G_t, sensor_obj, ICM20608G_init, NULL);
ICM20608G_t *ICM20608G_get(void)
{
return shared_resource_get_or_new(&sensor_obj);
}
void ICM20608G_init(mp_obj_t self_in)
{
ICM20608G_t *sensor = MP_OBJ_TO_PTR(self_in);
sensor->address = ICM20608_I2C_ADDR;
sensor->i2c_port = INTERNAL_I2C_BUS;
// Check the device ID
......
......@@ -8,6 +8,7 @@ and register map: https://invensense.tdk.com/download-pdf/icm-20608-g-register-m
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "shared_resource.h"
// Device ID
#define ICM20608G_WHO_AM_I 0x75
......@@ -63,6 +64,7 @@ and register map: https://invensense.tdk.com/download-pdf/icm-20608-g-register-m
typedef struct
{
shared_resource_base_t base;
i2c_inst_t *i2c_port; // I2C port of the sensor
uint8_t address; // I2C address of the sensor
uint8_t dev_id; // Device ID of the sensor
......@@ -74,7 +76,7 @@ typedef struct
} ICM20608G_t;
// Initialize ICM20608G
void ICM20608G_init(ICM20608G_t *sensor);
ICM20608G_t *ICM20608G_get(void);
// Write value to given register
void ICM20608G_write_register(ICM20608G_t *sensor, uint8_t reg, uint8_t value);
......
......@@ -21,7 +21,7 @@ STATIC mp_obj_t ICM20608_make_new(const mp_obj_type_t* type, size_t n_args, size
self->base.type = type;
self->icm = (ICM20608G_t*)malloc(sizeof(ICM20608G_t));
opencube_lock_i2c_or_raise();
ICM20608G_init(self->icm);
self->icm = ICM20608G_get();
opencube_unlock_i2c();
return MP_OBJ_FROM_PTR(self);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment