init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NRFuart.cpp Source File

NRFuart.cpp

00001 #include "NRFuart.h"
00002 
00003 void NRFuart_init_nohwfc() {
00004     if(NRF_UART0->ENABLE == UART_ENABLE_ENABLE_Disabled) {
00005         nrf_gpio_cfg_output(PN_UART_TX);
00006         nrf_gpio_cfg_input(PN_UART_RX, NRF_GPIO_PIN_NOPULL);
00007         NRF_UART0->PSELTXD = PN_UART_TX;
00008         NRF_UART0->PSELRXD = PN_UART_RX;
00009         NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Disabled;
00010         NRF_UART0->BAUDRATE = NRF_UART_BAUDRATE_115200;
00011         NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled;
00012         NRF_UART0->EVENTS_RXDRDY = 0;
00013         NRF_UART0->EVENTS_TXDRDY = 0;
00014         NRF_UART0->EVENTS_ERROR  = 0;
00015         NRF_UART0->EVENTS_TXDRDY = 0;
00016         NRF_UART0->TASKS_STARTRX = 1;
00017         NRF_UART0->TASKS_STARTTX = 1;
00018         //NRF_UART0->INTENCLR = 0xffffffffUL;
00019         //NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Msk; //or
00020         //NRF_UART0->INTENSET = (UART_INTENSET_RXDRDY_Set << UART_INTENSET_RXDRDY_Pos) |
00021         //                      (UART_INTENSET_TXDRDY_Set << UART_INTENSET_TXDRDY_Pos) |
00022         //                      (UART_INTENSET_ERROR_Set << UART_INTENSET_ERROR_Pos); 
00023         //NVIC_ClearPendingIRQ(UART0_IRQn);
00024         //NVIC_SetPriority(UART0_IRQn, 1); //3
00025         //NVIC_EnableIRQ(UART0_IRQn);
00026         //NVIC_SetVector(UART0_IRQn, (uint32_t) UART0_IRQHandler);
00027     }
00028 };
00029 void NRFuart_init_hwfc() {
00030     if(NRF_UART0->ENABLE == UART_ENABLE_ENABLE_Disabled) {
00031         nrf_gpio_cfg_output(PN_UART_TX);
00032         nrf_gpio_cfg_input(PN_UART_RX, NRF_GPIO_PIN_NOPULL);
00033         NRF_UART0->PSELTXD = PN_UART_TX;
00034         NRF_UART0->PSELRXD = PN_UART_RX;
00035         NRF_UART0->PSELCTS = PN_UART_CTS;
00036         NRF_UART0->PSELRTS = PN_UART_RTS;
00037         NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Enabled;
00038         NRF_UART0->BAUDRATE = NRF_UART_BAUDRATE_115200;
00039         NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled;
00040         NRF_UART0->EVENTS_RXDRDY = 0;
00041         NRF_UART0->EVENTS_TXDRDY = 0;
00042         NRF_UART0->EVENTS_ERROR  = 0;
00043         NRF_UART0->EVENTS_TXDRDY = 0;
00044         NRF_UART0->TASKS_STARTRX = 1;
00045         NRF_UART0->TASKS_STARTTX = 1;
00046         //NRF_UART0->INTENCLR = 0xffffffffUL;
00047         //NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Msk; //or
00048         //NRF_UART0->INTENSET = (UART_INTENSET_RXDRDY_Set << UART_INTENSET_RXDRDY_Pos) |
00049         //                      (UART_INTENSET_TXDRDY_Set << UART_INTENSET_TXDRDY_Pos) |
00050         //                      (UART_INTENSET_ERROR_Set << UART_INTENSET_ERROR_Pos);   
00051         //NVIC_ClearPendingIRQ(UART0_IRQn);
00052         //NVIC_SetPriority(UART0_IRQn, 1); //3
00053         //NVIC_EnableIRQ(UART0_IRQn);
00054         //NVIC_SetVector(UART0_IRQn, (uint32_t) UART0_IRQHandler);
00055     }
00056 };
00057 void NRFuart_uninit() {
00058     if (NRF_UART0->ENABLE == UART_ENABLE_ENABLE_Enabled) {
00059         NVIC_DisableIRQ(UART0_IRQn);
00060         NRF_UART0->INTENCLR = 0xffffffffUL;
00061         NRF_UART0->TASKS_STOPRX = 1;
00062         NRF_UART0->TASKS_STOPTX = 1;
00063         NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Disabled;
00064         NRF_UART0->PSELTXD = 0xFFFFFFFF;
00065         NRF_UART0->PSELRXD = 0xFFFFFFFF;
00066         NRF_UART0->PSELRTS = 0xFFFFFFFF;
00067         NRF_UART0->PSELCTS = 0xFFFFFFFF;
00068     }
00069 };
00070 void NRFuart_putc(char byte) {
00071     if (!NRF_UART0->ENABLE) NRFuart_init_nohwfc();
00072     NRF_UART0->TXD = byte;
00073     uint32_t safetycounter = 0;
00074     while(NRF_UART0->EVENTS_TXDRDY != 1 && safetycounter < 10000) {
00075         safetycounter ++; // Wait for the current TXD data to be sent.
00076     }
00077     NRF_UART0->EVENTS_TXDRDY = 0;
00078 };
00079 void NRFuart_puts(char* bytes) {
00080     if (!NRF_UART0->ENABLE) NRFuart_init_nohwfc();
00081     uint32_t safetycounter = 0;
00082     for(int i = 0; bytes[i] != 0x00; i++) {
00083         NRFuart_putc(bytes[i]);
00084         safetycounter ++;
00085         if (safetycounter > 10000) break;   
00086     }
00087 };
00088 void NRFuart_puts_debug(char* bytes) {
00089     if (!NRF_UART0->ENABLE) NRFuart_init_nohwfc();
00090     uint32_t safetycounter = 0;
00091     for(int i = 0; bytes[i] != 0x00; i++) {
00092         NRFuart_putc(bytes[i]);
00093         safetycounter ++;
00094         if (safetycounter > 10000) break; 
00095     }
00096     NRFuart_putc('\n');
00097 };
00098 void debug_prep(){
00099     memset(GLOBAL_debug_buffer, 0x00, sizeof(GLOBAL_debug_buffer));   
00100 }
00101 void debug_exe(){
00102     NRFuart_puts_debug(GLOBAL_debug_buffer);
00103 }
00104 char NRFuart_getc() {
00105     if (!NRF_UART0->ENABLE) NRFuart_init_nohwfc();
00106     uint32_t safetycounter = 0;
00107     while(NRF_UART0->EVENTS_RXDRDY != 1 && safetycounter < 10000){
00108         safetycounter ++;
00109     }
00110     NRF_UART0->EVENTS_RXDRDY = 0;
00111     return (uint8_t)NRF_UART0->RXD;
00112 };
00113 char* NRFuart_gets(char terminator) {
00114     if (!NRF_UART0->ENABLE) NRFuart_init_nohwfc();
00115     static char buffer[200];
00116     int charindex = 0;
00117     memset(buffer,0x00,sizeof(buffer));
00118     uint32_t safetycounter = 0;
00119     while(1 && safetycounter < 10000) {
00120         if (NRF_UART0->EVENTS_RXDRDY == 0) {
00121             safetycounter ++;
00122             //Nothing available from the UART.
00123             continue;
00124         } else {
00125             char inbyte = NRFuart_getc();
00126             if (inbyte == terminator) {
00127                 break;
00128             } else {
00129                 buffer[charindex] = inbyte;
00130                 charindex++;
00131             }
00132         }
00133     }
00134     buffer[charindex] = '\n'; //make sure we end with whitespace lf
00135     return buffer;
00136 };
00137 void NRFuart_flush() {
00138     if (!NRF_UART0->ENABLE) NRFuart_init_nohwfc();
00139     uint32_t safetycounter = 0;
00140     while (NRFuart_readable() && safetycounter < 10000) {
00141         safetycounter ++;
00142         char char1 = NRFuart_getc();
00143     }
00144 };
00145 bool NRFuart_readable() {
00146     if (!NRF_UART0->ENABLE) NRFuart_init_nohwfc();
00147     return (NRF_UART0->EVENTS_RXDRDY == 1);
00148 };