Other Parts Discussed in Thread: MSP430F5529
Hello,
I am trying to interface my MSP430 Launchpad with an external SD Card for my senior design project. I was able to establish communication using Elm-Chan's FatFS module. The two main questions/problems I have is:
1. When I try using the FatFS mount method to unmount the work area, I try setting the work area to null and I get an error that identifier "null" is unidentified. I'm having trouble believing that null isn't defined. Does this mean that I have to set the work area to 0 to unmount?
2. Even in the data I have, it is set in a particular format. For example, from the original code I found, the data in the text file on the card was as follows: data (directory) (file name). I would like to know if it would be possible if I only write data to the card. I am attaching my main file for reference
/*
* SD_main.c
*
* Created on: Jan 9, 2016
* Author: rajur
*/
/*The following code is a stripped down version of a previous project, there
* may be parts of this code that are unnecessary. This example will create
* text file and write a few letters. If you have any questions or would like
* to learn more how to implement this project using more extensive commands
* please contact me at OCB456@aol.com. Thanks!
* */
#include <MSP430F5529.h>
#include "ff.h"
#include "diskio.h"
void initCLK(void);
void SetVcoreUp (unsigned int level);
FRESULT WriteFile(char*, char*, WORD);
void fat_init(void);
FIL file; /* Opened file object */
FATFS fatfs; /* File system object */
DIRS dir; /* Directory object */
DIRS dir2;
FRESULT errCode; /* Error code object */
FRESULT res; /* Result object */
UINT bytesRead; /* Bytes read object */
UINT read; /* Read bytes object */
FATFS test_for_new; // to check if a new file can be written
FIL test_file;
unsigned char MST_Data,SLV_Data;
BYTE buffer[32];
int result=1;
void main(void){
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
initCLK();
__enable_interrupt();
fat_init(); //mount, set directory to read from, assign file
unsigned int bytesWritten;
f_write(&file, "abcdefg \r\n", 32, &bytesWritten);
f_write(&file, "ok", 32, &bytesWritten);
f_close(&file);
// f_closedir(&dir);
f_mount(0,null);
/* f_mount(0,&test_for_new); // f_mount(0,0);
//new
f_open(&test_file, "test_file.txt", FA_CREATE_NEW);
f_write(&test_file, "Hi Mom", 32, &bytesWritten);
f_close(&test_file);//new*/
// f_closedir(&dir2);
// f_mount(0,&test_for_new);
while(1);
}
void fat_init(void){
errCode = -1;
while (errCode != FR_OK){ //go until f_open returns FR_OK (function successful)
errCode = f_mount(0, &fatfs); //mount drive number 0
errCode = f_opendir(&dir, "/"); //root directory
errCode = f_open(&file, "TESTE.txt", FA_CREATE_ALWAYS | FA_WRITE);
if(errCode != FR_OK)
result=0; //used as a debugging flag
}
}
void initCLK(void){
volatile unsigned int i;
// Increase Vcore setting to level3 to support fsystem=25MHz
// NOTE: Change core voltage one level at a time..
SetVcoreUp (0x01);
SetVcoreUp (0x02);
SetVcoreUp (0x03);
UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
UCSCTL1 = DCORSEL_7; // Select DCO range 50MHz operation
UCSCTL2 = FLLD_1 + 762; // Set DCO Multiplier for 25MHz
// (N + 1) * FLLRef = Fdco
// (762 + 1) * 32768 = 25MHz
// Set FLL Div = fDCOCLK/2
__bic_SR_register(SCG0); // Enable the FLL control loop
// Worst-case settling time for the DCO when the DCO range bits have been
// changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
// UG for optimization.
// 32 x 32 x 25 MHz / 32,768 Hz ~ 780k MCLK cycles for DCO to settle
__delay_cycles(782000);
// Loop until XT1,XT2 & DCO stabilizes - In this case only DCO has to stabilize
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
// Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
}
void SetVcoreUp (unsigned int level)
{
// Open PMM registers for write
PMMCTL0_H = PMMPW_H;
// Set SVS/SVM high side new level
SVSMHCTL = SVSHE + SVSHRVL0 * level + SVMHE + SVSMHRRL0 * level;
// Set SVM low side to new level
SVSMLCTL = SVSLE + SVMLE + SVSMLRRL0 * level;
// Wait till SVM is settled
while ((PMMIFG & SVSMLDLYIFG) == 0);
// Clear already set flags
PMMIFG &= ~(SVMLVLRIFG + SVMLIFG);
// Set VCore to new level
PMMCTL0_L = PMMCOREV0 * level;
// Wait till new level reached
if ((PMMIFG & SVMLIFG))
while ((PMMIFG & SVMLVLRIFG) == 0);
// Set SVS/SVM low side to new level
SVSMLCTL = SVSLE + SVSLRVL0 * level + SVMLE + SVSMLRRL0 * level;
// Lock PMM registers for write access
PMMCTL0_H = 0x00;
}
Thanks,
Ravi Raju