Skip to content

Commit

Permalink
V3.2R4, corrections de bogues
Browse files Browse the repository at this point in the history
Modifier et tester commandes SPI sur carte NUCLEO-8S208RB
  • Loading branch information
Picatout committed Apr 25, 2023
1 parent 47aa941 commit ba20833
Show file tree
Hide file tree
Showing 13 changed files with 1,105 additions and 1,028 deletions.
27 changes: 27 additions & 0 deletions BASIC/spi-eeprom.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
NEW
1 SPI.EEPROM
4 ' test SPI with 25LC640 EEPROM
10 SPI.EN 1,2 ' Fspi=Fmstr/8
14 SPI.SEL 1:SPI.WR 6:SPI.SEL 0 'enable WEL bit in EEPROM
18 SPI.SEL 1:SPI.WR 5: IF (SPI.RD AND 2) :GOTO 26
22 GOTO 200
26 SPI.SEL 0
30 ? "writing 16 random values to EEPROM\n{"
34 SPI.SEL 1:SPI.WR 2,0,0
38 FOR I =0 TO 15
42 LET D=RND(256):?D;:SPI.WR D:NEXT I:?"}"
46 SPI.SEL 0
50 GOSUB 100' wait for write completed
54 SPI.SEL 1:SPI.WR 3,0,0
58 ? "\nreading back EEPROM\n{"
62 FOR I =0 TO 15:PRINT SPI.RD; :NEXT I:?"}"
66 SPI.SEL 0:
70 SPI.EN 0
74 END
98 ' wait for write completed
100 SPI.SEL 1:SPI.WR 5:LET S =SPI.RD :SPI.SEL 0
110 IF S AND 1 :GOTO 100
120 RETURN
200 PRINT "failed to enable WEL bit in EEPROM "; A
210 SPI.SEL 0 ' deselect EEPROM
220 SPI.EN 0 ' disable SPI
27 changes: 27 additions & 0 deletions BASIC/spi-test.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
NEW
1 SPI.TEST
2 REM RAM 23LC1024 write,read test
3 REM This is a 128KB SPI-RAM
4 REM Vdd=5.0 RAM Fck max is 16Mhz
6 CONST RD=3,WR=2,D.OFS=4
20 SPI.EN 1
30 REM write 16 random number at address 0
40 SPI.SEL 1
44 INPUT "ADDRESS "A
48 REM write command and address
50 SPI.WR WR,RSHIFT(A,16),RSHIFT(A,8) AND 255,A AND 255
84 ? "writing 16 bytes in 23LC1024 at address: "; A ;?"\n{";
90 FOR I=1 TO 16
100 LET D=RND(256):SPI.WR D: ? D;
110 NEXT I : SPI.SEL 0
120 ? "}"
130 ? "reading back 23LC1024\n{"
140 SPI.SEL 1
150 SPI.WR RD,RSHIFT(A,16),RSHIFT(A,8) AND 255,A AND 255
160 FOR I=1 TO 16
170 ? SPI.RD;
180 NEXT I : ? "}"
190 SPI.SEL 0
200 SPI.EN 0
300 END

122 changes: 59 additions & 63 deletions TinyBasic.asm
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ move_exit:
;-----------------------
MAJOR=3
MINOR=2
REV=3
REV=4
software: .asciz "\n\nTiny BASIC for STM8\nCopyright, Jacques Deschenes 2019,2022,2023\nversion "
board:
Expand Down Expand Up @@ -1202,15 +1202,15 @@ and_factor:
1$: cp a,#NOT_IDX
jrne 2$
cpl (NOT_OP,sp)
jra 4$
_next_token
2$: cp a,#LPAREN_IDX
jrne 3$
call condition
push a
ld a,#RPAREN_IDX
call expect
pop a
jra 8$
jra 5$
3$:
_unget_token
4$:
Expand Down Expand Up @@ -2174,30 +2174,23 @@ cmd_bit_toggle:
; addr memory address RAM|PERIPHERAL
; bit bit position {0..7}
; output:
; A:X bit value
; A:X 0|1 bit value
;--------------------------
MASK=1
ADDR=MASK+INT_SIZE
P2=1
ADDR=P2+INT_SIZE
func_bit_test:
call func_args
cp a,#2
jreq 0$
jp syntax_error
0$:
_i24_fetch 1 ; bit to test
_i24_fetch P2 ; bit to test
ld a,xl
and a,#7
call power2
ld a,xl
and a,#7
ld (1,sp),a
; create mask by shifting left
; until cnt=0
ld a,#1
1$: tnz (1,sp)
jreq 2$
sll a
dec (1,sp)
jra 1$
2$: ld (1,sp),a
_i24_fetch 4 ; address
_i24_fetch ADDR ; address
ld a,(1,sp)
and a,(x)
jreq 3$
Expand Down Expand Up @@ -4467,6 +4460,30 @@ func_log2:
9$:
ret

