at32f415_i2c.h 13.2 KB
Newer Older
1 2 3
/**
  **************************************************************************
  * @file     at32f415_i2c.h
4 5
  * @version  v2.0.5
  * @date     2022-05-20
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
  * @brief    at32f415 i2c header file
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __AT32F415_I2C_H
#define __AT32F415_I2C_H

#ifdef __cplusplus
extern "C" {
#endif


/* includes ------------------------------------------------------------------*/
#include "at32f415.h"

/** @addtogroup AT32F415_periph_driver
  * @{
  */

/** @addtogroup I2C
  * @{
  */

/** @defgroup I2C_sts1_flags_definition
  * @brief  i2c sts1 flag
  * @{
  */

#define I2C_STARTF_FLAG                  ((uint32_t)0x00000001) /*!< i2c start condition generation complete flag */
#define I2C_ADDR7F_FLAG                  ((uint32_t)0x00000002) /*!< i2c 0~7 bit address match flag */
#define I2C_TDC_FLAG                     ((uint32_t)0x00000004) /*!< i2c transmit data complete flag */
#define I2C_ADDRHF_FLAG                  ((uint32_t)0x00000008) /*!< i2c master 9~8 bit address header match flag */
#define I2C_STOPF_FLAG                   ((uint32_t)0x00000010) /*!< i2c stop condition generation complete flag */
#define I2C_RDBF_FLAG                    ((uint32_t)0x00000040) /*!< i2c receive data buffer full flag */
#define I2C_TDBE_FLAG                    ((uint32_t)0x00000080) /*!< i2c transmit data buffer empty flag */
#define I2C_BUSERR_FLAG                  ((uint32_t)0x00000100) /*!< i2c bus error flag */
#define I2C_ARLOST_FLAG                  ((uint32_t)0x00000200) /*!< i2c arbitration lost flag */
#define I2C_ACKFAIL_FLAG                 ((uint32_t)0x00000400) /*!< i2c acknowledge failure flag */
#define I2C_OUF_FLAG                     ((uint32_t)0x00000800) /*!< i2c overflow or underflow flag */
#define I2C_PECERR_FLAG                  ((uint32_t)0x00001000) /*!< i2c pec receive error flag */
#define I2C_TMOUT_FLAG                   ((uint32_t)0x00004000) /*!< i2c smbus timeout flag */
#define I2C_ALERTF_FLAG                  ((uint32_t)0x00008000) /*!< i2c smbus alert flag */

/**
  * @}
  */

/** @defgroup I2C_sts2_flags_definition
  * @brief  i2c sts2 flag
  * @{
  */

#define I2C_TRMODE_FLAG                  ((uint32_t)0x10010000) /*!< i2c transmission mode */
#define I2C_BUSYF_FLAG                   ((uint32_t)0x10020000) /*!< i2c bus busy flag transmission mode */
#define I2C_DIRF_FLAG                    ((uint32_t)0x10040000) /*!< i2c transmission direction flag */
#define I2C_GCADDRF_FLAG                 ((uint32_t)0x10100000) /*!< i2c general call address received flag */
#define I2C_DEVADDRF_FLAG                ((uint32_t)0x10200000) /*!< i2c smbus device address received flag */
#define I2C_HOSTADDRF_FLAG               ((uint32_t)0x10400000) /*!< i2c smbus host address received flag */
#define I2C_ADDR2_FLAG                   ((uint32_t)0x10800000) /*!< i2c own address 2 received flag */

/**
  * @}
  */

/** @defgroup I2C_interrupts_definition
  * @brief i2c interrupt
  * @{
  */

#define I2C_DATA_INT                     ((uint16_t)0x0400) /*!< i2c data transmission interrupt */
#define I2C_EVT_INT                      ((uint16_t)0x0200) /*!< i2c event interrupt */
#define I2C_ERR_INT                      ((uint16_t)0x0100) /*!< i2c error interrupt */

/**
  * @}
  */

/** @defgroup I2C_exported_types
  * @{
  */

/**
  * @brief i2c master receiving mode acknowledge control
  */
typedef enum
{
  I2C_MASTER_ACK_CURRENT                 = 0x00, /*!< acken bit acts on the current byte */
  I2C_MASTER_ACK_NEXT                    = 0x01  /*!< acken bit acts on the next byte */
} i2c_master_ack_type;

/**
  * @brief i2c pec position set
  */
typedef enum
{
  I2C_PEC_POSITION_CURRENT               = 0x00, /*!< the current byte is pec */
  I2C_PEC_POSITION_NEXT                  = 0x01  /*!< the next byte is pec */
} i2c_pec_position_type;


/**
  * @brief i2c smbus alert pin set
  */
typedef enum
{
  I2C_SMBUS_ALERT_HIGH                   = 0x00, /*!< smbus alert pin set high */
  I2C_SMBUS_ALERT_LOW                    = 0x01  /*!< smbus alert pin set low */
} i2c_smbus_alert_set_type;

/**
  * @brief i2c smbus mode set
  */
