main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #define F_CPU 8000000
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <stdlib.h>
  5. #include "uart.h"
  6. #include "mmc.h"
  7. #include "fat.h"
  8. //SREG defines
  9. #define S_MOSI PB3
  10. #define S_MISO PB4
  11. #define S_SCK PB5
  12. #define S_LATCH PB2
  13. //DEBUG defines
  14. #define D_LED0 PC5
  15. //SRAM defines
  16. #define R_WR PB6
  17. #define R_RD PB7
  18. #define R_DATA PORTD
  19. #define R_DIR DDRD
  20. void SPI_MasterInit(void)
  21. {
  22. /* Set MOSI and SCK output, all others input */
  23. DDRB |= ((1<<S_MOSI) | (1<<S_SCK) | (1<<S_LATCH));
  24. DDRB &= ~(1<<S_MISO);
  25. PORTB |= (1<<S_MISO);
  26. /* Enable SPI, Master*/
  27. SPCR = ((1<<SPE) | (1<<MSTR));
  28. }
  29. void SPI_MasterTransmit(unsigned char cData)
  30. {
  31. /* Start transmission */
  32. SPDR = cData;
  33. /* Wait for transmission complete */
  34. while(!(SPSR & (1<<SPIF)));
  35. }
  36. uint8_t SRAM_Read(uint32_t addr)
  37. {
  38. uint8_t byte;
  39. DDRD=0x00;
  40. PORTD=0xff;
  41. PORTB |= (1<<R_RD);
  42. PORTB |= (1<<R_WR);
  43. SPI_MasterTransmit((uint8_t)(addr>>16));
  44. SPI_MasterTransmit((uint8_t)(addr>>8));
  45. SPI_MasterTransmit((uint8_t)(addr>>0));
  46. PORTB |= (1<<S_LATCH);
  47. PORTB &= ~(1<<S_LATCH);
  48. PORTB &= ~(1<<R_RD);
  49. asm volatile ("nop");
  50. asm volatile ("nop");
  51. asm volatile ("nop");
  52. byte = PIND;
  53. PORTB |= (1<<R_RD);
  54. DDRD=0x00;
  55. PORTD=0x00;
  56. return byte;
  57. }
  58. void SRAM_Write(uint32_t addr, uint8_t data)
  59. {
  60. DDRD=0xff;
  61. PORTB |= (1<<R_RD);
  62. PORTB |= (1<<R_WR);
  63. SPI_MasterTransmit((uint8_t)(addr>>16));
  64. SPI_MasterTransmit((uint8_t)(addr>>8));
  65. SPI_MasterTransmit((uint8_t)(addr>>0));
  66. PORTB |= (1<<S_LATCH);
  67. PORTB &= ~(1<<S_LATCH);
  68. PORTB &= ~(1<<R_WR);
  69. PORTD=data;
  70. PORTB |= (1<<R_WR);
  71. DDRD=0x00;
  72. PORTD=0x00;
  73. }
  74. int main(void)
  75. {
  76. uint8_t read, buf[10], i=0;
  77. uint8_t Buffer[512];
  78. uint8_t rbuf[24];
  79. uint16_t Clustervar;
  80. uint8_t Dir_Attrib = 0;
  81. uint32_t Size = 0;
  82. DDRD=0x00;
  83. PORTD=0x00;
  84. DDRB |= ((1<<R_WR) | (1<<R_RD));
  85. PORTB |= (1<<R_RD);
  86. PORTB |= (1<<R_WR);
  87. DDRC |= (1<<D_LED0);
  88. uart_init();
  89. SPI_MasterInit();
  90. uart_puts("\n\r\n\rSPI_init!\n\r");
  91. SRAM_Write(0x00000000,0x23);
  92. SRAM_Write(0x00000001,0x42);
  93. SRAM_Write(0x00000003,0xee);
  94. for(uint8_t a=0; a<250;a++){
  95. SRAM_Write(a,0x00);
  96. }
  97. //Initialisierung der MMC/SD-Karte ANFANG:
  98. while ( mmc_init() !=0) //ist der Rückgabewert ungleich NULL ist ein Fehler aufgetreten
  99. {
  100. uart_puts("** Keine MMC/SD Karte gefunden!! **\n\r");
  101. }
  102. uart_puts("Karte gefunden!!\n\r");
  103. fat_init(Buffer); //laden Cluster OFFSET und Size
  104. //Initialisierung der MMC/SD-Karte ENDE!
  105. mmc_read_csd (Buffer);
  106. for (uint16_t tmp = 0;tmp<16;tmp++)
  107. {
  108. itoa(Buffer[tmp],buf,16);
  109. uart_puts(buf);
  110. }
  111. Clustervar = 0;//suche im Root Verzeichnis
  112. if (fat_search_file((unsigned char *)"rom.txt",&Clustervar,&Size,&Dir_Attrib,Buffer) == 1)
  113. {
  114. uart_puts("\r\n\r\n");
  115. uart_puts("rom.txt:print and write to RAM:\r\n");
  116. //Lese File und gibt es auf der seriellen Schnittstelle aus und schreibt es ins RAM
  117. for (uint8_t b = 0;b<1;b++)
  118. {
  119. fat_read_file (Clustervar,Buffer,b);
  120. for (uint8_t a = 0;a<50;a++)
  121. {
  122. SRAM_Write(0x00023420+a,Buffer[a]);
  123. uart_putc(Buffer[a]);
  124. }
  125. }
  126. }
  127. for (uint16_t b=0;b<65535;b++)
  128. {
  129. uart_puts("\r\n0x");
  130. ltoa(b*24,buf,16);
  131. uart_puts(buf);
  132. uart_putc(' ');
  133. uart_putc(' ');
  134. for(uint8_t a=0; a<24;a++)
  135. {
  136. rbuf[a]=SRAM_Read(b*24+a);
  137. }
  138. for(uint8_t a=0; a<24;a++)
  139. {
  140. itoa(rbuf[a],buf,16);
  141. uart_putc(' ');
  142. if(rbuf[a]<0x10)
  143. uart_putc('0');
  144. uart_puts(buf);
  145. }
  146. uart_puts(" | ");
  147. for(uint8_t a=0; a<24;a++)
  148. {
  149. if(0x20 <= rbuf[a] && rbuf[a] <= 0x7e)
  150. uart_putc(rbuf[a]);
  151. else
  152. uart_putc('.');
  153. }
  154. }
  155. while(1);
  156. return(0);
  157. }