;---------------------------
; return power of 2
; input:
; A exponent {0..23}
; output:
; A:X 2^exp
;---------------------------
power2:
cp a,#24
jrmi 1$
ld a,#23
1$: push a
clr a
ldw x,#1
2$: tnz (1,sp)
jreq 9$
3$:
slaw x
rlc a
dec (1,sp)
jrne 3$
9$: _drop 1
ret

;-----------------------------------
; BASIC: BIT(expr)
; expr ->{0..23}
Expand All @@ -4481,22 +4498,8 @@ func_bitmask:
jp syntax_error
1$: _i24_pop
ld a,xl
ldw x,#1
tnz a
jreq 9$
cp a,#24
jrmi 2$
ld a,#23
2$: push a
clr a
3$:
slaw x
rlc a
dec (1,sp)
jrne 3$
4$:
_drop 1
9$: ret
call power2
ret

;------------------------------
; BASIC: DO
Expand Down Expand Up @@ -4785,28 +4788,30 @@ read01:

.if NUCLEO_8S208RB
;---------------------------------
; BASIC: SPI.EN clkdiv, 0|1
; BASIC: SPI.EN 0|1 [,clkdiv]
; clkdiv -> {0..7} Fspi=Fclk/2^(n+1)
; if clkdiv==-1 disable SPI
; 0|1 -> disable|enable
;---------------------------------
CLKDIV=1
ON_OFF=CLKDIV+INT_SIZE
VSIZE=2*INT_SIZE
SPI_CS_BIT=5
cmd_spi_enable:
call arg_list
cp a,#2
jreq 1$
jp syntax_error
call expect_integer
tnzw x
jreq spi_disable
_next_token
cp a,#COMMA_IDX
jreq 1$
_unget_token
clrw x
jra 2$
1$:
call expect_integer
2$:
bset CLK_PCKENR1,#CLK_PCKENR1_SPI ; enable clock signal
ld a,(ON_OFF+2,SP)
jreq spi_disable
_i24_fetch CLKDIV
ld a,#(1<<SPI_CR1_BR)
mul x,a
ld a,xl
and a,#7
ld SPI_CR1,a
; configure ~CS on PE5 (D10 on CN8) as output.
bset PE_ODR,#SPI_CS_BIT ; set ~CS high
Expand All @@ -4818,18 +4823,14 @@ cmd_spi_enable:
bset SPI_CR2,#SPI_CR2_SSI
; enable SPI
bset SPI_CR1,#SPI_CR1_SPE
_drop VSIZE
_next

spi_disable:
; wait spi idle
1$: ld a,#0x82
and a,SPI_SR
cp a,#2
jrne 1$
btjt SPI_SR,#SPI_SR_BSY,.
bres SPI_CR1,#SPI_CR1_SPE
bres CLK_PCKENR1,#CLK_PCKENR1_SPI
bres PE_DDR,#SPI_CS_BIT
_drop VSIZE
_next

spi_clear_error:
Expand Down Expand Up @@ -4860,18 +4861,16 @@ spi_rcv_byte:
; write 1 or more byte
;------------------------------
cmd_spi_write:
call expression
call condition
1$:
ld a,xl
call spi_send_byte
_next_token
cp a,#COMMA_IDX
jrne 2$
jra cmd_spi_write
2$: tnz a
jreq 3$
_unget_token
3$: _next
2$: _unget_token
_next


;-------------------------------
Expand All @@ -4883,24 +4882,21 @@ func_spi_read:
clrw x
ld xl,a
clr a
ret
ret

;------------------------------
; BASIC: SPI.SEL 0|1
; set state of ~CS line
; 0|1 deselect|select
;------------------------------
cmd_spi_select:
_next_token
cp a,#LITC_IDX
jreq 1$
jp syntax_error
1$: _get_char
call expect_integer
tnzw x
jreq cs_high
; select
bres PE_ODR,#SPI_CS_BIT
_next
cs_high:
cs_high: ; deselect
bset PE_ODR,#SPI_CS_BIT
_next
.endif
Expand Down
Binary file modified build/stm8s207k8/TinyBasic.bin
Binary file not shown.
Loading

0 comments on commit ba20833

Please sign in to comment.