[Cbed-svn-commit] SF.net SVN: cbed:[1] cbed
Status: Planning
Brought to you by:
stl-ajk
From: <st...@us...> - 2011-03-19 19:30:29
|
Revision: 1 http://cbed.svn.sourceforge.net/cbed/?rev=1&view=rev Author: stl-ajk Date: 2011-03-19 19:30:22 +0000 (Sat, 19 Mar 2011) Log Message: ----------- initial import Added Paths: ----------- cbed/ cbed/cbed.h cbed/inc/ cbed/inc/iomacros.h cbed/inc/ticker.h cbed/src/ cbed/src/ticker.c cbed/src/uart0.c cbed/src/uart1.c cbed/src/uart2.c cbed/src/uart3.c cbed/src/uartx.h Added: cbed/cbed.h =================================================================== --- cbed/cbed.h (rev 0) +++ cbed/cbed.h 2011-03-19 19:30:22 UTC (rev 1) @@ -0,0 +1,53 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/** + * @file cbed.h + * @purpose The main cbed library header. + * @version see ChangeLog.h + * @date Mar 2011 + * @author Andy Kirkham + */ + + + +#ifndef CBED_CBED_H +#define CBED_CBED_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <time.h> + +#include "iomacros.h" +#include "ticker.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif Added: cbed/inc/iomacros.h =================================================================== --- cbed/inc/iomacros.h (rev 0) +++ cbed/inc/iomacros.h 2011-03-19 19:30:22 UTC (rev 1) @@ -0,0 +1,426 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/** + * @file iomacros.h + * @purpose Handles GPIO pins. + * @version see ChangeLog.h + * @date Mar 2011 + * @author Andy Kirkham + */ + +#ifndef CBED_IOMACROS_H +#define CBED_IOMACROS_H + +#ifndef __LPC17xx_H__ +#include "LPC17xx.h" +#endif + +#define PIN_PULLUP 0UL +#define PIN_REPEAT 1UL +#define PIN_NONE 2UL +#define PIN_PULLDOWN 3UL + +/* p5 is P0.9 */ +#define p5_SEL_MASK ~(3UL << 18) +#define p5_SET_MASK (1UL << 9) +#define p5_CLR_MASK ~(p5_SET_MASK) +#define p5_AS_OUTPUT LPC_PINCON->PINSEL0&=p5_SEL_MASK;LPC_GPIO0->FIODIR|=p5_SET_MASK +#define p5_AS_INPUT LPC_GPIO0->FIOMASK &= p5_CLR_MASK; +#define p5_SET LPC_GPIO0->FIOSET = p5_SET_MASK +#define p5_CLR LPC_GPIO0->FIOCLR = p5_SET_MASK +#define p5_IS_SET (bool)(LPC_GPIO0->FIOPIN & p5_SET_MASK) +#define p5_IS_CLR !(p5_IS_SET) +#define p5_MODE(x) LPC_PINCON->PINMODE0&=p5_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<18) + +/* p6 is P0.8 */ +#define p6_SEL_MASK ~(3UL << 16) +#define p6_SET_MASK (1UL << 8) +#define p6_CLR_MASK ~(p6_SET_MASK) +#define p6_AS_OUTPUT LPC_PINCON->PINSEL0&=p6_SEL_MASK;LPC_GPIO0->FIODIR|=p6-SET_MASK +#define p6_AS_INPUT LPC_GPIO0->FIOMASK &= p6_CLR_MASK; +#define p6_SET LPC_GPIO0->FIOSET = p6_SET_MASK +#define p6_CLR LPC_GPIO0->FIOCLR = p6_SET_MASK +#define p6_IS_SET (bool)(LPC_GPIO0->FIOPIN & p6_SET_MASK) +#define p6_IS_CLR !(p6_IS_SET) +#define p6_MODE(x) LPC_PINCON->PINMODE0&=p6_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<16) + +/* p7 is P0.7 */ +#define p7_SEL_MASK ~(3UL << 14) +#define p7_SET_MASK (1UL << 7) +#define p7_CLR_MASK ~(p7_SET_MASK) +#define p7_AS_OUTPUT LPC_PINCON->PINSEL0&=p7_SEL_MASK;LPC_GPIO0->FIODIR|=p7_SET_MASK +#define p7_AS_INPUT LPC_GPIO0->FIOMASK &= p7_CLR_MASK; +#define p7_SET LPC_GPIO0->FIOSET = p7_SET_MASK +#define p7_CLR LPC_GPIO0->FIOCLR = p7_SET_MASK +#define p7_IS_SET (bool)(LPC_GPIO0->FIOPIN & p7_SET_MASK) +#define p7_IS_CLR !(p7_IS_SET) +#define p7_MODE(x) LPC_PINCON->PINMODE0&=p7_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<14) + +/* p8 is P0.6 */ +#define p8_SEL_MASK ~(3UL << 12) +#define p8_SET_MASK (1UL << 6) +#define p8_CLR_MASK ~(p8_SET_MASK) +#define p8_AS_OUTPUT LPC_PINCON->PINSEL0&=p8_SEL_MASK;LPC_GPIO0->FIODIR|=p8_SET_MASK +#define p8_AS_INPUT LPC_GPIO0->FIOMASK &= p8_CLR_MASK; +#define p8_SET LPC_GPIO0->FIOSET = p8_SET_MASK +#define p8_CLR LPC_GPIO0->FIOCLR = p8_SET_MASK +#define p8_IS_SET (bool)(LPC_GPIO0->FIOPIN & p8_SET_MASK) +#define p8_IS_CLR !(p8_IS_SET) +#define p8_MODE(x) LPC_PINCON->PINMODE0&=p8_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<12) + +/* p9 is P0.0 */ +#define p9_SEL_MASK ~(3UL << 0) +#define p9_SET_MASK (1UL << 0) +#define p9_CLR_MASK ~(p9_SET_MASK) +#define p9_AS_OUTPUT LPC_PINCON->PINSEL0&=p9_SEL_MASK;LPC_GPIO0->FIODIR|=p9_SET_MASK +#define p9_AS_INPUT LPC_GPIO0->FIOMASK &= p9_CLR_MASK; +#define p9_SET LPC_GPIO0->FIOSET = p9_SET_MASK +#define p9_CLR LPC_GPIO0->FIOCLR = p9_SET_MASK +#define p9_IS_SET (bool)(LPC_GPIO0->FIOPIN & p9_SET_MASK) +#define p9_IS_CLR !(p9_IS_SET) +#define p9_MODE(x) LPC_PINCON->PINMODE0&=p9_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<0) + +/* p10 is P0.1 */ +#define p10_SEL_MASK ~(3UL << 2) +#define p10_SET_MASK (1UL << 1) +#define p10_CLR_MASK ~(p10_SET_MASK) +#define p10_AS_OUTPUT LPC_PINCON->PINSEL0&=p10_SEL_MASK;LPC_GPIO0->FIODIR|=p10_SET_MASK +#define p10_AS_INPUT LPC_GPIO0->FIOMASK &= p10_CLR_MASK; +#define p10_SET LPC_GPIO0->FIOSET = p10_SET_MASK +#define p10_CLR LPC_GPIO0->FIOCLR = p10_SET_MASK +#define p10_IS_SET (bool)(LPC_GPIO0->FIOPIN & p10_SET_MASK) +#define p10_IS_CLR !(p10_IS_SET) +#define p10_MODE(x) LPC_PINCON->PINMODE0&=p10_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<2) + +/* p11 is P0.18 */ +#define p11_SEL_MASK ~(3UL << 4) +#define p11_SET_MASK (1UL << 18) +#define p11_CLR_MASK ~(p11_SET_MASK) +#define p11_AS_OUTPUT LPC_PINCON->PINSEL1&=p11_SEL_MASK;LPC_GPIO0->FIODIR|=p11_SET_MASK +#define p11_AS_INPUT LPC_GPIO0->FIOMASK &= p11_CLR_MASK; +#define p11_SET LPC_GPIO0->FIOSET = p11_SET_MASK +#define p11_CLR LPC_GPIO0->FIOCLR = p11_SET_MASK +#define p11_IS_SET (bool)(LPC_GPIO0->FIOPIN & p11_SET_MASK) +#define p11_IS_CLR !(p11_IS_SET) +#define p11_MODE(x) LPC_PINCON->PINMODE1&=p11_SEL_MASK;LPC_PINCON->PINMODE1|=((x&0x3)<<4) + +/* p12 is P0.17 */ +#define p12_SEL_MASK ~(3UL << 2) +#define p12_SET_MASK (1UL << 17) +#define p12_CLR_MASK ~(p12_SET_MASK) +#define p12_AS_OUTPUT LPC_PINCON->PINSEL1&=p12_SEL_MASK;LPC_GPIO0->FIODIR|=p12_SET_MASK +#define p12_AS_INPUT LPC_GPIO0->FIOMASK &= p12_CLR_MASK; +#define p12_SET LPC_GPIO0->FIOSET = p12_SET_MASK +#define p12_CLR LPC_GPIO0->FIOCLR = p12_SET_MASK +#define p12_IS_SET (bool)(LPC_GPIO0->FIOPIN & p12_SET_MASK) +#define p12_IS_CLR !(p12_IS_SET) +#define p12_MODE(x) LPC_PINCON->PINMODE1&=p12_SEL_MASK;LPC_PINCON->PINMODE1|=((x&0x3)<<2) + +/* p13 is P0.15 */ +#define p13_SEL_MASK ~(3UL << 30) +#define p13_SET_MASK (1UL << 15) +#define p13_CLR_MASK ~(p13_SET_MASK) +#define p13_AS_OUTPUT LPC_PINCON->PINSEL0&=p13_SEL_MASK;LPC_GPIO0->FIODIR|=p13_SET_MASK +#define p13_AS_INPUT LPC_GPIO0->FIOMASK &= p13_CLR_MASK; +#define p13_SET LPC_GPIO0->FIOSET = p13_SET_MASK +#define p13_CLR LPC_GPIO0->FIOCLR = p13_SET_MASK +#define p13_IS_SET (bool)(LPC_GPIO0->FIOPIN & p13_SET_MASK) +#define p13_IS_CLR !(p13_IS_SET) +#define p13_MODE(x) LPC_PINCON->PINMODE0&=p13_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<30) + +/* p14 is P0.16 */ +#define p14_SEL_MASK ~(3UL << 0) +#define p14_SET_MASK (1UL << 16) +#define p14_CLR_MASK ~(p14_SET_MASK) +#define p14_AS_OUTPUT LPC_PINCON->PINSEL1&=p14_SEL_MASK;LPC_GPIO0->FIODIR|=p14_SET_MASK +#define p14_AS_INPUT LPC_GPIO0->FIOMASK &= p14_CLR_MASK; +#define p14_SET LPC_GPIO0->FIOSET = p14_SET_MASK +#define p14_CLR LPC_GPIO0->FIOCLR = p14_SET_MASK +#define p14_IS_SET (bool)(LPC_GPIO0->FIOPIN & p14_SET_MASK) +#define p14_IS_CLR !(p14_IS_SET) +#define p14_MODE(x) LPC_PINCON->PINMODE1&=p14_SEL_MASK;LPC_PINCON->PINMODE1|=((x&0x3)<<0) + +/* p15 is P0.23 */ +#define p15_SEL_MASK ~(3UL << 14) +#define p15_SET_MASK (1UL << 23) +#define p15_CLR_MASK ~(p15_SET_MASK) +#define p15_AS_OUTPUT LPC_PINCON->PINSEL1&=p15_SEL_MASK;LPC_GPIO0->FIODIR|=p15_SET_MASK +#define p15_AS_INPUT LPC_GPIO0->FIOMASK &= p15_CLR_MASK; +#define p15_SET LPC_GPIO0->FIOSET = p15_SET_MASK +#define p15_CLR LPC_GPIO0->FIOCLR = p15_SET_MASK +#define p15_IS_SET (bool)(LPC_GPIO0->FIOPIN & p15_SET_MASK) +#define p15_IS_CLR !(p15_IS_SET) +#define p15_MODE(x) LPC_PINCON->PINMODE1&=p15_SEL_MASK;LPC_PINCON->PINMODE1|=((x&0x3)<<14) + +/* p16 is P0.24 */ +#define p16_SEL_MASK ~(3UL << 16) +#define p16_SET_MASK (1UL << 24) +#define p16_CLR_MASK ~(p16_SET_MASK) +#define p16_AS_OUTPUT LPC_PINCON->PINSEL1&=p16_SEL_MASK;LPC_GPIO0->FIODIR|=p16_SET_MASK +#define p16_AS_INPUT LPC_GPIO0->FIOMASK &= p16_CLR_MASK; +#define p16_SET LPC_GPIO0->FIOSET = p16_SET_MASK +#define p16_CLR LPC_GPIO0->FIOCLR = p16_SET_MASK +#define p16_IS_SET (bool)(LPC_GPIO0->FIOPIN & p16_SET_MASK) +#define p16_IS_CLR !(p16_IS_SET) +#define p16_MODE(x) LPC_PINCON->PINMODE1&=p16_SEL_MASK;LPC_PINCON->PINMODE1|=((x&0x3)<<16) + +/* p17 is P0.25 */ +#define p17_SEL_MASK ~(3UL << 18) +#define p17_SET_MASK (1UL << 25) +#define p17_CLR_MASK ~(p17_SET_MASK) +#define p17_AS_OUTPUT LPC_PINCON->PINSEL1&=p17_SEL_MASK;LPC_GPIO0->FIODIR|=p17_SET_MASK +#define p17_AS_INPUT LPC_GPIO0->FIOMASK &= p17_CLR_MASK; +#define p17_SET LPC_GPIO0->FIOSET = p17_SET_MASK +#define p17_CLR LPC_GPIO0->FIOCLR = p17_SET_MASK +#define p17_IS_SET (bool)(LPC_GPIO0->FIOPIN & p17_SET_MASK) +#define p17_IS_CLR !(p17_IS_SET) +#define p17_MODE(x) LPC_PINCON->PINMODE1&=p17_SEL_MASK;LPC_PINCON->PINMODE1|=((x&0x3)<<18) + +/* p18 is P0.26 */ +#define p18_SEL_MASK ~(3UL << 20) +#define p18_SET_MASK (1UL << 26) +#define p18_CLR_MASK ~(p18_SET_MASK) +#define p18_AS_OUTPUT LPC_PINCON->PINSEL1&=p18_SEL_MASK;LPC_GPIO0->FIODIR|=p18_SET_MASK +#define p18_AS_INPUT LPC_GPIO0->FIOMASK &= p18_CLR_MASK; +#define p18_SET LPC_GPIO0->FIOSET = p18_SET_MASK +#define p18_CLR LPC_GPIO0->FIOCLR = p18_SET_MASK +#define p18_IS_SET (bool)(LPC_GPIO0->FIOPIN & p18_SET_MASK) +#define p18_IS_CLR !(p18_IS_SET) +#define p18_MODE(x) LPC_PINCON->PINMODE1&=p18_SEL_MASK;LPC_PINCON->PINMODE1|=((x&0x3)<<20) + +/* p19 is P1.30 */ +#define p19_SEL_MASK ~(3UL << 28) +#define p19_SET_MASK (1UL << 30) +#define p19_AS_OUTPUT LPC_PINCON->PINSEL3&=p19_SEL_MASK;LPC_GPIO1->FIODIR|=p19_SET_MASK +#define p19_AS_INPUT LPC_GPIO1->FIOMASK &= p19_CLR_MASK; +#define p19_SET LPC_GPIO1->FIOSET = p19_SET_MASK +#define p19_CLR LPC_GPIO1->FIOCLR = p19_SET_MASK +#define p19_IS_SET (bool)(LPC_GPIO1->FIOPIN & p19_SET_MASK) +#define p19_IS_CLR !(p19_IS_SET) +#define p19_MODE(x) LPC_PINCON->PINMODE3&=p19_SEL_MASK;LPC_PINCON->PINMODE3|=((x&0x3)<<28) + +/* p20 is P1.31 */ +#define p20_SEL_MASK ~(3UL << 30) +#define p20_SET_MASK (1UL << 31) +#define p20_CLR_MASK ~(p20_SET_MASK) +#define p20_AS_OUTPUT LPC_PINCON->PINSEL3&=p20_SEL_MASK;LPC_GPIO1->FIODIR|=p20_SET_MASK +#define p20_AS_INPUT LPC_GPIO1->FIOMASK &= p20_CLR_MASK; +#define p20_SET LPC_GPIO1->FIOSET = p20_SET_MASK +#define p20_CLR LPC_GPIO1->FIOCLR = p20_SET_MASK +#define p20_IS_SET (bool)(LPC_GPIO1->FIOPIN & p20_SET_MASK) +#define p20_IS_CLR !(p20_IS_SET) +#define p20_MODE(x) LPC_PINCON->PINMODE3&=p20_SEL_MASK;LPC_PINCON->PINMODE3|=((x&0x3)<<30) + +/* p21 is P2.5 */ +#define p21_SEL_MASK ~(3UL << 10) +#define p21_SET_MASK (1UL << 5) +#define p21_CLR_MASK ~(p21_SET_MASK) +#define p21_AS_OUTPUT LPC_PINCON->PINSEL4&=p21_SEL_MASK;LPC_GPIO2->FIODIR|=p21_SET_MASK +#define p21_AS_INPUT LPC_GPIO2->FIOMASK &= p21_CLR_MASK; +#define p21_SET LPC_GPIO2->FIOSET = p21_SET_MASK +#define p21_CLR LPC_GPIO2->FIOCLR = p21_SET_MASK +#define p21_IS_SET (bool)(LPC_GPIO2->FIOPIN & p21_SET_MASK) +#define p21_IS_CLR !(p21_IS_SET) +#define p21_TOGGLE p21_IS_SET?p21_CLR:p21_SET +#define p21_MODE(x) LPC_PINCON->PINMODE4&=p21_SEL_MASK;LPC_PINCON->PINMODE4|=((x&0x3)<<10) + +/* p22 is P2.4 */ +#define p22_SEL_MASK ~(3UL << 8) +#define p22_SET_MASK (1UL << 4) +#define p22_CLR_MASK ~(p22_SET_MASK) +#define p22_AS_OUTPUT LPC_PINCON->PINSEL4&=p22_SEL_MASK;LPC_GPIO2->FIODIR|=p22_SET_MASK +#define p22_AS_INPUT LPC_GPIO2->FIOMASK &= p22_CLR_MASK; +#define p22_SET LPC_GPIO2->FIOSET = p22_SET_MASK +#define p22_CLR LPC_GPIO2->FIOCLR = p22_SET_MASK +#define p22_IS_SET (bool)(LPC_GPIO2->FIOPIN & p22_SET_MASK) +#define p22_IS_CLR !(p22_IS_SET) +#define p22_TOGGLE p22_IS_SET?p22_CLR:p22_SET +#define p22_MODE(x) LPC_PINCON->PINMODE4&=p22_SEL_MASK;LPC_PINCON->PINMODE4|=((x&0x3)<<8) + +/* p23 is P2.3 */ +#define p23_SEL_MASK ~(3UL << 6) +#define p23_SET_MASK (1UL << 3) +#define p23_CLR_MASK ~(p23_SET_MASK) +#define p23_AS_OUTPUT LPC_PINCON->PINSEL4&=p23_SEL_MASK;LPC_GPIO2->FIODIR|=p23_SET_MASK +#define p23_AS_INPUT LPC_GPIO2->FIOMASK &= p23_CLR_MASK; +#define p23_SET LPC_GPIO2->FIOSET = p23_SET_MASK +#define p23_CLR LPC_GPIO2->FIOCLR = p23_SET_MASK +#define p23_IS_SET (bool)(LPC_GPIO2->FIOPIN & p23_SET_MASK) +#define p23_IS_CLR !(p23_IS_SET) +#define p23_TOGGLE p23_IS_SET?p23_CLR:p23_SET +#define p23_MODE(x) LPC_PINCON->PINMODE4&=p23_SEL_MASK;LPC_PINCON->PINMODE4|=((x&0x3)<<6) + +/* p24 is P2.2 */ +#define p24_SEL_MASK ~(3UL << 4) +#define p24_SET_MASK (1UL << 2) +#define p24_CLR_MASK ~(p24_SET_MASK) +#define p24_AS_OUTPUT LPC_PINCON->PINSEL4&=p24_SEL_MASK;LPC_GPIO2->FIODIR|=p24_SET_MASK +#define p24_AS_INPUT LPC_GPIO2->FIOMASK &= p24_CLR_MASK; +#define p24_SET LPC_GPIO2->FIOSET = p24_SET_MASK +#define p24_CLR LPC_GPIO2->FIOCLR = p24_SET_MASK +#define p24_IS_SET (bool)(LPC_GPIO2->FIOPIN & p24_SET_MASK) +#define p24_IS_CLR !(p24_IS_SET) +#define p24_TOGGLE p24_IS_SET?p24_CLR:p24_SET +#define p24_MODE(x) LPC_PINCON->PINMODE4&=p24_SEL_MASK;LPC_PINCON->PINMODE4|=((x&0x3)<<4) + +/* p25 is P2.1 */ +#define p25_SEL_MASK ~(3UL << 2) +#define p25_SET_MASK (1UL << 1) +#define p25_CLR_MASK ~(p25_SET_MASK) +#define p25_AS_OUTPUT LPC_PINCON->PINSEL4&=p25_SEL_MASK;LPC_GPIO2->FIODIR|=p25_SET_MASK +#define p25_AS_INPUT LPC_GPIO2->FIOMASK &= p25_CLR_MASK; +#define p25_SET LPC_GPIO2->FIOSET = p25_SET_MASK +#define p25_CLR LPC_GPIO2->FIOCLR = p25_SET_MASK +#define p25_IS_SET (bool)(LPC_GPIO2->FIOPIN & p25_SET_MASK) +#define p25_IS_CLR !(p25_IS_SET) +#define p25_MODE(x) LPC_PINCON->PINMODE4&=p25_SEL_MASK;LPC_PINCON->PINMODE4|=((x&0x3)<<2) + +/* p26 is P2.0 */ +#define p26_SEL_MASK ~(3UL << 0) +#define p26_SET_MASK (1UL << 0) +#define p26_CLR_MASK ~(p26_SET_MASK) +#define p26_AS_OUTPUT LPC_PINCON->PINSEL4&=p26_SEL_MASK;LPC_GPIO2->FIODIR|=p26_SET_MASK +#define p26_AS_INPUT LPC_GPIO2->FIOMASK &= p26_CLR_MASK; +#define p26_SET LPC_GPIO2->FIOSET = p26_SET_MASK +#define p26_CLR LPC_GPIO2->FIOCLR = p26_SET_MASK +#define p26_IS_SET (bool)(LPC_GPIO2->FIOPIN & p26_SET_MASK) +#define p26_IS_CLR !(p26_IS_SET) +#define p26_MODE(x) LPC_PINCON->PINMODE4&=p26_SEL_MASK;LPC_PINCON->PINMODE4|=((x&0x3)<<0) + +/* p27 is P0.11 */ +#define p27_SEL_MASK ~(3UL << 22) +#define p27_SET_MASK (1UL << 11) +#define p27_CLR_MASK ~(p27_SET_MASK) +#define p27_AS_OUTPUT LPC_PINCON->PINSEL0&=p27_SEL_MASK;LPC_GPIO0->FIODIR|=p27_SET_MASK +#define p27_AS_INPUT LPC_GPIO0->FIOMASK &= p27_CLR_MASK; +#define p27_SET LPC_GPIO0->FIOSET = p27_SET_MASK +#define p27_CLR LPC_GPIO0->FIOCLR = p27_SET_MASK +#define p27_IS_SET (bool)(LPC_GPIO0->FIOPIN & p27_SET_MASK) +#define p27_IS_CLR !(p27_IS_SET) +#define p27_MODE(x) LPC_PINCON->PINMODE0&=p27_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<22) + +/* p28 is P0.10 */ +#define p28_SEL_MASK ~(3UL << 20) +#define p28_SET_MASK (1UL << 10) +#define p28_CLR_MASK ~(p28_SET_MASK) +#define p28_AS_OUTPUT LPC_PINCON->PINSEL0&=p28_SEL_MASK;LPC_GPIO0->FIODIR|=p28_SET_MASK +#define p28_AS_INPUT LPC_GPIO0->FIOMASK &= p28_CLR_MASK; +#define p28_SET LPC_GPIO0->FIOSET = p28_SET_MASK +#define p28_CLR LPC_GPIO0->FIOCLR = p28_SET_MASK +#define p28_IS_SET (bool)(LPC_GPIO0->FIOPIN & p28_SET_MASK) +#define p28_IS_CLR !(p28_IS_SET) +#define p28_MODE(x) LPC_PINCON->PINMODE0&=p28_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<20) + +/* p29 is P0.5 */ +#define p29_SEL_MASK ~(3UL << 10) +#define p29_SET_MASK (1UL << 5) +#define p29_CLR_MASK ~(p29_SET_MASK) +#define p29_AS_OUTPUT LPC_PINCON->PINSEL0&=p29_SEL_MASK;LPC_GPIO0->FIODIR|=p29_SET_MASK +#define p29_AS_INPUT LPC_GPIO0->FIOMASK &= p29_CLR_MASK; +#define p29_SET LPC_GPIO0->FIOSET = p29_SET_MASK +#define p29_CLR LPC_GPIO0->FIOCLR = p29_SET_MASK +#define p29_IS_SET (bool)(LPC_GPIO0->FIOPIN & p29_SET_MASK) +#define p29_IS_CLR !(p29_IS_SET) +#define p29_TOGGLE p29_IS_SET?p29_CLR:p29_SET +#define p29_MODE(x) LPC_PINCON->PINMODE0&=p29_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<10) + +/* p30 is P0.4 */ +#define p30_SEL_MASK ~(3UL << 8) +#define p30_SET_MASK (1UL << 4) +#define p30_CLR_MASK ~(p30_SET_MASK) +#define p30_AS_OUTPUT LPC_PINCON->PINSEL0&=p30_SEL_MASK;LPC_GPIO0->FIODIR|=p30_SET_MASK +#define p30_AS_INPUT LPC_GPIO0->FIOMASK &= p30_CLR_MASK; +#define p30_SET LPC_GPIO0->FIOSET = p30_SET_MASK +#define p30_CLR LPC_GPIO0->FIOCLR = p30_SET_MASK +#define p30_IS_SET (bool)(LPC_GPIO0->FIOPIN & p30_SET_MASK) +#define p30_IS_CLR !(p30_IS_SET) +#define p30_MODE(x) LPC_PINCON->PINMODE0&=p30_SEL_MASK;LPC_PINCON->PINMODE0|=((x&0x3)<<8) + +/* The following definitions are for the four Mbed LEDs. + LED1 = P1.18 + LED2 = P1.20 + LED3 = P1.21 + LED4 = P1.23 */ + +#define P1_18_SEL_MASK ~(3UL << 4) +#define P1_18_SET_MASK (1UL << 18) +#define P1_18_CLR_MASK ~(P1_18_SET_MASK) +#define P1_18_AS_OUTPUT LPC_PINCON->PINSEL3&=P1_18_SEL_MASK;LPC_GPIO1->FIODIR|=P1_18_SET_MASK +#define P1_18_AS_INPUT LPC_GPIO1->FIOMASK &= P1_18_CLR_MASK; +#define P1_18_SET LPC_GPIO1->FIOSET = P1_18_SET_MASK +#define P1_18_CLR LPC_GPIO1->FIOCLR = P1_18_SET_MASK +#define P1_18_IS_SET (bool)(LPC_GPIO1->FIOPIN & P1_18_SET_MASK) +#define P1_18_IS_CLR !(P1_18_IS_SET) +#define LED1_USE P1_18_AS_OUTPUT;P1_18_AS_INPUT +#define LED1_ON P1_18_SET +#define LED1_OFF P1_18_CLR +#define LED1_IS_ON P1_18_IS_SET +#define LED1_TOGGLE P1_18_IS_SET?LED1_OFF:LED1_ON + +#define P1_20_SEL_MASK ~(3UL << 8) +#define P1_20_SET_MASK (1UL << 20) +#define P1_20_CLR_MASK ~(P1_20_SET_MASK) +#define P1_20_AS_OUTPUT LPC_PINCON->PINSEL3&=P1_20_SEL_MASK;LPC_GPIO1->FIODIR|=P1_20_SET_MASK +#define P1_20_AS_INPUT LPC_GPIO1->FIOMASK &= P1_20_CLR_MASK; +#define P1_20_SET LPC_GPIO1->FIOSET = P1_20_SET_MASK +#define P1_20_CLR LPC_GPIO1->FIOCLR = P1_20_SET_MASK +#define P1_20_IS_SET (bool)(LPC_GPIO1->FIOPIN & P1_20_SET_MASK) +#define P1_20_IS_CLR !(P1_20_IS_SET) +#define LED2_USE P1_20_AS_OUTPUT;P1_20_AS_INPUT +#define LED2_ON P1_20_SET +#define LED2_OFF P1_20_CLR +#define LED2_IS_ON P1_20_IS_SET +#define LED2_TOGGLE P1_20_IS_SET?LED2_OFF:LED2_ON + +#define P1_21_SEL_MASK ~(3UL << 10) +#define P1_21_SET_MASK (1UL << 21) +#define P1_21_CLR_MASK ~(P1_21_SET_MASK) +#define P1_21_AS_OUTPUT LPC_PINCON->PINSEL3&=P1_21_SEL_MASK;LPC_GPIO1->FIODIR|=P1_21_SET_MASK +#define P1_21_AS_INPUT LPC_GPIO1->FIOMASK &= P1_21_CLR_MASK; +#define P1_21_SET LPC_GPIO1->FIOSET = P1_21_SET_MASK +#define P1_21_CLR LPC_GPIO1->FIOCLR = P1_21_SET_MASK +#define P1_21_IS_SET (bool)(LPC_GPIO1->FIOPIN & P1_21_SET_MASK) +#define P1_21_IS_CLR !(P1_21_IS_SET) +#define LED3_USE P1_21_AS_OUTPUT;P1_21_AS_INPUT +#define LED3_ON P1_21_SET +#define LED3_OFF P1_21_CLR +#define LED3_IS_ON P1_21_IS_SET +#define LED3_TOGGLE P1_21_IS_SET?LED3_OFF:LED3_ON + +#define P1_23_SEL_MASK ~(3UL << 14) +#define P1_23_SET_MASK (1UL << 23) +#define P1_23_CLR_MASK ~(P1_23_SET_MASK) +#define P1_23_AS_OUTPUT LPC_PINCON->PINSEL3&=P1_23_SEL_MASK;LPC_GPIO1->FIODIR|=P1_23_SET_MASK +#define P1_23_AS_INPUT LPC_GPIO1->FIOMASK &= P1_23_CLR_MASK; +#define P1_23_SET LPC_GPIO1->FIOSET = P1_23_SET_MASK +#define P1_23_CLR LPC_GPIO1->FIOCLR = P1_23_SET_MASK +#define P1_23_IS_SET (bool)(LPC_GPIO1->FIOPIN & P1_23_SET_MASK) +#define P1_23_IS_CLR !(P1_23_IS_SET) +#define LED4_USE P1_23_AS_OUTPUT;P1_23_AS_INPUT +#define LED4_ON P1_23_SET +#define LED4_OFF P1_23_CLR +#define LED4_IS_ON P1_23_IS_SET +#define LED4_TOGGLE P1_23_IS_SET?LED4_OFF:LED4_ON + +#endif + Added: cbed/inc/ticker.h =================================================================== --- cbed/inc/ticker.h (rev 0) +++ cbed/inc/ticker.h 2011-03-19 19:30:22 UTC (rev 1) @@ -0,0 +1,98 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/** + * @file ticker.h + * @purpose Handles tickers, timeouts, timers and wait api. + * @version see ChangeLog.h + * @date Mar 2011 + * @author Andy Kirkham + */ + +#ifndef CBED_TICKER_H +#define CBED_TICKER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <time.h> +#include <stdint.h> +#include "LPC17xx.h" +#include "../cbedopts.h" + +#define CBED_TICKER 1 +#define CBED_TIMER 2 + +#define CBED_TIME_RUN (1UL << 0) +#define CBED_TIME_WRAPPED (1UL << 1) + +typedef struct _ticker_t { + _ticker_t *next; + union { + struct { + void (*callback)(_ticker_t *); + uint32_t counter; + uint32_t reload; + } ticker; + struct { + uint32_t unused; /* Aligns with "ticker.callback" */ + uint64_t time; + uint64_t start; + } timer; + } data; +} ticker_t; + +#define timeout_t ticker_t +#define timer_t ticker_t + +#define timer_value(x) ((x)->data.timer.time) + +void ticker_attach(ticker_t *p, void (*f)(_ticker_t *), uint32_t ms); +void ticker_detach(ticker_t *p); + +void timeout_attach(ticker_t *p, void (*f)(_ticker_t *), uint32_t ms); +void timeout_detach(ticker_t *p); + +void timer_setup(timer_t *p); +void timer_begin(timer_t *p); +void timer_stall(timer_t *p); +void timer_run(timer_t *p); +void timer_reset(timer_t *p); +uint64_t timer_get(timer_t *p); + +uint64_t time_since(void); + +void cbed_wait_us(uint32_t us); +void cbed_wait_ms(uint32_t ms); +void cbed_wait(double s); + +#ifdef __cplusplus +} +#endif + +#endif + Added: cbed/src/ticker.c =================================================================== --- cbed/src/ticker.c (rev 0) +++ cbed/src/ticker.c 2011-03-19 19:30:22 UTC (rev 1) @@ -0,0 +1,520 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/** + * @file ticker.c + * @purpose Handles tickers, timeouts, timers and wait api. + * @version see ChangeLog.h + * @date Mar 2011 + * @author Andy Kirkham + */ + +/** @defgroup API The CBED API */ +/** @defgroup CBedTimer The CBED Timer functions */ +/** @defgroup MISC Misc CBED functions */ +/** @defgroup INTERNALS CBED Internals */ + +#include "stdlib.h" +#include "cmsis_nvic.h" +#include "ticker.h" +#include "iomacros.h" + +#ifndef TICKERSYS_TIMER_CUSTOM +#define TICKERSYS_PCONP (1UL << 23) +#define TICKERSYS_PCLKSEL LPC_SC->PCLKSEL1 +#define TICKERSYS_PCLKSEL_VALUE (3UL << 14) +#define TICKERSYS_TCR LPC_TIM3->TCR +#define TICKERSYS_PR LPC_TIM3->PR +#define TICKERSYS_MCR LPC_TIM3->MCR +#define TICKERSYS_MR0 LPC_TIM3->MR0 +#define TICKERSYS_TC LPC_TIM3->TC +#define TICKERSYS_IR LPC_TIM3->IR +#define TICKERSYS_IRQn TIMER3_IRQn +#define TICKERSYS_TICK 1000 +#else +#define TICKERSYS_PCONP TICKERSYS_TIMER_PCONP +#define TICKERSYS_PCLKSEL TICKERSYS_TIMER_PCLKSEL +#define TICKERSYS_PCLKSEL_VALUE TICKERSYS_TIMER_PCLKSEL_VALUE +#define TICKERSYS_TCR TICKERSYS_TIMER_TCR +#define TICKERSYS_PR TICKERSYS_TIMER_PR +#define TICKERSYS_MCR TICKERSYS_TIMER_MCR +#define TICKERSYS_MR0 TICKERSYS_TIMER_MR0 +#define TICKERSYS_TC TICKERSYS_TIMER_TC +#define TICKERSYS_IR TICKERSYS_TIMER_IR +#define TICKERSYS_IRQn TICKERSYS_TIMER_IRQn +#define TICKERSYS_TICK TICKERSYS_TIMER_TICK +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +static ticker_t *_header_pointer; +static int _ticker_system_live; +static uint64_t _ticker_time; +static uint32_t _ticker_last_tc; + +static void _ticker_system_remove(ticker_t *p); + +void TIMERx_IRQHandler(void) __irq +{ + ticker_t *p, *q; + + _ticker_time += 1000UL; + _ticker_last_tc = TICKERSYS_TC; + + TICKERSYS_MR0 += TICKERSYS_TICK; + p = _header_pointer; + + while(p) { + if (p->data.ticker.callback != NULL && p->data.ticker.counter > 0) { + p->data.ticker.counter--; + if (p->data.ticker.counter == 0) { + (p->data.ticker.callback)(p); + if (p->data.ticker.reload) p->data.ticker.counter = p->data.ticker.reload; + else { + q = p; + p = p->next; + _ticker_system_remove(q); + continue; + } + } + } + p = p->next; + } + + TICKERSYS_IR |= 4; +} + +static void _ticker_system_init(void) +{ + if (_ticker_system_live) return; + + _ticker_system_live = 1; + + _header_pointer = (ticker_t *)NULL; + + _ticker_last_tc = 0; + _ticker_time = 0; + + // Set-up a timer as a periodic 1ms int. + LPC_SC->PCONP |= TICKERSYS_PCONP; + TICKERSYS_PCLKSEL &= ~TICKERSYS_PCLKSEL_VALUE; + TICKERSYS_PCLKSEL |= TICKERSYS_PCLKSEL_VALUE; + TICKERSYS_PR = 11; + TICKERSYS_MCR = 1; + TICKERSYS_MR0 = TICKERSYS_TICK; + TICKERSYS_TCR = 2; + TICKERSYS_TCR = 1; + + NVIC_SetVector(TICKERSYS_IRQn, (uint32_t)TIMERx_IRQHandler); + NVIC_EnableIRQ(TICKERSYS_IRQn); +} + +/** _ticker_system_add - Add a struct instance to the linked list. + * + * @ingroup INTERNALS + * @param[in] p A pointer the data structure. + */ +static void _ticker_system_add(ticker_t *p) +{ + if (!_ticker_system_live) _ticker_system_init(); + + ticker_t *q; + + /* Ensure only one instance of this structure in the list. */ + for (q = _header_pointer; q; q = q->next) if (q == p) return; + + /* Add at the head of the list. Faster than adding at the + tail and list order is not important. */ + p->next = _header_pointer; + _header_pointer = p; +} + +/** _ticker_system_remove - Remove a struct instance from the linked list. + * + * @ingroup INTERNALS + * @param[in] p A pointer the data structure. + */ +static void _ticker_system_remove(ticker_t *p) +{ + ticker_t *q; + + /* If it's the head being removed handle that */ + if (_header_pointer == p) { + _header_pointer = p->next; + } + /* Otherwise look through the list and find it. */ + else { + q = _header_pointer; + while(q) { + if (q->next == p) { + q->next = p->next; + break; + } + q = q->next; + } + } +} + +/** ticker_attach - Attach a callback to a ticker_t data structure. + * + * Used to attach a ticker_t to a callback. + * + * @ingroup API + * @ingroup CBedTimer + * + * @code + * #include "cbed.h" + * + * ticker_t t; + * + * void toggle_led2(ticker_t *p) { LED2_TOGGLE; } + * + * int main() { + * LED1_USE; //!< @see iomacros.h + * LED2_USE; //!< @see iomacros.h + * ticker_attach(&t, &toggle_led2, 100); + * while(1) { + * LED1_ON; + * cbed_wait(1.0); + * LED1_OFF; + * cbed_wait(1.0); + * } + * } + * @endcode + * + * @param[in] p A pointer the data structure for the ticker + * @param[in] f A function pointer to callback to. + * @param[in] ms The time period, in milliseconds, to invoke the callback. + */ +void ticker_attach(ticker_t *p, void (*f)(_ticker_t *), uint32_t ms) +{ + p->data.ticker.callback = f; + p->data.ticker.counter = ms; + p->data.ticker.reload = ms; + _ticker_system_add(p); +} + +/** ticker_detach - Detach a ticker_t data structure. + * + * Used to detach a ticker_t to a callback. + * + * @ingroup API + * @ingroup CBedTimer + * + * @param[in] p A pointer the data structure for the ticker + */ +void ticker_detach(ticker_t *p) +{ + _ticker_system_remove(p); +} + +/** timeout_attach - Attach a callback to a timeout_t data structure. + * + * Used to attach a timeout_t to a callback. <b>Note</b>, when the timeout occurs + * and after the callback has been invoked the timeout_t is automatically + * detached from the system (as though you had called <i>timeout_detact(p);</i> + * + * @ingroup API + * @ingroup CBedTimer + * + * @code + * #include "cbed.h" + * + * timeout_t t; + * + * void toggle_led2(timeout_t *p) { LED2_ON; } + * + * int main() { + * LED1_USE; //!< @see iomacros.h + * LED2_USE; //!< @see iomacros.h + * timeout_attach(&t, &toggle_led2, 5000); + * while(1) { + * LED1_ON; + * cbed_wait(1.0); + * LED1_OFF; + * cbed_wait(1.0); + * } + * } + * @endcode + * + * @param[in] p A pointer the data structure for the timeout + * @param[in] f A function pointer to callback to. + * @param[in] ms The time period, in milliseconds, until invoking the callback. + */ +void timeout_attach(ticker_t *p, void (*f)(_ticker_t *), uint32_t ms) +{ + p->data.ticker.callback = f; + p->data.ticker.counter = ms; + p->data.ticker.reload = 0; + _ticker_system_add(p); +} + +/** timeout_detach - Detach a timeout_t data structure. + * + * Used to detach a timeout_t to a callback. Use this to + * disable a pending timeout that was set to prevent the + * callback from being made. + * + * @ingroup API + * @ingroup CBedTimer + * @param[in] p A pointer the data structure for the timeout. + */ +void timeout_detach(ticker_t *p) +{ + _ticker_system_remove(p); +} + +/** timer_setup - Setup a timer_t structure ready for use. + * + * @code + * #include "cbed.h" + * + * timer_t t; + * + * int main() { + * LED1_USE; + * timer_setup(&t); + * while(1) { + * timer_begin(&t); + * LED1_ON; + * cbed_wait(0.5); + * LED1_OFF; + * cbed_wait(0.5); + * timer_stall(&t); + * printf("Time accumulated so far = %lld\n", timer_get(&t)); + * } + * } + * @endcode + * @see timer_begin + * @see timer_stall + * @see timer_get + * @see timer_reset + * @ingroup API + * @ingroup CBedTimer + * @param[in] p A pointer the data structure for the timer. + */ +void timer_setup(timer_t *p) +{ + p->data.timer.unused = 0; + p->data.timer.time = 0; + _ticker_system_add(p); +} + +/** timer_reset - Reset the timer. + * + * @ingroup API + * @ingroup CBedTimer + * @param[in] p A pointer the data structure for the timer. + */ +void timer_reset(timer_t *p) +{ + p->data.timer.unused = 0; + p->data.timer.time = 0; +} + +/** timer_run - Start the timer. + * + * @see timer_setup + * @ingroup API + * @ingroup CBedTimer + * @param[in] p A pointer the data structure for the timer. + */ +void timer_run(timer_t *p) +{ + NVIC_DisableIRQ(TICKERSYS_IRQn); + p->data.timer.start = _ticker_time + (uint64_t)(TICKERSYS_TC - _ticker_last_tc); + NVIC_EnableIRQ(TICKERSYS_IRQn); +} + +/** timer_begin - Start the timer. + * + * Same as timer_run() + * + * @see timer_setup + * @ingroup API + * @ingroup CBedTimer + * @param[in] p A pointer the data structure for the timer. + */ +void timer_begin(timer_t *p) +{ + NVIC_DisableIRQ(TICKERSYS_IRQn); + p->data.timer.start = _ticker_time + (uint64_t)(TICKERSYS_TC - _ticker_last_tc); + NVIC_EnableIRQ(TICKERSYS_IRQn); +} + +/** timer_get - Get the timer time so far as a 64bit unisgned long. + * + * This function returns the current value of the timer_t pointed to by p. + * The return type is uint64_t in microseconds. + * + * @see timer_setup + * @ingroup API + * @ingroup CBedTimer + * @param[in] p A pointer the data structure for the timer. + * @return[out] uint64_t microseconds timer_t value. + */ +uint64_t timer_get(timer_t *p) +{ + return p->data.timer.time; +} + +/** timer_stall - Halt the timer pointed to by p. + * + * This function halts the timer. Note, it does n't reset it so + * if the timer is restarted it continues accumulating the time. + * + * @see timer_setup + * @ingroup API + * @ingroup CBedTimer + * @param[in] p A pointer the data structure for the timer. + */ +void timer_stall(timer_t *p) +{ + uint64_t diff; + NVIC_DisableIRQ(TICKERSYS_IRQn); + diff = _ticker_time - p->data.timer.start + p->data.timer.time; + p->data.timer.time = diff + (uint64_t)(TICKERSYS_TC - _ticker_last_tc); + NVIC_EnableIRQ(TICKERSYS_IRQn); +} + +/** time_since_reset - Return the number of microseconds since power-on/reset. + * + * This function returns the current numer of microseconds since the + * power-on/reset epoch. + * + * @ingroup API + * @ingroup CBedTimer + * @param[out] Number of microseconds since power-on/reset. + */ +uint64_t time_since_reset(void) +{ + return _ticker_time; +} + +/** cbed_wait_us - Wait for a period of microseconds + * + * Cause your program to delay for a period of microseconds. + * Please note, this function is generally only useable with any + * accuracy down to around 10 to 20us or so due mainly to function + * call overhead. If you require accurate microsecond timing you + * should be using a dedicated timer/capture usint and suitable + * implementation. + * + * @code + * #include "cbed.h" + * + * int main() { + * LED1_USE; //!< @see iomacros.h + * while(1) { + * LED1_ON; + * cbed_wait_us(1000000); + * LED1_OFF; + * cbed_wait_us(1000000); + * } + * } + * @endcode + * + * @ingroup API + * @ingroup CBedTimer + * @param[in] us The number of microseconds to wait. + */ +void cbed_wait_us(uint32_t us) +{ + volatile uint32_t x, y; + if (!_ticker_system_live) _ticker_system_init(); + x = TICKERSYS_TC + (us); + do { + y = TICKERSYS_TC; + } + while(x > y); +} + +/** cbed_wait_ms - Wait for a period of milliseconds + * + * Cause your program to delay for a period of milliseconds. + * Please note, this function is generally only useable with any + * accuracy down to around 10 to 20us or so due mainly to function + * call overhead. If you require accurate microsecond timing you + * should be using a dedicated timer/capture usint and suitable + * implementation. + * + * @code + * #include "cbed.h" + * + * int main() { + * LED1_USE; //!< @see iomacros.h + * while(1) { + * LED1_ON; + * cbed_wait_ms(100000); + * LED1_OFF; + * cbed_wait_ms(100000); + * } + * } + * @endcode + * + * @ingroup API + * @ingroup CBedTimer + * @param[in] ms The number of milliseconds to wait. + */ +void cbed_wait_ms(uint32_t ms) +{ + cbed_wait_us(ms * 1000); +} + +/** cbed_wait - Wait for a period of seconds + * + * Cause your program to delay for a period of seconds. + * Please note, this function is generally only useable with any + * accuracy down to around 10 to 20us or so due mainly to function + * call overhead. If you require accurate microsecond timing you + * should be using a dedicated timer/capture and suitable + * implementation. + * + * @code + * #include "cbed.h" + * + * int main() { + * LED1_USE; //!< @see iomacros.h + * while(1) { + * LED1_ON; + * cbed_wait(1.0); + * LED1_OFF; + * cbed_wait(1.0); + * } + * } + * @endcode + * + * @ingroup API + * @ingroup CBedTimer + * @param[in] d The number of seconds to wait (is double, fractional seconds) + */ +void cbed_wait(double d) +{ + cbed_wait_us((uint32_t)(d * 1000000.0)); +} + +#ifdef __cplusplus +} +#endif + Added: cbed/src/uart0.c =================================================================== --- cbed/src/uart0.c (rev 0) +++ cbed/src/uart0.c 2011-03-19 19:30:22 UTC (rev 1) @@ -0,0 +1,100 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/** + * @file ticker.h + * @purpose Handles tickers, timeouts, timers and wait api. + * @version see ChangeLog.h + * @date Mar 2011 + * @author Andy Kirkham + */ + +#include "../cbedopts.h" + +#ifdef CBED_BUILD_UART0 + +#ifdef CBED_BUILD_UART0_WITH_TX_BUFFER +#define CBED_BUILD_UARTx_WITH_TX_BUFFER CBED_BUILD_UART0_WITH_TX_BUFFER +#endif + +#ifdef CBED_BUILD_UART0_WITH_RX_BUFFER +#define CBED_BUILD_UARTx_WITH_RX_BUFFER CBED_BUILD_UART0_WITH_RX_BUFFER +#endif + +#define UARTX_tx_buffer cbed_uart0_tx_buffer +#define UARTX_tx_buffer_in cbed_uart0_tx_buffer_in +#define UARTX_tx_buffer_out cbed_uart0_tx_buffer_out +#define UARTX_tx_buffer_count cbed_uart0_tx_buffer_count + +#define UARTX_rx_buffer cbed_uart0_rx_buffer +#define UARTX_rx_buffer_in cbed_uart0_rx_buffer_in +#define UARTX_rx_buffer_out cbed_uart0_rx_buffer_out +#define UARTX_rx_buffer_count cbed_uart0_rx_buffer_count + +#define UARTX_callback_tx cbed_uart0_callback_tx +#define UARTX_callback_rx cbed_uart0_callback_rx +#define UARTX_callback_rx_overflow cbed_uart0_callback_rx_overflow + +#define UARTX_isr cbed_uart0_isr +#define UARTX_isr_tx cbed_uart0_isr_tx +#define UARTX_isr_rx cbed_uart0_isr_rx +#define UARTX__getc uart0__get +#define UARTX_getc uart0_getc +#define UARTX_putc uart0_putc + +#define UARTX_init cbed_uart0_init + +#define UARTX_IRQn UART0_IRQn + +#define UARTX_IIR LPC_UART0->IIR +#define UARTX_IER LPC_UART0->IER +#define UARTX_RBR LPC_UART0->RBR +#define UARTX_THR LPC_UART0->THR +#define UARTX_LSR LPC_UART0->LSR +#define UARTX_FCR LPC_UART0->FCR + +#define UARTX_PCONP LPC_SC->PCONP +#define UARTX_PCONP_VALUE (1UL << 3) +#define UARTX_PCLKSEL LPC_SC->PCLKSEL0 +#define UARTX_PCLKSEL_VALUE (3UL << 6) +#define UARTX_PINSEL_TX LPC_PINCON->PINSEL0 +#define UARTX_PINSEL_TX_VAL (1UL << 4) +#define UARTX_PINSEL_RX LPC_PINCON->PINSEL0 +#define UARTX_PINSEL_RX_VAL (1UL << 6) + +/* Pull in the common core uart code. */ +#include "uartx.h" + +/* For UART0 only, handle stdout/stdin redirection. */ + +#if CBED_TARGET == CBED_MBED +int __putc(int c) +{ + return 0; +} +#endif + +#if CBED_TARGET == CBED_LPCXPRESSO + +#endif + +#endif Added: cbed/src/uart1.c =================================================================== --- cbed/src/uart1.c (rev 0) +++ cbed/src/uart1.c 2011-03-19 19:30:22 UTC (rev 1) @@ -0,0 +1,87 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/** + * @file uart1.c + * @purpose Builds the uart1 instance + * @version see ChangeLog.h + * @date Mar 2011 + * @author Andy Kirkham + */ + +#include "../cbedopts.h" + +#ifdef CBED_BUILD_UART1 + +#ifdef CBED_BUILD_UART1_WITH_TX_BUFFER +#define CBED_BUILD_UARTx_WITH_TX_BUFFER CBED_BUILD_UART1_WITH_TX_BUFFER +#endif + +#ifdef CBED_BUILD_UART1_WITH_RX_BUFFER +#define CBED_BUILD_UARTx_WITH_RX_BUFFER CBED_BUILD_UART1_WITH_RX_BUFFER +#endif + +#define UARTX_tx_buffer cbed_uart1_tx_buffer +#define UARTX_tx_buffer_in cbed_uart1_tx_buffer_in +#define UARTX_tx_buffer_out cbed_uart1_tx_buffer_out +#define UARTX_tx_buffer_count cbed_uart1_tx_buffer_count + +#define UARTX_rx_buffer cbed_uart1_rx_buffer +#define UARTX_rx_buffer_in cbed_uart1_rx_buffer_in +#define UARTX_rx_buffer_out cbed_uart1_rx_buffer_out +#define UARTX_rx_buffer_count cbed_uart1_rx_buffer_count + +#define UARTX_callback_tx cbed_uart1_callback_tx +#define UARTX_callback_rx cbed_uart1_callback_rx +#define UARTX_callback_rx_overflow cbed_uart1_callback_rx_overflow + +#define UARTX_isr cbed_uart1_isr +#define UARTX_isr_tx cbed_uart1_isr_tx +#define UARTX_isr_rx cbed_uart1_isr_rx +#define UARTX__getc uart1__get +#define UARTX_getc uart1_getc +#define UARTX_putc uart1_putc + +#define UARTX_init cbed_uart1_init + +#define UARTX_IRQn UART1_IRQn + +#define UARTX_IIR LPC_UART1->IIR +#define UARTX_IER LPC_UART1->IER +#define UARTX_RBR LPC_UART1->RBR +#define UARTX_THR LPC_UART1->THR +#define UARTX_LSR LPC_UART1->LSR +#define UARTX_FCR LPC_UART1->FCR + +#define UARTX_PCONP LPC_SC->PCONP +#define UARTX_PCONP_VALUE (1UL << 4) +#define UARTX_PCLKSEL LPC_SC->PCLKSEL0 +#define UARTX_PCLKSEL_VALUE (3UL << 8) +#define UARTX_PINSEL_TX LPC_PINCON->PINSEL0 +#define UARTX_PINSEL_TX_VAL (1UL << 30) +#define UARTX_PINSEL_RX LPC_PINCON->PINSEL1 +#define UARTX_PINSEL_RX_VAL (1UL << 0) + +/* Pull in the common core uart code. */ +#include "uartx.h" + +#endif Added: cbed/src/uart2.c =================================================================== --- cbed/src/uart2.c (rev 0) +++ cbed/src/uart2.c 2011-03-19 19:30:22 UTC (rev 1) @@ -0,0 +1,87 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/** + * @file uart2.c + * @purpose Creates an instance of uart2 + * @version see ChangeLog.h + * @date Mar 2011 + * @author Andy Kirkham + */ + +#include "../cbedopts.h" + +#ifdef CBED_BUILD_UART2 + +#ifdef CBED_BUILD_UART2_WITH_TX_BUFFER +#define CBED_BUILD_UARTx_WITH_TX_BUFFER CBED_BUILD_UART2_WITH_TX_BUFFER +#endif + +#ifdef CBED_BUILD_UART2_WITH_RX_BUFFER +#define CBED_BUILD_UARTx_WITH_RX_BUFFER CBED_BUILD_UART2_WITH_RX_BUFFER +#endif + +#define UARTX_tx_buffer cbed_uart2_tx_buffer +#define UARTX_tx_buffer_in cbed_uart2_tx_buffer_in +#define UARTX_tx_buffer_out cbed_uart2_tx_buffer_out +#define UARTX_tx_buffer_count cbed_uart2_tx_buffer_count + +#define UARTX_rx_buffer cbed_uart2_rx_buffer +#define UARTX_rx_buffer_in cbed_uart2_rx_buffer_in +#define UARTX_rx_buffer_out cbed_uart2_rx_buffer_out +#define UARTX_rx_buffer_count cbed_uart2_rx_buffer_count + +#define UARTX_callback_tx cbed_uart2_callback_tx +#define UARTX_callback_rx cbed_uart2_callback_rx +#define UARTX_callback_rx_overflow cbed_uart2_callback_rx_overflow + +#define UARTX_isr cbed_uart2_isr +#define UARTX_isr_tx cbed_uart2_isr_tx +#define UARTX_isr_rx cbed_uart2_isr_rx +#define UARTX__getc uart2__get +#define UARTX_getc uart2_getc +#define UARTX_putc uart2_putc + +#define UARTX_init cbed_uart2_init + +#define UARTX_IRQn UART2_IRQn + +#define UARTX_IIR LPC_UART2->IIR +#define UARTX_IER LPC_UART2->IER +#define UARTX_RBR LPC_UART2->RBR +#define UARTX_THR LPC_UART2->THR +#define UARTX_LSR LPC_UART2->LSR +#define UARTX_FCR LPC_UART2->FCR + +#define UARTX_PCONP LPC_SC->PCONP +#define UARTX_PCONP_VALUE (1UL << 24) +#define UARTX_PCLKSEL LPC_SC->PCLKSEL1 +#define UARTX_PCLKSEL_VALUE (3UL << 16) +#define UARTX_PINSEL_TX LPC_PINCON->PINSEL0 +#define UARTX_PINSEL_TX_VAL (1UL << 20) +#define UARTX_PINSEL_RX LPC_PINCON->PINSEL0 +#define UARTX_PINSEL_RX_VAL (1UL << 22) + +/* Pull in the common core uart code. */ +#include "uartx.h" + +#endif Added: cbed/src/uart3.c =================================================================== --- cbed/src/uart3.c (rev 0) +++ cbed/src/uart3.c 2011-03-19 19:30:22 UTC (rev 1) @@ -0,0 +1,87 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/** + * @file uart3.c + * @purpose Creates an instance of uart3 + * @version see ChangeLog.h + * @date Mar 2011 + * @author Andy Kirkham + */ + +#include "../cbedopts.h" + +#ifdef CBED_BUILD_UART3 + +#ifdef CBED_BUILD_UART3_WITH_TX_BUFFER +#define CBED_BUILD_UARTx_WITH_TX_BUFFER CBED_BUILD_UART3_WITH_TX_BUFFER +#endif + +#ifdef CBED_BUILD_UART3_WITH_RX_BUFFER +#define CBED_BUILD_UARTx_WITH_RX_BUFFER CBED_BUILD_UART3_WITH_RX_BUFFER +#endif + +#define UARTX_tx_buffer cbed_uart3_tx_buffer +#define UARTX_tx_buffer_in cbed_uart3_tx_buffer_in +#define UARTX_tx_buffer_out cbed_uart3_tx_buffer_out +#define UARTX_tx_buffer_count cbed_uart3_tx_buffer_count + +#define UARTX_rx_buffer cbed_uart3_rx_buffer +#define UARTX_rx_buffer_in cbed_uart3_rx_buffer_in +#define UARTX_rx_buffer_out cbed_uart3_rx_buffer_out +#define UARTX_rx_buffer_count cbed_uart3_rx_buffer_count + +#define UARTX_callback_tx cbed_uart3_callback_tx +#define UARTX_callback_rx cbed_uart3_callback_rx +#define UARTX_callback_rx_overflow cbed_uart3_callback_rx_overflow + +#define UARTX_isr cbed_uart3_isr +#define UARTX_isr_tx cbed_uart3_isr_tx +#define UARTX_isr_rx cbed_uart3_isr_rx +#define UARTX__getc uart3__get +#define UARTX_getc uart3_getc +#define UARTX_putc uart3_putc + +#define UARTX_init cbed_uart3_init + +#define UARTX_IRQn UART3_IRQn + +#define UARTX_IIR LPC_UART3->IIR +#define UARTX_IER LPC_UART3->IER +#define UARTX_RBR LPC_UART3->RBR +#define UARTX_THR LPC_UART3->THR +#define UARTX_LSR LPC_UART3->LSR +#define UARTX_FCR LPC_UART3->FCR + +#define UARTX_PCONP LPC_SC->PCONP +#define UARTX_PCONP_VALUE (1UL << 25) +#define UARTX_PCLKSEL LPC_SC->PCLKSEL1 +#define UARTX_PCLKSEL_VALUE (3UL << 18) +#define UARTX_PINSEL_TX LPC_PINCON->PINSEL0 +#define UARTX_PINSEL_TX_VAL (1UL << 0) +#define UARTX_PINSEL_RX LPC_PINCON->PINSEL0 +#define UARTX_PINSEL_RX_VAL (1UL << 2) + +/* Pull in the common core uart code. */ +#include "uartx.h" + +#endif Added: cbed/src/uartx.h =================================================================== --- cbed/src/uartx.h (rev 0) +++ cbed/src/uartx.h 2011-03-19 19:30:22 UTC (rev 1) @@ -0,0 +1,241 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/** + * @file uartx.h + * @purpose General file that exapnds to create a UART instance. + * @version see ChangeLog.h + * @date Mar 2011 + * @author Andy Kirkham + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <time.h> +#include <stdint.h> +#include "cmsis_nvic.h" +#include "LPC17xx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef CBED_BUILD_UARTx_WITH_TX_BUFFER +static char UARTX_tx_buffer[CBED_BUILD_UARTx_WITH_TX_BUFFER]; +static int UARTX_tx_buffer_in; +static int UARTX_tx_buffer_out; +static int UARTX_tx_buffer_count; +#endif + +#ifdef CBED_BUILD_UARTx_WITH_RX_BUFFER +static char UARTX_rx_buffer[CBED_BUILD_UARTx_WITH_RX_BUFFER]; +static int UARTX_rx_buffer_in; +static int UARTX_rx_buffer_out; +static int UARTX_rx_buffer_count; +#endif + +void (*UARTX_callback_tx)(void); +void (*UARTX_callback_rx)(void); +void (*UARTX_callback_rx_overflow)(char c); + +void UARTX_isr_tx(bool doCallback) +{ + #ifdef CBED_BUILD_UARTx_WITH_TX_BUFFER + if (UARTX_tx_buffer_in != UARTX_tx_buffer_out || UARTX_tx_buffer_count == CBED_BUILD_UARTx_WITH_TX_BUFFER) { + UARTX_THR = (uint32_t)UARTX_tx_buffer[UARTX_tx_buffer_out]; + UARTX_tx_buffer_count--; + UARTX_tx_buffer_out++; + if (UARTX_tx_buffer_out == CBED_BUILD_UARTx_WITH_TX_BUFFER) { + UARTX_tx_buffer_out = 0; + } + if (UARTX_tx_buffer_count == 0) { + UARTX_IER = 1; + } + if (doCallback && UARTX_callback_tx) (UARTX_callback_tx)(); + } + #else + UARTX_IER = 1; + if (UARTX_callback_tx) (UARTX_callback_tx)(); + #endif +} + +void UARTX_isr_rx(void) +{ + #ifdef CBED_BUILD_UARTx_WITH_RX_BUFFER + char c __attribute__((unused)); + while(UARTX_LSR & 0x1) { + if (UARTX_rx_buffer_count == CBED_BUILD_UARTx_WITH_RX_BUFFER) { + c = UARTX_RBR; + if (UARTX_callback_rx_overflow) (UARTX_callback_rx_overflow)(c); + } + else { + UARTX_rx_buffer[UARTX_rx_buffer_in] = UARTX_RBR; + UARTX_rx_buffer_in++; + if (UARTX_rx_buffer_in == CBED_BUILD_UARTx_WITH_RX_BUFFER) { + UARTX_rx_buffer_in = 0; + } + UARTX_rx_buffer_count++; + if (UARTX_callback_rx) (UARTX_callback_rx)(); + } + } + #else + if (UARTX_callback_rx) (UARTX_callback_rx)(); + #endif +} + +int UARTX_putc(int c, bool block) +{ + #ifdef CBED_BUILD_UARTx_WITH_TX_BUFFER + uint32_t ier; + if (UARTX_LSR & (1UL << 5) && UARTX_tx_buffer_count == 0) { + UARTX_THR = (uint32_t)c; + } + else { + if (block) { + ier = UARTX_IER; UARTX_IER = 1; + while(UARTX_tx_buffer_count == CBED_BUILD_UARTx_WITH_TX_BUFFER) { + UARTX_isr_tx(false); + } + UARTX_IER = ier; + } + else if (UARTX_tx_buffer_count == CBED_BUILD_UARTx_WITH_TX_BUFFER) { + return -1; + } + UARTX_IER &= ~2; + UARTX_tx_buffer[UARTX_tx_buffer_in] = c; + UARTX_tx_buffer_in++; + if (UARTX_tx_buffer_in == CBED_BUILD_UARTx_WITH_TX_BUFFER) UARTX_tx_buffer_in = 0; + UARTX_IER |= 2; + } + #else + while(!(UARTX_LSR & (1UL << 5)); + UARTX_THR = (uint32_t)c; + #endif + return 0; +} + +int UARTX__getc(bool block) { + #ifdef CBED_BUILD_UARTx_WITH_RX_BUFFER + uint32_t ier; + int c; + if (block) { while (UARTX_rx_buffer_count == 0) ; } /* blocks. */ + else if (UARTX_rx_buffer_count == 0) return -1; + c = UARTX_rx_buffer[UARTX_rx_buffer_out]; + UARTX_rx_buffer_out++; + if (UARTX_rx_buffer_out == CBED_BUILD_UARTx_WITH_RX_BUFFER) UARTX_rx_buffer_out = 0; + if (UARTX_rx_buffer_count != CBED_BUILD_UARTx_WITH_RX_BUFFER) { + ier = UARTX_IER; + UARTX_IER &= ~(1UL << 0); + UARTX_isr_rx(); + UARTX_IER = ier; + } + UARTX_rx_buffer_count--; + return c; + #else + if (block) { + while (!(UARTX_LSR & (1UL << 0)) ; + return (int)(UARTX_RBR & 0xFF); + } + else { + if (!(UARTX_LSR & (1UL << 0)) return -1; + return (int)(UARTX_RBR & 0xFF); + } + #endif +} + +int UARTX_getc(void) { + return UARTX__getc(true); +} + +extern "C" void UARTX_isr(void) __irq +{ + volatile uint32_t iir, lsr; + + iir = UARTX_IIR; + + if (iir & 1) return; + + iir = (iir >> 1) & 0x3; + + if (iir == 2) { + UARTX_isr_rx(); ... [truncated message content] |