Skip to content

Commit

Permalink
fixed some i2c-related errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dakhnod committed Oct 12, 2024
1 parent a9ad1a7 commit 3933b7b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ SRC_FILES += \
$(SDK_ROOT)/components/libraries/util/nrf_assert.c \
$(SDK_ROOT)/components/libraries/uart/retarget.c \
$(SDK_ROOT)/components/libraries/util/sdk_errors.c \
$(SDK_ROOT)/components/libraries/twi/app_twi.c \
$(SDK_ROOT)/components/drivers_nrf/clock/nrf_drv_clock.c \
$(SDK_ROOT)/components/drivers_nrf/common/nrf_drv_common.c \
$(SDK_ROOT)/components/drivers_nrf/uart/nrf_drv_uart.c \
$(SDK_ROOT)/components/drivers_nrf/spi_master/nrf_drv_spi.c \
$(SDK_ROOT)/components/drivers_nrf/twi_master/nrf_drv_twi.c \
$(SDK_ROOT)/external/segger_rtt/RTT_Syscalls_GCC.c \
$(SDK_ROOT)/external/segger_rtt/SEGGER_RTT.c \
$(SDK_ROOT)/external/segger_rtt/SEGGER_RTT_printf.c \
Expand Down Expand Up @@ -81,6 +83,7 @@ SRC_FILES += \
$(PROJ_DIR)/src/ble/services/cycling_speed_cadence/ble_cycling_speed_cadence.c \
$(PROJ_DIR)/src/ble/services/hid/ble_hid.c \
$(PROJ_DIR)/src/ble/services/temperature/ble_temperature_service.c \
$(PROJ_DIR)/src/ble/services/i2c/i2c_service.c \
$(PROJ_DIR)/src/ble/helpers/ble_helpers.c \
$(PROJ_DIR)/src/ble/sensor_ble.c \
$(PROJ_DIR)/src/gpio/sensor_gpio.c \
Expand Down Expand Up @@ -204,6 +207,7 @@ INC_FOLDERS += \
$(PROJ_DIR)/src/ble/services/cycling_speed_cadence/ \
$(PROJ_DIR)/src/ble/services/hid/ \
$(PROJ_DIR)/src/ble/services/temperature/ \
$(PROJ_DIR)/src/ble/services/i2c/ \
$(PROJ_DIR)/src/ble/helpers/ \
$(PROJ_DIR)/src/ble/ \
$(PROJ_DIR)/src/helpers/ \
Expand Down
1 change: 1 addition & 0 deletions src/ble/sensor_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "sleep.h"
#include "ble_temperature_service.h"
#include "ble_helpers.h"
#include "i2c_service.h"

#define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (15 seconds). */
#define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (5 seconds). */
Expand Down
61 changes: 35 additions & 26 deletions src/ble/services/i2c/i2c_service.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include "twi_app.h"
#include "app_twi.h"
#include "app_error.h"
#include "ble_srv_common.h"
#include "ble_helpers.h"

#define TRANSACTION_QUEUE_SIZE 1

#define UUID_I2C_BASE[] { 0x06, 0xC4, 0x44, 0x05, 0xD9, 0x7A, 0x45, 0x89, 0xBF, 0x5E, 0xF3, 0xE9, 0xA4, 0x37, 0x24, 0x2C }
#define UUID_I2C_SERVICE = 0x0000;
#define UUID_I2C_TX_CHARACTERISTIC = 0x0001;
#define UUID_I2C_RX_CHARACTERISTIC = 0x0002;
#define UUID_I2C_BASE { 0x06, 0xC4, 0x44, 0x05, 0xD9, 0x7A, 0x45, 0x89, 0xBF, 0x5E, 0xF3, 0xE9, 0xA4, 0x37, 0x24, 0x2C }
#define UUID_I2C_SERVICE 0x0000
#define UUID_I2C_TX_CHARACTERISTIC 0x0001
#define UUID_I2C_RX_CHARACTERISTIC 0x0002

app_twi_t twi_instance = APP_TWI_INSTANCE(0);

Expand All @@ -24,11 +27,11 @@ void i2c_add_service() {
.uuid128 = UUID_I2C_BASE
};

err_code = sd_ble_uuid_vs_add(&vs_uuid, &ble_gpio_asm_custom_uuid_type);
err_code = sd_ble_uuid_vs_add(&vs_uuid, &ble_i2c_custom_uuid_type);
APP_ERROR_CHECK(err_code);

ble_uuid_t uuid_service = {
.uuid = UUID_I2C_TX_SERVICE,
.uuid = UUID_I2C_SERVICE,
.type = ble_i2c_custom_uuid_type
};

