CuVoodoo STM32F1 firmware template
Macros | Enumerations | Functions | Variables
onewire_master.c File Reference

library for 1-wire protocol as master (code) More...

Go to the source code of this file.

Macros

#define ONEWIRE_MASTER_TIMER   2
 timer ID
 
#define ONEWIRE_MASTER_PORT   A
 GPIO port.
 
#define ONEWIRE_MASTER_PIN   4
 GPIO pin.
 

Enumerations

enum  {
  ONEWIRE_STATE_IDLE, ONEWIRE_STATE_DONE, ONEWIRE_STATE_ERROR, ONEWIRE_STATE_MASTER_RESET,
  ONEWIRE_STATE_SLAVE_PRESENCE, ONEWIRE_STATE_MASTER_WRITE, ONEWIRE_STATE_MASTER_READ, ONEWIRE_MAX
}
 state of 1-Wire communication More...
 

Functions

void onewire_master_setup (bool parasite, uint16_t recovery)
 setup 1-wire peripheral More...
 
bool onewire_master_reset (void)
 send reset pulse More...
 
static bool onewire_master_write (void)
 write bits on 1-Wire bus More...
 
static bool onewire_master_read (void)
 read bits on 1-Wire bus More...
 
uint8_t onewire_master_crc (uint8_t *data, uint32_t length)
 compute CRC for 1-Wire More...
 
bool onewire_master_function_read (uint8_t function, uint8_t *data, uint32_t bits)
 issue function and read data More...
 
bool onewire_master_function_write (uint8_t function, uint8_t *data, uint32_t bits)
 issue function and write data More...
 
uint64_t onewire_master_rom_read (void)
 send READ ROM command and read ROM code response More...
 
bool onewire_master_rom_search (uint64_t *code, bool alarm)
 send SEARCH ROM command More...
 
bool onewire_master_rom_skip (void)
 send SKIP ROM command (all slaves on the bus will be selected) More...
 
bool onewire_master_rom_match (uint64_t code)
 send MATCH ROM command to select a specific slave More...
 
void TIM_ISR() ONEWIRE_MASTER_TIMER (void)
 interrupt service routine called for timer
 

Variables

enum { ... }  onewire_master_state = ONEWIRE_STATE_IDLE
 state of 1-Wire communication
 
static volatile bool slave_presence = false
 if slaves have been detected
 
static uint8_t * buffer = NULL
 input/output buffer for read/write commands/functions
 
static uint32_t buffer_size = 0
 size of buffer in bits
 
static volatile uint32_t buffer_bit = 0
 number of bits read/written
 
static bool onewire_master_parasite = false
 if parasite power should be provided whenever the is no communication
 
static uint16_t onewire_master_recovery = 0
 the recovery time in us (1 < Trec)
 

Detailed Description

library for 1-wire protocol as master (code)

Author
King Kévin kingk.nosp@m.evin.nosp@m.@cuvo.nosp@m.odoo.nosp@m..info
Date
2017
Note
peripherals used: timer timer used to measure 1-wire signal timing, GPIO GPIO used for 1-wire signal
overdrive mode is not provided protocol description from Book of iButton Standards

Definition in file onewire_master.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

state of 1-Wire communication

Enumerator
ONEWIRE_STATE_IDLE 

no current communication

ONEWIRE_STATE_DONE 

communication complete

ONEWIRE_STATE_ERROR 

communication error

ONEWIRE_STATE_MASTER_RESET 

reset pulse started

ONEWIRE_STATE_SLAVE_PRESENCE 

waiting for slave response to reset pulse

ONEWIRE_STATE_MASTER_WRITE 

master is writing bits

ONEWIRE_STATE_MASTER_READ 

master is reading bits

ONEWIRE_MAX 

to count the number of possible states

Definition at line 55 of file onewire_master.c.

Function Documentation

◆ onewire_master_crc()

uint8_t onewire_master_crc ( uint8_t *  data,
uint32_t  length 
)

compute CRC for 1-Wire

Note
this CRC-8 uses normal polynomial 0x31, reverse polynomial 0x8C, start value 0x00
Parameters
[in]databytes on which to calculate CRC checksum on
[in]lengthnumber of bytes in data
Returns
computed CRC checksum

Definition at line 219 of file onewire_master.c.

◆ onewire_master_function_read()

bool onewire_master_function_read ( uint8_t  function,
uint8_t *  data,
uint32_t  bits 
)

issue function and read data

Note
user needs to send a ROM command before
Parameters
[in]functionfunction command to send
[out]databuffer to save read bits (NULL if only the function command should be sent)
[in]bitsnumber of bits to read (0 if only the function command should be sent)
Returns
if operation succeeded

Definition at line 239 of file onewire_master.c.

◆ onewire_master_function_write()

bool onewire_master_function_write ( uint8_t  function,
uint8_t *  data,
uint32_t  bits 
)

issue function and write data

Note
user needs to send a ROM command before
Parameters
[in]functionfunction command to send
[out]datadata to write (NULL if only the function command should be sent)
[in]bitsnumber of bits to write (0 if only the function command should be sent)
Returns
if operation succeeded

Definition at line 262 of file onewire_master.c.

◆ onewire_master_read()

static bool onewire_master_read ( void  )
static

read bits on 1-Wire bus

Warning
buffer_size must be set to the number of bits to read
Returns
if read succeeded

Definition at line 181 of file onewire_master.c.

◆ onewire_master_reset()

bool onewire_master_reset ( void  )

send reset pulse

Returns
if slaves have indicated their presence

Definition at line 116 of file onewire_master.c.

◆ onewire_master_rom_match()

bool onewire_master_rom_match ( uint64_t  code)

send MATCH ROM command to select a specific slave

Note
user needs to send reset pulse before
Parameters
[in]codeROM code of slave to select
Returns
if operation succeeded

Definition at line 380 of file onewire_master.c.

◆ onewire_master_rom_read()

uint64_t onewire_master_rom_read ( void  )

send READ ROM command and read ROM code response

Note
user needs to send reset pulse before
Returns
ROM code read

Definition at line 286 of file onewire_master.c.

◆ onewire_master_rom_search()

bool onewire_master_rom_search ( uint64_t *  code,
bool  alarm 
)

send SEARCH ROM command

Note
user needs to send reset pulse before
Warning
undefined behaviour if a ROM code different than the last found is provided
Parameters
[in,out]codeuse 0 to start search ROM code from scratch, or last know value to search next; writes back next ROM code found, or 0 if error occurred
[in]alarmsearch only for ROM codes for slaves with an alarm flag set
Returns
if an additional slave has been detected

Definition at line 305 of file onewire_master.c.

◆ onewire_master_rom_skip()

bool onewire_master_rom_skip ( void  )

send SKIP ROM command (all slaves on the bus will be selected)

Note
user needs to send reset pulse before
Returns
if operation succeeded

Definition at line 372 of file onewire_master.c.

◆ onewire_master_setup()

void onewire_master_setup ( bool  parasite,
uint16_t  recovery 
)

setup 1-wire peripheral

Parameters
[in]parasiteenable parasite power (provide power over 1-Wire line when not communicating)
Warning
multiple masters and interrupts are prevented when parasite power is used
Parameters
[in]recoveryrecovery time in us between timeslot, e.g. to ensure enough parasite power is provided (0 if not required)

Definition at line 73 of file onewire_master.c.

◆ onewire_master_write()

static bool onewire_master_write ( void  )
static

write bits on 1-Wire bus

Warning
buffer_size must be set to the number of bits to writen and buffer must contain the data to write
Returns
if write succeeded

Definition at line 144 of file onewire_master.c.