#define ADDR_CMD_BIT_READ_SELECT    6       // Set that bit number in the address/command byte for read operation.
#define ADDR_CMD_BIT_CONT_MODE      5       // Set that bit number in the address/command byte for continuous operation.

#define DATA_NO_TRANSITION          0xFF    // No bit transition during that byte sending.


static void trf7960a_write_register(const uint8_t reg_addr, const uint8_t reg_data)
{
    SPI_SS_PIN_LVL  = PIN_LVL_LOW;

    spi1_read_write(reg_addr);  // Send the address/command byte. The data returned is not important.
    spi1_read_write(reg_data);  // Send the register value.

    SPI_SS_PIN_LVL = PIN_LVL_HIGH;
}

static void trf7960a_read_continuous(const uint8_t reg_start_addr, const uint8_t reg_num, uint8_t *data)
{
    uint8_t addr_cmd_byte;
    uint8_t read_index;

    addr_cmd_byte   = reg_start_addr | (1<<ADDR_CMD_BIT_READ_SELECT) | (1<<ADDR_CMD_BIT_CONT_MODE);
    SPI_SS_PIN_LVL  = PIN_LVL_LOW;

    spi1_read_write(addr_cmd_byte); // Send the address/command byte. The data returned is not important.
    spi1_set_mode0();               // Clock polarity change required until read the data.

    for (read_index = 0; read_index < reg_num; read_index++)
        data[read_index] = spi1_read_write(DATA_NO_TRANSITION);

    SPI_SS_PIN_LVL = PIN_LVL_HIGH;

    spi1_set_mode1();               // After the read operation, restore the origin SPI mode.
}
