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: Queue mbed-rtos mbed
serial_bus.cpp
00001 #include "serial_bus.h" 00002 #include "mbed.h" 00003 #include "rtos.h" 00004 #include "Queue/queue.h" 00005 00006 /* CAN BUS */ 00007 CAN HSCAN(p30, p29); 00008 00009 void can_bus_rx_IRQ() 00010 { 00011 uint8 msg_raw[12]; 00012 CANMessage msg; 00013 if(HSCAN.read(msg)) 00014 { 00015 msg_raw[0] = (uint8)(msg.id >> 24); 00016 msg_raw[1] = (uint8)(msg.id >> 16); 00017 msg_raw[2] = (uint8)(msg.id >> 8); 00018 msg_raw[3] = (uint8)(msg.id >> 0); 00019 for(int i=0;i<8;i++) 00020 msg_raw[i+4] = msg.data[i]; 00021 00022 send_serial_bus(msg_raw,12); 00023 } 00024 } 00025 00026 void can_init(void) 00027 { 00028 HSCAN.frequency(500000); 00029 HSCAN.attach(can_bus_rx_IRQ,CAN::RxIrq); 00030 } 00031 00032 void can_send(uint32 id, uint8 * data) 00033 { 00034 HSCAN.write(CANMessage((int)id, (char *)data, 8)); 00035 } 00036 00037 /***********************/ 00038 00039 extern Serial pc; 00040 RawSerial uart0(p13,p14); 00041 00042 00043 #define PACKETS 10 00044 #define PACKET_SIZE 518 00045 #define BAUDRATE 921600 00046 00047 Queue uartQueue(1,PACKETS * PACKET_SIZE); 00048 00049 /********************************/ 00050 00051 void serial_data_callback() 00052 { 00053 while(uart0.readable()) 00054 { 00055 uint8 data = uart0.getc(); 00056 uartQueue.PutIrq(&data); 00057 } 00058 00059 } 00060 00061 #define STARTOFFRAME 4 00062 #define HEADER 0 00063 #define PAYLOAD 1 00064 #define CHECKSUM 2 00065 00066 00067 uint16 success = 0; 00068 uint16 errors = 0; 00069 00070 void wait_packet_layer(void) 00071 { 00072 static uint8 packet[PACKET_SIZE]; 00073 uint16 packet_index = 0; 00074 uint8 packet_state = STARTOFFRAME; 00075 uint16 packet_size = 0; 00076 uint8 packet_checksum = 0; 00077 00078 /* flush the data */ 00079 while(uart0.readable()) uart0.getc(); 00080 00081 uart0.attach(&serial_data_callback, Serial::RxIrq); 00082 wait(1.0); 00083 00084 uint8 data; 00085 while(1) 00086 { 00087 00088 /* get queue data */ 00089 while(uartQueue.Get(&data)) 00090 { 00091 switch(packet_state) 00092 { 00093 case STARTOFFRAME: 00094 if(data == 0xC0) 00095 { 00096 packet_state = HEADER; 00097 packet_index = 0; 00098 } 00099 break; 00100 case HEADER: 00101 packet[packet_index++] = data; 00102 if(packet_index == 4) 00103 { 00104 /* validating the packet header */ 00105 if( (packet[0] ^ packet[2]) == 0xFF && (packet[1] ^ packet[3]) == 0xFF ) 00106 { 00107 packet_size = ((uint16)packet[0] << 8) | (packet[1]); 00108 packet_state = PAYLOAD; 00109 packet_index = 0; 00110 packet_checksum = 0; 00111 } 00112 else 00113 { 00114 pc.printf("Wrong header\r\n"); 00115 packet_state = STARTOFFRAME; 00116 packet_index = 0; 00117 errors++; 00118 } 00119 } 00120 break; 00121 00122 case PAYLOAD: 00123 packet[packet_index++] = data; 00124 packet_checksum = packet_checksum ^ data; 00125 if(packet_index == (packet_size + 1)) 00126 { 00127 if(packet_checksum == 0) 00128 { 00129 packet_state = STARTOFFRAME; 00130 packet_index = 0; 00131 /* mark the packet for processing */ 00132 //pc.printf("Success = %d, Errors = %d \r\n",success,errors); 00133 //send_serial_bus(packet,packet_size); 00134 can_send(0x7E0,packet); 00135 success++; 00136 } 00137 else 00138 { 00139 pc.printf("Wrong checksum\r\n"); 00140 /* dropping the packet */ 00141 packet_state = STARTOFFRAME; 00142 packet_index = 0; 00143 errors++; 00144 } 00145 } 00146 break; 00147 } 00148 } 00149 } 00150 } 00151 00152 00153 void send_serial_bus(uint8 * packet, uint16 size) 00154 { 00155 uint8 cks = 0; 00156 uart0.putc(0xC0); 00157 uart0.putc((size >> 8) & 0xFF); 00158 uart0.putc((size >> 0) & 0xFF); 00159 size = size ^ 0xFFFF; 00160 uart0.putc((size >> 8) & 0xFF); 00161 uart0.putc((size >> 0) & 0xFF); 00162 size = size ^ 0xFFFF; 00163 00164 while(size > 0) 00165 { 00166 if(uart0.writeable()) 00167 { 00168 cks = cks ^ (*packet); 00169 uart0.putc(*packet++); 00170 size--; 00171 } 00172 } 00173 uart0.putc(cks); 00174 00175 00176 } 00177 00178 Thread uartThread; 00179 00180 void start_serial_bus(void) 00181 { 00182 can_init(); 00183 uart0.baud(BAUDRATE); 00184 uart0.attach(&serial_data_callback,Serial::RxIrq); 00185 uartThread.start(&wait_packet_layer); 00186 }
Generated on Thu Jul 14 2022 01:35:32 by
1.7.2