[Request] I2c_hardware.jal : Option for user-selected bus speed

27 views
Skip to first unread message

David VB

unread,
Apr 4, 2024, 9:30:20 AM4/4/24
to jallib
Hi,

I was struggling with an I2C LCD display until I found out on the manufacturer website that, due to a design flaw, the I²C bus speed should be 50 kHz

My request here is to allow the user to define the sspadd value directly in his application by modifying the following code in i2c_hardware.jal

   -- set correct speed
   if    _i2c_bus_speed == 10 then sspadd = _i2c_1mhz
   elsif _i2c_bus_speed ==  4 then sspadd = _i2c_400khz
   elsif _i2c_bus_speed ==  1 then sspadd = _i2c_100khz
   elsif _i2c_bus_speed ==  0 then sspadd = _i2c_slowest
   else                            sspadd = _i2c_100khz      -- default

   end if

so that the user may override the default 100 kHz value: 
by defining _i2c_bus_speed = 0xFF and initialize sspadd with the needed value in his application

   if _i2c_bus_speed != 0xFF then
     -- set correct speed
       if    _i2c_bus_speed == 10 then sspadd = _i2c_1mhz
        elsif _i2c_bus_speed ==  4 then sspadd = _i2c_400khz
        elsif _i2c_bus_speed ==  1 then sspadd = _i2c_100khz
        elsif _i2c_bus_speed ==  0 then sspadd = _i2c_slowest
        else                            sspadd = _i2c_100khz      -- default
       end if
   end if

What do you think ?

Kind regards,

David

Rob CJ

unread,
Apr 4, 2024, 1:33:24 PM4/4/24
to jal...@googlegroups.com
Hi David,

A few suggestions
Option 1
Why not add a value for 50 kHz, although we cannot enter 0.5.

Option 2
Or replace the 100 kHz by 50 kHz?
So instead of:
const _i2c_slowest = 127                           -- (40 kHz @ 20 MHz Fosc)
Use:
const _i2c_slowest  = target_clock / 50_000 / 4 - 1 –- 50 kHz

Option 3
Another option would be to use a user defined constant, for example:
const I2C_USER_BUS_SPEED = 50_000 

And then overrule everything by:
if defined(I2C_USER_BUS_SPEED) then
  spadd =  target_clock / I2C_USER BUS_SPEED / 4 - 1
else
   if    _i2c_bus_speed == 10 then sspadd = _i2c_1mhz
   elsif _i2c_bus_speed ==  4 then sspadd = _i2c_400khz
   elsif _i2c_bus_speed ==  1 then sspadd = _i2c_100khz
   elsif _i2c_bus_speed ==  0 then sspadd = _i2c_slowest
   else                            sspadd = _i2c_100khz      -- default
end if 

Option 3) would be the most flexible.

Kind regards,

Rob




Van: jal...@googlegroups.com <jal...@googlegroups.com> namens David VB <pinhe...@gmail.com>
Verzonden: donderdag 4 april 2024 15:30
Aan: jallib <jal...@googlegroups.com>
Onderwerp: [jallib] [Request] I2c_hardware.jal : Option for user-selected bus speed
 
--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/28c93aa1-8b14-4004-aa96-a4692f808654n%40googlegroups.com.

Rob CJ

unread,
Apr 4, 2024, 2:44:53 PM4/4/24
to jal...@googlegroups.com
Hi David,

I added option 3) to the library. Not yet tested and not yet uploaded but can you give it a try.

So define:
const I2C_USER_BUS_SPEED = 50_000 

And give it a try. This version also has the user defined i2c_pins that you can use, solving 2 issues at the same time.

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens Rob CJ <rob...@hotmail.com>
Verzonden: donderdag 4 april 2024 19:33
Aan: jal...@googlegroups.com <jal...@googlegroups.com>
Onderwerp: Re: [jallib] [Request] I2c_hardware.jal : Option for user-selected bus speed
 
i2c_hardware.jal

David VB

unread,
Apr 5, 2024, 9:40:44 AM4/5/24
to jallib
Option 3 is definitely to best one !  Thanks Rob !
Reply all
Reply to author
Forward
0 new messages