Additional library for MikroPascal PRO 2009

General discussion on mikroPascal PRO for PIC.
Author
Message
yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: Additional library for MikroPascal PRO 2009

#40 Post by yo2lio » 25 Apr 2010 18:44

Zsola wrote:Hello Yo2ilo, Dany says I should ask you about SDMMC_SPI2.
My code is here: http://www.mikroe.com/forum/viewtopic.p ... 53#p124253
Can you help me, whats the problem with this?
I don't have any idea, but this is the library:

Code: Select all

unit SDMMC_SPI2;

function SDMMC_Init : boolean;
function SDMMC_ReadSector(Sector : dWord; var MMC_Buffer : array[512] of byte) : boolean;
function SDMMC_WriteSector(Sector : dWord; var MMC_Buffer : array[512] of byte) : boolean;

const   CMD0  = $40 + 0;     // GO_IDLE_STATE
        CMD1  = $40 + 1;     // SEND_OP_COND
        CMD8  = $40 + 8;     // SEND_IF_COND
        CMD9  = $40 + 9;     // SEND_CSD
        CMD10 = $40 + 10;    // SEND_CID
        CMD12 = $40 + 12;    // STOP_TRANSMISSION
        CMD16 = $40 + 16;    // SET_BLOCKLEN
        CMD17 = $40 + 17;    // READ_SINGLE_BLOCK
        CMD18 = $40 + 18;    // READ_MULTIPLE_BLOCK
        CMD23 = $40 + 23;    // SET_BLOCK_COUNT
        CMD24 = $40 + 24;    // WRITE_BLOCK
        CMD25 = $40 + 25;    // WRITE_MULTIPLE_BLOCK
        CMD41 = $40 + 41;    // SEND_OP_COND (ACMD)
        CMD55 = $40 + 55;    // APP_CMD
        CMD58 = $40 + 58;    // READ_OCR

        TimeOutLimit = 10000;             // Set timeout limit based on clock

        sdMMC    = 1;                          // Multi Media Card (MMC)
        sdSD     = 2;                          // Secure Digital Card (SD)
        sdSDHC   = 4;                          // High Capacity Secure Digital Card (SDHC)

var  SDMMC_CS : sbit; sfr; external;
     SDMMC_CS_dir : sbit; sfr; external;
     cardtype : byte;

implementation

procedure SDMMC_Write(tx_data : byte);
begin
  asm
    MOVF SSP2BUF,W
  end;
  SSP2BUF := tx_data;
  ClrWdt;
  while SSP2STAT.BF = 0 do;
  asm
    MOVF SSP2BUF,W
  end;
end;

procedure SDMMC_Read;
begin
  asm
    MOVF SSP2BUF,W
    SETF SSP2BUF
  end;
  ClrWdt;
  while SSP2STAT.BF = 0 do;
  asm
    MOVFF SSP2BUF,R18
  end;
end;

function SeekResponse(Required : Byte) : Byte;
var Index : Word;
begin
  Index := 0;
  while Index < $FFFF do
    begin
      SDMMC_Read;
      result := R18;
      if result = Required then break;
      inc(Index);
    end;
end;

function SendCmd(Cmd : Byte; Arg : dWord) : Byte;
var CRC,index : Byte;
begin
  if SeekResponse($FF) <> $FF then result := $FF // wait for ready
  else
    begin
      SDMMC_Write(Cmd);             // Command
      SDMMC_Write(Highest(Arg));    // Argument[31..24]
      SDMMC_Write(Higher(Arg));     // Argument[23..16]
      SDMMC_Write(Hi(Arg));         // Argument[15..08]
      SDMMC_Write(Lo(Arg));         // Argument[07..00]
      CRC := $FF;                                   // Default CRC byte
      if Cmd = CMD0 then CRC := $95                           // CRC for CMD0($0)
      else if Cmd = CMD8 then CRC := $87;                           // CRC for CMD8($1AA)
      SDMMC_Write(CRC);
      Index := 0;
      while Index < 100 do
        begin                                   // Receive command response
          inc(index);
          SDMMC_Read;
          result := R18;
          if (result and $80) < 2 then break;   // Wait for a valid response in timeout of 100 attempts
        end;
    end;
