mirror of
https://github.com/elua/elua.git
synced 2025-01-25 01:02:54 +08:00
Ensure Mizar32 i2c pins are initialized if i2c.setup() is not called
This commit is contained in:
parent
374a21f15e
commit
8b0bb4bbb9
@ -159,11 +159,14 @@ static void ARBITRATION_LOST(void); // Bus control was lost
|
|||||||
// ************************
|
// ************************
|
||||||
// The bitbanger itself, taken from http://en.wikipedia.org/wiki/I2C
|
// The bitbanger itself, taken from http://en.wikipedia.org/wiki/I2C
|
||||||
|
|
||||||
// We don't use GPIO open-drain mode, which is not available on all hardware
|
// One-time initialization of the I2C hardware.
|
||||||
// models. Instead, we use two modes to simulate open-drain:
|
// Should be called at all valid initial i2c entry points.
|
||||||
// output of 0 and input.
|
static void i2c_init()
|
||||||
u32 i2c_setup( u32 speed )
|
|
||||||
{
|
{
|
||||||
|
static bool i2c_is_initialized = 0;
|
||||||
|
|
||||||
|
if( i2c_is_initialized ) return;
|
||||||
|
|
||||||
// First, set both pins as high-impedance inputs to avoid startup glitches
|
// First, set both pins as high-impedance inputs to avoid startup glitches
|
||||||
sda_regs->oderc = SDA_PINMASK;
|
sda_regs->oderc = SDA_PINMASK;
|
||||||
scl_regs->oderc = SCL_PINMASK;
|
scl_regs->oderc = SCL_PINMASK;
|
||||||
@ -176,6 +179,16 @@ u32 i2c_setup( u32 speed )
|
|||||||
sda_regs->gpers = SDA_PINMASK;
|
sda_regs->gpers = SDA_PINMASK;
|
||||||
scl_regs->gpers = SCL_PINMASK;
|
scl_regs->gpers = SCL_PINMASK;
|
||||||
|
|
||||||
|
i2c_is_initialized++; // set to true
|
||||||
|
}
|
||||||
|
|
||||||
|
// We don't use GPIO open-drain mode, which is not available on all hardware
|
||||||
|
// models. Instead, we use two modes to simulate open-drain:
|
||||||
|
// output of 0 and input.
|
||||||
|
u32 i2c_setup( u32 speed )
|
||||||
|
{
|
||||||
|
i2c_init();
|
||||||
|
|
||||||
// Limit range to possible values, to avoid divisions by zero below.
|
// Limit range to possible values, to avoid divisions by zero below.
|
||||||
if (speed == 0) speed = 1;
|
if (speed == 0) speed = 1;
|
||||||
if (speed > REQ_CPU_FREQ / 2) speed = REQ_CPU_FREQ / 2;
|
if (speed > REQ_CPU_FREQ / 2) speed = REQ_CPU_FREQ / 2;
|
||||||
@ -193,6 +206,8 @@ static int started = 0;
|
|||||||
|
|
||||||
void i2c_start_cond(void)
|
void i2c_start_cond(void)
|
||||||
{
|
{
|
||||||
|
i2c_init();
|
||||||
|
|
||||||
if (started) {
|
if (started) {
|
||||||
// if started, do a restart cond
|
// if started, do a restart cond
|
||||||
// set SDA to 1
|
// set SDA to 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user