Initial commit

This commit is contained in:
Maxim Salov 2012-10-03 21:37:11 +03:00
commit d722c76539
4 changed files with 241 additions and 0 deletions

56
Makefile Normal file
View file

@ -0,0 +1,56 @@
PROJECT_ELF=uart.elf
PROJECT_MOT=$(PROJECT_ELF:.elf=.mot)
PROJECT_MAP=$(PROJECT_ELF:.elf=.map)
PROJECT_LST=$(PROJECT_ELF:.elf=.lst)
PREFIX=rl78-elf
LD = $(PREFIX)-gcc
CC = $(PREFIX)-gcc
AS = $(PREFIX)-gcc
OBJCOPY = $(PREFIX)-objcopy
OBJDUMP = $(PREFIX)-objdump
SIZE = $(PREFIX)-size
COMMON_PATH = ../common
PROJECT_PATH = .
PROJECT_LNK = $(COMMON_PATH)/rl78-R5F100SL.ld
CFLAGS = -Wall -Wextra -Os -ggdb -ffunction-sections -fdata-sections -I$(PROJECT_PATH) -I$(COMMON_PATH) -mmul=g13
LDFLAGS = -Wl,--gc-sections -Wl,-Map=$(PROJECT_MAP) -T $(PROJECT_LNK) -nostartfiles
SOURCES = \
$(PROJECT_PATH)/main.c \
$(PROJECT_PATH)/uart0.c \
$(END)
OBJS = $(SOURCES:.c=.o) $(COMMON_PATH)/crt0.o
.PHONY: all
all: $(PROJECT_MOT) $(PROJECT_LST)
$(SIZE) $(PROJECT_ELF)
rom: $(PROJECT_MOT)
$(PROJECT_MOT): $(PROJECT_ELF)
$(OBJCOPY) -O srec $^ $@
$(PROJECT_LST): $(PROJECT_ELF)
$(OBJDUMP) -DS $^ > $@
$(PROJECT_ELF): $(OBJS)
$(LD) $(LDFLAGS) -o $@ $^
flash: $(PROJECT_MOT)
rl78flash -m2 -b500000 -vvwri /dev/ttyUSB0 $^
erase:
rl78flash -m2 -b500000 -vveri /dev/ttyUSB0
terminal:
rl78flash -t9600 /dev/ttyUSB0
clean:
-rm -f $(OBJS) $(PROJECT_ELF) $(PROJECT_MOT) $(PROJECT_MAP) $(PROJECT_LST)

62
main.c Normal file
View file

@ -0,0 +1,62 @@
#include "QB-R5F100SL-TB.h"
#include "uart0.h"
volatile unsigned char ticks = 0;
volatile unsigned char flag_1hz = 0;
__attribute__((interrupt))
void wdti_handler(void)
{
}
__attribute__((interrupt))
void it_handler(void)
{
++ticks;
LED1 ^= 1;
if (0 == (0x07 & ticks))
{
flag_1hz = 1;
}
}
int main(void)
{
asm("di");
/* Setup LEDs */
LED1 = 1;
LED2 = 1;
LED1_PIN = 0;
LED2_PIN = 0;
/* Setup clocks */
CMC.cmc = 0x11U; /* Enable XT1, disable X1 */
CSC.csc = 0x80U; /* Start XT1 and HOCO, stop X1 */
CKC.ckc = 0x00U;
/* Delay 1 second */
register unsigned long int i;
for (i = 0x000FFFFFUL; i; --i)
asm("nop");
OSMC.osmc = 0x00; /* Supply fsub to peripherals, including Interval Timer */
uart0_init();
/* Setup 12-bit interval timer */
RTCEN = 1; /* Enable 12-bit interval timer and RTC */
ITMK = 1; /* Disable IT interrupt */
ITPR0 = 0; /* Set interrupt priority - highest */
ITPR1 = 0;
ITMC.itmc = 0x8FFFU; /* Set maximum period 4096/32768Hz = 1/8 s, and start timer */
ITIF = 0; /* Clear interrupt request flag */
ITMK = 0; /* Enable IT interrupt */
asm ("ei"); /* Enable interrupts */
for(;;)
{
if (flag_1hz)
{
LED2 = 0;
flag_1hz = 0;
const char msg[] = "Hello, RL78! [:";
uart0_puts(msg);
LED2 = 1;
}
asm("halt");
}
}

116
uart0.c Normal file
View file

