Interface for the 2-Wire module. More...
#include <stdint.h>#include <stdbool.h>#include "nrf24le1.h"Go to the source code of this file.
Interface for the 2-Wire module.
Definition in file hal_w2.h.
| void hal_w2_set_clk_freq | ( | hal_w2_clk_freq_t | freq ) |
Function to set the clock frequency. Use this function select clock frequency of the 2-wire.
| freq | The clock frequency of the 2-wire |
Definition at line 94 of file hal_w2.c.
{
W2CON0 = (W2CON0 & 0xF3) | (((uint8_t)freq) << CLOCK_FREQUENCY_0);
} // Update "clockFrequency" bits
| void hal_w2_set_op_mode | ( | hal_w2_op_mode_t | mode ) |
Function to set the operation mode of the 2-wire. Use this function select master or slave mode.
| mode | The operation mode of the 2-wire |
Definition at line 99 of file hal_w2.c.
{
if(mode == HAL_W2_MASTER) // Check for master mode
{
W2CON0 = W2CON0 | (1 << MASTER_SELECT); // Set "masterSelect" bit
}
else
{
W2CON0 = W2CON0 & ~(1 << MASTER_SELECT); // Clear "masterSelect" bit
}
}
| void hal_w2_enable | ( | _Bool | en ) |
Function to enable the 2-wire. Use this function enable the 2-wire, must be done before any other programming of the 2-wire.
| en | True to enable, false to disable |
Definition at line 111 of file hal_w2.c.
{
if(en)
{
W2CON0 = W2CON0 | (1 << WIRE_2_ENABLE); // Set "wire2Enable" bit
}
else
{
W2CON0 = W2CON0 & ~(1 << WIRE_2_ENABLE); // Clear "wire2Enable" bit
}
}
| void hal_w2_all_irq_enable | ( | _Bool | irq ) |
Function to enable all interrupts. Use this function enable all interrupts.
| irq | True to enable, false to disable all interrupts in the 2-wire |
< The value of bit 5
< The value of bit 5
| void hal_w2_configure_master | ( | hal_w2_clk_freq_t | mode ) |
Definition at line 137 of file hal_w2.c.
{
hal_w2_enable(true);
hal_w2_set_clk_freq(mode);
hal_w2_set_op_mode(HAL_W2_MASTER);
INTEXP |= 0x04; // Enable 2 wire interrupts
W2CON1 = 0x00;
hal_w2_all_irq_enable(true); // Enable interrupts in the 2-wire
SPIF = 0;
}
| _Bool hal_w2_init_transfer | ( | uint8_t | address, |
| hal_w2_direction_t | direction | ||
| ) |
Definition at line 168 of file hal_w2.c.
{
uint8_t w2_status;
HAL_W2_ISSUE_START_COND;
HAL_W2_WRITE((address << 1) | (uint8_t)direction);
w2_status = hal_w2_wait_data_ready();
if (w2_status & W2CON1_FLAG_NACK)
{
return false; // NACK received from slave or timeout
}
else
{
return true; // ACK received from slave
}
}
| _Bool hal_w2_write | ( | uint8_t | address, |
| const uint8_t * | data_ptr, | ||
| uint8_t | data_len | ||
| ) |
Definition at line 187 of file hal_w2.c.
{
bool ack_received;
ack_received = hal_w2_init_transfer(address, HAL_W2_DIR_WRITE);
while (data_len-- > 0 && ack_received == true)
{
uint8_t w2_status;
HAL_W2_WRITE(*data_ptr++);
w2_status = hal_w2_wait_data_ready();
if (w2_status & W2CON1_FLAG_NACK)
{
ack_received = false;
}
}
HAL_W2_ISSUE_STOP_COND;
return ack_received;
}
| _Bool hal_w2_read | ( | uint8_t | address, |
| uint8_t * | buffer, | ||
| uint8_t | buffer_size | ||
| ) |
Definition at line 208 of file hal_w2.c.
{
uint8_t w2_status;
bool ack_received;
ack_received = hal_w2_init_transfer(address, HAL_W2_DIR_READ);
if (ack_received == false)
{
// This situation (NACK received on bus while trying to read from a slave) leads to a deadlock in the 2-wire interface.
hal_w2_soft_reset(); // Workaround for the deadlock
}
while (data_len-- && ack_received)
{
if (data_len == 0)
{
HAL_W2_ISSUE_STOP_COND;
}
w2_status = hal_w2_wait_data_ready();
*data_ptr++ = HAL_W2_READ();
ack_received = !(w2_status & W2CON1_FLAG_NACK);
}
return ack_received;
}
1.7.2