tested
Dependencies: Queue mbed-rtos mbed
Revision 0:265e6a986bf1, committed 2017-07-27
- Comitter:
- andreixc
- Date:
- Thu Jul 27 12:53:20 2017 +0000
- Commit message:
- init
Changed in this revision
diff -r 000000000000 -r 265e6a986bf1 Queue.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Queue.lib Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/wbasser/code/Queue/#a03810d46457
diff -r 000000000000 -r 265e6a986bf1 can_bus.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/can_bus.cpp Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,65 @@ +#include "mbed.h" +#include "types.h" +#include "rtos.h" + +#if 0 + +CAN HSCAN(p30, p29); +uint32 id; +volatile uint32 available = 0; +Thread canThread; + +void can_init(void) +{ + //thread.start(); +} + +void can_bus_rx_IRQ() +{ + uint8 msg_raw[12]; + CANMessage msg; + if(HSCAN.read(msg)) + { + msg_raw[0] = (uint8)(msg.id >> 24); + msg_raw[1] = (uint8)(msg.id >> 16); + msg_raw[2] = (uint8)(msg.id >> 8); + msg_raw[3] = (uint8)(msg.id >> 0); + for(int i=0;i<8;i++) + msg_raw[i+4] = msg.data[i]; + } +} + +void can_bus_configure(uint32 txID,uint32 rxID,uint32 speed) +{ + available = 0; + HSCAN.reset(); + wait(2); // wait for 2 seconds + HSCAN.frequency(speed); + HSCAN.filter(rxID,0xFFFFFF); + HSCAN.attach(can_bus_rx_IRQ,CAN::RxIrq); + id = txID; +} + +void can_bus_send(uint8 * message, uint8 dlc=8) +{ + CANMessage msg; + msg.id = id; + msg.len = dlc; + for(int i=0;i<dlc;i++) msg.data[i] = message[i]; + HSCAN.write(msg); +} + +CANMessage can_bus_receive(void) +{ + if(available) + { + + } +} + +uint32 can_bus_available(void) +{ + return available; +} + +#endif \ No newline at end of file
diff -r 000000000000 -r 265e6a986bf1 can_bus.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/can_bus.h Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,13 @@ + +#include "types.h" + +#ifndef __CAN_BUS__ +#define __CAN_BUS__ + +void can_bus_configure(uint32 txID,uint32 rxID,uint32 speed); +void can_bus_send(uint8 * message, uint8 dlc=8); +CANMessage can_bus_receive(void); +uint32 can_bus_available(void); +void can_init(void); + +#endif \ No newline at end of file
diff -r 000000000000 -r 265e6a986bf1 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,33 @@ +#include "mbed.h" +#include "rtos.h" +#include "ringbuf.h" +#include "serial_bus.h" +#include "can_bus.h" + +Serial pc(USBTX, USBRX); + +DigitalOut reset(p21); +DigitalOut enable(p22); +DigitalOut reflash(p23); + +void main() { + + pc.baud(115200); + + /* setup the system before using it */ + reset = 0; + enable = 1; + reflash = 1; + + wait(0.1); + reset = 1; + wait(2.0); + pc.printf("mbed boot up... \r\n"); + + start_serial_bus(); + + while(1) + { + Thread::wait(100); + } +}
diff -r 000000000000 -r 265e6a986bf1 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed-rtos/#5713cbbdb706
diff -r 000000000000 -r 265e6a986bf1 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed/builds/22da6e220af6 \ No newline at end of file
diff -r 000000000000 -r 265e6a986bf1 ringbuf.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ringbuf.cpp Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,79 @@ +#include "ringbuf.h" +#include "types.h" + +uint8 ICACHE_FLASH_ATTR ringbuf_init(ringbuf_t *r, uint8* buf, uint16 size) +{ + if( (r == NULL) || (buf == NULL) || (size < 2) ) + return FALSE; + + r->p_o = r->p_r = r->p_w = buf; + r->fill_cnt = 0; + r->size = size; + r->p_e = r->p_o + r->size; + return TRUE; +} + +uint8 ICACHE_FLASH_ATTR ringbuf_put(ringbuf_t *r, uint8 c) +{ + if(r->fill_cnt>=r->size) + return FALSE; // ring buffer is full, this should be atomic operation + + r->fill_cnt++; // increase filled slots count, this should be atomic operation + + *r->p_w++ = c; // put character into buffer + + if(r->p_w >= r->p_e) // rollback if write pointer go pass + r->p_w = r->p_o; // the physical boundary + + return TRUE; +} + +uint8 ICACHE_FLASH_ATTR ringbuf_get(ringbuf_t *r, uint8 * c) +{ + if(r->fill_cnt == 0) + return FALSE; // ring buffer is empty, this should be atomic operation + + r->fill_cnt--; // decrease filled slots count + + *c = *r->p_r++; // get the character out + + if(r->p_r >= r->p_e) // rollback if write pointer go pass + r->p_r = r->p_o; // the physical boundary + + return TRUE; +} + +uint16 ICACHE_FLASH_ATTR ringbuf_gets(ringbuf_t *r, uint8 * buff, uint16 size) +{ + uint16 read=0; + while(size) + { + if(ringbuf_get(r,buff++) == FALSE) + break; + + read++; + size--; + } + return read; +} + +uint16 ICACHE_FLASH_ATTR ringbuf_puts(ringbuf_t *r, uint8 * buff, uint16 size) +{ + uint16 write=0; + while(size) + { + if(ringbuf_put(r,*buff++) == FALSE) + break; + + write++; + size--; + } + return write; +} + +uint8 ICACHE_FLASH_ATTR ringbuf_clear(ringbuf_t *r) +{ + r->p_r = r->p_w = r->p_o; + r->fill_cnt = 0; + return TRUE; +} \ No newline at end of file
diff -r 000000000000 -r 265e6a986bf1 ringbuf.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ringbuf.h Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,22 @@ +#include "types.h" + +#ifndef _RING_BUF_H_ +#define _RING_BUF_H_ + +typedef struct{ + uint8* p_o; /**< Original pointer */ + uint8* p_e; /**< End boundary */ + uint8* volatile p_r; /**< Read pointer */ + uint8* volatile p_w; /**< Write pointer */ + volatile uint16 fill_cnt; /**< Number of filled slots */ + volatile uint16 size; /**< Buffer size */ +}ringbuf_t; + +uint8 ICACHE_FLASH_ATTR ringbuf_init(ringbuf_t *r, uint8* buf, uint16 size); +uint8 ICACHE_FLASH_ATTR ringbuf_clear(ringbuf_t *r); +uint8 ICACHE_FLASH_ATTR ringbuf_put(ringbuf_t *r, uint8 c); +uint8 ICACHE_FLASH_ATTR ringbuf_get(ringbuf_t *r, uint8 * c); +uint16 ICACHE_FLASH_ATTR ringbuf_gets(ringbuf_t *r, uint8 * buff, uint16 size); +uint16 ICACHE_FLASH_ATTR ringbuf_puts(ringbuf_t *r, uint8 * buff, uint16 size); + +#endif
diff -r 000000000000 -r 265e6a986bf1 serial_bus.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/serial_bus.cpp Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,186 @@ +#include "serial_bus.h" +#include "mbed.h" +#include "rtos.h" +#include "Queue/queue.h" + +/* CAN BUS */ +CAN HSCAN(p30, p29); + +void can_bus_rx_IRQ() +{ + uint8 msg_raw[12]; + CANMessage msg; + if(HSCAN.read(msg)) + { + msg_raw[0] = (uint8)(msg.id >> 24); + msg_raw[1] = (uint8)(msg.id >> 16); + msg_raw[2] = (uint8)(msg.id >> 8); + msg_raw[3] = (uint8)(msg.id >> 0); + for(int i=0;i<8;i++) + msg_raw[i+4] = msg.data[i]; + + send_serial_bus(msg_raw,12); + } +} + +void can_init(void) +{ + HSCAN.frequency(500000); + HSCAN.attach(can_bus_rx_IRQ,CAN::RxIrq); +} + +void can_send(uint32 id, uint8 * data) +{ + HSCAN.write(CANMessage((int)id, (char *)data, 8)); +} + +/***********************/ + +extern Serial pc; +RawSerial uart0(p13,p14); + + +#define PACKETS 10 +#define PACKET_SIZE 518 +#define BAUDRATE 921600 + +Queue uartQueue(1,PACKETS * PACKET_SIZE); + +/********************************/ + +void serial_data_callback() +{ + while(uart0.readable()) + { + uint8 data = uart0.getc(); + uartQueue.PutIrq(&data); + } + +} + +#define STARTOFFRAME 4 +#define HEADER 0 +#define PAYLOAD 1 +#define CHECKSUM 2 + + +uint16 success = 0; +uint16 errors = 0; + +void wait_packet_layer(void) +{ + static uint8 packet[PACKET_SIZE]; + uint16 packet_index = 0; + uint8 packet_state = STARTOFFRAME; + uint16 packet_size = 0; + uint8 packet_checksum = 0; + + /* flush the data */ + while(uart0.readable()) uart0.getc(); + + uart0.attach(&serial_data_callback, Serial::RxIrq); + wait(1.0); + + uint8 data; + while(1) + { + + /* get queue data */ + while(uartQueue.Get(&data)) + { + switch(packet_state) + { + case STARTOFFRAME: + if(data == 0xC0) + { + packet_state = HEADER; + packet_index = 0; + } + break; + case HEADER: + packet[packet_index++] = data; + if(packet_index == 4) + { + /* validating the packet header */ + if( (packet[0] ^ packet[2]) == 0xFF && (packet[1] ^ packet[3]) == 0xFF ) + { + packet_size = ((uint16)packet[0] << 8) | (packet[1]); + packet_state = PAYLOAD; + packet_index = 0; + packet_checksum = 0; + } + else + { + pc.printf("Wrong header\r\n"); + packet_state = STARTOFFRAME; + packet_index = 0; + errors++; + } + } + break; + + case PAYLOAD: + packet[packet_index++] = data; + packet_checksum = packet_checksum ^ data; + if(packet_index == (packet_size + 1)) + { + if(packet_checksum == 0) + { + packet_state = STARTOFFRAME; + packet_index = 0; + /* mark the packet for processing */ + //pc.printf("Success = %d, Errors = %d \r\n",success,errors); + //send_serial_bus(packet,packet_size); + can_send(0x7E0,packet); + success++; + } + else + { + pc.printf("Wrong checksum\r\n"); + /* dropping the packet */ + packet_state = STARTOFFRAME; + packet_index = 0; + errors++; + } + } + break; + } + } + } +} + + +void send_serial_bus(uint8 * packet, uint16 size) +{ + uint8 cks = 0; + uart0.putc(0xC0); + uart0.putc((size >> 8) & 0xFF); + uart0.putc((size >> 0) & 0xFF); + size = size ^ 0xFFFF; + uart0.putc((size >> 8) & 0xFF); + uart0.putc((size >> 0) & 0xFF); + size = size ^ 0xFFFF; + + while(size > 0) + { + if(uart0.writeable()) + { + cks = cks ^ (*packet); + uart0.putc(*packet++); + size--; + } + } + uart0.putc(cks); + + +} + +Thread uartThread; + +void start_serial_bus(void) +{ + can_init(); + uart0.baud(BAUDRATE); + uart0.attach(&serial_data_callback,Serial::RxIrq); + uartThread.start(&wait_packet_layer); +}
diff -r 000000000000 -r 265e6a986bf1 serial_bus.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/serial_bus.h Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,9 @@ +#include "types.h" + +#ifndef __SERIAL_BUS__ +#define __SERIAL_BUS__ + +void start_serial_bus(void); +void send_serial_bus(uint8 * packet, uint16 size); + +#endif \ No newline at end of file
diff -r 000000000000 -r 265e6a986bf1 types.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/types.h Thu Jul 27 12:53:20 2017 +0000 @@ -0,0 +1,17 @@ +#ifndef __TYPES__ +#define __TYPES__ + +#ifndef NULL + #define NULL ((void *)0) +#endif + +#define TRUE (1) +#define FALSE (0) + +typedef unsigned char uint8; +typedef unsigned short uint16; +typedef unsigned long uint32; + +#define ICACHE_FLASH_ATTR + +#endif \ No newline at end of file