Author Topic: SPI to UART, do I need WDOG and how do I set up ALT config?  (Read 9731 times)

Offline Raffaele

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
SPI to UART, do I need WDOG and how do I set up ALT config?
« on: December 21, 2019, 07:38:32 PM »
Hi,
I need a minimal code for the KL03 that defines an SPI master interface using the ALT3 pin configuration (B0 CLK, A3 MOSI, A5 SS, A6 MISO) and prints the result on UART. I want to disable everything that is not necessary.
Few questions:

1) Do I need to comment some of the functions in TaskConfig.h? I commented the wdog and app but apparently they are needed (?) otherwise I can't print to UART
2) I assume, I might be wrong though, that the SPI ALT3 configuration is already defined, but how do I enable the ALT3 instead of the default SPI, and where do I change the parameters (CLK etc)?
3) What function do I need to call to actually enable SPI?

I read this http://www.utasker.com/forum/index.php?topic=1902.0 so I know how to set up the SPI interface but  in app_hw_kinetis.h I can't find the INITIALISE_SPI_SD_INTERFACE() for the KL03

Thank you

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3243
    • View Profile
    • uTasker
Re: SPI to UART, do I need WDOG and how do I set up ALT config?
« Reply #1 on: December 21, 2019, 08:41:46 PM »
Hi

For a minimum project with UART use the configuration (in config.h):
#define HELLO_WORLD                                                    // gives the classic first step project with just a message on a UART start and echos back input afterwards [enter key shows memory use] (also blinks LED and is identical with or without BLINKY enabled)

It will have two tasks:
watchdog - called every 200ms to trigger the watchdog and toggle a heat-beat LED
application - which configures the UART and prints a message. It is also scheduled each tie something is received and echoes it back.
It can run with or without low power support
#define SUPPORT_LOW_POWER                                        // a low power task supervises power reduction when possible
which will then also add the low power task, typically halving the current consumption with no other effect on behavior.

