/* void i2C0_IRQHandler (void) { if (Chip_I2C_IsMasterActive(I2C0)) { Chip_I2C_MasterStateHandler(I2C0); } else { Chip_I2C_SlaveStateHandler(I2C0); } } */ void Board_I2C_Master_Init(void) { Board_I2C_Init(I2C0); /* Initialize I2C */ Chip_I2C_Init(I2C0); Chip_I2C_SetClockRate(I2C0, 100000); /* Set default mode to poll */ i2c_set_mode(I2C0, 1); }
/* Initialize the I2C bus */ static void i2c_app_init(I2C_ID_T id, int speed) { Board_I2C_Init(id); /* Initialize I2C */ Chip_I2C_Init(id); Chip_I2C_SetClockRate(id, speed); /* Set default mode to interrupt */ i2c_set_mode(id, 0); }
static void initHardware(void) { // Read clock settings and update SystemCoreClock variable SystemCoreClockUpdate(); // Set up and initialize all required blocks and // functions related to the board hardware Board_Init(); // Set the LED to the state of "Off" Board_LED_Set(0, false); Board_I2C_Init(I2C1); Chip_I2C_SetClockRate(I2C1, 100000); Chip_I2C_SetMasterEventHandler(I2C1, Chip_I2C_EventHandlerPolling); }
/* WM8904 initialize function */ int WM8904_Init(int input) { I2C_EVENTHANDLER_T old = Chip_I2C_GetMasterEventHandler(WM8904_I2C_BUS); int ret; /* Initialize I2C */ Board_I2C_Init(WM8904_I2C_BUS); Chip_I2C_Init(WM8904_I2C_BUS); Chip_I2C_SetClockRate(WM8904_I2C_BUS, 100000); Chip_I2C_SetMasterEventHandler(WM8904_I2C_BUS, Chip_I2C_EventHandlerPolling); /* Initialize the default values */ ret = Audio_Codec_SetDefaultValues((void *)&g_wm8904[0], sizeof(g_wm8904)/sizeof(WM8904_Init_Seq_t)); #if 0 if (ret) { ret = Audio_Codec_SetDefaultValues(UDA_interfil_regs_dat, sizeof(UDA_interfil_regs_dat)); } if (ret) { ret = Audio_Codec_SetDefaultValues(UDA_decimator_regs_dat, sizeof(UDA_decimator_regs_dat)); } if (ret && input) { /* Disable Power On for ADCR, PGAR, PGAL to get mic sound more clearly */ ret = UDA1380_REG_WriteVerify(UDA_POWER_CTRL, UDA1380_REG_PWRCTRL_DEFAULT_VALUE & (~(0x0B))); if (ret) { ret = UDA1380_REG_WriteVerify(UDA_ADC_CTRL, UDA1380_REG_ADC_DEFAULT_VALUE | input); } } #endif Chip_I2C_SetMasterEventHandler(WM8904_I2C_BUS, old); return ret; }
static void initHardware(void) { #if defined (__USE_LPCOPEN) #if !defined(NO_BOARD_LIB) // Read clock settings and update SystemCoreClock variable SystemCoreClockUpdate(); // Set up and initialize all required blocks and // functions related to the board hardware Board_Init(); // Set the LED to the state of "Off" Board_LED_Set(0, false); #endif #endif Board_I2C_Init(I2C1); /* pines del stick */ Chip_IOCON_PinMux(LPC_IOCON, 0, 0, IOCON_MODE_INACT, IOCON_FUNC3); Chip_IOCON_PinMux(LPC_IOCON, 0, 1, IOCON_MODE_INACT, IOCON_FUNC3); Chip_IOCON_EnableOD(LPC_IOCON, 0, 0); Chip_IOCON_EnableOD(LPC_IOCON, 0, 1); Chip_I2C_SetClockRate(I2C1, 100000); Chip_I2C_SetMasterEventHandler(I2C1, Chip_I2C_EventHandlerPolling); }
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); } } }
/****************************************************************************** * @brief 小板初始化 * @param * @retval TRUE: 初始化成功 FALSE: 初始化失败 ******************************************************************************/ Std_ReturnType Board_Init(void) { Board_MCO_Init(); Board_I2C_Init(); return TRUE; }