#define SEND_IDLE 0 #define START_DELAY 1 #define SEND 2 #define STOP_DELAY 3 uint8 Buffer[128]; uint8 Tail=0; volatile uint8 Head=0; uint8 Delay = 0; // in turms of ms volatile uint8 flag = false; volatile uint8 State = UART_SEND_IDLE; // uart tx state flag // an app function to write command in buffer for transmitt and exit uint8 writeUARTCmd( uint8 cmd ) { Buffer[Tail] = cmd; if (Tail >= 128) { Tail = 0; } else { Tail++; } if(flag == false ) // if command on uart not seding set intr. { IEN2 |= UTX0IE; } osal_pwrmgr_task_state(TaskID, PWRMGR_HOLD); return SUCCESS; } // function set timer3 for delay. void timer3(uint16 msdelay) { Delay = msdelay; start timer3 for msdelay. } // UART0 tx ISR . // First check is any data to send on UART by checking Head and Tail // If no data to send then disable UART tx intrrupt and set power in converse state. // If there is data to send on UART // clear IF, start TIMER 3 to 40 ms delay before actully data to put on UART U0DBUF register. // Exit. HAL_ISR_FUNCTION( halUART0TxIsr, UTX0_VECTOR ) { if(Head == Tail) { IEN2 &= ~UTX0IE; osal_pwrmgr_task_state(TaskID, PWRMGR_CONSERVE); } else { if(flag == false ) { flag = true; UTX0IF = 0; if(State == UART_SEND_IDLE ) // check UART state IDLE { State = UART_START_DELAY; // state change to START_DELAY timer3(40); // Start timer3 for delay } } } } // Timer 3 ISR is called every 1ms second // Decrement Delay counter // if Delay counter is zero. // if UART state is UART_START_DELAY then call UARTSendCmd() // else if state is UART_STOP_DELAY then call UARTSendComplete() HAL_ISR_FUNCTION( halTimer3Isr, T3_VECTOR ) { TIMIF &= 0xFE ; // cler timer3 flag; T3CTL &= ~0x10; //stop timer Delay--; if(Delay <= 0) { T3CTL |= 0x04; if(State == UART_START_DELAY ) { UARTSendCmd(); } else if (State == UART_STOP_DELAY) { UARTSendComplete(); } } else { T3CTL |= 0x10; // start timer } } // Function put Data on UODBIF from local Buffer // And start timer3 for 12ms delay // Put state to UART_SEND void UARTSendCmd() { UTX0IF = 0; U0DBUF = txBuffer[uartTxHead]; IEN2 &= ~UTX0IE; State = UART_SEND; timer3(12); } // after 12ms delay // set UART state to idle // Enable UART tx interrupt and set interrupt flag true // So UART tx interrupt called again. void UARTSendComplete() { Head++; if(Head >= 128) { Head = 0; } State = UART_SEND_IDLE; flag = false; IEN2 |= UTX0IE; UTX0IF = 1; }