I have created a library for HD44780 Hitachi LCD, and I can transmit data to the LCD through an IO Expander without any problem over TWI lines. Right now I am not using any TWI Handler function for transmitting data.
I have functions within lcd_init()
and lcd_print("")
that call expanderWrite(uint8_t buffer)
to send data to LCD and they work fine.
So, my question is do I really need a TWI Handler function? If yes, can I make a function like this?
/* Indicates if sending data has ended. */
static volatile bool xfer_completed = false;
static const nrf_drv_twi_t m_twi_hd44780 = NRF_DRV_TWI_INSTANCE(0);
/**
* @brief TWI initialization.
*/
void twi_init (void)
{
ret_code_t err_code;
const nrf_drv_twi_config_t twi_hd44780_config = {
.scl = TWI_SCL_M,
.sda = TWI_SDA_M,
.frequency = NRF_TWI_FREQ_400K,
.interrupt_priority = APP_IRQ_PRIORITY_HIGH
};
err_code = nrf_drv_twi_init(&m_twi_hd44780, &twi_hd44780_config, twi_handler, NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&m_twi_hd44780);
}
static ret_code_t expanderWrite(uint8_t buffer)
{
return nrf_drv_twi_tx(&m_twi_hd44780, LCD_addr, &buffer, sizeof(buffer), false);
}
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
if ((p_event->type == NRF_DRV_TWI_EVT_DONE) &&
(p_event->xfer_desc.type == NRF_DRV_TWI_XFER_TX))
{
xfer_completed = true;
}
else
{
xfer_completed = false;
}
}
int main(void)
{
lcd_init();
while(xfer_completed == false);
xfer_completed = false;
lcd_print("Hello, world!");
while(xfer_completed == false);
xfer_completed = false;
}
By the way, regardless of setting the last parameter of function nrf_drv_twi_tx()
(bool xfer_pending) to "true" or "false", my code works fine. Is it better practice to keep it "true" or "false"?
If you need more pieces of code to be able to help me please let me know.
Thanks