Skip to content

Commit

Permalink
switch back to si2c for supporting modules,DS,WS
Browse files Browse the repository at this point in the history
  • Loading branch information
salhk committed Oct 24, 2023
1 parent 5aec770 commit 59c0c5c
Show file tree
Hide file tree
Showing 17 changed files with 391 additions and 433 deletions.
36 changes: 18 additions & 18 deletions pic/PIC_HAL.tbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ function pic_reg_read(addr as word,length as byte)as string
exit function
end if

i2c.start()
si2c_start()

i2c.write(PIC_CHIP_ADDR_W)
i2c.write(PIC_CMD_R)
si2c_write(PIC_CHIP_ADDR_W)
si2c_write(PIC_CMD_R)

tmp=addr / 256
i2c.write(tmp)
si2c_write(tmp)
tmp=addr or 256
i2c.write(tmp)
si2c_write(tmp)

i2c.stop()
i2c.start()
i2c.write(PIC_CHIP_ADDR_R)
si2c_stop()
si2c_start()
si2c_write(PIC_CHIP_ADDR_R)
for tmp=1 to length-1
data=data+chr(i2c.read(ACK))
data=data+chr(si2c_read(ACK))
next tmp
data=data+chr(i2c.read(NACK))
data=data+chr(si2c_read(NACK))
pic_reg_read=data
i2c.stop()
si2c_stop()
end function

function pic_reg_write(addr as word, data as string)as ok_ng
Expand All @@ -47,23 +47,23 @@ function pic_reg_write(addr as word, data as string)as ok_ng
pic_reg_write=NG
if data="" then exit function

i2c.start()
si2c_start()

i2c.write(PIC_CHIP_ADDR_W)
i2c.write(PIC_CMD_W)
si2c_write(PIC_CHIP_ADDR_W)
si2c_write(PIC_CMD_W)

tmp=addr/256
i2c.write(tmp)
si2c_write(tmp)
tmp=addr or 256
i2c.write(tmp)
si2c_write(tmp)

buf=""
for pos=1 to len(data)
buf=mid(data,pos,1)
i2c.write(asc(buf))
si2c_write(asc(buf))
next pos

i2c.stop()
si2c_stop()