The UART can still be used with DMA (although in your case the KL03 doesn't have DMA capabilities) or interrupts.

You can add your own SPI initialisation code in the initialisation part of the application task (or in extern void fnUserHWInit(void) if you prefer it to be done immediately when the processor starts).

Although the SD card interface is probably not the one you need you can use it as reference for any SPI configuration and communication. There is no setup for KL03 since utFAT has never been used with it - it probably has too little memory for such file system use but it is the same as other KL parts - for example (from KL26)

        // Configure to suit special connection SPI mode at between 100k and 400k (SPI1)
        // - SPI1_CS   PTD-4 (J2-6) [VDD J3-4 / 0V J3-14]
        // - SPI1_SCK  PTD-5 (J2-12)
        // - SPI1_MOSI PTD-6 (J2-8)
        // - SPI1_MISO PTD-7 (J2-10)
        //
        #define SPI_CS1_0              PORTD_BIT4
        #define INITIALISE_SPI_SD_INTERFACE() POWER_UP_ATOMIC(4, SPI1); \
        _CONFIG_PERIPHERAL(D, 5, PD_5_SPI1_SCK); \
        _CONFIG_PERIPHERAL(D, 6, (PD_6_SPI1_MOSI | PORT_SRE_FAST | PORT_DSE_HIGH)); \
        _CONFIG_PERIPHERAL(D, 7, (PD_7_SPI1_MISO | PORT_PS_UP_ENABLE)); \
        _CONFIG_DRIVE_PORT_OUTPUT_VALUE(D, SPI_CS1_0, SPI_CS1_0, (PORT_SRE_FAST | PORT_DSE_HIGH)); \
        SPI1_C1 = (SPI_C1_CPHA | SPI_C1_CPOL | SPI_C1_MSTR | SPI_C1_SPE); \
        SPI1_BR = (SPI_BR_SPPR_PRE_8 | SPI_BR_SPR_DIV_16); \
        (void)SPI1_S; (void)SPI1_D
        #define ENABLE_SPI_SD_OPERATION()
        #define SET_SD_CARD_MODE()

        // Set maximum speed
        //
        #define SET_SPI_SD_INTERFACE_FULL_SPEED() SPI1_BR = (SPI_BR_SPPR_PRE_1 | SPI_BR_SPR_DIV_2)
        #if defined _WINDOWS
            #define WRITE_SPI_CMD(byte)    SPI1_D = (byte); SPI1_D = _fnSimSD_write((unsigned char)byte)
            #define WAIT_TRANSMISSON_END() while (((unsigned char)SPI1_S & (SPI_S_SPRF)) == 0) { SPI1_S |= (SPI_S_SPRF); }
            #define READ_SPI_DATA()        (unsigned char)SPI1_D
        #else
            #define WRITE_SPI_CMD(byte)    SPI1_D = (byte)
            #define WAIT_TRANSMISSON_END() while (((unsigned char)SPI1_S & (SPI_S_SPRF)) == 0) {}
            #define READ_SPI_DATA()        (unsigned char)SPI1_D
        #endif
        #define SET_SD_DI_CS_HIGH()  _SETBITS(D, SPI_CS1_0)              // force DI and CS lines high ready for the initialisation sequence
        #define SET_SD_CS_LOW()      _CLEARBITS(D, SPI_CS1_0)            // assert the CS line of the SD card to be read
        #define SET_SD_CS_HIGH()     _SETBITS(D, SPI_CS1_0)              // negate the CS line of the SD card to be read
        #define POWER_UP_SD_CARD()                                       // apply power to the SD card if appropriate
        #define POWER_DOWN_SD_CARD()
        #define GET_SDCARD_WP_STATE()  0                                 // never write protect



Note that MOSI is not on A3 of the KL03 but on A7. This means that a suitable setup for the KL03 would be

    // - SPI0_CS   PTA-5
    // - SPI0_SCK  PTB-0
    // - SPI0_MOSI PTA-7
    // - SPI0_MISO PTA-6
    //
#define SPI_CS1_0              PORTA_BIT5
POWER_UP_ATOMIC(4, SPI0);
_CONFIG_PERIPHERAL(B, 0, PB_0_SPI0_SCK);
_CONFIG_PERIPHERAL(A, 7, (PA_7_SPI0_MOSI | PORT_SRE_FAST | PORT_DSE_HIGH));
_CONFIG_PERIPHERAL(A, 6, (PA_6_SPI0_MISO | PORT_PS_UP_ENABLE)); \
_CONFIG_DRIVE_PORT_OUTPUT_VALUE(A, SPI_CS1_0, SPI_CS1_0, (PORT_SRE_FAST | PORT_DSE_HIGH));
SPI0_C1 = (SPI_C1_CPHA | SPI_C1_CPOL | SPI_C1_MSTR | SPI_C1_SPE);
SPI0_BR = (SPI_BR_SPPR_PRE_8 | SPI_BR_SPR_DIV_16);
(void)SPI0_S; (void)SPI0_D;


whereby the chip select is controlled as a GPIO output rather than using its peripheral function.
PB_0_SPI0_SCK is ALT3 so one doesn't necessarily need to know the ALT details if the names with integrated pin numbers are used like this.

For transmission and reception you just need to change SPI1 into SPI0.

Regards

Mark


P.S. You can disable the watchdog and remove the watchdog task if you don't need it but it is generally always present since watchdog functionality would normally be the highest priority for reliable equipment (that can recover from serious error situations)



« Last Edit: December 21, 2019, 08:44:21 PM by mark »

Offline Raffaele

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: SPI to UART, do I need WDOG and how do I set up ALT config?
« Reply #2 on: December 22, 2019, 08:58:49 PM »
Thank you Mark, SPI and UART work well, but I need to have the wdog active. Any ideas why?

One more question, is there a process that is executed every 50ms? Could it be  something like a timer to keep track of when my task (that is executed periodically) has to be re-executed?  The reason I am asking is because I have the KL03 on a custom PCB and I can see that every 50ms there is a signal like a spike leaking from the device and interfering with other components, but I am not sure if it is routed internally or it goes to a pin.
If I disable my task I don't see it any more, so I thought it was a tick

Thanks again

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3243
    • View Profile
    • uTasker
Re: SPI to UART, do I need WDOG and how do I set up ALT config?
« Reply #3 on: December 22, 2019, 10:19:24 PM »
Hi

If the watchdog is not disable you will need to trigger it (so it doesn't reset every 2s). To disable it set

#define WATCHDOG_DISABLE()     (1)

There is a periodic interrupt every _TICK_RESOLUTION.

This is usually

#define _TICK_RESOLUTION     TICK_UNIT_MS(50)                        // 50ms system tick period - max possible at 50MHz SYSTICK would be about 335ms !

and is used as basis for timing timed tasks.

The watchdog task itself will toggle an output at 200ms.
See
#define TOGGLE_WATCHDOG_LED()   _TOGGLE_PORT(A, BLINK_LED)
which can also be set to
#define TOGGLE_WATCHDOG_LED()
if not desired.

If you see a 50ms glitch it won't be the watchdog task but instead the interrupt. It may be that you are using

#define SUPPORT_LOW_POWER                                        // a low power task supervises power reduction when possible

which means that the processor is sleeping most of the time and wakes at each TICK - the current consumption increases when it handles it and reduces again when it goes back to sleep afterwards.

If there is not an output being changed by software such glitches may be due to the power supply not being well stabilised and so reacting to the changes in CPU current consumption.

Regards

Mark


Offline Raffaele

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: SPI to UART, do I need WDOG and how do I set up ALT config?
« Reply #4 on: December 22, 2019, 10:27:46 PM »
Nice, thanks a lot! Very helpful