uart (esp8266)

Discuss game and other programming topics not specifically covered in another forum

Moderator: Programming Moderators

User avatar
SevenFFF
Posts: 189
Joined: Mon Jun 05, 2017 5:30 pm
Location: USA

Re: uart (esp8266)

Postby SevenFFF » Thu Jun 07, 2018 11:09 pm

These are the notes and some example code for setting the baud rate. See the section Changes from TBU .29 onwards at the end.

UsingUART2andWIFI.txt

I think Tim can give you the source when he returns.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
Spectron 2084blog

User avatar
crgimenes
Posts: 18
Joined: Tue Jun 06, 2017 11:36 pm

Re: uart (esp8266)

Postby crgimenes » Thu Jun 07, 2018 11:13 pm

Ok, after replacing the uart command with the new version I am able to communicate with the module as expected. 'AT' now returns 'OK'.

Any idea why the little program I did is not working? I'm loading it as TAP file, not as binary, can that be the problem?

Alcoholics Anonymous
Posts: 463
Joined: Mon May 29, 2017 7:00 pm

Re: uart (esp8266)

Postby Alcoholics Anonymous » Thu Jun 07, 2018 11:36 pm

Re: z88dk

Code: Select all

#define TX 4923
#define RX 5179

void sendChar(char c) {
  char *txPort = (char *)TX;
  *txPort = c;
  printf("%c", c);
}
These are memory addresses so you are in fact writing into memory instead of to an io port. Also be careful to include <arch/zxn.h> and not <arch/zx.h>. This last one does not have the zx next defines in it as it's for the regular spectrum!

You have raw access to the uart but the streamed io (so you can use fprintf, fscanf, read, write, etc) is not in yet.

A description of the uart is here:
https://github.com/z88dk/z88dk/blob/mas ... xn_uart.m4

And arch/zxn.h defines these things for the uart:

Code: Select all

// io ports - uart

__sfr __banked __at 0x143b IO_143B;
__sfr __banked __at __IO_UART_RX IO_UART_RX;
__sfr __banked __at __IO_UART_BAUD_RATE IO_UART_BAUD_RATE;

__sfr __banked __at 0x133b IO_133B;
__sfr __banked __at __IO_UART_TX IO_UART_TX;
__sfr __banked __at __IO_UART_STATUS IO_UART_STATUS;

// 0x143b, IO_UART_BAUD_RATE

// actual uart clock as a function of video timing 0-7

#define CLK_28_0  __CLK_28_0
#define CLK_28_1  __CLK_28_1
#define CLK_28_2  __CLK_28_2
#define CLK_28_3  __CLK_28_3
#define CLK_28_4  __CLK_28_4
#define CLK_28_5  __CLK_28_5
#define CLK_28_6  __CLK_28_6
#define CLK_28_7  __CLK_28_7

// these are calculated using a nominal 28MHz clock

#define IUBR_115200_L  __IUBR_115200_L
#define IUBR_115200_H  __IUBR_115200_H
#define IUBR_57600_L  __IUBR_57600_L
#define IUBR_57600_H  __IUBR_57600_H
#define IUBR_38400_L  __IUBR_38400_L
#define IUBR_38400_H  __IUBR_38400_H
#define IUBR_31250_L  __IUBR_31250_L
#define IUBR_31250_H  __IUBR_31250_H
#define IUBR_19200_L  __IUBR_19200_L
#define IUBR_19200_H  __IUBR_19200_H
#define IUBR_9600_L  __IUBR_9600_L
#define IUBR_9600_H  __IUBR_9600_H
#define IUBR_4800_L  __IUBR_4800_L
#define IUBR_4800_H  __IUBR_4800_H
#define IUBR_2400_L  __IUBR_2400_L
#define IUBR_2400_H  __IUBR_2400_H

#define IO_143B_115200_L  __IO_143B_115200_L
#define IO_143B_115200_H  __IO_143B_115200_H
#define IO_143B_57600_L  __IO_143B_57600_L
#define IO_143B_57600_H  __IO_143B_57600_H
#define IO_143B_38400_L  __IO_143B_38400_L
#define IO_143B_38400_H  __IO_143B_38400_H
#define IO_143B_31250_L  __IO_143B_31250_L
#define IO_143B_31250_H  __IO_143B_31250_H
#define IO_143B_19200_L  __IO_143B_19200_L
#define IO_143B_19200_H  __IO_143B_19200_H
#define IO_143B_9600_L  __IO_143B_9600_L
#define IO_143B_9600_H  __IO_143B_9600_H
#define IO_143B_4800_L  __IO_143B_4800_L
#define IO_143B_4800_H  __IO_143B_4800_H
#define IO_143B_2400_L  __IO_143B_2400_L
#define IO_143B_2400_H  __IO_143B_2400_H

// 0x133b, IO_UART_STATUS

#define IUS_RX_AVAIL  __IUS_RX_AVAIL
#define IUS_RX_FULL  __IUS_RX_FULL
#define IUS_TX_BUSY  __IUS_TX_BUSY

#define IO_133B_RX_AVAIL  __IO_133B_RX_AVAIL
#define IO_133B_RX_FULL  __IO_133B_RX_FULL
#define IO_133B_TX_BUSY __IO_133B_TX_BUSY
sfrs (special function registers) are how io ports are defined in C. The header defines them for you.