Expand All @@ -41,7 +44,7 @@ void i2c_add_service() {

void i2c_add_characteristics(){
ble_helper_characteristic_init_t init_tx = {
.service_handle = ble_csc_service_handle,
.service_handle = ble_i2c_service_handle,
.uuid = UUID_I2C_TX_CHARACTERISTIC,
.is_writable = true,
.authorize_write = true,
Expand All @@ -52,7 +55,7 @@ void i2c_add_characteristics(){


ble_helper_characteristic_init_t init_rx = {
.service_handle = ble_csc_service_handle,
.service_handle = ble_i2c_service_handle,
.uuid = UUID_I2C_RX_CHARACTERISTIC,
.is_notifiable = true,
.value_handle = &ble_i2c_rx_characteristic_handle,
Expand All @@ -61,12 +64,12 @@ void i2c_add_characteristics(){
APP_ERROR_CHECK(ble_helper_characteristic_add(&init_rx));
}

void authorization_respond(success) {
void authorization_respond(bool success) {
ble_gatts_rw_authorize_reply_params_t authorize_params = {
.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
.params.write = {
.update = 0,
.gatt_status = (err_code != 0)
.gatt_status = success
}
};

Expand All @@ -79,11 +82,11 @@ void authorization_respond(success) {
void i2c_write(uint8_t address, uint8_t *data, uint8_t length) {
ret_code_t err_code;

app_twi_transfers transfer = APP_TWI_WRITE(address, data, length, 0);
app_twi_transfer_t transfer = APP_TWI_WRITE(address, data, length, 0);

// TODO: this should be async
err_code = app_twi_perform(
twi_instance,
&twi_instance,
&transfer,
1,
NULL
Expand All @@ -95,14 +98,14 @@ void i2c_write(uint8_t address, uint8_t *data, uint8_t length) {
void i2c_read(uint8_t address, uint8_t register_address, uint8_t *buffer, uint8_t length) {
ret_code_t err_code;

app_twi_transfers transfers = {
app_twi_transfer_t transfers[] = {
APP_TWI_WRITE(address, &register_address, 1, 0),
APP_TWI_READ(address, buffer, length, 0)
};

// TODO: this should be async
err_code = app_twi_perform(
twi_instance,
&twi_instance,
transfers,
2,
NULL
Expand All @@ -111,8 +114,10 @@ void i2c_read(uint8_t address, uint8_t register_address, uint8_t *buffer, uint8_
authorization_respond(err_code == 0);
}

void on_i2c_tx_authorize_write(p_ble_evt_t *ble_evt) {
ble_gatts_evt_rw_authorize_request_t *req = &(p_ble_evt
void on_i2c_tx_authorize_write(ble_evt_t *ble_evt) {
ret_code_t err_code;

ble_gatts_evt_rw_authorize_request_t *req = &(ble_evt
->evt.gatts_evt
.params
.authorize_request);
Expand All @@ -126,13 +131,13 @@ void on_i2c_tx_authorize_write(p_ble_evt_t *ble_evt) {
->request
.write;

if (write_evt->handle != ble_i2c_tx_characteristic_handle)
if (write_evt.handle != ble_i2c_tx_characteristic_handle)
{
return;
}

uint8_t *data = write_evt->p_data;
uint8_t len = write_evt->len;
uint8_t *data = write_evt.data;
uint8_t len = write_evt.len;

if(len < 2) {
return;
Expand All @@ -145,16 +150,17 @@ void on_i2c_tx_authorize_write(p_ble_evt_t *ble_evt) {
if(is_write) {
i2c_write(address, data, len);
return;
}

uint8_t buffer[19];

uint8_t read_len = 1;
uint16_t read_len = 1;

if(len > 2) {
read_len = data[2];
}

i2c_read(address, write_evt->p_data[1], buffer, read_len);
i2c_read(address, data[1], buffer, read_len);

ble_gatts_hvx_params_t params = {
.handle = ble_i2c_rx_characteristic_handle,
Expand Down Expand Up @@ -194,16 +200,19 @@ void ble_i2c_on_ble_evt(ble_evt_t *p_ble_evt)
}
}

void ble_i2c_init() {
ret_code_t ble_i2c_init() {
ret_code_t err_code;

p_twi_config config = {

nrf_drv_twi_config_t const config = {
.scl = 1,
.sda = 0
};

APP_TWI_INIT(twi_instance, &config, TRANSACTION_QUEUE_SIZE, err_code);
APP_TWI_INIT(&twi_instance, &config, TRANSACTION_QUEUE_SIZE, err_code);
APP_ERROR_CHECK(err_code);

i2c_add_service();
i2c_add_characteristics();

return NRF_SUCCESS;
}
1 change: 1 addition & 0 deletions src/ble/services/i2c/i2c_service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ret_code_t ble_i2c_init();
8 changes: 4 additions & 4 deletions src/config/sdk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2531,7 +2531,7 @@
// <e> TWI_ENABLED - nrf_drv_twi - TWI/TWIM peripheral driver
//==========================================================
#ifndef TWI_ENABLED
#define TWI_ENABLED 0
#define TWI_ENABLED 1
#endif
#if TWI_ENABLED
// <o> TWI_DEFAULT_CONFIG_FREQUENCY - Frequency
Expand Down Expand Up @@ -2574,7 +2574,7 @@
// <e> TWI0_ENABLED - Enable TWI0 instance
//==========================================================
#ifndef TWI0_ENABLED
#define TWI0_ENABLED 0
#define TWI0_ENABLED 1
#endif
#if TWI0_ENABLED
// <q> TWI0_USE_EASY_DMA - Use EasyDMA (if present)
Expand Down Expand Up @@ -2606,7 +2606,7 @@
// <e> TWI_CONFIG_LOG_ENABLED - Enables logging in the module.
//==========================================================
#ifndef TWI_CONFIG_LOG_ENABLED
#define TWI_CONFIG_LOG_ENABLED 0
#define TWI_CONFIG_LOG_ENABLED 1
#endif
#if TWI_CONFIG_LOG_ENABLED
// <o> TWI_CONFIG_LOG_LEVEL - Default Severity level
Expand Down Expand Up @@ -2996,7 +2996,7 @@


#ifndef APP_TWI_ENABLED
#define APP_TWI_ENABLED 0
#define APP_TWI_ENABLED 1
#endif

// <e> APP_UART_ENABLED - app_uart - UART driver
Expand Down

0 comments on commit 3933b7b

Please sign in to comment.