ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
fw_i2c.h File Reference
#include "fw_conf.h"
#include "fw_types.h"

Go to the source code of this file.

Macros

#define I2C_PRESCALER_COMPUTE(__FREQ__)   ((((__SYSCLOCK / __FREQ__) / 2U) - 4U) / 2U)
 
#define I2C_FREQUENCY_COMPUTE(__PRESCALER__)   ((__SYSCLOCK / 2U) / (((__PRESCALER__) * 2U) + 4U))
 
#define I2C_SetEnabled(__STATE__)   SFRX_ASSIGN(I2CCFG, 7, __STATE__)
 
#define I2C_SetWorkMode(__MODE__)   SFRX_ASSIGN(I2CCFG, 6, __MODE__)
 
#define I2C_SetClockPrescaler(__DIV__)
 
#define I2C_SendMasterCmd(__CMD__)
 
#define I2C_MasterStart()   I2C_SendMasterCmd(I2C_MasterCmd_Start)
 
#define I2C_MasterSendData(__DATA__)   do{I2CTXD = (__DATA__); I2C_SendMasterCmd(I2C_MasterCmd_Send);}while(0)
 
#define I2C_MasterRxAck()   I2C_SendMasterCmd(I2C_MasterCmd_RxAck)
 
#define I2C_MasterAck()   do{I2CMSST &= ~(0x01); I2C_SendMasterCmd(I2C_MasterCmd_TxAck);}while(0)
 
#define I2C_MasterNAck()   do{I2CMSST |= 0x01; I2C_SendMasterCmd(I2C_MasterCmd_TxAck);}while(0)
 
#define I2C_MasterStop()   I2C_SendMasterCmd(I2C_MasterCmd_Stop)
 
#define I2C_SetMasterAutoSend(__STATE__)   SFRX_ASSIGN(I2CMSAUX, 0, __STATE__)
 
#define I2C_IsMasterBusy()   (I2CMSST & 0x80)
 
#define I2C_ClearMasterCmdInterrupt()   do {SFRX_ON(); I2CMSST &= ~(0x01 << 6); SFRX_OFF();} while(0)
 
#define I2C_ResetSlaveMode()   SFRX_SET(I2CSLCR, 0)
 
#define I2C_IsSlaveBusy()   (I2CSLST & (0x01 << 7))
 
#define I2C_IsSlaveStartInterrupt()   (I2CSLST & (0x01 << 6))
 
#define I2C_ClearSlaveStartInterrupt()   (I2CSLST &= ~(0x01 << 6))
 
#define I2C_IsSlaveRecvInterrupt()   (I2CSLST & (0x01 << 5))
 
#define I2C_ClearSlaveRecvInterrupt()   (I2CSLST &= ~(0x01 << 5))
 
#define I2C_IsSlaveSendInterrupt()   (I2CSLST & (0x01 << 4))
 
#define I2C_ClearSlaveSendInterrupt()   (I2CSLST &= ~(0x01 << 4))
 
#define I2C_IsSlaveStopInterrupt()   (I2CSLST & (0x01 << 3))
 
#define I2C_ClearSlaveStopInterrupt()   (I2CSLST &= ~(0x01 << 3))
 
#define I2C_ReadSlaveAckIn()   (I2CSLST & (0x01 << 1))
 
#define I2C_ReadSlaveAckOut()   (I2CSLST & (0x01 << 0))
 
#define I2C_ClearAllSlaveInterrupts()   (I2CSLST = 0x00)
 
#define I2C_SetSlaveAddrControl(__STATE__)   SFRX_ASSIGN(I2CSLADR, 0, __STATE__)
 
#define I2C_SetPort(__ALTER_PORT__)   (P_SW2 = P_SW2 & ~(0x03 << 4) | ((__ALTER_PORT__) << 4))
 

Enumerations

enum  I2C_WorkMode_t { I2C_WorkMode_Slave = 0x00 , I2C_WorkMode_Master = 0x01 }
 
