void putMessage(MessageType msgType, uint16_t msgLength, uint16_t flags, const uint8_t *msg) { static uint32_t curId = 0; // Nasty way MsgHeader header; header.type = (uint16_t)msgType; header.flags = flags; header.length = msgLength; header.id = curId; header.crc = calcCrc16((uint8_t *)&header + 2, sizeof(MsgHeader) - 2); header.crc = calcCrc16(msg, msgLength, header.crc); curId++; // Start the message UARTputc(UART_PORT, '#'); // Send the message header writeHex((uint8_t *)&header, sizeof(header)); // Send the message writeHex(msg, msgLength); // End the message UARTputc(UART_PORT, '!'); }
static void writeHex(const uint8_t *buff, int buffSize) { int i; uint8_t *hexByte; for (i = 0; i < buffSize; i++) { hexByte = toHex(buff[i]); UARTputc(UART_PORT, hexByte[0]); UARTputc(UART_PORT, hexByte[1]); } }
void UARTputs(uint8_t UART, const char *str) { int a = 0; while (str[a] != '\0') { if (str[a] == '\n') { UARTputc(UART, '\r'); UARTputc(UART, '\n'); } else UARTputc(UART, str[a]); a++; } }
int addDatoBuffer(char input, UART *consolaBT, char *bufferInput) { int respuesta = -1; if ((input <= '9') && (input >= '0')) { UARTputc(consolaBT, input); bufferInput[iBuffer] = input; iBuffer++; bufferInput[iBuffer] = '\0'; } return (input == '\n'); }
//"gets" function through USBUART instead of stdio //echos the input //terminates on CR or LF //returns size of string uint16 UARTgets(uint8 * str, int bufferSize){ uint16 i = 0; uint16 size; do{ while(USBUART_DataIsReady() == 0); size = USBUART_GetCount(); if(size+i > bufferSize){ str[i]=0; UARTprintf("gets: Buffer not big enough. Size: %d Input: %s\r\n", size, str); return i; } USBUART_GetData(&(str[i]),size); i+=size; if(size == 1)UARTputc(str[i-1]); //echo if single char }while((str[i-1] != '\n') && (str[i-1] != '\r')); str[--i] = 0; //null terminate, remove the CR/LF //UARTprintf("gets: LF or CR found at end of message. Size: %d Input: %s\r\n", size, str); //while(USBUART_CDCIsReady()==0); //USBUART_PutCRLF(); //print CRLF return i; }
void UARTputln(uint8_t UART) { UARTputc(UART, '\r'); UARTputc(UART, '\n'); }