Browse Source

Merge branch 'master' of github.com:jamesbowman/gd2-lib

James Bowman 1 year ago
parent
commit
67e53292df
4 changed files with 26 additions and 21 deletions
  1. 11 6
      pc/SPI.h
  2. 3 3
      pc/spidriver.cpp
  3. 3 3
      pc/spidriver.h
  4. 9 9
      transports/tr-spidriver.h

+ 11 - 6
pc/SPI.h

@@ -113,18 +113,23 @@ static void digitalWrite(int pin, int state)
 {
 }
 
-static uint32_t __lfsr = 1;
+static uint32_t seed = 7;
+
 static void randomSeed(unsigned int n)
 {
-  __lfsr = 1 | n;
+  seed = n | (n == 0);
 }
 
+static unsigned int xrandom()
+{
+  seed ^= seed << 13;
+  seed ^= seed >> 17;
+  seed ^= seed << 5;
+  return seed;
+}
 static unsigned int random(unsigned int n)
 {
-  uint32_t a = __lfsr;
-  for (int i = 0; i < 33; i++)
-    __lfsr = (__lfsr >> 1) ^ (-(__lfsr & 1u) & 0xD0000001u); 
-  return (a ^ __lfsr) % n;
+  return xrandom() % n;
 }
 static unsigned int random(unsigned int a, unsigned int b)
 {

+ 3 - 3
pc/spidriver.cpp

@@ -140,7 +140,7 @@ void spi_setb(SPIDriver *sd, char v)
   sd->b = v;
 }
 
-void spi_write(SPIDriver *sd, size_t nn, const char bytes[])
+void spi_write(SPIDriver *sd, const char bytes[], size_t nn)
 {
   for (size_t i = 0; i < nn; i += 64) {
     size_t len = ((nn - i) < 64) ? (nn - i) : 64;
@@ -150,7 +150,7 @@ void spi_write(SPIDriver *sd, size_t nn, const char bytes[])
   }
 }
 
-void spi_read(SPIDriver *sd, size_t nn, char bytes[])
+void spi_read(SPIDriver *sd, char bytes[], size_t nn)
 {
   for (size_t i = 0; i < nn; i += 64) {
     size_t len = ((nn - i) < 64) ? (nn - i) : 64;
@@ -160,7 +160,7 @@ void spi_read(SPIDriver *sd, size_t nn, char bytes[])
   }
 }
 
-void spi_writeread(SPIDriver *sd, size_t nn, char bytes[])
+void spi_writeread(SPIDriver *sd, char bytes[], size_t nn)
 {
   for (size_t i = 0; i < nn; i += 64) {
     size_t len = ((nn - i) < 64) ? (nn - i) : 64;

+ 3 - 3
pc/spidriver.h

@@ -20,8 +20,8 @@ void spi_sel(SPIDriver *sd);
 void spi_unsel(SPIDriver *sd);
 void spi_seta(SPIDriver *sd, char v);
 void spi_setb(SPIDriver *sd, char v);
-void spi_write(SPIDriver *sd, size_t nn, const char bytes[]);
-void spi_read(SPIDriver *sd, size_t nn, char bytes[]);
-void spi_writeread(SPIDriver *sd, size_t nn, char bytes[]);
+void spi_write(SPIDriver *sd, const char bytes[], size_t nn);
+void spi_read(SPIDriver *sd, char bytes[], size_t nn);
+void spi_writeread(SPIDriver *sd, char bytes[], size_t nn);
 
 #endif

+ 9 - 9
transports/tr-spidriver.h

@@ -44,14 +44,14 @@ public:
   void cmd_n(byte *s, size_t n) {
     flush();
     getspace(n);
-    spi_write(&sd, n, (char*)s);
+    spi_write(&sd, (char*)s, n);
     wp += n;
   }
   void hostcmd(uint8_t a)
   {
     char buf[3] = {(char)a, 0, 0};
     spi_sel(&sd);
-    spi_write(&sd, 3, buf);
+    spi_write(&sd, buf, 3);
     spi_unsel(&sd);
   }
 
@@ -77,7 +77,7 @@ public:
     __end();
     char buf[4] = {WADDR3(a), (char)v};
     spi_sel(&sd);
-    spi_write(&sd, sizeof(buf), buf);
+    spi_write(&sd, buf, sizeof(buf));
     spi_unsel(&sd);
     stream();
   }
@@ -113,8 +113,8 @@ public:
     __end();
     char buf[4] = {ADDR3(a)};
     spi_sel(&sd);
-    spi_write(&sd, sizeof(buf), buf);
-    spi_read(&sd, n, (char*)dst);
+    spi_write(&sd, buf, sizeof(buf));
+    spi_read(&sd, (char*)dst, n);
     spi_unsel(&sd);
     stream();
   }
@@ -123,7 +123,7 @@ public:
   void flush() {
     if (nbuf) {
       getspace(nbuf);
-      spi_write(&sd, nbuf, (char*)buf);
+      spi_write(&sd, (char*)buf, nbuf);
       wp += nbuf;
       nbuf = 0;
     }
@@ -140,13 +140,13 @@ public:
   void __wstart(uint32_t a) {
     char buf[3] = {WADDR3(a)};
     spi_sel(&sd);
-    spi_write(&sd, sizeof(buf), buf);
+    spi_write(&sd, buf, sizeof(buf));
   }
 
   unsigned int __rd16(uint32_t a) {
     char buf[6] = {ADDR3(a), (char)-1, (char)-1, (char)-1};
     spi_sel(&sd);
-    spi_writeread(&sd, sizeof(buf), buf);
+    spi_writeread(&sd, buf, sizeof(buf));
     spi_unsel(&sd);
     return *(uint16_t*)&buf[4];
   }
@@ -154,7 +154,7 @@ public:
   void __wr16(uint32_t a, unsigned int v) {
     char buf[5] = {WADDR3(a), (char)v, (char)(v >> 8)};
     spi_sel(&sd);
-    spi_write(&sd, sizeof(buf), buf);
+    spi_write(&sd, buf, sizeof(buf));
     spi_unsel(&sd);
   }
   void stop() {} // end the SPI transaction