def test_populate_with_data_rejects_input_of_incorrect_size(self): target = lfd.LedDmaFrameData(2) byte_arr1 = array.array("B", [0b0001, 0b0010, 0b0011]) gp_info1 = gpio.GpioInfo(12) self.assertRaises(Exception, target.populate_with_data, byte_arr1, gp_info1)
def test_set_pins_to_output_sets_correct_bits_of_correct_registers(self): with mu.mmap_dev_mem(gpio.GPIO_BASE_PHYS) as m: # Set all pins in GPFSEL1 register to input (000) and check that the write was successful gpfsel1_reg_offset = 0x4 mu.write_word_to_byte_array(m, gpfsel1_reg_offset, 0) gpfsel1_reg_value = struct.unpack( '<L', m[gpfsel1_reg_offset:gpfsel1_reg_offset + 4])[0] self.assertEqual(0, gpfsel1_reg_value) # Set pins 15 and 18 to output and check that the register bits are set as expected gpio_info15 = gpio.GpioInfo(15) gpio_info18 = gpio.GpioInfo(18) gpio.set_pins_to_output([gpio_info15, gpio_info18]) expected_gpfsel1_reg_value = 1 << 15 | 1 << 24 gpfsel1_reg_value = struct.unpack( '<L', m[gpfsel1_reg_offset:gpfsel1_reg_offset + 4])[0] self.assertEqual(expected_gpfsel1_reg_value, gpfsel1_reg_value)
def test_constructor_calculates_correct_fsel_pin_bit_shift(self): self.assertEqual(0, gpio.GpioInfo(0).gp_fsel_bit_shift) self.assertEqual(15, gpio.GpioInfo(5).gp_fsel_bit_shift) self.assertEqual(0, gpio.GpioInfo(10).gp_fsel_bit_shift) self.assertEqual(21, gpio.GpioInfo(17).gp_fsel_bit_shift)
def test_constructor_calculates_correct_fsel_register_byte_offset(self): self.assertEqual(0, gpio.GpioInfo(0).gp_fsel_reg_offset) self.assertEqual(0, gpio.GpioInfo(9).gp_fsel_reg_offset) self.assertEqual(4, gpio.GpioInfo(10).gp_fsel_reg_offset) self.assertEqual(4, gpio.GpioInfo(19).gp_fsel_reg_offset) self.assertEqual(8, gpio.GpioInfo(20).gp_fsel_reg_offset) self.assertEqual(8, gpio.GpioInfo(29).gp_fsel_reg_offset) self.assertEqual(12, gpio.GpioInfo(30).gp_fsel_reg_offset) self.assertEqual(12, gpio.GpioInfo(39).gp_fsel_reg_offset) self.assertEqual(16, gpio.GpioInfo(40).gp_fsel_reg_offset) self.assertEqual(16, gpio.GpioInfo(49).gp_fsel_reg_offset) self.assertEqual(20, gpio.GpioInfo(50).gp_fsel_reg_offset) self.assertEqual(20, gpio.GpioInfo(53).gp_fsel_reg_offset)
def test_constructor_calculates_correct_set_and_clr_register_index(self): self.assertEqual(0, gpio.GpioInfo(0).set_clr_register_index) self.assertEqual(0, gpio.GpioInfo(31).set_clr_register_index) self.assertEqual(1, gpio.GpioInfo(32).set_clr_register_index) self.assertEqual(1, gpio.GpioInfo(53).set_clr_register_index)
def test_constructor_calculates_correct_pin_bit_offset(self): self.assertEqual(0, gpio.GpioInfo(0).pin_flip_bit_shift) self.assertEqual(31, gpio.GpioInfo(31).pin_flip_bit_shift) self.assertEqual(0, gpio.GpioInfo(32).pin_flip_bit_shift) self.assertEqual(21, gpio.GpioInfo(53).pin_flip_bit_shift)
def test_populate_with_data_correctly_fills_clr_register_data_array(self): target = lfd.LedDmaFrameData(2) gpio_register_data = target.gpio_data byte_arr1 = array.array( "B", [0b0001, 0b0010, 0b0011, 0b0100, 0b0101, 0b0110]) byte_arr2 = array.array( "B", [0b0010, 0b0011, 0b0100, 0b0101, 0b0110, 0b0111]) byte_arr3 = array.array( "B", [0b0011, 0b0100, 0b0101, 0b0110, 0b0111, 0b1000]) gp_info1 = gpio.GpioInfo(12) gp_info2 = gpio.GpioInfo(17) gp_info3 = gpio.GpioInfo(40) target.populate_with_data(byte_arr1, gp_info1) target.populate_with_data(byte_arr2, gp_info2) target.populate_with_data(byte_arr3, gp_info3) # Check data for the first gpio clr register self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[0][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[1][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[2][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[3][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[4][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[5][0]) self.assertEqual(1 << 12 | 0 << 17, gpio_register_data[6][0]) self.assertEqual(0 << 12 | 1 << 17, gpio_register_data[7][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[8][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[9][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[10][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[11][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[12][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[13][0]) self.assertEqual(0 << 12 | 0 << 17, gpio_register_data[14][0]) self.assertEqual(1 << 12 | 0 << 17, gpio_register_data[15][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[16][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[17][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[18][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[19][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[20][0]) self.assertEqual(1 << 12 | 0 << 17, gpio_register_data[21][0]) self.assertEqual(0 << 12 | 1 << 17, gpio_register_data[22][0]) self.assertEqual(0 << 12 | 1 << 17, gpio_register_data[23][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[24][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[25][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[26][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[27][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[28][0]) self.assertEqual(0 << 12 | 0 << 17, gpio_register_data[29][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[30][0]) self.assertEqual(1 << 12 | 0 << 17, gpio_register_data[31][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[32][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[33][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[34][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[35][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[36][0]) self.assertEqual(0 << 12 | 0 << 17, gpio_register_data[37][0]) self.assertEqual(1 << 12 | 0 << 17, gpio_register_data[38][0]) self.assertEqual(0 << 12 | 1 << 17, gpio_register_data[39][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[40][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[41][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[42][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[43][0]) self.assertEqual(1 << 12 | 1 << 17, gpio_register_data[44][0]) self.assertEqual(0 << 12 | 0 << 17, gpio_register_data[45][0]) self.assertEqual(0 << 12 | 0 << 17, gpio_register_data[46][0]) self.assertEqual(1 << 12 | 0 << 17, gpio_register_data[47][0]) # Check data for the second gpio clr register self.assertEqual(1 << 8, gpio_register_data[0][1]) self.assertEqual(1 << 8, gpio_register_data[1][1]) self.assertEqual(1 << 8, gpio_register_data[2][1]) self.assertEqual(1 << 8, gpio_register_data[3][1]) self.assertEqual(1 << 8, gpio_register_data[4][1]) self.assertEqual(1 << 8, gpio_register_data[5][1]) self.assertEqual(0 << 8, gpio_register_data[6][1]) self.assertEqual(0 << 8, gpio_register_data[7][1]) self.assertEqual(1 << 8, gpio_register_data[8][1]) self.assertEqual(1 << 8, gpio_register_data[9][1]) self.assertEqual(1 << 8, gpio_register_data[10][1]) self.assertEqual(1 << 8, gpio_register_data[11][1]) self.assertEqual(1 << 8, gpio_register_data[12][1]) self.assertEqual(0 << 8, gpio_register_data[13][1]) self.assertEqual(1 << 8, gpio_register_data[14][1]) self.assertEqual(1 << 8, gpio_register_data[15][1]) self.assertEqual(1 << 8, gpio_register_data[16][1]) self.assertEqual(1 << 8, gpio_register_data[17][1]) self.assertEqual(1 << 8, gpio_register_data[18][1]) self.assertEqual(1 << 8, gpio_register_data[19][1]) self.assertEqual(1 << 8, gpio_register_data[20][1]) self.assertEqual(0 << 8, gpio_register_data[21][1]) self.assertEqual(1 << 8, gpio_register_data[22][1]) self.assertEqual(0 << 8, gpio_register_data[23][1]) self.assertEqual(1 << 8, gpio_register_data[24][1]) self.assertEqual(1 << 8, gpio_register_data[25][1]) self.assertEqual(1 << 8, gpio_register_data[26][1]) self.assertEqual(1 << 8, gpio_register_data[27][1]) self.assertEqual(1 << 8, gpio_register_data[28][1]) self.assertEqual(0 << 8, gpio_register_data[29][1]) self.assertEqual(0 << 8, gpio_register_data[30][1]) self.assertEqual(1 << 8, gpio_register_data[31][1]) self.assertEqual(1 << 8, gpio_register_data[32][1]) self.assertEqual(1 << 8, gpio_register_data[33][1]) self.assertEqual(1 << 8, gpio_register_data[34][1]) self.assertEqual(1 << 8, gpio_register_data[35][1]) self.assertEqual(1 << 8, gpio_register_data[36][1]) self.assertEqual(0 << 8, gpio_register_data[37][1]) self.assertEqual(0 << 8, gpio_register_data[38][1]) self.assertEqual(0 << 8, gpio_register_data[39][1]) self.assertEqual(1 << 8, gpio_register_data[40][1]) self.assertEqual(1 << 8, gpio_register_data[41][1]) self.assertEqual(1 << 8, gpio_register_data[42][1]) self.assertEqual(1 << 8, gpio_register_data[43][1]) self.assertEqual(0 << 8, gpio_register_data[44][1]) self.assertEqual(1 << 8, gpio_register_data[45][1]) self.assertEqual(1 << 8, gpio_register_data[46][1]) self.assertEqual(1 << 8, gpio_register_data[47][1])
import app.dma as dma import app.gpio as gpio import app.led_frame_data as fd import app.memory_utils as mu import app.pwm as pwm PWM_CLK_SRC = pwm.CLK_SRC_PLLD PWM_CLK_DIV_INT = 5 PWM_CLK_DIV_FRAC = 0 PWM_CYCLES = 75 DMA_CH = 2 GPIO_INFO_PIN18 = gpio.GpioInfo(18) GPIO_INFO_PIN15 = gpio.GpioInfo(15) DMA_WAITS = 27 << 21 DMA_FLAGS_PWM = dma.DMA_TI_NO_WIDE_BURSTS | dma.DMA_TI_SRC_IGNORE | dma.DMA_TI_PERMAP | dma.DMA_TI_DEST_DREQ MS_BASE = 0x20000000 MS_BASE_BUS = 0x7e000000 MS_MBOX_REG_OFFSET = 0xA0 class NeopixelDriver: def __init__(self, num_leds, gpio_pins: [gpio.GpioInfo]): self.dma_data = fd.LedDmaFrameData(num_leds) # SET and CLR registers are spaced as follows, spanning 5 registers: # SET SET -- CLR CLR # 1C 20 24 28 2C # MS_MBOX_0 - MS_MBOX_7 are peripheral registers usable for storing this data. # We will only need MS_MBOX_0 - MS_MBOX_4.