Marek Belisko

FRDM-KL03Z LPUART problem receiving multiple bytes blocking

Discussion created by Marek Belisko on Sep 7, 2017
Latest reply on Sep 10, 2017 by Jing Pan

Hi,

I'm using FRDM-KL03Z dev. board which has attached sigfox controller (send AT command reply with OK or value or ERROR). I used some lpuart example which can be found in MCUXpresso IDE but example was with receiving 1 char only. I connected LP uart (which is debug console by unsoldering 2 resistors) and I can send AT command but have hard times to receive multiple chars from sigfox device. Code example:

 

 

lpuart_config_t config;

   CLOCK_SetLpuart0Clock(1U);
  /*
   * config.baudRate_Bps = 38400U;
   * config.parityMode = kLPUART_ParityDisabled;
   * config.stopBitCount = kLPUART_OneStopBit;
   * config.txFifoWatermark = 0;
   * config.rxFifoWatermark = 0;
   * config.enableTx = false;
   * config.enableRx = false;
   */

  LPUART_GetDefaultConfig(&config);
  config.baudRate_Bps = 9600;
  config.enableTx = true;
  config.enableRx = true;
  LPUART_Init(DEMO_LPUART, &config, CLOCK_GetFreq(DEMO_LPUART_CLKSRC));

if (writable(DEMO_LPUART)) {
         LPUART_WriteBlocking(DEMO_LPUART, "AT$I=10\n\r", 9);

         status_t status;

         while(true) {
         if (readable(DEMO_LPUART)) {
               status = LPUART_ReadBlocking(DEMO_LPUART, buf, sizeof(buf));
               if (status == kStatus_Success)
                    break;

          }
          }

         if (buf[0] == 'O')
              GPIO_TogglePinsOutput(BOARD_LED_GPIO, 1U << BOARD_LED_GPIO_PIN);

        }

 

 

with this code pushed to main.c I can receive chars from sigfox module (but strange for me is why I need to use while loop) as internally LPUART_ReadBlocking looping for whole buffer. Also when move code to different file and create wrapper I cannot receive anything (it is looping forever in receive routine).

 

/*
* sigfox.c
*
*  Created on: Sep 4, 2017
*      Author: marekbelisko
*/
#include "sigfox.h"

#define SIGFOX_STATUS "AT\n\r"
#define SIGFOX_DEVICE_ID "AT$I=10\n\r"
#define SIGFOX_PAC "AT$I=11\n\r"
#define SIGFOX_VOLTAGE "AT$V?\n\r"
#define SIGFOX_TEMPERATURE "AT$T?\n\r"

// global UART handle
static LPUART_Type *g_uart = NULL;
static uint8_t g_buf[20] = {0};

static bool writable(LPUART_Type *base) {
  return (LPUART_GetStatusFlags(base)&LPUART_STAT_TDRE_MASK)!=0;
}

static bool readable(LPUART_Type *base) {
  return (LPUART_GetStatusFlags(base)&LPUART_STAT_RDRF_MASK)!=0;
}

void setUart(LPUART_Type *uart)
{
     g_uart = uart;
}

int sendMessage(const char *buf)
{
     uint8_t *msg = NULL;
     msg = sendReceiveMessage(buf);

     return ((msg != NULL) && (msg[0] == 'O' && msg[1] == 'K')) ? 1 : 0;
}

uint8_t * sendReceiveMessage(const char *data)
{
     if (g_uart == NULL)
          return NULL;

     if (writable(g_uart)) {
          LPUART_WriteBlocking(g_uart, (uint8_t *)data, strlen(data));
          status_t status;

          while(true) {
                   if (readable(g_uart)) {
                         status = LPUART_ReadBlocking(g_uart, g_buf, sizeof(g_buf));
                         if (status == kStatus_Success)
                              break;

                    }
                    }

          /* this is not nice but I cannot figure out how to do it properly :)
          while (true) {
               if (readable(g_uart)) {
                    if (LPUART_ReadBlocking(g_uart, g_buf, sizeof(g_buf)) == kStatus_Success)
                         break;
                    }
               }*/
          }

     return g_buf;
}

bool sigfox_status() { return sendMessage(SIGFOX_STATUS); }
uint8_t *sigfox_device_id() { return sendReceiveMessage(SIGFOX_DEVICE_ID); }
uint8_t *sigfox_pac() { return sendReceiveMessage(SIGFOX_PAC); }
uint8_t *sigfox_voltage() { return sendReceiveMessage(SIGFOX_VOLTAGE); }
uint8_t *sigfox_temperature() { return sendReceiveMessage(SIGFOX_TEMPERATURE); }


 

Any ideas what I'm doing wrong? I'm fighting with this simple task for few days already . Thanks

Outcomes