| 1 |
/* |
| 2 |
****************************************************************************************** |
| 3 |
* @file main.c |
| 4 |
* @author GowinSemiconductor |
| 5 |
* @device Gowin_EMPU(GW1NS-4C) |
| 6 |
* @brief Main program body. |
| 7 |
****************************************************************************************** |
| 8 |
*/ |
| 9 |
|
| 10 |
/* Includes ------------------------------------------------------------------*/ |
| 11 |
#include "gw1ns4c.h" |
| 12 |
#include <stdint.h> |
| 13 |
#include <stdio.h> |
| 14 |
|
| 15 |
// First APB master address |
| 16 |
uint32_t const apb2_periph_base = 0x40002400; |
| 17 |
|
| 18 |
struct APB2_leds_peripheral { |
| 19 |
uint32_t volatile led : 1; // not memory - read/write the bus ;) |
| 20 |
}; |
| 21 |
|
| 22 |
struct APB2_leds_peripheral *leds = (struct APB2_leds_peripheral *)apb2_periph_base; |
| 23 |
|
| 24 |
void initialize_timer(); |
| 25 |
void initialize_UART(); |
| 26 |
void delay_millis(uint32_t ms); |
| 27 |
|
| 28 |
void print_led_state(void) { |
| 29 |
if (leds->led) { |
| 30 |
UART_SendString(UART0, "Read led: ON\r\n"); |
| 31 |
} else { |
| 32 |
UART_SendString(UART0, "Read led: OFF\r\n"); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
int main(void) { |
| 37 |
SystemInit(); |
| 38 |
initialize_timer(); |
| 39 |
initialize_UART(); |
| 40 |
|
| 41 |
UART_SendString(UART0, "Apicula Cortex-M3 APB blinky\r\n"); |
| 42 |
while (1) { |
| 43 |
leds->led = 0; |
| 44 |
print_led_state(); |
| 45 |
delay_millis(100); |
| 46 |
leds->led = 1; |
| 47 |
print_led_state(); |
| 48 |
delay_millis(1000); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// Initializes UART0 |
| 53 |
void initialize_UART() { |
| 54 |
UART_InitTypeDef uartInitStruct; |
| 55 |
// Enable transmission |
| 56 |
uartInitStruct.UART_Mode.UARTMode_Tx = ENABLE; |
| 57 |
// Disable reception |
| 58 |
uartInitStruct.UART_Mode.UARTMode_Rx = DISABLE; |
| 59 |
// 9600 baud rate typical of Arduinos |
| 60 |
uartInitStruct.UART_BaudRate = 9600; |
| 61 |
// Initialize UART0 using the struct configs |
| 62 |
UART_Init(UART0, &uartInitStruct); |
| 63 |
} |
| 64 |
|
| 65 |
void initialize_timer() { |
| 66 |
TIMER_InitTypeDef timerInitStruct; |
| 67 |
|
| 68 |
timerInitStruct.Reload = 0; |
| 69 |
|
| 70 |
// Disable interrupt requests from timer for now |
| 71 |
timerInitStruct.TIMER_Int = DISABLE; |
| 72 |
|
| 73 |
// Disable timer enabling/clocking from external pins (GPIO) |
| 74 |
timerInitStruct.TIMER_Exti = TIMER_DISABLE; |
| 75 |
|
| 76 |
TIMER_Init(TIMER0, &timerInitStruct); |
| 77 |
TIMER_StopTimer(TIMER0); |
| 78 |
} |
| 79 |
|
| 80 |
#define CYCLES_PER_MILLISEC (SystemCoreClock / 1000) |
| 81 |
void delay_millis(uint32_t ms) { |
| 82 |
TIMER_StopTimer(TIMER0); |
| 83 |
// Reset timer just in case it was modified elsewhere |
| 84 |
TIMER_SetValue(TIMER0, 0); |
| 85 |
TIMER_EnableIRQ(TIMER0); |
| 86 |
|
| 87 |
uint32_t reloadVal = CYCLES_PER_MILLISEC * ms; |
| 88 |
// Timer interrupt will trigger when it reaches the reload value |
| 89 |
TIMER_SetReload(TIMER0, reloadVal); |
| 90 |
|
| 91 |
TIMER_StartTimer(TIMER0); |
| 92 |
// Block execution until timer wastes the calculated amount of cycles |
| 93 |
while (TIMER_GetIRQStatus(TIMER0) != SET) |
| 94 |
; |
| 95 |
|
| 96 |
TIMER_StopTimer(TIMER0); |
| 97 |
TIMER_ClearIRQ(TIMER0); |
| 98 |
TIMER_SetValue(TIMER0, 0); |
| 99 |
} |
| 100 |
|