main.c 3.44 KB
Newer Older
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
1 2
#include "ch.h"
#include "hal.h"
3
#include "board.h"
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
4 5
#include "rt_test_root.h"
#include "oslib_test_root.h"
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <stdio.h>

#include "usbcfg.h"
#include "ssd1306.h"

#define usb_lld_connect_bus(usbp)
#define usb_lld_disconnect_bus(usbp)

static const I2CConfig i2ccfg = {
  STM32_TIMINGR_PRESC(8U)  | STM32_TIMINGR_SCLDEL(3U) | STM32_TIMINGR_SDADEL(3U) |
  STM32_TIMINGR_SCLH(3U)   | STM32_TIMINGR_SCLL(9U),
  0,
  0
};

static const SSD1306Config ssd1306cfg = {
  &I2CD1,
  &i2ccfg,
  SSD1306_SAD_0X78,
};

static SSD1306Driver SSD1306D1;

static THD_WORKING_AREA(waOledDisplay, 1024);
static THD_FUNCTION(OledDisplay, arg) {
  (void)arg;
  chRegSetThreadName("OledDisplay");

  ssd1306ObjectInit(&SSD1306D1);
  ssd1306Start(&SSD1306D1, &ssd1306cfg);
  ssd1306ToggleInvert(&SSD1306D1);
  ssd1306FillScreen(&SSD1306D1, 0x00);

  int i = 0;

  while (TRUE) {
    ssd1306FillScreen(&SSD1306D1, 0x01);
    char buf[32] = {0};
    ssd1306GotoXy(&SSD1306D1, 0, 1);
    ssd1306Puts(&SSD1306D1, "TESTE", &ssd1306_font_11x18, SSD1306_COLOR_WHITE);
    ssd1306GotoXy(&SSD1306D1, 0, 32);
    ssd1306Puts(&SSD1306D1, "0123456789", &ssd1306_font_11x18, SSD1306_COLOR_WHITE);
    ssd1306UpdateScreen(&SSD1306D1);
    i++;
    chThdSleepMilliseconds(1000);
  }

  ssd1306Stop(&SSD1306D1);
}

Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
56 57 58 59 60 61 62 63 64 65 66

/*
 * Green LED blinker thread, times are in milliseconds.
 */
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {

  (void)arg;
  chRegSetThreadName("blinker");
  while (true) {
    palClearLine(LINE_LED0);
67
    chThdSleepMilliseconds(100);
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
68
    palSetLine(LINE_LED0);
69 70 71 72 73 74 75 76 77
    chThdSleepMilliseconds(100);
    palClearLine(LINE_LED1);
    chThdSleepMilliseconds(100);
    palSetLine(LINE_LED1);
    chThdSleepMilliseconds(100);
    palClearLine(LINE_LED2);
    chThdSleepMilliseconds(100);
    palSetLine(LINE_LED2);
    chThdSleepMilliseconds(100);
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  }
}

/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

96 97 98 99 100 101 102 103 104 105 106 107
  palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(14));
  palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(14));

  sduObjectInit(&SDU1);
  sduStart(&SDU1, &serusbcfg1);
  sduObjectInit(&SDU2);
  sduStart(&SDU2, &serusbcfg2);

  usbStart(serusbcfg1.usbp, &usbcfg);
  usbConnectBus(serusbcfg1.usbp);


Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
108 109 110 111 112 113 114 115
  /*
   * Activates the serial driver 3 using the driver default configuration.
   */
  sdStart(&SD3, NULL);

  /*
   * Creates the blinker thread.
   */
116 117 118 119 120
  palSetLineMode(LINE_LED0, PAL_MODE_OUTPUT_PUSHPULL);
  palSetLineMode(LINE_LED1, PAL_MODE_OUTPUT_PUSHPULL);
  palSetLineMode(LINE_LED2, PAL_MODE_OUTPUT_PUSHPULL);
  palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4));
  palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(4));
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
121 122 123 124
  palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(7));
  palSetPadMode(GPIOB, 11, PAL_MODE_ALTERNATE(7));

  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
125 126
  chThdCreateStatic(waThread1, sizeof(waOledDisplay), NORMALPRIO, OledDisplay, NULL);
  
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
127 128 129 130 131 132 133 134 135

  /*
   * Normal main() thread activity, in this demo it does nothing except
   * sleeping in a loop and check the button state.
   */
  while (true) {
    chThdSleepMilliseconds(500);
  }
}