/**@snippet [Handling the data received over BLE] */ void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) { for (int i = 0; i < length; i++) { simple_uart_put(p_data[i]); } simple_uart_put('\n'); }
//reads a String from the terminal (until the user has pressed ENTER) //offset can be set to a value above 0 if the readBuffer already contains text void Terminal::ReadlineUART(char* readBuffer, u8 readBufferLength, u8 offset) { #ifdef ENABLE_TERMINAL if (!terminalIsInitialized) return; u8 byteBuffer; u8 counter = offset; //Read in an infinite loop until \r is recognized while (true) { //Read from terminal byteBuffer = simple_uart_get(); //BACKSPACE if (byteBuffer == 127) { if (counter > 0) { //Output Backspace if(promptAndEchoMode) simple_uart_put(byteBuffer); readBuffer[counter - 1] = 0; counter--; } //ALL OTHER CHARACTERS } else { //Display entered character in terminal if(promptAndEchoMode) simple_uart_put(byteBuffer); if (byteBuffer == '\r' || counter >= readBufferLength || counter >= 250) { readBuffer[counter] = '\0'; if(promptAndEchoMode) simple_uart_putstring((const u8*) EOL); break; } else { memcpy(readBuffer + counter, &byteBuffer, sizeof(u8)); } counter++; } } #endif }
/** Transmits one char at a time as check if the loopback received data is same as transmitted * Just used for testing with loopback setup (i.e, @ref TX_PIN_NUMBER connected to @ref RX_PIN_NUMBER) * return true if test passed, else return false */ static void uart_loopback_test() { uint8_t tx_data[] = ("\n\r LOOPBACK_TEST"); uint8_t rx_data[MAX_TEST_DATA_BYTES] = {0}; // Start sending one byte and see if you get the same for(uint8_t i = 0; i < MAX_TEST_DATA_BYTES; i++) { bool status; simple_uart_put(tx_data[i]); if(!simple_uart_get_with_timeout(2, &rx_data[i])) { show_error(); } } for(uint8_t i = 0; i < MAX_TEST_DATA_BYTES; i++) { if ((rx_data[i] != tx_data[i])) { show_error(); } } return; // Test passed }
/** * @brief Function for application main entry. * @return 0. int return type required by ANSI/ISO standard. */ int main(void) { init(); simple_uart_putstring((const uint8_t *)"\n\rPress '0' or '1': "); while(true) { uint8_t c = simple_uart_get(); if (c != '0' && c != '1') continue; simple_uart_put(c); // Place the read character in the payload, enable the radio and // send the packet: packet[0] = c; NRF_RADIO->EVENTS_READY = 0U; NRF_RADIO->TASKS_TXEN = 1; while (NRF_RADIO->EVENTS_READY == 0U) { } NRF_RADIO->TASKS_START = 1U; NRF_RADIO->EVENTS_END = 0U; while(NRF_RADIO->EVENTS_END == 0U) { } NRF_RADIO->EVENTS_DISABLED = 0U; // Disable radio NRF_RADIO->TASKS_DISABLE = 1U; while(NRF_RADIO->EVENTS_DISABLED == 0U) { } } }
/** * main() function * @return 0. int return type required by ANSI/ISO standard. */ int main(void) { simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC); #ifndef ENABLE_LOOPBACK_TEST uart_start(); while(true) { uint8_t cr = simple_uart_get(); simple_uart_put(cr); if(cr == 'q' || cr == 'Q') { uart_quit(); while(1){} } } #else /* This part of the example is just for testing, can be removed if you do not have a loopback setup */ // ERROR_PIN configure as output nrf_gpio_cfg_output(ERROR_PIN); while(true) { uart_loopback_test(); } #endif }
void simple_uart_putstring(const uint8_t *str) { uint_fast8_t i = 0; uint8_t ch = str[i++]; while (ch != '\0') { simple_uart_put(ch); ch = str[i++]; } }
/** @brief Function for sending a series of characters (string) to UART. * Execution is blocked until UART peripheral reports a character has been sent. * @param[in] str Null terminated array of characters to send. */ static void uart_puthidstring(const char *str) { uint_fast8_t i = 0; char ch = str[i++]; while (ch != '\0') { simple_uart_put(usb_hid2_ascii_look_up[(uint8_t)ch]); ch = str[i++]; } }
//Initialize the mhTerminal void Terminal::Init() { #ifdef ENABLE_TERMINAL registeredCallbacks = new SimplePushStack(MAX_TERMINAL_COMMAND_LISTENER_CALLBACKS); //Start UART communication simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC); char versionString[15]; Utility::GetVersionStringFromInt(Config->firmwareVersion, versionString); if (promptAndEchoMode) { //Send Escape sequence simple_uart_put(27); //ESC simple_uart_putstring((const u8*) "[2J"); //Clear Screen simple_uart_put(27); //ESC simple_uart_putstring((const u8*) "[H"); //Cursor to Home //Send App start header simple_uart_putstring((const u8*) "--------------------------------------------------" EOL); simple_uart_putstring((const u8*) "Terminal started, compile date: "); simple_uart_putstring((const u8*) __DATE__); simple_uart_putstring((const u8*) " "); simple_uart_putstring((const u8*) __TIME__); simple_uart_putstring((const u8*) ", version: "); simple_uart_putstring((const u8*) versionString); #ifdef NRF52 simple_uart_putstring((const u8*) ", nRF52"); #else simple_uart_putstring((const u8*) ", nRF51"); #endif simple_uart_putstring((const u8*) EOL "--------------------------------------------------" EOL); } terminalIsInitialized = true; #endif }
void simple_uart_putstringbuff(uint8_t * str,uint16_t len) { uint16_t i = 0; // uint8_t ch = str[i++]; simple_uart_tx_send(); while (i<len) { simple_uart_put(str[i]); //ch = str[i++]; i++; } simple_uart_tx_stop(); }
void simple_uart_putstring(const uint8_t * str) { uint_fast8_t i = 0; uint8_t ch = str[i++]; simple_uart_tx_send(); while (ch != '\0') { simple_uart_put(ch); ch = str[i++]; } simple_uart_tx_stop(); }
/** * @brief Function for application main entry. */ int main(void) { gpio_init(); interrupt_init(); LTC2492_Init(); nrf_gpio_pin_set(LED_1); simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC); int size = 128; double storage[128]; memset(storage, 0, sizeof(*storage) * size); //double * storage = malloc(sizeof(*storage) * size ); int i = 0; while (true) { nrf_delay_ms(150); nrf_gpio_pin_toggle(LED_1); uint32_t output = SPI0_LTC2492_Read(); uint8_t chuncks_8bit[4]; chuncks_8bit[0] = (output & 0xFF000000) >> 24; ; chuncks_8bit[1] = (output & 0x00FF0000) >> 16; chuncks_8bit[2] = (output & 0x0000FF00) >> 8; chuncks_8bit[3] = (output & 0x000000FF) >> 0; int count; for (count = 0; count < 4; count++) { simple_uart_put(chuncks_8bit[count]); } storage[i] = output; i++; if (i == 128) { i = 0; } } }
void uart_data_check(void) { if(UartRecvBuf.rp!=UartRecvBuf.wp) { simple_uart_tx_send(); while(UartRecvBuf.rp!=UartRecvBuf.wp) { simple_uart_put(UartRecvBuf.dat[UartRecvBuf.rp]); UartRecvBuf.rp++; if (UartRecvBuf.rp >= SIMPLE_UART_RX_BUF_SIZE) { UartRecvBuf.rp = 0; } } simple_uart_tx_stop(); } }
void Terminal::PollUART() { #ifdef ENABLE_TERMINAL if (!terminalIsInitialized) return; static char readBuffer[250] = { 0 }; static char testCopy[250] = {0}; readBuffer[0] = 0; if (simple_uart_get_with_timeout(0, (u8*) readBuffer)) { //Output query string and typed symbol to terminal if (promptAndEchoMode) { simple_uart_putstring((const u8*) EOL "mhTerm: "); //Display prompt simple_uart_put(readBuffer[0]); //echo back symbol } //Read line from uart ReadlineUART(readBuffer, 250, 1); //FIXME: remove after finding problem memcpy(testCopy, readBuffer, 250); //Clear previous command commandName.clear(); commandArgs.clear(); //Tokenize input string into vector char* token = strtok(readBuffer, " "); if (token != NULL) commandName.assign(token); while (token != NULL) { token = strtok(NULL, " "); if (token != NULL) commandArgs.push_back(string(token)); } //Check for clear screen if (commandName == "cls") { //Send Escape sequence simple_uart_put(27); //ESC simple_uart_putstring((const u8*) "[2J"); //Clear Screen simple_uart_put(27); //ESC simple_uart_putstring((const u8*) "[H"); //Cursor to Home } else { //Call all callbacks int handled = 0; for(u32 i=0; i<registeredCallbacks->size(); i++){ handled += ((TerminalCommandListener*)registeredCallbacks->GetItemAt(i))->TerminalCommandHandler(commandName, commandArgs); } if (handled == 0){ if(promptAndEchoMode){ simple_uart_putstring((const u8*)"Command not found" EOL); } else { uart_error(Logger::COMMAND_NOT_FOUND); } //FIXME: to find problems with uart input uart("ERROR", "{\"user_input\":\"%s\"}" SEP, testCopy); } } } #endif }
//from console.c void uart_put_dec32bit(uint32_t ww) // ww is in the range [0 4294967295] { uint8_t ww0; uint8_t ww1; uint8_t ww2; uint8_t ww3; uint8_t ww4; uint8_t ww5; uint8_t ww6; uint8_t ww7; uint8_t ww8; if ( 1 ) { ww0 = (ww % 10); // Remainder of ww when divided by 10 ww /= 10; // forces ww into the range [0 429496729] ww1 = (ww % 10); // Remainder of ww when divided by 10 ww /= 10; // forces ww into the range [0 42949672] ww2 = (ww % 10); // Remainder of ww when divided by 10 ww /= 10; // forces ww into the range [0 4294967] ww3 = (ww % 10); // Remainder of ww when divided by 10 ww /= 10; // forces ww into the range [0 429496] ww4 = (ww % 10); // Remainder of ww when divided by 10 ww /= 10; // forces ww into the range [0 42949] ww5 = (ww % 10); // Remainder of ww when divided by 10 ww /= 10; // forces ww into the range [0 4294] ww6 = (ww % 10); // Remainder of ww when divided by 10 ww /= 10; // forces ww into the range [0 429] ww7 = (ww % 10); // Remainder of ww when divided by 10 ww /= 10; // forces ww into the range [0 42] ww8 = (ww % 10); // Remainder of ww when divided by 10 ww /= 10; // forces ww into the range [0 4] if (ww != 0) { simple_uart_put((uint8_t)ww + '0'); /* We may safely cast ww to the smaller type, as we have */ /* made sure (above) that its value will fit. */ simple_uart_put(ww8 + '0'); simple_uart_put(ww7 + '0'); simple_uart_put(ww6 + '0'); simple_uart_put(ww5 + '0'); simple_uart_put(ww4 + '0'); simple_uart_put(ww3 + '0'); simple_uart_put(ww2 + '0'); simple_uart_put(ww1 + '0'); simple_uart_put(ww0 + '0'); } else if (ww8 != 0) { simple_uart_put(ww8 + '0'); simple_uart_put(ww7 + '0'); simple_uart_put(ww6 + '0'); simple_uart_put(ww5 + '0'); simple_uart_put(ww4 + '0'); simple_uart_put(ww3 + '0'); simple_uart_put(ww2 + '0'); simple_uart_put(ww1 + '0'); simple_uart_put(ww0 + '0'); } else if (ww7 != 0) { simple_uart_put(ww7 + '0'); simple_uart_put(ww6 + '0'); simple_uart_put(ww5 + '0'); simple_uart_put(ww4 + '0'); simple_uart_put(ww3 + '0'); simple_uart_put(ww2 + '0'); simple_uart_put(ww1 + '0'); simple_uart_put(ww0 + '0'); } else if (ww6 != 0) { simple_uart_put(ww6 + '0'); simple_uart_put(ww5 + '0'); simple_uart_put(ww4 + '0'); simple_uart_put(ww3 + '0'); simple_uart_put(ww2 + '0'); simple_uart_put(ww1 + '0'); simple_uart_put(ww0 + '0'); } else if (ww5 != 0) { simple_uart_put(ww5 + '0'); simple_uart_put(ww4 + '0'); simple_uart_put(ww3 + '0'); simple_uart_put(ww2 + '0'); simple_uart_put(ww1 + '0'); simple_uart_put(ww0 + '0'); } else if (ww4 != 0) { simple_uart_put(ww4 + '0'); simple_uart_put(ww3 + '0'); simple_uart_put(ww2 + '0'); simple_uart_put(ww1 + '0'); simple_uart_put(ww0 + '0'); } else if (ww3 != 0) { simple_uart_put(ww3 + '0'); simple_uart_put(ww2 + '0'); simple_uart_put(ww1 + '0'); simple_uart_put(ww0 + '0'); } else if (ww2 != 0) { simple_uart_put(ww2 + '0'); simple_uart_put(ww1 + '0'); simple_uart_put(ww0 + '0'); } else if (ww1 != 0) { simple_uart_put(ww1 + '0'); simple_uart_put(ww0 + '0'); } else { simple_uart_put(ww0 + '0'); } } }
int fputc(int ch, FILE * p_file) { simple_uart_put((uint8_t)ch); return ch; }
//重定义fputc函数 int fputc(int ch, FILE *f) { simple_uart_put(ch); return ch; }