Skip to content

Changed platform interface to improve multiplatform support #13

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions examples/RPI/Example1_Basics/Example1_Basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ HSCDTD008A geomag;
void setup() {
hscdtd_status_t status;

geomag.begin();
//geomag.begin();
// If you know the I2C address is different than in the provided
// data sheet. Uncomment the line below, and configure the address.
geomag.begin(0x0F);
geomag.begin(0x0F,(char*)"/dev/i2c-1");

// Initialize the hardware.
status = geomag.initialize();
Expand Down
12 changes: 9 additions & 3 deletions examples/RPI/Example1_Basics/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
INCLUDE=../../../src ../../../src/driver
FLAGS = -DRPI -Wall -c

Example1_Basics: Example1_Basics.o hscdtd008a.o hscdtd008a_driver.o transport.o platform_rpi.o
g++ -o Example1_Basics Example1_Basics.o hscdtd008a.o hscdtd008a_driver.o transport.o platform_rpi.o
all: Example1_Basics


Example1_Basics: Example1_Basics.o libhscdtd008a.a
g++ -o Example1_Basics Example1_Basics.o -L. -lhscdtd008a

libhscdtd008a.a: hscdtd008a.o hscdtd008a_driver.o transport.o platform_rpi.o
ar cvq libhscdtd008a.a hscdtd008a.o hscdtd008a_driver.o transport.o platform_rpi.o

Example1_Basics.o: Example1_Basics.cpp
g++ $(FLAGS) -I$(INCLUDE) -o Example1_Basics.o Example1_Basics.cpp
Expand All @@ -21,4 +27,4 @@ platform_rpi.o: ../../../src/driver/platform_rpi.cpp
g++ $(FLAGS) -I$(INCLUDE) -o platform_rpi.o ../../../src/driver/platform_rpi.cpp

clean:
rm -f *.o Example1_Basics
rm -f *.o Example1_Basics *.a
30 changes: 30 additions & 0 deletions src/driver/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* common.h
*
* Created on: Sep 25, 2022
* Author: tilman
*/

#ifndef SRC_DRIVER_COMMON_H_
#define SRC_DRIVER_COMMON_H_

typedef enum {
HSCDTD_STATE_NORMAL = 0b00,
HSCDTD_STATE_FORCE = 0b01,
} hscdtd_state_t;

typedef enum {
HSCDTD_MODE_STANDBY = 0b00,
HSCDTD_MODE_ACTIVE = 0b01,
} hscdtd_mode_t;

typedef struct {
uint8_t addr;
char* device;
int8_t fd;
hscdtd_state_t state;
hscdtd_mode_t mode;
} hscdtd_device_t;


#endif /* SRC_DRIVER_COMMON_H_ */
6 changes: 3 additions & 3 deletions src/driver/hscdtd008a_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
* @return hscdtd_status.
*/
hscdtd_status_t hscdtd_configure_virtual_device(hscdtd_device_t *p_dev,
uint8_t addr)
uint8_t addr, char* device)
{
if (!p_dev) {
return HSCDTD_STAT_ERROR;
}
p_dev->addr = addr;

p_dev->device = device;
// The force state is the default state for the device.
p_dev->state = HSCDTD_STATE_FORCE;

Expand Down Expand Up @@ -48,7 +48,7 @@ hscdtd_status_t hscdtd_initialize(hscdtd_device_t *p_dev)
}

// Open transport.
t_open();
t_open(p_dev);

// Wait a bit for the I2C bus to open.
t_sleep_ms(100);
Expand Down
18 changes: 3 additions & 15 deletions src/driver/hscdtd008a_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ extern "C"
* CTRL1 Settings
*/

typedef enum {
HSCDTD_MODE_STANDBY = 0b00,
HSCDTD_MODE_ACTIVE = 0b01,
} hscdtd_mode_t;
#include "common.h"

