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

This commit is contained in:
Markus Fröschle
2012-11-17 14:22:34 +00:00
parent d9d7b8b7bf
commit 845744778a
3 changed files with 79 additions and 13 deletions

View File

@@ -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_ */

View File

@@ -9,6 +9,7 @@
#include "startcf.h" #include "startcf.h"
#include "cache.h" #include "cache.h"
#include "bas_printf.h" #include "bas_printf.h"
#include "bas_types.h"
/* imported routines */ /* imported routines */
extern int mmu_init(); extern int mmu_init();
@@ -18,7 +19,8 @@ extern void sd_card_idle();
extern int sd_card_init(); extern int sd_card_init();
/* wait...() routines moved to sysinit.c */ /* 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 */ /* Symbols from the linker script */
extern uint8_t _STRAM_END[]; extern uint8_t _STRAM_END[];
@@ -32,10 +34,27 @@ extern uint8_t _EMUTOS[];
extern uint8_t _EMUTOS_SIZE[]; extern uint8_t _EMUTOS_SIZE[];
#define EMUTOS_SIZE ((uint32_t)_EMUTOS_SIZE) /* size of EmuTOS, in bytes */ #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) void write_pic_byte(uint8_t value)
{ {
/* Wait until the tramsmitter is ready */ /* Wait until the tramsmitter is ready or 1000us are passed */
while (!(MCF_PSC3_PSCSR & MCF_PSC_PSCSR_TXRDY)); waitfor(1000, pic_txready);
//while (!(MCF_PSC3_PSCSR & MCF_PSC_PSCSR_TXRDY));
/* Transmit the byte */ /* Transmit the byte */
//MCF_PSC3_PSCTB_8BIT = value; // This define is actually 32-bit //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) uint8_t read_pic_byte(void)
{ {
/* Wait until a byte has been received */ /* Wait until a byte has been received or 1000us are passed */
while (!(MCF_PSC3_PSCSR & MCF_PSC_PSCSR_RXRDY)); waitfor(1000, pic_rxready);
/* Return the received byte */ /* Return the received byte */
//return MCF_PSC3_PSCRB_8BIT; // This define is actually 32-bit //return MCF_PSC3_PSCRB_8BIT; // This define is actually 32-bit

View File

@@ -11,6 +11,7 @@
#include "cache.h" #include "cache.h"
#include "sysinit.h" #include "sysinit.h"
#include "bas_printf.h" #include "bas_printf.h"
#include "bas_types.h"
extern void xprintf_before_copy(const char *fmt, ...); extern void xprintf_before_copy(const char *fmt, ...);
#define xprintf xprintf_before_copy #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 * 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. * 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); uint32_t target = MCF_SLT_SCNT(0) - (us * 132);
while (MCF_SLT_SCNT(0) > target); 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 * init SLICE TIMER 0
* all = 32.538 sec = 30.736mHz * all = 32.538 sec = 30.736mHz
@@ -472,12 +487,25 @@ void test_upd720101(void)
xprintf("finished\r\n"); 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) 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 */ 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 * TFP410 (DVI) on
*/ */
@@ -544,7 +572,7 @@ void dvi_on(void) {
MCF_I2C_I2CR = 0x0; // stop MCF_I2C_I2CR = 0x0; // stop
MCF_I2C_I2SR = 0x0; // clear sr 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_I2CR = 0xb0; // on tx master
MCF_I2C_I2DR = 0x7A; MCF_I2C_I2DR = 0x7A;
@@ -566,7 +594,7 @@ void dvi_on(void) {
dummyByte = MCF_I2C_I2DR; // dummy read dummyByte = MCF_I2C_I2DR; // dummy read
MCF_I2C_I2SR = 0x0; // clear sr 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_I2CR = 0xb0;
MCF_I2C_I2DR = 0x7A; MCF_I2C_I2DR = 0x7A;