pic_reg_write=OK
end function
Expand Down
40 changes: 17 additions & 23 deletions pic/pic_gra.tbs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ function pic_init(byref signature as string, use_ssi as no_yes, byref fw_str as
pic_use_ssi=use_ssi
channel=si2c_register(signature,data_pin,clk_pin,use_ssi)

i2c.num=channel
i2c.sdamap=data_pin
i2c.sclmap=clk_pin
i2c.enabled=YES

'7.RXDTSEL 0 = RX/DT function is on RC5
'6.SDOSEL 0 = SDO function is on RC2
'5.SSSEL 1 = SS function is on RA3
Expand Down Expand Up @@ -300,7 +295,7 @@ sub pic_config(num as pic_pin_enum, f as pic_func_enum, int_pin as byte, channel
end if
#endif

i2c.num=channel
si2c_get(channel)

if pic_func(num)<>f then
if pic_func(num)=PIC_FUNC_PWM then
Expand Down Expand Up @@ -353,7 +348,7 @@ sub pic_config(num as pic_pin_enum, f as pic_func_enum, int_pin as byte, channel

#if PLATFORM_ID=TPP3W or PLATFORM_ID=TPP2W or PLATFORM_ID=TPP3WG2 or PLATFORM_ID=TPP2WG2
if pic_func(num)=PIC_FUNC_UART then
i2c.num=channel
si2c_get(channel)
'Disable uart before we change to another function
'SPEN = 0
s=pic_reg_read(PIC_RCSTA,1)
Expand Down Expand Up @@ -748,7 +743,7 @@ sub pic_adc_config( p as pic_adc_ref_positive_voltage,n as pic_adc_ref_negative_
dim value,value2 as byte
dim s as string

i2c.num=channel
si2c_get(channel)

s=pic_reg_read(PIC_ADCON1,1)
value=asc(s)
Expand Down Expand Up @@ -956,8 +951,7 @@ function pic_adc_get(ch as pic_adc_enum, channel as byte)as word
end if
end select


i2c.num=channel
si2c_get(channel)

pic_reg_write(PIC_ADCON0,chr(value))

Expand Down Expand Up @@ -995,7 +989,7 @@ sub pic_io_enabled(num as pic_pin_enum, en as no_yes, channel as byte)

if pic_func(num)<>PIC_FUNC_IO then exit sub

i2c.num=channel
si2c_get(channel)

select case num
case PIC_PIN0: 'RC5
Expand Down Expand Up @@ -1085,7 +1079,7 @@ sub pic_io_set(num as pic_pin_enum, state as low_high, channel as byte)

if pic_func(num)<>PIC_FUNC_IO then exit sub

i2c.num=channel
si2c_get(channel)

select case num
case PIC_PIN0: 'RC5
Expand Down Expand Up @@ -1176,7 +1170,7 @@ function pic_io_get(num as pic_pin_enum, channel as byte)as low_high

pic_io_get=LOW

i2c.num=channel
si2c_get(channel)

select case num
case PIC_PIN0: 'RC5
Expand Down Expand Up @@ -1286,7 +1280,7 @@ sub pic_pwm_config(ch as pic_pwm_enum, pulse_width as word,period as word, presc

if pic_func(ch)<>PIC_FUNC_PWM then exit sub

i2c.num=channel
si2c_get(channel)

select case ch
case PIC_PWM0:
Expand Down Expand Up @@ -1384,7 +1378,7 @@ sub pic_pwm_start(ch as pic_pwm_enum, channel as byte)

if pic_func(ch)<>PIC_FUNC_PWM then exit sub

i2c.num=channel
si2c_get(channel)
select case ch
case PIC_PWM0:
if pic_func(0)<>PIC_FUNC_PWM then exit sub
Expand Down Expand Up @@ -1432,7 +1426,7 @@ sub pic_pwm_stop(ch as pic_pwm_enum, channel as byte)

if pic_func(ch)<>PIC_FUNC_PWM then exit sub

i2c.num=channel
si2c_get(channel)

select case ch
case PIC_PWM0:
Expand Down Expand Up @@ -1527,7 +1521,7 @@ sub pic_pwm_duty_set(ratio as word, pwm_channel as byte, channel as byte)
ratio=1000
end if

i2c.num=channel
si2c_get(channel)
s=pic_reg_read(PIC_CCPXCON,1)
rCCPxN(pwm_channel)=asc(s)

Expand Down Expand Up @@ -1581,7 +1575,7 @@ sub pic_pwm_duty(ratio0 as word, ratio1 as word, ratio2 as word, channel as byte
if ratio2>1000 then
ratio2=1000
end if
i2c.num=channel
si2c_get(channel)
s=pic_reg_read(PIC_CCP1CON,1)
rCCPxN(0)=asc(s)
s=pic_reg_read(PIC_CCP2CON,1)
Expand Down Expand Up @@ -1651,7 +1645,7 @@ sub pic_ser_config(br as pic_brate_enum, par as pic_parity_mode_enum, irq_enable
exit sub
end if

i2c.num=channel
si2c_get(channel)

if pic_uart_flag and &h80 then
#if PIC_DEBUG_PRINT=1
Expand Down Expand Up @@ -1844,7 +1838,7 @@ sub pic_ser_setsend(data as string, channel as byte)
exit sub
end if

i2c.num=channel
si2c_get(channel)
pic_reg_write(TX_BUFF_ADDR,data)
pic_reg_write(TX_BYTE_COUNT,chr(len(data)))
end sub
Expand Down Expand Up @@ -1900,7 +1894,7 @@ function pic_ser_get(channel as byte)as string
exit function
end if
dim rec as byte
i2c.num=channel
si2c_get(channel)

keep_check:
s=pic_reg_read(RX_BYTE_COUNT,1)
Expand All @@ -1917,7 +1911,7 @@ function pic_ser_txlen(channel as byte)as byte
'Returns the number of bytes currently waiting in the TX buffer. The buffer is maintained by the GRA firmware and can fit up to 80 bytes.
const TX_BYTE_COUNT=&h77
dim s as string
i2c.num=channel
si2c_get(channel)
s=pic_reg_read(TX_BYTE_COUNT,1)
pic_ser_txlen=asc(s)
end function
Expand All @@ -1927,7 +1921,7 @@ function pic_ser_rxlen(channel as byte)as byte

const RX_BYTE_COUNT=&h76
dim s as string
i2c.num=channel
si2c_get(channel)
s=pic_reg_read(RX_BYTE_COUNT,1)
pic_ser_rxlen=asc(s)
end function
Expand Down
46 changes: 21 additions & 25 deletions pic/tbt_pic_gra.tbs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ function tbt_pic_gra_init(byref signature as string, byref tbt as tbt_pic_gra, u
tbt_pic_gra_init=OK
tbt.pic_init_flag=PIC_INIT_SIGNATURE
tbt.i2c_num_pic=si2c_register(signature,tbt.PIC_DATA_PIN,tbt.PIC_CLK_PIN,use_ssi)
i2c.num = tbt.i2c_num_pic
i2c.sdamap=tbt.PIC_DATA_PIN
i2c.sclmap=tbt.PIC_CLK_PIN
i2c.enabled=YES

i2c.num=tbt.i2c_num_pic
si2c_get(tbt.i2c_num_pic)

'7.RXDTSEL 0 = RX/DT function is on RC5
'6.SDOSEL 0 = SDO function is on RC2
Expand Down Expand Up @@ -219,25 +215,25 @@ function tbt_pic_gra_reg_read(addr as word,length as byte)as string
exit function
end if

i2c.start()
si2c_start()

i2c.write(PIC_CHIP_ADDR_W)
i2c.write(PIC_CMD_R)
si2c_write(PIC_CHIP_ADDR_W)
si2c_write(PIC_CMD_R)

tmp=addr / 256
i2c.write(tmp)
si2c_write(tmp)
tmp=addr or 256
i2c.write(tmp)
si2c_write(tmp)

i2c.stop()
i2c.start()
i2c.write(PIC_CHIP_ADDR_R)
si2c_stop()
si2c_start()
si2c_write(PIC_CHIP_ADDR_R)
for tmp=1 to length-1
data=data+chr(i2c.read(ACK))
data=data+chr(si2c_read(ACK))
next tmp
data=data+chr(i2c.read(NACK))
data=data+chr(si2c_read(NACK))
tbt_pic_gra_reg_read=data
i2c.stop()
si2c_stop()
end function


Expand All @@ -252,23 +248,23 @@ function tbt_pic_gra_reg_write(addr as word, data as string)as ok_ng
tbt_pic_gra_reg_write=NG
if data="" then exit function

i2c.start()
si2c_start()

i2c.write(PIC_CHIP_ADDR_W)
i2c.write(PIC_CMD_W)
si2c_write(PIC_CHIP_ADDR_W)
si2c_write(PIC_CMD_W)

tmp=addr/256
i2c.write(tmp)
si2c_write(tmp)
tmp=addr or 256
i2c.write(tmp)
si2c_write(tmp)

buf=""
for pos=1 to len(data)
buf=mid(data,pos,1)
i2c.write(asc(buf))
si2c_write(asc(buf))
next pos

i2c.stop()
si2c_stop()

tbt_pic_gra_reg_write=OK
end function
Expand Down Expand Up @@ -641,7 +637,7 @@ sub tbt_pic_gra_config(byref tbt as tbt_pic_gra, num as tbt_pic_gra_pin_enum, f
exit sub
end if

i2c.num=tbt.i2c_num_pic
si2c_get(tbt.i2c_num_pic)

if tbt.pic_func(num)<>f then
if tbt.pic_func(num)=PIC_FUNC_PWM then
Expand Down Expand Up @@ -733,7 +729,7 @@ sub tbt_pic_gra_pwm_config_simple(byref tbt as tbt_pic_gra, ch as tbt_pic_gra_pw
rCCPRxL = (ccprxl shr 2)
rCCPxN = &h0C or ((ccprxl and &h03) shl 4)

i2c.num=tbt.i2c_num_pic
si2c_get(tbt.i2c_num_pic)

select case ch

Expand Down
32 changes: 21 additions & 11 deletions super_i2c/super_i2c.tbs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ register_normal_slot:
end if

si2c_in_use(f)=YES
si2c_sda(f)=sda
si2c_scl(f)=scl
si2c_user_signature(f)=signature
si2c_register=f
#IF I2C_AVAILABLE
Expand All @@ -84,16 +86,16 @@ register_normal_slot:
i2c.sclmap=scl
i2c.enabled=YES
#ELSE
si2c_sda(f)=sda
si2c_scl(f)=scl
si2c_register=f

io.num=scl
io.state=HIGH
io.enabled=YES
io.num=sda
io.state=HIGH
io.enabled=NO
ssi.channel=channel
ssi.enabled=NO
ssi.baudrate=PL_SSI_BAUD_100kHz
ssi.clkmap=pin_clk
ssi.dimap=pin_data
ssi.domap=pin_data
ssi.direction=PL_SSI_DIRECTION_LEFT
ssi.zmode=PL_SSI_ZMODE_ENABLED_ON_ZERO
ssi.mode=PL_SSI_MODE_0
ssi.enabled=YES
#ENDIF

#if SI2C_DEBUG_PRINT
Expand Down Expand Up @@ -176,7 +178,7 @@ end sub

sub si2c_stop()
#IF I2C_AVAILABLE
i2c.stop()
si2c_stop()
#ELSE
io.num=si2c_sda(si2c_num)
io.enabled=YES
Expand Down Expand Up @@ -303,11 +305,19 @@ sub si2c_init()
end sub

function si2c_is_busy(num as byte)as no_yes
#IF I2C_AVAILABLE
if i2c.state <> I2C_IDLE
si2c_is_busy=YES
else
si2c_is_busy=NO
end if
#ELSE
if io.lineget(si2c_scl(num))=LOW then
si2c_is_busy=YES
else
si2c_is_busy=NO
end if
#ENDIF
end function

#if SI2C_DEBUG_PRINT
Expand Down
Loading

0 comments on commit 59c0c5c

Please sign in to comment.