Skip to content

Instantly share code, notes, and snippets.

@grundyoso
Created December 26, 2013 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grundyoso/8130626 to your computer and use it in GitHub Desktop.
Save grundyoso/8130626 to your computer and use it in GitHub Desktop.
After struggling with trying to get the CC2540-MINIDK to talk to another peripheral via the TEST header, I tried the x-ing out the POWERSAVING option in the defined symbols and it worked for me. I'm using USART0 on P1 with DMA for Rx. I was getting corrupted data similar to what's described in the posts here: http://e2e.ti.com/support/low_power_…
From fe276c1ef9560ce81319bc168c4d830de823268e Mon Sep 17 00:00:00 2001
From: Richard Grundy <richard@flomio.com>
Date: Wed, 25 Dec 2013 23:10:11 -0500
Subject: [PATCH] This is a working UART + DMA implementation for USART0 on P1
using the CC2540 Keyfob. The UART data however gets
corrupted during the copy over in the UART Callback. Will
be looking at fixing that in next commit.
---
.gitignore | 1 +
.../Components/hal/target/CC2540EB/_hal_uart_dma.c | 67 ++++++++--------------
.../CC2540DB/SimpleBLEPeripheral.ewp | 4 ++
.../Source/SimpleBLEPeripheral_Main.c | 46 ++++++++++++++-
4 files changed, 73 insertions(+), 45 deletions(-)
diff --git a/.gitignore b/.gitignore
index cbd918f..cc64861 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
# Object files
*.o
*.ko
+Obj/
# Libraries
*.lib
diff --git a/BLE-CC254x-1.4.0/Components/hal/target/CC2540EB/_hal_uart_dma.c b/BLE-CC254x-1.4.0/Components/hal/target/CC2540EB/_hal_uart_dma.c
index 40deeac..4cf7df4 100644
--- a/BLE-CC254x-1.4.0/Components/hal/target/CC2540EB/_hal_uart_dma.c
+++ b/BLE-CC254x-1.4.0/Components/hal/target/CC2540EB/_hal_uart_dma.c
@@ -122,12 +122,12 @@
#undef HAL_UART_Px_RTS
#undef HAL_UART_Px_SEL
#if (HAL_UART_DMA == 1)
-#define PxSEL P0SEL
-#define HAL_UART_PERCFG_BIT 0x01 // USART0 on P0, Alt-1; so clear this bit.
+#define PxSEL P1SEL
+#define HAL_UART_PERCFG_BIT 0x01 // USART0 on P0, Alt-2; so set this bit.
#define HAL_UART_PRIPO 0x00 // USART0 priority over UART1.
#define HAL_UART_Px_CTS 0x10 // Peripheral I/O Select for CTS flow control.
#define HAL_UART_Px_RTS 0x20 // Peripheral I/O Select for RTS must be manual.
-#define HAL_UART_Px_SEL 0x0C // Peripheral I/O Select for Rx/Tx.
+#define HAL_UART_Px_SEL 0x30 // Peripheral I/O Select for Rx/Tx (P1.4 and P1.5)
#elif (HAL_UART_DMA == 2)
#define PxSEL P1SEL
#define HAL_UART_PERCFG_BIT 0x02 // USART1 on P1, Alt-2; so set this bit.
@@ -160,20 +160,7 @@
#undef PICTL_BIT
#undef IENx
#undef IEN_BIT
-#if (HAL_UART_DMA == 1)
-#define PxDIR P0DIR
-#define PxIEN P0IEN
-#define PxIFG P0IFG
-#define PxIF P0IF
-#define DMA_RDYIn P0_4
-#define DMA_RDYOut P0_5
-#define DMA_RDYIn_BIT BV(4) // Same as the I/O Select for CTS flow control.
-#define DMA_RDYOut_BIT BV(5) // Same as the I/O Select for manual RTS flow ctrl.
-// Falling edge ISR on P0 pins.
-#define PICTL_BIT BV(0)
-#define IENx IEN1
-#define IEN_BIT BV(5)
-#elif (HAL_UART_DMA == 2)
+#if defined HAL_UART_DMA
#define PxDIR P1DIR
#define PxIEN P1IEN
#define PxIFG P1IFG
@@ -221,14 +208,14 @@
#if !defined HAL_UART_DMA_TX_MAX
#define HAL_UART_DMA_TX_MAX HAL_UART_DMA_RX_MAX
#endif
-#if !defined HAL_UART_DMA_HIGH
-#define HAL_UART_DMA_HIGH (HAL_UART_DMA_RX_MAX - 1)
-#endif
#if !defined HAL_UART_DMA_IDLE
#define HAL_UART_DMA_IDLE (0 * HAL_UART_MSECS_TO_TICKS)
#endif
#if !defined HAL_UART_DMA_FULL
-#define HAL_UART_DMA_FULL (HAL_UART_DMA_RX_MAX - 16)
+#define HAL_UART_DMA_FULL (HAL_UART_DMA_RX_MAX - 1)
+#endif
+#if !defined HAL_UART_DMA_HIGH
+#define HAL_UART_DMA_HIGH (HAL_UART_DMA_FULL - 16)
#endif
// ST-ticks for 1 byte @ 38.4-kB plus 1 tick added for when the txTick is forced from zero to 0xFF.
@@ -370,11 +357,7 @@ static void HalUARTArmTxDMA(void);
static void HalUARTInitDMA(void)
{
halDMADesc_t *ch;
-#if (HAL_UART_DMA == 1)
- PERCFG &= ~HAL_UART_PERCFG_BIT; // Set UART0 I/O to Alt. 1 location on P0.
-#else
- PERCFG |= HAL_UART_PERCFG_BIT; // Set UART1 I/O to Alt. 2 location on P1.
-#endif
+ PERCFG |= HAL_UART_PERCFG_BIT; // Set UART0 I/O to Alt. 2 location on P1.
PxSEL |= HAL_UART_Px_SEL; // Enable Peripheral control of Rx/Tx on Px.
UxCSR = CSR_MODE; // Mode is UART Mode.
UxUCR = UCR_FLUSH; // Flush it.
@@ -569,15 +552,15 @@ static uint16 HalUARTReadDMA(uint8 *buf, uint16 len)
{
uint16 cnt;
- for (cnt = 0; cnt < len; cnt++)
+ for (cnt = 0; cnt < len; cnt++) // Count the collected bytes in dmaCfg.rxBuf
{
- if (!HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead))
+ if (!HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead)) // Check to see if the Head ptr is an unused byte
{
- break;
+ break; // If the Head ptr is not a new byte, then stop counting/reporting bytes
}
- *buf++ = HAL_UART_DMA_GET_RX_BYTE(dmaCfg.rxHead);
- HAL_UART_DMA_CLR_RX_BYTE(dmaCfg.rxHead);
- HAL_UART_RX_IDX_T_INCR(dmaCfg.rxHead);
+ *buf++ = HAL_UART_DMA_GET_RX_BYTE(dmaCfg.rxHead); // report the byte
+ HAL_UART_DMA_CLR_RX_BYTE(dmaCfg.rxHead); // clear the byte to leave unused
+ HAL_UART_RX_IDX_T_INCR(dmaCfg.rxHead); // increment the circular buffer
}
/* Update pointers after reading the bytes */
@@ -823,9 +806,9 @@ static uint16 HalUARTRxAvailDMA(void)
do
{
- if (!HAL_UART_DMA_NEW_RX_BYTE(tail))
+ if (!HAL_UART_DMA_NEW_RX_BYTE(tail)) // check if byte is unused (not new)
{
- break;
+ break; // we found a unused byte, so stop counting
}
else
{
@@ -837,13 +820,13 @@ static uint16 HalUARTRxAvailDMA(void)
if(!cnt)
{
- while(sweepIdx < HAL_UART_DMA_RX_MAX)
+ while(sweepIdx < (HAL_UART_DMA_RX_MAX-1))
{
- if (HAL_UART_DMA_NEW_RX_BYTE(sweepIdx))
+ if (HAL_UART_DMA_NEW_RX_BYTE(sweepIdx)) // find first new byte (used)
{
- dmaCfg.rxTail = sweepIdx;
- dmaCfg.rxHead = sweepIdx;
- cnt = 0;
+ dmaCfg.rxTail = sweepIdx; // sync tail pointer to this new spot
+ dmaCfg.rxHead = sweepIdx; // sync head pointer to this new spot
+ cnt = 0; // no available bytes in buffer
#ifndef POWER_SAVING
detectOverflow = TRUE;
#endif
@@ -853,7 +836,7 @@ static uint16 HalUARTRxAvailDMA(void)
sweepIdx++;
}
- if ( sweepIdx == HAL_UART_DMA_RX_MAX )
+ if ( sweepIdx == (HAL_UART_DMA_RX_MAX-1) )
{
sweepIdx = 0;
}
@@ -1000,11 +983,7 @@ void HalUART_DMAIsrDMA(void)
*
* @return None.
*************************************************************************************************/
-#if (HAL_UART_DMA == 1)
-HAL_ISR_FUNCTION(port0Isr, P0INT_VECTOR)
-#else
HAL_ISR_FUNCTION(port1Isr, P1INT_VECTOR)
-#endif
{
HAL_ENTER_ISR();
diff --git a/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/CC2540DB/SimpleBLEPeripheral.ewp b/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/CC2540DB/SimpleBLEPeripheral.ewp
index c7b2b85..3f8688d 100644
--- a/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/CC2540DB/SimpleBLEPeripheral.ewp
+++ b/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/CC2540DB/SimpleBLEPeripheral.ewp
@@ -286,6 +286,10 @@
<state>xPLUS_BROADCASTER</state>
<state>HAL_LCD=FALSE</state>
<state>HAL_LED=TRUE</state>
+ <state>HAL_UART=TRUE</state>
+ <state>HAL_UART_DMA=1</state>
+ <state>HAL_UART_DMA_IDLE</state>
+ <state>HAL_UART_DMA_RX_MAX=256</state>
<state>CC2540_MINIDK</state>
</option>
<option>
diff --git a/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/Source/SimpleBLEPeripheral_Main.c b/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/Source/SimpleBLEPeripheral_Main.c
index 3181c42..b553a3f 100644
--- a/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/Source/SimpleBLEPeripheral_Main.c
+++ b/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/Source/SimpleBLEPeripheral_Main.c
@@ -46,6 +46,7 @@
#include "hal_timer.h"
#include "hal_drivers.h"
#include "hal_led.h"
+#include "hal_uart.h"
/* OSAL */
#include "OSAL.h"
@@ -61,6 +62,8 @@
/* This callback is triggered when a key is pressed */
void MSA_Main_KeyCallback(uint8 keys, uint8 state);
+void floBLECBack(uint8 port, uint8 event);
+
/**************************************************************************************************
* @fn main
*
@@ -73,6 +76,8 @@ void MSA_Main_KeyCallback(uint8 keys, uint8 state);
*/
int main(void)
{
+ halUARTCfg_t uartConfig;
+
/* Initialize hardware */
HAL_BOARD_INIT();
@@ -100,6 +105,24 @@ int main(void)
osal_pwrmgr_device( PWRMGR_BATTERY );
#endif
+ /* Shut off LED */
+ HalLedSet(HAL_LED_ALL, HAL_LED_MODE_OFF);
+
+ /* Configure UART to talk to FloJack */
+ uartConfig.configured = TRUE;
+ uartConfig.baudRate = HAL_UART_BR_9600;
+ uartConfig.flowControl = FALSE;
+ //uartConfig.flowControlThreshold = 48;
+ //uartConfig.rx.maxBufSize = 256;
+ //uartConfig.tx.maxBufSize = 128;
+ //uartConfig.idleTimeout = 30;
+ uartConfig.intEnable = TRUE;
+ uartConfig.callBackFunc = (halUARTCBack_t)floBLECBack;
+
+ // start UART
+ // Note: Assumes no issue opening UART port.
+ (void)HalUARTOpen( HAL_UART_PORT_0, &uartConfig );
+
/* Start OSAL */
osal_start_system(); // No Return from here
@@ -109,7 +132,28 @@ int main(void)
/**************************************************************************************************
CALL-BACKS
**************************************************************************************************/
-
+void floBLECBack(uint8 port, uint8 event)
+{
+ static uint16 count = 0;
+ static uint16 counter = 0;
+ static uint8 buffer[500];
+
+ if (event & HAL_UART_RX_TIMEOUT) {
+ count = HalUARTRead( port, &buffer[counter], 256 );
+ counter+=count;
+ HalLedSet(HAL_LED_1, HAL_LED_MODE_BLINK);
+ } else if (event & HAL_UART_RX_ABOUT_FULL) {
+ // error
+ buffer[counter++] = 0xEE;
+ HalLedSet(HAL_LED_2, HAL_LED_MODE_BLINK);
+ } else if (event & HAL_UART_RX_FULL) {
+ // error
+ buffer[counter++] = 0xFF;
+ HalLedSet(HAL_LED_ALL, HAL_LED_MODE_BLINK);
+ }
+ if (counter > 500)
+ while(1);
+}
/*************************************************************************************************
**************************************************************************************************/
--
1.7.11.1
From 500dae2b9957763e7103064acbf0c9aed36c55c3 Mon Sep 17 00:00:00 2001
From: Richard Grundy <richard@flomio.com>
Date: Wed, 25 Dec 2013 23:16:07 -0500
Subject: [PATCH] Stop tracking some build output files: Exe/ and List/ dirs.
---
.gitignore | 2 +
diff --git a/.gitignore b/.gitignore
index cc64861..37514b1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,8 @@
*.o
*.ko
Obj/
+List/
+Exe/
# Libraries
*.lib
--
1.7.11.1
From cb6ecee5bb334933fcd159ce78da43c648a2a946 Mon Sep 17 00:00:00 2001
From: Richard Grundy <richard@flomio.com>
Date: Thu, 26 Dec 2013 01:32:06 -0500
Subject: [PATCH] Fixes the corrupted RX data issue. Simply removed the
xPOWER_SAVING option form the build defines. Suggestions
on TI Forums mentioned removing HCI_EXT_ClkDivOnHaltCmd(
HCI_EXT_ENABLE_CLK_DIVIDE_ON_HALT ); but I had to do that.
---
.../CC2540DB/SimpleBLEPeripheral.ewp | 3 +--
.../Source/SimpleBLEPeripheral_Main.c | 29 ++++++++++++++++------
2 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/CC2540DB/SimpleBLEPeripheral.ewp b/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/CC2540DB/SimpleBLEPeripheral.ewp
index 3f8688d..77c6e8d 100644
--- a/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/CC2540DB/SimpleBLEPeripheral.ewp
+++ b/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/CC2540DB/SimpleBLEPeripheral.ewp
@@ -282,13 +282,12 @@
<state>OSAL_CBTIMER_NUM_TASKS=1</state>
<state>HAL_AES_DMA=TRUE</state>
<state>HAL_DMA=TRUE</state>
- <state>POWER_SAVING</state>
+ <state>xPOWER_SAVING</state>
<state>xPLUS_BROADCASTER</state>
<state>HAL_LCD=FALSE</state>
<state>HAL_LED=TRUE</state>
<state>HAL_UART=TRUE</state>
<state>HAL_UART_DMA=1</state>
- <state>HAL_UART_DMA_IDLE</state>
<state>HAL_UART_DMA_RX_MAX=256</state>
<state>CC2540_MINIDK</state>
</option>
diff --git a/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/Source/SimpleBLEPeripheral_Main.c b/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/Source/SimpleBLEPeripheral_Main.c
index b553a3f..e1c3102 100644
--- a/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/Source/SimpleBLEPeripheral_Main.c
+++ b/BLE-CC254x-1.4.0/Projects/ble/SimpleBLEPeripheral/Source/SimpleBLEPeripheral_Main.c
@@ -134,25 +134,38 @@ int main(void)
**************************************************************************************************/
void floBLECBack(uint8 port, uint8 event)
{
- static uint16 count = 0;
- static uint16 counter = 0;
+ static uint8 count = 0;
+ static uint8 counter = 0;
static uint8 buffer[500];
if (event & HAL_UART_RX_TIMEOUT) {
count = HalUARTRead( port, &buffer[counter], 256 );
+ if (count) {
+ for (int i=counter; i<(count+counter);i++) {
+ if (buffer[i]) {
+ HalLedSet(HAL_LED_1, HAL_LED_MODE_BLINK);
+ break;
+ } else {
+ HalLedSet(HAL_LED_2, HAL_LED_MODE_BLINK);
+ }
+ }
+ } else {
+ HalLedSet(HAL_LED_ALL, HAL_LED_MODE_BLINK);
+ }
counter+=count;
- HalLedSet(HAL_LED_1, HAL_LED_MODE_BLINK);
} else if (event & HAL_UART_RX_ABOUT_FULL) {
// error
- buffer[counter++] = 0xEE;
- HalLedSet(HAL_LED_2, HAL_LED_MODE_BLINK);
+ //buffer[counter++] = 0xEE;
+ HalLedSet(HAL_LED_1, HAL_LED_MODE_BLINK);
+ while(1);
} else if (event & HAL_UART_RX_FULL) {
// error
- buffer[counter++] = 0xFF;
+ //buffer[counter++] = 0xFF;
HalLedSet(HAL_LED_ALL, HAL_LED_MODE_BLINK);
- }
- if (counter > 500)
while(1);
+ }
+ //if (counter > 256)
+ //while(1);
}
/*************************************************************************************************
--
1.7.11.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment