summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halWdtInit.c
blob: eb2a9dffbdd55bf58a97e5d40d8029041a2eb87b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**************************************************************************//**
  \file  halWdtInit.c

  \brief Implementation of WDT start up procedure.

  \author
      Atmel Corporation: http://www.atmel.com \n
      Support email: avr@atmel.com

    Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
    Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
    History:
      29/05/07 E. Ivanov - Created
 ******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/

/******************************************************************************
                   Includes section
******************************************************************************/
#include <types.h>
#include <resetReason.h>
#include <halAppClock.h>

/******************************************************************************
                   Defines section
******************************************************************************/
#define PIN_OUT 62500
#define MEANING_BITS   0x1F

/******************************************************************************
                        Prototypes section
******************************************************************************/
#ifdef _SYS_ASSERT_ON_
  INLINE void halJumpNullHandler(void);
#endif

/******************************************************************************
                   Global variables section
******************************************************************************/
#if defined(__GNUC__)
  uint8_t halResetReason __attribute__ ((section (".noinit")));
#elif defined(__ICCAVR__)
  __no_init uint8_t halResetReason;
#endif

/******************************************************************************
                   Implementations section
******************************************************************************/
/******************************************************************************
Resets and stops wdt. Saves the reason of reset.
Parameters:
  none.
Returns:
  none.
******************************************************************************/
#if defined(__GNUC__)

  void halWdtInit(void) __attribute__ ((naked)) \
    __attribute__ ((section (".init0")));

  void halWdtInit(void)
  {
    ASM("clr r1");
#elif defined(__ICCAVR__)

  __task void halWdtInit(void)
  {
    ASM("clr r15");
#else
  #error 'Compiler not supported.'
#endif

    if (TEMP_WARM_RESET != halResetReason)
    {
      halResetReason = MCUSR & MEANING_BITS;

      if (halResetReason & POWER_ON_RESET)
        halResetReason = POWER_ON_RESET;
    }
    else
    {
      halResetReason = WARM_RESET;
    }
    MCUSR = 0;
    WDTCSR |= (1 << WDCE) | (1 << WDE);
    WDTCSR = 0x00;

  #ifdef _SYS_ASSERT_ON_
    halJumpNullHandler();
  #endif
  }

#ifdef _SYS_ASSERT_ON_
/******************************************************************************
Jump to NULL handler.
Parameters:
  none.
Returns:
  none.
******************************************************************************/
void halJumpNullHandler(void)
{
  if (0 == halResetReason) // was jump on NULL
  {
    register volatile uint16_t tmp;
    tmp = SP;

    ASM ("cli");
    DDRB |= 0xE0;
    /* Init UART*/
    UBRR1H = 0;
    #if (F_CPU == 4000000ul)
      UBRR1L = 12;
    #elif (F_CPU == 8000000ul)
      UBRR1L = 25;
    #endif
    UCSR1A = (1 << U2X1);
    UCSR1B = (1 << TXEN1);
    UCSR1C = (3 << UCSZ10);  // 8-bit data

    /* Init timer counter 4.*/
    OCR4A = 0;
    /* Disable TC4 interrupt */
    TIMSK4 &= ~(1 << OCIE4A);
    /* main clk / 8 */
    TCCR4B = (1 << WGM12) | (1 << CS11);

    while (1)
    {
      do
      { /* Send byte to UART */
        while (!(UCSR1A & (1 << UDRE1)));
        UDR1 = *((uint8_t *)SP);
        SP++;
      } while (RAMEND >= SP);
      SP = tmp;

      PORTB |= 0x80;
      TCNT4 = 0;
      while(TCNT4 < PIN_OUT);
      PORTB &= ~0x80;
      PORTB |= 0x40;
      TCNT4 = 0;
      while(TCNT4 < PIN_OUT);
      PORTB &= ~0x40;
      PORTB |= 0x20;
      TCNT4 = 0;
      while(TCNT4 < PIN_OUT);
      PORTB &= ~0x20;
    }
  }
}
#endif

#if defined(__GNUC__) && defined(_REPORT_STATS_)
void halFillStack(void) __attribute__ ((naked, section (".init1")));
/**************************************************************************//**
\brief Fill cstack with repeated pattern 0xCD
******************************************************************************/
void halFillStack(void)
{
  extern uint16_t __stack_start;
  extern uint16_t __stack;

  for (uint8_t *start = (uint8_t *)&__stack_start; start <= (uint8_t *)&__stack; start++)
    *start = 0xCD;
}
#endif

// eof halWdtInit.c