// Set the value of a register void writeReg(uint8_t regAddr, uint8_t regVal) { uint8_t txBuff[2]; txBuff[0] = regAddr; txBuff[1] = regVal; Chip_I2C_MasterSend(i2cDev, LSM303D_ADDR, txBuff, 2); }
// Read value from register uint8_t readReg(uint8_t regAddr) { uint8_t txBuff[1]; uint8_t rxBuff[1]; txBuff[0] = regAddr; Chip_I2C_MasterSend(i2cDev, LSM303D_ADDR, txBuff, 1); Chip_I2C_MasterRead(i2cDev, LSM303D_ADDR, rxBuff, 1); return rxBuff[0]; }
void Board_I2C_Reset(uint8_t reset_val, uint8_t *i2c_tx_buffer) { //Open I2C Channel 0 i2c_tx_buffer[0] = I2C_MUX_CHANNEL_0; Chip_I2C_MasterSend(DEFAULT_I2C, I2C_MUX_SLAVE_ADDRESS, i2c_tx_buffer, 1); //Write reset_val to gas gauge 0 control register i2c_tx_buffer[0] = I2C_GG_CTRL_REG; i2c_tx_buffer[1] = reset_val; Chip_I2C_MasterSend(DEFAULT_I2C, I2C_GG_SLAVE_ADDRESS, i2c_tx_buffer, 2); //Open I2C Channel 1 i2c_tx_buffer[0] = I2C_MUX_CHANNEL_1; Chip_I2C_MasterSend(DEFAULT_I2C, I2C_MUX_SLAVE_ADDRESS, i2c_tx_buffer, 1); //Write reset_val to gas gauge 1 control register i2c_tx_buffer[0] = I2C_GG_CTRL_REG; i2c_tx_buffer[1] = reset_val; Chip_I2C_MasterSend(DEFAULT_I2C, I2C_GG_SLAVE_ADDRESS, i2c_tx_buffer, 2); }
void LSM_write_reg_mag(uint8_t reg_addr, uint8_t data) { // Write the register and then the data // - Make a transmit buffer uint8_t tx_size = 2; uint8_t tx_buf[tx_size]; // - Set the register address tx_buf[0] = reg_addr; tx_buf[1] = data; // - Write the data Chip_I2C_MasterSend(LSM_i2c_id, LSM_mag_address, tx_buf, tx_size); }
int8_t LSM_read_reg_mag(uint8_t reg_addr) { // Write the register we want to read // - Make a transmit buffer uint8_t tx_size = 1; uint8_t tx_buf[tx_size]; // - Set the register address tx_buf[0] = reg_addr; // - Write the register value Chip_I2C_MasterSend(LSM_i2c_id, LSM_mag_address, tx_buf, tx_size); // Read the register value // - Make a receive buffer uint8_t rx_size = 1; uint8_t rx_buf[rx_size]; // - Read the register value Chip_I2C_MasterRead(LSM_i2c_id, LSM_mag_address, rx_buf, rx_size); return rx_buf[0]; }
/* Write data to UDA register */ void UDA1380_REG_Write(uint8_t reg, uint16_t val) { uint8_t dat[3]; dat[0] = reg; dat[1] = val >> 8; dat[2] = val & 0xFF; Chip_I2C_MasterSend(UDA1380_I2C_BUS, I2CDEV_UDA1380_ADDR, dat, sizeof(dat)); }
/* Write multiple registers in one go */ int UDA1380_REG_WriteMult(const uint8_t *buff, int len) { return Chip_I2C_MasterSend(UDA1380_I2C_BUS, I2CDEV_UDA1380_ADDR, buff, len) == len; }
uint32_t Board_I2C_Master_Write(uint8_t slaveAddr, uint8_t *buff, int len) { return Chip_I2C_MasterSend(I2C0, slaveAddr, buff, len); }
int main(void) { //--------------- // Initialize SysTick Timer to generate millisecond count if (Board_SysTick_Init()) { // Unrecoverable Error. Hang. while(1); } //--------------- // Initialize GPIO and LED as output Board_LEDs_Init(); Board_LED_On(LED0); Board_LED_On(LED1); Board_LED_On(LED2); Board_LED_On(LED3); //Initialize I2C Board_I2C_Init(); //--------------- // Initialize UART Communication Board_UART_Init(UART_BAUD_RATE); Board_UART_Println("Started up"); //--------------- // Initialize CAN and CAN Ring Buffer RingBuffer_Init(&can_rx_buffer, _rx_buffer, sizeof(CCAN_MSG_OBJ_T), BUFFER_SIZE); RingBuffer_Flush(&can_rx_buffer); Board_CAN_Init(CCAN_BAUD_RATE, CAN_rx, CAN_tx, CAN_error); // For your convenience. // typedef struct CCAN_MSG_OBJ { // uint32_t mode_id; // uint32_t mask; // uint8_t data[8]; // uint8_t dlc; // uint8_t msgobj; // } CCAN_MSG_OBJ_T; /* [Tutorial] How do filters work? Incoming ID & Mask == Mode_ID for msgobj to accept message Incoming ID : 0xabc Mask: 0xF0F & ----------- 0xa0c mode_id == 0xa0c for msgobj to accept message */ //Set mask to only accept messages from Driver Interface msg_obj.msgobj = 1; msg_obj.mode_id = DI_PACKET__id; msg_obj.mask = 0x555; LPC_CCAN_API->config_rxmsgobj(&msg_obj); can_error_flag = false; can_error_info = 0; lastPrint = msTicks; PDM_STATUS_T pdm_status; int tmp; bool lv_i2c = true, cs_i2c = true, mux_i2c = true; //Board_I2C_Reset(I2C_GG_CONTINUOUS, i2c_tx_buffer); //Open I2C Channel 0 i2c_tx_buffer[0] = I2C_MUX_CHANNEL_0; tmp = Chip_I2C_MasterSend(DEFAULT_I2C, I2C_MUX_SLAVE_ADDRESS, i2c_tx_buffer, 1); Board_UART_Print("Opened Channel 0: "); Board_UART_PrintNum(tmp, 10, true); //Set Gas Gauge 0 to continuous data collection i2c_tx_buffer[0] = I2C_GG_CTRL_REG; i2c_tx_buffer[1] = I2C_GG_CONTINUOUS; tmp = Chip_I2C_MasterSend(DEFAULT_I2C, I2C_GG_SLAVE_ADDRESS, i2c_tx_buffer, 2); Board_UART_Print("Set I2C0 to continuous data collection: "); Board_UART_PrintNum(tmp, 10, true); //Open I2C Channel 1 i2c_tx_buffer[0] = I2C_MUX_CHANNEL_1; tmp = Chip_I2C_MasterSend(DEFAULT_I2C, I2C_MUX_SLAVE_ADDRESS, i2c_tx_buffer, 1); Board_UART_Print("Opened Channel 1: "); Board_UART_PrintNum(tmp, 10, true); //Set Gas Gauge 1 to continuous data collection i2c_tx_buffer[0] = I2C_GG_CTRL_REG; i2c_tx_buffer[1] = I2C_GG_CONTINUOUS; tmp = Chip_I2C_MasterSend(DEFAULT_I2C, I2C_GG_SLAVE_ADDRESS, i2c_tx_buffer, 2); Board_UART_Print("Set I2C1 to continuous data collection: "); Board_UART_PrintNum(tmp, 10, true); while (1) { //Set PDM status based on CAN messages from Driver Interface if (!RingBuffer_IsEmpty(&can_rx_buffer)) { CCAN_MSG_OBJ_T temp_msg; RingBuffer_Pop(&can_rx_buffer, &temp_msg); //Test for DI OFF or SHUTDOWN IMPENDING message if((temp_msg.data[3] << 8 | temp_msg.data[2]) == ____DI_PACKET__DRIVE_STATUS__SHUTDOWN_IMPENDING || (temp_msg.data[3] << 8 | temp_msg.data[2]) == ____DI_PACKET__DRIVE_STATUS__OFF) { if(pdm_status.pdm_on) { pdm_status.pdm_on = false; Board_LED_Off(LED0); Board_I2C_Reset(I2C_GG_SLEEP, i2c_tx_buffer); } } else { if(!(pdm_status.pdm_on)) { pdm_status.pdm_on = true; Board_LED_On(LED0); Board_I2C_Reset(I2C_GG_CONTINUOUS, i2c_tx_buffer); } } } //Reset gas gauge 0 if it has been diconnected and then reconnected to power i2c_tx_buffer[0] = I2C_MUX_CHANNEL_0; tmp = Chip_I2C_MasterSend(DEFAULT_I2C, I2C_MUX_SLAVE_ADDRESS, i2c_tx_buffer, 1); //Open I2C Channel 0 tmp = Chip_I2C_MasterCmdRead(DEFAULT_I2C, I2C_GG_SLAVE_ADDRESS, I2C_GG_CTRL_REG, i2c_rx_buffer, 1); Board_UART_PrintNum(i2c_rx_buffer[0],16,true); if((uint16_t)i2c_rx_buffer[0] == I2C_GG_DEFAULT) { //Test for default values in control register if(pdm_status.pdm_on) { Board_I2C_Reset(I2C_GG_CONTINUOUS, i2c_tx_buffer); } else { Board_I2C_Reset(I2C_GG_SLEEP, i2c_tx_buffer); } //Send a heartbeat with a com error Board_CAN_SendHeartbeat(&pdm_status, &msg_obj, true); } //Reset gas gauge 1 if it has been diconnected and then reconnected to power i2c_tx_buffer[0] = I2C_MUX_CHANNEL_1; tmp = Chip_I2C_MasterSend(DEFAULT_I2C, I2C_MUX_SLAVE_ADDRESS, i2c_tx_buffer, 1); //Open 12C Channel 1 tmp = Chip_I2C_MasterCmdRead(DEFAULT_I2C, I2C_GG_SLAVE_ADDRESS, I2C_MUX_CHANNEL_0, i2c_rx_buffer, 1); Board_UART_PrintNum(i2c_rx_buffer[0],16,true); if((uint16_t)i2c_rx_buffer[0] == I2C_GG_DEFAULT) { //Test for default values in control register if(pdm_status.pdm_on) { Board_I2C_Reset(I2C_GG_CONTINUOUS, i2c_tx_buffer); } else { Board_I2C_Reset(I2C_GG_SLEEP, i2c_tx_buffer); } //Send a heartbeat with a com error Board_CAN_SendHeartbeat(&pdm_status, &msg_obj, true); } /* Update PDM and Debug LED code */ //Attempt to open I2C Channel 0 i2c_tx_buffer[0] = I2C_MUX_CHANNEL_0; mux_i2c = Chip_I2C_MasterSend(DEFAULT_I2C, I2C_MUX_SLAVE_ADDRESS, i2c_tx_buffer, 1); //Attempt to update Critical Systems PDM Struct cs_i2c = Board_PDM_Status_Update(&pdm_status, i2c_rx_buffer, true); //Attempt to open I2C Channel 1 i2c_tx_buffer[0] = I2C_MUX_CHANNEL_1; mux_i2c = Chip_I2C_MasterSend(DEFAULT_I2C, I2C_MUX_SLAVE_ADDRESS, i2c_tx_buffer, 1); //Attempt to update Low Voltage PDM struct lv_i2c = Board_PDM_Status_Update(&pdm_status, i2c_rx_buffer, false); //Run debug logic and update state Board_PDM_Status_Debug(&pdm_status, mux_i2c, cs_i2c, lv_i2c); if(msTicks - lastPrint > FREQ_THRESHOLD){ // 10 times per second lastPrint = msTicks; // Store the current time, to allow the process to be done in another 1/10 seconds Board_CAN_SendHeartbeat(&pdm_status, &msg_obj, false); } } }