SPI.write_readinto not working anymore after firmwareupdate



  • I updated the firmware from version 1.10.2.b1 to ver. 1.15.0.b1. after the update the command spi.write_readinto returned only zeros. I checked MISO, MOSI, CLK and CS lines everything as expected.
    I then went back in firmware to ver. 1.10.2.b1 as described in: https://forum.pycom.io/topic/517/downgrading-firmware-advanced-users
    With old firmware the same code is running again.
    Is there a command that changed with firmwareupdate?



  • @seb
    Thanks!
    Someone should fix this in doc. I'm running the latest stable firmware on wipy3, SPI device is hooked to default pins (as in pinout diagram) and it wasn't working until I've explicitly passed them in pins during SPI initialization.
    Most of the day yesterday I was struggling to make it work.



  • @tom

    The safe boot functionality of pin 12 has always been like that, the only advice I can give to work around this issue is to modify the micropython firmware to use a different pin and build it from source. The change is quite trivial, you will just need to change this one line to a different GPIO number, https://github.com/pycom/pycom-micropython-sigfox/blob/ec4c3bb22a59a06870f37ea05d4be054c9bf44ef/esp32/boards/LOPY/mpconfigboard.h#L15



  • I'am using L01 and it is soldered on a PCB I made myself. Therefore changing from Pin12 to Pin14 is not possible for me.

    Instead I tested:

       spi = SPI(0, mode = SPI.MASTER, baudrate=2000000, polarity=0, phase=0, pins=('P10','P11','P12'))
    

    It works again. But using Pin12 as MISO is not a good idea anyway since MISO in my case is held high by the slave. Pin12 is used to change the boot mode as well. If it's hold high long enough it selects factory firmware instead.
    In my case this prevents the board from autostarting boot.py and main.py.

    Is there a workaround?



  • Can you try specifying the pin in your code like so:

    spi = SPI(0, mode=SPI.MASTER, baudrate=2000000, polarity=0, phase=0, pins=('P10','P11','P14'))
    

    We changed our default SPI pins recently and this might be what's causing the issue



  • main.py

    import machine
    import os
    from machine import UART
    from machine import Pin
    from machine import SPI

    #MS5611_CS = Pin('P3', mode= Pin.OUT) #Pulling CS-Pin LOW activates the device
    LIS331_CS = Pin('P2', mode= Pin.OUT)
    MCP_00_CS = Pin('P22', mode= Pin.OUT)
    MCP_01_CS = Pin('P21', mode= Pin.OUT)
    #Pulling CS-Pin LOW activates the device

    #MS5611_CS.value(1)- is defined in the class
    LIS331_CS.value(1)
    MCP_00_CS.value(1)
    MCP_01_CS.value(1)

    spi = SPI(0, mode = SPI.MASTER, baudrate=2000000, polarity=0, bits=32, phase=0)

    baro = MS5611()
    baro.reset()

    while True:

    A= baro.get_altitude_temp()
    print(A)
    time.sleep(1)
    

    library
    from machine import SPI
    from machine import Pin
    import time

    class MS5611:

    CMD_MS5611_RESET = const(0x1E)
    CMD_MS5611_PROM_Setup = const(0xA0)
    CMD_MS5611_PROM_C1 = const(0xA2)
    CMD_MS5611_PROM_C2 = const(0xA4)
    CMD_MS5611_PROM_C3 = const(0xA6)
    CMD_MS5611_PROM_C4 = const(0xA8)
    CMD_MS5611_PROM_C5 = const(0xAA)
    CMD_MS5611_PROM_C6 = const(0xAC)
    CMD_MS5611_PROM_CRC = const(0xAE)
    CMD_CONVERT_D1_OSR4096 = const(0x48)   # Maximum resolution (oversampling)
    CMD_CONVERT_D2_OSR4096 = const(0x58)   # Maximum resolution (oversampling)
    
    def __init__(self, CS_Pin='P3'):
        #spi = SPI(0, mode = SPI.MASTER, baudrate=2000000, polarity=0, bits=32, phase=0)
        self.CS = Pin(CS_Pin, mode= Pin.OUT)  #Pulling CS-Pin LOW activates the device
    
    def reset(self):
        self.CS.value(0)
        time.sleep(1)
        spi.write(bytes([CMD_MS5611_RESET]))
        self.CS.value(1)
        time.sleep(2)
        self.calib = self._get_calibration()
    
    def _read_calib_16(self,reg):
        buf3 = bytearray(3)
        self.CS.value(0)
        spi.write_readinto(bytes([reg,0x00,0x00]),buf3)
        self.CS.value(1)
        data = buf3[1]*256+buf3[2]
        return data
    
    def _read_raw_32(self,reg):
        buf4 = bytearray(4)
        self.CS.value(0)
        spi.write(bytes([reg]))
        self.CS.value(1)
        time.sleep_ms(10) #needs to be at least 8.22ms
        self.CS.value(0)
        spi.write_readinto(bytes([0x00,0x00,0x00,0x00]),buf4)
        self.CS.value(1)
        val = 65536*buf4[1]+256*buf4[2]+buf4[1]
        return val
    
    def _get_calibration(self): #is called in reset stored as self.calib
        C1 = self._read_calib_16(CMD_MS5611_PROM_C1)
        C2 = self._read_calib_16(CMD_MS5611_PROM_C2)
        C3 = self._read_calib_16(CMD_MS5611_PROM_C3)
        C4 = self._read_calib_16(CMD_MS5611_PROM_C4)
        C5 = self._read_calib_16(CMD_MS5611_PROM_C5)
        C6 = self._read_calib_16(CMD_MS5611_PROM_C6)
        return C1,C2,C3,C4,C5,C6
    
    def _get_temperature_raw(self):
        T_raw = self._read_raw_32(CMD_CONVERT_D2_OSR4096)
        return T_raw
    
    def _get_pressure_raw(self):
        p_raw = self._read_raw_32(CMD_CONVERT_D1_OSR4096)
        return p_raw
    
    def _calc_calibration_values(self):
        T_raw = self._get_temperature_raw()
        dT = T_raw-self.calib[4]*256
        OFF = self.calib[1]*65536+(self.calib[3]*dT)/128
        SENS = self.calib[0]*32768+(self.calib[2]*dT)/256
        return dT, OFF, SENS
    
    def get_temp_press(self):
        [dT,OFF,SENS]=self._calc_calibration_values()
        p_raw =self._get_pressure_raw()
        TEMP = 2000+dT*self.calib[5]/8388608
        PRESS = (p_raw*SENS/2097152-OFF)/32768
        return TEMP/100, PRESS
    
    def get_altitude_temp(self): #calculation Altitude using standart 1013.25 mbar at sea level reference
        [TEMP,PRESS] =self.get_temp_press()
        tmp = (PRESS/101325.0)
        tmp = pow(tmp, 0.190295)
        Altitude = 44330.0 * (1.0-tmp)
        return Altitude, TEMP


  • Those files haven't been properly uploaded by the looks of it. In your code to do specify the exact pins that the SPI should use or are you replying on the defaults? The defaults changed recently because on newer modules P12 controls which WiFi antenna is used. If you don't specify the pins, add this and let me know if this fixes it.



  • Here is my code. It is written for the barometer MS5611.
    [1_1518533565589_main.py](Lade 100% hoch) [0_1518533565589_boot.py](Lade 100% hoch) [0_1518533588418_MS5611.py](Lade 100% hoch)



  • Hi,

    I just tested this function on version 1.15.0.b1 and it worked successfully. This code has not been changed so maybe the issue lies elsewhere. Could you please share your source code?


Log in to reply
 

Pycom on Twitter