@ -0,0 +1,116 @@
#include "uart0.h"
#include <iodefine.h>
#include <iodefine_ext.h>
void uart0_init(void)
{
/* Reference R01AN0459EJ0100 or hardware manual for details */
PIOR.pior = 0U; /* Disable IO redirection */
PM1.pm1 |= 0x06U; /* Set P11 and P12 as inputs */
SAU0EN = 1; /* Supply clock to serial array unit 0 */
SPS0.sps0 = 0x44U; /* Set input clock (CK00 and CK01) to fclk/16 = 2MHz */
ST0.st0 = 0x03U; /* Stop operation of channel 0 and 1 */
/* Setup interrupts (disable) */
STMK0 = 1; /* Disable INTST0 interrupt */
STIF0 = 0; /* Clear INTST0 interrupt request flag */
STPR10 = 1; /* Set INTST0 priority: lowest */
STPR00 = 1;
SRMK0 = 1; /* Disable INTSR0 interrupt */
SRIF0 = 0; /* Clear INTSR0 interrupt request flag */
SRPR10 = 1; /* Set INTSR0 priority: lowest */
SRPR00 = 1;
SREMK0 = 1; /* Disable INTSRE0 interrupt */
SREIF0 = 0; /* Clear INTSRE0 interrupt request flag */
SREPR10 = 1; /* Set INTSRE0 priority: lowest */
SREPR00 = 1;
/* Setup operation mode for transmitter (channel 0) */
SMR00.smr00 = 0x0023U; /* Operation clock : CK00,
Transfer clock : division of CK00
Start trigger : software
Detect falling edge as start bit
Operation mode : UART
Interrupt source : buffer empty
*/
SCR00.scr00 = 0x8097U; /* Transmission only
Reception error interrupt masked
Phase clock : type 1
No parity
LSB first
1 stop bit
8-bit data length
*/
SDR00.sdr00 = 0xCE00U; /* transfer clock : operation clock divided by 208
2 MHz / 208 = ~9600 bps
*/
/* Setup operation mode for receiver (channel 1) */
NFEN0.nfen0 |= 1; /* Enable noise filter on RxD0 pin */
SIR01.sir01 = 0x0007U; /* Clear error flags */
SMR01.smr01 = 0x0122U; /* Operation clock : CK00
Transfer clock : division of CK00
Start trigger : valid edge on RxD pin
Detect falling edge as start bit
Operation mode : UART
Interrupt source : transfer end
*/
SCR01.scr01 = 0x4097U; /* Reception only
Reception error interrupt masked
Phase clock : type 1
No parity
LSB first
1 stop bit
8-bit data length
*/
SDR01.sdr01 = 0xCE00U; /* transfer clock : operation clock divided by 208
2 MHz / 208 = ~9600 bps
*/
SO0.so0 |= 1; /* Prepare for use of channel 0 */
SOE0.soe0 |= 1;
P1.p1 |= (1 << 2); /* Set TxD0 high */
PM1.pm1 &= ~(1 << 2); /* Set output mode for TxD0 */
PM1.pm1 |= (1 << 1); /* Set input mode for RxD0 */
SS0.ss0 |= 0x03U; /* Enable UART0 operation (both channels) */
STIF0 = 1; /* Set buffer empty interrupt request flag */
}
int uart0_puts(const char * s)
{
int len = 0;
SMR00.smr00 |= 0x0001U; /* Set buffer empty interrupt */
while ('\0' != *s)
{
while (0 == STIF0);
STIF0 = 0;
SDR00.sdr00 = *s++;
++len;
}
#if 0
while (0 == STIF0);
STIF0 = 0;
SDR00.sdr00 = '\r';
#endif
while (0 == STIF0);
STIF0 = 0;
SMR00.smr00 &= ~0x0001U;
SDR00.sdr00 = '\n';
while (0 == STIF0);
#if 0
while (0 != SSR00.BIT.bit6); /* Wait until TSF00 == 0 */
#endif
return len;
}
__attribute__((interrupt))
void st0_handler(void)
{
}
__attribute__((interrupt))
void sr0_handler(void)
{
}
/* This is actually INTSRE0 interrupt handler */
__attribute__((interrupt))
void tm01h_handler(void)
{
}

7
uart0.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef UART0_H__
#define UART0_H__
void uart0_init(void);
int uart0_puts(const char * s);
#endif // UART0_H__