uint8_t sio2host_tx(uint8_t *data, uint8_t length) { #if SAMD || SAMR21 || SAML21 status_code_genare_t status; #else status_code_t status; #endif /*SAMD || SAMR21 || SAML21 */ do { #if SAMD || SAMR21 || SAML21 status = usart_serial_write_packet(&host_uart_module, (const uint8_t *)data, length); #elif SAM4S || SAM4E status = usart_serial_write_packet((Usart *)USART_HOST, (const uint8_t *)data, length); #else status = usart_serial_write_packet(USART_HOST, (const uint8_t *)data, length); #endif } while (status != STATUS_OK); return length; }
//*** Private method that actually sends data ***// int wifi_send_data(int ch, const uint8_t* data, int size){ int cmd_buf_size = MD_BUF_SIZE; char *cmd; int timeout = 7; //wait 7 seconds to transmit the data //allocate memory cmd = core_malloc(cmd_buf_size); snprintf(cmd,cmd_buf_size,"AT+CIPSEND=%d,%d\r\n",ch,size); data_tx_status=TX_PENDING; rx_wait=true; resp_buf_idx = 0; memset(resp_buf,0x0,RESP_BUF_SIZE); memset(wifi_rx_buf,0x0,WIFI_RX_BUF_SIZE); //to make debugging easier wifi_rx_buf_idx=0; usart_serial_write_packet(WIFI_UART,(uint8_t*)cmd,strlen(cmd)); delay_ms(250); //wait for module to be ready to accept data usart_serial_write_packet(WIFI_UART,(uint8_t*)data,size); //now wait for the data to be sent while(timeout>0){ //start the timer tc_start(TC0, 0); //when timer expires, return what we have in the buffer rx_wait=true; //reset the wait flag while(rx_wait && data_tx_status!=TX_SUCCESS); tc_stop(TC0,0); //the success flag is set *before* we receive the server's response //core_process_wifi_data receives the response but discards it if(data_tx_status==TX_SUCCESS){ data_tx_status=TX_IDLE; rx_wait = false; //free memory core_free(cmd); return TX_SUCCESS; } timeout--; } //check if this is a timeout error if(strlen((char*)resp_buf)==0){ printf("timeout error\n"); core_log("timeout error"); data_tx_status = TX_TIMEOUT; } //an error occured, see if it is due to a module reset else if(strcmp((char*)resp_buf,"\r\nready\r\n")==0){ //module reset itself!!! printf("detected module reset\n"); core_log("module reset"); data_tx_status = TX_ERR_MODULE_RESET; } else { data_tx_status=TX_ERROR; core_log("TX error:"); core_log((char*)resp_buf); data_tx_status = TX_ERROR; } //free memory core_free(cmd); return data_tx_status; }
/** * \brief * * \param pdata * \param len * * \return HOST_APP_MSG_ID_E */ HOST_APP_MSG_ID_E sl808x_cmd_rw( char *pdata, uint8_t len ) { usart_serial_write_packet( BOARD_USART0, (uint8_t *)pdata, len ); delay_ms(1000); return (rxMsgId); }
uint8_t sio2host_tx(uint8_t *data, uint8_t length) { #if SAMD20 status_code_genare_t status; #else status_code_t status; #endif /* SAMD20 */ do { #if SAMD20 status = usart_serial_write_packet(&cdc_uart_module,(const uint8_t *)data,length); #else status = usart_serial_write_packet(USART_HOST,(const uint8_t *)data,length); #endif } while (status != STATUS_OK); return length; }
uint8_t sio2ncp_tx(uint8_t *data, uint8_t length) { status_code_t status; do { status = usart_serial_write_packet(USART_NCP,(const uint8_t *)data,length); }while(status!= STATUS_OK); return length; }
static void mxt_handler(struct mxt_device *device) { /* USART tx buffer initialized to 0 */ char tx_buf[STRING_LENGTH * MAX_ENTRIES] = {0}; uint8_t i = 0; /* Iterator */ /* Temporary touch event data struct */ struct mxt_touch_event touch_event; /* Collect touch events and put the data in a string, * maximum 2 events at the time */ do { /* Temporary buffer for each new touch event line */ char buf[STRING_LENGTH]; /* Read next next touch event in the queue, discard if read fails */ if (mxt_read_touch_event(device, &touch_event) != STATUS_OK) { continue; } /* Format a new entry in the data string that will be sent over USART */ sprintf(buf, "Nr: %1d, X:%4d, Y:%4d, Status:0x%2x\n\r", touch_event.id, touch_event.x, touch_event.y, touch_event.status); /* Add the new string to the string buffer */ strcat(tx_buf, buf); i++; /* Check if there is still messages in the queue and * if we have reached the maximum numbers of events */ } while ((mxt_is_message_pending(device)) & (i < MAX_ENTRIES)); /* If there is any entries in the buffer, send them over USART */ if (i > 0) { usart_serial_write_packet(USART_SERIAL_EXAMPLE, (uint8_t *)tx_buf, strlen(tx_buf)); } }
BaseOutStream& USARTStream::write(const char_type* str, size_type count) { usart_serial_write_packet(usart, reinterpret_cast<const uint8_t*>(str), count); return *this; }
/** * \brief * * \param * * \return void */ void uart_config(void) { const sam_uart_opt_t uart_settings = { .ul_mck = sysclk_get_peripheral_hz(), .ul_baudrate = 19200, .ul_mode = UART_MR_PAR_NO }; sysclk_enable_peripheral_clock(ID_UART); uart_init( BOARD_UART, &uart_settings ); } /** * \brief * * \param * * \return void */ void usart0_config(void) { const sam_usart_opt_t usart_0_settings = { .baudrate = 19200, .char_length = US_MR_CHRL_8_BIT, .parity_type = US_MR_PAR_NO, .stop_bits = US_MR_NBSTOP_1_BIT, .channel_mode = US_MR_CHMODE_NORMAL, /* This field is only used in IrDA mode. */ .irda_filter = 0 }; sysclk_enable_peripheral_clock(BOART_ID_USART0); usart_init_rs232(BOARD_USART0, &usart_0_settings, sysclk_get_peripheral_hz()); /* Disable all the interrupts. */ usart_disable_interrupt(BOARD_USART0, 0xffffffff); /* Enable the receiver and transmitter. */ usart_enable_tx(BOARD_USART0); usart_enable_rx(BOARD_USART0); /* Configure and enable interrupt of USART. */ NVIC_EnableIRQ(USART0_IRQn); usart_enable_interrupt(BOARD_USART0, US_IER_RXRDY); } /** * \brief * * \param * * \return void */ void usart1_config(void) { const sam_usart_opt_t usart_1_settings = { .baudrate = 115200, .char_length = US_MR_CHRL_8_BIT, .parity_type = US_MR_PAR_NO, .stop_bits = US_MR_NBSTOP_1_BIT, .channel_mode = US_MR_CHMODE_NORMAL, /* This field is only used in IrDA mode. */ .irda_filter = 0 }; sysclk_enable_peripheral_clock( BOART_ID_USART1 ); usart_init_rs232( BOARD_USART1, &usart_1_settings, sysclk_get_peripheral_hz() ); /* Disable all the interrupts. */ usart_disable_interrupt(BOARD_USART1, 0xffffffff); /* Enable the receiver and transmitter. */ usart_enable_tx(BOARD_USART1); usart_enable_rx(BOARD_USART1); /* Configure and enable interrupt of USART. */ NVIC_EnableIRQ( USART1_IRQn ); usart_enable_interrupt( BOARD_USART1, US_IER_RXRDY); } /** * \brief * * \param uxPriority * * \return void */ void vStartUartTaskLauncher( unsigned portBASE_TYPE uxPriority ) { /* Spawn the Sentinel task. */ xTaskCreate( vUartTask, (const signed portCHAR *)"SPILAUNCH", TASK_RADIO_STACK_SIZE, NULL, uxPriority, (xTaskHandle *)NULL ); } /** * \brief UART handle task.. * * \param * \param * * \return None */ portTASK_FUNCTION_PROTO( vUartTask, pvParameters ) { uint8_t i, sms_text[] = "AT\r"; (void)pvParameters; /* Initialize UART model.*/ uart_config(); usart0_config(); usart1_config(); gpio_set_pin_low( PIO_PA17_IDX ); for (;;) { vTaskDelay(1000); uart_write( BOARD_UART, '1' ); usart_serial_write_packet( BOARD_USART1, sms_text, sizeof(sms_text) ); } } /** * \brief * * \param * * \return void */ void USART0_Handler(void) { uint32_t ul_status; /* Read USART Status. */ ul_status = usart_get_status( BOARD_USART0 ); /* Receive buffer is full. */ if (ul_status & US_CSR_RXRDY) { usart_read( BOARD_USART0, (uint32_t *)&gs_ul_read_buffer[0] ); } }
int wifi_send_cmd(const char* cmd, const char* resp_complete, char* resp, uint32_t maxlen, int timeout){ resp_buf_idx = 0; uint32_t rx_start, rx_end; //clear out the response buffer memset(resp,0x0,maxlen); memset(resp_buf,0x0,RESP_BUF_SIZE); //setup the rx_complete buffer so we know when the command is finished if(strlen(resp_complete)>RESP_COMPLETE_BUF_SIZE-3){ printf("resp_complete, too long exiting\n"); return -1; } strcpy(resp_complete_buf,resp_complete); strcat(resp_complete_buf,"\r\n"); //enable RX interrupts usart_enable_interrupt(WIFI_UART, US_IER_RXRDY); NVIC_SetPriority(WIFI_UART_IRQ,2); NVIC_EnableIRQ(WIFI_UART_IRQ); //write the command rx_wait=true; //we want this data returned in resp_buf rx_complete =false; //reset the early complete flag usart_serial_write_packet(WIFI_UART,(uint8_t*)cmd,strlen(cmd)); //terminate the command usart_serial_putchar(WIFI_UART,'\r'); usart_serial_putchar(WIFI_UART,'\n'); //wait for [timeout] seconds while(timeout>0){ //start the timer tc_start(TC0, 0); //when timer expires, return what we have in the buffer rx_wait=true; //reset the wait flag while(rx_wait); tc_stop(TC0,0); if(rx_complete) //if the uart interrupt signals rx is complete break; timeout--; } //now null terminate the response resp_buf[resp_buf_idx]=0x0; //remove any ECHO if(strstr((char*)resp_buf,cmd)!=(char*)resp_buf){ printf("bad echo: %s\n",resp_buf); return 0; } rx_start = strlen(cmd); //remove leading whitespace while(resp_buf[rx_start]=='\r'|| resp_buf[rx_start]=='\n') rx_start++; //remove trailing whitespace rx_end = strlen((char*)resp_buf)-1; while(resp_buf[rx_end]=='\r'|| resp_buf[rx_end]=='\n') rx_end--; //make sure we have a response if(rx_end<=rx_start){ printf("no response by timeout\n"); return 0; } //copy the data to the response buffer if((rx_end-rx_start+1)>maxlen){ memcpy(resp,&resp_buf[rx_start],maxlen-1); resp[maxlen-1]=0x0; printf((char*)resp_buf); printf("truncated output!\n"); } else{ memcpy(resp,&resp_buf[rx_start],rx_end-rx_start+1); //null terminate the response buffer resp[rx_end-rx_start+1]=0x0; } return rx_end-rx_start; }