end;

function SDMMC_ReadSector(Sector : dWord; var MMC_Buffer : array[512] of byte) : boolean;
var TimeOut : Byte;
    Index : Word;
begin
  result := false;
  if cardtype = 0 then exit;
  TimeOut := 0;
  if (cardtype and $04) = 0 then Sector := Sector shl 9; // MMC or SD - byte addressing (SDHC use block addressing)
  while result = false do
    begin
      SDMMC_CS := 0;
      nop;
      nop;
      if SendCmd(CMD17, Sector) = $00 then      // Send Cmd 17
        if SeekResponse($FE) = $FE then         // Read start token
          begin
            Index := 0;
            FSR2 := word(@MMC_Buffer);
            while Index < 512 do                            // Read data block
              begin
                SDMMC_Read;
                POSTINC2 := R18;
                Inc(Index);
              end;
            SDMMC_Read;                        // Read dummy CRC to conclude data block
            SDMMC_Read;
            result := true;                           // Read completed successfully
          end;
      SDMMC_CS := 1;
      nop;
      nop;
      SDMMC_Read;                              // Clock SD/MMC to complete read
      if Inc(TimeOut) > 1 then break
    end;
end;

function SDMMC_WriteSector(Sector : dWord; var MMC_Buffer : array[512] of byte) : boolean;
var Response : Byte;
    Index : Word;
    TimeOut : Byte;
begin
  result := false;
  if cardtype = 0 then exit;
  TimeOut := $00;
  if (cardtype and $04) = 0 then Sector := Sector shl 9; // MMC or SD - byte addressing (SDHC use block addressing)
  while result = false do
    begin
      SDMMC_CS := 0;
      nop;
      nop;                            // Send Cmd 24
      if SeekResponse($FF) = $FF then    // Wait for ready
        if SendCmd(CMD24, Sector) = $00 then
          begin
            SDMMC_Write($FE);                // Write start token
            Index := 0;
            FSR2 := word(@MMC_Buffer);
            while Index < 512 do                       // Write data block
              begin
                SDMMC_Write(POSTINC2);
                Inc(Index);
              end;
            SDMMC_Read;                // Send dummy CRC to conclude data block
            SDMMC_Read;
            SDMMC_Read;
            Response := R18;     // Data response token
            Response := Response and $1F;
            if Response = $05 then result := true;
          end;
      SDMMC_CS := 1;
      nop;
      nop;
      SDMMC_Read;                      // Complete SPI transaction
      if Inc(TimeOut) > 10 then break;
    end;
end;

function Cmd5541(BitSet : Boolean) : Byte;
begin
  result := SendCmd(CMD55, 0);
  if result > 1 then Exit
  else
    begin
      if BitSet = true then result := SendCmd(CMD41, $40000000)
      else result := SendCmd(CMD41, 0);
    end;
end;

function SDMMC_Init_ : boolean;
var Index : Word;
    OCR : array[4] of Byte;
    tmp : string[2];
