printf() with the FRDM-KL25Z Board and without Processor Expert
Join the DZone community and get the full member experience.
Join For Free
in
this tutorial
i explored how to use printf(), and this tutorial is so generic that it works for any processor/microcontroller. that flexibility is because i’m using processor expert. in case processor expert shall not be used, then some tweaks are needed. here i show what is needed to have
printf()
working with the frdm-kl25z board. i use the uart0 connected to
opensda usb cdc
for this.
creating the project
i create a new bareboard project. nothing special here, except that ‘uart’ should be selected:
low level uart support
as explained in this tutorial i, i need low-level uart support. codewarrior comes with the needed files for the twr-kl25z in “\mcu\arm_gcc_support\uart\twr-kl25z128″:
i copy these files into my project:
the twr-kl25z is using uart0 with pta14/pta15, while the frdm-kl25z is using uart0, but pta1/pta2. so i need to change the pin settings.
for this i disable in consoleio.c, in consoleio_init() the settings for the tower board, and add my change to use pta1/pta2:
void consoleio_init() { initclock(); /* sim_scgc4: uart0=1 */ sim_scgc4 |= sim_scgc4_uart0_mask; #if 0 /* twr version: pta14, pta15 */ /* porta_pcr15: isf=0,mux=3 */ porta_pcr15 = (uint32_t)((porta_pcr15 & (uint32_t)~(uint32_t)( port_pcr_isf_mask | port_pcr_mux(0x04) )) | (uint32_t)( port_pcr_mux(0x03) )); /* porta_pcr14: isf=0,mux=3 */ porta_pcr14 = (uint32_t)((porta_pcr14 & (uint32_t)~(uint32_t)( port_pcr_isf_mask | port_pcr_mux(0x04) )) | (uint32_t)( port_pcr_mux(0x03) )); #else /* frdm-kl25z: pta1/pta2 */ /* porta_pcr1: isf=0,mux=2 */ porta_pcr1 = (uint32_t)((porta_pcr1 & (uint32_t)~0x01000500ul) | (uint32_t)0x0200ul); /* porta_pcr2: isf=0,mux=2 */ porta_pcr2 = (uint32_t)((porta_pcr2 & (uint32_t)~0x01000500ul) | (uint32_t)0x0200ul); #endif uart0_pdd_enabletransmitter(uart0_base_ptr, pdd_disable); /* disable transmitter. */ uart0_pdd_enablereceiver(uart0_base_ptr, pdd_disable); /* disable receiver. */ /* uart0_c1: loops=0,dozeen=0,rsrc=0,m=0,wake=0,ilt=0,pe=0,pt=0 */ uart0_c1 = 0x00u; /* set the c1 register */ /* uart0_c3: r8t9=0,r9t8=0,txdir=0,txinv=0,orie=0,neie=0,feie=0,peie=0 */ uart0_c3 = 0x00u; /* set the c3 register */ /* uart0_s2: lbkdif=0,rxedgif=0,msbf=0,rxinv=0,rwuid=0,brk13=0,lbkde=0,raf=0 */ uart0_s2 = 0x00u; /* set the s2 register */ uart0_pdd_setclocksource(uart0_base_ptr, uart0_pdd_pll_fll_clock); uart0_pdd_setbaudrate(uart0_base_ptr, 313u); /* set the baud rate register. */ uart0_pdd_setoversamplingratio(uart0_base_ptr, 3u); uart0_pdd_enablesamplingonbothedges(uart0_base_ptr, pdd_enable); uart0_pdd_enabletransmitter(uart0_base_ptr, pdd_enable); /* enable transmitter */ uart0_pdd_enablereceiver(uart0_base_ptr, pdd_enable); /* enable receiver */ }
to use different uart/port settings, the easiest way is to use a processor expert project as explained in this tutorial .
example code
for testing, i print a ‘hello world’ from the
main()
in main.c:
/* * main implementation: use this 'c' sample to create your own application * */ #include "derivative.h" /* include peripheral declarations */ #include #include "consoleio.h" int main(void) { int counter = 0; consoleio_init(); for(;;) { counter++; printf("hello world!\r\n"); } return 0; }
do not forget to include the correct header files, and to call consoleio_init()!
that’s it! build, download and hopefully you see the messages printed to the console
.
project on github
for reference, i have committed the codewarrior project and sources on github here .
happy printing
Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Auditing Tools for Kubernetes
-
Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
-
Top 10 Engineering KPIs Technical Leaders Should Know
-
SRE vs. DevOps
Comments