run different code depending on the MHARTID reg value

This commit is contained in:
Stanislaw Kaushanski 2020-09-04 12:56:12 +02:00
parent e15e299968
commit ba27721c4d
3 changed files with 1383 additions and 1511 deletions

Binary file not shown.

View File

@ -5,7 +5,7 @@
#include "platform.h"
#include "encoding.h"
#define IOF_SPI1_MASK (0x38 | 0x4)
#define IOF_ENABLE_TERMINAL (0x30000)
int factorial(int i){
volatile int result = 1;
@ -16,62 +16,35 @@ int factorial(int i){
}
unsigned read_adc(unsigned index){
unsigned char txdata[3];
unsigned result=0;
volatile int x;
txdata[0]=0x1;
txdata[1]=(0x8 | (index&0x7))<<4;
txdata[2]=0x0;
GPIO_REG(GPIO_IOF_SEL) &= ~IOF_SPI1_MASK;
GPIO_REG(GPIO_IOF_EN) |= IOF_SPI1_MASK;
SPI1_REG(SPI_REG_FMT) =
SPI_FMT_PROTO(SPI_PROTO_S) |
SPI_FMT_ENDIAN(SPI_ENDIAN_MSB) |
SPI_FMT_DIR(SPI_DIR_RX) |
SPI_FMT_LEN(8);
SPI1_REG(SPI_REG_CSID) = 0;
SPI1_REG(SPI_REG_CSDEF) = 0xFFFF;
SPI1_REG(SPI_REG_SCKDIV) = 7;
SPI1_REG(SPI_REG_SCKMODE) = SPI_SCK_PHA | SPI_SCK_POL; //shifted on the leading edge, sampled on trailing, Inactive state of SCK is logical 1
SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD;
while (SPI1_REG(SPI_REG_TXFIFO) & SPI_TXFIFO_FULL) ;
SPI1_REG(SPI_REG_TXFIFO) = txdata[0];
while ((x = SPI1_REG(SPI_REG_RXFIFO)) & SPI_RXFIFO_EMPTY);
while (SPI1_REG(SPI_REG_TXFIFO) & SPI_TXFIFO_FULL) ;
SPI1_REG(SPI_REG_TXFIFO) = txdata[1];
while ((x = SPI1_REG(SPI_REG_RXFIFO)) & SPI_RXFIFO_EMPTY);
result = (x & 0xFF)<<8;
while (SPI1_REG(SPI_REG_TXFIFO) & SPI_TXFIFO_FULL) ;
SPI1_REG(SPI_REG_TXFIFO) = txdata[2];
while ((x = SPI1_REG(SPI_REG_RXFIFO)) & SPI_RXFIFO_EMPTY);
result += (x & 0xFF);
SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO;
return result&0x03ff;
}
//int read_csr(int csr_num) __attribute__((always_inline)) {
// int result;
// asm("csrr %0, %1" : "=r"(result) : "I"(csr_num));
// return result;
//}
int main()
{
GPIO_REG(GPIO_IOF_EN) |= 0x30000;
*(int *)0x90000000 = 0x5AA5;
int result = factorial (10);
printf("Factorial of 10 is %d\n", result);
for(unsigned i=0; i<8; ++i)
printf("ADC%u value read is %u\n", i, read_adc(i));
printf("Read a value from another thread %x\n", *(int *)0x80000000);
GPIO_REG(GPIO_IOF_EN) |= IOF_ENABLE_TERMINAL; // enable GPIO connection to the terminal
int hartid = read_csr(0xf14); // CSR_MHARTID
int target_mem_base = 0x90000000;
int local_mem_base = 0x80000000;
if (hartid == 0) {
int val_a = 2;
int val_b = 3;
*(int *)target_mem_base = val_a;
*(int *)(target_mem_base+4) = val_b;
printf("HW thread ID %d: write value A=%x and value B=%x to thread 1\n", hartid, val_a, val_b);
}
int result = factorial (10);
printf("HW thread ID %d: spend some time calculating factorial of 10 = %x\n", hartid, result);
if (hartid == 1) {
int val_a = *(int *)local_mem_base;
int val_b = *(int *)(local_mem_base+4);
int sum = val_a + val_b;
printf("HW thread ID %d: sum of A+B=%x \n", hartid, sum);
}
printf("End of execution");
return 0;
}

File diff suppressed because it is too large Load Diff