/******************************************************************************* * Function Name: ServiceUSB ******************************************************************************** * Summary: This routine performs tasks that should be done soon after USB * enumeration is completed (configure DMA, initialize state machine etc). * When the USB configuration is changed, this routine reinitializes all * the USB endpoints as required by the application. * * Parameters: * void * * Return: * void * *******************************************************************************/ void ServiceUSB(void) { CYBIT macPC_flag=FALSE; if(USB_INTERFACE_INACTIVE == USBDeviceState) { USBDeviceState = USB_INIT_AFTER_ENUMERATION_REQUIRED; } /* Initialization sequence for every USB host enumeration event */ if(USBDeviceState == USB_INIT_AFTER_ENUMERATION_REQUIRED) { uint16 index = 0; USBDeviceState = USB_INIT_AFTER_ENUMERATION_COMPLETED; SetAppleDeviceAudioSource(AUDIO_SOURCE_DIGITAL); macPC_flag = IsMacPCConnected(); #if(USBFS_EP_MM == USBFS__EP_DMAAUTO) /* USER_CODE: [Audio Buffers] Add a separate for loop if the playback and recording audio buffer size are * not equal */ for(index=0; index< OUT_AUDIOMAXPKT; index++) { #ifndef ENABLE_DIGITAL_AUDIO_OUT_ONLY inRam[index] = 0; #endif #ifndef ENABLE_DIGITAL_AUDIO_IN_ONLY outRam[index] = 0; #endif } #ifndef ENABLE_DIGITAL_AUDIO_OUT_ONLY inCnt = IN_AUDIOMAXPKT; #endif #endif #ifdef CDC_ENABLED USBUARTStart(); /* Initializes the USB UART interface */ #endif /* Configure the HID input endpoint buffer for Mac/PC playlist control */ if(macPC_flag) { USBFS_LoadInEP(MAC_PC_HID_CONTROL_ENDPOINT, (uint8 *)&playlistControlReport, sizeof(playlistControlReport)); #ifdef PHONE_CONTROL_ENABLED USBFS_ReadOutEP(MAC_PC_HID_OUT_ENDPOINT, &hidOutReport, sizeof(hidOutReport)); USBFS_EnableOutEP(MAC_PC_HID_OUT_ENDPOINT); #endif } /* If Aux is not currently configured, then switch to digital audio mode */ if(IS_AUX_NOT_SELECTED()) { ConfigureDigitalAudioDMA(); } else { #ifdef LCD_MODULE_ENABLED /* Else Display Aux Configured message on the LCD */ LCD2LineDisplay("Analog Loopback ", " "); #endif } /* USER_CODE: [USB enumeration] placeholder for initializing custom user code after the USB host enumerates the * accessory. This routine will be called once per accessory connection after the host issues SET_CONFIGURATION * request */ } #ifdef MIDI_ENABLED if (midiEPInitialized == FALSE || usbReset) { /* Initialize MIDI only when a valid USB host is connected */ if ((IsUSBConfigured() && IsMacPCConnected())) { USBFS_MIDI_EP_Init(); /* USB Component internally sets the priority of the UART TX and RX ISRs to 4 and 2 respectively, change the * interrupt priority in the application code to match the system interrupt setup */ CyIntSetPriority(MIDI1_UART_TX_VECT_NUM, MIDI_UART_INTERRUPT_PRIORITY_SIX); CyIntSetPriority(MIDI1_UART_RX_VECT_NUM, MIDI_UART_INTERRUPT_PRIORITY_FIVE); #if (USBFS_MIDI_EXT_MODE >= USBFS_TWO_EXT_INTRF) CyIntSetPriority(MIDI2_UART_TX_VECT_NUM, MIDI_UART_INTERRUPT_PRIORITY_SIX); CyIntSetPriority(MIDI2_UART_RX_VECT_NUM, MIDI_UART_INTERRUPT_PRIORITY_FIVE); #endif midiEPInitialized = TRUE; usbReset = 0; } } if(USBFS_midiInPointer%USBFS_EVENT_LENGTH == 0 && USBFS_midiInPointer!=0) { if(midiInWaitTimer == 0) { midiInWaitTimer = MIDI_IN_EP_WAIT_TIME; USBFS_MIDI_IN_EP_Service(); } } else { midiInWaitTimer = MIDI_IN_EP_WAIT_TIME; } #endif /* USBFS_IsConfigurationChanged() is a clear on read status update therefore, only one read of * USBFS_IsConfigurationChanged() should ever exist in user code */ if(USBFS_IsConfigurationChanged()) { macPC_flag = IsMacPCConnected(); #ifndef ENABLE_DIGITAL_AUDIO_IN_ONLY /* Get Alternate setting */ altSetting[AUDIO_OUT_INTERFACE_INDEX] = (macPC_flag? USBFS_GetInterfaceSetting(1):USBFS_GetInterfaceSetting(2)); /* ByteSwap control register bit is set to 1 if alt setting 2 is selected so that * Byte swap digital logic processes data as 16 bits. ByteSwap control register is set to 0 * if alt setting 1 is selected and byte swap processes data as 24 bits */ if (altSetting[AUDIO_OUT_INTERFACE_INDEX]==ALT_SETTING_ACTIVE_24_BIT) { ByteSwap_Tx_CONTROL_REG = ByteSwap_Tx_CONTROL_REG & (~ByteSwap_Tx_RES_CTRL_16); } else if (altSetting[AUDIO_OUT_INTERFACE_INDEX]==ALT_SETTING_ACTIVE_16_BIT) { ByteSwap_Tx_CONTROL_REG = ByteSwap_Tx_CONTROL_REG | ByteSwap_Tx_RES_CTRL_16; } /* Arming the audio out EP if it is not zero bandwidth alt setting */ if (altSetting[AUDIO_OUT_INTERFACE_INDEX]!= ALT_SETTING_ZERO_BANDWIDTH && (CY_GET_REG8(USBFS_SIE_EP1_CR0_PTR + ((AUDIO_OUT_ENDPOINT - USBFS_EP1) << USBFS_EPX_CNTX_ADDR_SHIFT)) & USBFS_MODE_MASK) == USBFS_MODE_NAK_IN_OUT) { /* Init DMA configurations for audio OUT EP */ USBFS_ReadOutEP(AUDIO_OUT_ENDPOINT, &outRam[0], OUT_AUDIOMAXPKT); USBFS_EnableOutEP(AUDIO_OUT_ENDPOINT); } #endif #ifndef ENABLE_DIGITAL_AUDIO_OUT_ONLY #ifndef ENABLE_DIGITAL_AUDIO_IN_ONLY if(altSetting[AUDIO_IN_INTERFACE_INDEX] != (macPC_flag? USBFS_GetInterfaceSetting(2):USBFS_GetInterfaceSetting(3))) { altSetting[AUDIO_IN_INTERFACE_INDEX] = (macPC_flag? USBFS_GetInterfaceSetting(2):USBFS_GetInterfaceSetting(3)); #else if(altSetting[AUDIO_IN_INTERFACE_INDEX] != (macPC_flag? USBFS_GetInterfaceSetting(1):USBFS_GetInterfaceSetting(2))) { altSetting[AUDIO_IN_INTERFACE_INDEX] = (macPC_flag? USBFS_GetInterfaceSetting(1):USBFS_GetInterfaceSetting(2)); #endif /* Setting the ByteSwap control register bit to 0 regardless of alt setting is selected. Because audio in * interface both the alternate settings alt setting1 and alt setting 2 both use 3 byte subframe size. */ ByteSwap_Rx_CONTROL_REG = ByteSwap_Rx_CONTROL_REG & (~ByteSwap_Rx_RES_CTRL_16); /* Arming the audio in EP if it is not zero bandwidth alt setting */ if (altSetting[AUDIO_IN_INTERFACE_INDEX]!= ALT_SETTING_ZERO_BANDWIDTH && (CY_GET_REG8(USBFS_SIE_EP1_CR0_PTR + ((AUDIO_IN_ENDPOINT - USBFS_EP1) << USBFS_EPX_CNTX_ADDR_SHIFT)) & USBFS_MODE_MASK) == USBFS_MODE_NAK_IN_OUT) { /* Init DMA configurations for audio IN EP */ inCnt = IN_AUDIOMAXPKT; USBFS_LoadInEP(AUDIO_IN_ENDPOINT, &inRam[0], inCnt); /* Pre-arm first audio IN request */ USBFS_LoadInEP(AUDIO_IN_ENDPOINT, USBFS_NULL, inCnt); } } #endif /* USER_CODE: [USB configuration changed] Placeholder for adding additional USB endpoint initialization code * when the host issues either a SET_INTERFACE or SET_CONFIGURATION request to the accessory. After receiving * a SET_INTERFACE request from the host, the endpoint belonging to the alternate setting being configured * by the USB host is reset and must be reinitialized here for proper operation of the USB block */ } } /******************************************************************************* * Function Name: ServiceUSBSuspend ******************************************************************************** * Summary: * This function handles USB suspend event from USB host and forces PSoC 3 * to enter low power mode. Once the USB resume event is detected, PSoC3 * wakes up and starts operating in normal mode. * * Parameters: * void * * Return: * void * *******************************************************************************/ #ifdef HANDLE_USB_SUSPEND void ServiceUSBSuspend(void) { if(!IsMacPCConnected() || ! USBFS_initVar) { return; } /* Check if the host is active */ if(USBFS_bCheckActivity() != 0 ) { usbActivityCounter = 0; } else { usbActivityCounter++; } /* USB Suspend event is lack of greater than 3 consecutive SOF's */ if(usbActivityCounter > USB_SUSPEND_TIME_TICKS ) { /* The debounce delay is taken care by increasing the suspend time to 40ms (5 * 8ms) */ if(IsMacPCConnected() && IsUSBConfigured()) { /* USER_CODE: [USB suspend] Placeholder for configuring ALL the additional components added by the user in * sleep mode before calling USB suspend/PSoC 3 sleep API */ #ifdef LCD_MODULE_ENABLED LCD_Position(0,0); LCD_PrintString(" USB Suspend "); #endif /* If the accessory is not in low power mode, enter low power mode on seeing a USB suspend */ if(!lowPowerIdle) { lowPowerIdle = TRUE; StopAudioComponents(); /* Changes to 24 MHz IMO for USB */ StopAnalogAudioComponents(); /* Turn OFF Analog path for Audio-In/iPod Analog */ } if(!midiPowerIdle) { if(!lowPowerIdle) { StopAudioComponents(); /* Changes to 24 MHz IMO for USB */ } CyPins_SetPin(PSOC_MIDI_PWR_0); /* Turn off the MIDI I/O hardware */ midiPowerIdle = TRUE; /* MIDI low power mode enabled */ } CyPins_ClearPin(PSOC_CODEC_PWR_0); /* Turn off the regulator to reduce suspend mode current */ USBFS_Suspend(); I2C_Master_Sleep(); /* Configure I2C master block in sleep mode */ #ifdef CAPSENSE_ENABLED while(CapSense_IsBusy()); /* Wait for current scan to complete */ CapSense_Sleep(); #endif #ifdef MIDI_ENABLED MIDI1_UART_Sleep(); #if (USBFS_MIDI_EXT_MODE >= USBFS_TWO_EXT_INTRF) MIDI2_UART_Sleep(); #endif #endif CyPmSaveClocks(); CyPmSleep(PM_SLEEP_TIME_NONE, PM_SLEEP_SRC_PICU); /* PSoC 3 is in sleep mode */ CyPmRestoreClocks(); USBFS_Resume(); I2C_Master_Wakeup(); #ifdef CAPSENSE_ENABLED CapSense_Wakeup(); CapSense_IntClock_SetSource(CYCLK_SRC_SEL_IMO); #endif #ifdef MIDI_ENABLED MIDI1_UART_Wakeup(); #if (USBFS_MIDI_EXT_MODE >= USBFS_TWO_EXT_INTRF) MIDI2_UART_Wakeup(); #endif #endif CyPins_SetPin(PSOC_CODEC_PWR_0); /* Turn ON the CODEC regulator after wake up */ #ifdef WINDOWS7_WORKAROUND if(USBFS_GetConfiguration() != 0) { USBFS_configurationChanged = USBFS_TRUE; USBFS_Config(USBFS_FALSE); } #endif #ifdef LCD_MODULE_ENABLED LCD_Position(0,0); LCD_PrintString("Mac/PC Interface"); #endif /* USER_CODE: [USB wakeup] Placeholder for re-configuring ALL the additional components added by the user in * wakeup mode after calling USB wakeup */ } usbActivityCounter = 0; /* After coming out of USB suspend, MIDI end point should be re-initialized */ midiEPInitialized = 0; } } #endif /******************************************************************************* * Function Name: HandlePCMacUSBInterface ******************************************************************************** * Summary: Checks if PC/Mac is connected/disconnected and start the USB component * * Parameters: * void * * Return: * void * *******************************************************************************/ void HandlePCMacUSBInterface(void) { /* If Aux mode is enabled, then Mac/PC connection disconnection is handled only when the system is not in * aux In mode. For self powered case, Apple device connection is also checked before starting the Mac/PC * interface */ if(IS_AUX_NOT_SELECTED() && !USBFS_initVar && IsMacPCConnected()) { /* Switch the PSoC USB D+ and D- lines to USB Mini B */ CyPins_ClearPin(PSOC_USB_SEL_0); /* Start the USB component when PC/Mac is connected */ USBFS_Start(PC_MAC_AUDIO_WITH_VOLUME_DEVICE, USBFS_DWR_VDDD_OPERATION); USBDeviceState = USB_INIT_AFTER_ENUMERATION_REQUIRED; #ifdef LCD_MODULE_ENABLED if(IS_AUX_NOT_SELECTED()) { LCD2LineDisplay("Mac/PC Interface", " "); } #endif #ifdef ENABLE_VOLUME_CONTROL currentLCDVolume--; /* dirty the LCD volume and mute update flag to update volume and mute info on the LCD */ currentLCDMute--; #endif usbMiniBActive = TRUE; /* USER_CODE: [Mac/PC connection] Placeholder for initializing components or external peripherals when the * accessory is plugged into Mac/PC (USB VBus = High) */ } /* Check for PC/Mac USB Audio device disconnection in self powered mode. * In device powered mode project, no need of checking disconnection event as power is shut off * as soon as USB cable is disconnected from USB mini connector. */ else if(usbMiniBActive && (USBFS_bGetConfiguration() || USBDeviceState == USB_INIT_AFTER_ENUMERATION_REQUIRED)) { /* If VBUS voltage from mini B is now gone and was previous present then stop USB interface */ if(!IsMacPCConnected()) { if(USBFS_initVar) { USBFS_Stop(); } CyPins_SetPin(PSOC_USB_SEL_0); /* Switch the PSoC USB D+ and D- lines back to Apple device */ /* If Aux was not configured when PC is unplugged, then switch off CODEC */ if(!auxConfigured) { CyPins_ClearPin(PSOC_CODEC_RST_0); /* Hold CODEC in reset */ codecInit = FALSE; } usbMiniBActive = FALSE; /* USER_CODE: [Mac/PC disconnection] Placeholder for shutting down components or external peripherals when * the accessory is disconnected from Mac/PC (USB VBus transitioned from High to Low) */ } } } /******************************************************************************* * Function Name: EnableNAKBulkIN ******************************************************************************** * Summary: Enables the NAK interrupt on a USB endpoint * * Parameters: * Endpoint number for which NAK interrupt is to be enabled * * Return: * void * *******************************************************************************/ void EnableNAKBulkIN(uint8 bEPNumber) { uint8 index = (bEPNumber - USBFS_EP1) << USBFS_EPX_CNTX_ADDR_SHIFT; if((CY_GET_REG8(&USBFS_SIE_EP1_CR0_PTR[index]) & USBFS_MODE_MASK) != USBFS_MODE_ACK_IN) { CY_SET_REG8(&USBFS_SIE_EP1_CR0_PTR[index], CY_GET_REG8(&USBFS_SIE_EP1_CR0_PTR[index]) | NAK_IN_INTERRUPT_ENABLE_MASK); } }
void main() { /* Place your initialization/startup code here (e.g. MyInst_Start()) */ CyGlobalIntEnable; /* Uncomment this line to enable global interrupts. */ CyDelay(5u); // Short delay to make sure device is ready for low power entry sleep_isr_StartEx(Wakeup_ISR); // Start Sleep ISR SleepTimer_Start(); // Start SleepTimer Compnent modem_set_api_feed(FEED_ID, API_KEY); //ADC_SAR_1_Start(); //modem_start(); //modem_power_on(); //modem_get_serial_number(); // Check/set flow control: Change from 3 (default - bidirectional) to 0 (no flow control) --> Did NOT fix ability to connect using AT#SD /* modem_set_flow_control(0u); modem_get_profile(); // modem_get_sw_version(); modem_set_error_reports(2u); */ // Init and enable CDMA /* modem_set_user(""); modem_set_password(""); modem_test_CDMA_data(); modem_enable_CDMA_data(1u); modem_test_CDMA_data(); */ // Initialize variables ready = 1u; loops = 0u; lock = 0u; packet_ready = 0u; iter = 0; sign = 1; packet_len = 0u; wakeup_interval_counter = 0u; v_out = 0.0; VBAT_READ_EN_Write(0u); Pin2_Write(0u); Pin17_Write(0u); Pin18_Write(0u); Pin37_Write(0u); Pin38_Write(0u); Pin39_Write(0u); //Pin34_Write(0u); //Pin35_Write(0u); // WaveDAC8_1_Start(); /* Start WaveDAC8 */ for(;;) { if ( ready == 1u ){ iter = 0; if (clear_packet(data_packet)) { packet_len = 0u; packet_ready = 0u; } blink_LED(3u); if (ultrasonic_get_reading(&ultrasonic_reading)){ sprintf(data_packet,"%s%s, %d\r\n", data_packet, "depth_sonic", (uint16) (ultrasonic_reading.depth)); } blink_LED(3u); // Set up the modem and connect to network LED_Write(!LED_Read()); if (modem_startup()) { //modem_get_serial_number(); // Send a packet LED_Write(!LED_Read()); sprintf(data_packet,"%s%s, %u\r\n", data_packet, "tmp", loops); ready = modem_send_packet(data_packet); CyDelay(5000u); if (clear_packet(data_packet)) { packet_len = 0u; packet_ready = 0u; } // Request a packet LED_Write(!LED_Read()); if( modem_get_packet(data_packet,"lock,tmp") ){ // Code to test moving the actuator if(packet_get_uint8(data_packet,"lock",&lock)){ if(lock == 1u){ // Supply power to breakout board // This powers the Reed Switch Pin2_Write(1u); CyDelay(1000u); // Move the actuator out Pin17_Write(1u); CyDelay(3000u); Pin17_Write(0u); CyDelay(2000u); // Move the actuator in Pin18_Write(1u); CyDelay(3000u); Pin18_Write(0u); // Cut power to breakout board CyDelay(1000u); Pin2_Write(0u); } } } /* modem_power_off(); modem_stop(); */ } modem_shutdown(); loops += 10; ready = 0u; } /* If not ready, update the counter for the sleep timer */ else { /* Blink the LED to indicate sleeping */ blink_LED(5u); /* Prepares system clocks for the Sleep mode */ CyPmSaveClocks(); do { /******************************************************************* * Switch to the Sleep Mode for the other devices: * - PM_SLEEP_TIME_NONE: wakeup time is defined by Sleep Timer * - PM_SLEEP_SRC_CTW : wakeup on CTW sources is allowed *******************************************************************/ CyPmSleep(PM_SLEEP_TIME_NONE, PM_SLEEP_SRC_CTW); /*********************************************************************** * After the device is woken up the Sleep Timer's ISR is executed. * Afterwards the CyPmSleep() execution is finished the clock * configuration is restored. ***********************************************************************/ if(SLEEPTIMER_INTERVAL_COUNTER == wakeup_interval_counter) { /* Blink with LED */ //(0u == LED_P1_2_Read()) ? LED_P1_2_Write(1u) : LED_P1_2_Write(0u); wakeup_interval_counter = 0u; ready = 1u; } else { wakeup_interval_counter++; } } while (wakeup_interval_counter != 0u); /* Restore clock configuration */ CyPmRestoreClocks(); } /* // Place your application code here. Pin37_Write(!Pin37_Read()); clear_packet(data_packet); sprintf(data_packet,"%s, %u\r\n", "tmp", iter); modem_connect(); LED_Write(!LED_Read()); // modem_state should now be IDLE or READY //modem_send_packet(data_packet); modem_get_google(); //Can't get to work if (tmp > 10) { sign = -1; } else if (tmp < -10) { sign = 1; } tmp = tmp + sign; Pin37_Write(!Pin37_Read()); LED_Write(!LED_Read()); CyDelay(100u); modem_get_time(); Pin38_Write(!Pin38_Read()); LED_Write(!LED_Read()); CyDelay(100u); modem_get_signal_quality(); Pin39_Write(!Pin39_Read()); LED_Write(!LED_Read()); CyDelay(100u); modem_check_network(); //Pin34_Write(!Pin34_Read()); LED_Write(!LED_Read()); CyDelay(100u); modem_get_serial_number(); //modem_disconnect(); //Pin35_Write(!Pin35_Read()); LED_Write(!LED_Read()); CyDelay(100u); ADC_SAR_1_StartConvert(); ADC_SAR_1_IsEndConversion(ADC_SAR_1_WAIT_FOR_RESULT); v_out = ADC_SAR_1_CountsTo_Volts(ADC_SAR_1_GetResult16()); //v_out = Pin30_Read(); Pin25_Write(v_out); */ } /* modem_power_off(); modem_stop(); blink_LED(3u); CyPmSaveClocks(); CyPmHibernate(); */ }