AnsweredAssumed Answered

FRDM-KE04Z Software bit banging

Question asked by vikranth Nk on Apr 16, 2018
Latest reply on Apr 16, 2018 by Mark Butcher

I am using FRDM-KE04Z dev board with onboard MKE04Z8VFK4 MCU, I an=m trying to implement bit banging on this board using 2 GPIO pins one for RX and one for TX. During implementation am unuseful with FRDM-KE04Z. Someone can help me with implementing software UART on this board. As am new to this platform and am unable to configure properly. 

 

Note: The below code has been tested and working on microchip boards. I am to port the same code to FRDM-KE04Z dev board.

 

My code.

#include "common.h"
#include "ics.h"
#include "gpio.h"
#include "sysinit.h"
#include "start.h"


int main (void);


* Global functions
******************************************************************************/
unsigned char UART_Receive(void);
void UART_Transmit(const char DataValue);
void DelayUs(unsigned int cnt);
#define UART_RX    GPIO_PTA3 // UART RX pin
#define UART_TX     GPIO_PTA2 // UART TX pin
#define DataBitCount   8 // no parity, no flow control
#define OneBitDelay    2 //(1000000/1200) // microseconds
//#define GPIOA ((GPIO_Type *)GPIOA_BASE)

/********************************************************************/
int main (void)
{
/* Perform processor initialization */
sysinit();
cpu_identify();

 

/* way 2. GPIO initialize single pin name */

GPIO_PinInit(GPIO_PTA2, GPIO_PinOutput);
GPIO_PinInit(GPIO_PTA3, GPIO_PinInput);

unsigned char ch = 0; // Variable to store Rx character
GPIO_PinSet(UART_TX);  / /TX pin is high in idle state
while (1)
{
ch = UART_Receive(); // Receive a character from UART
UART_Transmit(ch); // Echo back that character
}

}

unsigned char UART_Receive(void)
{
unsigned char DataValue = 0;
uint32_t data;

//wait for start bit
while(UART_RX==1);

DelayUs(OneBitDelay);
DelayUs(OneBitDelay/2); // Take sample value in the mid of bit duration

for ( unsigned char i = 0; i < DataBitCount; i++ )
{
if ( UART_RX == 1 ) //if received bit is high
{
DataValue += (1<<i);
}

DelayUs(OneBitDelay);
}

// Check for stop bit
if ( UART_RX == 1 ) //Stop bit should be high
{
DelayUs(OneBitDelay/2);
return DataValue;
}
else //some error occurred !
{
DelayUs(OneBitDelay/2);
return 0x000;
}
}

 

void UART_Transmit(const char DataValue)
{
/* Basic Logic

TX pin is usually high. A high to low bit is the starting bit and
a low to high bit is the ending bit. No parity bit. No flow control.
BitCount is the number of bits to transmit. Data is transmitted LSB first.

*/

// Send Start Bit
GPIO_PinClear(UART_TX);
DelayUs(OneBitDelay);

for ( unsigned char i = 0; i < DataBitCount; i++ )
{
//Set Data pin according to the DataValue
if( ((DataValue>>i)&0x1) == 0x1 ) //if Bit is high
{
GPIO_PinSet(UART_TX);
}
else //if Bit is low
{
GPIO_PinClear(UART_TX);
}

DelayUs(OneBitDelay);
}

//Send Stop Bit
GPIO_PinSet(UART_TX);
DelayUs(OneBitDelay);
}

void DelayUs(unsigned int cnt)
{
while (--cnt != 0)continue;
}

Outcomes