begin
  result := true;
  cardtype := 0;
  SDMMC_CS := 1;                                        // Pull CS high
  SDMMC_CS_dir := 0;
  Index := 0;
  while Index < 20 do                                        // Clock SD for min 80 cycles
    begin
      SDMMC_Read;
      inc(Index);
    end;
  SDMMC_CS := 0;  // Pull CS low
  nop;
  nop;
  if SendCmd(CMD0, 0) = $01 then                // Send Cmd 0 - enter idle state
    if SendCmd(CMD8, $01AA) = $01 then             // SDC Version 2+
      begin
        for Index := 0 To 3 do
          begin
            SDMMC_Read;
            OCR[Index] := R18;
          end;
        if (OCR[2] = $01) and (OCR[3] = $AA) then           // SD Vdd range of 2.7 - 3.6 volts
          begin
            Index := TimeOutLimit;
            while Index > 0 do
              begin
                dec(Index);
                if Cmd5541(True) = 0 then break;
              end;
            if Index > 0 then                            // Cmd 55/41 successful with HCS bit set
              if SendCmd(CMD58, 0) = 0 then     // Send Cmd 58
                begin
                  for Index := 0 To 3 do
                    begin
                      SDMMC_Read;
                      OCR[Index] := R18;
                    end;
                  // CCS[30] bit in the OCR set
                  if (OCR[0] and $40) > 0 then cardtype := sdSDHC              // Init successful - SDHC (3)
                  else cardtype := sdSD;                // Init successful - SD (2)
                end;
          end
      end
    else                                               // SD Version 1 or MMC
      begin
        if Cmd5541(False) <= 1 then
          begin
            cardtype := sdSD;                         // Init successful - SD (2)
            Index := TimeOutLimit;
            while Index > 0 do
              begin
                dec(Index);
                if Cmd5541(True) = 0 then break;
              end;
          end         // Wait for leaving idle state
        else
          begin
            cardtype := sdMMC;                        // Init successful - MMC (1)
            Index := TimeOutLimit;
            while Index > 0 do
              begin
                dec(Index);
                if SendCmd(CMD1, 0) = 0 then break;    // Wait for leaving idle state
              end;
          end;
      end;
  if Index = 0 then result := false
  else SendCmd(CMD16, $0200);
  SDMMC_CS := 1;
  nop;
  nop;
  SDMMC_Read;    // Clock SD/MMC to complete
end;

function SDMMC_Init : boolean;
var Index : byte;
begin
  Index := 0;
  while Index < 10 do
    begin
      result := SDMMC_Init_;
      if cardtype <> 0 then break;
      inc(index);
    end;
  if cardtype <> 0 then result := true;
end;

end.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

Zsola
Posts: 16
Joined: 29 Nov 2008 02:31

Re: Additional library for MikroPascal PRO 2009

#41 Post by Zsola » 25 Apr 2010 22:45

Thank you Florin, if I figure out what was the cause, I will let you know!
Anyway, you are close enough to me (I'm near Budapest).
Be well! :)

Zsola
Posts: 16
Joined: 29 Nov 2008 02:31

Re: Additional library for MikroPascal PRO 2009

#42 Post by Zsola » 30 Apr 2010 17:17

I have just figured out what was wrong! :lol:
It seems the PORT MAPPING is faulty, unless someone find difference in the uP and assembly code. Just because I not find. Someone?
The hardware config is the same, 1WayIOLOCK Set bit Disabled.

This is NOT working, the detection of the cards lasts 8-60s with 0 cardtype and any card:

Unlock_IOLOCK;
PPS_Mapping(0, _OUTPUT, _CCP1_P1A);
PPS_Mapping(1, _OUTPUT, _CCP2_P2A);
PPS_Mapping(11, _INPUT, _SDI2); //SDI2
PPS_Mapping(12, _INPUT, _SCK2IN); //SCK2IN
PPS_Mapping(17, _OUTPUT, _SDO2); //SDO2
PPS_Mapping(12, _OUTPUT, _SCK2); //SCK2
Lock_IOLOCK;

However the clock out is right, the PWM is working, and data out is OK - but no input, especially the SCLKIN i think. Just before this I disabled interrupts (maybe I should enable GIE after all?).

And this IS WORKING, the card recognition is blazingly fast (with PIC18F46J50@10MHz).

asm
movlb 0x0E //Select bank 14 for access to PPS registers
bcf INTCON, GIE //I/O Pin unlock sequence will not work if CPU services an interrupt during the sequence
movlw 0x55 //Unlock sequence consists of writing 0x55
movwf EECON2 //;and 0xAA to the EECON2 register.
movlw 0xAA
movwf EECON2
bcf PPSCON, IOLOCK // ;We may now write to RPINRx and RPORx registers
//Input SDI2, SCK2IN - RPINR21-RP11, RPINR22 /CLK:RC1(RP12), DI:RC0(RP11)
movlw 0x0B //;RP11 will be SDI2
movwf RPINR21 //;Assign the SDI2 function to pin RP11
movlw 0x0C //;SCK2 also needs to be configured as an input on the same pin
movwf RPINR22 //;SCK2 input function taken from RP12
// OUT
movlw 0x0A //;Let’s assign SCK2 output
movwf RPOR12 //;RPOR4 maps output signals to RP12
movlw 0x09 //;0x09 is SDO2 output
movwf RPOR17 //;Assign SDO2 output signal to the RP17
movlw 0x0E //PWM1
movwf RPOR0
movlw 0x12 //PWM2
movwf RPOR1
//SET IOLOCK
movlw 0x55 //Unlock sequence consists of writing 0x55
movwf EECON2 //;and 0xAA to the EECON2 register.
movlw 0xAA
movwf EECON2
bsf PPSCON, IOLOCK // ;We may now write to RPINRx and RPORx registers
bsf INTCON, GIE // ;May now turn back on interrupts if desired
movlb 0x0F //;Done with PPS registers, bank 15 has other SFRs
end;

(Don't mention about comments, Im not sure everything is correct.)

The summary of the s**king: The SD-MMC and SPI2 is not documented enough in the compilers, till now the SPI2 support and PWM2 support is missing from 18F4xJx family, I have checked in the latest beta too.
I found circuits with pullup resistors and without, the SD documentations have holes (CLK is high or low, when latch the data and is the IO lines use open collector/drain or push-pull gates!? and so on). I had to modifiy my circuit to sure SD card get 3,3V (not 3V as it got). I had to pull up all the data lines - but its good against floating.
I had to read a lot of docs, now I understand SD and MMC specs with its differences. I analyze your SD-MMC library (fine ;)), and of course write a lot in assembly.

Thank you much Yo2lio!
Greetings! :D :D :D

juanelectro
Posts: 47
Joined: 24 Oct 2009 16:01
Location: Colombia

Re: Additional library for MikroPascal PRO 2009

#43 Post by juanelectro » 06 May 2010 16:21

I need to send information by TCP from PIC to server, what is the functions sequence that I must do for reaching this objetive?

Sorry by my english

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Ethernet library changes...

#44 Post by yo2lio » 23 May 2010 12:03

Hello to all,

I want to make some changes in Ethernet library, for both, embedded and ENC28J60.

Now, Open_TCP_Connection function, search for opened connection and send SYN for both connected or unconnected socket.
I want to change this, to do not send SYN if socket is already connected, just reset to 0 the Socket.Exp_Time

I will add a new Open_TCP_Connection function, Open_TCP_Connection_S, this will make TCP connection and return the number of connected socket.

In this way, you will be able to send data via TCP, from both RAM or ROM and don't wait for transmission, just check periodically if Socket.Start_addr = Socket.Stop_addr and <> 0, this mean: transmission complete.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

szabi
Posts: 9
Joined: 23 Jun 2007 19:46

Re: Additional library for MikroPascal PRO 2009

#45 Post by szabi » 11 Jun 2010 10:43

Hello yo2ilo,
I've installed your v1.4 library pack, but in the library manager nothing appears. I have MikroPascal PRO 2.5 (also tried on v3.8 )
(The components were succesfully extracted into the uses\p18 folder by the way)
Can you please tell me what should I do?
Thanks.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: Additional library for MikroPascal PRO 2009

#46 Post by yo2lio » 11 Jun 2010 21:52

szabi wrote: I've installed your v1.4 library pack, but in the library manager nothing appears. I have MikroPascal PRO 2.5 (also tried on v3.8 )
(The components were succesfully extracted into the uses\p18 folder by the way)
Can you please tell me what should I do?
Hi,

