uint8_t GPS_MTK3339::continueRead(){ #ifdef GPS_DEBUG_READ_VERBOSE GPS_DEBUG_READ_PRINTLN("READ GPS CONTINUE"); #endif while(serial_available() > 0){ char b = (char)serial_read(); if(b == GPS_START_DELIM){ GPS_DEBUG_READ_PRINTLN("unexpected start delimiter found"); _machineState = GPS_MS_ERROR; return GPS_ERROR_PARSE_ERR; } if(b == GPS_CHKSUM_DELIM){ writeBuffer(b); uint32_t ticks = millis(); while(serial_available() < 2){ if(millis() - ticks > 250){ GPS_DEBUG_READ_PRINTLN("too long to read checksum bytes"); _machineState = GPS_MS_ERROR; return GPS_ERROR_TIMEOUT; } } b = serial_read(); writeBuffer(b); b = serial_read(); writeBuffer(b); writeBuffer(0x00); return parseSentence(); } writeBuffer(b); } return GPS_STATUS_READING; }
int main (void) { init(); #if SERIAL_PROTOCOL == PROTOCOL_KISS while (true) { ax25_poll(&AX25); if (serial_available(0)) { char sbyte = uart0_getchar_nowait(); kiss_serialCallback(sbyte); } } #endif #if SERIAL_PROTOCOL == PROTOCOL_SIMPLE_SERIAL ticks_t start = timer_clock(); while (1) { ax25_poll(&AX25); if (!sertx && serial_available(0)) { sbyte = uart0_getchar_nowait(); #if SERIAL_DEBUG if ((serialLen < AX25_MAX_FRAME_LEN) && (sbyte != 10)) { serialBuffer[serialLen] = sbyte; serialLen++; } else { sertx = true; } #else if (serialLen < AX25_MAX_FRAME_LEN-1) { serialBuffer[serialLen] = sbyte; serialLen++; } else { serialBuffer[serialLen] = sbyte; serialLen++; sertx = true; } start = timer_clock(); #endif } else { if (!SERIAL_DEBUG && serialLen > 0 && timer_clock() - start > ms_to_ticks(TX_MAXWAIT)) { sertx = true; } } if (sertx) { ss_serialCallback(serialBuffer, serialLen, &AX25); sertx = false; serialLen = 0; } } #endif return(0); }
static void print_response() { char c; printf_P(PSTR("*** START RESPONSE ***\n")); while(!serial_available()); _delay_ms(50); while(serial_available()) { c = fgetc(serial_input); if(c != '\r') putchar(c); } if(c != '\n') putchar('\n'); printf_P(PSTR("*** END RESPONSE ***\n")); }
void task_echo() { printf("ECHO TASK (ID %d) started.\n", CURRENT_TASK_ID); yield(); while(true) { while (!serial_available()) yield(); while (serial_available()) putc(getc()); } }
void readSerialCmd() { int result; char data; static uint8_t pos = 0; while (serial_available() && (serialMode == SERIALCMDMODE)) { //ATSL changes commandmode while there is a char waiting in the serial buffer. result = NOTHING; data = serial_read(); serialData[pos++] = data; //serialData is our global serial buffer if (data == SERIALCMDTERMINATOR) { if (pos > 3) { // we need 4 bytes result = processSerialCmd(pos); } else { result = ERR; } pos = 0; } // check if we don't overrun the buffer, if so empty it if (pos > BUFFLEN) { result = ERR; pos = 0; } if (result == OK) { printf("ok\r\n"); } else if (result == ERR) { printf("error\r\n"); } } }
static void loop() { if (serial_available() > 0) { #ifdef DEBUG printf("Sdat\r\n"); #endif sleepCounter = 1000; // reset the sleep counter if (serialMode == SERIALCMDMODE) { readSerialCmd(); } else { readSerialData(); } } if (GDO_PIN & GDO0) { #ifdef DEBUG printf("Rdat\r\n"); #endif writeSerialData(); sleepCounter++; // delay sleep } sleepCounter--; // check if we can go to sleep again, going into low power too early will result in lost data in the CCx fifo. if ((sleepCounter == 0) && (config_get(CONFIG_RFBEE_MODE) == LOWPOWER_MODE)) { #ifdef DEBUG printf("low power on\r\n"); #endif lowPowerOn(); #ifdef DEBUG printf("woke up\r\n"); #endif } }
void wifi_connect() { char response[256]; char response_len; printf_P(PSTR("[WIFI] Connecting...\n")); wifi_enable(); _delay_ms(50); // Reset wireless fputs_P(AT_RST, serial_output); _delay_ms(50); while(serial_available()) fgetc(serial_input); wifi_repeat_until_ok("AT\r\n"); fputs_P(AT_CWMODE_1, serial_output); _delay_ms(50); print_response(); fprintf_P(serial_output, AT_CWJAP, WIFI_SSID, WIFI_PASS); print_response(); // TODO: Retry connect after x failures while(!wifi_is_connected()); fputs_P(AT_CIPMUX_0, serial_output); print_response(); printf_P(PSTR("[WIFI] Connected.\n")); }
static void handle_serial_input(void) { while (1) { switch (serial_getchar()) { case '\0': cli(); if (!serial_available()) events &= ~EV_SERIAL; sei(); return; case 'O': /* open */ pin_low(PIN_GREEN_LED); pin_low(PIN_OPEN_LOCK); _delay_ms(500); pin_high(PIN_OPEN_LOCK); serial_print("OPENAKCK\n"); pin_high(PIN_GREEN_LED); break; case 'D': /* day */ pin_low(PIN_GREEN_LED); pin_low(PIN_DAYMODE); /* day mode */ pin_high(PIN_STATUS_LED); /* status on */ break; case 'N': /* night */ pin_high(PIN_GREEN_LED); pin_high(PIN_DAYMODE); /* nightmode */ pin_low(PIN_STATUS_LED); /* status off */ break; case 'R': /* rejected */ pin_low(PIN_YELLOW_LED); _delay_ms(200); pin_high(PIN_YELLOW_LED); _delay_ms(200); pin_low(PIN_YELLOW_LED); _delay_ms(200); pin_high(PIN_YELLOW_LED); break; case 'V': /* validated */ pin_low(PIN_GREEN_LED); _delay_ms(300); pin_high(PIN_GREEN_LED); _delay_ms(200); pin_low(PIN_GREEN_LED); _delay_ms(300); pin_high(PIN_GREEN_LED); _delay_ms(200); pin_low(PIN_GREEN_LED); _delay_ms(300); pin_high(PIN_GREEN_LED); break; } } }
u8 serial_getkey() { u8 c; while (!(serial_available())); c = serial_read(); serial_flush(); return (c); }
void wifi_test_tcp() { printf("[WIFI] Test...\n"); softserial_printf("AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", "173.254.30.60", 80); _delay_ms(1000); print_response(); const char *http = "GET /Test.txt HTTP/1.0\r\n\r\nHost: thearduinoguy.org\r\n\r\n"; softserial_printf("AT+CIPSEND=%d\r\n", strlen(http)); print_response(); while(serial_available()) fgetc(serial_input); softserial_printf(http); _delay_ms(1000); char line_buf[128]; int content_length = -1; while(serial_available()) { softserial_readline(line_buf, ARRAYSIZE(line_buf)); if(memcmp(line_buf, "Content-Length: ", 16) == 0) { content_length = atoi(&line_buf[16]); } else if(content_length >= 0 && line_buf[0] == '\r') { break; } } // TODO: Protect against overflow char content[128]; for(int i = 0; i < content_length; ++i) { content[i] = softserial_getc(); } content[content_length] = '\0'; printf("CONTENT: %s\n", content); while(serial_available()) fgetc(serial_input); // Will error if remote closed connection already, that's fine softserial_printf("AT+CIPCLOSE\r\n"); _delay_ms(100); print_response(); }
int kbhit() { // return usb_serial_available() != 0; if(USB_SERIAL_ON && (usb_serial_available() != 0)) return 1; #ifdef USE_UART return serial_available() != 0; #endif return 0; }
bool wifi_is_connected() { char line[32]; bool connected = false; while(serial_available()) fgetc(serial_input); fputs_P(AT_CWJAPq, serial_output); _delay_ms(50); while(!connected && serial_available()) { fgets(line, ARRAYSIZE(line), serial_input); connected = MEMCMP_CONST(line, "OK\r\n") == 0; } while(serial_available()) fgetc(serial_input); return connected; }
void loop() { if (serial_available()) { digitalwrite(0,HIGH); caractere=serial_read(); serial_write("test"); if (caractere=='A') digitalwrite(1,digitalread(1)^1); } }
uint8_t GPS_MTK3339::startRead(){ #ifdef GPS_DEBUG_READ_VERBOSE if(serial_available() > 0){ GPS_DEBUG_READ_PRINTLN("GPS DATA IN BUFFER"); } GPS_DEBUG_READ_PRINTLN("READ GPS"); #endif while(serial_available() > 0){ uint8_t b = serial_read(); if(b == GPS_START_DELIM){ memset(Buffer, 0x00, sizeof(Buffer)); Buffer[0] = b; _bufferOffset = 1; _machineState = GPS_MS_READING; return continueRead(); } } return GPS_STATUS_NODATA; }
void loop() { serial_write("TEST\n\r"); if (serial_available()) { serial_write("caractere"); caractere=serial_read(); serial_write(chaine); serial_write(&caractere); } }
bool wifi_is_send_ok() { char line[32]; bool send_ok = false; while(!send_ok && serial_available()) { fgets(line, ARRAYSIZE(line), serial_input); send_ok = (MEMCMP_CONST(line, "SEND OK\r\n") == 0); } return send_ok; }
static bool serial_getc_timeout(char *c, const int timeout_ms) { for(int i = 0; i < timeout_ms / 10; ++i) { if(!serial_available()) { _delay_ms(10); } else { *c = fgetc(serial_input); return true; } } return false; }
int main() { TIL311 = 0x01; default_interrupts(); serial_start(SERIAL_SAFE); millis_start(); lcd_init(); lcd_puts("Hello from C,\non the 68008!"); beep(700,250); DELAY_MS(50); beep(500,300); DELAY_MS(100); beep(700,500); DELAY_MS(500); play_rtttl(songs[0]); while(true) { TIL311 = 0xCC; DELAY_MS(1000); TIL311 = 0xC0; DELAY_MS(1000); TIL311 = 0xDE; DELAY_MS(1000); TIL311 = 0x00; DELAY_MS(1000); if(serial_available()) { int16_t ch; lcd_clear(); uint8_t c = 0; while ((ch = getc()) != -1) { putc(ch); lcd_data(ch); if (c++ == 15) lcd_cursor(0,1); } } else { lcd_clear(); lcd_puts("Time since boot:"); lcd_cursor(0,1); lcd_printf("%3d.%02d seconds",millis()/1000, (millis()%1000)/10); } } }
void fill_i2c_buffer_from_serial(char len, char addr, char rw) { //Set R/W bit of address addr = addr << 1 | rw; //Put address into i2c buffer serial_i2c_buffer[0] = addr; for(char i = 1; i <= len; i++) { while(serial_available() < 1); serial_i2c_buffer[i] = serial_read(); } }
int main (void) { init(); while (true) { ax25_poll(&AX25); digipeater_processPackets(); if (serial_available(0)) { //char sbyte = uart0_getchar_nowait(); // Do something with the data :) } } return(0); }
static void wifi_repeat_until_ok(const char *cmd) { char line[32]; for(int i = 0;;++i) { printf_P(PSTR("%s - Attempt %2d\n"), cmd, i); fprintf(serial_output, "%s", cmd); _delay_ms(200); while(serial_available()) { fgets(line, ARRAYSIZE(line), serial_input); if(MEMCMP_CONST(line, "OK\r\n") == 0) return; } _delay_ms(300); } }
void wifi_sendn(const void *message, int message_len) { printf_P(PSTR("[WIFI] Sending...\n")); char cmd[64]; sprintf_P(cmd, AT_CIPSTART_UDP, WIFI_DEST_IP, WIFI_DEST_PORT); wifi_repeat_until_ok(cmd); fprintf_P(serial_output, AT_CIPSEND, message_len); print_response(); while(serial_available()) fgetc(serial_input); const char *message_chars = message; for(int i = 0; i < message_len; ++i) { fputc(message_chars[i], serial_output); } }
inline void process_serial_message() { if(serial_available() > 2) { status_led_on(); char buffer[3]; serial_read_buffer(buffer, 3); switch(buffer[0]) { //Set I2C Address case 0x22: usi_i2c_slave_address = buffer[1]; eeprom_write_byte((uint8_t*)1, usi_i2c_slave_address); break; //Read I2C Address case 0x23: serial_transmit_byte(usi_i2c_slave_address); break; //Send I2C Write case 0x24: fill_i2c_buffer_from_serial(buffer[1], buffer[2], 0); serial_transmit_byte(USI_I2C_Master_Start_Transmission(serial_i2c_buffer, buffer[1]+1)); break; //Send I2C Read case 0x25: { char addr = buffer[2] << 1 | 1; serial_i2c_buffer[0] = addr; USI_I2C_Master_Start_Transmission(serial_i2c_buffer, buffer[1]+1); for(char i = 1; i <= buffer[1]; i++) { serial_transmit_byte(serial_i2c_buffer[i]); } } break; } status_led_off(); } }
// check serial in and handle the character void read_line(void) { if (serial_available()) { uint8_t c; c = serial_read(); if (line_pos >= cols - 1) { exec_line(); reset_line(); char_to_line(c); } else if (c == '\n') { // ignore '\n' } else if (c == '\r') { exec_line(); reset_line(); } else { char_to_line(c); } } }
void update(void) { // receive new packets mavlink_message_t msg; mavlink_status_t status; status.packet_rx_drop_count = 0; // process received bytes while(serial_available()) { uint8_t c = serial_read_ch(); // Try to get a new message if (mavlink_parse_char(chan, c, &msg, &status)) { mavlink_active = true; handleMessage(&msg); } } }
uint32_t wifi_request_time(Wifi_Error *error) { const int char_timeout_ms = 500; wifi_send("TIME"); _delay_ms(1000); // "Received data" marker const char* marker = "\n+IPD,"; int marker_pos = 1; // Wait for response while(marker_pos < strlen(marker)) { char c; if(!serial_getc_timeout(&c, char_timeout_ms)) { if(error != NULL) *error = Wifi_Error_Timeout; return 0; } if(c == marker[marker_pos]) { ++marker_pos; } else { marker_pos = 0; } } char response_len_str[16]; for(int i = 0; true; ++i) { char c; if(!serial_getc_timeout(&c, char_timeout_ms)) { if(error != NULL) *error = Wifi_Error_Timeout; return 0; } if(c >= '0' && c <= '9') { response_len_str[i] = c; } else if(c == ':') { response_len_str[i] = '\0'; break; } else { if(error != NULL) *error = Wifi_Error_Unknown; return 0; } } int response_len = atoi(response_len_str); char response[64]; if(response_len > ARRAYSIZE(response) - 1) { if(error != NULL) *error = Wifi_Error_Buffer_Size; return 0; } for(int i = 0; i < response_len; ++i) { char c; if(!serial_getc_timeout(&c, char_timeout_ms)) { if(error != NULL) *error = Wifi_Error_Timeout; return 0; } response[i] = c; } response[response_len] = '\0'; uint32_t time_val = strtoul(response, NULL, 10); while(serial_available()) fgetc(serial_input); // Will error if remote closed connection already, that's fine softserial_printf("AT+CIPCLOSE\r\n"); _delay_ms(100); print_response(); if(error != NULL) *error = Wifi_Error_None; return time_val; }
int main() { TIL311 = 0x01; __vectors.address_error = &address_err; __vectors.bus_error = &bus_error; __vectors.illegal_instr = &illegal_inst; __vectors.divide_by_0 = &divby0; __vectors.uninitialized_isr = &bad_isr; __vectors.int_spurious = &spurious; __vectors.auto_level2 = &auto_lvl2; __vectors.priv_violation = &priv_vio; serial_start(); millis_start(); init_printf(null, &printf_putc); lcd_init(); lcd_puts("Hello from C,\non the 68008!"); mario(); init_printf(null, &printf_lcd); /* while(true) { lcd_cursor(0,0); printf("Runtime: %d.%02d ",millis/1000, (millis%1000)/10); }*/ while(true) { TIL311 = 0xCC; DELAY_MS(1000); TIL311 = 0xC0; DELAY_MS(1000); TIL311 = 0xDE; DELAY_MS(1000); TIL311 = 0x00; DELAY_MS(1000); if(serial_available()) { int16_t ch; lcd_clear(); uint8_t c = 0; while ((ch = getc()) != -1) { putc(ch); lcd_data(ch); if (c++ == 15) lcd_cursor(0,1); } } else { lcd_clear(); lcd_puts("Time since boot:"); lcd_cursor(0,1); printf("%3d.%02d seconds",millis()/1000, (millis()%1000)/10); } } }
// serial_int is called by interruption service routine void serial_interrupt(void) { char caractere; unsigned char newwp; #if defined(__18f1220) || defined(__18f1320) || \ defined(__18f14k22) || defined(__18lf14k22) || \ defined(__18f2550) || defined(__18f4550) || \ defined(__18f25k50) || defined(__18f45k50) || \ defined(__18f2455) || defined(__18f4455) if (PIR1bits.RCIF) { PIR1bits.RCIF=0; // clear RX interrupt flag caractere=RCREG; // take received char #elif defined(__18f26j50) || defined(__18f46j50) || \ defined(__18f27j53) || defined(__18f47j53) if (PIR1bits.RC1IF) { PIR1bits.RC1IF=0; // clear RX interrupt flag caractere=RCREG1; // take received char #else #error "Processor Not Yet Supported. Please, Take Contact with Developpers." #endif if (wpointer!=RXBUFFERLENGTH-1) // if not last place in buffer newwp=wpointer+1; // place=place+1 else newwp=1; // else place=1 if (rpointer!=newwp) // if read pointer!=write pointer rx[wpointer++]=caractere; // store received char if (wpointer==RXBUFFERLENGTH) // if write pointer=length buffer wpointer=1; // write pointer = 1 } } // write char void serial_putchar(unsigned char caractere) { #if defined(__18f1220) || defined(__18f1320) || \ defined(__18f14k22) || defined(__18lf14k22) || \ defined(__18f2455) || \ defined(__18f2550) || defined(__18f4550) || \ defined(__18f25k50) || defined(__18f45k50) while (!TXSTAbits.TRMT); TXREG=caractere; // yes, send char #elif defined(__18f26j50) || defined(__18f46j50) || \ defined(__18f27j53) || defined(__18f47j53) while (!TXSTA1bits.TRMT); TXREG1=caractere; // yes, send char #else #error "Processor Not Yet Supported. Please, Take Contact with Developpers." #endif } // get char unsigned char serial_read() { unsigned char caractere=0; if (serial_available()) { PIE1bits.RCIE=0; // Atomic operation start caractere=rx[rpointer++]; if (rpointer==RXBUFFERLENGTH) rpointer=1; PIE1bits.RCIE=1; // Atomic operation end } return(caractere); }
void wifi_request_ntp(uint32_t *time_val, Wifi_Error *error) { const int char_timeout_ms = 500; NTP_Packet packet = {0}; packet.li_version_mode = (3 << 0) | (4 << 2) | (3 << 5); // TODO: More here... printf_P(PSTR("[WIFI] Sending NTP request...\n")); char cmd[64]; sprintf_P(cmd, AT_CIPSTART_UDP, NTP_IP, NTP_PORT); wifi_repeat_until_ok(cmd); fprintf_P(serial_output, AT_CIPSEND, sizeof(NTP_Packet)); print_response(); while(serial_available()) fgetc(serial_input); const char *message_chars = (char*)&packet; for(int i = 0; i < sizeof(NTP_Packet); ++i) { fputc(message_chars[i], serial_output); } _delay_ms(1000); // "Received data" marker const char* marker = "\n+IPD,"; int marker_pos = 1; // Wait for response while(marker_pos < strlen(marker)) { char c; if(!serial_getc_timeout(&c, char_timeout_ms)) { if(error != NULL) *error = Wifi_Error_Timeout; return; } if(c == marker[marker_pos]) { ++marker_pos; } else { marker_pos = 0; } } char response_len_str[16]; for(int i = 0; true; ++i) { char c; if(!serial_getc_timeout(&c, char_timeout_ms)) { if(error != NULL) *error = Wifi_Error_Timeout; return; } if(c >= '0' && c <= '9') { response_len_str[i] = c; } else if(c == ':') { response_len_str[i] = '\0'; break; } else { if(error != NULL) *error = Wifi_Error_Unknown; return; } } int response_len = atoi(response_len_str); if(response_len != sizeof(NTP_Packet)) { if(error != NULL) *error = Wifi_Error_Unknown; return; } char* packet_bytes = (char*)&packet; for(int i = 0; i < response_len; ++i) { char c; if(!serial_getc_timeout(&c, char_timeout_ms)) { if(error != NULL) *error = Wifi_Error_Timeout; return; } packet_bytes[i] = c; } while(serial_available()) fgetc(serial_input); // Will error if remote closed connection already, that's fine fprintf_P(serial_output, AT_CIPCLOSE); _delay_ms(100); print_response(); // TODO: Use other data in some way *time_val = swap_endian(packet.transmit_time); //*time_val -= 3155673600ul; // Y2K time *time_val -= 0x83AA7E80; // Unix time if(error != NULL) *error = Wifi_Error_None; }
int main(void) { uint8_t count = 0; uint8_t buf[64]; #if defined(BUTTERFLY) butterfly_init(); #endif // NOTE: If BUTTERFLY is defined then the serial_being() is bypassed // and the usart is set to 19200 regardless of what this says serial_begin(57600); /* buf[0] = 'H'; buf[1] = 'o'; buf[2] = 'w'; buf[3] = 'd'; buf[4] = 'y'; buf[5] = 0; serial_write(buf,6); // Insert a delay to keep this from stepping on the next statement delay(10); */ serial_out("Hello70"); // Send a bunch of stuff to see if it poops out serial_out("serial_println The quick brown fox jumped over the lazy dog."); serial_out("serial_out The quick brown fox jumped over the lazy dog.\n",0); serial_out("1serial_out The quick brown fox jumped over the lazy dog.\n",0); serial_out("2serial_out The quick brown fox jumped over the lazy dog.\n",0); serial_out("3serial_out The quick brown fox jumped over the lazy dog.\n",0); serial_out("4serial_out The quick brown fox jumped over the lazy dog.\n",0); serial_out("5serial_out The quick brown fox jumped over the lazy dog.\n",0); char c = 'A'; uint8_t x = 0x22; uint16_t X = 0x1122; serial_out("Test character: %c\n", c); serial_out("Test hexadecimal integer: %x\n", x); serial_out("Test 16-bit hexadecimal integer: %x\n", X); while(1) { delay(1000); usart0_transmit_buffer_insert('#'); if(serial_available()) { count = usart0_receive_buffer_inuse_count(); for(int i = 0 ; i < count; i++) { buf[i] = serial_in(); //usart0_transmit_buffer_insert(serial_read());//usart0_receive_buffer_remove()); } serial_write(buf,count); // Test serial_flush /* serial_write(buf,count); serial_flush(); serial_write(buf,count); */ // Test usart0_uninit() /* _delay_ms(500); serial_out("Before serial_end()\n",0); serial_end(); serial_out("After serial_end()\n",0); serial_begin(57600); serial_out("After serial_begin(57600)\n",0); */ } } }