implement rx/tx protocol routine of atmel microcontroller.

This commit is contained in:
ichiro 2001-08-02 18:51:00 +00:00
parent 528686cb43
commit 9ada1599ca
3 changed files with 164 additions and 8 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: ipaq_atmel.h,v 1.1 2001/07/31 21:33:03 ichiro Exp $ */
/* $NetBSD: ipaq_atmel.h,v 1.2 2001/08/02 18:51:01 ichiro Exp $ */
/*-
* Copyright (c) 2001 The NetBSD Foundation, Inc. All rights reserved.
@ -42,6 +42,7 @@
/* Frame character */
#define FRAME_SOF 0x02 /* Start of Frame character */
#define FRAME_EOF 0x04 /* End of Frame character */
#define FRAME_OVERHEAD_SIZE 3 /* FRAME_SOF + (ID+len) + checksum */
/* ID */
#define ID_VERSION 0x00 /* Get version ID */
@ -56,6 +57,10 @@
#define SET_BACKLIGHT 0x0d /* Backlight Control command */
#define STATUS_EXTP 0xa1 /* Get status of Extension Pack */
#define MAX_SENDSIZE 32 /* maximum data size of sendData() */
#define MAX_RECVSIZE 16 /* maximum data size of recv data */
/*
* Battery status
*/

View File

@ -1,4 +1,4 @@
/* $NetBSD: ipaq_atmelgpio.c,v 1.1 2001/08/01 07:59:43 ichiro Exp $ */
/* $NetBSD: ipaq_atmelgpio.c,v 1.2 2001/08/02 18:51:01 ichiro Exp $ */
/*-
* Copyright (c) 2001 The NetBSD Foundation, Inc. All rights reserved.
@ -54,15 +54,16 @@
#include <hpcarm/dev/ipaq_saipvar.h>
#include <hpcarm/dev/ipaq_gpioreg.h>
#include <hpcarm/dev/ipaq_atmel.h>
#include <hpcarm/dev/ipaq_atmelvar.h>
#include <hpcarm/sa11x0/sa11x0_gpioreg.h>
#include <hpcarm/sa11x0/sa11x0_comreg.h>
#include <hpcarm/sa11x0/sa11x0_reg.h>
struct atmelgpio_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
struct ipaq_softc *sc_parent;
};
#ifdef ATMEL_DEBUG
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif
static int atmelgpio_match(struct device *, struct cfdata *, void *);
static void atmelgpio_attach(struct device *, struct device *, void *);
@ -70,6 +71,9 @@ static int atmelgpio_print(void *, const char *);
static int atmelgpio_search(struct device *, struct cfdata *, void *);
static void atmelgpio_init(struct atmelgpio_softc *);
static void rxtx_data(struct atmelgpio_softc *, int, int,
u_int8_t *, struct atmel_rx *);
struct cfattach atmelgpio_ca = {
sizeof(struct atmelgpio_softc), atmelgpio_match, atmelgpio_attach
};
@ -92,6 +96,8 @@ atmelgpio_attach(parent, self, aux)
struct atmelgpio_softc *sc = (struct atmelgpio_softc *)self;
struct ipaq_softc *psc = (struct ipaq_softc *)parent;
struct atmel_rx *rxbuf;
printf("\n");
printf("%s: Atmel microcontroller GPIO\n", sc->sc_dev.dv_xname);
@ -107,6 +113,18 @@ atmelgpio_attach(parent, self, aux)
atmelgpio_init(sc);
#if 1 /* this is sample */
rxtx_data(sc, STATUS_BATTERY, 0, NULL, rxbuf);
printf("ac_status = %x\n", rxbuf->data[0]);
printf("Battery kind = %x\n", rxbuf->data[1]);
printf("Voltage = %d mV\n",
1000 * (rxbuf->data[3] << 8 | rxbuf->data[2]) /228);
printf("Battery Status = %x\n", rxbuf->data[4]);
printf("Battery percentage = %d\n",
425 * (rxbuf->data[3] << 8 | rxbuf->data[2]) /1000 - 298);
#endif
/*
* Attach each devices
*/
@ -144,4 +162,81 @@ atmelgpio_init(sc)
/* Set baud rate 115k */
bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR1, 0);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR2, SACOMSPEED(115200));
/* RX/TX enable, RX/TX FIFO interrupt enable */
bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
(CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE));
}
static void
rxtx_data(sc, id, size, buf, rxbuf)
struct atmelgpio_softc *sc;
int id, size;
u_int8_t *buf;
struct atmel_rx *rxbuf;
{
int i, checksum, length, rx_data;
u_int8_t data[MAX_SENDSIZE];
length = size + FRAME_OVERHEAD_SIZE;
while (! (bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACOM_SR0) & SR0_TFS))
;
data[0] = (u_int8_t)FRAME_SOF;
data[1] = (u_int8_t)((id << 4) | size);
checksum = data[1];
i = 2;
while (size--) {
data[i++] = *buf;
checksum += (u_int8_t)(*buf++);
}
data[length-1] = checksum;
while (! (bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACOM_SR1) & SR1_TNF))
;
i = 0;
while (i < length)
bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_DR, data[i++]);
delay(10000);
#if 0
while (! (bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACOM_SR0) &
(SR0_RID | SR0_RFS)))
#endif
rxbuf->state = STATE_SOF;
while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACOM_SR1) & SR1_RNE) {
rx_data = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACOM_DR);
DPRINTF(("DATA = %x\n", rx_data));
switch (rxbuf->state) {
case STATE_SOF:
if (rx_data == FRAME_SOF)
rxbuf->state = STATE_ID;
break;
case STATE_ID:
rxbuf->id = (rx_data & 0xf0) >> 4;
rxbuf->len = rx_data & 0x0f;
rxbuf->idx = 0;
rxbuf->checksum = rx_data;
rxbuf->state = (rxbuf->len > 0 ) ? STATE_DATA : STATE_EOF;
break;
case STATE_DATA:
rxbuf->checksum += rx_data;
rxbuf->data[rxbuf->idx] = rx_data;
if (++rxbuf->idx == rxbuf->len)
rxbuf->state = STATE_EOF;
break;
case STATE_EOF:
rxbuf->state = STATE_SOF;
if (rx_data == FRAME_EOF || rx_data == rxbuf->checksum)
DPRINTF(("frame EOF\n"));
else
DPRINTF(("BadFrame\n"));
break;
default:
break;
}
}
}

View File

@ -0,0 +1,56 @@
/* $NetBSD: ipaq_atmelvar.h,v 1.1 2001/08/02 18:51:00 ichiro Exp $ */
/*-
* Copyright (c) 2001 The NetBSD Foundation, Inc. All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Ichiro FUKUHARA (ichiro@ichiro.org).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
struct atmelgpio_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
struct ipaq_softc *sc_parent;
};
struct atmel_rx {
u_int8_t id;
u_int8_t len;
u_int8_t idx;
u_int8_t data[MAX_RECVSIZE];
u_int8_t checksum;
u_int8_t state;
#define STATE_SOF 0
#define STATE_ID 1
#define STATE_DATA 2
#define STATE_EOF 3
};