-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhw_i2c.c
110 lines (95 loc) · 3.28 KB
/
hw_i2c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2014 Technical Machine, Inc. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#include <stddef.h>
#include <stdint.h>
#include "hw.h"
#include "lpc18xx_i2c.h"
#include "lpc18xx_cgu.h"
#include "lpc18xx_gpio.h"
#include "lpc18xx_scu.h"
#include "lpc18xx_libcfg.h"
#ifdef __cplusplus
extern "C" {
#endif
void hw_i2c_initialize (uint32_t port)
{
// Initialize Slave I2C peripheral
if (port == (uint32_t) LPC_I2C1){
scu_pinmux(2, 3, MD_PLN_FAST, FUNC1);
scu_pinmux(2, 4, MD_PLN_FAST, FUNC1);
}
I2C_Init((LPC_I2Cn_Type*) port, 25000);
}
void hw_i2c_enable (uint32_t port, uint8_t mode)
{
// mode: I2C_MASTER_MODE, I2C_SLAVE_MODE, I2C_GENERAL_MODE
// Enable Master/Slave I2C operation
I2C_Cmd((LPC_I2Cn_Type*) port, (I2C_Mode) mode, ENABLE);
}
void hw_i2c_disable (uint32_t port)
{
// disable Master/Slave I2C operation
// mode does not matter
I2C_Cmd((LPC_I2Cn_Type*) port, I2C_MASTER_MODE, DISABLE);
}
void hw_i2c_set_slave_addr (uint32_t port, uint8_t slave_addr)
{
// port: LPC_I2C1, LPC_I2C0
I2C_OWNSLAVEADDR_CFG_Type OwnSlavAdr;
OwnSlavAdr.GeneralCallState = DISABLE;
OwnSlavAdr.SlaveAddrChannel= 0;
OwnSlavAdr.SlaveAddrMaskValue = 0xFF;
OwnSlavAdr.SlaveAddr_7bit = slave_addr;
I2C_SetOwnSlaveAddr((LPC_I2Cn_Type*) port, &OwnSlavAdr);
}
int hw_i2c_slave_transfer (uint32_t port, const uint8_t *txbuf, size_t txbuf_len, uint8_t *rxbuf, size_t rxbuf_len)
{
I2C_S_SETUP_Type transferSCfg;
transferSCfg.tx_data = (uint8_t*) txbuf;
transferSCfg.tx_length = txbuf_len;
transferSCfg.rx_data = rxbuf;
transferSCfg.rx_length = rxbuf_len;
transferSCfg.tx_count = 0;
transferSCfg.rx_count = 0;
return I2C_SlaveTransferData((LPC_I2Cn_Type*) port, &transferSCfg, I2C_TRANSFER_POLLING);
}
int hw_i2c_slave_send (uint32_t port, const uint8_t *txbuf, size_t txbuf_len)
{
return hw_i2c_slave_transfer(port, txbuf, txbuf_len, NULL, 0);
}
int hw_i2c_slave_receive (uint32_t port, uint8_t *rxbuf, size_t rxbuf_len)
{
return hw_i2c_slave_transfer(port, NULL, 0, rxbuf, rxbuf_len);
}
int hw_i2c_master_transfer (uint32_t port, uint32_t addr, const uint8_t *txbuf, size_t txbuf_len, uint8_t *rxbuf, size_t rxbuf_len)
{
I2C_M_SETUP_Type transferMCfg;
transferMCfg.sl_addr7bit = addr;
transferMCfg.tx_data = (uint8_t*) txbuf;
transferMCfg.tx_length = txbuf_len;
transferMCfg.rx_data = rxbuf;
transferMCfg.rx_length = rxbuf_len;
transferMCfg.retransmissions_max = 3;
transferMCfg.tx_count = 0;
transferMCfg.rx_count = 0;
transferMCfg.retransmissions_count = 0;
int status = I2C_MasterTransferData((LPC_I2Cn_Type*) port, &transferMCfg, I2C_TRANSFER_POLLING);
return status == SUCCESS ? 0 : 1;
}
int hw_i2c_master_send (uint32_t port, uint32_t addr, const uint8_t *txbuf, size_t txbuf_len)
{
return hw_i2c_master_transfer(port, addr, txbuf, txbuf_len, NULL, 0);
}
int hw_i2c_master_receive (uint32_t port, uint32_t addr, uint8_t *rxbuf, size_t rxbuf_len)
{
return hw_i2c_master_transfer(port, addr, NULL, 0, rxbuf, rxbuf_len);
}
#ifdef __cplusplus
}
#endif