pinsh8051/pinsh.c

171 lines
2.1 KiB
C
Raw Permalink Normal View History

2024-05-26 15:35:24 +02:00
#include "mc8051fun/mc8051fun.h"
#include "pinsh.h"
void (*pinsh_output_chr)(char);
uint8_t pinsh_buf[PINSH_BUF_SIZE];
volatile int pinsh_buf_pos=0;
2024-05-26 16:10:33 +02:00
void pinsh_uart_irq_handler()
2024-05-26 15:35:24 +02:00
{
if (RI) {
unsigned char b = SBUF;
if (b=='\r'){
//pinsh_go=1;
pinsh_exec(pinsh_buf);
} else {
SBUF=b;
if (pinsh_buf_pos < PINSH_BUF_SIZE-1){
pinsh_buf[pinsh_buf_pos++]=b;
pinsh_buf[pinsh_buf_pos]=0;
}
}
RI=0;
}
}
static void pinsh_output(const char *str)
{
while(*str)
pinsh_output_chr(*str++);
}
void pinsh_welcome_msg()
{
pinsh_output("Pinsh 8051, V1.0\r\n");
}
static void show_port(int n)
{
pinsh_output("P");
uint8_t b = getport(n);
pinsh_output_chr(n+0x30);
pinsh_output("=");
for (int i=0; i<8; i++) {
if (b&(1<<(7-i)))
pinsh_output("1");
else
pinsh_output("0");
}
pinsh_output("\r\n");
}
static void show_stat()
{
for (int i=0; i<4; i++)
show_port(i);
/* uint8_t b = P0;
show_port("P0",b);
b = P1;
show_port("P1",b);
b = P2;
show_port("P2",b);
b = P3;
show_port("P3",b);
*/
}
static void syntax_error()
{
pinsh_output("Syntax error\r\n");
}
static void assign_pin(int n, int p, char val)
{
if (val){
switch(n){
case 0:
P0 |= 1<<p;
return;
case 1:
P1 |= 1<<p;
return;
case 2:
P2 |= 1<<p;
return;
case 3:
P3 |= 1<<p;
return;
}
}
switch(n){
case 0:
P0 &= (1<<p)^0xff;
return;
case 1:
P1 &= (1<<p)^0xff;
return;
case 2:
P2 &= (1<<p)^0xff;
return;
case 3:
P3 &= (1<<p)^0xff;
return;
}
}
static void assign_port(int n, const char*cmd)
{
for (int i=0; i<8 && cmd[i]!=0; i++)
{
if(cmd[i]!='0' && cmd[i]!='1'){
syntax_error();
return;
}
}
for (int i=0; i<8 && cmd[i]!=0; i++)
{
assign_pin(n,7-i,cmd[i]-0x30);
}
}
static void pinsh_exec_p(const char *cmd)
{
if (!cmd[0]){
show_stat();
return;
}
int n = cmd[0];
if(n<0x30 || n>0x33){
syntax_error();
return;
}
if(cmd[1]==0){
show_port(n-0x30);
return;
}
if(cmd[1]=='='){
assign_port(n-0x30,cmd+2);
return;
}
}
void pinsh_exec(const char *cmd)
{
2024-05-26 16:10:33 +02:00
pinsh_output("\r\n");
2024-05-26 15:35:24 +02:00
if(cmd[0]=='p')
pinsh_exec_p(cmd+1);
2024-05-26 16:10:33 +02:00
pinsh_buf_pos=0;
2024-05-26 15:35:24 +02:00
}