Skip to content

Instantly share code, notes, and snippets.

View youngkin's full-sized avatar

Richard Youngkin youngkin

  • Littleton, CO United States
View GitHub Profile
func writeMax7219Byte(b byte) {
rpio.SpiTransmit(b)
}
func writeMax7219(addr byte, value byte) {
// The csPin (chip select) is set to LOW to direct the MAX7219 to accept data from the MOSI line.
// Setting it to HIGH at the end of the function directs the MAX7219 to ignore data on the MOSI line.
csPin.Low()
writeMax7219Byte(addr)
writeMax7219Byte(value)
csPin.High()
}
func initMax7219() {
writeMax7219(0x09, 0x00) // Decode mode register
writeMax7219(0x0a, 0x03) // medium brightness
// writeMax7219(0x0a,0x0f);// max brightness
writeMax7219(0x0b, 0x07) // Scan limit register
writeMax7219(0x0c, 0x01) // Shutdown register
writeMax7219(0x0f, 0x00) // Display test register, normal mode
// writeMax7219(0x0f,0x01);// Display test register, test mode (light all leds)
}
// Initialize SPI on the SPI0 associated GPIO pins
if err := rpio.SpiBegin(rpio.Spi0); err != nil {
fmt.Println(err)
os.Exit(1)
}
csPin.Output()
rpio.SpiChipSelect(uint8(csPin)) // Select CE0 (csPin) as slave
initMax7219()
func main() {
// stop channel is used to synchronize exiting the
// program so that the board is reset to the state
// it was in prior to the program starting.
stop := make(chan interface{})
// sigs is the channel used by Go's signals capability
// to notify the program that a signal has been raised.
sigs := make(chan os.Signal)
// signal.Notify() registers the program's interest
const csPin = rpio.Pin(uint8(8)) //csPin represents the chip select pin and specifies it is on GPIO pin 8
const NUM_CHARS = 37 // Number of characters that can be displayed
const MATRIX_ROW = 8 // The number of rows of LEDs on the MAX7219
var disp1 = [][]byte{
{0x3C, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3C}, //0
{0x08, 0x18, 0x28, 0x08, 0x08, 0x08, 0x08, 0x08}, //1
...
}
void bcm_peri_set_bits(volatile uint32_t* paddr, uint32_t value, uint32_t mask)
{
uint32_t v = bcm_peri_read(paddr);
v = (v & ~mask) | (value & mask);
bcm_peri_write(paddr, v);
}
void bcm_spi_setBitOrder(uint8_t order)
{
bcm_spi_bit_order = order;
}
uint8_t bcm_spi_transfer(uint8_t value)
{
volatile uint32_t* paddr = bcm_spi0 + BCM_SPI0_CS/4;
volatile uint32_t* fifo = bcm_spi0 + BCM_SPI0_FIFO/4;
uint32_t ret;
bcm_peri_set_bits(paddr, BCM_SPI0_CS_CLEAR, BCM_SPI0_CS_CLEAR);
/* Set TA = 1, data transfer is active */
bcm_peri_set_bits(paddr, BCM_SPI0_CS_TA, BCM_SPI0_CS_TA);
void bcm_spi_setClockDivider(uint16_t divider)
{
volatile uint32_t* paddr = bcm_spi0 + BCM_SPI0_CLK/4;
bcm_peri_write(paddr, divider);
}