Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <p18cxxx.h>
- #include "mySPI.h"
- #include "mmc.h"
- #include <usart.h> //pour la phase de debuggage
- unsigned int MMC_Command( unsigned char cmd, unsigned long arg, unsigned char crc) {
- unsigned char *ptr_arg = (unsigned char *)&arg + 4;
- unsigned char err = 0;
- if (cmd >= CMD_NBR) {
- return PB_FORMAT;
- }
- // NRC
- err |= SPI_W(OCTET_NRC);
- // commande
- err |= SPI_W(cmd | 0x40);
- // argument
- err |= SPI_W(*(--ptr_arg));
- err |= SPI_W(*(--ptr_arg));
- err |= SPI_W(*(--ptr_arg));
- err |= SPI_W(*(ptr_arg));
- // crc + bit de stop
- err |= SPI_W(crc);
- if (err & CONFLIT_BUFFER) {
- return PB_ACCES_BUFFER;
- }
- return OK;
- }
- unsigned char MMC_R1_response(unsigned char *rep) {
- unsigned char c;
- int i;
- // 255 tentatives maximum
- for (i=0; i!=255; i++) {
- SPI_R(&c);
- // une réponse est reçue
- if (c != OCTET_NEUTRE) {
- *rep = c;
- return OK;
- }
- }
- // timeout
- *rep = OCTET_NEUTRE;
- return -1;
- }
- unsigned int MMC_Init(void) {
- unsigned char resp;
- char i;
- SPI_LowSpeed();
- PORTCbits.RC2 = 1; // CS=1
- for (i=0; i!=10; i++) {
- SPI_W(OCTET_NEUTRE);
- }
- PORTCbits.RC2 = 0; // CS=0
- // je préfère voir large comme ça au moins je suis sécu
- for (i=0; i!=3; i++) {
- SPI_W(OCTET_NEUTRE);
- }
- MMC_Command(CMD_GO_IDLE_STATE, 0, 0x95);
- MMC_R1_response(&resp);
- if (resp != R1_IDLESTATE) {
- return PB_RESPONSE;
- }
- do {
- MMC_Command(CMD_SEND_OP_COND, 0xFFFFFFFF, 0x01);
- MMC_R1_response(&resp);
- } while (resp != R1_READY);
- SPI_MiddleSpeed();
- return OK;
- }
- unsigned int MMC_WriteBlock(unsigned long adr, unsigned char *ptrBufIn) {
- unsigned char resp;
- int i;
- // commande écriture
- MMC_Command(CMD_WRITE_BLOCK, adr, CRC_VIDE);
- MMC_R1_response(&resp);
- if (resp != R1_READY) {
- return PB_RESPONSE;
- }
- // première trame indiquant l'écriture d'un bloc
- SPI_W(OCTET_NEUTRE);
- SPI_W(DT_STARTWRITESINGLEBLOCK);
- // envoi de 512 octets de données
- for (i=0; i!=0x200; i++) {
- SPI_W(ptrBufIn[i]);
- }
- // envoi du crc sur 2 octets
- SPI_W(CRC_VIDE);
- SPI_W(CRC_VIDE);
- // vérification de l'acceptation des données
- SPI_R(&resp);
- if ((resp & 0x1F) != DR_ACCEPTED) {
- return PB_RESPONSE;
- }
- // attente de la fin d'écriture
- do {
- SPI_R(&resp);
- } while (resp != OCTET_NEUTRE);
- return 0;
- }
- unsigned int MMC_ReadBlock(unsigned long adr, unsigned char *ptrBufOut) {
- unsigned char resp;
- int i;
- // commande lecture
- MMC_Command(CMD_READ_SINGLE_BLOCK, adr, CRC_VIDE);
- MMC_R1_response(&resp);
- if (resp != R1_READY) {
- return PB_RESPONSE;
- }
- // vérification de la première trame
- SPI_W(OCTET_NEUTRE);
- SPI_R(&resp);
- /*if (resp != DT_STARTREADSINGLEBLOCK) {
- return PB_DATA;
- }*/
- // bidouille
- do {
- SPI_R(&resp);
- } while (resp == OCTET_NEUTRE);
- // réception de 512 octets de données
- for (i=0; i!=0x200; i++) {
- SPI_R(&ptrBufOut[i]);
- }
- // réception du crc, on l'ignore
- SPI_W(OCTET_NEUTRE);
- SPI_W(OCTET_NEUTRE);
- // attente de la fin de lecture
- do {
- SPI_R(&resp);
- } while (resp != OCTET_NEUTRE);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement