Quantcast
Channel: Microcontrollers
Viewing all articles
Browse latest Browse all 217577

Forum Post: RE: EK-TM4C123GXL: Trying to Read BME280 using I2C.

$
0
0
#ifndef FUNCTIONS_H_ #define FUNCTIONS_H_ void InitConsole(void) { SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOA); GPIOPinConfigure (GPIO_PA0_U0RX); GPIOPinConfigure (GPIO_PA1_U0TX); SysCtlPeripheralEnable (SYSCTL_PERIPH_UART0); UARTClockSourceSet(UART0_BASE, UART_CLOCK_SYSTEM); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioConfig(0, 115200, SysCtlClockGet()); } void InitI2C1(void) { SysCtlPeripheralEnable (SYSCTL_PERIPH_I2C1); GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7); GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); GPIOPinConfigure (GPIO_PA6_I2C1SCL); GPIOPinConfigure (GPIO_PA7_I2C1SDA); I2CMasterInitExpClk(I2C1_BASE, SysCtlClockGet(), false); } void I2CSend(uint8_t slave_addr, uint8_t num_of_args, ...) { int i; // Tell the master module what address it will place on the bus when // communicating with the slave. I2CMasterSlaveAddrSet(I2C1_BASE, slave_addr, false); //stores list of variable number of arguments va_list vargs; //specifies the va_list to "open" and the last fixed argument //so vargs knows where to start looking va_start(vargs, num_of_args); //put data to be sent into FIFO I2CMasterDataPut(I2C1_BASE, va_arg(vargs, uint32_t)); //if there is only one argument, we only need to use the //single send I2C function if (num_of_args == 1) { //Initiate send of data from the MCU I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_SEND); // Wait until MCU is done transferring. while (!I2CMasterBusy(I2C1_BASE)) ; while (I2CMasterBusy(I2C1_BASE)) ; //"close" variable argument list va_end(vargs); } //otherwise, we start transmission of multiple bytes on the //I2C bus else { //Initiate send of data from the MCU I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START); // Wait until MCU is done transferring. while (I2CMasterBusy(I2C1_BASE)) ; //send num_of_args-2 pieces of data, using the //BURST_SEND_CONT command of the I2C module for (i = 1; i < (num_of_args - 1); i++) { //put next piece of data into I2C FIFO I2CMasterDataPut(I2C1_BASE, va_arg(vargs, uint32_t)); //send next data that was just placed into FIFO I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); // Wait until MCU is done transferring. while (!I2CMasterBusy(I2C1_BASE)) ; while (I2CMasterBusy(I2C1_BASE)) ; } //put last piece of data into I2C FIFO I2CMasterDataPut(I2C1_BASE, va_arg(vargs, uint32_t)); //send next data that was just placed into FIFO I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH); // Wait until MCU is done transferring. while (!I2CMasterBusy(I2C1_BASE)) ; while (I2CMasterBusy(I2C1_BASE)) ; //"close" variable args list va_end(vargs); } } void user_delay_ms(uint32_t period) { SysCtlDelay((SysCtlClockGet() / 1000) * (uint32_t) period); } int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ /* * The parameter dev_id can be used as a variable to store the I2C address of the device */ /* * Data on the bus should be like * |------------+---------------------| * | I2C action | Data | * |------------+---------------------| * | Start | - | * | Write | (reg_addr) | * | Stop | - | * | Start | - | * | Read | (reg_data[0]) | * | Read | (....) | * | Read | (reg_data[len - 1]) | * | Stop | - | * |------------+---------------------| */ I2CMasterSlaveAddrSet(I2C1_BASE, (uint8_t) dev_id, false); I2CMasterDataPut(I2C1_BASE, (uint8_t) reg_addr); I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START); while (!I2CMasterBusy(I2C1_BASE)) ; while (I2CMasterBusy(I2C1_BASE)) ; I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH); I2CMasterSlaveAddrSet(I2C1_BASE, dev_id, true); while (len--) { I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); while (!I2CMasterBusy(I2C1_BASE)) ; while (I2CMasterBusy(I2C1_BASE)) ; *reg_data = I2CMasterDataGet(I2C1_BASE); reg_data++; } return rslt; } int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ /* * The parameter dev_id can be used as a variable to store the I2C address of the device */ /* * Data on the bus should be like * |------------+---------------------| * | I2C action | Data | * |------------+---------------------| * | Start | - | * | Write | (reg_addr) | * | Write | (reg_data[0]) | * | Write | (....) | * | Write | (reg_data[len - 1]) | * | Stop | - | * |------------+---------------------| */ I2CSend(dev_id,1,reg_addr); while(len--) { I2CSend(dev_id,1,*reg_data); reg_data++; } } #endif /* FUNCTIONS_H_ */

Viewing all articles
Browse latest Browse all 217577

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>