Exemplo n.º 1
0
/**@brief   Function for handling app_uart events.
 *
 * @details This function will receive a single character from the app_uart module and append it to 
 *          a string. The string will be converted to Morse Code
 */
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[100];
    static uint8_t index = 0;
        
    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            if (data_array[index] == '\n')
            {
                printf("%s \n",data_array);
                printf("index = %d\n", index);
                morse_generate((char*)data_array, index);
            }
            index++;
            break;    
        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}
/**
 * UART event handler function.
 */
static void uart_event_handler(app_uart_evt_t * uart_event)
{
#ifdef USE_UART_LOGGING
  int status;

  switch(uart_event->evt_type)
  {
    case APP_UART_DATA_READY:
      do {
        uint8_t c;
        status = app_uart_get(&c); // Discard input
      } while(status == NRF_SUCCESS);
      break;
    case APP_UART_FIFO_ERROR:
      APP_ERROR_HANDLER(uart_event->data.error_code);
      break;
    case APP_UART_COMMUNICATION_ERROR:
      APP_ERROR_HANDLER(uart_event->data.error_communication);
      break;
    case APP_UART_DATA:
    default:
      break;
  }
#endif
}
Exemplo n.º 3
0
void uart_evt_handler(app_uart_evt_t *p_evt)
{
	uint8_t buff[24];
	uint32_t err = 0;
	uint16_t len;
    ble_gatts_hvx_params_t params;

	switch (p_evt->evt_type)
	{
		case APP_UART_TX_EMPTY:
			break;
		case APP_UART_DATA_READY:
			for (len = 0; len < 24 && err == 0; len++)
				err = app_uart_get(&buff[len]);

		    memset(&params, 0, sizeof(params));
		    params.type = BLE_GATT_HVX_NOTIFICATION;
		    params.handle = g_UartServ.data_char_handles.value_handle;
		    params.p_data = buff;
		    params.p_len = &len;

		    return sd_ble_gatts_hvx(g_UartServ.conn_handle, &params);

			//ble_uarts_on_data_change(buff, len);
			break;
		case APP_UART_DATA:
			break;
	}
}
Exemplo n.º 4
0
int fgetc(FILE * p_file)
{
    uint8_t input;
    while (app_uart_get(&input) == NRF_ERROR_NOT_FOUND)
    {
        // No implementation needed.
    }
    return input;
}
Exemplo n.º 5
0
int _read(int file, char * p_char, int len)
{
    UNUSED_PARAMETER(file);
    while (app_uart_get((uint8_t *)p_char) == NRF_ERROR_NOT_FOUND)
    {
        // No implementation needed.
    }

    return 1;
}
Exemplo n.º 6
0
/**@brief  Application main function.
 */
int main(void)
{
    
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint8_t newbyte;
    
    // Initialize
    leds_init();
    timers_init();
		gpiote_init();
    buttons_init();
    uart_init();
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
    sec_params_init();
    
    uart_putstring(START_STRING);
    
    advertising_start();
    
    // Enter main loop
    for (;;)
    { 
        /*Stop reading new data if there are no ble buffers available */
        if(ble_buffer_available)
        {
            if(app_uart_get(&newbyte) == NRF_SUCCESS)
            {
                data_array[index++] =  newbyte;
               
                if (index >= (BLE_NUS_MAX_DATA_LEN))
				{
                    ble_buffer_available=ble_attempt_to_send(&data_array[0],index);
                 	if(ble_buffer_available) index=0;
				}
            }
        }
        /* Re-transmission if ble_buffer_available was set to false*/
        if(tx_complete)
        {
            tx_complete=false;
            
            ble_buffer_available=ble_attempt_to_send(&data_array[0],index);
            if(ble_buffer_available) index =0;
        }

        power_manage();
    }
}
Exemplo n.º 7
0
/**
 * @brief Function for main application entry.
 */
int main(void)
{
    LEDS_CONFIGURE(LEDS_MASK);
    LEDS_OFF(LEDS_MASK);
    uint32_t err_code;
    const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          APP_UART_FLOW_CONTROL_ENABLED,
          false,
          UART_BAUDRATE_BAUDRATE_Baud115200
      };

    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_error_handle,
                         APP_IRQ_PRIORITY_LOW,
                         err_code);

    APP_ERROR_CHECK(err_code);

#ifndef ENABLE_LOOPBACK_TEST
    printf("\n\rStart: \n\r");

    while (true)
    {
        uint8_t cr;
        while(app_uart_get(&cr) != NRF_SUCCESS);
        while(app_uart_put(cr) != NRF_SUCCESS);

        if (cr == 'q' || cr == 'Q')
        {
            printf(" \n\rExit!\n\r");

            while (true)
            {
                // Do nothing.
            }
        }
    }
