Hello, im trying to implement PingPong buffer( i guess that's the name of mechanism )
the way i have it currently done is.
#include "twim_data.h"
#include "twim_r.h"
#define MASTER_SCL_PIN 3
#define MASTER_SDA_PIN 4
sensor_data_t BUFF1[8];
sensor_data_t BUFF2[8];
#define BME280_I2C_ADDRESS 0x76
// SYF
LOG_MODULE_REGISTER(twim_r, LOG_LEVEL_INF);
uint8_t counter = 0;
struct k_work work;
struct twi_info {
};
sensor_data_t *ptr_curr = BUFF1;
bool dupa = true;
int XD = 1;
// Za kazdym razem jak stanie sie counter >8 to zresetuj buffor.
void show_dump_twim(struct k_work *work)
{
if(ptr_curr == BUFF1)
{
LOG_INF("BUFF1\n");
}
else
{
LOG_INF("BUFF2\n");
}
LOG_HEXDUMP_INF(ptr_curr, 48, "TWIM RX BUFFER");
//disable twim with buff A
nrfx_twim_disable(&mytwim.instance);
//init twim with buff B
twim_init(&mytwim);
}
void Buff_reset(uint8_t *ptr_buff)
{
if (ptr_buff != NULL){
memset(ptr_buff, 0, sizeof(sensor_data_t)*8);
LOG_HEXDUMP_INF(ptr_buff, 48, "RESET BUFFER");
}
}
static void twim_handler(nrfx_twim_evt_t const *p_event, void *p_context)
{
if(counter >= 8){
counter = 0;
k_work_submit(&work);
}
LOG_INF("TWIM HANDLER\n");
if (p_event->type == NRFX_TWIM_EVT_DONE)
{
LOG_INF("TWIM EVT DONE\n");
counter++;
LOG_INF("COUNTER: %d\n", counter);
}
if (p_event->type == NRFX_TWIM_EVT_BUS_ERROR)
{
LOG_INF("BUS ERROR");
}
if (p_event->type == NRFX_TWIM_EVT_ADDRESS_NACK)
{
LOG_INF("ADDRESS NACK\n");
}
}
void twim_init(twim_config_t *twim)
{
// if (twim->read_buffer == NULL && twim->write_buffer == NULL)
// {
// LOG_INF("READ BUFFER IS NULL");
// }
if(dupa == true) {
k_work_init(&work, show_dump_twim);
dupa = false;
}
pp_buffer_swap(twim);
void *p_context = ptr_curr;
nrfx_twim_config_t twim_config = {
.scl_pin = MASTER_SCL_PIN,
.sda_pin = MASTER_SDA_PIN,
.frequency = NRF_TWIM_FREQ_100K,
.interrupt_priority = NRFX_TWIM_DEFAULT_CONFIG_IRQ_PRIORITY,
.hold_bus_uninit = true
};
nrfx_err_t status;
if(XD == 1){
status = nrfx_twim_init(&twim->instance, &twim_config, twim_handler, p_context);
if (status != NRFX_SUCCESS)
{
LOG_ERR("TWIM INIT FAILED\n");
}
}
if(status == NRFX_ERROR_ALREADY_INITIALIZED){
LOG_INF("TWIM ALREADY INITIALIZED\n");
}
nrfx_twim_enable(&twim->instance);
nrfx_twim_xfer_desc_t twim_xfer_desc = NRFX_TWIM_XFER_DESC_TXRX(BME280_I2C_ADDRESS, // BME280 I2C
twim->write_buffer, // Buffer z adresem rejestru TX
1, // Amount
ptr_curr, // RX buffer
twim->read_len // Amount
);
status = nrfx_twim_xfer(&twim->instance, &twim_xfer_desc, NRFX_TWIM_FLAG_RX_POSTINC | NRFX_TWIM_FLAG_REPEATED_XFER);
if (status != NRFX_SUCCESS)
{
LOG_ERR("TWIM XFER FAILED\n");
}
Buff_reset(ptr_curr);
XD = 2;
}
void pp_buffer_swap(twim_config_t *twim)
{
if (ptr_curr == BUFF1)
{
ptr_curr = BUFF2;
}
else
{
ptr_curr = BUFF1;
}
}Here comes the problem what should i do to reinitialize(if its needed) the twim instance with different buffer.
should i just do nrfx_twim_disable(my instance) and change nrfx_twim_xfer_desc_t to my second buffer then enable it, or should i do nrfx_twim_uninit. and do everything again.
PS: Dont mind my stupid bools checkers, ive tried to figure it out myself with easiest implementation in my head.