diff --git a/F0-nolib/Snippets/printuhex.c b/F0-nolib/Snippets/printuhex.c new file mode 100644 index 0000000..34f1c33 --- /dev/null +++ b/F0-nolib/Snippets/printuhex.c @@ -0,0 +1,14 @@ +// print 32bit unsigned int as hex +void printuhex(uint32_t val){ + addtobuf("0x"); + uint8_t *ptr = (uint8_t*)&val + 3; + int8_t i, j; + for(i = 0; i < 4; ++i, --ptr){ + if(*ptr == 0 && i != 3) continue; // omit leading zeros + for(j = 1; j > -1; --j){ + uint8_t half = (*ptr >> (4*j)) & 0x0f; + if(half < 10) bufputchar(half + '0'); + else bufputchar(half - 10 + 'a'); + } + } +} diff --git a/F0-nolib/usbcdc/can.c b/F0-nolib/usbcdc/can.c index f499723..860658d 100644 --- a/F0-nolib/usbcdc/can.c +++ b/F0-nolib/usbcdc/can.c @@ -31,6 +31,7 @@ static CAN_message messages[CAN_INMESSAGE_SIZE]; static uint8_t first_free_idx = 0; // index of first empty cell static int8_t first_nonfree_idx = -1; // index of first data cell +static uint16_t oldspeed = 100; // speed of last init static uint16_t CANID = 0xFFFF; #ifdef EBUG @@ -52,14 +53,14 @@ CAN_status CAN_get_status(){ // push next message into buffer; return 1 if buffer overfull static int CAN_messagebuf_push(CAN_message *msg){ - MSG("Try to push\n"); + //MSG("Try to push\n"); if(first_free_idx == first_nonfree_idx) return 1; // no free space if(first_nonfree_idx < 0) first_nonfree_idx = 0; // first message in empty buffer memcpy(&messages[first_free_idx++], msg, sizeof(CAN_message)); // need to roll? if(first_free_idx == CAN_INMESSAGE_SIZE) first_free_idx = 0; #ifdef EBUG - MSG("1st free: "); usart_putchar('0' + first_free_idx); NL(); + //MSG("1st free: "); printu(first_free_idx); NL(); #endif return 0; } @@ -68,14 +69,16 @@ static int CAN_messagebuf_push(CAN_message *msg){ CAN_message *CAN_messagebuf_pop(){ if(first_nonfree_idx < 0) return NULL; #ifdef EBUG - MSG("read from idx "); usart_putchar('0' + first_nonfree_idx); NL(); + //MSG("read from idx "); printu(first_nonfree_idx); NL(); #endif CAN_message *msg = &messages[first_nonfree_idx++]; if(first_nonfree_idx == CAN_INMESSAGE_SIZE) first_nonfree_idx = 0; if(first_nonfree_idx == first_free_idx){ // buffer is empty - refresh it first_nonfree_idx = -1; first_free_idx = 0; - MSG("refresh buffer\n"); +#ifdef EBUG + // MSG("refresh buffer\n"); NL(); +#endif } return msg; } @@ -91,15 +94,42 @@ uint16_t getCANID(){ return CANID; } -void CAN_reinit(){ +void CAN_reinit(uint16_t speed){ readCANID(); CAN->TSR |= CAN_TSR_ABRQ0 | CAN_TSR_ABRQ1 | CAN_TSR_ABRQ2; RCC->APB1RSTR |= RCC_APB1RSTR_CANRST; RCC->APB1RSTR &= ~RCC_APB1RSTR_CANRST; - CAN_setup(); + CAN_setup(speed); } -void CAN_setup(){ +/* +Can filtering: FSCx=0 (CAN->FS1R) -> 16-bit identifiers +MASK: FBMx=0 (CAN->FM1R), two filters (n in FR1 and n+1 in FR2) + ID: CAN->sFilterRegister[x].FRn[0..15] + MASK: CAN->sFilterRegister[x].FRn[16..31] + FR bits: STID[10:0] RTR IDE EXID[17:15] +LIST: FBMx=1, four filters (n&n+1 in FR1, n+2&n+3 in FR2) + IDn: CAN->sFilterRegister[x].FRn[0..15] + IDn+1: CAN->sFilterRegister[x].FRn[16..31] +*/ + +/* +Can timing: main freq - APB (PLL=48MHz) +segment = 1sync + TBS1 + TBS2, sample point is between TBS1 and TBS2, +so if TBS1=4 and TBS2=3, sum=8, bit sampling freq is 48/8 = 6MHz +-> to get 100kbps we need prescaler=60 + 250kbps - 24 + 500kbps - 12 + 1MBps - 6 +*/ + +// speed - in kbps +void CAN_setup(uint16_t speed){ + LED_off(LED1); + if(speed == 0) speed = oldspeed; + else if(speed < 50) speed = 50; + else if(speed > 3000) speed = 3000; + oldspeed = speed; uint32_t tmout = 16000000; if(CANID == 0xFFFF) readCANID(); // Configure GPIO: PB8 - CAN_Rx, PB9 - CAN_Tx @@ -115,10 +145,10 @@ void CAN_setup(){ /* (1) Enter CAN init mode to write the configuration */ /* (2) Wait the init mode entering */ /* (3) Exit sleep mode */ - /* (4) Loopback mode, set timing to 100kb/s: BS1 = 4, BS2 = 3, prescaler = 60 */ + /* (4) Normal mode, set timing to 100kb/s: TBS1 = 4, TBS2 = 3, prescaler = 60 */ /* (5) Leave init mode */ /* (6) Wait the init mode leaving */ - /* (7) Enter filter init mode, (16-bit + mask, filter 0 for FIFO 0) */ + /* (7) Enter filter init mode, (16-bit + mask, bank 0 for FIFO 0) */ /* (8) Acivate filter 0 for two IDs */ /* (9) Identifier list mode */ /* (10) Set the Id list */ @@ -130,20 +160,22 @@ void CAN_setup(){ if(--tmout == 0) break; } CAN->MCR &=~ CAN_MCR_SLEEP; /* (3) */ - CAN->MCR |= CAN_MCR_ABOM; + CAN->MCR |= CAN_MCR_ABOM; /* allow automatically bus-off */ - CAN->BTR |= 2 << 20 | 3 << 16 | 59 << 0; /* (4) */ + CAN->BTR |= 2 << 20 | 3 << 16 | (6000/speed) << 0; /* (4) */ CAN->MCR &=~ CAN_MCR_INRQ; /* (5) */ tmout = 16000000; - while((CAN->MSR & CAN_MSR_INAK)==CAN_MSR_INAK) /* (6) */ - { - if(--tmout == 0) break; - } + while((CAN->MSR & CAN_MSR_INAK)==CAN_MSR_INAK) if(--tmout == 0) break; /* (6) */ + // accept ALL CAN->FMR = CAN_FMR_FINIT; /* (7) */ CAN->FA1R = CAN_FA1R_FACT0; /* (8) */ + // set to 1 all needed bits of CAN->FFA1R to switch given filters to FIFO1 +#if 0 CAN->FM1R = CAN_FM1R_FBM0; /* (9) */ CAN->sFilterRegister[0].FR1 = CANID << 5 | ((BCAST_ID << 5) << 16); /* (10) */ - +#else + CAN->sFilterRegister[0].FR1 = 0; +#endif CAN->FMR &=~ CAN_FMR_FINIT; /* (12) */ CAN->IER |= CAN_IER_ERRIE | CAN_IER_FOVIE0 | CAN_IER_FOVIE1; /* (13) */ @@ -200,9 +232,8 @@ void can_proc(){ // reset CAN bus RCC->APB1RSTR |= RCC_APB1RSTR_CANRST; RCC->APB1RSTR &= ~RCC_APB1RSTR_CANRST; - CAN_setup(); + CAN_setup(0); } - LED_off(LED1); #if 0 static uint32_t esr, msr, tsr; uint32_t msr_now = CAN->MSR & 0xf; @@ -212,7 +243,6 @@ void can_proc(){ NL(); } if((CAN->ESR) != esr){ - usart_putchar(((CAN->ESR & CAN_ESR_BOFF) != 0) + '0'); esr = CAN->ESR; MSG("CAN->ESR: "); printuhex(esr); NL(); @@ -236,8 +266,20 @@ CAN_status can_send(uint8_t *msg, uint8_t len, uint16_t target_id){ if(CAN->TSR & (CAN_TSR_TME)){ mailbox = (CAN->TSR & CAN_TSR_CODE) >> 24; }else{ // no free mailboxes +#ifdef EBUG + MSG("No free mailboxes"); NL(); +#endif return CAN_BUSY; } +#ifdef EBUG + MSG("Send data. Len="); printu(len); + SEND(", tagid="); printuhex(target_id); + SEND(", data="); + for(int i = 0; i < len; ++i){ + SEND(" "); printuhex(msg[i]); + } + NL(); +#endif CAN_TxMailBox_TypeDef *box = &CAN->sTxMailBox[mailbox]; uint32_t lb = 0, hb = 0; switch(len){ @@ -291,14 +333,16 @@ void can_send_broadcast(){ static void can_process_fifo(uint8_t fifo_num){ if(fifo_num > 1) return; - LED_on(LED1); // Toggle LED1 + LED_on(LED1); // Turn on LED1 - message received CAN_FIFOMailBox_TypeDef *box = &CAN->sFIFOMailBox[fifo_num]; volatile uint32_t *RFxR = (fifo_num) ? &CAN->RF1R : &CAN->RF0R; - MSG("Receive, RDTR="); + /* + MSG("\nReceive, RDTR="); #ifdef EBUG printuhex(box->RDTR); NL(); #endif + */ // read all while(*RFxR & CAN_RF0R_FMP0){ // amount of messages pending // CAN_RDTxR: (16-31) - timestamp, (8-15) - filter match index, (0-3) - data length @@ -307,6 +351,7 @@ static void can_process_fifo(uint8_t fifo_num){ uint8_t *dat = msg.data; uint8_t len = box->RDTR & 0x7; msg.length = len; + msg.ID = box->RIR >> 21; if(len){ // message can be without data uint32_t hb = box->RDHR, lb = box->RDLR; switch(len){ @@ -338,7 +383,8 @@ static void can_process_fifo(uint8_t fifo_num){ if(CAN_messagebuf_push(&msg)) return; // error: buffer is full, try later *RFxR |= CAN_RF0R_RFOM0; // release fifo for access to next message } - if(*RFxR & CAN_RF0R_FULL0) *RFxR &= ~CAN_RF0R_FULL0; + //if(*RFxR & CAN_RF0R_FULL0) *RFxR &= ~CAN_RF0R_FULL0; + *RFxR = 0; // clear FOVR & FULL } void cec_can_isr(){ diff --git a/F0-nolib/usbcdc/can.h b/F0-nolib/usbcdc/can.h index 50f8206..859d71a 100644 --- a/F0-nolib/usbcdc/can.h +++ b/F0-nolib/usbcdc/can.h @@ -41,9 +41,11 @@ // incoming message buffer size #define CAN_INMESSAGE_SIZE (8) +// CAN message typedef struct{ - uint8_t data[8]; - uint8_t length; + uint8_t data[8]; // up to 8 bytes of data + uint8_t length; // data length + uint16_t ID; // ID of receiver } CAN_message; typedef enum{ @@ -59,9 +61,10 @@ CAN_status CAN_get_status(); void readCANID(); uint16_t getCANID(); -void CAN_reinit(); -void CAN_setup(); +void CAN_reinit(uint16_t speed); +void CAN_setup(uint16_t speed); +CAN_status can_send(uint8_t *msg, uint8_t len, uint16_t target_id); void can_send_dummy(); void can_send_broadcast(); void can_proc(); diff --git a/F0-nolib/usbcdc/main.c b/F0-nolib/usbcdc/main.c index f4f0a52..2537e9b 100644 --- a/F0-nolib/usbcdc/main.c +++ b/F0-nolib/usbcdc/main.c @@ -100,7 +100,7 @@ int main(void){ gpio_setup(); usart_setup(); readCANID(); - CAN_setup(); + CAN_setup(100); switchbuff(0); SEND("Greetings! My address is "); @@ -127,19 +127,24 @@ int main(void){ can_proc(); usb_proc(); if(CAN_get_status() == CAN_FIFO_OVERRUN){ - SEND("CAN bus fifo overrun occured!\n"); NL(); + switchbuff(3); + SEND("CAN bus fifo overrun occured!\n"); + newline(); sendbuf(); } can_mesg = CAN_messagebuf_pop(); - if(can_mesg){ // new data in buff + if(can_mesg && isgood(can_mesg->ID)){ // new data in buff len = can_mesg->length; - switchbuff(0); - SEND("got message, len: "); usart_putchar('0' + len); + switchbuff(3); + SEND("got message, ID="); + printuhex(can_mesg->ID); + SEND(", len="); + printu(len); SEND(", data: "); for(ctr = 0; ctr < len; ++ctr){ printuhex(can_mesg->data[ctr]); - usart_putchar(' '); + SEND(" "); } - NL(); + newline(); sendbuf(); } if(usartrx()){ // usart1 received data, store in in buffer usart_getline(&txt); diff --git a/F0-nolib/usbcdc/proto.c b/F0-nolib/usbcdc/proto.c index 4702c96..df31e22 100644 --- a/F0-nolib/usbcdc/proto.c +++ b/F0-nolib/usbcdc/proto.c @@ -30,6 +30,8 @@ extern volatile uint8_t canerror; +uint16_t Ignore_IDs[IGN_SIZE]; +uint8_t IgnSz = 0; static char buff[UARTBUFSZ+1], *bptr = buff; static uint8_t blen = 0, USBcmd = 0; @@ -38,7 +40,7 @@ void sendbuf(){ if(blen == 0) return; *bptr = 0; if(USBcmd) USB_sendstr(buff); - else{ + if(USBcmd != 1){ usart_send(buff); transmit_tbuf(); } @@ -46,6 +48,7 @@ void sendbuf(){ blen = 0; } +// 1 - USB, 0 - USART, other number - both void switchbuff(uint8_t isUSB){ USBcmd = isUSB; } @@ -158,24 +161,107 @@ char *getnum(char *txt, uint32_t *N){ // send command, format: ID (hex/bin/dec) data bytes (up to 8 bytes, space-delimeted) -void sendCANcommand(char *txt){ +TRUE_INLINE void sendCANcommand(char *txt){ SEND("CAN command with arguments:\n"); uint32_t N; + uint16_t ID = 0xffff; char *n; + uint8_t data[8]; + int ctr = -1; do{ txt = omit_spaces(txt); n = getnum(txt, &N); if(txt == n) break; + txt = n; + if(ctr == -1){ + if(N > 0x7ff){ + SEND("ID should be 11-bit number!"); + return; + } + ID = (uint16_t)(N&0x7ff); + SEND("ID="); printuhex(ID); newline(); + ctr = 0; + continue; + } + if(ctr > 7){ + SEND("ONLY 8 data bytes allowed!"); + return; + } + if(N > 0xff){ + SEND("Every data portion is a byte!"); + return; + } + data[ctr++] = (uint8_t)(N&0xff); printu(N); SEND(", hex: "); printuhex(N); newline(); - txt = n; }while(1); if(*n){ - SEND("\nThe rest: "); + SEND("\nUnusefull data: "); SEND(n); } - newline(); + if(ID == 0xffff){ + SEND("NO ID given, send nothing!"); + return; + } sendbuf(); + N = 1000000; + while(CAN_BUSY == can_send(data, (uint8_t)ctr, ID)){ + if(--N == 0) break; + } +} + +TRUE_INLINE void CANini(char *txt){ + txt = omit_spaces(txt); + uint32_t N; + char *n = getnum(txt, &N); + if(txt == n){ + SEND("No speed given"); + return; + } + if(N < 50){ + SEND("Lowest speed is 50kbps"); + return; + }else if(N > 3000){ + SEND("Highest speed is 3000kbps"); + return; + } + CAN_reinit((uint16_t)N); + SEND("Reinit CAN bus with speed "); + printu(N); SEND("kbps"); +} + +TRUE_INLINE void addIGN(char *txt){ + if(IgnSz == IGN_SIZE){ + MSG("Ignore buffer is full"); + return; + } + txt = omit_spaces(txt); + uint32_t N; + char *n = getnum(txt, &N); + if(txt == n){ + SEND("No ID given"); + return; + } + if(N > 0x7ff){ + SEND("ID should be 11-bit number!"); + return; + } + Ignore_IDs[IgnSz++] = (uint16_t)(N & 0x7ff); + SEND("Added ID "); printu(N); + SEND("\nIgn buffer size: "); printu(IgnSz); +} + +TRUE_INLINE void print_ign_buf(){ + if(IgnSz == 0){ + SEND("Ignore buffer is empty"); + return; + } + for(int i = 0; i < IgnSz; ++i){ + printu(i); + SEND(": "); + printuhex(Ignore_IDs[i]); + newline(); + } } /** @@ -190,9 +276,19 @@ void cmd_parser(char *txt, uint8_t isUSB){ /* * parse long commands here */ - if(_1st == 's' || _1st == 'S'){ - sendCANcommand(txt + 1); - return; + switch(_1st){ + case 's': + case 'S': + sendCANcommand(txt + 1); + goto eof; + break; + case 'b': + CANini(txt + 1); + goto eof; + break; + case 'a': + addIGN(txt + 1); + goto eof; } if(txt[1] != '\n') *txt = '?'; // help for wrong message length switch(_1st){ @@ -205,17 +301,23 @@ void cmd_parser(char *txt, uint8_t isUSB){ case 'C': can_send_dummy(); break; + case 'd': + IgnSz = 0; + break; case 'G': SEND("Can address: "); printuhex(getCANID()); newline(); break; case 'I': - CAN_reinit(); + CAN_reinit(0); SEND("Can address: "); printuhex(getCANID()); newline(); break; + case 'p': + print_ign_buf(); + break; case 'R': SEND("Soft reset\n"); sendbuf(); @@ -238,11 +340,15 @@ void cmd_parser(char *txt, uint8_t isUSB){ break; default: // help SEND( + "'a' - add ID to ignore list (max 10 IDs)\n" + "'b' - reinit CAN with given baudrate\n" "'B' - send broadcast dummy byte\n" "'C' - send dummy byte over CAN\n" + "'d' - delete ignore list\n" "'f' - flush UART buffer\n" "'G' - get CAN address\n" "'I' - reinit CAN (with new address)\n" + "'p' - print ignore buffer\n" "'R' - software reset\n" "'s/S' - send data over CAN: s ID byte0 .. byteN\n" "'T' - gen time from start (ms)\n" @@ -251,6 +357,8 @@ void cmd_parser(char *txt, uint8_t isUSB){ ); break; } +eof: + newline(); sendbuf(); } @@ -273,8 +381,9 @@ void printu(uint32_t val){ void printuhex(uint32_t val){ addtobuf("0x"); uint8_t *ptr = (uint8_t*)&val + 3; - int i, j; + int8_t i, j; for(i = 0; i < 4; ++i, --ptr){ + if(*ptr == 0 && i != 3) continue; // omit leading zeros for(j = 1; j > -1; --j){ uint8_t half = (*ptr >> (4*j)) & 0x0f; if(half < 10) bufputchar(half + '0'); @@ -282,3 +391,10 @@ void printuhex(uint32_t val){ } } } + +// check Ignore_IDs & return 1 if ID isn't in list +uint8_t isgood(uint16_t ID){ + for(int i = 0; i < IgnSz; ++i) + if(Ignore_IDs[i] == ID) return 0; + return 1; +} diff --git a/F0-nolib/usbcdc/proto.h b/F0-nolib/usbcdc/proto.h index e41b5a1..1f4905e 100644 --- a/F0-nolib/usbcdc/proto.h +++ b/F0-nolib/usbcdc/proto.h @@ -40,6 +40,10 @@ // newline with buffer sending over USART #define NL() do{bufputchar('\n'); switchbuff(0); sendbuf();}while(0) +#define IGN_SIZE 10 +extern uint16_t Ignore_IDs[IGN_SIZE]; +extern uint8_t IgnSz; + void cmd_parser(char *buf, uint8_t isUSB); void addtobuf(const char *txt); void bufputchar(char ch); @@ -51,6 +55,8 @@ void switchbuff(uint8_t isUSB); char *omit_spaces(char *buf); char *getnum(char *buf, uint32_t *N); +uint8_t isgood(uint16_t ID); + //int strlen(const char *txt); //void memcpy(void *dest, const void *src, int len); #endif // __PROTO_H__ diff --git a/F0-nolib/usbcdc/usb_defs.h b/F0-nolib/usbcdc/usb_defs.h index 5054528..f7e1339 100644 --- a/F0-nolib/usbcdc/usb_defs.h +++ b/F0-nolib/usbcdc/usb_defs.h @@ -33,7 +33,7 @@ * Buffers size definition **/ // !!! when working with CAN bus change USB_BTABLE_SIZE to 768 !!! -#define USB_BTABLE_SIZE 1024 +#define USB_BTABLE_SIZE 768 // for USB FS EP0 buffers are from 8 to 64 bytes long (64 for PL2303) #define USB_EP0_BUFSZ 64 // USB transmit buffer size (64 for PL2303) diff --git a/F0-nolib/usbcdc/usb_lib.c b/F0-nolib/usbcdc/usb_lib.c index 8ba2acb..9449ca9 100644 --- a/F0-nolib/usbcdc/usb_lib.c +++ b/F0-nolib/usbcdc/usb_lib.c @@ -357,7 +357,7 @@ int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*f if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE) return 2; // out of btable USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA); USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1; - if(rxsz & 1 || rxsz > 992) return 3; // wrong rx buffer size + if(rxsz & 1 || rxsz > 512) return 3; // wrong rx buffer size uint16_t countrx = 0; if(rxsz < 64) countrx = rxsz / 2; else{ diff --git a/F0-nolib/usbcdc/usbcan.bin b/F0-nolib/usbcdc/usbcan.bin index 3b8cfc9..374a6d1 100755 Binary files a/F0-nolib/usbcdc/usbcan.bin and b/F0-nolib/usbcdc/usbcan.bin differ