#else

    // This part of the example is just for testing the loopback .
    while (true)
    {
        uart_loopback_test();
    }
#endif
}
Exemplo n.º 8
0
int _read(int file, char *ptr, int len)
{
  int ret_len = len;
  uint8_t input;
  UNUSED_PARAMETER(file);

  while (len--)
  {
    UNUSED_VARIABLE(app_uart_get(&input));
    *ptr++ = input;
  }

  return ret_len;
}
Exemplo n.º 9
0
void uart0_event_handle(app_uart_evt_t * p_event) {
  if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR) {
    jshPushIOEvent(IOEVENTFLAGS_SERIAL_TO_SERIAL_STATUS(EV_SERIAL1) | EV_SERIAL_STATUS_FRAMING_ERR, 0);
  } else if (p_event->evt_type == APP_UART_TX_EMPTY) {
    int ch = jshGetCharToTransmit(EV_SERIAL1);
    if (ch >= 0) {
      uartIsSending = true;
      while (app_uart_put((uint8_t)ch) != NRF_SUCCESS);
    } else
      uartIsSending = false;
  } else if (p_event->evt_type == APP_UART_DATA) {
    uint8_t character;
    while (app_uart_get(&character) != NRF_SUCCESS);
    jshPushIOCharEvent(EV_SERIAL1, (char) character);
  }
}
Exemplo n.º 10
0
// UART callback function. Registered in uart_init(). Allows to asychronously read characters from UART.
void uart_event_handle(app_uart_evt_t * p_event)
{
  if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
  {
    APP_ERROR_HANDLER(p_event->data.error_communication);
  }
  else if (p_event->evt_type == APP_UART_FIFO_ERROR)
  {
    APP_ERROR_HANDLER(p_event->data.error_code);
  }
  else if (p_event->evt_type == APP_UART_DATA_READY) // There is one byte in the RX FIFO.
  {
    uint8_t character;
    while(app_uart_get(&character) != NRF_SUCCESS); // Non blocking.
    jshPushIOCharEvent(EV_SERIAL1, (char) character);
  }
}
Exemplo n.º 11
0
/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') || (index >= (m_ble_nus_max_data_len)))
            {
                NRF_LOG_DEBUG("Ready to send data over BLE NUS\r\n");
                NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                do
                {
                    err_code = ble_nus_string_send(&m_nus, data_array, index);
                    if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) )
                    {
                        APP_ERROR_CHECK(err_code);
                    }
                } while (err_code == NRF_ERROR_BUSY);

                index = 0;
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}
Exemplo n.º 12
0
void uart_process(void)
{
    uint8_t newbyte;
    uint8_t* params;
    uint8_t plen;

    while(app_uart_get(&newbyte) == NRF_SUCCESS)
    {
        if (m_index < BUFFER_LENGTH) {
            m_data_array[m_index++] =  newbyte;
        } else {
            memset(m_data_array, 0, BUFFER_LENGTH);
            m_index = 0;
        }

        if (m_data_array[0] != SERIAL_SYNC_BYTE) {
            memset(m_data_array, 0, BUFFER_LENGTH);
            m_index = 0;
            return;
        }

        if (m_index < SERIAL_HEADER_LENGTH) {
            // receiving frame header
            return;
        }

        plen = m_data_array[1];

        if (m_index < plen + SERIAL_HEADER_LENGTH) {
            // receiving frame body
            return;
        }

        params = m_data_array + SERIAL_HEADER_LENGTH;

        hif_on_message(params, plen);

        memset(m_data_array, 0, BUFFER_LENGTH);
        m_index = 0;
    }
}
Exemplo n.º 13
0
/** @brief Function for testing UART loop back. 
 *  @details Transmitts one character at a time to check if the data received from the loopback is same as the transmitted data.
 *  @note  @ref TX_PIN_NUMBER must be connected to @ref RX_PIN_NUMBER)
 */