enum  I2C_MasterCmd_t {
  I2C_MasterCmd_Wait = 0x00 , I2C_MasterCmd_Start = 0x01 , I2C_MasterCmd_Send = 0x02 , I2C_MasterCmd_RxAck = 0x03 ,
  I2C_MasterCmd_Recv = 0x04 , I2C_MasterCmd_TxAck = 0x05 , I2C_MasterCmd_Stop = 0x06 , I2C_MasterCmd_StartSendRxAck = 0x09 ,
  I2C_MasterCmd_SendRxAck = 0x0A , I2C_MasterCmd_RecvTxAck0 = 0x0B , I2C_MasterCmd_RecvNAck = 0x0C
}
 
enum  I2C_AlterPort_t { I2C_AlterPort_P15_P14 = 0x00 , I2C_AlterPort_P25_P24 = 0x01 , I2C_AlterPort_P77_P76 = 0x02 , I2C_AlterPort_P32_P33 = 0x03 }
 

Functions

uint8_t I2C_Write (uint8_t devAddr, uint8_t memAddr, uint8_t *dat, uint16_t size)
 
uint8_t I2C_Read (uint8_t devAddr, uint8_t memAddr, uint8_t *buf, uint16_t size)
 
uint8_t I2C_Write16BitAddr (uint8_t devAddr, uint16_t memAddr, uint8_t *dat, uint16_t size)
 
uint8_t I2C_Read16BitAddr (uint8_t devAddr, uint16_t memAddr, uint8_t *buf, uint16_t size)
 

Macro Definition Documentation

◆ I2C_ClearAllSlaveInterrupts

#define I2C_ClearAllSlaveInterrupts ( )    (I2CSLST = 0x00)

Definition at line 115 of file fw_i2c.h.

◆ I2C_ClearMasterCmdInterrupt

#define I2C_ClearMasterCmdInterrupt ( )    do {SFRX_ON(); I2CMSST &= ~(0x01 << 6); SFRX_OFF();} while(0)

Definition at line 98 of file fw_i2c.h.

◆ I2C_ClearSlaveRecvInterrupt

#define I2C_ClearSlaveRecvInterrupt ( )    (I2CSLST &= ~(0x01 << 5))

Definition at line 108 of file fw_i2c.h.

◆ I2C_ClearSlaveSendInterrupt

#define I2C_ClearSlaveSendInterrupt ( )    (I2CSLST &= ~(0x01 << 4))

Definition at line 110 of file fw_i2c.h.

◆ I2C_ClearSlaveStartInterrupt

#define I2C_ClearSlaveStartInterrupt ( )    (I2CSLST &= ~(0x01 << 6))

Definition at line 106 of file fw_i2c.h.

◆ I2C_ClearSlaveStopInterrupt

#define I2C_ClearSlaveStopInterrupt ( )    (I2CSLST &= ~(0x01 << 3))

Definition at line 112 of file fw_i2c.h.

◆ I2C_FREQUENCY_COMPUTE

#define I2C_FREQUENCY_COMPUTE (   __PRESCALER__)    ((__SYSCLOCK / 2U) / (((__PRESCALER__) * 2U) + 4U))

Definition at line 62 of file fw_i2c.h.

◆ I2C_IsMasterBusy

#define I2C_IsMasterBusy ( )    (I2CMSST & 0x80)

Call P_SW2=0x80 before invoking this

Definition at line 97 of file fw_i2c.h.

◆ I2C_IsSlaveBusy

#define I2C_IsSlaveBusy ( )    (I2CSLST & (0x01 << 7))

Call SFRX_ON() before invoking these

Definition at line 104 of file fw_i2c.h.

◆ I2C_IsSlaveRecvInterrupt

#define I2C_IsSlaveRecvInterrupt ( )    (I2CSLST & (0x01 << 5))

Definition at line 107 of file fw_i2c.h.

◆ I2C_IsSlaveSendInterrupt

#define I2C_IsSlaveSendInterrupt ( )    (I2CSLST & (0x01 << 4))

Definition at line 109 of file fw_i2c.h.

◆ I2C_IsSlaveStartInterrupt

