Files
firmware-test/project/ble_peripheral/ble_app_bladder_patch/cat_i2c.c
Charles Kwon a8ba31871e Initial commit: MT firmware project
- BLE peripheral applications
- dr_piezo and bladder_patch projects

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 17:26:39 +09:00

164 lines
3.8 KiB
C

/*******************************************************************************
* @file cat_i2c.c
* @brief
******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include "nrf.h"
#include "app_error.h"
#include "boards.h"
#include "nrfx_gpiote.h"
#include "nrfx_twi.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"
#include "cat_i2c.h"
#define CAT_I2C_ADDR 0x50
#define MAX_SERIAL_WRITE 16
//int16_t read_from_DS3930 = 0;
//uint16_t data_160_to_write = 0;
//static volatile bool m_xfer_done = false;
/* TWI instance. */
//const nrfx_twi_t m_twi_ir = NRFX_TWI_INSTANCE(IR_I2C_INSTANCE);
const nrf_drv_twi_t m_twi_cat = NRF_DRV_TWI_INSTANCE (CAT_I2C_INSTANCE);
//void twi_handler(nrfx_twi_evt_t const * p_event, void * p_context)
//{
// m_xfer_done = true;
//}
//void ir_irq_init(void){
// ret_code_t err_code;
// /* Initialize int pin */
// if (!nrfx_gpiote_is_init())
// {
// err_code = nrfx_gpiote_init();
// APP_ERROR_CHECK(err_code);
// }
// nrfx_gpiote_in_config_t in_config = NRFX_GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
// in_config.pull = NRF_GPIO_PIN_PULLDOWN;
// err_code = nrfx_gpiote_in_init(ADA2200_SYNCO_PIN, &in_config, NULL);
// APP_ERROR_CHECK(err_code);
// nrfx_gpiote_in_event_enable(ADA2200_SYNCO_PIN, true);
//}
//void ir_irq_uninit(void){
// nrfx_gpiote_in_event_disable(ADA2200_SYNCO_PIN);
// nrfx_gpiote_in_uninit(ADA2200_SYNCO_PIN);
//}
void cat_i2c_uninit(void){
nrf_drv_twi_disable(&m_twi_cat);
nrf_drv_twi_uninit(&m_twi_cat);
//
}
void cat_i2c_init(void){
ret_code_t err_code;
const nrf_drv_twi_config_t twi_cat_config = {
.scl = CAT_I2C_SCL_PIN,
.sda = CAT_I2C_SDA_PIN,
.frequency = NRF_DRV_TWI_FREQ_100K,
.interrupt_priority = APP_IRQ_PRIORITY_HIGH,
.clear_bus_init = false
};
err_code = nrf_drv_twi_init(&m_twi_cat, &twi_cat_config, NULL, NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&m_twi_cat);
// ir_irq_init();
}
uint8_t cat_command_read(uint8_t device_id, uint8_t address, uint8_t *data)
{
uint8_t read_data = 0;
char adata[8];
ret_code_t err_code;
//address = 1|(address<<1);
address = (address & 0xFF);
err_code = nrf_drv_twi_tx(&m_twi_cat, device_id, &address, 1, true);
if (err_code != NRF_SUCCESS) {
// Handle error
// return;
}
err_code = nrf_drv_twi_rx(&m_twi_cat, device_id, data, 8);
if (err_code != NRF_SUCCESS) {
// Handle error
return 0;
}
read_data = data[0];
memcpy(adata,data,8);
printf("Data %s . \r\n", adata);
return read_data;
}
void cat_command_write(uint8_t device_id, uint8_t address, uint8_t *data)
{
//uint16_t data_to_write = 0;
uint8_t buffer[9]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
address = (address & 0xFF);
//buffer[0] = 0x00;
buffer[0] = (address);
// buffer[1] =(data & 0xFF);
// buffer[2] = data1+1;
// buffer[3] = data1+2;
// buffer[4] = data1+3;
// buffer[5] = data1+4;
// buffer[6] = data1+5;
memcpy(&buffer[1], data, 8 );
ret_code_t err_code;
//err_code = nrf_drv_twi_tx(&m_twi_ir, device_id, 0x00, 1, false);
err_code = nrf_drv_twi_tx(&m_twi_cat, device_id, buffer, 9, false);
printf("Data %x %x %x %x. \r\n", buffer[0], buffer[1], buffer[2], buffer[3]);
printf("Data %s. \r\n", buffer);
//err_code = nrf_drv_twi_tx(&m_twi_ir, device_id, buffer, 6, false);
if (err_code != NRF_SUCCESS) {
printf("TWI Error.\r\n");
}
}