def main(): """Initialize SPI, send a few bytes, then close and exit.""" WATCH_DOG = 85 WATCH_DOG_REPLY = 170 DUMMY = 255 print 'initializing GPIO:', soc.bcm2835_init() # Reset the AVR and the force GPIO8, pin 24, to high to enable the AVR soc.bcm2835_gpio_fsel(soc.RPI_GPIO_P1_24, soc.BCM2835_GPIO_FSEL_OUTP) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_clr(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) time.sleep(10) print 'initializing SPI:', soc.bcm2835_spi_begin() soc.bcm2835_spi_setBitOrder(soc.BCM2835_SPI_BIT_ORDER_MSBFIRST) soc.bcm2835_spi_setDataMode(soc.BCM2835_SPI_MODE0) soc.bcm2835_spi_setClockDivider(soc.BCM2835_SPI_CLOCK_DIVIDER_65536) soc.bcm2835_spi_chipSelect(soc.BCM2835_SPI_CS1) soc.bcm2835_spi_setChipSelectPolarity(soc.BCM2835_SPI_CS0, soc.LOW) while (True): time.sleep(1) soc.bcm2835_spi_transfer(WATCH_DOG) # send command data_byte = soc.bcm2835_spi_transfer(DUMMY) # read response if data_byte != WATCH_DOG_REPLY: print 'Watchdog reply missing. received: ', data_byte break soc.bcm2835_spi_end() print 'closing GPIO:', soc.bcm2835_close()
def initialize(): """ Clock hardware initialization. Any exceptions raised here should not abort the program, but return a '0' to indicate initialization failure. """ # Initialize RPi GPIO try: gpio_initialized = soc.bcm2835_init() # Reset the AVR and then force GPIO8, pin 24, to high to enable the AVR soc.bcm2835_gpio_fsel(soc.RPI_GPIO_P1_24, soc.BCM2835_GPIO_FSEL_OUTP) _avr_reset() # Initializing SPI soc.bcm2835_spi_begin() soc.bcm2835_spi_setBitOrder(soc.BCM2835_SPI_BIT_ORDER_MSBFIRST) soc.bcm2835_spi_setDataMode(soc.BCM2835_SPI_MODE0) soc.bcm2835_spi_setClockDivider(soc.BCM2835_SPI_CLOCK_DIVIDER_65536) soc.bcm2835_spi_chipSelect(soc.BCM2835_SPI_CS1) soc.bcm2835_spi_setChipSelectPolarity(soc.BCM2835_SPI_CS0, soc.LOW) except: gpio_initialized = 0 return gpio_initialized
def main(): """Initialize SPI, send a few bytes, then close and exit.""" SPI_CMD_WDOG = 85 WATCH_DOG_REPLY = 170 SPI_CMD_MINUTES = 1 SPI_CMD_TENS_MINUTES = 2 SPI_CMD_HOURS = 3 SPI_CMD_TENS_HOURS = 4 DUMMY = 255 print 'initializing GPIO:', soc.bcm2835_init() # Reset the AVR and the force GPIO8, pin 24, to high to enable the AVR soc.bcm2835_gpio_fsel(soc.RPI_GPIO_P1_24, soc.BCM2835_GPIO_FSEL_OUTP) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_clr(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) time.sleep(1) print 'initializing SPI:', soc.bcm2835_spi_begin() soc.bcm2835_spi_setBitOrder(soc.BCM2835_SPI_BIT_ORDER_MSBFIRST) soc.bcm2835_spi_setDataMode(soc.BCM2835_SPI_MODE0) soc.bcm2835_spi_setClockDivider(soc.BCM2835_SPI_CLOCK_DIVIDER_65536) soc.bcm2835_spi_chipSelect(soc.BCM2835_SPI_CS1) soc.bcm2835_spi_setChipSelectPolarity(soc.BCM2835_SPI_CS0, soc.LOW) while (True): for i in range(0, 5): time.sleep(1) soc.bcm2835_spi_transfer(SPI_CMD_MINUTES) # send command data_byte = soc.bcm2835_spi_transfer(i) # send digit soc.bcm2835_spi_transfer(SPI_CMD_TENS_MINUTES) # send command data_byte = soc.bcm2835_spi_transfer(i * 2) # send digit soc.bcm2835_spi_transfer(SPI_CMD_HOURS) # send command data_byte = soc.bcm2835_spi_transfer((i * 2) + 1) # send digit soc.bcm2835_spi_transfer(SPI_CMD_TENS_HOURS) # send command data_byte = soc.bcm2835_spi_transfer(i + 5) # send digit print 'cycle: ', i # Periodic watch-dog 'keep alive' soc.bcm2835_spi_transfer(SPI_CMD_WDOG) # send command data_byte = soc.bcm2835_spi_transfer(DUMMY) # read response if data_byte != WATCH_DOG_REPLY: print 'Watchdog reply missing. received: ', data_byte break soc.bcm2835_spi_end() print 'closing GPIO:', soc.bcm2835_close()
def spi_init(): if io.bcm2835_spi_begin()==0: print('SPI failed to initialize') else: print('SPI sucessfully initialized') io.bcm2835_spi_setBitOrder(io.BCM2835_SPI_BIT_ORDER_MSBFIRST); io.bcm2835_spi_setDataMode(io.BCM2835_SPI_MODE0); #io.bcm2835_spi_setClockDivider(io.BCM2835_SPI_CLOCK_DIVIDER_65536); #SPI_CLOCK_DIVIDER_128=128: about 2.0MHz io.bcm2835_spi_setClockDivider(io.BCM2835_SPI_CLOCK_DIVIDER_128); io.bcm2835_spi_chipSelect(io.BCM2835_SPI_CS0); io.bcm2835_spi_setChipSelectPolarity(io.BCM2835_SPI_CS0, io.LOW); print('SPI configured')
def main(): """Initialize SPI, send a few bytes, then close and exit.""" print 'initializing GPIO:', soc.bcm2835_init() print 'initializing SPI:', soc.bcm2835_spi_begin() soc.bcm2835_spi_setBitOrder(soc.BCM2835_SPI_BIT_ORDER_MSBFIRST) soc.bcm2835_spi_setDataMode(soc.BCM2835_SPI_MODE0) soc.bcm2835_spi_setClockDivider(soc.BCM2835_SPI_CLOCK_DIVIDER_65536) soc.bcm2835_spi_chipSelect(soc.BCM2835_SPI_CS1) soc.bcm2835_spi_setChipSelectPolarity(soc.BCM2835_SPI_CS0, soc.LOW) for i in range(0, 100): data_byte = soc.bcm2835_spi_transfer(i) print 'sent:', i, 'returned:', data_byte soc.bcm2835_spi_end() print 'closing GPIO:', soc.bcm2835_close()
def main(): """Initialize SPI, send a few bytes, then close and exit.""" SPI_CMD_MINUTES = 1 SPI_CMD_TENS_MINUTES = 2 SPI_CMD_HOURS = 3 SPI_CMD_TENS_HOURS = 4 SPI_CMD_BRIGHTNESS = 5 SPI_CMD_GET_LIGHT = 6 SPI_CMD_WDOG = 85 WATCH_DOG_REPLY = 170 DUMMY = 255 # Brightness value filter br = [0, 0, 0, 0, 0] print 'initializing GPIO:', soc.bcm2835_init() # Reset the AVR and the force GPIO8, pin 24, to high to enable the AVR soc.bcm2835_gpio_fsel(soc.RPI_GPIO_P1_24, soc.BCM2835_GPIO_FSEL_OUTP) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_clr(soc.RPI_GPIO_P1_24) soc.bcm2835_gpio_set(soc.RPI_GPIO_P1_24) print 'initializing SPI:', soc.bcm2835_spi_begin() soc.bcm2835_spi_setBitOrder(soc.BCM2835_SPI_BIT_ORDER_MSBFIRST) soc.bcm2835_spi_setDataMode(soc.BCM2835_SPI_MODE0) soc.bcm2835_spi_setClockDivider(soc.BCM2835_SPI_CLOCK_DIVIDER_65536) soc.bcm2835_spi_chipSelect(soc.BCM2835_SPI_CS1) soc.bcm2835_spi_setChipSelectPolarity(soc.BCM2835_SPI_CS0, soc.LOW) while (True): for i in range(0, 10): time.sleep(0.5) # Get light sensor value, which can be between 0 and 255 soc.bcm2835_spi_transfer(SPI_CMD_GET_LIGHT) # send command light_sensor = soc.bcm2835_spi_transfer(DUMMY) # read response # Convert light sensor input to a 0 to 10 brightness range br.insert(0, int(light_sensor / 20.0)) br = br[:5] brightness = (br[0] + br[1] + br[2] + br[3] + br[4]) / 5 if brightness > 10.0: brightness = 10.0 elif brightness < 1: brightness = 1 print 'Light sensor: ', light_sensor, ' Brightness: ', brightness # Send brightness command soc.bcm2835_spi_transfer(SPI_CMD_BRIGHTNESS) # send command soc.bcm2835_spi_transfer(int(brightness)) # send brightness # Send digits soc.bcm2835_spi_transfer(SPI_CMD_MINUTES) # send command data_byte = soc.bcm2835_spi_transfer(i) # send digit soc.bcm2835_spi_transfer(SPI_CMD_TENS_MINUTES) # send command data_byte = soc.bcm2835_spi_transfer(i) # send digit soc.bcm2835_spi_transfer(SPI_CMD_HOURS) # send command data_byte = soc.bcm2835_spi_transfer(i) # send digit soc.bcm2835_spi_transfer(SPI_CMD_TENS_HOURS) # send command data_byte = soc.bcm2835_spi_transfer(i) # send digit # Periodic watch-dog 'keep alive' soc.bcm2835_spi_transfer(SPI_CMD_WDOG) # send command data_byte = soc.bcm2835_spi_transfer(DUMMY) # read response if data_byte != WATCH_DOG_REPLY: print 'Watch-dog reply missing. received: ', data_byte break soc.bcm2835_spi_end() print 'closing GPIO:', soc.bcm2835_close()