Do nothing, the library will not appear in library manager, the pack just put the library files in USES folder.
To use libraries, you must include this files at the beginning of program/units.

For example:

Code: Select all

uses pic_additional_string_library,Lib_Delays_LIO,lib1_18F97J60_V3_5,eth_lib_user,
     lib2_18F97J60_V3_5,def_io;
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

szabi
Posts: 9
Joined: 23 Jun 2007 19:46

Re: Additional library for MikroPascal PRO 2009

#47 Post by szabi » 12 Jun 2010 09:57

Thanks Florin! :D

ambroise
Posts: 5
Joined: 11 Jul 2010 17:20

Re: Additional library for MikroPascal PRO 2009

#48 Post by ambroise » 11 Jul 2010 17:41

Hello,

first of all , thank you for those libraries.

I use the sensirion one with MP pro with a 18F4550.

8 sht75 are to be read one at a time, and then again every 15 minutes.

I use PORTE.0 as common SCLK for all 8 SHT.
PORTB.0 to 7 as power supply for each sensor ( one at a time )
PORTD.0 to 7 as SDA ( also one at a time )

Well, i've got a problem with var definitions SHTxx_SDA and SHTxx_SDA_dir.

How to change SHTxx_SDA : sbit at PORTD.0 to SHTxx_SDA : sbit at PORTD.1 within the main loop ?

sensirion_init doesn't seem to accept any parameters .

is it an easy way to solve it ?

I hate multiplexing :mrgreen:

any clues are welcome.

thank you.

Ambroise

juanelectro
Posts: 47
Joined: 24 Oct 2009 16:01
Location: Colombia

Re: Additional library for MikroPascal PRO 2009

#49 Post by juanelectro » 22 Jul 2010 14:31

Good day,

I have a question, I can configurate 2 gateways and 2 DNS for sending information to 2 servers?

Thanks for attention.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: Additional library for MikroPascal PRO 2009

#50 Post by yo2lio » 22 Jul 2010 14:37

Hello,

If you want, you can change GATEWAY and DNS on the fly... will works.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

juanelectro
Posts: 47
Joined: 24 Oct 2009 16:01
Location: Colombia

Re: Additional library for MikroPascal PRO 2009

#51 Post by juanelectro » 27 Jul 2010 20:58

Thanks yo2lio

mjoco
Posts: 13
Joined: 06 Apr 2010 16:36

Difference between units

#52 Post by mjoco » 26 Aug 2010 15:35

Hello
What is the difference between the sdmmc_spi1 and the sdmmc_spi2 unit (fat32_spi1 and fat32_spi2)?
Thanks

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Re: Difference between units

#53 Post by yo2lio » 29 Aug 2010 14:43

mjoco wrote:Hello
What is the difference between the sdmmc_spi1 and the sdmmc_spi2 unit (fat32_spi1 and fat32_spi2)?
Thanks
sdmmc_spi1 are for hardware SPI1 module and sdmmc_spi2 are for hardware SPI2 module.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

javor.pas
Posts: 98
Joined: 16 Mar 2007 11:25
Contact:

Re: Additional library for MikroPascal PRO 2009

#54 Post by javor.pas » 13 Sep 2010 11:52

Thank you Florin for the library. I've just started using it and found something that may be useful:
You have to call several Str2Ip(...) prior to calling Eth_SetParameters to set the Ethernet parameters. However in most cases IP, Gateway, MAC, Mask (at least in my case) are written as separate bytes in a flash. So I have to turn those bytes to strings and then call Str2Ip... I think it will be useful to have a function prototype like SetIP(IP : array[4] of byte) or SetIP(IP0, IP1, IP2, IP3 : byte) and so on with other parameters. I think you parse those strings and set the IP when calling Str2Ip.
All this is just a suggestion.

Kind regards,

javor.pas
I sit and think, sit and think... and some time I notice I only sit :)

Post Reply

Return to “mikroPascal PRO for PIC General”