Skip to content

Commit 7962759

Browse files
committed
Create sample main to present library capabilities
1 parent fd721ea commit 7962759

File tree

11 files changed

+178
-6
lines changed

11 files changed

+178
-6
lines changed

slim-fat-sln/slim-fat-lib/slim-fat-lib.cproj

+4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@
132132
<SubType>compile</SubType>
133133
<Link>slimfat\fileio\fileio.h</Link>
134134
</Compile>
135+
<Compile Include="..\..\src\slimfat\slimfat.h">
136+
<SubType>compile</SubType>
137+
<Link>slimfat\slimfat.h</Link>
138+
</Compile>
135139
<Compile Include="..\..\src\slimfat\slimfaterr.h">
136140
<SubType>compile</SubType>
137141
<Link>slimfat\slimfaterr.h</Link>

slim-fat-sln/slim-fat-runner/main.c

+66-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,72 @@
77

88
#include <avr/io.h>
99

10+
// Device SPI driver
11+
#include "spi/spi_master.h"
1012

11-
int main(void)
12-
{
13-
/* Replace with your application code */
14-
while (1)
15-
{
16-
}
13+
// Library supplied SD card driver
14+
#include "sd-driver/sd_driver.h"
15+
16+
// SlimFAT file system driver
17+
#include "slimfat/slimfat.h"
18+
19+
// Device buffer
20+
uint8_t buffer[SECTOR_SIZE] = { 0 };
21+
22+
int main(void) {
23+
spi_master_init();
24+
25+
// Create handle for SD card connected to SPI
26+
sd_card_t sd_card = GET_SD_HANDLE(spi_master_transfer, spi_slave_sd_select);
27+
28+
// Create handle for generic storage device with sector buffer
29+
fs_storage_device storage_dev = GET_DEV_HANDLE(buffer, &sd_card, sd_card_read, sd_card_write);
30+
31+
// Create handle for partition to be mounted
32+
fs_partition_t partition = GET_PART_HANDLE(storage_dev);
33+
34+
// Initialize SD card
35+
sd_card_err err = sd_card_init(&sd_card);
36+
if(SD_SUCCESS == err) {
37+
// Mount selected partition (0 - 3)
38+
if (FS_SUCCESS == fs_mount(&partition, 0)) {
39+
uint8_t line_buff[100] = { 0 };
40+
41+
/* Read existing files on mounted partition */
42+
fs_file_t read_file = GET_FILE_HANDLE(partition);
43+
if (FS_SUCCESS == fs_fopen(&read_file, "read.txt", READ)) {
44+
/* Access data stored in file */
45+
fs_fgetc(&read_file);
46+
fs_fgets(&read_file, line_buff, sizeof(line_buff));
47+
fs_fread(&read_file, line_buff, 100);
48+
49+
/* Navigate file */
50+
fs_fseek(&read_file, 300, FS_SEEK_SET);
51+
}
52+
53+
/* Write existing files or create new on mounted partition */
54+
fs_file_t write_file = GET_FILE_HANDLE(partition);
55+
if (FS_SUCCESS == fs_fopen(&write_file, "write.txt", WRITE)) {
56+
/* Store data to file */
57+
fs_fputc(&write_file, 'a');
58+
fs_fputs(&write_file, "Hello, world!");
59+
fs_fflush(&write_file);
60+
fs_fwrite(&write_file, line_buff, 100);
61+
62+
/* Close file to make sure data is flushed */
63+
fs_fclose(&write_file);
64+
}
65+
66+
/* Append existing file on mounted partition */
67+
fs_file_t append_file = GET_FILE_HANDLE(partition);
68+
if (FS_SUCCESS == fs_fopen(&append_file, "append.txt", APPEND)) {
69+
/* Store data to file */
70+
fs_fputs(&append_file, "This is simple append.");
71+
fs_fclose(&append_file);
72+
}
73+
74+
}
75+
}
76+
while (1);
1777
}
1878

slim-fat-sln/slim-fat-runner/slim-fat-runner.cproj

+15
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<avrgcc.compiler.directories.IncludePaths>
8787
<ListValues>
8888
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\include\</Value>
89+
<Value>../../../src</Value>
8990
</ListValues>
9091
</avrgcc.compiler.directories.IncludePaths>
9192
<avrgcc.compiler.optimization.level>Optimize debugging experience (-Og)</avrgcc.compiler.optimization.level>
@@ -111,13 +112,27 @@
111112
<Compile Include="main.c">
112113
<SubType>compile</SubType>
113114
</Compile>
115+
<Compile Include="spi\spi_master.c">
116+
<SubType>compile</SubType>
117+
</Compile>
118+
<Compile Include="spi\spi_master.h">
119+
<SubType>compile</SubType>
120+
</Compile>
114121
</ItemGroup>
115122
<ItemGroup>
116123
<ProjectReference Include="..\sd-card-driver\sd-card-driver.cproj">
117124
<Name>sd-card-driver</Name>
118125
<Project>{00eb8831-c401-4c63-a56d-ed2e89fd5742}</Project>
119126
<Private>True</Private>
120127
</ProjectReference>
128+
<ProjectReference Include="..\slim-fat-lib\slim-fat-lib.cproj">
129+
<Name>slim-fat-lib</Name>
130+
<Project>{463e6b59-0cca-49b2-9e5d-773df16c2cd5}</Project>
131+
<Private>True</Private>
132+
</ProjectReference>
133+
</ItemGroup>
134+
<ItemGroup>
135+
<Folder Include="spi" />
121136
</ItemGroup>
122137
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
123138
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "spi_master.h"
2+
3+
void spi_master_init(void) {
4+
// SPI hardware initialization
5+
DDRB |= (1<<PORTB3)|(1<<PORTB5)|(1<<PORTB2);
6+
PORTB |= (1<<PORTB2); // set SS high
7+
SPCR |= (1<<SPE)|(1<<MSTR);
8+
SPSR |= (1<<SPI2X);
9+
10+
// Slave Select lines initialization
11+
DDRB |= (1<<PORTB1);
12+
PORTB |= (1<<PORTB1);
13+
}
14+
15+
uint8_t spi_master_transfer(uint8_t byte) {
16+
SPDR = byte;
17+
while(!(SPSR & (1<<SPIF)));
18+
return SPDR;
19+
}
20+
21+
void spi_slave_sd_select(uint8_t state) {
22+
if(state) PORTB |= (1<<PORTB1);
23+
else PORTB &= ~(1<<PORTB1);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* spi_master.h
3+
*
4+
* Created: 02.03.2021 14:14:23
5+
* Author: Micha³ Granda
6+
*/
7+
8+
9+
#ifndef SPI_MASTER_H_
10+
#define SPI_MASTER_H_
11+
12+
#include <avr/io.h>
13+
#include <inttypes.h>
14+
15+
/* SPI master module setup */
16+
void spi_master_init(void);
17+
uint8_t spi_master_transfer(uint8_t byte);
18+
19+
/* Slave select lines management*/
20+
void spi_slave_sd_select(uint8_t state);
21+
22+
23+
24+
#endif /* SPI_MASTER_H_ */

src/sd-driver/sd_driver.h

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ typedef struct {
5050
void (*spi_chip_select)(uint8_t);
5151
} sd_card_t;
5252

53+
#define GET_SD_HANDLE(transfer_byte, chip_select) {.spi_transfer_byte = transfer_byte, .spi_chip_select = chip_select}
54+
5355
sd_card_err sd_card_init(sd_card_t* sd);
5456
sd_card_err sd_card_read(sd_card_t* sd, const uint32_t sector, uint8_t* buffer);
5557
sd_card_err sd_card_write(sd_card_t* sd, const uint32_t sector, const uint8_t* buffer);

src/slimfat/fat32/fat32.h

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ typedef struct fat32_entry {
3535
uint16_t root_dir_offset;
3636
} fat_entry_t;
3737

38+
#define GET_PART_HANDLE(dev) {.device = &dev}
39+
3840
/* Partition operation */
3941
fs_error fat32_mount_partition(fs_partition_t* partition, const uint32_t start_sector);
4042

src/slimfat/fileio/fileio.c

+20
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ fs_error fs_fopen(fs_file_t* file, const char* file_name, const fs_mode mode) {
112112
return err;
113113
}
114114

115+
fs_error fs_fclose(fs_file_t* file) {
116+
uint8_t err = FS_SUCCESS;
117+
118+
if (READ != file->mode) {
119+
err = fat32_update_entry(file->partition, &file->entry);
120+
}
121+
122+
return err;
123+
}
124+
125+
fs_error fs_fflush(fs_file_t* file) {
126+
uint8_t err = FS_SUCCESS;
127+
128+
if (READ != file->mode) {
129+
err = fat32_update_entry(file->partition, &file->entry);
130+
}
131+
132+
return err;
133+
}
134+
115135
uint16_t fs_fread(fs_file_t* file, uint8_t* ptr, const uint16_t count) {
116136
uint8_t err = FS_SUCCESS;
117137

src/slimfat/fileio/fileio.h

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ typedef struct fs_generic_file {
4343
uint32_t current_offset;
4444
} fs_file_t;
4545

46+
#define GET_FILE_HANDLE(part) {.partition = &part}
47+
4648
/* Partition operations */
4749
fs_error fs_mount(fs_partition_t* partition, const uint8_t partition_number);
4850

src/slimfat/slimfat.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* slimfat.h
3+
*
4+
* Created: 24.09.2021 14:12:04
5+
* Author : Micha³ Granda
6+
*/
7+
8+
9+
#ifndef SLIMFAT_H_
10+
#define SLIMFAT_H_
11+
12+
#include "fileio/fileio.h"
13+
14+
15+
16+
17+
#endif /* SLIMFAT_H_ */

src/slimfat/storage/storage.h

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ typedef struct {
2828
uint8_t(*write_sector)(void*, const uint32_t, const uint8_t*);
2929
} fs_storage_device;
3030

31+
#define GET_DEV_HANDLE(buff, dev, read, write) {.disk = dev, .status = 0, .buffer = buff, .read_sector = read, .write_sector = write }
32+
3133
fs_error find_partition(fs_storage_device* device, const uint8_t partition_number, uint32_t* sector);
3234
fs_error read_buffered_sector(fs_storage_device* device, const uint32_t sector);
3335
fs_error write_buffered_sector(fs_storage_device* device, const uint32_t sector);

0 commit comments

Comments
 (0)