/* * Print out current value of push buttons, if board is moving or tiled, * the value of the potentiometer, and the elapsed time since bootup */ int_32 Shell_status(int_32 argc, char_ptr argv[] ) { boolean print_usage, shorthelp = FALSE; int_32 return_code = SHELL_EXIT_SUCCESS; int pot; TIME_STRUCT time; DATE_STRUCT date; print_usage = Shell_check_help_request(argc, argv, &shorthelp ); if (!print_usage) { if (argc > 1) { printf("Error, invalid number of parameters\n"); return_code = SHELL_EXIT_ERROR; print_usage=TRUE; } else { /* Print out 'Door' status */ printf("Door (SW2): "); if(SEC_GetDoorStatus()==OPENED) printf("Open\n"); else printf("Closed\n"); /* Print out 'Window' status */ printf("Window (SW3): "); if(SEC_GetWindowStatus()==OPENED) printf("Open\n"); else printf("Closed\n"); /* Print out movement/tilt status */ printf("Motion Detection: "); if(SEC_GetMovementStatus()==MOVING) printf("Movement\n"); else printf("No Movement\n"); /* Print out potentiometer percentage in 10% increments */ pot = (SEC_Params.GarageStatus / 409) * 10; printf("Garage Door (Potentiometer R2): %d%% Open\n",pot); /* Print out elapsed time since bootup */ _time_get(&time); _time_to_date(&time,&date); printf("Time Since Bootup: %02d:%02d:%02d\n\n",date.HOUR,date.MINUTE,date.SECOND); } } if (print_usage) { print_usage_simple (shorthelp, argv[0], "Display security system status"); } return return_code; }
_mqx_int _rtc_sync_with_mqx ( /* [IN] whether to update MQX time (source is RTC) or RTC time (source is MQX) */ boolean update_mqx ) { RTC_TIME_STRUCT rtc_time; DATE_STRUCT mqx_date; TIME_STRUCT mqx_time; if (update_mqx == TRUE) { _rtc_get_time(&rtc_time); _rtc_time_to_mqx_date(&rtc_time, &mqx_date); _time_from_date(&mqx_date, &mqx_time); _time_set(&mqx_time); } else { _time_get(&mqx_time); _time_to_date(&mqx_time, &mqx_date); _rtc_time_from_mqx_date (&mqx_date, &rtc_time); _rtc_set_time (&rtc_time); } return MQX_OK; }
double current_time(int reset) { TIME_STRUCT tv; _time_get(&tv); return (double)tv.SECONDS + (double)tv.MILLISECONDS / 1000; }
int_32 Shell_dir_r(int_32 argc, char_ptr argv[]) { boolean print_usage, shorthelp = FALSE; TIME_STRUCT time, time_new; char full_name[255]; print_usage = Shell_check_help_request(argc, argv, &shorthelp); /* Check if help should be printed out */ if (print_usage) { if (shorthelp) { printf("%s dir \n", argv[0]); } else { printf("Usage: %s dir \n", argv[0]); printf("\tList all the files in the directory.\n"); printf(" dir = directory to be listed.\n"); } } set_file_filter(ext_wantted); _time_get(&time); if (0 == strcmp(argv[2], "next")) get_next_file_name(argv[1], full_name); else if (0 == strcmp(argv[2], "prev")) get_prev_file_name(argv[1], full_name); _time_get(&time_new); printf("%s\n", full_name); /* if(NULL != fopen(full_name, "r")){ printf("The file %s opened.\n", full_name); } */ /* printf("Taken %dms to list %d directories and %d files.\n", (time_new.SECONDS - time.SECONDS) * 1000 + (time_new.MILLISECONDS - time.MILLISECONDS), dir_listed, file_listed ); */ return (SHELL_EXIT_SUCCESS); }
/* * Add event string to log queue. */ void SEC_AddLog(SEC_Event_t event) { /* Add new event to ring buffer*/ SEC_Params.History[SEC_Params.HistoryIndex].eventptr = SEC_Event_Tab[event]; _time_get(&SEC_Params.History[SEC_Params.HistoryIndex++].time); if (SEC_Params.HistoryIndex >= MAX_QUEUE) SEC_Params.HistoryIndex = 0; }
void HVAC_ReadAmbientTemperature(void) { TIME_STRUCT time; _time_get(&time); if (time.SECONDS>=(LastUpdate.SECONDS+HVAC_TEMP_UPDATE_RATE)) { LastUpdate=time; if (HVAC_GetOutput(HVAC_HEAT_OUTPUT)) { AmbientTemperature += HVAC_TEMP_UPD_DELTA; } else if (HVAC_GetOutput(HVAC_COOL_OUTPUT)) { AmbientTemperature -= HVAC_TEMP_UPD_DELTA; } } }
static _mqx_int cgi_rtc_data(HTTPD_SESSION_STRUCT *session) { TIME_STRUCT time; int min, hour; _time_get(&time); min = time.SECONDS / 60; hour = min / 60; min %= 60; CGI_SEND_NUM(hour); CGI_SEND_NUM(min); CGI_SEND_NUM(time.SECONDS % 60); return session->request.content_len; }
boolean SEC_GetTime() { boolean res = FALSE; #if DEMOCFG_ENABLE_SNTP _ip_address ipaddr; TIME_STRUCT time; DATE_STRUCT date; char tries = 0; /* Try three times to get time */ while(tries<3) { _time_delay(1000); printf("\nGetting time from time server ... "); if (RTCS_resolve_ip_address(SNTP_SERVER,&ipaddr,NULL,0)) { /* Contact SNTP server and update time */ if(SNTP_oneshot(ipaddr,1000)==RTCS_OK) { printf("Succeeded\n"); res = TRUE; break; } else { printf("Failed\n"); } } else { printf("Failed - address not resolved\n"); break; } tries++; } /* Get current time */ _time_get(&time); _time_to_date(&time,&date); printf("\nCurrent Time: %02d/%02d/%02d %02d:%02d:%02d\n", date.YEAR,date.MONTH,date.DAY,date.HOUR,date.MINUTE,date.SECOND); #endif return res; }
void setup_clock(void) { uint32_t ret = 0, i = 0; uint32_t sntp_connected = 0; uint32_t sntp_max_tries = 3; TIME_STRUCT time_s; DATE_STRUCT date_s; /* NTP server: nist1-lnk.binary.net */ _ip_address ipaddr = IPADDR(216,229,0,179); for (i = 0; i < sntp_max_tries; i++) { printf("Getting time from NTP server [ attempt %d of %d ]...\n", i+1, sntp_max_tries); /* update time from NTP server */ ret = SNTP_oneshot(ipaddr, 5000); if (ret == RTCS_OK) { sntp_connected = 1; printf("SNTP successfully updated device time\n"); break; } else if (ret == RTCSERR_TIMEOUT) { printf("SNTP attempt timed out.\n"); } _time_delay(1000); } if (sntp_connected == 0) { err_sys("SNTP failed to update device time"); } /* print device time, for debug purposes */ _time_get(&time_s); _time_to_date(&time_s, &date_s); printf("Current time: %02d/%02d/%02d %02d:%02d:%02d\n", date_s.YEAR, date_s.MONTH, date_s.DAY, date_s.HOUR, date_s.MINUTE, date_s.SECOND); return; }
static _mqx_int cgi_rtc_data(HTTPD_SESSION_STRUCT *session) { TIME_STRUCT time; int min, hour; _time_get(&time); min = time.SECONDS / 60; hour = min / 60; min %= 60; session->response.contenttype = CONTENT_TYPE_PLAIN; httpd_sendhdr(session, 0, 0); CGI_SEND_NUM(hour); CGI_SEND_NUM(min); CGI_SEND_NUM(time.SECONDS % 60); return session->request.content_len; }
static void Clock_child_task ( #if CREATE_WITH_RTCS pointer param, pointer creator #else uint_32 sock #endif ) { #if CREATE_WITH_RTCS uint_32 sock = (uint_32) param; #endif int_32 size; char buffer[256]; TIME_STRUCT time; #if CREATE_WITH_RTCS RTCS_task_resume_creator(creator, RTCS_OK); #endif printf("Clock child: Servicing socket %x\n", sock); #if USE_RTCS_ATTACH_DETACH RTCS_attachsock(sock); #endif while (sock) { _time_get(&time); size = sprintf(buffer, "\r\nSeconds elapsed = %d.%03d", time.SECONDS, time.MILLISECONDS); size++; // account for null byte if (size != send(sock, buffer, size, 0)) { printf("\nClock child: closing socket %x\n",sock); shutdown(sock, FLAG_CLOSE_TX); sock=0; } else { _time_delay(5000); } } printf("Clock child: Terminating\n"); }
static _mqx_int cgi_rtc_data(HTTPSRV_CGI_REQ_STRUCT* param) { #define BUFF_SIZE sizeof("00\n00\n00\n") HTTPSRV_CGI_RES_STRUCT response; TIME_STRUCT time; uint_32 min; uint_32 hour; uint_32 sec; char str[BUFF_SIZE]; uint_32 length = 0; if (param->request_method != HTTPSRV_REQ_GET) { return(0); } _time_get(&time); sec = time.SECONDS % 60; min = time.SECONDS / 60; hour = min / 60; min %= 60; response.ses_handle = param->ses_handle; response.content_type = HTTPSRV_CONTENT_TYPE_PLAIN; response.status_code = 200; /* ** When the keep-alive is used we have to calculate a correct content length ** so the receiver knows when to ACK the data and continue with a next request. ** Please see RFC2616 section 4.4 for further details. */ /* Calculate content length while saving it to buffer */ length = snprintf(str, BUFF_SIZE, "%ld\n%ld\n%ld\n", hour, min, sec); response.data = str; response.data_length = length; response.content_length = response.data_length; /* Send response */ HTTPSRV_cgi_write(&response); return (response.content_length); }
int32_t MFS_Close_file ( MFS_HANDLE_PTR handle, MFS_DRIVE_STRUCT_PTR drive_ptr ) { TIME_STRUCT time; DATE_STRUCT clk_time; _mfs_error error_code; error_code = MFS_lock_dos_disk( drive_ptr ); if ( error_code != MFS_NO_ERROR ) { return error_code; } #if !MFSCFG_READ_ONLY #if MFSCFG_READ_ONLY_CHECK if (MFS_is_read_only (drive_ptr)) { error_code = MFS_DISK_IS_WRITE_PROTECTED; } #endif if ((handle->TOUCHED) && (error_code == MFS_NO_ERROR)) { _time_get(&time); _time_to_date(&time, &clk_time); NORMALIZE_DATE(&clk_time); mqx_htods(handle->DIR_ENTRY.TIME, PACK_TIME(clk_time)); mqx_htods(handle->DIR_ENTRY.DATE, PACK_DATE(clk_time)); error_code = MFS_Update_entry(drive_ptr, handle); } #endif MFS_Free_handle(drive_ptr, handle); MFS_unlock(drive_ptr,TRUE); return(int32_t) error_code; }
void *MFS_Create_file ( MFS_DRIVE_STRUCT_PTR drive_ptr, unsigned char attr, /*[IN] attribute for created file*/ char *pathname, /*[IN] directory and filename of the file to be created */ _mfs_error_ptr error_ptr /*[IN/OUT] error code is written to this address */ ) { MFS_HANDLE_PTR handle; MFS_HANDLE_PTR next_handle; MFS_DIR_ENTRY_PTR dir_entry_ptr; TIME_STRUCT time; DATE_STRUCT clk_time; uint32_t dir_cluster; uint32_t dir_index; char access; _mfs_error error_code, saved_code = 0; if ( (pathname == NULL) || (*pathname == '\0') ) { *error_ptr = MFS_INVALID_PARAMETER; return( NULL ); } #if MFSCFG_READ_ONLY_CHECK if (MFS_is_read_only (drive_ptr)) { *error_ptr = MFS_DISK_IS_WRITE_PROTECTED; return NULL; } #endif error_code = MFS_lock_dos_disk( drive_ptr ); if ( error_code != MFS_NO_ERROR ) { *error_ptr = error_code; return( NULL ); } handle = NULL; attr &= (MFS_ATTR_READ_ONLY | MFS_ATTR_HIDDEN_FILE | MFS_ATTR_SYSTEM_FILE | MFS_ATTR_ARCHIVE | MFS_ATTR_VOLUME_NAME); attr |= MFS_ATTR_ARCHIVE; access = (attr & MFS_ATTR_READ_ONLY) ? MFS_ACCESS_READ_ONLY : MFS_ACCESS_READ_WRITE; dir_entry_ptr = MFS_Create_entry_slave(drive_ptr, attr, pathname, &dir_cluster, &dir_index, &error_code, TRUE); if ( (dir_entry_ptr != NULL) && !(attr & MFS_ATTR_VOLUME_NAME) ) { handle = MFS_Get_handle(drive_ptr,dir_entry_ptr); if ( handle != NULL ) { handle->DIR_CLUSTER = dir_cluster; handle->DIR_INDEX = dir_index; handle->ACCESS = access; handle->CURRENT_CLUSTER = 0; handle->PREVIOUS_CLUSTER = 0; /* ** If file exists, overwrite and set size to 0 */ if ( error_code == MFS_FILE_EXISTS ) { _time_get(&time); _time_to_date(&time, &clk_time); NORMALIZE_DATE(&clk_time); saved_code = MFS_Release_chain(drive_ptr, clustoh(handle->DIR_ENTRY.HFIRST_CLUSTER, handle->DIR_ENTRY.LFIRST_CLUSTER)); if ( saved_code == MFS_NO_ERROR || saved_code == MFS_LOST_CHAIN ) { clustod(handle->DIR_ENTRY.HFIRST_CLUSTER, handle->DIR_ENTRY.LFIRST_CLUSTER, 0); mqx_htodl(handle->DIR_ENTRY.FILE_SIZE, 0L); mqx_htodc(handle->DIR_ENTRY.ATTRIBUTE, attr); mqx_htods(handle->DIR_ENTRY.TIME, PACK_TIME(clk_time)); mqx_htods(handle->DIR_ENTRY.DATE, PACK_DATE(clk_time)); error_code = MFS_Update_entry(drive_ptr, handle); /* ** If the same file is already open, mark it as 'freshly ** truncated' so reads and writes don't clobber any data. */ if ( error_code == MFS_NO_ERROR ) { next_handle = (MFS_HANDLE_PTR) _queue_head(&drive_ptr->HANDLE_LIST); while ( next_handle ) { if ( next_handle->DIR_CLUSTER == dir_cluster && next_handle->DIR_INDEX == dir_index ) { next_handle->CURRENT_CLUSTER = 0; next_handle->PREVIOUS_CLUSTER = 0; } next_handle = (MFS_HANDLE_PTR) _queue_next(&drive_ptr->HANDLE_LIST, (QUEUE_ELEMENT_STRUCT_PTR) next_handle); } } } } /* ** No need to update the disk image if we didn't change anything. */ if ( (mqx_dtohc(handle->DIR_ENTRY.ATTRIBUTE) != attr) && (error_code == MFS_NO_ERROR) ) { mqx_htodc(handle->DIR_ENTRY.ATTRIBUTE, attr); error_code = MFS_Update_entry(drive_ptr, handle); } } else { error_code = MFS_INSUFFICIENT_MEMORY; } } MFS_unlock(drive_ptr,FALSE); if ( error_code == MFS_NO_ERROR && saved_code == MFS_LOST_CHAIN ) { *error_ptr = saved_code; } else { *error_ptr = error_code; } return((void *)handle); }
int32_t Shell_smtp (int32_t argc, char *argv[]) { struct addrinfo hints; struct addrinfo *result, *rp; int32_t retval = 0; uint32_t sfd = 0; int32_t err_code = 0; bool print_help; bool shorthelp = FALSE; SMTP_PARAM_STRUCT params; char *errstr = NULL; char *server = NULL; char *optstring = ":f:t:s:u:p:m:h"; int32_t next_option; char *email_text = NULL; uint32_t email_size = 0; TIME_STRUCT time; DATE_STRUCT date; char date_str[DATE_LENGTH]; SHELL_GETOPT_CONTEXT gopt_context; params.login = NULL; params.pass = NULL; print_help = Shell_check_help_request(argc, argv, &shorthelp); if (print_help) { if (!shorthelp) { fprintf(stdout,"Usage:"); } fprintf(stdout, "%s", argv[0]); print_usage(stdout, shorthelp); err_code = SHELL_EXIT_SUCCESS; return(err_code); } /* Parse command line options */ Shell_getopt_init(&gopt_context); do { next_option = Shell_getopt(argc, argv, optstring, &gopt_context); switch (next_option) { case 'f': params.envelope.from = gopt_context.optarg; break; case 't': params.envelope.to = gopt_context.optarg; break; case 'm': params.text = gopt_context.optarg; break; case 's': server = gopt_context.optarg; break; case 'u': params.login = gopt_context.optarg; break; case 'p': params.pass = gopt_context.optarg; break; case 'h': print_usage (stdout, FALSE); return(SHELL_EXIT_SUCCESS); case '?': /* User specified an invalid option. */ print_usage(stderr, TRUE); return(SHELL_EXIT_ERROR); case ':': /* Option has a missing parameter. */ printf("Option -%c requires a parameter.\n", next_option); return(SHELL_EXIT_ERROR); case -1: /* Done with options. */ break; default: break; } }while(next_option != -1); _time_get(&time); _time_to_date(&time,&date); snprintf(date_str, DATE_LENGTH, "%s, %d %s %d %02d:%02d:%02d -0000", wday[date.WDAY],date.DAY, months[date.MONTH-1],date.YEAR, date.HOUR, date.MINUTE, date.SECOND); /* Evaluate email size */ email_size = strlen(params.envelope.from) + strlen(params.envelope.to) + strlen(params.text) + strlen(date_str) + strlen("From: <>\r\n") + strlen("To: <>\r\n") + strlen("Subject: Freescale Tower Email\r\n") + strlen("Date: \r\n\r\n") + strlen("\r\n") + 1; /* Allocate space */ email_text = (char *) _mem_alloc_zero(email_size); if (email_text == NULL) { fprintf(stderr, " Unable to allocate memory for email message.\n"); err_code = SHELL_EXIT_ERROR; return(err_code); } /* Prepare email message */ snprintf(email_text, email_size, "From: <%s>\r\n" "To: <%s>\r\n" "Subject: Freescale Tower Email\r\n" "Date: %s\r\n\r\n" "%s\r\n", params.envelope.from, params.envelope.to, date_str, params.text); params.text = email_text; _mem_zero(&hints, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; /* TCP socket */ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ hints.ai_protocol = 0; /* Any protocol */ hints.ai_canonname = NULL; hints.ai_addr = NULL; hints.ai_next = NULL; retval = getaddrinfo(server, NULL, &hints, &result); if (retval != 0) { fprintf(stderr, " getaddrinfo failed. Error: 0x%X\n", retval); err_code = SHELL_EXIT_ERROR; return(err_code); } /* Allocate buffer for error message */ errstr = (char *) _mem_alloc_system_zero(ERR_MSG_BUFF_SIZE); /* Try to send email using one of addresses. If it fails try another one. */ for (rp = result; rp != NULL; rp = rp->ai_next) { _mem_copy(rp->ai_addr, ¶ms.server, sizeof(params.server)); /* Try to send email */ retval = SMTP_send_email(¶ms, errstr, ERR_MSG_BUFF_SIZE); /* If connection failed try another address */ if (retval != SMTP_ERR_CONN_FAILED) { break; } } /* No address succeeded */ if (rp == NULL) { fprintf(stderr, " Unable to connect to %s.\n", server); err_code = SHELL_EXIT_ERROR; } if (retval != SMTP_OK) { printf(" Email sending failed%s %s\n", (strlen(errstr) > 0) ? ":":".", errstr); err_code = SHELL_EXIT_ERROR; } else { printf(" Email send. Server response: %s", errstr); } /* Cleanup */ freeaddrinfo(result); _mem_free(errstr); _mem_free(email_text); return(err_code); }
MFS_DIR_ENTRY_PTR MFS_Create_directory_entry ( MFS_DRIVE_STRUCT_PTR drive_ptr, /*[IN] the drive on which to operate */ char_ptr filename_ptr, /*[IN] pointer to the file's name */ char attribute, /*[IN] attribute to be assigned to the file */ uint_32_ptr dir_cluster_ptr, /*[IN/OUT] indicates cluster in which the entry was put.*/ uint_32_ptr dir_index_ptr, /*[IN/OUT] index of the location of new file within the directory */ uint_32_ptr error_ptr /*[IN/OUT] error_return_address */ ) { MFS_DIR_ENTRY_PTR dir_entry_ptr, dir_entry_ptr1; MFS_DIR_ENTRY_PTR temp_entry_ptr; TIME_STRUCT time; DATE_STRUCT clk_time; uint_32 dir_index, saved_index, temp_index; uint_32 entry_cluster, saved_cluster, temp_cluster; uint_32 needed_entries, i; int_32 length; boolean found_all = FALSE; char temp_name[SFILENAME_SIZE+1], short_name[SFILENAME_SIZE+1]; uchar checksum = 0; uint_32 prev_cluster= CLUSTER_INVALID, saved_prev_cluster, temp_prev_cluster; dir_entry_ptr = NULL; entry_cluster = *dir_cluster_ptr; dir_index = 0; /* ** Check for duplicate file */ if ( filename_ptr == NULL ) { *error_ptr = MFS_INVALID_PARAMETER; } else if ( *filename_ptr == '\0' ) { *error_ptr = MFS_FILE_NOT_FOUND; } #if MFSCFG_READ_ONLY_CHECK else if (MFS_is_read_only (NULL, drive_ptr)) { *error_ptr = MFS_DISK_IS_WRITE_PROTECTED; } #endif else if ( (dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, filename_ptr, &entry_cluster, &dir_index, &prev_cluster, MFS_ATTR_ANY, error_ptr)) != NULL ) { *error_ptr = MFS_FILE_EXISTS; } else { /* ** Search for an empty slot */ dir_index = 0; dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &entry_cluster, &dir_index, &prev_cluster, attribute, error_ptr); if ( MFS_Dirname_valid(filename_ptr) ) { found_all = TRUE; } if ( dir_entry_ptr == NULL && !*error_ptr ) { found_all = TRUE; } /* ** Save it now because we might not go into the while loop ** If we don't go into while, we lose original values when these ** variables are restored after the while loop */ saved_cluster = entry_cluster; saved_index = dir_index; while ( !found_all ) { /* Calculate the amount of extra entries needed for LFN's */ saved_cluster = entry_cluster; saved_index = dir_index; for ( needed_entries = (strlen(filename_ptr) + 12) / 13; needed_entries >= 1 && !found_all; needed_entries-- ) { *error_ptr = MFS_Increment_dir_index(drive_ptr, &entry_cluster, &dir_index, NULL); if ( entry_cluster == CLUSTER_EOF && !*error_ptr ) { /* This means the LFN will span a cluster. */ found_all = TRUE; break; } temp_index = dir_index; temp_cluster = entry_cluster; temp_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &entry_cluster, &dir_index, &prev_cluster, MFS_ATTR_ANY,error_ptr); if ( *error_ptr ) { return NULL; } if ( !temp_entry_ptr ) { found_all = TRUE; dir_entry_ptr = NULL; break; } else if ( dir_index == temp_index && temp_cluster == entry_cluster ) { continue; } else { break; } } if ( needed_entries == 0 ) { found_all = TRUE; } if ( found_all ) { dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &saved_cluster, &saved_index, &saved_prev_cluster, MFS_ATTR_ANY,error_ptr); } } entry_cluster = saved_cluster; dir_index = saved_index; if ( dir_entry_ptr == NULL && !*error_ptr ) { /* ** Empty spot not found... get a new cluster and use the first entry. */ dir_index = 0; if ( entry_cluster == 0 ) { *error_ptr = MFS_ROOT_DIR_FULL; return(NULL); } else { *error_ptr = MFS_Add_cluster_to_chain(drive_ptr, entry_cluster, &entry_cluster); if ( *error_ptr == MFS_NO_ERROR ) { *error_ptr = MFS_Clear_cluster(drive_ptr, entry_cluster); if ( *error_ptr ) { return(NULL); } dir_entry_ptr = (pointer) drive_ptr->DIR_SECTOR_PTR; *error_ptr = MFS_Write_back_fat(drive_ptr); if ( *error_ptr ) { return(NULL); } } } } } if ( *error_ptr == MFS_NO_ERROR ) { /* At this point we have one of the following cases ** ** 1. We have a normal 8.3 filename and an empty slot for it ** 2. We have a LFN, and a slot which has enough consecutive free slots ** to store the LFN, followed by the actual entry */ /* If the file is a normal 8.3 file */ if ( MFS_Dirname_valid(filename_ptr) ) { _mem_zero(dir_entry_ptr, sizeof (MFS_DIR_ENTRY)); htodc(dir_entry_ptr->ATTRIBUTE, attribute); MFS_Expand_dotfile(filename_ptr, dir_entry_ptr->NAME); _time_get(&time); _time_to_date(&time, &clk_time); NORMALIZE_DATE(&clk_time); htods(dir_entry_ptr->TIME, PACK_TIME(clk_time)); htods(dir_entry_ptr->DATE, PACK_DATE(clk_time)); drive_ptr->DIR_SECTOR_DIRTY = TRUE; } else { /* Case where the file is a LFN */ length = strlen(filename_ptr); /* Get the 8.3 name and calculate the checksum */ temp_index = 0; temp_cluster = *dir_cluster_ptr; *error_ptr = MFS_lfn_to_sfn(filename_ptr, temp_name); if ( !*error_ptr ) { do { dir_entry_ptr1 = MFS_Find_directory_entry(drive_ptr, temp_name, &temp_cluster, &temp_index, &temp_prev_cluster, MFS_ATTR_ANY, error_ptr); if ( !*error_ptr && dir_entry_ptr1 != NULL ) { MFS_increment_lfn(temp_name); temp_index = 0; } } while ( !*error_ptr && dir_entry_ptr1 != NULL ); dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &entry_cluster, &dir_index, &prev_cluster, attribute, error_ptr); /* ** The memory should be cleared after finding the directory entry. */ _mem_zero(dir_entry_ptr, sizeof (MFS_DIR_ENTRY)); /* Setup the final slot */ ((MFS_LNAME_ENTRY_PTR) dir_entry_ptr)->ID |= MFS_LFN_END; drive_ptr->DIR_SECTOR_DIRTY = TRUE; MFS_Expand_dotfile(temp_name,short_name); checksum = MFS_lfn_checksum(short_name); } while ( length && !*error_ptr ) { i = length - ((length -1) % 13 + 1); *error_ptr = MFS_lfn_name_to_entry(filename_ptr + i, (MFS_LNAME_ENTRY_PTR) dir_entry_ptr); /* Set the entry number, the checksum value and the attribute */ ((MFS_LNAME_ENTRY_PTR) dir_entry_ptr)->ID |= (length + 12) / 13; ((MFS_LNAME_ENTRY_PTR) dir_entry_ptr)->ALIAS_CHECKSUM= checksum; ((MFS_LNAME_ENTRY_PTR) dir_entry_ptr)->ATTR = MFS_ATTR_LFN; drive_ptr->DIR_SECTOR_DIRTY = TRUE; length -= (length - i); if ( length < 0 ) { length = 0; } if ( length && !*error_ptr ) { dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &entry_cluster, &dir_index, &prev_cluster, attribute, error_ptr); if ( dir_entry_ptr == NULL && !*error_ptr ) { /* No empty spots... We need to get a new cluster */ dir_index = 0; if ( entry_cluster == 0 ) { *error_ptr = MFS_ROOT_DIR_FULL; return(NULL); } else { *error_ptr = MFS_Add_cluster_to_chain( drive_ptr, entry_cluster, &entry_cluster); if ( *error_ptr == MFS_NO_ERROR ) { *error_ptr = MFS_Clear_cluster(drive_ptr, entry_cluster); if ( *error_ptr ) { return(NULL); } dir_entry_ptr = (pointer) drive_ptr->DIR_SECTOR_PTR; *error_ptr = MFS_Write_back_fat(drive_ptr); if ( *error_ptr ) { return(NULL); } } } } _mem_zero(dir_entry_ptr, sizeof (MFS_DIR_ENTRY)); drive_ptr->DIR_SECTOR_DIRTY = TRUE; } } /* LFN is written, so write the actual entry */ if ( !*error_ptr ) { dir_entry_ptr = MFS_Find_directory_entry(drive_ptr, "", &entry_cluster, &dir_index, &prev_cluster, attribute, error_ptr); if ( dir_entry_ptr == NULL && !*error_ptr ) { dir_index = 0; if ( entry_cluster == 0 ) { *error_ptr = MFS_ROOT_DIR_FULL; return(NULL); } else { *error_ptr = MFS_Add_cluster_to_chain( drive_ptr, entry_cluster, &entry_cluster); if ( *error_ptr == MFS_NO_ERROR ) { *error_ptr = MFS_Clear_cluster(drive_ptr, entry_cluster); if ( *error_ptr ) { return(NULL); } dir_entry_ptr = (pointer) drive_ptr->DIR_SECTOR_PTR; *error_ptr = MFS_Write_back_fat(drive_ptr); if ( *error_ptr ) { return(NULL); } } } } _mem_zero(dir_entry_ptr, sizeof (MFS_DIR_ENTRY)); htodc(dir_entry_ptr->ATTRIBUTE, attribute); MFS_Expand_dotfile(temp_name, dir_entry_ptr->NAME); _time_get(&time); _time_to_date(&time, &clk_time); NORMALIZE_DATE(&clk_time); htods(dir_entry_ptr->TIME, PACK_TIME(clk_time)); htods(dir_entry_ptr->DATE, PACK_DATE(clk_time)); drive_ptr->DIR_SECTOR_DIRTY = TRUE; } } if ( *error_ptr ) { return(NULL); } } *dir_cluster_ptr = entry_cluster; *dir_index_ptr = dir_index; return(dir_entry_ptr); }
void main_task ( uint_32 initial_data ) { DATE_STRUCT time_rtc; TIME_STRUCT time_mqx; if (_lwevent_create(&lwevent,0) != MQX_OK) { printf("\nMake event failed"); _task_block(); } printf ("\fStart time (MQX synchronized to RTC time during bsp init):\n\n"); /* initialize time */ time_rtc.YEAR = 2010; time_rtc.MONTH = 10; time_rtc.DAY = 15; time_rtc.HOUR = 10; time_rtc.MINUTE = 8; time_rtc.SECOND = 0; time_rtc.MILLISEC = 0; _time_from_date (&time_rtc, &time_mqx); _time_set( &time_mqx); if( _rtc_sync_with_mqx(FALSE) != MQX_OK ) { printf("\nError synchronize time!\n"); _task_block(); } _time_get (&time_mqx); _time_to_date (&time_mqx, &time_rtc); print_mqx_time(&time_rtc, &time_mqx); print_current_time(); /* except MPC5125 */ #ifndef BSP_TWRMPC5125 install_interrupt(); /* enable stopwatch */ install_stopwatch(); /* enable alarm */ install_alarm(); _lwevent_wait_ticks(&lwevent,LWE_ALARM,FALSE,0); _lwevent_clear(&lwevent,LWE_ALARM); printf ("\nALARM!\n"); print_current_time(); /* end of alarm */ printf ("Continue wasting time (2 minutes max) ...\n"); _lwevent_wait_ticks(&lwevent,LWE_STOPWATCH,FALSE,0); _lwevent_clear(&lwevent,LWE_STOPWATCH); printf ("\nSTOPWATCH!\n"); print_current_time(); printf ("\nClearing RTC:\n"); _rtc_init (RTC_INIT_FLAG_CLEAR | RTC_INIT_FLAG_ENABLE); print_current_time(); install_alarm(); _lwevent_wait_ticks(&lwevent,LWE_ALARM,FALSE,0); _lwevent_clear(&lwevent,LWE_ALARM); printf ("ALARM!\n"); print_current_time(); #else /* BSP_TWRMPC5125 */ printf ("Waste 10 seconds here\n"); _time_delay(10000); _rtc_get_time_mqxd (&time_rtc); print_rtc_time(&time_rtc, &time_mqx); #endif printf ("Synchronize RTC to MQX time again:\n"); _rtc_sync_with_mqx (FALSE); _rtc_get_time_mqxd (&time_rtc); _time_from_date (&time_rtc, &time_mqx); print_rtc_time(&time_rtc, &time_mqx); #if PSP_HAS_IRTC == 1 irtc_test(); #endif /* PSP_HAS_IRTC == 1 */ /* Test tamper event functionality on MCF51EMxx device */ #if PSP_MQX_CPU_IS_MCF51EM test_tamper(); #else printf ("Finish, press/hold reset to repeat.\n"); _task_block() ; #endif }
/*TASK*------------------------------------------------------------------- * * Task Name : main_task * Comments : * *END*----------------------------------------------------------------------*/ void main_task ( uint_32 dummy ) { MQX_FILE_PTR qspifd; int_32 ret = 0, i, byte_write, byte_read; uint_8_ptr data; uint_8 test_data[512]; TIME_STRUCT start_time, end_time, diff_time; printf ("\n-------------- QSPI driver example --------------\n\n"); printf ("This example application demonstrates usage of QSPI driver.\n"); /* Open the QSPI driver */ qspifd = fopen (TEST_CHANNEL, NULL); if (qspifd == NULL) { printf ("Error opening QSPI driver!\n"); _time_delay (200L); _task_block (); } /* erase all */ printf("\n\n************************************************************************\n"); printf("Erase the first flash chip, for S25FL128S/256S, it might take 30s/60s....\n"); printf("************************************************************************\n"); _time_get(&start_time); memory_chip_erase(qspifd, FLASH_BASE_ADR); _time_get(&end_time); _time_diff(&start_time, &end_time, &diff_time); printf("\nErase whole flash %ld sec, %ld millisec\n", diff_time.SECONDS, diff_time.MILLISECONDS); printf("Finish erase all flash\n"); printf("\n\n*****************************************\n"); printf("*** Function Test <memory_read_data> ****\n"); printf("*****************************************\n"); printf("From Flash %08x: first 20 btyes\n", TEST_BUFFER1); byte_read = memory_read_data(qspifd, TEST_BUFFER1, 20, test_data); if (byte_read < 0) { printf("memory_read_data failed!\n"); return; } for (i = 0; i < 20; i++) { printf("0x%02x ", test_data[i]); } printf("\n\n*****************************************\n"); printf("*** Function Test <memory_read_byte> ****\n"); printf("*****************************************\n"); printf("From Flash %08x: first 20 bytes\n", TEST_BUFFER1); for (i = 0; i < 20; i++) { printf("0x%02x ", memory_read_byte(qspifd, TEST_BUFFER1 + i)); } printf("\n"); printf("\n\n*****************************************\n"); printf("*** Function Test <memory_write_data> ***\n"); printf("*****************************************\n"); data = (uint_8_ptr)_mem_alloc_zero(TEST_BUF_SIZE1); if (data == NULL) { printf("fail to allocate write buffer\n"); fclose(qspifd); return; } for (i = 0; i < TEST_BUF_SIZE1; i++) { data[i] = i % 256; } _time_get(&start_time); byte_write = memory_write_data (qspifd, TEST_BUFFER1, TEST_BUF_SIZE1, data); if (byte_write < 0) { printf("memory_write_data failed!\n"); return; } _time_get(&end_time); _time_diff(&start_time, &end_time, &diff_time); printf("\ndata = %d, Time spends on Flash write is %ld sec, %ld millisec, rate = %ld kbps\n", byte_write, diff_time.SECONDS, diff_time.MILLISECONDS, (byte_write)/(diff_time.SECONDS * 1000 + diff_time.MILLISECONDS)); printf("\n\n*****************************************\n"); printf("***** Time Test <memory_read_data> ******\n"); printf("*****************************************\n"); for (i = 0; i < TEST_BUF_SIZE1; i++) { data[i] = 255; } _time_get(&start_time); byte_read = memory_read_data (qspifd, TEST_BUFFER1, TEST_BUF_SIZE1, data); if (byte_read < 0) { printf("memory_read_data failed!\n"); return; } _time_get(&end_time); _time_diff(&start_time, &end_time, &diff_time); printf("\ndata = %d, Time spends on Flash read is %ld sec, %ld millisec, rate = %ld kbps\n\n", byte_write, diff_time.SECONDS, diff_time.MILLISECONDS, (byte_write)/(diff_time.SECONDS * 1000 + diff_time.MILLISECONDS)); printf("memory_read_data read data from %08x: first 20 bytes \n", TEST_BUFFER1); for(i = 0; i < 20; i++) { printf ("0x%02x ", data[i]); } printf("\n"); printf("\n\n*****************************************\n"); printf("**** Compare Test <memory_read_byte> ****\n"); printf("*****************************************\n"); printf("memory_read_byte from %08x: first 20 bytes \n", TEST_BUFFER1); for (i = 0; i < 20; i++) { printf("0x%02x ", memory_read_byte(qspifd, TEST_BUFFER1 + i)); } printf("\n"); /* Close the SPI */ _mem_free(data); ret = (uint_32)fclose (qspifd); if (ret) { printf ("Error closing QSPI, returned: 0x%08x\n", ret); } printf ("\n-------------- End of example --------------\n\n"); }
static uint_32 DHCPSRV_expire ( DHCPSRV_STATE_STRUCT_PTR state ) { /* Body */ DHCPSRV_ADDR_STRUCT_PTR addr; TIME_STRUCT time; uint_32 elapsed; RTCS_mutex_lock(&state->IPLIST_SEM); _time_get(&time); /* Expire offers */ elapsed = time.SECONDS - state->TIME; while (state->IP_OFFERED) { if (state->IP_OFFERED->EXPIRE > elapsed) { state->IP_OFFERED->EXPIRE -= elapsed; break; } /* Endif */ addr = state->IP_OFFERED; state->IP_OFFERED = addr->NEXT; elapsed -= addr->EXPIRE; DHCPSRV_lease_start(&state->IP_AVAIL, addr, 0); } /* Endwhile */ /* Expire leases */ elapsed = time.SECONDS - state->TIME; while (state->IP_LEASED && state->IP_LEASED->EXPIRE < DHCP_LEASE_INFINITE) { if (state->IP_LEASED->EXPIRE > elapsed) { state->IP_LEASED->EXPIRE -= elapsed; break; } /* Endif */ addr = state->IP_LEASED; state->IP_LEASED = addr->NEXT; elapsed -= addr->EXPIRE; DHCPSRV_lease_start(&state->IP_AVAIL, addr, 0); } /* Endwhile */ /* Start CR 1547 */ /* Expire takens */ elapsed = time.SECONDS - state->TIME; while (state->IP_TAKEN && state->IP_TAKEN->EXPIRE < DHCP_LEASE_INFINITE) { if (state->IP_TAKEN->EXPIRE > elapsed) { state->IP_TAKEN->EXPIRE -= elapsed; break; } /* Endif */ addr = state->IP_TAKEN; state->IP_TAKEN = addr->NEXT; elapsed -= addr->EXPIRE; DHCPSRV_lease_start(&state->IP_AVAIL, addr, 0); } /* Endwhile */ /* End CR 1547 */ /* Calculate time to next expiry */ elapsed = DHCP_LEASE_INFINITE; if (state->IP_OFFERED && state->IP_OFFERED->EXPIRE < elapsed) { elapsed = state->IP_OFFERED->EXPIRE; } /* Endif */ if (state->IP_LEASED && state->IP_LEASED->EXPIRE < elapsed) { elapsed = state->IP_LEASED->EXPIRE; } /* Endif */ if (elapsed == DHCP_LEASE_INFINITE) { elapsed = 0; } /* Endif */ state->TIME = time.SECONDS; RTCS_mutex_unlock(&state->IPLIST_SEM); return elapsed; } /* Endbody */
void main_task ( uint32_t initial_data ) { /* Body */ uint32_t rtc_time; uint32_t rtc_time_default; TIME_STRUCT mqx_time; DATE_STRUCT date_time; if (_lwevent_create(&lwevent,0) != MQX_OK) { printf("\nMake event failed"); _task_block(); } printf ("\f RTC Demo :\n\n"); _rtc_get_time(&rtc_time); print_rtc_time(rtc_time); /* initialize time */ date_time.YEAR = RTC_TIME_INIT_TM_YEAR; date_time.MONTH = RTC_TIME_INIT_TM_MON; date_time.DAY = RTC_TIME_INIT_TM_MDAY; date_time.HOUR = RTC_TIME_INIT_TM_HOUR; date_time.MINUTE = RTC_TIME_INIT_TM_MIN; date_time.SECOND = RTC_TIME_INIT_TM_SEC; date_time.MILLISEC = 0; /* Convert date time to time struct */ if ( _time_from_date(&date_time, &mqx_time) == FALSE) { printf("\n Cannot convert date_time "); _task_block(); } /* Convert rtc time to TIME_STRUCT */ rtc_time = mqx_time.SECONDS; printf(" Set RTC time is: %s %s %3d %.2d:%.2d:%.2d %d\n", _days_abbrev[date_time.WDAY], _months_abbrev[date_time.MONTH - 1], date_time.DAY, date_time.HOUR, date_time.MINUTE, date_time.SECOND, date_time.YEAR); /* Set MQX time*/ _time_set(&mqx_time); /* Set RTC time*/ _rtc_set_time(rtc_time); printf("\n MQX time: %d SECONDS, %d MILISECOND ", mqx_time.SECONDS, mqx_time.MILLISECONDS); /* * set-up alarm */ /* install callback */ _rtc_callback_reg((INT_ISR_FPTR)rtc_callback, (void*)NULL); /* Set alarm to maximum time to avoid unexpected interrupt in next running */ _rtc_set_alarm(MAXIMUM_SECONDS_IN_TIME,(uint32_t)0); _lwevent_clear(&lwevent,LWE_ALARM); /* Get current time */ _rtc_get_time(&rtc_time); /* setup alarm in next 10 seconds & period 4 seconds*/ rtc_time += (uint32_t)ALARM_NEXT_TIME; /* Alarm occurs in next 10 seconds */ printf("\n Setup to occur alarm in next %d seconds & with period: %d seconds",ALARM_NEXT_TIME, ALARM_PERIOD); _rtc_set_alarm(rtc_time,(uint32_t)ALARM_PERIOD); printf("\n Wait %d seconds ....",ALARM_NEXT_TIME); /* Wait to clear LWE_ALARM event */ _lwevent_wait_ticks(&lwevent,LWE_ALARM,FALSE,0); _lwevent_clear(&lwevent,LWE_ALARM); printf ("\nALARM! ALARM! ALARM!\n"); /* Print current time */ _rtc_get_time(&rtc_time); print_rtc_time(rtc_time); printf("\n Wait next alarm in %d seconds....",ALARM_PERIOD); _lwevent_wait_ticks(&lwevent,LWE_ALARM,FALSE,0); _lwevent_clear(&lwevent,LWE_ALARM); printf ("\nALARM! ALARM! ALARM!\n"); _rtc_get_time(&rtc_time); print_rtc_time(rtc_time); printf ("\nClearing RTC:\n"); _rtc_set_time(0); _rtc_get_time(&rtc_time_default); print_rtc_time(rtc_time_default); /* Wait 2 seconds after resynchronize rtc time with mqx time*/ do{ _rtc_get_time(&rtc_time); } while ((rtc_time - rtc_time_default) < 2); /* Get current time & display on terminal */ _rtc_get_time(&rtc_time); print_rtc_time(rtc_time); printf ("Synchronize RTC to MQX time again:\n"); _time_get(&mqx_time); rtc_time = mqx_time.SECONDS; _rtc_set_time(rtc_time); _rtc_get_time(&rtc_time); print_rtc_time(rtc_time); #if PSP_HAS_IRTC == 1 irtc_test(); #endif /* PSP_HAS_IRTC == 1 */ printf ("Finish, press/hold reset to repeat.\n"); _task_block() ; } /* Endbody */