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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009, 2010 Daniel Beer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <string.h>
#include "simio_device.h"
#include "simio_wdt.h"
#include "output.h"
#include "expr.h"
/* WDTCTL flags, taken from mspgcc.
*
* Watchdog timer password. Always read as 069h. Must be written as 05Ah,
* or a PUC will be generated.
*/
#define WDTIS0 0x0001
#define WDTIS1 0x0002
#define WDTSSEL 0x0004
#define WDTCNTCL 0x0008
#define WDTTMSEL 0x0010
#define WDTNMI 0x0020
#define WDTNMIES 0x0040
#define WDTHOLD 0x0080
#define WDTPW 0x5A00
/* Flags in IE1 */
#define WDTIE 0x01
#define NMIIE 0x10
/* Flags in IFG1 */
#define WDTIFG 0x01
#define NMIIFG 0x10
struct wdt {
struct simio_device base;
int pin_state;
int wdt_irq;
int count_reg;
int reset_triggered;
uint8_t wdtctl;
};
struct simio_device *wdt_create(char **arg_text)
{
struct wdt *w = malloc(sizeof(*w));
(void)arg_text;
if (!w) {
pr_error("wdt: can't allocate memory");
return NULL;
}
memset(w, 0, sizeof(*w));
w->base.type = &simio_wdt;
w->pin_state = 1;
w->wdt_irq = 10;
w->reset_triggered = 0;
w->wdtctl = 0;
w->count_reg = 0;
return (struct simio_device *)w;
}
static void wdt_destroy(struct simio_device *dev)
{
free(dev);
}
static void wdt_reset(struct simio_device *dev) {
struct wdt *w = (struct wdt *)dev;
w->reset_triggered = 0;
w->wdtctl = 0;
w->count_reg = 0;
}
static int parse_int(int *val, char **arg_text)
{
const char *text = get_arg(arg_text);
address_t value;
if (!text) {
printc_err("wdt: expected integer argument\n");
return -1;
}
if (expr_eval(text, &value) < 0) {
printc_err("wdt: couldn't parse argument: %s\n", text);
return -1;
}
*val = value;
return 0;
}
static int wdt_config(struct simio_device *dev, const char *param,
char **arg_text)
{
struct wdt *w = (struct wdt *)dev;
if (!strcasecmp(param, "nmi")) {
int old = w->pin_state;
if (parse_int(&w->pin_state, arg_text) < 0)
return -1;
if (w->wdtctl & WDTNMI) {
if (((w->wdtctl & WDTNMIES) &&
old && !w->pin_state) ||
(!(w->wdtctl & WDTNMIES) &&
!old && w->pin_state))
simio_sfr_modify(SIMIO_IFG1, NMIIFG, NMIIFG);
}
return 0;
}
if (!strcasecmp(param, "irq"))
return parse_int(&w->wdt_irq, arg_text);
printc_err("wdt: unknown configuration parameter: %s\n", param);
return -1;
}
static int wdt_info(struct simio_device *dev)
{
struct wdt *w = (struct wdt *)dev;
printc("Configured WDT IRQ: %d\n", w->wdt_irq);
printc("WDTCTL: 0x__%02x\n", w->wdtctl);
printc("NMI/RST# pin: %s\n", w->pin_state ? "HIGH" : "low");
printc("Counter: 0x%04x\n", w->count_reg);
printc("Reset: %s\n",
w->reset_triggered ? "TRIGGERED" : "not triggered");
return 0;
}
static int wdt_write(struct simio_device *dev, address_t addr, uint16_t data)
{
struct wdt *w = (struct wdt *)dev;
if (addr != 0x120)
return 1;
if (data >> 8 != 0x5a)
w->reset_triggered = 1;
w->wdtctl = data & 0xf7;
if (w->wdtctl & WDTCNTCL)
w->count_reg = 0;
return 0;
}
static int wdt_read(struct simio_device *dev, address_t addr, uint16_t *data)
{
struct wdt *w = (struct wdt *)dev;
if (addr != 0x120)
return 1;
*data = 0x6900 | w->wdtctl;
return 0;
}
static int wdt_check_interrupt(struct simio_device *dev)
{
struct wdt *w = (struct wdt *)dev;
uint8_t flags;
if (!(w->wdtctl & WDTNMI) && !w->pin_state)
return 15;
if (w->reset_triggered)
return 15;
flags = simio_sfr_get(SIMIO_IFG1) & simio_sfr_get(SIMIO_IE1);
if (flags & NMIIFG)
return 14;
if (flags & WDTIFG)
return w->wdt_irq;
return -1;
}
static void wdt_ack_interrupt(struct simio_device *dev, int irq)
{
struct wdt *w = (struct wdt *)dev;
if (irq == 14)
simio_sfr_modify(SIMIO_IFG1, NMIIFG, 0);
else if (irq == w->wdt_irq)
simio_sfr_modify(SIMIO_IFG1, WDTIFG, 0);
}
static void wdt_step(struct simio_device *dev, uint16_t status_register,
const int *clocks)
{
struct wdt *w = (struct wdt *)dev;
int max = 1;
(void)status_register;
/* If on hold, nothing happens */
if (w->wdtctl & WDTHOLD)
return;
/* Count input clock cycles */
if (w->wdtctl & WDTSSEL)
w->count_reg += clocks[SIMIO_ACLK];
else
w->count_reg += clocks[SIMIO_SMCLK];
/* Figure out the divisor */
switch (w->wdtctl & 3) {
case 0: max = 32768; break;
case 1: max = 8192; break;
case 2: max = 512; break;
case 3: max = 64; break;
}
/* Check for overflow */
if (w->count_reg >= max) {
if (w->wdtctl & WDTTMSEL)
simio_sfr_modify(SIMIO_IFG1, WDTIFG, WDTIFG);
else
w->reset_triggered = 1;
}
w->count_reg &= (max - 1);
}
const struct simio_class simio_wdt = {
.name = "wdt",
.help =
"This module simulates the Watchdog Timer+ peripheral. There are no\n"
"constructor arguments. Configuration parameters are:\n"
" irq <irq>\n"
" Set the interrupt vector for the WDT interrupt.\n"
" nmi <0|1>\n"
" Set the state of the NMI/RST# pin.\n",
.create = wdt_create,
.destroy = wdt_destroy,
.reset = wdt_reset,
.config = wdt_config,
.info = wdt_info,
.write = wdt_write,
.read = wdt_read,
.check_interrupt = wdt_check_interrupt,
.ack_interrupt = wdt_ack_interrupt,
.step = wdt_step
};
|