#include "project.h" #define NCS (GPIO7) #define NCS_PORT GPIOG #define SCK (GPIO3) #define SCK_PORT GPIOB #define MOSI (GPIO5) #define MOSI_PORT GPIOB #define N_DISPLAYS 3 #define N_DIGITS 8 static uint8_t max7219_fb[N_DISPLAYS][N_DIGITS]; static void set (int sck, int ncs, int mosi) { if (sck) SET (SCK); else CLEAR (SCK); if (ncs) SET (NCS); else CLEAR (NCS); if (mosi) SET (MOSI); else CLEAR (MOSI); // delay_us(1); //delay_us(10); } static void spip_send_8 (uint8_t wot) { int i; for (i = 0; i < 8; ++i) { set (0, 0, wot & 0x80); set (1, 0, wot & 0x80); set (0, 0, wot & 0x80); wot <<= 1; } } static int mutex = 0; static int lock (void) { if (__sync_add_and_fetch (&mutex, 1) != 1) { __sync_sub_and_fetch (&mutex, 1); return -1; } return 0; } static void unlock (void) { __sync_sub_and_fetch (&mutex, 1); } static void write_reg (uint8_t reg, uint8_t data) { unsigned i; while (lock()); set (0, 1, 0); set (0, 0, 0); for (i = 0; i < N_DISPLAYS; ++i) { spip_send_8 (reg); spip_send_8 (data); } set (0, 0, 0); set (0, 1, 0); unlock(); } void max7219_cls (void) { unsigned i; for (i = 0; i < N_DISPLAYS; ++i) memset (&max7219_fb[i], 0, N_DIGITS); } void max7219_refresh (void) { static uint8_t last_brightness = 255; unsigned i, j; if (last_brightness != pot_brightness) { last_brightness = pot_brightness; write_reg (0xa, last_brightness); } while (lock()); for (j = 0; j < N_DIGITS; ++j) { set (0, 1, 0); set (0, 0, 0); i = N_DISPLAYS - 1; do { spip_send_8 (N_DIGITS - j); spip_send_8 (max7219_fb[i][j]); } while (i--); set (0, 0, 0); set (0, 1, 0); } unlock(); } #define SDP 0x80 #define SA 0x40 #define SB 0x20 #define SC 0x10 #define SD 0x08 #define SE 0x04 #define SF 0x02 #define SG 0x01 /* * AAAA * F B * F B * F B * GGGG * E C * E C * E C * DDDD DP * */ uint8_t max7219_char_to_bits (unsigned v) { switch (v) { case 0: case '0': return SA | SF | SB | SE | SC | SD; case 1: case '1': return SB | SC; case 2: case '2': case 'z': return SA | SB | SG | SE | SD; case 3: case '3': return SA | SB | SG | SC | SD; case 4: case '4': return SF | SG | SB | SC; case 5: case '5': case 's': return SA | SF | SG | SC | SD; case 6: case '6': return SA | SF | SG | SE | SC | SD; case 7: case '7': return SA | SB | SC; case 8: case '8': return SA | SF | SB | SG | SE | SC | SD; case 9: case '9': case 'g': return SA | SF | SB | SG | SC | SD; case 0xa: case 'a': return SA | SF | SB | SG | SE | SC; case 0xb: case 'b': return SF | SG | SE | SC | SD; case 0xc: case 'c': return SG | SE | SD; case 0xd: case 'd': return SB | SG | SE | SC | SD; case 0xe: case 'e': return SA | SF | SG | SE | SD; case 0xf: case 'f': return SA | SF | SG | SE; case 'h': return SF | SG | SE | SC; case 'i': return SE; case 'j': return SB | SC | SD; case 'k': case 'x': return SF | SB | SG | SE | SC; case 'l': return SF | SE | SD; case 'm': case 'n': return SG | SE | SC; case 'M': return SG | SC; case 'o': return SG | SE | SC | SD; case 'p': return SA | SF | SB | SG | SE; case 'q': return SA | SF | SB | SG | SC; case 'r': return SG | SE; case 't': return SF | SG | SE | SD; case 'u': case 'w': return SE | SC | SD; case 'W': return SD | SC; case 'y': return SF | SB | SG | SC | SD; case '-': return SG; case '.': return SDP; } return 0; } void max7219_write_dd (int u, unsigned x, unsigned y) { unsigned t; if (u < 0) return; if (u > 99) return; t = u / 10; u %= 10; max7219_fb[y][x] = max7219_char_to_bits (t); max7219_fb[y][x + 1] = max7219_char_to_bits (u); } void max7219_write_string (const char *s, unsigned x, unsigned y) { for (; (x < N_DIGITS) && *s; s++, x++) { max7219_fb[y][x] = max7219_char_to_bits (*s); if (* (s + 1) == '.') { max7219_fb[y][x] |= SDP; s++; } } } void max7219_write_dp (unsigned x, unsigned y) { max7219_fb[y][x] |= SDP; } void max7219_write_hh (int v, unsigned x, unsigned y) { if (v < 0) return; if (v > 0xff) return; max7219_fb[y][x] = max7219_char_to_bits ((v >> 4) & 0xf); max7219_fb[y][x + 1] = max7219_char_to_bits (v & 0xf); } void max7219_init (int on) { unsigned i; MAP_OUTPUT_PP (SCK); MAP_OUTPUT_PP (NCS); MAP_OUTPUT_PP (MOSI); set (0, 1, 0); if (on) { write_reg (0xc, 0x1); //Power up write_reg (0xf, 0x0); //normal mode write_reg (0x9, 0x0); //no decode write_reg (0xb, N_DIGITS - 1); //8 digits write_reg (0xa, pot_brightness); for (i = 0; i < N_DIGITS; ++i) write_reg (N_DIGITS - i, 0x0); } else { write_reg (0xc, 0x0); //Power up } }