Defines | Enumerations | Functions

hal/nrf24le1/hal_w2.h File Reference

Interface for the 2-Wire module. More...

#include <stdint.h>
#include <stdbool.h>
#include "nrf24le1.h"

Go to the source code of this file.

Defines

#define W2CON0_FLAG_X_STOP   0x20
#define W2CON0_FLAG_X_START   0x10
#define W2CON1_FLAG_NACK   0x02
#define W2CON1_FLAG_DATA_READY   0x01
#define HAL_W2_ISSUE_START_COND   (W2CON0 |= W2CON0_FLAG_X_START)
#define HAL_W2_ISSUE_STOP_COND   (W2CON0 |= W2CON0_FLAG_X_STOP)
#define HAL_W2_WAIT_FOR_INTERRUPT   {while(!SPIF); SPIF = 0; }
#define HAL_W2_WRITE(a)   W2DAT = (a)
#define HAL_W2_READ()   W2DAT
#define W2_SOFT_RESET_NOT_AVAILABLE

Enumerations

enum  hal_w2_clk_freq_t { HAL_W2_IDLE = 0x00, HAL_W2_100KHZ = 0x01, HAL_W2_400KHZ = 0x02 }
enum  hal_w2_op_mode_t { HAL_W2_MASTER, HAL_W2_SLAVE }
enum  hal_w2_irq_source_t { HAL_W2_STOP_COND = 0x08, HAL_W2_ADRESS_MATCH = 0x04, HAL_W2_DATA_READY = 0x01 }
enum  hal_w2_direction_t { HAL_W2_DIR_WRITE, HAL_W2_DIR_READ }

Functions

void hal_w2_set_clk_freq (hal_w2_clk_freq_t freq)
void hal_w2_set_op_mode (hal_w2_op_mode_t mode)
void hal_w2_enable (_Bool en)
void hal_w2_all_irq_enable (_Bool irq)
void hal_w2_configure_master (hal_w2_clk_freq_t mode)
_Bool hal_w2_init_transfer (uint8_t address, hal_w2_direction_t direction)
_Bool hal_w2_write (uint8_t address, const uint8_t *data_ptr, uint8_t data_len)
_Bool hal_w2_read (uint8_t address, uint8_t *buffer, uint8_t buffer_size)
SLAVE SPECIFIC FUNCTIONS
void hal_w2_respond_to_gen_adr (_Bool resp_gen)
void hal_w2_alter_clock (_Bool alt_clk)
void hal_w2_irq_stop_cond_enable (_Bool stop_cond)
void hal_w2_irq_adr_match_enable (_Bool addr_match)
void hal_w2_set_slave_address (uint8_t address)

Detailed Description

Interface for the 2-Wire module.

Definition in file hal_w2.h.


Function Documentation

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.

Parameters:
freqThe 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.

Parameters:
modeThe 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.

Parameters:
enTrue 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.

Parameters:
irqTrue to enable, false to disable all interrupts in the 2-wire

< The value of bit 5

< The value of bit 5

Definition at line 123 of file hal_w2.c.

{ /* In this function the standard "read-modify-write" is not used because
     bit 4:0 (the status bits) in W2CON1 are cleared when read. These bits
     are read only so they can not be modified. */
  if(irq)
  {
    W2CON1 = ~(BIT_5);                        // Clear "maskIrq" bit
  }
  else
  {
    W2CON1 = BIT_5;                           // Set "maskIrq" bit
  }
}
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;  
}