If you read the uart document above you should be able to follow this totally untested program:

Code: Select all

// zcc +zxn -vn -startup=0 -clib=sdcc_iy -SO3 --max-allocs-per-node200000 uart.c -o uart -subtype=sna -Cz"--128 --clean" -create-app

#include <arch/zxn.h>
#include <stdio.h>
#include <stdint.h>

uint32_t uart_clock[] = {
   CLK_28_0,
   CLK_28_1,
   CLK_28_2,
   CLK_28_3,
   CLK_28_4,
   CLK_28_5,
   CLK_28_6,
   CLK_28_7
};

void set_baud_rate(uint32_t baud)
{
   unsigned int prescalar;
   
   prescalar = uart_clock[ZXN_READ_REG(REG_VIDEO_TIMING)] / baud;
   
   IO_UART_BAUD_RATE = prescalar & 0x7f;                   // lower 7 bits
   IO_UART_BAUD_RATE = ((prescalar >> 7) & 0x7f) | 0x80;   // upper 7 bits
}

void send_char(unsigned char c)
{
   while (IO_UART_STATUS & IUS_TX_BUSY) ;
   IO_UART_TX = c;
}

void send_string(unsigned char *s)
{
   while (*s)
   {
      if (*s == '\n')
         send_char('\r');
      
      send_char(*s++);
   }
}

unsigned char buf[128];

void main(void)
{
   zx_border(INK_WHITE);
   zx_cls(INK_BLACK | PAPER_WHITE);
  
   set_baud_rate(115200UL);
   
   while (1)
   {
      // read input from keyboard
      
      fgets(buf, sizeof(buf), stdin);
      fflush(stdin);  // get rid of any chars still in the edit buffer
      
      // transmit message
      
      if (*buf && (*buf != '\n'))  // can press enter to try to get more response
         send_string(buf);
      
      // read response
      
      for (unsigned char i = 0; i != 100; ++i)    // wait a bit for a response
         if (IO_UART_STATUS & IUS_RX_AVAIL) break;

      while (IO_UART_STATUS & IUS_RX_AVAIL)
         fputc(IO_UART_RX, stdout);
   }
}
Compile line a the top for a 128k sna. You may prefer a tap instead so you can return to basic.
No idea if it works :D
Last edited by Alcoholics Anonymous on Fri Jun 08, 2018 12:12 am, edited 2 times in total.

User avatar
crgimenes
Posts: 18
Joined: Tue Jun 06, 2017 11:36 pm

Re: uart (esp8266)

Postby crgimenes » Thu Jun 07, 2018 11:59 pm

Fantastic!
I have a lot to study, thanks for the help!
I'll see what I can do ... soon I'll return with more doubts :D

Alcoholics Anonymous
Posts: 463
Joined: Mon May 29, 2017 7:00 pm

Re: uart (esp8266)

Postby Alcoholics Anonymous » Fri Jun 08, 2018 12:13 am

I made a pretty big mistake there that is now corrected.

fputc(IO_UART_RX, stdin);

should be

fputc(IO_UART_RX, stdout);

You can't print chars to an input stream.

User avatar
SevenFFF
Posts: 189
Joined: Mon Jun 05, 2017 5:30 pm
Location: USA

Re: uart (esp8266)

Postby SevenFFF » Fri Jun 08, 2018 12:17 am

Don’t cross the streams!!
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
Spectron 2084blog

User avatar
crgimenes
Posts: 18
Joined: Tue Jun 06, 2017 11:36 pm

Re: uart (esp8266)

Postby crgimenes » Fri Jun 08, 2018 3:11 am

hey, the program compiled and worked very well, I tested it as sna and as tap.
Now I have a base to keep playing. :D
Thank you guys for the help!

Alcoholics Anonymous
Posts: 463
Joined: Mon May 29, 2017 7:00 pm

Re: uart (esp8266)

Postby Alcoholics Anonymous » Fri Jun 08, 2018 4:08 am

A pleasant surprise it worked. It's the first time a uart communication has been tried from c afaik.

User avatar
mitja_i
Posts: 213
Joined: Tue May 30, 2017 6:27 am
Location: Ljubljana, Slovenia
Contact:

Re: uart (esp8266)

Postby mitja_i » Fri Jun 08, 2018 7:01 am

Off topic:
Timbucus wrote:
Thu Jun 07, 2018 10:35 pm
...as Forum does not allow file uploads at the moment.
I tested it and it works fine. What was the problem?
Backer 423 @ 37% funded 29 days left
---
ZX Spectrum Next glossary

User avatar
Timbucus
Posts: 210
Joined: Tue May 30, 2017 7:43 pm

Re: uart (esp8266)

Postby Timbucus » Fri Jun 08, 2018 7:10 pm

Must have been a problem with my browser as the Attachments TAB would not select - then I got invalid extension for .txt. Anyway here is the UART22c and the README. Look out for the V3 IRQ driven stuff mentioned very soon.
Attachments
UART22C.zip
UART22C for TBU>.29 with README
(5.32 KiB) Downloaded 40 times
I'm Infinite Imaginations when not in work... PAWS for thought.


Who is online

Users browsing this forum: No registered users and 1 guest