HWA
Bare metal programming with style
hwa/atmel/avr/examples/07-1-swuart-eeprom-read-write/main.c
Read/write bytes of EEPROM memory
Test application
 ./main.py --help
 ./main.py read 0 512
 ./main.py write 0x01F0 0x55
config.h
#ifndef CONFIG_H
#define CONFIG_H
#include BOARD_H
#define UART HW_SWUARTA( txd, DIABOLO_PIN_TX, \
rxd, DIABOLO_PIN_RX, \
startirq, (DIABOLO_PIN_RX,port,pcic,irq), \
counter, counter1, \
compare, compare0, \
clkdiv, 1, \
autosync, 51, \
fastreg, (shared,gpior0) )
HW_DECLARE(UART);
#endif
main.c
/* This file is part of the HWA project.
* Copyright (c) 2012,2015 Christophe Duparquet.
* All rights reserved. Read LICENSE.TXT for details.
*/
#include "config.h"
uint8_t uart_getbyte ( )
{
while ( !hw(stat,UART).rxc )
hw( wait, irq );
return hw( read, UART );
}
void uart_putbyte ( uint8_t byte )
{
while ( !hw(stat,UART).txc )
hw( wait, irq );
hw( write, UART, byte );
}
/* Process received bytes. Valid sequences are:
*
* 'e'+al+ah+n+'\n' Read n bytes from eeprom address al:ah
* 'E'+al+ah+v+'\n' Write v in eeprom at address al:ah
*/
static void process ( uint8_t byte )
{
static uint8_t bcount = 0 ;
static union {
uint8_t buf[4] ;
struct {
uint8_t cmd ;
uint16_t addr ;
uint8_t n ;
} ;
} buf ;
if ( bcount == 0 ) {
if ( byte == 'E' || byte == 'e' ) {
buf.cmd = byte ;
bcount++ ;
}
} else if ( bcount < 4 )
buf.buf[bcount++] = byte ;
else {
bcount = 0 ;
if ( byte == '\n'
&& buf.addr < HW_DEVICE_EEPROM_SIZE ) {
if ( buf.cmd == 'E' )
hw( write, eeprom0, buf.addr, buf.n );
else {
while ( buf.n-- ) {
uint8_t byte = hw( read, eeprom0, buf.addr );
uart_putbyte( byte );
buf.addr++ ;
}
}
uart_putbyte( '$' );
return ;
}
uart_putbyte( '!' );
}
}
int
main ( )
{
/* Create a HWA context to collect the hardware configuration
* Preload this context with RESET values
*/
hwa( begin, reset );
/* Configure the software UART
*/
hwa( configure, UART );
/* Have the CPU enter idle mode when the 'sleep' instruction is executed.
*/
hwa( configure, core0,
sleep, enabled,
sleep_mode, idle );
/* Write this configuration into the hardware
*/
hwa( commit );
hw( enable, interrupts );
/* Wait for UART synchronization, then send the prompt
*/
while ( !hw(stat,UART).sync )
hw( wait, irq );
uart_putbyte( '$');
for(;;) {
/*
* Main loop:
* put the MCU into sleep mode, an interrupt will awake it
* process incomming bytes
*/
hw( wait, irq );
if ( hw(stat,UART).rxc ) {
/*
* MCU awakened by SWUART that has received a stop bit
* Process the received byte (clears rxc flag)
*/
process( hw( read, UART) );
}
}
}
hwa
#define hwa(...)
hwa( action, object [,...] ) stores an action for an object into a HWA context.
Definition: hwa_macros.h:552
HW_DECLARE
#define HW_DECLARE(...)
Declares the functions that implement an object.
Definition: hwa_1.h:521
swuarta.h
Software-emulated UART.
hw
#define hw(...)
hw( action, object [,...] ) executes an action immediately on an object.
Definition: hwa_macros.h:523
HW_DEVICE_EEPROM_SIZE
#define HW_DEVICE_EEPROM_SIZE
Definition: atmega168x.h:42