typedef enum {
HSCDTD_ODR_0_5HZ = 0b00,
Expand All @@ -40,10 +37,7 @@ typedef enum {
} hscdtd_odr_t;


typedef enum {
HSCDTD_STATE_NORMAL = 0b00,
HSCDTD_STATE_FORCE = 0b01,
} hscdtd_state_t;



/* --------------------------------------------------
Expand Down Expand Up @@ -102,12 +96,6 @@ typedef struct {
} hscdtd_mag_t;


typedef struct {
uint8_t addr;
hscdtd_state_t state;
hscdtd_mode_t mode;
} hscdtd_device_t;


typedef enum {
HSCDTD_STAT_OK = 0x00,
Expand All @@ -121,7 +109,7 @@ typedef enum {


hscdtd_status_t hscdtd_configure_virtual_device(hscdtd_device_t *p_dev,
uint8_t addr);
uint8_t addr, char *device);

hscdtd_status_t hscdtd_initialize(hscdtd_device_t *p_dev);

Expand Down
23 changes: 14 additions & 9 deletions src/driver/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@
#define __PLATFORM__

#include <stdint.h>
#include "hscdtd008a_driver.h"
#include "common.h"

#define I2C_MODE_STD 100000
#define I2C_MODE_FAST 400000
#define I2C_MODE_FAST_PLUS 1000000
#define I2C_MODE_HIGH_SPEED 3400000



#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus


/**
* @brief Open a connection with the device.
*
* @return 0 on success.
*/
int8_t t_open(void);
int8_t t_open(hscdtd_device_t *p_dev);

/**
* @brief Read registers from the device.
Expand All @@ -31,10 +36,10 @@ int8_t t_open(void);
*
* @return 0 on success.
*/
int8_t t_read_register(uint8_t addr,
uint8_t reg,
int8_t t_read_register(uint8_t reg,
uint8_t length,
uint8_t *p_buffer);
uint8_t *p_buffer,
hscdtd_device_t *p_dev);


/**
Expand All @@ -47,25 +52,25 @@ int8_t t_read_register(uint8_t addr,
*
* @return 0 on success.
*/
int8_t t_write_register(uint8_t addr,
uint8_t reg,
int8_t t_write_register(uint8_t reg,
uint8_t length,
uint8_t *p_buffer);
uint8_t *p_buffer,
hscdtd_device_t *p_dev);

/**
* @brief Flush the connection.
*
* @return 0 on success.
*/
int8_t t_flush(void);
int8_t t_flush(hscdtd_device_t *p_dev);


/**
* @brief Close the connection.
*
* @return 0 on success.
*/
int8_t t_close(void);
int8_t t_close(hscdtd_device_t *p_dev);


/**
Expand Down
25 changes: 13 additions & 12 deletions src/driver/platform_arduino.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
#ifdef ARDUINO
#include "platform.h"
#include "hscdtd008a_driver.h"

#include <Arduino.h>
#include <Wire.h>


int8_t t_open(void)
int8_t t_open(hscdtd_device_t *p_dev)
{
Wire.begin();
Wire.setClock(I2C_MODE_STD);
return 0;
}

int8_t t_read_register(uint8_t addr,
uint8_t reg,
int8_t t_read_register(uint8_t reg,
uint8_t length,
uint8_t *p_buffer)
uint8_t *p_buffer,
hscdtd_device_t *p_dev)
{
uint8_t status;
int8_t i = 0;

Wire.beginTransmission(addr);
Wire.beginTransmission(p_dev->addr);
Wire.write(reg);
status = Wire.endTransmission();
if (status != 0)
return -status;

Wire.requestFrom(addr, length);
Wire.requestFrom(p_dev->addr, length);

for (i = 0; i < length; i++) {
p_buffer[i] = Wire.read();
}
return 0;
}

int8_t t_write_register(uint8_t addr,
uint8_t reg,
int8_t t_write_register(uint8_t reg,
uint8_t length,
uint8_t *p_buffer)
uint8_t *p_buffer,
hscdtd_device_t *p_dev)
{
uint8_t status;

Wire.beginTransmission(addr);
Wire.beginTransmission(p_dev->addr);
Wire.write(reg);

for (int8_t i = 0; i < length; i++)
Expand All @@ -55,7 +56,7 @@ int8_t t_write_register(uint8_t addr,
return 0;
}

int8_t t_flush(void)
int8_t t_flush(hscdtd_device_t *p_dev)
{
while (Wire.available() > 0) {
Wire.read(); // just flush the data.
Expand All @@ -64,7 +65,7 @@ int8_t t_flush(void)
}


int8_t t_close(void)
int8_t t_close(hscdtd_device_t *p_dev)
{
uint8_t status;

Expand Down
Loading