-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,42 @@ | ||
#define SET_BITS(REG, BIT) ((REG) |= (BIT)) | ||
#define CLEAR_BITS(REG, BIT) ((REG) &= ~(BIT)) | ||
// some handy macros to do bit operations | ||
#define SET_BITS(REG, BIT) ((REG) |= (BIT)) | ||
#define CLEAR_BITS(REG, BIT) ((REG) &= !(BIT)) | ||
|
||
#define GPIOA_BASE 0x10010000U | ||
// peripheral MMIO addresses | ||
#define GPIOA_OUTPUT_VAL 0x1001000CUL | ||
#define GPIOA_OUTPUT_EN 0x10010008UL | ||
#define CLINT_MTIME 0x0200BFF8UL | ||
|
||
#define GPIO_PIN_0 0b0001U | ||
#define GPIO_PIN_1 0b0001U | ||
#define GPIO_PIN_2 0b0001U | ||
#define GPIO_PIN_3 0b0001U | ||
// the pin we are using | ||
const unsigned int GPIO_PIN = 0x01; | ||
|
||
// a global counter | ||
volatile unsigned int counter; | ||
|
||
|
||
|
||
static inline void simpleDelay() { | ||
for (int i=0; i<1000000; i+=1) { | ||
__asm__("nop"); | ||
} | ||
// A simple delay function. | ||
void delay(unsigned int ticks) { | ||
unsigned int mtime_start; | ||
while ((*(volatile unsigned int *)CLINT_MTIME) - mtime_start < ticks) {} | ||
} | ||
|
||
int main() { | ||
*(unsigned int volatile *)(GPIOA_BASE + 0x00U) = GPIO_PIN_0; | ||
void main() { | ||
// enable GPIOA as output | ||
SET_BITS(*(volatile unsigned int *)GPIOA_OUTPUT_EN, GPIO_PIN); | ||
|
||
while (1) { | ||
SET_BITS(*(unsigned int volatile *)(GPIOA_BASE + 0x00U), GPIO_PIN_0); | ||
simpleDelay(); | ||
// if counter is even, turn on the LED, otherwise turn it off | ||
if (counter % 2 == 0) { | ||
SET_BITS(*(volatile unsigned int *)GPIOA_OUTPUT_VAL, GPIO_PIN); | ||
} else { | ||
CLEAR_BITS(*(volatile unsigned int *)GPIOA_OUTPUT_VAL, GPIO_PIN); | ||
} | ||
|
||
// delay for 1 second | ||
delay(1000); | ||
|
||
CLEAR_BITS(*(unsigned int volatile *)(GPIOA_BASE + 0x00U), GPIO_PIN_0); | ||
simpleDelay(); | ||
// increment the counter | ||
counter += 1; | ||
} | ||
} | ||
|
||
// we won't reach here if everything is working | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters