HP85 GPIB Disk Emulator  1.0
HP85GPIBDiskEmulator
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mmc_hal.c
Go to the documentation of this file.
1 
27 #include "user_config.h"
28 
29 #ifdef AVR
30 #include <stdlib.h>
31 #endif
32 
33 #include "mathio.h"
34 
35 #include "time.h"
36 #include "timer.h"
37 
38 #ifdef ESP8266
39 #include "hspi.h"
40 #endif
41 
42 #include "fatfs.h"
43 
44 extern DSTATUS Stat;
45 
47 uint16_t _mmc_timeout = 0;
48 
50 uint32_t _mmc_clock = 0;
51 
52 // @brief MMC media status prescaler
53 uint16_t _mmc_pre = 0;
54 
59 static void mmc_task(void)
60 {
61  if(_mmc_timeout)
62  _mmc_timeout--;
63 
64 // 100HZ
65  if(_mmc_pre++ < 10)
66  return;
67 
68  _mmc_pre = 0;
69 
70  if(!mmc_ins_status())
71  set_error(1);
72 
74 }
75 
76 
82 void mmc_install_timer( void )
83 {
84  _mmc_timeout = 0;
85  if(set_timers(mmc_task,1) == -1)
86  printf("MMC Clock task init failed\n");
87 }
88 
89 
93 {
94  chip_select_init(MMC_CS);
95  mmc_slow();
96 }
97 
98 
102 {
103  spi_begin(_mmc_clock,MMC_CS);
104 }
105 
106 
110 {
111  spi_end(MMC_CS);
112 }
113 
114 
119 void mmc_slow()
120 {
122 }
123 
124 
129 void mmc_fast()
130 {
132 }
133 
134 
139 void mmc_spi_TX_buffer(const uint8_t *data, int count)
140 {
141  spi_TX_buffer((uint8_t *) data,count);
142 }
143 
144 
149 void mmc_spi_RX_buffer(const uint8_t *data, int count)
150 {
151  spi_RX_buffer((uint8_t *) data,count);
152 }
153 
154 
158 uint8_t mmc_spi_TXRX(uint8_t data)
159 {
160  return( spi_TXRX(data) );
161 }
162 
163 
168 MEMSPACE
169 void mmc_set_ms_timeout(uint16_t ms)
170 {
171  mmc_cli();
172  _mmc_timeout = ms;
173  mmc_sei();
174 }
175 
176 
180 MEMSPACE
182 {
183 
184  if( Stat & STA_NODISK )
185  return(1);
186 
187 #ifdef ESP8266
188  optimistic_yield(1000);
189  wdt_reset();
190 #endif
191  if(!_mmc_timeout)
192  {
193  printf("MMC TIMEOUT\n");
194  Stat |= (STA_NODISK | STA_NOINIT);
195  return(1);
196  }
197  return(0);
198 }
199 
200 
203 MEMSPACE
204 void mmc_ms_wait(int ms)
205 {
206  mmc_set_ms_timeout(ms);
207  while(!mmc_test_timeout())
208  ;
209 }
210 
211 
213 static int mmc_init_flag = 0;
218 MEMSPACE
219 int mmc_init(int verbose)
220 {
221  int rc;
222 
223  char buff[MAX_NAME_LEN+1];
224 
225  Stat = 0;
226  clear_error();
227 
228  // Card Detect
229  GPIO_PIN_LATCH_LOW(GPIO_C7);
230  GPIO_PIN_DIR_IN(GPIO_C7);
231 
232  mmc_spi_init();
233 
234  if( verbose)
235  {
236  printf("START MMC INIT\n");
237  }
238 // we only install timers once!
239  if(!mmc_init_flag)
241 
242  if(!mmc_ins_status())
243  {
244  printf("*** MMC Card NOT Inserted! ***\n");
245  return(RES_NOTRDY);
246  }
247 
248 
249  if( verbose)
250  {
251  printf("MMC Card Inserted\n");
252 
253 #if defined (FF_USE_LFN)
254  printf("LFN Enabled");
255 #else
256  printf("LFN Disabled");
257 #endif
258  printf(", Code page: %u\n", FF_CODE_PAGE);
259  }
260 
261  rc = disk_initialize(DEV_MMC); // aliased to mmc_disk_initialize()
262 
263  if( rc != RES_OK )
264  put_rc(rc);
265 
266  if( rc == RES_OK)
267  rc = f_mount(&Fatfs[0],"/", 0);
268 
269  if( rc != RES_OK || verbose)
270  put_rc( rc );
271 
272  if (verbose )
273  {
274  DWORD blksize = 0;
275  if(rc == RES_OK)
276  {
277  rc = disk_ioctl ( DEV_MMC, GET_BLOCK_SIZE, (void *) &blksize);
278  if( rc != RES_OK)
279  {
280  put_rc( rc );
281  printf("MMC Block Size - read failed\n");
282  }
283  else
284  {
285  printf("MMC Block Size: %ld\n", blksize);
286  }
287  if( rc == RES_OK)
288  {
289  strcpy(buff,"/");
290  fatfs_status(buff);
291  }
292  }
293  printf("END MMC INIT\n");
294  }
295  mmc_init_flag = 1;
296 
297  if(rc != RES_OK)
298  set_error(1);
299 
300  return( rc ) ;
301 }
302 
303 
307 MEMSPACE
309 {
310 }
311 
312 
316 MEMSPACE
318 {
319 }
320 
321 
326 
327 MEMSPACE
329 {
330  // HI when card is NOT inserted
331  if( GPIO_PIN_RD(GPIO_C7) )
332  return (0);
333  else
334  return(1);;
335 }
336 
337 
342 
343 MEMSPACE
345 {
346  return (0);
347 }
mmc_init_flag
static int mmc_init_flag
has the MMC interface been initialized yet ?
Definition: mmc_hal.c:213
fatfs.h
MAX_NAME_LEN
#define MAX_NAME_LEN
Definition: posix.h:133
mmc_spi_begin
void mmc_spi_begin()
MMC SPI setup and chip select.
Definition: mmc_hal.c:101
printf
MEMSPACE int printf(const char *format,...)
spi_TXRX
uint8_t spi_TXRX(uint8_t data)
SPI read and write 1 byte.
Definition: hal.c:491
DSTATUS
BYTE DSTATUS
Definition: diskio.h:26
mmc_sei
#define mmc_sei()
Definition: mmc_hal.h:35
mmc_install_timer
MEMSPACE void mmc_install_timer(void)
Install MMC timer task: mmc_task()
Definition: mmc_hal.c:82
mmc_spi_end
void mmc_spi_end()
MMC SPI end and chip deselect.
Definition: mmc_hal.c:109
MEMSPACE
#define MEMSPACE
Definition: user_config.h:17
_mmc_clock
uint32_t _mmc_clock
MMC SPI CLOCK cache.
Definition: mmc_hal.c:50
GET_BLOCK_SIZE
#define GET_BLOCK_SIZE
Definition: diskio.h:70
mmc_disk_timerproc
void mmc_disk_timerproc(void)
Write Sector(s)
Definition: mmc.c:761
_mmc_pre
uint16_t _mmc_pre
Definition: mmc_hal.c:53
MMC_SLOW
#define MMC_SLOW
Definition: user_config.h:26
FF_CODE_PAGE
#define FF_CODE_PAGE
Definition: ffconf.h:71
GPIO_C7
#define GPIO_C7
Definition: gpio-1284p.h:37
mmc_wp_status
MEMSPACE int mmc_wp_status()
MMC Card Write Protect status.
Definition: mmc_hal.c:344
spi_TX_buffer
void spi_TX_buffer(const uint8_t *data, int count)
SPI write buffer.
Definition: hal.c:418
_mmc_timeout
uint16_t _mmc_timeout
MMC timeout counter in ms.
Definition: mmc_hal.c:47
strcpy
MEMSPACE WEAK_ATR char * strcpy(char *dest, const char *src)
copy a string
Definition: stringsup.c:160
disk_initialize
DSTATUS disk_initialize(BYTE pdrv)
Definition: diskio.c:78
mmc_slow
void mmc_slow()
MMC set slow SPI bus speed Only called when deselected.
Definition: mmc_hal.c:119
put_rc
MEMSPACE void put_rc(int rc)
display FatFs return code as ascii string
Definition: fatfs_sup.c:145
timer.h
timer functions
STA_NODISK
#define STA_NODISK
Definition: diskio.h:61
time.h
Common Linux/POSIX time functions.
mmc_power_on
MEMSPACE void mmc_power_on()
MMC Power ON.
Definition: mmc_hal.c:308
mmc_spi_init
void mmc_spi_init()
MMC SPI setup and chip select.
Definition: mmc_hal.c:92
clear_error
void clear_error()
Set error condition.
Definition: hal.c:513
mmc_spi_TX_buffer
void mmc_spi_TX_buffer(const uint8_t *data, int count)
SPI write buffer.
Definition: mmc_hal.c:139
set_error
void set_error(uint8_t error)
Set error condition.
Definition: hal.c:505
fatfs_status
MEMSPACE int fatfs_status(char *name)
Compute space used, number of directories and files contained used by a drive.
Definition: fatfs_sup.c:259
Stat
DSTATUS Stat
Definition: mmc.c:78
mmc_fast
void mmc_fast()
MMC fast SPI bus speed Only called when deselected.
Definition: mmc_hal.c:129
MMC_FAST
#define MMC_FAST
Definition: user_config.h:30
mmc_ins_status
MEMSPACE int mmc_ins_status()
MMC Card Inserted status.
Definition: mmc_hal.c:328
STA_NOINIT
#define STA_NOINIT
Definition: diskio.h:60
mmc_init
MEMSPACE int mmc_init(int verbose)
Initialize MMC and FatFs interface, display diagnostics.
Definition: mmc_hal.c:219
mmc_task
static void mmc_task(void)
1000HZ timer task
Definition: mmc_hal.c:59
chip_select_init
void chip_select_init(uint8_t pin)
CHIP select HAL.
Definition: hal.c:194
mmc_power_off
MEMSPACE void mmc_power_off()
MMC Power OFF.
Definition: mmc_hal.c:317
disk_ioctl
DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
Definition: diskio.c:181
f_mount
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Definition: ff.c:3649
spi_RX_buffer
void spi_RX_buffer(const uint8_t *data, int count)
SPI read buffer.
Definition: hal.c:433
set_timers
MEMSPACE int set_timers(void(*handler)(void), int timer __attribute__((unused)))
Install a user timer task.
Definition: timer.c:59
RES_NOTRDY
@ RES_NOTRDY
Definition: diskio.h:34
DEV_MMC
#define DEV_MMC
Definition: diskio.c:38
Fatfs
FATFS Fatfs[FF_VOLUMES]
FatFs Drive Volumes.
Definition: fatfs_sup.c:42
mmc_spi_RX_buffer
void mmc_spi_RX_buffer(const uint8_t *data, int count)
SPI read buffer.
Definition: mmc_hal.c:149
mmc_spi_TXRX
uint8_t mmc_spi_TXRX(uint8_t data)
SPI read and write 1 byte.
Definition: mmc_hal.c:158
mathio.h
Math IO functions, and verious conversion code with floating point support.
DWORD
unsigned long DWORD
Definition: ff.h:56
spi_end
void spi_end(uint8_t pin)
SPI chip disable function wait for current tranaction to finish!
Definition: hal.c:391
mmc_test_timeout
MEMSPACE int mmc_test_timeout()
Wait for timeout.
Definition: mmc_hal.c:181
RES_OK
@ RES_OK
Definition: diskio.h:31
spi_begin
void spi_begin(uint32_t clock, int pin)
SPI chip enable function Function waits for current tranaction to finish before proceeding.
Definition: hal.c:357
mmc_set_ms_timeout
MEMSPACE void mmc_set_ms_timeout(uint16_t ms)
Set MMC timeout timer in Milliseconds.
Definition: mmc_hal.c:169
mmc_cli
#define mmc_cli()
Definition: mmc_hal.h:34
mmc_ms_wait
MEMSPACE void mmc_ms_wait(int ms)
Wait for time in milliseconds.
Definition: mmc_hal.c:204