typedef enum
{
  I2C_SMBUS_MODE_DEVICE                  = 0x00, /*!< smbus device mode */
  I2C_SMBUS_MODE_HOST                    = 0x01  /*!< smbus host mode */
} i2c_smbus_mode_set_type;


/**
  * @brief i2c fast mode duty cycle
  */
typedef enum
{
  I2C_FSMODE_DUTY_2_1                    = 0x00, /*!< duty cycle is 2:1 in fast mode */
  I2C_FSMODE_DUTY_16_9                   = 0x01  /*!< duty cycle is 16:9 in fast mode */
} i2c_fsmode_duty_cycle_type;

/**
  * @brief i2c address mode
  */
typedef enum
{
  I2C_ADDRESS_MODE_7BIT                  = 0x00, /*!< 7bit address mode */
  I2C_ADDRESS_MODE_10BIT                 = 0x01  /*!< 10bit address mode */
} i2c_address_mode_type;

/**
  * @brief i2c address direction
  */
typedef enum
{
  I2C_DIRECTION_TRANSMIT                 = 0x00, /*!< transmit mode */
  I2C_DIRECTION_RECEIVE                  = 0x01  /*!< receive mode */
} i2c_direction_type;

/**
  * @brief type define i2c register all
  */
typedef struct
{
  /**
    * @brief i2c ctrl1 register, offset:0x00
    */
  union
  {
    __IO uint32_t ctrl1;
    struct
    {
      __IO uint32_t i2cen                : 1; /* [0] */
      __IO uint32_t permode              : 1; /* [1] */
      __IO uint32_t reserved1            : 1; /* [2] */
      __IO uint32_t smbmode              : 1; /* [3] */
      __IO uint32_t arpen                : 1; /* [4] */
      __IO uint32_t pecen                : 1; /* [5] */
      __IO uint32_t gcaen                : 1; /* [6] */
      __IO uint32_t stretch              : 1; /* [7] */
      __IO uint32_t genstart             : 1; /* [8] */
      __IO uint32_t genstop              : 1; /* [9] */
      __IO uint32_t acken                : 1; /* [10] */
      __IO uint32_t mackctrl             : 1; /* [11] */
      __IO uint32_t pecten               : 1; /* [12] */
      __IO uint32_t smbalert             : 1; /* [13] */
      __IO uint32_t reserved2            : 1; /* [14] */
      __IO uint32_t reset                : 1; /* [15] */
      __IO uint32_t reserved3            : 16;/* [31:16] */
    } ctrl1_bit;
  };

  /**
    * @brief i2c ctrl2 register, offset:0x04
    */
  union
  {
    __IO uint32_t ctrl2;
    struct
    {
      __IO uint32_t clkfreq              : 8; /* [7:0] */
      __IO uint32_t errien               : 1; /* [8] */
      __IO uint32_t evtien               : 1; /* [9] */
      __IO uint32_t dataien              : 1; /* [10] */
      __IO uint32_t dmaen                : 1; /* [11] */
      __IO uint32_t dmaend               : 1; /* [12] */
      __IO uint32_t reserved1            : 19;/* [31:13] */
    } ctrl2_bit;
  };

  /**
    * @brief i2c oaddr1 register, offset:0x08
    */
  union
  {
    __IO uint32_t oaddr1;
    struct
    {
      __IO uint32_t addr1                : 10;/* [9:0] */
      __IO uint32_t reserved1            : 5; /* [14:10] */
      __IO uint32_t addr1mode            : 1; /* [15] */
      __IO uint32_t reserved2            : 16;/* [31:16] */
    } oaddr1_bit;
  };

  /**
    * @brief i2c oaddr2 register, offset:0x0C
    */
  union
  {
    __IO uint32_t oaddr2;
    struct
    {
      __IO uint32_t addr2en              : 1; /* [0] */
      __IO uint32_t addr2                : 7; /* [7:1] */
      __IO uint32_t reserved1            : 24;/* [31:8] */
    } oaddr2_bit;
  };

  /**
    * @brief i2c dt register, offset:0x10
    */
  union
  {
    __IO uint32_t dt;
    struct
    {
      __IO uint32_t dt                   : 8; /* [7:0] */
      __IO uint32_t reserved1            : 24;/* [31:8] */
    } dt_bit;
  };

  /**
    * @brief i2c sts1 register, offset:0x14
    */
  union
  {
    __IO uint32_t sts1;
    struct
    {
      __IO uint32_t startf               : 1; /* [0] */
      __IO uint32_t addr7f               : 1; /* [1] */
      __IO uint32_t tdc                  : 1; /* [2] */
      __IO uint32_t addrhf               : 1; /* [3] */
      __IO uint32_t stopf                : 1; /* [4] */
      __IO uint32_t reserved1            : 1; /* [5] */
      __IO uint32_t rdbf                 : 1; /* [6] */
      __IO uint32_t tdbe                 : 1; /* [7] */
      __IO uint32_t buserr               : 1; /* [8] */
      __IO uint32_t arlost               : 1; /* [9] */
      __IO uint32_t ackfail              : 1; /* [10] */
      __IO uint32_t ouf                  : 1; /* [11] */
      __IO uint32_t pecerr               : 1; /* [12] */
      __IO uint32_t reserved2            : 1; /* [13] */
      __IO uint32_t tmout                : 1; /* [14] */
      __IO uint32_t alertf               : 1; /* [15] */
      __IO uint32_t reserved3            : 16; /* [31:16] */
    } sts1_bit;
  };

  /**
    * @brief i2c sts2 register, offset:0x18
    */
  union
  {
    __IO uint32_t sts2;
    struct
    {
      __IO uint32_t trmode               : 1; /* [0] */
      __IO uint32_t busyf                : 1; /* [1] */
      __IO uint32_t dirf                 : 1; /* [2] */
      __IO uint32_t reserved1            : 1; /* [3] */
      __IO uint32_t gcaddrf              : 1; /* [4] */
      __IO uint32_t devaddrf             : 1; /* [5] */
      __IO uint32_t hostaddrf            : 1; /* [6] */
      __IO uint32_t addr2                : 1; /* [7] */
      __IO uint32_t pecval               : 8; /* [15:8] */
      __IO uint32_t reserved2            : 16;/* [31:16] */
    } sts2_bit;
  };

  /**
    * @brief i2c clkctrl register, offset:0x1C
    */
  union
  {
    __IO uint32_t clkctrl;
    struct
    {
      __IO uint32_t speed                : 12;/* [11:0] */
      __IO uint32_t reserved1            : 2; /* [13:12] */
      __IO uint32_t dutymode             : 1; /* [14] */
      __IO uint32_t speedmode            : 1; /* [15] */
      __IO uint32_t reserved2            : 16;/* [31:16] */
    } clkctrl_bit;
  };

  /**
    * @brief i2c tmrise register, offset:0x20
    */
  union
  {
    __IO uint32_t tmrise;
    struct
    {
      __IO uint32_t risetime             : 6; /* [5:0] */
      __IO uint32_t reserved1            : 26;/* [31:6] */
    } tmrise_bit;
  };

} i2c_type;

