• I hope this it the correct place to post this..

    I'm trying to create a CAN Bus lib and I'm running Espruino on a Nucleo STM32F4 board. I'm not great with C but I can usually make my way around, but I've been just looking at other libs to try to figure this out.

    I'm running out of memory (I think) and the runtime crashes. I wrote a nodejs app to blast data (a frame every 2ms) to the Nucleo. When I use jsiConsolePrintf to dump the CAN frame out to the console it works without issue, so this tells me the Interrupt code is fine. But I want to dispatch an event into the JS runtime so that the frame data can be used there. When I do this the chip crashes after just a few minutes.

    Here is how I am dispatching the event:

    /**
      * @brief  Read CAN FIFO and dispatch JS event with the CAN Message data
      * @param  None
      * @retval None
      */
    void CANx_ReadAndDispatch(CAN_TypeDef *CANx, uint8_t FIFONumber){
    
      CanRxMsg RxMessage;
      CAN_Receive(CANx, FIFONumber, &RxMessage);
    
      // Dispatch event
      JsVar *messageObj = messageObjectFromRxStruct(&RxMessage);
    
      if(messageObj){
        jsiQueueObjectCallbacks(jsVarCan1, JS_EVENT_PREFIX"message", &messageObj, 1);
        jsvUnLock(messageObj);
      }
    }
    
    /*
    *   Build a JS Object from the CAN RX data
    */
    JsVar *messageObjectFromRxStruct(CanRxMsg *rx){
    
      uint32_t *id;
      // uint8_t size = rx->DLC+2;
      uint8_t size = 11;
      char dest[size];
    
      if(rx->IDE == CAN_ID_EXT)
        id = &rx->ExtId;
      else
        id = &rx->StdId;
    
      // memset(dest, '\0', size);
      dest[0] = (*id >> 8);
      dest[1] = (*id);
      dest[10] = rx->DLC;
      strncpy(dest+2, rx->Data, 8);
    
      // Mem check
      if (jsuGetFreeStack() < 88) {
        jsExceptionHere(JSET_ERROR, "CAN:: Not enough free stack to receive this mesage. stackfree:%d", jsuGetFreeStack());
        return 0;
      }
    
      JsVar *buffer = jsvNewArrayBufferWithData(11, dest);
      return buffer;
    
    }
    
About

Avatar for user63214 @user63214 started