Tool/software: Code Composer Studio
I'm writing some test code for a test signal generator. I'm generating analog voltage with a MCP4725 chip (will use a MCP4728 later). I write the 12-bit value I want to the chip register and expect to read the same when I read it later, however, this is to the case.
It works when I step into my MCP4725SendReceive function, but not when I step over.
Any ideas? I tried a lot of delays everywhere.
#include <stdint.h> #include <stdbool.h> #include "driverlib/fpu.h" #include "driverlib/gpio.h" #include "driverlib/i2c.h" #include "driverlib/pin_map.h" #include "driverlib/rom.h" #include "driverlib/sysctl.h" #include "driverlib/uart.h" #include "inc/hw_gpio.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "inc/hw_sysctl.h" #include "inc/hw_types.h" #include "utils/uartstdio.h" //***************************************************************************** // // System config. // //***************************************************************************** uint32_t g_ui32SysClock; // holds the main clock speed //***************************************************************************** // // Init UART interface 115200bps, 8, N, 1 // //***************************************************************************** void InitUART(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOA)) { } GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); while(!SysCtlPeripheralReady(SYSCTL_PERIPH_UART0)) { } UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioConfig(0, 115200, 16000000); } //***************************************************************************** // // Init the DAC chip // //***************************************************************************** void MCP4725Init(void) { UARTprintf("Initiating.\n"); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); while( !SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOD) ); GPIOPinConfigure(GPIO_PD0_I2C7SCL); GPIOPinConfigure(GPIO_PD1_I2C7SDA); GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C7); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C7); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C7); while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C7)) { } I2CMasterGlitchFilterConfigSet(I2C7_BASE, I2C_MASTER_GLITCH_FILTER_8); I2CMasterInitExpClk(I2C7_BASE, g_ui32SysClock, false); } //***************************************************************************** // // Update the DAC output and then read back the output register // //***************************************************************************** uint16_t MCP4725SendReceive(uint16_t loadvalue) { uint8_t pui8WriteData[2]; uint8_t pui8ReadData[5]; uint16_t readvalue; // // Write the desired value to the DAC register // see mcp4725 datasheet section 6.1.1 // pui8WriteData[0] = (uint8_t) ( (loadvalue >> 8) & 0xF ); pui8WriteData[1] = (uint8_t) (loadvalue); I2CMasterSlaveAddrSet(I2C7_BASE, 0x62, false); I2CMasterDataPut( I2C7_BASE, pui8WriteData[0] ); // 1st byte I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_SEND_START); // start send while(I2CMasterBusy(I2C7_BASE)); // wait until done if ( I2CMasterErr(I2C7_BASE) != I2C_MASTER_ERR_NONE ) // check if error { UARTprintf("I2C error\n"); } I2CMasterDataPut( I2C7_BASE, pui8WriteData[1] );// 2nd byte I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);// start send while(I2CMasterBusy(I2C7_BASE)); // wait until done if ( I2CMasterErr(I2C7_BASE) != I2C_MASTER_ERR_NONE ) // check if error { UARTprintf("I2C error\n"); } //ROM_SysCtlDelay(100000000); // tried to put a big delay here, it doesn't change the result // // Read the registers from the chip. // See mcp4725 datasheet section 6.2 // I2CMasterSlaveAddrSet(I2C7_BASE, 0x62, true); // byte 2 of figure 6.3 (actually, the 1st to be read) I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); while(I2CMasterBusy(I2C7_BASE)); pui8ReadData[0] = I2CMasterDataGet(I2C7_BASE); if ( I2CMasterErr(I2C7_BASE) != I2C_MASTER_ERR_NONE ) UARTprintf("I2C error\n"); // byte 3 of figure 6.3 I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while(I2CMasterBusy(I2C7_BASE)); pui8ReadData[1] = I2CMasterDataGet(I2C7_BASE); if ( I2CMasterErr(I2C7_BASE) != I2C_MASTER_ERR_NONE ) UARTprintf("I2C error\n"); // byte 4 of figure 6.3 I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while(I2CMasterBusy(I2C7_BASE)); pui8ReadData[2] = I2CMasterDataGet(I2C7_BASE); if ( I2CMasterErr(I2C7_BASE) != I2C_MASTER_ERR_NONE ) UARTprintf("I2C error\n"); // byte 5 of figure 6.3 I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while(I2CMasterBusy(I2C7_BASE)); pui8ReadData[3] = I2CMasterDataGet(I2C7_BASE); if ( I2CMasterErr(I2C7_BASE) != I2C_MASTER_ERR_NONE ) UARTprintf("I2C error\n"); // byte 6 of figure 6.3 I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); while(I2CMasterBusy(I2C7_BASE)); pui8ReadData[4] = I2CMasterDataGet(I2C7_BASE); if ( I2CMasterErr(I2C7_BASE) != I2C_MASTER_ERR_NONE ) UARTprintf("I2C error\n"); readvalue = pui8ReadData[1] << 4 | pui8ReadData[2] >> 4; // register D11..D0 return( readvalue ); } //***************************************************************************** // // Main // //***************************************************************************** int main(void) { uint16_t writeMillivolts = 0; uint16_t readMillivolts = 0; FPUEnable(); FPULazyStackingEnable(); g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); InitUART(); UARTprintf("\033[2J\033[H"); UARTprintf("CAN Sensors Simulator (CSS) starting.\n"); MCP4725Init(); while(1) { UARTprintf("Writing DAC with %d mV\t ", writeMillivolts); readMillivolts = MCP4725SendReceive( writeMillivolts ); UARTprintf("Reading %d mV from DAC\n", readMillivolts); if (writeMillivolts > 4095) { writeMillivolts = 0; } else { writeMillivolts += 128; } ROM_SysCtlDelay(8000000); } return 1; }