#define I2C_IsSlaveStartInterrupt ( )    (I2CSLST & (0x01 << 6))

Definition at line 105 of file fw_i2c.h.

◆ I2C_IsSlaveStopInterrupt

#define I2C_IsSlaveStopInterrupt ( )    (I2CSLST & (0x01 << 3))

Definition at line 111 of file fw_i2c.h.

◆ I2C_MasterAck

#define I2C_MasterAck ( )    do{I2CMSST &= ~(0x01); I2C_SendMasterCmd(I2C_MasterCmd_TxAck);}while(0)

Definition at line 86 of file fw_i2c.h.

◆ I2C_MasterNAck

#define I2C_MasterNAck ( )    do{I2CMSST |= 0x01; I2C_SendMasterCmd(I2C_MasterCmd_TxAck);}while(0)

Definition at line 87 of file fw_i2c.h.

◆ I2C_MasterRxAck

#define I2C_MasterRxAck ( )    I2C_SendMasterCmd(I2C_MasterCmd_RxAck)

Definition at line 85 of file fw_i2c.h.

◆ I2C_MasterSendData

#define I2C_MasterSendData (   __DATA__)    do{I2CTXD = (__DATA__); I2C_SendMasterCmd(I2C_MasterCmd_Send);}while(0)

Definition at line 84 of file fw_i2c.h.

◆ I2C_MasterStart

#define I2C_MasterStart ( )    I2C_SendMasterCmd(I2C_MasterCmd_Start)

Definition at line 83 of file fw_i2c.h.

◆ I2C_MasterStop

#define I2C_MasterStop ( )    I2C_SendMasterCmd(I2C_MasterCmd_Stop)

Definition at line 88 of file fw_i2c.h.

◆ I2C_PRESCALER_COMPUTE

#define I2C_PRESCALER_COMPUTE (   __FREQ__)    ((((__SYSCLOCK / __FREQ__) / 2U) - 4U) / 2U)

I2C bus clock = FOSC / 2 / (prescaler * 2 + 4) prescaler range is [0, 63]

Definition at line 60 of file fw_i2c.h.

◆ I2C_ReadSlaveAckIn

#define I2C_ReadSlaveAckIn ( )    (I2CSLST & (0x01 << 1))

Definition at line 113 of file fw_i2c.h.

◆ I2C_ReadSlaveAckOut

#define I2C_ReadSlaveAckOut ( )    (I2CSLST & (0x01 << 0))

Definition at line 114 of file fw_i2c.h.

◆ I2C_ResetSlaveMode

#define I2C_ResetSlaveMode ( )    SFRX_SET(I2CSLCR, 0)

Definition at line 100 of file fw_i2c.h.

◆ I2C_SendMasterCmd

#define I2C_SendMasterCmd (   __CMD__)
Value:
{ \
(I2CMSCR) = (I2CMSCR) & ~(0x0F) | ((__CMD__) & 0x0F); \
while (!(I2CMSST & 0x40)); \
I2CMSST &= ~0x40; \
}
#define I2CMSST
#define I2CMSCR

Definition at line 77 of file fw_i2c.h.

◆ I2C_SetClockPrescaler

#define I2C_SetClockPrescaler (   __DIV__)
Value:
do { \
SFRX_ON(); \
(I2CCFG) = (I2CCFG) & ~(0x3F) | ((__DIV__) & 0x3F); \
SFRX_OFF(); \
} while(0)
#define I2CCFG

I2C bus clock = FOSC / 2 / (DIV * 2 + 4) DIV range [0, 63]

Definition at line 71 of file fw_i2c.h.

◆ I2C_SetEnabled

#define I2C_SetEnabled (   __STATE__)    SFRX_ASSIGN(I2CCFG, 7, __STATE__)

Definition at line 64 of file fw_i2c.h.

◆ I2C_SetMasterAutoSend

#define I2C_SetMasterAutoSend (   __STATE__)    SFRX_ASSIGN(I2CMSAUX, 0, __STATE__)

If enabled, Send Data+RxAck will be executed automatically after write operation on I2CTXD

