Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

TSoC and CY8CKIT-149 Software Reset and WDT Reset Sample

MotooTanaka
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

A few days ago, I saw a question about how to use WDT reset.

How can I set WDT to reset directly? PSoC 4100S (CY8C4125AXI-S433)

Then I wrote a sample just trigger WDT interrupt and/or Software Reset,

but I failed to Implement feeding the dog.

So in this example I tried to provide a simple sample which does

(0) Report the reason of the previous reset

(1) Software Reset

(2) WDT Timeout Reset

and to make it easier to realize, I let the LED blink a few times

when program got started (or restarted).

数日前、WDTリセットの使用方法についての質問トピックがありました。

How can I set WDT to reset directly? PSoC 4100S (CY8C4125AXI-S433)

その時は、とりあえず WDT リセットを発生するか、

Software Reset を発生するサンプルを書いてみましたが、

そもそも正常な WDT のクリアは実装していませんでした。

今回、このサンプルでは、下記の機能を付けてみました

(0) 起動時、直前のリセット原因を(わかる範囲で) 表示する

(1) Software Reset を発生させる

(2) WDT Reset を発生させる

実際にリセットが掛かったのかが分かりにくかったので、

起動時(又は再起動時) に数回 LED を点滅させてみました。

The schematic

001-schematic.JPG

Pin for TSoC

002-pin-tsoc.JPG

Pin for CY8CKIT-149

003-pin-cy8ckit-149.JPG

main.c

============================

#include "project.h"

#include "stdio.h"

#include "string.h"

#define STR_LEN 32

#define RX_BUF_LEN 128

#define SPACE ' '

#define TAB '\t'

#define CR  '\r'

#define LF  '\n'

volatile char rx_buf[RX_BUF_LEN] ;

volatile int  rx_write_index = 0 ;

int           rx_read_index = 0 ;

char          str[STR_LEN+1] ; /* print buffer */

int           str_index = 0 ;

int           wdt_feed_flag = 1 ;

inline int is_delimiter(uint8_t c) ;

int        get_str(void) ;

void       str2upper(char str[]) ;

void       init_hardware(void) ;

void       splash(void) ;

void       prompt(void) ;

void       blink_led(void) ;

void       print(char *str) ;

CY_ISR_PROTO(usr_isr) ;

void help(void)

{

    print("=== reset test command ===\n") ;

    print("sw   : cause software reset\n") ;

    print("wdt  : cause watch dog reset (wait a few seconds)\n") ;

    print("help : print this message\n") ;

    print("==========================\n") ;

}

           

void print_reset_cause(void)

{

    // uint32_t value ;

    // value = CY_SYS_RES_CAUSE_REG ;

    print("Reason of previous reset: ") ;

    if (CY_SYS_RESET_WDT ==  CySysGetResetReason(CY_SYS_RESET_WDT)) {

        print("Watch Dog Timeout") ;

    } else if (CY_SYS_RESET_PROTFAULT == CySysGetResetReason(CY_SYS_RESET_PROTFAULT)) {

        print("Protection Violation") ;

    } else if (CY_SYS_RESET_SW == CySysGetResetReason(CY_SYS_RESET_SW)) {

        print("Software") ;

    } else {

        print("Unknown") ;

    }

    print("\n") ;

}

void disable_wdt(void)

{

    CY_SYS_WDT_DISABLE_KEY_REG = 0xACED8865 ;

}

void enable_wdt(void)

{

    CY_SYS_WDT_DISABLE_KEY_REG = 0x0000 ;

}

void feed_wdt(void)

{

    CY_SYS_SRSS_INTR_REG |= CY_SYS_RESET_WDT ;

}

void init_hardware(void)

{

    disable_wdt() ;

   

    UART_SpiUartClearRxBuffer() ;

    UART_SpiUartClearTxBuffer() ;

    UART_ClearRxInterruptSource(UART_GetRxInterruptSource()) ;

    uart_rx_int_ClearPending() ;

    uart_rx_int_StartEx(usr_isr) ;

    UART_Start() ;

    CyGlobalIntEnable; /* Enable global interrupts. */   

}

void do_command(char *cmd)

{

    str2upper(cmd) ;

    if (strcmp(cmd, "WDT") == 0) {

        // enable_wdt() ;

        wdt_feed_flag = 0 ;

    } else if (strcmp(cmd, "SW") == 0) {

        CySoftwareReset() ;

    } else if (strcmp(cmd, "HELP") == 0) {

        help() ;

    } else {

        print("Unknown command: ") ;

        print(cmd) ;

        print("\n") ;

        help() ;

    }

}

int main(void)

{

    init_hardware() ;

   

    splash() ;   

    print_reset_cause() ;

    help() ;

    blink_led() ;

    enable_wdt() ;

   

    prompt() ;

    for(;;) {

        if (get_str() > 0) {

            do_command(str) ;

            prompt() ;

        }

        if (wdt_feed_flag) {

            feed_wdt() ;

        }

    }

}

inline int is_delimiter(uint8_t c)

{

    int result = 0 ;

    switch(c) {

    case CR:

    case LF:

    case TAB:

    case SPACE:

        result = c ;

        break ;

    }

    return( result ) ;

}

int get_str(void)

{

    int result = 0 ;

    if (rx_read_index != rx_write_index) {

        if (is_delimiter(rx_buf[rx_read_index])) {

            if (str_index > 0) {

                str[str_index] = 0 ;

                str_index = 0 ;

                result = 1 ;

            }

        } else {

            str[str_index++] = rx_buf[rx_read_index] ;

            if (str_index >= STR_LEN) {

                str[STR_LEN] = 0 ;

                str_index = 0 ;

                result = -1 ;

            }

        }

        rx_read_index = (rx_read_index + 1) % RX_BUF_LEN ;

    }

    return( result ) ;

}

CY_ISR(usr_isr)

{

    UART_ClearRxInterruptSource(UART_INTR_RX_NOT_EMPTY) ;

    if (UART_SpiUartGetRxBufferSize()) {

        rx_buf[rx_write_index] = UART_UartGetByte() ;

        rx_write_index = (rx_write_index + 1) % RX_BUF_LEN ;     

    }

}

void print(char *str)

{

    UART_UartPutString(str) ;

}

void splash(void)

{

    sprintf(str, "\nWDT Test (%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

}

void blink_led(void)

{

    int i ;

    for (i = 0 ; i < 10 ; i++ ) {

        LED_Write(!LED_Read()) ;

        CyDelay(100) ;

    }

}

void prompt(void)

{

    print("> ") ;

}

void str2upper(char str[])

{

    int i ;

    for (i = 0 ; str ; i++ ) {

        if (('a' <= str)&&(str <= 'z')) {

            str -= ('a' - 'A') ;

        }

    }

}

/* [] END OF FILE */

============================

Tera Term Log

000-TeraTerm-Log.JPG

moto

0 Likes
0 Replies