From 845744778aff4a04ce3aa137a2e20931d8dd6073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Fr=C3=B6schle?= Date: Sat, 17 Nov 2012 14:22:34 +0000 Subject: [PATCH] modified busy waiting loops (new function: waitfor(us, condition) to only wait for a certain time until the expected condition comes true, otherwise just return without the job done --- BaS_gcc/include/bas_types.h | 19 ++++++++++++++++ BaS_gcc/sources/BaS.c | 29 +++++++++++++++++++----- BaS_gcc/sources/sysinit.c | 44 ++++++++++++++++++++++++++++++------- 3 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 BaS_gcc/include/bas_types.h diff --git a/BaS_gcc/include/bas_types.h b/BaS_gcc/include/bas_types.h new file mode 100644 index 0000000..0c47b01 --- /dev/null +++ b/BaS_gcc/include/bas_types.h @@ -0,0 +1,19 @@ +/* + * bas_types.h + * + * Created on: 17.11.2012 + * Author: mfro + */ + +#ifndef BAS_TYPES_H_ +#define BAS_TYPES_H_ + +#ifndef _CPLUSPLUS_ +typedef int bool; +#define TRUE 1 +#define FALSE 0 +#endif /* _CPLUSPLUS_ */ + + + +#endif /* BAS_TYPES_H_ */ diff --git a/BaS_gcc/sources/BaS.c b/BaS_gcc/sources/BaS.c index 6d42ea2..5f65485 100644 --- a/BaS_gcc/sources/BaS.c +++ b/BaS_gcc/sources/BaS.c @@ -9,6 +9,7 @@ #include "startcf.h" #include "cache.h" #include "bas_printf.h" +#include "bas_types.h" /* imported routines */ extern int mmu_init(); @@ -18,7 +19,8 @@ extern void sd_card_idle(); extern int sd_card_init(); /* wait...() routines moved to sysinit.c */ -extern void wait(uint32_t us); +extern inline void wait(uint32_t us); +extern inline bool waitfor(uint32_t us,int (*condition)(void)); /* Symbols from the linker script */ extern uint8_t _STRAM_END[]; @@ -32,10 +34,27 @@ extern uint8_t _EMUTOS[]; extern uint8_t _EMUTOS_SIZE[]; #define EMUTOS_SIZE ((uint32_t)_EMUTOS_SIZE) /* size of EmuTOS, in bytes */ +static inline bool pic_txready(void) +{ + if (MCF_PSC3_PSCSR & MCF_PSC_PSCSR_TXRDY) + return TRUE; + + return FALSE; +} + +static inline bool pic_rxready(void) +{ + if (MCF_PSC3_PSCSR & MCF_PSC_PSCSR_RXRDY) + return TRUE; + + return FALSE; +} + void write_pic_byte(uint8_t value) { - /* Wait until the tramsmitter is ready */ - while (!(MCF_PSC3_PSCSR & MCF_PSC_PSCSR_TXRDY)); + /* Wait until the tramsmitter is ready or 1000us are passed */ + waitfor(1000, pic_txready); + //while (!(MCF_PSC3_PSCSR & MCF_PSC_PSCSR_TXRDY)); /* Transmit the byte */ //MCF_PSC3_PSCTB_8BIT = value; // This define is actually 32-bit @@ -44,8 +63,8 @@ void write_pic_byte(uint8_t value) uint8_t read_pic_byte(void) { - /* Wait until a byte has been received */ - while (!(MCF_PSC3_PSCSR & MCF_PSC_PSCSR_RXRDY)); + /* Wait until a byte has been received or 1000us are passed */ + waitfor(1000, pic_rxready); /* Return the received byte */ //return MCF_PSC3_PSCRB_8BIT; // This define is actually 32-bit diff --git a/BaS_gcc/sources/sysinit.c b/BaS_gcc/sources/sysinit.c index 515be71..94b3292 100644 --- a/BaS_gcc/sources/sysinit.c +++ b/BaS_gcc/sources/sysinit.c @@ -11,6 +11,7 @@ #include "cache.h" #include "sysinit.h" #include "bas_printf.h" +#include "bas_types.h" extern void xprintf_before_copy(const char *fmt, ...); #define xprintf xprintf_before_copy @@ -25,14 +26,28 @@ extern volatile long _VRAM; /* start address of video ram from linker script */ * wait for the specified number of us on slice timer 0. Replaces the original routines that had * the number of useconds to wait for hardcoded in their name. */ -void wait(uint32_t us) +inline void wait(uint32_t us) { uint32_t target = MCF_SLT_SCNT(0) - (us * 132); while (MCF_SLT_SCNT(0) > target); } +/* + * the same as above, with a checker function which gets called while + * busy waiting and allows for an early return if it returns true + */ +inline bool waitfor(uint32_t us, int (*condition)(void)) +{ + uint32_t target = MCF_SLT_SCNT(0) - (us * 132); + do + { + if ((*condition)()) + return TRUE; + } while (MCF_SLT_SCNT(0) > target); + return FALSE; +} /* * init SLICE TIMER 0 * all = 32.538 sec = 30.736mHz @@ -472,12 +487,25 @@ void test_upd720101(void) xprintf("finished\r\n"); } +static bool i2c_transfer_finished(void) +{ + if (MCF_I2C_I2SR & MCF_I2C_I2SR_IIF) + return TRUE; + + return FALSE; +} + static void wait_i2c_transfer_finished(void) { - while (!(MCF_I2C_I2SR & MCF_I2C_I2SR_IIF)); /* wait until interrupt bit has been set */ + waitfor(100000, i2c_transfer_finished); /* wait until interrupt bit has been set */ MCF_I2C_I2SR &= ~MCF_I2C_I2SR_IIF; /* clear interrupt bit (byte transfer finished */ } +static bool i2c_bus_free(void) +{ + return (MCF_I2C_I2SR & MCF_I2C_I2SR_IBB); +} + /* * TFP410 (DVI) on */ @@ -508,7 +536,7 @@ void dvi_on(void) { /* i2c enable, master mode, transmit acknowledge */ MCF_I2C_I2CR = MCF_I2C_I2CR_IEN | MCF_I2C_I2CR_MSTA | MCF_I2C_I2CR_MTX; - MCF_I2C_I2DR = 0x7a; /* send data: address of TFP410 */ + MCF_I2C_I2DR = 0x7a; /* send data: address of TFP410 */ wait_i2c_transfer_finished(); if (MCF_I2C_I2SR & MCF_I2C_I2SR_RXAK) /* next try if no acknowledge */ @@ -524,13 +552,13 @@ void dvi_on(void) { if (MCF_I2C_I2SR & MCF_I2C_I2SR_RXAK) /* next try if no acknowledge */ continue; - MCF_I2C_I2CR &= 0xef; //~MCF_I2C_I2CR_MTX; /* switch to receive mode */ - dummyByte = MCF_I2C_I2DR; /* dummy read */ + MCF_I2C_I2CR &= 0xef; //~MCF_I2C_I2CR_MTX; /* switch to receive mode */ + dummyByte = MCF_I2C_I2DR; /* dummy read */ wait_i2c_transfer_finished(); MCF_I2C_I2CR |= MCF_I2C_I2CR_TXAK; /* transmit acknowledge enable */ - receivedByte = MCF_I2C_I2DR; /* read a byte */ + receivedByte = MCF_I2C_I2DR; /* read a byte */ wait_i2c_transfer_finished(); @@ -544,7 +572,7 @@ void dvi_on(void) { MCF_I2C_I2CR = 0x0; // stop MCF_I2C_I2SR = 0x0; // clear sr - while ((MCF_I2C_I2SR & MCF_I2C_I2SR_IBB)); /* wait for bus free */ + waitfor(10000, i2c_bus_free); MCF_I2C_I2CR = 0xb0; // on tx master MCF_I2C_I2DR = 0x7A; @@ -566,7 +594,7 @@ void dvi_on(void) { dummyByte = MCF_I2C_I2DR; // dummy read MCF_I2C_I2SR = 0x0; // clear sr - while ((MCF_I2C_I2SR & MCF_I2C_I2SR_IBB)); /* wait until bus free */ + waitfor(10000, i2c_bus_free); MCF_I2C_I2CR = 0xb0; MCF_I2C_I2DR = 0x7A;