int io_getc(char *c) { int ci; int status; status = usart_read_char(&CONFIG_CONSOLE_PORT, &ci); if (status == USART_RX_EMPTY) return 1; if (status == USART_RX_ERROR) { CONFIG_CONSOLE_PORT.cr = AVR32_USART_CR_RSTSTA_MASK; return 1; } /* Echo char. */ if (ci == '\r') usart_putchar(&CONFIG_CONSOLE_PORT, '\n'); else if (ci == '\b'){ usart_putchar(&CONFIG_CONSOLE_PORT, ci); usart_putchar(&CONFIG_CONSOLE_PORT, ' '); usart_putchar(&CONFIG_CONSOLE_PORT, ci); } else usart_putchar(&CONFIG_CONSOLE_PORT, ci); *c = ci; return 0; }
static void usart_int_handler(void) { int c; // Empfangenes Datum usart_read_char(USART_0, &c); //INTERRUPT CODE USART }
int usart_getchar(volatile avr32_usart_t *usart) { int c, ret; while ((ret = usart_read_char(usart, &c)) == USART_RX_EMPTY); if (ret == USART_RX_ERROR) return USART_FAILURE; return c; }
void ISO7816_Usart_ISR_Test (void) { static int n = 0; static int c; // static int x; // Test for tx int // Tx int enabled ? if (1 == EXAMPLE_INT_USART->IMR.txempty) { // Yes // Data send ? if (1 == EXAMPLE_INT_USART->CSR.txempty) { // Data send EXAMPLE_INT_USART->IDR.txempty = 1; usart_reset_tx (EXAMPLE_INT_USART); /* // Disable tx empty interrupt Disable_global_interrupt(); EXAMPLE_INT_USART->IDR.txempty = 1; Enable_global_interrupt(); while (1 == EXAMPLE_INT_USART->IMR.txempty) ; */ /* // clearing interrupt request x = EXAMPLE_INT_USART->cr; EXAMPLE_INT_USART->cr = x; x = EXAMPLE_INT_USART->cr; */ } } else { n++; } // Test for rx int if (1 == EXAMPLE_INT_USART->CSR.rxrdy) { // Data is ready usart_read_char (EXAMPLE_INT_USART, &c); // EXAMPLE_INT_USART->IER.txempty = 1; // Send data // usart_write_char(EXAMPLE_INT_USART, c); // Enable tx int Disable_global_interrupt (); EXAMPLE_INT_USART->IER.txempty = 1; Enable_global_interrupt (); while (0 == EXAMPLE_INT_USART->IMR.txempty); } /* usart_read_char(EXAMPLE_INT_USART, &c); usart_write_char(EXAMPLE_INT_USART, c); */ n++; }
__interrupt #endif static void ISO7816_Usart_ISR (void) { static int n = 0; static int nError = 0; int c; int nRet; u16 SaveBufferPointer_u16; // Test for rx int if (1 == EXAMPLE_INT_USART->CSR.rxrdy) { // Get Data nRet = usart_read_char (EXAMPLE_INT_USART, &c); if (USART_RX_ERROR == nRet) { usart_reset_tx (ISO7816_USART); // Reset all usart_reset_rx (ISO7816_USART); nError++; return; } // Save old end pointer SaveBufferPointer_u16 = ISO7816_UsartRxBufferPointerEnd_u16; // Set next buffer position SaveBufferPointer_u16++; // End of buffer reached ? if (ISO7816_RX_BUFFER_SIZE <= SaveBufferPointer_u16) { // Yes SaveBufferPointer_u16 = 0; } // Buffer full ? if (ISO7816_UsartRxBufferPointerEnd_u16 == SaveBufferPointer_u16) { // Buffer full ISO7816_UsartRxBufferStatus_e = ISO7816_USART_INT_STATUS_OVERFLOW; } else { // Store char ISO7816_UsartRxBuffer_u8[ISO7816_UsartRxBufferPointerEnd_u16] = c; // Set external var to new buffer end ISO7816_UsartRxBufferPointerEnd_u16 = SaveBufferPointer_u16; } } n++; }
//! @brief This function wait a key press //! //! @return true, if the action must be continue //! bool ushell_more_wait( void ) { int c_key; printf("\n\r-- space for more--"); c_key=0; while( (c_key!='q') && (c_key!=' ') ) { usart_reset_status(SHL_USART); while(usart_read_char(SHL_USART, &c_key) != USART_SUCCESS); } printf("\r \r"); return (c_key==' '); }
/*! \brief Gets the full command line on RS232 input to be interpreted. * The cmd_str variable is built with the user inputs. */ static void fat_example_build_cmd(void) { int c; // If something new in the USART usart_reset_status(SHL_USART); if (usart_read_char(SHL_USART, &c) == USART_SUCCESS) { switch (c) { case CR: // Add LF. print_char(SHL_USART, LF); // Add NUL char. cmd_str[i_str] = '\0'; // Decode the command. fat_example_parse_cmd(); i_str = 0; break; // ^c abort cmd. case ABORT_CHAR: // Reset command length. i_str = 0; // Display prompt. print(SHL_USART, "\n" MSG_PROMPT); break; // Backspace. case BKSPACE_CHAR: if (i_str > 0) { // Replace last char. print(SHL_USART, "\b \b"); // Decraese command length. i_str--; } break; default: // Echo. print_char(SHL_USART, c); // Append to cmd line. cmd_str[i_str++] = c; break; } } }
__interrupt #endif void BUFFERED_SIO_Usart_ISR (void) { static int nRxBytes = 0; static int nTxBytes = 0; static int nIntCalls = 0; static int nError = 0; int c; int nRet; // clearing interrupt request c = BUFFERED_SIO_USART->cr; BUFFERED_SIO_USART->cr = c; c = BUFFERED_SIO_USART->cr; // Test for rx int if (1 == BUFFERED_SIO_USART->CSR.rxrdy) { // Get Data nRet = usart_read_char (BUFFERED_SIO_USART, &c); if (USART_RX_ERROR == nRet) { usart_reset_status (BUFFERED_SIO_USART); nError++; } else { BUFFERED_SIO_RxIntHandler (c); nRxBytes++; } } // Test for tx empty int if (1 == BUFFERED_SIO_USART->CSR.txempty) { BUFFERED_SIO_TxIntHandler (); nTxBytes++; } nIntCalls++; }
//! @brief Minimalist file editor to append char to a file //! //! @verbatim //! hit ^q to exit and save file //! @endverbatim //! void ushell_cmd_append_file( void ) { int c_key; if( g_s_arg[0][0] == 0 ) return; // Select file or directory if( !nav_setcwd( (FS_STRING)g_s_arg[0], true, false ) ) { fputs(MSG_ER_UNKNOWN_FILE, stdout); return; } // Open file if( !file_open(FOPEN_MODE_APPEND) ) { fputs(MSG_KO, stdout); return; } // Append file fputs(MSG_APPEND_WELCOME, stdout); while( 1 ) { usart_reset_status(SHL_USART); while(usart_read_char(SHL_USART, &c_key) != USART_SUCCESS); if( c_key == ASCII_CTRL_Q ) break; // ^q to quit putchar( c_key ); file_putc( c_key ); if( c_key == ASCII_CR ) { putchar(ASCII_LF); file_putc(ASCII_LF); } } // Close file file_close(); putchar(ASCII_CR);putchar(ASCII_LF); }
__interrupt #endif static void usart_int_handler(void) { int c; /* * In the code line below, the interrupt priority level does not need to * be explicitly masked as it is already because we are within the * interrupt handler. * The USART Rx interrupt flag is cleared by side effect when reading * the received character. * Waiting until the interrupt has actually been cleared is here useless * as the call to usart_write_char will take enough time for this before * the interrupt handler is left and the interrupt priority level is * unmasked by the CPU. */ usart_read_char(EXAMPLE_USART, &c); // Print the received character to USART. This is a simple echo. usart_write_char(EXAMPLE_USART, c); }
/*! \brief Minimalist file editor to append char to a file. * * \note Hit ^q to exit and save file. */ static void fat_example_append_file(void) { int c; print(SHL_USART, MSG_APPEND_WELCOME); // Wait for ^q to quit. while (TRUE) { // If something new in the USART usart_reset_status(SHL_USART); if (usart_read_char(SHL_USART, &c) == USART_SUCCESS) { // if this is not the quit char if (c != QUIT_APPEND) { // Echo the char. print_char(SHL_USART, c); // Add it to the file. file_putc(c); // if it is a carriage return. if (c == CR) { // Echo line feed. print_char(SHL_USART, LF); // Add line feed to the file. file_putc(LF); } } // Quit char received. else { // Exit the append function. break; } } } }
//! @brief This function initializes the hardware/software resources required for ushell task. //! void ushell_task_init(uint32_t pba_hz) { uint8_t u8_i; //** Initialize the USART used by uShell with the configured parameters static const gpio_map_t SHL_USART_GPIO_MAP = { {SHL_USART_RX_PIN, SHL_USART_RX_FUNCTION}, {SHL_USART_TX_PIN, SHL_USART_TX_FUNCTION} }; #if (defined __GNUC__) set_usart_base((void *)SHL_USART); gpio_enable_module(SHL_USART_GPIO_MAP, sizeof(SHL_USART_GPIO_MAP) / sizeof(SHL_USART_GPIO_MAP[0])); usart_init(SHL_USART_BAUDRATE); #elif (defined __ICCAVR32__) static const usart_options_t SHL_USART_OPTIONS = { .baudrate = SHL_USART_BAUDRATE, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; extern volatile avr32_usart_t *volatile stdio_usart_base; stdio_usart_base = SHL_USART; gpio_enable_module(SHL_USART_GPIO_MAP, sizeof(SHL_USART_GPIO_MAP) / sizeof(SHL_USART_GPIO_MAP[0])); usart_init_rs232(SHL_USART, &SHL_USART_OPTIONS, pba_hz); #endif //** Configure standard I/O streams as unbuffered. #if (defined __GNUC__) setbuf(stdin, NULL); #endif setbuf(stdout, NULL); // Set default state of ushell g_b_ushell_task_run = false; for( u8_i=0; u8_i<USHELL_HISTORY; u8_i++ ) { g_s_cmd_his[u8_i][0] = 0; // Set end of line for all cmd line history } fputs(MSG_EXIT, stdout ); g_u32_ushell_pba_hz = pba_hz; // Save value to manage a time counter during perform command #ifdef FREERTOS_USED xTaskCreate(ushell_task, configTSK_USHELL_NAME, configTSK_USHELL_STACK_SIZE, NULL, configTSK_USHELL_PRIORITY, NULL); #endif // FREERTOS_USED } #ifdef FREERTOS_USED /*! \brief Entry point of the explorer task management. * * This function performs uShell decoding to access file-system functions. * * \param pvParameters Unused. */ void ushell_task(void *pvParameters) #else /*! \brief Entry point of the explorer task management. * * This function performs uShell decoding to access file-system functions. */ void ushell_task(void) #endif { #ifdef FREERTOS_USED //** Inifinite loop for RTOS because it is a RTOS task portTickType xLastWakeTime; xLastWakeTime = xTaskGetTickCount(); while (true) { vTaskDelayUntil(&xLastWakeTime, configTSK_USHELL_PERIOD); #else //** No loop with the basic scheduler { #endif // FREERTOS_USED //** Check the USB mode and authorize/unauthorize ushell if(!g_b_ushell_task_run) { if( Is_usb_id_device() ) #ifdef FREERTOS_USED continue; // Continue in the RTOS task #else return; // Exit of the task scheduled #endif g_b_ushell_task_run = true; // Display shell startup fputs(MSG_WELCOME, stdout); ushell_cmd_nb_drive(); fputs(MSG_PROMPT, stdout); // Reset the embedded FS on ushell navigator and on first drive nav_reset(); nav_select( FS_NAV_ID_USHELL_CMD ); nav_drive_set( 0 ); }else{ if( Is_usb_id_device() ) { g_b_ushell_task_run = false; fputs(MSG_EXIT, stdout ); nav_exit(); #ifdef FREERTOS_USED continue; // Continue in the RTOS task #else return; // Exit of the task scheduled #endif } } //** Scan shell command if( !ushell_cmd_scan() ) #ifdef FREERTOS_USED continue; // Continue in the RTOS task #else return; // Exit of the task scheduled #endif //** Command ready then decode and execute this one switch( ushell_cmd_decode() ) { // Displays number of drives case CMD_NB_DRIVE: ushell_cmd_nb_drive(); break; // Displays free space information for all connected drives case CMD_DF: ushell_cmd_free_space(); break; // Formats disk case CMD_FORMAT: ushell_cmd_format(); break; // Mounts a drive (e.g. "b:") case CMD_MOUNT: ushell_cmd_mount(); break; // Displays the space information for current drive case CMD_SPACE: ushell_cmd_space(); break; // Lists the files present in current directory (e.g. "ls") case CMD_LS: ushell_cmd_ls(false); break; case CMD_LS_MORE: ushell_cmd_ls(true); break; // Enters in a directory (e.g. "cd folder_toto") case CMD_CD: ushell_cmd_cd(); break; // Enters in parent directory ("cd..") case CMD_UP: ushell_cmd_gotoparent(); break; // Displays a text file case CMD_CAT: ushell_cmd_cat(false); break; case CMD_CAT_MORE: ushell_cmd_cat(true); break; // Displays the help case CMD_HELP: ushell_cmd_help(); break; // Creates directory case CMD_MKDIR: ushell_cmd_mkdir(); break; // Creates file case CMD_TOUCH: ushell_cmd_touch(); break; // Deletes files or directories case CMD_RM: ushell_cmd_rm(); break; // Appends char to selected file case CMD_APPEND: ushell_cmd_append_file(); break; // Index routines (= specific shortcut from ATMEL FileSystem) case CMD_SET_ID: g_mark_index = nav_getindex(); break; case CMD_GOTO_ID: nav_gotoindex( &g_mark_index ); break; // Copies file to other location case CMD_CP: ushell_cmd_copy(); break; // Renames file case CMD_MV: ushell_cmd_rename(); break; // Synchronize folders case CMD_SYNC: ushell_cmd_sync(); break; case CMD_PERFORM: ushell_cmd_perform(); break; // USB commands #if USB_HOST_FEATURE == true case CMD_LS_USB: ushell_cmdusb_ls(); break; case CMD_USB_SUSPEND: ushell_cmdusb_suspend(); break; case CMD_USB_RESUME: ushell_cmdusb_resume(); break; #endif case CMD_NONE: break; // Unknown command default: fputs(MSG_ER_CMD_NOT_FOUND, stdout); break; } fputs(MSG_PROMPT, stdout); } } //! @brief Get the full command line to be interpreted. //! //! @return true, if a command is ready //! bool ushell_cmd_scan(void) { int c_key; // Something new of the UART ? if (usart_read_char(SHL_USART, &c_key) != USART_SUCCESS) { usart_reset_status(SHL_USART); return false; } if( 0 != g_u8_escape_sequence ) { //** Decode escape sequence if( 1 == g_u8_escape_sequence ) { if( 0x5B != c_key ) { g_u8_escape_sequence=0; return false; // Escape sequence cancel } g_u8_escape_sequence=2; } else { // Decode value of the sequence switch (c_key) { /* Note: OVERRUN error on USART with an RTOS and USART without interrupt management If you want support "Escape sequence", then you have to implement USART interrupt management case 0x41: // UP command ushell_clean_cmd_line(); ushell_history_up(); ushell_history_display(); break; case 0x42: // DOWN command ushell_clean_cmd_line(); ushell_history_down(); ushell_history_display(); break; */ default: // Ignore other command break; } g_u8_escape_sequence=0; // End of Escape sequence } return false; } //** Normal sequence switch (c_key) { //** Command validation case ASCII_CR: putchar(ASCII_CR); // Echo putchar(ASCII_LF); // Add new line flag g_s_cmd_his[g_u8_history_pos][g_u8_cmd_size]=0; // Add NULL terminator at the end of command line return true; //** Enter in escape sequence case ASCII_ESCAPE: g_u8_escape_sequence=1; break; //** backspace case ASCII_BKSPACE: if(g_u8_cmd_size>0) // Beginning of line ? { // Remove the last character on terminal putchar(ASCII_BKSPACE); // Send a backspace to go in previous character putchar(' '); // Send a space to erase previous character putchar(ASCII_BKSPACE); // Send a backspace to go in new end position (=previous character position) // Remove the last character on cmd line buffer g_u8_cmd_size--; } break; // History management case '!': ushell_clean_cmd_line(); ushell_history_up(); ushell_history_display(); break; case '$': ushell_clean_cmd_line(); ushell_history_down(); ushell_history_display(); break; //** Other char default: if( (0x1F<c_key) && (c_key<0x7F) && (USHELL_SIZE_CMD_LINE!=g_u8_cmd_size) ) { // Accept char putchar(c_key); // Echo g_s_cmd_his[g_u8_history_pos][g_u8_cmd_size++] = c_key; // append to cmd line } break; } return false; }
*/ #include <asf.h> #include "uart.h" #include "main.h" #include "ui.h" static usart_options_t usart_options; ISR(usart_interrupt, USART_IRQ_GROUP, 3) { if (USART->csr & AVR32_USART_CSR_RXRDY_MASK) { // Data received ui_com_tx_start(); int value; bool b_error = (USART_SUCCESS != usart_read_char(USART, &value)); if (b_error) { usart_reset_status(USART); udi_cdc_signal_framing_error(); ui_com_error(); } // Transfer UART RX fifo to CDC TX if (!udi_cdc_is_tx_ready()) { // Fifo full udi_cdc_signal_overrun(); ui_com_overflow(); }else{ udi_cdc_putc(value); } ui_com_tx_stop(); return;
/*~~~~~~~~~~~~ ISR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ static void usart_int_handler(void) { int rx_character; /* * In the code line below, the interrupt priority level does not need to * be explicitly masked as it is already because we are within the * interrupt handler. * The USART Rx interrupt flag is cleared by side effect when reading * the received character. * Waiting until the interrupt has actually been cleared is here useless * as the call to usart_write_char will take enough time for this before * the interrupt handler is left and the interrupt priority level is * unmasked by the CPU. */ usart_read_char(USART, &rx_character); if(global_is_command == 0) { switch(rx_character) { case 'a': { //Get Roll, Pitch, Yaw, Z //request data //usart_write_char(USART, rx_character); //usart_write_line(USART,"\r\nGet Motor Speed #1\r\n"); global_command = rx_character; global_is_command = 1; global_ready = 0; break; } case 'R': { //End transmission //end ISR //usart_write_line(USART,"\r\nEnding Transmission\r\n"); global_is_command = 0; global_ready = 0; break; } default: { //error //end ISR //usart_write_line(USART,"\r\nBad Command\r\n"); global_is_command = 0; global_ready = 0; break; } } } else //received character is data { if(global_data_counter < 4) //if data { //usart_write_char(USART, rx_character); global_data[global_data_counter] = rx_character; //save char as data global_data_counter++; //increment data counter } else { if(global_data_counter == 4) { //usart_write_char(USART, rx_character); global_data[global_data_counter] = rx_character; //save last received char global_is_command = 0; //next char should be a command global_ready = 1; //data is ready global_data_counter =0; //reset data counter //usart_write_line(USART,"\r\nData Saved\r\n"); } } } }