| 1 |
/* |
| 2 |
* The MIT License (MIT) |
| 3 |
* |
| 4 |
* Copyright (c) 2021 Renesas Electronics Corporation |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
* THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifndef RA_RA_I2C_H_ |
| 26 |
#define RA_RA_I2C_H_ |
| 27 |
|
| 28 |
#include <stdint.h> |
| 29 |
#include <stdbool.h> |
| 30 |
|
| 31 |
#ifdef RA6M5 |
| 32 |
// #define R_IIC2_BASE 0x4009F200 |
| 33 |
#ifndef R_IIC2 |
| 34 |
#define R_IIC2 ((R_IIC0_Type *)0x4009F200) |
| 35 |
#endif |
| 36 |
#endif |
| 37 |
|
| 38 |
#define RA_I2C_DEF_TIMEOUT 1000 // 1000 ms |
| 39 |
#define RA_I2C_TIMEOUT_STOP_CONDITION 100000 // counts |
| 40 |
#define RA_I2C_TIMEOUT_BUS_BUSY 100000 // counts |
| 41 |
#define RA_I2C_CLOCK_MAX 1000000 // counts |
| 42 |
|
| 43 |
typedef enum |
| 44 |
{ |
| 45 |
RA_I2C_STATUS_Idle = 1, |
| 46 |
RA_I2C_STATUS_Started = 2, |
| 47 |
RA_I2C_STATUS_AddrWriteCompleted = 3, |
| 48 |
RA_I2C_STATUS_DataWriteCompleted = 4, |
| 49 |
RA_I2C_STATUS_DataSendCompleted = 5, |
| 50 |
RA_I2C_STATUS_FirstReceiveCompleted = 6, |
| 51 |
RA_I2C_STATUS_LastReceiveCompleted = 7, |
| 52 |
RA_I2C_STATUS_Stopped = 8, |
| 53 |
} xaction_status_t; |
| 54 |
|
| 55 |
typedef enum |
| 56 |
{ |
| 57 |
RA_I2C_ERROR_OK = 0, |
| 58 |
RA_I2C_ERROR_TMOF = 1, |
| 59 |
RA_I2C_ERROR_AL = 2, |
| 60 |
RA_I2C_ERROR_NACK = 3, |
| 61 |
RA_I2C_ERROR_BUSY = 4, |
| 62 |
} xaction_error_t; |
| 63 |
|
| 64 |
typedef struct { |
| 65 |
volatile uint32_t m_bytes_transferred; |
| 66 |
volatile uint32_t m_bytes_transfer; |
| 67 |
uint32_t m_bytes_total; |
| 68 |
bool m_fread; |
| 69 |
uint8_t *buf; |
| 70 |
void *next; |
| 71 |
} xaction_unit_t; |
| 72 |
|
| 73 |
typedef struct { |
| 74 |
xaction_unit_t *units; |
| 75 |
uint32_t m_num_of_units; |
| 76 |
uint32_t m_current; |
| 77 |
uint32_t m_address; |
| 78 |
volatile xaction_status_t m_status; |
| 79 |
volatile xaction_error_t m_error; |
| 80 |
bool m_stop; |
| 81 |
} xaction_t; |
| 82 |
|
| 83 |
bool ra_i2c_find_af_ch(uint32_t scl, uint32_t sda, uint8_t *ch); |
| 84 |
void ra_i2c_irq_enable(R_IIC0_Type *i2c_inst); |
| 85 |
void ra_i2c_irq_disable(R_IIC0_Type *i2c_inst); |
| 86 |
void ra_i2c_priority(R_IIC0_Type *i2c_inst, uint32_t ipl); |
| 87 |
void ra_i2c_set_baudrate(R_IIC0_Type *i2c_inst, uint32_t baudrate); |
| 88 |
void ra_i2c_clear_IR(R_IIC0_Type *i2c_inst); |
| 89 |
void ra_i2c_init(R_IIC0_Type *i2c_inst, uint32_t scl, uint32_t sda, uint32_t baudrate); |
| 90 |
void ra_i2c_deinit(R_IIC0_Type *i2c_inst); |
| 91 |
void ra_i2c_read_last_byte(R_IIC0_Type *i2c_inst); |
| 92 |
void ra_i2c_stop_condition(R_IIC0_Type *i2c_inst); |
| 93 |
void ra_i2c_abort(R_IIC0_Type *i2c_inst); |
| 94 |
void ra_i2c_xaction_start(R_IIC0_Type *i2c_inst, xaction_t *xaction, bool repeated_start); |
| 95 |
void ra_i2c_xaction_stop(void); |
| 96 |
void ra_i2c_get_clock(uint32_t rateKhz, uint8_t *clockRate, uint8_t *clockRate2); |
| 97 |
void ra_i2c_xunit_write_byte(R_IIC0_Type *i2c_inst, xaction_unit_t *unit); |
| 98 |
void ra_i2c_xunit_read_byte(R_IIC0_Type *i2c_inst, xaction_unit_t *unit); |
| 99 |
void ra_i2c_xunit_init(xaction_unit_t *unit, uint8_t *buf, uint32_t size, bool fread, void *next); |
| 100 |
void ra_i2c_xaction_init(xaction_t *action, xaction_unit_t *units, uint32_t size, uint32_t address, bool stop); |
| 101 |
void iic_master_rxi_isr(void); |
| 102 |
void iic_master_txi_isr(void); |
| 103 |
void iic_master_tei_isr(void); |
| 104 |
void iic_master_eri_isr(void); |
| 105 |
bool ra_i2c_action_execute(R_IIC0_Type *i2c_inst, xaction_t *action, bool repeated_start, uint32_t timeout_ms); |
| 106 |
|
| 107 |
#endif /* RA_RA_I2C_H_ */ |