-
SDFS on 18F46K22
Greetings all, i hope everyone is well...
I havn't posted here in a long time, i've not hit problems i couldn't solve till now... :mad:
First time playing with SPI, so, i may have a noob mistake...
Ok, would love to read and write files to SD using 18F46K22 @ 64MHz so since the 46K22 is 'almost' pin compatible with the 4550 i thaught i'd start with what already works befor writing my own.
I renames SDFS to SDFS16 because i have the fat32 version to which is called SDFS32... just so their was no confusion...
I'm aware the 46K22 has two hardware SPI ports, the necessary register changes have been made.
Here is my code with the include.
Code:
@ __config _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_ON_1H & _PRICLKEN_ON_1H & _FCMEN_ON_1H & _IESO_ON_1H
@ __config _CONFIG2L, _PWRTEN_ON_2L & _BOREN_OFF_2L & _BORV_220_2L
@ __config _CONFIG2H, _WDTEN_ON_2H & _WDTPS_32768_2H
@ __config _CONFIG3H, _CCP2MX_PORTC1_3H & _PBADEN_OFF_3H & _CCP3MX_PORTE0_3H & _HFOFST_OFF_3H & _MCLRE_INTMCLR_3H
@ __config _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
@ __config _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
@ __config _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __config _CONFIG6L, _WRT0_OFF_6L
@ __config _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
@ __config _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
@ __config _CONFIG7H, _EBTRB_OFF_7H
OSCCON = %01110000
OSCTUNE.6 = 1 ;PLL enable
ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000
TRISB = 255
TRISC = 0
PORTC = 0
TRISD = 0
PORTD = 0
TRISE = 0
PORTE = 0
TRISA = 0
WPUB = 255
IOCB = 255
Define OSC 64 ' Set clock speed
;SDFS defines
' Alias PIC pins and registers
SD_WE Var PORTA.4 ' SD card write protect
SD_WE_TRIS Var TRISA.4 ' SD card write protect direction
SD_WE_used var bit 'flag to determine if WE is used or not
SD_WE_used = 0 'SD_WE not used
SDI Var PORTC.4 ' SPI data in
SDI_TRIS Var TRISC.4 ' SPI data in direction
SCL Var PORTC.3 ' SPI clock
SCL_TRIS Var TRISC.3 ' SPI clock direction
SD_CS Var PORTA.5 ' SD card chip select
SD_CS_TRIS Var TRISA.5 ' SD card chip select direction
SD_CD Var PORTB.4 ' SD card detect
SD_CD_TRIS Var TRISB.4 ' SD card detect direction
SD_CD_used Var Bit 'flag to determine if CD is used or not
SD_CD_used = 0 'SD_CD not used
SDO Var PORTC.5 ' SPI data out
SDO_TRIS Var TRISC.5 ' SPI data out direction
SSPEN Var SSP1CON1.5 ' SSP enable
WCOL Var SSP1CON1.7 ' SSP write collision
BF Var SSP1STAT.0 ' SSP buffer full
SSPIF Var PIR1.3 ' SSP interrupt flag
PAUSE 100
INCLUDE "SDFS16/sdfs.pbp"
SDC_UseHardSPI = TRUE
;DEFINE NO_SOFTWARE_SPI
DEFINE LCD_DREG PORTD ' LCD data port
DEFINE LCD_DBIT 0 ' LCD data starting bit 0 or 4
DEFINE LCD_RSREG PORTE ' LCD register select port
DEFINE LCD_RSBIT 0 ' LCD register select bit
DEFINE LCD_EREG PORTE ' LCD enable port
DEFINE LCD_EBIT 2 ' LCD enable bit
DEFINE LCD_RWREG PORTE ' LCD read/write port
DEFINE LCD_RWBIT 1 ' LCD read/write bit
DEFINE LCD_BITS 8 ' LCD bus size 4 or 8 data bits
DEFINE LCD_LINES 2 ' Number lines on LCD
DEFINE LCD_COMMANDUS 2000 ' LCD Command delay time in us
DEFINE LCD_DATAUS 50 ' LCD Data delay time in us
LCDOUT $FE, 1, "FSINIT"
PAUSE 1000
:MAIN
GOSUB FSINIT
IF FAT_ERROR > 0 THEN
LCDOUT $FE, $80, "FSINIT ERROR"
LCDOUT $FE, $C0, DEC FAT_ERROR, " ", Dec SDC_status, " ", Dec SDC_response," "
ELSE
LCDOUT $FE, $80, "FSINIT OK "
LCDOUT $FE, $C0, DEC FAT_ERROR, " ", Dec SDC_status, " ", Dec SDC_response," "
ENDIF
PAUSE 100
GOTO MAIN
So, how do i have the hardware, first off i'm using plug board...
Two SD cards are old ones, i plugged them into my PC and formatted them to FAT16 before i soldered on some sip pins. (stop cringing)
Pic and LCD running from 3.3V so connections are direct...
Pull ups on SDI, SDO and CLK lines... (no huggies)
For diagnostics i placed some LEDs inline with the pull up 1k resistors so i could see they flash. this includes the CS line...
On the LCD it said FSINIT ERROR then 2 6 0
so thats FAT_ERROR = 2, SDC_STATUS = 6 and SDC_RESPONSE = 0
In the SDFS file, SDC_STATUS 6 is labeled as initialisation error which isn't helpfull since it was doing the initialisation subroutine when it errored... SDC_RESPONSE i have no idea what this refers to, is it the status byte returned by the card?
If so, i swapped SDO and SDI around between straight and crossover just incase and same symptoms...
So, I'd appreciate any help as both cards do the same.
Thanks (bangs head on wall)
-
Re: SDFS on 18F46K22
ok, just for fun, i set the tris register manually i now get a different error...
FAT_ERROR = 6, SDC_STATUS = 1 and SDC_RESPONSE = 255
EDIT:
I can also confirm i have it wired correctly fo MOSI / MISO
MISO led is flashing as well as CLK and CS. but MOSI is not which tells me the pin from the chip is high.... ????
-
Re: SDFS on 18F46K22
Quote:
SDC_UseHardSPI = TRUE
Change that to FALSE to test if it is a SPI problem. If it work then you can probably guess the problem is outside of the PIC, connections, bad SD card...
-
Re: SDFS on 18F46K22
hardSPI to false works it reports OK...
so, am i right in thinking it's the configuration of the hardware SPI on the 46K22?
I'm going to grab a 4550, and copy the test program and set it for the 4550 to see if it's a register issue.
Any more help appreciated.
-
Re: SDFS on 18F46K22
hardSPI to false works it reports OK...
so, am i right in thinking it's the configuration of the hardware SPI on the 46K22?
I'm going to grab a 4550, and copy the test program and set it for the 4550 to see if it's a register issue.
Any more help appreciated.
-
Re: SDFS on 18F46K22
after tinkering and remembering why i moved away from the 4550 (LCD problems)
I got the card to initialise in both software and hardware mode on the 4550...
I'm wondering if the 46K22 is too fast at 64MHZ? simple answer is to slow it down in the config...
I have noticed on the 4550 the SPI LED pullups behave completely diferently between softwar and hardware modes...
-
Re: SDFS on 18F46K22
right, i dropped the 46K22 to 8MHz and the hardwareSPI now works...
so... now we need to work out a way of slowing the SPI bus down for initialisation...
EDIT: works at 16MHz
EDIT2: works at 32MHz with PLL enabled (8MHz x 4)
-
Re: SDFS on 18F46K22
ok, i have tried everything to get the SPI to work at 64MHz...
I tried using SSP1ADD for the baud rate generator and it locks up the chip if you use a value above 170 ish...
I tried using TMR2 with it's highest per and post scalers... same effect...
I looked at the datasheet for the baud rate generator, it give a table for speeds...
It give values for OSC upto 32MHz but not 64MHz...
It looks like the 46K22 has an issue running stuff at 64MHz because so far I've had to write work arounds for ADC and HWPWM to operate at 64MHz... BUT the EUARTS seem to be ok at 64MHz so, i don't get it... did microchip not test this chip properly at 64MHz or something...
Luckily, i think i can get away with 32MHz...
thanks for all the help
-
Re: SDFS on 18F46K22
Have you tried dividing down the SPI clock? SSPxCON1 register allows you to select three different ratios.
/Henrik.
-
Re: SDFS on 18F46K22
yes, it was already set to divide by 64... tried setting it to use the ADDress register, and selecting TMR2...
According to the LED's theirs nothing coming out of the SPI ports at 64MHz, it's like it doesn't even initialised the ports at all..