// automatically generated by spin2cpp v6.9.5-HEAD-v6.9.5 on Sun May 05 15:31:17 2024 // command line: spin2cpp -I.. --p2 --ccode c:\Propeller2\Micropython\SimplestSerial\simplestserial.spin2 // Simpleast Serial Version 1a. // Rigged to "Begin" with 2 MHz baud for use with PropTool Debug and/or FlexProp and/or Spin Tools IDE // Note: If you do use a debug statement in PropTool, you must call Begin() again before using this serial or it will hang // (This is because PropTool's debug resets the serial port when used) // Some basid debug functions, like dUDEC(), that also restart this serial are provided here. // Based on SmartSerial.spin2 that is part of FlexProp // simple smart pin serial object for P2 eval board // implements a subset of FullDuplexSerial functionality // Does not use a cog // RJA added a few things and changing definition of start to be easier to use // Written by Eric R. Smith // Copyright 2020 Total Spectrum Software Inc. // Distributed under the MIT License (See LICENSE.md) // // 11Jun23: Version 2 has fixed RX and TX pins to make it easier to use in subobjects // #include #define __SPIN2CPP__ #include #include "simplestserial.h" #if defined(__GNUC__) #define INLINE__ static inline #else #define INLINE__ static #endif INLINE__ int32_t Rotl__(uint32_t a, uint32_t b) { return (a<>(32-b)); } INLINE__ int32_t Rotr__(uint32_t a, uint32_t b) { return (a>>b) | (a<<(32-b)); } INLINE__ int32_t Shr__(uint32_t a, uint32_t b) { return (a>>b); } static unsigned char dat[] = { //3f=63, 3e=62 0x3f, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x80, 0x84, 0x1e, 0x00, //RJA changing pins so as to use WiFi module for serial I/O //P62 is output of P2, needs to go to DI //P63 is input to P2, needs to go to DO 29, 0x00, 0x00, 0x00, 30, 0x00, 0x00, 0x00, 0x80, 0x84, 0x1e, 0x00, }; void simplestserial_Begin(void) { // Start at 2MHz baud simplestserial_Start(230400); } void simplestserial_Start(int32_t baudrate) { int32_t bitperiod, bit_mode; // start using user defined pins // Note: The "mode" parameter, used by fullduplexserial, has been removed // calculate delay between bits bitperiod = _clkfreq/ baudrate; // calculate smartpin mode for 8 bits per character bit_mode = 7 + (bitperiod << 16); // set up the transmit pin _pinf(((int32_t *)&dat[4])[0]); _wrpin(((int32_t *)&dat[4])[0], simplestserial__TXMODE); _wxpin(((int32_t *)&dat[4])[0], bit_mode); // turn smartpin on by making the pin an output _pinl(((int32_t *)&dat[4])[0]); // set up the receive pin _pinf(29); _wrpin(29, simplestserial__RXMODE); _wxpin(29, bit_mode); // turn smartpin on _pinl(29); ((int32_t *)&dat[8])[0] = baudrate; } /* void simplestserial_pinf(int32_t x) { } void simplestserial_pinl(int32_t x) { } int32_t simplestserial_pinr(int32_t x) { int32_t r; r = 0; return r; } void simplestserial_rdpin(int32_t x) { } void simplestserial_dUDEC(int32_t x) { // Debug an unsigned decimal number simplestserial_Start(((int32_t *)&dat[8])[0]); } void simplestserial_dUHEX(int32_t x, int32_t n) { // Debug an unsigned hexidecimal number simplestserial_Start(((int32_t *)&dat[8])[0]); } */ void simplestserial_tx(int32_t val) { // send one byte _wypin(((int32_t *)&dat[4])[0], val); // rja simplestserial_txflush(); } void simplestserial_crlf(void) { // send CR and LF // CR simplestserial_tx(13); // LF simplestserial_tx(10); } void simplestserial_println(int32_t p) { // print a string and then do CRLF simplestserial_str(p); simplestserial_crlf(); } void simplestserial_txflush(void) { int32_t z; // wait for character sent do { z = _pinr(((int32_t *)&dat[4])[0]); } while (z == 0); } int32_t simplestserial_rxcheck(void) { int32_t rxpin, z;//,t; int32_t rxbyte; // check if byte received (never waits) ' returns -1 if no byte, otherwise byte rxbyte = -1; rxpin = 29;//((int32_t *)&dat[0])[0]; z = _pinr(rxpin); /* if (z) { z=_rdpin(rxpin)>>24; if (z>0) { printf("got byte %x/r/n",z); rxbyte =z; } } //rxbyte = _rdpin(rxpin);//RJA thinks the below is breaking it... */ if (z) { rxbyte= _rdpin(rxpin)>>24; //rxbyte = Shr__(( t, 0 ), 24); //_akpin(rxpin); //printf(" %x ",rxbyte); } return rxbyte; } void simplestserial_rxFlush(void) { int32_t i; // RJA added this for compatibility with jm_fullduplexserial, but might not make any sense... do { i = simplestserial_rxcheck(); } while (!(i == (-1))); } int32_t simplestserial_rx(void) { int32_t v; // receive a byte (waits until one ready) do { // rja v = simplestserial_rxcheck(); } while (v == (-1)); return v; } void simplestserial_str(int32_t s) { int32_t c, _temp__0000; // transmit a string while (( ( (_temp__0000 = ((char *)(s++))[0]), (c = _temp__0000) ), _temp__0000 ) != 0) { simplestserial_tx(c); } } int32_t simplestserial_dec(int32_t value) { int32_t i, x, _idx__0000; int32_t result; // print decimal value 'rja added :result // Print a decimal number result = 0; // Check for max negative x = -(value == (int32_t)0x80000000U); if (value < 0) { // RJA 'If negative, make positive; adjust for max negative value = abs((value + x)); // and output sign simplestserial_tx('-'); } // Initialize divisor i = 1000000000; for(_idx__0000 = 0; _idx__0000 < 10; _idx__0000++) { // Loop for 10 digits if (value >= i) { // rja // If non-zero digit, output digit; adjust for max negative simplestserial_tx(((value / i) + '0') + (x * -(i == 1))); // and digit from value value = value % i; // flag non-zero found result = -1; } else { if ((result) || (i == 1)) { // If zero digit (or only digit) output it simplestserial_tx('0'); } } // Update divisor i = i / 10; } return result; } void simplestserial_hex(int32_t val, int32_t digits) { int32_t shft, x, _idx__0000; // print hex value shft = (digits - 1) << 2; for(_idx__0000 = digits; _idx__0000 != 0; --_idx__0000) { x = (Shr__(val, shft)) & 0xf; shft = shft - 4; if (x >= 10) { // rja x = (x - 10) + 'A'; } else { x = x + '0'; } simplestserial_tx(x); } } void simplestserial_Bin(int32_t value, int32_t digits) { int32_t _temp__0000, _idx__0001; // pring binary value value = value << (32 - digits); for(_idx__0001 = digits; _idx__0001 != 0; --_idx__0001) { /* Transmit the ASCII value of each binary digitÿ */ simplestserial_tx((( ( (_temp__0000 = Rotl__(value, 1)), (value = _temp__0000) ), _temp__0000 ) & 0x1) + '0'); } }