ESOS32
ESOSOn32-bitProcessors
esos_stm32l4_i2c.h
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2019 J. W. Bruce ("AUTHOR(S)")"
3  * All rights reserved.
4  * (J. W. Bruce, jwbruce_AT_tntech.edu, Tennessee Tech University)
5  *
6  * Permission to use, copy, modify, and distribute this software and its
7  * documentation for any purpose, without fee, and without written agreement is
8  * hereby granted, provided that the above copyright notice, the following
9  * two paragraphs and the authors appear in all copies of this software.
10  *
11  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
12  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
13  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
14  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15  *
16  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
19  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
20  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
21  *
22  * Please maintain this header in its entirety when copying/modifying
23  * these files.
24  *
25  *
26  */
27 
28 #ifndef _ESOS_STM32L4_I2C_H
29 #define _ESOS_STM32L4_I2C_H
30 
31 
37 // Documentation for this file. If the \file tag isn't present,
38 // this file won't be documented.
46 /* I N C L U D E S **********************************************************/
47 #include "esos.h"
48 #include "esos_i2c.h"
49 #include "esos_stm32l4.h"
50 #include <libopencm3/stm32/i2c.h>
51 
52 /* D E F I N I T I O N S ****************************************************/
53 
54 /* E X T E R N S ************************************************************/
55 extern struct stTask __stChildTaskI2C, __stGrandChildTaskI2C;
56 extern uint8_t __esos_i2c_dataBytes[2]; // used to store arguments
57 
58 /* M A C R O S **************************************************************/
59 #define CLEAR_REGISTER_BITS(reg,mask) reg &= (~(mask))
60 #define SET_REGISTER_BITS(reg,mask) reg |= (mask)
61 #define MAKE_I2C_WR_ADDR(bits) bits &= 0xFFF
62 #define MAKE_I2C_RD_ADDR(bits) bits &= 0x001
63 
64 #define __ESOS_I2C_STM32L4_SET_WRITE_DIR() I2C1_CR2 &= (~I2C_CR2_RD_WRN)
65 #define __ESOS_I2C_STM32L4_SET_READ_DIR() I2C1_CR2 |= I2C_CR2_RD_WRN
66 #define __ESOS_I2C_STM32L4_SET_NUM_BYTES(x) I2C1_CR2 = (I2C1_CR2 & ~I2C_CR2_NBYTES_MASK) | (x << I2C_CR2_NBYTES_SHIFT) // & ~I2C_CR2_START )|((x) << I2C_CR2_NBYTES_SHIFT)
67 #define __ESOS_I2C_STM32L4_SET_ADDR7_MODE() I2C1_CR2 &= (~I2C_CR2_ADD10)
68 #define __ESOS_I2C_STM32L4_SET_ADDR10_MODE() I2C1_CR2 |= I2C_CR2_ADD10
69 #define __ESOS_I2C_STM32L4_SET_ADDR7(x) I2C1_CR2 = (I2C1_CR2 & ~I2C_CR2_SADD_7BIT_MASK) | ((x & 0x7F) << I2C_CR2_SADD_7BIT_SHIFT) //& ~I2C_CR2_START)|((x) << I2C_CR2_SADD_7BIT_SHIFT)
70 #define __ESOS_I2C_STM32L4_SET_AUTOEND() I2C1_CR2 |= I2C_CR2_AUTOEND
71 #define __ESOS_I2C_STM32L4_CLEAR_AUTOEND() I2C1_CR2 &= (~I2C_CR2_AUTOEND)
72 #define __ESOS_I2C_STM32L4_SET_RELOAD() I2C1_CR2 |= I2C_CR2_RELOAD
73 #define __ESOS_I2C_STM32L4_CLEAR_RELOAD() I2C1_CR2 &= (~I2C_CR2_RELOAD)
74 #define __ESOS_I2C_STM32L4_IS_NACK_RECEIVED() (I2C1_ISR & I2C_ISR_NACKF)
75 
76 #define __ESOS_I2C_STM32L4_CLEAR_STOP_FLAG() I2C1_ICR |= I2C_ICR_STOPCF
77 #define __ESOS_I2C_STM32L4_RESET_CR2() I2C1_CR2 &= (uint32_t)~((uint32_t)(I2C_CR2_SADD_7BIT_MASK | I2C_CR2_HEAD10R | I2C_CR2_NBYTES_MASK | I2C_CR2_RELOAD | I2C_CR2_RD_WRN))
78 
79 
80 #define __ESOS_I2C_STM32L4_IS_BUSY() (I2C1_ISR & I2C_ISR_BUSY)
81 // FROM Ref Manual:
82 // This bit is set by hardware when the I2C_TXDR register is empty and the data to be
83 // transmitted must be written in the I2C_TXDR register. It is cleared when the next data to be
84 // sent is written in the I2C_TXDR register.
85 #define __ESOS_I2C_STM32L4_IS_TX_EMPTY() (I2C1_ISR & I2C_ISR_TXIS)
86 
87 // FROM Ref Manual:
88 // This bit is set by hardware when the received data is copied into the I2C_RXDR register, and
89 // is ready to be read. It is cleared when I2C_RXDR is read.
90 #define __ESOS_I2C_STM32L4_IS_RX_NOT_EMPTY() (I2C1_ISR & I2C_ISR_RXNE)
91 
92 // Macros to perform hardware I2C fundamentals onwithin the ESOS
93 // I2C child tasks
94 #define __ESOS_I2C_HW_START() \
95  do{ \
96  I2C1_CR2 |= I2C_CR2_START; \
97  ESOS_TASK_WAIT_WHILE( I2C1_CR2 & I2C_CR2_START); \
98  }while(0)
99 
100 #define __ESOS_I2C_HW_RSTART() \
101  do{ \
102  I2C1CONbits.RSEN = 1; \
103  ESOS_TASK_WAIT_WHILE( I2C1CONbits.RSEN); \
104  }while(0)
105 
106 #define __ESOS_I2C_HW_STOP() \
107  do{ \
108  I2C1_CR2 |= I2C_CR2_STOP; \
109  ESOS_TASK_WAIT_WHILE( I2C1_CR2 & I2C_CR2_STOP); \
110  }while(0)
111 
112 #define __ESOS_I2C_HW_PUT(byte) \
113  do{ \
114  I2C1_TXDR = (byte); \
115  ESOS_TASK_WAIT_UNTIL( __esos_i2c_hw_IsTransferComplete() ); \
116  }while(0)
117 
118 /* P U B L I C P R O T O T Y P E S *****************************************/
119 
120 //I2C Operations
121 void __esos_i2c_hw_config(uint32_t u32_i2cbps);
122 
123 //I2C Transactions
124 ESOS_CHILD_TASK( __esos_hw_getI2C1, uint8_t* pu8_x, uint8_t u8_ack2Send);
125 ESOS_CHILD_TASK( __esos_hw_writeNI2C1, uint8_t u8_addr, uint8_t* pu8_d, uint16_t u16_cnt);
126 ESOS_CHILD_TASK( __esos_hw_readNI2C1, uint8_t u8_addr, uint8_t* pu8_d, uint16_t u16_cnt);
127 
129 #endif // end ESOS_STM32L4_I2C_H
esos_stm32l4.h
This is the master include file for implementing ESOS32 the STMicroelectronics STM32L4xx MCUs.
__esos_i2c_hw_config
void __esos_i2c_hw_config(uint32_t u32_i2cbps)
Definition: esos_hwxxx_i2c.c:74
esos.h
ESOS_CHILD_TASK
#define ESOS_CHILD_TASK(taskname,...)
Definition: esos_task.h:246
stTask
Definition: esos_task.h:54
esos_i2c.h