Definition at line 93 of file fw_i2c.h.

◆ I2C_SetPort

#define I2C_SetPort (   __ALTER_PORT__)    (P_SW2 = P_SW2 & ~(0x03 << 4) | ((__ALTER_PORT__) << 4))

Alternative ports

Definition at line 121 of file fw_i2c.h.

◆ I2C_SetSlaveAddrControl

#define I2C_SetSlaveAddrControl (   __STATE__)    SFRX_ASSIGN(I2CSLADR, 0, __STATE__)

Definition at line 117 of file fw_i2c.h.

◆ I2C_SetWorkMode

#define I2C_SetWorkMode (   __MODE__)    SFRX_ASSIGN(I2CCFG, 6, __MODE__)

Definition at line 65 of file fw_i2c.h.

Enumeration Type Documentation

◆ I2C_AlterPort_t

Enumerator
I2C_AlterPort_P15_P14 
I2C_AlterPort_P25_P24 
I2C_AlterPort_P77_P76 
I2C_AlterPort_P32_P33 

Definition at line 45 of file fw_i2c.h.

46{
47 // SCL SDA
I2C_AlterPort_t
Definition: fw_i2c.h:46
@ I2C_AlterPort_P32_P33
Definition: fw_i2c.h:51
@ I2C_AlterPort_P77_P76
Definition: fw_i2c.h:50
@ I2C_AlterPort_P25_P24
Definition: fw_i2c.h:49
@ I2C_AlterPort_P15_P14
Definition: fw_i2c.h:48

◆ I2C_MasterCmd_t

Enumerator
I2C_MasterCmd_Wait 
I2C_MasterCmd_Start 
I2C_MasterCmd_Send 
I2C_MasterCmd_RxAck 
I2C_MasterCmd_Recv 
I2C_MasterCmd_TxAck 
I2C_MasterCmd_Stop 
I2C_MasterCmd_StartSendRxAck 
I2C_MasterCmd_SendRxAck 
I2C_MasterCmd_RecvTxAck0 
I2C_MasterCmd_RecvNAck 

Definition at line 27 of file fw_i2c.h.

28{
29 I2C_MasterCmd_Wait = 0x00, // Wait, idle
30 I2C_MasterCmd_Start = 0x01, // START
31 I2C_MasterCmd_Send = 0x02, /* Send data. This command will generate 8 clocks on SCL, and
32 send I2CTXD to SDA bit by bit from MSB */
33 I2C_MasterCmd_RxAck = 0x03, /* Recive Ack. This command will generate 1 clock on SCL, and
34 save the received bit to MSACKI(I2CMSST.1) */
35 I2C_MasterCmd_Recv = 0x04, // Recive data
36 I2C_MasterCmd_TxAck = 0x05, /* Send Ack. This command will generate 1 clock on SCL, and
37 write the bit value of MSACKO(I2CMSST.0) to SDA */
38 I2C_MasterCmd_Stop = 0x06, // STOP. This command will send STOP signal, and reset MSBUSY flag
39 I2C_MasterCmd_StartSendRxAck = 0x09, // START + Send data + RxAck
40 I2C_MasterCmd_SendRxAck = 0x0A, // Send data + RxAck
41 I2C_MasterCmd_RecvTxAck0 = 0x0B, // Receive data + TxAck(0)
42 I2C_MasterCmd_RecvNAck = 0x0C, // Receive data + NAck
I2C_MasterCmd_t
Definition: fw_i2c.h:28
@ I2C_MasterCmd_Wait
Definition: fw_i2c.h:29
@ I2C_MasterCmd_Recv
Definition: fw_i2c.h:35
@ I2C_MasterCmd_RecvNAck
Definition: fw_i2c.h:42
@ I2C_MasterCmd_RecvTxAck0
Definition: fw_i2c.h:41
@ I2C_MasterCmd_Stop
Definition: fw_i2c.h:38
@ I2C_MasterCmd_StartSendRxAck
Definition: fw_i2c.h:39
@ I2C_MasterCmd_Send
Definition: fw_i2c.h:31
@ I2C_MasterCmd_SendRxAck
Definition: fw_i2c.h:40
@ I2C_MasterCmd_RxAck
Definition: fw_i2c.h:33
@ I2C_MasterCmd_TxAck
Definition: fw_i2c.h:36
@ I2C_MasterCmd_Start
Definition: fw_i2c.h:30

◆ I2C_WorkMode_t

Enumerator
I2C_WorkMode_Slave 
I2C_WorkMode_Master 

Definition at line 21 of file fw_i2c.h.

22{
23 I2C_WorkMode_Slave = 0x00,
I2C_WorkMode_t
Definition: fw_i2c.h:22
@ I2C_WorkMode_Slave
Definition: fw_i2c.h:23
@ I2C_WorkMode_Master
Definition: fw_i2c.h:24

Function Documentation

◆ I2C_Read()

uint8_t I2C_Read ( uint8_t  devAddr,
uint8_t  memAddr,
uint8_t buf,
uint16_t  size 
)

Definition at line 36 of file fw_i2c.c.

37{
38 SFRX_ON();
40 I2C_MasterSendData(devAddr & 0xFE);
42 I2C_MasterSendData(memAddr);
45 I2C_MasterSendData(devAddr | 0x01);
47 while(size--)
48 {
50 *buf++ = I2CRXD;
51 if (size == 0)
52 {
54 }
55 else
56 {
58 }
59 }
61 SFRX_OFF();
62 return HAL_OK;
63}
#define I2C_MasterSendData(__DATA__)
Definition: fw_i2c.h:84
#define I2C_MasterRxAck()
Definition: fw_i2c.h:85
#define I2C_SendMasterCmd(__CMD__)
Definition: fw_i2c.h:77
#define I2C_MasterStart()
Definition: fw_i2c.h:83
#define I2C_MasterAck()
Definition: fw_i2c.h:86
#define I2C_MasterNAck()
Definition: fw_i2c.h:87
#define I2C_MasterStop()
Definition: fw_i2c.h:88
#define I2CRXD
#define SFRX_ON()
Definition: fw_types.h:112
@ HAL_OK
Definition: fw_types.h:76
#define SFRX_OFF()
Definition: fw_types.h:113

◆ I2C_Read16BitAddr()

uint8_t I2C_Read16BitAddr ( uint8_t  devAddr,
uint16_t  memAddr,
uint8_t buf,
uint16_t  size 
)

Definition at line 85 of file fw_i2c.c.

86{
87 SFRX_ON();
89 I2C_MasterSendData(devAddr & 0xFE);
91 I2C_MasterSendData(memAddr >> 8);
93 I2C_MasterSendData(memAddr & 0xFF);
96 I2C_MasterSendData(devAddr | 0x01);
98 while(size--)
99 {
101 *buf++ = I2CRXD;
102 if (size == 0)
103 {
105 }
106 else
107 {
109 }
110 }
112 SFRX_OFF();
113 return HAL_OK;
114}

◆ I2C_Write()

uint8_t I2C_Write ( uint8_t  devAddr,
uint8_t  memAddr,
uint8_t dat,
uint16_t  size 
)

Definition at line 18 of file fw_i2c.c.

19{
20 SFRX_ON();
22 I2C_MasterSendData(devAddr & 0xFE);
24 I2C_MasterSendData(memAddr);
26 while(size--)
27 {
30 }
32 SFRX_OFF();
33 return HAL_OK;
34}
__CODE int8_t dat[20]

◆ I2C_Write16BitAddr()

uint8_t I2C_Write16BitAddr ( uint8_t  devAddr,
uint16_t  memAddr,
uint8_t dat,
uint16_t  size 
)

Definition at line 65 of file fw_i2c.c.

66{
67 SFRX_ON();
69 I2C_MasterSendData(devAddr & 0xFE);
71 I2C_MasterSendData(memAddr >> 8);
73 I2C_MasterSendData(memAddr & 0xFF);
75 while(size--)
76 {
79 }
81 SFRX_OFF();
82 return HAL_OK;
83}