Gents,
I need some expert advice. I want to breakout the needed basic functions in the Serial3 library and put them directly into a sketch that will eventually transmit one way to a serial LCD. I have some code below... which is what I dream of my end result being. FYI, I have not tested this on a compiler, so there may be some slight errors.
but in order to get the LCD_write function to work, I have to
1. rename the tx_buffer[]
2. rename tx_buffer_head & tx_buffer tail,
3. add #include "serial3.c"
4. remove the LCD begin() and LCD_end() functions,
5. and use Serial3.begin(9600). I want stop using this and roll my own.
My question is... what else am I missing that is linked elsewhere in another library in order to accomplish a stand alone function for LCD_write(###, #) with LCD_begin() instead of Serial3.begin().
Please help.
Thanks
BLMinTenn
I need some expert advice. I want to breakout the needed basic functions in the Serial3 library and put them directly into a sketch that will eventually transmit one way to a serial LCD. I have some code below... which is what I dream of my end result being. FYI, I have not tested this on a compiler, so there may be some slight errors.
Code:
#include "kinetis.h"
#include "core_pins.h"
#define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
#define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
#define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
#define C2_TX_INACTIVE C2_ENABLE
static volatile uint8_t tx_buff_head = 0;
static volatile uint8_t tx_buff_tail = 0;
static volatile uint8_t tx_buff[40];
static volatile uint8_t transmitting = 0;
static volatile uint8_t *transmit_pin=NULL;
#define transmit_assert() *transmit_pin = 1
#define transmit_deassert() *transmit_pin = 0
uint8_t my_array[8] = {0xfe, 0x94, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35};
//8N1 = 0x00
//8N2 - 0x04
LCD_begin(uint16_t baud, uint8_t format) {
//format section--------------------
uint8_t c;
c = UART0_C1;
c = (c & ~0x13) | (format & 0x03); // configure parity
if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
UART0_C1 = c;
if ((format & 0x0F) == 0x04) UART0_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
c = UART0_S2 & ~0x10;
if (format & 0x10) c |= 0x10; // rx invert
UART0_S2 = c;
c = UART0_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART0_C3 = c;
//baud & setup section--------------
SIM_SCGC4 |= SIM_SCGC4_UART2; // turn on clock, TODO: use bitband
tx_buff_head = 0;
tx_buff_tail = 0;
transmitting = 0;
CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
uint32_t divisor = ((F_BUS * 2) + ((baud) >> 1)) / (baud);
if(divisor < 32) {divisor = 32;}
if (divisor < 32) divisor = 32;
UART2_BDH = (divisor >> 13) & 0x1F;
UART2_BDL = (divisor >> 5) & 0xFF;
UART2_C4 = divisor & 0x1F;
UART2_C1 = 0;
UART2_PFIFO = 0;
UART2_C2 = C2_TX_INACTIVE;
NVIC_SET_PRIORITY(IRQ_UART2_STATUS, IRQ_PRIORITY);
NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
}
void LCD_end()
{
if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
UART2_C2 = 0;
CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
UART2_S1;
UART2_D; // clear leftover error status
}
void LCD_write(const void *buf, unsigned int count)
{
const uint8_t *p = (const uint8_t *)buf;
while (count-- > 0) {
uint32_t head, n;
if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
//if (transmit_pin) transmit_assert();-------------------------------not needed because not using receive
head = tx_buff_head;
if (++head >= 40) head = 0;
while (tx_buff_tail == head) {
int priority = nvic_execution_priority();
if (priority <= IRQ_PRIORITY) {
if ((UART2_S1 & UART_S1_TDRE)) {
uint32_t tail = tx_buff_tail;
if (++tail >= 40) tail = 0;
n = tx_buff[tail];
UART2_D = n;
tx_buff_tail = tail;
}
} }
tx_buff[head] = *p++;
transmitting = 1;
tx_buff_head = head;
UART2_C2 = C2_TX_ACTIVE;
}
}
void setup()
{
pinMode(8, PUTPUT);
delay(10);
LCD_begin(9600, 0x00);
}
void main()
{
LCD_write(my_array, 8);
}
but in order to get the LCD_write function to work, I have to
1. rename the tx_buffer[]
2. rename tx_buffer_head & tx_buffer tail,
3. add #include "serial3.c"
4. remove the LCD begin() and LCD_end() functions,
5. and use Serial3.begin(9600). I want stop using this and roll my own.
My question is... what else am I missing that is linked elsewhere in another library in order to accomplish a stand alone function for LCD_write(###, #) with LCD_begin() instead of Serial3.begin().
Please help.
Thanks
BLMinTenn