USB Serial application
Fork of USBSerial_HelloWorld by
mbed-dev/targets/TARGET_ONSEMI/TARGET_NCS36510/i2c.h@11:b3f2a8bdac4d, 2017-12-16 (annotated)
- Committer:
- Zaitsev
- Date:
- Sat Dec 16 10:26:48 2017 +0000
- Revision:
- 11:b3f2a8bdac4d
- Parent:
- 10:41552d038a69
A copy for D.S;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Zaitsev | 10:41552d038a69 | 1 | /** |
Zaitsev | 10:41552d038a69 | 2 | ****************************************************************************** |
Zaitsev | 10:41552d038a69 | 3 | * @file i2c.h |
Zaitsev | 10:41552d038a69 | 4 | * @brief (API) Public header of i2c driver |
Zaitsev | 10:41552d038a69 | 5 | * @internal |
Zaitsev | 10:41552d038a69 | 6 | * @author ON Semiconductor |
Zaitsev | 10:41552d038a69 | 7 | * $Rev: $ |
Zaitsev | 10:41552d038a69 | 8 | * $Date: 2016-04-20 $ |
Zaitsev | 10:41552d038a69 | 9 | ****************************************************************************** |
Zaitsev | 10:41552d038a69 | 10 | * Copyright 2016 Semiconductor Components Industries LLC (d/b/a ON Semiconductor). |
Zaitsev | 10:41552d038a69 | 11 | * All rights reserved. This software and/or documentation is licensed by ON Semiconductor |
Zaitsev | 10:41552d038a69 | 12 | * under limited terms and conditions. The terms and conditions pertaining to the software |
Zaitsev | 10:41552d038a69 | 13 | * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf |
Zaitsev | 10:41552d038a69 | 14 | * (ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software) and |
Zaitsev | 10:41552d038a69 | 15 | * if applicable the software license agreement. Do not use this software and/or |
Zaitsev | 10:41552d038a69 | 16 | * documentation unless you have carefully read and you agree to the limited terms and |
Zaitsev | 10:41552d038a69 | 17 | * conditions. By using this software and/or documentation, you agree to the limited |
Zaitsev | 10:41552d038a69 | 18 | * terms and conditions. |
Zaitsev | 10:41552d038a69 | 19 | * |
Zaitsev | 10:41552d038a69 | 20 | * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED |
Zaitsev | 10:41552d038a69 | 21 | * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF |
Zaitsev | 10:41552d038a69 | 22 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. |
Zaitsev | 10:41552d038a69 | 23 | * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, |
Zaitsev | 10:41552d038a69 | 24 | * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
Zaitsev | 10:41552d038a69 | 25 | * @endinternal |
Zaitsev | 10:41552d038a69 | 26 | * |
Zaitsev | 10:41552d038a69 | 27 | * @ingroup i2c |
Zaitsev | 10:41552d038a69 | 28 | */ |
Zaitsev | 10:41552d038a69 | 29 | |
Zaitsev | 10:41552d038a69 | 30 | #include "mbed_assert.h" |
Zaitsev | 10:41552d038a69 | 31 | #include "i2c_api.h" |
Zaitsev | 10:41552d038a69 | 32 | #include "clock.h" |
Zaitsev | 10:41552d038a69 | 33 | #include "i2c_ipc7208_map.h" |
Zaitsev | 10:41552d038a69 | 34 | #include "memory_map.h" |
Zaitsev | 10:41552d038a69 | 35 | #include "PeripheralPins.h" |
Zaitsev | 10:41552d038a69 | 36 | |
Zaitsev | 10:41552d038a69 | 37 | #ifndef I2C_H_ |
Zaitsev | 10:41552d038a69 | 38 | #define I2C_H_ |
Zaitsev | 10:41552d038a69 | 39 | |
Zaitsev | 10:41552d038a69 | 40 | /* Miscellaneous I/O and control operations codes */ |
Zaitsev | 10:41552d038a69 | 41 | #define I2C_IPC7208_IOCTL_NOT_ACK 0x03 |
Zaitsev | 10:41552d038a69 | 42 | #define I2C_IPC7208_IOCTL_NULL_CMD 0x04 |
Zaitsev | 10:41552d038a69 | 43 | #define I2C_IPC7208_IOCTL_ACK 0x05 |
Zaitsev | 10:41552d038a69 | 44 | |
Zaitsev | 10:41552d038a69 | 45 | /* Definitions for the clock speed. */ |
Zaitsev | 10:41552d038a69 | 46 | #define I2C_SPEED_100K_AT_8MHZ (uint8_t)0x12 |
Zaitsev | 10:41552d038a69 | 47 | #define I2C_SPEED_100K_AT_16MHZ (uint8_t)0x26 |
Zaitsev | 10:41552d038a69 | 48 | #define I2C_SPEED_400K_AT_8MHZ (uint8_t)0x03 |
Zaitsev | 10:41552d038a69 | 49 | #define I2C_SPEED_400K_AT_16MHZ (uint8_t)0x08 |
Zaitsev | 10:41552d038a69 | 50 | |
Zaitsev | 10:41552d038a69 | 51 | |
Zaitsev | 10:41552d038a69 | 52 | /* I2C commands */ |
Zaitsev | 10:41552d038a69 | 53 | #define I2C_CMD_NULL 0x00 |
Zaitsev | 10:41552d038a69 | 54 | #define I2C_CMD_WDAT0 0x10 |
Zaitsev | 10:41552d038a69 | 55 | #define I2C_CMD_WDAT1 0x11 |
Zaitsev | 10:41552d038a69 | 56 | #define I2C_CMD_WDAT8 0x12 |
Zaitsev | 10:41552d038a69 | 57 | #define I2C_CMD_RDAT8 0x13 |
Zaitsev | 10:41552d038a69 | 58 | #define I2C_CMD_STOP 0x14 |
Zaitsev | 10:41552d038a69 | 59 | #define I2C_CMD_START 0x15 |
Zaitsev | 10:41552d038a69 | 60 | #define I2C_CMD_VRFY_ACK 0x16 |
Zaitsev | 10:41552d038a69 | 61 | #define I2C_CMD_VRFY_VACK 0x17 |
Zaitsev | 10:41552d038a69 | 62 | |
Zaitsev | 10:41552d038a69 | 63 | /* Status register bits */ |
Zaitsev | 10:41552d038a69 | 64 | #define I2C_STATUS_CMD_FIFO_MPTY_BIT 0x01 |
Zaitsev | 10:41552d038a69 | 65 | #define I2C_STATUS_RD_DATA_RDY_BIT 0x02 |
Zaitsev | 10:41552d038a69 | 66 | #define I2C_STATUS_BUS_ERR_BIT 0x04 |
Zaitsev | 10:41552d038a69 | 67 | #define I2C_STATUS_RD_DATA_UFL_BIT 0x08 |
Zaitsev | 10:41552d038a69 | 68 | #define I2C_STATUS_CMD_FIFO_OFL_BIT 0x10 |
Zaitsev | 10:41552d038a69 | 69 | #define I2C_STATUS_CMD_FIFO_FULL_BIT 0x20 |
Zaitsev | 10:41552d038a69 | 70 | |
Zaitsev | 10:41552d038a69 | 71 | /* I2C return status */ |
Zaitsev | 10:41552d038a69 | 72 | #define I2C_STATUS_INVALID 0xFF |
Zaitsev | 10:41552d038a69 | 73 | #define I2C_STATUS_SUCCESS 0x00 |
Zaitsev | 10:41552d038a69 | 74 | #define I2C_STATUS_FAIL 0x01 |
Zaitsev | 10:41552d038a69 | 75 | #define I2C_STATUS_BUS_ERROR 0x02 |
Zaitsev | 10:41552d038a69 | 76 | #define I2C_STATUS_RD_DATA_UFL 0x03 |
Zaitsev | 10:41552d038a69 | 77 | #define I2C_STATUS_CMD_FIFO_OFL 0x04 |
Zaitsev | 10:41552d038a69 | 78 | #define I2C_STATUS_INTERRUPT_ERROR 0x05 |
Zaitsev | 10:41552d038a69 | 79 | #define I2C_STATUS_CMD_FIFO_EMPTY 0x06 |
Zaitsev | 10:41552d038a69 | 80 | |
Zaitsev | 10:41552d038a69 | 81 | /* I2C clock divider position */ |
Zaitsev | 10:41552d038a69 | 82 | #define I2C_CLOCKDIVEDER_VAL_MASK 0x1F |
Zaitsev | 10:41552d038a69 | 83 | #define I2C_APB_CLK_DIVIDER_VAL_MASK 0x1FE0 |
Zaitsev | 10:41552d038a69 | 84 | |
Zaitsev | 10:41552d038a69 | 85 | /* Error check */ |
Zaitsev | 10:41552d038a69 | 86 | #define I2C_UFL_CHECK (d->membase->STATUS.WORD & 0x80) |
Zaitsev | 10:41552d038a69 | 87 | #define FIFO_OFL_CHECK (d->membase->STATUS.WORD & 0x10) |
Zaitsev | 10:41552d038a69 | 88 | #define I2C_BUS_ERR_CHECK (d->membase->STATUS.WORD & 0x04) |
Zaitsev | 10:41552d038a69 | 89 | #define RD_DATA_READY (d->membase->STATUS.WORD & 0x02) |
Zaitsev | 10:41552d038a69 | 90 | |
Zaitsev | 10:41552d038a69 | 91 | #define I2C_API_STATUS_SUCCESS 0 |
Zaitsev | 10:41552d038a69 | 92 | #define PAD_REG_ADRS_BYTE_SIZE 4 |
Zaitsev | 10:41552d038a69 | 93 | |
Zaitsev | 10:41552d038a69 | 94 | /** Init I2C device. |
Zaitsev | 10:41552d038a69 | 95 | * @details |
Zaitsev | 10:41552d038a69 | 96 | * Sets the necessary registers. The baud rate is set default to 100K |
Zaitsev | 10:41552d038a69 | 97 | * |
Zaitsev | 10:41552d038a69 | 98 | * @param obj A I2C device instance. |
Zaitsev | 10:41552d038a69 | 99 | * @param sda GPIO number for SDA line |
Zaitsev | 10:41552d038a69 | 100 | * @param scl GPIO number for SCL line |
Zaitsev | 10:41552d038a69 | 101 | * @return None |
Zaitsev | 10:41552d038a69 | 102 | */ |
Zaitsev | 10:41552d038a69 | 103 | extern void fI2cInit(i2c_t *obj,PinName sda,PinName scl); |
Zaitsev | 10:41552d038a69 | 104 | |
Zaitsev | 10:41552d038a69 | 105 | /** Set baud rate or frequency |
Zaitsev | 10:41552d038a69 | 106 | * @details |
Zaitsev | 10:41552d038a69 | 107 | * Sets user baudrate |
Zaitsev | 10:41552d038a69 | 108 | * |
Zaitsev | 10:41552d038a69 | 109 | * @param obj A I2C device instance. |
Zaitsev | 10:41552d038a69 | 110 | * @param hz User desired baud rate/frequency |
Zaitsev | 10:41552d038a69 | 111 | * @return None |
Zaitsev | 10:41552d038a69 | 112 | */ |
Zaitsev | 10:41552d038a69 | 113 | extern void fI2cFrequency(i2c_t *obj, uint32_t hz); |
Zaitsev | 10:41552d038a69 | 114 | |
Zaitsev | 10:41552d038a69 | 115 | /** Sends start bit |
Zaitsev | 10:41552d038a69 | 116 | * @details |
Zaitsev | 10:41552d038a69 | 117 | * Sends start bit on i2c pins |
Zaitsev | 10:41552d038a69 | 118 | * |
Zaitsev | 10:41552d038a69 | 119 | * @param obj A I2C device instance. |
Zaitsev | 10:41552d038a69 | 120 | * @return status |
Zaitsev | 10:41552d038a69 | 121 | */ |
Zaitsev | 10:41552d038a69 | 122 | extern int32_t fI2cStart(i2c_t *obj); |
Zaitsev | 10:41552d038a69 | 123 | |
Zaitsev | 10:41552d038a69 | 124 | /** Sends stop bit |
Zaitsev | 10:41552d038a69 | 125 | * @details |
Zaitsev | 10:41552d038a69 | 126 | * Sends stop bit on i2c pins |
Zaitsev | 10:41552d038a69 | 127 | * |
Zaitsev | 10:41552d038a69 | 128 | * @param obj A I2C device instance. |
Zaitsev | 10:41552d038a69 | 129 | * @return status |
Zaitsev | 10:41552d038a69 | 130 | */ |
Zaitsev | 10:41552d038a69 | 131 | extern int32_t fI2cStop(i2c_t *obj); |
Zaitsev | 10:41552d038a69 | 132 | |
Zaitsev | 10:41552d038a69 | 133 | /** Reads data from a I2C device in blocking fashion. |
Zaitsev | 10:41552d038a69 | 134 | * @details |
Zaitsev | 10:41552d038a69 | 135 | * The data is read from the receive queue into the buffer. The receive queue is |
Zaitsev | 10:41552d038a69 | 136 | * filled by the interrupt handler. If not enough data is available, |
Zaitsev | 10:41552d038a69 | 137 | * |
Zaitsev | 10:41552d038a69 | 138 | * @param d The device to read from. |
Zaitsev | 10:41552d038a69 | 139 | * @param buf The buffer to read into (only the contents of the buffer may be modified, not the buffer itself). |
Zaitsev | 10:41552d038a69 | 140 | * @param len The maximum number of bytes to read, typically the buffer length. |
Zaitsev | 10:41552d038a69 | 141 | * @return On Success: The actual number of bytes read. On Failure: Failure code. |
Zaitsev | 10:41552d038a69 | 142 | */ |
Zaitsev | 10:41552d038a69 | 143 | extern int32_t fI2cReadB(i2c_t *d, char *buf, int len); |
Zaitsev | 10:41552d038a69 | 144 | |
Zaitsev | 10:41552d038a69 | 145 | /** Write data to an I2C device. |
Zaitsev | 10:41552d038a69 | 146 | * @details |
Zaitsev | 10:41552d038a69 | 147 | * The commands(I2C instructions) and data arrive at the I2C Engine via the Command FIFO. |
Zaitsev | 10:41552d038a69 | 148 | * The command to write the data & data to be written is sent to command FIFO by writing it into command register. |
Zaitsev | 10:41552d038a69 | 149 | * |
Zaitsev | 10:41552d038a69 | 150 | * @param d The device to write to. |
Zaitsev | 10:41552d038a69 | 151 | * @param buf The buffer to write from (the contents of the buffer may not be modified). |
Zaitsev | 10:41552d038a69 | 152 | * @param len The number of bytes to write. |
Zaitsev | 10:41552d038a69 | 153 | * @return On success: The actual number of bytes written. On Failure: Failure code |
Zaitsev | 10:41552d038a69 | 154 | */ |
Zaitsev | 10:41552d038a69 | 155 | extern int32_t fI2cWriteB(i2c_t *d, const char *buf, int len); |
Zaitsev | 10:41552d038a69 | 156 | |
Zaitsev | 10:41552d038a69 | 157 | #endif /* I2C_H_ */ |