Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MbedJSONValue
Diff: main.cpp
- Revision:
- 0:6080c5029b3f
- Child:
- 1:711a27baa42d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Jan 15 10:40:06 2018 +0000
@@ -0,0 +1,215 @@
+////////////////////////////////////////
+// Serial Packet - picojson //
+// Arkadiraf@gmail.com - 05/06/2017 //
+////////////////////////////////////////
+/*
+ Construct packet from uart and parse with json format
+ json:<PACKET>\r\n
+*/
+
+/*
+ Board : Nucleo STM32F446RE
+*/
+
+/*
+ Pinout:
+ PC - Serial 2
+ PA_2 (Tx) --> STLINK
+ PA_3 (Rx) --> STLINK
+*/
+
+///////////////
+// Libraries //
+///////////////
+#include "mbed.h"
+#include "picojson.h"
+//#include "BufferedSerial.h" // uart handler library
+///////////////
+// #defines //
+///////////////
+
+#define DEBUG_MOD1
+#define MSG_BUFFER_SIZE 512
+#define HEADER_SIZE 5
+#define FOOTER_SIZE 2
+
+/////////////
+// Objects //
+/////////////
+
+// uart
+Serial pc(USBTX, USBRX);
+
+// digital output
+DigitalOut led(PA_5);
+///////////////
+// variables //
+///////////////
+
+// json buffer
+char json[MSG_BUFFER_SIZE];
+
+// packet variables
+struct packetMSG_struct {
+ // recieve message variables
+ uint8_t header[HEADER_SIZE];
+ uint8_t footer[FOOTER_SIZE];
+ uint8_t syncIndex; // sync index for header / footer
+ uint8_t syncFlag; // 0 - waiting for header, 1 - waiting for footer, 2 - verify footer, 3 - finish footer send to parser, flash buffer
+ // buffer
+ uint16_t bufferIndex; // buffer index
+ uint8_t buffer[MSG_BUFFER_SIZE];
+} ;
+packetMSG_struct packetMSG;
+///////////////
+// Functions //
+///////////////
+
+// Serial Event function
+void rxCallback(void);
+
+// initialize packet struct
+void initPacket(void);
+////////////////////////
+// Main Code Setup : //
+////////////////////////
+int main()
+{
+ // init packet:
+ initPacket();
+ // init uart
+ pc.baud(57600);
+ // attach serial event interrupt
+ pc.attach(&rxCallback, Serial::RxIrq);
+
+#ifdef DEBUG_MOD1
+ pc.printf("UART Test \r\n");
+#endif
+ ///////////////////////
+ // Main Code Loop : //
+ ///////////////////////
+ while(1) {
+ //sleep();
+ }// end main loop
+}// end main
+///////////////
+// Functions //
+///////////////
+
+// Serial Event function
+void rxCallback(void)
+{
+ while (pc.readable()) {
+ // read icoming
+ led = !led;
+ uint8_t in_byte = pc.getc();
+#ifdef DEBUG_MOD1
+ pc.putc(in_byte);
+#endif
+ // detect start message , end message
+ switch (packetMSG.syncFlag) {
+ // waiting for header
+ case 0: {
+ if (packetMSG.header[packetMSG.syncIndex] == in_byte) {
+ packetMSG.syncIndex++;
+ if (packetMSG.syncIndex == HEADER_SIZE) { // finish header SYNC
+ packetMSG.syncFlag = 1; // start collecting data, wait for footer
+ packetMSG.bufferIndex = 0;
+ packetMSG.syncIndex=0;
+ }
+ } else { // reinit sync
+ packetMSG.syncIndex=0;
+ }
+ //pc.printf("case 0 , %d \r\n",packetMSG.syncIndex);
+ break;
+
+ } // waiting for header
+
+ // waiting for footer
+ case 1: {
+ // add byte to buffer
+ packetMSG.buffer[packetMSG.bufferIndex] = in_byte;
+ packetMSG.bufferIndex++;
+ if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow
+ // reset buffer
+ packetMSG.bufferIndex = 0;
+ packetMSG.syncIndex = 0;
+ packetMSG.syncFlag = 0;
+ } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved
+ packetMSG.syncIndex++;
+ packetMSG.syncFlag=2; // move to verify footer
+ }
+ //pc.printf("case 2 , %d \r\n",packetMSG.syncIndex);
+ break;
+ } // waiting for footer
+
+ // verify footer
+ case 2: {
+ // add byte to buffer
+ packetMSG.buffer[packetMSG.bufferIndex] = in_byte;
+ packetMSG.bufferIndex++;
+ if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow
+ // reset buffer
+ packetMSG.bufferIndex = 0;
+ packetMSG.syncIndex = 0;
+ packetMSG.syncFlag = 0;
+ } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved
+ packetMSG.syncIndex++;
+ if (packetMSG.syncIndex == FOOTER_SIZE) { // finish footer SYNC
+ packetMSG.syncFlag = 3;
+
+ // copy packet to json buffer - failed attempt
+ //char *json = (char*) malloc(packetMSG.bufferIndex+1);
+ memcpy (&json, &packetMSG.buffer, packetMSG.bufferIndex);
+ json[packetMSG.bufferIndex]=NULL; // end NULL to indicate end of string
+
+ // copy packet to json buffer with sprintf
+ //sprintf(json, "%.*s", packetMSG.bufferIndex, packetMSG.buffer );
+
+ // send msg to parse.
+ pc.printf("%d, %.*s", packetMSG.bufferIndex ,packetMSG.bufferIndex, packetMSG.buffer );
+ pc.printf("%d, %s", sizeof(json), json);
+ //free(json);
+ // reset buffer
+ packetMSG.bufferIndex = 0;
+ packetMSG.syncIndex = 0;
+ packetMSG.syncFlag = 0;
+ }
+ } else { // footer broke restart wait for footer
+ packetMSG.syncFlag=1;
+ // verify that it didnt broke on first footer char
+ if (packetMSG.footer[0] == in_byte) {
+ packetMSG.syncIndex=1;
+ } else {
+ packetMSG.syncIndex=0;
+ }
+ }
+ break;
+
+ } // verify footer
+ default: {
+ pc.printf("Sonmething went wrong \r\n");
+ break;
+ }
+ } // end switch
+ }// end uart readable
+} // end rxCallback
+
+
+// initialize packet struct
+void initPacket(void)
+{
+ // init variables to default:
+ packetMSG.header[0] = 'j';
+ packetMSG.header[1] = 's';
+ packetMSG.header[2] = 'o';
+ packetMSG.header[3] = 'n';
+ packetMSG.header[4] = ':';
+
+ packetMSG.footer[0]= 13; // /r
+ packetMSG.footer[1]= 10; // /n
+
+ packetMSG.syncIndex=0; // sync index for header / footer
+ packetMSG.syncFlag=0; // 0 - waiting for header, 1 - waiting for footer, 2 - verify footer, 3 - finish footer send to parser, flash buffer
+ packetMSG.bufferIndex=0; // buffer index
+}
\ No newline at end of file