aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/TIVA/TM4C129x/hal_lld.c
blob: 7af3cc4b6a8a32489fce428a0b8c29937fe05f1c (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
/*
    Copyright (C) 2014..2017 Marco Veeneman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    TIVA/TM4C129x/hal_lld.c
 * @brief   TM4C129x HAL Driver subsystem low level driver source.
 *
 * @addtogroup HAL
 * @{
 */

#include "hal.h"

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local variables and types.                                         */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Driver interrupt handlers.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   Low level HAL driver initialization.
 *
 * @notapi
 */
void hal_lld_init(void)
{
}

/**
 * @brief   TM4C129x clocks and PLL initialization.
 * @note    All the involved constants come from the file @p board.h and 
 *          @p mcuconf.h.
 * @note    This function should be invoked just after the system reset.
 *
 * @special
 */
void tiva_clock_init(void)
{
  uint32_t moscctl, rsclkcfg;

  /*
   * 1. Once POR has completed, the PIOSC is acting as the system clock.
   */

  /*
   * 2. Power up the MOSC by clearing the NOXTAL bit in the MOSCCTL register.
   */
  moscctl = HWREG(SYSCTL_MOSCCTL);
  moscctl &= ~SYSCTL_MOSCCTL_NOXTAL;

  /*
   * 3. If single-ended MOSC mode is required, the MOSC is ready to use. If crystal mode is required,
   * clear the PWRDN bit and wait for the MOSCPUPRIS bit to be set in the Raw Interrupt Status
   * (RIS), indicating MOSC crystal mode is ready.
   */
#if TIVA_MOSC_SINGLE_ENDED
  HWREG(SYSCTL_MOSCCTL) = moscctl;
#else
  moscctl &= ~SYSCTL_MOSCCTL_PWRDN;
  HWREG(SYSCTL_MOSCCTL) = moscctl;

  while (!(HWREG(SYSCTL_RIS) & SYSCTL_RIS_MOSCPUPRIS));
#endif

  /*
   * 4. Set the OSCSRC field to 0x3 in the RSCLKCFG register at offset 0x0B0.
   */
  rsclkcfg = HWREG(SYSCTL_RSCLKCFG);

  rsclkcfg |= TIVA_RSCLKCFG_OSCSRC;

  /*
   * 5. If the application also requires the MOSC to be the deep-sleep clock source, then program the
   * DSOSCSRC field in the DSCLKCFG register to 0x3.
   */

  /*
   * 6. Write the PLLFREQ0 and PLLFREQ1 registers with the values of Q, N, MINT, and MFRAC to
   * the configure the desired VCO frequency setting.
   */
  HWREG(SYSCTL_PLLFREQ1) = (0x04 << 0); // 5 - 1
  HWREG(SYSCTL_PLLFREQ0) = (0x60 << 0) | SYSCTL_PLLFREQ0_PLLPWR;

  /*
   * 7. Write the MEMTIM0 register to correspond to the new system clock setting.
   */
  HWREG(SYSCTL_MEMTIM0) = (SYSCTL_MEMTIM0_FBCHT_3_5 | (5 << SYSCTL_MEMTIM0_FWS_S) | SYSCTL_MEMTIM0_EBCHT_3_5 | (5 << SYSCTL_MEMTIM0_EWS_S) | SYSCTL_MEMTIM0_MB1);

  /*
   * Wait for the PLLSTAT register to indicate the PLL has reached lock at the new operating point
   * (or that a timeout period has passed and lock has failed, in which case an error condition exists
   * and this sequence is abandoned and error processing is initiated).
   */
  while (!HWREG(SYSCTL_PLLSTAT) & SYSCTL_PLLSTAT_LOCK);

  /*
   * 9. Write the RSCLKCFG register's PSYSDIV value, set the USEPLL bit to enabled, and MEMTIMU
   * bit.
   */

  rsclkcfg = HWREG(SYSCTL_RSCLKCFG);

  rsclkcfg |= (SYSCTL_RSCLKCFG_USEPLL | (0x03 << 0) | (0x03 << 20) | (0x03 << 24));

  //rsclkcfg |= ((0x03 << 0) | (1 << 28) | (0x03 << 20));

  rsclkcfg |= SYSCTL_RSCLKCFG_MEMTIMU;

  // set new configuration
  HWREG(SYSCTL_RSCLKCFG) = rsclkcfg;

#if HAL_USE_PWM
#if TIVA_PWM_USE_PWM0
  HWREG(PWM0_CC) = TIVA_PWM_FIELDS;
#endif
#endif
}

/** @} */