|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief PIC18 I2C Controller driver (master only) |
| 4 | + * |
| 5 | + * This module provides I2C master functionality for the PIC18F26K83 microcontroller. |
| 6 | + * It supports 7-bit addressing, configurable clock frequency, and register read/write |
| 7 | + * operations for common I2C devices. |
| 8 | + */ |
| 9 | + |
1 | 10 | #ifndef ROCKETLIB_I2C_H |
2 | 11 | #define ROCKETLIB_I2C_H |
3 | 12 |
|
|
8 | 17 | #error "C++ is not supported" |
9 | 18 | #endif |
10 | 19 |
|
11 | | -/* Initialize I2C Controller |
12 | | - I2C Frequency = (100 kHz) / (2 ^ clkdiv) |
13 | | - Max frequency clkdiv = b000, 100kHz |
14 | | - Min frequency clkdiv = b111, 781.25 Hz |
15 | | -*/ |
| 20 | +/** |
| 21 | + * @brief Initialize I2C Controller |
| 22 | + * |
| 23 | + * This function initializes the I2C1 peripheral as a master device. The I2C clock |
| 24 | + * frequency is determined by the clock divider value according to the formula: |
| 25 | + * I2C Frequency = (100 kHz) / (2 ^ clkdiv) |
| 26 | + * |
| 27 | + * @param clkdiv Clock divider value (0-7) |
| 28 | + * - clkdiv = 0: Maximum frequency = 100 kHz |
| 29 | + * - clkdiv = 7: Minimum frequency = 781.25 Hz |
| 30 | + */ |
16 | 31 | void i2c_init(uint8_t clkdiv); |
17 | 32 |
|
| 33 | +/** |
| 34 | + * @brief Write data to an I2C device |
| 35 | + * |
| 36 | + * This function sends a block of data to the specified I2C device address. |
| 37 | + * |
| 38 | + * @param address 7-bit I2C device address (0-127) |
| 39 | + * @param data Pointer to the data buffer to transmit |
| 40 | + * @param len Number of bytes to transmit |
| 41 | + * @return bool Returns true on success, false on error (NACK, timeout, or bus collision) |
| 42 | + */ |
18 | 43 | bool i2c_write_data(uint8_t address, const uint8_t *data, uint8_t len); |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief Read data from an I2C device |
| 47 | + * |
| 48 | + * This function reads a block of data from the specified I2C device address. |
| 49 | + * |
| 50 | + * @param address 7-bit I2C device address (0-127) |
| 51 | + * @param data Pointer to the buffer to store received data |
| 52 | + * @param len Number of bytes to read |
| 53 | + * @return bool Returns true on success, false on error (NACK, timeout, or bus collision) |
| 54 | + */ |
19 | 55 | bool i2c_read_data(uint8_t address, uint8_t *data, uint8_t len); |
| 56 | + |
| 57 | +/** |
| 58 | + * @brief Write a single byte to an I2C device register |
| 59 | + * |
| 60 | + * This function writes an 8-bit value to a register on the specified I2C device. |
| 61 | + * |
| 62 | + * @param address 7-bit I2C device address (0-127) |
| 63 | + * @param reg Register address to write to |
| 64 | + * @param val 8-bit value to write |
| 65 | + * @return bool Returns true on success, false on error (NACK, timeout, or bus collision) |
| 66 | + */ |
20 | 67 | bool i2c_write_reg8(uint8_t address, uint8_t reg, uint8_t val); |
| 68 | + |
| 69 | +/** |
| 70 | + * @brief Write a 16-bit value to an I2C device register |
| 71 | + * |
| 72 | + * This function writes a 16-bit value to a register on the specified I2C device. |
| 73 | + * The value is sent in big-endian format (MSB first). |
| 74 | + * |
| 75 | + * @param address 7-bit I2C device address (0-127) |
| 76 | + * @param reg Register address to write to |
| 77 | + * @param val 16-bit value to write |
| 78 | + * @return bool Returns true on success, false on error (NACK, timeout, or bus collision) |
| 79 | + */ |
21 | 80 | bool i2c_write_reg16(uint8_t address, uint8_t reg, uint16_t val); |
| 81 | + |
| 82 | +/** |
| 83 | + * @brief Read a single byte from an I2C device register |
| 84 | + * |
| 85 | + * This function reads an 8-bit value from a register on the specified I2C device. |
| 86 | + * |
| 87 | + * @param address 7-bit I2C device address (0-127) |
| 88 | + * @param reg Register address to read from |
| 89 | + * @param value Pointer to store the read value |
| 90 | + * @return bool Returns true on success, false on error (NACK, timeout, or bus collision) |
| 91 | + */ |
22 | 92 | bool i2c_read_reg8(uint8_t address, uint8_t reg, uint8_t *value); |
| 93 | + |
| 94 | +/** |
| 95 | + * @brief Read a 16-bit value from an I2C device register |
| 96 | + * |
| 97 | + * This function reads a 16-bit value from a register on the specified I2C device. |
| 98 | + * The value is read in big-endian format (MSB first). |
| 99 | + * |
| 100 | + * @param address 7-bit I2C device address (0-127) |
| 101 | + * @param reg Register address to read from |
| 102 | + * @param value Pointer to store the read value |
| 103 | + * @return bool Returns true on success, false on error (NACK, timeout, or bus collision) |
| 104 | + */ |
23 | 105 | bool i2c_read_reg16(uint8_t address, uint8_t reg, uint16_t *value); |
24 | 106 |
|
25 | 107 | #endif |
0 commit comments