]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - i3-mote/i3-mote.git/blobdiff - Basic-Test-Package/MSP432/Test_MSP432_3wSPI_Master_CC2650_Slave/uart_printf.c
MSP432 Test TI-RTOS
[i3-mote/i3-mote.git] / Basic-Test-Package / MSP432 / Test_MSP432_3wSPI_Master_CC2650_Slave / uart_printf.c
diff --git a/Basic-Test-Package/MSP432/Test_MSP432_3wSPI_Master_CC2650_Slave/uart_printf.c b/Basic-Test-Package/MSP432/Test_MSP432_3wSPI_Master_CC2650_Slave/uart_printf.c
new file mode 100644 (file)
index 0000000..03bf96c
--- /dev/null
@@ -0,0 +1,189 @@
+/**************************************************************************************************\r
+  Filename:       uart_printf.c\r
+\r
+  Description:    This file contains the TI-RTOS hooks for printing to UART via\r
+                  System_printf(..).\r
+\r
+                  This is a very basic implementation made for the purposes of\r
+                  terminal feedback in workshops, trainings and debug.\r
+\r
+  Copyright 2015 Texas Instruments Incorporated. All rights reserved.\r
+\r
+  IMPORTANT: Your use of this Software is limited to those specific rights\r
+  granted under the terms of a software license agreement between the user\r
+  who downloaded the software, his/her employer (which must be your employer)\r
+  and Texas Instruments Incorporated (the "License").  You may not use this\r
+  Software unless you agree to abide by the terms of the License. The License\r
+  limits your use, and you acknowledge, that the Software may not be modified,\r
+  copied or distributed unless embedded on a Texas Instruments microcontroller\r
+  or used solely and exclusively in conjunction with a Texas Instruments radio\r
+  frequency transceiver, which is integrated into your product.  Other than for\r
+  the foregoing purpose, you may not use, reproduce, copy, prepare derivative\r
+  works of, modify, distribute, perform, display or sell this Software and/or\r
+  its documentation for any purpose.\r
+\r
+  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE\r
+  PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,\r
+  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,\r
+  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL\r
+  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,\r
+  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER\r
+  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES\r
+  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE\r
+  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT\r
+  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES\r
+  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.\r
+\r
+  Should you have any questions regarding your right to use this Software,\r
+  contact Texas Instruments Incorporated at www.TI.com.\r
+**************************************************************************************************/\r
+\r
+/*********************************************************************\r
+ * INCLUDES\r
+ */\r
+#include <Board.h>\r
+#include <ti/drivers/UART.h>\r
+//#include <ti/drivers/uart/UARTCC26XX.h>\r
+#include <stdint.h>\r
+\r
+/*********************************************************************\r
+ * CONSTANTS\r
+ */\r
+#define UART_PRINTF_BUF_LEN      1024\r
+\r
+/*********************************************************************\r
+ * TYPEDEFS\r
+ */\r
+\r
+/*********************************************************************\r
+ * LOCAL VARIABLES\r
+ */\r
+static uint8_t  uartPrintf_outArray[UART_PRINTF_BUF_LEN];\r
+static uint16_t uartPrintf_head = 0;\r
+static uint16_t uartPrintf_tail = 0;\r
+static UART_Handle hUart = NULL;\r
+\r
+\r
+/*********************************************************************\r
+ * PUBLIC FUNCTIONS\r
+ */\r
+\r
+/*********************************************************************\r
+ * @fn      UartPrintf_init\r
+ *\r
+ * @brief   Initializes the putchar hooks with the handle to the UART.\r
+ *\r
+ * @param   handle - UART driver handle to an initialized and opened UART.\r
+ *\r
+ * @return  None.\r
+ */\r
+void UartPrintf_init(UART_Handle handle)\r
+{\r
+       hUart = handle;\r
+}\r
+\r
+/*********************************************************************\r
+ * SYSTEM HOOK FUNCTIONS\r
+ */\r
+\r
+/*********************************************************************\r
+ * @fn      uartPrintf_putch\r
+ *\r
+ * @brief   User supplied PutChar function.\r
+ *          typedef Void (*SysCallback_PutchFxn)(Char);\r
+ *\r
+ *          This function is called whenever the System module needs\r
+ *          to output a character.\r
+ *\r
+ *          This implementation fills a very basic ring-buffer, and relies\r
+ *          on another function to flush this buffer out to UART.\r
+ *\r
+ *          Requires SysCallback to be the system provider module.\r
+ *          Initialized via SysCallback.putchFxn = "&uartPrintf_putch"; in the\r
+ *          TI-RTOS configuration script.\r
+ *\r
+ * @param   ch - Character\r
+ *\r
+ * @return  None.\r
+ *\r
+ * @post    ::uartPrintf_head is incremented by one with wrap at UART_PRINTF_BUF_LEN\r
+ *          if there is room.\r
+ */\r
+void uartPrintf_putch(char ch)\r
+{\r
+    // uartPrintf_tail should never catch up with uartPrintf_head. Discard in-between bytes.\r
+       if ( (uartPrintf_head + 1) % UART_PRINTF_BUF_LEN == uartPrintf_tail )\r
+               return;\r
+\r
+       uartPrintf_outArray[uartPrintf_head] = ch;\r
+       uartPrintf_head++;\r
+\r
+       if (uartPrintf_head >= UART_PRINTF_BUF_LEN)\r
+               uartPrintf_head = 0;\r
+}\r
+\r
+/*********************************************************************\r
+ * @fn      uartPrintf_flush\r
+ *\r
+ * @brief   Printf-buffer flush function\r
+ *\r
+ *          In this implementation it is intended to be called by the\r
+ *          Idle task when nothing else is running.\r
+ *\r
+ *          This is achieved by setting up the Idle task in the TI-RTOS\r
+ *          configuration script like so:\r
+ *\r
+ *          var Idle = xdc.useModule('ti.sysbios.knl.Idle');\r
+ *          Idle.addFunc('&uartPrintf_flush');\r
+ *\r
+ * @param   None. Relies on global state.\r
+ *\r
+ * @return  None.\r
+ *\r
+ * @post    ::uartPrintf_tail is incremented to where uartPrintf_head\r
+ *          was at the time the function was called.\r
+  */\r
+void uartPrintf_flush()\r
+{\r
+       // Abort in case UART hasn't been initialized.\r
+       if (NULL == hUart)\r
+               return;\r
+\r
+  // Lock head position to avoid race conditions\r
+  uint16_t curHead = uartPrintf_head;\r
+\r
+  // Find out how much data must be output, and how to output it.\r
+  int needWrap = curHead < uartPrintf_tail;\r
+\r
+  uint16_t outLen = needWrap?(UART_PRINTF_BUF_LEN-uartPrintf_tail+curHead):(curHead-uartPrintf_tail);\r
+\r
+       if (outLen)\r
+       {\r
+               if (needWrap)\r
+               {\r
+                       UART_write(hUart, &uartPrintf_outArray[uartPrintf_tail], UART_PRINTF_BUF_LEN - uartPrintf_tail);\r
+                       UART_write(hUart, uartPrintf_outArray, curHead);\r
+               }\r
+               else\r
+               {\r
+                       UART_write(hUart, &uartPrintf_outArray[uartPrintf_tail], outLen);\r
+               }\r
+       }\r
+\r
+       uartPrintf_tail = curHead;\r
+}\r
+\r
+\r
+char uart_getch()\r
+{\r
+       char input;\r
+\r
+       if (NULL == hUart)\r
+               return 0;\r
+\r
+       // Blocking Read\r
+       UART_read(hUart,&input,1);\r
+       return input;\r
+}\r
+\r
+\r