Ejemplo n.º 1
0
static size_t _uart_get_one_packet(uint8_t* inBuf, int inBufLen)
{
  
  int datalen;
  
  while(1) {
    if( MicoUartRecv( UART_FOR_APP, inBuf, inBufLen, 500) == kNoErr){
      return inBufLen;
    }
    else{
      datalen = MicoUartGetLengthInBuffer( UART_FOR_APP );
      if(datalen){
        MicoUartRecv(UART_FOR_APP, inBuf, datalen, 500);
        return datalen;
      }
    }
    
  }
  
}
Ejemplo n.º 2
0
/* Packet format: BB 00 CMD(2B) Status(2B) datalen(2B) data(x) checksum(2B)
* copy to buf, return len = datalen+10
*/
size_t _uart_get_one_packet(uint8_t* inBuf, int inBufLen)
{
    uart_recv_log_trace();

    int datalen;

    while(1)
    {
        if( MicoUartRecv( UART_FOR_APP, inBuf, inBufLen, UART_RECV_TIMEOUT) == kNoErr)
        {
            return inBufLen;
        }
        else
        {
            datalen = MicoUartGetLengthInBuffer( UART_FOR_APP );
            if(datalen)
            {
                MicoUartRecv(UART_FOR_APP, inBuf, datalen, UART_RECV_TIMEOUT);
                return datalen;
            }
        }
    }

}
uint32_t user_uartRecv(unsigned char *outBuf, unsigned int getLen)
{
  unsigned int data_len = 0;

  if( (NULL == outBuf) || (0 == getLen) ){
    user_uart_log("ERROR: user_uartRecv input params error!");
    return 0;
  }
  
  if( MicoUartRecv( USER_UART, outBuf, getLen, USER_UART_RECV_TIMEOUT) == kNoErr){
    data_len = getLen;
  }
  else{
    data_len = MicoUartGetLengthInBuffer( USER_UART );
    if(data_len){
      MicoUartRecv(USER_UART, outBuf, data_len, USER_UART_RECV_TIMEOUT);
    }
    else{
      data_len = 0;
    }
  }
  
  return data_len;
}
Ejemplo n.º 4
0
static int get_line()
{
#define CNTLQ      0x11
#define CNTLS      0x13
#define DEL        0x7F
#define BACKSPACE  0x08
#define CR         0x0D
#define LF         0x0A
  
  char *p = cmd_str;
  int i = 0;
  char c;
  
  memset(cmd_str, 0, sizeof(cmd_str));
  while(1) {
    if( MicoUartRecv( MFG_TEST, p, 1, 100) != kNoErr)
      continue;
    
    mf_putc(*p);
    if (*p == BACKSPACE  ||  *p == DEL)  {
      if(i>0) {
        c = 0x20;
        mf_putc(c); 
        mf_putc(*p); 
        p--;
        i--; 
      }
      continue;
    }
    if(*p == CR || *p == LF) {
      *p = 0;
      return i;
    }
    
    p++;
    i++;
    if (i>sizeof(cmd_str))
      break;
  }
  
  return 0;
}
Ejemplo n.º 5
0
int stdio_break_in(void)
{
    uint8_t c;
    int i, j;
    
    for(i=0, j=0;i<10;i++) {
      if (kNoErr != MicoUartRecv( STDIO_UART, &c, 1, 10)) 
        continue;

      if (c == 0x20) {
        j++;
        if (j > 5)
          return 1; 
      } else {
        j = 0;
      }
    }

    return 0;
}
Ejemplo n.º 6
0
/**
  * @brief  Upload a file via serial port.
  * @param  None
  * @retval None
  */
void SerialUpload(mico_flash_t flash, uint32_t flashdestination, char *fileName, int32_t maxRecvSize)
{
  uint8_t status = 0;
  uint8_t key;

  printf("Select Receive File\n\r");
  MicoUartRecv( STDIO_UART, &key, 1, MICO_NEVER_TIMEOUT );

  if (key == CRC16)
  {
    /* Transmit the flash image through ymodem protocol */
    status = Ymodem_Transmit(flash, flashdestination, (uint8_t *)fileName, maxRecvSize);

    if (status != 0)
    {
      printf("\n\rError Occurred while Transmitting File\n\r");
    }
    else
    {
      printf("\n\rFile uploaded successfully \n\r");
    }
  }
}
Ejemplo n.º 7
0
void getline (char *line, int n)  {
  int  cnt = 0;
  char c;

  do  {
    MicoUartRecv( STDIO_UART, &c, 1, MICO_NEVER_TIMEOUT );
    if (c == CR)  c = LF;     /* read character                 */
    if (c == BACKSPACE  ||  c == DEL)  {    /* process backspace              */
      if (cnt != 0)  {
        cnt--;                              /* decrement count                */
        line--;                             /* and line pointer               */
        putchar (BACKSPACE);                /* echo backspace                 */
        putchar (' ');
        putchar (BACKSPACE);
      }
    }
    else if (c != CNTLQ && c != CNTLS)  {   /* ignore Control S/Q             */
      putchar (*line = c);                  /* echo and store character       */
      line++;                               /* increment line pointer         */
      cnt++;                                /* and count                      */
    }
  }  while (cnt < n - 1  &&  c != LF);      /* check limit and line feed      */
  *(line - 1) = 0;                          /* mark end of string             */
}
Ejemplo n.º 8
0
//==================================
static int file_send( lua_State* L )
{
  int8_t res = 0;
  int8_t newname = 0;
  uint8_t c;
  spiffs_stat s;
  const char *fname;
  const char *newfname;
  size_t len;
  char buff[LUAL_BUFFERSIZE];

  fname = luaL_checklstring( L, 1, &len );
  
  if( len > SPIFFS_OBJ_NAME_LEN )
    return luaL_error(L, "filename too long");
  
  if(FILE_NOT_OPENED!=file_fd){
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }
  
  if (lua_gettop(L) == 2 && lua_type( L, 2 ) == LUA_TSTRING) {
    size_t len;
    newfname = luaL_checklstring( L, 2, &len );
    newname = 1;
  }

  // Open the file
  file_fd = SPIFFS_open(&fs,(char*)fname,mode2flag("r"),0);
  if(file_fd < FILE_NOT_OPENED){
    file_fd = FILE_NOT_OPENED;
    l_message(NULL,"Error opening file.");
    return 0;
  }

  // Get file size
  SPIFFS_fstat(&fs, file_fd, &s);
  if (newname == 1) {
    sprintf(buff,"sending \"%s\" as \"%s\"\r\n", fname, newfname);
    l_message(NULL,buff);
    fname = newfname;
  }
  
  l_message(NULL,"Start Ymodem file transfer...");

  while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {}
  
  res = Ymodem_Transmit(fname, s.size);
  
  luaWdgReload();
  mico_thread_msleep(500);
  while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {}

  if(FILE_NOT_OPENED!=file_fd){
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }

  if (res == 0) {
    l_message(NULL,"\r\nFile sent successfuly.");
  }
  else if (res == 99) {
    l_message(NULL,"\r\nNo response.");
  }
  else if (res == 98) {
    l_message(NULL,"\r\nAborted.");
  }
  else {
    l_message(NULL,"\r\nError sending file.");
  }
  return 0;
}
Ejemplo n.º 9
0
//==================================
static int file_recv( lua_State* L )
{
  int32_t fsize = 0;
  uint8_t c, gnm;
  char fnm[SPIFFS_OBJ_NAME_LEN];
  char buff[LUAL_BUFFERSIZE];
  spiffs_DIR d;
  struct spiffs_dirent e;
  struct spiffs_dirent *pe = &e;
  uint32_t total, used;

  SPIFFS_info(&fs, &total, &used);
  if(total>2000000 || used>2000000 || used > total)
  {
    return luaL_error(L, "file system error");;
  }

  gnm = 0;
  if (lua_gettop(L) == 1 && lua_type( L, 1 ) == LUA_TSTRING) {
    size_t len;
    const char *fname = luaL_checklstring( L, 1, &len );
    if (len > 0 && len < SPIFFS_OBJ_NAME_LEN) {
      // use given file name
      for (c=0; c<len; c++) {
        fnm[c] = fname[c];
      }
      fnm[len] = '\0';
      gnm = 1;
    }
  }

  if (FILE_NOT_OPENED != file_fd) {
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }

  l_message(NULL,"Start Ymodem file transfer...");

  while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {}
  
  fsize = Ymodem_Receive(fnm, total-used-10000, gnm);
  
  luaWdgReload();
  mico_thread_msleep(500);
  while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {}

  if (FILE_NOT_OPENED != file_fd) {
    SPIFFS_fflush(&fs,file_fd);
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }

  mico_thread_msleep(500);
  if (fsize > 0)
  {
    sprintf(buff,"\r\nReceived successfully, %d\r\n",fsize);
    l_message(NULL,buff);
  }
  else if (fsize == -1)
  {
    l_message(NULL,"\r\nFile write error!\r\n");
  }
  else if (fsize == -2)
  {
    l_message(NULL,"\r\nFile open error!\r\n");
  }
  else if (fsize == -3)
  {
    l_message(NULL,"\r\nAborted.\r\n");
  }
  else if (fsize == -4)
  {
    l_message(NULL,"\r\nFile size too big, aborted.\r\n");
  }
  else
  {
    l_message(NULL,"\r\nReceive failed!");
  }
  
  if (fsize > 0) {
    SPIFFS_opendir(&fs, "/", &d);
    while ((pe = SPIFFS_readdir(&d, pe))) {
      sprintf(buff," %-32s size: %i", pe->name, pe->size);
      l_message(NULL,buff);
    }
    SPIFFS_closedir(&d);
  }

  return 0;
}
Ejemplo n.º 10
0
//--------------------------------------------------------------------------
static uint8_t Ymodem_Transmit (const char* sendFileName, uint32_t sizeFile)
{
  uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD];
  uint8_t filename[SPIFFS_OBJ_NAME_LEN];
  uint16_t blkNumber;
  uint8_t receivedC[1], i, err;
  uint32_t size = 0;

  for (i = 0; i < (SPIFFS_OBJ_NAME_LEN - 1); i++)
  {
    filename[i] = sendFileName[i];
  }
    
  while (MicoUartRecv( MICO_UART_1, &receivedC[0], 1, 10 ) == kNoErr) {};

  // Wait for response from receiver
  err = 0;
  do {
    luaWdgReload();
    Send_Byte(CRC16);
  } while (Receive_Byte(&receivedC[0], NAK_TIMEOUT) < 0 && err++ < 45);
  if (err >= 45 || receivedC[0] != CRC16) {
    send_CA();
    return 99;
  }
  
  // === Prepare first block and send it =======================================
  /* When the receiving program receives this block and successfully
   * opened the output file, it shall acknowledge this block with an ACK
   * character and then proceed with a normal YMODEM file transfer
   * beginning with a "C" or NAK tranmsitted by the receiver.
   */
  Ymodem_PrepareIntialPacket(&packet_data[0], filename, &sizeFile);
  do 
  {
    // Send Packet
    Ymodem_SendPacket(packet_data, PACKET_SIZE + PACKET_OVERHEAD);
    // Wait for Ack
    err = Ymodem_WaitACK(ACK, 10);
    if (err == 0 || err == 4) {
      send_CA();
      return 90;                  // timeout or wrong response
    }
    else if (err == 2) return 98; // abort
  }while (err != 1);

  // After initial block the receiver sends 'C' after ACK
  if (Ymodem_WaitACK(CRC16, 10) != 1) {
    send_CA();
    return 90;
  }
  
  // === Send file blocks ======================================================
  size = sizeFile;
  blkNumber = 0x01;
  
  // Resend packet if NAK  for a count of 10 else end of communication
  while (size)
  {
    // Prepare and send next packet
    Ymodem_PreparePacket(&packet_data[0], blkNumber, size);
    do
    {
      Ymodem_SendPacket(packet_data, PACKET_1K_SIZE + PACKET_OVERHEAD);
      // Wait for Ack
      err = Ymodem_WaitACK(ACK, 10);
      if (err == 1) {
        blkNumber++;
        if (size > PACKET_1K_SIZE) size -= PACKET_1K_SIZE; // Next packet
        else size = 0; // Last packet sent
      }
      else if (err == 0 || err == 4) {
        send_CA();
        return 90;                  // timeout or wrong response
      }
      else if (err == 2) return 98; // abort
    }while(err != 1);
  }
  
  // === Send EOT ==============================================================
  Send_Byte(EOT); // Send (EOT)
  // Wait for Ack
  do 
  {
    // Wait for Ack
    err = Ymodem_WaitACK(ACK, 10);
    if (err == 3) {   // NAK
      Send_Byte(EOT); // Send (EOT)
    }
    else if (err == 0 || err == 4) {
      send_CA();
      return 90;                  // timeout or wrong response
    }
    else if (err == 2) return 98; // abort
  }while (err != 1);
  
  // === Receiver requests next file, prepare and send last packet =============
  if (Ymodem_WaitACK(CRC16, 10) != 1) {
    send_CA();
    return 90;
  }

  Ymodem_PrepareLastPacket(&packet_data[0]);
  do 
  {
    Ymodem_SendPacket(packet_data, PACKET_SIZE + PACKET_OVERHEAD); // Send Packet
    // Wait for Ack
    err = Ymodem_WaitACK(ACK, 10);
    if (err == 0 || err == 4) {
      send_CA();
      return 90;                  // timeout or wrong response
    }
    else if (err == 2) return 98; // abort
  }while (err != 1);
  
  return 0; // file transmitted successfully
}