Marco Hsu / WIFI_API_32kRAM
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers simple_uart.c Source File

simple_uart.c

00001 /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
00002  *
00003  * The information contained herein is property of Nordic Semiconductor ASA.
00004  * Terms and conditions of usage are described in detail in NORDIC
00005  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
00006  *
00007  * Licensees are granted free, non-transferable use of the information. NO
00008  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
00009  * the file.
00010  *
00011  */
00012 
00013 #include <stdint.h>
00014 
00015 //#include "nrf.h"  //command by Tsungta @12/12 when trying to porting on mbed online compilier
00016 #include "simple_uart.h"
00017 //#include "nrf_delay.h"
00018 #include "nrf_gpio.h"
00019 
00020 extern uint8_t rx_isr;
00021 extern uint8_t hif;
00022 extern uint8_t udp_client_test;
00023 uint8_t simple_uart_get(void)
00024 {
00025   while (NRF_UART0->EVENTS_RXDRDY != 1 && rx_isr == 0 && hif == 0 && udp_client_test == 0)
00026   {
00027     // Wait for RXD data to be received
00028   }
00029   
00030   NRF_UART0->EVENTS_RXDRDY = 0;
00031   return (uint8_t)NRF_UART0->RXD;
00032 }
00033 
00034 //bool simple_uart_get_with_timeout(int32_t timeout_ms, uint8_t *rx_data)
00035 //{
00036 //  bool ret = true;
00037 //  
00038 //  while (NRF_UART0->EVENTS_RXDRDY != 1)
00039 //  {
00040 //    if (timeout_ms-- >= 0)
00041 //    {
00042 //      // wait in 1ms chunk before checking for status
00043 //      nrf_delay_us(1000);
00044 //    }
00045 //    else
00046 //    {
00047 //      ret = false;
00048 //      break;
00049 //    }
00050 //  }  // Wait for RXD data to be received
00051 //
00052 //  if (timeout_ms >= 0)
00053 //  {
00054 //    // clear the event and set rx_data with received byte
00055 //      NRF_UART0->EVENTS_RXDRDY = 0;
00056 //      *rx_data = (uint8_t)NRF_UART0->RXD;
00057 //  }
00058 //
00059 //  return ret;
00060 //}
00061 
00062 void simple_uart_put(uint8_t cr)
00063 {
00064   NRF_UART0->TXD = (uint8_t)cr;
00065 
00066   while (NRF_UART0->EVENTS_TXDRDY!=1)
00067   {
00068     // Wait for TXD data to be sent
00069   }
00070 
00071   NRF_UART0->EVENTS_TXDRDY=0;
00072 }
00073 
00074 void simple_uart_putstring(const uint8_t *str)
00075 {
00076   uint_fast8_t i = 0;
00077   uint8_t ch = str[i++];
00078   while (ch != '\0')
00079   {
00080     simple_uart_put(ch);
00081     ch = str[i++];
00082   }
00083 }
00084 
00085 void simple_uart_config(  uint8_t rts_pin_number,
00086                           uint8_t txd_pin_number,
00087                           uint8_t cts_pin_number,
00088                           uint8_t rxd_pin_number,
00089                           bool hwfc)
00090 {
00091   nrf_gpio_cfg_output(txd_pin_number);
00092   nrf_gpio_cfg_input(rxd_pin_number, NRF_GPIO_PIN_NOPULL);  
00093 
00094   NRF_UART0->PSELTXD = txd_pin_number;
00095   NRF_UART0->PSELRXD = rxd_pin_number;
00096 
00097   if (hwfc)
00098   {
00099     nrf_gpio_cfg_output(rts_pin_number);
00100     nrf_gpio_cfg_input(cts_pin_number, NRF_GPIO_PIN_NOPULL);
00101     NRF_UART0->PSELCTS = cts_pin_number;
00102     NRF_UART0->PSELRTS = rts_pin_number;
00103     NRF_UART0->CONFIG  = (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos);
00104   }
00105 
00106 //  NRF_UART0->BAUDRATE         = (UART_BAUDRATE_BAUDRATE_Baud38400 << UART_BAUDRATE_BAUDRATE_Pos);
00107   NRF_UART0->BAUDRATE         = (UART_BAUDRATE_BAUDRATE_Baud115200 << UART_BAUDRATE_BAUDRATE_Pos);
00108   NRF_UART0->ENABLE           = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
00109   NRF_UART0->TASKS_STARTTX    = 1;
00110   NRF_UART0->TASKS_STARTRX    = 1;
00111   NRF_UART0->EVENTS_RXDRDY    = 0;
00112 }