/**
  * @}
  */

#define I2C1                             ((i2c_type *) I2C1_BASE)
#define I2C2                             ((i2c_type *) I2C2_BASE)

/** @defgroup I2C_exported_functions
  * @{
  */

void i2c_reset(i2c_type *i2c_x);
void i2c_software_reset(i2c_type *i2c_x, confirm_state new_state);
void i2c_init(i2c_type *i2c_x, i2c_fsmode_duty_cycle_type duty, uint32_t speed);
void i2c_own_address1_set(i2c_type *i2c_x, i2c_address_mode_type mode, uint16_t address);
void i2c_own_address2_set(i2c_type *i2c_x, uint8_t address);
void i2c_own_address2_enable(i2c_type *i2c_x, confirm_state new_state);
void i2c_smbus_enable(i2c_type *i2c_x, confirm_state new_state);
void i2c_enable(i2c_type *i2c_x, confirm_state new_state);
void i2c_fast_mode_duty_set(i2c_type *i2c_x, i2c_fsmode_duty_cycle_type duty);
void i2c_clock_stretch_enable(i2c_type *i2c_x, confirm_state new_state);
void i2c_ack_enable(i2c_type *i2c_x, confirm_state new_state);
void i2c_master_receive_ack_set(i2c_type *i2c_x, i2c_master_ack_type pos);
void i2c_pec_position_set(i2c_type *i2c_x, i2c_pec_position_type pos);
void i2c_general_call_enable(i2c_type *i2c_x, confirm_state new_state);
void i2c_arp_mode_enable(i2c_type *i2c_x, confirm_state new_state);
void i2c_smbus_mode_set(i2c_type *i2c_x, i2c_smbus_mode_set_type mode);
void i2c_smbus_alert_set(i2c_type *i2c_x, i2c_smbus_alert_set_type level);
void i2c_pec_transmit_enable(i2c_type *i2c_x, confirm_state new_state);
void i2c_pec_calculate_enable(i2c_type *i2c_x, confirm_state new_state);
uint8_t i2c_pec_value_get(i2c_type *i2c_x);
void i2c_dma_end_transfer_set(i2c_type *i2c_x, confirm_state new_state);
void i2c_dma_enable(i2c_type *i2c_x, confirm_state new_state);
void i2c_interrupt_enable(i2c_type *i2c_x, uint16_t source, confirm_state new_state);
void i2c_start_generate(i2c_type *i2c_x);
void i2c_stop_generate(i2c_type *i2c_x);
void i2c_7bit_address_send(i2c_type *i2c_x, uint8_t address, i2c_direction_type direction);
void i2c_data_send(i2c_type *i2c_x, uint8_t data);
uint8_t i2c_data_receive(i2c_type *i2c_x);
flag_status i2c_flag_get(i2c_type *i2c_x, uint32_t flag);
void i2c_flag_clear(i2c_type *i2c_x, uint32_t flag);

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif