/// @cond debug int _spi_find_divider_index (unsigned int divider) { int i; if(_spi_divider_index[0]==divider) { #ifdef SPI_DBG_CFG UARTWrite(1,"[SPI_DBG] SPI cfg array selected: 0\n"); #endif return 0; } for (i=1;i<24;i++) { if (( _spi_divider_index[i-1] < divider ) && ( _spi_divider_index[i] >= divider )) { #ifdef SPI_DBG_CFG sprintf(spi_dbg_msg,"[SPI_DBG] SPI cfg array selected: %d\n",i); UARTWrite(1,spi_dbg_msg); #endif return i; } } #ifdef SPI_DBG_CFG UARTWrite(1,"[SPI_DBG] SPI cfg array selected: 24\n"); #endif return 24; }
void _USBNTransmit(EPInfo* ep) { int i; if(ep->Size > 0) { UARTWrite("t"); if(ep->Index <= ep->Size) { USBNWrite(TXC0,FLUSH); //send data to the FIFO //USBNDebug(" "); for(i=0;((i < 8) & (ep->Index < ep->Size)); i++) { USBNWrite(TXD0,ep->Buf[ep->Index]); ep->Index++; } // if end of multipaket if(ep->Size<=ep->Index){ //UARTWrite("x"); //SendHex(ep->Size); //SendHex(ep->Index); /* if(ep->Index==48){ //USBNWrite(TXC0,TX_TOGL+TX_EN); //enable the TX (DATA1) //EP0tx.DataPid = 1; //USBNWrite(TXC0,FLUSH); //USBNWrite(TXD0,0); //USBNWrite(TXC0,TX_EN); //USBNWrite(RXC0,FLUSH); //USBNWrite(RXC0,RX_EN); //ep->DataPid =1; //return; } */ } } else { UARTWrite("e"); USBNWrite(RXC0,RX_EN); return; } // toggle mechanism if(ep->DataPid == 1) { USBNWrite(ep->usbnCommand,TX_TOGL+TX_EN); ep->DataPid=0; } else { USBNWrite(ep->usbnCommand,TX_EN); ep->DataPid=1; } } }
void FlyportTask() { // Flyport connects to default network //WFConnect(WF_DEFAULT); //while(WFGetStat() != CONNECTED); vTaskDelay(25); UARTWrite(1,"Flyport initialized...hello world!\r\n"); //p18 - AI4 acts as camera reader //http://wiki.openpicus.com/index.php/USB_NEST //p4 JP5 - OUT1/PWM Digital output acts as CLK //p6 JP5 - OUT2/PWM Digital output acts as SI (signal input) int CBT=128; int row[128]; //int i;for(i=0;i<CBT;i++)row[i]=0; //if (0) while(1) { ImageCapture(row); int prog=0; int i; for (i = 0; i < CBT; ++i) { int this=sprintf(t+prog,"%d ", row[i]); prog+=this; } prog+=sprintf(t+prog, "\r\n"); UARTWrite(1, t); // vTaskDelay(100); //FlyportTask2(); } while(1); }
// class requests void USBNDecodeClassRequest(DeviceRequest *req,EPInfo* ep) { UARTWrite("class"); static unsigned char serialNotification[10] = {0xa1,0x20,0,0,0,0,2,0,3,0}; int loop; switch(req->bRequest) { case 0x20: //SET_LINE_CODING: UARTWrite("set line\r\n"); USBNWrite(RXC0,RX_EN); USBNRead(RXD0); USBNRead(RXD0); USBNRead(RXD0); USBNRead(RXD0); USBNRead(RXD0); USBNRead(RXD0); USBNRead(RXD0); //USBNWrite(RXC0,RX_EN); //USBNWrite(RXC0,FLUSH); USBNWrite(TXC0,FLUSH); USBNWrite(TXC0,TX_TOGL+TX_EN); break; case 0x21: // GET_LINE_CODING: //UARTWrite("get line coding"); USBNWrite(TXC0,FLUSH); // baud rate USBNWrite(TXD0,0x80); USBNWrite(TXD0,0x25); USBNWrite(TXD0,0); USBNWrite(TXD0,0); USBNWrite(TXD0,0); //stopbit USBNWrite(TXD0,0); // parity USBNWrite(TXD0,8); // databits interrupt_ep_send(); USBNWrite(TXC0,TX_TOGL+TX_EN); break; case 0x22: //SET_CONTROL_LINE_STATE: //UARTWrite("set ctrl line state"); USBNWrite(TXC1,FLUSH); // fill endpoint fifo for(loop=0;loop<8;loop++) USBNWrite(TXD1,serialNotification[loop]); //send and control togl bit interrupt_ep_send(); USBNWrite(TXC0,TX_TOGL+TX_EN); break; } }
// INCOMING CALL void OnRing(char* phoneNumber) { #if defined(STACK_USE_UART) char buf[20]; UARTWrite(1,"Event: On Ring\r\nPhone number: "); sprintf(buf, "%s", phoneNumber); UARTWrite(1,buf); UARTWrite(1,"\r\n"); #endif }
void OnSMSSentReport(int msgreference, int msgreport) { #if defined(STACK_USE_UART) char buf[30]; UARTWrite(1,"Event: On SMS Sent Report\r\n"); sprintf(buf, "message reference:%d\r\n", msgreference); UARTWrite(1, buf); sprintf(buf, "report value:%d\r\n", msgreport); UARTWrite(1, buf); #endif }
void OnError(int error, int errorNumber) { #if defined(STACK_USE_UART) char numErr[10]; UARTWrite(1,"Event: On Error\r\nerror: "); sprintf(numErr, "%d\r\n", error); UARTWrite(1, numErr); UARTWrite(1, "error number: "); sprintf(numErr, "%d\r\n", errorNumber); UARTWrite(1, numErr); #endif }
void FlyportTask2() { const int maxBright = 100; //here we set max % of brightness const int minBright = 0; //and here the min % float bright = (float)maxBright; int static initialized=0; if(!initialized) { initialized=1; PWMInit(1,50,maxBright); PWMOn(p19, 1);//Assign pin 9 as PWM1 and turns it on } //while(1) { for (bright = maxBright; bright > minBright; bright--) { PWMDuty(bright, 1); vTaskDelay(1); //used to slow down the effect } for (bright = minBright; bright < maxBright; bright ++) { PWMDuty(bright, 1); vTaskDelay(1); //used to slow down the effect } UARTWrite(1,"changed"); } }
//--- Dynmxl Functions ----// void DymxlTx(const uint8_t id, const uint8_t instruction, const uint8_t *params, const uint8_t params_len, const uint32_t BASE) { uint8_t checksum = 0; uint8_t tx_buf[256]; uint8_t* tbuf = tx_buf; ROM_GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_1, GPIO_DIR_MODE_HW); //ROM_GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_0, GPIO_DIR_MODE_IN); // find disable reciver HWREG(BASE + UART_O_CTL) &= ~UART_CTL_RXE; tx_buf[0] = 0xff; tx_buf[1] = 0xff; tx_buf[2] = id; tx_buf[3] = 2 + params_len; tx_buf[4] = instruction; checksum = 2+ params_len + id + instruction; for (uint8_t i = 0; i < params_len; i++) { tx_buf[5+i] = params[i]; checksum += params[i]; } checksum = ((~checksum) & 255); tx_buf[5+params_len] = checksum; UARTWrite(BASE,tbuf, 6+params_len); while(HWREG(BASE + UART_O_FR) & UART_FR_BUSY) { } ROM_GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_1, GPIO_DIR_MODE_IN); HWREG(BASE + UART_O_CTL) |= UART_CTL_RXE; }
void initializeTime(int GMT_zone) { GMT_hour_adding = GMT_zone; while(epoch<epochtime) { vTaskDelay(50); //Remember to enable the SNTP Client in the wizard. epoch=SNTPGetUTCSeconds(); } epoch=SNTPGetUTCSeconds(); now=(time_t)epoch; ts=localtime(&now); vTaskDelay(20); ts->tm_hour = (ts->tm_hour + GMT_hour_adding); // Correct if overflowed hour 0-24 format if(ts->tm_hour > 24) { ts->tm_hour = ts->tm_hour - 24; } else if(ts->tm_hour < 0) { ts->tm_hour = ts->tm_hour +24; } sprintf (epochString, "\nReceived date/time is: %s \r\n", asctime (ts) ); UARTWrite(1,epochString); RTCCSet(ts); //Set time };
void _dbgwrite(char* dbgstr) { #if defined(STACK_USE_UART) UARTWrite(1, dbgstr); #else #endif }
/**************************************************************************** MAIN APPLICATION ENTRY POINT ****************************************************************************/ int main(void) { // Queue creation - will be used for communication between the stack and other tasks xQueue = xQueueCreate(3, sizeof (int)); xSemFrontEnd = xSemaphoreCreateMutex(); // Initialize application specific hardware HWInit(HWDEFAULT); UARTInit(1,19200); UARTOn(1); // RTOS starting if (xSemFrontEnd != NULL) { // Creates the task to handle all HiLo functions xTaskCreate(GSMTask, (signed char*) "GSM", STACK_SIZE_GSM, NULL, tskIDLE_PRIORITY + 1, &hGSMTask); // Start of the RTOS scheduler, this function should never return vTaskStartScheduler(); } #if defined (STACK_USE_UART) UARTWrite(1, "Unexpected end of program...\r\n"); #endif while(1); return -1; }
/* * reads the calibration state from channels variable * reads the WiFi config from configuration variable * then calls HAConfigSave */ void SaveCalibrationStateToFlash() { #if defined(STACK_USE_UART) UARTWrite(1,"Saving Settings\r\n"); UARTWrite(1,"\r\n"); #endif unsigned long int saveAddress = CALIBRATION_SETTING_ADDRESS; SPIFlashEraseSector(CALIBRATION_SETTING_ADDRESS_BLOCK1); SPIFlashEraseSector(CALIBRATION_SETTING_ADDRESS_BLOCK2); SPIFlashBeginWrite(saveAddress); vTaskSuspendAll(); SPIFlashWrite(0x0A); SPIFlashWrite(0x0B); xTaskResumeAll(); saveAddress += 2; int i; for(i=0; i<NUM_OF_CHANNELS; i++) { switch (channels[i].channelType) { case BLINDS: saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelStatus.blinds.calibrationStatus, sizeof(BlindsCalibrationStatus)); saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelStatus.blinds.channelStatus.buttonUpStatus.buttonSettings, sizeof(ButtonSettings)); saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelStatus.blinds.channelStatus.buttonDownStatus.buttonSettings, sizeof(ButtonSettings)); break; case ONOFF_W_KEY: case ONOFF_W_BUTTON: saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelStatus.onOff.channelStatus.buttonStatus.buttonSettings, sizeof(ButtonSettings)); break; case ONOFF_BUTTON: saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelStatus.onOffButton.buttonStatus.buttonSettings, sizeof(ButtonSettings)); break; default: break; } saveAddress = SaveVariableToFlash(saveAddress, (BYTE *) &channels[i].channelPower, sizeof(unsigned int)); } }
void OnRegistration(BYTE Status) { #if defined(STACK_USE_UART) UARTWrite(1,"Event: On Registration\r\n"); switch(Status) { case 0: UARTWrite(1, "Not registered\r\n"); IOPut(p21, off); break; case 1: UARTWrite(1, "Registered on Network\r\n"); IOPut(p21, on); break; case 2: UARTWrite(1, "Searching for new operator\r\n"); IOPut(p21, off); break; case 3: UARTWrite(1, "Registration denied\r\n"); IOPut(p21, off); break; case 4: UARTWrite(1, "Unkown registration status\r\n"); IOPut(p21, off); break; case 5: UARTWrite(1, "Roaming\r\n"); IOPut(p21, off); break; } #endif }
//Interrupt function for motion triggering void external_interrupt_function() { UARTWrite(1,"Motion Sensed -> Timer Reset!!\r\n"); opt_trig=0; // reseting the trigger so that clock will restart every time we sense any motion if(WFStatus==TURNED_OFF) { on_flag=1; //setting ON_flag to 1 to wake up the module } }
void OnSMSReceived(BYTE memtype, int index) { #if defined(STACK_USE_UART) UARTWrite(1,"Event: On SMS Received\r\nmemory type: "); if(memtype == SM_MEM) UARTWrite(1, "SIM card"); else UARTWrite(1, "Module Memory"); UARTWrite(1, "\r\nmemory index: "); char smsRec[7]; sprintf(smsRec, "%d\r\n", index); UARTWrite(1, smsRec); #endif incomingIndexSMS = index; incomingMemSMS = memtype; incomingSMS = TRUE; }
void __attribute__((interrupt, auto_psv)) _OscillatorFail(void) { UARTWrite(1, "!!! Oscillator Fail interrupt handler !!!\r\n" ); while (1) { Nop(); Nop(); Nop(); } }
void __attribute__((interrupt, auto_psv)) _DefaultInterrupt(void) { UARTWrite(1,"!!! Default interrupt handler !!!\r\n" ); while (1) { Nop(); Nop(); Nop(); } }
void __attribute__((interrupt, auto_psv)) _MathError(void) { UARTWrite(1,"!!! Math Error interrupt handler !!!\r\n" ); while (1) { Nop(); Nop(); Nop(); } }
void ClockInit() //Initializes RTCC based on SNTP and also configures RTCC to raise an alarm every second { time_t now; struct tm *ts; DWORD epoch=0; DWORD epochtime=0xA2C2A; t_RTCC mytime, myalarm; if(profile.SNTPEnable) { UARTWrite(1,"waiting..."); while(epoch<epochtime) { vTaskDelay(50); epoch=SNTPGetUTCSeconds();//Get SNTP time } UARTWrite(1, "done!\r\n"); } else { epoch=profile.Time+19800; } //Get SNTP time //epoch = SNTPGetUTCSeconds(); //+ 19800; //Converting GMT to Indian time//manoj now = (time_t)epoch; ts = gmtime(&now); //Time Library related conversion for GMT format //ts = localtime(&now); //Time Library related conversion //Manoj //Set the RTC according to SNTP time mytime.year = ts->tm_year; //mytime is internal structure for Flyport defined for RTC mytime.month = ts->tm_mon+1; mytime.dweek = ts->tm_wday; mytime.day = ts->tm_mday; mytime.hour = ts->tm_hour; mytime.min = ts->tm_min; mytime.sec = ts->tm_sec; RTCCWrite (&mytime); myalarm = mytime; //Configuring RTC to raise an alarm every second (This will be used for sampling the sensors and transmitting the data) myalarm.sec = myalarm.sec + 1; RTCCSetAlarm(&myalarm, REPEAT_INFINITE, EVERY_SEC); RTCCRunAlarm(1); }
void SaveWiFiStateToFlash() { #if defined(STACK_USE_UART) UARTWrite(1,"Saving WiFi State\r\n"); UARTWrite(1,"IP Address: "); UARTWrite(1,config_parms.MyIPAddr); UARTWrite(1,"\r\n"); #endif SPIFlashEraseSector(WIFI_SETTING_ADDRESS); SPIFlashBeginWrite(WIFI_SETTING_ADDRESS); vTaskSuspendAll(); SPIFlashWrite(0x0A); SPIFlashWrite(0x0B); xTaskResumeAll(); SaveVariableToFlash(WIFI_SETTING_ADDRESS + 2, (BYTE *) &config_parms, sizeof(config_parms)); }
/* * loads the calibration state * loads the WiFi config * sets the channels variable and saved WiFi config */ void LoadCalibrationStateFromFlash() { if (!FlashConfigExists(CALIBRATION_SETTING_ADDRESS)) return; #if defined(STACK_USE_UART) UARTWrite(1,"Calibration Configuration Exists\r\n"); #endif unsigned long int loadAddress = CALIBRATION_SETTING_ADDRESS + 2; int i; for(i=0; i<NUM_OF_CHANNELS; i++) { switch (channels[i].channelType) { case BLINDS: loadAddress = LoadVariableFromFlash(loadAddress, (BYTE *) &channels[i].channelStatus.blinds.calibrationStatus, sizeof(BlindsCalibrationStatus)); loadAddress = LoadVariableFromFlash(loadAddress, (BYTE *) &channels[i].channelStatus.blinds.channelStatus.buttonUpStatus.buttonSettings, sizeof(ButtonSettings)); loadAddress = LoadVariableFromFlash(loadAddress, (BYTE *) &channels[i].channelStatus.blinds.channelStatus.buttonDownStatus.buttonSettings, sizeof(ButtonSettings)); break; case ONOFF_W_KEY: case ONOFF_W_BUTTON: loadAddress = LoadVariableFromFlash(loadAddress, (BYTE *) &channels[i].channelStatus.onOff.channelStatus.buttonStatus.buttonSettings, sizeof(ButtonSettings)); break; case ONOFF_BUTTON: loadAddress = LoadVariableFromFlash(loadAddress, (BYTE *) &channels[i].channelStatus.onOffButton.buttonStatus.buttonSettings, sizeof(ButtonSettings)); break; default: break; } loadAddress = LoadVariableFromFlash(loadAddress, (BYTE *) &channels[i].channelPower, sizeof(unsigned int)); } #if defined(STACK_USE_UART) UARTWrite(1,"Calibration Configuration Loaded\r\n"); #endif }
void LoadWiFiStateFromFlash() { if (!FlashConfigExists(WIFI_SETTING_ADDRESS)) { #if defined(STACK_USE_UART) UARTWrite(1,"No custom Flash config\r\n"); #endif return; } #if defined(STACK_USE_UART) UARTWrite(1,"WiFi Configuration Exists\r\n"); #endif LoadVariableFromFlash(WIFI_SETTING_ADDRESS + 2, (BYTE *) &config_parms, sizeof(config_parms)); #if defined(STACK_USE_UART) UARTWrite(1,"WiFi Configuration Loaded\r\n"); UARTWrite(1,"IP Address: "); UARTWrite(1,config_parms.MyIPAddr); UARTWrite(1,"\r\n"); #endif CheckAndWriteCustom(); }
void _485Write(const unsigned char* Data, short len) { char loggBuff[256]; sprintf(loggBuff, "\r\n_485Write... going to write %d bytes to port # %d", (int)len, (int)Port_); UARTWrite(1, loggBuff); IOPut((int)DE_485, on); IOPut((int)RE_485, on);// RE is inverted, so, ON value turns it off vTaskDelay(1); short ind = len; do { UARTWriteCh(Port_, Data[ind - len]); }while(--len); vTaskDelay(1); UARTWrite(1, "\r\n_SerialWrite... enabling reciever"); IOPut((int)DE_485, off); IOPut((int)RE_485, off); }
void UARTTestPing(void) { char test_buf[256]; char* t_buf = test_buf; t_buf[0] = 0xff; t_buf[1] = 0xff; t_buf[2] = broadcast; t_buf[3] = 2; t_buf[4] = 0x01; t_buf[5] = (~(broadcast+2+1)&0xFF); UARTWrite(UART3_BASE,t_buf,6); }
void _232Write(const unsigned char* Data, short len) { char loggBuff[256]; sprintf(loggBuff, "\r\n_232Write... going to write %d bytes to port # %d", (int)len, (int)Port_); UARTWrite(1, loggBuff); vTaskDelay(1); short ind = len; do { UARTWriteCh(Port_, Data[ind - len]); }while(--len); vTaskDelay(1); }
TCP_SOCKET XivelyConn(char* ServerName, char* ServerPort) { int j; TCP_SOCKET MySocket = INVALID_SOCKET; #if defined(STACK_USE_UART) UARTWrite(1,"Connecting to Xively...\r\n"); #endif MySocket = TCPClientOpen(ServerName, ServerPort); #if defined(STACK_USE_UART) if(MySocket == INVALID_SOCKET) { UARTWrite(1,"INVALID_SOCKET\r\n"); } UARTWrite(1,"Checking connection.\r\n"); #endif // Make sure it connects j = 10; while (j>0 && !TCPisConn(MySocket)) { //vTaskDelay(xDelay(500)); vTaskDelay(500); j--; //sprintf(j, "%d\r\n", j+1); } if(j==0) { #if defined(STACK_USE_UART) UARTWrite(1,"Connection failed.\r\n"); #endif // Close the socket so it can be used by other modules TCPClientClose(MySocket); MySocket = INVALID_SOCKET; // We are in an error condition, so light up the Flyport led } return MySocket; }
void FlyportTask() { vTaskDelay(100); UARTWrite(1,"Welcome to GROVE NEST example!\r\n"); // GROVE board void *board = new(GroveNest); void *tempAn = new(An_Temp); // Attach devices attachToBoard(board, tempAn, AN1); float anVal = 0.0; char mess[50]; char msg[50]; // Connection to Network #if defined (FLYPORT_WF) WFConnect(WF_DEFAULT); while (WFStatus != CONNECTED); #endif #if defined (FLYPORTETH) while(!MACLinked); #endif UARTWrite(1,"Flyport connected... hello world!\r\n"); vTaskDelay(200); while(1) { postSensorValue("temperature", (double)(get(tempAn)), "1377515972748279ff7a4b8ee4b27ba6f6db0412227ba"); vTaskDelay(6000); } }
int main(void) { char c; // PORTC_PCR5 = PORT_PCR_MUX(0x1); // LED is on PC5 (pin 13), config as GPIO (alt = 1) // GPIOC_PDDR = (1<<5); // make this an output pin // LED_OFF; // start with LED off UARTInit(TERM_UART, TERM_BAUD); // open UART for comms UARTWrite(hello, strlen(hello)); EnableInterrupts; while (1) { while (UARTAvail() != 0) { UARTRead(&c, 1); UARTWrite(&c, 1); if (c == '\r') UARTWrite("\n", 1); } } return 0; // should never get here! }
//this is only my debug tool void Terminal(char cmd) { char h,l; unsigned char tmp; int i; struct list_entry *ptr; char *values; switch(cmd) { case 'i': USBNStart(); break; // write to usb register case 'w': //UARTWrite("write to USB reg:"); //USBNDEBUGPRINT("write to USB reg:"); h = UARTGetChar(); l = UARTGetChar(); SendHex(AsciiToHex(h,l)); tmp = AsciiToHex(h,l); UARTWrite("value:"); h = UARTGetChar(); l = UARTGetChar(); SendHex(AsciiToHex(h,l)); //USBNWrite(tmp,AsciiToHex(h,l)); UARTWrite("result:"); SendHex(USBNRead(tmp)); UARTWrite("\r\n"); break; // read from usb register case 'r': UARTWrite("read USB reg:"); h = UARTGetChar(); l = UARTGetChar(); SendHex(AsciiToHex(h,l)); UARTWrite("->"); SendHex(USBNRead(AsciiToHex(h,l))); UARTWrite("\r\n"); break; case 'h': UARTWrite("i usbn init procedure\r\n"); UARTWrite("w write USBN Register <h,l>(address) <h,l> (value) e.g 05 00\r\n"); UARTWrite("r read USBN Register <h,l> e.g. 02 ( RID)\r\n"); UARTWrite("s show all USBN Registers\r\n"); UARTWrite("b send test data from func to host\r\n"); UARTWrite("d show descriptors\r\n"); break; // show all registers case 's': for(i=0;i<=63;i++) { SendHex(i); UARTWrite("->"); SendHex(USBNRead(i)); UARTWrite("\r\n"); } break; case 'd': USBNDebug("\r\nDescriptor List\r\n"); ptr = DescriptorList; while(ptr != NULL) { values = (char*)ptr->data; SendHex(ptr->type); SendHex(ptr->len); SendHex(ptr->conf); SendHex(ptr->interf); USBNDebug(" "); for(i=0;i<ptr->len;i++) SendHex(values[i]); USBNDebug("\r\n"); ptr=ptr->next; } break; case 'b': UARTWrite("send test data from fifo1\r\n"); int j,i; char stat; USBNWrite(TXC1,FLUSH); USBNWrite(TXD1,0x01); for(j=0;j<63;j++) USBNBurstWrite((unsigned char)j); USBNWrite(TXC1,TX_LAST+TX_EN); //USBNWrite(TXC1,TX_LAST+TX_EN+TX_TOGL); break; case 'p': USBNWrite(TXC1,TX_LAST+TX_EN); break; default: UARTWrite("unknown command\r\n"); } }