void hardware_init(void) { /* enable clock for PORTs */ CLOCK_SYS_EnablePortClock(PORTA_IDX); CLOCK_SYS_EnablePortClock(PORTB_IDX); CLOCK_SYS_EnablePortClock(PORTC_IDX); configure_i2c_pins(0); configure_i2c_pins(1U); /* Init board clock */ BOARD_ClockInit(); dbg_uart_init(); }
void hardware_init(void) { uint8_t i; /* enable clock for PORTs */ for (i = 0; i < HW_PORT_INSTANCE_COUNT; i++) { CLOCK_SYS_EnablePortClock(i); } /* Setup board clock source. */ g_xtal0ClkFreq = 50000000U; // g_xtalRtcClkFreq = 32768U; for (i = 0; i < HW_PORT_INSTANCE_COUNT; i++) { configure_gpio_pins(i); } configure_i2c_pins(0);//FXO8700 & L3G4200d // configure_adc_pins_for_quadcopter(); configure_remote_control_pins_for_quadcopter() ; configure_sensors_interrupt_pins_for_quadcopter(); configure_ftm_pins_for_quadcopter(); configure_uart_pins(BOARD_DEBUG_UART_INSTANCE); }
/*! * @brief The i2c slave * The function runs i2c slave with interrupt active mode. Slave receive data from * master and echo back to master */ void main(void) { // Number byte data will be transfer uint32_t count = 0; // Buffer store data to transfer uint8_t dataBuff[100] = {0}; // state of slave i2c_slave_state_t slave; // user configuration i2c_slave_user_config_t userConfig = { .address = 0x7FU, .slaveCallback = NULL, .callbackParam = NULL, .slaveListening = false, #if FSL_FEATURE_I2C_HAS_START_STOP_DETECT .startStopDetect = false, #endif #if FSL_FEATURE_I2C_HAS_STOP_DETECT .stopDetect = false, #endif }; // Initialize hardware hardware_init(); // Configure pin for i2c slave configure_i2c_pins(BOARD_I2C_COMM_INSTANCE); // Initialize uart to debug dbg_uart_init(); // Initialize OSA OSA_Init(); printf("Slave is running ..."); // Initialize slave I2C_DRV_SlaveInit(BOARD_I2C_COMM_INSTANCE, &userConfig, &slave); // Loop transfer while(1) { // Slave receive 1 byte from master I2C_DRV_SlaveReceiveDataBlocking(BOARD_I2C_COMM_INSTANCE, (uint8_t*)&count, 1, OSA_WAIT_FOREVER); // Slave receive buffer from master I2C_DRV_SlaveReceiveDataBlocking(BOARD_I2C_COMM_INSTANCE, dataBuff, count, 1000); // Slave send buffer received from master I2C_DRV_SlaveSendDataBlocking(BOARD_I2C_COMM_INSTANCE, dataBuff, count, 1000); } }
/*! * @brief The i2c slave * The function runs i2c slave with polling mode (HAL layer). Slave receive data * from master and echo back to master */ void main(void) { // Number byte data will be transfer uint32_t count = 0; // Buffer store data to transfer uint8_t dataBuff[50] = {0}; // slave address uint16_t address = 0x7FU; // i2c slave base address I2C_Type * baseAddr = (I2C_Type*)I2C0_BASE; // Initialize hardware hardware_init(); // Configure pin for i2c slave configure_i2c_pins(BOARD_I2C_COMM_INSTANCE); // Initialize uart to debug dbg_uart_init(); printf("Slave is running ..."); /* Enable clock for I2C.*/ CLOCK_SYS_EnableI2cClock(BOARD_I2C_COMM_INSTANCE); /* Init instance to known state. */ I2C_HAL_Init(baseAddr); /* Set slave address.*/ I2C_HAL_SetAddress7bit(baseAddr, address); /* Enable the peripheral operation.*/ I2C_HAL_Enable(baseAddr); // Loop transfer while(1) { // count is length of string I2C_HAL_SlaveReceiveDataPolling(baseAddr, (uint8_t*)&count, 1); // Slave receive buffer from master I2C_HAL_SlaveReceiveDataPolling(baseAddr, dataBuff, count); // Slave send buffer received from master I2C_HAL_SlaveSendDataPolling(baseAddr, dataBuff, count); } }
void hardware_init(void) { /* Board specific RDC settings */ BOARD_RdcInit(); /* Board specific clock settings */ BOARD_ClockInit(); /* initialize debug uart */ dbg_uart_init(); configure_pwm_pins(PWM2); configure_pwm_pins(PWM3); RDC_SetPdapAccess(RDC, rdcPdapPwm2, 3 << (BOARD_DOMAIN_ID * 2), false, false); RDC_SetPdapAccess(RDC, rdcPdapPwm3, 3 << (BOARD_DOMAIN_ID * 2), false, false); /* Configure GPIOS */ configure_platform_gpio(); /* Enable Interrupts */ NVIC_EnableIRQ(BOARD_ENC_IRQ); /* In this example, we need to grasp board I2C exclusively */ RDC_SetPdapAccess(RDC, BOARD_I2C_RDC_PDAP, 3 << (BOARD_DOMAIN_ID * 2), false, false); /* Select I2C clock derived from OSC clock(24M) */ CCM_UpdateRoot(CCM, BOARD_I2C_CCM_ROOT, ccmRootmuxI2cOsc24m, 0, 0); /* Enable I2C clock */ CCM_EnableRoot(CCM, BOARD_I2C_CCM_ROOT); CCM_ControlGate(CCM, BOARD_I2C_CCM_CCGR, ccmClockNeededRunWait); /* I2C Pin setting */ configure_i2c_pins(BOARD_I2C_BASEADDR); /* RDC MU*/ RDC_SetPdapAccess(RDC, BOARD_MU_RDC_PDAP, 3 << (BOARD_DOMAIN_ID * 2), false, false); /* Enable clock gate for MU*/ CCM_ControlGate(CCM, BOARD_MU_CCM_CCGR, ccmClockNeededRun); }
/*! * @brief main function */ int main(void) { uint8_t i; uint8_t index, indexChar, value; uint8_t cmdBuff[1] = {0xFF}; uint8_t sendBuff[1] = {0xFF}; // save data sent to i2c slave uint8_t receiveBuff[1] = {0xFF}; // save data received from i2c slave i2c_master_state_t master; i2c_status_t returnValue; i2c_device_t slave = { .address = 0x3A, .baudRate_kbps = 100 }; hardware_init(); dbg_uart_init(); // Configure I2C pins configure_i2c_pins(BOARD_I2C_COMM_INSTANCE); OSA_Init(); GPIO_DRV_Init(0, ledPins); // Init I2C module I2C_DRV_MasterInit(BOARD_I2C_COMM_INSTANCE, &master); printf("\r\n====== I2C Master ======\r\n\r\n"); OSA_TimeDelay(500); LED_toggle_master(); OSA_TimeDelay(500); LED_toggle_master(); OSA_TimeDelay(500); LED_toggle_master(); OSA_TimeDelay(500); LED_toggle_master(); while (1) { printf("\r\nI2C Master reads values from I2C Slave sub address:\r\n"); printf("\r\n------------------------------------"); printf("\r\nSlave Sub Address | Character "); printf("\r\n------------------------------------"); for (i=Subaddress_Index_0; i<Invalid_Subaddress_Index; i++) { cmdBuff[0] = i; returnValue = I2C_DRV_MasterReceiveDataBlocking( BOARD_I2C_COMM_INSTANCE, &slave, cmdBuff, 1, receiveBuff, sizeof(receiveBuff), 500); if (returnValue == kStatus_I2C_Success) { printf("\r\n[%d] %c", i, receiveBuff[0]); } else { printf("\r\nI2C communication failed, error code: %d", returnValue); } } printf("\r\n------------------------------------"); printf("\r\n"); printf("\r\nPlease input Slave sub address and the new character."); do { printf("\r\nSlave Sub Address: "); indexChar = getchar(); putchar(indexChar); printf("\r\nInput New Character: "); value = getchar(); putchar(value); printf("\n"); index = (uint8_t)(indexChar - '0'); if (index >= Invalid_Subaddress_Index) { printf("\r\nInvalid Sub Address."); } } while (index >= Invalid_Subaddress_Index); cmdBuff[0] = index; sendBuff[0] = value; returnValue = I2C_DRV_MasterSendDataBlocking( BOARD_I2C_COMM_INSTANCE, &slave, cmdBuff, 1, sendBuff, sizeof(sendBuff), 500); if (returnValue != kStatus_I2C_Success) { printf("\r\nI2C communication failed, error code: %d", returnValue); } } }