/****************************************************************************** * FunctionName : pwm_period_timer * Description : pwm period timer function, output high level, * start each channel's high level timer * Parameters : NONE * Returns : NONE *******************************************************************************/ LOCAL void ICACHE_RAM_ATTR pwm_tim1_intr_handler(void) { uint8 local_toggle = pwm_toggle; // pwm_toggle may change outside RTC_CLR_REG_MASK(FRC1_INT_ADDRESS, FRC1_INT_CLR_MASK); if (pwm_current_channel >= (*pwm_channel - 1)) { // *pwm_channel may change outside pwm_single = pwm_single_toggle[local_toggle]; pwm_channel = &pwm_channel_toggle[local_toggle]; gpio_output_set(pwm_single[*pwm_channel - 1].gpio_set, pwm_single[*pwm_channel - 1].gpio_clear, pwm_gpio, 0); pwm_current_channel = 0; if (*pwm_channel != 1) { RTC_REG_WRITE(FRC1_LOAD_ADDRESS, pwm_single[pwm_current_channel].h_time); } else { pwm_timer_down = 1; } } else { gpio_output_set(pwm_single[pwm_current_channel].gpio_set, pwm_single[pwm_current_channel].gpio_clear, pwm_gpio, 0); pwm_current_channel++; RTC_REG_WRITE(FRC1_LOAD_ADDRESS, pwm_single[pwm_current_channel].h_time); } }
/****************************************************************************** * FunctionName : pwm_init * Description : pwm gpio, params and timer initialization * Parameters : uint16 freq : pwm freq param * uint16 *duty : each channel's duty * Returns : NONE *******************************************************************************/ void ICACHE_FLASH_ATTR pwm_init(uint16 freq, uint16 *duty) { uint8 i; RTC_REG_WRITE(FRC1_CTRL_ADDRESS, //FRC2_AUTO_RELOAD| DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); RTC_REG_WRITE(FRC1_LOAD_ADDRESS, 0); // PIN_FUNC_SELECT(PWM_0_OUT_IO_MUX, PWM_0_OUT_IO_FUNC); // PIN_FUNC_SELECT(PWM_1_OUT_IO_MUX, PWM_1_OUT_IO_FUNC); // PIN_FUNC_SELECT(PWM_2_OUT_IO_MUX, PWM_2_OUT_IO_FUNC); // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_0_OUT_IO_NUM), 0); // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_1_OUT_IO_NUM), 0); // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_2_OUT_IO_NUM), 0); for (i = 0; i < PWM_CHANNEL; i++) { // pwm_gpio |= (1 << pwm_out_io_num[i]); pwm_gpio = 0; pwm.duty[i] = 0; } pwm_set_freq(500, 0); // pwm_set_freq_duty(freq, duty); pwm_start(); ETS_FRC_TIMER1_INTR_ATTACH(pwm_tim1_intr_handler, NULL); TM1_EDGE_INT_ENABLE(); ETS_FRC1_INTR_ENABLE(); }
/****************************************************************************** * FunctionName : pwm_period_timer * Description : pwm period timer function, output high level, * start each channel's high level timer * Parameters : NONE * Returns : NONE *******************************************************************************/ LOCAL void pwm_tim1_intr_handler(void) { RTC_CLR_REG_MASK(FRC1_INT_ADDRESS, FRC1_INT_CLR_MASK); if (pwm_current_channel == (*pwm_channel - 1)) { pwm_single = pwm_single_toggle[pwm_toggle]; pwm_channel = &pwm_channel_toggle[pwm_toggle]; gpio_output_set(pwm_single[*pwm_channel - 1].gpio_set, pwm_single[*pwm_channel - 1].gpio_clear, pwm_gpio, 0); pwm_current_channel = 0; if (*pwm_channel != 1) { RTC_REG_WRITE(FRC1_LOAD_ADDRESS, pwm_single[pwm_current_channel].h_time); } else { pwm_timer_down = 1; } } else { gpio_output_set(pwm_single[pwm_current_channel].gpio_set, pwm_single[pwm_current_channel].gpio_clear, pwm_gpio, 0); pwm_current_channel++; RTC_REG_WRITE(FRC1_LOAD_ADDRESS, pwm_single[pwm_current_channel].h_time); } }
void hw_timer_init(uint8 req) { if (req == 1) { RTC_REG_WRITE(FRC1_CTRL_ADDRESS, FRC1_AUTO_LOAD | DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); } else { RTC_REG_WRITE(FRC1_CTRL_ADDRESS, DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); } _xt_isr_attach(ETS_FRC_TIMER1_INUM, hw_timer_isr_cb, NULL); TM1_EDGE_INT_ENABLE(); _xt_isr_unmask(1 << ETS_FRC_TIMER1_INUM); }
/****************************************************************************** * FunctionName : hw_timer_init * Description : initilize the hardware isr timer * Parameters : FRC1_TIMER_SOURCE_TYPE source_type: FRC1_SOURCE, timer use frc1 isr as isr source. NMI_SOURCE, timer use nmi isr as isr source. u8 req: 0, not autoload, 1, autoload mode, * Returns : NONE *******************************************************************************/ void ICACHE_FLASH_ATTR hw_timer_init(FRC1_TIMER_SOURCE_TYPE source_type, u8 req) { if (req == 1) { RTC_REG_WRITE(FRC1_CTRL_ADDRESS, FRC1_AUTO_LOAD | DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); } else { RTC_REG_WRITE(FRC1_CTRL_ADDRESS, DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); } if (source_type == NMI_SOURCE) { ETS_FRC_TIMER1_NMI_INTR_ATTACH(hw_timer_isr_cb); } else { ETS_FRC_TIMER1_INTR_ATTACH(hw_timer_isr_cb, NULL); } TM1_EDGE_INT_ENABLE(); ETS_FRC1_INTR_ENABLE(); }
void ICACHE_FLASH_ATTR StartHPATimer() { RTC_REG_WRITE(FRC1_CTRL_ADDRESS, FRC1_AUTO_RELOAD| DIVDED_BY_16 | //5MHz main clock. FRC1_ENABLE_TIMER | TM_EDGE_INT ); RTC_REG_WRITE(FRC1_LOAD_ADDRESS, 5000000/DFREQ); RTC_REG_WRITE(FRC1_COUNT_ADDRESS, 5000000/DFREQ); //pwm_set_freq_duty(freq, duty); //pwm_start(); // RTC_REG_WRITE(FRC1_LOAD_ADDRESS, local_single[0].h_time); ETS_FRC_TIMER1_INTR_ATTACH(timerhandle, NULL); TM1_EDGE_INT_ENABLE(); ETS_FRC1_INTR_ENABLE(); system_timer_reinit(); hs_adc_start(); }
void gpioCallback(void *arg) { uint16 gpio_status = 0; static uint32 currTicks = 0; static uint32 prevTicks = 0; /* get the GPIO interrupt status */ gpio_status = GPIO_REG_READ( GPIO_STATUS_ADDRESS ); /* clear the interrupt */ GPIO_REG_WRITE( GPIO_STATUS_W1TC_ADDRESS, gpio_status); /* did GPIO 12 (connected to IR receiver) generate the ISR? */ if( gpio_status == BIT(12) ) { /* yes, and is it the first edge of the IR message frame? */ if ( edgeIndex == 0 ) { /* yes, then store counter value */ prevTicks = RTC_REG_READ(FRC1_COUNT_ADDRESS); /* and start the HW TIMER */ RTC_REG_WRITE(FRC1_CTRL_ADDRESS, DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); /* reset relevant variables */ minInterval = 0xFFFFFFFF; // initialize to a very big number rawIrMsgLen = 0; #if 1 os_printf("\n\nBeginning of IR message frame detected"); #endif //Set GPIO0 to HIGH gpio_output_set( BIT0, 0, BIT0, 0 ); } else { /* record time of current edge */ currTicks = RTC_REG_READ( FRC1_COUNT_ADDRESS ); /* record time interval between current edge and previous edge */ intervalArr[ edgeIndex - 1 ] = ticksToUs( prevTicks - currTicks ); /* keep track the shortest interval */ minInterval = ( intervalArr[ edgeIndex - 1 ] < minInterval ) ? intervalArr[ edgeIndex - 1 ] : minInterval; /* save time of current edge */ prevTicks = currTicks; } edgeIndex++; } }
bool Hardware_Timer::start(bool repeating/* = true*/) { this->repeating = repeating; stop(); if(interval == 0 || !callback) return started; if (this->repeating == 1) { RTC_REG_WRITE(FRC1_CTRL_ADDRESS, FRC1_AUTO_LOAD | DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); } else { RTC_REG_WRITE(FRC1_CTRL_ADDRESS, DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); } TM1_EDGE_INT_ENABLE(); ETS_FRC1_INTR_ENABLE(); started = true; RTC_REG_WRITE(FRC1_LOAD_ADDRESS, US_TO_RTC_TIMER_TICKS(interval)); return started; }
//Call this with PWM disabled. static void ICACHE_FLASH_ATTR enableNightlight(int ena) { int x; //Kill PWM RTC_REG_WRITE(FRC1_CTRL_ADDRESS, 0); TM1_EDGE_INT_DISABLE(); if (ena) { gpio_output_set((1<<NIGHTLIGHT_ON_PIN), (1<<NIGHTLIGHT_OFF_PIN), (1<<NIGHTLIGHT_ON_PIN)|(1<<NIGHTLIGHT_OFF_PIN), 0); os_delay_us(100); } else { gpio_output_set((1<<NIGHTLIGHT_OFF_PIN), (1<<NIGHTLIGHT_ON_PIN), (1<<NIGHTLIGHT_ON_PIN)|(1<<NIGHTLIGHT_OFF_PIN), 0); os_delay_us(10000); } gpio_output_set(0, (1<<NIGHTLIGHT_OFF_PIN)|(1<<NIGHTLIGHT_ON_PIN), (1<<NIGHTLIGHT_ON_PIN)|(1<<NIGHTLIGHT_OFF_PIN), 0); }
void hw_timer_arm(uint32 val) { RTC_REG_WRITE(FRC1_LOAD_ADDRESS, US_TO_RTC_TIMER_TICKS(val)); }
void ICACHE_FLASH_ATTR pwm_start(void) { uint8 i, j; PWM_DBG("--Function pwm_start() is called\n"); PWM_DBG("pwm_gpio:%x,pwm_channel_num:%d\n",pwm_gpio,pwm_channel_num); PWM_DBG("pwm_out_io_num[0]:%d,[1]:%d,[2]:%d\n",pwm_out_io_num[0],pwm_out_io_num[1],pwm_out_io_num[2]); PWM_DBG("pwm.period:%d,pwm.duty[0]:%d,[1]:%d,[2]:%d\n",pwm.period,pwm.duty[0],pwm.duty[1],pwm.duty[2]); LOCK_PWM(critical); // enter critical struct pwm_single_param *local_single = pwm_single_toggle[pwm_toggle ^ 0x01]; uint8 *local_channel = &pwm_channel_toggle[pwm_toggle ^ 0x01]; // step 1: init PWM_CHANNEL+1 channels param for (i = 0; i < pwm_channel_num; i++) { uint32 us = pwm.period * pwm.duty[i] / PWM_DEPTH; local_single[i].h_time = US_TO_RTC_TIMER_TICKS(us); PWM_DBG("i:%d us:%d ht:%d\n",i,us,local_single[i].h_time); local_single[i].gpio_set = 0; local_single[i].gpio_clear = 1 << pin_num[pwm_out_io_num[i]]; } local_single[pwm_channel_num].h_time = US_TO_RTC_TIMER_TICKS(pwm.period); local_single[pwm_channel_num].gpio_set = pwm_gpio; local_single[pwm_channel_num].gpio_clear = 0; PWM_DBG("i:%d period:%d ht:%d\n",pwm_channel_num,pwm.period,local_single[pwm_channel_num].h_time); // step 2: sort, small to big pwm_insert_sort(local_single, pwm_channel_num + 1); *local_channel = pwm_channel_num + 1; PWM_DBG("1channel:%d,single[0]:%d,[1]:%d,[2]:%d,[3]:%d\n",*local_channel,local_single[0].h_time,local_single[1].h_time,local_single[2].h_time,local_single[3].h_time); // step 3: combine same duty channels for (i = pwm_channel_num; i > 0; i--) { if (local_single[i].h_time == local_single[i - 1].h_time) { local_single[i - 1].gpio_set |= local_single[i].gpio_set; local_single[i - 1].gpio_clear |= local_single[i].gpio_clear; for (j = i + 1; j < *local_channel; j++) { os_memcpy(&local_single[j - 1], &local_single[j], sizeof(struct pwm_single_param)); } (*local_channel)--; } } PWM_DBG("2channel:%d,single[0]:%d,[1]:%d,[2]:%d,[3]:%d\n",*local_channel,local_single[0].h_time,local_single[1].h_time,local_single[2].h_time,local_single[3].h_time); // step 4: cacl delt time for (i = *local_channel - 1; i > 0; i--) { local_single[i].h_time -= local_single[i - 1].h_time; } // step 5: last channel needs to clean local_single[*local_channel-1].gpio_clear = 0; // step 6: if first channel duty is 0, remove it if (local_single[0].h_time == 0) { local_single[*local_channel - 1].gpio_set &= ~local_single[0].gpio_clear; local_single[*local_channel - 1].gpio_clear |= local_single[0].gpio_clear; for (i = 1; i < *local_channel; i++) { os_memcpy(&local_single[i - 1], &local_single[i], sizeof(struct pwm_single_param)); } (*local_channel)--; } // if timer is down, need to set gpio and start timer if (pwm_timer_down == 1) { pwm_channel = local_channel; pwm_single = local_single; // start gpio_output_set(local_single[0].gpio_set, local_single[0].gpio_clear, pwm_gpio, 0); // yeah, if all channels' duty is 0 or 255, don't need to start timer, otherwise start... if (*local_channel != 1) { pwm_timer_down = 0; RTC_REG_WRITE(FRC1_LOAD_ADDRESS, local_single[0].h_time); } } if (pwm_toggle == 1) { pwm_toggle = 0; } else { pwm_toggle = 1; } UNLOCK_PWM(critical); // leave critical PWM_DBG("3channel:%d,single[0]:%d,[1]:%d,[2]:%d,[3]:%d\n",*local_channel,local_single[0].h_time,local_single[1].h_time,local_single[2].h_time,local_single[3].h_time); }
void ICACHE_FLASH_ATTR pwm_start(void) { uint8 i, j; struct pwm_single_param *local_single = pwm_single_toggle[pwm_toggle ^ 0x01]; uint8 *local_channel = &pwm_channel_toggle[pwm_toggle ^ 0x01]; // step 1: init PWM_CHANNEL+1 channels param for (i = 0; i < PWM_CHANNEL; i++) { uint32 us = pwm.period * pwm.duty[i] / PWM_DEPTH; local_single[i].h_time = US_TO_RTC_TIMER_TICKS(us); local_single[i].gpio_set = 0; local_single[i].gpio_clear = 1 << pwm_out_io_num[i]; } local_single[PWM_CHANNEL].h_time = US_TO_RTC_TIMER_TICKS(pwm.period); local_single[PWM_CHANNEL].gpio_set = pwm_gpio; local_single[PWM_CHANNEL].gpio_clear = 0; // step 2: sort, small to big pwm_insert_sort(local_single, PWM_CHANNEL + 1); *local_channel = PWM_CHANNEL + 1; // step 3: combine same duty channels for (i = PWM_CHANNEL; i > 0; i--) { if (local_single[i].h_time == local_single[i - 1].h_time) { local_single[i - 1].gpio_set |= local_single[i].gpio_set; local_single[i - 1].gpio_clear |= local_single[i].gpio_clear; for (j = i + 1; j < *local_channel; j++) { os_memcpy(&local_single[j - 1], &local_single[j], sizeof(struct pwm_single_param)); } (*local_channel)--; } } // step 4: cacl delt time for (i = *local_channel - 1; i > 0; i--) { local_single[i].h_time -= local_single[i - 1].h_time; } // step 5: last channel needs to clean local_single[*local_channel-1].gpio_clear = 0; // step 6: if first channel duty is 0, remove it if (local_single[0].h_time == 0) { local_single[*local_channel - 1].gpio_set &= ~local_single[0].gpio_clear; local_single[*local_channel - 1].gpio_clear |= local_single[0].gpio_clear; for (i = 1; i < *local_channel; i++) { os_memcpy(&local_single[i - 1], &local_single[i], sizeof(struct pwm_single_param)); } (*local_channel)--; } // if timer is down, need to set gpio and start timer if (pwm_timer_down == 1) { pwm_channel = local_channel; pwm_single = local_single; // start gpio_output_set(local_single[0].gpio_set, local_single[0].gpio_clear, pwm_gpio, 0); // yeah, if all channels' duty is 0 or 255, don't need to start timer, otherwise start... if (*local_channel != 1) { pwm_timer_down = 0; RTC_REG_WRITE(FRC1_LOAD_ADDRESS, local_single[0].h_time); } } if (pwm_toggle == 1) { pwm_toggle = 0; } else { pwm_toggle = 1; } }
// initialize the custom stuff that goes beyond esp-link void user_init() { // Initialize the GPIO subsystem. gpio_init(); /* ====================================== */ /* UART */ /* ====================================== */ // Initialize UART0 and UART1 /* NOTE: UART1 and I2S share same GPIO. Cannot use simultaneously. */ uart_init( BIT_RATE_115200, BIT_RATE_115200 ); // uart0_sendStr( "\nUART0 - USED TO PROGRAM THE MODULE\n" ); os_printf("\n===================\nUART1 - DEBUG OUPUT\n===================\n"); /* NOTE: PWM CANNOT BE USED SIMULTANEOUSLY WITH HW TIMER */ #if 0 /* ====================================== */ /* PWM */ /* ====================================== */ uint32 pwm_period = 1000; uint32 pwm_duty[PWM_CHANNEL] = {0}; uint32 io_info[][3] = { {PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM}, {PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM}, }; /* PIN FUNCTION INIT FOR PWM OUTPUT */ pwm_init(pwm_period, pwm_duty, PWM_CHANNEL, io_info); /* set pwm_duty cycle */ pwm_set_duty (14185, 0); pwm_set_duty (22222, 1); // todo: explain why 22222 is the highest possible value /* start PWM */ pwm_start(); // NOTE: PWM causes spikes in other GPIOs #endif /* ====================================== */ /* GPIO INTERRPUT */ /* ====================================== */ /* Set GPIO12 in GPIO mode */ PIN_FUNC_SELECT( PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12 ); /* Set GPIO12 as input */ GPIO_DIS_OUTPUT( GPIO_ID_PIN(12) ); /* Disable all GPIO interrupts */ ETS_GPIO_INTR_DISABLE(); /* Set a GPIO callback function */ ETS_GPIO_INTR_ATTACH( gpioCallback, NULL ); /* Configure the type of edge */ gpio_pin_intr_state_set( GPIO_ID_PIN(12), GPIO_PIN_INTR_ANYEDGE ); ETS_GPIO_INTR_ENABLE(); /* ====================================== */ /* SOFTWARE TIMER */ /* ====================================== */ // Set GPIO0 to output mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); //Set GPIO0 low gpio_output_set(0, RELAY_PIN, RELAY_PIN, 0); /* disarm timer */ os_timer_disarm((ETSTimer*)&some_timer); /* set callback */ os_timer_setfn((ETSTimer*)&some_timer, (os_timer_func_t *) softwareTimerCallback, NULL); /* arm the timer -> os_timer_arm(<pointer>, <period in ms>, <fire periodically>) */ os_timer_arm((ETSTimer*)&some_timer, 10, 1); /* ====================================== */ /* OS TASK */ /* ====================================== */ /* setup OS task */ // system_os_task(user_procTask, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen); /* send a message to OS task (fire task) */ // system_os_post(user_procTaskPrio, 0, 0 ); /* ====================================== */ /* HARDWARE TIMER */ /* ====================================== */ /* The hardware timer is used to indicate when a complete IR message frame should have * arrived in order to process the received data and calculate the IR command. * * It is configured in "one-shot" mode. It is started when the beginning of an * IR message frame is detected and stopped after the complete message frame has been read. * This means that the duration of the HW timer should be longer than the duration of * the longest message frame. In the NEC IR tranmission protocol all message frames have * a duration of approximately 67.5ms. */ /* load the HW TIMER */ uint32 ticks = usToTicks(70000); // 70ms RTC_REG_WRITE(FRC1_LOAD_ADDRESS, ticks); /* register callback function */ ETS_FRC_TIMER1_INTR_ATTACH( hwTimerCallback, NULL ); /* enable interrupts */ TM1_EDGE_INT_ENABLE(); ETS_FRC1_INTR_ENABLE(); /* don't start timer yet */ /* the timer is started inside the GPIO INT callback */ /* ====================================== */ /* UDP SERVER */ /* ====================================== */ /* usage: echo <data> | nc -wl -u <ip address> <port> * example: echo "foo" | nc -w1 -u 192.168.1.187 7777 */ /* allocate space for server */ pUdpServer = (struct espconn *) os_zalloc(sizeof(struct espconn)); /* clear allocated memory */ ets_memset(pUdpServer, 0, sizeof(struct espconn)); /* create the server */ espconn_create(pUdpServer); /* set the type of server */ pUdpServer->type = ESPCONN_UDP; /* allocate memory for UDP settings */ pUdpServer->proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp)); /* set the port that the server will be listening to */ pUdpServer->proto.udp->local_port = 7777; /* register the callback */ espconn_regist_recvcb(pUdpServer, udpServerRxCb); /* start listening */ if (espconn_create(pUdpServer)) { while (1) { os_printf("Error creating a UDP server\n"); } } /* ====================================== */ /* WIFI */ /* ====================================== */ wifi_set_opmode(STATION_MODE); wifi_station_get_config_default(&stconf); // os_strncpy((char*) stconf.ssid, "TP-LINK_2.4GHz_FC2E51", 32); // os_strncpy((char*) stconf.password, "tonytony", 64); os_strncpy((char*) stconf.ssid, "WLAN-PUB", 32); os_strncpy((char*) stconf.password, "", 64); // os_strncpy((char*) stconf.ssid, "MAD air", 32); // os_strncpy((char*) stconf.password, "glioninlog", 64); stconf.bssid_set = 0; wifi_station_set_config(&stconf); // /* ====================================== */ // /* WS2812 LED STRIP */ // /* ====================================== */ // // /* NOTE: UART1 and I2S share same GPIO. Cannot use simultaneously. */ // ws2812_init(); // // /* G R B */ // uint8_t ledout[] = { // 0xff, 0x00, 0x00, //4th //// 0xff, 0x00, 0x00, //3rd //// 0x00, 0xff, 0x00, //2nd //// 0x00, 0x00, 0xff, //1st // }; // //#if 0 // os_printf("\r\nB R G: %x %x %x\r\n", ledout[0], ledout[1],ledout[2]); //#endif // // ws2812_push( ledout, sizeof( ledout ) ); /* ====================================== */ /* TCP CONNECTION */ /* ====================================== */ /* allocate space for server */ pTcpConn = (struct espconn *) os_zalloc(sizeof(struct espconn)); /* clear allocated memory */ ets_memset(pTcpConn, 0, sizeof(struct espconn)); /* set the type of connection */ pTcpConn->type = ESPCONN_TCP; /* set state to NONE */ pTcpConn->state = ESPCONN_NONE; /* allocate memory for TCP settings */ pTcpConn->proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp)); /* set the port that the connection will be listening to */ pTcpConn->proto.tcp->local_port = espconn_port(); /* set the remote port and IP address */ pTcpConn->proto.tcp->remote_port = 80; os_memcpy(pTcpConn->proto.tcp->remote_ip, server_ip_address, sizeof(server_ip_address)); /* register callbacks */ espconn_regist_connectcb(pTcpConn, tcpConnCb); espconn_regist_reconcb(pTcpConn, tcpReconnCb); /* disarm timer */ os_timer_disarm((ETSTimer*)&wifi_setup_timer); /* set callback */ os_timer_setfn((ETSTimer*)&wifi_setup_timer, (os_timer_func_t *) wifiConnectTimerCb, NULL); /* arm the timer -> os_timer_arm(<pointer>, <period in ms>, <fire periodically>) */ os_timer_arm((ETSTimer*)&wifi_setup_timer, 5000, 1); return; }
void hwTimerCallback( void ) { int i, j; int logicState = 1; int logicStateLen = 0; bool repeatCode = false; /* stop the HW TIMER */ RTC_REG_WRITE(FRC1_CTRL_ADDRESS, DIVDED_BY_16 | TM_EDGE_INT); //Set GPIO0 to LOW gpio_output_set(0, BIT0, BIT0, 0); /* load the HW TIMER for next IR message frame */ uint32 ticks = usToTicks(70000); RTC_REG_WRITE(FRC1_LOAD_ADDRESS, ticks); /* derive the raw IR message frame */ for( i = 0 ; i < ( edgeIndex - 1 ) ; i++) { /* find number of bits in current interval */ logicStateLen = ( intervalArr[i] / minInterval ); for( j = 0 ; j < logicStateLen ; j++) { rawIrMsg[ rawIrMsgLen ] = logicState; rawIrMsgLen++; } /* toggle state */ logicState ^= 1; #if 0 os_printf( "\r\nDuration of interval %d: %d us\r\n", i, intervalArr[i] ); #endif } #if 1 /* print the received raw IR message frame */ os_printf( "\r\nRAW IR CODE: "); for ( i = 0 ; i < rawIrMsgLen ; i++ ) { os_printf( "%d", rawIrMsg[i] ); } #endif /********************************************** * DECODE NEC MESSAGE FRAME! * - every message frame contains 32 coded bits **********************************************/ /* set index to the beginning of the coded bits */ /* the message frame starts with a burst of 16 logic 1's, skip them all */ i = 0 ; while ( rawIrMsg[i] == 1 ) i++; /* the message frame continues with a burst of 8 logic 0's, skip them all */ j = 0; while (rawIrMsg[i] == 0) { i++; j++; } /* if the number of zeros is 4, then ignore the current message frame since * it corresponds to a "REPEATED CODE". */ if ( j <= 4 ) { #if 1 os_printf( "\r\nREPEATED CODE"); #endif repeatCode = true; } /* decode raw message only if it is not a repeat code */ if (repeatCode == false) { /* at this point 'i' contains the index of the beginning of the encoded bits */ /* decode raw message * - [1][0][0][0] represents a '1' * - [1][0] represents a '0' */ irCmd = 0; for (j = 0; j < 32; j++) { if (rawIrMsg[i + 2] == 0) { /* it is a '1', so left shift a '1' */ irCmd = (irCmd << 1) | 1; /* move to the beginning of the next encoded bit * (increment i until next 1 in raw message frame) */ do {i++;} while ( rawIrMsg[i] == 0 ); } else { /* it is a '0', so left shift a '0' */ irCmd = irCmd << 1; /* move to the beginning of the next encoded bit */ i += 2; } } #if 1 /* print the received IR cmd */ os_printf("\r\nIR CMD: %x", irCmd); #endif } /********************************************** * END - DECODE NEC MESSAGE FRAME! * - every message frame contains 32 coded bits **********************************************/ /* reset index */ edgeIndex = 0; #if 1 os_printf("\r\nEnd of IR message frame\r\n"); #endif return; }