Plant Monitor SAM3X8E
Loading...
Searching...
No Matches
twi.c File Reference

HAL interface for TWI peripheral of SAM3X8E. More...

#include "hal/twi.h"
#include "sam3xa.h"

Go to the source code of this file.

Functions

void twi_configure (const twi_cfg_t *cfg)
 Configures TWI0 peripheral of SAM3X8E.
void twi_init ()
 Initializes TWI0 peripheral of SAM3X8E.
void twi_read (uint8_t *buffer, size_t len)
 Reads from TWI target device.
void twi_write (const uint8_t *buffer, size_t len)
 Writes to TWI target device.

Detailed Description

HAL interface for TWI peripheral of SAM3X8E.

Function implementations for TWI HAL of SAM3X8E.

Author
Vladimir Zhelev

Definition in file twi.c.

Function Documentation

◆ twi_configure()

void twi_configure ( const twi_cfg_t * cfg)

Configures TWI0 peripheral of SAM3X8E.

Sets target device address and internal address configuration.

Parameters
cfgPointer to TWI configuration parameters.

Definition at line 13 of file twi.c.

14{
15 /* Disable slave mode & enable master mode */
16 TWI0->TWI_CR =
17 TWI_CR_SVDIS |
18 TWI_CR_MSEN;
19
20 /* Configure internal address register if internal address of device exists */
21 switch (cfg->iadr_size)
22 {
23 case TWI_ADDR_0BYTE:
24 TWI0->TWI_MMR = TWI_MMR_IADRSZ_NONE;
25 TWI0->TWI_IADR = 0;
26 break;
27 case TWI_ADDR_1BYTE:
28 TWI0->TWI_MMR = TWI_MMR_IADRSZ_1_BYTE;
29 TWI0->TWI_IADR = TWI_IADR_IADR(cfg->iadr & 0x0000FF);
30 break;
31 case TWI_ADDR_2BYTE:
32 TWI0->TWI_MMR = TWI_MMR_IADRSZ_2_BYTE;
33 TWI0->TWI_IADR = TWI_IADR_IADR(cfg->iadr & 0x00FFFF);
34 break;
35 case TWI_ADDR_3BYTE:
36 TWI0->TWI_MMR = TWI_MMR_IADRSZ_3_BYTE;
37 TWI0->TWI_IADR = TWI_IADR_IADR(cfg->iadr);
38 break;
39 }
40
41 TWI0->TWI_MMR |= TWI_MMR_DADR(cfg->dadr);
42}
twi_iadr_size_t iadr_size
Definition twi.h:40
uint32_t iadr
Definition twi.h:39
uint32_t dadr
Definition twi.h:38
@ TWI_ADDR_1BYTE
Definition twi.h:25
@ TWI_ADDR_0BYTE
Definition twi.h:24
@ TWI_ADDR_2BYTE
Definition twi.h:26
@ TWI_ADDR_3BYTE
Definition twi.h:27

◆ twi_init()

void twi_init ( )

Initializes TWI0 peripheral of SAM3X8E.

Enables peripheral clock & configures TWI0 pins.

Definition at line 44 of file twi.c.

45{
46 /* Enable clocks */
47 PMC->PMC_PCER0 |= (1u << ID_PIOA);
48 PMC->PMC_PCER0 |= (1u << ID_TWI0);
49
50 /* Select Peripheral A functions for TWI0 pins
51 * & give control of TWI0 pins to TWI0 peripheral */
52 PIOA->PIO_ABSR &= ~TWI0_PINS;
53 PIOA->PIO_PDR = TWI0_PINS;
54
55 /* Configure clock */
56 TWI0->TWI_CWGR =
57 TWI_CWGR_CKDIV(TWI_CKDIV) |
58 TWI_CWGR_CHDIV(TWI_CHDIV) |
59 TWI_CWGR_CLDIV(TWI_CLDIV);
60}
#define TWI0_PINS
Definition twi.h:52
#define TWI_CLDIV
Definition twi.h:57
#define TWI_CHDIV
Definition twi.h:56
#define TWI_CKDIV
Definition twi.h:55

◆ twi_read()

void twi_read ( uint8_t * buffer,
size_t len )

Reads from TWI target device.

Parameters
bufferBuffer to store reading.
lenNumber of bytes to read into buffer.

Definition at line 62 of file twi.c.

63{
64 /* Set Master Read */
65 TWI0->TWI_MMR |= TWI_MMR_MREAD;
66
67 /* Send START condition */
68 TWI0->TWI_CR = TWI_CR_START;
69
70 for (size_t i = 0; i < len; i++)
71 {
72 if (i == (len - 1))
73 TWI0->TWI_CR = TWI_CR_STOP;
74
75 while (!(TWI0->TWI_SR & TWI_SR_RXRDY));
76 buffer[i] = TWI0->TWI_RHR;
77 }
78
79 while(!(TWI0->TWI_SR & TWI_SR_TXCOMP));
80}

◆ twi_write()

void twi_write ( const uint8_t * buffer,
size_t len )

Writes to TWI target device.

Parameters
bufferBuffer to write to target device.
lenNumber of bytes to write.

Definition at line 82 of file twi.c.

83{
84 /* Set Master Write */
85 TWI0->TWI_MMR &= ~TWI_MMR_MREAD;
86
87 /* Send START condition */
88 TWI0->TWI_CR = TWI_CR_START;
89
90 /* Transmit message */
91 for (size_t i = 0; i < len; i++)
92 {
93 while (!(TWI0->TWI_SR & TWI_SR_TXRDY));
94 TWI0->TWI_THR = TWI_THR_TXDATA(buffer[i]);
95 }
96
97 TWI0->TWI_CR = TWI_CR_STOP;
98 while(!(TWI0->TWI_SR & TWI_SR_TXCOMP));
99}