Advertisement
AtomSoft

XC32 Fubarino UART

Apr 15th, 2014
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <xc.h>
  4. #include <plib.h>
  5.  
  6. #include "fubmini.h"
  7.  
  8.  
  9. #define SYS_FREQ    (48000000L)
  10. #define GetPeripheralClock()        (SYS_FREQ /(1 << OSCCONbits.PBDIV))
  11. typedef unsigned int UINT;
  12.  
  13. //CONFIGS
  14. #pragma config PMDL1WAY = OFF           // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
  15. #pragma config IOL1WAY = OFF            // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
  16. #pragma config FUSBIDIO = OFF           // USB USID Selection (Controlled by Port Function)
  17. #pragma config FVBUSONIO = OFF          // USB VBUS ON Selection (Controlled by Port Function)
  18. #pragma config FPLLIDIV = DIV_2         // PLL Input Divider (2x Divider)
  19. #pragma config FPLLMUL = MUL_24         // PLL Multiplier (24x Multiplier)
  20. #pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider (2x Divider)
  21. #pragma config UPLLEN = ON              // USB PLL Enable (Enabled)
  22. #pragma config FPLLODIV = DIV_2         // System PLL Output Clock Divider (PLL Divide by 2)
  23. #pragma config FNOSC = PRIPLL           // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
  24. #pragma config FSOSCEN = OFF            // Secondary Oscillator Enable (Disabled)
  25. #pragma config IESO = OFF               // Internal/External Switch Over (Disabled)
  26. #pragma config POSCMOD = XT             // Primary Oscillator Configuration (XT osc mode)
  27. #pragma config OSCIOFNC = OFF           // CLKO Output Signal Active on the OSCO Pin (Disabled)
  28. #pragma config FPBDIV = DIV_1           // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
  29. #pragma config FCKSM = CSECME           // Clock Switching and Monitor Selection (Clock Switch Enable, FSCM Enabled)
  30. #pragma config WDTPS = PS1024           // Watchdog Timer Postscaler (1:1024)
  31. #pragma config WINDIS = OFF             // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
  32. #pragma config FWDTEN = OFF             // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
  33. #pragma config FWDTWINSZ = WISZ_25      // Watchdog Timer Window Size (Window Size is 25%)
  34. #pragma config JTAGEN = OFF             // JTAG Enable (JTAG Disabled)
  35. #pragma config ICESEL = ICS_PGx1        // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
  36. #pragma config PWP = OFF                // Program Flash Write Protect (Disable)
  37. #pragma config BWP = OFF                // Boot Flash Write Protect bit (Protection Disabled)
  38. #pragma config CP = OFF                 // Code Protect (Protection Disabled)
  39.  
  40. #define ALL_BITS 0xFFFFFFFF
  41.  
  42. void init(void);
  43. void configIO(void);
  44.  
  45. //----------------
  46. BYTE UART1_RX(void);
  47. void UART1_TX(BYTE data);
  48. void UART1Init(void);
  49. void UART1ConfigPins(void);
  50. void UART1_RX_STR(char *buff, int len, char end);
  51. void UART1_RX_BUFF(char *buff, int len);
  52. void UART1_TX_STRING(char *string);
  53. void UART1_TX_BUFF(char *string, int len);
  54. //----------------
  55. char inBuff[128];
  56. char outBuff[128];
  57.  
  58. void delay()
  59. {
  60.     unsigned int tt;
  61.     tt = 10;
  62.     while(tt--)
  63.         Nop();
  64. }
  65.  
  66. void main ()
  67. {
  68.  
  69.     init();
  70.     configIO();
  71.     UART1Init();
  72.     UART1ConfigPins();
  73.  
  74.     sprintf(outBuff, "Hello!!\r\n\0");
  75.     UART1_TX_STRING(outBuff);
  76.  
  77.     while(1)
  78.     {
  79.         UART1_RX_STR(inBuff,0,1);
  80.         sprintf(outBuff, "You Wrote: %s\r\n\0", inBuff);
  81.         UART1_TX_STRING(outBuff);
  82.     }
  83. }
  84.  
  85.  
  86. void configIO(void)
  87. {
  88.     //ALL INPUT TO AVOID ISSUES
  89.     PORTSetPinsDigitalIn(IOPORT_A, ALL_BITS);
  90.     PORTSetPinsDigitalIn(IOPORT_B, ALL_BITS);
  91.     PORTSetPinsDigitalIn(IOPORT_C, ALL_BITS);
  92. }
  93.  
  94. void init(void)
  95. {
  96.     SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL);
  97.     OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_24,OSC_PLL_POST_2,0);
  98.     mOSCSetPBDIV( OSC_PB_DIV_1 );    // Configure the PB bus to run at 1/4 the CPU frequency
  99. }
  100.  
  101. void UART1ConfigPins(void)
  102. {
  103.     SYSKEY = 0x0; // Make sure SYSKEY is locked
  104.     SYSKEY = 0xAA996655; // SYSKEY unlock sequence
  105.     SYSKEY = 0x556699AA;
  106.  
  107.     PPSInput(3,U1RX,RPB2); //RX - P9
  108.     PPSOutput(1,RPC0,U1TX); //TX - P11
  109.  
  110.     SYSKEY = 0x0; // Relock SYSKEY
  111.  
  112.     PORTSetPinsDigitalIn(FUB_P9);
  113.     PORTSetPinsDigitalOut(FUB_P11);
  114. }
  115.  
  116. void UART1Init(void)
  117. {
  118.     UARTConfigure(UART1 , UART_ENABLE_PINS_TX_RX_ONLY);
  119.     UARTSetFifoMode(UART1 , 0);//UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
  120.     UARTSetLineControl(UART1 , UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
  121.     UARTSetDataRate(UART1 , GetPeripheralClock(), 57600);
  122.     UARTEnable(UART1 , UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
  123. }
  124.  
  125. void UART1_TX(BYTE data)
  126. {
  127.     while(UARTTransmitterIsReady(UART1)==0);
  128.  
  129.     UARTSendDataByte(UART1, data);
  130. }
  131.  
  132. BYTE UART1_RX(void)
  133. {
  134.     BYTE character;
  135.  
  136.     while(UARTReceivedDataIsAvailable(UART1)==0);
  137.     character = UARTGetDataByte(UART1);
  138.    
  139.     return character;
  140. }
  141.  
  142. void UART1_TX_BUFF(char *string, int len)
  143. {
  144.     char inTemp;
  145.  
  146.     while(len--)
  147.     {
  148.         UART1_TX(*string++);
  149.     }
  150. }
  151.  
  152. void UART1_TX_STRING(char *string)
  153. {
  154.     while(*string)
  155.     {
  156.         UART1_TX(*string++);
  157.     }
  158. }
  159.  
  160. void UART1_RX_BUFF(char *buff, int len)
  161. {
  162.     while(len--)
  163.     {
  164.         *buff++ = UART1_RX();
  165.     }
  166. }
  167.  
  168. void UART1_RX_STR(char *buff, int len, char end)
  169. {
  170.     char inTemp;
  171.     if(!end)
  172.     {
  173.         if(!len)
  174.         {
  175.             do
  176.             {
  177.                 inTemp = UART1_RX();
  178.                 *buff++ = inTemp;
  179.             }while(inTemp != 0);
  180.         } else {
  181.             while(len--)
  182.                 *buff++ = UART1_RX();
  183.         }
  184.     } else {
  185.         while(inTemp != '\n')
  186.         {
  187.             inTemp = UART1_RX();
  188.             if(inTemp != '\n')
  189.                 *buff++ = inTemp;
  190.             else
  191.                 *buff++ = 0;
  192.         }
  193.     }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement