241 lines
4.6 KiB
C
241 lines
4.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <math.h>
|
|
#include <sys/times.h>
|
|
#include <sys/time.h>
|
|
|
|
#define CPU_FREQ_HZ 100000000
|
|
#include "libsys.h"
|
|
#include "gpio.h"
|
|
#include "irq.h"
|
|
|
|
char buffer[16384];
|
|
char * volatile pPtr_r;
|
|
char * volatile pPtr_w;
|
|
static volatile INT32 mouse_x;
|
|
static volatile INT32 mouse_y;
|
|
static volatile int _g_posx, _g_posy;
|
|
static volatile g_btn_l, g_btn_r;
|
|
static volatile UINT32 g_color;
|
|
uint8_t g_rx_buf[256];
|
|
volatile uint32_t g_rx_cmd_valid, g_rx_cid;
|
|
gpio_if_t gpio_if;
|
|
uint8_t *pRxBuf;
|
|
uint32_t power;
|
|
float temp;
|
|
|
|
#define T_AMB 20.f
|
|
#define T_MAX 100.f
|
|
#define T_SAMPLE 0.1f
|
|
#define P_LOSS 100.f
|
|
#define MASS 10.f
|
|
#define KCM (1.f/(MASS*4190))
|
|
|
|
enum
|
|
{
|
|
rx_idle = 0,
|
|
rx_sync,
|
|
rx_cid,
|
|
rx_data
|
|
};
|
|
|
|
// -------------------------------------------------------------
|
|
void handler0(void)
|
|
{
|
|
printf("Interrupt 0\n");
|
|
interrupt_clr(0);
|
|
}
|
|
|
|
void handler1(void)
|
|
{
|
|
printf("Interrupt 1\n");
|
|
interrupt_clr(1);
|
|
}
|
|
|
|
void handler2(void)
|
|
{
|
|
printf("Interrupt 2\n");
|
|
}
|
|
|
|
void handler3(void)
|
|
{
|
|
volatile UINT32 *pUART1_stat = (UINT32*)SYS_UART1_STAT;
|
|
volatile UINT32 *pUART1_data = (UINT32*)SYS_UART1_DATA;
|
|
|
|
static uint32_t rx_state;
|
|
uint8_t c;
|
|
|
|
while(0x200 & *pUART1_stat)
|
|
{
|
|
|
|
c = *pUART1_data;
|
|
|
|
switch (rx_state)
|
|
{
|
|
case rx_idle:
|
|
if (g_rx_cmd_valid)
|
|
break;
|
|
|
|
|
|
if ((c == 0x0A) || (c == 0x0D))
|
|
{
|
|
pRxBuf = g_rx_buf;
|
|
break;
|
|
}
|
|
|
|
if (c == '!')
|
|
{
|
|
*(pRxBuf) = 0;
|
|
rx_state = rx_data;
|
|
}
|
|
else
|
|
*(pRxBuf++) = c;
|
|
|
|
case rx_data:
|
|
if ((c == 0x0A) || (c == 0x0D))
|
|
{
|
|
pRxBuf = g_rx_buf;
|
|
g_rx_cmd_valid = 1;
|
|
rx_state = rx_idle;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void handler4(void)
|
|
{
|
|
printf("Interrupt 4\n");
|
|
}
|
|
|
|
void handler5(void)
|
|
{
|
|
printf("Interrupt 5\n");
|
|
}
|
|
|
|
void handler6(void)
|
|
{
|
|
printf("Interrupt 6\n");
|
|
}
|
|
|
|
void handler7(void)
|
|
{
|
|
UINT32 volatile *pTim_stat = (UINT32*)SYS_ITIM_STAT;
|
|
UINT32 volatile *pTim_ctrl = (UINT32*)SYS_ITIM_CTRL;
|
|
UINT32 volatile *pTim0_cmp = (UINT32*)SYS_ITIM0_CMP;
|
|
time_t curr_date;
|
|
struct tm *pDate;
|
|
uint32_t port;
|
|
int i;
|
|
|
|
if (*pTim_stat & 4)
|
|
{
|
|
*pTim_stat = 4;
|
|
}
|
|
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
|
|
int i;
|
|
uint8_t *pi;
|
|
|
|
volatile UINT32 *pUART0_stat = (UINT32*)SYS_UART0_STAT;
|
|
volatile UINT32 *pUART1_stat = (UINT32*)SYS_UART1_STAT;
|
|
volatile UINT32 *pUART0_baud = (UINT32*)SYS_UART0_BAUD;
|
|
volatile UINT32 *pUART1_baud = (UINT32*)SYS_UART1_BAUD;
|
|
UINT32 volatile *pTim_ctrl = (UINT32*)SYS_ITIM_CTRL;
|
|
UINT32 volatile *pTim_stat = (UINT32*)SYS_ITIM_STAT;
|
|
UINT32 volatile *pTim0_cnt = (UINT32*)SYS_ITIM0_CNT;
|
|
UINT32 volatile *pTim0_cmp = (UINT32*)SYS_ITIM0_CMP;
|
|
UINT32 volatile *pTim1_cnt = (UINT32*)SYS_ITIM1_CNT;
|
|
UINT32 volatile *pTim1_cmp = (UINT32*)SYS_ITIM1_CMP;
|
|
UINT32 volatile *pUART1_data = (UINT32*)SYS_UART1_DATA;
|
|
UINT32 start, end;
|
|
char string[65];
|
|
FILE *pFile1;
|
|
|
|
sleep(1000);
|
|
|
|
interrupt_register(0, (void*)handler0);
|
|
interrupt_register(1, (void*)handler1);
|
|
// interrupt_register(2, (void*)handler2);
|
|
interrupt_register(3, (void*)handler3);
|
|
// interrupt_register(4, (void*)handler4);
|
|
// interrupt_register(5, (void*)handler5);
|
|
interrupt_register(6, (void*)handler6);
|
|
interrupt_register(7, (void*)handler7);
|
|
|
|
gpio_init(&gpio_if, SYS_GPIO_0_BASE);
|
|
|
|
srand(clock());
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
pPtr_r = buffer;
|
|
pPtr_w = buffer;
|
|
|
|
UART1_setbaud(460800);
|
|
// UART: enable RX interrupt
|
|
*pUART1_stat = (1 << 6);
|
|
// printf("UART0 Status = 0x%8.8X\n", *pUART0_stat);
|
|
|
|
// *pTim0_cnt = 0;
|
|
// *pTim1_cnt = 0;
|
|
// *pTim0_cmp = 100000;
|
|
// *pTim1_cmp = 100000000;
|
|
// *pTim_stat = (1 << 2) | (1 << 0);
|
|
// *pTim_ctrl = (3 << 2);
|
|
// printf("Timer Status = 0x%8.8X\n", *pTim_stat);
|
|
// printf("Timer Ctrl = 0x%8.8X\n", *pTim_ctrl);
|
|
|
|
|
|
// interrupt_enable(0);
|
|
// interrupt_enable(1);
|
|
// interrupt_enable(2);
|
|
interrupt_enable(3);
|
|
// interrupt_enable(4);
|
|
// interrupt_enable(5);
|
|
// interrupt_enable(6);
|
|
// interrupt_enable(7);
|
|
|
|
|
|
// *pTim_ctrl |= 3;
|
|
|
|
pRxBuf = g_rx_buf;
|
|
g_rx_cmd_valid = 0;
|
|
temp = T_AMB;
|
|
pi = (uint8_t*)&temp;
|
|
while(1)
|
|
{
|
|
if (g_rx_cmd_valid)
|
|
{
|
|
sscanf(g_rx_buf, "%d", &power);
|
|
|
|
temp += (float)power*KCM*T_SAMPLE - P_LOSS*(float)(temp-T_AMB)/T_AMB*KCM*T_SAMPLE;
|
|
|
|
temp = fmax(T_AMB, fmin(T_MAX, temp));
|
|
|
|
printf("power: %d\n", power);
|
|
printf("temp : %f\n", temp);
|
|
|
|
*pUART1_data = pi[3];
|
|
*pUART1_data = pi[2];
|
|
*pUART1_data = pi[1];
|
|
*pUART1_data = pi[0];
|
|
i++;
|
|
g_rx_cmd_valid = 0;
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
}
|