static void uart_loopback_test()
{
    uint8_t * tx_data = (uint8_t *)("\n\rLOOPBACK_TEST\n\r");
    uint8_t   rx_data;

    // Start sending one byte and see if you get the same
    for (uint32_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
    {
        uint32_t err_code;
        while(app_uart_put(tx_data[i]) != NRF_SUCCESS);

        nrf_delay_ms(10);
        err_code = app_uart_get(&rx_data);

        if ((rx_data != tx_data[i]) || (err_code != NRF_SUCCESS))
        {
            show_error();
        }
    }
    return;
}
Exemplo n.º 14
0
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
//    uint32_t       err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
				    //DbgTOZ8(data_array,1);
				    while(app_uart_put(data_array[index]) != NRF_SUCCESS);
            /*index++;

            if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)))
            {
                err_code = ble_nus_string_send(&m_nus, data_array, index);
                if (err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
								//while(app_uart_put(p_data[i]) != NRF_SUCCESS);
                
                index = 0;
            }*/
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}
/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t index = 0;
    static uint8_t ch;

    switch (p_event->evt_type) {
    case APP_UART_DATA_READY:
        UNUSED_VARIABLE(app_uart_get(&ch));
        app_uart_put(ch);
        if (ch == '{') {
            index = 0;
            time_buffer[index++] = ch;
        }
        else if (ch == '}') {
            time_buffer[index] = '\0';
            index = 0;
            printf("\r\n* received string = [%s]\r\n", time_buffer + 1);
            change_rtc = true;
        }
        else if (index >= 128)
            index = 0;
        else
            time_buffer[index++] = ch;
        break;

    case APP_UART_COMMUNICATION_ERROR:
        APP_ERROR_HANDLER(p_event->data.error_communication);
        break;

    case APP_UART_FIFO_ERROR:
        APP_ERROR_HANDLER(p_event->data.error_code);
        break;

    default:
        break;
    }
}
Exemplo n.º 16
0
/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t err_code;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)))
            {
                err_code = ble_uart_c_write_string(&m_ble_uart_c, data_array, index);
                if (err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
                
                index = 0;
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
            }
}
Exemplo n.º 17
0
void bridge_uart_event_handler(app_uart_evt_t *p_app_uart_event)
{
    uint8_t uart_rx;
  
	  // Check if UART data has been received.
    if(p_app_uart_event->evt_type == APP_UART_DATA_READY)
    {
			  // Getting a byte from the UART.
        if(app_uart_get(&uart_rx) != NRF_SUCCESS)
        {
            return;
        }
        
        switch(bridge.rx.state) 
        {
            static uint8_t   payload_count;
            static uint16_t  crc16;
            
					  // Start of next packet detected.
            case BRIDGE_STATE_COMMAND_WAIT:
            {
                bridge_check_command_rcv((bridge_commands_t)uart_rx);
                break;
            }
            
						// Length byte received.
            case BRIDGE_STATE_LENGTH_WAIT:
            {
                bridge.rx.packet.payload_length = uart_rx;
                payload_count = 0;
                bridge.rx.state = BRIDGE_STATE_PAYLOAD_WAIT;
                break;
            }
            
						// Reading payload.
            case BRIDGE_STATE_PAYLOAD_WAIT:
            {
                
                bridge.rx.packet.payload[payload_count] = uart_rx;
                payload_count++;
                if(payload_count >= bridge.rx.packet.payload_length)
                {
                    bridge.rx.state = BRIDGE_STATE_CRC16_LOW_WAIT;
                }
                
                break;
            }
            
						// Get first crc16 byte.
            case BRIDGE_STATE_CRC16_LOW_WAIT:
            {
                crc16 = uart_rx;
                bridge.rx.state = BRIDGE_STATE_CRC16_HIGH_WAIT;
                break;
            }
            
						// Get second crc16 byte.
            case BRIDGE_STATE_CRC16_HIGH_WAIT:
            {  
                
							  // If device is not connected of notification/indication is not enabled send BRIDGE_COMM_NCONN byte.
                if( !ble_is_device_connected() ||
                    (
                        ((characteristic_sensorData_up_info.state & BLE_CHARACTERISTIC_IS_NOTIFYING)  == 0)&&
                        ((characteristic_sensorData_up_info.state & BLE_CHARACTERISTIC_IS_INDICATING) == 0)
                    ) 
                  )
                {
                    app_uart_put(BRIDGE_COMM_NCONN);
                }  
                else
                {
                    crc16 += ((uint16_t)uart_rx << 8);
                    crc16_1 = crc16_compute((uint8_t *)&bridge.rx.packet, bridge.rx.packet.payload_length + BRIDGE_HEDER_SIZE, NULL);
									  // Compare received crc16 with calculated value.
                    if(
                        (crc16 == crc16_compute((uint8_t *)&bridge.rx.packet, bridge.rx.packet.payload_length + BRIDGE_HEDER_SIZE, NULL)) &&
                        (bridge.rx.packet.payload_length <= BRIDGE_PAYLOAD_SIZE)
                      )
                    {
                        // Send data over ble and ACK over uart.
											  memset((uint8_t*)&sensor_bridge.data_up, 0, sizeof(sensor_bridge.data_up)); 
                        sensor_bridge.data_up.payload_length = bridge.rx.packet.payload_length;
                        memcpy((uint8_t*)&sensor_bridge.data_up.payload, (uint8_t*)&bridge.rx.packet.payload, bridge.rx.packet.payload_length); 
                        ble_update_characteristic_value(&characteristic_sensorData_up_info, (uint8_t *)(&sensor_bridge.data_up), sizeof(sensor_bridge.data_up));
                        app_uart_put(BRIDGE_COMM_ACK);
                    }
										// Send BRIDGE_COMM_NACK byte.
                    else
                    {
                        app_uart_put(BRIDGE_COMM_NACK);
                    }
                }
                
								// Go to BRIDGE_STATE_COMMAND_WAIT state (wait to next packet).
                bridge.rx.state = BRIDGE_STATE_COMMAND_WAIT;
                break;
            }
            
            // ACK wait state. 
            case BRIDGE_STATE_ACK_WAIT:
            {
							  // If ACK received reset resend counter and go to BRIDGE_STATE_COMMAND_WAIT state.
                if(uart_rx == BRIDGE_COMM_ACK)
                {
                    bridge.tx.resend_counter = 0;
                    bridge.rx.state = BRIDGE_STATE_COMMAND_WAIT;
                }
                
								// If NACK received?
                else if(uart_rx == BRIDGE_COMM_NACK)
                {
									  // If there is need to do more resends?
                    if(bridge.tx.resend_counter < NUMBER_OF_RESEND)
                    {
											  // Resend last packet, and increment resend_counter.
                        bridge_send_packet();
                        bridge.tx.resend_counter++;
                    }
                    else
                    {
											  // Reset resend counter and go to BRIDGE_STATE_COMMAND_WAIT state.
                        bridge.tx.resend_counter = 0;
                        bridge.rx.state = BRIDGE_STATE_COMMAND_WAIT;
                    }
                }
								
								// Can be considered to have received the beginning of a new package.
                else
                {
                    bridge_check_command_rcv((bridge_commands_t)uart_rx);
                }
                
                break;
            }
        }
    }
}
Exemplo n.º 18
0
int fgetc(FILE *f)
{
  uint8_t c;

  return ( NRF_SUCCESS == app_uart_get(&c) ) ? ((int) c) : 0;
}
Exemplo n.º 19
0
static void uart_event_handle(app_uart_evt_t * p_event)
{
  static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  static uint8_t index = 0;
  uint32_t       err_code;

  //uint8_t   dbg_idx = 0;
  //uint8_t   nDataLen = 0;

  switch (p_event->evt_type)
  {
      case APP_UART_DATA_READY:
          UNUSED_VARIABLE(app_uart_get(&data_array[index]));

          //log_print("0x%02x ", data_array[index]);

          index++;

          if ((data_array[index - 1] == 0x03/*'\n'*/) || (index >= (BLE_NUS_MAX_DATA_LEN)))
          {

              //err_code = ble_nus_string_send(&m_nus, data_array, index);
              err_code = ble_uart_send_data(data_array, index);
              if (err_code != NRF_ERROR_INVALID_STATE)
              {
                  APP_ERROR_CHECK(err_code);
              }

              index = 0;
              //nrf_delay_ms(10);
          }

          //nDataLen = sizeof(json);
#if 0
          do
            {
              if (nDataLen < BLE_NUS_MAX_DATA_LEN)
              {
                ble_uart_send_data((uint8_t *)json+dbg_idx, nDataLen);
                dbg_idx += nDataLen;
                nDataLen = 0;
              }
              else
                {
                ble_uart_send_data((uint8_t *)json+dbg_idx, BLE_NUS_MAX_DATA_LEN);
                dbg_idx += BLE_NUS_MAX_DATA_LEN;
                nDataLen -= BLE_NUS_MAX_DATA_LEN;
                }


               nrf_delay_ms(10);


            } while(nDataLen > 0);
#endif
          break;

      case APP_UART_COMMUNICATION_ERROR:
          APP_ERROR_HANDLER(p_event->data.error_communication);
          break;

      case APP_UART_FIFO_ERROR:
          APP_ERROR_HANDLER(p_event->data.error_code);
          break;

      default:
          break;
  }
}
Exemplo n.º 20
0
// currently only single receive character is implemented
uint8_t uart_receive_byte(void){
	uint8_t byte;
	app_uart_get(&byte);
	
	return byte;
}